-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailbox.vala
453 lines (390 loc) · 12.6 KB
/
mailbox.vala
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using Gee;
using Gtk;
namespace GmailFeed {
/**
* The Mailbox is a collection of MailItems. They are sorted by time received.
* Actions on the messages are made through the mailbox.
**/
public class Mailbox : GLib.Object {
//{ Members
/**
* The list has the messages in sorted order.
* the map allows for fast access of the desired message.
**/
private Gee.List<MessageItem> mail_list;
private Gee.Map<string, MessageItem> messages;
//}
//{ Properties
/**
* Mailbox message count
**/
public int size {
get {
return mail_list.size;
}
}
/**
* Read only view of the mailbox in sorted order
**/
public Gee.List<MessageItem> items {
owned get {
return mail_list.read_only_view;
}
}
//}
public Mailbox() {
this.mail_list = new ArrayList<MessageItem>();
this.messages = new HashMap<string, MessageItem>();
}
/**
* Adds a message to the mailbox. Places the message at the proper place in the mail list.
**/
public void add_message(MessageItem msg) {
if(!messages.has_key(msg.id)) {
messages[msg.id] = msg;
int i;
for(i = 0; i < mail_list.size; i++) {
if(mail_list[i].time.compare(msg.time) < 0) {
break;
}
}
mail_list.insert(i, msg);
}
}
/**
* Gets the mail item with the given id
**/
public new MessageItem? get(string id) {
if(messages.has_key(id)) {
return messages[id];
} else {
return null;
}
}
/**
* Removes the message with the given id
**/
public void remove_message(string id) {
if(messages.has_key(id)) {
MessageItem mess = null;
messages.unset(id, out mess);
mail_list.remove(mess);
}
}
/**
* Reactivates all messages in the mailbox.
* This is called on error to ensure the mailbox is functional again.
**/
public void reactivate_all() {
foreach(var mess in mail_list) {
mess.resetActions();
}
}
}
[GtkTemplate (ui = "/org/wrowclif/gmailnotify/ui/message_visual.ui")]
public class MessageItem : Box {
//{ Static data
public enum RemoveAction {
NONE = 0,
MARK_READ = 1,
ARCHIVE = 2,
SPAM = 3,
TRASH = 4
}
/**
* These images are static because they can be reused by all messages.
**/
private static Gdk.Pixbuf STAR_FULL;
private static Gdk.Pixbuf STAR_EMPTY;
private static Gdk.Pixbuf STAR_HALF;
static construct {
// This is the directory the images are located in.
var base_dir = "/usr/share/gmailnotify";
try {
STAR_FULL = new Gdk.Pixbuf.from_file_at_size(
"%s/star_full.png".printf(base_dir), 16, 16);
STAR_EMPTY = new Gdk.Pixbuf.from_file_at_size(
"%s/star_empty.png".printf(base_dir), 16, 16);
STAR_HALF = new Gdk.Pixbuf.from_file_at_size(
"%s/star_half.png".printf(base_dir), 16, 16);
} catch(GLib.Error e) {
stderr.printf("Could not load images\n");
}
}
private static const string inactiveTemplate =
"<small><span foreground='darkred'>%s</span>%s</small>";
private static const string hoverTemplate =
"<small><u><span foreground='darkred'>%s</span></u>%s</small>";
private static const string activeTemplate =
"<small><i><u><span foreground='darkred'>%s</span></u></i>%s</small>";
//}
//{ Template members
[GtkChild]
private Label subjectLbl;
[GtkChild]
private Label fromLbl;
[GtkChild]
private Label summaryLbl;
[GtkChild]
private Image starImg;
[GtkChild]
private Label markReadLbl;
[GtkChild]
private Label archiveLbl;
[GtkChild]
private Label spamLbl;
[GtkChild]
private Label trashLbl;
//}
//{ Members
/**
* The controller that interacts with Gmail for us.
**/
private FeedController feed;
/**
* The action we are currently performing, or RemoveAction.NONE if we haven't
* started an action yet.
**/
private RemoveAction curAction;
/**
* Whether the user should be able to change the star state of the message currently.
**/
private bool starEnabled;
/**
* Whether the message is starred or not. We update this variable to
* match the state of our internal `msg` when `updateMessage()` is called.
* Otherwise it tracks the current state of the star. We do not want to modify `msg`
* ourselves, which is why we have this variable.
**/
private bool starred;
//}
//{ Properties
/**
* This message's id.
**/
public string id {
get {
return this.msg.id;
}
}
/**
* When this message was sent.
**/
public DateTime time {
get {
return this.msg.time;
}
}
/**
* All the details we know about the message.
**/
public GMessage msg {get; private set;}
//}
public MessageItem(GMessage msg, FeedController feed) {
this.curAction = RemoveAction.NONE;
this.starEnabled = true;
this.feed = feed;
this.updateMessage(msg);
this.feed.messageStarred.connect(this.onMessageStarred);
this.feed.messageUnstarred.connect(this.onMessageUnstarred);
}
/**
* Update the view to match the state of the message.
*
* @param msg: The message we are showing.
**/
public void updateMessage(GMessage msg) {
this.msg = msg;
this.starred = this.msg.starred;
this.starImg.pixbuf = this.starred ? STAR_FULL : STAR_EMPTY;
this.subjectLbl.label = this.msg.subject;
this.fromLbl.label = this.msg.author;
this.summaryLbl.set_markup(this.msg.summary);
//{ Set the styling for the removal actions
this.onMarkReadLeave();
this.onArchiveLeave();
this.onSpamLeave();
this.onTrashLeave();
//}
}
/**
* Called when the feed successfully stars a message.
*
* Note: This signal is sent in response to a request made by us.
* If the star state is changed through the gmail website or
* other means, we will get that update via an `updateMessage()` call.
*
* @param id: The id of the message that was starred.
**/
private void onMessageStarred(string id) {
// We get this signal when any message is starred, so we
// need to check if if applies to this message before doing anything.
if(id == this.msg.id) {
this.starred = true;
this.starEnabled = true;
this.starImg.pixbuf = STAR_FULL;
}
}
/**
* Called when the feed successfully unstars a message.
*
* Note: This signal is sent in response to a request made by us.
* If the star state is changed through the gmail website or
* other means, we will get that update via an `updateMessage()` call.
*
* @param id: The id of the message that was starred.
**/
private void onMessageUnstarred(string id) {
// We get this signal when any message is unstarred, so we
// need to check if if applies to this message before doing anything.
if(id == this.msg.id) {
this.starred = false;
this.starEnabled = true;
this.starImg.pixbuf = STAR_EMPTY;
}
}
//{ Star
[GtkCallback]
private bool onStarEnter() {
if(this.starEnabled) {
this.starImg.pixbuf = STAR_HALF;
}
return false;
}
[GtkCallback]
private bool onStarLeave() {
if(this.starEnabled) {
this.starImg.pixbuf = this.starred ? STAR_FULL : STAR_EMPTY;
}
return false;
}
[GtkCallback]
private bool onStarClicked() {
if(this.starEnabled) {
this.starEnabled = false;
// The feed distinguishes between starring and unstarring, but we
// use a single button for both. So we check our state to determine
// which call to make.
if(this.starred) {
feed.unstarMsg(this.msg.id);
} else {
feed.starMsg(this.msg.id);
}
}
return false;
}
//}
//{ Mark read
[GtkCallback]
private bool onMarkReadEnter() {
if(this.curAction == RemoveAction.NONE) {
this.markReadLbl.set_markup(hoverTemplate.printf("Mark as read", " |"));
}
return false;
}
[GtkCallback]
private bool onMarkReadLeave() {
if(this.curAction == RemoveAction.NONE) {
this.markReadLbl.set_markup(inactiveTemplate.printf("Mark as read", " |"));
}
return false;
}
[GtkCallback]
private bool onMarkReadClicked() {
if(this.curAction == RemoveAction.NONE) {
this.markReadLbl.set_markup(activeTemplate.printf("Marking as read...", " |"));
this.curAction = RemoveAction.MARK_READ;
feed.markRead(this.msg.id);
}
return false;
}
//}
//{ Archive
[GtkCallback]
private bool onArchiveEnter() {
if(this.curAction == RemoveAction.NONE) {
this.archiveLbl.set_markup(hoverTemplate.printf("Archive", " |"));
}
return false;
}
[GtkCallback]
private bool onArchiveLeave() {
if(this.curAction == RemoveAction.NONE) {
this.archiveLbl.set_markup(inactiveTemplate.printf("Archive", " |"));
}
return false;
}
[GtkCallback]
private bool onArchiveClicked() {
if(this.curAction == RemoveAction.NONE) {
this.archiveLbl.set_markup(activeTemplate.printf("Archiving...", " |"));
this.curAction = RemoveAction.ARCHIVE;
feed.archive(this.msg.id);
}
return false;
}
//}
//{ Spam
[GtkCallback]
private bool onSpamEnter() {
if(this.curAction == RemoveAction.NONE) {
this.spamLbl.set_markup(hoverTemplate.printf("Report spam", " |"));
}
return false;
}
[GtkCallback]
private bool onSpamLeave() {
if(this.curAction == RemoveAction.NONE) {
this.spamLbl.set_markup(inactiveTemplate.printf("Report spam", " |"));
}
return false;
}
[GtkCallback]
private bool onSpamClicked() {
if(this.curAction == RemoveAction.NONE) {
this.spamLbl.set_markup(activeTemplate.printf("Reporting spam...", " |"));
this.curAction = RemoveAction.SPAM;
feed.spam(this.msg.id);
}
return false;
}
//}
//{ Trash
[GtkCallback]
private bool onTrashEnter() {
if(this.curAction == RemoveAction.NONE) {
this.trashLbl.set_markup(hoverTemplate.printf("Delete", ""));
}
return false;
}
[GtkCallback]
private bool onTrashLeave() {
if(this.curAction == RemoveAction.NONE) {
this.trashLbl.set_markup(inactiveTemplate.printf("Delete", ""));
}
return false;
}
[GtkCallback]
private bool onTrashClicked() {
if(this.curAction == RemoveAction.NONE) {
this.trashLbl.set_markup(activeTemplate.printf("Deleting...", ""));
this.curAction = RemoveAction.TRASH;
feed.trash(this.msg.id);
}
return false;
}
//}
/**
* Called when we recover from a connection error of some sort.
* Resets the view so that it will respond to user interactions again.
*
* When we begin an action we freeze the message UI until that action completes.
* If the connection fails while we are trying to make the request, we need to get
* back into a state where we can retry.
**/
public void resetActions() {
this.curAction = RemoveAction.NONE;
this.starEnabled = true;
}
}
}