|
| 1 | +# - coding: utf-8 - |
| 2 | + |
| 3 | +# Copyright (C) 2020 Sharaf Zaman <sharafzaz121@gmail.com> |
| 4 | + |
| 5 | +# This file is part of Project Hamster. |
| 6 | + |
| 7 | +# Project Hamster is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | + |
| 12 | +# Project Hamster is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | + |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with Project Hamster. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +import datetime |
| 21 | +import dbus |
| 22 | +import hamster.client |
| 23 | + |
| 24 | +from gi.repository import Gtk |
| 25 | +from gi.repository import GObject as gobject |
| 26 | +from hamster.lib.configuration import conf |
| 27 | + |
| 28 | + |
| 29 | +class Notification(object): |
| 30 | + def __init__(self): |
| 31 | + self.bus = dbus.SessionBus() |
| 32 | + self.appname = "Hamster Time Tracker" |
| 33 | + self.replace_id = 0 |
| 34 | + # FIXME: icon location? |
| 35 | + self.icon = "/usr/share/hamster-time-tracker/art/hamster-time-tracker.png" |
| 36 | + self.summary = "Hamster Time Tracker" |
| 37 | + self.hints = {} |
| 38 | + self.actions = [] |
| 39 | + self.data = {} |
| 40 | + self.timeout = -1 |
| 41 | + |
| 42 | + def show(self, message): |
| 43 | + """ |
| 44 | + Show notitification |
| 45 | + returns: True if successful |
| 46 | + """ |
| 47 | + try: |
| 48 | + self.server = dbus.Interface(self.bus.get_object("org.freedesktop.Notifications", |
| 49 | + "/org/freedesktop/Notifications"), |
| 50 | + dbus_interface="org.freedesktop.Notifications") |
| 51 | + except dbus.exceptions.DBusException as e: |
| 52 | + # TODO: Log? |
| 53 | + print(e) |
| 54 | + print("Notifications will be disabled") |
| 55 | + return False |
| 56 | + |
| 57 | + try: |
| 58 | + self.server.Notify(self.appname, |
| 59 | + self.replace_id, |
| 60 | + self.icon, |
| 61 | + self.summary, |
| 62 | + message, |
| 63 | + self.actions, |
| 64 | + self.hints, |
| 65 | + self.timeout |
| 66 | + ) |
| 67 | + except: |
| 68 | + return False |
| 69 | + |
| 70 | + return True |
| 71 | + |
| 72 | + |
| 73 | +class NotificationsManager(gobject.GObject): |
| 74 | + def __init__(self): |
| 75 | + self.notify_interval = conf.notify_interval |
| 76 | + self.minutes_passed = 0 |
| 77 | + self.notification = Notification() |
| 78 | + gobject.timeout_add_seconds(60, self.check_interval) |
| 79 | + |
| 80 | + def _notify_interval_value_changed(self, range): |
| 81 | + self.notify_interval = range.get_value() |
| 82 | + if self.notify_interval != conf.notify_interval: |
| 83 | + self.minutes_passed = 0 |
| 84 | + conf.set("notify-interval-minutes", self.notify_interval) |
| 85 | + |
| 86 | + def check_interval(self): |
| 87 | + if not conf.notifications_enabled: |
| 88 | + self.minutes_passed = 0 |
| 89 | + return True |
| 90 | + |
| 91 | + self.minutes_passed += 1 |
| 92 | + |
| 93 | + storage = hamster.client.Storage() |
| 94 | + facts = storage.get_todays_facts() |
| 95 | + |
| 96 | + if self.minutes_passed == self.notify_interval: |
| 97 | + # if the activity is still active |
| 98 | + if len(facts) > 0 and facts[-1].end_time is None: |
| 99 | + timedelta_secs = (datetime.datetime.now() - facts[-1].start_time).seconds |
| 100 | + hours, rem = divmod(timedelta_secs, 60 * 60) |
| 101 | + minutes, seconds = divmod(rem, 60) |
| 102 | + if hours != 0: |
| 103 | + msg = str.format("Working on {} for {} hours and {} minutes", facts[-1].activity, hours, minutes) |
| 104 | + else: |
| 105 | + msg = str.format("Working on {} for {} minutes", facts[-1].activity, minutes) |
| 106 | + self.notification.show(msg) |
| 107 | + elif conf.notify_on_idle: |
| 108 | + self.notification.show("No Activity") |
| 109 | + |
| 110 | + self.minutes_passed = 0 |
| 111 | + |
| 112 | + return True |
| 113 | + |
| 114 | + def send_test(self): |
| 115 | + return self.notification.show("This is a test notification!") |
| 116 | + |
| 117 | + |
| 118 | +notifs_mgr = NotificationsManager() |
0 commit comments