Fixed various issues around the exporting and importing of settings.

bzr-revno: 1740
Fixes: https://launchpad.net/bugs/838831
This commit is contained in:
Stevan Pettit 2011-09-05 19:21:12 +02:00 committed by Raoul Snyman
commit 796e779300
1 changed files with 71 additions and 68 deletions

View File

@ -540,6 +540,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
self.uiSettingsSection = u'user interface' self.uiSettingsSection = u'user interface'
self.generalSettingsSection = u'general' self.generalSettingsSection = u'general'
self.advancedlSettingsSection = u'advanced' self.advancedlSettingsSection = u'advanced'
self.shortcutsSettingsSection = u'shortcuts'
self.servicemanagerSettingsSection = u'servicemanager' self.servicemanagerSettingsSection = u'servicemanager'
self.songsSettingsSection = u'songs' self.songsSettingsSection = u'songs'
self.themesSettingsSection = u'themes' self.themesSettingsSection = u'themes'
@ -913,42 +914,43 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
QtGui.QMessageBox.No) QtGui.QMessageBox.No)
if answer == QtGui.QMessageBox.No: if answer == QtGui.QMessageBox.No:
return return
importFileName = unicode(QtGui.QFileDialog.getOpenFileName(self, import_file_name = unicode(QtGui.QFileDialog.getOpenFileName(self,
translate('OpenLP.MainWindow', 'Open File'), translate('OpenLP.MainWindow', 'Open File'),
'', '',
translate('OpenLP.MainWindow', translate('OpenLP.MainWindow',
'OpenLP Export Settings Files (*.conf)'))) 'OpenLP Export Settings Files (*.conf)')))
if not importFileName: if not import_file_name:
return return
settingSections = [] setting_sections = []
# Add main sections. # Add main sections.
settingSections.extend([self.generalSettingsSection]) setting_sections.extend([self.generalSettingsSection])
settingSections.extend([self.advancedlSettingsSection]) setting_sections.extend([self.advancedlSettingsSection])
settingSections.extend([self.uiSettingsSection]) setting_sections.extend([self.uiSettingsSection])
settingSections.extend([self.servicemanagerSettingsSection]) setting_sections.extend([self.shortcutsSettingsSection])
settingSections.extend([self.themesSettingsSection]) setting_sections.extend([self.servicemanagerSettingsSection])
settingSections.extend([self.displayTagsSection]) setting_sections.extend([self.themesSettingsSection])
settingSections.extend([self.headerSection]) setting_sections.extend([self.displayTagsSection])
setting_sections.extend([self.headerSection])
# Add plugin sections. # Add plugin sections.
for plugin in self.pluginManager.plugins: for plugin in self.pluginManager.plugins:
settingSections.extend([plugin.name]) setting_sections.extend([plugin.name])
settings = QtCore.QSettings() settings = QtCore.QSettings()
importSettings = QtCore.QSettings(importFileName, import_settings = QtCore.QSettings(import_file_name,
QtCore.QSettings.IniFormat) QtCore.QSettings.IniFormat)
importKeys = importSettings.allKeys() import_keys = import_settings.allKeys()
for sectionKey in importKeys: for section_key in import_keys:
# We need to handle the really bad files. # We need to handle the really bad files.
try: try:
section, key = sectionKey.split(u'/') section, key = section_key.split(u'/')
except ValueError: except ValueError:
section = u'unknown' section = u'unknown'
key = u'' key = u''
# Switch General back to lowercase. # Switch General back to lowercase.
if section == u'General': if section == u'General':
section = u'general' section = u'general'
sectionKey = section + "/" + key section_key = section + "/" + key
# Make sure it's a valid section for us. # Make sure it's a valid section for us.
if not section in settingSections: if not section in setting_sections:
QtGui.QMessageBox.critical(self, QtGui.QMessageBox.critical(self,
translate('OpenLP.MainWindow', 'Import settings'), translate('OpenLP.MainWindow', 'Import settings'),
translate('OpenLP.MainWindow', translate('OpenLP.MainWindow',
@ -961,13 +963,13 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
QtGui.QMessageBox.Ok)) QtGui.QMessageBox.Ok))
return return
# We have a good file, import it. # We have a good file, import it.
for sectionKey in importKeys: for section_key in import_keys:
value = importSettings.value(sectionKey) value = import_settings.value(section_key)
settings.setValue(u'%s' % (sectionKey) , settings.setValue(u'%s' % (section_key) ,
QtCore.QVariant(value)) QtCore.QVariant(value))
now = datetime.now() now = datetime.now()
settings.beginGroup(self.headerSection) settings.beginGroup(self.headerSection)
settings.setValue( u'file_imported' , QtCore.QVariant(importFileName)) settings.setValue( u'file_imported' , QtCore.QVariant(import_file_name))
settings.setValue(u'file_date_imported', settings.setValue(u'file_date_imported',
now.strftime("%Y-%m-%d %H:%M")) now.strftime("%Y-%m-%d %H:%M"))
settings.endGroup() settings.endGroup()
@ -986,78 +988,79 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
self.cleanUp() self.cleanUp()
QtCore.QCoreApplication.exit() QtCore.QCoreApplication.exit()
def onSettingsExportItemClicked(self, exportFileName=None): def onSettingsExportItemClicked(self):
""" """
Export settings to an INI file Export settings to a .conf file in INI format
""" """
if not exportFileName: export_file_name = unicode(QtGui.QFileDialog.getSaveFileName(self,
exportFileName = unicode(QtGui.QFileDialog.getSaveFileName(self, translate('OpenLP.MainWindow', 'Export Settings File'), '',
translate('OpenLP.MainWindow', 'Export Settings File'), '', translate('OpenLP.MainWindow',
translate('OpenLP.MainWindow', 'OpenLP Export Settings File (*.conf)')))
'OpenLP Export Settings File (*.conf)'))) if not export_file_name:
if not exportFileName:
return return
# Make sure it's an .ini file. # Make sure it's a .conf file.
if not exportFileName.endswith(u'conf'): if not export_file_name.endswith(u'conf'):
exportFileName = exportFileName + u'.conf' export_file_name = export_file_name + u'.conf'
temp_file = os.path.join(unicode(gettempdir()), temp_file = os.path.join(unicode(gettempdir()),
u'openlp', u'exportIni.tmp') u'openlp', u'exportConf.tmp')
self.saveSettings() self.saveSettings()
settingSections = [] setting_sections = []
# Add main sections. # Add main sections.
settingSections.extend([self.generalSettingsSection]) setting_sections.extend([self.generalSettingsSection])
settingSections.extend([self.advancedlSettingsSection]) setting_sections.extend([self.advancedlSettingsSection])
settingSections.extend([self.uiSettingsSection]) setting_sections.extend([self.uiSettingsSection])
settingSections.extend([self.servicemanagerSettingsSection]) setting_sections.extend([self.shortcutsSettingsSection])
settingSections.extend([self.themesSettingsSection]) setting_sections.extend([self.servicemanagerSettingsSection])
settingSections.extend([self.displayTagsSection]) setting_sections.extend([self.themesSettingsSection])
setting_sections.extend([self.displayTagsSection])
# Add plugin sections. # Add plugin sections.
for plugin in self.pluginManager.plugins: for plugin in self.pluginManager.plugins:
settingSections.extend([plugin.name]) setting_sections.extend([plugin.name])
# Delete old files if found. # Delete old files if found.
if os.path.exists(temp_file): if os.path.exists(temp_file):
os.remove(temp_file) os.remove(temp_file)
if os.path.exists(exportFileName): if os.path.exists(export_file_name):
os.remove(exportFileName) os.remove(export_file_name)
settings = QtCore.QSettings() settings = QtCore.QSettings()
settings.remove(self.headerSection) settings.remove(self.headerSection)
# Get the settings. # Get the settings.
keys = settings.allKeys() keys = settings.allKeys()
exportSettings = QtCore.QSettings(temp_file, export_settings = QtCore.QSettings(temp_file,
QtCore.QSettings.IniFormat) QtCore.QSettings.IniFormat)
# Add a header section. # Add a header section.
# This is to insure it's our ini file for import. # This is to insure it's our conf file for import.
now = datetime.now() now = datetime.now()
applicationVersion = get_application_version() application_version = get_application_version()
# Write INI format using Qsettings. # Write INI format using Qsettings.
# Write our header. # Write our header.
exportSettings.beginGroup(self.headerSection) export_settings.beginGroup(self.headerSection)
exportSettings.setValue(u'Make_Changes', u'At_Own_RISK') export_settings.setValue(u'Make_Changes', u'At_Own_RISK')
exportSettings.setValue(u'type', u'OpenLP_settings_export') export_settings.setValue(u'type', u'OpenLP_settings_export')
exportSettings.setValue(u'file_date_created', export_settings.setValue(u'file_date_created',
now.strftime("%Y-%m-%d %H:%M")) now.strftime("%Y-%m-%d %H:%M"))
exportSettings.setValue(u'version', applicationVersion[u'full']) export_settings.setValue(u'version', application_version[u'full'])
exportSettings.endGroup() export_settings.endGroup()
# Write all the sections and keys. # Write all the sections and keys.
for sectionKey in keys: for section_key in keys:
section, key = sectionKey.split(u'/') section, key = section_key.split(u'/')
keyValue = settings.value(sectionKey) key_value = settings.value(section_key)
sectionKey = section + u"/" + key
# Change the service section to servicemanager. # Change the service section to servicemanager.
if section == u'service': if section == u'service':
sectionKey = u'servicemanager/' + key section_key = u'servicemanager/' + key
exportSettings.setValue(sectionKey, keyValue) export_settings.setValue(section_key, key_value)
exportSettings.sync() export_settings.sync()
# Temp INI file has been written. Blanks in keys are now '%20'. # Temp CONF file has been written. Blanks in keys are now '%20'.
# Read the temp file and output the user's INI file with blanks to # Read the temp file and output the user's CONF file with blanks to
# make it more readable. # make it more readable.
tempIni = open(temp_file, u'r') temp_conf = open(temp_file, u'r')
exportIni = open(exportFileName, u'w') export_conf = open(export_file_name, u'w')
for fileRecord in tempIni: for file_record in temp_conf:
fileRecord = fileRecord.replace(u'%20', u' ') # Get rid of any invalid entries.
exportIni.write(fileRecord) if file_record.find(u'@Invalid()') == -1:
tempIni.close() file_record = file_record.replace(u'%20', u' ')
exportIni.close() export_conf.write(file_record)
temp_conf.close()
export_conf.close()
os.remove(temp_file) os.remove(temp_file)
return return