2017-03-23 01:07:51 +01:00
|
|
|
#!/usr/bin/env python3
|
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-11 01:32:43 +02:00
|
|
|
import os, gi, json, shutil, dbus, logging, operator, psutil, sys
|
2016-11-09 04:08:21 +01:00
|
|
|
from threading import Timer
|
2016-10-30 10:44:15 +01:00
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
2016-10-15 06:11:27 +02:00
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
2017-04-11 01:32:43 +02:00
|
|
|
from safeeyes.AboutDialog import AboutDialog
|
|
|
|
from safeeyes.BreakScreen import BreakScreen
|
|
|
|
from safeeyes.Notification import Notification
|
2017-04-12 16:57:54 +02:00
|
|
|
from safeeyes.Plugins import Plugins
|
2017-04-11 01:32:43 +02:00
|
|
|
from safeeyes.SafeEyesCore import SafeEyesCore
|
|
|
|
from safeeyes.SettingsDialog import SettingsDialog
|
|
|
|
from safeeyes.TrayIcon import TrayIcon
|
|
|
|
from safeeyes import Utility
|
2016-10-15 06:11:27 +02:00
|
|
|
|
|
|
|
# Define necessary paths
|
2017-04-12 16:57:54 +02:00
|
|
|
config_file_path = os.path.join(Utility.config_directory, 'safeeyes.json')
|
|
|
|
style_sheet_path = os.path.join(Utility.config_directory, 'style/safeeyes_style.css')
|
|
|
|
log_file_path = os.path.join(Utility.config_directory, 'safeeyes.log')
|
2017-02-12 14:36:09 +01:00
|
|
|
break_screen_glade = os.path.join(Utility.bin_directory, "glade/break_screen.glade")
|
|
|
|
settings_dialog_glade = os.path.join(Utility.bin_directory, "glade/settings_dialog.glade")
|
|
|
|
about_dialog_glade = os.path.join(Utility.bin_directory, "glade/about_dialog.glade")
|
|
|
|
system_config_file_path = os.path.join(Utility.bin_directory, "config/safeeyes.json")
|
|
|
|
system_style_sheet_path = os.path.join(Utility.bin_directory, "config/style/safeeyes_style.css")
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2016-10-30 10:44:15 +01:00
|
|
|
is_active = True
|
2017-03-10 19:45:54 +01:00
|
|
|
CONFIGURATION_VERSION = 4
|
2017-04-19 01:34:22 +02:00
|
|
|
SAFE_EYES_VERSION = "1.2.0"
|
2016-10-30 10:44:15 +01:00
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Listen to tray icon Settings action and send the signal to Settings dialog.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
def show_settings():
|
2016-11-08 14:17:48 +01:00
|
|
|
logging.info("Show Settings dialog")
|
2017-03-13 14:30:44 +01:00
|
|
|
settings_dialog = SettingsDialog(config, language, Utility.read_lang_files(), save_settings, settings_dialog_glade)
|
2016-10-15 06:11:27 +02:00
|
|
|
settings_dialog.show()
|
|
|
|
|
2016-12-26 01:29:30 +01:00
|
|
|
"""
|
|
|
|
Listen to tray icon About action and send the signal to About dialog.
|
|
|
|
"""
|
|
|
|
def show_about():
|
|
|
|
logging.info("Show About dialog")
|
|
|
|
about_dialog = AboutDialog(about_dialog_glade, SAFE_EYES_VERSION)
|
|
|
|
about_dialog.show()
|
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Receive the signal from core and pass it to the Notification.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
def show_notification():
|
2017-03-25 02:22:47 +01:00
|
|
|
if config['strict_break']:
|
|
|
|
Utility.execute_main_thread(tray_icon.lock_menu)
|
2017-04-12 16:57:54 +02:00
|
|
|
plugins.pre_notification(context)
|
2016-10-15 06:11:27 +02:00
|
|
|
notification.show(config['pre_break_warning_time'])
|
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Receive the break signal from core and pass it to the break screen.
|
|
|
|
"""
|
2017-04-07 22:20:23 +02:00
|
|
|
def show_alert(message, image_name):
|
2016-11-08 14:17:48 +01:00
|
|
|
logging.info("Show the break screen")
|
2016-10-15 06:11:27 +02:00
|
|
|
notification.close()
|
2017-04-12 16:57:54 +02:00
|
|
|
plugins_data = plugins.pre_break(context)
|
|
|
|
break_screen.show_message(message, Utility.get_resource_path(image_name), plugins_data)
|
2017-03-25 02:22:47 +01:00
|
|
|
if config['strict_break'] and is_active:
|
|
|
|
Utility.execute_main_thread(tray_icon.unlock_menu)
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Receive the stop break signal from core and pass it to the break screen.
|
|
|
|
"""
|
2017-02-12 14:36:09 +01:00
|
|
|
def close_alert(audible_alert_on):
|
2016-11-08 14:17:48 +01:00
|
|
|
logging.info("Close the break screen")
|
2017-04-09 02:29:51 +02:00
|
|
|
if config['enable_screen_lock'] and context['break_type'] == 'long':
|
|
|
|
# Lock the screen before closing the break screen
|
2017-04-12 23:46:30 +02:00
|
|
|
Utility.lock_desktop(config['lock_screen_command'])
|
2016-10-15 06:11:27 +02:00
|
|
|
break_screen.close()
|
2017-02-12 14:36:09 +01:00
|
|
|
if audible_alert_on:
|
2017-02-01 01:20:17 +01:00
|
|
|
Utility.play_notification()
|
2017-04-12 16:57:54 +02:00
|
|
|
plugins.post_break(context)
|
2016-10-15 06:11:27 +02:00
|
|
|
|
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Listen to the tray menu quit action and stop the core, notification and the app itself.
|
|
|
|
"""
|
2016-11-08 14:17:48 +01:00
|
|
|
def on_quit():
|
|
|
|
logging.info("Quit Safe Eyes")
|
2017-04-12 16:57:54 +02:00
|
|
|
plugins.exit(context)
|
2016-10-15 06:11:27 +02:00
|
|
|
core.stop()
|
|
|
|
notification.quite();
|
|
|
|
Gtk.main_quit()
|
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
If the system goes to sleep, Safe Eyes stop the core if it is already active.
|
|
|
|
If it was active, Safe Eyes will become active after wake up.
|
|
|
|
"""
|
2016-10-30 10:44:15 +01:00
|
|
|
def handle_suspend_callback(sleeping):
|
|
|
|
if sleeping:
|
|
|
|
# Sleeping / suspending
|
|
|
|
if is_active:
|
|
|
|
core.stop()
|
2016-11-08 14:17:48 +01:00
|
|
|
logging.info("Stopped Safe Eyes due to system suspend")
|
2016-10-30 10:44:15 +01:00
|
|
|
else:
|
|
|
|
# Resume from sleep
|
|
|
|
if is_active:
|
|
|
|
core.start()
|
2016-11-08 14:17:48 +01:00
|
|
|
logging.info("Resumed Safe Eyes after system wakeup")
|
2016-10-30 10:44:15 +01:00
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Setup system suspend listener.
|
|
|
|
"""
|
2016-10-30 10:44:15 +01:00
|
|
|
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-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Listen to break screen Skip action and send the signal to core.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
def on_skipped():
|
2016-11-08 14:17:48 +01:00
|
|
|
logging.info("User skipped the break")
|
2017-04-09 02:29:51 +02:00
|
|
|
if config['enable_screen_lock'] and context['break_type'] == 'long' and context.get('count_down', 0) >= config['time_to_screen_lock']:
|
|
|
|
# Lock the screen before closing the break screen
|
2017-04-12 23:46:30 +02:00
|
|
|
Utility.lock_desktop(config['lock_screen_command'])
|
2016-10-23 13:56:54 +02:00
|
|
|
core.skip_break()
|
2017-04-18 02:44:23 +02:00
|
|
|
plugins.post_break(context)
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2017-04-03 20:18:23 +02:00
|
|
|
"""
|
|
|
|
Listen to break screen Postpone action and send the signal to core.
|
|
|
|
"""
|
|
|
|
def on_postponed():
|
|
|
|
logging.info("User postponed the break")
|
2017-04-09 02:29:51 +02:00
|
|
|
if config['enable_screen_lock'] and context['break_type'] == 'long' and context.get('count_down', 0) >= config['time_to_screen_lock']:
|
|
|
|
# Lock the screen before closing the break screen
|
2017-04-12 23:46:30 +02:00
|
|
|
Utility.lock_desktop(config['lock_screen_command'])
|
2017-04-03 20:18:23 +02:00
|
|
|
core.postpone_break()
|
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Listen to Settings dialog Save action and write to the config file.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
def save_settings(config):
|
2017-01-10 12:52:55 +01:00
|
|
|
global language
|
|
|
|
|
2016-11-08 14:17:48 +01:00
|
|
|
logging.info("Saving settings to safeeyes.json")
|
2017-01-10 12:52:55 +01:00
|
|
|
|
|
|
|
# Stop the Safe Eyes core
|
2016-11-09 04:08:21 +01:00
|
|
|
if is_active:
|
|
|
|
core.stop()
|
2017-01-10 12:52:55 +01:00
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
# Write the configuration to file
|
|
|
|
with open(config_file_path, 'w') as config_file:
|
|
|
|
json.dump(config, config_file, indent=4, sort_keys=True)
|
2017-01-10 12:52:55 +01:00
|
|
|
|
|
|
|
# Reload the language translation
|
2017-03-13 14:30:44 +01:00
|
|
|
language = Utility.load_language(config['language'])
|
2017-02-25 13:23:08 +01:00
|
|
|
|
2017-01-10 12:52:55 +01:00
|
|
|
tray_icon.set_labels(language)
|
2017-02-25 13:23:08 +01:00
|
|
|
|
2016-11-09 04:08:21 +01:00
|
|
|
logging.info("Initialize SafeEyesCore with modified settings")
|
2017-01-10 12:52:55 +01:00
|
|
|
|
|
|
|
# Restart the core and intialize the components
|
2016-11-04 11:27:47 +01:00
|
|
|
core.initialize(config, language)
|
|
|
|
break_screen.initialize(config, language)
|
2016-11-09 04:08:21 +01:00
|
|
|
if is_active:
|
|
|
|
# 1 sec delay is required to give enough time for core to be stopped
|
|
|
|
Timer(1.0, core.start).start()
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Listen to tray icon enable action and send the signal to core.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
def enable_safeeyes():
|
2017-01-15 03:11:31 +01:00
|
|
|
global is_active
|
2016-10-30 10:44:15 +01:00
|
|
|
is_active = True
|
2016-12-25 14:04:12 +01:00
|
|
|
core.start()
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Listen to tray icon disable action and send the signal to core.
|
|
|
|
"""
|
2016-10-15 06:11:27 +02:00
|
|
|
def disable_safeeyes():
|
2017-01-15 03:11:31 +01:00
|
|
|
global is_active
|
2016-10-30 10:44:15 +01:00
|
|
|
is_active = False
|
2016-12-25 14:04:12 +01:00
|
|
|
core.stop()
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2016-11-15 14:06:02 +01:00
|
|
|
"""
|
|
|
|
Initialize the configuration directory and copy the files to ~/.config directory.
|
|
|
|
"""
|
2016-11-15 05:26:48 +01:00
|
|
|
def initialize_config():
|
|
|
|
global config
|
2017-02-12 14:36:09 +01:00
|
|
|
config_dir_path = os.path.join(Utility.home_directory, '.config/safeeyes/style')
|
|
|
|
startup_dir_path = os.path.join(Utility.home_directory, '.config/autostart')
|
2017-02-25 13:23:08 +01:00
|
|
|
|
2017-02-10 02:45:49 +01:00
|
|
|
Utility.mkdir(config_dir_path)
|
2016-10-15 06:11:27 +02:00
|
|
|
|
|
|
|
if not os.path.isfile(config_file_path):
|
2017-04-12 23:46:30 +02:00
|
|
|
# Copy the safeeyes.json
|
2016-10-15 06:11:27 +02:00
|
|
|
shutil.copy2(system_config_file_path, config_file_path)
|
2016-10-15 19:10:08 +02:00
|
|
|
|
2017-04-12 23:46:30 +02:00
|
|
|
# Overwrite the startup file only if config file is replaced
|
2017-02-10 02:45:49 +01:00
|
|
|
Utility.mkdir(startup_dir_path)
|
2016-10-15 19:10:08 +02:00
|
|
|
|
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
|
|
|
|
2017-04-12 23:46:30 +02:00
|
|
|
# Copy the safeeyes_style.css
|
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)
|
|
|
|
|
2016-11-15 05:26:48 +01:00
|
|
|
# Read the configuration
|
2017-02-25 13:23:08 +01:00
|
|
|
with open(config_file_path) as config_file:
|
2016-11-15 05:26:48 +01:00
|
|
|
config = json.load(config_file)
|
|
|
|
|
|
|
|
"""
|
|
|
|
Configuration file has a version config_version.
|
|
|
|
It is used to overwrite the exsiting config file if there is an update.
|
2017-03-13 14:30:44 +01:00
|
|
|
Earlier versions did not have this attribute so the following method
|
2016-11-15 05:26:48 +01:00
|
|
|
checks the version and if it mismatches, it will overwrite the exsiting
|
2017-03-13 14:30:44 +01:00
|
|
|
config files. If the version property is not available, the file is
|
2016-11-15 05:26:48 +01:00
|
|
|
considered as an older one and replaced by the new configuration file.
|
|
|
|
"""
|
|
|
|
def validate_config():
|
|
|
|
version_mismatch = False
|
|
|
|
try:
|
|
|
|
# Check the config version
|
|
|
|
config_version = config['meta']['config_version']
|
|
|
|
version_mismatch = config_version is not CONFIGURATION_VERSION
|
|
|
|
except:
|
|
|
|
version_mismatch = True
|
|
|
|
|
|
|
|
if version_mismatch:
|
2017-04-12 23:46:30 +02:00
|
|
|
# Remove ~/.config/safeeyes/safeeyes.json file
|
2016-11-15 05:26:48 +01:00
|
|
|
try:
|
2017-04-12 23:46:30 +02:00
|
|
|
os.remove(config_file_path)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Remove ~/.config/safeeyes/style/safeeyes_style.css file
|
|
|
|
try:
|
|
|
|
os.remove(style_sheet_path)
|
2016-11-15 05:26:48 +01:00
|
|
|
except:
|
|
|
|
pass
|
2017-02-23 23:41:02 +01:00
|
|
|
|
2016-11-15 05:26:48 +01:00
|
|
|
# Remove startup script
|
|
|
|
try:
|
2017-02-12 14:36:09 +01:00
|
|
|
os.remove(os.path.join(Utility.home_directory, '.config/autostart/safeeyes.desktop'))
|
2016-11-15 05:26:48 +01:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Create config files again
|
|
|
|
initialize_config()
|
|
|
|
|
2017-01-10 12:52:55 +01:00
|
|
|
|
2017-02-23 23:41:02 +01:00
|
|
|
def running():
|
|
|
|
'''
|
|
|
|
Check if SafeEyes is already running.
|
|
|
|
'''
|
2017-02-25 13:23:08 +01:00
|
|
|
process_count = 0
|
2017-02-23 23:41:02 +01:00
|
|
|
for proc in psutil.process_iter():
|
2017-03-23 01:07:51 +01:00
|
|
|
try:
|
2017-02-23 23:41:02 +01:00
|
|
|
# Check if safeeyes is in process arguments
|
2017-03-10 20:22:13 +01:00
|
|
|
cmd_line = proc.cmdline()
|
2017-04-11 03:18:47 +02:00
|
|
|
if 'python3' in cmd_line[0] and 'safeeyes' in cmd_line[1]:
|
2017-02-25 13:23:08 +01:00
|
|
|
process_count += 1
|
|
|
|
if process_count > 1:
|
|
|
|
return True
|
|
|
|
|
2017-02-23 23:41:02 +01:00
|
|
|
# Ignore if process does not exist or does not have command line args
|
|
|
|
except (IndexError, psutil.NoSuchProcess):
|
|
|
|
pass
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2016-10-15 06:11:27 +02:00
|
|
|
def main():
|
2016-11-15 05:26:48 +01:00
|
|
|
initialize_config()
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2016-11-08 14:17:48 +01:00
|
|
|
# Configure logging. Reset with every restart
|
2017-02-16 00:29:14 +01:00
|
|
|
logging.basicConfig(format='%(asctime)s [%(levelname)s]:[%(threadName)s] %(message)s', filename=log_file_path, filemode='w', level=logging.INFO)
|
2016-11-08 14:17:48 +01:00
|
|
|
logging.info("Starting Safe Eyes")
|
|
|
|
|
2017-02-23 23:41:02 +01:00
|
|
|
if not running():
|
|
|
|
validate_config()
|
2016-11-15 05:26:48 +01:00
|
|
|
|
2017-02-23 23:41:02 +01:00
|
|
|
global break_screen
|
|
|
|
global core
|
|
|
|
global notification
|
|
|
|
global tray_icon
|
|
|
|
global language
|
2017-04-09 02:29:51 +02:00
|
|
|
global context
|
2017-04-12 16:57:54 +02:00
|
|
|
global plugins
|
2016-10-30 10:44:15 +01:00
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
context = {}
|
2017-03-13 14:30:44 +01:00
|
|
|
language = Utility.load_language(config['language'])
|
2017-02-23 23:41:02 +01:00
|
|
|
|
2017-04-09 02:29:51 +02:00
|
|
|
# Initialize the Safe Eyes Context
|
|
|
|
context['version'] = SAFE_EYES_VERSION
|
|
|
|
|
2017-02-23 23:41:02 +01:00
|
|
|
tray_icon = TrayIcon(config, language, show_settings, show_about, enable_safeeyes, disable_safeeyes, on_quit)
|
2017-04-03 20:18:23 +02:00
|
|
|
break_screen = BreakScreen(on_skipped, on_postponed, break_screen_glade, style_sheet_path)
|
2017-02-23 23:41:02 +01:00
|
|
|
break_screen.initialize(config, language)
|
2017-04-12 16:57:54 +02:00
|
|
|
notification = Notification(language)
|
|
|
|
plugins = Plugins(config)
|
2017-04-12 23:46:30 +02:00
|
|
|
core = SafeEyesCore(context, show_notification, show_alert, close_alert, break_screen.show_count_down, tray_icon.next_break_time)
|
2017-02-23 23:41:02 +01:00
|
|
|
core.initialize(config, language)
|
2017-04-12 16:57:54 +02:00
|
|
|
plugins.start(context) # Call the start method of all plugins
|
2017-02-23 23:41:02 +01:00
|
|
|
core.start()
|
|
|
|
|
|
|
|
handle_system_suspend()
|
|
|
|
|
|
|
|
Gtk.main()
|
|
|
|
else:
|
|
|
|
logging.info('SafeEyes is already running')
|
|
|
|
sys.exit(0)
|
2016-10-15 06:11:27 +02:00
|
|
|
|
2017-02-23 23:41:02 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|