root/branches/0.3/src/preference_window.py

Revision 607, 7.4 kB (checked in by nicfit, 2 years ago)

Lotta minore updates and fix for #302 (j keybinding changed to Ctrl+j)

Line 
1 ################################################################################
2 #  Copyright (C) 2006  Travis Shirk <travis@pobox.com>
3 #
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 #
18 #  $Id$
19 ################################################################################
20 import gtk, gtk.glade
21
22 import mesk
23 from mesk.i18n import _
24 from mesk.plugin import PluginMgr
25 import mesk.window
26
27 from dialogs import ErrorDialog
28
29 class PreferenceWindow(mesk.window.Window):
30     def __init__(self):
31         mesk.window.Window.__init__(self, 'preference_window',
32                                     'preference_window.glade')
33
34         self.prefs_notebook = self.xml.get_widget('pref_notebook')
35         self.close_button = self.xml.get_widget('close_button')
36         self.plugins_notebook = self.xml.get_widget('plugins_notebook')
37         self.plugins_notebook.set_show_tabs(False)
38         self.plugin_config_container = self.xml.get_widget('plugin_config_vbox')
39
40         # Plugin info widgets
41         self._name_label = self.xml.get_widget('name_label')
42         self._desc_label = self.xml.get_widget('description_label')
43         self._author_label = self.xml.get_widget('author_label')
44         self._copyright_label = self.xml.get_widget('copyright_label')
45         self._plugin_image = self.xml.get_widget('plugin_image')
46         self._config_button = self.xml.get_widget('plugin_config_button')
47         self._config_button.set_sensitive(False)
48
49         self._url_linkbutton = gtk.LinkButton('')
50         from mesk.gtk_utils import default_linkbutton_callback
51         self._url_linkbutton.connect('clicked', default_linkbutton_callback)
52
53         # Create plugins list and model
54         (self.MODEL_ENABLED,
55          self.MODEL_NAME) = range(2)
56         plugins_model = gtk.ListStore(int, # Enabled
57                                       str, # Plugin name
58                                      )
59
60         plugins_view = self.xml.get_widget('plugins_treeview')
61         plugins_view.set_model(plugins_model)
62
63         col = gtk.TreeViewColumn(_('Plugin'))
64         txt_renderer = gtk.CellRendererText()
65         col.pack_start(txt_renderer, True)
66         col.add_attribute(txt_renderer, 'text', 1)
67         plugins_view.append_column(col)
68
69         col = gtk.TreeViewColumn(_('Enabled'))
70         toggle_renderer = gtk.CellRendererToggle()
71         toggle_renderer.set_property('activatable', True)
72         toggle_renderer.connect('toggled', self._on_plugin_toggled,
73                                 plugins_model)
74         col.pack_start(toggle_renderer, True)
75         col.add_attribute(toggle_renderer, 'active', 0)
76         plugins_view.append_column(col)
77
78         # Populate plugins list
79         self._plugins = list(mesk.plugin.get_manager().get_all_plugins())
80         self._plugins.sort(cmp=lambda x,y: cmp(x['DISPLAY_NAME'],
81                                                y['DISPLAY_NAME']))
82         active_plugins = mesk.plugin.get_manager().get_active_plugins()
83         if self._plugins:
84             active_names = []
85             for plugin in active_plugins:
86                 active_names.append(plugin.name)
87
88             for plugin in self._plugins:
89                 plugins_model.append([plugin['NAME'] in active_names,
90                                       plugin['DISPLAY_NAME']])
91                 # Create a Pixbuf from the xpm data
92                 pixbuf = gtk.gdk.pixbuf_new_from_xpm_data(plugin['XPM'])
93                 plugin['PIXBUF'] = pixbuf
94                 # Save some memory
95                 plugin['XPM'] = ''
96
97             plugins_view.set_cursor(0)
98         else:
99             # TODO: Hide plugins tab
100             pass
101
102         self._plugins_view = plugins_view
103
104     def _on_close_button_clicked(self, button):
105         self.hide()
106
107     def _on_plugins_treeview_cursor_changed(self, treeview):
108         # Update info panel for the selected plugin
109         row = treeview.get_cursor()[0][0]
110         plugin = self._plugins[row]
111
112         # Update plugin info
113         self._name_label.set_markup('<big><b>%s</b></big>' %
114                                     plugin['DISPLAY_NAME'])
115         self._plugin_image.set_from_pixbuf(plugin['PIXBUF'])
116         self._desc_label.set_text(plugin['DESCRIPTION'])
117         self._author_label.set_text(plugin['AUTHOR'])
118         self._copyright_label.set_text(plugin['COPYRIGHT'])
119
120         self._url_linkbutton.set_label(plugin['URL'])
121         self._url_linkbutton.set_uri(plugin['URL'])
122
123         # Show/hide configure button
124         is_active = treeview.get_model()[row][self.MODEL_ENABLED]
125         mgr = mesk.plugin.get_manager()
126         selected_plugin = self._plugins[row]
127         plugin = mesk.plugin.get_manager().get_plugin(selected_plugin['NAME'])
128         if is_active and plugin.is_configurable():
129             self._config_button.set_sensitive(True)
130         else:
131             self._config_button.set_sensitive(False)
132
133     def _on_plugin_toggled(self, cell, path, model):
134         new_state = not model[path][0]
135         path = int(path)
136         plugin_name = self._plugins[path]['NAME']
137
138         plugin_mgr = mesk.plugin.get_manager()
139         state_str = ''
140         try:
141             if new_state:
142                 state_str = _('Plugin activation error')
143                 plugin_mgr.activate_plugin(plugin_name)
144             else:
145                 state_str = _('Plugin deactivation error')
146                 plugin_mgr.deactivate_plugin(plugin_name)
147             # Update model
148             model[path][0] = new_state
149         except Exception, ex:
150             d = ErrorDialog(self.window)
151             d.set_markup('<b>%s</b>' % state_str)
152             d.format_secondary_text(str(ex))
153             d.run()
154             d.destroy()
155
156     def _on_plugin_config_button_clicked(self, button):
157         selected_row = self._plugins_view.get_cursor()[0][0]
158         selected_plugin = self._plugins[selected_row]
159         plugin = mesk.plugin.get_manager().get_plugin(selected_plugin['NAME'])
160
161         self.config_widget = plugin.get_config_widget(self.window)
162         self.plugin_config_container.pack_start(self.config_widget)
163         self.plugin_config_container.show_all()
164         self.close_button.set_sensitive(False)
165         self._plugins_view.set_sensitive(False)
166         self.plugins_notebook.set_current_page(1)
167         self.config_plugin = plugin
168
169     def _config_action(self, action):
170         '''A helper for the config ok/cancel buttons'''
171         action()
172
173         self.plugin_config_container.remove(self.config_widget)
174         self.config_widget.destroy()
175         self.config_widget = None
176         del self.config_widget
177         self.config_plugin = None
178
179         self.plugins_notebook.set_current_page(0)
180         self.close_button.set_sensitive(True)
181         self._plugins_view.set_sensitive(True)
182
183     def _on_plugin_config_cancel_button_clicked(self, button):
184         self._config_action(self.config_plugin.config_cancel)
185     def _on_plugin_config_ok_button_clicked(self, button):
186         self._config_action(self.config_plugin.config_ok)
Note: See TracBrowser for help on using the browser.