Fixed the custom_commands errors, adding a method to the ConfigBase class to

delete a plugin configuration section (maybe more delete methods should be 
added), and fixed the order problem adding a 'position' field to the config 
properties of each command.
This commit is contained in:
2011-12-28 23:00:52 +01:00
parent ce9e8d8453
commit 15c92c09a0
2 changed files with 56 additions and 39 deletions

View File

@ -393,6 +393,10 @@ class Config(object):
"""Set a whole config tree for a given plugin""" """Set a whole config tree for a given plugin"""
return(self.base.set_plugin(plugin, tree)) return(self.base.set_plugin(plugin, tree))
def plugin_del_config(self, plugin):
"""Delete a whole config tree for a given plugin"""
return(self.base.del_plugin(plugin))
def layout_get_config(self, layout): def layout_get_config(self, layout):
"""Return a layout""" """Return a layout"""
return(self.base.get_layout(layout)) return(self.base.get_layout(layout))
@ -670,6 +674,11 @@ class ConfigBase(Borg):
"""Set a whole tree for a plugin""" """Set a whole tree for a plugin"""
self.plugins[plugin] = tree self.plugins[plugin] = tree
def del_plugin(self, plugin):
"""Delete a whole tree for a plugin"""
if plugin in self.plugins:
del self.plugins[plugin]
def add_profile(self, profile): def add_profile(self, profile):
"""Add a new profile""" """Add a new profile"""
if profile in self.profiles: if profile in self.profiles:

View File

