diff --git a/safeeyes/__main__.py b/safeeyes/__main__.py index 2eab4cd7..a3c11714 100755 --- a/safeeyes/__main__.py +++ b/safeeyes/__main__.py @@ -25,9 +25,7 @@ import logging import signal import sys -from threading import Timer -import gi import psutil from safeeyes import utility from safeeyes.model import Config @@ -35,9 +33,6 @@ from safeeyes.safeeyes import SAFE_EYES_VERSION from safeeyes.rpc import RPCClient -gi.require_version('Gtk', '3.0') -from gi.repository import Gtk - gettext.install('safeeyes', utility.LOCALE_PATH) @@ -68,22 +63,6 @@ def __running(): return False -def __evaluate_arguments(args, safe_eyes): - """ - Evaluate the arguments and execute the operations. - """ - if args.about: - utility.execute_main_thread(safe_eyes.show_about) - elif args.disable: - utility.execute_main_thread(safe_eyes.disable_safeeyes) - elif args.enable: - utility.execute_main_thread(safe_eyes.enable_safeeyes) - elif args.settings: - utility.execute_main_thread(safe_eyes.show_settings) - elif args.take_break: - utility.execute_main_thread(safe_eyes.take_break) - - def main(): """ Start the Safe Eyes. @@ -147,10 +126,8 @@ def main(): sys.exit(0) elif not args.quit: logging.info("Starting Safe Eyes") - safe_eyes = SafeEyes(system_locale, config) + safe_eyes = SafeEyes(system_locale, config, args) safe_eyes.start() - Timer(1.0, lambda: __evaluate_arguments(args, safe_eyes)).start() - Gtk.main() if __name__ == '__main__': diff --git a/safeeyes/plugins/trayicon/plugin.py b/safeeyes/plugins/trayicon/plugin.py index 4d2ba09f..405dd89d 100644 --- a/safeeyes/plugins/trayicon/plugin.py +++ b/safeeyes/plugins/trayicon/plugin.py @@ -77,9 +77,6 @@ def __init__(self, context, plugin_config): # Next break info menu item self.item_info = Gtk.MenuItem() - #img_timer = Gtk.Image() - #img_timer.set_from_icon_name("io.github.slgobinath.SafeEyes-timer", 16) - #self.item_info.set_image(img_timer) self.item_separator = Gtk.SeparatorMenuItem() diff --git a/safeeyes/safeeyes.py b/safeeyes/safeeyes.py index 0a7bcaea..883d6597 100644 --- a/safeeyes/safeeyes.py +++ b/safeeyes/safeeyes.py @@ -38,17 +38,21 @@ from safeeyes.ui.settings_dialog import SettingsDialog gi.require_version('Gtk', '3.0') -from gi.repository import Gtk +from gi.repository import Gtk, Gio SAFE_EYES_VERSION = "2.1.6" -class SafeEyes: +class SafeEyes(Gtk.Application): """ This class represents a runnable Safe Eyes instance. """ - def __init__(self, system_locale, config): + def __init__(self, system_locale, config, cli_args): + super().__init__( + application_id="io.github.slgobinath.SafeEyes", + flags=Gio.ApplicationFlags.IS_SERVICE + ) self.active = False self.break_screen = None self.safe_eyes_core = None @@ -58,6 +62,7 @@ def __init__(self, system_locale, config): self.settings_dialog_active = False self.rpc_server = None self._status = '' + self.cli_args = cli_args # Initialize the Safe Eyes Context self.context['version'] = SAFE_EYES_VERSION @@ -98,6 +103,9 @@ def __init__(self, system_locale, config): self.context['api']['postpone'] = self.safe_eyes_core.postpone self.context['api']['get_break_time'] = self.safe_eyes_core.get_break_time self.plugins_manager.init(self.context, self.config) + + self.hold() + atexit.register(self.persist_session) def start(self): @@ -114,6 +122,22 @@ def start(self): self.safe_eyes_core.start() self.handle_system_suspend() + self.run() + + def do_startup(self): + Gtk.Application.do_startup(self) + + if self.cli_args.about: + self.show_about() + elif self.cli_args.disable: + self.disable_safeeyes() + elif self.cli_args.enable: + self.enable_safeeyes() + elif self.cli_args.settings: + self.show_settings() + elif self.cli_args.take_break: + self.take_break() + def show_settings(self): """ Listen to tray icon Settings action and send the signal to Settings dialog. @@ -144,9 +168,8 @@ def quit(self): self.plugins_manager.exit() self.__stop_rpc_server() self.persist_session() - Gtk.main_quit() - # Exit all threads - os._exit(0) + + super().quit() def handle_suspend_callback(self, sleeping): """