[ontv] Separate applet from backend



commit 8466414ab0750825541f0b03e07c291a5b090932
Author: Olof Kindgren <olki src gnome org>
Date:   Tue Apr 13 21:29:21 2010 +0200

    Separate applet from backend
    
    First step in allowing different frontends for OnTV. The backend is
    no longer dependent on (soon deprecated) gnome libraries. Frontends
    can use the new file ontv_core.py as entry point.

 ontv/Makefile.am     |    1 +
 ontv/applet.py       |  148 +++++++++++------------------------------------
 ontv/config.py       |    2 -
 ontv/dbus_service.py |   16 ++----
 ontv/main.py         |    7 +-
 ontv/ontv_core.py    |  158 ++++++++++++++++++++++++++++++++++++++++++++++++++
 ontv/window.py       |   11 ++--
 7 files changed, 207 insertions(+), 136 deletions(-)
---
diff --git a/ontv/Makefile.am b/ontv/Makefile.am
index caf67ac..f5e13d3 100644
--- a/ontv/Makefile.am
+++ b/ontv/Makefile.am
@@ -12,6 +12,7 @@ ontv_PYTHON =			\
 	listings.py		\
 	main.py			\
 	notify.py		\
+	ontv_core.py		\
 	program.py		\
 	reminders.py		\
 	utils.py		\
diff --git a/ontv/applet.py b/ontv/applet.py
index af581b5..f8aefd1 100644
--- a/ontv/applet.py
+++ b/ontv/applet.py
@@ -24,18 +24,9 @@ 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 reminders import Reminders
-from window import ProgramWindow
-from xmltv_file import XMLTVFile
-from ontv import NAME, VERSION, UI_DIR, LOCALE_DIR
+from ontv import NAME, UI_DIR
+from ontv_core import OnTVCore
 
 class OnTVApplet(object):
     """Singleton representing the applet"""
@@ -49,44 +40,31 @@ class OnTVApplet(object):
         return OnTVApplet.instance
 
     def __init(self, *args):
-        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)
-        if os.path.exists(self.reminders.file):
-            self.reminders = self.reminders.load()
-
+        self.applet     = args[0]
+        configure       = args[1]
+        debug           = args[2]
+        message         = args[3]
 
-        self.pw = ProgramWindow(self.xmltvfile,
-                                self.get_docking_data)
-        self.sd = SearchDialog(self.xmltvfile, self.reminders)
-        self.pd = PreferencesDialog(self.config, self.xmltvfile,
-									self.reminders, self.pw, self.sd)
+        self.ontv_core = OnTVCore(configure, debug, message, self.cb_status)
 
         self.applet.set_applet_flags(gnomeapplet.EXPAND_MINOR)
 
         ui_file = os.path.join(UI_DIR, "GNOME_OnTVApplet.xml")
         verb_list = [("Update TV listings", self.__update_listings),
-                     ("Preferences", self.pd.show),
-                     ("Search Program", self.sd.show),
+                     ("Preferences", self.__show_pd),
+                     ("Search Program", self.__show_sd),
                      ("About", self.__run_ad)]
         self.applet.setup_menu_from_file(None, ui_file, NAME, verb_list)
 
         self.applet.connect("button-press-event", self.__button_press)
 
         self.image = gtk.Image()
-        if not self.config.standalone:
-            size = self.applet.get_size()
-            pixbuf = gui.load_icon("ontv", size, size)
-            if pixbuf.get_width() != size or pixbuf.get_height() != size:
-                pixbuf = pixbuf.scale_simple(size-4, size-4,
-                                             gtk.gdk.INTERP_BILINEAR)
-            self.image.set_from_pixbuf(pixbuf)
-        else:
-            self.image.set_from_icon_name(NAME.lower(), gtk.ICON_SIZE_BUTTON)
+        size = self.applet.get_size()
+        pixbuf = gui.load_icon("ontv", size, size)
+        if pixbuf.get_width() != size or pixbuf.get_height() != size:
+            pixbuf = pixbuf.scale_simple(size-4, size-4,
+                                         gtk.gdk.INTERP_BILINEAR)
+        self.image.set_from_pixbuf(pixbuf)
 
         self.applet.add(self.image)
 
