Debian Codegraph @

src/librssguard/network-web/readability.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "network-web/readability.h"
4
5 #include "3rd-party/boolinq/boolinq.h"
6 #include "exceptions/applicationexception.h"
7 #include "miscellaneous/application.h"
8
9 #include <QDir>
10
11 #define READABILITY_PACKAGE "@mozilla/readability"
12 #define READABILITY_VERSION "0.5.0"
13
14 #define JSDOM_PACKAGE "jsdom"
15 #define JSDOM_VERSION "24.0.0"
16
17 Readability::Readability(QObject* parent) : QObject{parent}, m_modulesInstalling(false), m_modulesInstalled(false) {
18 connect(qApp->nodejs(), &NodeJs::packageInstalledUpdated, this, &Readability::onPackageReady);
19 connect(qApp->nodejs(), &NodeJs::packageError, this, &Readability::onPackageError);
20 }
21
22 void Readability::onPackageReady(const QObject* sndr,
23 const QList<NodeJs::PackageMetadata>& pkgs,
24 bool already_up_to_date) {
25 Q_UNUSED(already_up_to_date)
26
27 bool concerns_readability = boolinq::from(pkgs).any([](const NodeJs::PackageMetadata& pkg) {
28 return pkg.m_name == QSL(READABILITY_PACKAGE);
29 });
30
31 if (!concerns_readability) {
32 return;
33 }
34
35 m_modulesInstalled = true;
36 m_modulesInstalling = false;
37
38 qApp->showGuiMessage(Notification::Event::NodePackageUpdated,
39 {tr("Packages for reader mode are installed"),
40 tr("Reload your webpage and then you can use reader mode!"),
41 QSystemTrayIcon::MessageIcon::Information},
42 {true, true, false});
43
44 // Emit this just to allow readability again for user.
45 emit errorOnHtmlReadabiliting(sndr, tr("Packages for reader mode are installed. You can now use reader mode!"));
46 }
47
48 void Readability::onPackageError(const QObject* sndr,
49 const QList<NodeJs::PackageMetadata>& pkgs,
50 const QString& error) {
51 bool concerns_readability = boolinq::from(pkgs).any([](const NodeJs::PackageMetadata& pkg) {
52 return pkg.m_name == QSL(READABILITY_PACKAGE);
53 });
54
55 if (!concerns_readability) {
56 return;
57 }
58
59 m_modulesInstalled = m_modulesInstalling = false;
60
61 qApp->showGuiMessage(Notification::Event::NodePackageUpdated,
62 {tr("Packages for reader mode are NOT installed"),
63 tr("There is error: %1").arg(error),
64 QSystemTrayIcon::MessageIcon::Critical},
65 {true, true, false});
66
67 // Emit this just to allow readability again for user.
68 emit errorOnHtmlReadabiliting(sndr, tr("Packages for reader mode are NOT installed. There is error: %1").arg(error));
69 }
70
71 void Readability::makeHtmlReadable(QObject* sndr, const QString& html, const QString& base_url) {
72 if (!m_modulesInstalled) {
73 try {
74 NodeJs::PackageStatus st_readability =
75 qApp->nodejs()->packageStatus({QSL(READABILITY_PACKAGE), QSL(READABILITY_VERSION)});
76 NodeJs::PackageStatus st_jsdom = qApp->nodejs()->packageStatus({QSL(JSDOM_PACKAGE), QSL(JSDOM_VERSION)});
77
78 if (st_readability != NodeJs::PackageStatus::UpToDate || st_jsdom != NodeJs::PackageStatus::UpToDate) {
79 if (!m_modulesInstalling) {
80 // We make sure to update modules.
81 m_modulesInstalling = true;
82 qApp->nodejs()->installUpdatePackages(sndr,
83 {{QSL(READABILITY_PACKAGE), QSL(READABILITY_VERSION)},
84 {QSL(JSDOM_PACKAGE), QSL(JSDOM_VERSION)}});
85 }
86
87 return;
88 }
89 else {
90 m_modulesInstalled = true;
91 }
92 }
93 catch (const ApplicationException& ex) {
94 qApp->showGuiMessage(Notification::Event::NodePackageUpdated,
95 {tr("Node.js libraries not installed"),
96 tr("Node.js is not configured properly. Go to \"Settings\" -> \"Node.js\" and check "
97 "if your Node.js is properly configured."),
98 QSystemTrayIcon::MessageIcon::Critical},
99 {true, true, false});
100
101 qCriticalNN << LOGSEC_CORE << "Failed to check for Node.js package status:" << QUOTE_W_SPACE_DOT(ex.message());
102
103 // Emit this just to allow readability again for user.
104 emit
105 errorOnHtmlReadabiliting(sndr,
106 tr("Node.js is not configured properly. Go to \"Settings\" -> \"Node.js\" and check "
107 "if your Node.js is properly configured."));
108 }
109 }
110
111 QString temp_script =
112 QDir::toNativeSeparators(IOFactory::getSystemFolder(QStandardPaths::StandardLocation::TempLocation)) +
113 QDir::separator() + QSL("readabilize-article.js");
114
115 if (!IOFactory::copyFile(QSL(":/scripts/readability/readabilize-article.js"), temp_script)) {
116 qWarningNN << LOGSEC_ADBLOCK << "Failed to copy Readability script to TEMP.";
117 }
118
119 QProcess* proc = new QProcess(this);
120
121 connect(proc,
122 QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
123 this,
124 [=](int exit_code, QProcess::ExitStatus exit_status) {
125 onReadabilityFinished(sndr, exit_code, exit_status);
126 });
127
128 qApp->nodejs()->runScript(proc, temp_script, {base_url});
129
130 proc->write(html.toUtf8());
131 proc->closeWriteChannel();
132 }
133
134 void Readability::onReadabilityFinished(QObject* sndr, int exit_code, QProcess::ExitStatus exit_status) {
135 QProcess* proc = qobject_cast<QProcess*>(sender());
136
137 if (exit_status == QProcess::ExitStatus::NormalExit && exit_code == EXIT_SUCCESS) {
138 emit htmlReadabled(sndr, QString::fromUtf8(proc->readAllStandardOutput()));
139 }
140 else {
141 QString err = QString::fromUtf8(proc->readAllStandardError());
142 emit errorOnHtmlReadabiliting(sndr, err);
143 }
144
145 proc->deleteLater();
146 }