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