openlp/openlp/plugins/songs/forms/songimportform.py

398 lines
17 KiB
Python
Raw Normal View History

2010-04-02 12:23:40 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
2010-07-24 22:10:47 +00:00
# Gorven, Scott Guerrieri, Meinert Jordan, Andreas Preikschat, Christian #
# Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
# Carsten Tinggaard, Frode Woldsund #
2010-04-02 12:23:40 +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; version 2 of the License. #
# #
# 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, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
import logging
2010-08-20 19:40:07 +00:00
import os
2010-04-02 12:23:40 +00:00
from PyQt4 import QtCore, QtGui
from songimportwizard import Ui_SongImportWizard
from openlp.core.lib import Receiver, SettingsManager, translate
#from openlp.core.utils import AppLocation
from openlp.plugins.songs.lib.importer import SongFormat
2010-04-02 12:23:40 +00:00
log = logging.getLogger(__name__)
class ImportWizardForm(QtGui.QWizard, Ui_SongImportWizard):
"""
2010-08-08 14:38:51 +00:00
This is the Song Import Wizard, which allows easy importing of Songs
into OpenLP from other formats like OpenLyrics, OpenSong and CCLI.
2010-04-02 12:23:40 +00:00
"""
2010-08-08 14:38:51 +00:00
log.info(u'SongImportForm loaded')
2010-04-02 12:23:40 +00:00
2010-08-08 14:38:51 +00:00
def __init__(self, parent, manager, plugin):
2010-04-02 12:23:40 +00:00
"""
Instantiate the wizard, and run any extra setup we need to.
``parent``
The QWidget-derived parent of the wizard.
``config``
The configuration object for storing and retrieving settings.
``manager``
2010-08-08 14:38:51 +00:00
The songs manager.
2010-04-02 12:23:40 +00:00
2010-08-08 14:38:51 +00:00
``plugin``
The songs plugin.
2010-04-02 12:23:40 +00:00
"""
QtGui.QWizard.__init__(self, parent)
self.setupUi(self)
self.registerFields()
2010-04-02 12:23:40 +00:00
self.finishButton = self.button(QtGui.QWizard.FinishButton)
self.cancelButton = self.button(QtGui.QWizard.CancelButton)
self.manager = manager
2010-08-08 14:38:51 +00:00
self.plugin = plugin
2010-04-02 12:23:40 +00:00
#self.manager.set_process_dialog(self)
2010-08-20 19:40:07 +00:00
QtCore.QObject.connect(self.openLP2BrowseButton,
QtCore.SIGNAL(u'clicked()'),
self.onOpenLP2BrowseButtonClicked)
QtCore.QObject.connect(self.openLP1BrowseButton,
QtCore.SIGNAL(u'clicked()'),
self.onOpenLP1BrowseButtonClicked)
QtCore.QObject.connect(self.openLyricsAddButton,
2010-08-08 14:38:51 +00:00
QtCore.SIGNAL(u'clicked()'),
self.onOpenLyricsAddButtonClicked)
2010-08-20 19:40:07 +00:00
QtCore.QObject.connect(self.openLyricsRemoveButton,
2010-08-08 14:38:51 +00:00
QtCore.SIGNAL(u'clicked()'),
self.onOpenLyricsRemoveButtonClicked)
2010-08-20 19:40:07 +00:00
QtCore.QObject.connect(self.openSongAddButton,
QtCore.SIGNAL(u'clicked()'),
self.onOpenSongAddButtonClicked)
QtCore.QObject.connect(self.openSongRemoveButton,
QtCore.SIGNAL(u'clicked()'),
self.onOpenSongRemoveButtonClicked)
QtCore.QObject.connect(self.wordsOfWorshipAddButton,
QtCore.SIGNAL(u'clicked()'),
self.onWordsOfWorshipAddButtonClicked)
QtCore.QObject.connect(self.wordsOfWorshipRemoveButton,
QtCore.SIGNAL(u'clicked()'),
self.onWordsOfWorshipRemoveButtonClicked)
QtCore.QObject.connect(self.songsOfFellowshipBrowseButton,
QtCore.SIGNAL(u'clicked()'),
self.onSongsOfFellowshipBrowseButtonClicked)
QtCore.QObject.connect(self.genericBrowseButton,
QtCore.SIGNAL(u'clicked()'),
self.onGenericBrowseButtonClicked)
2010-04-02 12:23:40 +00:00
QtCore.QObject.connect(self.cancelButton,
QtCore.SIGNAL(u'clicked(bool)'),
self.onCancelButtonClicked)
QtCore.QObject.connect(self,
QtCore.SIGNAL(u'currentIdChanged(int)'),
self.onCurrentIdChanged)
2010-04-02 12:23:40 +00:00
def exec_(self):
"""
Run the wizard.
"""
self.setDefaults()
2010-04-02 12:23:40 +00:00
return QtGui.QWizard.exec_(self)
def validateCurrentPage(self):
"""
Validate the current page before moving on to the next page.
"""
if self.currentId() == 0:
# Welcome page
return True
elif self.currentId() == 1:
# Select page
source_format = self.formatComboBox.currentIndex()
2010-08-20 19:40:07 +00:00
if source_format == SongFormat.OpenLP2:
if self.openLP2FilenameEdit.text().isEmpty():
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.ImportWizardForm',
'No OpenLP 2.0 Song Database Selected'),
translate('SongsPlugin.ImportWizardForm',
'You need to select an OpenLP 2.0 song database '
'file to import from.'))
self.openLP2BrowseButton.setFocus()
return False
elif source_format == SongFormat.OpenLP1:
if self.openSongFilenameEdit.text().isEmpty():
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.ImportWizardForm',
'No openlp.org 1.x Song Database Selected'),
translate('SongsPlugin.ImportWizardForm',
'You need to select an openlp.org 1.x song '
'database file to import from.'))
self.openLP1BrowseButton.setFocus()
return False
elif source_format == SongFormat.OpenLyrics:
if self.openLyricsFileListWidget.count() == 0:
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.ImportWizardForm',
'No OpenLyrics Files Selected'),
translate('SongsPlugin.ImportWizardForm',
'You need to add at least one OpenLyrics '
2010-07-31 02:06:44 +00:00
'song file to import from.'))
2010-08-20 19:40:07 +00:00
self.openLyricsAddButton.setFocus()
return False
elif source_format == SongFormat.OpenSong:
2010-08-20 19:40:07 +00:00
if self.openSongFileListWidget.count() == 0:
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.ImportWizardForm',
'No OpenSong Files Selected'),
translate('SongsPlugin.ImportWizardForm',
'You need to add at least one OpenSong '
2010-07-31 02:06:44 +00:00
'song file to import from.'))
2010-08-20 19:40:07 +00:00
self.openSongAddButton.setFocus()
return False
elif source_format == SongFormat.WordsOfWorship:
if self.wordsOfWorshipListWidget.count() == 0:
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.ImportWizardForm',
'No Words of Worship Files Selected'),
translate('SongsPlugin.ImportWizardForm',
'You need to add at least one Words of Worship '
'file to import from.'))
self.wordsOfWorshipAddButton.setFocus()
return False
elif source_format == SongFormat.ccli:
if self.ccliFileListWidget.count() == 0:
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.ImportWizardForm',
'No CCLI Files Selected'),
translate('SongsPlugin.ImportWizardForm',
'You need to add at least one CCLI file '
2010-07-31 02:06:44 +00:00
'to import from.'))
self.ccliAddButton.setFocus()
return False
2010-08-20 19:40:07 +00:00
elif source_format == SongFormat.SongsOfFellowship:
if self.songsOfFellowshipFilenameEdit.text().isEmpty():
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.ImportWizardForm',
2010-08-20 19:40:07 +00:00
'No Songs of Fellowship File Selected'),
translate('SongsPlugin.ImportWizardForm',
2010-08-20 19:40:07 +00:00
'You need to select a Songs of Fellowship file to '
'import from.'))
self.songsOfFellowshipBrowseButton.setFocus()
return False
elif source_format == SongFormat.Generic:
if self.genericFilenameEdit.text().isEmpty():
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.ImportWizardForm',
'No Document/Presentation Selected'),
translate('SongsPlugin.ImportWizardForm',
'You need to select a document/presentation file '
'to import from.'))
self.genericBrowseButton.setFocus()
return False
return True
elif self.currentId() == 2:
# Progress page
return True
2010-04-02 12:23:40 +00:00
2010-08-20 19:40:07 +00:00
def getFileName(self, title, editbox):
filename = QtGui.QFileDialog.getOpenFileName(self, title,
SettingsManager.get_last_dir(self.plugin.settingsSection, 1))
if filename:
editbox.setText(filename)
SettingsManager.set_last_dir(
self.plugin.settingsSection,
os.path.split(unicode(filename))[0], 1)
2010-08-08 14:38:51 +00:00
def getFiles(self, title, listbox):
filenames = QtGui.QFileDialog.getOpenFileNames(self, title,
SettingsManager.get_last_dir(self.plugin.settingsSection, 1))
if filenames:
listbox.addItems(filenames)
2010-08-20 19:40:07 +00:00
SettingsManager.set_last_dir(
self.plugin.settingsSection,
os.path.split(unicode(filenames[0]))[0], 1)
2010-08-08 14:38:51 +00:00
def getListOfFiles(self, listbox):
files = []
for row in range(0, listbox.count()):
files.append(unicode(listbox.item(row)))
return files
2010-08-08 14:38:51 +00:00
def removeSelectedItems(self, listbox):
for item in listbox.selectedItems():
item = listbox.takeItem(listbox.row(item))
del item
2010-08-20 19:40:07 +00:00
def onOpenLP2BrowseButtonClicked(self):
self.getFileName(
translate('SongsPlugin.ImportWizardForm',
'Select OpenLP 2.0 Database File'),
self.openLP2FilenameEdit
)
def onOpenLP1BrowseButtonClicked(self):
self.getFileName(
translate('SongsPlugin.ImportWizardForm',
'Select openlp.org 1.x Database File'),
self.openLP1FilenameEdit
)
2010-08-08 14:38:51 +00:00
def onOpenLyricsAddButtonClicked(self):
self.getFiles(
translate('SongsPlugin.ImportWizardForm',
'Select OpenLyrics Files'),
2010-08-20 19:40:07 +00:00
self.openLyricsFileListWidget
2010-08-08 14:38:51 +00:00
)
def onOpenLyricsRemoveButtonClicked(self):
2010-08-20 19:40:07 +00:00
self.removeSelectedItems(self.openLyricsFileListWidget)
def onOpenSongAddButtonClicked(self):
self.getFiles(
translate('SongsPlugin.ImportWizardForm',
'Select Open Song Files'),
2010-08-20 19:40:07 +00:00
self.openSongFileListWidget
)
def onOpenSongRemoveButtonClicked(self):
self.removeSelectedItems(self.openSongFileListWidget)
def onWordsOfWorshipAddButtonClicked(self):
self.getFiles(
translate('SongsPlugin.ImportWizardForm',
'Select Words of Worship Files'),
2010-08-20 19:40:07 +00:00
self.wordsOfWorshipFileListWidget
)
def onWordsOfWorshipRemoveButtonClicked(self):
self.removeSelectedItems(self.wordsOfWorshipFileListWidget)
def onSongsOfFellowshipBrowseButtonClicked(self):
self.getFileName(
translate('SongsPlugin.ImportWizardForm',
'Select Songs of Fellowship File'),
self.songsOfFellowshipFilenameEdit
)
def onGenericBrowseButtonClicked(self):
self.getFileName(
translate('SongsPlugin.ImportWizardForm',
'Select Document/Presentation File'),
self.genericFilenameEdit
)
2010-08-08 14:38:51 +00:00
2010-04-02 12:23:40 +00:00
def onCancelButtonClicked(self, checked):
"""
Stop the import on pressing the cancel button.
"""
log.debug('Cancel button pressed!')
if self.currentId() == 3:
Receiver.send_message(u'openlp_stop_song_import')
2010-04-02 12:23:40 +00:00
def onCurrentIdChanged(self, id):
if id == 2:
self.preImport()
self.performImport()
self.postImport()
2010-04-02 20:03:51 +00:00
def registerFields(self):
pass
2010-04-02 20:03:51 +00:00
def setDefaults(self):
self.formatComboBox.setCurrentIndex(0)
2010-08-20 19:40:07 +00:00
self.openLP2FilenameEdit.setText(u'')
self.openLP1FilenameEdit.setText(u'')
self.openLyricsFileListWidget.clear()
self.openSongFileListWidget.clear()
self.wordsOfWorshipFileListWidget.clear()
self.ccliFileListWidget.clear()
2010-08-20 19:40:07 +00:00
self.songsOfFellowshipFilenameEdit.setText(u'')
self.genericFilenameEdit.setText(u'')
#self.csvFilenameEdit.setText(u'')
2010-04-02 12:23:40 +00:00
def incrementProgressBar(self, status_text):
log.debug(u'IncrementBar %s', status_text)
self.importProgressLabel.setText(status_text)
self.importProgressBar.setValue(self.importProgressBar.value() + 1)
2010-04-02 12:23:40 +00:00
Receiver.send_message(u'process_events')
def preImport(self):
self.finishButton.setVisible(False)
self.importProgressBar.setMinimum(0)
self.importProgressBar.setMaximum(1188)
self.importProgressBar.setValue(0)
self.importProgressLabel.setText(
translate('SongsPlugin.ImportWizardForm', 'Starting import...'))
2010-04-02 12:23:40 +00:00
Receiver.send_message(u'process_events')
def performImport(self):
"""
Perform the actual import. This method pulls in the correct importer
class, and then runs the ``do_import`` method of the importer to do
the actual importing.
"""
source_format = self.formatComboBox.currentIndex()
importer = None
if source_format == SongFormat.OpenLP2:
# Import an OpenLP 2.0 database
importer = self.plugin.importSongs(SongFormat.OpenLP2,
filename=unicode(self.openLP2FilenameEdit.text())
)
#elif source_format == SongFormat.OpenLP1:
# # Import an openlp.org database
# importer = self.plugin.importSongs(SongFormat.OpenLP1,
# filename=unicode(self.field(u'openlp1_filename').toString())
# )
elif source_format == SongFormat.OpenLyrics:
# Import OpenLyrics songs
importer = self.plugin.importSongs(SongFormat.OpenLyrics,
filenames=self.getListOfFiles(self.openLyricsFileListWidget)
)
elif source_format == SongFormat.OpenSong:
# Import OpenSong songs
importer = self.plugin.importSongs(SongFormat.OpenSong,
filenames=self.getListOfFiles(self.openSongFileListWidget)
)
elif source_format == SongFormat.WordsOfWorship:
# Import Words Of Worship songs
importer = self.plugin.importSongs(SongFormat.WordsOfWorship,
filenames=self.getListOfFiles(self.wordsOfWorshipFileListWidget)
)
elif source_format == SongFormat.CCLI:
# Import Words Of Worship songs
importer = self.plugin.importSongs(SongFormat.CCLI,
filenames=self.getListOfFiles(self.ccliFileListWidget)
)
elif source_format == SongFormat.SongsOfFellowship:
# Import a Songs of Fellowship RTF file
importer = self.plugin.importSongs(SongFormat.SongsOfFellowship,
filename=unicode(self.songsOfFellowshipFilenameEdit.text())
)
success = importer.do_import()
if success:
# reload songs
self.ImportProgressLabel.setText(
translate('SongsPlugin.SongImportForm', 'Finished import.'))
else:
self.ImportProgressLabel.setText(
translate('SongsPlugin.SongImportForm',
'Your song import failed.'))
2010-04-02 12:23:40 +00:00
def postImport(self):
self.importProgressBar.setValue(self.importProgressBar.maximum())
2010-04-02 12:23:40 +00:00
self.finishButton.setVisible(True)
self.cancelButton.setVisible(False)
2010-07-27 09:32:52 +00:00
Receiver.send_message(u'process_events')