Edit book: Allow exporting all selected files to the computer from the File Browser by right clicking

This commit is contained in:
Kovid Goyal 2017-07-10 16:09:55 +05:30
parent 7ed129d73f
commit d57943fac2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 28 additions and 4 deletions

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import tempfile, shutil, sys, os
import tempfile, shutil, sys, os, errno
from functools import partial, wraps
from urlparse import urlparse
@ -1283,7 +1283,20 @@ class Boss(QObject):
self.set_modified()
@in_thread_job
def export_requested(self, name, path):
def export_requested(self, name_or_names, path):
if isinstance(name_or_names, basestring):
return self.export_file(name_or_names, path)
for name in name_or_names:
dest = os.path.abspath(os.path.join(path, name))
if '/' in name or os.sep in name:
try:
os.makedirs(os.path.dirname(dest))
except EnvironmentError as err:
if err.errno != errno.EEXIST:
raise
self.export_file(name, dest)
def export_file(self, name, path):
if name in editors and not editors[name].is_synced_to_container:
self.commit_editor_to_container(name)
with current_container().open(name, 'rb') as src, open(path, 'wb') as dest:

View File

@ -25,7 +25,8 @@ from calibre.ebooks.oeb.polish.cover import (
)
from calibre.ebooks.oeb.polish.replace import get_recommended_folders
from calibre.gui2 import (
choose_files, choose_save_file, elided_text, error_dialog, question_dialog
choose_dir, choose_files, choose_save_file, elided_text, error_dialog,
question_dialog
)
from calibre.gui2.tweak_book import (
CONTAINER_DND_MIMETYPE, current_container, editors, tprefs
@ -34,7 +35,6 @@ from calibre.gui2.tweak_book.editor import syntax_from_mime
from calibre.gui2.tweak_book.templates import template_for
from calibre.utils.icu import sort_key
TOP_ICON_SIZE = 24
NAME_ROLE = Qt.UserRole
CATEGORY_ROLE = NAME_ROLE + 1
@ -482,6 +482,9 @@ class FileList(QTreeWidget):
m.addAction(QIcon(I('save.png')), _('Export %s') % n, partial(self.export, cn))
if cn not in container.names_that_must_not_be_changed and cn not in container.names_that_must_not_be_removed and mt not in OEB_FONTS:
m.addAction(_('Replace %s with file...') % n, partial(self.replace, cn))
if num > 1:
m.addAction(QIcon(I('save.png')), _('Export all %d selected files') % num, self.export_selected)
m.addSeparator()
m.addAction(QIcon(I('modified.png')), _('&Rename %s') % n, self.edit_current_item)
@ -732,6 +735,14 @@ class FileList(QTreeWidget):
if path:
self.export_requested.emit(name, path)
def export_selected(self):
names = self.selected_names
if not names:
return
path = choose_dir(self, 'tweak_book_export_selected', _('Choose location'))
if path:
self.export_requested.emit(names, path)
def replace(self, name):
c = current_container()
mt = c.mime_map[name]