Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/model/notifications.py
blob: da5c590b223b5a3663cb44c3253c505b28d07cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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"

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

_instance = None

def get_service():
    global _instance
    if not _instance:
        _instance = NotificationService()
    return _instance

def init():
    get_service()