2019-03-20 04:44:30 +01:00
|
|
|
import functools
|
|
|
|
|
2018-03-06 00:05:55 +01:00
|
|
|
from gi.repository import Gio # pylint: disable=import-error
|
|
|
|
from gi.repository import GLib # pylint: disable=import-error
|
2015-08-27 20:08:33 +02:00
|
|
|
import sys
|
2018-03-05 23:37:42 +01:00
|
|
|
import os
|
2015-08-27 20:08:33 +02:00
|
|
|
|
2018-03-05 23:37:42 +01:00
|
|
|
def pid_callback(launcher, pid, pid_list):
|
|
|
|
pid_list.append(pid)
|
|
|
|
|
2019-03-20 04:44:30 +01:00
|
|
|
def dbus_name_change(loop, name, old_owner, new_owner):
|
|
|
|
if not new_owner:
|
|
|
|
loop.quit()
|
|
|
|
|
2018-03-05 23:37:42 +01:00
|
|
|
def launch(desktop, *files, **kwargs):
|
|
|
|
wait = kwargs.pop('wait', True)
|
2015-08-27 20:08:33 +02:00
|
|
|
launcher = Gio.DesktopAppInfo.new_from_filename(desktop)
|
2017-12-16 19:45:10 +01:00
|
|
|
try:
|
|
|
|
import dbus
|
2019-03-20 04:44:30 +01:00
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
2017-12-16 19:45:10 +01:00
|
|
|
if hasattr(launcher, 'get_boolean'):
|
|
|
|
activatable = launcher.get_boolean('DBusActivatable')
|
|
|
|
if activatable:
|
2019-03-20 04:44:30 +01:00
|
|
|
loop = GLib.MainLoop()
|
|
|
|
DBusGMainLoop(set_as_default=True)
|
2017-12-16 19:45:10 +01:00
|
|
|
bus = dbus.SessionBus()
|
|
|
|
service_id = launcher.get_id()
|
|
|
|
# cut the .desktop suffix
|
2019-03-20 04:44:30 +01:00
|
|
|
service_id = service_id[:-len('.desktop')]
|
|
|
|
# see D-Bus Activation Desktop entry specification
|
|
|
|
object_path = '/' + service_id.replace('.', '/').\
|
|
|
|
replace('-', '_')
|
2017-12-16 19:45:10 +01:00
|
|
|
try:
|
2019-03-20 04:44:30 +01:00
|
|
|
proxy = bus.get_object(service_id, object_path)
|
|
|
|
match = bus.add_signal_receiver(
|
|
|
|
functools.partial(dbus_name_change, loop),
|
|
|
|
'NameOwnerChanged',
|
|
|
|
dbus.BUS_DAEMON_IFACE,
|
|
|
|
dbus.BUS_DAEMON_NAME,
|
|
|
|
dbus.BUS_DAEMON_PATH)
|
|
|
|
if files:
|
|
|
|
proxy.Open(files, {},
|
|
|
|
dbus_interface='org.freedesktop.Application')
|
|
|
|
else:
|
|
|
|
proxy.Activate({},
|
|
|
|
dbus_interface='org.freedesktop.Application')
|
|
|
|
except dbus.DBusException as e:
|
|
|
|
print(e)
|
|
|
|
# fallback to non-dbus version
|
2017-12-16 19:45:10 +01:00
|
|
|
pass
|
2019-03-20 04:44:30 +01:00
|
|
|
else:
|
|
|
|
if wait:
|
|
|
|
loop.run()
|
|
|
|
match.remove()
|
|
|
|
return
|
2017-12-16 19:45:10 +01:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2018-03-05 23:37:42 +01:00
|
|
|
if wait:
|
|
|
|
pid_list = []
|
|
|
|
flags = GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD
|
|
|
|
launcher.launch_uris_as_manager(files, None, flags, None, None,
|
|
|
|
pid_callback, pid_list)
|
|
|
|
for pid in pid_list:
|
|
|
|
os.waitpid(pid, 0)
|
|
|
|
else:
|
|
|
|
launcher.launch(files, None)
|
2015-08-27 20:08:33 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
launch(*sys.argv[1:])
|