Merge branch 'plugin-hooks' into 'master'

Add some registry functions and more that makes it easier for plugins to integrate

See merge request openlp/openlp!741
This commit is contained in:
Tomas Groth 2024-04-17 20:06:18 +00:00
commit 6d636f3235
8 changed files with 28 additions and 9 deletions

View File

@ -27,6 +27,7 @@ import logging
from openlp.core.state import State
from openlp.core.common.i18n import translate
from openlp.core.common.registry import Registry
from openlp.core.lib import build_icon
from openlp.core.db.manager import DBManager
from openlp.core.lib.plugin import Plugin, StringContent
@ -53,6 +54,7 @@ class CustomPlugin(Plugin):
self.weight = -5
self.db_manager = DBManager('custom', init_schema)
self.icon_path = UiIcons().custom
Registry().register('custom_manager', self.db_manager)
self.icon = build_icon(self.icon_path)
State().add_service(self.name, self.weight, is_plugin=True)
State().update_pre_conditions(self.name, self.check_pre_conditions())

View File

@ -119,6 +119,7 @@ class EditCustomForm(QtWidgets.QDialog, Ui_CustomEditDialog):
self.custom_slide.theme_name = self.theme_combo_box.currentText()
success = self.manager.save_object(self.custom_slide)
self.media_item.auto_select_id = self.custom_slide.id
Registry().execute('custom_changed', self.custom_slide.id)
return success
def on_up_button_clicked(self):

View File

@ -112,7 +112,7 @@ class CustomMediaItem(MediaManagerItem):
self.load_list(self.plugin.db_manager.get_all_objects(CustomSlide, order_by_ref=CustomSlide.title))
self.config_update()
def load_list(self, custom_slides, target_group=None):
def load_list(self, custom_slides=None, target_group=None):
# Sort out what custom we want to select after loading the list.
"""
@ -121,6 +121,8 @@ class CustomMediaItem(MediaManagerItem):
"""
self.save_auto_select_id()
self.list_view.clear()
if not custom_slides:
custom_slides = self.plugin.db_manager.get_all_objects(CustomSlide, order_by_ref=CustomSlide.title)
custom_slides.sort()
for custom_slide in custom_slides:
custom_name = QtWidgets.QListWidgetItem(custom_slide.title)
@ -201,6 +203,7 @@ class CustomMediaItem(MediaManagerItem):
id_list = [(item.data(QtCore.Qt.UserRole)) for item in self.list_view.selectedIndexes()]
for id in id_list:
self.plugin.db_manager.delete_object(CustomSlide, id)
Registry().execute('custom_deleted', id)
self.on_search_text_button_clicked()
def on_focus(self):
@ -257,6 +260,7 @@ class CustomMediaItem(MediaManagerItem):
credits=old_custom_slide.credits,
theme_name=old_custom_slide.theme_name)
self.plugin.db_manager.save_object(new_custom_slide)
Registry().execute('custom_changed', new_custom_slide.id)
self.on_search_text_button_clicked()
def on_search_text_button_clicked(self):

View File

@ -1128,6 +1128,7 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties):
clean_song(self.manager, self.song)
self.manager.save_object(self.song)
self.media_item.auto_select_id = self.song.id
Registry().execute('song_changed', self.song.id)
def provide_help(self):
"""

View File

@ -515,27 +515,30 @@ def strip_rtf(text, default_encoding=None):
return text, default_encoding
def delete_song(song_id, song_plugin):
def delete_song(song_id, trigger_event=True):
"""
Deletes a song from the database. Media files associated to the song are removed prior to the deletion of the song.
:param song_id: The ID of the song to delete.
:param song_plugin: The song plugin instance.
:param trigger_event: If True the song_deleted event is triggered through the registry
"""
save_path = ''
media_files = song_plugin.manager.get_all_objects(MediaFile, MediaFile.song_id == song_id)
songs_manager = Registry().get('songs_manager')
media_files = songs_manager.get_all_objects(MediaFile, MediaFile.song_id == song_id)
for media_file in media_files:
try:
media_file.file_path.unlink()
except OSError:
log.exception('Could not remove file: {name}'.format(name=media_file.file_path))
try:
save_path = AppLocation.get_section_data_path(song_plugin.name) / 'audio' / str(song_id)
save_path = AppLocation.get_section_data_path('songs') / 'audio' / str(song_id)
if save_path.exists():
save_path.rmdir()
except OSError:
log.exception('Could not remove directory: {path}'.format(path=save_path))
song_plugin.manager.delete_object(Song, song_id)
songs_manager.delete_object(Song, song_id)
if trigger_event:
Registry().execute('song_deleted', song_id)
def transpose_lyrics(lyrics, transpose_value):

View File

@ -546,6 +546,7 @@ class SongMediaItem(MediaManagerItem):
new_song.media_files.append(new_media_file)
self.plugin.manager.save_object(new_song)
new_song.init_on_load()
Registry().execute('song_changed', new_song.id)
self.on_song_list_load()
def generate_slide_data(self, service_item, *, item=None, context=ServiceItemContext.Service, **kwargs):

View File

@ -229,7 +229,7 @@ class OpenLyrics(object):
self.manager = manager
FormattingTags.load_tags()
def song_to_xml(self, song):
def song_to_xml(self, song, version=None):
"""
Convert the song to OpenLyrics Format.
"""
@ -258,6 +258,9 @@ class OpenLyrics(object):
'verseOrder', properties, song.verse_order.lower())
if song.ccli_number:
self._add_text_to_element('ccliNo', properties, song.ccli_number)
# Add a custom version
if version:
self._add_text_to_element('version', properties, version)
if song.authors_songs:
authors = etree.SubElement(properties, 'authors')
for author_song in song.authors_songs:
@ -376,7 +379,7 @@ class OpenLyrics(object):
end_tags.reverse()
return ''.join(start_tags), ''.join(end_tags)
def xml_to_song(self, xml, parse_and_temporary_save=False):
def xml_to_song(self, xml, parse_and_temporary_save=False, update_song_id=None):
"""
Create and save a song from OpenLyrics format xml to the database. Since we also export XML from external
sources (e. g. OpenLyrics import), we cannot ensure, that it completely conforms to the OpenLyrics standard.
@ -398,7 +401,10 @@ class OpenLyrics(object):
# Formatting tags are new in OpenLyrics 0.8
if float(song_xml.get('version')) > 0.7:
self._process_formatting_tags(song_xml, parse_and_temporary_save)
song = Song()
if update_song_id:
song = self.manager.get_object(Song, update_song_id)
else:
song = Song()
# Values will be set when cleaning the song.
song.search_lyrics = ''
song.verse_order = ''

View File

@ -121,6 +121,7 @@ class SongsPlugin(Plugin):
"""
super(SongsPlugin, self).__init__('songs', SongMediaItem, SongsTab)
self.manager = DBManager('songs', init_schema, upgrade_mod=upgrade)
Registry().register('songs_manager', self.manager)
self.weight = -10
self.icon_path = UiIcons().music
self.icon = build_icon(self.icon_path)