| 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 locale |
| 28 |
|
| 29 |
from gi.repository import GLib |
| 30 |
from gi.repository import Gtk |
| 31 |
|
| 32 |
from xl import event, main, plugins, xdg |
| 33 |
from xlgui.widgets import common, dialogs |
| 34 |
from xl.nls import gettext as _, ngettext |
| 35 |
|
| 36 |
import logging |
| 37 |
|
| 38 |
logger = logging.getLogger(__name__) |
| 39 |
|
| 40 |
name = _('Plugins') |
| 41 |
ui = xdg.get_data_path('ui', 'preferences', 'plugin.ui') |
| 42 |
|
| 43 |
|
| 44 |
class PluginManager: |
| 45 |
""" |
| 46 |
Gui to manage plugins |
| 47 |
""" |
| 48 |
|
| 49 |
def __init__(self, preferences, builder): |
| 50 |
""" |
| 51 |
Initializes the manager |
| 52 |
""" |
| 53 |
self.preferences = preferences |
| 54 |
builder.connect_signals(self) |
| 55 |
self.plugins = main.exaile().plugins |
| 56 |
|
| 57 |
self.window = builder.get_object('PreferencesDialog') |
| 58 |
|
| 59 |
self.message = dialogs.MessageBar( |
| 60 |
parent=builder.get_object('preferences_pane'), buttons=Gtk.ButtonsType.CLOSE |
| 61 |
) |
| 62 |
self.message.connect('response', self.on_messagebar_response) |
| 63 |
|
| 64 |
self.list = builder.get_object('plugin_tree') |
| 65 |
self.enabled_cellrenderer = builder.get_object('enabled_cellrenderer') |
| 66 |
|
| 67 |
if main.exaile().options.Debug: |
| 68 |
reload_cellrenderer = common.ClickableCellRendererPixbuf() |
| 69 |
reload_cellrenderer.props.icon_name = 'view-refresh' |
| 70 |
reload_cellrenderer.props.xalign = 1 |
| 71 |
reload_cellrenderer.connect('clicked', self.on_reload_cellrenderer_clicked) |
| 72 |
|
| 73 |
name_column = builder.get_object('name_column') |
| 74 |
name_column.pack_start(reload_cellrenderer, True) |
| 75 |
name_column.add_attribute(reload_cellrenderer, 'visible', 3) |
| 76 |
|
| 77 |
"""Show trash can""" |
| 78 |
remove_cellrenderer = common.ClickableCellRendererPixbuf() |
| 79 |
remove_cellrenderer.props.icon_name = 'edit-delete' |
| 80 |
remove_cellrenderer.props.xalign = 1 |
| 81 |
remove_cellrenderer.connect('clicked', self.on_remove_cellrenderer_clicked) |
| 82 |
|
| 83 |
name_column = builder.get_object('name_column') |
| 84 |
name_column.pack_start(remove_cellrenderer, True) |
| 85 |
name_column.add_attribute(remove_cellrenderer, 'visible', 8) |
| 86 |
|
| 87 |
self.version_label = builder.get_object('version_label') |
| 88 |
self.author_label = builder.get_object('author_label') |
| 89 |
self.name_label = builder.get_object('name_label') |
| 90 |
self.description = builder.get_object('description_view') |
| 91 |
|
| 92 |
self.model = builder.get_object('model') |
| 93 |
self.filter_model = self.model.filter_new() |
| 94 |
|
| 95 |
self.show_incompatible_cb = builder.get_object('show_incompatible_cb') |
| 96 |
|
| 97 |
self.filter_model.set_visible_func(self._model_visible_func) |
| 98 |
|
| 99 |
selection = self.list.get_selection() |
| 100 |
selection.connect('changed', self.on_selection_changed) |
| 101 |
self._load_plugin_list() |
| 102 |
|
| 103 |
self._evt_rm1 = event.add_ui_callback( |
| 104 |
self.on_plugin_event, 'plugin_enabled', None, True |
| 105 |
) |
| 106 |
self._evt_rm2 = event.add_ui_callback( |
| 107 |
self.on_plugin_event, 'plugin_disabled', None, False |
| 108 |
) |
| 109 |
self.list.connect('destroy', self.on_destroy) |
| 110 |
|
| 111 |
GLib.idle_add(selection.select_path, (0,)) |
| 112 |
GLib.idle_add(self.list.grab_focus) |
| 113 |
|
| 114 |
def _load_plugin_list(self, select_plugin=None): |
| 115 |
""" |
| 116 |
Loads the plugin list |
| 117 |
""" |
| 118 |
plugins = self.plugins.list_installed_plugins() |
| 119 |
uncategorized = _('Uncategorized') |
| 120 |
plugins_dict = {uncategorized: []} |
| 121 |
failed_list = [] |
| 122 |
|
| 123 |
self.plugin_to_path = {} |
| 124 |
|
| 125 |
for plugin_name in plugins: |
| 126 |
try: |
| 127 |
info = self.plugins.get_plugin_info(plugin_name) |
| 128 |
|
| 129 |
compatible = self.plugins.is_compatible(info) |
| 130 |
broken = self.plugins.is_potentially_broken(info) |
| 131 |
user_installed = self.plugins.is_user_installed(plugin_name) |
| 132 |
|
| 133 |
except Exception: |
| 134 |
logger.exception("Error loading plugin info for %r", plugin_name) |
| 135 |
failed_list += [plugin_name] |
| 136 |
continue |
| 137 |
|
| 138 |
# determine icon to show |
| 139 |
if not compatible: |
| 140 |
icon = 'dialog-error' |
| 141 |
elif broken: |
| 142 |
icon = 'dialog-warning' |
| 143 |
else: |
| 144 |
icon = None |
| 145 |
|
| 146 |
enabled = plugin_name in self.plugins.enabled_plugins |
| 147 |
plugin_data = ( |
| 148 |
plugin_name, |
| 149 |
info['Name'], |
| 150 |
str(info['Version']), |
| 151 |
enabled, |
| 152 |
icon, |
| 153 |
broken, |
| 154 |
compatible, |
| 155 |
True, |
| 156 |
user_installed, |
| 157 |
) |
| 158 |
|
| 159 |
if 'Category' in info: |
| 160 |
cat = plugins_dict.setdefault(info['Category'], []) |
| 161 |
cat.append(plugin_data) |
| 162 |
else: |
| 163 |
plugins_dict[uncategorized].append(plugin_data) |
| 164 |
|
| 165 |
self.list.set_model(None) |
| 166 |
self.model.clear() |
| 167 |
|
| 168 |
def categorykey(item): |
| 169 |
if item[0] == uncategorized: |
| 170 |
return '\xff' * 10 |
| 171 |
return locale.strxfrm(item[0]) |
| 172 |
|
| 173 |
plugins_dict = sorted(plugins_dict.items(), key=categorykey) |
| 174 |
|
| 175 |
for category, plugins_list in plugins_dict: |
| 176 |
plugins_list.sort(key=lambda x: locale.strxfrm(x[1])) |
| 177 |
|
| 178 |
it = self.model.append( |
| 179 |
None, (None, category, '', False, '', False, True, False, False) |
| 180 |
) |
| 181 |
|
| 182 |
for plugin_data in plugins_list: |
| 183 |
pit = self.model.append(it, plugin_data) |
| 184 |
path = self.model.get_string_from_iter(pit) |
| 185 |
self.plugin_to_path[plugin_data[0]] = path |
| 186 |
|
| 187 |
self.list.set_model(self.filter_model) |
| 188 |
|
| 189 |
# TODO: Keep track of which categories are already expanded, and only expand those |
| 190 |
self.list.expand_all() |
| 191 |
|
| 192 |
if select_plugin: |
| 193 |
path = self.plugin_to_path[select_plugin] |
| 194 |
selection = self.list.get_selection() |
| 195 |
selection.select_path(path) |
| 196 |
self.list.scroll_to_cell(path) |
| 197 |
|
| 198 |
if failed_list: |
| 199 |
self.message.show_error( |
| 200 |
_('Could not load plugin info!'), |
| 201 |
ngettext('Failed plugin: %s', 'Failed plugins: %s', len(failed_list)) |
| 202 |
% ', '.join(failed_list), |
| 203 |
) |
| 204 |
|
| 205 |
def on_destroy(self, widget): |
| 206 |
self._evt_rm1() |
| 207 |
self._evt_rm2() |
| 208 |
|
| 209 |
def on_messagebar_response(self, widget, response): |
| 210 |
""" |
| 211 |
Hides the messagebar if requested |
| 212 |
""" |
| 213 |
if response == Gtk.ResponseType.CLOSE: |
| 214 |
widget.hide() |
| 215 |
|
| 216 |
def on_plugin_tree_row_activated(self, tree, path, column): |
| 217 |
""" |
| 218 |
Enables or disables the selected plugin |
| 219 |
""" |
| 220 |
self.enabled_cellrenderer.emit('toggled', path[0]) |
| 221 |
|
| 222 |
def on_remove_cellrenderer_clicked(self, cellrenderer, path: str) -> None: |
| 223 |
""" |
| 224 |
Remove a user installed plugin |
| 225 |
""" |
| 226 |
plugin_name = self.filter_model[path][0] |
| 227 |
user_installed = self.filter_model[path][8] |
| 228 |
|
| 229 |
if not user_installed: |
| 230 |
return |
| 231 |
|
| 232 |
GLib.idle_add(self.ask_for_remove, plugin_name) |
| 233 |
|
| 234 |
def ask_for_remove(self, plugin_name: str): |
| 235 |
response = dialogs.yesno( |
| 236 |
self.window, |
| 237 |
"\n".join( |
| 238 |
[ |
| 239 |
_('Do you really want to remove this plugin?'), |
| 240 |
] |
| 241 |
), |
| 242 |
) |
| 243 |
if response != Gtk.ResponseType.YES: |
| 244 |
return |
| 245 |
|
| 246 |
try: |
| 247 |
self.plugins.uninstall_plugin(plugin_name) |
| 248 |
except Exception as e: |
| 249 |
self.message.show_error(_('Could not remove plugin!'), str(e)) |
| 250 |
return |
| 251 |
|
| 252 |
self._load_plugin_list() |
| 253 |
|
| 254 |
def on_reload_cellrenderer_clicked(self, cellrenderer, path): |
| 255 |
""" |
| 256 |
Reloads a plugin from scratch |
| 257 |
""" |
| 258 |
plugin_name = self.filter_model[path][0] |
| 259 |
enabled = self.filter_model[path][3] |
| 260 |
|
| 261 |
if enabled: |
| 262 |
try: |
| 263 |
self.plugins.disable_plugin(plugin_name) |
| 264 |
except Exception as e: |
| 265 |
self.message.show_error(_('Could not disable plugin!'), str(e)) |
| 266 |
return |
| 267 |
|
| 268 |
logger.info('Reloading plugin %s...', plugin_name) |
| 269 |
self.plugins.load_plugin(plugin_name, reload_plugin=True) |
| 270 |
|
| 271 |
if enabled: |
| 272 |
try: |
| 273 |
self.plugins.enable_plugin(plugin_name) |
| 274 |
except Exception as e: |
| 275 |
self.message.show_error(_('Could not enable plugin!'), str(e)) |
| 276 |
return |
| 277 |
|
| 278 |
def on_install_plugin_button_clicked(self, button): |
| 279 |
""" |
| 280 |
Shows a dialog allowing the user to choose a plugin to install |
| 281 |
from the filesystem |
| 282 |
""" |
| 283 |
dialog = Gtk.FileChooserDialog( |
| 284 |
title=_('Choose a Plugin'), |
| 285 |
transient_for=self.preferences.parent, |
| 286 |
) |
| 287 |
dialog.add_buttons( |
| 288 |
Gtk.STOCK_CANCEL, |
| 289 |
Gtk.ResponseType.CANCEL, |
| 290 |
Gtk.STOCK_ADD, |
| 291 |
Gtk.ResponseType.OK, |
| 292 |
) |
| 293 |
|
| 294 |
filter = Gtk.FileFilter() |
| 295 |
filter.set_name(_('Plugin Archives')) |
| 296 |
filter.add_pattern("*.exz") |
| 297 |
filter.add_pattern("*.tar.gz") |
| 298 |
filter.add_pattern("*.tar.bz2") |
| 299 |
dialog.add_filter(filter) |
| 300 |
|
| 301 |
filter = Gtk.FileFilter() |
| 302 |
filter.set_name(_('All Files')) |
| 303 |
filter.add_pattern('*') |
| 304 |
dialog.add_filter(filter) |
| 305 |
|
| 306 |
result = dialog.run() |
| 307 |
dialog.hide() |
| 308 |
|
| 309 |
if result == Gtk.ResponseType.OK: |
| 310 |
|
| 311 |
def install(filename: str, overwrite_existing: bool = False) -> str: |
| 312 |
try: |
| 313 |
plugin_name = self.plugins.install_plugin( |
| 314 |
filename, overwrite_existing |
| 315 |
) |
| 316 |
except plugins.PluginExistsError as e: |
| 317 |
response = dialogs.yesno( |
| 318 |
self.window, |
| 319 |
"\n".join( |
| 320 |
[ |
| 321 |
_('Plugin already exists!'), |
| 322 |
_('Do you want to override it?'), |
| 323 |
] |
| 324 |
), |
| 325 |
) |
| 326 |
if response == Gtk.ResponseType.YES: |
| 327 |
return install(filename, True) |
| 328 |
except plugins.InvalidPluginError as e: |
| 329 |
self.message.show_error( |
| 330 |
_('Plugin file installation failed!'), str(e) |
| 331 |
) |
| 332 |
return '' |
| 333 |
return plugin_name |
| 334 |
|
| 335 |
plugin_name = install(dialog.get_filename()) |
| 336 |
if not plugin_name: |
| 337 |
return |
| 338 |
|
| 339 |
self._load_plugin_list(plugin_name) |
| 340 |
|
| 341 |
def on_selection_changed(self, selection, user_data=None): |
| 342 |
""" |
| 343 |
Called when a row is selected |
| 344 |
""" |
| 345 |
model, paths = selection.get_selected_rows() |
| 346 |
if not paths: |
| 347 |
return |
| 348 |
|
| 349 |
row = model[paths[0]] |
| 350 |
|
| 351 |
if not row[7]: |
| 352 |
self.author_label.set_label('') |
| 353 |
self.description.get_buffer().set_text('') |
| 354 |
self.name_label.set_label('') |
| 355 |
return |
| 356 |
|
| 357 |
info = self.plugins.get_plugin_info(row[0]) |
| 358 |
|
| 359 |
self.author_label.set_label(",\n".join(info['Authors'])) |
| 360 |
|
| 361 |
self.description.get_buffer().set_text(info['Description'].replace(r'\n', "\n")) |
| 362 |
|
| 363 |
self.name_label.set_markup( |
| 364 |
"<b>%s</b> <small>%s</small>" % (info['Name'], info['Version']) |
| 365 |
) |
| 366 |
|
| 367 |
def on_enabled_cellrenderer_toggled(self, cellrenderer, path): |
| 368 |
""" |
| 369 |
Called when the checkbox is toggled |
| 370 |
""" |
| 371 |
path = Gtk.TreePath.new_from_string(path) |
| 372 |
plugin_name = self.filter_model[path][0] |
| 373 |
if plugin_name is None: |
| 374 |
return |
| 375 |
|
| 376 |
enable = not self.filter_model[path][3] |
| 377 |
|
| 378 |
if enable: |
| 379 |
try: |
| 380 |
self.plugins.enable_plugin(plugin_name, True) |
| 381 |
except Exception as e: |
| 382 |
self.message.show_error(_('Could not enable plugin!'), str(e)) |
| 383 |
return |
| 384 |
else: |
| 385 |
try: |
| 386 |
self.plugins.disable_plugin(plugin_name) |
| 387 |
except Exception as e: |
| 388 |
self.message.show_error(_('Could not disable plugin!'), str(e)) |
| 389 |
return |
| 390 |
|
| 391 |
self.on_selection_changed(self.list.get_selection()) |
| 392 |
|
| 393 |
def on_plugin_event(self, evtname, obj, plugin_name, enabled): |
| 394 |
if hasattr(self.plugins.loaded_plugins[plugin_name], 'get_preferences_pane'): |
| 395 |
self.preferences._load_plugin_pages() |
| 396 |
|
| 397 |
path = self.plugin_to_path[plugin_name] |
| 398 |
self.model[path][3] = enabled |
| 399 |
|
| 400 |
def on_show_incompatible_cb_toggled(self, widget): |
| 401 |
self.filter_model.refilter() |
| 402 |
|
| 403 |
def _model_visible_func(self, model, iter, data): |
| 404 |
row = model[iter] |
| 405 |
compatible = row[6] |
| 406 |
return compatible or self.show_incompatible_cb.get_active() |
| 407 |
|
| 408 |
|
| 409 |
def init(preferences, xml): |
| 410 |
PluginManager(preferences, xml) |