From 28d4cc355cbae9cb5832d79738f232994b20d35b Mon Sep 17 00:00:00 2001 From: Dragon2fly Date: Sun, 21 Aug 2016 12:37:16 +0900 Subject: [PATCH] add blinking icon when connecting to vpn --- connecting1.svg | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ connecting2.svg | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ connecting3.svg | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ vpn_indicator.py | 27 ++++++++++---- vpnproxy_tui.py | 23 ++++++------ 5 files changed, 307 insertions(+), 16 deletions(-) create mode 100644 connecting1.svg create mode 100644 connecting2.svg create mode 100644 connecting3.svg diff --git a/connecting1.svg b/connecting1.svg new file mode 100644 index 0000000..957a8e4 --- /dev/null +++ b/connecting1.svg @@ -0,0 +1,91 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + VPN + + + + diff --git a/connecting2.svg b/connecting2.svg new file mode 100644 index 0000000..85bd6c4 --- /dev/null +++ b/connecting2.svg @@ -0,0 +1,91 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + VPN + + + + diff --git a/connecting3.svg b/connecting3.svg new file mode 100644 index 0000000..afac44a --- /dev/null +++ b/connecting3.svg @@ -0,0 +1,91 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + VPN + + + + diff --git a/vpn_indicator.py b/vpn_indicator.py index cc1d1be..12e3aa8 100755 --- a/vpn_indicator.py +++ b/vpn_indicator.py @@ -185,8 +185,10 @@ def __init__(self, q_info, sender): self.APPINDICATOR_ID = 'myappindicator' self.icon1 = os.path.abspath('connected.svg') self.icon2 = os.path.abspath('connectnot.svg') - self.icon3 = os.path.abspath('connecting.gif') + self.icon345 = [os.path.abspath(icon) for icon in ['connecting1.svg','connecting2.svg','connecting3.svg']] self.hang = False + self.is_connecting = False + self.icon_th = 0 self.last_recv = [''] self.indicator = appindicator.Indicator.new(self.APPINDICATOR_ID, self.icon2, @@ -203,14 +205,29 @@ def __init__(self, q_info, sender): notify.init(self.APPINDICATOR_ID) def run(self, *args): - GLib.timeout_add(2000, self.callback, *args) + GLib.timeout_add(1000, self.callback, *args) + GLib.timeout_add(700, self.blinking) Gtk.main() + def blinking(self): + if self.is_connecting: + if self.icon_th == 3: + self.icon_th = 0 + self.indicator.set_icon(self.icon345[self.icon_th]) + self.icon_th += 1 + return True + def reload(self, data_in): if data_in: print data_in[:12] self.last_recv = data_in.split(';') + if 'connecting' in data_in: + print 'set blinking' + self.is_connecting = True + else: + self.is_connecting = False + if 'connected' in data_in: self.hang = False self.status('', self.last_recv) @@ -227,7 +244,7 @@ def reload(self, data_in): elif 'main exit' in data_in: self.quit() - return True + # return True def build_menu(self): menu = Gtk.Menu() @@ -302,7 +319,6 @@ def status(self, menu_obj, messages=''): {:<22}{:<15} {:<21}{:<15} '''.format(*msg) - elif 'terminate' in messages[0]: summary = 'VPN tunnel has broken' body = 'Please choose a different server and try again' @@ -313,7 +329,7 @@ def status(self, menu_obj, messages=''): summary = 'Status unknown' body = "Waiting for connection from main program" - self.notifier.update(summary, ''+body+'', icon=None) + self.notifier.update(summary, body, icon=None) self.notifier.show() def handler(self, signal_num, frame): @@ -325,7 +341,6 @@ def send_cmd(self, menu_obj, arg): self.send(arg) def callback(self): - try: data = self.q_info.get_nowait() self.reload(data) diff --git a/vpnproxy_tui.py b/vpnproxy_tui.py index b40b02b..ed327d6 100755 --- a/vpnproxy_tui.py +++ b/vpnproxy_tui.py @@ -26,8 +26,7 @@ if 'buntu' in platform.platform() and not Popen(['pgrep', '-f', 'vpn_indicator'], stdout=PIPE).communicate()[0]: print 'Launch vpn_indicator' - with open('out.txt', 'w+') as f: - Popen(['python', 'vpn_indicator.py'], stdout=PIPE, stderr=PIPE, bufsize=1, ) + Popen(['python', 'vpn_indicator.py'], stdout=PIPE, stderr=PIPE, bufsize=1, ) args = ['sudo', '-E', sys.executable] + sys.argv + [os.environ] os.execlpe('sudo', *args) @@ -119,7 +118,7 @@ def __init__(self): self.vpn_server = None self.vpn_process = None self.vpn_queue = None - self.is_connected = False + self.is_connected = 0 # 0: not, 1: connecting, 2: connected self.kill = False self.get_limit = 1 @@ -412,7 +411,9 @@ def vpn_output(out, queue): def vpn_connect(self, chosen): """ Disconnect the current connection and spawn a new one """ if self.is_connected: - self.vpn_cleanup() + self.vpn_cleanup(1) + else: + self.is_connected = 1 server = self.vpndict[self.sorted[chosen]] self.vpn_server = server @@ -432,12 +433,12 @@ def vpn_connect(self, chosen): self.vpn_process = p self.vpn_queue = q - def vpn_cleanup(self): + def vpn_cleanup(self, status_code=0): p, q = self.vpn_process, self.vpn_queue if p.poll() is None: p.send_signal(signal.SIGINT) p.wait() - self.is_connected = False + self.is_connected = status_code self.dns_manager('restore') # make sure openvpn did close its device @@ -480,7 +481,7 @@ def vpn_checker(self): self.dropped_time = 0 self.dns_manager('change') self.messages['status'] += ['VPN tunnel established successfully', 'Ctrl+C to quit VPN'] - self.is_connected = True + self.is_connected = 2 elif self.is_connected and 'Restart pause, ' in line and self.dropped_time <= self.max_retry: self.dropped_time += 1 self.messages['status'][1] = 'Vpn has restarted %s time(s)' % self.dropped_time @@ -910,11 +911,13 @@ def communicator(self): # send info if self.ovpn.is_connected != self.prev_status: self.prev_status = self.ovpn.is_connected - if self.prev_status: + if self.prev_status == 2: msgs = 'successfully;' + repr(self.ovpn.vpn_server) - self.infoclient.send(msgs) + elif self.prev_status == 1: + msgs = 'connecting' else: - self.infoclient.send('terminate') + msgs = 'terminate' + self.infoclient.send(msgs) # receive cmd try: