qBittorrent
rss_session.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  * Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
5  * Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * In addition, as a special exception, the copyright holders give permission to
22  * link this program with the OpenSSL project's "OpenSSL" library (or with
23  * modified versions of it that use the same license as the "OpenSSL" library),
24  * and distribute the linked executables. You must obey the GNU General Public
25  * License in all respects for all of the code used other than "OpenSSL". If you
26  * modify file(s), you may extend this exception to your version of the file(s),
27  * but you are not obligated to do so. If you do not wish to do so, delete this
28  * exception statement from your version.
29  */
30 
31 #pragma once
32 
33 /*
34  * RSS Session configuration file format (JSON):
35  *
36  * =============== BEGIN ===============
37  *
38  {
39  * "folder1":
40  {
41  * "subfolder1":
42  {
43  * "Feed name 1 (Alias)":
44  {
45  * "uid": "feed unique identifier",
46  * "url": "http://some-feed-url1"
47  * }
48  * "Feed name 2 (Alias)":
49  {
50  * "uid": "feed unique identifier",
51  * "url": "http://some-feed-url2"
52  * }
53  * },
54  * "subfolder2": {},
55  * "Feed name 3 (Alias)":
56  {
57  * "uid": "feed unique identifier",
58  * "url": "http://some-feed-url3"
59  * }
60  * },
61  * "folder2": {},
62  * "folder3": {}
63  * }
64  * ================ END ================
65  *
66  * 1. Document is JSON object (the same as Folder)
67  * 2. Folder is JSON object (keys are Item names, values are Items)
68  * 3. Feed is JSON object (keys are property names, values are property values; 'uid' and 'url' are required)
69  */
70 
71 #include <QHash>
72 #include <QObject>
73 #include <QPointer>
74 #include <QTimer>
75 
77 #include "base/settingvalue.h"
78 
79 class QThread;
80 
81 class Application;
82 class AsyncFileStorage;
83 
84 namespace RSS
85 {
86  class Feed;
87  class Folder;
88  class Item;
89 
90  class Session : public QObject
91  {
92  Q_OBJECT
93  Q_DISABLE_COPY_MOVE(Session)
94 
95  friend class ::Application;
96 
97  Session();
98  ~Session() override;
99 
100  public:
101  static Session *instance();
102 
103  bool isProcessingEnabled() const;
104  void setProcessingEnabled(bool enabled);
105 
106  QThread *workingThread() const;
109 
110  int maxArticlesPerFeed() const;
111  void setMaxArticlesPerFeed(int n);
112 
113  int refreshInterval() const;
115 
116  nonstd::expected<void, QString> addFolder(const QString &path);
117  nonstd::expected<void, QString> addFeed(const QString &url, const QString &path);
118  nonstd::expected<void, QString> moveItem(const QString &itemPath, const QString &destPath);
119  nonstd::expected<void, QString> moveItem(Item *item, const QString &destPath);
120  nonstd::expected<void, QString> removeItem(const QString &itemPath);
121 
122  QList<Item *> items() const;
123  Item *itemByPath(const QString &path) const;
124  QList<Feed *> feeds() const;
125  Feed *feedByURL(const QString &url) const;
126 
127  Folder *rootFolder() const;
128 
129  public slots:
130  void refresh();
131 
132  signals:
133  void processingStateChanged(bool enabled);
135  void itemAdded(Item *item);
136  void itemPathChanged(Item *item);
138  void feedIconLoaded(Feed *feed);
139  void feedStateChanged(Feed *feed);
140 
141  private slots:
143  void handleFeedTitleChanged(Feed *feed);
144 
145  private:
146  QUuid generateUID() const;
147  void load();
148  void loadFolder(const QJsonObject &jsonObj, Folder *folder);
149  void loadLegacy();
150  void store();
151  nonstd::expected<Folder *, QString> prepareItemDest(const QString &path);
152  Folder *addSubfolder(const QString &name, Folder *parentFolder);
153  Feed *addFeedToFolder(const QUuid &uid, const QString &url, const QString &name, Folder *parentFolder);
154  void addItem(Item *item, Folder *destFolder);
155 
156  static QPointer<Session> m_instance;
157 
161  QThread *m_workingThread;
165  QHash<QString, Item *> m_itemsByPath;
166  QHash<QUuid, Feed *> m_feedsByUID;
167  QHash<QString, Feed *> m_feedsByURL;
168  };
169 }
int refreshInterval() const
nonstd::expected< Folder *, QString > prepareItemDest(const QString &path)
void loadFolder(const QJsonObject &jsonObj, Folder *folder)
void handleFeedTitleChanged(Feed *feed)
bool isProcessingEnabled() const
void loadLegacy()
static Session * instance()
CachedSettingValue< int > m_storeRefreshInterval
Definition: rss_session.h:159
QTimer m_refreshTimer
Definition: rss_session.h:164
QThread * workingThread() const
QHash< QString, Item * > m_itemsByPath
Definition: rss_session.h:165
Feed * addFeedToFolder(const QUuid &uid, const QString &url, const QString &name, Folder *parentFolder)
AsyncFileStorage * m_dataFileStorage
Definition: rss_session.h:163
void itemPathChanged(Item *item)
QList< Feed * > feeds() const
AsyncFileStorage * dataFileStorage() const
void handleItemAboutToBeDestroyed(Item *item)
nonstd::expected< void, QString > addFolder(const QString &path)
void maxArticlesPerFeedChanged(int n)
Feed * feedByURL(const QString &url) const
CachedSettingValue< bool > m_storeProcessingEnabled
Definition: rss_session.h:158
CachedSettingValue< int > m_storeMaxArticlesPerFeed
Definition: rss_session.h:160
int maxArticlesPerFeed() const
Folder * rootFolder() const
nonstd::expected< void, QString > addFeed(const QString &url, const QString &path)
Folder * addSubfolder(const QString &name, Folder *parentFolder)
void feedStateChanged(Feed *feed)
QUuid generateUID() const
AsyncFileStorage * confFileStorage() const
void setRefreshInterval(int refreshInterval)
Item * itemByPath(const QString &path) const
QThread * m_workingThread
Definition: rss_session.h:161
void setMaxArticlesPerFeed(int n)
nonstd::expected< void, QString > moveItem(const QString &itemPath, const QString &destPath)
void processingStateChanged(bool enabled)
AsyncFileStorage * m_confFileStorage
Definition: rss_session.h:162
QList< Item * > items() const
void itemAdded(Item *item)
nonstd::expected< void, QString > removeItem(const QString &itemPath)
static QPointer< Session > m_instance
Definition: rss_session.h:156
QHash< QUuid, Feed * > m_feedsByUID
Definition: rss_session.h:166
void feedIconLoaded(Feed *feed)
void addItem(Item *item, Folder *destFolder)
QHash< QString, Feed * > m_feedsByURL
Definition: rss_session.h:167
void itemAboutToBeRemoved(Item *item)
void setProcessingEnabled(bool enabled)