From 66c2d2132238a86bbd413add4b2bd6a2dc724963 Mon Sep 17 00:00:00 2001 From: Patrick Ulbrich Date: Wed, 22 Jun 2016 19:01:08 +0200 Subject: [PATCH] Port to GtkApplication --- Mailnag/common/utils.py | 9 ++-- Mailnag/configuration/configwindow.py | 32 ++++++-------- data/config_window.ui | 46 +++++++++---------- mailnag-config | 63 +++++++++++++++++++-------- 4 files changed, 87 insertions(+), 63 deletions(-) diff --git a/Mailnag/common/utils.py b/Mailnag/common/utils.py index cfdda7f..6c6af04 100644 --- a/Mailnag/common/utils.py +++ b/Mailnag/common/utils.py @@ -3,7 +3,7 @@ # # utils.py # -# Copyright 2011 - 2015 Patrick Ulbrich +# Copyright 2011 - 2016 Patrick Ulbrich # Copyright 2007 Marco Ferragina # # This program is free software; you can redistribute it and/or modify @@ -85,7 +85,7 @@ def try_call(f, err_retval = None): return err_retval -def shutdown_existing_instance(): +def shutdown_existing_instance(wait_for_completion = True): bus = dbus.SessionBus() if bus.name_has_owner(DBUS_BUS_NAME): @@ -98,8 +98,9 @@ def shutdown_existing_instance(): shutdown() - while bus.name_has_owner(DBUS_BUS_NAME): - time.sleep(2) + if wait_for_completion: + while bus.name_has_owner(DBUS_BUS_NAME): + time.sleep(2) print 'OK' except: diff --git a/Mailnag/configuration/configwindow.py b/Mailnag/configuration/configwindow.py index 2fb1c9d..fe8de41 100644 --- a/Mailnag/configuration/configwindow.py +++ b/Mailnag/configuration/configwindow.py @@ -32,7 +32,7 @@ from gi.repository import Gtk from Mailnag.common.dist_cfg import PACKAGE_NAME, APP_VERSION, BIN_DIR, DESKTOP_FILE_DIR from Mailnag.common.i18n import _ -from Mailnag.common.utils import get_data_file, get_data_paths +from Mailnag.common.utils import get_data_file from Mailnag.common.config import read_cfg, write_cfg from Mailnag.common.accounts import Account, AccountManager from Mailnag.common.credentialstore import CredentialStore @@ -42,7 +42,7 @@ from Mailnag.configuration.plugindialog import PluginDialog class ConfigWindow: - def __init__(self): + def __init__(self, app): builder = Gtk.Builder() builder.set_translation_domain(PACKAGE_NAME) builder.add_from_file(get_data_file("config_window.ui")) @@ -59,18 +59,13 @@ class ConfigWindow: "treeview_plugins_cursor_changed" : self._on_treeview_plugins_cursor_changed, \ }) - # Add icons in alternative data paths (e.g. ./data/icons) - # to the icon search path in case Mailnag is launched - # from a local directory (without installing). - icon_theme = Gtk.IconTheme.get_default() - for path in get_data_paths(): - icon_theme.append_search_path(os.path.join(path, "icons")) self._window = builder.get_object("config_window") self._window.set_icon_name("mailnag") + self._window.set_application(app) self._cfg = read_cfg() - self.daemon_enabled = False + self._daemon_enabled = False self._switch_daemon_enabled = builder.get_object("switch_daemon_enabled") @@ -130,6 +125,14 @@ class ConfigWindow: self._window.show() + def get_gtk_window(self): + return self._window + + + def get_daemon_enabled(self): + return self._daemon_enabled + + def _load_config(self): self._switch_daemon_enabled.set_active(bool(int(self._cfg.get('core', 'autostart')))) @@ -356,15 +359,8 @@ class ConfigWindow: plugin, model, iter = self._get_selected_plugin() if iter != None: self._button_edit_plugin.set_sensitive(plugin.has_config_ui()) - - - def _save_and_quit(self): - self._save_config() - self.daemon_enabled = self._switch_daemon_enabled.get_active() - Gtk.main_quit() def _on_config_window_deleted(self, widget, event): - self._save_and_quit() - - + self._save_config() + self._daemon_enabled = self._switch_daemon_enabled.get_active() diff --git a/data/config_window.ui b/data/config_window.ui index 329bd1d..1b1accc 100644 --- a/data/config_window.ui +++ b/data/config_window.ui @@ -2,29 +2,7 @@ - - - - - - - - - - - - - - - - - - - - - - - + False 580 420 @@ -225,4 +203,26 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/mailnag-config b/mailnag-config index 2eb4b66..6b75a67 100755 --- a/mailnag-config +++ b/mailnag-config @@ -4,7 +4,6 @@ # mailnag-config # # Copyright 2011 - 2016 Patrick Ulbrich -# Copyright 2011 Ralf Hersel # # 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 @@ -28,34 +27,62 @@ gi.require_version('Gtk', '3.0') import os import subprocess from gi.repository import Gtk -from dbus.mainloop.glib import DBusGMainLoop from Mailnag.common.utils import fix_cwd fix_cwd() -from Mailnag.common.utils import set_procname, shutdown_existing_instance +from Mailnag.common.utils import set_procname, shutdown_existing_instance, get_data_paths from Mailnag.common.dist_cfg import BIN_DIR from Mailnag.configuration.configwindow import ConfigWindow +class App(Gtk.Application): + def __init__(self): + Gtk.Application.__init__(self, application_id = 'com.github.pulp.Mailnag') + self.win = None + + + def do_startup(self): + Gtk.Application.do_startup(self) + + # Add icons in alternative data paths (e.g. ./data/icons) + # to the icon search path in case Mailnag is launched + # from a local directory (without installing). + icon_theme = Gtk.IconTheme.get_default() + for path in get_data_paths(): + icon_theme.append_search_path(os.path.join(path, "icons")) + + + def do_activate(self): + Gtk.Application.do_activate(self) + + if not self.win: + self.win = ConfigWindow(self) + self.win.get_gtk_window().present() + + + def do_shutdown(self): + Gtk.Application.do_shutdown(self) + + if self.win.get_daemon_enabled(): + try: + # the launched daemon shuts down + # an already running daemon + print "Launching Mailnag daemon." + subprocess.Popen(os.path.join(BIN_DIR, "mailnag")) + except: + print "ERROR: Failed to launch Mailnag daemon." + else: + # shutdown running Mailnag daemon + shutdown_existing_instance(wait_for_completion = False) + + def main(): set_procname("mailnag-config") - confwin = ConfigWindow() - Gtk.main() - - if confwin.daemon_enabled: - try: - # the launched daemon shuts down - # an already running daemon - print "Launching Mailnag daemon." - subprocess.Popen(os.path.join(BIN_DIR, "mailnag")) - except: - print "ERROR: Failed to launch Mailnag daemon." - else: - DBusGMainLoop(set_as_default = True) - # shutdown running Mailnag daemon - shutdown_existing_instance() + + app = App() + app.run(None) if __name__ == "__main__": main()