From 51ecd372f24dda20b1247871f7058a649a1eab1e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 24 Mar 2020 09:35:01 +0530 Subject: [PATCH] Add a button to download custom recipes to the dialog used for creating recipes. Fixes #1868209 [[Enhancement] Add a Download news button](https://bugs.launchpad.net/calibre/+bug/1868209) --- src/calibre/db/adding.py | 4 ++-- src/calibre/gui2/actions/fetch_news.py | 15 +++++++++++++++ src/calibre/gui2/dialogs/custom_recipes.py | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/calibre/db/adding.py b/src/calibre/db/adding.py index ef5f7594f5..eea53810e1 100644 --- a/src/calibre/db/adding.py +++ b/src/calibre/db/adding.py @@ -281,9 +281,9 @@ def add_news(cache, path, arg, dbapi=None): if mi.series_index is None: mi.series_index = cache._get_next_series_num_for(mi.series) mi.tags = [_('News')] - if arg['add_title_tag']: + if arg.get('add_title_tag'): mi.tags += [arg['title']] - if arg['custom_tags']: + if arg.get('custom_tags'): mi.tags += arg['custom_tags'] if mi.pubdate is None: mi.pubdate = utcnow() diff --git a/src/calibre/gui2/actions/fetch_news.py b/src/calibre/gui2/actions/fetch_news.py index 5aeeb5c3ba..eabc5a9869 100644 --- a/src/calibre/gui2/actions/fetch_news.py +++ b/src/calibre/gui2/actions/fetch_news.py @@ -48,6 +48,21 @@ class FetchNewsAction(InterfaceAction): self.gui.library_view.model().delete_books_by_id, type=Qt.QueuedConnection) + def download_custom_recipe(self, title, urn): + arg = {'title': title, 'urn': urn, 'username': None, 'password': None} + func, args, desc, fmt, temp_files = fetch_scheduled_recipe(arg) + job = self.gui.job_manager.run_job( + Dispatcher(self.custom_recipe_fetched), func, args=args, description=desc) + self.conversion_jobs[job] = (temp_files, fmt, arg) + self.gui.status_bar.show_message(_('Fetching news from ')+arg['title'], 2000) + + def custom_recipe_fetched(self, job): + temp_files, fmt, arg = self.conversion_jobs.pop(job) + fname = temp_files[0].name + if job.failed: + return self.gui.job_exception(job) + self.gui.library_view.model().add_news(fname, arg) + def download_scheduled_recipe(self, arg): func, args, desc, fmt, temp_files = \ fetch_scheduled_recipe(arg) diff --git a/src/calibre/gui2/dialogs/custom_recipes.py b/src/calibre/gui2/dialogs/custom_recipes.py index 1b62415154..efb53b9974 100644 --- a/src/calibre/gui2/dialogs/custom_recipes.py +++ b/src/calibre/gui2/dialogs/custom_recipes.py @@ -38,6 +38,11 @@ class CustomRecipeModel(QAbstractListModel): # {{{ if row > -1 and row < self.rowCount(): return self.recipe_model.custom_recipe_collection[row].get('title', '') + def urn(self, index): + row = index.row() + if row > -1 and row < self.rowCount(): + return self.recipe_model.custom_recipe_collection[row].get('id') + def has_title(self, title): for x in self.recipe_model.custom_recipe_collection: if x.get('title', False) == title: @@ -194,6 +199,10 @@ class RecipeList(QWidget): # {{{ b.clicked.connect(self.remove) b.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) l.addWidget(b) + self.download_button = b = QPushButton(QIcon(I('download-metadata.png')), _('&Download this recipe'), w) + b.clicked.connect(self.download) + b.setSizePolicy(QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)) + l.addWidget(b) self.select_row() v.selectionModel().currentRowChanged.connect(self.recipe_selected) @@ -247,6 +256,15 @@ class RecipeList(QWidget): # {{{ if self.model.rowCount() == 0: self.stacks.setCurrentIndex(0) + def download(self): + idx = self.view.currentIndex() + if idx.isValid(): + urn = self.model.urn(idx) + title = self.model.title(idx) + from calibre.gui2.ui import get_gui + gui = get_gui() + gui.iactions['Fetch News'].download_custom_recipe(title, urn) + def has_title(self, title): return self.model.has_title(title)