-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhamster-indicator
executable file
·325 lines (270 loc) · 12.4 KB
/
hamster-indicator
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
#!/usr/bin/python
# - coding: utf-8 -
# Copyright (C) 2014 Nicolas Chachereau <nicolas.chachereau@gmail.com>
# This program is an interface for Project Hamster, and is partly
# based on its code. See <https://github.com/projecthamster/hamster>.
# Furthermore, it was inspired by Alberto Milone's hamster-appindicator,
# see <https://github.com/tseliot/hamster-appindicator>.
# 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/>.
from gi.repository import AppIndicator3 as appindicator
from gi.repository import Gtk as gtk
from gi.repository import Gdk as gdk
from gi.repository import GLib as glib
from gi.repository import Gio as gio
import dbus
import datetime as dt
from hamster import client
from hamster.lib import i18n, stuff, Fact
# TODO: separate translations from Project Hamster
# -> also translate .desktop file
# TODO: create installation script
# - copy icons
# - copy .desktop file (also for autostart)
# - copy and compile gsettings schema
# TODO: write README
class HamsterIndicator(object):
BASE_SETTINGS_KEY = "org.gnome.HamsterIndicator"
# The "label-guide" property does not seem to have any effects.
# So let's just always use the same string for all settings.
# See also https://bugs.launchpad.net/indicator-application/+bug/1310386
INDICATOR_LABEL_GUIDE = "00:00"
def __init__(self):
self.storage = client.Storage()
self.storage.connect("facts-changed", self.on_facts_changed, None)
self._settings = gio.Settings(self.BASE_SETTINGS_KEY)
self._settings.connect("changed", self.on_settings_changed, None)
self._recent_activity_items = []
self._create_indicator()
glib.timeout_add_seconds(60, self.update_activity_info)
def _window_service_do(self, method_name):
"""Call any method from Hamster DBus windows service"""
bus = dbus.SessionBus()
server = bus.get_object("org.gnome.Hamster.WindowServer",
"/org/gnome/Hamster/WindowServer")
getattr(server, method_name)()
def overview(self):
self._window_service_do("overview")
def add(self):
self._window_service_do("edit")
def stop(self):
self.storage.stop_tracking()
def prefs(self):
self._window_service_do("preferences")
def current(self):
"""Return current activity text and duration."""
if self._facts and not self._facts[-1].end_time:
text = self._facts[-1].activity
# Calculate duration manually because self._facts[-1].delta
# may not have been updated when this is called from a timeout
now = dt.datetime.now().replace(microsecond = 0)
delta = now - self._facts[-1].start_time
duration = stuff.format_duration(delta, human=False)
return text, duration
else:
return _("No activity"), ""
def update_activity_info(self):
# Update icon when activity is ongoing
if self._facts:
is_active = not self._facts[-1].end_time
if is_active:
self.indicator.set_status(appindicator.IndicatorStatus.ATTENTION)
else:
self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
self.stop_activity_item.set_sensitive(is_active)
self._update_quit_menu_item()
# Update labels
show_text = self._get_setting("show-description-on-panel")
show_time = self._get_setting("show-duration-on-panel")
visible = not (show_text and show_time)
self.activity_info_item.set_visible(visible)
self.activity_info_sep.set_visible(visible)
text, duration = self.current()
if show_text and show_time:
indicator_label = "%s %s" % (text, duration)
elif show_time:
indicator_label = duration
elif show_text:
indicator_label = text
else:
indicator_label = ""
self.indicator.set_label(
indicator_label,
self.INDICATOR_LABEL_GUIDE
)
menu_label = "%s %s" % (text, duration)
self.activity_info_item.set_label(menu_label)
# Make sure not to stop the timeout
return True
def update_recent_activities(self):
activities_to_show = self._settings.get_uint("recent-activities-number")
for menu_item in self._recent_activity_items:
self.menu.remove(menu_item)
self._recent_activity_items = []
if activities_to_show:
activities = self.storage.get_activities() # activities come sorted
# if there is ongoing activity, it will be first one. Skip it
if activities and self._facts and not self._facts[-1].end_time:
activities = activities[1:]
# setup menu items
for activity in activities:
full_name = activity['name'] + u"@" + activity['category']
item = gtk.MenuItem(full_name)
item.connect("activate", self.on_switch_to_recent, full_name)
self._recent_activity_items.append(item)
activities_to_show -= 1
if not activities_to_show:
break
self._recent_activity_items.append(gtk.SeparatorMenuItem())
# add them to menu and make visible
for i in reversed(self._recent_activity_items):
self.menu.prepend(i)
i.show()
def _get_setting(self, key):
return self._settings.get_boolean(key)
def _create_indicator(self):
self.indicator = appindicator.Indicator.new(
"hamster-indicator",
"hamster-indicator-inactive",
appindicator.IndicatorCategory.APPLICATION_STATUS
)
self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
self._set_icons()
self.menu = gtk.Menu()
self.activity_info_item = gtk.MenuItem()
self.activity_info_item.set_sensitive(False)
self.menu.append(self.activity_info_item)
self.activity_info_sep = gtk.SeparatorMenuItem()
self.menu.append(self.activity_info_sep)
new_activity_item = \
gtk.MenuItem.new_with_mnemonic(_(u"_New Activity…"))
self.menu.append(new_activity_item)
new_activity_item.connect("activate", self.on_new_activity, None)
self.stop_activity_item = \
gtk.MenuItem.new_with_mnemonic(_(u"_Stop Tracking"))
self.menu.append(self.stop_activity_item)
self.stop_activity_item.connect("activate", self.on_stop_activity, None)
show_overview_item = \
gtk.MenuItem.new_with_mnemonic(_(u"Show _Overview"))
self.menu.append(show_overview_item)
show_overview_item.connect("activate", self.on_show_overview, None)
self.menu.append(gtk.SeparatorMenuItem())
tracking_settings_item = gtk.MenuItem(_(u"Tracking Settings"))
self.menu.append(tracking_settings_item)
tracking_settings_item.connect("activate", self.on_show_settings, None)
indicator_options_item = gtk.MenuItem(_(u"Indicator Options"))
self.menu.append(indicator_options_item)
indicator_options_item.connect("activate", self.on_show_options, None)
self.menu.append(gtk.SeparatorMenuItem())
self.quit_item = gtk.MenuItem()
self.menu.append(self.quit_item)
self.quit_item.connect("activate", self.on_quit, None)
self.menu.show_all()
self.indicator.set_menu(self.menu)
# Load facts and update interface
self.on_facts_changed()
def _set_icons(self):
# Set icons according to settings
if self._get_setting("change-icon-when-active"):
# TODO: this icon needs some love
self.indicator.set_attention_icon('hamster-indicator-active')
else:
# just use the same icon
self.indicator.set_attention_icon('hamster-indicator-inactive')
def _update_quit_menu_item(self):
if self._get_setting("stop-on-quit") and self._facts and \
self._facts[-1] and not self._facts[-1].end_time:
self.quit_item.set_label(_(u"_Quit and stop tracking"))
else:
self.quit_item.set_label(_(u"_Quit"))
def on_new_activity(self, *args):
self.add()
def on_stop_activity(self, *args):
self.stop()
def on_show_overview(self, *args):
self.overview()
def on_show_settings(self, *args):
self.prefs()
def on_show_options(self, *args):
window = gtk.Window(type=gtk.WindowType.TOPLEVEL)
window.set_title(_(u"Indicator Options"))
window.set_icon_name("hamster-time-tracker")
window.set_border_width(12)
window.set_resizable(False)
window.connect_after("destroy", gtk.main_quit)
window.connect("key-press-event", self.on_options_key_press)
box = gtk.Box(orientation=gtk.Orientation.VERTICAL, spacing=6)
window.add(box)
options = [
("show-description-on-panel", _(u"Show current _activity")),
("show-duration-on-panel", _(u"Show _duration")),
("change-icon-when-active", _(u"Show different _icon when active")),
("stop-on-quit", _(u"_Stop tracking on indicator close"))
]
for setting, label in options:
check_button = gtk.CheckButton(label=label, use_underline=True)
check_button.get_child().set_line_wrap(True)
# important: bind settings to check buttons
# (changing one will change the other)
self._settings.bind(setting,
check_button,
'active',
gio.SettingsBindFlags.DEFAULT)
box.add(check_button)
recent_box = gtk.Box(orientation=gtk.Orientation.HORIZONTAL, spacing=6)
recent_box.add(gtk.Label(label=_(u"Recent activities in menu")))
recent_spin_button = gtk.SpinButton()
recent_spin_button.set_numeric(True)
recent_spin_button.set_adjustment(gtk.Adjustment(0, 0, 99, 1, 10, 0))
self._settings.bind('recent-activities-number', recent_spin_button,
'value', gio.SettingsBindFlags.DEFAULT)
recent_box.add(recent_spin_button)
box.add(recent_box)
close_box = gtk.Box(orientation=gtk.Orientation.HORIZONTAL)
close_button = gtk.Button(_(u"_Close"), use_underline=True)
close_button.connect("clicked", self.on_options_close_button, window)
close_box.pack_end(close_button, expand=False, fill=False, padding=0)
box.pack_end(close_box, expand=False, fill=False, padding=0)
window.show_all()
gtk.main()
def on_quit(self, *args):
if self._get_setting("stop-on-quit"):
self.stop()
self.storage.conn.Quit()
self._window_service_do("Quit")
gtk.main_quit()
def on_facts_changed(self, *args):
self._facts = self.storage.get_todays_facts()
self.update_activity_info()
self.update_recent_activities()
def on_settings_changed(self, settings, key, data):
if key == "change-icon-when-active":
self._set_icons()
elif key == "stop-on-quit":
self._update_quit_menu_item()
elif key == "recent-activities-number":
self.update_recent_activities()
else:
self.update_activity_info()
def on_options_key_press(self, window, event):
if event.keyval == gdk.KEY_Escape:
window.close()
def on_options_close_button(self, widget, window):
window.close()
def on_switch_to_recent(self, *args):
self.storage.add_fact(Fact(args[1]))
if __name__ == "__main__":
i18n.setup_i18n()
hamster_indicator = HamsterIndicator()
# make Ctrl+C work despite GTK3
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
gtk.main()