~mjasnik/timekpr-revived/stable

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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#!/usr/bin/python
# -*- coding: utf-8 -*-

# generic import
import gi

# configparser
try:
    # python3
    import configparser
except ImportError:
    # python2.x
    import ConfigParser as configparser

# indicator stuff
try:
    gi.require_version('AppIndicator3', '0.1')
    from gi.repository import AppIndicator3 as AppIndicator
    USE_INDICATOR = True
except (ImportError, ValueError):
    USE_INDICATOR = False
    pass

# init speech
try:
    from espeak import espeak as espeak
    USE_SPEECH = True
except (ImportError, ValueError):
    USE_SPEECH = False
    pass

# trying to get on the bus
try:
    import dbus
    USE_DBUS = True
except  (ImportError, ValueError):
    USE_DBUS = False
    pass

import datetime
import os
import sys
import time

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import GLib

import locale
import gettext
from gettext import gettext as _
from gettext import ngettext

import subprocess

# debug
DEBUG = False
# if DEVACTIVE is true, it uses files from local directory
DEVACTIVE = False

# check for debug option
try:
    for arg in sys.argv:
        if arg == "debug":
            DEBUG = True
        elif arg == "devactive":
            DEVACTIVE = True
except Exception:
    pass

# local files
if DEVACTIVE:
    from sys import path
    path.append('.')
    print("%s: development config" % ("main"))

from common.timekprpam import *
from common.timekprcommon import *

# timekpr.conf variables (dictionary variable)
VAR = getvariables(DEVACTIVE)
VERSION = getversion()

UI_POPUPMENU = """
<ui>
    <popup name='PopupMenu'>
        <menuitem action='TimeLeft' />
        <separator />
        <menuitem action='Properties' />
        <separator />
        <menuitem action='Timekpr-GUI' />
        <separator />
        <menuitem action='About' />
    </popup>
</ui>
"""

def debug(text, force=False):
    prefix="LOG"
    if DEBUG:
        prefix="DBG%s" % (prefix)
    if DEBUG or force:
        print('%s: %s' % (prefix, text))

#translation stuff
def init_locale():
    locale_domain = "timekpr" # must match locale domain set in *.ui files
    locale_path = '/usr/share/locale'

    # init python gettext
    gettext.bindtextdomain(locale_domain, locale_path)
    gettext.textdomain(locale_domain)

def get_language():
    lang = ""

    try:
        lang = locale.getlocale()[0].split('_')[0]
    except Exception:
        pass

    return lang

def config_filename(DEVACTIVE):
    cfg_filename = ""
    if DEVACTIVE:
        cfg_filename = "../support"

    cfg_path = os.getenv("XDG_CONFIG_HOME", default=os.path.join(os.path.expanduser("~"), ".config", "timekpr"))
    cfg_filename = cfg_filename + os.path.join(cfg_path, 'timekpr-client.conf')
    return cfg_filename

def load_config(DEVACTIVE):
    fconf = config_filename(DEVACTIVE)

    conf = configparser.ConfigParser()
    try:
        conf.read(fconf)
    except configparser.ParsingError:
        debug('Error: Could not parse the configuration file properly %s' % fconf, True)

    #Creating a dictionary file
    var = dict()

    try:
        var['SHOW_FIRST_NOTIFICATION'] = conf.getboolean('notifier', 'show_first_notification')
    except (configparser.NoSectionError, configparser.NoOptionError):
        var['SHOW_FIRST_NOTIFICATION'] = True

    try:
        var['USE_SPEECH_NOTIFICATION'] = conf.getboolean('notifier', 'use_speech_notification')
    except (configparser.NoSectionError, configparser.NoOptionError):
        var['USE_SPEECH_NOTIFICATION'] = True

    return var

def save_config(DEVACTIVE):
    fconf = config_filename(DEVACTIVE)

    dir = os.path.dirname(fconf)
    if not os.path.exists(dir):
        os.makedirs(dir)

    conf = configparser.ConfigParser()
    conf.add_section('notifier')
    conf.set('notifier', 'show_first_notification', VAR['SHOW_FIRST_NOTIFICATION'])
    conf.set('notifier', 'use_speech_notification', VAR['USE_SPEECH_NOTIFICATION'])

    with open(fconf, 'wb') as configfile:
        conf.write(configfile)