@@ -94,82 +72,33 @@ class OnTVApplet(object):
         self.applet.connect("change-background", self.__change_background)
 
         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.pw, self.sd, self.xmltvfile)
+        self.ontv_core.set_program_window_position(self.get_docking_data())
 
     def __update_listings(self, uicomponent=None, verb=None):
-        self.xmltvfile.download()
+        self.ontv_core.update_listings()
 
     def __run_ad(self, uicomponent=None, verb=None):
-        builder = gtk.Builder()
-        builder.add_from_file(gui.ui_file)
-        ad = builder.get_object("about_dialog")
-        ad.set_name(NAME)
-        ad.set_version(VERSION)
-        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
+        self.ontv_core.show_about_dialog()
+
+    def __show_sd(self, uicomponent, verb):
+        self.ontv_core.show_search_dialog()
+
+    def __show_pd(self, uicomponent, verb):
+        self.ontv_core.show_preferences_dialog()
 
     def run(self):
-        if self.configure or self.config.grabber_command == '':
-            xmltv_assistant = XMLTVAssistant(self.config, self.xmltvfile)
-            xmltv_assistant.show()
-        else:
-            self.xmltvfile.connect("downloading", self.__xmltvfile_activity,
-                                   (_("Downloading"),))
-            self.xmltvfile.connect("downloading-done",
-                                   self.__xmltvfile_downloading_done)
-            self.xmltvfile.connect("sorting", self.__xmltvfile_activity,
-                                   (_("Sorting"),))
-            self.xmltvfile.connect("sorting-done",
-                                   self.__xmltvfile_sorting_done)
-            self.xmltvfile.connect("loading", self.__xmltvfile_activity,
-                                   (_("Loading"),))
-            self.xmltvfile.connect("loading-done",
-                                   self.__xmltvfile_loading_done)
-            # self.xmltvfile.download()
-            self.xmltvfile.load()
+        self.ontv_core.run()
 
     def __button_press(self, button, event):
         if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1:
             self.__press_button()
 
     def __press_button(self):
-        if not self.pw.props.visible:
+        self.ontv_core.set_program_window_position(self.get_docking_data())
+        if self.ontv_core.toggle_program_window():
             self.applet.set_state(gtk.STATE_SELECTED)
-            self.pw.position_window()
-            self.pw.stick()
-            self.pw.show_all()
-            self.pw.grab_focus()
         else:
             self.applet.set_state(gtk.STATE_NORMAL)
-            self.pw.hide()
-
-        if not self.config.display_current_programs:
-            self.pw.cpt.hide()
-        if not self.config.display_upcoming_programs:
-            self.pw.upt.hide()
 
     def __change_size(self, widget, size):
         pixbuf = gui.load_icon("ontv", size, size)
@@ -194,19 +123,9 @@ class OnTVApplet(object):
     def set_tooltip(self, text):
         self.applet.set_tooltip_text(text)
 
-    def __xmltvfile_activity(self, xmltvfile, activity):
-        self.set_tooltip(_("%s TV Listings...") % activity)
-
-    def __xmltvfile_downloading_done(self, xmltvfile, pid, condition):
-        self.set_tooltip(None)
-        self.xmltvfile.sort()
-
-    def __xmltvfile_sorting_done(self, xmltvfile, pid, condition):
-        self.set_tooltip(None)
-        self.xmltvfile.load()
-
-    def __xmltvfile_loading_done(self, xmltvfile, listings):
-        self.set_tooltip(None)
+    def cb_status(self, msg):
+        if msg:
+            self.set_tooltip(msg)
 
     def __kb_activated(self, kb, name, time):
         if name == "show-window-hotkey":
@@ -214,7 +133,9 @@ class OnTVApplet(object):
         elif name == "show-search-program-hotkey":
             self.sd.present(time)
 
-    def get_docking_data(self, middle, w=0, h=0):
+    def get_docking_data(self):
+        (w, h) = self.ontv_core.get_program_window_size()
+        middle = False
         self.applet.realize()
         (x, y) = self.applet.window.get_origin()
 
@@ -266,7 +187,7 @@ class OnTVApplet(object):
                 gravity = gtk.gdk.GRAVITY_SOUTH_EAST
             else:
                 gravity = gtk.gdk.GRAVITY_NORTH_EAST
