Adds a dialouge asking if the user wants to replace an existing theme when importing.

It works, but I dont know, it just feels like my code is a little bit "rough 'n' ready" let me know what you think!
This commit is contained in:
Philip Ridout 2012-03-09 21:43:27 +00:00
parent a2e40543ea
commit e9caa194f5

View File

@ -511,6 +511,20 @@ class ThemeManager(QtGui.QWidget):
return ThemeXML() return ThemeXML()
else: else:
return self._createThemeFromXml(xml, self.path) return self._createThemeFromXml(xml, self.path)
def overWriteMessageBox(self, themeName):
ret = QtGui.QMessageBox.question(self,
translate('OpenLP.ThemeManager', 'Theme Already Exists!'),
translate('OpenLP.ThemeManager',
'The theme %s already exists. Do you want to replace it?'
% themeName),
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Yes |
QtGui.QMessageBox.No),
QtGui.QMessageBox.No)
if ret == QtGui.QMessageBox.Yes:
return True
elif ret == QtGui.QMessageBox.No:
return False
def unzipTheme(self, filename, dir): def unzipTheme(self, filename, dir):
""" """
@ -522,7 +536,7 @@ class ThemeManager(QtGui.QWidget):
filename = unicode(filename) filename = unicode(filename)
zip = None zip = None
outfile = None outfile = None
filexml = None filexml = None
try: try:
zip = zipfile.ZipFile(filename) zip = zipfile.ZipFile(filename)
xmlfile = filter(lambda name: xmlfile = filter(lambda name:
@ -533,10 +547,17 @@ class ThemeManager(QtGui.QWidget):
xml_tree = ElementTree(element=XML(zip.read(xmlfile[0]))).getroot() xml_tree = ElementTree(element=XML(zip.read(xmlfile[0]))).getroot()
v1_background = xml_tree.find(u'BackgroundType') v1_background = xml_tree.find(u'BackgroundType')
if v1_background is not None: if v1_background is not None:
(themename, filexml, outfile) = self.unzipVersion122(dir, zip, (themename, filexml, outfile, abortimport) = self.unzipVersion122(dir, zip,
xmlfile[0], xml_tree, v1_background, outfile) xmlfile[0], xml_tree, v1_background, outfile)
else: else:
themename = xml_tree.find(u'name').text.strip() themename = xml_tree.find(u'name').text.strip()
themefolder = os.path.join(dir, themename)
themeexists = os.path.exists(themefolder)
if themeexists and not self.overWriteMessageBox(themename):
abortimport = True
return
else:
abortimport = False
for name in zip.namelist(): for name in zip.namelist():
try: try:
uname = unicode(name, u'utf-8') uname = unicode(name, u'utf-8')
@ -575,19 +596,20 @@ class ThemeManager(QtGui.QWidget):
zip.close() zip.close()
if outfile: if outfile:
outfile.close() outfile.close()
# As all files are closed, we can create the Theme. if not abortimport:
if filexml: # As all files are closed, we can create the Theme.
theme = self._createThemeFromXml(filexml, self.path) if filexml:
self.generateAndSaveImage(dir, themename, theme) theme = self._createThemeFromXml(filexml, self.path)
# Only show the error message, when IOError was not raised (in this self.generateAndSaveImage(dir, themename, theme)
# case the error message has already been shown). # Only show the error message, when IOError was not raised (in this
elif zip is not None: # case the error message has already been shown).
critical_error_message_box( elif zip is not None:
translate('OpenLP.ThemeManager', 'Validation Error'), critical_error_message_box(
translate('OpenLP.ThemeManager', translate('OpenLP.ThemeManager', 'Validation Error'),
'File is not a valid theme.')) translate('OpenLP.ThemeManager',
log.exception(u'Theme file does not contain XML data %s' % 'File is not a valid theme.'))
filename) log.exception(u'Theme file does not contain XML data %s' %
filename)
def unzipVersion122(self, dir, zip, xmlfile, xml_tree, background, outfile): def unzipVersion122(self, dir, zip, xmlfile, xml_tree, background, outfile):
""" """
@ -596,6 +618,10 @@ class ThemeManager(QtGui.QWidget):
""" """
themename = xml_tree.find(u'Name').text.strip() themename = xml_tree.find(u'Name').text.strip()
themename = self.bad_v1_name_chars.sub(u'', themename) themename = self.bad_v1_name_chars.sub(u'', themename)
themefolder = os.path.join(dir, themename)
themeexists = os.path.exists(themefolder)
if themeexists and not self.overWriteMessageBox(themename):
return ( '', '', '', True)
themedir = os.path.join(dir, themename) themedir = os.path.join(dir, themename)
check_directory_exists(themedir) check_directory_exists(themedir)
filexml = unicode(zip.read(xmlfile), u'utf-8') filexml = unicode(zip.read(xmlfile), u'utf-8')
@ -617,7 +643,7 @@ class ThemeManager(QtGui.QWidget):
log.exception(u'Theme file does not contain image file "%s"' % log.exception(u'Theme file does not contain image file "%s"' %
imagename.decode(u'utf-8', u'replace')) imagename.decode(u'utf-8', u'replace'))
raise Exception(u'validation') raise Exception(u'validation')
return (themename, filexml, outfile) return (themename, filexml, outfile, False)
def checkIfThemeExists(self, themeName): def checkIfThemeExists(self, themeName):
""" """