openlp/openlp/core/lib/plugin.py

432 lines
14 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.
2013-03-19 19:43:22 +00:00
``settings_section``
2010-09-10 19:47:33 +00:00
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**
2013-03-19 17:53:32 +00:00
``check_pre_conditions()``
2010-09-10 19:47:33 +00:00
Provides the Plugin with a handle to check if it can be loaded.
2013-03-19 17:53:32 +00:00
``create_media_manager_item()``
Creates a new instance of MediaManagerItem to be used in the Media
Manager.
2010-09-10 19:47:33 +00:00
2013-03-19 19:43:22 +00:00
``add_import_menu_item(import_menu)``
2010-09-10 19:47:33 +00:00
Add an item to the Import menu.
2013-03-19 19:43:22 +00:00
``add_export_menu_Item(export_menu)``
2010-09-10 19:47:33 +00:00
Add an item to the Export menu.
2013-03-19 19:43:22 +00:00
``create_settings_Tab()``
Creates a new instance of SettingsTabItem to be used in the Settings
2010-09-10 19:47:33 +00:00
dialog.
2013-03-19 19:43:22 +00:00
``add_to_menu(menubar)``
2010-09-10 19:47:33 +00:00
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
2013-03-19 19:43:22 +00:00
self.text_strings = {}
2013-02-19 21:23:56 +00:00
self.set_plugin_text_strings()
2013-03-19 19:43:22 +00:00
self.name_strings = self.text_strings[StringContent.Name]
2011-03-24 19:12:27 +00:00
if version:
self.version = version
else:
self.version = get_application_version()[u'version']
2013-03-19 19:43:22 +00:00
self.settings_section = self.name
2010-09-10 19:47:33 +00:00
self.icon = None
2013-03-19 19:43:22 +00:00
self.media_item_class = media_item_class
self.settings_tab_class = settings_tab_class
self.settings_tab = None
self.media_item = 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-03-19 19:43:22 +00:00
Registry().register_function(u'%s_add_service_item' % self.name, self.process_add_service_event)
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
2013-03-19 17:53:32 +00:00
def check_pre_conditions(self):
2010-09-10 19:47:33 +00:00
"""
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
2013-03-19 17:53:32 +00:00
def set_status(self):
2010-09-10 19:47:33 +00:00
"""
Sets the status of the plugin
"""
2013-03-19 19:43:22 +00:00
self.status = Settings().value(self.settings_section + u'/status')
2010-09-10 19:47:33 +00:00
2013-03-19 17:53:32 +00:00
def toggle_status(self, new_status):
2010-09-10 19:47:33 +00:00
"""
Changes the status of the plugin and remembers it
"""
self.status = new_status
2013-03-19 19:43:22 +00:00
Settings().setValue(self.settings_section + 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
2013-03-19 17:53:32 +00:00
def is_active(self):
2010-09-10 19:47:33 +00:00
"""
Indicates if the plugin is active
Returns True or False.
"""
return self.status == PluginStatus.Active
2013-03-19 17:53:32 +00:00
def create_media_manager_item(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
"""
2013-03-19 19:43:22 +00:00
if self.media_item_class:
self.media_item = self.media_item_class(self.main_window.media_dock_manager.media_dock, self)
2010-09-10 19:47:33 +00:00
def upgrade_settings(self, settings):
"""
Upgrade the settings of this plugin.
2013-03-14 11:32:58 +00:00
``settings``
The Settings object containing the old settings.
"""
pass
2013-03-19 19:43:22 +00:00
def add_import_menu_item(self, importMenu):
2010-09-10 19:47:33 +00:00
"""
Create a menu item and add it to the "Import" menu.
``importMenu``
The Import menu.
"""
pass
2013-03-19 19:43:22 +00:00
def add_export_menu_Item(self, exportMenu):
2010-09-10 19:47:33 +00:00
"""
Create a menu item and add it to the "Export" menu.
``exportMenu``
The Export menu
"""
pass
2013-03-19 19:43:22 +00:00
def add_tools_menu_item(self, toolsMenu):
2010-09-10 19:47:33 +00:00
"""
Create a menu item and add it to the "Tools" menu.
``toolsMenu``
The Tools menu
"""
pass
2013-03-19 19:43:22 +00:00
def create_settings_Tab(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
"""
2013-03-19 19:43:22 +00:00
if self.settings_tab_class:
self.settings_tab = self.settings_tab_class(parent, self.name,
self.get_string(StringContent.VisibleName)[u'title'], self.icon_path)
2010-09-10 19:47:33 +00:00
2013-03-19 19:43:22 +00:00
def add_to_menu(self, menubar):
2010-09-10 19:47:33 +00:00
"""
Add menu items to the menu, given the menubar.
``menubar``
The application's menu bar.
"""
pass
2013-03-19 19:43:22 +00:00
def process_add_service_event(self, replace=False):
2010-09-10 19:47:33 +00:00
"""
Generic Drag and drop handler triggered from service_manager.
"""
2013-03-19 19:43:22 +00:00
log.debug(u'process_add_service_event event called for plugin %s' % self.name)
2010-09-10 19:47:33 +00:00
if replace:
2013-03-19 22:00:50 +00:00
self.media_item.on_add_edit_click()
2010-09-10 19:47:33 +00:00
else:
2013-03-19 22:00:50 +00:00
self.media_item.on_add_click()
2010-09-10 19:47:33 +00:00
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.
"""
2013-03-19 19:43:22 +00:00
if self.media_item:
self.media_item.initialise()
self.main_window.media_dock_manager.insert_dock(self.media_item, self.icon, self.weight)
2010-09-10 19:47:33 +00:00
def finalise(self):
"""
Called by the plugin Manager to cleanup things.
"""
2013-03-19 19:43:22 +00:00
if self.media_item:
self.main_window.media_dock_manager.remove_dock(self.media_item)
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.
2013-03-19 19:43:22 +00:00
if self.media_item_class is not None and self.name != u'images':
loaded_list = Settings().get_files_from_config(self)
# Now save the list to the config using our Settings class.
2013-03-19 19:43:22 +00:00
Settings().setValue(u'%s/%s files' % (self.settings_section, self.name), loaded_list)
2013-01-18 19:24:06 +00:00
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
2013-03-19 19:43:22 +00:00
def get_string(self, name):
"""
2013-03-19 19:43:22 +00:00
Encapsulate access of plugins translated text strings
"""
2013-03-19 19:43:22 +00:00
return self.text_strings[name]
2010-09-10 19:47:33 +00:00
2013-03-19 19:43:22 +00:00
def set_plugin_ui_text_strings(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 ##
2013-03-19 19:43:22 +00:00
self.__set_name_text_string(StringContent.Load, UiStrings().Load, tooltips[u'load'])
2011-02-14 17:25:51 +00:00
## Import Action ##
2013-03-19 19:43:22 +00:00
self.__set_name_text_string(StringContent.Import, UiStrings().Import, tooltips[u'import'])
2011-02-09 17:30:38 +00:00
## New Action ##
2013-03-19 19:43:22 +00:00
self.__set_name_text_string(StringContent.New, UiStrings().Add, tooltips[u'new'])
2011-02-09 17:30:38 +00:00
## Edit Action ##
2013-03-19 19:43:22 +00:00
self.__set_name_text_string(StringContent.Edit, UiStrings().Edit, tooltips[u'edit'])
2011-02-09 17:30:38 +00:00
## Delete Action ##
2013-03-19 19:43:22 +00:00
self.__set_name_text_string(StringContent.Delete, UiStrings().Delete, tooltips[u'delete'])
2011-02-09 17:30:38 +00:00
## Preview Action ##
2013-03-19 19:43:22 +00:00
self.__set_name_text_string(StringContent.Preview, UiStrings().Preview, tooltips[u'preview'])
2011-02-09 17:30:38 +00:00
## Send Live Action ##
2013-03-19 19:43:22 +00:00
self.__set_name_text_string(StringContent.Live, UiStrings().Live, tooltips[u'live'])
2011-02-09 17:30:38 +00:00
## Add to Service Action ##
2013-03-19 19:43:22 +00:00
self.__set_name_text_string(StringContent.Service, UiStrings().Service, tooltips[u'service'])
2011-02-09 17:30:38 +00:00
2013-03-19 19:43:22 +00:00
def __set_name_text_string(self, name, title, tooltip):
2011-02-09 17:30:38 +00:00
"""
2013-03-19 19:43:22 +00:00
Utility method for creating a plugin's text_strings. This method makes
2011-02-09 17:30:38 +00:00
use of the singular name of the plugin object so must only be called
after this has been set.
"""
2013-03-19 19:43:22 +00:00
self.text_strings[name] = {u'title': title, u'tooltip': tooltip}
2011-06-08 13:18:05 +00:00
2013-03-19 19:43:22 +00:00
def get_display_css(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''
2013-03-19 19:43:22 +00:00
def get_display_javascript(self):
2011-06-08 13:18:05 +00:00
"""
2011-10-16 08:25:27 +00:00
Add javascript functions to htmlbuilder.
"""
return u''
2013-03-19 19:43:22 +00:00
def refresh_css(self, frame):
2011-10-15 06:32:01 +00:00
"""
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''
2013-03-19 19:43:22 +00:00
def get_display_html(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')
2013-03-19 19:43:22 +00:00
if self.media_item:
self.media_item.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)