[GTKUI] Refactor shutdown signal code

This commit is contained in:
Calum Lind 2015-11-12 22:03:27 +00:00
parent b754f9f908
commit 471276716b
1 changed files with 22 additions and 36 deletions

View File

@ -145,48 +145,34 @@ DEFAULT_PREFS = {
class GtkUI(object): class GtkUI(object):
def __init__(self, args): def __init__(self, args):
self.daemon_bps = (0, 0, 0) self.daemon_bps = (0, 0, 0)
# Setup btkbuilder/glade translation
# Setup gtkbuilder/glade translation
deluge.common.setup_translations(setup_gettext=False, setup_pygtk=True) deluge.common.setup_translations(setup_gettext=False, setup_pygtk=True)
# Setup signals # Setup signals
try: def on_die(*args):
import gnome.ui log.debug("OS signal 'die' caught with args: %s", args)
import gnome reactor.stop()
# Suppress: Warning: Attempt to add property GnomeProgram::*** after class was initialised
original_filters = warnings.filters[:]
warnings.simplefilter("ignore")
try:
self.gnome_prog = gnome.init("Deluge", deluge.common.get_version())
finally:
warnings.filters = original_filters
self.gnome_client = gnome.ui.master_client()
def on_die(*args):
reactor.stop()
if deluge.common.osx_check() and gtk.gdk.WINDOWING == "quartz":
import gtkosx_application
self.osxapp = gtkosx_application.gtkosx_application_get()
self.osxapp.connect("NSApplicationWillTerminate", on_die)
else:
self.gnome_client.connect("die", on_die)
log.debug("GNOME session 'die' handler registered!")
except Exception as ex:
log.warning("Unable to register a 'die' handler with the GNOME session manager: %s", ex)
if deluge.common.windows_check(): if deluge.common.windows_check():
from win32api import SetConsoleCtrlHandler from win32api import SetConsoleCtrlHandler
from win32con import CTRL_CLOSE_EVENT SetConsoleCtrlHandler(on_die, True)
from win32con import CTRL_SHUTDOWN_EVENT log.debug("Win32 'die' handler registered!")
elif deluge.common.osx_check():
def win_handler(ctrl_type): if gtk.gdk.WINDOWING == "quartz":
log.debug("ctrl_type: %s", ctrl_type) import gtkosx_application
if ctrl_type in (CTRL_CLOSE_EVENT, CTRL_SHUTDOWN_EVENT): self.osxapp = gtkosx_application.gtkosx_application_get()
reactor.stop() self.osxapp.connect("NSApplicationWillTerminate", on_die)
return 1 log.debug("OSX quartz 'die' handler registered!")
SetConsoleCtrlHandler(win_handler) else:
import gnome.ui
# Suppress warning: 'Attempt to add property GnomeProgram::* after class was initialised'
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.gnome_prog = gnome.init("Deluge", deluge.common.get_version())
self.gnome_client = gnome.ui.master_client()
self.gnome_client.connect("die", on_die)
log.debug("GNOME session 'die' handler registered!")
# Set process name again to fix gtk issue # Set process name again to fix gtk issue
setproctitle(getproctitle()) setproctitle(getproctitle())