From 8b0de1872d06516f09d17b9e855997e55d654a7b Mon Sep 17 00:00:00 2001 From: Robert Klep Date: Sat, 12 Nov 2011 10:55:15 +0100 Subject: [PATCH] Using dynamic store to load network services and active interface; changed active icon --- ToggleProxy.py | 50 ++++++++++++++++++++++++----------------- resources/active.png | Bin 296 -> 294 bytes resources/menuicon.psd | Bin 29584 -> 29998 bytes 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/ToggleProxy.py b/ToggleProxy.py index 80c29a7..d7a0e79 100644 --- a/ToggleProxy.py +++ b/ToggleProxy.py @@ -12,7 +12,7 @@ class ToggleProxy(NSObject): self.active_color = NSColor.colorWithSRGBRed_green_blue_alpha_(0, 0.5, 0, 1) self.inactive_color = NSColor.colorWithSRGBRed_green_blue_alpha_(0.6, 0, 0, 1) self.title_font = NSFont.fontWithName_size_('HelveticaNeue-Bold', 12.0) - + # find image files self.active_image = NSImage.imageNamed_("active") self.inactive_image = NSImage.imageNamed_("inactive") @@ -25,50 +25,58 @@ class ToggleProxy(NSObject): self.statusitem.setHighlightMode_(False) self.statusitem.setEnabled_(True) + # open connection to the dynamic (configuration) store + self.store = SCDynamicStoreCreate(None, "name.klep.toggleproxy", self.dynamicStoreCallback, None) + # start working self.loadNetworkServices() self.watchForProxyChanges() self.updateProxyStatus() + @property + def interface(self): + # get primary interface + return SCDynamicStoreCopyValue(self.store, 'State:/Network/Global/IPv4')['PrimaryInterface'] + def loadNetworkServices(self): - """ load list of network services, the easy way """ - self.services = {} - output = commands.getoutput("/usr/sbin/networksetup listnetworkserviceorder") - for service, device in re.findall(r'Hardware Port:\s*(.*?), Device:\s*(.*?)\)', output): - self.services[device] = service + """ load list of network services """ + self.services = {} + for key, dictionary in SCDynamicStoreCopyMultiple(self.store, None, [ 'Setup:/Network/Service/.*/Interface' ]).items(): + self.services[dictionary['DeviceName']] = dictionary['UserDefinedName'] def watchForProxyChanges(self): """ install a watcher for proxy changes """ - store = SCDynamicStoreCreate(None, "name.klep.toggleproxy", self.proxyStateChanged, None) - SCDynamicStoreSetNotificationKeys(store, None, [ 'State:/Network/Global/Proxies' ]) + SCDynamicStoreSetNotificationKeys(self.store, None, [ 'State:/Network/Global/Proxies' ]) - source = SCDynamicStoreCreateRunLoopSource(None, store, 0) + source = SCDynamicStoreCreateRunLoopSource(None, self.store, 0) loop = NSRunLoop.currentRunLoop().getCFRunLoop() CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes) - def proxyStateChanged(self, store, keys, info): + def dynamicStoreCallback(self, store, keys, info): """ callback for watcher """ self.updateProxyStatus() def updateProxyStatus(self): """ update proxy status """ - proxydict = SCDynamicStoreCopyProxies(None) - interface = proxydict['__SCOPED__'].keys()[0] - status = proxydict['__SCOPED__'][interface] - self.active = status.get('HTTPEnable', False) and True or False - self.device = interface + # load proxy dictionary + proxydict = SCDynamicStoreCopyProxies(None) + + # get status for primary interface + status = proxydict['__SCOPED__'][self.interface] + self.active = status.get('HTTPEnable', False) and True or False - # set image + # set image self.statusitem.setImage_( self.active and self.active_image or self.inactive_image ) # set tooltip if self.active: - tooltip = "Proxy active on %s:%s" % ( + tooltip = "[%s] proxy active on %s:%s" % ( + self.interface, proxydict.get('HTTPProxy', '??'), - proxydict.get('HTTPPort', '??') + proxydict.get('HTTPPort', '??'), ) else: - tooltip = "Proxy is not active" + tooltip = "[%s] proxy not active" % self.interface self.statusitem.setToolTip_(tooltip) def toggleProxy_(self, sender): @@ -80,9 +88,9 @@ class ToggleProxy(NSObject): NSApp.terminate_(self) return - servicename = self.services.get(self.device) + servicename = self.services.get(self.interface) if not servicename: - NSLog("device '%s' not found in services?" % self.device) + NSLog("interface '%s' not found in services?" % self.interface) return newstate = self.active and "off" or "on" commands.getoutput("networksetup setwebproxystate %s %s" % ( diff --git a/resources/active.png b/resources/active.png index 0f9c6b1bec4ff9e55d0c4116456971214bc3d2f7..b31be71a26a94311405b86451842fa398deaab89 100644 GIT binary patch delta 73 zcmZ3%w2WzjC*z5UUh{3uCmw2H?ETN#QMvHq>2_~jVP;c?#y!pFtzDkWE;{hQu%%Jz dDW^jNBf}BC%>Is)rPCOIz|+;wWt~$(698(19S#5h delta 75 zcmZ3+w1R1ZC*!G!Ui0nc7c?;iNX`7u-4WUN!~Dw6h^t110n7p?tbgorF1e@3#Utk| fu_ACD1B(R18|lntLT}9Gv$0JbhVM7(hT=hB1nPSz6V7qMnS*@mTSvC0p~p-ri?9<-p6z z7_d4OkTTmZ8`J(UvYHxN8JJB@XL5wFHm_yc$~f7FQ<%ruz|F|q(#6Qw)x~&oBg-20 z$$hM@llO5sVaQIF=XQXavw`#EW?i1S#+x75+lw&1ovaWd&UkUNMMx4;;_X*^_1A=6 z)GM9q6IohOnR|9ia@gg`yLQ(d-<|oF{YLqJhUR~j>3>~b{&jo5s@Ck>(wGjq5q)IXbb zBT9K$OfQx{n!GGE3(VLolD3PL@#g09*>f3LJEJTfKi^!K+r+G__W%EXHLm}>47~r< vfNUVf5Se_ha4D75DNjCG)TDg#Kg0iykoiEi= delta 494 zcmZ4YigCho#t8~;X$}@np1v$B3?RTS!x+WDEUoG;!^FSQy=WHL{w F5&$te(rW+!