[PATCH] Make gccontrol.py a little more robust
Michael Ellerman
michael at ellerman.id.au
Mon Feb 27 00:00:32 EST 2006
Hi ya'll,
With a straight-out-of-the-box ccontrol, the GUI died on me, because my .ccontrol/default
wasn't a link. So I hacked the GUI a little to make it a little more robust, and a bunch
of other stuff - change log at the URL below.
You can clone it with bzr from http://michael.ellerman.id.au/files/gccontrol
Patch (-p0) against ccontrol 0.9.1 below.
cheers
=== modified file 'gccontrol.py'
--- gccontrol.py
+++ gccontrol.py
@@ -2,55 +2,95 @@
import gtk
import egg.trayicon
-import os
-import stat
-icon = egg.trayicon.TrayIcon("ccontrol")
-eventbox = gtk.EventBox()
-icon.add(eventbox)
+import os
-img = gtk.Image()
-img.set_from_file("/usr/lib/ccontrol/ccontrol-key.png")
-
-DEFAULT_NAME = "default"
-default_file = os.path.join(os.getenv("HOME"), ".ccontrol", DEFAULT_NAME)
-
-def file_ok(file):
- if file[-1] == '~':
- return False
- if file[0] == '.':
- return False
- return stat.S_ISREG(os.lstat(os.path.join(os.getenv("HOME"), ".ccontrol", file)).st_mode)
+CC_DIR = os.path.join(os.getenv("HOME"), '.ccontrol')
+DEFAULT = os.path.join(CC_DIR, 'default')
def config_files():
- return filter(file_ok, os.listdir(os.path.join(os.getenv("HOME"), ".ccontrol")))
+ for file in os.listdir(CC_DIR):
+ path = os.path.join(CC_DIR, file)
+ if os.path.isfile(path) and not os.path.islink(path):
+ yield file
def get_current():
- return os.readlink(default_file)
+ return os.readlink(DEFAULT)
-def menuitem_response(file):
- if os.access(default_file, os.F_OK):
- os.unlink(default_file)
- os.symlink(file, default_file)
+def item_clicked(file):
+ try:
+ os.unlink(DEFAULT)
+ os.symlink(file, DEFAULT)
+ except Exception, e:
+ msg = 'An error occured switching configuration files! ' \
+ 'The error was: %s' % e
+ dialog = gtk.MessageDialog(None, flags=gtk.DIALOG_MODAL,
+ type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE,
+ message_format=msg)
+ dialog.run()
+ gtk.main_quit()
-def icon_clicked(widget, event):
- menu = gtk.Menu()
- current = get_current()
-
- for f in config_files():
- item = gtk.CheckMenuItem(f)
- item.set_draw_as_radio(1)
- if f == current:
- item.set_active(1)
- menu.append(item)
- item.connect_object("activate", menuitem_response, f)
- item.show()
-
- menu.show()
- menu.popup(None, None, None, event.button, event.time)
-eventbox.add(img)
-eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
-eventbox.connect("button_press_event", icon_clicked)
-icon.show_all()
+def tray_clicked(widget, event):
+ menu = gtk.Menu()
+ current = get_current()
-gtk.main()
+ files = list(config_files())
+
+ for f in files:
+ item = gtk.CheckMenuItem(f)
+ item.set_draw_as_radio(True)
+ if f == current:
+ item.set_active(True)
+ menu.append(item)
+ item.connect_object('activate', item_clicked, f)
+ item.show()
+
+ if len(files) == 0:
+ item = gtk.MenuItem('No config files found')
+ item.set_property('sensitive', False)
+ menu.append(item)
+ item.show()
+
+ menu.show()
+ menu.popup(None, None, None, event.button, event.time)
+
+def unexpand_user(path):
+ home = os.path.expanduser('~')
+ common = os.path.commonprefix([path, home])
+ if len(common) > 0:
+ return '~%s' % path[len(common):]
+ else:
+ return path
+
+def setup_ok():
+ if os.path.islink(DEFAULT):
+ return True
+
+ msg = 'To use the ccontrol GUI you must symlink %s to a configuration ' \
+ 'file in %s.' % (unexpand_user(DEFAULT), unexpand_user(CC_DIR))
+
+ dialog = gtk.MessageDialog(None, flags=gtk.DIALOG_MODAL,
+ type=gtk.MESSAGE_ERROR,
+ buttons=gtk.BUTTONS_CLOSE,
+ message_format=msg)
+ dialog.run()
+
+
+def main():
+ if not setup_ok():
+ return
+
+ eventbox = gtk.EventBox()
+ img = gtk.Image()
+ img.set_from_file("/usr/lib/ccontrol/ccontrol-key.png")
+ eventbox.add(img)
+ eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
+ eventbox.connect('button_press_event', tray_clicked)
+
+ tray_icon = egg.trayicon.TrayIcon("ccontrol")
+ tray_icon.add(eventbox)
+ tray_icon.show_all()
+
+ gtk.main()
+
+main()
More information about the ccontrol
mailing list