SafeEyes/safeeyes/safeeyes/safeeyes

183 lines
5.4 KiB
Plaintext
Raw Normal View History

2016-10-23 18:40:41 +02:00
#!/usr/bin/env python2
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/>.
import os
import gi
import json
import shutil
import errno
2016-10-30 10:44:15 +01:00
import dbus
from dbus.mainloop.glib import DBusGMainLoop
2016-10-15 06:11:27 +02:00
from BreakScreen import BreakScreen
from TrayIcon import TrayIcon
from SettingsDialog import SettingsDialog
from SafeEyesCore import SafeEyesCore
from Notification import Notification
2016-10-30 10:44:15 +01:00
2016-10-15 06:11:27 +02:00
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Define necessary paths
bin_directory = os.path.dirname(os.path.realpath(__file__))
config_file_path = os.path.join(os.path.expanduser('~'), '.config/safeeyes/safeeyes.json')
style_sheet_path = os.path.join(os.path.expanduser('~'), '.config/safeeyes/style/safeeyes_style.css')
break_screen_glade = os.path.join(bin_directory, "glade/break_screen.glade")
settings_dialog_glade = os.path.join(bin_directory, "glade/settings_dialog.glade")
system_config_file_path = os.path.join(bin_directory, "config/safeeyes.json")
system_style_sheet_path = os.path.join(bin_directory, "config/style/safeeyes_style.css")
2016-11-04 09:47:21 +01:00
system_language_directory = os.path.join(bin_directory, "config/lang")
2016-10-15 06:11:27 +02:00
2016-10-30 10:44:15 +01:00
is_active = True
2016-10-15 06:11:27 +02:00
def show_settings():
2016-11-04 09:53:14 +01:00
settings_dialog = SettingsDialog(config, language, save_settings, settings_dialog_glade)
2016-10-15 06:11:27 +02:00
settings_dialog.show()
def show_notification():
notification.show(config['pre_break_warning_time'])
def show_alert(message):
notification.close()
# Hide and show tray icon if strict break is on
# This feature is required because, by disabling Safe Eyes,
# user can skip the break.
# if config['strict_break']:
# tray_icon.hide_icon()
2016-10-15 06:11:27 +02:00
break_screen.show_message(message)
def close_alert():
# Hide and show tray icon if strict break is on
# This feature is required because, by disabling Safe Eyes,
# user can skip the break.
# if config['strict_break']:
# tray_icon.show_icon()
2016-10-15 06:11:27 +02:00
break_screen.close()
def on_countdown(count):
break_screen.show_count_down(count)
def on_quite():
core.stop()
notification.quite();
Gtk.main_quit()
2016-10-30 10:44:15 +01:00
def handle_suspend_callback(sleeping):
if sleeping:
# Sleeping / suspending
if is_active:
core.stop()
else:
# Resume from sleep
if is_active:
core.start()
def handle_system_suspend():
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
bus.add_signal_receiver(handle_suspend_callback, 'PrepareForSleep', 'org.freedesktop.login1.Manager', 'org.freedesktop.login1')
2016-10-15 06:11:27 +02:00
def on_skipped():
# Hide and show tray icon if strict break is on
# This feature is required because, by disabling Safe Eyes,
# user can skip the break.
if config['strict_break']:
tray_icon.show_icon()
core.skip_break()
2016-10-15 06:11:27 +02:00
def save_settings(config):
# Write the configuration to file
with open(config_file_path, 'w') as config_file:
json.dump(config, config_file, indent=4, sort_keys=True)
# Restart the core and intialize the components
core.initialize(config)
break_screen.initialize(config)
def enable_safeeyes():
2016-10-30 10:44:15 +01:00
is_active = True
core.toggle_active_state()
2016-10-15 06:11:27 +02:00
def disable_safeeyes():
2016-10-30 10:44:15 +01:00
is_active = False
core.toggle_active_state()
2016-10-15 06:11:27 +02:00
def prepare_local_config_dir():
config_dir_path = os.path.join(os.path.expanduser('~'), '.config/safeeyes/style')
2016-10-15 19:10:08 +02:00
startup_dir_path = os.path.join(os.path.expanduser('~'), '.config/autostart')
2016-10-15 06:11:27 +02:00
try:
os.makedirs(config_dir_path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(config_dir_path):
pass
else:
raise
if not os.path.isfile(config_file_path):
shutil.copy2(system_config_file_path, config_file_path)
2016-10-15 19:10:08 +02:00
try:
os.makedirs(startup_dir_path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(startup_dir_path):
pass
else:
raise
2016-10-15 06:11:27 +02:00
# Add to startup for the first time only
2016-10-25 16:14:50 +02:00
try:
os.symlink("/usr/share/applications/safeeyes.desktop", os.path.join(startup_dir_path, "safeeyes.desktop"))
except OSError as exc:
pass
2016-10-15 06:11:27 +02:00
if not os.path.isfile(style_sheet_path):
shutil.copy2(system_style_sheet_path, style_sheet_path)
def main():
prepare_local_config_dir()
global break_screen
global core
global notification
global config
global tray_icon
2016-11-04 09:47:21 +01:00
global language
2016-10-15 06:11:27 +02:00
# Read the configuration
with open(config_file_path) as config_file:
config = json.load(config_file)
2016-11-04 09:47:21 +01:00
language_file_path = os.path.join(system_language_directory, str(config['language']) + '.json')
with open(language_file_path) as language_file:
language = json.load(language_file)
tray_icon = TrayIcon(language, show_settings, enable_safeeyes, disable_safeeyes, on_quite)
2016-10-15 06:11:27 +02:00
break_screen = BreakScreen(on_skipped, break_screen_glade, style_sheet_path)
2016-11-04 09:47:21 +01:00
break_screen.initialize(config, language)
2016-10-15 06:11:27 +02:00
core = SafeEyesCore(show_notification, show_alert, close_alert, on_countdown)
2016-11-04 09:47:21 +01:00
core.initialize(config, language)
2016-10-15 06:11:27 +02:00
core.start()
2016-11-04 09:47:21 +01:00
notification = Notification(language)
2016-10-15 06:11:27 +02:00
2016-11-04 09:53:14 +01:00
handle_system_suspend()
2016-10-30 10:44:15 +01:00
2016-10-15 06:11:27 +02:00
Gtk.main()
main()