E-book viewer: Add an aoption to not save the bookmarks inside EPUB files. Fixes #379 (Add a store_epub_bookmarks_in_the_file option for ebook-viewer)

This commit is contained in:
Kovid Goyal 2015-04-03 11:33:07 +05:30
parent b2383f4061
commit 426bbb2d60
6 changed files with 45 additions and 31 deletions

View File

@ -50,7 +50,8 @@ class EbookIterator(BookmarksMixin):
CHARACTERS_PER_PAGE = 1000
def __init__(self, pathtoebook, log=None):
def __init__(self, pathtoebook, log=None, copy_bookmarks_to_file=True):
BookmarksMixin.__init__(self, copy_bookmarks_to_file=copy_bookmarks_to_file)
self.log = log or default_log
pathtoebook = pathtoebook.strip()
self.pathtoebook = os.path.abspath(pathtoebook)
@ -101,8 +102,8 @@ class EbookIterator(BookmarksMixin):
if not only_input_plugin:
# Run the HTML preprocess/parsing from the conversion pipeline as
# well
if (processed or plumber.input_fmt.lower() in {'pdb', 'pdf', 'rb'}
and not hasattr(self.pathtoopf, 'manifest')):
if (processed or plumber.input_fmt.lower() in {'pdb', 'pdf', 'rb'} and
not hasattr(self.pathtoopf, 'manifest')):
if hasattr(self.pathtoopf, 'manifest'):
self.pathtoopf = write_oebbook(self.pathtoopf, self.base)
self.pathtoopf = create_oebbook(self.log, self.pathtoopf,

View File

@ -17,6 +17,9 @@ BM_LEGACY_ESC = u'esc-text-%&*#%(){}ads19-end-esc'
class BookmarksMixin(object):
def __init__(self, copy_bookmarks_to_file=True):
self.copy_bookmarks_to_file = copy_bookmarks_to_file
def parse_bookmarks(self, raw):
for line in raw.splitlines():
bm = None
@ -64,16 +67,14 @@ class BookmarksMixin(object):
def read_bookmarks(self):
self.bookmarks = []
bmfile = os.path.join(self.base, 'META-INF', 'calibre_bookmarks.txt')
raw = ''
if os.path.exists(bmfile):
with open(bmfile, 'rb') as f:
raw = f.read()
else:
saved = self.config['bookmarks_'+self.pathtoebook]
if saved:
raw = saved
if not isinstance(raw, unicode):
raw = self.config['bookmarks_'+self.pathtoebook] or ''
if not raw:
# Look for bookmarks saved inside the ebook
bmfile = os.path.join(self.base, 'META-INF', 'calibre_bookmarks.txt')
if os.path.exists(bmfile):
with open(bmfile, 'rb') as f:
raw = f.read()
if isinstance(raw, bytes):
raw = raw.decode('utf-8')
self.parse_bookmarks(raw)
@ -81,8 +82,9 @@ class BookmarksMixin(object):
if bookmarks is None:
bookmarks = self.bookmarks
dat = self.serialize_bookmarks(bookmarks)
if os.path.splitext(self.pathtoebook)[1].lower() == '.epub' and \
os.access(self.pathtoebook, os.R_OK):
self.config['bookmarks_'+self.pathtoebook] = dat
if 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')
except IOError:
@ -90,8 +92,6 @@ class BookmarksMixin(object):
safe_replace(zf, 'META-INF/calibre_bookmarks.txt',
BytesIO(dat.encode('utf-8')),
add_missing=True)
else:
self.config['bookmarks_'+self.pathtoebook] = dat
def add_bookmark(self, bm):
self.bookmarks = [x for x in self.bookmarks if x['title'] !=

View File

@ -49,6 +49,8 @@ def config(defaults=None):
help=_('Default language for hyphenation rules'))
c.add_opt('remember_current_page', default=True,
help=_('Save the current position in the document, when quitting'))
c.add_opt('copy_bookmarks_to_file', default=True,
help=_('Copy bookmarks to the ebook file for easy sharing, if possible'))
c.add_opt('wheel_flips_pages', default=False,
help=_('Have the mouse wheel turn pages'))
c.add_opt('tap_flips_pages', default=True,
@ -281,6 +283,7 @@ class ConfigDialog(QDialog, Ui_Dialog):
def load_options(self, opts):
self.opt_remember_window_size.setChecked(opts.remember_window_size)
self.opt_remember_current_page.setChecked(opts.remember_current_page)
self.opt_copy_bookmarks_to_file.setChecked(opts.copy_bookmarks_to_file)
self.opt_wheel_flips_pages.setChecked(opts.wheel_flips_pages)
self.opt_tap_flips_pages.setChecked(opts.tap_flips_pages)
self.opt_page_flip_duration.setValue(opts.page_flip_duration)
@ -383,6 +386,7 @@ class ConfigDialog(QDialog, Ui_Dialog):
c.set('max_fs_height', max_fs_height)
c.set('hyphenate', self.hyphenate.isChecked())
c.set('remember_current_page', self.opt_remember_current_page.isChecked())
c.set('copy_bookmarks_to_file', self.opt_copy_bookmarks_to_file.isChecked())
c.set('wheel_flips_pages', self.opt_wheel_flips_pages.isChecked())
c.set('tap_flips_pages', self.opt_tap_flips_pages.isChecked())
c.set('page_flip_duration', self.opt_page_flip_duration.value())

View File

@ -213,7 +213,7 @@ QToolBox::tab:hover {
<item row="7" column="0">
<widget class="QLabel" name="label_22">
<property name="text">
<string>M&amp;inimum font size:</string>
<string>Minimum font si&amp;ze:</string>
</property>
<property name="buddy">
<cstring>minimum_font_size</cstring>
@ -551,8 +551,8 @@ QToolBox::tab:hover {
<rect>
<x>0</x>
<y>0</y>
<width>799</width>
<height>378</height>
<width>384</width>
<height>140</height>
</rect>
</property>
<attribute name="label">
@ -629,8 +629,8 @@ QToolBox::tab:hover {
<rect>
<x>0</x>
<y>0</y>
<width>368</width>
<height>167</height>
<width>799</width>
<height>378</height>
</rect>
</property>
<attribute name="label">
@ -661,7 +661,7 @@ QToolBox::tab:hover {
</property>
</widget>
</item>
<item row="5" column="0">
<item row="6" column="0">
<widget class="QPushButton" name="clear_search_history_button">
<property name="text">
<string>Clear search history</string>
@ -689,6 +689,16 @@ QToolBox::tab:hover {
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<widget class="QCheckBox" name="opt_copy_bookmarks_to_file">
<property name="toolTip">
<string>Keep a copy of all bookmarks/current page information inside the ebook file, so that you can share them by simply sending the ebook file itself. Currently only works with ebooks in the EPUB format.</string>
</property>
<property name="text">
<string>Keep a copy of bookmarks/current page inside the ebook file, for easy sharing</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>

View File

@ -183,6 +183,8 @@ class Document(QWebPage): # {{{
self.side_margin = opts.side_margin
self.top_margin, self.bottom_margin = opts.top_margin, opts.bottom_margin
self.show_controls = opts.show_controls
self.remember_current_page = opts.remember_current_page
self.copy_bookmarks_to_file = opts.copy_bookmarks_to_file
def fit_images(self):
if self.do_fit_images and not self.in_paged_mode:

View File

@ -304,11 +304,6 @@ class EbookViewer(MainWindow):
url = default_lookup_website(canonicalize_lang(self.view.current_language) or 'en').format(word=word)
open_url(url)
def get_remember_current_page_opt(self):
from calibre.gui2.viewer.documentview import config
c = config().parse()
return c.remember_current_page
def print_book(self):
p = Printing(self.iterator, self)
p.start_print()
@ -764,6 +759,8 @@ class EbookViewer(MainWindow):
def do_config(self):
self.view.config(self)
self.load_theme_menu()
if self.iterator is not None:
self.iterator.copy_bookmarks_to_file = self.view.document.copy_bookmarks_to_file
from calibre.gui2 import config
if not config['viewer_search_history']:
self.search.clear_history()
@ -802,7 +799,7 @@ class EbookViewer(MainWindow):
self.existing_bookmarks = []
for bm in bookmarks:
if bm['title'] == 'calibre_current_page_bookmark':
if self.get_remember_current_page_opt():
if self.view.document.remember_current_page:
current_page = bm
else:
self.existing_bookmarks.append(bm['title'])
@ -821,7 +818,7 @@ class EbookViewer(MainWindow):
return bm
def save_current_position(self):
if not self.get_remember_current_page_opt():
if not self.view.document.remember_current_page:
return
if hasattr(self, 'current_index'):
try:
@ -833,7 +830,7 @@ class EbookViewer(MainWindow):
if self.iterator is not None:
self.save_current_position()
self.iterator.__exit__()
self.iterator = EbookIterator(pathtoebook)
self.iterator = EbookIterator(pathtoebook, copy_bookmarks_to_file=self.view.document.copy_bookmarks_to_file)
self.history.clear()
self.open_progress_indicator(_('Loading ebook...'))
worker = Worker(target=partial(self.iterator.__enter__, view_kepub=True))