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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
"""
Main.py
by Jason Conti
February 19, 2010
Applet for viewing recent notifications.
"""
import gnomeapplet
import gtk
import logging
import os.path
from string import Template
from About import show_about
from Config import Config
from Icon import clear_icon_cache, load_icon
from ImageToggleButton import ImageToggleButton
from MessageWindow import MessageWindow
from Notification import Notification
from Preferences import show_preferences
from Translations import _, ngettext
logger = logging.getLogger("Main")
class Main(object):
"""The main applet wrapper."""
def __init__(self, applet, iid):
self._applet = applet
self._applet.set_background_widget(applet)
self._applet.connect("change-size", self.on_change_size)
self._applet.connect("change-orient", self.on_change_orient)
self._unread_messages = 0
self._show_count = False
self._applet_size = self.get_applet_size()
icon_size = self.get_icon_size(self._applet_size)
self.load_icons(icon_size)
self._button = ImageToggleButton()
self._button.set_from_pixbuf(self._read_icon)
self._button.set_orient(self.get_orient())
self._button.connect("toggled", self.on_button_toggled)
self._button.show()
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"/>
<menuitem name="Preferences" verb="Preferences" _label="$preferences_label" pixtype="stock" pixname="gtk-preferences"/>
</popup>
""").substitute(
about_label=_("_About"),
clear_label=_("_Clear Messages"),
preferences_label=_("_Preferences")), [
("Clear", lambda a, b: self.on_clear_messages(a)),
("About", lambda a, b: self.on_show_about(a)),
("Preferences", lambda a, b: self.on_show_preferences(a))
])
self._window = MessageWindow(self)
self._applet.connect("destroy", self.on_applet_destroyed)
self._applet.show_all()
self.update()
self._blacklist = Config("~/.config/recent-notifications/blacklist")
self._preferences = Config("~/.config/recent-notifications/preferences")
self._preferences.set_if_unset("debug", False)
self._preferences.set_if_unset("message_limit", 0)
self._preferences.set_if_unset("show_count", False)
self._preferences.add_listener("debug", self.on_preference_change)
self._preferences.add_listener("message_limit", self.on_preference_change)
self._preferences.add_listener("show_count", self.on_preference_change)
self._preferences.notify_all()
self._notify = Notification()
self._notify.set_blacklist(self._blacklist)
self._notify.connect("message-received", self.on_message_received)
self._theme = gtk.icon_theme_get_default()
self._theme.connect("changed", self.on_theme_changed)
def add_to_blacklist(self, app_name):
"""Adds the given app_name to the blacklist."""
self._blacklist.set(app_name, True)
def get_blacklist_items(self):
"""Returns a list of (app_name, boolean) tuples for the blacklist items."""
result = []
for key in self._blacklist:
result.append((key, self._blacklist.get_bool(key)))
return result
def set_debug(self, debug):
"""Enables/disables debug messages."""
global_logger = logging.getLogger("")
if debug:
global_logger.setLevel(logging.DEBUG)
else:
global_logger.setLevel(logging.ERROR)
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._button.get_pixbuf():
self._button.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)
if self._show_count:
self._button.set_label(str(n))
def show_window(self):
"""Shows the applet window"""
self._button.set_has_tooltip(False)
self._window.show_dropdown()
def hide_window(self):
"""Hides the applet window"""
self._button.set_has_tooltip(True)
self._window.hide_dropdown()
self._unread_messages = 0
self.update()
# Callbacks
def on_change_size(self, applet, size):
"""Update the icon size if the panel size changes."""
if self._applet_size == size:
return
self._applet_size = size
icon_size = self.get_icon_size(size)
self.load_icons(icon_size)
self.update_applet_image()
def on_change_orient(self, applet, orient):
"""Updates the button orientation when the panel changes orientation"""
self._button.set_orient(orient)
def on_applet_destroyed(self, applet):
"""Stop the message notifications before destroying the applet."""
self._notify.close()
def on_button_toggled(self, button):
"""Show/Hide the message window when the button is toggled."""
if button.get_active():
self.show_window()
else:
self.hide_window()
def on_clear_messages(self, item):
"""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."""
self._window.add_message(message)
self._unread_messages += 1
self.update()
def on_show_about(self, item):
"""Show the about dialog."""
show_about(self)
def on_show_preferences(self, item):
"""Show the preferences dialog."""
show_preferences(self)
def on_preference_change(self, key, value):
"""Triggers actions on preference changes."""
if key == "debug":
self.set_debug(self._preferences.get_bool(key, False))
elif key == "message_limit":
self._window.set_message_limit(self._preferences.get_int(key, 0))
elif key == "show_count":
self._show_count = self._preferences.get_bool(key, False)
if self._show_count:
self.update()
else:
self._button.set_label("")
def on_theme_changed(self, theme):
"""Updates applet icons when the theme changes"""
clear_icon_cache()
icon_size = self.get_icon_size(self._applet_size)
self.load_icons(icon_size)
self.update_applet_image()
|