qBittorrent
torrentfilter.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2014 Vladimir Golovnev <[email protected]>
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 "torrentfilter.h"
30 
31 #include "bittorrent/infohash.h"
32 #include "bittorrent/torrent.h"
33 
34 const QString TorrentFilter::AnyCategory;
36 const QString TorrentFilter::AnyTag;
37 
50 
52 
53 TorrentFilter::TorrentFilter(const Type type, const TorrentIDSet &idSet, const QString &category, const QString &tag)
54  : m_type(type)
55  , m_category(category)
56  , m_tag(tag)
57  , m_idSet(idSet)
58 {
59 }
60 
61 TorrentFilter::TorrentFilter(const QString &filter, const TorrentIDSet &idSet, const QString &category, const QString &tag)
62  : m_type(All)
63  , m_category(category)
64  , m_tag(tag)
65  , m_idSet(idSet)
66 {
67  setTypeByName(filter);
68 }
69 
71 {
72  if (m_type != type)
73  {
74  m_type = type;
75  return true;
76  }
77 
78  return false;
79 }
80 
81 bool TorrentFilter::setTypeByName(const QString &filter)
82 {
83  Type type = All;
84 
85  if (filter == "downloading")
86  type = Downloading;
87  else if (filter == "seeding")
88  type = Seeding;
89  else if (filter == "completed")
90  type = Completed;
91  else if (filter == "paused")
92  type = Paused;
93  else if (filter == "resumed")
94  type = Resumed;
95  else if (filter == "active")
96  type = Active;
97  else if (filter == "inactive")
98  type = Inactive;
99  else if (filter == "stalled")
100  type = Stalled;
101  else if (filter == "stalled_uploading")
102  type = StalledUploading;
103  else if (filter == "stalled_downloading")
104  type = StalledDownloading;
105  else if (filter == "checking")
106  type = Checking;
107  else if (filter == "errored")
108  type = Errored;
109 
110  return setType(type);
111 }
112 
114 {
115  if (m_idSet != idSet)
116  {
117  m_idSet = idSet;
118  return true;
119  }
120 
121  return false;
122 }
123 
124 bool TorrentFilter::setCategory(const QString &category)
125 {
126  // QString::operator==() doesn't distinguish between empty and null strings.
127  if ((m_category != category)
128  || (m_category.isNull() && !category.isNull())
129  || (!m_category.isNull() && category.isNull()))
130  {
131  m_category = category;
132  return true;
133  }
134 
135  return false;
136 }
137 
138 bool TorrentFilter::setTag(const QString &tag)
139 {
140  // QString::operator==() doesn't distinguish between empty and null strings.
141  if ((m_tag != tag)
142  || (m_tag.isNull() && !tag.isNull())
143  || (!m_tag.isNull() && tag.isNull()))
144  {
145  m_tag = tag;
146  return true;
147  }
148 
149  return false;
150 }
151 
152 bool TorrentFilter::match(const Torrent *const torrent) const
153 {
154  if (!torrent) return false;
155 
156  return (matchState(torrent) && matchHash(torrent) && matchCategory(torrent) && matchTag(torrent));
157 }
158 
159 bool TorrentFilter::matchState(const BitTorrent::Torrent *const torrent) const
160 {
161  switch (m_type)
162  {
163  case All:
164  return true;
165  case Downloading:
166  return torrent->isDownloading();
167  case Seeding:
168  return torrent->isUploading();
169  case Completed:
170  return torrent->isCompleted();
171  case Paused:
172  return torrent->isPaused();
173  case Resumed:
174  return torrent->isResumed();
175  case Active:
176  return torrent->isActive();
177  case Inactive:
178  return torrent->isInactive();
179  case Stalled:
182  case StalledUploading:
184  case StalledDownloading:
186  case Checking:
190  case Errored:
191  return torrent->isErrored();
192  default: // All
193  return true;
194  }
195 }
196 
197 bool TorrentFilter::matchHash(const BitTorrent::Torrent *const torrent) const
198 {
199  if (m_idSet == AnyID) return true;
200 
201  return m_idSet.contains(torrent->id());
202 }
203 
204 bool TorrentFilter::matchCategory(const BitTorrent::Torrent *const torrent) const
205 {
206  if (m_category.isNull()) return true;
207 
208  return (torrent->belongsToCategory(m_category));
209 }
210 
211 bool TorrentFilter::matchTag(const BitTorrent::Torrent *const torrent) const
212 {
213  // Empty tag is a special value to indicate we're filtering for untagged torrents.
214  if (m_tag.isNull()) return true;
215  if (m_tag.isEmpty()) return torrent->tags().isEmpty();
216 
217  return (torrent->hasTag(m_tag));
218 }
virtual bool isErrored() const =0
TorrentID id() const
Definition: torrent.cpp:54
virtual TagSet tags() const =0
virtual bool isDownloading() const =0
virtual bool isCompleted() const =0
virtual bool isInactive() const =0
virtual bool hasTag(const QString &tag) const =0
virtual bool belongsToCategory(const QString &category) const =0
virtual TorrentState state() const =0
bool isResumed() const
Definition: torrent.cpp:59
virtual bool isActive() const =0
virtual bool isPaused() const =0
virtual bool isUploading() const =0
bool isEmpty() const
Definition: orderedset.h:74
bool setType(Type type)
static const QString AnyCategory
Definition: torrentfilter.h:64
bool setCategory(const QString &category)
static const TorrentIDSet AnyID
Definition: torrentfilter.h:65
QString m_category
static const TorrentFilter ActiveTorrent
Definition: torrentfilter.h:73
bool setTag(const QString &tag)
static const TorrentFilter InactiveTorrent
Definition: torrentfilter.h:74
static const TorrentFilter CheckingTorrent
Definition: torrentfilter.h:78
static const QString AnyTag
Definition: torrentfilter.h:66
static const TorrentFilter DownloadingTorrent
Definition: torrentfilter.h:68
static const TorrentFilter ResumedTorrent
Definition: torrentfilter.h:72
bool match(const BitTorrent::Torrent *torrent) const
bool matchTag(const BitTorrent::Torrent *torrent) const
TorrentFilter()=default
static const TorrentFilter PausedTorrent
Definition: torrentfilter.h:71
static const TorrentFilter ErroredTorrent
Definition: torrentfilter.h:79
bool setTorrentIDSet(const TorrentIDSet &idSet)
static const TorrentFilter StalledUploadingTorrent
Definition: torrentfilter.h:76
TorrentIDSet m_idSet
static const TorrentFilter SeedingTorrent
Definition: torrentfilter.h:69
bool matchHash(const BitTorrent::Torrent *torrent) const
bool matchState(const BitTorrent::Torrent *torrent) const
static const TorrentFilter StalledDownloadingTorrent
Definition: torrentfilter.h:77
bool setTypeByName(const QString &filter)
bool matchCategory(const BitTorrent::Torrent *torrent) const
static const TorrentFilter CompletedTorrent
Definition: torrentfilter.h:70
static const TorrentFilter StalledTorrent
Definition: torrentfilter.h:75
QSet< BitTorrent::TorrentID > TorrentIDSet
Definition: torrentfilter.h:41