Commits

Vladimir Kolev committed ad5c9c1

gSharkDown as a single Service

Comments (0)

Files changed (1)

 import os
 import sys
 import random
+import dbus
+import dbus.service
+import dbus.glib
 from lib.tfuncs import t_init
 from lib.tfuncs import t_download
 from lib.tfuncs import t_play
         """
         pass
 
+class SingleService(dbus.service.Object):
+    """
+    Class to ensure that gSharkDown is running as a single service
+    """
+
+    def __init__(self, app):
+        self.app = app
+        bus_name = dbus.service.BusName('org.gsharkdown.Single',
+                                       bus=dbus.SessionBus())
+        dbus.service.Object.__init__(self, bus_name, '/org/gsharkdown/Single')
+
+    @dbus.service.method(dbus_interface='org.gsharkdown.Single')
+    def show_window(self):
+        self.app.window.present()
+
+
 if __name__ == "__main__":
-    app = SharkDown()
-    init_thread = t_init(app)
-    init_thread.run()
-    gtk.main()
+    owner = dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER
+    if dbus.SessionBus().request_name("org.gsharkdown.Single") != owner:
+        message = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT,
+                                   gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE,
+                                   _("gSharkDown is already running!"))
+        message.set_title(_("gSharkDown Running"))
+        message.run()
+        message.destroy()
+        method = dbus.SessionBus().get_object("org.gsharkdown.Single",
+                        "/org/gsharkdown/Single").get_dbus_method("show_window")
+        method()
+    else:
+        app = SharkDown()
+        service = SingleService(app)
+        init_thread = t_init(app)
+        init_thread.run()
+        gtk.main()