openlp/openlp/plugins/songs/lib/mediaitem.py

799 lines
39 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-12-31 15:54:46 +00:00
# Copyright (c) 2008-2023 OpenLP Developers #
2019-04-13 13:00:22 +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, 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/>. #
##########################################################################
import logging
2017-06-12 17:43:24 +00:00
import mako
import os
from shutil import copyfile
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtWidgets
from sqlalchemy.sql import and_, or_
from openlp.core.state import State
2017-10-07 07:05:07 +00:00
from openlp.core.common.applocation import AppLocation
from openlp.core.common.enum import SongSearch
2018-10-02 04:39:42 +00:00
from openlp.core.common.i18n import UiStrings, get_natural_key, translate
from openlp.core.common.path import create_paths
2017-10-07 07:05:07 +00:00
from openlp.core.common.registry import Registry
from openlp.core.lib import ServiceItemContext, check_item_selected, create_separated_list
from openlp.core.lib.mediamanageritem import MediaManagerItem
from openlp.core.lib.plugin import PluginStatus
from openlp.core.lib.serviceitem import ItemCapabilities
2017-06-12 17:43:24 +00:00
from openlp.core.lib.ui import create_widget_action, critical_error_message_box
2018-10-02 04:39:42 +00:00
from openlp.core.ui.icons import UiIcons
2021-04-15 17:33:10 +00:00
from openlp.core.ui.confirmationform import ConfirmationForm
2013-03-07 12:34:35 +00:00
from openlp.plugins.songs.forms.editsongform import EditSongForm
from openlp.plugins.songs.forms.songexportform import SongExportForm
2017-09-30 20:16:30 +00:00
from openlp.plugins.songs.forms.songimportform import SongImportForm
from openlp.plugins.songs.forms.songmaintenanceform import SongMaintenanceForm
2017-06-12 17:43:24 +00:00
from openlp.plugins.songs.lib import VerseType, clean_string, delete_song
from openlp.plugins.songs.lib.db import Author, AuthorType, SongBook, MediaFile, Song, SongBookEntry, Topic
2014-07-03 16:54:51 +00:00
from openlp.plugins.songs.lib.openlyricsxml import OpenLyrics, SongXML
2017-09-30 20:16:30 +00:00
from openlp.plugins.songs.lib.ui import SongStrings
2018-10-02 04:39:42 +00:00
2010-02-27 15:31:23 +00:00
log = logging.getLogger(__name__)
2012-11-08 21:28:42 +00:00
class SongMediaItem(MediaManagerItem):
"""
This is the custom media manager item for Songs.
"""
2015-11-07 00:49:40 +00:00
songs_go_live = QtCore.pyqtSignal(list)
songs_add_to_service = QtCore.pyqtSignal(list)
2013-08-31 18:17:38 +00:00
log.info('Song Media Item loaded')
2013-03-07 13:14:31 +00:00
def __init__(self, parent, plugin):
2013-08-31 18:17:38 +00:00
self.icon_path = 'songs/song'
2013-07-18 19:38:37 +00:00
super(SongMediaItem, self).__init__(parent, plugin)
def setup_item(self):
"""
Do some additional setup.
"""
2015-11-07 00:49:40 +00:00
self.songs_go_live.connect(self.go_live_remote)
self.songs_add_to_service.connect(self.add_to_service_remote)
2013-03-19 22:00:50 +00:00
self.single_service_item = False
2013-04-18 09:17:00 +00:00
# Holds information about whether the edit is remotely triggered and which Song is required.
2013-04-18 09:22:20 +00:00
self.remote_song = -1
self.edit_item = None
2013-03-19 22:00:50 +00:00
self.quick_preview_allowed = True
2013-03-23 06:46:41 +00:00
self.has_search = True
2013-04-18 09:20:56 +00:00
def _update_background_audio(self, song, item):
song.media_files = []
for i, bga in enumerate(item.background_audio):
2017-09-30 20:16:30 +00:00
dest_path =\
AppLocation.get_section_data_path(self.plugin.name) / 'audio' / str(song.id) / os.path.split(bga[0])[1]
2017-10-10 02:29:56 +00:00
create_paths(dest_path.parent)
copyfile(AppLocation.get_section_data_path('servicemanager') / bga[0], dest_path)
song.media_files.append(MediaFile(weight=i, file_path=dest_path, file_hash=bga[1]))
self.plugin.manager.save_object(song, True)
def add_middle_header_bar(self):
self.toolbar.addSeparator()
2014-04-12 20:19:22 +00:00
# Song Maintenance Button
2014-03-06 22:05:15 +00:00
self.maintenance_action = self.toolbar.add_toolbar_action('maintenance_action',
2018-04-07 11:12:31 +00:00
icon=UiIcons().database,
2014-03-06 22:05:15 +00:00
triggers=self.on_song_maintenance_click)
2013-03-19 22:00:50 +00:00
self.add_search_to_toolbar()
# Signals and slots
2013-08-31 18:17:38 +00:00
Registry().register_function('songs_load_list', self.on_song_list_load)
Registry().register_function('songs_preview', self.on_preview_click)
2015-11-07 00:49:40 +00:00
self.search_text_edit.cleared.connect(self.on_clear_text_button_click)
self.search_text_edit.searchTypeChanged.connect(self.on_search_text_button_clicked)
2013-03-19 22:00:50 +00:00
def add_custom_context_actions(self):
create_widget_action(self.list_view, separator=True)
2014-03-06 22:05:15 +00:00
create_widget_action(
2018-04-13 18:54:42 +00:00
self.list_view, text=translate('OpenLP.MediaManagerItem', '&Clone'), icon=UiIcons().clone,
2013-04-18 09:30:55 +00:00
triggers=self.on_clone_click)
2013-04-18 09:27:11 +00:00
def on_focus(self):
2013-03-19 22:00:50 +00:00
self.search_text_edit.setFocus()
self.search_text_edit.selectAll()
2013-02-07 07:08:35 +00:00
def config_update(self):
2013-03-17 09:21:18 +00:00
"""
2013-04-18 09:17:00 +00:00
Is triggered when the songs config is updated
2013-03-17 09:21:18 +00:00
"""
2013-08-31 18:17:38 +00:00
log.debug('config_updated')
self.is_search_as_you_type_enabled = self.settings.value('advanced/search as type')
2020-06-06 16:05:36 +00:00
self.update_service_on_edit = self.settings.value('songs/update service on edit')
self.add_song_from_service = self.settings.value('songs/add song from service')
2009-08-26 05:00:19 +00:00
def retranslate_ui(self):
self.search_text_label.setText('{text}:'.format(text=UiStrings().Search))
2013-03-19 22:00:50 +00:00
self.search_text_button.setText(UiStrings().Search)
2014-03-06 22:05:15 +00:00
self.maintenance_action.setText(SongStrings.SongMaintenance)
self.maintenance_action.setToolTip(translate('SongsPlugin.MediaItem',
'Maintain the lists of authors, topics and books.'))
def initialise(self):
2013-04-23 19:08:07 +00:00
"""
Initialise variables when they cannot be initialised in the constructor.
"""
2013-05-23 16:15:15 +00:00
self.song_maintenance_form = SongMaintenanceForm(self.plugin.manager, self)
self.edit_song_form = EditSongForm(self, self.main_window, self.plugin.manager)
2014-03-06 22:05:15 +00:00
self.open_lyrics = OpenLyrics(self.plugin.manager)
2013-03-19 22:00:50 +00:00
self.search_text_edit.set_search_types([
2018-04-08 17:24:31 +00:00
(SongSearch.Entire, UiIcons().music,
translate('SongsPlugin.MediaItem', 'Entire Song'),
translate('SongsPlugin.MediaItem', 'Search Entire Song...')),
2018-06-03 16:07:20 +00:00
(SongSearch.Titles, UiIcons().search_text,
translate('SongsPlugin.MediaItem', 'Titles'),
translate('SongsPlugin.MediaItem', 'Search Titles...')),
(SongSearch.Lyrics, UiIcons().search_lyrics,
translate('SongsPlugin.MediaItem', 'Lyrics'),
translate('SongsPlugin.MediaItem', 'Search Lyrics...')),
2018-06-03 16:07:20 +00:00
(SongSearch.Authors, UiIcons().user, SongStrings.Authors,
translate('SongsPlugin.MediaItem', 'Search Authors...')),
(SongSearch.Topics, UiIcons().light_bulb, SongStrings.Topics,
translate('SongsPlugin.MediaItem', 'Search Topics...')),
2018-04-08 18:21:22 +00:00
(SongSearch.Books, UiIcons().address, SongStrings.SongBooks,
2016-01-04 18:46:00 +00:00
translate('SongsPlugin.MediaItem', 'Search Songbooks...')),
2018-06-03 16:07:20 +00:00
(SongSearch.Themes, UiIcons().theme, UiStrings().Themes, UiStrings().SearchThemes),
2018-04-08 17:24:31 +00:00
(SongSearch.Copyright, UiIcons().copyright,
translate('SongsPlugin.MediaItem', 'Copyright'),
translate('SongsPlugin.MediaItem', 'Search Copyright...')),
2018-06-03 16:07:20 +00:00
(SongSearch.CCLInumber, UiIcons().search_ccli,
translate('SongsPlugin.MediaItem', 'CCLI number'),
translate('SongsPlugin.MediaItem', 'Search CCLI number...'))
2010-12-28 21:12:20 +00:00
])
2013-02-07 07:08:35 +00:00
self.config_update()
2013-03-19 22:00:50 +00:00
def on_search_text_button_clicked(self):
# Reload the list considering the new search type.
2013-08-31 18:17:38 +00:00
search_keywords = str(self.search_text_edit.displayText())
2013-03-19 22:00:50 +00:00
search_type = self.search_text_edit.current_search_type()
2011-02-12 22:01:18 +00:00
if search_type == SongSearch.Entire:
2013-08-31 18:17:38 +00:00
log.debug('Entire Song Search')
2013-04-18 09:28:21 +00:00
search_results = self.search_entire(search_keywords)
self.display_results_song(search_results)
2011-02-12 22:01:18 +00:00
elif search_type == SongSearch.Titles:
2013-08-31 18:17:38 +00:00
log.debug('Titles Search')
search_string = '%{text}%'.format(text=clean_string(search_keywords))
2014-03-06 22:05:15 +00:00
search_results = self.plugin.manager.get_all_objects(Song, Song.search_title.like(search_string))
2013-04-18 09:28:21 +00:00
self.display_results_song(search_results)
2011-02-12 22:01:18 +00:00
elif search_type == SongSearch.Lyrics:
2013-08-31 18:17:38 +00:00
log.debug('Lyrics Search')
search_string = '%{text}%'.format(text=clean_string(search_keywords))
2014-03-06 22:05:15 +00:00
search_results = self.plugin.manager.get_all_objects(Song, Song.search_lyrics.like(search_string))
2013-04-18 09:28:21 +00:00
self.display_results_song(search_results)
2011-02-12 22:01:18 +00:00
elif search_type == SongSearch.Authors:
2013-08-31 18:17:38 +00:00
log.debug('Authors Search')
search_string = '%{text}%'.format(text=search_keywords)
2014-03-06 22:05:15 +00:00
search_results = self.plugin.manager.get_all_objects(
Author, Author.display_name.like(search_string))
2013-04-18 09:28:21 +00:00
self.display_results_author(search_results)
elif search_type == SongSearch.Topics:
log.debug('Topics Search')
search_string = '%{text}%'.format(text=search_keywords)
search_results = self.plugin.manager.get_all_objects(
Topic, Topic.name.like(search_string))
self.display_results_topic(search_results)
elif search_type == SongSearch.Books:
2016-01-04 18:46:00 +00:00
log.debug('Songbook Search')
search_keywords = search_keywords.rpartition(' ')
search_book = '{text}%'.format(text=search_keywords[0])
search_entry = '{text}%'.format(text=search_keywords[2])
search_results = (self.plugin.manager.session.query(SongBookEntry.entry, SongBook.name, Song.title, Song.id)
.join(Song)
.join(SongBook)
.filter(SongBook.name.like(search_book), SongBookEntry.entry.like(search_entry),
Song.temporary.is_(False)).all())
self.display_results_book(search_results)
2011-02-12 22:01:18 +00:00
elif search_type == SongSearch.Themes:
2013-08-31 18:17:38 +00:00
log.debug('Theme Search')
search_string = '%{text}%'.format(text=search_keywords)
search_results = self.plugin.manager.get_all_objects(
Song, Song.theme_name.like(search_string))
self.display_results_themes(search_results)
elif search_type == SongSearch.Copyright:
log.debug('Copyright Search')
search_string = '%{text}%'.format(text=search_keywords)
search_results = self.plugin.manager.get_all_objects(
Song, and_(Song.copyright.like(search_string), Song.copyright != ''))
2013-04-18 09:28:21 +00:00
self.display_results_song(search_results)
elif search_type == SongSearch.CCLInumber:
log.debug('CCLI number Search')
search_string = '%{text}%'.format(text=search_keywords)
search_results = self.plugin.manager.get_all_objects(
Song, and_(Song.ccli_number.like(search_string), Song.ccli_number != ''))
self.display_results_cclinumber(search_results)
2013-04-18 09:28:21 +00:00
def search_entire(self, search_keywords):
search_string = '%{text}%'.format(text=clean_string(search_keywords))
2017-06-05 21:41:29 +00:00
return self.plugin.manager.session.query(Song) \
.join(SongBookEntry, isouter=True) \
.join(SongBook, isouter=True) \
.filter(or_(SongBook.name.like(search_string), SongBookEntry.entry.like(search_string),
2017-06-05 21:41:29 +00:00
# hint: search_title contains alternate title
Song.search_title.like(search_string), Song.search_lyrics.like(search_string),
Song.comments.like(search_string))) \
.all()
2013-02-07 11:33:47 +00:00
def on_song_list_load(self):
"""
2014-03-06 22:05:15 +00:00
Handle the exit from the edit dialog and trigger remote updates of songs
"""
2013-08-31 18:17:38 +00:00
log.debug('on_song_list_load - start')
2013-04-18 09:17:00 +00:00
# Called to redisplay the song list screen edit from a search or from the exit of the Song edit dialog. If
# remote editing is active Trigger it and clean up so it will not update again. Push edits to the service
# manager to update items
2014-03-06 22:05:15 +00:00
if self.edit_item and self.update_service_on_edit and not self.remote_triggered:
2013-04-18 09:22:20 +00:00
item = self.build_service_item(self.edit_item)
2013-01-27 20:36:18 +00:00
self.service_manager.replace_service_item(item)
2013-03-19 22:00:50 +00:00
self.on_search_text_button_clicked()
2013-08-31 18:17:38 +00:00
log.debug('on_song_list_load - finished')
2009-06-21 17:45:59 +00:00
2014-03-06 22:05:15 +00:00
def display_results_song(self, search_results):
"""
Display the song search results in the media manager list
:param search_results: A list of db Song objects
:return: None
"""
def get_song_key(song):
"""Get the key to sort by"""
return song.sort_key
2016-04-25 11:01:32 +00:00
2013-08-31 18:17:38 +00:00
log.debug('display results Song')
2013-03-19 22:00:50 +00:00
self.save_auto_select_id()
self.list_view.clear()
search_results.sort(key=get_song_key)
2014-03-06 22:05:15 +00:00
for song in search_results:
2011-12-03 09:05:01 +00:00
# Do not display temporary songs
2011-12-03 19:08:02 +00:00
if song.temporary:
2011-12-03 17:01:57 +00:00
continue
2011-02-25 16:29:03 +00:00
author_list = [author.display_name for author in song.authors]
text = create_separated_list(author_list) if author_list else song.title
song_detail = '{title} ({author})'.format(title=song.title, author=text)
2015-11-07 00:49:40 +00:00
song_name = QtWidgets.QListWidgetItem(song_detail)
2012-05-17 15:13:09 +00:00
song_name.setData(QtCore.Qt.UserRole, song.id)
2013-03-19 22:00:50 +00:00
self.list_view.addItem(song_name)
# Auto-select the item if name has been set
2013-03-20 18:35:28 +00:00
if song.id == self.auto_select_id:
2013-03-19 22:00:50 +00:00
self.list_view.setCurrentItem(song_name)
2013-03-20 18:35:28 +00:00
self.auto_select_id = -1
2014-03-06 22:05:15 +00:00
def display_results_author(self, search_results):
"""
Display the song search results in the media manager list, grouped by author
:param search_results: A list of db Author objects
:return: None
"""
def get_author_key(author):
"""Get the key to sort by"""
return get_natural_key(author.display_name)
2016-04-25 11:01:32 +00:00
def get_song_key(song):
"""Get the key to sort by"""
return song.sort_key
2016-04-25 11:01:32 +00:00
2013-08-31 18:17:38 +00:00
log.debug('display results Author')
2013-03-19 22:00:50 +00:00
self.list_view.clear()
search_results.sort(key=get_author_key)
2014-03-06 22:05:15 +00:00
for author in search_results:
author.songs.sort(key=get_song_key)
for song in author.songs:
2011-12-03 09:05:01 +00:00
# Do not display temporary songs
2011-12-03 19:08:02 +00:00
if song.temporary:
2011-12-03 17:01:57 +00:00
continue
song_detail = '{author} ({title})'.format(author=author.display_name, title=song.title)
2015-11-07 00:49:40 +00:00
song_name = QtWidgets.QListWidgetItem(song_detail)
2012-05-17 15:13:09 +00:00
song_name.setData(QtCore.Qt.UserRole, song.id)
2013-03-19 22:00:50 +00:00
self.list_view.addItem(song_name)
def display_results_book(self, search_results):
"""
Display the song search results in the media manager list, grouped by book and entry
2016-04-29 16:43:14 +00:00
:param search_results: A tuple containing (songbook entry, book name, song title, song id)
:return: None
"""
def get_songbook_key(text):
2018-01-19 21:31:36 +00:00
"""
Get the key to sort by
:param text: the text tuple to be processed.
2018-01-19 21:31:36 +00:00
"""
return get_natural_key('{0} {1} {2}'.format(text[1], text[0], text[2]))
2016-04-25 11:01:32 +00:00
2013-08-31 18:17:38 +00:00
log.debug('display results Book')
2013-03-19 22:00:50 +00:00
self.list_view.clear()
search_results.sort(key=get_songbook_key)
for result in search_results:
song_detail = '{result1} #{result0}: {result2}'.format(result1=result[1], result0=result[0],
result2=result[2])
2016-01-04 18:46:00 +00:00
song_name = QtWidgets.QListWidgetItem(song_detail)
song_name.setData(QtCore.Qt.UserRole, result[3])
2016-01-04 18:46:00 +00:00
self.list_view.addItem(song_name)
2009-06-21 17:45:59 +00:00
def display_results_topic(self, search_results):
"""
Display the song search results in the media manager list, grouped by topic
:param search_results: A list of db Topic objects
:return: None
"""
def get_topic_key(topic):
"""Get the key to sort by"""
return get_natural_key(topic.name)
2016-04-25 11:01:32 +00:00
def get_song_key(song):
"""Get the key to sort by"""
return song.sort_key
2016-04-25 11:01:32 +00:00
log.debug('display results Topic')
self.list_view.clear()
search_results.sort(key=get_topic_key)
for topic in search_results:
topic.songs.sort(key=get_song_key)
for song in topic.songs:
# Do not display temporary songs
if song.temporary:
continue
song_detail = '{topic} ({title})'.format(topic=topic.name, title=song.title)
2016-02-06 17:50:58 +00:00
song_name = QtWidgets.QListWidgetItem(song_detail)
song_name.setData(QtCore.Qt.UserRole, song.id)
self.list_view.addItem(song_name)
def display_results_themes(self, search_results):
"""
Display the song search results in the media manager list, sorted by theme
:param search_results: A list of db Song objects
:return: None
"""
def get_theme_key(song):
"""Get the key to sort by"""
2018-01-19 21:31:36 +00:00
return get_natural_key(song.theme_name), song.sort_key
2016-04-25 11:01:32 +00:00
log.debug('display results Themes')
self.list_view.clear()
search_results.sort(key=get_theme_key)
for song in search_results:
# Do not display temporary songs
if song.temporary:
continue
song_detail = '{theme} ({song})'.format(theme=song.theme_name, song=song.title)
2016-02-06 17:50:58 +00:00
song_name = QtWidgets.QListWidgetItem(song_detail)
song_name.setData(QtCore.Qt.UserRole, song.id)
self.list_view.addItem(song_name)
def display_results_cclinumber(self, search_results):
"""
Display the song search results in the media manager list, sorted by CCLI number
:param search_results: A list of db Song objects
:return: None
"""
def get_cclinumber_key(song):
"""Get the key to sort by"""
2018-01-19 21:31:36 +00:00
return get_natural_key(song.ccli_number), song.sort_key
2016-04-25 11:01:32 +00:00
log.debug('display results CCLI number')
self.list_view.clear()
search_results.sort(key=get_cclinumber_key)
for song in search_results:
# Do not display temporary songs
if song.temporary:
continue
song_detail = '{ccli} ({song})'.format(ccli=song.ccli_number, song=song.title)
2016-02-06 17:50:58 +00:00
song_name = QtWidgets.QListWidgetItem(song_detail)
song_name.setData(QtCore.Qt.UserRole, song.id)
self.list_view.addItem(song_name)
2013-04-18 17:10:26 +00:00
def on_clear_text_button_click(self):
"""
Clear the search text.
"""
2013-03-19 22:00:50 +00:00
self.search_text_edit.clear()
self.on_search_text_button_clicked()
2013-03-19 22:00:50 +00:00
def on_search_text_edit_changed(self, text):
2009-11-03 06:15:35 +00:00
"""
2013-04-18 09:17:00 +00:00
If search as type enabled invoke the search on each key press. If the Lyrics are being searched do not start
till 7 characters have been entered.
2009-11-03 06:15:35 +00:00
"""
2015-11-02 20:52:22 +00:00
if self.is_search_as_you_type_enabled:
2009-10-17 05:47:17 +00:00
search_length = 1
2013-03-19 22:00:50 +00:00
if self.search_text_edit.current_search_type() == SongSearch.Entire:
2011-03-11 18:03:16 +00:00
search_length = 4
2013-03-19 22:00:50 +00:00
elif self.search_text_edit.current_search_type() == SongSearch.Lyrics:
2011-03-11 18:03:16 +00:00
search_length = 3
2009-10-17 05:47:17 +00:00
if len(text) > search_length:
2013-03-19 22:00:50 +00:00
self.on_search_text_button_clicked()
elif not text:
2013-04-18 17:10:26 +00:00
self.on_clear_text_button_click()
2013-03-19 20:05:13 +00:00
def on_import_click(self):
2013-08-31 18:17:38 +00:00
if not hasattr(self, 'import_wizard'):
2013-03-07 08:05:43 +00:00
self.import_wizard = SongImportForm(self, self.plugin)
2015-11-07 00:49:40 +00:00
self.import_wizard.exec()
# Run song load as list may have been cancelled but some songs loaded
2013-08-31 18:17:38 +00:00
Registry().execute('songs_load_list')
2010-04-02 12:23:40 +00:00
2013-03-19 20:05:13 +00:00
def on_export_click(self):
2014-03-06 22:05:15 +00:00
if not hasattr(self, 'export_wizard'):
self.export_wizard = SongExportForm(self, self.plugin)
2015-11-07 00:49:40 +00:00
self.export_wizard.exec()
2011-01-23 16:05:46 +00:00
2013-03-19 22:00:50 +00:00
def on_new_click(self):
2013-08-31 18:17:38 +00:00
log.debug('on_new_click')
2013-04-18 09:20:56 +00:00
self.edit_song_form.new_song()
2015-11-07 00:49:40 +00:00
self.edit_song_form.exec()
2013-04-18 17:10:26 +00:00
self.on_clear_text_button_click()
2013-03-19 22:00:50 +00:00
self.on_selection_change()
2013-03-20 18:35:28 +00:00
self.auto_select_id = -1
2013-04-18 09:30:55 +00:00
def on_song_maintenance_click(self):
2015-11-07 00:49:40 +00:00
self.song_maintenance_form.exec()
2013-04-18 09:32:48 +00:00
def on_remote_edit(self, song_id, preview=False):
2009-11-03 06:15:35 +00:00
"""
2013-04-18 09:17:00 +00:00
Called by ServiceManager or SlideController by event passing the Song Id in the payload along with an indicator
to say which type of display is required.
2018-01-19 21:31:36 +00:00
:param song_id: the id of the song
:param preview: show we preview after the update
2009-11-03 06:15:35 +00:00
"""
log.debug('on_remote_edit for song {song}'.format(song=song_id))
2011-04-10 18:44:27 +00:00
song_id = int(song_id)
2011-05-28 09:53:37 +00:00
valid = self.plugin.manager.get_object(Song, song_id)
2009-11-03 19:01:53 +00:00
if valid:
2013-04-18 09:20:56 +00:00
self.edit_song_form.load_song(song_id, preview)
2015-11-07 00:49:40 +00:00
if self.edit_song_form.exec() == QtWidgets.QDialog.Accepted:
2013-03-20 18:35:28 +00:00
self.auto_select_id = -1
2013-02-07 11:33:47 +00:00
self.on_song_list_load()
2013-04-18 09:22:20 +00:00
self.remote_song = song_id
2013-03-19 22:00:50 +00:00
self.remote_triggered = True
item = self.build_service_item(remote=True)
2013-04-18 09:22:20 +00:00
self.remote_song = -1
2013-03-19 22:00:50 +00:00
self.remote_triggered = None
2013-01-27 09:57:03 +00:00
if item:
if preview:
# A song can only be edited if it comes from plugin, so we set it again for the new item.
item.from_plugin = True
2013-01-27 09:57:03 +00:00
return item
return None
2009-10-29 09:18:26 +00:00
2013-03-19 22:00:50 +00:00
def on_edit_click(self):
"""
Edit a song
"""
2013-08-31 18:17:38 +00:00
log.debug('on_edit_click')
2013-03-19 22:00:50 +00:00
if check_item_selected(self.list_view, UiStrings().SelectEdit):
2013-04-18 09:22:20 +00:00
self.edit_item = self.list_view.currentItem()
item_id = self.edit_item.data(QtCore.Qt.UserRole)
2013-04-18 09:20:56 +00:00
self.edit_song_form.load_song(item_id, False)
2015-11-07 00:49:40 +00:00
self.edit_song_form.exec()
2013-03-20 18:35:28 +00:00
self.auto_select_id = -1
2013-02-07 11:33:47 +00:00
self.on_song_list_load()
2013-04-18 09:22:20 +00:00
self.edit_item = None
2013-03-19 22:00:50 +00:00
def on_delete_click(self):
"""
2021-04-15 17:33:10 +00:00
Remove a song or songs from the list and database
"""
2013-03-19 22:00:50 +00:00
if check_item_selected(self.list_view, UiStrings().SelectDelete):
items = self.list_view.selectedItems()
2021-04-15 17:33:10 +00:00
item_strings = map(lambda i: i.text(), items)
delete_confirmed = ConfirmationForm(self, UiStrings().ConfirmDelete, item_strings,
translate('SongsPlugin.MediaItem',
'Are you sure you want to delete these songs?')).exec()
if not delete_confirmed:
2010-04-04 13:53:39 +00:00
return
2013-02-03 19:23:12 +00:00
self.application.set_busy_cursor()
2013-03-16 11:05:52 +00:00
self.main_window.display_progress_bar(len(items))
2010-04-03 19:17:37 +00:00
for item in items:
2012-05-19 09:13:32 +00:00
item_id = item.data(QtCore.Qt.UserRole)
2013-06-14 20:20:26 +00:00
delete_song(item_id, self.plugin)
2013-03-07 08:05:43 +00:00
self.main_window.increment_progress_bar()
2013-03-16 11:05:52 +00:00
self.main_window.finished_progress_bar()
2013-02-03 19:23:12 +00:00
self.application.set_normal_cursor()
2013-03-19 22:00:50 +00:00
self.on_search_text_button_clicked()
2013-04-18 09:30:55 +00:00
def on_clone_click(self):
"""
Clone a Song
"""
2013-08-31 18:17:38 +00:00
log.debug('on_clone_click')
2013-03-19 22:00:50 +00:00
if check_item_selected(self.list_view, UiStrings().SelectEdit):
2013-04-18 09:22:20 +00:00
self.edit_item = self.list_view.currentItem()
item_id = self.edit_item.data(QtCore.Qt.UserRole)
old_song = self.plugin.manager.get_object(Song, item_id)
2014-03-06 22:05:15 +00:00
song_xml = self.open_lyrics.song_to_xml(old_song)
new_song = self.open_lyrics.xml_to_song(song_xml)
new_song.title = '{title} <{text}>'.format(title=new_song.title,
text=translate('SongsPlugin.MediaItem',
'copy', 'For song cloning'))
# Copy audio files from the old to the new song
if len(old_song.media_files) > 0:
2017-09-30 20:16:30 +00:00
save_path = AppLocation.get_section_data_path(self.plugin.name) / 'audio' / str(new_song.id)
2017-10-10 02:29:56 +00:00
create_paths(save_path)
for media_file in old_song.media_files:
2017-09-30 20:16:30 +00:00
new_media_file_path = save_path / media_file.file_path.name
copyfile(media_file.file_path, new_media_file_path)
new_media_file = MediaFile()
2017-09-30 20:16:30 +00:00
new_media_file.file_path = new_media_file_path
new_media_file.file_hash = media_file.file_hash
new_media_file.type = media_file.type
new_media_file.weight = media_file.weight
new_song.media_files.append(new_media_file)
self.plugin.manager.save_object(new_song)
new_song.init_on_load()
2013-02-07 11:33:47 +00:00
self.on_song_list_load()
2019-03-17 10:01:52 +00:00
def generate_slide_data(self, service_item, *, item=None, context=ServiceItemContext.Service, **kwargs):
2013-03-19 22:00:50 +00:00
"""
Generate the slide data. Needs to be implemented by the plugin.
2014-03-06 22:05:15 +00:00
:param service_item: The service item to be built on
:param item: The Song item to be used
:param context: Why is it being generated
2019-03-17 10:01:52 +00:00
:param kwargs: Consume other unused args specified by the base implementation, but not use by this one.
2013-03-19 22:00:50 +00:00
"""
log.debug('generate_slide_data: {service}, {item}, {remote}'.format(service=service_item, item=item,
remote=self.remote_song))
2013-04-18 09:22:20 +00:00
item_id = self._get_id_of_item_to_generate(item, self.remote_song)
2011-08-28 17:45:13 +00:00
service_item.add_capability(ItemCapabilities.CanEdit)
service_item.add_capability(ItemCapabilities.CanPreview)
service_item.add_capability(ItemCapabilities.CanLoop)
2010-09-30 05:12:06 +00:00
service_item.add_capability(ItemCapabilities.OnLoadUpdate)
service_item.add_capability(ItemCapabilities.AddIfNewItem)
service_item.add_capability(ItemCapabilities.CanSoftBreak)
2018-02-18 16:16:52 +00:00
service_item.add_capability(ItemCapabilities.HasMetaData)
2011-05-28 09:53:37 +00:00
song = self.plugin.manager.get_object(Song, item_id)
service_item.theme = song.theme_name
service_item.edit_id = item_id
2014-03-06 22:05:15 +00:00
verse_list = SongXML().get_verses(song.lyrics)
if self.settings.value('songs/add songbook slide') and song.songbook_entries:
2018-03-09 21:58:45 +00:00
first_slide = '\n'
2018-02-23 08:27:33 +00:00
for songbook_entry in song.songbook_entries:
if songbook_entry.entry:
first_slide += '{book} #{num}'.format(book=songbook_entry.songbook.name,
num=songbook_entry.entry)
else:
first_slide += songbook_entry.songbook.name
2019-02-22 07:34:40 +00:00
if songbook_entry.songbook.publisher:
first_slide += ' ({pub})'.format(pub=songbook_entry.songbook.publisher)
first_slide += '\n\n'
2018-02-23 08:27:33 +00:00
2018-03-09 21:58:45 +00:00
service_item.add_from_text(first_slide, 'O1')
2014-03-06 22:05:15 +00:00
# no verse list or only 1 space (in error)
verse_tags_translated = False
if VerseType.from_translated_string(str(verse_list[0][0]['type'])) is not None:
verse_tags_translated = True
if not song.verse_order.strip():
for verse in verse_list:
# We cannot use from_loose_input() here, because database is supposed to contain English lowercase
2017-04-05 20:28:43 +00:00
# single char tags.
2014-03-06 22:05:15 +00:00
verse_tag = verse[0]['type']
verse_index = None
if len(verse_tag) > 1:
verse_index = VerseType.from_translated_string(verse_tag)
if verse_index is None:
2014-03-06 22:05:15 +00:00
verse_index = VerseType.from_string(verse_tag, None)
if verse_index is None:
verse_index = VerseType.from_tag(verse_tag)
verse_tag = VerseType.translated_tags[verse_index].upper()
verse_def = '{tag}{label}'.format(tag=verse_tag, label=verse[0]['label'])
2017-08-27 17:13:14 +00:00
force_verse = verse[1].split('[--}{--]\n')
2017-04-05 20:28:43 +00:00
for split_verse in force_verse:
service_item.add_from_text(split_verse, verse_def)
else:
2014-03-06 22:05:15 +00:00
# Loop through the verse list and expand the song accordingly.
for order in song.verse_order.lower().split():
if not order:
break
for verse in verse_list:
if verse[0]['type'][0].lower() == \
order[0] and (verse[0]['label'].lower() == order[1:] or not order[1:]):
if verse_tags_translated:
verse_index = VerseType.from_translated_tag(verse[0]['type'])
else:
verse_index = VerseType.from_tag(verse[0]['type'])
verse_tag = VerseType.translated_tags[verse_index]
2016-07-10 06:25:25 +00:00
verse_def = '{tag}{label}'.format(tag=verse_tag, label=verse[0]['label'])
2017-08-27 17:13:14 +00:00
force_verse = verse[1].split('[--}{--]\n')
2017-04-05 20:28:43 +00:00
for split_verse in force_verse:
service_item.add_from_text(split_verse, verse_def)
service_item.title = song.title
2013-04-21 15:53:51 +00:00
author_list = self.generate_footer(service_item, song)
service_item.data_string = {
'title': song.search_title,
2020-10-19 07:18:00 +00:00
'alternate_title': song.alternate_title,
'authors': ', '.join(author_list),
2020-10-19 07:18:00 +00:00
'ccli_number': song.ccli_number,
'copyright': song.copyright
}
2014-03-06 22:05:15 +00:00
service_item.xml_version = self.open_lyrics.song_to_xml(song)
2011-08-28 17:45:13 +00:00
# Add the audio file to the service item.
if song.media_files:
if State().check_preconditions('media'):
service_item.add_capability(ItemCapabilities.HasBackgroundAudio)
2018-11-20 21:41:23 +00:00
total_length = 0
for m in song.media_files:
total_length += self.media_controller.media_length(m.file_path)
service_item.background_audio = [(m.file_path, m.file_hash) for m in song.media_files]
2018-11-20 21:41:23 +00:00
service_item.set_media_length(total_length)
service_item.metadata.append('<em>{label}:</em> {media}'.
2019-01-04 20:11:12 +00:00
format(label=translate('SongsPlugin.MediaItem', 'Media'),
media=service_item.background_audio))
# In older versions of OpenLP, this setting may have been a Qt enum, so we typecast to bool
# TODO: Remove this after 3.0
service_item.will_auto_start = bool(self.settings.value('songs/auto play audio'))
2010-07-27 09:32:52 +00:00
return True
2010-09-30 05:12:06 +00:00
2013-04-21 15:53:51 +00:00
def generate_footer(self, item, song):
"""
Generates the song footer based on a song and adds details to a service item.
2014-03-06 22:05:15 +00:00
:param item: The service item to be amended
:param song: The song to be used to generate the footer
2014-03-30 17:23:36 +00:00
:return: List of all authors (only required for initial song generation)
2013-04-21 15:53:51 +00:00
"""
authors_words = []
authors_music = []
authors_words_music = []
authors_translation = []
authors_none = []
for author_song in song.authors_songs:
2014-03-30 17:23:36 +00:00
if author_song.author_type == AuthorType.Words:
authors_words.append(author_song.author.display_name)
2014-03-30 17:23:36 +00:00
elif author_song.author_type == AuthorType.Music:
authors_music.append(author_song.author.display_name)
elif author_song.author_type == AuthorType.WordsAndMusic:
authors_words_music.append(author_song.author.display_name)
2014-03-30 17:23:36 +00:00
elif author_song.author_type == AuthorType.Translation:
authors_translation.append(author_song.author.display_name)
else:
authors_none.append(author_song.author.display_name)
authors_all = authors_words_music + authors_words + authors_music + authors_translation + authors_none
2013-04-21 15:53:51 +00:00
item.audit = [
song.title, authors_all, song.copyright, str(song.ccli_number)
2013-04-21 15:53:51 +00:00
]
item.raw_footer = []
item.raw_footer.append(song.title)
if authors_none:
item.raw_footer.append("{text}: {authors}".format(text=translate('OpenLP.Ui', 'Written by'),
authors=create_separated_list(authors_none)))
if authors_words_music:
item.raw_footer.append("{text}: {authors}".format(text=AuthorType.Types[AuthorType.WordsAndMusic],
authors=create_separated_list(authors_words_music)))
if authors_words:
item.raw_footer.append("{text}: {authors}".format(text=AuthorType.Types[AuthorType.Words],
authors=create_separated_list(authors_words)))
if authors_music:
item.raw_footer.append("{text}: {authors}".format(text=AuthorType.Types[AuthorType.Music],
authors=create_separated_list(authors_music)))
if authors_translation:
item.raw_footer.append("{text}: {authors}".format(text=AuthorType.Types[AuthorType.Translation],
authors=create_separated_list(authors_translation)))
2014-07-09 12:33:26 +00:00
if song.copyright:
item.raw_footer.append("{symbol} {song}".format(symbol=SongStrings.CopyrightSymbol,
song=song.copyright))
songbooks = [str(songbook_entry) for songbook_entry in song.songbook_entries]
if song.songbook_entries:
item.raw_footer.append(", ".join(songbooks))
if self.settings.value('core/ccli number'):
item.raw_footer.append(translate('SongsPlugin.MediaItem', 'CCLI License: ') +
self.settings.value('core/ccli number'))
footer_template = self.settings.value('songs/footer template')
# Keep this in sync with the list in songstab.py
2017-06-12 17:43:24 +00:00
vars = {
'title': song.title,
'alternate_title': song.alternate_title,
'authors_none_label': translate('OpenLP.Ui', 'Written by'),
2017-06-12 17:43:24 +00:00
'authors_none': authors_none,
'authors_words_label': AuthorType.Types[AuthorType.Words],
2017-06-12 17:43:24 +00:00
'authors_words': authors_words,
'authors_music_label': AuthorType.Types[AuthorType.Music],
2017-06-12 17:43:24 +00:00
'authors_music': authors_music,
'authors_words_music_label': AuthorType.Types[AuthorType.WordsAndMusic],
2017-06-12 17:43:24 +00:00
'authors_words_music': authors_words_music,
'authors_translation_label': AuthorType.Types[AuthorType.Translation],
2017-06-12 17:43:24 +00:00
'authors_translation': authors_translation,
'authors_words_all': authors_words + authors_words_music,
'authors_music_all': authors_music + authors_words_music,
'copyright': song.copyright,
2017-06-12 17:43:24 +00:00
'songbook_entries': songbooks,
'ccli_license': self.settings.value('core/ccli number'),
'ccli_license_label': translate('SongsPlugin.MediaItem', 'CCLI License'),
'ccli_number': song.ccli_number,
2017-06-12 17:43:24 +00:00
'topics': [topic.name for topic in song.topics]
}
try:
item.footer_html = mako.template.Template(footer_template).render_unicode(**vars).replace('\n', '')
except mako.exceptions.SyntaxException:
log.error('Failed to render Song footer html:\n' + mako.exceptions.text_error_template().render())
critical_error_message_box(message=translate('SongsPlugin.MediaItem',
'Failed to render Song footer html.\nSee log for details'))
return authors_all
2013-04-21 15:53:51 +00:00
2013-03-19 22:00:50 +00:00
def service_load(self, item):
2010-09-30 05:12:06 +00:00
"""
2011-03-14 18:59:59 +00:00
Triggered by a song being loaded by the service manager.
2010-09-30 05:12:06 +00:00
"""
2013-08-31 18:17:38 +00:00
log.debug('service_load')
2011-02-05 23:52:05 +00:00
if self.plugin.status != PluginStatus.Active or not item.data_string:
return
2014-05-01 17:06:47 +00:00
search_results = self.plugin.manager.get_all_objects(
Song, Song.search_title == item.data_string['title'], Song.search_title.asc())
2013-04-21 15:53:51 +00:00
edit_id = 0
2011-02-05 23:52:05 +00:00
add_song = True
if search_results:
for song in search_results:
2014-05-13 09:27:22 +00:00
if self._authors_match(song, item.data_string['authors']):
2011-02-05 23:52:05 +00:00
add_song = False
2013-04-21 15:53:51 +00:00
edit_id = song.id
2011-02-05 23:52:05 +00:00
break
# If there's any backing tracks, copy them over.
if item.background_audio:
2013-04-18 09:20:56 +00:00
self._update_background_audio(song, item)
2014-03-06 22:05:15 +00:00
if add_song and self.add_song_from_service:
song = self.open_lyrics.xml_to_song(item.xml_version)
# If there's any backing tracks, copy them over.
if item.background_audio:
2013-04-18 09:20:56 +00:00
self._update_background_audio(song, item)
2014-03-06 22:05:15 +00:00
edit_id = song.id
song.init_on_load()
2013-03-19 22:00:50 +00:00
self.on_search_text_button_clicked()
2014-03-06 22:05:15 +00:00
elif add_song and not self.add_song_from_service:
# Make sure we temporary import formatting tags.
2014-03-06 22:05:15 +00:00
song = self.open_lyrics.xml_to_song(item.xml_version, True)
2011-12-05 19:47:50 +00:00
# If there's any backing tracks, copy them over.
if item.background_audio:
2013-04-18 09:20:56 +00:00
self._update_background_audio(song, item)
2013-04-21 15:53:51 +00:00
edit_id = song.id
# Update service with correct song id and return it to caller.
item.edit_id = edit_id
self.generate_footer(item, song)
return item
2010-12-09 15:08:04 +00:00
2018-01-19 21:31:36 +00:00
@staticmethod
def _authors_match(song, authors):
2014-05-13 09:27:22 +00:00
"""
Checks whether authors from a song in the database match the authors of the song to be imported.
:param song: A list of authors from the song in the database
:param authors: A string with authors from the song to be imported
2014-05-13 09:44:19 +00:00
:return: True when Authors do match, else False.
2014-05-13 09:27:22 +00:00
"""
author_list = authors.split(', ')
for author in song.authors:
if author.display_name in author_list:
2014-05-13 09:44:19 +00:00
author_list.remove(author.display_name)
2014-05-13 09:27:22 +00:00
else:
return False
# List must be empty at the end
return not author_list
2018-01-19 21:31:36 +00:00
def search(self, string, show_error=True):
"""
Search for some songs
2014-03-06 22:05:15 +00:00
:param string: The string to show
:param show_error: Is this an error?
2018-01-19 21:31:36 +00:00
:return: the results of the search
"""
2013-04-18 09:28:21 +00:00
search_results = self.search_entire(string)
2015-10-16 16:58:22 +00:00
return [[song.id, song.title, song.alternate_title] for song in search_results]