[ontv] First attempt at a DBus interface



commit 7a180ebfadfbbd050c4c96da9819223c8f336278
Author: Olof Kindgren <olki src gnome org>
Date:   Mon Feb 8 19:28:44 2010 +0100

    First attempt at a DBus interface
    
    OnTV now has a DBus interface on the session bus. The first instance
    is started normally. If new instances of ntv are called with -wX, a
    message to the first instance is sent. The -w is a temporary workaround
    The X should be substitued as follows:
    s = open search window
    t = toggle visibility of program window
    u = update TV listings
    
    This interface is intended to replace the keybinder eventually.

 ontv/Makefile.am     |    1 +
 ontv/applet.py       |   29 +++++++++++++++++++++++++++++
 ontv/dbus_service.py |   27 +++++++++++++++++++++++++++
 ontv/main.py         |   32 +++++++++++++++++++++++---------
 4 files changed, 80 insertions(+), 9 deletions(-)
---
diff --git a/ontv/Makefile.am b/ontv/Makefile.am
index 13de732..ba5b5e2 100644
--- a/ontv/Makefile.am
+++ b/ontv/Makefile.am
@@ -7,6 +7,7 @@ ontv_PYTHON =			\
 	assistant.py		\
 	channel.py		\
 	config.py		\
+	dbus_service.py		\
 	dialogs.py		\
 	gui.py			\
 	__init__.py		\
diff --git a/ontv/applet.py b/ontv/applet.py
index 1fb87ff..0c5910d 100644
--- a/ontv/applet.py
+++ b/ontv/applet.py
@@ -24,9 +24,13 @@ from gettext import gettext as _
 import gtk
 import gnomeapplet
 
+import dbus
+import dbus.mainloop.glib
+
 import gui
 from assistant import XMLTVAssistant
 from config import Configuration
+from dbus_service import DBusService
 from dialogs import PreferencesDialog, SearchDialog
 from key_binder import KeyBinder
 from reminders import Reminders
@@ -49,6 +53,7 @@ class OnTVApplet(object):
         self.applet = args[0]
         self.configure = args[1]
         self.config = Configuration(args[2], args[3])
+        message = args[4]
         self.xmltvfile = XMLTVFile(self.config)
 
         self.reminders = Reminders(self.config)
@@ -92,6 +97,20 @@ class OnTVApplet(object):
 
         self.applet.show_all()
 
+        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+        remote_object = self.__get_running_instance()
+
+        if remote_object:
+            if message == "update":
+                remote_object.UpdateListings()
+            elif message == "toggle_pw":
+                remote_object.ToggleWindow()
+            elif message == "search":
+                remote_object.ShowSearch()
+            exit()
+        else:
+            DBusService(self)
+
     def __update_listings(self, uicomponent=None, verb=None):
         self.xmltvfile.download()
 
@@ -104,6 +123,16 @@ class OnTVApplet(object):
         ad.connect("response", lambda d, r: d.destroy())
         ad.show()
 
+    def __get_running_instance(self):
+        session_bus = dbus.SessionBus()
+        dbus_object = session_bus.get_object('org.freedesktop.DBus',
+                                     '/org/freedesktop/DBus')
+        dbus_iface = dbus.Interface(dbus_object, 'org.freedesktop.DBus')
+        services = dbus_iface.ListNames()
+        if "org.gnome.OnTV" in services:
+            return session_bus.get_object("org.gnome.OnTV","/DBusService")
+        return False
+
     def run(self):
         if self.configure or self.config.grabber_command == '':
             xmltv_assistant = XMLTVAssistant(self.config, self.xmltvfile)
