#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set ft=python ts=4 sw=4 sts=4 et :
import sys
from gi.repository import Gio, GLib
def main(myname, desktop, *files):
info = Gio.DesktopAppInfo()
dbus_activate = False
try:
launcher = info.new_from_filename(desktop)
except TypeError:
launcher = None
if launcher:
dbus_activate = launcher.get_boolean('DBusActivatable')
if not dbus_activate:
launcher.launch(files, None)
else:
#import subprocess
#if dbus_activate:
# print launcher.get_id().replace('.desktop', '')
# process = subprocess.Popen(['gapplication', 'launch', launcher.get_id().replace('.desktop', '')], close_fds=True)
#return
# (Gio.DBusConnection) – A Gio.DBusConnection
session = Gio.bus_get_sync(Gio.BusType.SESSION, None)
# (Gio.DBusProxyFlags) – Flags used when constructing the proxy.
flags = Gio.DBusProxyFlags.NONE
# (Gio.DBusInterfaceInfo|None) – A Gio.DBusInterfaceInfo specifying the minimal interface that proxy conforms to or None.
info = None
# (str or None) – A bus name (well-known or unique) or None if connection is not a message bus connection.
app_id = launcher.get_id()
if app_id.endswith('.desktop'):
app_id = app_id[:-8]
# (str) – An object path.
object_path = '/' + app_id.replace('.', '/').replace('-', '_')
# (str) – A D-Bus interface name.
interface_name = 'org.freedesktop.Application'
# cancellable (Gio.Cancellable or None) – A Gio.Cancellable or None.
cancellable = None
proxy = Gio.DBusProxy.new_sync(session,
flags,
info,
app_id,
object_path,
interface_name,
cancellable
)
builder = GLib.VariantBuilder.new(GLib.VariantType.new('r'))
if files:
builder.open(GLib.VariantType.new('as'))
for filename in files:
print 'Adding ', filename
builder.add_value(GLib.Variant('s', filename))
builder.close()
builder.open(GLib.VariantType.new('a{sv}'))
#startup_id = GLib.getenv("DESKTOP_STARTUP_ID") or app_id
#if startup_id:
# #builder.add_value(GLib.Variant('a{sv}', {'startup-id': GLib.Variant('s', startup_id)}))
builder.close()
parameters = builder.end()
print 'Calling call_sync...'
if files:
print 'Open...'
result = proxy.call_sync ("Open", parameters, Gio.DBusCallFlags.NONE, -1, cancellable)
else:
print 'Activate...'
result = proxy.call_sync ("Activate", parameters, Gio.DBusCallFlags.NONE, -1, cancellable)
print 'Result: ', result
print 'Done.'
if __name__ == "__main__":
main(*sys.argv)