E-book viewer: Automatically save the current reading position every ten seconds. Prevents loss of reading position on crash/forced shutdown. Fixes #1473737 [Save page position in Viewer while open](https://bugs.launchpad.net/calibre/+bug/1473737)

This commit is contained in:
Kovid Goyal 2015-07-15 09:44:38 +05:30
parent 9474a3ecef
commit a9a5e39279
2 changed files with 14 additions and 6 deletions

View File

@ -78,12 +78,12 @@ class BookmarksMixin(object):
raw = raw.decode('utf-8')
self.parse_bookmarks(raw)
def save_bookmarks(self, bookmarks=None):
def save_bookmarks(self, bookmarks=None, no_copy_to_file=False):
if bookmarks is None:
bookmarks = self.bookmarks
dat = self.serialize_bookmarks(bookmarks)
self.config['bookmarks_'+self.pathtoebook] = dat
if self.copy_bookmarks_to_file and os.path.splitext(
if not no_copy_to_file and self.copy_bookmarks_to_file and os.path.splitext(
self.pathtoebook)[1].lower() == '.epub' and os.access(self.pathtoebook, os.W_OK):
try:
zf = open(self.pathtoebook, 'r+b')
@ -93,11 +93,11 @@ class BookmarksMixin(object):
BytesIO(dat.encode('utf-8')),
add_missing=True)
def add_bookmark(self, bm):
def add_bookmark(self, bm, no_copy_to_file=False):
self.bookmarks = [x for x in self.bookmarks if x['title'] !=
bm['title']]
self.bookmarks.append(bm)
self.save_bookmarks()
self.save_bookmarks(no_copy_to_file=no_copy_to_file)
def set_bookmarks(self, bookmarks):
self.bookmarks = bookmarks

View File

@ -74,6 +74,7 @@ class EbookViewer(MainWindow):
'into pages like a paper book')
PAGED_MODE_TT = _('Switch to flow mode - where the text is not broken up '
'into pages')
AUTOSAVE_INTERVAL = 10 # seconds
def __init__(self, pathtoebook=None, debug_javascript=False, open_at=None,
start_in_fullscreen=False):
@ -96,7 +97,11 @@ class EbookViewer(MainWindow):
self.was_maximized = False
self.page_position_on_footnote_toggle = []
self.read_settings()
self.autosave_timer = t = QTimer(self)
t.setInterval(self.AUTOSAVE_INTERVAL * 1000), t.setSingleShot(True)
t.timeout.connect(self.autosave)
self.pos.value_changed.connect(self.update_pos_label)
self.pos.value_changed.connect(self.autosave_timer.start)
self.pos.setMinimumWidth(150)
self.setFocusPolicy(Qt.StrongFocus)
self.view.set_manager(self)
@ -783,6 +788,9 @@ class EbookViewer(MainWindow):
self.set_bookmarks(self.iterator.bookmarks)
self.bookmarks.set_current_bookmark(bm)
def autosave(self):
self.save_current_position(no_copy_to_file=True)
def bookmarks_edited(self, bookmarks):
self.build_bookmarks_menu(bookmarks)
self.iterator.set_bookmarks(bookmarks)
@ -816,12 +824,12 @@ class EbookViewer(MainWindow):
bm['title'] = 'calibre_current_page_bookmark'
return bm
def save_current_position(self):
def save_current_position(self, no_copy_to_file=False):
if not self.view.document.remember_current_page:
return
if hasattr(self, 'current_index'):
try:
self.iterator.add_bookmark(self.current_page_bookmark)
self.iterator.add_bookmark(self.current_page_bookmark, no_copy_to_file=no_copy_to_file)
except:
traceback.print_exc()