Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61df7e8

Browse files
author
张国晔
committed
Mac Tray: Load Settings from Config
1 parent 5b74a52 commit 61df7e8

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

launcher/mac_tray.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self):
3333

3434
def applicationDidFinishLaunching_(self, notification):
3535
setupHelper()
36+
loadConfig()
3637
self.setupUI()
3738
self.registerObserver()
3839

@@ -177,6 +178,8 @@ def enableAutoProxy_(self, _):
177178

178179
xlog.info("try enable auto proxy:%s", executeCommand)
179180
subprocess.call(['osascript', '-e', executeCommand])
181+
config.set(["modules", "launcher", "proxy"], "pac")
182+
config.save()
180183
self.updateStatusBarMenu()
181184

182185
def enableGlobalProxy_(self, _):
@@ -190,6 +193,8 @@ def enableGlobalProxy_(self, _):
190193

191194
xlog.info("try enable global proxy:%s", executeCommand)
192195
subprocess.call(['osascript', '-e', executeCommand])
196+
config.set(["modules", "launcher", "proxy"], "gae")
197+
config.save()
193198
self.updateStatusBarMenu()
194199

195200
def disableProxy_(self, _):
@@ -203,6 +208,8 @@ def disableProxy_(self, _):
203208

204209
xlog.info("try disable proxy:%s", executeCommand)
205210
subprocess.call(['osascript', '-e', executeCommand])
211+
config.set(["modules", "launcher", "proxy"], "disable")
212+
config.save()
206213
self.updateStatusBarMenu()
207214

208215

@@ -274,6 +281,27 @@ def helperDisableGlobalProxy(service):
274281
subprocess.check_call([helper_path, 'disablehttp', service])
275282
subprocess.check_call([helper_path, 'disablehttps', service])
276283

284+
def loadConfig():
285+
if not currentService:
286+
return
287+
proxy_setting = config.get(["modules", "launcher", "proxy"], "pac")
288+
if getProxyState(currentService) == proxy_setting:
289+
return
290+
try:
291+
if proxy_setting == "pac":
292+
helperDisableGlobalProxy(currentService)
293+
helperEnableAutoProxy(currentService)
294+
elif proxy_setting == "gae":
295+
helperDisableAutoProxy(currentService)
296+
helperEnableGlobalProxy(currentService)
297+
elif proxy_setting == "disable":
298+
helperDisableAutoProxy(currentService)
299+
helperDisableGlobalProxy(currentService)
300+
else:
301+
xlog.warn("proxy_setting:%r", proxy_setting)
302+
except:
303+
xlog.warn("helper failed, please manually reset proxy settings after switching connection")
304+
277305

278306
sys_tray = MacTrayObject.alloc().init()
279307
currentService = None
@@ -294,6 +322,7 @@ def fetchCurrentService(protocol):
294322
@AppKit.objc.callbackFor(AppKit.CFNotificationCenterAddObserver)
295323
def networkChanged(center, observer, name, object, userInfo):
296324
fetchCurrentService('IPv4')
325+
loadConfig()
297326
sys_tray.updateStatusBarMenu()
298327

299328
# Note: the following code can't run in class

0 commit comments

Comments
 (0)