6
Monitors DBUS for org.freedesktop.Notifications.Notify messages, parses them,
7
and notifies listeners when they arrive.
11
import dbus.mainloop.glib
18
class Message(gobject.GObject):
19
"""Parses a DBUS message in the Notify format specified at:
20
http://www.galago-project.org/specs/notification/0.9/index.html"""
21
def __init__(self, dbus_message, timestamp):
22
gobject.GObject.__init__(self)
24
self.timestamp = timestamp
26
args = dbus_message.get_args_list()
28
self.app_name = str(args[0])
29
self.replaces_id = args[1]
30
self.app_icon = args[2]
31
self.summary = str(args[3])
32
self.body = str(args[4])
33
self.actions = args[5]
35
self.expire_timeout = args[7]
39
def get_icon_pixbuf(self):
40
"""Loads the icon into a pixbuf. Adapted from the load_icon code in
41
bubble.c of notify-osd."""
42
icon_name = str(self.app_icon)
44
# Try to load the pixbuf from a file
45
if icon_name.startswith("file://") or icon_name.startswith("/"):
47
return gtk.gdk.pixbuf_new_from_file_at_size(icon_name, 48, 48)
51
# Try to load the pixbuf from the current icon theme
53
theme = gtk.icon_theme_get_default()
54
return theme.load_icon(icon_name, 48, gtk.ICON_LOOKUP_FORCE_SVG)
58
def formatted_timestamp(self):
59
"""Returned the timestamp in a different format."""
60
return time.strftime("%B %d, %Y at %I:%M:%S %p", self.timestamp)
62
def log_message(self):
63
"""Write debug info about a message."""
64
logging.debug("-" * 50)
65
logging.debug("Message created at: " + self.formatted_timestamp())
66
logging.debug("app_name: " + repr(self.app_name))
67
logging.debug("replaces_id: " + repr(self.replaces_id))
68
logging.debug("app_icon: " + repr(self.app_icon))
69
logging.debug("summary: " + repr(self.summary))
70
logging.debug("body: " + repr(self.body))
71
logging.debug("actions: " + repr(self.actions))
72
logging.debug("hints: " + repr(self.hints))
73
logging.debug("expire_timeout: " + repr(self.expire_timeout))
74
logging.debug("-" * 50)
76
class Monitor(gobject.GObject):
77
"""Monitors DBUS for org.freedesktop.Notifications.Notify messages, parses them,
78
and notifies listeners when they arrive."""
80
"message-received": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [Message])
83
gobject.GObject.__init__(self)
85
self._match_string = "type='method_call',interface='org.freedesktop.Notifications',member='Notify'"
87
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
88
self._bus = dbus.SessionBus()
89
self._bus.add_match_string(self._match_string)
90
self._bus.add_message_filter(self._message_filter)
95
def _message_filter(self, connection, dbus_message):
96
if dbus_message.get_member() == "Notify" and dbus_message.get_interface() == "org.freedesktop.Notifications":
97
glib.idle_add(self.emit, "message-received", Message(dbus_message, time.localtime()))
99
def test_receiver(w, m):
101
print "timestamp", m.formatted_timestamp()
102
print "app_name", repr(m.app_name)
103
print "replaces_id", repr(m.replaces_id)
104
print "app_icon", repr(m.app_icon)
105
print "summary", repr(m.summary)
106
print "body", repr(m.body)
107
print "actions", repr(m.actions)
108
print "hints", repr(m.hints)
109
print "expire_timeout", repr(m.expire_timeout)
114
x.connect("message-received", test_receiver)
117
if gtk.pygtk_version < (2, 8, 0):
118
gobject.type_register(Message)
119
gobject.type_register(Monitor)
121
if __name__ == '__main__':