~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
150
151
152
153
"""
Main.py
by Jason Conti
February 19, 2010

Applet for viewing recent notifications.
"""

import gnomeapplet
import gtk
import logging
import os.path

from About import show_about
from Icon import load_icon
from MessageWindow import MessageWindow
from Notification import Notification

class Main(object):
  """The main applet wrapper."""
  def __init__(self, applet, iid):
    self._applet = applet
    self._applet.connect("size-allocate", self.on_allocate)

    self._unread_messages = 0

    size = self.get_icon_size(self.get_applet_size())
    self.load_icons(size)

    self._image = gtk.Image()
    self._image.set_from_pixbuf(self._read_icon)

    self._button = gtk.ToggleButton()
    self._button.set_image(self._image)
    self._button.connect("button_press_event", self.do_not_eat_button_press)
    self._button.connect("toggled", self.on_button_toggled)
    self._applet.add(self._button)

    self._applet.setup_menu("""
      <popup name="button3">
        <menuitem name="About" verb="About" _label="_About" pixtype="stock" pixname="gtk-about"/>
      </popup>
      """, [
      ("About", lambda a, b: self.on_show_about(a))
      ])

    self._window = MessageWindow(self)

    self._applet.connect("destroy", self.on_applet_destroyed)
    self._applet.show_all()

    self.update()

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

  def get_applet_size(self):
    return self._applet.get_size()

  def get_icon_size(self, size):
    if size < 22:
      size = 16
    elif size < 24:
      size = 22
    elif size < 32:
      size = 24
    elif size < 48:
      size = 32
    else:
      size = 48

    return size

  def get_orient(self):
    return self._applet.get_orient()

  def get_origin(self):
    return self._button.get_window().get_origin()

  def get_screen(self):
    return self._button.get_window().get_screen()

  def get_size(self):
    return self._button.get_window().get_size()

  def load_icons(self, size):
    self._read_icon = load_icon("notification-read", size)
    self._unread_icon = load_icon("notification-unread", size)

  def update(self):
    self.update_applet_image()
    self.update_unread_messages()

  def update_applet_image(self):
    if self._unread_messages > 0:
      icon = self._unread_icon
    else:
      icon = self._read_icon

    if icon != self._image.get_pixbuf():
      self._image.set_from_pixbuf(icon)

  def update_unread_messages(self):
    if self._unread_messages == 0:
      label = "No unread messages"
    elif self._unread_messages == 1:
      label = "1 unread message"
    else:
      label = str(self._unread_messages) + " unread messages"

    self._button.set_tooltip_text(label)

  # Callbacks

  def do_not_eat_button_press(self, widget, event):
    """Adapted from the GNOME clock applet."""
    if event.button != 1:
      widget.stop_emission("button_press_event")

    return False

  def on_allocate(self, applet, alloc):
    if self.get_orient() in [gnomeapplet.ORIENT_DOWN, gnomeapplet.ORIENT_UP]:
      size_alloc = alloc.height
    else:
      size_alloc = alloc.width

    icon_size = self.get_icon_size(size_alloc)

    if self._read_icon != None and icon_size != self._read_icon.get_width():
      self.load_icons(size)
      self.update_applet_image()

  def on_applet_destroyed(self, *args):
    self._notify.close()

  def on_button_toggled(self, button, *args):
    if button.get_active():
      self._window.show_all()
      self._window.align_with_parent()
    else:
      self._window.hide_all()
      self._unread_messages = 0
      self.update()

  def on_message_received(self, monitor, message):
    self._window.add_message(message)
    self._unread_messages += 1
    self.update()

  def on_show_about(self, *args):
    show_about(self)