|
@@ -8,6 +8,15 @@ import commands, re, time |
|
|
class ToggleProxy(NSObject): |
|
|
class ToggleProxy(NSObject): |
|
|
|
|
|
|
|
|
def applicationDidFinishLaunching_(self, notification): |
|
|
def applicationDidFinishLaunching_(self, notification): |
|
|
|
|
|
# define some title-related stuff |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
# make status bar item |
|
|
# make status bar item |
|
|
self.statusitem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength) |
|
|
self.statusitem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength) |
|
|
self.statusitem.retain() |
|
|
self.statusitem.retain() |
|
@@ -16,23 +25,20 @@ class ToggleProxy(NSObject): |
|
|
self.statusitem.setHighlightMode_(False) |
|
|
self.statusitem.setHighlightMode_(False) |
|
|
self.statusitem.setEnabled_(True) |
|
|
self.statusitem.setEnabled_(True) |
|
|
|
|
|
|
|
|
# define some title-related stuff |
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
# start working |
|
|
# start working |
|
|
self.loadNetworkServices() |
|
|
self.loadNetworkServices() |
|
|
self.watchForProxyChanges() |
|
|
self.watchForProxyChanges() |
|
|
self.updateProxyStatus() |
|
|
self.updateProxyStatus() |
|
|
|
|
|
|
|
|
def loadNetworkServices(self): |
|
|
def loadNetworkServices(self): |
|
|
|
|
|
""" load list of network services, the easy way """ |
|
|
self.services = {} |
|
|
self.services = {} |
|
|
output = commands.getoutput("/usr/sbin/networksetup listnetworkserviceorder") |
|
|
output = commands.getoutput("/usr/sbin/networksetup listnetworkserviceorder") |
|
|
for service, device in re.findall(r'Hardware Port:\s*(.*?), Device:\s*(.*?)\)', output): |
|
|
for service, device in re.findall(r'Hardware Port:\s*(.*?), Device:\s*(.*?)\)', output): |
|
|
self.services[device] = service |
|
|
self.services[device] = service |
|
|
|
|
|
|
|
|
def watchForProxyChanges(self): |
|
|
def watchForProxyChanges(self): |
|
|
|
|
|
""" install a watcher for proxy changes """ |
|
|
store = SCDynamicStoreCreate(None, "name.klep.toggleproxy", self.proxyStateChanged, None) |
|
|
store = SCDynamicStoreCreate(None, "name.klep.toggleproxy", self.proxyStateChanged, None) |
|
|
SCDynamicStoreSetNotificationKeys(store, None, [ 'State:/Network/Global/Proxies' ]) |
|
|
SCDynamicStoreSetNotificationKeys(store, None, [ 'State:/Network/Global/Proxies' ]) |
|
|
|
|
|
|
|
@@ -41,32 +47,39 @@ class ToggleProxy(NSObject): |
|
|
CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes) |
|
|
CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes) |
|
|
|
|
|
|
|
|
def proxyStateChanged(self, store, keys, info): |
|
|
def proxyStateChanged(self, store, keys, info): |
|
|
|
|
|
""" callback for watcher """ |
|
|
self.updateProxyStatus() |
|
|
self.updateProxyStatus() |
|
|
|
|
|
|
|
|
def updateProxyStatus(self): |
|
|
def updateProxyStatus(self): |
|
|
|
|
|
""" update proxy status """ |
|
|
proxydict = SCDynamicStoreCopyProxies(None) |
|
|
proxydict = SCDynamicStoreCopyProxies(None) |
|
|
interface = proxydict['__SCOPED__'].keys()[0] |
|
|
interface = proxydict['__SCOPED__'].keys()[0] |
|
|
status = proxydict['__SCOPED__'][interface] |
|
|
status = proxydict['__SCOPED__'][interface] |
|
|
self.active = status.get('HTTPEnable', False) and True or False |
|
|
self.active = status.get('HTTPEnable', False) and True or False |
|
|
self.device = interface |
|
|
self.device = interface |
|
|
|
|
|
|
|
|
title = NSAttributedString.alloc().initWithString_attributes_( |
|
|
|
|
|
"Proxy %sactive" % (not self.active and "in" or ""), { |
|
|
|
|
|
# NSFontAttributeName : self.title_font, |
|
|
|
|
|
NSForegroundColorAttributeName : self.active and self.active_color or self.inactive_color, |
|
|
|
|
|
} |
|
|
|
|
|
) |
|
|
|
|
|
self.statusitem.setAttributedTitle_(title) |
|
|
|
|
|
|
|
|
# set image |
|
|
|
|
|
self.statusitem.setImage_( self.active and self.active_image or self.inactive_image ) |
|
|
|
|
|
|
|
|
|
|
|
# set tooltip |
|
|
if self.active: |
|
|
if self.active: |
|
|
tooltip = "Proxy active on %s:%s" % ( |
|
|
tooltip = "Proxy active on %s:%s" % ( |
|
|
proxydict.get('HTTPProxy', '??'), |
|
|
proxydict.get('HTTPProxy', '??'), |
|
|
proxydict.get('HTTPPort', '??') |
|
|
proxydict.get('HTTPPort', '??') |
|
|
) |
|
|
) |
|
|
else: |
|
|
else: |
|
|
tooltip = "" |
|
|
|
|
|
|
|
|
tooltip = "Proxy is not active" |
|
|
self.statusitem.setToolTip_(tooltip) |
|
|
self.statusitem.setToolTip_(tooltip) |
|
|
|
|
|
|
|
|
def toggleProxy_(self, sender): |
|
|
def toggleProxy_(self, sender): |
|
|
|
|
|
""" callback for clicks on menu item """ |
|
|
|
|
|
event = NSApp.currentEvent() |
|
|
|
|
|
|
|
|
|
|
|
# Ctrl pressed? if so, quit |
|
|
|
|
|
if event.modifierFlags() & NSControlKeyMask: |
|
|
|
|
|
NSApp.terminate_(self) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
servicename = self.services.get(self.device) |
|
|
servicename = self.services.get(self.device) |
|
|
if not servicename: |
|
|
if not servicename: |
|
|
NSLog("device '%s' not found in services?" % self.device) |
|
|
NSLog("device '%s' not found in services?" % self.device) |
|
|