[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
1 changed files with 18 additions and 12 deletions

View File

@ -81,6 +81,9 @@ TORRENT_STATE = [
# The output formatting for json.dump
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
@ -355,16 +358,19 @@ def show_file(path, timestamp=None):
timestamp,
timestamp,
)
if dbus:
bus = dbus.SessionBus()
filemanager1 = bus.get_object(
'org.freedesktop.FileManager1', '/org/freedesktop/FileManager1'
)
paths = [urljoin('file:', pathname2url(path))]
filemanager1.ShowItems(
paths, startup_id, dbus_interface='org.freedesktop.FileManager1'
)
try:
filemanager1 = bus.get_object(DBUS_FM_ID, DBUS_FM_PATH)
except dbus.exceptions.DBusException as ex:
log.debug('Unable to get dbus file manager: %s', ex)
# Fallback to xdg-open
else:
paths = [urljoin('file:', pathname2url(path))]
filemanager1.ShowItems(paths, startup_id, dbus_interface=DBUS_FM_ID)
return
env = os.environ.copy()
env['DESKTOP_STARTUP_ID'] = startup_id.replace('dbus', 'xdg-open')
# No option in xdg to highlight a file so just open parent folder.