-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix notifications #521
Open
sh-zam
wants to merge
1
commit into
projecthamster:master
Choose a base branch
from
sh-zam:fix-notifications
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix notifications #521
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# - coding: utf-8 - | ||
|
||
# Copyright (C) 2020 Sharaf Zaman <[email protected]> | ||
|
||
# This file is part of Project Hamster. | ||
|
||
# Project Hamster 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. | ||
|
||
# Project Hamster 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 Project Hamster. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import logging | ||
logger = logging.getLogger(__name__) # noqa: E402 | ||
import datetime | ||
import dbus | ||
import hamster.client | ||
|
||
from gi.repository import Gtk | ||
from gi.repository import GObject as gobject | ||
from hamster.lib.configuration import conf | ||
|
||
|
||
class Notification(object): | ||
def __init__(self): | ||
self.bus = dbus.SessionBus() | ||
self.appname = "Hamster Time Tracker" | ||
self.replace_id = 0 | ||
self.summary = "Hamster Time Tracker" | ||
self.hints = {} | ||
self.actions = [] | ||
self.data = {} | ||
self.timeout = -1 | ||
|
||
info = Gtk.IconTheme().lookup_icon("hamster-time-tracker", 0, 0) | ||
self.icon = info.get_filename() | ||
|
||
def show(self, message): | ||
""" | ||
Show notitification | ||
returns: True if successful | ||
""" | ||
try: | ||
self.server = dbus.Interface(self.bus.get_object("org.freedesktop.Notifications", | ||
"/org/freedesktop/Notifications"), | ||
dbus_interface="org.freedesktop.Notifications") | ||
except dbus.exceptions.DBusException as e: | ||
# TODO: Log? | ||
logger.error(e) | ||
logger.warning("Notifications will be disabled") | ||
return False | ||
|
||
try: | ||
self.notif_id = self.server.Notify( | ||
self.appname, | ||
self.replace_id, | ||
self.icon, | ||
self.summary, | ||
message, | ||
self.actions, | ||
self.hints, | ||
self.timeout) | ||
except: | ||
return False | ||
|
||
return True | ||
|
||
def close(self): | ||
try: self.server.CloseNotification(self.notif_id) | ||
except: pass | ||
|
||
|
||
|
||
class NotificationsManager(gobject.GObject): | ||
def __init__(self): | ||
self.notify_interval = conf.notify_interval | ||
self.minutes_passed = 0 | ||
self.notification = Notification() | ||
gobject.timeout_add_seconds(60, self.check_interval) | ||
|
||
def notify_interval_changed(self, value): | ||
self.minutes_passed = 0 | ||
self.notify_interval = value | ||
|
||
def check_interval(self): | ||
if not conf.notifications_enabled: | ||
self.minutes_passed = 0 | ||
return True | ||
|
||
self.minutes_passed += 1 | ||
|
||
storage = hamster.client.Storage() | ||
facts = storage.get_todays_facts() | ||
|
||
if self.minutes_passed == self.notify_interval: | ||
# if the activity is still active | ||
if len(facts) > 0 and facts[-1].end_time is None: | ||
timedelta_secs = (datetime.datetime.now() - facts[-1].start_time).seconds | ||
hours, rem = divmod(timedelta_secs, 60 * 60) | ||
minutes, seconds = divmod(rem, 60) | ||
if hours != 0: | ||
msg = str.format("Working on {} for {} hours and {} minutes", facts[-1].activity, hours, minutes) | ||
else: | ||
msg = str.format("Working on {} for {} minutes", facts[-1].activity, minutes) | ||
self.notification.show(msg) | ||
elif conf.notify_on_idle: | ||
self.notification.show("No Activity") | ||
|
||
self.notification.close() | ||
|
||
self.minutes_passed = 0 | ||
|
||
return True | ||
|
||
def send_test(self): | ||
return self.notification.show("This is a test notification!") | ||
|
||
|
||
notifs_mgr = NotificationsManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notifs_mgr
seems to be referenced nowhere in this file. What am I missing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must be remnants of my debugging session. I'll remove it, thanks!