Debian Codegraph @

xlgui/preferences/__init__.py

raw
1 # Copyright (C) 2008-2010 Adam Olsen
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 #
18 # The developers of the Exaile media player hereby grant permission
19 # for non-GPL compatible GStreamer and Exaile plugins to be used and
20 # distributed together with GStreamer and Exaile. This permission is
21 # above and beyond the permissions granted by the GPL license by which
22 # Exaile is covered. If you modify this code, you may extend this
23 # exception to your version of the code, but you are not obligated to
24 # do so. If you do not wish to do so, delete this exception statement
25 # from your version.
26
27 import inspect
28 import locale
29 import logging
30
31 from gi.repository import GdkPixbuf
32 from gi.repository import GLib
33 from gi.repository import Gtk
34
35 from xl import xdg
36 from xl.nls import gettext as _
37 from xlgui import icons, guiutil
38 from . import (
39 appearance,
40 collection,
41 cover,
42 lyrics,
43 playback,
44 playlists,
45 plugin,
46 widgets,
47 )
48
49 logger = logging.getLogger(__name__)
50
51
52 class PreferencesDialog:
53 """
54 Preferences Dialog
55 """
56
57 PAGES = (playlists, appearance, playback, collection, cover, lyrics)
58 PREFERENCES_DIALOG = None
59
60 def __init__(self, parent, main):
61 """
62 Initializes the preferences dialog
63 """
64 self.main = main
65 self.last_child = None
66 self.last_page = None
67 self.parent = parent
68 self.fields = {}
69 self.panes = {}
70 self.builders = {}
71 self.popup = None
72
73 self.builder = guiutil.get_builder(
74 xdg.get_data_path('ui', 'preferences', 'preferences_dialog.ui')
75 )
76 self.builder.connect_signals(self)
77
78 self.window = self.builder.get_object('PreferencesDialog')
79 self.window.set_transient_for(parent)
80 self.window.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
81 self.window.connect('delete-event', lambda *e: self.close())
82
83 self.box = self.builder.get_object('preferences_box')
84
85 self.tree = self.builder.get_object('preferences_tree')
86 self.model = self.builder.get_object('model')
87
88 title_cellrenderer = self.builder.get_object('title_cellrenderer')
89 title_cellrenderer.props.ypad = 3
90
91 self.default_icon = icons.MANAGER.pixbuf_from_icon_name(
92 'document-properties', Gtk.IconSize.MENU
93 )
94
95 # sets up the default panes
96 for page in self.PAGES:
97 icon = self.default_icon
98
99 if hasattr(page, 'icon'):
100 if isinstance(page.icon, GdkPixbuf.Pixbuf):
101 icon = page.icon
102 else:
103 icon = icons.MANAGER.pixbuf_from_icon_name(
104 page.icon, Gtk.IconSize.MENU
105 )
106
107 self.model.append(None, [page, page.name, icon])
108
109 # Use icon name to allow overrides
110 plugin_icon = icons.MANAGER.pixbuf_from_icon_name(
111 'extension', Gtk.IconSize.MENU
112 )
113 self.plug_root = self.model.append(None, [plugin, _('Plugins'), plugin_icon])
114
115 self._load_plugin_pages()
116
117 selection = self.tree.get_selection()
118 selection.connect('changed', self.switch_pane)
119 # Disallow selection on rows with no widget to show
120 selection.set_select_function(
121 (lambda sel, model, path, issel, dat: model[path][0] is not None), None
122 )
123
124 GLib.idle_add(selection.select_path, (0,))
125
126 def _load_plugin_pages(self):
127 self._clear_children(self.plug_root)
128 plugin_pages = []
129 plugin_manager = self.main.exaile.plugins
130
131 for name in plugin_manager.enabled_plugins:
132 plugin = plugin_manager.enabled_plugins[name]
133 if hasattr(plugin, 'get_preferences_pane'):
134 try:
135 plugin_pages.append(plugin.get_preferences_pane())
136 except Exception:
137 logger.exception('Error loading preferences pane')
138
139 plugin_pages.sort(key=lambda x: locale.strxfrm(x.name))
140
141 for page in plugin_pages:
142 icon = self.default_icon
143
144 if hasattr(page, 'icon'):
145 if isinstance(page.icon, GdkPixbuf.Pixbuf):
146 icon = page.icon
147 else:
148 icon = icons.MANAGER.pixbuf_from_icon_name(
149 page.icon, Gtk.IconSize.MENU
150 )
151
152 self.model.append(self.plug_root, [page, page.name, icon])
153
154 GLib.idle_add(self.tree.expand_row, self.model.get_path(self.plug_root), False)
155
156 def _clear_children(self, node):
157 remove = []
158 iter = self.model.iter_children(node)
159 while iter:
160 remove.append(iter)
161 iter = self.model.iter_next(iter)
162
163 for iter in remove:
164 self.model.remove(iter)
165
166 def on_close_button_clicked(self, widget):
167 """
168 Called when the user clicks 'ok'
169 """
170 self.close()
171
172 def close(self):
173 """
174 Closes the preferences dialog
175 """
176 if hasattr(self.last_page, 'page_leave'):
177 self.last_page.page_leave(self)
178 self.window.hide()
179 self.window.destroy()
180 PreferencesDialog.PREFERENCES_DIALOG = None
181
182 def switch_pane(self, selection):
183 """
184 Switches a pane
185 """
186 (model, iter) = selection.get_selected()
187 if not iter:
188 return
189 page = self.model.get_value(iter, 0)
190 if not page:
191 return
192
193 if self.last_child:
194 self.box.remove(self.last_child)
195
196 if self.last_page:
197 if hasattr(self.last_page, 'page_leave'):
198 self.last_page.page_leave(self)
199
200 self.last_page = page
201
202 child = self.panes.get(page)
203 if not child:
204 if hasattr(page, 'ui'):
205 builder = guiutil.get_builder(page.ui)
206 else:
207 logger.error("No preference pane found")
208 return
209
210 child = builder.get_object('preferences_pane')
211 init = getattr(page, 'init', None)
212 if init:
213 init(self, builder)
214 self.panes[page] = child
215 self.builders[page] = builder
216
217 if page not in self.fields:
218 self._populate_fields(page, self.builders[page])
219
220 if hasattr(page, 'page_enter'):
221 page.page_enter(self)
222
223 child.unparent()
224 self.box.pack_start(child, True, True, 0)
225 self.last_child = child
226 self.box.show_all()
227
228 childs = self.fields[page]
229 for c in childs:
230 if hasattr(c, 'check_condition'):
231 c.check_condition()
232
233 def _populate_fields(self, page, builder):
234 """
235 Populates field pages
236 """
237 self.fields[page] = []
238
239 attributes = dir(page)
240
241 for attr in attributes:
242 try:
243 klass = getattr(page, attr)
244
245 if inspect.isclass(klass) and issubclass(klass, widgets.Preference):
246 widget = builder.get_object(klass.name)
247
248 if not widget:
249 logger.warning('Invalid preferences widget: %s', klass.name)
250 continue
251
252 if issubclass(klass, widgets.Conditional):
253 klass.condition_widget = builder.get_object(
254 klass.condition_preference_name
255 )
256 elif issubclass(klass, widgets.MultiConditional):
257 for name in klass.condition_preference_names:
258 klass.condition_widgets[name] = builder.get_object(name)
259
260 field = klass(self, widget)
261
262 label_widget = builder.get_object('label:%s' % klass.name)
263 if label_widget:
264 field.label_widget = label_widget
265
266 self.fields[page].append(field)
267 except Exception:
268 logger.exception('Broken preferences class: %s', attr)
269
270 def run(self):
271 """
272 Runs the dialog
273 """
274 if PreferencesDialog.PREFERENCES_DIALOG:
275 self = PreferencesDialog.PREFERENCES_DIALOG
276 self.window.present()
277 else:
278 PreferencesDialog.PREFERENCES_DIALOG = self
279 self.window.show_all()
280
281
282 # vim: et sts=4 sw=4