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
|
"""
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
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):
gtk.TreeView.__init__(self)
self._message_icon_size = 32
self._filter_app = None
self._unread_messages = []
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)
# 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)
else:
iter = self._liststore.prepend([icon,
message.summary,
message.body,
message.timestamp,
message.app_name,
1,
True])
if iter != None:
self._unread_messages.append(iter)
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 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."""
result = text.replace("&", "&")
result = result.replace(">", ">")
result = result.replace("<", "<")
return glib.markup_escape_text(result)
def increment_first_message(self, message):
"""Increments the duplicate_count column of the message and updates its timestamp."""
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)
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 mark_as_read(self):
"""Marks the unread column as False for all the unread messages."""
for iter in self._unread_messages:
self._liststore.set_value(iter, Column.UNREAD, False)
self._unread_messages = []
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_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 _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
|