diff --git a/ontv/dbus_service.py b/ontv/dbus_service.py
new file mode 100644
index 0000000..6b45554
--- /dev/null
+++ b/ontv/dbus_service.py
@@ -0,0 +1,27 @@
+import dbus.service
+class DBusService(dbus.service.Object):
+    def __init__(self, applet):
+        bus_name = dbus.service.BusName('org.gnome.OnTV', dbus.SessionBus())
+        dbus.service.Object.__init__(self, bus_name, '/DBusService')
+        self.pw = applet.pw
+        self.xmltvfile = applet.xmltvfile
+        self.sd = applet.sd
+
+    @dbus.service.method("org.gnome.OnTV",
+                         in_signature='', out_signature='')
+    def ShowSearch(self):
+        self.sd.show()
+
+    @dbus.service.method("org.gnome.OnTV",
+                         in_signature='', out_signature='')
+    def ToggleWindow(self):
+        if self.pw.props.visible:
+            self.pw.hide()
+        else:
+            self.pw.show_all()
+        
+    @dbus.service.method("org.gnome.OnTV",
+                         in_signature='', out_signature='')
+    def UpdateListings(self):
+        self.xmltvfile.download()
+            
diff --git a/ontv/main.py b/ontv/main.py
index d8a3f98..9c67799 100644
--- a/ontv/main.py
+++ b/ontv/main.py
@@ -42,13 +42,18 @@ def main():
     locale.textdomain(NAME.lower())
 
     try:
-        opts, args = getopt.getopt(sys.argv[1:], "cdhw", ["configure", "debug",
-                                                          "help", "window"])
+        opts, args = getopt.getopt(sys.argv[1:], "cdhstuw", ["configure",
+                                                             "debug",
+                                                             "help",
+                                                             "search",
+                                                             "toggle",
+                                                             "update",
+                                                             "window"])
     except getopt.GetoptError:
         opts = []
         args = sys.argv[1:]
 
-    configure = debug = standalone = False
+    configure = debug = standalone = message = False
     for o, a in opts:
         if o in ("-c", "--configure"):
             configure = True
@@ -56,6 +61,12 @@ def main():
             debug = True
         elif o in ("-h", "--help"):
             print_usage()
+        elif o in ("-s", "--search"):
+            message = "search"
+        elif o in ("-t", "--toggle"):
+            message = "toggle_pw"
+        elif o in ("-u", "--update"):
+            message = "update"
         elif o in ("-w", "--window"):
             standalone = True
 
@@ -71,12 +82,12 @@ def main():
                                                              48]))
         window.connect("destroy", gtk.main_quit)
         applet = gnomeapplet.Applet()
-        applet_factory(applet, None, configure, debug, True)
+        applet_factory(applet, None, configure, debug, message, True)
         applet.reparent(window)
         window.show_all()
         gtk.main()
     else:
-        activate_factory(debug)
+        activate_factory(debug, message)
 
 def print_usage():
     print _("Usage: %s [OPTIONS]...") % (sys.argv[0])
@@ -85,19 +96,22 @@ def print_usage():
     print "  -c, --configure	%s" % (_("run XMLTV assistant on startup"))
     print "  -d, --debug		%s" % (_("enable debug messages"))
     print "  -h, --help		%s" % (_("show this help message and exit"))
+    print "  -s, --search	%s" % (_("brings up search dialog"))
+    print "  -t, --toggle	%s" % (_("toggles visibility of program window"))
+    print "  -u, --update	%s" % (_("update TV listings"))
     print "  -w, --window		%s" % (_("run OnTV in a standalone window (for testing purposes)"))
 
     sys.exit()
 
-def applet_factory(applet, iid=None, configure=False, debug=False,
+def applet_factory(applet, iid=None, configure=False, debug=False, message=False,
                    standalone=False):
-    ontvapplet = OnTVApplet(applet, configure, debug, standalone)
+    ontvapplet = OnTVApplet(applet, configure, debug, standalone, message)
     ontvapplet.run()
     return True
 
-def activate_factory(configure=False, debug=False):
+def activate_factory(configure=False, debug=False, message=False):
     gnomeapplet.bonobo_factory("OAFIID:GNOME_OnTVApplet_Factory",
                                gnomeapplet.Applet.__gtype__, NAME, VERSION,
-                               applet_factory, (configure, debug,))
+                               applet_factory, (configure, debug, message,))
 
 # vim: set sw=4 et sts=4 tw=79 fo+=l:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]