Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / torrentfilter.cpp
blob89d9a97e9f6d70d897c1a3be38fe596ab188df8b
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
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.
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.
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.
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.
29 #include "torrentfilter.h"
31 #include "bittorrent/infohash.h"
32 #include "bittorrent/torrent.h"
34 const std::optional<QString> TorrentFilter::AnyCategory;
35 const std::optional<TorrentIDSet> TorrentFilter::AnyID;
36 const std::optional<Tag> TorrentFilter::AnyTag;
38 const TorrentFilter TorrentFilter::DownloadingTorrent(TorrentFilter::Downloading);
39 const TorrentFilter TorrentFilter::SeedingTorrent(TorrentFilter::Seeding);
40 const TorrentFilter TorrentFilter::CompletedTorrent(TorrentFilter::Completed);
41 const TorrentFilter TorrentFilter::StoppedTorrent(TorrentFilter::Stopped);
42 const TorrentFilter TorrentFilter::RunningTorrent(TorrentFilter::Running);
43 const TorrentFilter TorrentFilter::ActiveTorrent(TorrentFilter::Active);
44 const TorrentFilter TorrentFilter::InactiveTorrent(TorrentFilter::Inactive);
45 const TorrentFilter TorrentFilter::StalledTorrent(TorrentFilter::Stalled);
46 const TorrentFilter TorrentFilter::StalledUploadingTorrent(TorrentFilter::StalledUploading);
47 const TorrentFilter TorrentFilter::StalledDownloadingTorrent(TorrentFilter::StalledDownloading);
48 const TorrentFilter TorrentFilter::CheckingTorrent(TorrentFilter::Checking);
49 const TorrentFilter TorrentFilter::MovingTorrent(TorrentFilter::Moving);
50 const TorrentFilter TorrentFilter::ErroredTorrent(TorrentFilter::Errored);
52 using BitTorrent::Torrent;
54 TorrentFilter::TorrentFilter(const Type type, const std::optional<TorrentIDSet> &idSet
55 , const std::optional<QString> &category, const std::optional<Tag> &tag, const std::optional<bool> isPrivate)
56 : m_type {type}
57 , m_category {category}
58 , m_tag {tag}
59 , m_idSet {idSet}
60 , m_private {isPrivate}
64 TorrentFilter::TorrentFilter(const QString &filter, const std::optional<TorrentIDSet> &idSet
65 , const std::optional<QString> &category, const std::optional<Tag> &tag, const std::optional<bool> isPrivate)
66 : m_category {category}
67 , m_tag {tag}
68 , m_idSet {idSet}
69 , m_private {isPrivate}
71 setTypeByName(filter);
74 bool TorrentFilter::setType(Type type)
76 if (m_type != type)
78 m_type = type;
79 return true;
82 return false;
85 bool TorrentFilter::setTypeByName(const QString &filter)
87 Type type = All;
89 if (filter == u"downloading")
90 type = Downloading;
91 else if (filter == u"seeding")
92 type = Seeding;
93 else if (filter == u"completed")
94 type = Completed;
95 else if (filter == u"stopped")
96 type = Stopped;
97 else if (filter == u"running")
98 type = Running;
99 else if (filter == u"active")
100 type = Active;
101 else if (filter == u"inactive")
102 type = Inactive;
103 else if (filter == u"stalled")
104 type = Stalled;
105 else if (filter == u"stalled_uploading")
106 type = StalledUploading;
107 else if (filter == u"stalled_downloading")
108 type = StalledDownloading;
109 else if (filter == u"checking")
110 type = Checking;
111 else if (filter == u"moving")
112 type = Moving;
113 else if (filter == u"errored")
114 type = Errored;
116 return setType(type);
119 bool TorrentFilter::setTorrentIDSet(const std::optional<TorrentIDSet> &idSet)
121 if (m_idSet != idSet)
123 m_idSet = idSet;
124 return true;
127 return false;
130 bool TorrentFilter::setCategory(const std::optional<QString> &category)
132 if (m_category != category)
134 m_category = category;
135 return true;
138 return false;
141 bool TorrentFilter::setTag(const std::optional<Tag> &tag)
143 if (m_tag != tag)
145 m_tag = tag;
146 return true;
149 return false;
152 bool TorrentFilter::setPrivate(const std::optional<bool> isPrivate)
154 if (m_private != isPrivate)
156 m_private = isPrivate;
157 return true;
160 return false;
163 bool TorrentFilter::match(const Torrent *const torrent) const
165 if (!torrent) return false;
167 return (matchState(torrent) && matchHash(torrent) && matchCategory(torrent) && matchTag(torrent) && matchPrivate(torrent));
170 bool TorrentFilter::matchState(const BitTorrent::Torrent *const torrent) const
172 const BitTorrent::TorrentState state = torrent->state();
174 switch (m_type)
176 case All:
177 return true;
178 case Downloading:
179 return torrent->isDownloading();
180 case Seeding:
181 return torrent->isUploading();
182 case Completed:
183 return torrent->isCompleted();
184 case Stopped:
185 return torrent->isStopped();
186 case Running:
187 return torrent->isRunning();
188 case Active:
189 return torrent->isActive();
190 case Inactive:
191 return torrent->isInactive();
192 case Stalled:
193 return (state == BitTorrent::TorrentState::StalledUploading)
194 || (state == BitTorrent::TorrentState::StalledDownloading);
195 case StalledUploading:
196 return state == BitTorrent::TorrentState::StalledUploading;
197 case StalledDownloading:
198 return state == BitTorrent::TorrentState::StalledDownloading;
199 case Checking:
200 return (state == BitTorrent::TorrentState::CheckingUploading)
201 || (state == BitTorrent::TorrentState::CheckingDownloading)
202 || (state == BitTorrent::TorrentState::CheckingResumeData);
203 case Moving:
204 return torrent->isMoving();
205 case Errored:
206 return torrent->isErrored();
207 default:
208 Q_ASSERT(false);
209 return false;
213 bool TorrentFilter::matchHash(const BitTorrent::Torrent *const torrent) const
215 if (!m_idSet)
216 return true;
218 return m_idSet->contains(torrent->id());
221 bool TorrentFilter::matchCategory(const BitTorrent::Torrent *const torrent) const
223 if (!m_category)
224 return true;
226 return (torrent->belongsToCategory(*m_category));
229 bool TorrentFilter::matchTag(const BitTorrent::Torrent *const torrent) const
231 if (!m_tag)
232 return true;
234 // Empty tag is a special value to indicate we're filtering for untagged torrents.
235 if (m_tag->isEmpty())
236 return torrent->tags().isEmpty();
238 return torrent->hasTag(*m_tag);
241 bool TorrentFilter::matchPrivate(const BitTorrent::Torrent *const torrent) const
243 if (!m_private)
244 return true;
246 return m_private == torrent->isPrivate();