forked from projecthamster/hamster-shell-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
586 lines (457 loc) · 20.2 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* Simple Hamster extension for gnome-shell
* Copyright (c) 2011 Jerome Oufella <[email protected]>
* Copyright (c) 2011-2012 Toms Baugis <[email protected]>
* Icons Artwork Copyright (c) 2012 Reda Lazri <[email protected]>
*
* Portions originate from the gnome-shell source code, Copyright (c)
* its respectives authors.
* This project is released under the GNU GPL License.
* See COPYING for details.
*
*/
// TODO - investigate usage of third party libs (d3/underscore/whatever)
// otherwise even most primitive operations are hardcore
const Clutter = imports.gi.Clutter;
const Config = imports.misc.config;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const St = imports.gi.St;
const Shell = imports.gi.Shell;
const Meta = imports.gi.Meta;
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const Gio = imports.gi.Gio;
const PopupMenu = imports.ui.popupMenu;
const PanelMenu = imports.ui.panelMenu;
const Util = imports.misc.util;
const Gettext = imports.gettext.domain('hamster-shell-extension');
const _ = Gettext.gettext;
const N_ = function(x) { return x; };
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
const Stuff = Me.imports.stuff;
// dbus-send --session --type=method_call --print-reply --dest=org.gnome.Hamster /org/gnome/Hamster org.freedesktop.DBus.Introspectable.Introspect
const ApiProxyIface = '<node> \
<interface name="org.gnome.Hamster"> \
<method name="GetTodaysFacts"> \
<arg direction="out" type="a(iiissisasii)" /> \
</method> \
<method name="StopTracking"> \
<arg direction="in" type="v" name="end_time" /> \
</method> \
<method name="AddFact"> \
<arg direction="in" type="s" name="fact" /> \
<arg direction="in" type="i" name="start_time" /> \
<arg direction="in" type="i" name="end_time" /> \
<arg direction="in" type="b" name="temporary" /> \
<arg direction="out" type="i" /> \
</method> \
<method name="GetActivities"> \
<arg direction="in" type="s" name="search" /> \
<arg direction="out" type="a(ss)" /> \
</method> \
<signal name="FactsChanged"></signal> \
<signal name="ActivitiesChanged"></signal> \
<signal name="TagsChanged"></signal> \
</interface> \
</node>';
let ApiProxy = Gio.DBusProxy.makeProxyWrapper(ApiProxyIface);
// dbus-send --session --type=method_call --print-reply --dest=org.gnome.Hamster.WindowServer /org/gnome/Hamster/WindowServer org.freedesktop.DBus.Introspectable.Introspect
const WindowsProxyIface = '<node> \
<interface name="org.gnome.Hamster.WindowServer"> \
<method name="edit"> \
<arg direction="in" type="v" name="id" /> \
</method> \
<method name="overview"></method> \
<method name="preferences"></method> \
</interface> \
</node>';
let WindowsProxy = Gio.DBusProxy.makeProxyWrapper(WindowsProxyIface);
/* a little box or something */
function HamsterBox() {
this._init.apply(this, arguments);
}
HamsterBox.prototype = {
__proto__: PopupMenu.PopupBaseMenuItem.prototype,
_init: function(itemParams) {
PopupMenu.PopupBaseMenuItem.prototype._init.call(this, {reactive: false});
let box = new St.BoxLayout({style_class: 'hamster-box'});
box.set_vertical(true);
let label = new St.Label({style_class: 'hamster-box-label'});
label.set_text(_("What are you doing?"));
box.add(label);
this._textEntry = new St.Entry({name: 'searchEntry',
can_focus: true,
track_hover: true,
hint_text: _("Enter activity...")});
this._textEntry.clutter_text.connect('activate', Lang.bind(this, this._onEntryActivated));
this._textEntry.clutter_text.connect('key-release-event', Lang.bind(this, this._onKeyReleaseEvent));
box.add(this._textEntry);
// autocomplete popup - couldn't spark it up just yet
//this._popup = new PopupMenu.PopupComboMenu(this._textEntry);
label = new St.Label({style_class: 'hamster-box-label'});
label.set_text(_("Today's activities"));
box.add(label);
let scrollbox = new St.ScrollView({style_class: 'hamster-scrollbox'});
this._scrollAdjustment = scrollbox.vscroll.adjustment;
box.add(scrollbox);
// Since St.Table does not implement StScrollable, we create a
// container object that does.
let container = new St.BoxLayout({});
container.set_vertical(true);
scrollbox.add_actor(container);
this.activities = new St.Widget({style_class: 'hamster-activities',
layout_manager: new Clutter.TableLayout(),
reactive: true });
container.add(this.activities);
this.summaryLabel = new St.Label({style_class: 'summary-label'});
box.add(this.summaryLabel);
this.actor.add_child(box);
this.autocompleteActivities = [];
this.runningActivitiesQuery = null;
this._prevText = "";
},
focus: function() {
Mainloop.timeout_add(20, Lang.bind(this, function() {
// scroll the activities to the bottom
this._scrollAdjustment.value = this._scrollAdjustment.upper;
// focus the text entry
global.stage.set_key_focus(this._textEntry);
}));
},
blur: function() {
global.stage.set_key_focus(null);
},
_onEntryActivated: function() {
this.emit('activate');
this._textEntry.set_text('');
},
_getActivities: function() {
if (this.runningActivitiesQuery)
return this.autocompleteActivities;
this.runningActivitiesQuery = true;
this.proxy.GetActivitiesRemote("", Lang.bind(this, function([response], err) {
this.runningActivitiesQuery = false;
this.autocompleteActivities = response;
}));
return this.autocompleteActivities;
},
_onKeyReleaseEvent: function(textItem, evt) {
let symbol = evt.get_key_symbol();
let text = this._textEntry.get_text().toLowerCase();
let starttime = "";
let activitytext = text;
// Don't include leading times in the activity autocomplete
let match = [];
if ((match = text.match(/^\d\d:\d\d /)) ||
(match = text.match(/^-\d+ /))) {
starttime = text.substring(0, match[0].length);
activitytext = text.substring(match[0].length);
}
// if nothing has changed or we still have selection then that means
// that special keys are at play and we don't attempt to autocomplete
if (activitytext == "" ||
this._prevText == text ||
this._textEntry.clutter_text.get_selection()) {
return;
}
this._prevText = text;
// ignore deletions
let ignoreKeys = [Clutter.BackSpace, Clutter.Delete, Clutter.Escape];
for (var key of ignoreKeys) {
if (symbol == key)
return;
}
let allActivities = this._getActivities();
for (var rec of allActivities) {
let completion = rec[0];
if (rec[1].length > 0)
completion += "@" + rec[1];
if (completion.toLowerCase().substring(0, activitytext.length) == activitytext) {
this.prevText = text;
completion = starttime + completion;
this._textEntry.set_text(completion);
this._textEntry.clutter_text.set_selection(text.length, completion.length);
this._prevText = completion.toLowerCase();
return;
}
}
}
};
/* Panel button */
function HamsterExtension(extensionMeta) {
this._init(extensionMeta);
}
HamsterExtension.prototype = {
__proto__: PanelMenu.Button.prototype,
_init: function(extensionMeta) {
PanelMenu.Button.prototype._init.call(this, 0.0);
this.extensionMeta = extensionMeta;
this._proxy = new ApiProxy(Gio.DBus.session, 'org.gnome.Hamster', '/org/gnome/Hamster');
this._proxy.connectSignal('FactsChanged', Lang.bind(this, this.refresh));
this._proxy.connectSignal('ActivitiesChanged', Lang.bind(this, this.refreshActivities));
this._proxy.connectSignal('TagsChanged', Lang.bind(this, this.refresh));
this._windowsProxy = new WindowsProxy(Gio.DBus.session,
"org.gnome.Hamster.WindowServer",
"/org/gnome/Hamster/WindowServer");
this._settings = Convenience.getSettings();
this.panelContainer = new St.BoxLayout({style_class: "panel-box"});
this.actor.add_actor(this.panelContainer);
this.actor.add_style_class_name('panel-status-button');
this.panelLabel = new St.Label({text: _("Loading..."),
y_align: Clutter.ActorAlign.CENTER});
this.currentActivity = null;
// panel icon
this._trackingIcon = Gio.icon_new_for_string(this.extensionMeta.path + "/images/hamster-tracking-symbolic.svg");
this._idleIcon = Gio.icon_new_for_string(this.extensionMeta.path + "/images/hamster-idle-symbolic.svg");
this.icon = new St.Icon({gicon: this._trackingIcon,
icon_size: 16,
style_class: "panel-icon"});
this.panelContainer.add(this.icon);
this.panelContainer.add(this.panelLabel);
let item = new HamsterBox();
item.connect('activate', Lang.bind(this, this._onActivityEntry));
this.activityEntry = item;
this.activityEntry.proxy = this._proxy; // lazy proxying
this.menu.addMenuItem(item);
// overview
item = new PopupMenu.PopupMenuItem(_("Show Overview"));
item.connect('activate', Lang.bind(this, this._onShowHamsterActivate));
this.menu.addMenuItem(item);
// stop tracking
item = new PopupMenu.PopupMenuItem(_("Stop Tracking"));
item.connect('activate', Lang.bind(this, this._onStopTracking));
this.menu.addMenuItem(item);
// add new task
item = new PopupMenu.PopupMenuItem(_("Add Earlier Activity"));
item.connect('activate', Lang.bind(this, this._onNewFact));
this.menu.addMenuItem(item);
// settings
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
item = new PopupMenu.PopupMenuItem(_("Tracking Settings"));
item.connect('activate', Lang.bind(this, this._onShowSettingsActivate));
this.menu.addMenuItem(item);
// focus menu upon display
this.menu.connect('open-state-changed', Lang.bind(this,
function(menu, open) {
if (open) {
this.activityEntry.focus();
} else {
this.activityEntry.blur();
}
}
));
// load data
this.facts = null;
// refresh the label every 60 secs
this.timeout = GLib.timeout_add_seconds(0, 60, Lang.bind(this, this.refresh));
this.refresh();
},
show: function() {
this.menu.open();
},
toggle: function() {
this.menu.toggle();
},
refreshActivities: function(proxy, sender) {
this.activityEntry.autocompleteActivities = [];
this.refresh();
},
refresh: function(proxy, sender) {
this._proxy.GetTodaysFactsRemote(Lang.bind(this, this._refresh));
return true;
},
_refresh: function([response], err) {
let facts = [];
if (err) {
log(err);
} else if (response.length > 0) {
facts = Stuff.fromDbusFacts(response);
}
this.currentActivity = null;
let fact = null;
if (facts.length) {
fact = facts[facts.length - 1];
if (!fact.endTime)
this.currentActivity = fact;
}
this.updatePanelDisplay(fact);
let activities = this.activityEntry.activities;
activities.destroy_all_children(); // remove previous entries
var i = 0;
let layout = activities.layout_manager;
for (var fact of facts) {
let label;
label = new St.Label({style_class: 'cell-label'});
let text = "%02d:%02d - ".format(fact.startTime.getHours(), fact.startTime.getMinutes());
if (fact.endTime) {
text += "%02d:%02d".format(fact.endTime.getHours(), fact.endTime.getMinutes());
}
label.set_text(text);
layout.pack(label, 0, i);
label = new St.Label({style_class: 'cell-label'});
label.set_text(fact.name + (0 < fact.tags.length ? (" #" + fact.tags.join(", #")) : ""));
layout.pack(label, 1, i);
label = new St.Label({style_class: 'cell-label'});
label.set_text(Stuff.formatDurationHuman(fact.delta));
layout.pack(label, 2, i);
let icon;
let button;
button = new St.Button({style_class: 'clickable cell-button'});
icon = new St.Icon({icon_name: "document-open-symbolic",
icon_size: 16});
button.set_child(icon);
button.fact = fact;
button.connect('clicked', Lang.bind(this, function(button, event) {
this._windowsProxy.editSync(GLib.Variant.new('i', [button.fact.id]));
this.menu.close();
}));
layout.pack(button, 3, i);
if (!this.currentActivity ||
this.currentActivity.name != fact.name ||
this.currentActivity.category != fact.category ||
this.currentActivity.tags.join(",") != fact.tags.join(",")) {
button = new St.Button({style_class: 'clickable cell-button'});
icon = new St.Icon({icon_name: "media-playback-start-symbolic",
icon_size: 16});
button.set_child(icon);
button.fact = fact;
button.connect('clicked', Lang.bind(this, function(button, event) {
let factStr = button.fact.name
+ "@" + button.fact.category
+ ", " + (button.fact.description);
if (button.fact.tags.length) {
factStr += " #" + button.fact.tags.join(", #");
}
this._proxy.AddFactRemote(factStr, 0, 0, false, Lang.bind(this, function(response, err) {
// not interested in the new id - this shuts up the warning
}));
this.menu.close();
}));
layout.pack(button, 4, i);
}
i += 1;
}
let byCategory = {};
let categories = [];
for (var fact of facts) {
byCategory[fact.category] = (byCategory[fact.category] || 0) + fact.delta;
if (categories.indexOf(fact.category) == -1)
categories.push(fact.category);
}
let label = "";
for (var category of categories) {
label += category + ": " + Stuff.formatDurationHours(byCategory[category]) + ", ";
}
label = label.slice(0, label.length - 2); // strip trailing comma
this.activityEntry.summaryLabel.set_text(label);
},
updatePanelDisplay: function(fact) {
// 0 = show label, 1 = show icon + duration, 2 = just icon
let appearance = this._settings.get_int("panel-appearance");
if (appearance === 0) {
this.panelLabel.show();
this.icon.hide();
if (fact && !fact.endTime) {
this.panelLabel.text = "%s %s".format(fact.name, Stuff.formatDuration(fact.delta));
} else {
this.panelLabel.text = _("No activity");
}
} else {
this.icon.show();
if (appearance == 1)
this.panelLabel.hide();
else
this.panelLabel.show();
// updates panel label. if fact is none, will set panel status to "no activity"
if (fact && !fact.endTime) {
this.panelLabel.text = Stuff.formatDuration(fact.delta);
this.icon.gicon = this._trackingIcon;
} else {
this.panelLabel.text = "";
this.icon.gicon = this._idleIcon;
}
}
},
_onStopTracking: function() {
let now = new Date();
let epochSeconds = Date.UTC(now.getFullYear(),
now.getMonth(),
now.getDate(),
now.getHours(),
now.getMinutes(),
now.getSeconds());
epochSeconds = Math.floor(epochSeconds / 1000);
this._proxy.StopTrackingRemote(GLib.Variant.new('i', [epochSeconds]));
},
_onShowHamsterActivate: function() {
this._windowsProxy.overviewSync();
},
_onNewFact: function() {
this._windowsProxy.editSync(GLib.Variant.new('i', [0]));
},
_onShowSettingsActivate: function() {
this._windowsProxy.preferencesSync();
},
_onActivityEntry: function() {
let text = this.activityEntry._textEntry.get_text();
this._proxy.AddFactRemote(text, 0, 0, false, Lang.bind(this, function(response, err) {
// not interested in the new id - this shuts up the warning
}));
}
};
function ExtensionController(extensionMeta) {
let dateMenu = Main.panel.statusArea.dateMenu;
return {
extensionMeta: extensionMeta,
extension: null,
settings: null,
placement: 0,
activitiesText: null,
enable: function() {
this.settings = Convenience.getSettings();
this.extension = new HamsterExtension(this.extensionMeta);
this.placement = this.settings.get_int("panel-placement");
if (this.placement == 1) {
Main.panel.addToStatusArea("hamster", this.extension, 0, "center");
Main.panel._centerBox.remove_actor(dateMenu.container);
Main.panel._addToPanelBox('dateMenu', dateMenu, -1, Main.panel._rightBox);
} else if (this.placement == 2) {
this._activitiesText = Main.panel._leftBox.get_children()[0].get_children()[0].get_children()[0].get_children()[0].get_text();
Main.panel._leftBox.get_children()[0].get_children()[0].get_children()[0].get_children()[0].set_text('');
Main.panel.addToStatusArea("hamster", this.extension, 1, "left");
} else {
Main.panel.addToStatusArea("hamster", this.extension, 0, "right");
}
Main.panel.menuManager.addMenu(this.extension.menu);
Main.wm.addKeybinding("show-hamster-dropdown",
this.extension._settings,
Meta.KeyBindingFlags.NONE,
//since Gnome 3.16, Shell.KeyBindingMode is replaced by Shell.ActionMode
Shell.KeyBindingMode ? Shell.KeyBindingMode.ALL : Shell.ActionMode.ALL,
Lang.bind(this.extension, this.extension.toggle)
);
},
disable: function() {
Main.wm.removeKeybinding("show-hamster-dropdown");
if (this.placement == 1) {
Main.panel._rightBox.remove_actor(dateMenu.container);
Main.panel._addToPanelBox('dateMenu', dateMenu, Main.sessionMode.panel.center.indexOf('dateMenu'), Main.panel._centerBox);
} else if (this.placement == 2) {
Main.panel._leftBox.get_children()[0].get_children()[0].get_children()[0].get_children()[0].set_text(this._activitiesText);
}
Main.panel.menuManager.removeMenu(this.extension.menu);
GLib.source_remove(this.extension.timeout);
this.extension.actor.destroy();
this.extension.destroy();
this.extension = null;
}
};
}
function init(extensionMeta) {
Convenience.initTranslations("hamster-shell-extension");
return new ExtensionController(extensionMeta);
}