[Deskbar] Deskbar Configurator Class



Hello all,

I've thrown together a useful class to simplify the creation of configuration dialogs for deskbar handlers. The goal is to allow the handler creater to focus on the configuration as a collection of values that can be set by the user and stored in gconf, without forcing the handler writer to worry about the details of doing this. For example, configuration code that used to look like:

def _check_requirements():
  global GCONF_NOTED_PATH, GCONF_NOTED_EDITOR, _on_config_account

  if deskbar.GCONF_CLIENT.get_string(GCONF_NOTED_PATH):
    return (deskbar.Handler.HANDLER_IS_CONFIGURABLE, _('You may change the search path and note editor.'), _on_config_account)
  else:
    return (deskbar.Handler.HANDLER_HAS_REQUIREMENTS, _('Please configure Noted.'), _on_config_account)

def _on_config_account(dialog):
  global GCONF_NOTED_PATH, GCONF_NOTED_EDITOR

  dialog = gtk.Dialog(_('Noted Configuration'), dialog,
                          gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                          (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                          gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))

  table = gtk.Table(rows=3, columns=2)
  # attach(object, leftbound, rightbound, topbound, bottombound)
  table.attach(gtk.Label(_('Choose a path for Noted to search:')), 0, 2, 0, 1)
  path_entry = gtk.Entry()
  path_entry.set_text(deskbar.GCONF_CLIENT.get_string(GCONF_NOTED_PATH) or '')
  table.attach(path_entry, 1, 2, 1, 2)   
  table.attach(gtk.Label(_('Choose an editor to use for notes (leave blank for gvim):')), 0, 2, 2, 3)
  editor_entry = gtk.Entry()
  editor_entry.set_text(deskbar.GCONF_CLIENT.get_string(GCONF_NOTED_EDITOR) or '')
  table.attach(editor_entry, 1, 2, 3, 4)   
  table.show_all()
  dialog.vbox.add(table)
  response = dialog.run()
  dialog.destroy()

  if response == gtk.RESPONSE_ACCEPT and path_entry.get_text():
    deskbar.GCONF_CLIENT.set_string(GCONF_NOTED_PATH, path_entry.get_text())
    # we allow empty string setting here, indicating option declined:
    deskbar.GCONF_CLIENT.set_string(GCONF_NOTED_EDITOR, editor_entry.get_text())

GCONF_NOTED_PATH = deskbar.GCONF_DIR + '/noted/path'
GCONF_NOTED_EDITOR = deskbar.GCONF_DIR + '/noted/editor'


Now looks like:

myconfig=Configurator(HANDLERS)
item={	'type' : 'string',
	'name' : 'path',
	'desc' : 'Which path should noted search?',
	'default' : os.environ.get('HOME'),
	'required' : 'You need to configure a path for noted.'
     }
myconfig.add_config_item(**item)


Similarly, calls to

var = deskbar.GCONF_CLIENT.get_string(GCONF_PATH_STRING)

become:

var = myconfig.get_value('name')

You can find the development thus far here:
http://www.opensourcejason.info/Coding/DeskbarAppletConfigurator

The basic class so far supports only string type items. If there's enough interest, and the developers think it appropriate, I'll develop this class so that it is suitable for integration into the deskbar-applet package. 

Cheers,

Jason



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]