From 532ad84b87fe0e8d94e57f015e0ff754c6b90c63 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sun, 28 Aug 2011 22:51:44 +0200 Subject: [PATCH] - Bundle audio files with the song when adding it to the service - Save audio files with service - Load audio files from service - Copy audio files to songs plugin when adding a song from a service. --- openlp/__init__.py | 6 ++++++ openlp/core/ui/servicemanager.py | 19 ++++++++++++++---- openlp/plugins/songs/forms/editsongform.py | 20 +++++++++---------- openlp/plugins/songs/lib/mediaitem.py | 23 +++++++++++++++++++--- openlp/plugins/songs/lib/xml.py | 2 +- 5 files changed, 52 insertions(+), 18 deletions(-) diff --git a/openlp/__init__.py b/openlp/__init__.py index 9038b48cc..5f7608770 100644 --- a/openlp/__init__.py +++ b/openlp/__init__.py @@ -27,3 +27,9 @@ """ The :mod:`openlp` module contains all the project produced OpenLP functionality """ + +import core +import plugins + +__all__ = [u'core', u'plugins'] + diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py index c84fd5d23..f4786af57 100644 --- a/openlp/core/ui/servicemanager.py +++ b/openlp/core/ui/servicemanager.py @@ -28,6 +28,7 @@ import cgi import cPickle import logging import os +import shutil import zipfile log = logging.getLogger(__name__) @@ -493,7 +494,7 @@ class ServiceManager(QtGui.QWidget): if len(service_item[u'header'][u'background_audio']) > 0: for i, filename in \ enumerate(service_item[u'header'][u'background_audio']): - new_file = os.path.join(u'audio', item._uuid, + new_file = os.path.join(u'audio', item[u'service_item']._uuid, os.path.split(filename)[1]) audio_files.append((filename, new_file)) service_item[u'header'][u'background_audio'][i] = new_file @@ -552,6 +553,8 @@ class ServiceManager(QtGui.QWidget): # Finally add all the listed media files. for path_from in write_list: zip.write(path_from, path_from.encode(u'utf-8')) + for path_from, path_to in audio_files: + zip.write(path_from, path_to.encode(u'utf-8')) except IOError: log.exception(u'Failed to save service to disk') success = False @@ -606,8 +609,14 @@ class ServiceManager(QtGui.QWidget): 'The content encoding is not UTF-8.')) continue osfile = unicode(QtCore.QDir.toNativeSeparators(ucsfile)) - filePath = os.path.join(self.servicePath, - os.path.split(osfile)[1]) + if not osfile.startswith(u'audio'): + osfile = os.path.split(osfile)[1] + else: + path = os.path.join(self.servicePath, + os.path.split(osfile)[0]) + if not os.path.exists(path): + os.makedirs(path) + filePath = os.path.join(self.servicePath, osfile) fileTo = open(filePath, u'wb') fileTo.write(zip.read(file)) fileTo.flush() @@ -644,10 +653,10 @@ class ServiceManager(QtGui.QWidget): 'File is not a valid service.')) log.exception(u'File contains no service data') except (IOError, NameError, zipfile.BadZipfile): + log.exception(u'Problem loading service file %s' % fileName) critical_error_message_box( message=translate('OpenLP.ServiceManager', 'File could not be opened because it is corrupt.')) - log.exception(u'Problem loading service file %s' % fileName) except zipfile.BadZipfile: if os.path.getsize(fileName) == 0: log.exception(u'Service file is zero sized: %s' % fileName) @@ -1012,6 +1021,8 @@ class ServiceManager(QtGui.QWidget): for file in os.listdir(self.servicePath): file_path = os.path.join(self.servicePath, file) delete_file(file_path) + if os.path.exists(os.path.join(self.servicePath, u'audio')): + shutil.rmtree(os.path.join(self.servicePath, u'audio'), False) def onThemeComboBoxSelected(self, currentIndex): """ diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index 24e2c852f..5aa8fec80 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -465,9 +465,9 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): self.verseDeleteButton.setEnabled(True) def onVerseAddButtonClicked(self): - self.verse_form.setVerse(u'', True) - if self.verse_form.exec_(): - after_text, verse_tag, verse_num = self.verse_form.getVerse() + self.verseForm.setVerse(u'', True) + if self.verseForm.exec_(): + after_text, verse_tag, verse_num = self.verseForm.getVerse() verse_def = u'%s%s' % (verse_tag, verse_num) item = QtGui.QTableWidgetItem(after_text) item.setData(QtCore.Qt.UserRole, QtCore.QVariant(verse_def)) @@ -483,9 +483,9 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): if item: tempText = item.text() verseId = unicode(item.data(QtCore.Qt.UserRole).toString()) - self.verse_form.setVerse(tempText, True, verseId) - if self.verse_form.exec_(): - after_text, verse_tag, verse_num = self.verse_form.getVerse() + self.verseForm.setVerse(tempText, True, verseId) + if self.verseForm.exec_(): + after_text, verse_tag, verse_num = self.verseForm.getVerse() verse_def = u'%s%s' % (verse_tag, verse_num) item.setData(QtCore.Qt.UserRole, QtCore.QVariant(verse_def)) item.setText(after_text) @@ -515,12 +515,12 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog): verse_list += u'---[%s:%s]---\n' % (verse_tag, verse_num) verse_list += item.text() verse_list += u'\n' - self.verse_form.setVerse(verse_list) + self.verseForm.setVerse(verse_list) else: - self.verse_form.setVerse(u'') - if not self.verse_form.exec_(): + self.verseForm.setVerse(u'') + if not self.verseForm.exec_(): return - verse_list = self.verse_form.getVerseAll() + verse_list = self.verseForm.getVerseAll() verse_list = unicode(verse_list.replace(u'\r\n', u'\n')) self.verseListWidget.clear() self.verseListWidget.setRowCount(0) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 16c2b5ecf..100761ac8 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -28,6 +28,8 @@ import logging import locale import re +import os +import shutil from PyQt4 import QtCore, QtGui from sqlalchemy.sql import or_ @@ -37,11 +39,12 @@ from openlp.core.lib import MediaManagerItem, Receiver, ItemCapabilities, \ from openlp.core.lib.searchedit import SearchEdit from openlp.core.lib.ui import UiStrings, context_menu_action, \ context_menu_separator +from openlp.core.utils import AppLocation from openlp.plugins.songs.forms import EditSongForm, SongMaintenanceForm, \ SongImportForm, SongExportForm from openlp.plugins.songs.lib import OpenLyrics, SongXML, VerseType, \ clean_string -from openlp.plugins.songs.lib.db import Author, Song +from openlp.plugins.songs.lib.db import Author, Song, MediaFile from openlp.plugins.songs.lib.ui import SongStrings log = logging.getLogger(__name__) @@ -474,7 +477,7 @@ class SongMediaItem(MediaManagerItem): # Add the audio file to the service item. if len(song.media_files) > 0: service_item.add_capability(ItemCapabilities.HasBackgroundAudio) - + service_item.background_audio = [m.file_name for m in song.media_files] return True def serviceLoad(self, item): @@ -515,7 +518,21 @@ class SongMediaItem(MediaManagerItem): editId = song.id break if add_song and self.addSongFromService: - editId = self.openLyrics.xml_to_song(item.xml_version) + song = self.openLyrics.xml_to_song(item.xml_version) + # If there's any backing tracks, copy them over. + if len(item.background_audio) > 0: + for i, bga in enumerate(item.background_audio): + dest_file = os.path.join( + AppLocation.get_section_data_path(self.plugin.name), + u'audio', str(song.id), os.path.split(bga)[1]) + shutil.copyfile(os.path.join( + AppLocation.get_section_data_path( + u'servicemanager'), bga), + dest_file) + song.media_files.append(MediaFile.populate( + weight=i, file_name=dest_file)) + self.plugin.manager.save_object(song, True) + editId = song.id self.onSearchTextButtonClick() # Update service with correct song id. if editId: diff --git a/openlp/plugins/songs/lib/xml.py b/openlp/plugins/songs/lib/xml.py index 193a823d5..9da7a0a65 100644 --- a/openlp/plugins/songs/lib/xml.py +++ b/openlp/plugins/songs/lib/xml.py @@ -343,7 +343,7 @@ class OpenLyrics(object): self._process_topics(properties, song) clean_song(self.manager, song) self.manager.save_object(song) - return song.id + return song def _add_text_to_element(self, tag, parent, text=None, label=None): if label: