From 2388668aa9c219801824e0c38e99fa675e32c24e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 8 Apr 2013 16:16:58 +0530 Subject: [PATCH] Allow restoring of the ORIGINAL_XXX format by right-clicking it in the book details panel --- src/calibre/gui2/actions/delete.py | 7 +++++++ src/calibre/gui2/book_details.py | 14 ++++++++++++-- src/calibre/gui2/init.py | 2 ++ src/calibre/library/database2.py | 8 ++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/calibre/gui2/actions/delete.py b/src/calibre/gui2/actions/delete.py index 7bdcb18644..178d94a477 100644 --- a/src/calibre/gui2/actions/delete.py +++ b/src/calibre/gui2/actions/delete.py @@ -180,6 +180,13 @@ class DeleteAction(InterfaceAction): self.gui.library_view.currentIndex()) self.gui.tags_view.recount() + def restore_format(self, book_id, original_fmt): + self.gui.current_db.restore_original_format(book_id, original_fmt) + self.gui.library_view.model().refresh_ids([book_id]) + self.gui.library_view.model().current_changed(self.gui.library_view.currentIndex(), + self.gui.library_view.currentIndex()) + self.gui.tags_view.recount() + def delete_selected_formats(self, *args): ids = self._get_selected_ids() if not ids: diff --git a/src/calibre/gui2/book_details.py b/src/calibre/gui2/book_details.py index 4d00d282d5..45430da6f4 100644 --- a/src/calibre/gui2/book_details.py +++ b/src/calibre/gui2/book_details.py @@ -405,6 +405,7 @@ class BookInfo(QWebView): link_clicked = pyqtSignal(object) remove_format = pyqtSignal(int, object) save_format = pyqtSignal(int, object) + restore_format = pyqtSignal(int, object) def __init__(self, vertical, parent=None): QWebView.__init__(self, parent) @@ -418,7 +419,7 @@ class BookInfo(QWebView): palette.setBrush(QPalette.Base, Qt.transparent) self.page().setPalette(palette) self.css = P('templates/book_details.css', data=True).decode('utf-8') - for x, icon in [('remove', 'trash.png'), ('save', 'save.png')]: + for x, icon in [('remove', 'trash.png'), ('save', 'save.png'), ('restore', 'edit-undo.png')]: ac = QAction(QIcon(I(icon)), '', self) ac.current_fmt = None ac.triggered.connect(getattr(self, '%s_format_triggerred'%x)) @@ -436,6 +437,9 @@ class BookInfo(QWebView): def save_format_triggerred(self): self.context_action_triggered('save') + def restore_format_triggerred(self): + self.context_action_triggered('restore') + def link_activated(self, link): self._link_clicked = True if unicode(link.scheme()) in ('http', 'https'): @@ -479,7 +483,11 @@ class BookInfo(QWebView): traceback.print_exc() else: for a, t in [('remove', _('Delete the %s format')), - ('save', _('Save the %s format to disk'))]: + ('save', _('Save the %s format to disk')), + ('restore', _('Restore the %s format')), + ]: + if a == 'restore' and not fmt.upper().startswith('ORIGINAL_'): + continue ac = getattr(self, '%s_format_action'%a) ac.current_fmt = (book_id, fmt) ac.setText(t%parts[2]) @@ -585,6 +593,7 @@ class BookDetails(QWidget): # {{{ view_specific_format = pyqtSignal(int, object) remove_specific_format = pyqtSignal(int, object) save_specific_format = pyqtSignal(int, object) + restore_specific_format = pyqtSignal(int, object) remote_file_dropped = pyqtSignal(object, object) files_dropped = pyqtSignal(object, object) cover_changed = pyqtSignal(object, object) @@ -654,6 +663,7 @@ class BookDetails(QWidget): # {{{ self.book_info.link_clicked.connect(self.handle_click) self.book_info.remove_format.connect(self.remove_specific_format) self.book_info.save_format.connect(self.save_specific_format) + self.book_info.restore_format.connect(self.restore_specific_format) self.setCursor(Qt.PointingHandCursor) def handle_click(self, link): diff --git a/src/calibre/gui2/init.py b/src/calibre/gui2/init.py index eff36e865b..2a5b061819 100644 --- a/src/calibre/gui2/init.py +++ b/src/calibre/gui2/init.py @@ -272,6 +272,8 @@ class LayoutMixin(object): # {{{ self.iactions['Remove Books'].remove_format_by_id) self.book_details.save_specific_format.connect( self.iactions['Save To Disk'].save_library_format_by_ids) + self.book_details.restore_specific_format.connect( + self.iactions['Remove Books'].restore_format) self.book_details.view_device_book.connect( self.iactions['View'].view_device_book) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 376eb52c3c..06a2e5ff71 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1541,6 +1541,14 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): opath = self.format_abspath(book_id, nfmt, index_is_id=True) return fmt if opath is None else nfmt + def restore_original_format(self, book_id, original_fmt, notify=True): + opath = self.format_abspath(book_id, original_fmt, index_is_id=True) + if opath is not None: + fmt = original_fmt.partition('_')[2] + with lopen(opath, 'rb') as f: + self.add_format(book_id, fmt, f, index_is_id=True, notify=False) + self.remove_format(book_id, original_fmt, index_is_id=True, notify=notify) + def delete_book(self, id, notify=True, commit=True, permanent=False, do_clean=True): '''