From 5cb2e7f02757ee912e380cddb2b6da666536e7cf Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Mon, 16 Dec 2013 04:44:58 -0700 Subject: [PATCH] Adding autostart in Ubuntu --- README.md | 17 ++++++----------- pinger.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ee839fd..6d3f585 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,13 @@ Pinger.py A ping tool that sits in your system tray --------- -- Currently for Ubuntu only - - (Requires Python, GTK, and AppIndicator3) - - **Contributions welcome to expand to other OSes!** - Saves your sanity when the wifi sucks - Doesn't clutter up your screen with ping windows - Lets you know when pings fail instead of silently failing +- **Currently for Ubuntu only** + - (Requires Python, GTK, and AppIndicator3) + - Startup Automatically option creates a `~/.config/autostart/pinger.desktop` file + - **Contributions welcome to expand to other OSes!** **Usage (in Ubuntu):** @@ -16,14 +17,8 @@ Open the Terminal program and enter the following commands: cd ~ git clone https://github.com/zyphlar/pinger.git + python pinger/pinger.py & -Open the Startup Applications program and click Add. - - Name: Pinger - Command: python ~/pinger/pinger.py - -Click Save, and Close. - -Pinger should now start automatically in your system tray when you login next. (It just says "xx.x ms"). You can test it manually by typing `python ~/pinger/pinger.py` in your terminal and pressing Enter. +Pinger should open in your system tray (It just looks like "XX.X ms"). To set Pinger to start automatically (in Ubuntu) click it and choose Start Automatically. Report bugs or feature requests at https://github.com/zyphlar/pinger/issues diff --git a/pinger.py b/pinger.py index e13f506..57b9167 100644 --- a/pinger.py +++ b/pinger.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- # # Pinger.py -- A ping tool that sits in your system tray # Copyright 2013 Will Bradley @@ -43,6 +44,14 @@ import subprocess import re # Ctrl-c import signal +# File paths +import os + +# Vars +startup_active_label = "✓ Start Automatically" +startup_inactive_label = "Start Automatically" +home_path = os.path.expanduser("~") +startup_path = home_path+'/.config/autostart/pinger.desktop' # # Main Class @@ -71,11 +80,34 @@ class Pinger: self.menu.append(menu_item) menu_item.connect("activate", callback, text) menu_item.show() + return menu_item def destroy(self, widget, data=None): - print "destroy signal occurred" + print "Quitting..." Gtk.main_quit() + def create_autostart(self, widget, data=None): + with open(startup_path,'w') as f: + f.write("[Desktop Entry]\r\n" + "Type=Application\r\n" + "Exec=python "+os.path.abspath( __file__ )+"\r\n" + "X-GNOME-Autostart-enabled=true\r\n" + "Name=Pinger\r\n" + "Comment=Pings the internet every few seconds") + self.update_startup_menu() + + def remove_autostart(self, widget, data=None): + os.remove(startup_path) + self.update_startup_menu() + + def update_startup_menu(self): + if os.path.exists(startup_path): + self.startup_menu.set_label(startup_active_label) + self.startup_menu.connect("activate", self.remove_autostart, startup_active_label) + else: + self.startup_menu.set_label(startup_inactive_label) + self.startup_menu.connect("activate", self.create_autostart, startup_inactive_label) + def __init__(self): # Handle ctrl-c signal.signal(signal.SIGINT, self.destroy) @@ -90,6 +122,8 @@ class Pinger: # create a menu self.menu = Gtk.Menu() + self.startup_menu = self.create_menu_item(startup_inactive_label, self.create_autostart) + self.update_startup_menu() self.create_menu_item("Exit", self.destroy) self.ind.set_menu(self.menu)