Merge lp:~raoul-snyman/openlp/bug-855342 into lp:openlp

Proposed by Raoul Snyman
Status: Merged
Approved by: Tim Bentley
Approved revision: 1760
Merged at revision: 1760
Proposed branch: lp:~raoul-snyman/openlp/bug-855342
Merge into: lp:openlp
Diff against target: 143 lines (+48/-24)
1 file modified
openlp/core/ui/servicemanager.py (+48/-24)
To merge this branch: bzr merge lp:~raoul-snyman/openlp/bug-855342
Reviewer Review Type Date Requested Status
Tim Bentley Approve
Review via email: mp+76850@code.launchpad.net

Commit message

Fixed the last part of bug #855342 where when you save a file a few times, it throws an exception.

Description of the change

Fixed the last part of bug #855342 where when you save a file a few times, it throws an exception.

To post a comment you must log in.
Revision history for this message
Tim Bentley (trb143) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'openlp/core/ui/servicemanager.py'
--- openlp/core/ui/servicemanager.py 2011-09-21 20:47:30 +0000
+++ openlp/core/ui/servicemanager.py 2011-09-24 13:06:26 +0000
@@ -30,6 +30,7 @@
30import os30import os
31import shutil31import shutil
32import zipfile32import zipfile
33from tempfile import mkstemp
3334
34log = logging.getLogger(__name__)35log = logging.getLogger(__name__)
3536
@@ -467,15 +468,24 @@
467468
468 def saveFile(self):469 def saveFile(self):
469 """470 """
470 Save the current Service file.471 Save the current service file.
472
473 A temporary file is created so that we don't overwrite the existing one
474 and leave a mangled service file should there be an error when saving.
475 Audio files are also copied into the service manager directory, and
476 then packaged into the zip file.
471 """477 """
472 if not self.fileName():478 if not self.fileName():
473 return self.saveFileAs()479 return self.saveFileAs()
480 temp_file, temp_file_name = mkstemp(u'.osz', u'openlp_')
481 # We don't need the file handle.
482 os.close(temp_file)
483 log.debug(temp_file_name)
474 path_file_name = unicode(self.fileName())484 path_file_name = unicode(self.fileName())
475 path, file_name = os.path.split(path_file_name)485 path, file_name = os.path.split(path_file_name)
476 basename, extension = os.path.splitext(file_name)486 basename, extension = os.path.splitext(file_name)
477 service_file_name = '%s.osd' % basename487 service_file_name = '%s.osd' % basename
478 log.debug(u'ServiceManager.saveFile - %s' % path_file_name)488 log.debug(u'ServiceManager.saveFile - %s', path_file_name)
479 SettingsManager.set_last_dir(489 SettingsManager.set_last_dir(
480 self.mainwindow.servicemanagerSettingsSection,490 self.mainwindow.servicemanagerSettingsSection,
481 path)491 path)
@@ -494,7 +504,8 @@
494 if len(service_item[u'header'][u'background_audio']) > 0:504 if len(service_item[u'header'][u'background_audio']) > 0:
495 for i, filename in \505 for i, filename in \
496 enumerate(service_item[u'header'][u'background_audio']):506 enumerate(service_item[u'header'][u'background_audio']):
497 new_file = os.path.join(u'audio', item[u'service_item']._uuid,507 new_file = os.path.join(u'audio',
508 item[u'service_item']._uuid,
498 os.path.split(filename)[1])509 os.path.split(filename)[1])
499 audio_files.append((filename, new_file))510 audio_files.append((filename, new_file))
500 service_item[u'header'][u'background_audio'][i] = new_file511 service_item[u'header'][u'background_audio'][i] = new_file
@@ -545,30 +556,38 @@
545 success = True556 success = True
546 self.mainwindow.incrementProgressBar()557 self.mainwindow.incrementProgressBar()
547 try:558 try:
548 zip = zipfile.ZipFile(path_file_name, 'w', zipfile.ZIP_STORED,559 zip = zipfile.ZipFile(temp_file_name, 'w', zipfile.ZIP_STORED,
549 allow_zip_64)560 allow_zip_64)
550 # First we add service contents.561 # First we add service contents.
551 # We save ALL filenames into ZIP using UTF-8.562 # We save ALL filenames into ZIP using UTF-8.
552 zip.writestr(service_file_name.encode(u'utf-8'), service_content)563 zip.writestr(service_file_name.encode(u'utf-8'), service_content)
553 # Finally add all the listed media files.564 # Finally add all the listed media files.
554 for path_from in write_list:565 for write_from in write_list:
555 zip.write(path_from, path_from.encode(u'utf-8'))566 zip.write(write_from, write_from.encode(u'utf-8'))
556 for path_from, path_to in audio_files:567 for audio_from, audio_to in audio_files:
557 if path_from == path_to:568 if audio_from.startswith(u'audio'):
558 # If this file has already been saved, let's use set the569 # When items are saved, they get new UUID's. Let's copy the
559 # from path to the real location of the files570 # file to the new location. Unused files can be ignored,
560 path_from = os.path.join(self.servicePath, path_from)571 # OpenLP automatically cleans up the service manager dir on
561 else:572 # exit.
562 # If this file has not yet been saved, let's copy the file573 audio_from = os.path.join(self.servicePath, audio_from)
563 # to the service manager path574 save_file = os.path.join(self.servicePath, audio_to)
564 save_file = os.path.join(self.servicePath, path_to)575 save_path = os.path.split(save_file)[0]
565 save_path = os.path.split(save_file)[0]576 if not os.path.exists(save_path):
566 if not os.path.exists(save_path):577 os.makedirs(save_path)
567 os.makedirs(save_path)578 if not os.path.exists(save_file):
568 shutil.copy(path_from, save_file)579 shutil.copy(audio_from, save_file)
569 zip.write(path_from, path_to.encode(u'utf-8'))580 zip.write(audio_from, audio_to.encode(u'utf-8'))
570 except IOError:581 except IOError:
571 log.exception(u'Failed to save service to disk')582 log.exception(u'Failed to save service to disk: %s', temp_file_name)
583 # Add this line in after the release to notify the user that saving
584 # their file failed. Commented out due to string freeze.
585 #Receiver.send_message(u'openlp_error_message', {
586 # u'title': translate(u'OpenLP.ServiceManager',
587 # u'Error Saving File'),
588 # u'message': translate(u'OpenLP.ServiceManager',
589 # u'There was an error saving your file.')
590 #})
572 success = False591 success = False
573 finally:592 finally:
574 if zip:593 if zip:
@@ -576,10 +595,13 @@
576 self.mainwindow.finishedProgressBar()595 self.mainwindow.finishedProgressBar()
577 Receiver.send_message(u'cursor_normal')596 Receiver.send_message(u'cursor_normal')
578 if success:597 if success:
598 shutil.copy(temp_file_name, path_file_name)
579 self.mainwindow.addRecentFile(path_file_name)599 self.mainwindow.addRecentFile(path_file_name)
580 self.setModified(False)600 self.setModified(False)
581 else:601 try:
582 delete_file(path_file_name)602 delete_file(temp_file_name)
603 except:
604 pass
583 return success605 return success
584606
585 def saveFileAs(self):607 def saveFileAs(self):
@@ -623,6 +645,7 @@
623 osfile = unicode(QtCore.QDir.toNativeSeparators(ucsfile))645 osfile = unicode(QtCore.QDir.toNativeSeparators(ucsfile))
624 if not osfile.startswith(u'audio'):646 if not osfile.startswith(u'audio'):
625 osfile = os.path.split(osfile)[1]647 osfile = os.path.split(osfile)[1]
648 log.debug(u'Extract file: %s', osfile)
626 zipinfo.filename = osfile649 zipinfo.filename = osfile
627 zip.extract(zipinfo, self.servicePath)650 zip.extract(zipinfo, self.servicePath)
628 if osfile.endswith(u'osd'):651 if osfile.endswith(u'osd'):
@@ -1022,11 +1045,12 @@
1022 """1045 """
1023 Empties the servicePath of temporary files.1046 Empties the servicePath of temporary files.
1024 """1047 """
1048 log.debug(u'Cleaning up servicePath')
1025 for file in os.listdir(self.servicePath):1049 for file in os.listdir(self.servicePath):
1026 file_path = os.path.join(self.servicePath, file)1050 file_path = os.path.join(self.servicePath, file)
1027 delete_file(file_path)1051 delete_file(file_path)
1028 if os.path.exists(os.path.join(self.servicePath, u'audio')):1052 if os.path.exists(os.path.join(self.servicePath, u'audio')):
1029 shutil.rmtree(os.path.join(self.servicePath, u'audio'), False)1053 shutil.rmtree(os.path.join(self.servicePath, u'audio'), True)
10301054
1031 def onThemeComboBoxSelected(self, currentIndex):1055 def onThemeComboBoxSelected(self, currentIndex):
1032 """1056 """