2016-10-15 06:11:27 +02:00
|
|
|
# Safe Eyes is a utility to remind you to take break frequently
|
|
|
|
# to protect your eyes from eye strain.
|
|
|
|
|
|
|
|
# Copyright (C) 2016 Gobinath
|
|
|
|
|
|
|
|
# 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/>.
|
|
|
|
|
2017-04-10 19:39:50 +02:00
|
|
|
import gi, logging
|
2016-10-15 06:11:27 +02:00
|
|
|
gi.require_version('Notify', '0.7')
|
|
|
|
from gi.repository import Notify
|
2017-04-11 01:32:43 +02:00
|
|
|
from safeeyes import Utility
|
2017-02-01 01:20:17 +01:00
|
|
|
|
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
APPINDICATOR_ID = 'safeeyes'
|
|
|
|
|
2017-07-05 21:59:04 +02:00
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
class Notification:
|
2017-02-01 01:20:17 +01:00
|
|
|
"""
|
2017-07-06 23:58:08 +02:00
|
|
|
This class is responsible for the notification to the user before the break.
|
2017-02-01 01:20:17 +01:00
|
|
|
"""
|
2017-04-10 19:39:50 +02:00
|
|
|
|
2017-07-02 20:05:48 +02:00
|
|
|
def __init__(self, context, language):
|
2017-04-10 19:39:50 +02:00
|
|
|
logging.info('Initialize the notification')
|
2016-10-15 06:11:27 +02:00
|
|
|
Notify.init(APPINDICATOR_ID)
|
2017-07-02 20:05:48 +02:00
|
|
|
self.context = context
|
2016-11-04 09:47:21 +01:00
|
|
|
self.language = language
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2017-07-02 20:05:48 +02:00
|
|
|
def initialize(self, language):
|
|
|
|
"""
|
|
|
|
Initialize the notification object.
|
|
|
|
"""
|
|
|
|
self.language = language
|
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
def show(self, warning_time):
|
2017-04-10 19:39:50 +02:00
|
|
|
"""
|
|
|
|
Show the notification
|
|
|
|
"""
|
|
|
|
logging.info('Show pre-break notification')
|
2017-07-02 20:05:48 +02:00
|
|
|
|
|
|
|
# Construct the message based on the type of the next break
|
|
|
|
message = '\n'
|
|
|
|
if self.context['break_type'] == 'short':
|
|
|
|
message += self.language['messages']['ready_for_a_short_break'].format(warning_time)
|
|
|
|
else:
|
|
|
|
message += self.language['messages']['ready_for_a_long_break'].format(warning_time)
|
2017-07-05 21:59:04 +02:00
|
|
|
|
2017-07-02 20:05:48 +02:00
|
|
|
self.notification = Notify.Notification.new('Safe Eyes', message, icon='safeeyes_enabled')
|
2017-04-10 19:39:50 +02:00
|
|
|
try:
|
|
|
|
self.notification.show()
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception('Error in showing notification', e)
|
2016-10-15 06:11:27 +02:00
|
|
|
|
|
|
|
def close(self):
|
2017-04-10 19:39:50 +02:00
|
|
|
"""
|
|
|
|
Close the notification if it is not closed by the system already.
|
|
|
|
"""
|
|
|
|
logging.info('Close pre-break notification')
|
2016-10-26 04:03:45 +02:00
|
|
|
try:
|
|
|
|
self.notification.close()
|
|
|
|
except:
|
2017-02-01 01:20:17 +01:00
|
|
|
# Some Linux systems automatically close the notification.
|
2016-10-26 04:03:45 +02:00
|
|
|
pass
|
2016-10-15 06:11:27 +02:00
|
|
|
|
|
|
|
def quite(self):
|
2017-04-10 19:39:50 +02:00
|
|
|
"""
|
|
|
|
Uninitialize the notification. Call this method when closing the application.
|
|
|
|
"""
|
|
|
|
logging.info('Uninitialize Safe Eyes notification')
|
2017-07-05 21:59:04 +02:00
|
|
|
Utility.execute_main_thread(Notify.uninit)
|