openlp/openlp/core/lib/plugin.py

437 lines
15 KiB
Python
Raw Normal View History

2010-09-10 19:47:33 +00:00
# -*- coding: utf-8 -*-
2012-12-28 22:06:43 +00:00
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2010-09-10 19:47:33 +00:00
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2012-12-29 20:56:56 +00:00
# Copyright (c) 2008-2013 Raoul Snyman #
# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
2012-11-11 21:16:14 +00:00
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
2012-10-21 13:16:22 +00:00
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
2010-09-10 19:47:33 +00:00
# --------------------------------------------------------------------------- #
# 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; version 2 of the License. #
# #
# 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., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
Provide the generic plugin functionality for OpenLP plugins.
"""
import logging
from PyQt4 import QtCore
2013-02-07 08:42:17 +00:00
from openlp.core.lib import Settings, Registry, UiStrings
2011-03-24 19:12:27 +00:00
from openlp.core.utils import get_application_version
2010-09-10 19:47:33 +00:00
log = logging.getLogger(__name__)
2013-02-01 19:58:18 +00:00
2010-09-10 19:47:33 +00:00
class PluginStatus(object):
"""
Defines the status of the plugin
"""
Active = 1
Inactive = 0
Disabled = -1
class StringContent(object):
2011-02-07 15:55:02 +00:00
"""
Provide standard strings for objects to use.
"""
2010-09-10 19:47:33 +00:00
Name = u'name'
Import = u'import'
Load = u'load'
New = u'new'
Edit = u'edit'
Delete = u'delete'
Preview = u'preview'
Live = u'live'
Service = u'service'
VisibleName = u'visible_name'
2010-09-10 19:47:33 +00:00
2010-09-10 19:47:33 +00:00
class Plugin(QtCore.QObject):
"""
Base class for openlp plugins to inherit from.
**Basic Attributes**
``name``
The name that should appear in the plugins list.
``version``
The version number of this iteration of the plugin.
``settingsSection``
The namespace to store settings for the plugin.
``icon``
An instance of QIcon, which holds an icon for this plugin.
``log``
A log object used to log debugging messages. This is pre-instantiated.
``weight``
A numerical value used to order the plugins.
**Hook Functions**
``checkPreConditions()``
Provides the Plugin with a handle to check if it can be loaded.
``createMediaManagerItem()``
Creates a new instance of MediaManagerItem to be used in the Media
Manager.
2010-09-10 19:47:33 +00:00
``addImportMenuItem(import_menu)``
Add an item to the Import menu.
``addExportMenuItem(export_menu)``
Add an item to the Export menu.
``createSettingsTab()``
Creates a new instance of SettingsTabItem to be used in the Settings
2010-09-10 19:47:33 +00:00
dialog.
``addToMenu(menubar)``
A method to add a menu item to anywhere in the menu, given the menu bar.
``handle_event(event)``
A method use to handle events, given an Event object.
``about()``
Used in the plugin manager, when a person clicks on the 'About' button.
"""
log.info(u'loaded')
2013-01-23 21:05:25 +00:00
def __init__(self, name, default_settings, media_item_class=None, settings_tab_class=None, version=None):
2010-09-10 19:47:33 +00:00
"""
This is the constructor for the plugin object. This provides an easy
way for descendent plugins to populate common data. This method *must*
be overridden, like so::
class MyPlugin(Plugin):
def __init__(self):
2011-03-10 19:17:05 +00:00
Plugin.__init__(self, u'MyPlugin', version=u'0.1')
2010-09-10 19:47:33 +00:00
``name``
Defaults to *None*. The name of the plugin.
2013-01-10 20:41:16 +00:00
``default_settings``
A dict containing the plugin's settings. The value to each key is the default value to be used.
2010-09-10 19:47:33 +00:00
2011-04-15 17:17:51 +00:00
``media_item_class``
The class name of the plugin's media item.
2011-04-12 17:45:32 +00:00
``settings_tab_class``
The class name of the plugin's settings tab.
2013-01-10 20:41:16 +00:00
``version``
Defaults to *None*, which means that the same version number is used as OpenLP's version number.
2010-09-10 19:47:33 +00:00
"""
2011-04-12 19:32:17 +00:00
log.debug(u'Plugin %s initialised' % name)
2010-09-10 19:47:33 +00:00
QtCore.QObject.__init__(self)
2010-09-12 21:03:16 +00:00
self.name = name
self.textStrings = {}
2013-02-19 21:23:56 +00:00
self.set_plugin_text_strings()
2011-02-16 03:06:34 +00:00
self.nameStrings = self.textStrings[StringContent.Name]
2011-03-24 19:12:27 +00:00
if version:
self.version = version
else:
self.version = get_application_version()[u'version']
self.settingsSection = self.name
2010-09-10 19:47:33 +00:00
self.icon = None
self.mediaItemClass = media_item_class
self.settingsTabClass = settings_tab_class
self.settingsTab = None
self.mediaItem = None
2010-09-10 19:47:33 +00:00
self.weight = 0
self.status = PluginStatus.Inactive
# Add the default status to the default settings.
default_settings[name + u'/status'] = PluginStatus.Inactive
default_settings[name + u'/last directory'] = u''
2013-01-18 20:35:30 +00:00
# Append a setting for files in the mediamanager (note not all plugins
# which have a mediamanager need this).
if media_item_class is not None:
default_settings[u'%s/%s files' % (name, name)] = []
# Add settings to the dict of all settings.
Settings.extend_default_settings(default_settings)
2013-02-07 08:42:17 +00:00
Registry().register_function(u'%s_add_service_item' % self.name, self.processAddServiceEvent)
2013-02-07 07:08:35 +00:00
Registry().register_function(u'%s_config_updated' % self.name, self.config_update)
2010-09-10 19:47:33 +00:00
def checkPreConditions(self):
"""
Provides the Plugin with a handle to check if it can be loaded.
Failing Preconditions does not stop a settings Tab being created
Returns ``True`` or ``False``.
2010-09-10 19:47:33 +00:00
"""
return True
def setStatus(self):
"""
Sets the status of the plugin
"""
self.status = Settings().value(self.settingsSection + u'/status')
2010-09-10 19:47:33 +00:00
def toggleStatus(self, new_status):
"""
Changes the status of the plugin and remembers it
"""
self.status = new_status
2012-10-03 16:38:06 +00:00
Settings().setValue(self.settingsSection + u'/status', self.status)
if new_status == PluginStatus.Active:
self.initialise()
2011-01-09 08:47:48 +00:00
elif new_status == PluginStatus.Inactive:
self.finalise()
2010-09-10 19:47:33 +00:00
def isActive(self):
"""
Indicates if the plugin is active
Returns True or False.
"""
return self.status == PluginStatus.Active
def createMediaManagerItem(self):
2010-09-10 19:47:33 +00:00
"""
Construct a MediaManagerItem object with all the buttons and things
you need, and return it for integration into OpenLP.
2010-09-10 19:47:33 +00:00
"""
if self.mediaItemClass:
2013-03-16 11:05:52 +00:00
self.mediaItem = self.mediaItemClass(self.main_window.media_dock_manager.media_dock, self)
2010-09-10 19:47:33 +00:00
def addImportMenuItem(self, importMenu):
"""
Create a menu item and add it to the "Import" menu.
``importMenu``
The Import menu.
"""
pass
def addExportMenuItem(self, exportMenu):
"""
Create a menu item and add it to the "Export" menu.
``exportMenu``
The Export menu
"""
pass
def addToolsMenuItem(self, toolsMenu):
"""
Create a menu item and add it to the "Tools" menu.
``toolsMenu``
The Tools menu
"""
pass
def createSettingsTab(self, parent):
2010-09-10 19:47:33 +00:00
"""
Create a tab for the settings window to display the configurable options
for this plugin to the user.
2010-09-10 19:47:33 +00:00
"""
if self.settingsTabClass:
self.settingsTab = self.settingsTabClass(parent, self.name,
2012-12-28 22:06:43 +00:00
self.getString(StringContent.VisibleName)[u'title'], self.iconPath)
2010-09-10 19:47:33 +00:00
def addToMenu(self, menubar):
"""
Add menu items to the menu, given the menubar.
``menubar``
The application's menu bar.
"""
pass
def processAddServiceEvent(self, replace=False):
"""
Generic Drag and drop handler triggered from service_manager.
"""
2012-12-28 22:06:43 +00:00
log.debug(u'processAddServiceEvent event called for plugin %s' % self.name)
2010-09-10 19:47:33 +00:00
if replace:
self.mediaItem.onAddEditClick()
else:
self.mediaItem.onAddClick()
def about(self):
"""
Show a dialog when the user clicks on the 'About' button in the plugin
manager.
"""
2012-12-28 22:06:43 +00:00
raise NotImplementedError(u'Plugin.about needs to be defined by the plugin')
2010-09-10 19:47:33 +00:00
def initialise(self):
"""
Called by the plugin Manager to initialise anything it needs.
"""
if self.mediaItem:
self.mediaItem.initialise()
2013-03-16 11:05:52 +00:00
self.main_window.media_dock_manager.insert_dock(self.mediaItem, self.icon, self.weight)
2010-09-10 19:47:33 +00:00
def finalise(self):
"""
Called by the plugin Manager to cleanup things.
"""
if self.mediaItem:
2013-03-16 11:05:52 +00:00
self.main_window.media_dock_manager.remove_dock(self.mediaItem)
2010-09-10 19:47:33 +00:00
def app_startup(self):
"""
2012-12-28 22:06:43 +00:00
Perform tasks on application startup
"""
2013-01-18 19:24:06 +00:00
# FIXME: Remove after 2.2 release.
# This is needed to load the list of images/media/presentation from the config saved
# before the settings rewrite.
if self.mediaItemClass is not None:
# We need QSettings instead of Settings here to bypass our central settings dict.
# Do NOT do this anywhere else!
settings = QtCore.QSettings()
settings.beginGroup(self.settingsSection)
if settings.contains(u'%s count' % self.name):
list_count = int(settings.value(u'%s count' % self.name, 0))
loaded_list = []
if list_count:
for counter in range(list_count):
item = settings.value(u'%s %d' % (self.name, counter), u'')
if item:
loaded_list.append(item)
settings.remove(u'%s %d' % (self.name, counter))
settings.remove(u'%s count' % self.name)
# Now save the list to the config using our Settings class.
Settings().setValue(u'%s/%s files' % (self.settingsSection, self.name), loaded_list)
settings.endGroup()
2013-02-19 21:23:56 +00:00
def uses_theme(self, theme):
2010-09-10 19:47:33 +00:00
"""
Called to find out if a plugin is currently using a theme.
Returns True if the theme is being used, otherwise returns False.
"""
return False
2013-02-19 21:23:56 +00:00
def rename_theme(self, oldTheme, newTheme):
2010-09-10 19:47:33 +00:00
"""
Renames a theme a plugin is using making the plugin use the new name.
``oldTheme``
The name of the theme the plugin should stop using.
``newTheme``
The new name the plugin should now use.
"""
pass
2010-09-10 19:47:33 +00:00
def getString(self, name):
"""
encapsulate access of plugins translated text strings
"""
return self.textStrings[name]
2010-09-10 19:47:33 +00:00
2011-02-14 17:25:51 +00:00
def setPluginUiTextStrings(self, tooltips):
2010-09-10 19:47:33 +00:00
"""
Called to define all translatable texts of the plugin
"""
2011-02-09 17:30:38 +00:00
## Load Action ##
2012-12-28 22:06:43 +00:00
self.__setNameTextString(StringContent.Load, UiStrings().Load, tooltips[u'load'])
2011-02-14 17:25:51 +00:00
## Import Action ##
2012-12-28 22:06:43 +00:00
self.__setNameTextString(StringContent.Import, UiStrings().Import, tooltips[u'import'])
2011-02-09 17:30:38 +00:00
## New Action ##
2012-12-28 22:06:43 +00:00
self.__setNameTextString(StringContent.New, UiStrings().Add, tooltips[u'new'])
2011-02-09 17:30:38 +00:00
## Edit Action ##
2012-12-28 22:06:43 +00:00
self.__setNameTextString(StringContent.Edit, UiStrings().Edit, tooltips[u'edit'])
2011-02-09 17:30:38 +00:00
## Delete Action ##
2012-12-28 22:06:43 +00:00
self.__setNameTextString(StringContent.Delete, UiStrings().Delete, tooltips[u'delete'])
2011-02-09 17:30:38 +00:00
## Preview Action ##
2012-12-28 22:06:43 +00:00
self.__setNameTextString(StringContent.Preview, UiStrings().Preview, tooltips[u'preview'])
2011-02-09 17:30:38 +00:00
## Send Live Action ##
2012-12-28 22:06:43 +00:00
self.__setNameTextString(StringContent.Live, UiStrings().Live, tooltips[u'live'])
2011-02-09 17:30:38 +00:00
## Add to Service Action ##
2012-12-28 22:06:43 +00:00
self.__setNameTextString(StringContent.Service, UiStrings().Service, tooltips[u'service'])
2011-02-09 17:30:38 +00:00
2011-02-14 17:25:51 +00:00
def __setNameTextString(self, name, title, tooltip):
2011-02-09 17:30:38 +00:00
"""
Utility method for creating a plugin's textStrings. This method makes
use of the singular name of the plugin object so must only be called
after this has been set.
"""
2011-02-14 17:25:51 +00:00
self.textStrings[name] = {u'title': title, u'tooltip': tooltip}
2011-06-08 13:18:05 +00:00
2011-06-14 16:10:20 +00:00
def getDisplayCss(self):
2011-06-08 13:18:05 +00:00
"""
2011-10-16 08:25:27 +00:00
Add css style sheets to htmlbuilder.
2011-06-08 13:18:05 +00:00
"""
return u''
2011-08-29 21:51:03 +00:00
def getDisplayJavaScript(self):
2011-06-08 13:18:05 +00:00
"""
2011-10-16 08:25:27 +00:00
Add javascript functions to htmlbuilder.
"""
return u''
2011-10-15 06:32:01 +00:00
def refreshCss(self, frame):
"""
2011-10-16 08:25:27 +00:00
Allow plugins to refresh javascript on displayed screen.
2011-10-17 18:01:07 +00:00
``frame``
The Web frame holding the page.
2011-06-08 13:18:05 +00:00
"""
return u''
2011-06-14 16:10:20 +00:00
def getDisplayHtml(self):
2011-06-08 13:18:05 +00:00
"""
2011-10-16 08:25:27 +00:00
Add html code to htmlbuilder.
2011-06-08 13:18:05 +00:00
"""
2011-07-10 21:43:07 +00:00
return u''
2013-02-07 07:08:35 +00:00
def config_update(self):
"""
2013-03-17 09:21:18 +00:00
Called when Config is changed to restart values dependent on configuration.
"""
2013-03-17 09:21:18 +00:00
log.info(u'config update processed')
if self.mediaItem:
self.mediaItem.config_update()
2013-02-03 09:07:31 +00:00
def new_service_created(self):
"""
The plugin's needs to handle a new song creation
"""
pass
def _get_main_window(self):
"""
Adds the main window to the class dynamically
"""
if not hasattr(self, u'_main_window'):
self._main_window = Registry().get(u'main_window')
return self._main_window
2013-01-23 19:53:40 +00:00
main_window = property(_get_main_window)
2013-02-03 19:23:12 +00:00
def _get_application(self):
2013-02-03 09:07:31 +00:00
"""
Adds the openlp to the class dynamically
"""
2013-02-03 19:23:12 +00:00
if not hasattr(self, u'_application'):
self._application = Registry().get(u'application')
return self._application
2013-02-03 09:07:31 +00:00
2013-02-03 19:23:12 +00:00
application = property(_get_application)