Merge bug fix from David Caro. Closes LP#684340

This commit is contained in:
Stephen Boddy 2013-07-16 20:00:07 +02:00
commit 3d6edb437e
2 changed files with 42 additions and 22 deletions

View File

@ -412,6 +412,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))
@ -702,6 +706,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,21 @@ 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 +71,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 +88,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 +124,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)
@ -189,24 +200,27 @@ class CustomCommandsMenu(plugin.MenuItem):
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()