Fix autostart toggle bug, consolidated some functions

Fixed toggling autostart which was broken last commit.

Consolidated some functions which were only called from one place and
  honestly work more elegantly when merged together.
This commit is contained in:
AltF4 2014-01-04 22:36:18 -07:00
parent fc6f5bdbcf
commit 2be6c388ad

View File

@ -99,6 +99,7 @@ else:
class Pinger: class Pinger:
ping_log = [] ping_log = []
paused = False paused = False
autostart = False
def ping(self, widget=None, data=None): def ping(self, widget=None, data=None):
if not self.paused: if not self.paused:
@ -139,40 +140,30 @@ class Pinger:
print "Quitting..." print "Quitting..."
Gtk.main_quit() Gtk.main_quit()
def create_autostart(self, widget, data=None): def toggle_autostart(self, widget, data=None):
if not os.path.exists(startup_dir): if not self.autostart:
os.makedirs(startup_dir) if not os.path.exists(startup_dir):
with open(startup_path,'w') as f: os.makedirs(startup_dir)
f.write("[Desktop Entry]\r\n" with open(startup_path,'w') as f:
"Type=Application\r\n" f.write("[Desktop Entry]\r\n"
"Exec=python "+os.path.abspath( __file__ )+arguments+"\r\n" "Type=Application\r\n"
"X-GNOME-Autostart-enabled=true\r\n" "Exec=python "+os.path.abspath( __file__ )+arguments+"\r\n"
"Name=Pinger\r\n" "X-GNOME-Autostart-enabled=true\r\n"
"Comment=Pings the internet every few seconds") "Name=Pinger\r\n"
self.update_startup_menu() "Comment=Pings the internet every few seconds")
self.autostart = True
def remove_autostart(self, widget, data=None): self.startup_menu.set_label(startup_active_label)
os.remove(startup_path) else:
self.update_startup_menu() os.remove(startup_path)
print "Removed" self.autostart = False
self.startup_menu.set_label(startup_inactive_label)
def toggle_pause(self, widget, data=None): def toggle_pause(self, widget, data=None):
if self.paused: if self.paused:
self.paused = False self.paused = False
else:
self.paused = True
self.update_pause_menu()
def update_startup_menu(self):
if os.path.exists(startup_path):
self.startup_menu.set_label(startup_active_label)
else:
self.startup_menu.set_label(startup_inactive_label)
def update_pause_menu(self):
if self.paused:
self.pause_menu.set_label(play_label) self.pause_menu.set_label(play_label)
else: else:
self.paused = True
self.pause_menu.set_label(pause_label) self.pause_menu.set_label(pause_label)
def update_log_menu(self): def update_log_menu(self):
@ -210,10 +201,14 @@ class Pinger:
self.menu = Gtk.Menu() self.menu = Gtk.Menu()
# with pause option # with pause option
self.pause_menu = self.create_menu_item(pause_label, self.toggle_pause) 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) # first, check current autostart state by checking existance of .desktop file
self.update_startup_menu() try:
with open(startup_dir):
self.autostart = True
except IOError:
self.autostart = False
self.startup_menu = self.create_menu_item(startup_inactive_label, self.toggle_autostart)
# and log display # and log display
self.log_menu = self.create_menu_item("Ping Log", None) self.log_menu = self.create_menu_item("Ping Log", None)
# and exit option # and exit option