-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathMessageList.vala
More file actions
350 lines (291 loc) · 13.7 KB
/
MessageList.vala
File metadata and controls
350 lines (291 loc) · 13.7 KB
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
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2017-2023 elementary, Inc. (https://elementary.io)
*
* Authored by: Corentin Noël <corentin@elementary.io>
*/
public class Mail.MessageList : Gtk.Box {
public signal void hovering_over_link (string? label, string? uri);
public Hdy.HeaderBar headerbar { get; private set; }
private Gtk.ListBox list_box;
private Gtk.ScrolledWindow scrolled_window;
private Gee.HashMap<string, MessageListItem> messages;
construct {
get_style_context ().add_class (Gtk.STYLE_CLASS_BACKGROUND);
get_style_context ().add_class ("message-list");
var application_instance = (Gtk.Application) GLib.Application.get_default ();
var load_images_menuitem = new Granite.SwitchModelButton (_("Always Show Remote Images"));
var account_settings_menuitem = new Gtk.ModelButton () {
text = _("Account Settings…")
};
var app_menu_separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL) {
margin_bottom = 3,
margin_top = 3
};
var app_menu_box = new Gtk.Box (VERTICAL, 0) {
margin_bottom = 3,
margin_top = 3
};
app_menu_box.add (load_images_menuitem);
app_menu_box.add (app_menu_separator);
app_menu_box.add (account_settings_menuitem);
app_menu_box.show_all ();
var app_menu_popover = new Gtk.Popover (null);
app_menu_popover.add (app_menu_box);
var app_menu = new Gtk.MenuButton () {
image = new Gtk.Image.from_icon_name ("open-menu", Gtk.IconSize.LARGE_TOOLBAR),
popover = app_menu_popover,
tooltip_text = _("Menu")
};
var reply_button = new Gtk.Button.from_icon_name ("mail-reply-sender", Gtk.IconSize.LARGE_TOOLBAR) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_REPLY,
action_target = ""
};
reply_button.tooltip_markup = Granite.markup_accel_tooltip (
application_instance.get_accels_for_action (reply_button.action_name + "::"),
_("Reply")
);
var reply_all_button = new Gtk.Button.from_icon_name ("mail-reply-all", Gtk.IconSize.LARGE_TOOLBAR) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_REPLY_ALL,
action_target = ""
};
reply_all_button.tooltip_markup = Granite.markup_accel_tooltip (
application_instance.get_accels_for_action (reply_all_button.action_name + "::"),
_("Reply All")
);
var forward_button = new Gtk.Button.from_icon_name ("mail-forward", Gtk.IconSize.LARGE_TOOLBAR) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_FORWARD,
action_target = ""
};
forward_button.tooltip_markup = Granite.markup_accel_tooltip (
application_instance.get_accels_for_action (forward_button.action_name + "::"),
_("Forward")
);
var mark_unread_item = new Gtk.MenuItem () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_MARK_UNREAD
};
mark_unread_item.bind_property ("sensitive", mark_unread_item, "visible");
mark_unread_item.add (new Granite.AccelLabel.from_action_name (_("Mark as Unread"), mark_unread_item.action_name));
var mark_read_item = new Gtk.MenuItem () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_MARK_READ
};
mark_read_item.bind_property ("sensitive", mark_read_item, "visible");
mark_read_item.add (new Granite.AccelLabel.from_action_name (_("Mark as Read"), mark_read_item.action_name));
var mark_star_item = new Gtk.MenuItem () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_MARK_STAR
};
mark_star_item.bind_property ("sensitive", mark_star_item, "visible");
mark_star_item.add (new Granite.AccelLabel.from_action_name (_("Star"), mark_star_item.action_name));
var mark_unstar_item = new Gtk.MenuItem () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_MARK_UNSTAR
};
mark_unstar_item.bind_property ("sensitive", mark_unstar_item, "visible");
mark_unstar_item.add (new Granite.AccelLabel.from_action_name (_("Unstar"), mark_unstar_item.action_name));
var mark_menu = new Gtk.Menu ();
mark_menu.add (mark_unread_item);
mark_menu.add (mark_read_item);
mark_menu.add (mark_star_item);
mark_menu.add (mark_unstar_item);
mark_menu.show_all ();
var mark_button = new Gtk.MenuButton () {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_MARK,
image = new Gtk.Image.from_icon_name ("edit-mark", Gtk.IconSize.LARGE_TOOLBAR),
popup = mark_menu,
tooltip_text = _("Mark Conversation")
};
var archive_button = new Gtk.Button.from_icon_name ("mail-archive", Gtk.IconSize.LARGE_TOOLBAR) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_ARCHIVE
};
archive_button.tooltip_markup = Granite.markup_accel_tooltip (
application_instance.get_accels_for_action (archive_button.action_name),
_("Move conversations to archive")
);
var trash_button = new Gtk.Button.from_icon_name ("edit-delete", Gtk.IconSize.LARGE_TOOLBAR) {
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_MOVE_TO_TRASH
};
trash_button.tooltip_markup = Granite.markup_accel_tooltip (
application_instance.get_accels_for_action (trash_button.action_name),
_("Move conversations to Trash")
);
headerbar = new Hdy.HeaderBar () {
show_close_button = true
};
headerbar.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
headerbar.pack_start (reply_button);
headerbar.pack_start (reply_all_button);
headerbar.pack_start (forward_button);
headerbar.pack_start (new Gtk.Separator (Gtk.Orientation.VERTICAL));
headerbar.pack_start (mark_button);
headerbar.pack_start (archive_button);
headerbar.pack_start (trash_button);
headerbar.pack_end (app_menu);
var settings = new GLib.Settings ("io.elementary.mail");
settings.bind ("always-load-remote-images", load_images_menuitem, "active", SettingsBindFlags.DEFAULT);
account_settings_menuitem.clicked.connect (() => {
try {
AppInfo.launch_default_for_uri ("settings://accounts/online", null);
} catch (Error e) {
warning ("Failed to open account settings: %s", e.message);
}
});
var placeholder = new Gtk.Label (_("No Message Selected")) {
visible = true
};
var placeholder_style_context = placeholder.get_style_context ();
placeholder_style_context.add_class (Granite.STYLE_CLASS_H2_LABEL);
placeholder_style_context.add_class (Gtk.STYLE_CLASS_DIM_LABEL);
list_box = new Gtk.ListBox () {
hexpand = true,
vexpand = true,
selection_mode = NONE
};
list_box.get_style_context ().add_class (Gtk.STYLE_CLASS_BACKGROUND);
list_box.set_placeholder (placeholder);
list_box.set_sort_func (message_sort_function);
list_box.set_header_func (message_header_func);
scrolled_window = new Gtk.ScrolledWindow (null, null) {
hscrollbar_policy = NEVER
};
scrolled_window.add (list_box);
// Prevent the focus of the webview causing the ScrolledWindow to scroll
var scrolled_child = scrolled_window.get_child ();
if (scrolled_child is Gtk.Container) {
((Gtk.Container) scrolled_child).set_focus_vadjustment (new Gtk.Adjustment (0, 0, 0, 0, 0, 0));
}
orientation = VERTICAL;
add (headerbar);
add (scrolled_window);
}
public void set_conversation (Camel.FolderThreadNode? node) {
/*
* Prevent the user from interacting with the message thread while it
* is being reloaded. can_reply will be set to true after loading the
* thread.
*/
can_reply (false);
can_move_thread (false);
list_box.get_children ().foreach ((child) => {
child.destroy ();
});
messages = new Gee.HashMap<string, MessageListItem> (null, null);
if (node == null) {
return;
}
/*
* If there is a node, we can move the thread even without loading all
* individual messages.
*/
can_move_thread (true);
var item = new MessageListItem (node.message);
list_box.add (item);
messages.set (node.message.uid, item);
if (node.child != null) {
go_down ((Camel.FolderThreadNode?) node.child);
}
var children = list_box.get_children ();
var num_children = children.length ();
if (num_children > 0) {
var child = list_box.get_row_at_index ((int) num_children - 1);
if (child != null && child is MessageListItem) {
var list_item = (MessageListItem) child;
list_item.expanded = true;
can_reply (list_item.loaded);
list_item.notify["loaded"].connect (() => {
can_reply (list_item.loaded);
});
}
}
if (node.message != null && Camel.MessageFlags.DRAFT in (int) node.message.flags) {
compose.begin (Composer.Type.DRAFT, "");
}
}
private void go_down (Camel.FolderThreadNode node) {
unowned Camel.FolderThreadNode? current_node = node;
while (current_node != null) {
var item = new MessageListItem (current_node.message);
list_box.add (item);
messages.set (current_node.message.uid, item);
if (current_node.next != null) {
go_down ((Camel.FolderThreadNode?) current_node.next);
}
current_node = (Camel.FolderThreadNode?) current_node.child;
}
}
public async void compose (Composer.Type type, Variant uid) {
/* Can't open a new composer if thread is empty*/
var last_child = list_box.get_row_at_index ((int) list_box.get_children ().length () - 1);
if (last_child == null) {
return;
}
MessageListItem message_item = null;
if (uid.get_string () == "") {
message_item = (MessageListItem) last_child;
} else {
message_item = messages.get (uid.get_string ());
}
string content_to_quote = "";
Camel.MimeMessage? mime_message = null;
Camel.MessageInfo? message_info = null;
content_to_quote = yield message_item.get_message_body_html ();
mime_message = message_item.mime_message;
message_info = message_item.message_info;
var composer = new Composer.with_quote (type, message_info, mime_message, content_to_quote);
composer.present ();
composer.finished.connect (() => {
can_reply (true);
can_move_thread (true);
});
can_reply (false);
can_move_thread (true);
}
public void print (Variant uid) {
messages.get (uid.get_string ()).print ();
}
private void can_reply (bool enabled) {
unowned var main_window = (Gtk.ApplicationWindow) get_toplevel ();
((SimpleAction) main_window.lookup_action (MainWindow.ACTION_FORWARD)).set_enabled (enabled);
((SimpleAction) main_window.lookup_action (MainWindow.ACTION_REPLY_ALL)).set_enabled (enabled);
((SimpleAction) main_window.lookup_action (MainWindow.ACTION_REPLY)).set_enabled (enabled);
}
private void can_move_thread (bool enabled) {
unowned var main_window = (Gtk.ApplicationWindow) get_toplevel ();
((SimpleAction) main_window.lookup_action (MainWindow.ACTION_ARCHIVE)).set_enabled (enabled);
((SimpleAction) main_window.lookup_action (MainWindow.ACTION_MARK)).set_enabled (enabled);
((SimpleAction) main_window.lookup_action (MainWindow.ACTION_MOVE_TO_TRASH)).set_enabled (enabled);
}
private void message_header_func (Gtk.ListBoxRow row, Gtk.ListBoxRow? before) {
unowned var message = (MessageListItem) row;
unowned var message_before = (MessageListItem) before;
var subject = message.message_info.subject;
if (message_before == null || message_before != null && sanitize_subject (message_before.message_info.subject) != sanitize_subject (subject)) {
if (subject == "") {
subject = _("No Subject");
}
var header_label = new Granite.HeaderLabel (subject) {
wrap = true,
};
row.set_header (header_label);
}
}
private string sanitize_subject (string _subject) {
var subject = _subject.down ();
subject = subject.replace (" ", "");
subject = subject.replace (":", "");
subject = subject.replace ("re", "");
subject = subject.replace ("fwd", "");
return subject;
}
private static int message_sort_function (Gtk.ListBoxRow item1, Gtk.ListBoxRow item2) {
unowned MessageListItem message1 = (MessageListItem)item1;
unowned MessageListItem message2 = (MessageListItem)item2;
var timestamp1 = message1.message_info.date_received;
if (timestamp1 == 0) {
timestamp1 = message1.message_info.date_sent;
}
var timestamp2 = message2.message_info.date_received;
if (timestamp2 == 0) {
timestamp2 = message2.message_info.date_sent;
}
return (int)(timestamp1 - timestamp2);
}
}