diff options
Diffstat (limited to 'src/jarabe/model/notifications.py')
-rw-r--r-- | src/jarabe/model/notifications.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/jarabe/model/notifications.py b/src/jarabe/model/notifications.py new file mode 100644 index 0000000..ec14056 --- /dev/null +++ b/src/jarabe/model/notifications.py @@ -0,0 +1,98 @@ +# Copyright (C) 2008 One Laptop Per Child +# +# This program 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. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import sys +import logging + +import dbus + +from sugar import dispatch + +from jarabe import config + + +_DBUS_SERVICE = 'org.freedesktop.Notifications' +_DBUS_IFACE = 'org.freedesktop.Notifications' +_DBUS_PATH = '/org/freedesktop/Notifications' + +_instance = None + + +class NotificationService(dbus.service.Object): + def __init__(self): + bus = dbus.SessionBus() + bus_name = dbus.service.BusName(_DBUS_SERVICE, bus=bus) + dbus.service.Object.__init__(self, bus_name, _DBUS_PATH) + + self._notification_counter = 0 + self.notification_received = dispatch.Signal() + self.notification_cancelled = dispatch.Signal() + + @dbus.service.method(_DBUS_IFACE, + in_signature='susssava{sv}i', out_signature='u') + def Notify(self, app_name, replaces_id, app_icon, summary, body, actions, + hints, expire_timeout): + + logging.debug('Received notification: %r', [app_name, replaces_id, + '<app_icon>', summary, body, actions, '<hints>', + expire_timeout]) + + if replaces_id > 0: + notification_id = replaces_id + else: + if self._notification_counter == sys.maxint: + self._notification_counter = 1 + else: + self._notification_counter += 1 + notification_id = self._notification_counter + + self.notification_received.send(self, app_name=app_name, + replaces_id=replaces_id, app_icon=app_icon, summary=summary, + body=body, actions=actions, hints=hints, + expire_timeout=expire_timeout) + + return notification_id + + @dbus.service.method(_DBUS_IFACE, in_signature='u', out_signature='') + def CloseNotification(self, notification_id): + self.notification_cancelled.send(self, notification_id=notification_id) + + @dbus.service.method(_DBUS_IFACE, in_signature='', out_signature='as') + def GetCapabilities(self): + return [] + + @dbus.service.method(_DBUS_IFACE, in_signature='', out_signature='sss') + def GetServerInformation(self, name, vendor, version): + return 'Sugar Shell', 'Sugar', config.version + + @dbus.service.signal(_DBUS_IFACE, signature='uu') + def NotificationClosed(self, notification_id, reason): + pass + + @dbus.service.signal(_DBUS_IFACE, signature='us') + def ActionInvoked(self, notification_id, action_key): + pass + + +def get_service(): + global _instance + if not _instance: + _instance = NotificationService() + return _instance + + +def init(): + get_service() |