diff --git a/src/calibre/gui2/actions/__init__.py b/src/calibre/gui2/actions/__init__.py index e595c53601..4ab15f6099 100644 --- a/src/calibre/gui2/actions/__init__.py +++ b/src/calibre/gui2/actions/__init__.py @@ -6,6 +6,7 @@ __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' from functools import partial +from zipfile import ZipFile from PyQt4.Qt import QToolButton, QAction, QIcon, QObject @@ -108,6 +109,30 @@ class InterfaceAction(QObject): setattr(self, attr, action) return action + def load_resource(self, name): + ''' + If this plugin comes in a ZIP file (user added plugin), this method + will allow you to load resources from the ZIP file. + + For example to load an image:: + + pixmap = QPixmap() + pixmap.loadFromData(self.load_resource('images/icon.png')) + icon = QIcon(pixmap) + + :param name: Path to resource in zip file using / as separator + + ''' + if self.plugin_path is None: + raise ValueError('This plugin was not loaded from a ZIP file') + with ZipFile(self.plugin_path, 'r') as zf: + for candidate in zf.namelist(): + if candidate == name: + return zf.read(name) + raise ValueError('The name %r was not found in the plugin zip' + ' file'%name) + + def genesis(self): ''' Setup this plugin. Only called once during initialization. self.gui is diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 7279b7f8df..c3e0bcb0da 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -103,6 +103,7 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ acmap = OrderedDict() for action in interface_actions(): ac = action.load_actual_plugin(self) + ac.plugin_path = action.plugin_path if ac.name in acmap: if ac.priority >= acmap[ac.name].priority: acmap[ac.name] = ac