-        elif orient == gnomeapplet.ORIENT_DOWN or self.config.standalone:
+        elif orient == gnomeapplet.ORIENT_DOWN:
             y += button_h
 
             if ((x + w) > monitor.x + monitor.width):
@@ -288,7 +209,6 @@ class OnTVApplet(object):
                 y += h/2
 
             gravity = gtk.gdk.GRAVITY_SOUTH_WEST
-
         return (x, y, gravity)
 
 # vim: set sw=4 et sts=4 tw=79 fo+=l:
diff --git a/ontv/config.py b/ontv/config.py
index 9e0a954..6c6e572 100644
--- a/ontv/config.py
+++ b/ontv/config.py
@@ -76,8 +76,6 @@ class Configuration(object):
             ed.run()
             sys.exit(1)
 
-        self.standalone = args[1]
-
     def __init_option_cache(self):
         self.option_cache = {}
         for key in FUNCTION_SUFFIXES.keys():
diff --git a/ontv/dbus_service.py b/ontv/dbus_service.py
index a566506..7942d6e 100644
--- a/ontv/dbus_service.py
+++ b/ontv/dbus_service.py
@@ -20,28 +20,22 @@
 
 import dbus.service
 class DBusService(dbus.service.Object):
-    def __init__(self, pw, sd, xmltvfile):
+    def __init__(self, ontv_core):
+        self.ontv_core = ontv_core
         bus_name = dbus.service.BusName('org.gnome.OnTV', dbus.SessionBus())
         dbus.service.Object.__init__(self, bus_name, '/DBusService')
-        self.pw = pw
-        self.xmltvfile = xmltvfile
-        self.sd = sd
 
     @dbus.service.method("org.gnome.OnTV",
                          in_signature='', out_signature='')
     def ShowSearch(self):
-        self.sd.show()
+        self.ontv_core.show_search_dialog()
 
     @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()
-        
+        self.ontv_core.toggle_program_window()
     @dbus.service.method("org.gnome.OnTV",
                          in_signature='', out_signature='')
     def UpdateListings(self):
-        self.xmltvfile.download()
+        self.ontv_core.update_listings()
             
diff --git a/ontv/main.py b/ontv/main.py
index 720e533..ade33c3 100644
--- a/ontv/main.py
+++ b/ontv/main.py
@@ -82,7 +82,7 @@ def main():
                                                              48]))
         window.connect("destroy", gtk.main_quit)
         applet = gnomeapplet.Applet()
-        applet_factory(applet, None, configure, debug, message, True)
+        applet_factory(applet, None, configure, debug, message)
         applet.reparent(window)
         window.show_all()
         gtk.main()
@@ -103,9 +103,8 @@ def print_usage():
 
     sys.exit()
 
-def applet_factory(applet, iid=None, configure=False, debug=False, message=False,
-                   standalone=False):
-    ontvapplet = OnTVApplet(applet, configure, debug, standalone, message)
+def applet_factory(applet, iid=None, configure=False, debug=False, message=False):
+    ontvapplet = OnTVApplet(applet, configure, debug, message)
     ontvapplet.run()
     return True
 
