From 0c83471ee46af92b8ca1bea64d158b92e333fb11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armin=20K=C3=B6hler?= Date: Wed, 25 May 2011 16:18:03 +0200 Subject: [PATCH] move some code stop moving bibles to subfolders some fixes --- openlp.pyw | 16 +-- openlp/core/ui/mainwindow.py | 9 ++ openlp/plugins/bibles/bibleplugin.py | 16 ++- .../plugins/bibles/forms/bibleimportform.py | 4 +- .../plugins/bibles/forms/bibleupgradeform.py | 105 +++++++++--------- openlp/plugins/bibles/lib/db.py | 29 +++-- openlp/plugins/bibles/lib/manager.py | 11 +- 7 files changed, 110 insertions(+), 80 deletions(-) diff --git a/openlp.pyw b/openlp.pyw index 34980b1c6..6cf3dd2da 100755 --- a/openlp.pyw +++ b/openlp.pyw @@ -37,8 +37,7 @@ from traceback import format_exception from PyQt4 import QtCore, QtGui -from openlp.core.lib import Receiver, check_directory_exists, SettingsManager, \ - translate +from openlp.core.lib import Receiver, check_directory_exists from openlp.core.lib.ui import UiStrings from openlp.core.resources import qInitResources from openlp.core.ui.mainwindow import MainWindow @@ -130,18 +129,7 @@ class OpenLP(QtGui.QApplication): u'general/update check', QtCore.QVariant(True)).toBool() if update_check: VersionThread(self.mainWindow).start() - for plugin in self.mainWindow.pluginManager.plugins: - if plugin.name == u'Bibles' and plugin.isActive() and not \ - len(SettingsManager.get_files( u'bibles/bibles', u'.sqlite')) \ - and SettingsManager.get_files(u'bibles', u'.sqlite'): - if QtGui.QMessageBox.information(self.mainWindow, - translate('OpenLP', 'Information'), translate('OpenLP', - 'Bible format has changed.\nYou have to upgrade your ' - 'existing bibles.\nShould OpenLP upgrade now?'), - QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes | - QtGui.QMessageBox.No)) == QtGui.QMessageBox.Yes: - plugin.onToolsUpgradeItemTriggered() - break + self.mainWindow.appStartup() DelayStartThread(self.mainWindow).start() return self.exec_() diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index 09e2ec9e7..8535670fa 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -639,6 +639,15 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow): self.setViewMode(False, True, False, False, True) self.ModeLiveItem.setChecked(True) + def appStartup(self): + # Give all the plugins a chance to perform some tasks at startup + Receiver.send_message(u'openlp_process_events') + for plugin in self.pluginManager.plugins: + if hasattr(plugin, u'appStartup'): + Receiver.send_message(u'openlp_process_events') + plugin.appStartup() + Receiver.send_message(u'openlp_process_events') + def firstTime(self): # Import themes if first time Receiver.send_message(u'openlp_process_events') diff --git a/openlp/plugins/bibles/bibleplugin.py b/openlp/plugins/bibles/bibleplugin.py index fd56d9d32..c326d6c00 100644 --- a/openlp/plugins/bibles/bibleplugin.py +++ b/openlp/plugins/bibles/bibleplugin.py @@ -60,7 +60,7 @@ class BiblePlugin(Plugin): #action_list.add_action(self.exportBibleItem, UiStrings().Export) # Set to invisible until we can export bibles self.exportBibleItem.setVisible(False) - if SettingsManager.get_files(u'bibles', u'.sqlite'): + if len(self.manager.old_bible_databases): self.toolsUpgradeItem.setVisible(True) def finalise(self): @@ -76,6 +76,20 @@ class BiblePlugin(Plugin): #action_list.remove_action(self.exportBibleItem, UiStrings().Export) self.exportBibleItem.setVisible(False) + def appStartup(self): + """ + Perform tasks on application starup + """ + if not len(self.manager.db_cache) and \ + len(self.manager.old_bible_databases): + if QtGui.QMessageBox.information(self.formparent, + translate('OpenLP', 'Information'), translate('OpenLP', + 'Bible format has changed.\nYou have to upgrade your ' + 'existing bibles.\nShould OpenLP upgrade now?'), + QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes | + QtGui.QMessageBox.No)) == QtGui.QMessageBox.Yes: + self.onToolsUpgradeItemTriggered() + def addImportMenuItem(self, import_menu): self.importBibleItem = base_action(import_menu, u'importBibleItem') self.importBibleItem.setText(translate('BiblesPlugin', '&Bible')) diff --git a/openlp/plugins/bibles/forms/bibleimportform.py b/openlp/plugins/bibles/forms/bibleimportform.py index 8e9073775..1df6946dd 100644 --- a/openlp/plugins/bibles/forms/bibleimportform.py +++ b/openlp/plugins/bibles/forms/bibleimportform.py @@ -472,7 +472,7 @@ class BibleImportForm(OpenLPWizard): license_version = unicode(self.field(u'license_version').toString()) license_copyright = \ unicode(self.field(u'license_copyright').toString()) - path = AppLocation.get_section_data_path(u'bibles/bibles') + path = AppLocation.get_section_data_path(u'bibles') if not license_version: critical_error_message_box(UiStrings().EmptyField, translate('BiblesPlugin.ImportWizardForm', @@ -720,5 +720,5 @@ class BibleImportForm(OpenLPWizard): self.progressLabel.setText(translate( 'BiblesPlugin.ImportWizardForm', 'Your Bible import failed.')) del self.manager.db_cache[importer.name] - delete_database(AppLocation.get_section_data_path(u'bibles/bibles'), + delete_database(AppLocation.get_section_data_path(u'bibles'), importer.file) diff --git a/openlp/plugins/bibles/forms/bibleupgradeform.py b/openlp/plugins/bibles/forms/bibleupgradeform.py index da75d9553..4a8ad488d 100644 --- a/openlp/plugins/bibles/forms/bibleupgradeform.py +++ b/openlp/plugins/bibles/forms/bibleupgradeform.py @@ -67,18 +67,12 @@ class BibleUpgradeForm(OpenLPWizard): self.manager = manager self.mediaItem = bibleplugin.mediaItem self.suffix = u'.sqlite' - self.settingsSection = u'bibles/bibles' - self.oldsettingsSection = u'bibles' - self.oldpath = AppLocation.get_section_data_path( - self.oldsettingsSection) - self.newpath = AppLocation.get_section_data_path( + self.settingsSection = u'bibles' + self.path = AppLocation.get_section_data_path( self.settingsSection) - self.files = SettingsManager.get_files(self.oldsettingsSection, - self.suffix) + self.files = self.manager.old_bible_databases self.success = {} self.newbibles = {} - self.maxBibles = len(self.files) - self.stop_import_flag = False OpenLPWizard.__init__(self, parent, bibleplugin, u'bibleUpgradeWizard', u':/wizards/wizard_importbible.bmp') @@ -106,7 +100,8 @@ class BibleUpgradeForm(OpenLPWizard): self.verticalWidget[number].hide() self.formWidget[number].hide() else: - if os.path.exists(os.path.join(self.newpath, filename)): + version_name = unicode(self.versionNameEdit[number].text()) + if self.manager.exists(version_name): self.verticalWidget[number].show() self.formWidget[number].show() @@ -118,7 +113,7 @@ class BibleUpgradeForm(OpenLPWizard): if self.currentPage() == self.progressPage: Receiver.send_message(u'openlp_stop_wizard') for bible in self.newbibles.itervalues(): - delete_database(self.newpath, clean_filename( + delete_database(self.path, clean_filename( bible.get_name())) self.done(QtGui.QDialog.Rejected) @@ -139,7 +134,7 @@ class BibleUpgradeForm(OpenLPWizard): """ for number, filename in enumerate(self.files): if self.success[number]: - delete_file(os.path.join(self.oldpath, filename)) + delete_file(os.path.join(self.path, filename)) def customInit(self): """ @@ -152,10 +147,6 @@ class BibleUpgradeForm(OpenLPWizard): """ Set up the signals used in the bible importer. """ - for number, filename in enumerate(self.files): - QtCore.QObject.connect(self.checkBox[number], - QtCore.SIGNAL(u'stateChanged(int)'), - self.onCheckBoxIndexChanged) QtCore.QObject.connect(self.finishButton, QtCore.SIGNAL(u'clicked()'), self.onFinishButton) @@ -195,24 +186,24 @@ class BibleUpgradeForm(OpenLPWizard): self.formWidget = {} self.formLayoutAttention = {} for number, filename in enumerate(self.files): - bible = OldBibleDB(self.mediaItem, path=self.oldpath, file=filename) + bible = OldBibleDB(self.mediaItem, path=self.path, file=filename) self.checkBox[number] = QtGui.QCheckBox(self.scrollAreaContents) - checkBoxName = u'checkBox['+unicode(number)+u']' + checkBoxName = u'checkBox[%d]' % number self.checkBox[number].setObjectName(checkBoxName) self.checkBox[number].setText(bible.get_name()) self.checkBox[number].setCheckState(QtCore.Qt.Checked) self.formLayout.addWidget(self.checkBox[number]) self.verticalWidget[number] = QtGui.QWidget(self.scrollAreaContents) - verticalWidgetName = u'verticalWidget['+unicode(number)+u']' + verticalWidgetName = u'verticalWidget[%d]' % number self.verticalWidget[number].setObjectName(verticalWidgetName) self.horizontalLayout[number] = QtGui.QHBoxLayout( self.verticalWidget[number]) self.horizontalLayout[number].setContentsMargins(25, 0, 0, 0) - horizontalLayoutName = u'horizontalLayout['+unicode(number)+u']' + horizontalLayoutName = u'horizontalLayout[%d]' % number self.horizontalLayout[number].setObjectName(horizontalLayoutName) self.versionInfoPixmap[number] = QtGui.QLabel( self.verticalWidget[number]) - versionInfoPixmapName = u'versionInfoPixmap['+unicode(number)+u']' + versionInfoPixmapName = u'versionInfoPixmap[%d]' % number self.versionInfoPixmap[number].setObjectName(versionInfoPixmapName) self.versionInfoPixmap[number].setPixmap(QtGui.QPixmap( u':/bibles/bibles_upgrade_alert.png')) @@ -221,7 +212,7 @@ class BibleUpgradeForm(OpenLPWizard): self.versionInfoPixmap[number]) self.versionInfoLabel[number] = QtGui.QLabel( self.verticalWidget[number]) - versionInfoLabelName = u'versionInfoLabel['+unicode(number)+u']' + versionInfoLabelName = u'versionInfoLabel[%d]' % number self.versionInfoLabel[number].setObjectName(versionInfoLabelName) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred) @@ -234,13 +225,12 @@ class BibleUpgradeForm(OpenLPWizard): self.versionInfoLabel[number]) self.formLayout.addWidget(self.verticalWidget[number]) self.formWidget[number] = QtGui.QWidget(self.scrollAreaContents) - formWidgetName = u'formWidget['+unicode(number)+u']' + formWidgetName = u'formWidget[%d]' % number self.formWidget[number].setObjectName(formWidgetName) self.formLayoutAttention[number] = QtGui.QFormLayout( self.formWidget[number]) self.formLayoutAttention[number].setContentsMargins(25, 0, 0, 5) - formLayoutAttentionName = u'formLayoutAttention['+unicode(number)+\ - u']' + formLayoutAttentionName = u'formLayoutAttention[%d]' % number self.formLayoutAttention[number].setObjectName( formLayoutAttentionName) self.versionNameLabel[number] = QtGui.QLabel( @@ -255,6 +245,10 @@ class BibleUpgradeForm(OpenLPWizard): QtGui.QFormLayout.FieldRole, self.versionNameEdit[number]) self.versionNameEdit[number].setText(bible.get_name()) self.formLayout.addWidget(self.formWidget[number]) + #Set up the Signal for the checkbox + QtCore.QObject.connect(self.checkBox[number], + QtCore.SIGNAL(u'stateChanged(int)'), + self.onCheckBoxIndexChanged) self.spacerItem = QtGui.QSpacerItem(20, 5, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) self.formLayout.addItem(self.spacerItem) @@ -329,7 +323,7 @@ class BibleUpgradeForm(OpenLPWizard): if not self.checkBox[number].checkState() == QtCore.Qt.Checked: continue version_name = unicode(self.versionNameEdit[number].text()) - oldbible = OldBibleDB(self.mediaItem, path=self.oldpath, + oldbible = OldBibleDB(self.mediaItem, path=self.path, file=filename) oldname = oldbible.get_name() if not version_name: @@ -348,18 +342,28 @@ class BibleUpgradeForm(OpenLPWizard): 'uncheck.')) self.versionNameEdit[number].setFocus() return False - elif os.path.exists(os.path.join(self.newpath, filename)) and \ - version_name == oldname: - critical_error_message_box( - translate('BiblesPlugin.UpgradeWizardForm', + elif os.path.exists(os.path.join(self.path, clean_filename( + version_name))) and version_name == oldname: + newfilename = u'old_database_%s' % filename + if not os.path.exists(os.path.join(self.path, + newfilename)): + os.rename(os.path.join(self.path, filename), + os.path.join(self.path, newfilename)) + self.files[number] = newfilename + continue + else: + critical_error_message_box( + translate('BiblesPlugin.UpgradeWizardForm', 'Bible Exists'), - translate('BiblesPlugin.UpgradeWizardForm', - 'This Bible already exists. Please upgrade ' - 'a different Bible, delete the existing one or ' - 'uncheck.')) - self.versionNameEdit[number].setFocus() - return False - elif os.path.exists(os.path.join(self.newpath, + translate('BiblesPlugin.UpgradeWizardForm', + 'This Bible already exists. Please upgrade ' + 'a different Bible, delete the existing one or ' + 'uncheck.')) + self.verticalWidget[number].show() + self.formWidget[number].show() + self.versionNameEdit[number].setFocus() + return False + elif os.path.exists(os.path.join(self.path, clean_filename(version_name))): critical_error_message_box( translate('BiblesPlugin.UpgradeWizardForm', @@ -385,15 +389,16 @@ class BibleUpgradeForm(OpenLPWizard): self.success.clear() self.newbibles.clear() self.clearScrollArea() - self.files = SettingsManager.get_files(self.oldsettingsSection, - self.suffix) + self.files = self.manager.old_bible_databases self.addScrollArea() - self.customSignals() self.retranslateUi() self.maxBibles = len(self.files) for number, filename in enumerate(self.files): self.checkBox[number].setCheckState(QtCore.Qt.Checked) - if os.path.exists(os.path.join(self.newpath, filename)): + oldbible = OldBibleDB(self.mediaItem, path=self.path, + file=filename) + oldname = oldbible.get_name() + if self.manager.exists(oldname): self.verticalWidget[number].show() self.formWidget[number].show() else: @@ -440,11 +445,11 @@ class BibleUpgradeForm(OpenLPWizard): if not self.checkBox[biblenumber].checkState() == QtCore.Qt.Checked: continue self.progressBar.reset() - oldbible = OldBibleDB(self.mediaItem, path=self.oldpath, + oldbible = OldBibleDB(self.mediaItem, path=self.path, file=filename) name = oldbible.get_name() if name is None: - delete_file(os.path.join(self.oldpath, filename)) + delete_file(os.path.join(self.path, filename)) self.incrementProgressBar(unicode(translate( 'BiblesPlugin.UpgradeWizardForm', 'Upgrading Bible %s of %s: "%s"\nFailed')) % @@ -456,9 +461,9 @@ class BibleUpgradeForm(OpenLPWizard): 'BiblesPlugin.UpgradeWizardForm', 'Upgrading Bible %s of %s: "%s"\nImporting ...')) % (number + 1, self.maxBibles, name)) - if os.path.exists(os.path.join(self.newpath, filename)): + if os.path.exists(os.path.join(self.path, filename)): name = unicode(self.versionNameEdit[biblenumber].text()) - self.newbibles[number] = BibleDB(self.mediaItem, path=self.oldpath, + self.newbibles[number] = BibleDB(self.mediaItem, path=self.path, name=name) metadata = oldbible.get_metadata() webbible = False @@ -488,7 +493,7 @@ class BibleUpgradeForm(OpenLPWizard): u'name: "%s" failed' % ( meta_data[u'download source'], meta_data[u'download name'])) - delete_database(self.newpath, + delete_database(self.path, clean_filename(self.newbibles[number].get_name())) del self.newbibles[number] critical_error_message_box( @@ -517,7 +522,7 @@ class BibleUpgradeForm(OpenLPWizard): if not language_id: log.exception(u'Upgrading from "%s" '\ 'failed' % filename) - delete_database(self.newpath, + delete_database(self.path, clean_filename(self.newbibles[number].get_name())) del self.newbibles[number] self.incrementProgressBar(unicode(translate( @@ -543,7 +548,7 @@ class BibleUpgradeForm(OpenLPWizard): u'name: "%s" aborted by user' % ( meta_data[u'download source'], meta_data[u'download name'])) - delete_database(self.newpath, + delete_database(self.path, clean_filename(self.newbibles[number].get_name())) del self.newbibles[number] bible_failed = True @@ -559,7 +564,7 @@ class BibleUpgradeForm(OpenLPWizard): if not language_id: log.exception(u'Importing books from "%s" '\ 'failed' % name) - delete_database(self.newpath, + delete_database(self.path, clean_filename(self.newbibles[number].get_name())) del self.newbibles[number] self.incrementProgressBar(unicode(translate( @@ -585,7 +590,7 @@ class BibleUpgradeForm(OpenLPWizard): if not book_ref_id: log.exception(u'Importing books from %s " '\ 'failed - aborted by user' % name) - delete_database(self.newpath, + delete_database(self.path, clean_filename(self.newbibles[number].get_name())) del self.newbibles[number] bible_failed = True diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index 0bbaaaf1b..9ece16500 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -62,18 +62,18 @@ class Verse(BaseModel): """ pass -def clean_filename(old_filename): +def clean_filename(filename): """ Clean up the version name of the Bible and convert it into a valid file name. - ``old_filename`` + ``filename`` The "dirty" file name or version name. """ - if not isinstance(old_filename, unicode): - old_filename = unicode(old_filename, u'utf-8') - old_filename = re.sub(r'[^\w]+', u'_', old_filename).strip(u'_') - return old_filename + u'.sqlite' + if not isinstance(filename, unicode): + filename = unicode(filename, u'utf-8') + filename = re.sub(r'[^\w]+', u'_', filename).strip(u'_') + return filename + u'.sqlite' def init_schema(url): """ @@ -160,7 +160,7 @@ class BibleDB(QtCore.QObject, Manager): self.file = clean_filename(self.name) if u'file' in kwargs: self.file = kwargs[u'file'] - Manager.__init__(self, u'bibles/bibles', init_schema, self.file) + Manager.__init__(self, u'bibles', init_schema, self.file) if u'file' in kwargs: self.get_name() if u'path' in kwargs: @@ -487,6 +487,16 @@ class BibleDB(QtCore.QObject, Manager): self.create_meta(u'language_id', language_id) return language_id + def find_old_database(self): + """ + Returns true if it is an old bible database. + """ + try: + columns = self.session.query(Book).all() + except: + return True + return False + def dump_bible(self): """ Utility debugging method to dump the contents of a bible. @@ -863,12 +873,9 @@ class AlternativeBookNamesDB(QtCore.QObject, Manager): database doesn't exist. """ if AlternativeBookNamesDB.cursor is None: - check_directory_exists(os.path.join(AppLocation.get_directory( - AppLocation.DataDir), u'bibles', u'resources')) filepath = os.path.join( AppLocation.get_directory(AppLocation.DataDir), u'bibles', - u'resources', u'alternative_book_names.sqlite') - log.debug(u'Filepath: %s' % filepath) + u'alternative_book_names.sqlite') if not os.path.exists(filepath): #create new DB, create table alternative_book_names AlternativeBookNamesDB.conn = sqlite3.connect(filepath) diff --git a/openlp/plugins/bibles/lib/manager.py b/openlp/plugins/bibles/lib/manager.py index ee5491878..12490ba24 100644 --- a/openlp/plugins/bibles/lib/manager.py +++ b/openlp/plugins/bibles/lib/manager.py @@ -33,7 +33,7 @@ from openlp.core.lib import Receiver, SettingsManager, translate from openlp.core.lib.ui import critical_error_message_box from openlp.core.utils import AppLocation, delete_file from openlp.plugins.bibles.lib import parse_reference -from openlp.plugins.bibles.lib.db import BibleDB, BibleMeta +from openlp.plugins.bibles.lib.db import BibleDB, BibleMeta, OldBibleDB from csvbible import CSVBible from http import HTTPBible from opensong import OpenSongBible @@ -120,7 +120,7 @@ class BibleManager(object): """ log.debug(u'Bible Initialising') self.parent = parent - self.settingsSection = u'bibles/bibles' + self.settingsSection = u'bibles' self.web = u'Web' self.db_cache = None self.path = AppLocation.get_section_data_path(self.settingsSection) @@ -140,8 +140,11 @@ class BibleManager(object): """ log.debug(u'Reload bibles') files = SettingsManager.get_files(self.settingsSection, self.suffix) + if u'alternative_book_names.sqlite' in files: + files.remove(u'alternative_book_names.sqlite') log.debug(u'Bible Files %s', files) self.db_cache = {} + self.old_bible_databases = [] for filename in files: bible = BibleDB(self.parent, path=self.path, file=filename) name = bible.get_name() @@ -149,6 +152,10 @@ class BibleManager(object): if name is None: delete_file(os.path.join(self.path, filename)) continue + # Find old database versions + if bible.find_old_database(): + self.old_bible_databases.append(filename) + continue log.debug(u'Bible Name: "%s"', name) self.db_cache[name] = bible # Look to see if lazy load bible exists and get create getter.