qBittorrent
rsscontroller.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "rsscontroller.h"
30 
31 #include <QJsonArray>
32 #include <QJsonDocument>
33 #include <QJsonObject>
34 #include <QJsonValue>
35 #include <QVector>
36 
37 #include "base/rss/rss_article.h"
40 #include "base/rss/rss_feed.h"
41 #include "base/rss/rss_folder.h"
42 #include "base/rss/rss_session.h"
43 #include "base/utils/string.h"
44 #include "apierror.h"
45 
47 
49 {
50  requireParams({"path"});
51 
52  const QString path = params()["path"].trimmed();
53  const nonstd::expected<void, QString> result = RSS::Session::instance()->addFolder(path);
54  if (!result)
55  throw APIError(APIErrorType::Conflict, result.error());
56 }
57 
59 {
60  requireParams({"url", "path"});
61 
62  const QString url = params()["url"].trimmed();
63  const QString path = params()["path"].trimmed();
64  const nonstd::expected<void, QString> result = RSS::Session::instance()->addFeed(url, (path.isEmpty() ? url : path));
65  if (!result)
66  throw APIError(APIErrorType::Conflict, result.error());
67 }
68 
70 {
71  requireParams({"path"});
72 
73  const QString path = params()["path"].trimmed();
74  const nonstd::expected<void, QString> result = RSS::Session::instance()->removeItem(path);
75  if (!result)
76  throw APIError(APIErrorType::Conflict, result.error());
77 }
78 
80 {
81  requireParams({"itemPath", "destPath"});
82 
83  const QString itemPath = params()["itemPath"].trimmed();
84  const QString destPath = params()["destPath"].trimmed();
85  const nonstd::expected<void, QString> result = RSS::Session::instance()->moveItem(itemPath, destPath);
86  if (!result)
87  throw APIError(APIErrorType::Conflict, result.error());
88 }
89 
91 {
92  const bool withData {parseBool(params()["withData"]).value_or(false)};
93 
94  const auto jsonVal = RSS::Session::instance()->rootFolder()->toJsonValue(withData);
95  setResult(jsonVal.toObject());
96 }
97 
99 {
100  requireParams({"itemPath"});
101 
102  const QString itemPath {params()["itemPath"]};
103  const QString articleId {params()["articleId"]};
104 
105  RSS::Item *item = RSS::Session::instance()->itemByPath(itemPath);
106  if (!item) return;
107 
108  if (!articleId.isNull())
109  {
110  RSS::Feed *feed = qobject_cast<RSS::Feed *>(item);
111  if (feed)
112  {
113  RSS::Article *article = feed->articleByGUID(articleId);
114  if (article)
115  article->markAsRead();
116  }
117  }
118  else
119  {
120  item->markAsRead();
121  }
122 }
123 
125 {
126  requireParams({"itemPath"});
127 
128  const QString itemPath {params()["itemPath"]};
129  RSS::Item *item = RSS::Session::instance()->itemByPath(itemPath);
130  if (item)
131  item->refresh();
132 }
133 
135 {
136  requireParams({"ruleName", "ruleDef"});
137 
138  const QString ruleName {params()["ruleName"].trimmed()};
139  const QByteArray ruleDef {params()["ruleDef"].trimmed().toUtf8()};
140 
141  const auto jsonObj = QJsonDocument::fromJson(ruleDef).object();
143 }
144 
146 {
147  requireParams({"ruleName", "newRuleName"});
148 
149  const QString ruleName {params()["ruleName"].trimmed()};
150  const QString newRuleName {params()["newRuleName"].trimmed()};
151 
152  RSS::AutoDownloader::instance()->renameRule(ruleName, newRuleName);
153 }
154 
156 {
157  requireParams({"ruleName"});
158 
159  const QString ruleName {params()["ruleName"].trimmed()};
161 }
162 
164 {
165  const QList<RSS::AutoDownloadRule> rules {RSS::AutoDownloader::instance()->rules()};
166  QJsonObject jsonObj;
167  for (const auto &rule : rules)
168  jsonObj.insert(rule.name(), rule.toJsonObject());
169 
170  setResult(jsonObj);
171 }
172 
174 {
175  requireParams({"ruleName"});
176 
177  const QString ruleName {params()["ruleName"]};
179 
180  QJsonObject jsonObj;
181  for (const QString &feedURL : rule.feedURLs())
182  {
183  const RSS::Feed *feed = RSS::Session::instance()->feedByURL(feedURL);
184  if (!feed) continue; // feed doesn't exist
185 
186  QJsonArray matchingArticles;
187  for (const RSS::Article *article : feed->articles())
188  {
189  if (rule.matches(article->data()))
190  matchingArticles << article->title();
191  }
192  if (!matchingArticles.isEmpty())
193  jsonObj.insert(feed->name(), matchingArticles);
194  }
195 
196  setResult(jsonObj);
197 }
void requireParams(const QVector< QString > &requiredParams) const
const StringMap & params() const
void setResult(const QString &result)
Definition: rss_article.h:43
void markAsRead()
static AutoDownloadRule fromJsonObject(const QJsonObject &jsonObj, const QString &name="")
QStringList feedURLs() const
bool matches(const QVariantHash &articleData) const
QList< AutoDownloadRule > rules() const
void removeRule(const QString &ruleName)
bool renameRule(const QString &ruleName, const QString &newRuleName)
static AutoDownloader * instance()
AutoDownloadRule ruleByName(const QString &ruleName) const
void insertRule(const AutoDownloadRule &rule)
Article * articleByGUID(const QString &guid) const
Definition: rss_feed.cpp:179
QList< Article * > articles() const override
Definition: rss_feed.cpp:105
QString title() const
Definition: rss_feed.cpp:159
QJsonValue toJsonValue(bool withData=false) const override
Definition: rss_folder.cpp:99
virtual void markAsRead()=0
QString name() const
Definition: rss_item.cpp:62
virtual void refresh()=0
static Session * instance()
nonstd::expected< void, QString > addFolder(const QString &path)
Feed * feedByURL(const QString &url) const
Folder * rootFolder() const
nonstd::expected< void, QString > addFeed(const QString &url, const QString &path)
Item * itemByPath(const QString &path) const
nonstd::expected< void, QString > moveItem(const QString &itemPath, const QString &destPath)
nonstd::expected< void, QString > removeItem(const QString &itemPath)
void moveItemAction()
void removeRuleAction()
void markAsReadAction()
void addFolderAction()
void addFeedAction()
void removeItemAction()
void renameRuleAction()
void matchingArticlesAction()
void refreshItemAction()
std::optional< bool > parseBool(const QString &string)
Definition: string.cpp:72