Added Pause option, fixed bug with accumulating event triggers

Pause option added. So you can easily toggle whether pings are going out
  without having to fully kill pinger.

Fixed a bug where toggling the autostart option would cumulatively add
  new event handlers, eventually making the program unresponsive after
  several toggles.
This commit is contained in:
AltF4 2014-01-04 22:00:47 -07:00
parent f09333305b
commit fc6f5bdbcf

View File

@ -50,6 +50,8 @@ import sys
# Vars # Vars
startup_active_label = "✓ Start Automatically" startup_active_label = "✓ Start Automatically"
startup_inactive_label = "Start Automatically" startup_inactive_label = "Start Automatically"
pause_label = "Pause"
play_label = "Resume"
home_path = os.path.expanduser("~") home_path = os.path.expanduser("~")
startup_path = home_path+'/.config/autostart/pinger.desktop' startup_path = home_path+'/.config/autostart/pinger.desktop'
startup_dir = home_path+'/.config/autostart/' startup_dir = home_path+'/.config/autostart/'
@ -96,22 +98,24 @@ else:
class Pinger: class Pinger:
ping_log = [] ping_log = []
paused = False
def ping(self, widget=None, data=None): def ping(self, widget=None, data=None):
ping = subprocess.Popen( if not self.paused:
["ping", "-c", "1", host], ping = subprocess.Popen(
stdout = subprocess.PIPE, ["ping", "-c", "1", host],
stderr = subprocess.PIPE stdout = subprocess.PIPE,
) stderr = subprocess.PIPE
out, error = ping.communicate() )
m = re.search('time=(.*) ms', out) out, error = ping.communicate()
if error or m == None: m = re.search('time=(.*) ms', out)
label = "PING FAIL" if error or m == None:
self.log_ping(-1) label = "PING FAIL"
else: self.log_ping(-1)
label = m.group(1)+" ms" else:
self.log_ping(m.group(1)) label = m.group(1)+" ms"
self.ind.set_label (label, "100.0 ms") self.log_ping(m.group(1))
self.ind.set_label (label, "100.0 ms")
#self.ping_menu_item.set_label(out) #self.ping_menu_item.set_label(out)
gobject.timeout_add_seconds(self.timeout, self.ping) gobject.timeout_add_seconds(self.timeout, self.ping)
@ -150,14 +154,26 @@ class Pinger:
def remove_autostart(self, widget, data=None): def remove_autostart(self, widget, data=None):
os.remove(startup_path) os.remove(startup_path)
self.update_startup_menu() self.update_startup_menu()
print "Removed"
def toggle_pause(self, widget, data=None):
if self.paused:
self.paused = False
else:
self.paused = True
self.update_pause_menu()
def update_startup_menu(self): def update_startup_menu(self):
if os.path.exists(startup_path): if os.path.exists(startup_path):
self.startup_menu.set_label(startup_active_label) self.startup_menu.set_label(startup_active_label)
self.startup_menu.connect("activate", self.remove_autostart, startup_active_label)
else: else:
self.startup_menu.set_label(startup_inactive_label) self.startup_menu.set_label(startup_inactive_label)
self.startup_menu.connect("activate", self.create_autostart, startup_inactive_label)
def update_pause_menu(self):
if self.paused:
self.pause_menu.set_label(play_label)
else:
self.pause_menu.set_label(pause_label)
def update_log_menu(self): def update_log_menu(self):
graph = "" graph = ""
@ -192,6 +208,9 @@ class Pinger:
# create a menu # create a menu
self.menu = Gtk.Menu() self.menu = Gtk.Menu()
# with pause option
self.pause_menu = self.create_menu_item(pause_label, self.toggle_pause)
self.update_pause_menu()
# with autostart option # with autostart option
self.startup_menu = self.create_menu_item(startup_inactive_label, self.create_autostart) self.startup_menu = self.create_menu_item(startup_inactive_label, self.create_autostart)
self.update_startup_menu() self.update_startup_menu()