Linux installer: Manually presereve the defaults.list mimetype association file to workaround buggy xdg-desktop-menu implementations in some distros. Fixes #926559 (File associations)

This commit is contained in:
Kovid Goyal 2012-03-25 21:12:20 +05:30
parent 91643b20a3
commit aba3f4686f

View File

@ -40,6 +40,46 @@ entry_points = {
], ],
} }
class PreserveMIMEDefaults(object):
def __init__(self):
self.initial_values = {}
def __enter__(self):
def_data_dirs = '/usr/local/share:/usr/share'
paths = os.environ.get('XDG_DATA_DIRS', def_data_dirs)
paths = paths.split(':')
paths.append(os.environ.get('XDG_DATA_HOME', os.path.expanduser(
'~/.local/share')))
paths = list(filter(os.path.isdir, paths))
if not paths:
# Env var had garbage in it, ignore it
paths = def_data_dirs.split(':')
paths = list(filter(os.path.isdir, paths))
self.paths = {os.path.join(x, 'applications/defaults.list') for x in
paths}
self.initial_values = {}
for x in self.paths:
try:
with open(x, 'rb') as f:
self.initial_values[x] = f.read()
except:
self.initial_values[x] = None
def __exit__(self, *args):
for path, val in self.initial_values.iteritems():
if val is None:
try:
os.remove(path)
except:
pass
elif os.path.exists(path):
with open(path, 'r+b') as f:
if f.read() != val:
f.seek(0)
f.truncate()
f.write(val)
# Uninstall script {{{ # Uninstall script {{{
UNINSTALL = '''\ UNINSTALL = '''\
#!{python} #!{python}
@ -333,57 +373,55 @@ class PostInstall:
def setup_desktop_integration(self): # {{{ def setup_desktop_integration(self): # {{{
try: try:
self.info('Setting up desktop integration...') self.info('Setting up desktop integration...')
with TemporaryDirectory() as tdir, CurrentDir(tdir), \
PreserveMIMEDefaults():
render_img('mimetypes/lrf.png', 'calibre-lrf.png')
check_call('xdg-icon-resource install --noupdate --context mimetypes --size 128 calibre-lrf.png application-lrf', shell=True)
self.icon_resources.append(('mimetypes', 'application-lrf', '128'))
check_call('xdg-icon-resource install --noupdate --context mimetypes --size 128 calibre-lrf.png text-lrs', shell=True)
self.icon_resources.append(('mimetypes', 'application-lrs',
'128'))
render_img('lt.png', 'calibre-gui.png')
check_call('xdg-icon-resource install --noupdate --size 128 calibre-gui.png calibre-gui', shell=True)
self.icon_resources.append(('apps', 'calibre-gui', '128'))
render_img('viewer.png', 'calibre-viewer.png')
check_call('xdg-icon-resource install --size 128 calibre-viewer.png calibre-viewer', shell=True)
self.icon_resources.append(('apps', 'calibre-viewer', '128'))
with TemporaryDirectory() as tdir: mimetypes = set([])
with CurrentDir(tdir): for x in all_input_formats():
render_img('mimetypes/lrf.png', 'calibre-lrf.png') mt = guess_type('dummy.'+x)[0]
check_call('xdg-icon-resource install --noupdate --context mimetypes --size 128 calibre-lrf.png application-lrf', shell=True) if mt and 'chemical' not in mt and 'ctc-posml' not in mt:
self.icon_resources.append(('mimetypes', 'application-lrf', '128')) mimetypes.add(mt)
check_call('xdg-icon-resource install --noupdate --context mimetypes --size 128 calibre-lrf.png text-lrs', shell=True)
self.icon_resources.append(('mimetypes', 'application-lrs',
'128'))
render_img('lt.png', 'calibre-gui.png')
check_call('xdg-icon-resource install --noupdate --size 128 calibre-gui.png calibre-gui', shell=True)
self.icon_resources.append(('apps', 'calibre-gui', '128'))
render_img('viewer.png', 'calibre-viewer.png')
check_call('xdg-icon-resource install --size 128 calibre-viewer.png calibre-viewer', shell=True)
self.icon_resources.append(('apps', 'calibre-viewer', '128'))
mimetypes = set([]) def write_mimetypes(f):
for x in all_input_formats(): f.write('MimeType=%s;\n'%';'.join(mimetypes))
mt = guess_type('dummy.'+x)[0]
if mt and 'chemical' not in mt and 'ctc-posml' not in mt:
mimetypes.add(mt)
def write_mimetypes(f): f = open('calibre-lrfviewer.desktop', 'wb')
f.write('MimeType=%s;\n'%';'.join(mimetypes)) f.write(VIEWER)
f.close()
f = open('calibre-lrfviewer.desktop', 'wb') f = open('calibre-ebook-viewer.desktop', 'wb')
f.write(VIEWER) f.write(EVIEWER)
f.close() write_mimetypes(f)
f = open('calibre-ebook-viewer.desktop', 'wb') f.close()
f.write(EVIEWER) f = open('calibre-gui.desktop', 'wb')
write_mimetypes(f) f.write(GUI)
f.close() write_mimetypes(f)
f = open('calibre-gui.desktop', 'wb') f.close()
f.write(GUI) des = ('calibre-gui.desktop', 'calibre-lrfviewer.desktop',
write_mimetypes(f) 'calibre-ebook-viewer.desktop')
f.close() for x in des:
des = ('calibre-gui.desktop', 'calibre-lrfviewer.desktop', cmd = ['xdg-desktop-menu', 'install', '--noupdate', './'+x]
'calibre-ebook-viewer.desktop') check_call(' '.join(cmd), shell=True)
for x in des: self.menu_resources.append(x)
cmd = ['xdg-desktop-menu', 'install', '--noupdate', './'+x] check_call(['xdg-desktop-menu', 'forceupdate'])
check_call(' '.join(cmd), shell=True) f = open('calibre-mimetypes', 'wb')
self.menu_resources.append(x) f.write(MIME)
check_call(['xdg-desktop-menu', 'forceupdate']) f.close()
f = open('calibre-mimetypes', 'wb') self.mime_resources.append('calibre-mimetypes')
f.write(MIME) check_call('xdg-mime install ./calibre-mimetypes', shell=True)
f.close()
self.mime_resources.append('calibre-mimetypes')
check_call('xdg-mime install ./calibre-mimetypes', shell=True)
except Exception: except Exception:
if self.opts.fatal_errors: if self.opts.fatal_errors:
raise raise