From fdac643e72fec20733b309fd2a6c0e8807756c4b Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 8 Aug 2009 08:38:33 -0400 Subject: [PATCH 1/3] Copy messages to clipboard. --- src/calibre/gui2/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 9898eb5c13..68a582d42e 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -108,7 +108,7 @@ def warning_dialog(parent, title, msg, det_msg='', show=False): parent) d.setDetailedText(det_msg) d.setIconPixmap(QPixmap(':/images/dialog_warning.svg')) - + QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) if show: return d.exec_() return d @@ -118,6 +118,7 @@ def error_dialog(parent, title, msg, det_msg='', show=False): parent) d.setDetailedText(det_msg) d.setIconPixmap(QPixmap(':/images/dialog_error.svg')) + QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) if show: return d.exec_() return d @@ -127,6 +128,7 @@ def question_dialog(parent, title, msg, det_msg=''): parent) d.setDetailedText(det_msg) d.setIconPixmap(QPixmap(':/images/dialog_information.svg')) + QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) return d.exec_() == QMessageBox.Yes def info_dialog(parent, title, msg, det_msg='', show=False): @@ -134,6 +136,7 @@ def info_dialog(parent, title, msg, det_msg='', show=False): parent) d.setDetailedText(det_msg) d.setIconPixmap(QPixmap(':/images/dialog_information.svg')) + QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) if show: return d.exec_() return d From 3b23ff4518162815b5a823918e2bbeb1fb42c4ab Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 8 Aug 2009 11:22:13 -0400 Subject: [PATCH 2/3] Fix bug #3104: FB2 output handle images with the same name in different directories. --- src/calibre/ebooks/fb2/fb2ml.py | 54 +++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py index 357bce0b22..c77cb876f8 100644 --- a/src/calibre/ebooks/fb2/fb2ml.py +++ b/src/calibre/ebooks/fb2/fb2ml.py @@ -9,8 +9,15 @@ Transform OEB content into FB2 markup ''' import os +import cStringIO from base64 import b64encode +try: + from PIL import Image + Image +except ImportError: + import Image + from lxml import etree from calibre import prepare_string_for_xml @@ -37,8 +44,10 @@ STYLES = [ ] class FB2MLizer(object): + def __init__(self, log): self.log = log + self.image_hrefs = {} def extract_content(self, oeb_book, opts): self.log.info('Converting XHTML to FB2 markup...') @@ -47,6 +56,7 @@ class FB2MLizer(object): return self.fb2mlize_spine() def fb2mlize_spine(self): + self.image_hrefs = {} output = self.fb2_header() if 'titlepage' in self.oeb_book.guide: self.log.debug('Generating cover page...') @@ -54,11 +64,11 @@ class FB2MLizer(object): item = self.oeb_book.manifest.hrefs[href] if item.spine_position is None: stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) - output += self.dump_text(item.data.find(XHTML('body')), stylizer) + output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) for item in self.oeb_book.spine: self.log.debug('Converting %s to FictionBook2 XML' % item.href) stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts.output_profile) - output += self.dump_text(item.data.find(XHTML('body')), stylizer) + output += self.dump_text(item.data.find(XHTML('body')), stylizer, item) output += self.fb2_body_footer() output += self.fb2mlize_images() output += self.fb2_footer() @@ -102,20 +112,29 @@ class FB2MLizer(object): images = u'' for item in self.oeb_book.manifest: if item.media_type in OEB_RASTER_IMAGES: - raw_data = b64encode(item.data) - # Don't put the encoded image on a single line. - data = '' - col = 1 - for char in raw_data: - if col == 72: - data += '\n' - col = 1 - col += 1 - data += char - images += '%s\n' % (os.path.basename(item.href), item.media_type, data) + try: + im = Image.open(cStringIO.StringIO(item.data)) + data = cStringIO.StringIO() + im.save(data, 'JPEG') + data = data.getvalue() + + raw_data = b64encode(data) + # Don't put the encoded image on a single line. + data = '' + col = 1 + for char in raw_data: + if col == 72: + data += '\n' + col = 1 + col += 1 + data += char + images += '%s\n' % (self.image_hrefs.get(item.href, '0000.JPEG'), item.media_type, data) + except Exception as e: + self.log.error('Error: Could not include file %s becuase ' \ + '%s.' % (item.href, e)) return images - def dump_text(self, elem, stylizer, tag_stack=[]): + def dump_text(self, elem, stylizer, page, tag_stack=[]): if not isinstance(elem.tag, basestring) \ or namespace(elem.tag) != XHTML_NS: return u'' @@ -131,7 +150,10 @@ class FB2MLizer(object): tag_count = 0 if tag == 'img': - fb2_text += '' % os.path.basename(elem.attrib['src']) + if page.abshref(elem.attrib['src']) not in self.image_hrefs.keys(): + self.image_hrefs[page.abshref(elem.attrib['src'])] = '%s.jpg' % len(self.image_hrefs.keys()) + fb2_text += '' % self.image_hrefs[page.abshref(elem.attrib['src'])] + fb2_tag = TAG_MAP.get(tag, None) if fb2_tag and fb2_tag not in tag_stack: @@ -155,7 +177,7 @@ class FB2MLizer(object): fb2_text += prepare_string_for_xml(elem.text) for item in elem: - fb2_text += self.dump_text(item, stylizer, tag_stack) + fb2_text += self.dump_text(item, stylizer, page, tag_stack) close_tag_list = [] for i in range(0, tag_count): From a25d93dff2a8c50637d929c172a2582a9c0bbd9f Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 8 Aug 2009 15:31:40 -0400 Subject: [PATCH 3/3] Copy to clipboard button on message dialogs. --- src/calibre/ebooks/fb2/fb2ml.py | 1 - src/calibre/gui2/__init__.py | 26 +++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py index c77cb876f8..5d84c78e60 100644 --- a/src/calibre/ebooks/fb2/fb2ml.py +++ b/src/calibre/ebooks/fb2/fb2ml.py @@ -153,7 +153,6 @@ class FB2MLizer(object): if page.abshref(elem.attrib['src']) not in self.image_hrefs.keys(): self.image_hrefs[page.abshref(elem.attrib['src'])] = '%s.jpg' % len(self.image_hrefs.keys()) fb2_text += '' % self.image_hrefs[page.abshref(elem.attrib['src'])] - fb2_tag = TAG_MAP.get(tag, None) if fb2_tag and fb2_tag not in tag_stack: diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 68a582d42e..6a865ad109 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -5,7 +5,7 @@ import os from PyQt4.QtCore import QVariant, QFileInfo, QObject, SIGNAL, QBuffer, Qt, QSize, \ QByteArray, QUrl, QTranslator, QCoreApplication, QThread from PyQt4.QtGui import QFileDialog, QMessageBox, QPixmap, QFileIconProvider, \ - QIcon, QTableView, QApplication, QDialog + QIcon, QTableView, QApplication, QDialog, QPushButton ORG_NAME = 'KovidsBrain' APP_UID = 'libprs500' @@ -108,7 +108,11 @@ def warning_dialog(parent, title, msg, det_msg='', show=False): parent) d.setDetailedText(det_msg) d.setIconPixmap(QPixmap(':/images/dialog_warning.svg')) - QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) + clipboard_button = QPushButton(_('Copy to Clipboard')) + d.layout().addWidget(clipboard_button) + def copy_to_clipboard(): + QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) + d.connect(clipboard_button, SIGNAL('clicked()'), copy_to_clipboard) if show: return d.exec_() return d @@ -118,7 +122,11 @@ def error_dialog(parent, title, msg, det_msg='', show=False): parent) d.setDetailedText(det_msg) d.setIconPixmap(QPixmap(':/images/dialog_error.svg')) - QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) + clipboard_button = QPushButton(_('Copy to Clipboard')) + d.layout().addWidget(clipboard_button) + def copy_to_clipboard(): + QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) + d.connect(clipboard_button, SIGNAL('clicked()'), copy_to_clipboard) if show: return d.exec_() return d @@ -128,7 +136,11 @@ def question_dialog(parent, title, msg, det_msg=''): parent) d.setDetailedText(det_msg) d.setIconPixmap(QPixmap(':/images/dialog_information.svg')) - QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) + clipboard_button = QPushButton(_('Copy to Clipboard')) + d.layout().addWidget(clipboard_button) + def copy_to_clipboard(): + QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) + d.connect(clipboard_button, SIGNAL('clicked()'), copy_to_clipboard) return d.exec_() == QMessageBox.Yes def info_dialog(parent, title, msg, det_msg='', show=False): @@ -136,7 +148,11 @@ def info_dialog(parent, title, msg, det_msg='', show=False): parent) d.setDetailedText(det_msg) d.setIconPixmap(QPixmap(':/images/dialog_information.svg')) - QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) + clipboard_button = QPushButton(_('Copy to Clipboard')) + d.layout().addWidget(clipboard_button) + def copy_to_clipboard(): + QApplication.clipboard().setText('%s - %s: %s' % (title, msg, det_msg)) + d.connect(clipboard_button, SIGNAL('clicked()'), copy_to_clipboard) if show: return d.exec_() return d