def which(program, use_secure_path=False, options=None):
    """Searches the environment PATH (or an hard-coded 'secure' path) for an executable with the given name."""
    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, name = os.path.split(program)
    if fpath:
        if is_exe(program):
            if options:
                program += " " + options
            return program
    else:
        if use_secure_path:
            path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        else:
            path = os.environ["PATH"]

        for part in path.split(os.pathsep):
            part = part.strip('"')
            exe_file = os.path.join(part, program)
            if is_exe(exe_file):
                if options:
                    exe_file += " " + exe_file
                return exe_file

    return None

def init_espeak():
    if USE_SPEECH:
        # set up speech synth
        espeak.set_voice(get_language());
        espeak.set_parameter(espeak.Parameter.Pitch, 1)
        espeak.set_parameter(espeak.Parameter.Rate, 145)
        espeak.set_parameter(espeak.Parameter.Range, 600)

class IndicatorTimekpr(object):
    def __init__(self):
        # get which DE we are running
        self.isAppIndicator = (True if (self.getSessionName().find("Unity") > -1 or (self.getSessionName() == "KDE" and self.getSessionVersion(self.getSessionName()) == 5)) and USE_INDICATOR else False)

        # this is for Unity stuff
        if self.isAppIndicator:
            # init indicator itself (icon will be set later)
            self.ind = AppIndicator.Indicator.new("indicator-timekpr", "", AppIndicator.IndicatorCategory.APPLICATION_STATUS)
            self.ind.set_status(AppIndicator.IndicatorStatus.ACTIVE)

            # define empty menu
            self.menu = Gtk.Menu()
            self.item_left = Gtk.MenuItem(_("Time left..."))
            self.menu.append(self.item_left)
            self.menu.append(Gtk.SeparatorMenuItem())
            self.item_properties = Gtk.MenuItem(_('Properties'))
            self.menu.append(self.item_properties)
            self.menu.append(Gtk.SeparatorMenuItem())
            self.item_cp = Gtk.MenuItem(_('Timekpr Control Panel'))
            self.menu.append(self.item_cp)
            self.menu.append(Gtk.SeparatorMenuItem())
            self.item_about = Gtk.MenuItem(_('About'))
            self.menu.append(self.item_about)
            self.menu.show_all()
            self.item_left.connect("activate", self.on_click)
            self.item_properties.connect("activate", self.on_properties)
            self.item_cp.connect("activate", self.on_timekpr_gui)
            self.item_about.connect("activate", self.on_about)

            self.ind.set_menu(self.menu)
        # this is for Non-Unity stuff
        else:
            # set up tray
            self.tray = Gtk.StatusIcon()
            self.tray.set_visible(True)

            self.tray.connect('activate', self.on_click)
            self.tray.connect('popup-menu', self.onPopupMenu)

            action_group = Gtk.ActionGroup("timekpr-client_actions")
            action_group.add_actions([
                ("TimeLeft", Gtk.STOCK_INFO, _("Time left..."), None, None, self.on_click),
                ("Properties", Gtk.STOCK_PROPERTIES, None, None, None, self.on_properties),
                ("Timekpr-GUI", Gtk.STOCK_PREFERENCES, _('Timekpr Control Panel'), None, None, self.on_timekpr_gui),
                ("About", Gtk.STOCK_ABOUT, None, None, None, self.on_about)
                                    ])

            uimanager = Gtk.UIManager()
            uimanager.add_ui_from_string(UI_POPUPMENU)
            uimanager.insert_action_group(action_group)
            self.popup = uimanager.get_widget("/PopupMenu")

        # initialize timekpr related stuff
        self.initTimekpr()

    def on_click(self, evt):
        self.click = True
        self.regularNotifier()

    def on_properties(self, evt):
        ui_file = os.path.join(VAR['TIMEKPRSHARED'], 'client.ui')

        self.builder = Gtk.Builder()
        self.builder.add_from_file(ui_file)

        #connect signals to python methods
        self.builder.connect_signals(self)

        #connect gtk objects to python variables
        for obj in self.builder.get_objects():
            if issubclass(type(obj), Gtk.Buildable):
                name = Gtk.Buildable.get_name(obj)
                setattr(self, name, obj)

        self.clientwindow.set_title(_('Timekpr Client Control Panel'))
        self.userLabel.set_text(self.username)

        self.showFirstNotificationCheck.set_active(VAR['SHOW_FIRST_NOTIFICATION'])
        self.useSpeechNotificationCheck.set_active(VAR['USE_SPEECH_NOTIFICATION'])

        # do not allow user to mess around when speech is not available
        self.useSpeechNotificationCheck.set_sensitive(USE_SPEECH)

    def on_aboutMenuItem_selected(self, evt):
        self.on_about(evt)

    def on_applyButton_clicked(self, evt):
        VAR['SHOW_FIRST_NOTIFICATION'] = self.showFirstNotificationCheck.get_active()
        VAR['USE_SPEECH_NOTIFICATION'] = self.useSpeechNotificationCheck.get_active()
        save_config(DEVACTIVE)

    def on_cancelButton_clicked(self, evt):
        self.clientwindow.destroy()

    def on_window_destroy(self, evt):
        pass

    def on_timekpr_gui(self, evt):
        import subprocess
        rc = subprocess.call(['pkexec', 'timekpr-gui'])

    def onPopupMenu(self, status, button, time):
        self.popup.popup(None, None, None, None, 0, time)

    def on_about(self, evt):
        ui_file = os.path.join(VAR['TIMEKPRSHARED'], 'about_dialog.ui')

        builder = Gtk.Builder()
        builder.add_from_file(ui_file)

        dialog = builder.get_object('aboutdialog')
        dialog.set_version(VERSION)
        dialog.set_translator_credits(_("translator-credits"))
        dialog.set_comments(_('Keep control of computer usage'))
        dialog.show_all()
        dialog.run()
        dialog.destroy()

    def initTimekpr(self):
        # get variables and set interval
        self.VAR = getvariables(DEVACTIVE)
        self.checkInterval = 30
        self.timerLevelInEffect = 1

        # get username
        self.username = os.getenv('USER')

        # replace stuff for DEV
        if DEVACTIVE:
            self.VAR['TIMEKPRSHARED'] = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'icons/'))
            self.VAR['TIMEKPRWORK'] = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'sample/'))
            self.VAR['TIMEKPRDIR'] = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'sample/'))

        # get both locked and unlocked icons
        self.limited_green = os.path.join(self.VAR['TIMEKPRSHARED'], 'padlock-limited-green.png')
        self.limited_yellow = os.path.join(self.VAR['TIMEKPRSHARED'], 'padlock-limited-yellow.png')
        self.limited_red = os.path.join(self.VAR['TIMEKPRSHARED'], 'padlock-limited-red.png')
        self.unlimited_green = os.path.join(self.VAR['TIMEKPRSHARED'], 'padlock-unlimited-green.png')

        # get language
        self.lang = get_language()

        # set up control files
        self.timefile = os.path.join(self.VAR['TIMEKPRWORK'], self.username + '.time')
        self.allowfile = os.path.join(self.VAR['TIMEKPRWORK'], self.username + '.allow')
        self.conffile = os.path.join(self.VAR['TIMEKPRDIR'], self.username)

        # create stuff for DEV
        if DEVACTIVE:
            debug("creating sample dir")
            dir = os.path.dirname(self.VAR['TIMEKPRWORK']+"/")
            if not os.path.exists(dir):
                debug("creating sample files")
                os.makedirs(dir)
                f=open(self.timefile, "a")
                f.write("84540")
                f.close()
                f=open(self.allowfile, "a")
                f.close()
                f=open(self.conffile + ".late", "a")
                f.close()

        # nobody wanted info by clicking on the icon and other default stuff
        self.click = False
        self.firstNotif = True
        self.notifTimer = None
        self.notifyInterface = None
        self.timeSpentPrev, verified = self.getTime(self.timefile)
        self.notificationLimits = (
            [0*60,1*60,'critical',self.limited_red,0] # very critial, once a minute, urgency is critical (on top of everyting), icon is red
            ,[3*60,1*60,'critical',self.limited_red,1] # less than 3 mins - notification goes off every minute, urgency is critical (on top of everyting), icon is red
            ,[10*60,5*60,'critical',self.limited_yellow,2] # normal, but yellow as warning, not much time left at all, urgency is critical (on top of everyting), icon is still yellow
            ,[30*60,10*60,'normal',self.limited_yellow,3] # normal, but yellow as warning, not much time left
            ,[2*60*60,30*60,'low',self.limited_green,4] # plenty of time
            ,[9999999,60*60,'low',self.limited_green,5] # even more plenty of time
        )

        # this is needed only if DBUS is used
        if USE_DBUS:
            # create a dict for urgencies
            self.dbusUrgencies = {"low":dbus.Byte(0, variant_level=1), "normal":dbus.Byte(1, variant_level=1), "critical":dbus.Byte(2, variant_level=1)}
            # init DBUS early
            self.initNotificatioDelivery(True)
            # first time it may fail, we need to init it the lazy way later

        # initial check of the limits
        self.reReadConfigAndcheckLimits()

        # set up first notification as per config
        self.firstNotif = VAR['SHOW_FIRST_NOTIFICATION']

        # add a GLib loop to check limits:
        GLib.timeout_add_seconds(self.checkInterval, self.reReadConfigAndcheckLimits)

        # add a notifier for the first time to x seconds (only if not already initialized)
        self.notifTimer = GLib.timeout_add_seconds(self.timerLevelInEffect+9, self.initNotificatioDelivery)

    def reReadConfigAndcheckLimits(self):
        name = "reReadConfigAndcheckLimits"
        # defaults
        urgency = 'low'

        # re-read settings in case they changed
        self.limits, self.bfrom, self.bto = readusersettings(self.username, self.conffile)

        # get the day
        index = int(strftime("%w"))

        # if the user is not a restricted user for this day, set the tray icon to green padlock
        if not isrestricteduser(self.username, self.limits[index]):
            # user is not limited
            if self.isAppIndicator:
                self.ind.set_icon(self.unlimited_green)
                self.ind.set_label("∞", "")
                self.ind.set_title("∞")
            else:
                self.tray.set_from_file(self.unlimited_green)
                self.tray.set_tooltip_text("∞")

            # come back later
            return True
        elif self.firstNotif:
            # user is limited
            if self.isAppIndicator:
                self.ind.set_icon(self.limited_red)
            else:
                self.tray.set_from_file(self.limited_red)

        # get the time already spent
        time, verified = self.getTime(self.timefile)

        # check if we have file, if not - exit
        if verified != True:
            self.notifyUser(_('Can not read configuration, time left is unknown at the moment... Please wait a little for configuration to be initialized...'), 'normal')
            return True

        debug("%s: previous time: %d, new time: %d, check interval: %d" % (name, time, self.timeSpentPrev, 3*self.checkInterval-1))

        # check if time changed too rapildly, we have to resched the notifier
        if abs(time - self.timeSpentPrev) > (3*self.checkInterval-1):
            # end the current callback
            GLib.source_remove(self.notifTimer)

            # add call very shortly
            self.timerLevelInEffect = 1
            self.notifTimer = GLib.timeout_add_seconds(self.timerLevelInEffect, self.regularNotifier)
            debug("%s: time changed rapidly, emit notification" % (name))

        # store previous reading
        self.timeSpentPrev = time

        # get the time left
        left = self.getTimeLeft()
        debug("%s: time left %d" % (name, left))

        # normalize time for display
        if left <= 0:
            left = 0
            urgency = 'critical'
        # in case more time added while very low on minutes
        elif self.timerLevelInEffect <= 0 and left > 0:
            self.regularNotifier()

        # split hours, minutes, seconds
        h, m, s = self.fractSec(left)

        # indicators only
        if self.isAppIndicator:
            self.ind.set_label("(%s:%s)" % (str(h).rjust(2, "0"), str(m).rjust(2, "0")), "")
            self.ind.set_title("(%s:%s)" % (str(h).rjust(2, "0"), str(m).rjust(2, "0")))
        else:
            self.tray.set_tooltip_text("(%s:%s)" % (str(h).rjust(2, "0"), str(m).rjust(2, "0")))

        # now if it arrives too early
        if isearly(self.bfrom, self.allowfile):
            self.notifyUser(_('You are early, you will be logged out in LESS than 2 minutes'), urgency)

        # now if it arrives too late
        if islate(self.bto, self.allowfile):
            self.notifyUser(_('You are late, you will be logged out in LESS than 2 minutes'), urgency)

        # now if allowed time is almost spent
        if ispasttime(self.limits, time):
            self.notifyUser(_('Your time is up, you will be logged out in LESS than 2 minutes'), urgency)

        # done
        return True

    # initialize anything related to notifications and send first notification (called from glib timer)
    def initNotificatioDelivery(self, firstInit=False):
        # changeable global variables
        global USE_DBUS

        # trying to get on the bus
        if USE_DBUS and self.notifyInterface is None:
            try:
                # dbus connection
                self.sessionDbus = dbus.SessionBus()
                self.notifyObject = self.sessionDbus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
                self.notifyInterface = dbus.Interface(self.notifyObject, 'org.freedesktop.Notifications')
            except:
                if firstInit != True:
                    USE_DBUS = False
                else:
                    debug("%s: DBUS init failed for the first time, trying second time..." % ("initNotificatioDelivery"))
                pass

        # add call very shortly
        if firstInit != True:
            # check if we are in gnome and issue a warning
            if self.getSessionName().find("GNOME") > -1:
                self.notifyUser(_('If You can not see Timekpr icon in the notification area and are using Gnome3, please install or ask Your supervisor to install TopIcons extension or similar to show the notification area icons!'), 'low')

            # issue a first notification
            self.timerLevelInEffect = 1
            self.notifTimer = GLib.timeout_add_seconds(self.timerLevelInEffect, self.regularNotifier)

        # limit this to one call (called from timer, has to return False not to repeat the message)
        return False

    # periodic notifier, gives notifications to the user
    def regularNotifier(self):
        # default values
        name = "regularNotifier"
        result = True
        timerLevelInEffectTmp = self.timerLevelInEffect
        urgency = 'low'

        # get the day
        index = int(strftime("%w"))

        # if the user is not a restricted user for this day
        if not isrestricteduser(self.username, self.limits[index]):
            if self.firstNotif or self.click:
                self.notifyUser(_('Your time is not limited today'), 'low')
                self.click = False

                # restricted user - no more notifs
                self.timerLevelInEffect = 0
            result = False
        # if there is time to check
        else:
            time, verified = self.getTime(self.timefile)
            if verified != True:
                result = False
                debug("%s: file (%s) can NOT be read, we have a problem..." % (name, self.timefile))
                return result

        # first notification is no more
        self.firstNotif = False

        # we don't need to process any further
        if result != False:
            # get how much time is left
            left = self.getTimeLeft()

            debug("%s: time left: %d" % (name, left))

            # if the time is up, notifications is taken care of by reReadConfigAndcheckLimits
            if left < 0:
                # handled in read config
                self.timerLevelInEffect = 0
                result = False

            # we don't need to process any further
            if result != False:
                # split hours, minutes, seconds
                h, m, s = self.fractSec(left)

                # build up message in human language :)
                message = self.calculateTimeLeftString(h, m, s)

                # check limits and do actions based on results
                for rLimit in self.notificationLimits:
                    debug("%s: checking limit: %d" % (name, rLimit[0]))

                    # check time left according to defined limits
                    if left < rLimit[0] + self.checkInterval + 5:
                        debug("%s: found the right limit: %d" % (name, rLimit[0]))

                        # set urgency
                        urgency = rLimit[2]

                        # if not clicked
                        if not self.click:
                            # new timer
                            self.timerLevelInEffect = rLimit[1]
                            debug("%s: check if limit has changed, old: %d, new: %d" % (name, timerLevelInEffectTmp, self.timerLevelInEffect))

                            # if limit has changed
                            if timerLevelInEffectTmp != self.timerLevelInEffect:
                                # depending on DE
                                if self.isAppIndicator:
                                    self.ind.set_icon(rLimit[3])
                                else:
                                    self.tray.set_from_file(rLimit[3])

                                # change tmp for adjustments
                                timerLevelInEffectTmp = self.timerLevelInEffect

                                debug("%s: limit compensation, left: %d, prev: %d, check: %d" % (name, left, self.notificationLimits[rLimit[4]-1][0], self.timerLevelInEffect))

                                # calculate new interval: say if we have 26 mins notification will be scheduled at 16 mins, but the rule
                                # is that if we have less than 25 mins, notification goes off every 5 mins, so that has to be at 25, 20, ... mins
                                # to compensate that we need to schedule time to go off at 25 not 16
                                if left - self.notificationLimits[rLimit[4]-1][0] < self.timerLevelInEffect:
                                    timerLevelInEffectTmp = left - self.notificationLimits[rLimit[4]-1][0]
                                    debug("%s: adjusted limit: %d" % (name, timerLevelInEffectTmp))

                                # set up new interval
                                self.notifTimer = GLib.timeout_add_seconds(timerLevelInEffectTmp, self.regularNotifier)

                                # end existing interval
                                result = False

                        # that's it
                        break

                # notify (taking into account calculated urgency as well)
                self.notifyUser(message, urgency)

        # noone clicked us
        self.click = False

        # result
        return result

    # returns a formated string with the time left for a user
    def calculateTimeLeftString(self, h, m, s):
        # variables
        strLevel = 0

        ## printing correctly
        # header
        # TRANSLATORS: this is a part of message "You have %(hour)s hour(s), %(min)s minute(s) and %(sec)s second(s) left" please translate accordingly
        message = _('You have')

        # for hours
        if h > 0:
            # TRANSLATORS: this is a part of message "You have %(hour)s hour(s), %(min)s minute(s) and %(sec)s second(s) left" please translate accordingly
            messageTmp = ' ' + ngettext('%(hour)s hour', '%(hour)s hours', h) % {'hour': h}
            strLevel=strLevel+1
        else:
            messageTmp = ''

        # compose
        message = message + messageTmp

        # for minutes
        if m > 0:
            if strLevel > 0:
                messageTmp = ','
            else:
                messageTmp = ''

            # TRANSLATORS: this is a part of message "You have %(hour)s hour(s), %(min)s minute(s) and %(sec)s second(s) left" please translate accordingly
            messageTmp = messageTmp + ' ' + ngettext('%(min)s minute', '%(min)s minutes', m) % {'min': m}
            strLevel = strLevel + 1
        else:
            messageTmp = ''

        # compose
        message = message + messageTmp

        # for seconds
        if s > 0 or (m == 0 and h == 0):
            if strLevel > 0:
                messageTmp = ','
            else:
                messageTmp = ''

            # TRANSLATORS: this is a part of message "You have %(hour)s hour(s), %(min)s minute(s) and %(sec)s second(s) left" please translate accordingly
            messageTmp = messageTmp + ' ' + ngettext('%(sec)s second', '%(sec)s seconds', s) % {'sec': s}
            strLevel = strLevel + 1
        else:
            messageTmp = ''

        # compose
        # TRANSLATORS: this is a part of message "You have %(hour)s hour(s), %(min)s minute(s) and %(sec)s second(s) left" please translate accordingly
        message = message + messageTmp + ' ' + _('left')

        # final message
        return message

    def notifyUser(self, message, urgency):
        name = "notifyUser"
        # defaults
        icon = 'gtk-dialog-info'
        durationSecs = 3
        durationMsecs = durationSecs * 1000
        title = _('Timekpr notification')

        # based on urgency, choose different icon
        if urgency == 'normal':
            icon = 'gtk-dialog-warning'
        elif urgency == 'critical':
            icon = 'gtk-dialog-error'

        # if speech is enabled, don't bother user with urgent notifications when it's not urgent (these go off even in fullscreen games)
        if not VAR['USE_SPEECH_NOTIFICATION']:
            urgency = 'critical'

        # if dbus available
        if USE_DBUS:
            # onl if dbus is initiated
            if self.notifyInterface is not None:
                # notify
                self.notifyInterface.Notify('Timekpr', 0, icon, title, message, '', {"urgency":self.dbusUrgencies[urgency]}, durationMsecs)
                debug("%s: notification via dbus" % (name))
            else:
                debug("%s: notification via dbus, BUT DBUS is NOT initalized" % (name), True)
        # KDE uses different tech to notify users
        elif self.getSessionName() == 'KDE' and self.getSessionVersion(self.getSessionName()) == 3:
            # KDE3 and friends use dcop
            getcmdoutput('dcop knotify default notify notifying timekpr-client "' + message + '" "" "" 16 0')
            debug("%s: notification via dcop" % (name))
        # failover
        else:
            # for the rest try standard notification
            getcmdoutput('notify-send --icon=' + icon + ' --urgency=' + urgency + ' -t ' + str(durationMsecs) + ' "' + title + '" "' + message + '"')
            debug("%s: notification via notify-send" % (name))

        # if speech is enabled, let's make some noise
        if VAR['USE_SPEECH_NOTIFICATION'] and USE_SPEECH:
            espeak.synth(message)
            debug("%s: speach" % (name))

    # returns the number of seconds a user has spent
    def getTime(self, tfile):
        name = "getTime"
        debug("%s: config file (%s) read" % (name, tfile))

        # check for file
        if not isfile(tfile):
            debug("%s: config file (%s) NOT found!" % (name, tfile), True)
            return 0, False

        # get seconds spent
        t = open(tfile)
        time = int(t.readline())
        t.close()

        # pass back time
        return time, True

    def getTimeLeft(self):
        name = "getTimeLeft"
        # get day
        index = int(strftime("%w"))

        # calculates how much time if left
        usedtime, verified = self.getTime(self.timefile)
        timeleft = self.limits[index] - usedtime
        timeuntil = self.timeofbto(index) - datetime.datetime.now()
        tuntil = timeuntil.seconds

        # what is less (is "until" time earlier than limit)
        if timeleft <= tuntil:
            left = timeleft
        else:
            left = tuntil

        debug("%s: used: %d, left: %d, until: %d, left: %d" % (name, usedtime, timeleft, tuntil, left))

        # return what's left :)
        return left

    # return the time limit
    def timeofbto(self, index):
        # current date
        y = datetime.date.today().year
        m = datetime.date.today().month
        d = datetime.date.today().day

        # to hour
        h = self.bto[index]

        # compose new date
        date = datetime.date(y, m, d)

        # correct date
        if h == 24:
            h = 0
            date = date + datetime.timedelta(days=1)

        # final calculation for date
        dt = datetime.datetime(date.year, date.month, date.day, h, 0, 0)

        # done
        return dt

    # divides time into larger pieces :)
    def fractSec(self, s):
        m, s = divmod(s, 60)
        h, m = divmod(m, 60)
        return h, m, s

    # this gets session information
    def getSessionName(self):
        debug("session name: %s" % (os.getenv('XDG_CURRENT_DESKTOP')))
        return os.getenv('XDG_CURRENT_DESKTOP')

    # get KDE version
    def getSessionVersion(self, sessionName):
        # defaults
        version = 0

        # for KDE check KDE version
        if sessionName == "KDE":
            # get KDE version
            versionTmp = os.getenv('KDE_SESSION_VERSION')

            # get version
            if version == "\n" or version == "":
                version = 3
            else:
                version = int(versionTmp)

        debug("session version: %d" % (version))
        # final version
        return version

    def quit(self):
        Gtk.main_quit()

if __name__ == "__main__":
    init_locale()
    init_espeak()
    # add local configuration to dictionary
    VAR.update(load_config(DEVACTIVE))
    indicator = IndicatorTimekpr()
    Gtk.main()