Port to GtkApplication

This commit is contained in:
Patrick Ulbrich
2016-06-22 19:01:08 +02:00
parent 9d97eb2292
commit 66c2d21322
4 changed files with 87 additions and 63 deletions

View File

@@ -3,7 +3,7 @@
#
# utils.py
#
# Copyright 2011 - 2015 Patrick Ulbrich <zulu99@gmx.net>
# Copyright 2011 - 2016 Patrick Ulbrich <zulu99@gmx.net>
# Copyright 2007 Marco Ferragina <marco.ferragina@gmail.com>
#
# 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:

View File

@@ -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()

View File

@@ -2,29 +2,7 @@
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkListStore" id="liststore_accounts">
<columns>
<!-- column-name account_obj -->
<column type="PyObject"/>
<!-- column-name account_enabled -->
<column type="gboolean"/>
<!-- column-name account_name -->
<column type="gchararray"/>
</columns>
<signal name="row-deleted" handler="liststore_accounts_row_deleted" swapped="no"/>
<signal name="row-inserted" handler="liststore_accounts_row_inserted" swapped="no"/>
</object>
<object class="GtkListStore" id="liststore_plugins">
<columns>
<!-- column-name plugin_obj -->
<column type="PyObject"/>
<!-- column-name plugin_enabled -->
<column type="gboolean"/>
<!-- column-name plugin_name -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="config_window">
<object class="GtkApplicationWindow" id="config_window">
<property name="can_focus">False</property>
<property name="default_width">580</property>
<property name="default_height">420</property>
@@ -225,4 +203,26 @@
</object>
</child>
</object>
<object class="GtkListStore" id="liststore_accounts">
<columns>
<!-- column-name account_obj -->
<column type="PyObject"/>
<!-- column-name account_enabled -->
<column type="gboolean"/>
<!-- column-name account_name -->
<column type="gchararray"/>
</columns>
<signal name="row-deleted" handler="liststore_accounts_row_deleted" swapped="no"/>
<signal name="row-inserted" handler="liststore_accounts_row_inserted" swapped="no"/>
</object>
<object class="GtkListStore" id="liststore_plugins">
<columns>
<!-- column-name plugin_obj -->
<column type="PyObject"/>
<!-- column-name plugin_enabled -->
<column type="gboolean"/>
<!-- column-name plugin_name -->
<column type="gchararray"/>
</columns>
</object>
</interface>

View File

@@ -4,7 +4,6 @@
# mailnag-config
#
# Copyright 2011 - 2016 Patrick Ulbrich <zulu99@gmx.net>
# Copyright 2011 Ralf Hersel <ralf.hersel@gmx.net>
#
# 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()