def add_empty_format_choose(self): if not self.is_ok_to_add_empty_formats(): return from calibre.ebooks.oeb.polish.create import valid_empty_formats from calibre.gui2.dialogs.choose_format import ChooseFormatDialog d = ChooseFormatDialog(self.gui, _('Choose format of empty file'), sorted(valid_empty_formats)) if d.exec_() != QDialog.DialogCode.Accepted or not d.format(): return self._add_empty_format(d.format())
def download_book(self, result): d = ChooseFormatDialog(self, _('Choose format to download to your library.'), result.downloads.keys()) if d.exec_() == d.Accepted: ext = d.format() fname = result.title[:60] + '.' + ext.lower() fname = ascii_filename(fname) self.gui.download_ebook(result.downloads[ext], filename=fname)
def view_specific_format(self, triggered): rows = list(self.gui.library_view.selectionModel().selectedRows()) if not rows or len(rows) == 0: d = error_dialog(self.gui, _('Cannot view'), _('No book selected')) d.exec_() return db = self.gui.library_view.model().db rows = [r.row() for r in rows] formats = [db.formats(row) for row in rows] formats = [list(f.upper().split(',')) if f else None for f in formats] all_fmts = set([]) for x in formats: if x: for f in x: all_fmts.add(f) if not all_fmts: error_dialog(self.gui, _('Format unavailable'), _('Selected books have no formats'), show=True) return d = ChooseFormatDialog(self.gui, _('Choose the format to view'), list(sorted(all_fmts))) if d.exec_() == d.Accepted: fmt = d.format() orig_num = len(rows) rows = [rows[i] for i in range(len(rows)) if formats[i] and fmt in formats[i]] if self._view_check(len(rows)): for row in rows: self.view_format(row, fmt) if len(rows) < orig_num: info_dialog(self.gui, _('Format unavailable'), _('Not all the selected books were available in' ' the %s format. You should convert' ' them first.')%fmt, show=True)
def download_book(self, result): d = ChooseFormatDialog(self, _('Choose format to download to your library.'), list(result.downloads.keys())) if d.exec_() == QDialog.DialogCode.Accepted: ext = d.format() fname = result.title[:60] + '.' + ext.lower() fname = ascii_filename(fname) show_download_info(result.title, parent=self) self.gui.download_ebook(result.downloads[ext], filename=fname, create_browser=result.create_browser)
def view_specific_format(self, triggered): rows = list(self.gui.library_view.selectionModel().selectedRows()) if not rows or len(rows) == 0: d = error_dialog(self.gui, _('Cannot view'), _('No book selected')) d.exec_() return db = self.gui.library_view.model().db rows = [r.row() for r in rows] book_ids = [db.id(r) for r in rows] formats = [[x.upper() for x in db.new_api.formats(book_id)] for book_id in book_ids] all_fmts = set() for x in formats: if x: for f in x: all_fmts.add(f) if not all_fmts: error_dialog(self.gui, _('Format unavailable'), _('Selected books have no formats'), show=True) return d = ChooseFormatDialog(self.gui, _('Choose the format to view'), list(sorted(all_fmts)), show_open_with=True) self.gui.book_converted.connect(d.book_converted) if d.exec_() == QDialog.DialogCode.Accepted: formats = [[x.upper() for x in db.new_api.formats(book_id)] for book_id in book_ids] fmt = d.format() orig_num = len(rows) rows = [ rows[i] for i in range(len(rows)) if formats[i] and fmt in formats[i] ] if self._view_check(len(rows)): for row in rows: if d.open_with_format is None: self.view_format(row, fmt) else: self.open_fmt_with(row, *d.open_with_format) if len(rows) < orig_num: info_dialog( self.gui, _('Format unavailable'), _('Not all the selected books were available in' ' the %s format. You should convert' ' them first.') % fmt, show=True) self.gui.book_converted.disconnect(d.book_converted)
def select_format(self, db, book_id): format = None formats = db.formats(book_id, index_is_id=True).upper().split(',') if len(formats) == 1: format = formats[0] elif len(formats) > 1: d = ChooseFormatDialog(self, _('Choose the format to view'), formats) d.exec_() if d.result() == QDialog.Accepted: format = d.format() else: return False if not format: error_dialog( self, _('No formats available'), _('Cannot build regex using the GUI builder without a book.'), show=True) return False try: fpath = db.format(book_id, format, index_is_id=True, as_path=True) except OSError: if iswindows: import traceback error_dialog( self, _('Could not open file'), _('Could not open the file, do you have it open in' ' another program?'), show=True, det_msg=traceback.format_exc()) return False raise try: self.open_book(fpath) finally: try: os.remove(fpath) except: # Fails on windows if the input plugin for this format keeps the file open # Happens for LIT files pass return True
def view_specific_format(self, triggered): rows = list(self.gui.library_view.selectionModel().selectedRows()) if not rows or len(rows) == 0: d = error_dialog(self.gui, _('Cannot view'), _('No book selected')) d.exec_() return db = self.gui.library_view.model().db rows = [r.row() for r in rows] book_ids = [db.id(r) for r in rows] formats = [[x.upper() for x in db.new_api.formats(book_id)] for book_id in book_ids] all_fmts = set([]) for x in formats: if x: for f in x: all_fmts.add(f) if not all_fmts: error_dialog(self.gui, _('Format unavailable'), _('Selected books have no formats'), show=True) return d = ChooseFormatDialog(self.gui, _('Choose the format to view'), list(sorted(all_fmts)), show_open_with=True) self.gui.book_converted.connect(d.book_converted) if d.exec_() == d.Accepted: formats = [[x.upper() for x in db.new_api.formats(book_id)] for book_id in book_ids] fmt = d.format() orig_num = len(rows) rows = [rows[i] for i in range(len(rows)) if formats[i] and fmt in formats[i]] if self._view_check(len(rows)): for row in rows: if d.open_with_format is None: self.view_format(row, fmt) else: self.open_fmt_with(row, *d.open_with_format) if len(rows) < orig_num: info_dialog(self.gui, _('Format unavailable'), _('Not all the selected books were available in' ' the %s format. You should convert' ' them first.')%fmt, show=True) self.gui.book_converted.disconnect(d.book_converted)
def select_format(self, db, book_id): format = None formats = db.formats(book_id, index_is_id=True).upper().split(',') if len(formats) == 1: format = formats[0] elif len(formats) > 1: d = ChooseFormatDialog(self, _('Choose the format to view'), formats) d.exec_() if d.result() == QDialog.Accepted: format = d.format() else: return False if not format: error_dialog(self, _('No formats available'), _('Cannot build regex using the GUI builder without a book.'), show=True) return False try: fpath = db.format(book_id, format, index_is_id=True, as_path=True) except OSError: if iswindows: import traceback error_dialog(self, _('Could not open file'), _('Could not open the file, do you have it open in' ' another program?'), show=True, det_msg=traceback.format_exc()) return False raise try: self.open_book(fpath) finally: try: os.remove(fpath) except: # Fails on windows if the input plugin for this format keeps the file open # Happens for LIT files pass return True