Ticket #167: winswitch-dbus-notifications.patch

File winswitch-dbus-notifications.patch, 6.7 KB (added by Antoine Martin, 12 years ago)

use dbus for notification ahead of pynotify

  • winswitch/ui/notification_util.py

     
    1010import sys
    1111import gobject
    1212
     13from winswitch.util.simple_logger import Logger, msig
     14logger = Logger("notification_util", log_colour=Logger.HIGHLIGHTED_BROWN)
     15
    1316from winswitch.consts import APPLICATION_NAME, NOTIFY_ERROR, NOTIFY_INFO, NOTIFY_MESSAGE, NOTIFY_AUTH_ERROR, NOTIFY_RETRY
    14 from winswitch.globals import WIN32
    15 from winswitch.util.simple_logger import Logger, msig
     17from winswitch.globals import WIN32, OSX
    1618from winswitch.util.common import no_newlines, escape_newlines
    1719from winswitch.ui import icons
    1820
    19 try:
    20         import appindicator
    21         assert appindicator
    22         hasappindicator=True
    23 except ImportError:
    24         hasappindicator=False
     21PREFER_DBUS = True
    2522
    26 try:
    27         import Growl            #@UnresolvedImport
    28         hasgrowl=True
     23hasappindicator = False
     24hasgrowl                = False
     25hasdbusnotify   = False
     26haspynotify             = False
    2927
    30         class gNotifier(Growl.GrowlNotifier):
    31                 applicationName = APPLICATION_NAME
    32                 notifications = ['highlight']
    33                 #applicationIcon = Growl.Image.imageWithIconForApplication("iChat")
    34                 app_icon = icons.get("winswitch")
    35                 applicationIcon = Growl.Image.imageFromPath(app_icon.full_path_to_icon_file)
    3628
    37 except ImportError:
    38         hasgrowl=False
     29def init_appindicator():
     30        try:
     31                import appindicator
     32                assert appindicator
     33                global hasappindicator
     34                hasappindicator=True
     35        except ImportError, e:
     36                logger.slog("no appindicator: %s" % e)
    3937
    40 PREFER_PYNOTIFY = True
     38def init_growl():
     39        try:
     40                import Growl.GrowlNotifier              #@UnresolvedImport
     41                assert Growl.GrowlNotifier
     42                global hasgrowl
     43                hasgrowl=True
     44        except ImportError, e:
     45                logger.slog("no growl: %s" % e)
    4146
     47def init_dbus():
     48        try:
     49                import dbus.glib
     50                assert dbus.glib
     51                bus = dbus.SessionBus()
     52                obj = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
     53                global dbusnotify
     54                dbusnotify = dbus.Interface(obj, 'org.freedesktop.Notifications')
     55                global hasdbusnotify
     56                hasdbusnotify = True
     57        except Exception, e:
     58                logger.slog("no dbus-notify: %s" % e)
    4259
     60def init_pynotify():
     61        try:
     62                import pynotify                                 #@UnresolvedImport
     63                pynotify.init(APPLICATION_NAME)
     64                haspynotify = True
     65        except Exception, e:
     66                logger.slog("no pynotify: %s" % e)
    4367
     68init_appindicator()
     69init_growl()
     70if not WIN32 and not OSX:
     71        if PREFER_DBUS:
     72                init_dbus()
     73        if not hasdbusnotify:
     74                init_pynotify()
     75        if not haspynotify and not PREFER_DBUS:
     76                init_dbus()
    4477
     78
    4579notification_util = None
    4680def get_notification_util():
    4781        global notification_util
     
    5892                self.notification_stack = None          #for gtk mode (no pynotify available / switched off)
    5993                self.prefer_growl = True
    6094                self.growl = None
    61                 try:
    62                         import pynotify                                 #@UnresolvedImport
    63                         pynotify.init(APPLICATION_NAME)
    64                         self.pynotify = pynotify
    65                 except:
    66                         self.pynotify = None
    6795
    6896        def close(self):
    6997                #cleanup:
     
    97125                if delay is None:
    98126                        delay = 5
    99127                icon = icons.get(icon_name)
    100                 self.sdebug("pynotify=%s, icon_name=%s, prefer_growl=%s, hasgrowl=%s, PREFER_PYNOTIFY=%s, platform=%s" % (self.pynotify, icon_name, self.prefer_growl, hasgrowl, PREFER_PYNOTIFY, sys.platform), tray, escape_newlines(title),escape_newlines(message),delay,callback,icon_name)
     128                self.sdebug("hasdbusnotify=%s, haspynotify=%s, icon_name=%s, prefer_growl=%s, hasgrowl=%s, PREFER_DBUS=%s, platform=%s" % (hasdbusnotify, haspynotify, icon_name, self.prefer_growl, hasgrowl, PREFER_DBUS, sys.platform), tray, escape_newlines(title),escape_newlines(message),delay,callback,icon_name)
    101129                if self.prefer_growl and hasgrowl:
    102                         if not self.growl:
    103                                 self.growl = gNotifier()
    104                                 self.growl.register()
    105                         isSticky = type==NOTIFY_AUTH_ERROR
    106                         self.growl.notify('highlight', title, message, "", isSticky)
    107                         return
    108                                
    109                 if self.pynotify and PREFER_PYNOTIFY:
     130                        self.growlnotify(title, message)
     131                        return  False
     132
     133                if hasdbusnotify:
     134                        self.dbusnotify(title, message, delay, callback, icon)
     135                        return False
     136
     137                if haspynotify:
    110138                        try:
    111139                                import glib
    112140                                try:
    113                                         self.py_notify(tray, title, message, delay, callback, icon)
     141                                        self.pynotify(tray, title, message, delay, callback, icon)
    114142                                        return False
    115143                                except glib.GError, e:
    116144                                        self.serror("code=%s, domain=%s, message=%s" % (e.code, e.domain, e.message), tray, title, message, delay, callback, icon)
    117145                                        if e.message and e.message.find("org.freedesktop.DBus.Error.ServiceUnknown")>=0:
    118146                                                #GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files
    119147                                                self.serror("disabling pynotify as there does not seem to be a notification daemon running", tray, title, message, delay, callback, icon)
    120                                                 self.pynotify = False
     148                                                global haspynotify
     149                                                haspynotify = False
    121150                        except Exception, e:
    122151                                self.serror("error type: %s" % e.__class__)
    123152                                self.serror("error: %s" % dir(e))
     
    141170                        self.serr("gtkpopupnotify failed - all options exhausted!", e, tray, title, message, delay, callback, icon)
    142171                return  False
    143172
     173        def growlnotify(self, title, message):
     174                import Growl.GrowlNotifier              #@UnresolvedImport
     175                class gNotifier(Growl.GrowlNotifier):
     176                        applicationName = APPLICATION_NAME
     177                        notifications = ['highlight']
     178                        #applicationIcon = Growl.Image.imageWithIconForApplication("iChat")
     179                        app_icon = icons.get("winswitch")
     180                        applicationIcon = Growl.Image.imageFromPath(app_icon.full_path_to_icon_file)
     181                if not self.growl:
     182                        self.growl = gNotifier()
     183                        self.growl.register()
     184                isSticky = type==NOTIFY_AUTH_ERROR
     185                self.growl.notify('highlight', title, message, "", isSticky)
     186
     187
    144188        def win32notify(self, tray, title, message, delay, callback=None):
    145189                hwnd = tray.getHWND()
    146190                sig = msig(hwnd, no_newlines(title), no_newlines(message), delay, callback)
     
    173217                        self.notification_stack.y = y
    174218                self.notification_stack.new_popup(title=title, message=message, callback=callback, image=icon)
    175219
    176         def py_notify(self, tray, title, message, delay, callback, icon):
     220        def dbusnotify(self, title, message, delay, callback, icon):
     221                global dbusnotify
     222                def cbReply(*args):
     223                        self.slog(None, *args)
     224                        return False
     225                def cbError(*args):
     226                        self.slog(None, *args)
     227                        return False
     228                iconpath = ""
     229                if icon and hasattr(icon, "full_path_to_icon_file"):
     230                        iconpath = icon.full_path_to_icon_file
     231                dbusnotify.Notify(APPLICATION_NAME, 0, iconpath, title, message, [], [], delay,
     232             reply_handler = cbReply,
     233             error_handler = cbError)
     234
     235        def pynotify(self, tray, title, message, delay, callback, icon):
    177236                self.sdebug(None, tray, no_newlines(title), no_newlines(message), delay, callback, icon)
    178237                #Find out position on screen
    179238                n = self.pynotify.Notification(title, message)