diff --git a/ontv/ontv_core.py b/ontv/ontv_core.py
new file mode 100644
index 0000000..e547258
--- /dev/null
+++ b/ontv/ontv_core.py
@@ -0,0 +1,158 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (C) 2010 Olof Kindgren <olki src gnome org>
+
+# This file is part of OnTV.
+
+# OnTV is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+# OnTV is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with OnTV; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+
+import os.path
+from gettext import gettext as _
+
+import gtk
+
+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 reminders import Reminders
+from window import ProgramWindow
+from xmltv_file import XMLTVFile
+from ontv import NAME, VERSION, UI_DIR, LOCALE_DIR
+
+class OnTVCore:
+    """Entry point for OnTV backend"""
+
+    def __init__(self, configure, debug, message, cb_status):
+
+        self.configure = configure
+        self.debug = debug
+        self.config = Configuration(self.debug)
+
+        self.xmltvfile = XMLTVFile(self.config)
+        self.cb_status = cb_status
+        self.reminders = Reminders(self.config)
+        if os.path.exists(self.reminders.file):
+            self.reminders = self.reminders.load()
+
+        self.pw = ProgramWindow(self.xmltvfile)
+        self.sd = SearchDialog(self.xmltvfile, self.reminders)
+        self.pd = PreferencesDialog(self.config, self.xmltvfile,
+                                    self.reminders, self.pw, self.sd)
+
+        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):
+        self.xmltvfile.download()
+
+    def show_about_dialog(self):
+        builder = gtk.Builder()
+        builder.add_from_file(gui.ui_file)
+        ad = builder.get_object("about_dialog")
+        ad.set_name(NAME)
+        ad.set_version(VERSION)
+        ad.connect("response", lambda d, r: d.destroy())
+        ad.show()
+
+    def show_preferences_dialog(self):
+        self.pd.show()
+
+    def show_search_dialog(self):
+        self.sd.show()
+
+    def toggle_program_window(self):
+        visible = True
+        if self.pw.props.visible:
+            self.pw.hide()
+            visible = False
+        else:
+            self.pw.position_window(self.window_position)
+            self.pw.stick()
+            self.pw.show_all()
+            self.pw.grab_focus()
+
+        if not self.config.display_current_programs:
+            self.pw.cpt.hide()
+        if not self.config.display_upcoming_programs:
+            self.pw.upt.hide()
+
+        return visible
+
+    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)
+            xmltv_assistant.show()
+        else:
+            self.xmltvfile.connect("downloading", self.__xmltvfile_activity,
+                                   (_("Downloading"),))
+            self.xmltvfile.connect("downloading-done",
+                                   self.__xmltvfile_downloading_done)
+            self.xmltvfile.connect("sorting", self.__xmltvfile_activity,
+                                   (_("Sorting"),))
+            self.xmltvfile.connect("sorting-done",
+                                   self.__xmltvfile_sorting_done)
+            self.xmltvfile.connect("loading", self.__xmltvfile_activity,
+                                   (_("Loading"),))
+            self.xmltvfile.connect("loading-done",
+                                   self.__xmltvfile_loading_done)
+            self.xmltvfile.load()
+
+    def __xmltvfile_activity(self, xmltvfile, activity):
+        self.cb_status(_("%s TV Listings...") % activity)
+
+    def __xmltvfile_downloading_done(self, xmltvfile, pid, condition):
+        self.cb_status(None)
+        self.xmltvfile.sort()
+
+    def __xmltvfile_sorting_done(self, xmltvfile, pid, condition):
+        self.cb_status(None)
+        self.xmltvfile.load()
+
+    def __xmltvfile_loading_done(self, xmltvfile, listings):
+        self.cb_status(None)
+
+    def get_program_window_size(self):
+        return self.pw.get_window_size()
+
+    def set_program_window_position(self, window_position):
+        self.window_position = window_position
+
+# vim: set sw=4 et sts=4 tw=79 fo+=l:
diff --git a/ontv/window.py b/ontv/window.py
index 524dfbe..e46d60e 100644
--- a/ontv/window.py
+++ b/ontv/window.py
@@ -273,10 +273,9 @@ class ProgramTimeLeftBar(ProgramBar, gtk.Label):
         return True
 
 class ProgramWindow(gtk.Window):
-    def __init__(self, xmltvfile, get_docking_data):
+    def __init__(self, xmltvfile):
         gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
         xmltvfile.connect("loading-done", self.__xmltvfile_loading_done)
-        self.get_docking_data = get_docking_data
 
         self.cpt = CurrentProgramTable(xmltvfile.listings)
         self.upt = UpcomingProgramTable(xmltvfile.listings)
@@ -319,14 +318,16 @@ class ProgramWindow(gtk.Window):
         if event.keyval == gtk.keysyms.Escape:
             window.hide()
 
-    def position_window(self):
+    def get_window_size(self):
         self.realize()
         gtk.gdk.flush()
-
         (w, h) = self.get_size()
         (w, h) = self.size_request()
+        return (w, h)
+
+    def position_window(self, window_position):#window_position):
 
-        (x, y, gravity) = self.get_docking_data(False, w, h)
+        (x, y, gravity) = window_position
 
         self.move(x, y)
         self.set_gravity(gravity)



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