~jconti/recent-notifications/trunk

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Monitor.py
by Jason Conti
February 19, 2010

Monitors DBUS for org.freedesktop.Notifications.Notify messages, parses them,
and notifies listeners when they arrive.
"""

import dbus
import glib
import gobject
import gtk
import logging
import time

from dbus.mainloop.glib import DBusGMainLoop

from Icon import load_icon, load_icon_from_file

class Message(gobject.GObject):
  """Parses a DBUS message in the Notify format specified at:
  http://www.galago-project.org/specs/notification/0.9/index.html"""
  # Message urgency
  LOW = 0
  NORMAL = 1
  CRITICAL = 2
  def __init__(self, dbus_message, timestamp):
    gobject.GObject.__init__(self)

    self.timestamp = timestamp

    args = dbus_message.get_args_list()

    self.app_name = str(args[0])
    self.replaces_id = args[1]
    self.app_icon = args[2]
    self.summary = str(args[3])
    self.body = str(args[4])
    self.actions = args[5]
    self.hints = dict(args[6])
    self.expire_timeout = args[7]

    if "urgency" in self.hints:
      urgency = self.hints["urgency"]
      if urgency == 0:
        self.urgency = Message.LOW
      elif urgency == 2:
        self.urgency = Message.CRITICAL
      else:
        self.urgency = Message.NORMAL
    else:
      self.urgency = Message.NORMAL

    self.log_message()

  def get_icon(self):
    """Loads the icon into a pixbuf. Adapted from the load_icon code in
    bubble.c of notify-osd."""
    icon_name = str(self.app_icon)
    
    # Try to load the pixbuf from a file
    if icon_name.startswith("file://") or icon_name.startswith("/"):
      icon = load_icon_from_file(icon_name)
      if icon != None:
        return icon

    # Try to load the pixbuf from the current icon theme
    else:
      icon = load_icon(icon_name)
      if icon != None:
        return icon

    return self.get_default_icon()

  def get_default_icon(self):
    """Attempts to load the default message icon, returns None on failure."""
    if self.urgency == Message.LOW:
      return load_icon("notification-low")
    elif self.urgency == Message.CRITICAL:
      return load_icon("notification-critical")
    else:
      return load_icon("notification-normal")

  def formatted_timestamp(self):
    """Returned the timestamp in a different format."""
    return time.strftime("%B %d, %Y at %I:%M:%S %p", self.timestamp)

  def log_message(self):
    """Write debug info about a message."""
    logging.debug("-" * 50)
    logging.debug("Message created at: " + self.formatted_timestamp())
    logging.debug("app_name: " + repr(self.app_name))
    logging.debug("replaces_id: " + repr(self.replaces_id))
    logging.debug("app_icon: " + repr(self.app_icon))
    logging.debug("summary: " + repr(self.summary))
    logging.debug("body: " + repr(self.body))
    logging.debug("actions: " + repr(self.actions))
    logging.debug("hints: " + repr(self.hints))
    logging.debug("expire_timeout: " + repr(self.expire_timeout))
    logging.debug("-" * 50)

class Monitor(gobject.GObject):
  """Monitors DBUS for org.freedesktop.Notifications.Notify messages, parses them,
  and notifies listeners when they arrive."""
  __gsignals__ = {
      "message-received": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [Message])
  }
  def __init__(self):
    gobject.GObject.__init__(self)

    self._match_string = "type='method_call',interface='org.freedesktop.Notifications',member='Notify'"

    DBusGMainLoop(set_as_default=True)
    self._bus = dbus.SessionBus()
    self._bus.add_match_string(self._match_string)
    self._bus.add_message_filter(self._message_filter)

  def close(self):
    self._bus.close()

  def _message_filter(self, connection, dbus_message):
    if dbus_message.get_member() == "Notify" and dbus_message.get_interface() == "org.freedesktop.Notifications":
      glib.idle_add(self.emit, "message-received", Message(dbus_message, time.localtime()))

def test_receiver(w, m):
  print "-"*50
  print "timestamp", m.formatted_timestamp()
  print "app_name", repr(m.app_name)
  print "replaces_id", repr(m.replaces_id)
  print "app_icon", repr(m.app_icon)
  print "summary", repr(m.summary)
  print "body", repr(m.body)
  print "actions", repr(m.actions)
  print "hints", repr(m.hints)
  print "expire_timeout", repr(m.expire_timeout)
  print "-"*50

def main():
  x = Monitor()
  x.connect("message-received", test_receiver)
  gtk.main()

if gtk.pygtk_version < (2, 8, 0):
  gobject.type_register(Message)
  gobject.type_register(Monitor)

if __name__ == '__main__':
  main()