openlp/openlp/core/lib/plugin.py

399 lines
13 KiB
Python
Raw Normal View History

2010-09-10 19:47:33 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2019 OpenLP Developers #
# ---------------------------------------------------------------------- #
# 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 3 of the License, 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2010-09-10 19:47:33 +00:00
"""
Provide the generic plugin functionality for OpenLP plugins.
"""
import logging
2014-03-16 21:25:23 +00:00
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import UiStrings
2017-10-23 22:09:57 +00:00
from openlp.core.common.mixins import RegistryProperties
2018-10-20 14:41:32 +00:00
from openlp.core.common.registry import Registry, RegistryBase
2017-10-23 22:09:57 +00:00
from openlp.core.common.settings import Settings
2017-09-09 05:19:22 +00:00
from openlp.core.version import get_version
2010-09-10 19:47:33 +00:00
2018-10-02 04:39:42 +00:00
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.
"""
2013-08-31 18:17:38 +00:00
Name = 'name'
Import = 'import'
Load = 'load'
New = 'new'
Edit = 'edit'
Delete = 'delete'
Preview = 'preview'
Live = 'live'
Service = 'service'
VisibleName = 'visible_name'
2010-09-10 19:47:33 +00:00
2018-10-20 14:41:32 +00:00
class Plugin(RegistryBase, RegistryProperties):
2010-09-10 19:47:33 +00:00
"""
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.
2014-04-12 20:19: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-04-05 19:37:15 +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.
"""
2013-08-31 18:17:38 +00:00
log.info('loaded')
2010-09-10 19:47:33 +00:00
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
"""
2014-03-17 19:05:55 +00:00
This is the constructor for the plugin object. This provides an easy way for descendant plugins to populate
common data. This method *must*
2010-09-10 19:47:33 +00:00
be overridden, like so::
class MyPlugin(Plugin):
def __init__(self):
2014-05-02 06:42:17 +00:00
super(MyPlugin, self).__init__('MyPlugin', version='0.1')
2010-09-10 19:47:33 +00:00
2014-03-17 19:05:55 +00:00
:param name: Defaults to *None*. The name of the plugin.
:param default_settings: A dict containing the plugin's settings. The value to each key is the default value
to be used.
:param media_item_class: The class name of the plugin's media item.
:param settings_tab_class: The class name of the plugin's settings tab.
:param 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
"""
log.debug('Plugin {plugin} initialised'.format(plugin=name))
2013-08-01 13:04:42 +00:00
super(Plugin, self).__init__()
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]
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.
2013-08-31 18:17:38 +00:00
default_settings[name + '/status'] = PluginStatus.Inactive
default_settings[name + '/last directory'] = None
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['{name}/{name} files'.format(name=name)] = []
# Add settings to the dict of all settings.
Settings.extend_default_settings(default_settings)
Registry().register_function('{name}_add_service_item'.format(name=self.name), self.process_add_service_event)
Registry().register_function('{name}_config_updated'.format(name=self.name), self.config_update)
2017-11-18 11:23:15 +00:00
self._setup(version)
def _setup(self, version):
"""
Run some initial setup. This method is separate from __init__ in order to mock it out in tests.
:param version: Defaults to *None*, which means that the same version number is used as OpenLP's version number.
:rtype: None
"""
if version:
self.version = version
else:
self.version = get_version()['version']
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-08-31 18:17:38 +00:00
self.status = Settings().value(self.settings_section + '/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-08-31 18:17:38 +00:00
Settings().setValue(self.settings_section + '/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
2014-03-17 19:05:55 +00:00
:param settings: The Settings object containing the old settings.
"""
pass
2013-12-24 08:56:50 +00:00
def add_import_menu_item(self, import_menu):
2010-09-10 19:47:33 +00:00
"""
Create a menu item and add it to the "Import" menu.
2014-03-17 19:05:55 +00:00
:param import_menu: The Import menu.
2010-09-10 19:47:33 +00:00
"""
pass
2014-04-12 20:19:22 +00:00
def add_export_menu_item(self, export_menu):
2010-09-10 19:47:33 +00:00
"""
Create a menu item and add it to the "Export" menu.
2014-03-17 19:05:55 +00:00
:param export_menu: The Export menu
2010-09-10 19:47:33 +00:00
"""
pass
2013-12-24 08:56:50 +00:00
def add_tools_menu_item(self, tools_menu):
2010-09-10 19:47:33 +00:00
"""
Create a menu item and add it to the "Tools" menu.
2014-03-17 19:05:55 +00:00
:param tools_menu: The Tools menu
2010-09-10 19:47:33 +00:00
"""
pass
2013-04-05 19:37:15 +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,
2013-12-24 08:56:50 +00:00
self.get_string(StringContent.VisibleName)['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.
2014-03-17 19:05:55 +00:00
:param menubar: The application's menu bar.
2010-09-10 19:47:33 +00:00
"""
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.
"""
log.debug('process_add_service_event event called for plugin {name}'.format(name=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
@staticmethod
def about():
2010-09-10 19:47:33 +00:00
"""
2014-03-17 19:05:55 +00:00
Show a dialog when the user clicks on the 'About' button in the plugin manager.
2010-09-10 19:47:33 +00:00
"""
2013-08-31 18:17:38 +00:00
raise NotImplementedError('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.add_item_to_dock(self.media_item)
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
"""
2015-10-16 16:26:01 +00:00
pass
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-12-24 08:56:50 +00:00
def rename_theme(self, old_theme, new_theme):
2010-09-10 19:47:33 +00:00
"""
Renames a theme a plugin is using making the plugin use the new name.
2014-03-17 19:05:55 +00:00
:param old_theme: The name of the theme the plugin should stop using.
:param new_theme: The new name the plugin should now use
2010-09-10 19:47:33 +00:00
"""
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
2018-10-23 16:43:52 +00:00
def set_plugin_text_strings(self):
pass
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
2014-04-12 20:19:22 +00:00
:param tooltips:
"""
2014-04-12 20:19:22 +00:00
# Load Action
2013-08-31 18:17:38 +00:00
self.__set_name_text_string(StringContent.Load, UiStrings().Load, tooltips['load'])
2014-04-12 20:19:22 +00:00
# Import Action
2013-08-31 18:17:38 +00:00
self.__set_name_text_string(StringContent.Import, UiStrings().Import, tooltips['import'])
2014-04-12 20:19:22 +00:00
# New Action
2013-08-31 18:17:38 +00:00
self.__set_name_text_string(StringContent.New, UiStrings().Add, tooltips['new'])
2014-04-12 20:19:22 +00:00
# Edit Action
2013-08-31 18:17:38 +00:00
self.__set_name_text_string(StringContent.Edit, UiStrings().Edit, tooltips['edit'])
2014-04-12 20:19:22 +00:00
# Delete Action
2013-08-31 18:17:38 +00:00
self.__set_name_text_string(StringContent.Delete, UiStrings().Delete, tooltips['delete'])
2014-04-12 20:19:22 +00:00
# Preview Action
2013-08-31 18:17:38 +00:00
self.__set_name_text_string(StringContent.Preview, UiStrings().Preview, tooltips['preview'])
2014-04-12 20:19:22 +00:00
# Send Live Action
2013-08-31 18:17:38 +00:00
self.__set_name_text_string(StringContent.Live, UiStrings().Live, tooltips['live'])
2014-04-12 20:19:22 +00:00
# Add to Service Action
2013-08-31 18:17:38 +00:00
self.__set_name_text_string(StringContent.Service, UiStrings().Service, tooltips['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-12-24 08:56:50 +00:00
Utility method for creating a plugin's text_strings. This method makes use of the singular name of the
plugin object so must only be called after this has been set.
2011-02-09 17:30:38 +00:00
"""
2013-08-31 18:17:38 +00:00
self.text_strings[name] = {'title': title, '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
"""
2013-08-31 18:17:38 +00:00
return ''
2011-06-08 13:18:05 +00:00
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.
"""
2013-08-31 18:17:38 +00:00
return ''
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
"""
2013-08-31 18:17:38 +00:00
return ''
2011-06-08 13:18:05 +00:00
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
"""
2013-08-31 18:17:38 +00:00
return ''
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-08-31 18:17:38 +00:00
log.info('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
"""
2014-03-20 19:10:31 +00:00
pass