From 906b0fe67e83343cc16476b6a32537179e76ef38 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 12 Sep 2011 08:29:58 +0200 Subject: [PATCH] Added support for copying files from openlp.org 1.2.x. --- openlp/plugins/songs/lib/olp1import.py | 26 ++++++++++++++-- openlp/plugins/songs/lib/songimport.py | 43 ++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/openlp/plugins/songs/lib/olp1import.py b/openlp/plugins/songs/lib/olp1import.py index edcc14a1a..cd12ca937 100644 --- a/openlp/plugins/songs/lib/olp1import.py +++ b/openlp/plugins/songs/lib/olp1import.py @@ -32,6 +32,7 @@ openlp.org 1.x song databases into the current installation database. import logging from chardet.universaldetector import UniversalDetector import sqlite +import sys from openlp.core.lib import translate from openlp.plugins.songs.lib import retrieve_windows_encoding @@ -131,7 +132,8 @@ class OpenLP1SongImport(SongImport): break if new_db: cursor.execute(u'-- types int, unicode, int') - cursor.execute(u'SELECT trackid, fulltrackname, listindex FROM songtracks ' + cursor.execute(u'SELECT trackid, fulltrackname, listindex ' + u'FROM songtracks ' u'WHERE songid = %s ORDER BY listindex' % song_id) track_ids = cursor.fetchall() for track_id, listindex in track_ids: @@ -139,7 +141,8 @@ class OpenLP1SongImport(SongImport): break for track in tracks: if track[0] == track_id: - self.addMediaFile(track[1], listindex) + media_file = self.expandMediaFile(track[1]) + self.addMediaFile(media_file, listindex) break if self.stopImportFlag: break @@ -186,3 +189,22 @@ class OpenLP1SongImport(SongImport): return detector.result[u'encoding'] detector.close() return retrieve_windows_encoding(detector.result[u'encoding']) + + def expandMediaFile(self, filename): + """ + When you're on Windows, this function expands the file name to include + the path to OpenLP's application data directory. If you are not on + Windows, it returns the original file name. + + ``filename`` + The filename to expand. + """ + if sys.platform != u'win32' and \ + not os.environ.get(u'ALLUSERSPROFILE') and \ + not os.environ.get(u'APPDATA'): + return filename + common_app_data = os.path.join(os.environ[u'ALLUSERSPROFILE'], + os.path.split(os.environ[u'APPDATA'])[1]) + if not common_app_data: + return filename + return os.path.join(common_app_data, u'openlp.org', 'Audio', filename) diff --git a/openlp/plugins/songs/lib/songimport.py b/openlp/plugins/songs/lib/songimport.py index 5b00dbd96..7afe6b054 100644 --- a/openlp/plugins/songs/lib/songimport.py +++ b/openlp/plugins/songs/lib/songimport.py @@ -26,11 +26,14 @@ ############################################################################### import logging import re +import shutil +import os from PyQt4 import QtCore from openlp.core.lib import Receiver, translate from openlp.core.ui.wizard import WizardStrings +from openlp.core.utils import AppLocation from openlp.plugins.songs.lib import clean_song, VerseType from openlp.plugins.songs.lib.db import Song, Author, Topic, Book, MediaFile from openlp.plugins.songs.lib.ui import SongStrings @@ -330,12 +333,6 @@ class SongImport(QtCore.QObject): last_name=authortext.split(u' ')[-1], first_name=u' '.join(authortext.split(u' ')[:-1])) song.authors.append(author) - for filename, weight in self.mediaFiles: - media_file = self.manager.get_object_filtered(MediaFile, - MediaFile.file_name == filename) - if not media_file: - song.media_files.append( - MediaFile.populate(file_name=filename, weight=weight)) if self.songBookName: song_book = self.manager.get_object_filtered(Book, Book.name == self.songBookName) @@ -351,7 +348,41 @@ class SongImport(QtCore.QObject): if topic is None: topic = Topic.populate(name=topictext) song.topics.append(topic) + # We need to save the song now, before adding the media files, so that + # we know where to save the media files to. clean_song(self.manager, song) self.manager.save_object(song) + # Now loop through the media files, copy them to the correct location, + # and save the song again. + for filename, weight in self.mediaFiles: + media_file = self.manager.get_object_filtered(MediaFile, + MediaFile.file_name == filename) + if not media_file: + if os.path.dirname(filename): + filename = self.copyMediaFile(filename) + song.media_files.append( + MediaFile.populate(file_name=filename, weight=weight) + ) + self.manager.save_object(song) self.setDefaults() return True + + def copyMediaFile(self, filename): + """ + This method copies the media file to the correct location and returns + the new file location. + + ``filename`` + The file to copy. + """ + if not hasattr(self, u'save_path'): + self.save_path = os.path.join( + AppLocation.get_section_data_path(self.mediaitem.plugin.name), + 'audio', str(self.song.id)) + if not os.path.exists(self.save_path): + os.makedirs(self.save_path) + if not filename.startswith(save_path): + oldfile, filename = filename, os.path.join(save_path, + os.path.split(filename)[1]) + shutil.copyfile(oldfile, filename) + return filename