Ticket #167: winswitch-dbus-notifications.patch
File winswitch-dbus-notifications.patch, 6.7 KB (added by , 12 years ago) |
---|
-
winswitch/ui/notification_util.py
10 10 import sys 11 11 import gobject 12 12 13 from winswitch.util.simple_logger import Logger, msig 14 logger = Logger("notification_util", log_colour=Logger.HIGHLIGHTED_BROWN) 15 13 16 from 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 17 from winswitch.globals import WIN32, OSX 16 18 from winswitch.util.common import no_newlines, escape_newlines 17 19 from winswitch.ui import icons 18 20 19 try: 20 import appindicator 21 assert appindicator 22 hasappindicator=True 23 except ImportError: 24 hasappindicator=False 21 PREFER_DBUS = True 25 22 26 try: 27 import Growl #@UnresolvedImport 28 hasgrowl=True 23 hasappindicator = False 24 hasgrowl = False 25 hasdbusnotify = False 26 haspynotify = False 29 27 30 class gNotifier(Growl.GrowlNotifier):31 applicationName = APPLICATION_NAME32 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)36 28 37 except ImportError: 38 hasgrowl=False 29 def 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) 39 37 40 PREFER_PYNOTIFY = True 38 def 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) 41 46 47 def 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) 42 59 60 def 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) 43 67 68 init_appindicator() 69 init_growl() 70 if 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() 44 77 78 45 79 notification_util = None 46 80 def get_notification_util(): 47 81 global notification_util … … 58 92 self.notification_stack = None #for gtk mode (no pynotify available / switched off) 59 93 self.prefer_growl = True 60 94 self.growl = None 61 try:62 import pynotify #@UnresolvedImport63 pynotify.init(APPLICATION_NAME)64 self.pynotify = pynotify65 except:66 self.pynotify = None67 95 68 96 def close(self): 69 97 #cleanup: … … 97 125 if delay is None: 98 126 delay = 5 99 127 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) 101 129 if self.prefer_growl and hasgrowl: 102 if not self.growl:103 self.growl = gNotifier()104 self.growl.register() 105 isSticky = type==NOTIFY_AUTH_ERROR106 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: 110 138 try: 111 139 import glib 112 140 try: 113 self.py _notify(tray, title, message, delay, callback, icon)141 self.pynotify(tray, title, message, delay, callback, icon) 114 142 return False 115 143 except glib.GError, e: 116 144 self.serror("code=%s, domain=%s, message=%s" % (e.code, e.domain, e.message), tray, title, message, delay, callback, icon) 117 145 if e.message and e.message.find("org.freedesktop.DBus.Error.ServiceUnknown")>=0: 118 146 #GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files 119 147 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 121 150 except Exception, e: 122 151 self.serror("error type: %s" % e.__class__) 123 152 self.serror("error: %s" % dir(e)) … … 141 170 self.serr("gtkpopupnotify failed - all options exhausted!", e, tray, title, message, delay, callback, icon) 142 171 return False 143 172 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 144 188 def win32notify(self, tray, title, message, delay, callback=None): 145 189 hwnd = tray.getHWND() 146 190 sig = msig(hwnd, no_newlines(title), no_newlines(message), delay, callback) … … 173 217 self.notification_stack.y = y 174 218 self.notification_stack.new_popup(title=title, message=message, callback=callback, image=icon) 175 219 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): 177 236 self.sdebug(None, tray, no_newlines(title), no_newlines(message), delay, callback, icon) 178 237 #Find out position on screen 179 238 n = self.pynotify.Notification(title, message)