-
Notifications
You must be signed in to change notification settings - Fork 13
/
hamster-indicator
executable file
·388 lines (308 loc) · 13.9 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
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
#!/usr/bin/python
#
# Copyright 2011 Alberto Milone <[email protected]>
#
# Author: Alberto Milone <[email protected]>
#
# This program is largely based on applet.py from the Hamster project.
#
# 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/>.
#
import gobject
import gtk
import appindicator
import gconf
import logging
import datetime as dt
import os.path
import pygtk
pygtk.require("2.0")
import dbus, dbus.service, dbus.mainloop.glib
import locale
from hamster.configuration import conf, runtime, dialogs
try:
from hamster import stuff
from hamster import i18n
except ImportError:
from hamster.lib import stuff
from hamster.lib import i18n
# controllers for other windows
from hamster import widgets
from hamster import idle
from hamster.applet import HamsterApplet
import pango
try:
import wnck
except:
logging.warning("Could not import wnck - workspace tracking will be disabled")
wnck = None
try:
import pynotify
pynotify.init('Hamster Applet')
except:
logging.warning("Could not import pynotify - notifications will be disabled")
pynotify = None
class FakeApplet(object):
'''Fake Applet class to trick HamsterApplet'''
def __init__(self):
pass
def add(self, *args):
pass
def setup_menu_from_file(self, *args):
pass
class HamsterIndicator(HamsterApplet):
BASE_KEY = "/apps/hamster-indicator"
def __init__(self, applet=None):
# Gconf settings
self._settings = gconf.client_get_default()
self._settings.add_dir(self.BASE_KEY, gconf.CLIENT_PRELOAD_NONE)
# Key to enable/disable icon glow
self._icon_glow_path = os.path.join(self.BASE_KEY, "icon_glow")
self._use_icon_glow = self._settings.get_bool(self._icon_glow_path)
self._settings.notify_add(self._icon_glow_path, self._on_icon_glow_changed)
# Key to show/hide the indicator label
self._show_label_path = os.path.join(self.BASE_KEY, "show_label")
self._show_label = self._settings.get_bool(self._show_label_path)
self._settings.notify_add(self._show_label_path, self._on_show_label_changed)
# Key to set the length of indicator label
self._label_length_path = os.path.join(self.BASE_KEY, "label_length")
self._label_length = self._settings.get_int(self._label_length_path)
self._settings.notify_add(self._label_length_path, self._on_label_length_changed)
self._activity_as_attribute = None
# Create a fake applet since HamsterApplet requires one
applet = FakeApplet()
self.indicator = appindicator.Indicator ("hamster-applet",
"hamster-applet-inactive",
appindicator.CATEGORY_SYSTEM_SERVICES)
self.indicator.set_status (appindicator.STATUS_ACTIVE)
# Set the attention icon as per the icon_glow gconf key
self._set_attention_icon()
# Initialise the activity label with "No Activity"
self.indicator.set_label(self._get_no_activity_label())
self.activity, self.duration = None, None
self.menu = gtk.Menu()
self.activity_item = gtk.MenuItem("")
self.menu.append(self.activity_item)
# this is where you would connect your menu item up with a function:
self.activity_item.connect("activate", self.on_new_activity_activated, None)
self.activity_label = self.activity_item.get_child()
self.activity_label.connect('style-set', self.on_label_style_set)
# show the items
self.activity_item.show()
self.stop_activity_item = gtk.MenuItem(_(u"Sto_p Tracking"))
self.menu.append(self.stop_activity_item)
# this is where you would connect your menu item up with a function:
self.stop_activity_item.connect("activate", self.on_stop_activity_activated, None)
# show the items
self.stop_activity_item.show()
self.append_separator(self.menu)
self.earlier_activity_item = gtk.MenuItem(_(u"Add earlier activity"))
self.menu.append(self.earlier_activity_item)
# this is where you would connect your menu item up with a function:
self.earlier_activity_item.connect("activate", self.on_earlier_activity_activated, None)
# show the items
self.earlier_activity_item.show()
self.overview_show_item = gtk.MenuItem(_(u"Show _Overview"))
self.menu.append(self.overview_show_item)
# this is where you would connect your menu item up with a function:
self.overview_show_item.connect("activate", self.on_overview_show_activated, None)
# show the items
self.overview_show_item.show()
self.append_separator(self.menu)
self.preferences_show_item = gtk.MenuItem(_(u"Preferences"))
self.menu.append(self.preferences_show_item)
# this is where you would connect your menu item up with a function:
self.preferences_show_item.connect("activate", self.on_show_preferences_activated, None)
# show the items
self.preferences_show_item.show()
self.append_separator(self.menu)
self.quit_item = gtk.MenuItem(_(u"_Quit"))
self.menu.append(self.quit_item)
# this is where you would connect your menu item up with a function:
self.quit_item.connect("activate", gtk.main_quit, None)
# show the items
self.quit_item.show()
# Call constructor after the gtk.Menu is ready
super(HamsterIndicator, self).__init__(applet)
# Hide the panel button since it's not supported
self.button.hide()
self.window.set_title(_(u"Time Tracker"))
# Add a window decoration
self.window.set_decorated(True)
# Place the window near the mouse cursor
self.window.set_position(gtk.WIN_POS_MOUSE)
# Do not skip the taskbar
self.window.set_skip_taskbar_hint(False)
# Do not skip the pager
self.window.set_skip_pager_hint(False)
def on_new_activity_activated(self, *args):
self.show_dialog()
def on_stop_activity_activated(self, *args):
runtime.storage.stop_tracking()
self.last_activity = None
def on_show_preferences_activated(self, *args):
dialogs.prefs.show(self.indicator)
def on_overview_show_activated(self, *args):
dialogs.overview.show(self.indicator)
def on_earlier_activity_activated(self, *args):
dialogs.edit.show(self.indicator)
def refresh_menu(self):
'''Update the menu so that the new activity text is visible'''
self.indicator.set_menu(self.menu)
def reformat_label(self):
'''This adds a method which belongs to hamster.applet.PanelButton'''
label = self.activity
if self.duration:
label = "%s %s" % (self.activity, self.duration)
label = '<span gravity="south">%s</span>' % label
if self.activity_label:
self.activity_label.set_markup("") #clear - seems to fix the warning
self.activity_label.set_markup(label)
def on_label_style_set(self, widget, something):
self.reformat_label()
def append_separator(self, menu):
'''Add separator to the menu'''
separator = gtk.SeparatorMenuItem()
separator.show()
menu.append(separator)
def update_label(self):
'''Override for menu items sensitivity and to update the menu'''
if self.last_activity:
# Let's see if activity is an attribute and cache the result.
# This is only required for backwards compatibility
if self._activity_as_attribute == None:
self._activity_as_attribute = hasattr(self.last_activity,
'activity')
if self._activity_as_attribute:
start_time = self.last_activity.start_time
end_time = self.last_activity.end_time
last_activity_name = self.last_activity.activity
else:
start_time = self.last_activity['start_time']
end_time = self.last_activity['end_time']
last_activity_name = self.last_activity['name']
if self.last_activity and end_time is None:
self._set_activity_status(1)
delta = dt.datetime.now() - start_time
duration = delta.seconds / 60
label = "%s %s" % (last_activity_name,
stuff.format_duration(duration, False))
self.set_activity_text(last_activity_name,
stuff.format_duration(duration, False))
indicator_label = "%s %s" % (self._clamp_text(self.activity,
length=self._label_length,
with_ellipsis=False),
self.duration)
else:
self._set_activity_status(0)
label = "%s" % _(u"New activity")
self.set_activity_text(label, None)
indicator_label = self._get_no_activity_label()
# Update the indicator label, if needed
if self._show_label:
self.indicator.set_label(indicator_label)
else:
self.indicator.set_label("")
# Update the menu or the new activity text won't show up
self.refresh_menu()
def _clamp_text(self, text, length=25, with_ellipsis=True, is_indicator=False):
text = stuff.escape_pango(text)
if len(text) > length: #ellipsize at some random length
if with_ellipsis:
if is_indicator:
text = "%s%s" % (text[:length], "...")
else:
text = "%s%s" % (text[:length], "…")
else:
text = "%s" % (text[:length])
return text
def _set_activity_status(self, is_active):
if is_active:
# There's an active task
self.indicator.set_status (appindicator.STATUS_ATTENTION)
else:
# There's no active task
self.indicator.set_status (appindicator.STATUS_ACTIVE)
self.stop_activity_item.set_sensitive(is_active)
def _set_attention_icon(self):
'''Set the attention icon as per the gconf key'''
if self._use_icon_glow:
self.indicator.set_attention_icon('hamster-applet-active')
else:
self.indicator.set_attention_icon('hamster-applet-inactive')
def _on_icon_glow_changed(self, client, connection_id, entry, *args):
self._use_icon_glow = self._settings.get_bool(self.BASE_KEY + "/icon_glow")
self._set_attention_icon()
def _on_show_label_changed(self, client, connection_id, entry, *args):
'''Hide or show the indicator's label'''
self._show_label = self._settings.get_bool(self._show_label_path)
if self._show_label:
self.update_label()
else:
self.indicator.set_label("")
def _on_label_length_changed(self, client, connection_id, entry, *args):
'''Resize the indicator's label'''
self._label_length = self._settings.get_int(self._label_length_path)
if self._show_label:
self.update_label()
def _get_no_activity_label(self):
'''Get the indicator label set to "No activity"'''
return self._clamp_text(_(u"No activity"),
length=self._label_length,
with_ellipsis=False)
def position_popup(self):
'''Override the superclass method and do nothing'''
pass
def on_window_size_request(self, *args):
'''Override the superclass method and do nothing'''
pass
def set_last_activity(self):
'''Override to change the Stop button sensitivity'''
#self._set_activity_status(self.last_activity != None)
super(HamsterIndicator, self).set_last_activity()
def on_stop_tracking_clicked(self, widget):
'''Override to make the Stop button insensitive'''
self._set_activity_status(0)
super(HamsterIndicator, self).on_stop_tracking_clicked(widget)
def on_switch_activity_clicked(self, widget):
'''Override to make the Stop button sensitive'''
self._set_activity_status(1)
super(HamsterIndicator, self).on_switch_activity_clicked(widget)
def set_activity_text(self, activity, duration):
'''This adds a method which belongs to hamster.applet.PanelButton'''
# activity = stuff.escape_pango(activity)
# if len(activity) > 25: #ellipsize at some random length
# activity = "%s%s" % (activity[:25], "…")
activity = self._clamp_text(activity)
self.activity = activity
self.duration = duration
self.reformat_label()
def show_dialog(self, is_active=True):
"""Show new task window"""
self.button.set_active(is_active)
if is_active == False:
self.window.hide()
return True
# # doing unstick / stick here, because sometimes while switching
# # between workplaces window still manages to disappear
# self.window.unstick()
# self.window.stick() #show on all desktops
self.new_name.set_text("");
self.new_tags.set_text("");
gobject.idle_add(self._delayed_display)
if __name__ == "__main__":
i18n.setup_i18n()
hamster_indicator = HamsterIndicator()
# ind.set_menu(hamster_indicator.menu)
gtk.main()