~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
"""
test-appindicator.py
August 16, 2010
"""

import appindicator
import gtk
import os

from recent_notifications.Notification import Notification

def get_icon(name):
  return os.path.join(os.path.dirname(os.path.abspath(__file__)), "icons", name + ".svg")

class RecentIndicator(object):
  def __init__(self):
    self._menu = gtk.Menu()
    self._message_items = []
    self._max_items = 5

    self._menu.append(gtk.SeparatorMenuItem())
    self._mark_as_read_item = gtk.MenuItem("Mark all as read")
    self._mark_as_read_item.connect("activate", self.on_mark_as_read)
    self._menu.append(self._mark_as_read_item)

    self._menu.show_all()
    self._menu.set_take_focus(False)

    self._indicator = appindicator.Indicator("recent-notifications", 
        get_icon("mono-dark-2-notification-read"), 
        appindicator.CATEGORY_OTHER)
    self._indicator.set_attention_icon(get_icon("mono-dark-2-notification-unread"))
    self._indicator.set_status(appindicator.STATUS_ACTIVE)
    self._indicator.set_menu(self._menu)

    self._notify = Notification()
    self._notify.connect("message-received", self.on_message_received)

  def add_item(self, message):
    #self._menu.prepend(gtk.SeparatorMenuItem())
    current_item = gtk.MenuItem(message.summary)
    self._menu.prepend(current_item)
    self._message_items.insert(0, current_item)
    self._menu.show_all()
    #self._indicator.set_menu(self._menu)
    self._indicator.set_status(appindicator.STATUS_ATTENTION)

  def remove_last_item(self):
    if len(self._message_items) > 0:
      last_item = self._message_items.pop()
      self._menu.remove(last_item)
      last_item.destroy()

  def on_mark_as_read(self, item):
    self._indicator.set_status(appindicator.STATUS_ACTIVE)

  def on_message_received(self, monitor, message):
    if len(self._message_items) >= self._max_items:
      self.remove_last_item()
    self.add_item(message)

def main():
  indicator = RecentIndicator()
  gtk.main()

if __name__ == '__main__':
  main()