[Common] Fix show_file unhandled dbus error

If dbus org.freedesktop.FileManager1 service is missing then show_file
raised an unhandled exception. The service is not available on certain
desktop environments e.g. i3wm.

The solution is to fallback to xdg-open.

Fixes: #3272
This commit is contained in:
int3l 2019-06-19 19:01:42 +03:00 committed by Calum Lind
parent 24b094a04a
commit 833b5a1f30

View File

@ -81,6 +81,9 @@ TORRENT_STATE = [
# The output formatting for json.dump # The output formatting for json.dump
JSON_FORMAT = {'indent': 4, 'sort_keys': True, 'ensure_ascii': False} JSON_FORMAT = {'indent': 4, 'sort_keys': True, 'ensure_ascii': False}
DBUS_FM_ID = 'org.freedesktop.FileManager1'
DBUS_FM_PATH = '/org/freedesktop/FileManager1'
PY2 = sys.version_info.major == 2 PY2 = sys.version_info.major == 2
@ -355,20 +358,23 @@ def show_file(path, timestamp=None):
timestamp, timestamp,
timestamp, timestamp,
) )
if dbus: if dbus:
bus = dbus.SessionBus() bus = dbus.SessionBus()
filemanager1 = bus.get_object( try:
'org.freedesktop.FileManager1', '/org/freedesktop/FileManager1' filemanager1 = bus.get_object(DBUS_FM_ID, DBUS_FM_PATH)
) except dbus.exceptions.DBusException as ex:
paths = [urljoin('file:', pathname2url(path))] log.debug('Unable to get dbus file manager: %s', ex)
filemanager1.ShowItems( # Fallback to xdg-open
paths, startup_id, dbus_interface='org.freedesktop.FileManager1' else:
) paths = [urljoin('file:', pathname2url(path))]
else: filemanager1.ShowItems(paths, startup_id, dbus_interface=DBUS_FM_ID)
env = os.environ.copy() return
env['DESKTOP_STARTUP_ID'] = startup_id.replace('dbus', 'xdg-open')
# No option in xdg to highlight a file so just open parent folder. env = os.environ.copy()
subprocess.Popen(['xdg-open', os.path.dirname(path.rstrip('/'))], env=env) env['DESKTOP_STARTUP_ID'] = startup_id.replace('dbus', 'xdg-open')
# No option in xdg to highlight a file so just open parent folder.
subprocess.Popen(['xdg-open', os.path.dirname(path.rstrip('/'))], env=env)
def open_url_in_browser(url): def open_url_in_browser(url):