diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 2e1e6104..6246a024 100755 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -412,6 +412,10 @@ class Config(object): """Set a whole config tree for a given plugin""" 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): """Return a layout""" return(self.base.get_layout(layout)) @@ -702,6 +706,11 @@ class ConfigBase(Borg): """Set a whole tree for a plugin""" 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): """Add a new profile""" if profile in self.profiles: diff --git a/terminatorlib/plugins/custom_commands.py b/terminatorlib/plugins/custom_commands.py index 8ae62ef1..3d87e5b0 100755 --- a/terminatorlib/plugins/custom_commands.py +++ b/terminatorlib/plugins/custom_commands.py @@ -23,7 +23,7 @@ AVAILABLE = ['CustomCommandsMenu'] class CustomCommandsMenu(plugin.MenuItem): """Add custom commands to the terminal menu""" capabilities = ['terminal_menu'] - cmd_list = [] + cmd_list = {} conf_file = os.path.join(get_config_dir(),"custom_commands") def __init__( self): @@ -31,6 +31,7 @@ class CustomCommandsMenu(plugin.MenuItem): sections = config.plugin_get_config(self.__class__.__name__) if not isinstance(sections, dict): return + noord_cmds = [] for part in sections: s = sections[part] if not (s.has_key("name") and s.has_key("command")): @@ -39,12 +40,21 @@ class CustomCommandsMenu(plugin.MenuItem): name = s["name"] command = s["command"] 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, 'name' : name, 'command' : command } ) + for cmd in noord_cmds: + self.cmd_list[len(self.cmd_list)] = cmd + def callback(self, menuitems, menu, terminal): """Add our menu items to the menu""" item = gtk.MenuItem(_('Custom Commands')) @@ -61,7 +71,7 @@ class CustomCommandsMenu(plugin.MenuItem): submenu.append(menuitem) 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']: continue exe = command['command'].split(' ')[0] @@ -78,21 +88,22 @@ class CustomCommandsMenu(plugin.MenuItem): def _save_config(self): config = Config() + config.plugin_del_config(self.__class__.__name__) i = 0 - length = len(self.cmd_list) - while i < length: - enabled = self.cmd_list[i]['enabled'] - name = self.cmd_list[i]['name'] - command = self.cmd_list[i]['command'] + for command in [ self.cmd_list[key] for key in sorted(self.cmd_list.keys()) ] : + enabled = command['enabled'] + name = command['name'] + command = command['command'] item = {} item['enabled'] = enabled item['name'] = name item['command'] = command + item['position'] = i config.plugin_set(self.__class__.__name__, name, item) - config.save() i = i + 1 + config.save() def _execute(self, widget, data): command = data['command'] @@ -113,7 +124,7 @@ class CustomCommandsMenu(plugin.MenuItem): ) 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']]) treeview = gtk.TreeView(store) @@ -189,24 +200,27 @@ class CustomCommandsMenu(plugin.MenuItem): dbox.show_all() res = dbox.run() 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() - self.cmd_list = [] + self.cmd_list = {} + i=0 while iter: (enabled, name, command) = store.get(iter, CC_COL_ENABLED, CC_COL_NAME, CC_COL_COMMAND) - self.cmd_list.append( - {'enabled' : enabled, + self.cmd_list[i] = {'enabled' : enabled, 'name': name, 'command' : command} - ) iter = store.iter_next(iter) - self._save_config() + i = i + 1 - dbox.destroy() - return def on_toggled(self, widget, path, data): treeview = data['treeview'] @@ -218,10 +232,7 @@ class CustomCommandsMenu(plugin.MenuItem): CC_COL_COMMAND ) 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): treeview = selection.get_tree_view()