~jconti/recent-notifications/trunk

« back to all changes in this revision

Viewing changes to Monitor.py

  • Committer: Jason Conti
  • Date: 2010-02-21 00:51:16 UTC
  • Revision ID: jason.conti@gmail.com-20100221005116-6r8y8tta8f21h3x9
Reorganizing to make it easier to build a package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Monitor.py
3
 
by Jason Conti
4
 
February 19, 2010
5
 
 
6
 
Monitors DBUS for org.freedesktop.Notifications.Notify messages, parses them,
7
 
and notifies listeners when they arrive.
8
 
"""
9
 
 
10
 
import dbus
11
 
import dbus.mainloop.glib
12
 
import glib
13
 
import gobject
14
 
import gtk
15
 
import logging
16
 
import time
17
 
 
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)
23
 
 
24
 
    self.timestamp = timestamp
25
 
 
26
 
    args = dbus_message.get_args_list()
27
 
 
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]
34
 
    self.hints = args[6]
35
 
    self.expire_timeout = args[7]
36
 
 
37
 
    self.log_message()
38
 
 
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)
43
 
    
44
 
    # Try to load the pixbuf from a file
45
 
    if icon_name.startswith("file://") or icon_name.startswith("/"):
46
 
      try:
47
 
        return gtk.gdk.pixbuf_new_from_file_at_size(icon_name, 48, 48)
48
 
      except:
49
 
        return None
50
 
 
51
 
    # Try to load the pixbuf from the current icon theme
52
 
    try:
53
 
      theme = gtk.icon_theme_get_default()
54
 
      return theme.load_icon(icon_name, 48, gtk.ICON_LOOKUP_FORCE_SVG)
55
 
    except:
56
 
      return None
57
 
 
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)
61
 
 
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)
75
 
 
76
 
class Monitor(gobject.GObject):
77
 
  """Monitors DBUS for org.freedesktop.Notifications.Notify messages, parses them,
78
 
  and notifies listeners when they arrive."""
79
 
  __gsignals__ = {
80
 
      "message-received": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [Message])
81
 
  }
82
 
  def __init__(self):
83
 
    gobject.GObject.__init__(self)
84
 
 
85
 
    self._match_string = "type='method_call',interface='org.freedesktop.Notifications',member='Notify'"
86
 
 
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)
91
 
 
92
 
  def close(self):
93
 
    self._bus.close()
94
 
 
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()))
98
 
 
99
 
def test_receiver(w, m):
100
 
  print "-"*50
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)
110
 
  print "-"*50
111
 
 
112
 
def main():
113
 
  x = Monitor()
114
 
  x.connect("message-received", test_receiver)
115
 
  gtk.main()
116
 
 
117
 
if gtk.pygtk_version < (2, 8, 0):
118
 
  gobject.type_register(Message)
119
 
  gobject.type_register(Monitor)
120
 
 
121
 
if __name__ == '__main__':
122
 
  main()