|
| 1 | +from gi.repository import GObject, Gtk, Gedit, Gio |
| 2 | + |
| 3 | +class ConfigMaster(GObject.Object, Gedit.WindowActivatable): |
| 4 | + |
| 5 | + __gtype_name__ = "configmaster" |
| 6 | + |
| 7 | + window = GObject.property(type=Gedit.Window) |
| 8 | + |
| 9 | + GSCHEMA_ID = 'org.gnome.gedit.plugins.configmaster' |
| 10 | + |
| 11 | + UI_XML = """<ui> |
| 12 | +<menubar name="MenuBar"> |
| 13 | + <menu name="ToolsMenu" action="Tools"> |
| 14 | + <placeholder name="ToolsOps_3"> |
| 15 | + <menuitem name="RubyConfig" action="RubyConfig"/> |
| 16 | + </placeholder> |
| 17 | + <placeholder name="ToolsOps_4"> |
| 18 | + <menuitem name="PythonConfig" action="PythonConfig"/> |
| 19 | + </placeholder> |
| 20 | + </menu> |
| 21 | +</menubar> |
| 22 | +</ui>""" |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + GObject.Object.__init__(self) |
| 26 | + |
| 27 | + def do_activate(self): |
| 28 | + self._load_default_configurations() |
| 29 | + self._add_ui() |
| 30 | + |
| 31 | + def do_deactivate(self): |
| 32 | + self._remove_ui() |
| 33 | + |
| 34 | + def do_update_state(self): |
| 35 | + pass |
| 36 | + |
| 37 | + ### PRIVATE - GSETTINGS ### |
| 38 | + |
| 39 | + def _set_current_profile(self, name): |
| 40 | + """Given a profile named 'name', take its configuration parameters and |
| 41 | + store them in Gedit's current configuration.""" |
| 42 | + in_config = self._create_config_for(name) |
| 43 | + out_config = self._create_gedit_conf() |
| 44 | + out_config.set_uint('tabs-size', in_config.get_uint('tabs-size')) |
| 45 | + out_config.set_boolean('insert-spaces', in_config.get_boolean('insert-spaces')) |
| 46 | + |
| 47 | + def _load_default_configurations(self): |
| 48 | + self._load_config('ruby', {'tabs-size' : 2, 'insert-spaces' : True}) |
| 49 | + self._load_config('python', {'tabs-size' : 4, 'insert-spaces' : False}) |
| 50 | + |
| 51 | + def _load_config(self, conf_name, options): |
| 52 | + config = self._create_config_for(conf_name) |
| 53 | + config.set_uint('tabs-size', options['tabs-size']) |
| 54 | + config.set_boolean('insert-spaces', options['insert-spaces']) |
| 55 | + |
| 56 | + def _create_config_for(self, name): |
| 57 | + return Gio.Settings.new_with_path(self.GSCHEMA_ID, \ |
| 58 | + '/' + self.GSCHEMA_ID.replace('.', '/') + '/' + name + '/') |
| 59 | + |
| 60 | + def _create_gedit_conf(self): |
| 61 | + return Gio.Settings.new('org.gnome.gedit.preferences.editor') |
| 62 | + |
| 63 | + ### PRIVATE - UI ### |
| 64 | + |
| 65 | + def _add_ui(self): |
| 66 | + manager = self.window.get_ui_manager() |
| 67 | + self._actions = Gtk.ActionGroup("Example04Actions") |
| 68 | + self._actions.add_actions([ |
| 69 | + ('RubyConfig', Gtk.STOCK_INFO, "Choose _Ruby config", |
| 70 | + None, "", self._config_selected) |
| 71 | + ], 'ruby') |
| 72 | + self._actions.add_actions([ |
| 73 | + ('PythonConfig', Gtk.STOCK_INFO, "Choose _Python config", |
| 74 | + None, "", self._config_selected) |
| 75 | + ], 'python') |
| 76 | + manager.insert_action_group(self._actions) |
| 77 | + self._ui_merge_id = manager.add_ui_from_string(self.UI_XML) |
| 78 | + manager.ensure_update() |
| 79 | + |
| 80 | + def _config_selected(self, action, data=None): |
| 81 | + self._set_current_profile(data) |
| 82 | + |
| 83 | + def _remove_ui(self): |
| 84 | + manager = self.window.get_ui_manager() |
| 85 | + manager.remove_ui(self._ui_merge_id) |
| 86 | + manager.remove_action_group(self._actions) |
| 87 | + manager.ensure_update() |
0 commit comments