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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
"""
Main.py
by Jason Conti
February 19, 2010
Applet for viewing recent notifications.
"""
import gnomeapplet
import gtk
import os.path
from string import Template
from About import show_about
from Icon import clear_icon_cache, load_icon
from MessageWindow import MessageWindow
from Notification import Notification
from Translations import _, ngettext
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_relief(gtk.RELIEF_NONE)
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(Template("""
<popup name="button3">
<menuitem name="Clear" verb="Clear" _label="$clear_label" pixtype="stock" pixname="gtk-clear"/>
<separator/>
<menuitem name="About" verb="About" _label="$about_label" pixtype="stock" pixname="gtk-about"/>
</popup>
""").substitute(
about_label=_("_About"),
clear_label=_("_Clear Messages")), [
("Clear", lambda a, b: self.on_clear_messages(a)),
("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):
"""Gets the size of the applet."""
return self._applet.get_size()
def get_icon_size(self, size):
"""Gets the icon size from the given 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):
"""Gets the orientation of the panel."""
return self._applet.get_orient()
def get_origin(self):
"""Gets the position of the button window."""
return self._button.get_window().get_origin()
def get_screen(self):
"""Gets the screen the button window is on."""
return self._button.get_window().get_screen()
def get_size(self):
"""Gets the size of the button window."""
return self._button.get_window().get_size()
def load_icons(self, size):
"""Loads icons from the default icon theme."""
self._read_icon = load_icon("humanity-notification-read", size)
self._unread_icon = load_icon("humanity-notification-unread", size)
def update(self):
"""Updates the applet icon and unread messages tooltip."""
self.update_applet_image()
self.update_unread_messages()
def update_applet_image(self):
"""Update the applet icon if it has changed."""
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):
"""Updates the unread message tooltip."""
n = self._unread_messages
label = ngettext("1 unread message", "{0} unread messages", n).format(n)
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):
"""Update the icon size if the panel size changes."""
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):
"""Stop the message notifications before destroying the applet."""
self._notify.close()
def on_button_toggled(self, button, *args):
"""Show/Hide the message window when the button is toggled."""
if button.get_active():
self._window.show_all()
self._window.align_with_parent()
else:
self._window.hide_all()
self._window.clear_selections()
self._unread_messages = 0
self.update()
def on_clear_messages(self, *args):
"""Clear messages from the message window."""
self._window.clear_messages()
clear_icon_cache()
def on_message_received(self, monitor, message):
"""Received a message from the notifier."""
# Discard volume notifications
if not message.is_volume_notification():
self._window.add_message(message)
self._unread_messages += 1
self.update()
def on_show_about(self, *args):
"""Show the about dialog."""
show_about(self)
|