From 494a252f3f2e6eb955c00c331c9865cabfb63148 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 16 Oct 2013 17:57:15 +0530 Subject: [PATCH] Saving implemented --- src/calibre/ebooks/oeb/polish/container.py | 9 +++++++++ src/calibre/gui2/tweak_book/boss.py | 23 ++++++++++++++++++++-- src/calibre/gui2/tweak_book/save.py | 19 ++++++++++++++---- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index 66efb697c7..b8362b73c9 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -745,6 +745,10 @@ class EpubContainer(Container): f.write(guess_type('a.epub')) zip_rebuilder(self.root, outpath) + @property + def path_to_ebook(self): + return self.pathtoepub + # }}} # AZW3 {{{ @@ -839,6 +843,11 @@ class AZW3Container(Container): oeb = create_oebbook(default_log, opf, plumber.opts) set_cover(oeb) outp.convert(oeb, outpath, inp, plumber.opts, default_log) + + @property + def path_to_ebook(self): + return self.pathtoepub + # }}} def get_container(path, log=None, tdir=None): diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 076f6edb82..575978bdc3 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -10,7 +10,7 @@ import tempfile, shutil from PyQt4.Qt import QObject, QApplication -from calibre.gui2 import error_dialog, choose_files +from calibre.gui2 import error_dialog, choose_files, question_dialog, info_dialog from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ebooks.oeb.polish.main import SUPPORTED from calibre.ebooks.oeb.polish.container import get_container, clone_container @@ -26,6 +26,7 @@ class Boss(QObject): self.container_count = 0 self.tdir = None self.save_manager = SaveManager(parent) + self.save_manager.report_error.connect(self.report_save_error) def __call__(self, gui): self.gui = gui @@ -42,7 +43,10 @@ class Boss(QObject): def open_book(self, path=None): if not self.check_dirtied(): return - # TODO: Check if a save is in progress and abort if it is + if self.save_manager.has_tasks: + return info_dialog(self.gui, _('Cannot open'), + _('The current book is being saved, you cannot open a new book until' + ' the saving is completed'), show=True) if not hasattr(path, 'rpartition'): path = choose_files(self.gui, 'open-book-for-tweaking', _('Choose book'), @@ -95,6 +99,7 @@ class Boss(QObject): container = current_container() self.gui.file_list.build(container) self.update_global_history_actions() + self.gui.action_save.setEnabled(True) # TODO: Apply to other GUI elements def do_global_undo(self): @@ -127,6 +132,11 @@ class Boss(QObject): container = clone_container(current_container(), tdir) self.save_manager.schedule(tdir, container) + def report_save_error(self, tb): + error_dialog(self.gui, _('Could not save'), + _('Saving of the book failed. Click "Show Details"' + ' for more information.'), det_msg=tb, show=True) + def quit(self): if not self.confirm_quit(): return @@ -134,10 +144,19 @@ class Boss(QObject): QApplication.instance().quit() def confirm_quit(self): + if self.save_manager.has_tasks: + if not question_dialog( + self.gui, _('Are you sure?'), _( + 'The current book is being saved in the background, quitting will abort' + ' the save process, are you sure?'), default_yes=False): + return False + return True def shutdown(self): self.save_state() + self.save_manager.shutdown() + self.save_manager.wait(0.1) def save_state(self): with tprefs: diff --git a/src/calibre/gui2/tweak_book/save.py b/src/calibre/gui2/tweak_book/save.py index c18ce6de11..6c89a75413 100644 --- a/src/calibre/gui2/tweak_book/save.py +++ b/src/calibre/gui2/tweak_book/save.py @@ -6,14 +6,17 @@ from __future__ import (unicode_literals, division, absolute_import, __license__ = 'GPL v3' __copyright__ = '2013, Kovid Goyal ' -import shutil +import shutil, os from threading import Thread from Queue import LifoQueue, Empty from PyQt4.Qt import (QObject, pyqtSignal, QLabel, QWidget, QHBoxLayout, Qt) -from calibre.utils import join_with_timeout +from calibre.constants import iswindows +from calibre.ptempfile import PersistentTemporaryFile from calibre.gui2.progress_indicator import ProgressIndicator +from calibre.utils import join_with_timeout +from calibre.utils.filenames import atomic_rename class SaveWidget(QWidget): @@ -101,10 +104,18 @@ class SaveManager(QObject): self.save_done.emit() def do_save(self, tdir, container): + temp = None try: - import time - time.sleep(10) + path = container.path_to_ebook + temp = PersistentTemporaryFile( + prefix=('_' if iswindows else '.'), suffix=os.path.splitext(path)[1], dir=os.path.dirname(path)) + temp.close() + temp = temp.name + container.commit(temp) + atomic_rename(temp, path) finally: + if temp and os.path.exists(temp): + os.remove(temp) shutil.rmtree(tdir, ignore_errors=True) @property