From 8398d4a7d706e6958d8bf112e60c12eecbd0fa4c Mon Sep 17 00:00:00 2001 From: John Schember Date: Tue, 19 May 2009 20:23:40 -0400 Subject: [PATCH] Auto convert when syncing news. --- src/calibre/gui2/device.py | 35 ++++++++++++++++++++++++++------ src/calibre/gui2/main.py | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index b176c25062..caed0358cc 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -640,12 +640,33 @@ class DeviceGUI(object): ', '.join(sent_mails), 3000) - def sync_news(self): + def sync_news(self, send_ids=None, do_auto=True): if self.device_connected: - ids = list(dynamic.get('news_to_be_synced', set([]))) + ids = list(dynamic.get('news_to_be_synced', set([]))) if send_ids is None else send_ids ids = [id for id in ids if self.library_view.model().db.has_id(id)] - files, auto = self.library_view.model().get_preferred_formats_from_ids( - ids, self.device_manager.device_class.settings().format_map) + files, _auto_ids = self.library_view.model().get_preferred_formats_from_ids( + ids, self.device_manager.device_class.settings().format_map, + exclude_auto=do_auto) + auto = [] + if _auto_ids: + for id in _auto_ids: + formats = [f.lower() for f in self.library_view.model().db.formats(id, index_is_id=True).split(',')] + formats = formats if formats != None else [] + if list(set(formats).intersection(available_input_formats())) != [] and list(set(self.device_manager.device_class.settings().format_map).intersection(available_output_formats())) != []: + auto.append(id) + if auto != []: + format = None + for fmt in self.device_manager.device_class.settings().format_map: + if fmt in list(set(self.device_manager.device_class.settings().format_map).intersection(set(available_output_formats()))): + format = fmt + break + if format is not None: + autos = [self.library_view.model().db.title(id, index_is_id=True) for id in auto] + autos = '\n'.join('%s'%i for i in autos) + info_dialog(self, _('No suitable formats'), + _('Auto converting the following books before uploading to ' + 'the device:'), det_msg=autos, show=True) + self.auto_convert_news(auto, format) files = [f for f in files if f is not None] if not files: dynamic.set('news_to_be_synced', set([])) @@ -667,8 +688,10 @@ class DeviceGUI(object): if config['upload_news_to_device'] and files: remove = ids if \ config['delete_news_from_library_on_upload'] else [] - on_card = self.location_view.model().free[0] < \ - self.location_view.model().free[1] + space = { self.location_view.model().free[0] : 'main', + self.location_view.model().free[1] : 'carda', + self.location_view.model().free[2] : 'cardb' } + on_card = space.get(sorted(space.keys(), reverse=True)[0], 'main') self.upload_books(files, names, metadata, on_card=on_card, memory=[[f.name for f in files], remove]) diff --git a/src/calibre/gui2/main.py b/src/calibre/gui2/main.py index f50bffbb76..7f36a9560c 100644 --- a/src/calibre/gui2/main.py +++ b/src/calibre/gui2/main.py @@ -1080,6 +1080,24 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): current = self.library_view.currentIndex() self.library_view.model().current_changed(current, previous) + def auto_convert_news(self, book_ids, format): + previous = self.library_view.currentIndex() + rows = [x.row() for x in \ + self.library_view.selectionModel().selectedRows()] + jobs, changed, bad = convert_single_ebook(self, self.library_view.model().db, book_ids, True, format) + if jobs == []: return + for func, args, desc, fmt, id, temp_files in jobs: + if id not in bad: + job = self.job_manager.run_job(Dispatcher(self.book_auto_converted_news), + func, args=args, description=desc) + self.conversion_jobs[job] = (temp_files, fmt, id) + + if changed: + self.library_view.model().refresh_rows(rows) + current = self.library_view.currentIndex() + self.library_view.model().current_changed(current, previous) + + def get_books_for_conversion(self): rows = [r.row() for r in \ self.library_view.selectionModel().selectedRows()] @@ -1175,6 +1193,29 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): self.send_by_mail(to, fmts, delete_from_library, specific_format=fmt, send_ids=[book_id], do_auto_convert=False) + def book_auto_converted_news(self, job): + temp_files, fmt, book_id = self.conversion_jobs.pop(job) + try: + if job.failed: + return self.job_exception(job) + data = open(temp_files[0].name, 'rb') + self.library_view.model().db.add_format(book_id, fmt, data, index_is_id=True) + data.close() + self.status_bar.showMessage(job.description + (' completed'), 2000) + finally: + for f in temp_files: + try: + if os.path.exists(f.name): + os.remove(f.name) + except: + pass + self.tags_view.recount() + if self.current_view() is self.library_view: + current = self.library_view.currentIndex() + self.library_view.model().current_changed(current, QModelIndex()) + + self.sync_news(send_ids=[book_id], do_auto_convert=False) + def book_converted(self, job): temp_files, fmt, book_id = self.conversion_jobs.pop(job) try: