Skip to content

Commit 6f79427

Browse files
committed
Stop/start servers on system shutdown/sleep/wake (macOS only)
1 parent 03c71a9 commit 6f79427

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

emailproxy.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
__author__ = 'Simon Robinson'
55
__copyright__ = 'Copyright (c) 2021 Simon Robinson'
66
__license__ = 'Apache 2.0'
7-
__version__ = '2022-01-24' # ISO 8601
7+
__version__ = '2022-01-26' # ISO 8601
88

99
import argparse
1010
import asyncore
@@ -1071,15 +1071,37 @@ def __init__(self):
10711071
self.icon.run(self.post_create)
10721072

10731073
# noinspection PyUnresolvedReferences
1074-
@staticmethod
1075-
def init_platforms():
1074+
def init_platforms(self):
10761075
if sys.platform == 'darwin':
10771076
# hide dock icon (but not LSBackgroundOnly as we need input via webview)
10781077
info = AppKit.NSBundle.mainBundle().infoDictionary()
10791078
info['LSUIElement'] = '1'
1079+
1080+
# track shutdown/sleep/wake events and stop/start servers appropriately (also saving activity+configuration)
1081+
# note: no need to manually remove this observer after OS X 10.11 (https://developer.apple.com/library
1082+
# /archive/releasenotes/Foundation/RN-FoundationOlderNotes/index.html#10_11NotificationCenter)
1083+
notification_centre = AppKit.NSWorkspace.sharedWorkspace().notificationCenter()
1084+
notification_centre.addObserver_selector_name_object_(self, 'macos_nsworkspace_notification_listener:',
1085+
AppKit.NSWorkspaceWillPowerOffNotification, None)
1086+
notification_centre.addObserver_selector_name_object_(self, 'macos_nsworkspace_notification_listener:',
1087+
AppKit.NSWorkspaceWillSleepNotification, None)
1088+
notification_centre.addObserver_selector_name_object_(self, 'macos_nsworkspace_notification_listener:',
1089+
AppKit.NSWorkspaceDidWakeNotification, None)
1090+
10801091
else:
10811092
pass # currently no special initialisation/configuration required for other platforms
10821093

1094+
# noinspection PyUnresolvedReferences
1095+
def macos_nsworkspace_notification_listener_(self, notification):
1096+
notification_name = notification.name()
1097+
if notification_name in [AppKit.NSWorkspaceWillSleepNotification, AppKit.NSWorkspaceWillPowerOffNotification]:
1098+
Log.info('Detected imminent workspace sleep or shutdown - saving configuration and stopping servers')
1099+
AppConfig.save()
1100+
self.stop_servers()
1101+
elif notification_name == AppKit.NSWorkspaceDidWakeNotification:
1102+
Log.info('Detected resume from workspace sleep - restarting servers')
1103+
self.load_and_start_servers(self.icon)
1104+
10831105
def create_icon(self):
10841106
icon_class = RetinaIcon if sys.platform == 'darwin' else pystray.Icon
10851107
return icon_class(APP_NAME, App.get_image(), APP_NAME, menu=pystray.Menu(

0 commit comments

Comments
 (0)