qBittorrent
rss_folder.cpp
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 #include "rss_folder.h"
32 
33 #include <algorithm>
34 
35 #include <QJsonObject>
36 #include <QJsonValue>
37 
38 #include "base/global.h"
39 #include "rss_article.h"
40 
41 using namespace RSS;
42 
43 Folder::Folder(const QString &path)
44  : Item(path)
45 {
46 }
47 
49 {
50  emit aboutToBeDestroyed(this);
51 
52  for (auto item : asConst(items()))
53  delete item;
54 }
55 
56 QList<Article *> Folder::articles() const
57 {
58  QList<Article *> news;
59 
60  for (Item *item : asConst(items()))
61  {
62  int n = news.size();
63  news << item->articles();
64  std::inplace_merge(news.begin(), news.begin() + n, news.end()
65  , [](Article *a1, Article *a2)
66  {
67  return Article::articleDateRecentThan(a1, a2->date());
68  });
69  }
70  return news;
71 }
72 
74 {
75  const auto itemList = items();
76  return std::accumulate(itemList.cbegin(), itemList.cend(), 0, [](const int acc, const Item *item)
77  {
78  return (acc + item->unreadCount());
79  });
80 }
81 
83 {
84  for (Item *item : asConst(items()))
85  item->markAsRead();
86 }
87 
89 {
90  for (Item *item : asConst(items()))
91  item->refresh();
92 }
93 
94 QList<Item *> Folder::items() const
95 {
96  return m_items;
97 }
98 
99 QJsonValue Folder::toJsonValue(bool withData) const
100 {
101  QJsonObject jsonObj;
102  for (Item *item : asConst(items()))
103  jsonObj.insert(item->name(), item->toJsonValue(withData));
104 
105  return jsonObj;
106 }
107 
109 {
110  emit unreadCountChanged(this);
111 }
112 
114 {
115  for (Item *item : asConst(items()))
116  item->cleanup();
117 }
118 
120 {
121  Q_ASSERT(item);
122  Q_ASSERT(!m_items.contains(item));
123 
124  m_items.append(item);
125  connect(item, &Item::newArticle, this, &Item::newArticle);
126  connect(item, &Item::articleRead, this, &Item::articleRead);
129 
130  for (auto article : asConst(item->articles()))
131  emit newArticle(article);
132 
133  if (item->unreadCount() > 0)
134  emit unreadCountChanged(this);
135 }
136 
138 {
139  Q_ASSERT(m_items.contains(item));
140 
141  for (auto article : asConst(item->articles()))
142  emit articleAboutToBeRemoved(article);
143 
144  item->disconnect(this);
145  m_items.removeOne(item);
146  if (item->unreadCount() > 0)
147  emit unreadCountChanged(this);
148 }
Definition: rss_article.h:43
void cleanup() override
Definition: rss_folder.cpp:113
~Folder() override
Definition: rss_folder.cpp:48
void markAsRead() override
Definition: rss_folder.cpp:82
void addItem(Item *item)
Definition: rss_folder.cpp:119
void removeItem(Item *item)
Definition: rss_folder.cpp:137
QList< Article * > articles() const override
Definition: rss_folder.cpp:56
void refresh() override
Definition: rss_folder.cpp:88
QList< Item * > items() const
Definition: rss_folder.cpp:94
void handleItemUnreadCountChanged()
Definition: rss_folder.cpp:108
QJsonValue toJsonValue(bool withData=false) const override
Definition: rss_folder.cpp:99
QList< Item * > m_items
Definition: rss_folder.h:68
int unreadCount() const override
Definition: rss_folder.cpp:73
void newArticle(Article *article)
void articleAboutToBeRemoved(Article *article)
virtual QList< Article * > articles() const =0
void aboutToBeDestroyed(Item *item=nullptr)
friend class Folder
Definition: rss_item.h:47
void unreadCountChanged(Item *item=nullptr)
void articleRead(Article *article)
virtual int unreadCount() const =0
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42