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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
"""
MessageList.py
by Jason Conti
March 13, 2010
Displays a list of messages in the message window.
"""
import glib
import gtk
import logging
import pango
import webbrowser
from string import Template
import Timestamp
from Translations import _
logger = logging.getLogger("MessageList")
read_message_template = Template("""<b>${summary}</b>
${body}
<small><i>${timestamp} ${from} <b>${app_name}</b></i></small>""")
unread_message_template = Template("""<small>${new}</small> <b>${summary}</b>
${body}
<small><i>${timestamp} ${from} <b>${app_name}</b></i></small>""")
class Column(object):
"""Maps column names to column numbers."""
ICON = 0
SUMMARY = 1
BODY = 2
TIMESTAMP = 3
APP_NAME = 4
DUPLICATE_COUNT = 5
UNREAD = 6
class MessageList(gtk.TreeView):
"""Displays a list of messages in the message window."""
def __init__(self, parent):
gtk.TreeView.__init__(self)
self._parent = parent
self._message_icon_size = 32
self._message_limit = 0
self._filter_app = None
self._popup_shown = False
self.set_headers_visible(False)
self.set_size_request(0, -1)
self.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_HORIZONTAL)
self.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
self.connect("button-press-event", self.on_button_press)
# icon, summary, body, timestamp, app_name, duplicate_count, unread
self._liststore = gtk.ListStore(gtk.gdk.Pixbuf, str, str, object, str, int, bool)
self._treefilter = self._liststore.filter_new()
self._treefilter.set_visible_func(self._message_visible_func)
self.set_model(self._treefilter)
self._column = gtk.TreeViewColumn()
self.append_column(self._column)
self._icon_cell = gtk.CellRendererPixbuf()
self._icon_cell.set_property("xalign", 0)
self._icon_cell.set_property("yalign", 0)
self._icon_cell.set_property("xpad", 5)
self._icon_cell.set_property("ypad", 5)
self._column.pack_start(self._icon_cell, False)
self._column.add_attribute(self._icon_cell, "pixbuf", Column.ICON)
self._message_cell = gtk.CellRendererText()
self._message_cell.set_property("wrap-mode", pango.WRAP_WORD_CHAR)
self._message_cell.set_property("xpad", 5)
self._message_cell.set_property("ypad", 5)
self._column.pack_start(self._message_cell, True)
self._column.set_cell_data_func(self._message_cell, self._message_data_func)
def add_message(self, message):
"""Adds a message to the message list."""
try:
icon = message.get_icon(self._message_icon_size)
except:
logger.exception("Failed to get icon: " + message.app_icon)
icon = None
if self.is_duplicate_message(message):
iter = self.increment_first_message(message, icon)
else:
iter = self._liststore.prepend([icon,
message.summary,
message.body,
message.timestamp,
message.app_name,
1,
True])
self._parent.increment_combobox(message.app_name)
# Discard old messages if over the message limit
if self._message_limit > 0 and len(self._liststore) > self._message_limit:
n = self._liststore.iter_n_children(None)
old_iter = self._liststore.iter_nth_child(None, n - 1)
if old_iter:
app_name = self._liststore.get_value(old_iter, Column.APP_NAME)
unread = self._liststore.get_value(old_iter, Column.UNREAD)
self._liststore.remove(old_iter)
self._parent.decrement_combobox(app_name)
if unread:
self._parent._parent._unread_messages -= 1
self._parent._parent.update_unread_messages()
def all_rows_changed(self):
"""Notify the liststore that all rows have changed.
TODO: there should be quicker way to do this."""
iter = self._liststore.get_iter_first()
while iter and self._liststore.iter_is_valid(iter):
self._liststore.row_changed(self._liststore.get_path(iter), iter)
iter = self._liststore.iter_next(iter)
def clear(self):
"""Remove all the messages in this message list."""
self._liststore.clear()
def create_popup(self, path):
"""Creates a popup menu."""
iter = self._liststore.get_iter(path)
body = self._liststore.get_value(iter, Column.BODY)
app_name = self._liststore.get_value(iter, Column.APP_NAME)
links = self.get_links(body)
clipboard = gtk.clipboard_get("CLIPBOARD")
popup = gtk.Menu()
popup.connect("selection-done", self.on_selection_done)
copy_text_item = gtk.MenuItem(_("Copy Text"))
copy_text_item.connect("activate", lambda *x: clipboard.set_text(body))
copy_text_item.show()
popup.append(copy_text_item)
def do_copy_link(link):
def wrapper(*args):
clipboard.set_text(link)
return wrapper
def do_visit_link(link):
def wrapper(*args):
webbrowser.open(link)
return wrapper
if len(links) > 0:
copy_link_item = gtk.MenuItem(_("Copy Link"))
copy_link_item.show()
copy_link_menu = gtk.Menu()
copy_link_item.set_submenu(copy_link_menu)
popup.append(copy_link_item)
visit_link_item = gtk.MenuItem(_("Visit Link"))
visit_link_item.show()
visit_link_menu = gtk.Menu()
visit_link_item.set_submenu(visit_link_menu)
popup.append(visit_link_item)
for link in links:
copy_item = gtk.MenuItem(link)
copy_item.connect("activate", do_copy_link(link))
copy_item.show()
copy_link_menu.append(copy_item)
visit_item = gtk.MenuItem(link)
visit_item.connect("activate", do_visit_link(link))
visit_item.show()
visit_link_menu.append(visit_item)
separator = gtk.SeparatorMenuItem()
separator.show()
popup.append(separator)
blacklist_item = gtk.MenuItem(_("Blacklist {0}").format(app_name))
blacklist_item.connect("activate", lambda *x: self._parent._parent.add_to_blacklist(app_name))
blacklist_item.show()
popup.append(blacklist_item)
return popup
def escape_body(self, text):
"""Text needs to be escaped in case it contains characters that will cause errors
parsing the markup, but some clients send text that is already escaped. This causes
messages to have &, <, and > strings instead of the appropriate characters.
This method replaces those with the appropriate characters and then escapes the text.
TODO: Create a better HTML/entity parser.
"""
result = text.replace("&", "&")
result = result.replace("'", "'")
result = result.replace(">", ">")
result = result.replace("<", "<")
result = result.replace(""", '"')
return glib.markup_escape_text(result)
def get_links(self, text):
"""Returns a list of all the links found in the given text."""
links = []
for s in text.split():
if s.startswith("http://"):
links.append(s)
elif s.startswith("www."):
links.append("http://" + s)
return links
def increment_first_message(self, message, icon):
"""Increments the duplicate_count column of the message and updates its timestamp.
Also some applications, like rhythmbox, like to send duplicate messages with a
new icon, so this is updated as well if it is not None."""
iter = self._liststore.get_iter_first()
if iter != None:
count = self._liststore.get_value(iter, Column.DUPLICATE_COUNT)
self._liststore.set_value(iter, Column.DUPLICATE_COUNT, count + 1)
self._liststore.set_value(iter, Column.TIMESTAMP, message.timestamp)
self._liststore.set_value(iter, Column.UNREAD, True)
if icon != None:
self._liststore.set_value(iter, Column.ICON, icon)
else:
logger.debug("increment_first_message: called without any messages")
return iter
def is_duplicate_message(self, message):
"""Returns true if the top message summary, body and app_name match the given
message."""
iter = self._liststore.get_iter_first()
if iter != None:
summary = self._liststore.get_value(iter, Column.SUMMARY)
body = self._liststore.get_value(iter, Column.BODY)
app_name = self._liststore.get_value(iter, Column.APP_NAME)
return (message.summary == summary and message.body == body and message.app_name == app_name)
else:
return False
def popup_shown(self):
"""Returns true if the popup is currently being shown"""
return self._popup_shown
def mark_as_read(self):
"""Marks the unread column as False for all the unread messages."""
iter = self._liststore.get_iter_first()
while iter and self._liststore.get_value(iter, Column.UNREAD):
self._liststore.set_value(iter, Column.UNREAD, False)
iter = self._liststore.iter_next(iter)
def set_filter_app(self, app_name):
"""Sets the filter app_name and refilters the tree."""
self._filter_app = app_name
self._treefilter.refilter()
def set_message_limit(self, n):
"""Sets the maximum number of messages to save before old messages are
discarded."""
if 0 <= n <= 100:
self._message_limit = n
def set_wrap_width(self, width):
"""Sets the wrap width for the message list. The width the text wraps at
will be the given width minus the icon width and the padding."""
icon_pad = self._icon_cell.get_property("xpad")
message_pad = self._message_cell.get_property("xpad")
new_width = width - self._message_icon_size - 2*icon_pad - 2*message_pad - 40
self._message_cell.set_property("wrap-width", new_width)
self.all_rows_changed()
def unselect_all(self):
"""Removes all selections from the message list."""
self.get_selection().unselect_all()
def on_button_press(self, treeview, event):
"""Shows a popup menu for the current cell on right click"""
if event.button == 3:
x = int(event.x)
y = int(event.y)
path_info = treeview.get_path_at_pos(x, y)
if path_info != None:
path, col, cell_x, cell_y = path_info
treeview.grab_focus()
treeview.set_cursor(path, col, 0)
popup = self.create_popup(path)
self._popup_shown = True
popup.popup(None, None, None, event.button, event.time)
return True
def on_selection_done(self, menushell):
"""Called when the popup menu selection is complete"""
self._popup_shown = False
def _message_data_func(self, column, cell, model, iter, *user_data):
"""Sets the cell text by substituting the row data into the message template."""
summary = glib.markup_escape_text(model.get_value(iter, Column.SUMMARY))
body = self.escape_body(model.get_value(iter, Column.BODY))
timestamp = glib.markup_escape_text(Timestamp.time_ago_in_words(model.get_value(iter, Column.TIMESTAMP)))
app_name = glib.markup_escape_text(model.get_value(iter, Column.APP_NAME))
count = model.get_value(iter, Column.DUPLICATE_COUNT)
unread = model.get_value(iter, Column.UNREAD)
if count > 1:
summary += " (" + str(count) + ")"
mapping = {
"new": _("NEW"),
"summary": summary,
"body": body,
"timestamp": timestamp,
"from": _("from"),
"app_name": app_name
}
if unread:
message = unread_message_template.substitute(mapping)
else:
message = read_message_template.substitute(mapping)
cell.set_property('markup', message)
def _message_visible_func(self, model, iter, *user_data):
"""Filters rows based on app name."""
if self._filter_app == None:
return True
else:
app_name = model.get_value(iter, Column.APP_NAME)
if self._filter_app == app_name:
return True
else:
return False
|