From a30823d42a5084b9a64370176eb217ce0ef68991 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Tue, 15 Feb 2022 08:27:45 +0100 Subject: [PATCH] fixed #638 --- .../desktop/com.github.rssguard.appdata.xml | 2 +- src/librssguard/gui/webbrowser.cpp | 6 +- src/librssguard/network-web/readability.cpp | 80 ++++++++++++++++++- src/librssguard/network-web/readability.h | 8 ++ 4 files changed, 91 insertions(+), 5 deletions(-) diff --git a/resources/desktop/com.github.rssguard.appdata.xml b/resources/desktop/com.github.rssguard.appdata.xml index 8b034e804..b61e096bd 100644 --- a/resources/desktop/com.github.rssguard.appdata.xml +++ b/resources/desktop/com.github.rssguard.appdata.xml @@ -26,7 +26,7 @@ https://github.com/sponsors/martinrotter - + none diff --git a/src/librssguard/gui/webbrowser.cpp b/src/librssguard/gui/webbrowser.cpp index ddf54f9b6..bfbfd9ce5 100644 --- a/src/librssguard/gui/webbrowser.cpp +++ b/src/librssguard/gui/webbrowser.cpp @@ -38,7 +38,7 @@ WebBrowser::WebBrowser(QWidget* parent) : TabContent(parent), m_actionOpenInSystemBrowser(new QAction(qApp->icons()->fromTheme(QSL("document-open")), tr("Open this website in system web browser"), this)), - m_actionReadabilePage(new QAction(qApp->icons()->fromTheme(QSL("document-preview")), + m_actionReadabilePage(new QAction(qApp->icons()->fromTheme(QSL("text-html")), tr("View website in reader mode"), this)) { // Initialize the components and layout. @@ -223,7 +223,9 @@ void WebBrowser::onIconChanged(const QIcon& icon) { } void WebBrowser::setReadabledHtml(const QString& better_html) { - m_webView->setHtml(better_html, m_webView->url()); + if (!better_html.isEmpty()) { + m_webView->setHtml(better_html, m_webView->url()); + } } void WebBrowser::readabilityFailed(const QString& error) { diff --git a/src/librssguard/network-web/readability.cpp b/src/librssguard/network-web/readability.cpp index d4d5f6610..3c000ceff 100755 --- a/src/librssguard/network-web/readability.cpp +++ b/src/librssguard/network-web/readability.cpp @@ -2,12 +2,88 @@ #include "network-web/readability.h" +#include "3rd-party/boolinq/boolinq.h" +#include "gui/messagebox.h" #include "miscellaneous/application.h" -#include "miscellaneous/nodejs.h" -Readability::Readability(QObject* parent) : QObject{parent} {} +#define READABILITY_PACKAGE "@mozilla/readability" +#define READABILITY_VERSION "0.4.2" + +Readability::Readability(QObject* parent) : QObject{parent}, m_modulesInstalling(false), m_modulesInstalled(false) { + connect(qApp->nodejs(), &NodeJs::packageInstalledUpdated, this, &Readability::onPackageReady); + connect(qApp->nodejs(), &NodeJs::packageError, this, &Readability::onPackageError); +} + +void Readability::onPackageReady(const QList& pkgs, bool already_up_to_date) { + Q_UNUSED(already_up_to_date) + + bool concerns_readability = boolinq::from(pkgs).any([](const NodeJs::PackageMetadata& pkg) { + return pkg.m_name == QSL(READABILITY_PACKAGE); + }); + + if (!concerns_readability) { + return; + } + + m_modulesInstalled = true; + m_modulesInstalling = false; + + qApp->showGuiMessage(Notification::Event::NodePackageUpdated, + { tr("Packages for reader mode are installed"), + tr("You can now use reader mode!"), + QSystemTrayIcon::MessageIcon::Information }, + { true, true, false }); + + // Emit this just to allow readability again for user. + emit htmlReadabled({}); +} + +void Readability::onPackageError(const QList& pkgs, const QString& error) { + bool concerns_readability = boolinq::from(pkgs).any([](const NodeJs::PackageMetadata& pkg) { + return pkg.m_name == QSL(READABILITY_PACKAGE); + }); + + if (!concerns_readability) { + return; + } + + m_modulesInstalled = m_modulesInstalling = false; + + qApp->showGuiMessage(Notification::Event::NodePackageUpdated, + { tr("Packages for reader mode are NOT installed"), + tr("There is error: %1").arg(error), + QSystemTrayIcon::MessageIcon::Critical }, + { true, true, false }); + + // Emit this just to allow readability again for user. + emit htmlReadabled({}); +} void Readability::makeHtmlReadable(const QString& html, const QString& base_url) { + if (!m_modulesInstalled) { + NodeJs::PackageStatus st = qApp->nodejs()->packageStatus({ QSL(READABILITY_PACKAGE), QSL(READABILITY_VERSION) }); + + if (st != NodeJs::PackageStatus::UpToDate) { + if (!m_modulesInstalling) { + // We make sure to update modules. + m_modulesInstalling = true; + + qApp->showGuiMessage(Notification::Event::NodePackageUpdated, + { tr("Node.js libraries not installed"), + tr("%1 will now install some needed libraries, this will take only a few seconds. " + "You will be notified when installation is complete.").arg(QSL(APP_NAME)), + QSystemTrayIcon::MessageIcon::Information }, + { true, true, false }); + qApp->nodejs()->installUpdatePackages({ { QSL(READABILITY_PACKAGE), QSL(READABILITY_VERSION) } }); + } + + return; + } + else { + m_modulesInstalled = true; + } + } + QProcess* proc = new QProcess(this); connect(proc, QOverload::of(&QProcess::finished), this, &Readability::onReadabilityFinished); diff --git a/src/librssguard/network-web/readability.h b/src/librssguard/network-web/readability.h index d35680290..1ea0b0ded 100755 --- a/src/librssguard/network-web/readability.h +++ b/src/librssguard/network-web/readability.h @@ -5,6 +5,8 @@ #include +#include "miscellaneous/nodejs.h" + #include class Readability : public QObject { @@ -17,10 +19,16 @@ class Readability : public QObject { private slots: void onReadabilityFinished(int exit_code, QProcess::ExitStatus exit_status); + void onPackageReady(const QList& pkgs, bool already_up_to_date); + void onPackageError(const QList& pkgs, const QString& error); signals: void htmlReadabled(const QString& better_html); void errorOnHtmlReadabiliting(const QString& error); + + private: + bool m_modulesInstalling; + bool m_modulesInstalled; }; #endif // READABILITY_H