-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
TaskListGrid.vala
423 lines (358 loc) · 16.5 KB
/
TaskListGrid.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
/*
* Copyright 2019 elementary, Inc. (https://elementary.io)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
*/
public class Tasks.Widgets.TaskListGrid : Gtk.Grid {
public E.Source source { get; construct; }
private ECal.ClientView view;
private EditableLabel editable_title;
private Gtk.ListBox add_task_list;
private Gtk.ListBox task_list;
private bool is_gtasks;
public TaskListGrid (E.Source source) {
Object (source: source);
}
construct {
try {
view = Tasks.Application.model.create_task_list_view (
source,
"(contains? 'any' '')",
on_tasks_added,
on_tasks_modified,
on_tasks_removed );
} catch (Error e) {
critical ("Error creating view for source %s: %s", source.display_name, e.message);
}
E.SourceRegistry? registry = null;
try {
registry = Application.model.get_registry_sync ();
is_gtasks = Application.model.get_collection_backend_name (source, registry) == "google";
} catch (Error e) {
warning ("unable to get the registry, assuming task list is not from gtasks");
}
editable_title = new EditableLabel () {
margin_start = 24
};
unowned Gtk.StyleContext title_context = editable_title.get_style_context ();
title_context.add_class (Granite.STYLE_CLASS_H1_LABEL);
title_context.add_class (Granite.STYLE_CLASS_ACCENT);
var list_settings_popover = new Tasks.Widgets.ListSettingsPopover ();
var settings_button = new Gtk.MenuButton () {
margin_end = 24,
valign = Gtk.Align.CENTER,
tooltip_text = _("Edit Name and Appearance"),
popover = list_settings_popover,
image = new Gtk.Image.from_icon_name ("view-more-symbolic", Gtk.IconSize.MENU)
};
settings_button.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
settings_button.get_style_context ().add_class (Gtk.STYLE_CLASS_DIM_LABEL);
var placeholder = new Gtk.Label (_("No Tasks"));
placeholder.show ();
unowned Gtk.StyleContext placeholder_context = placeholder.get_style_context ();
placeholder_context.add_class (Gtk.STYLE_CLASS_DIM_LABEL);
placeholder_context.add_class (Granite.STYLE_CLASS_H2_LABEL);
add_task_list = new Gtk.ListBox () {
margin_top = 24,
selection_mode = Gtk.SelectionMode.SINGLE,
activate_on_single_click = true
};
add_task_list.get_style_context ().add_class (Gtk.STYLE_CLASS_BACKGROUND);
var add_task_row = new Tasks.Widgets.TaskRow.for_source (source);
add_task_row.unselect.connect (on_row_unselect);
add_task_row.task_changed.connect ((task) => {
Tasks.Application.model.add_task.begin (source, task, (obj, res) => {
GLib.Idle.add (() => {
try {
Tasks.Application.model.add_task.end (res);
} catch (Error e) {
var error_dialog = new Granite.MessageDialog (
_("Adding task failed"),
_("The task list registry may be unavailable or unable to be written to."),
new ThemedIcon ("dialog-error"),
Gtk.ButtonsType.CLOSE
);
error_dialog.show_error_details (e.message);
error_dialog.run ();
error_dialog.destroy ();
}
return GLib.Source.REMOVE;
});
});
});
add_task_list.add (add_task_row);
task_list = new Gtk.ListBox () {
selection_mode = Gtk.SelectionMode.MULTIPLE,
activate_on_single_click = true
};
task_list.set_filter_func (filter_function);
task_list.set_placeholder (placeholder);
task_list.set_sort_func (sort_function);
task_list.get_style_context ().add_class (Gtk.STYLE_CLASS_BACKGROUND);
var scrolled_window = new Gtk.ScrolledWindow (null, null) {
expand = true,
hscrollbar_policy = Gtk.PolicyType.NEVER
};
scrolled_window.add (task_list);
column_spacing = 12;
attach (editable_title, 0, 0);
attach (settings_button, 1, 0);
attach (add_task_list, 0, 1, 2);
attach (scrolled_window, 0, 2, 2);
Application.settings.changed["show-completed"].connect (() => {
task_list.invalidate_filter ();
});
settings_button.toggled.connect (() => {
unowned GLib.ActionMap win_action_map = (GLib.ActionMap) get_action_group (MainWindow.ACTION_GROUP_PREFIX);
if (settings_button.active) {
list_settings_popover.source = source;
if (win_action_map != null) {
((SimpleAction) win_action_map.lookup_action (MainWindow.ACTION_DELETE_SELECTED_LIST)).set_enabled (true);
}
} else if (win_action_map != null) {
/*
* We can't immediate disable the action once the popover is closed,
* because this would lead to the action not beeing executed in case
* the popover was closed because the user clicked on the action.
* Therefore we wait a tiny bit using GLib.Idle to allow the action to
* be executed if needed.
*/
GLib.Idle.add (() => {
((SimpleAction) win_action_map.lookup_action (MainWindow.ACTION_DELETE_SELECTED_LIST)).set_enabled (
add_task_list.get_selected_rows ().length () == 0 &&
task_list.get_selected_rows ().length () == 0
);
return GLib.Source.REMOVE;
});
}
});
add_task_list.row_activated.connect (on_row_activated);
task_list.row_activated.connect (on_row_activated);
editable_title.notify["editing"].connect (() => {
unowned GLib.ActionMap win_action_map = (GLib.ActionMap) get_action_group (MainWindow.ACTION_GROUP_PREFIX);
if (win_action_map != null) {
((SimpleAction) win_action_map.lookup_action (MainWindow.ACTION_DELETE_SELECTED_LIST)).set_enabled (!editable_title.editing);
}
});
editable_title.changed.connect (() => {
Application.model.update_task_list_display_name.begin (source, editable_title.text, (obj, res) => {
GLib.Idle.add (() => {
try {
Application.model.update_task_list_display_name.end (res);
} catch (Error e) {
editable_title.text = source.display_name;
var error_dialog = new Granite.MessageDialog (
_("Renaming task list failed"),
_("The task list registry may be unavailable or unable to be written to."),
new ThemedIcon ("dialog-error"),
Gtk.ButtonsType.CLOSE
);
error_dialog.show_error_details (e.message);
error_dialog.run ();
error_dialog.destroy ();
}
return GLib.Source.REMOVE;
});
});
});
show_all ();
}
private void on_row_activated (Gtk.ListBoxRow row) {
var task_row = (Tasks.Widgets.TaskRow) row;
task_row.reveal_child_request (true);
unowned GLib.ActionMap win_action_map = (GLib.ActionMap) get_action_group (MainWindow.ACTION_GROUP_PREFIX);
if (win_action_map != null) {
((SimpleAction) win_action_map.lookup_action (MainWindow.ACTION_DELETE_SELECTED_LIST)).set_enabled (false);
}
}
private void on_row_unselect (Gtk.ListBoxRow row) {
if (row.parent is Gtk.ListBox) {
((Gtk.ListBox) row.parent).unselect_row (row);
}
if (add_task_list.get_selected_rows ().length () == 0 && task_list.get_selected_rows ().length () == 0) {
unowned GLib.ActionMap win_action_map = (GLib.ActionMap) get_action_group (MainWindow.ACTION_GROUP_PREFIX);
if (win_action_map != null) {
((SimpleAction) win_action_map.lookup_action (MainWindow.ACTION_DELETE_SELECTED_LIST)).set_enabled (true);
}
}
}
public void update_request () {
editable_title.text = source.dup_display_name ();
Tasks.Application.set_task_color (source, editable_title);
task_list.@foreach ((row) => {
if (row is Tasks.Widgets.TaskRow) {
var task_row = (row as Tasks.Widgets.TaskRow);
task_row.update_request ();
}
});
}
[CCode (instance_pos = -1)]
private bool filter_function (Gtk.ListBoxRow row) {
if (
Application.settings.get_boolean ("show-completed") == false &&
((TaskRow) row).completed
) {
return false;
}
return true;
}
[CCode (instance_pos = -1)]
private int sort_function (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) {
var row_a = (Tasks.Widgets.TaskRow) row1;
var row_b = (Tasks.Widgets.TaskRow) row2;
if (row_a.completed == row_b.completed) {
if (is_gtasks) {
var gtask_position_a = Util.get_gtasks_position_property_value (row_a.task);
var gtask_position_b = Util.get_gtasks_position_property_value (row_b.task);
if (gtask_position_a == gtask_position_b) {
return row_b.task.get_last_modified ().compare (row_a.task.get_last_modified ());
}
return gtask_position_a.collate (gtask_position_b);
} else {
var apple_sortorder_a = Util.get_apple_sortorder_property_value (row_a.task);
if (apple_sortorder_a == null) {
apple_sortorder_a = Util.get_apple_sortorder_default_value (row_a.task).as_int ().to_string ();
}
var apple_sortorder_b = Util.get_apple_sortorder_property_value (row_b.task);
if (apple_sortorder_b == null) {
apple_sortorder_b = Util.get_apple_sortorder_default_value (row_b.task).as_int ().to_string ();
}
return apple_sortorder_a.collate (apple_sortorder_b);
}
} else if (row_a.completed && !row_b.completed) {
return 1;
} else if (row_b.completed && !row_a.completed) {
return -1;
}
return 0;
}
private void on_tasks_added (Gee.Collection<ECal.Component> tasks, E.Source source) {
tasks.foreach ((task) => {
var task_row = new Tasks.Widgets.TaskRow.for_component (task, source, false);
task_row.unselect.connect (on_row_unselect);
task_row.task_completed.connect ((task) => {
Tasks.Application.model.complete_task.begin (source, task, (obj, res) => {
GLib.Idle.add (() => {
try {
Tasks.Application.model.complete_task.end (res);
} catch (Error e) {
var error_dialog = new Granite.MessageDialog (
_("Completing task failed"),
_("The task registry may be unavailable or unable to be written to."),
new ThemedIcon ("dialog-error"),
Gtk.ButtonsType.CLOSE
);
error_dialog.show_error_details (e.message);
error_dialog.run ();
error_dialog.destroy ();
}
return GLib.Source.REMOVE;
});
});
});
task_row.task_changed.connect ((task) => {
Tasks.Application.model.update_task.begin (source, task, ECal.ObjModType.THIS_AND_FUTURE, (obj, res) => {
GLib.Idle.add (() => {
try {
Tasks.Application.model.update_task.end (res);
} catch (Error e) {
var error_dialog = new Granite.MessageDialog (
_("Updating task failed"),
_("The task registry may be unavailable or unable to be written to."),
new ThemedIcon ("dialog-error"),
Gtk.ButtonsType.CLOSE
);
error_dialog.show_error_details (e.message);
error_dialog.run ();
error_dialog.destroy ();
}
return GLib.Source.REMOVE;
});
});
});
task_row.task_removed.connect ((task) => {
Tasks.Application.model.remove_task.begin (source, task, ECal.ObjModType.ALL, (obj, res) => {
GLib.Idle.add (() => {
try {
Tasks.Application.model.remove_task.end (res);
} catch (Error e) {
var error_dialog = new Granite.MessageDialog (
_("Removing task failed"),
_("The task registry may be unavailable or unable to be written to."),
new ThemedIcon ("dialog-error"),
Gtk.ButtonsType.CLOSE
);
error_dialog.show_error_details (e.message);
error_dialog.run ();
error_dialog.destroy ();
}
return GLib.Source.REMOVE;
});
});
});
task_list.add (task_row);
return true;
});
Idle.add (() => {
task_list.invalidate_sort ();
task_list.show_all ();
return Source.REMOVE;
});
}
private void on_tasks_modified (Gee.Collection<ECal.Component> tasks) {
Tasks.Widgets.TaskRow task_row = null;
var row_index = 0;
do {
task_row = (Tasks.Widgets.TaskRow) task_list.get_row_at_index (row_index);
if (task_row != null) {
foreach (ECal.Component task in tasks) {
if (Util.calcomponent_equal_func (task_row.task, task)) {
task_row.task = task;
break;
}
}
}
row_index++;
} while (task_row != null);
Idle.add (() => {
task_list.invalidate_sort ();
return Source.REMOVE;
});
}
private void on_tasks_removed (SList<ECal.ComponentId?> cids) {
unowned Tasks.Widgets.TaskRow? task_row = null;
var row_index = 0;
do {
task_row = (Tasks.Widgets.TaskRow) task_list.get_row_at_index (row_index);
if (task_row != null) {
foreach (unowned ECal.ComponentId cid in cids) {
if (cid == null) {
continue;
} else if (cid.get_uid () == task_row.task.get_icalcomponent ().get_uid ()) {
task_list.remove (task_row);
break;
}
}
}
row_index++;
} while (task_row != null);
Idle.add (() => {
task_list.invalidate_sort ();
return Source.REMOVE;
});
}
}