Skip to content

Commit 882212c

Browse files
committed
When XDG compliant is force due to both XDG_DATA_HOME and
XDG_STATE_HOME directories already exist, update the local Desktop file, so that the Icon point to its new location
1 parent 4c299e2 commit 882212c

File tree

4 files changed

+80
-32
lines changed

4 files changed

+80
-32
lines changed

devel/fix_pyradio_desktop_file

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ function get_data_dir(){
5353
XDG="$(sed 's/[A-Z]/\l&/' "$CFG_FILE" | grep 'xdg_compliant = true')"
5454
fi
5555
if [ -z "$XDG" ]
56+
then
57+
XDG="$(sed 's/[A-Z]/\l&/' "${XDG_CONFIG_HOME-"$HOME"/.config}"/pyradio/config | grep 'xdg_compliant = true')"
58+
fi
59+
if [ -z "$XDG" ]
5660
then
5761
DATA_DIR=~/.config/pyradio/data
5862
else

pyradio/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,8 @@ class PyRadioConfig(PyRadioStations):
13781378

13791379
_linux_resource_opener = None
13801380

1381+
need_to_fix_desktop_file_icon = False
1382+
13811383
def __init__(self, user_config_dir=None, headless=False):
13821384
# keep old recording / new recording dir
13831385
self.rec_dirs = ()
@@ -2084,6 +2086,7 @@ def read_config(self, distro_config=False):
20842086
self.xdg.ensure_paths_exist()
20852087

20862088
def _read_config(self, distro_config=False):
2089+
xdg_compliant_read_from_file = False
20872090
if distro_config:
20882091
file_to_read = path.join(path.dirname(__file__), 'config')
20892092
else:
@@ -2271,6 +2274,7 @@ def _read_config(self, distro_config=False):
22712274
not platform.startswith('win') and \
22722275
sp[1].lower() == 'true':
22732276
self.xdg_compliant = True
2277+
xdg_compliant_read_from_file = True
22742278
elif sp[0] == 'resource_opener' and \
22752279
not (platform.startswith('win') or \
22762280
platform.startswith('dar')):
@@ -2356,6 +2360,16 @@ def _read_config(self, distro_config=False):
23562360
if path.exists(d_dir) and path.exists(s_dir):
23572361
print('[magenta]XDG Dirs[/magenta] found; enabling [magenta]XDG Base compliant[/magenta] operation')
23582362
self.xdg_compliant = True
2363+
self.need_to_fix_desktop_file_icon = True
2364+
2365+
if self.need_to_fix_desktop_file_icon and \
2366+
not distro_config and \
2367+
not xdg_compliant_read_from_file:
2368+
with open(file_to_read, 'a', encoding='utf-8') as f:
2369+
f.write('\n')
2370+
f.write('# Both XDG_DATA_HOME and XDG_STATE_HOME exist\n')
2371+
f.write('# This will only be read by fix_pyradio_desktop_file\n')
2372+
f.write('xdg_compliant = True')
23592373

23602374
def _make_sure_dirs_exist(self):
23612375
if self.opts['recording_dir'][1] == '':

pyradio/main.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -375,41 +375,46 @@ def shell():
375375
print('Error in config path: "[red]{}[/red]"\n This directory cannot be used by [magenta]PyRadio[/magenta]!'.format(args.config_dir))
376376
sys.exit(1)
377377

378-
if not system().lower().startswith('darwin') and \
379-
not system().lower().startswith('win'):
380-
# if args.generate_systemd_service_files:
381-
# create_systemd_service_files()
382-
# sys.exit()
383-
# elif args.terminal:
384-
if args.terminal:
385-
try:
386-
from urllib.request import urlretrieve
387-
except:
388-
from urllib import urlretrieve
389-
try:
390-
r = urlretrieve('https://raw.githubusercontent.com/coderholic/pyradio/master/devel/fix_pyradio_desktop_file')
391-
except:
392-
print('Cannot contact github...')
393-
sys.exit(1)
394-
if int(r[1]['content-length']) < 1000:
395-
print('Cannot contact github...')
396-
sys.exit(1)
397-
script = r[0]
398-
# script = '/home/spiros/projects/my-gits/pyradio/devel/fix_pyradio_desktop_file'
399-
chmod(script , 0o766)
400-
if args.terminal_param:
401-
command = 'bash -c "' + script + ' -t ' + args.terminal + " -p '-" + args.terminal_param + "'" + '"'
402-
command = 'bash -c "' + script + ' -t ' + args.terminal + " -p '" + args.terminal_param.replace('\\', '') + "'" + '"'
403-
# print(command)
404-
subprocess.call(command, shell=True)
405-
else:
406-
subprocess.call('bash -c "' + script + ' -t ' + args.terminal + '"', shell=True)
407-
remove(r[0])
408-
sys.exit()
409-
410378
with pyradio_config_file(user_config_dir, args.headless) as pyradio_config:
411379
read_config(pyradio_config)
412380

381+
if not system().lower().startswith('darwin') and \
382+
not system().lower().startswith('win'):
383+
# if args.generate_systemd_service_files:
384+
# create_systemd_service_files()
385+
# sys.exit()
386+
# elif args.terminal:
387+
388+
if args.terminal:
389+
import subprocess
390+
r = None
391+
script = None
392+
script = '/home/spiros/projects/my-gits/pyradio/devel/fix_pyradio_desktop_file'
393+
if script is None:
394+
try:
395+
from urllib.request import urlretrieve
396+
except:
397+
from urllib import urlretrieve
398+
try:
399+
r = urlretrieve('https://raw.githubusercontent.com/coderholic/pyradio/master/devel/fix_pyradio_desktop_file')
400+
except:
401+
print('Cannot contact github...')
402+
sys.exit(1)
403+
if int(r[1]['content-length']) < 1000:
404+
print('Cannot contact github...')
405+
sys.exit(1)
406+
script = r[0]
407+
chmod(script , 0o766)
408+
if args.terminal_param:
409+
command = 'bash -c "' + script + ' -t ' + args.terminal + " -p '-" + args.terminal_param + "'" + '"'
410+
command = 'bash -c "' + script + ' -t ' + args.terminal + " -p '" + args.terminal_param.replace('\\', '') + "'" + '"'
411+
subprocess.call(command, shell=True)
412+
else:
413+
subprocess.call('bash -c "' + script + ' -t ' + args.terminal + '"', shell=True)
414+
if r is not None:
415+
remove(r[0])
416+
sys.exit()
417+
413418
if args.write_theme:
414419
if args.write_theme[0]:
415420
from .themes import PyRadioTheme

pyradio/xdg.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class XdgMigrate(object):
4545

4646
def __init__(self, config=None):
4747
self.home_dir = path.expanduser('~')
48+
self._desktop_file = path.join(self.home_dir, '.local', 'share', 'applications', 'pyradio.desktop')
4849
self.other_dir = path.join(self.home_dir, 'pyradio-not-migrated')
4950
if config is None:
5051
# test values
@@ -56,12 +57,16 @@ def __init__(self, config=None):
5657
else:
5758
self.old_registers_dir = path.join(self.home_dir, '.config', 'pyradio', '.registers')
5859
self.new_registers_dir = path.join(self.state_dir, 'registers')
60+
self._need_to_fix_desktop_file = config.need_to_fix_desktop_file_icon
61+
self._icon_location = path.join(self.home_dir, '.config', 'data', 'pyradio.png')
5962
else:
6063
self.data_dir = config.data_dir
6164
self.state_dir = config.state_dir
6265
self.path_to_scan = config.stations_dir
6366
self.old_registers_dir = config.xdg._old_dirs[config.xdg.REGISTERS]
6467
self.new_registers_dir = config.xdg._new_dirs[config.xdg.REGISTERS]
68+
self._need_to_fix_desktop_file = True
69+
self._icon_location = path.join(config.data_dir, 'pyradio.png')
6570
self._get_files()
6671

6772
def _print_file(self, an_item, max_length):
@@ -139,6 +144,10 @@ def rename_files(self, to_console=True):
139144
'> Nothing to copy to "state" dir',
140145
'> Nothing to copy to "pytadio-not-migrated" dir',
141146
)
147+
148+
''' update Deskto file '''
149+
self._update_desktop_file()
150+
142151
go_on = False
143152
move_registers = False
144153
for n in self.files_to_data, self.files_to_state, self.files_to_other:
@@ -215,6 +224,22 @@ def rename_files(self, to_console=True):
215224
if to_console and self._verbose:
216225
input('Press ENTER to continue... ')
217226

227+
def _update_desktop_file(self):
228+
if path.exists(self._desktop_file) and \
229+
self._need_to_fix_desktop_file:
230+
try:
231+
with open(self._desktop_file, 'r', encoding='utf-8') as d:
232+
lines = d.readlines()
233+
except:
234+
return
235+
for i, l in enumerate(lines):
236+
if l.startswith('Icon='):
237+
sp = l.split('=')
238+
if sp[1].strip() != self._icon_location:
239+
lines[i] = 'Icon=' + self._icon_location + '\n'
240+
with open(self._desktop_file, 'w', encoding='utf-8') as d:
241+
d.writelines(lines)
242+
218243
def _print_error_wit_ask_enter(self):
219244
print('[red]Error:[/red] moving files to [green]XDG[/green] directories failed...\nCleaning up...')
220245
self._remove_new_files_on_failure()

0 commit comments

Comments
 (0)