qBittorrent
rss_autodownloader.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2017 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 #pragma once
30 
31 #include <QBasicTimer>
32 #include <QHash>
33 #include <QList>
34 #include <QObject>
35 #include <QPointer>
36 #include <QRegularExpression>
37 #include <QSharedPointer>
38 
39 #include "base/exceptions.h"
40 #include "base/settingvalue.h"
41 
42 class QThread;
43 class QTimer;
44 
45 class Application;
46 class AsyncFileStorage;
47 struct ProcessingJob;
48 
49 namespace RSS
50 {
51  class Article;
52  class Feed;
53  class Item;
54 
55  class AutoDownloadRule;
56 
57  class ParsingError : public RuntimeError
58  {
59  public:
60  using RuntimeError::RuntimeError;
61  };
62 
63  class AutoDownloader final : public QObject
64  {
65  Q_OBJECT
66  Q_DISABLE_COPY_MOVE(AutoDownloader)
67 
68  friend class ::Application;
69 
71  ~AutoDownloader() override;
72 
73  public:
74  enum class RulesFileFormat
75  {
76  Legacy,
77  JSON
78  };
79 
80  static AutoDownloader *instance();
81 
82  bool isProcessingEnabled() const;
83  void setProcessingEnabled(bool enabled);
84 
85  QStringList smartEpisodeFilters() const;
86  void setSmartEpisodeFilters(const QStringList &filters);
87  QRegularExpression smartEpisodeRegex() const;
88 
89  bool downloadRepacks() const;
90  void setDownloadRepacks(bool enabled);
91 
92  bool hasRule(const QString &ruleName) const;
93  AutoDownloadRule ruleByName(const QString &ruleName) const;
94  QList<AutoDownloadRule> rules() const;
95 
96  void insertRule(const AutoDownloadRule &rule);
97  bool renameRule(const QString &ruleName, const QString &newRuleName);
98  void removeRule(const QString &ruleName);
99 
100  QByteArray exportRules(RulesFileFormat format = RulesFileFormat::JSON) const;
101  void importRules(const QByteArray &data, RulesFileFormat format = RulesFileFormat::JSON);
102 
103  signals:
104  void processingStateChanged(bool enabled);
105  void ruleAdded(const QString &ruleName);
106  void ruleChanged(const QString &ruleName);
107  void ruleRenamed(const QString &ruleName, const QString &oldRuleName);
108  void ruleAboutToBeRemoved(const QString &ruleName);
109 
110  private slots:
111  void process();
112  void handleTorrentDownloadFinished(const QString &url);
113  void handleTorrentDownloadFailed(const QString &url);
114  void handleNewArticle(const Article *article);
115 
116  private:
117  void timerEvent(QTimerEvent *event) override;
118  void setRule_impl(const AutoDownloadRule &rule);
119  void resetProcessingQueue();
120  void startProcessing();
121  void addJobForArticle(const Article *article);
122  void processJob(const QSharedPointer<ProcessingJob> &job);
123  void load();
124  void loadRules(const QByteArray &data);
125  void loadRulesLegacy();
126  void store();
127  void storeDeferred();
128  QByteArray exportRulesToJSONFormat() const;
129  void importRulesFromJSONFormat(const QByteArray &data);
130  QByteArray exportRulesToLegacyFormat() const;
131  void importRulesFromLegacyFormat(const QByteArray &data);
132 
133  static QPointer<AutoDownloader> m_instance;
134 
138 
140  QThread *m_ioThread;
142  QHash<QString, AutoDownloadRule> m_rules;
143  QList<QSharedPointer<ProcessingJob>> m_processingQueue;
144  QHash<QString, QSharedPointer<ProcessingJob>> m_waitingJobs;
145  bool m_dirty = false;
146  QBasicTimer m_savingTimer;
147  QRegularExpression m_smartEpisodeRegex;
148  };
149 }
Definition: rss_article.h:43
QByteArray exportRulesToJSONFormat() const
void importRulesFromLegacyFormat(const QByteArray &data)
CachedSettingValue< bool > m_storeProcessingEnabled
void ruleChanged(const QString &ruleName)
void setDownloadRepacks(bool enabled)
bool isProcessingEnabled() const
bool hasRule(const QString &ruleName) const
QList< QSharedPointer< ProcessingJob > > m_processingQueue
QList< AutoDownloadRule > rules() const
QByteArray exportRulesToLegacyFormat() const
void setRule_impl(const AutoDownloadRule &rule)
QHash< QString, AutoDownloadRule > m_rules
AsyncFileStorage * m_fileStorage
SettingValue< bool > m_storeDownloadRepacks
void setSmartEpisodeFilters(const QStringList &filters)
void removeRule(const QString &ruleName)
static QPointer< AutoDownloader > m_instance
void processingStateChanged(bool enabled)
SettingValue< QVariant > m_storeSmartEpisodeFilter
void handleTorrentDownloadFinished(const QString &url)
bool renameRule(const QString &ruleName, const QString &newRuleName)
void processJob(const QSharedPointer< ProcessingJob > &job)
static AutoDownloader * instance()
void ruleAdded(const QString &ruleName)
void timerEvent(QTimerEvent *event) override
void ruleAboutToBeRemoved(const QString &ruleName)
QHash< QString, QSharedPointer< ProcessingJob > > m_waitingJobs
void loadRules(const QByteArray &data)
void ruleRenamed(const QString &ruleName, const QString &oldRuleName)
AutoDownloadRule ruleByName(const QString &ruleName) const
QByteArray exportRules(RulesFileFormat format=RulesFileFormat::JSON) const
void handleNewArticle(const Article *article)
QRegularExpression smartEpisodeRegex() const
QStringList smartEpisodeFilters() const
void addJobForArticle(const Article *article)
void insertRule(const AutoDownloadRule &rule)
void setProcessingEnabled(bool enabled)
QRegularExpression m_smartEpisodeRegex
void handleTorrentDownloadFailed(const QString &url)
void importRules(const QByteArray &data, RulesFileFormat format=RulesFileFormat::JSON)
void importRulesFromJSONFormat(const QByteArray &data)