@ -23,7 +23,7 @@ AVAILABLE = ['CustomCommandsMenu']
class CustomCommandsMenu(plugin.MenuItem): class CustomCommandsMenu(plugin.MenuItem):
"""Add custom commands to the terminal menu""" """Add custom commands to the terminal menu"""
capabilities = ['terminal_menu'] capabilities = ['terminal_menu']
cmd_list = [] cmd_list = {}
conf_file = os.path.join(get_config_dir(),"custom_commands") conf_file = os.path.join(get_config_dir(),"custom_commands")
def __init__( self): def __init__( self):
@ -31,6 +31,7 @@ class CustomCommandsMenu(plugin.MenuItem):
sections = config.plugin_get_config(self.__class__.__name__) sections = config.plugin_get_config(self.__class__.__name__)
if not isinstance(sections, dict): if not isinstance(sections, dict):
return return
noord_cmds = []
for part in sections: for part in sections:
s = sections[part] s = sections[part]
if not (s.has_key("name") and s.has_key("command")): if not (s.has_key("name") and s.has_key("command")):
@ -39,12 +40,22 @@ class CustomCommandsMenu(plugin.MenuItem):
name = s["name"] name = s["name"]
command = s["command"] command = s["command"]
enabled = s["enabled"] and s["enabled"] or False enabled = s["enabled"] and s["enabled"] or False
self.cmd_list.append( if s.has_key("position"):
self.cmd_list[int(s["position"])] = {'enabled' : enabled,
'name' : name,
'command' : command
}
else:
noord_cmds.append(
{'enabled' : enabled, {'enabled' : enabled,
'name' : name, 'name' : name,
'command' : command 'command' : command
} }
) )
for cmd in noord_cmds:
self.cmd_list[len(self.cmd_list)] = cmd
def callback(self, menuitems, menu, terminal): def callback(self, menuitems, menu, terminal):
"""Add our menu items to the menu""" """Add our menu items to the menu"""
item = gtk.MenuItem(_('Custom Commands')) item = gtk.MenuItem(_('Custom Commands'))
@ -61,7 +72,7 @@ class CustomCommandsMenu(plugin.MenuItem):
submenu.append(menuitem) submenu.append(menuitem)
theme = gtk.IconTheme() theme = gtk.IconTheme()
for command in self.cmd_list: for command in [ self.cmd_list[key] for key in sorted(self.cmd_list.keys()) ] :
if not command['enabled']: if not command['enabled']:
continue continue
exe = command['command'].split(' ')[0] exe = command['command'].split(' ')[0]
@ -78,21 +89,22 @@ class CustomCommandsMenu(plugin.MenuItem):
def _save_config(self): def _save_config(self):
config = Config() config = Config()
config.plugin_del_config(self.__class__.__name__)
i = 0 i = 0
length = len(self.cmd_list) for command in [ self.cmd_list[key] for key in sorted(self.cmd_list.keys()) ] :
while i < length: enabled = command['enabled']
enabled = self.cmd_list[i]['enabled'] name = command['name']
name = self.cmd_list[i]['name'] command = command['command']
command = self.cmd_list[i]['command']
item = {} item = {}
item['enabled'] = enabled item['enabled'] = enabled
item['name'] = name item['name'] = name
item['command'] = command item['command'] = command
item['position'] = i
config.plugin_set(self.__class__.__name__, name, item) config.plugin_set(self.__class__.__name__, name, item)
config.save()
i = i + 1 i = i + 1
config.save()
def _execute(self, widget, data): def _execute(self, widget, data):
command = data['command'] command = data['command']
@ -113,7 +125,7 @@ class CustomCommandsMenu(plugin.MenuItem):
) )
store = gtk.ListStore(bool, str, str) store = gtk.ListStore(bool, str, str)
for command in self.cmd_list: for command in [ self.cmd_list[key] for key in sorted(self.cmd_list.keys()) ]:
store.append([command['enabled'], command['name'], command['command']]) store.append([command['enabled'], command['name'], command['command']])
treeview = gtk.TreeView(store) treeview = gtk.TreeView(store)
@ -183,30 +195,32 @@ class CustomCommandsMenu(plugin.MenuItem):
button.set_sensitive(False) button.set_sensitive(False)
ui['button_delete'] = button ui['button_delete'] = button
hbox.pack_start(button_box) hbox.pack_start(button_box)
dbox.show_all() dbox.show_all()
res = dbox.run() res = dbox.run()
if res == gtk.RESPONSE_ACCEPT: if res == gtk.RESPONSE_ACCEPT:
#we save the config self.update_cmd_list(store)
self._save_config()
dbox.destroy()
return
def update_cmd_list(self, store):
iter = store.get_iter_first() iter = store.get_iter_first()
self.cmd_list = [] self.cmd_list = {}
i=0
while iter: while iter:
(enabled, name, command) = store.get(iter, (enabled, name, command) = store.get(iter,
CC_COL_ENABLED, CC_COL_ENABLED,
CC_COL_NAME, CC_COL_NAME,
CC_COL_COMMAND) CC_COL_COMMAND)
self.cmd_list.append( self.cmd_list[i] = {'enabled' : enabled,
{'enabled' : enabled,
'name': name, 'name': name,
'command' : command} 'command' : command}
)
iter = store.iter_next(iter) iter = store.iter_next(iter)
self._save_config() i = i + 1
dbox.destroy()
return
def on_toggled(self, widget, path, data): def on_toggled(self, widget, path, data):
treeview = data['treeview'] treeview = data['treeview']
@ -218,10 +232,7 @@ class CustomCommandsMenu(plugin.MenuItem):
CC_COL_COMMAND CC_COL_COMMAND
) )
store.set_value(iter, CC_COL_ENABLED, not enabled) store.set_value(iter, CC_COL_ENABLED, not enabled)
for cmd in self.cmd_list:
if cmd['name'] == name:
cmd['enabled'] = not enabled
break
def on_selection_changed(self,selection, data=None): def on_selection_changed(self,selection, data=None):
treeview = selection.get_tree_view() treeview = selection.get_tree_view()
@ -376,17 +387,14 @@ class CustomCommandsMenu(plugin.MenuItem):
(store, iter) = selection.get_selected() (store, iter) = selection.get_selected()
if iter: if iter:
store.remove(iter) store.remove(iter)
return return
def on_edit(self, button, data): def on_edit(self, button, data):
treeview = data['treeview'] treeview = data['treeview']
selection = treeview.get_selection() selection = treeview.get_selection()
(store, iter) = selection.get_selected() (store, iter) = selection.get_selected()
if not iter: if not iter:
return return
(dialog,enabled,name,command) = self._create_command_dialog( (dialog,enabled,name,command) = self._create_command_dialog(
enabled_var = store.get_value(iter, CC_COL_ENABLED), enabled_var = store.get_value(iter, CC_COL_ENABLED),
name_var = store.get_value(iter, CC_COL_NAME), name_var = store.get_value(iter, CC_COL_NAME),