-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension.js
319 lines (294 loc) · 10 KB
/
extension.js
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
/*-*- coding: utf-8 -*-
* ---------------------------------------------------------------------------
* Gettings Things Gnome! - a personal organizer for the GNOME desktop
* Copyright (c) 2011 - Luca Invernizzi
*
* 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, see <http://www.gnu.org/licenses/>.
* --------------------------------------------------------------------------- */
/* Gnome Shell extension that integrates GTG into the calendar panel */
const St = imports.gi.St;
const Main = imports.ui.main;
const Lang = imports.lang;
const Clutter = imports.gi.Clutter;
const DBus = imports.dbus;
const PopupMenu = imports.ui.popupMenu;
/****************************************************************************
* Helpers
***************************************************************************/
/* The equivalent of functools.partial in Python :) */
function partial(func /*, 0..n args */) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
var allArguments = args.concat(Array.prototype.slice.call(arguments));
return func.apply(this, allArguments);
}
}
/* Handles the vertical separator look
* Function borrowed from dateTime.js */
function _onVertSepRepaint(area) {
let cr = area.get_context();
let themeNode = area.get_theme_node();
let [width, height] = area.get_surface_size();
let stippleColor = themeNode.get_color('-stipple-color');
let stippleWidth = themeNode.get_length('-stipple-width');
let x = Math.floor(width/2) + 0.5;
cr.moveTo(x, 0);
cr.lineTo(x, height);
Clutter.cairo_set_source_color(cr, stippleColor);
cr.setDash([1, 3], 1); // Hard-code for now
cr.setLineWidth(stippleWidth);
cr.stroke();
}
/****************************************************************************
* DBus
***************************************************************************/
const GtgIFace = {
name: 'org.gnome.GTG',
methods: [{name: 'GetActiveTasks',
inSignature: 'as',
outSignature: 'aa{sv}' },
{name: 'ShowTaskBrowser',
inSignature: '',
outSignature: '' },
{name: 'GetTask',
inSignature: 's',
outSignature: 'aa{sv}' },
{name: 'OpenTaskEditor',
inSignature: 's',
outSignature: '' }],
signals: [{name: 'TaskAdded',
inSignature: 's'},
{name: 'TaskModified',
inSignature: 's'},
{name: 'TaskDeleted',
inSignature: 's'}]
};
const Gtg = DBus.makeProxyClass(GtgIFace);
const gtg = new Gtg(DBus.session, 'org.gnome.GTG', '/org/gnome/GTG');
function getActiveTasks(tags, callback) {
function handler(results, error) {
if (error != null)
global.log("Error retrieving GTG tasks: "+error);
else
callback(results);
}
gtg.GetActiveTasksRemote(tags, handler);
}
function getTask(tid, callback) {
function handler(task, error) {
if (error != null)
global.log("Error retrieving GTG tasks: "+error);
else
callback(task);
}
gtg.GetTaskRemote(tid, handler);
}
function openTaskEditor(task_id) {
gtg.OpenTaskEditorRemote(task_id);
}
function showTaskBrowser() {
gtg.ShowTaskBrowserRemote();
}
function onTaskClicked(task_id) {
Main.panel._dateMenu.menu.close();
openTaskEditor(task_id);
}
/****************************************************************************
* Extension core
***************************************************************************/
let K = 10;
let gtgBox, tasksBox, topKTasks, more_tasks_button;
/* Returns a score on the importance of the task. The lower the score,
* the higher the importance.
* The top K tasks, in descending order of importance, will be displayed
*/
function _getTaskScore(task) {
let time = Date.parse(task.duedate);
let score = new Date();
if (isNaN(time)) {
let fuzzy_date_discount = {'now': -3, 'soon': -2, 'later': +1}[task.duedate];
let year = 2037; /* oh, 32-bit epoch! */
if (! isNaN(fuzzy_date_discount)) {
year += fuzzy_date_discount;
}
score.setYear(year);
} else {
score.setTime(time);
task.due_today = true;
}
return score;
}
/* Inserts a task button in the UI (if it's one of the top K)
*/
function _insertTaskAtIndex(index, task) {
topKTasks.splice(index, 0, task);
if (index <= K) {
/* if the task is due soon, add it to the UI */
task.button = _prepareTaskButton(task);
tasksBox.insert_actor(task.button, index, {expand: true});
}
}
/* Creates a task button
*/
function _prepareTaskButton(task) {
let title = task.title.substr(0, 70);
if (title.length != task.title.length) title += '...';
let style_class = 'events-day-task';
if (task.due_today) {style_class += ' due-today';}
let task_button = new PopupMenu.PopupMenuItem(title,
{style_class: style_class});
task_button.connect('activate', partial(onTaskClicked, task.id));
return task_button.actor;
}
/* Handles the TaskAdded, TaskModified signals
*/
function onTaskAddedOrModified(task) {
/* Delete all tasks with the same id in the UI */
while (onTaskDeleted(task.id)){}
if (task.status != 'Active') {
return;
}
/* compute task score */
task.score = _getTaskScore(task);
/* sorted insert */
let index = 0;
let inserted = false;
while (index < topKTasks.length) {
if (task.score <= topKTasks[index].score) {
_insertTaskAtIndex(index, task);
inserted = true;
break;
}
index += 1;
}
/* if the K have not been found yet, append */
if (index < K && ! inserted) {
_insertTaskAtIndex(index, task);
}
/* delete any element after K */
for (index in topKTasks.slice(K + 1)) {
if (task.button) {
task.button.destroy();
task.button = null;
}
}
/* if more than K tasks, add the [..] symbol */
if(topKTasks.length > K && ! more_tasks_button) {
more_tasks_button = new PopupMenu.PopupMenuItem('[...]',
{style_class: 'events-day-task'})
tasksBox.add(more_tasks_button.actor, -1, {expand: true});
}
}
/* Handles the TaskDeleted signal
*/
function onTaskDeleted(tid) {
let index = 0;
let deleted = false;
while (index < topKTasks.length) {
let task = topKTasks[index];
if (task.id == tid) {
if (task.button) {
task.button.destroy();
task.button = null;
}
deleted = true;
break;
}
index += 1;
}
if (deleted) {
topKTasks.splice(index, 1);
}
if (topKTasks.length <= K && more_tasks_button) {
more_tasks_button.destroy();
more_tasks_button = null;
}
return deleted;
}
/* Performs a full refresh of all tasks (useful when Gnome-Shell
* is started)
*/
function refreshAllTasks() {
topKTasks = new Array();
getActiveTasks(['@all'], function (tasks) {
for (var i in tasks) {
onTaskAddedOrModified(tasks[i]);
}
});
}
/****************************************************************************
* Extension interface
***************************************************************************/
let signal_added_handle, signal_modified_handle, signal_deleted_handle;
let separator;
/* Initialization */
function init() {
}
/* Extension disabling */
function disable() {
try {
gtgBox.destroy();
separator.destroy();
gtg.disconnect(signal_added_handle);
gtg.disconnect(signal_modified_handle);
gtg.disconnect(signal_deleted_handle);
} catch (err) {
}
}
/*Extension enabling */
function enable() {
/* Add GTG widget */
function getChildByName (a_parent, name) {
return a_parent.get_children().filter(
function(elem){
return elem.name == name
})[0];
}
let calendarArea = getChildByName(Main.panel._dateMenu.menu.box, 'calendarArea');
separator = new St.DrawingArea({style_class: 'calendar-vertical-separator',
pseudo_class: 'highlighted' });
separator.connect('repaint', Lang.bind(this, _onVertSepRepaint));
calendarArea.add_actor(separator);
gtgBox = new St.BoxLayout();
gtgBox.set_vertical(true);
calendarArea.add_actor(gtgBox, {expand: true});
tasksBox = new St.BoxLayout();
tasksBox.set_vertical(true);
gtgBox.add(tasksBox, {style_class: 'calendar'});
/* Add "Open GTG" button */
let open_gtg_button = new PopupMenu.PopupMenuItem("Open GTG");
open_gtg_button.connect('activate', function () {
Main.panel._dateMenu.menu.close();
showTaskBrowser();
});
open_gtg_button.actor.can_focus = false;
gtgBox.add(open_gtg_button.actor,
{y_align: St.Align.END,
expand: true,
y_fill: false});
/* start listening for tasks */
refreshAllTasks();
signal_added_handle = gtg.connect(
'TaskAdded', function(sender, tid) {
getTask(tid, onTaskAddedOrModified);
});
signal_modified_handle = gtg.connect(
'TaskModified', function(sender, tid) {
getTask(tid, onTaskAddedOrModified);
});
signal_deleted_handle = gtg.connect(
'TaskDeleted', function(sender, tid) {
onTaskDeleted(tid);
});
}