|
| 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 logging |
| 21 | +logger = logging.getLogger(__name__) # noqa: E402 |
| 22 | +import datetime |
| 23 | +import dbus |
| 24 | +import hamster.client |
| 25 | + |
| 26 | +from gi.repository import Gtk |
| 27 | +from gi.repository import GObject as gobject |
| 28 | +from hamster.lib.configuration import conf |
| 29 | + |
| 30 | + |
| 31 | +class Notification(object): |
| 32 | + def __init__(self): |
| 33 | + self.bus = dbus.SessionBus() |
| 34 | + self.appname = "Hamster Time Tracker" |
| 35 | + self.replace_id = 0 |
| 36 | + self.summary = "Hamster Time Tracker" |
| 37 | + self.hints = {} |
| 38 | + self.actions = [] |
| 39 | + self.data = {} |
| 40 | + self.timeout = -1 |
| 41 | + |
| 42 | + info = Gtk.IconTheme().lookup_icon("hamster-time-tracker", 0, 0) |
| 43 | + self.icon = info.get_filename() |
| 44 | + |
| 45 | + def show(self, message): |
| 46 | + """ |
| 47 | + Show notitification |
| 48 | + returns: True if successful |
| 49 | + """ |
| 50 | + try: |
| 51 | + self.server = dbus.Interface(self.bus.get_object("org.freedesktop.Notifications", |
| 52 | + "/org/freedesktop/Notifications"), |
| 53 | + dbus_interface="org.freedesktop.Notifications") |
| 54 | + except dbus.exceptions.DBusException as e: |
| 55 | + # TODO: Log? |
| 56 | + logger.error(e) |
| 57 | + logger.warning("Notifications will be disabled") |
| 58 | + return False |
| 59 | + |
| 60 | + try: |
| 61 | + self.notif_id = self.server.Notify( |
| 62 | + self.appname, |
| 63 | + self.replace_id, |
| 64 | + self.icon, |
| 65 | + self.summary, |
| 66 | + message, |
| 67 | + self.actions, |
| 68 | + self.hints, |
| 69 | + self.timeout) |
| 70 | + except: |
| 71 | + return False |
| 72 | + |
| 73 | + return True |
| 74 | + |
| 75 | + def close(self): |
| 76 | + try: self.server.CloseNotification(self.notif_id) |
| 77 | + except: pass |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +class NotificationsManager(gobject.GObject): |
| 82 | + def __init__(self): |
| 83 | + self.notify_interval = conf.notify_interval |
| 84 | + self.minutes_passed = 0 |
| 85 | + self.notification = Notification() |
| 86 | + gobject.timeout_add_seconds(60, self.check_interval) |
| 87 | + |
| 88 | + def notify_interval_changed(self, value): |
| 89 | + self.minutes_passed = 0 |
| 90 | + self.notify_interval = value |
| 91 | + |
| 92 | + def check_interval(self): |
| 93 | + if not conf.notifications_enabled: |
| 94 | + self.minutes_passed = 0 |
| 95 | + return True |
| 96 | + |
| 97 | + self.minutes_passed += 1 |
| 98 | + |
| 99 | + storage = hamster.client.Storage() |
| 100 | + facts = storage.get_todays_facts() |
| 101 | + |
| 102 | + if self.minutes_passed == self.notify_interval: |
| 103 | + # if the activity is still active |
| 104 | + if len(facts) > 0 and facts[-1].end_time is None: |
| 105 | + timedelta_secs = (datetime.datetime.now() - facts[-1].start_time).seconds |
| 106 | + hours, rem = divmod(timedelta_secs, 60 * 60) |
| 107 | + minutes, seconds = divmod(rem, 60) |
| 108 | + if hours != 0: |
| 109 | + msg = str.format("Working on {} for {} hours and {} minutes", facts[-1].activity, hours, minutes) |
| 110 | + else: |
| 111 | + msg = str.format("Working on {} for {} minutes", facts[-1].activity, minutes) |
| 112 | + self.notification.show(msg) |
| 113 | + elif conf.notify_on_idle: |
| 114 | + self.notification.show("No Activity") |
| 115 | + |
| 116 | + self.notification.close() |
| 117 | + |
| 118 | + self.minutes_passed = 0 |
| 119 | + |
| 120 | + return True |
| 121 | + |
| 122 | + def send_test(self): |
| 123 | + return self.notification.show("This is a test notification!") |
| 124 | + |
| 125 | + |
| 126 | +notifs_mgr = NotificationsManager() |
0 commit comments