Remove incorrect assertions
[qBittorrent.git] / src / base / torrentfilter.cpp
blob1c81da452854769fa229b3298a1f6b86053ae768
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<QString> 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::PausedTorrent(TorrentFilter::Paused);
42 const TorrentFilter TorrentFilter::ResumedTorrent(TorrentFilter::Resumed);
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<QString> &tag)
56 : m_type(type)
57 , m_category(category)
58 , m_tag(tag)
59 , m_idSet(idSet)
63 TorrentFilter::TorrentFilter(const QString &filter, const std::optional<TorrentIDSet> &idSet
64 , const std::optional<QString> &category, const std::optional<QString> &tag)
65 : m_category(category)
66 , m_tag(tag)
67 , m_idSet(idSet)
69 setTypeByName(filter);
72 bool TorrentFilter::setType(Type type)
74 if (m_type != type)
76 m_type = type;
77 return true;
80 return false;
83 bool TorrentFilter::setTypeByName(const QString &filter)
85 Type type = All;
87 if (filter == u"downloading")
88 type = Downloading;
89 else if (filter == u"seeding")
90 type = Seeding;
91 else if (filter == u"completed")
92 type = Completed;
93 else if (filter == u"paused")
94 type = Paused;
95 else if (filter == u"resumed")
96 type = Resumed;
97 else if (filter == u"active")
98 type = Active;
99 else if (filter == u"inactive")
100 type = Inactive;
101 else if (filter == u"stalled")
102 type = Stalled;
103 else if (filter == u"stalled_uploading")
104 type = StalledUploading;
105 else if (filter == u"stalled_downloading")
106 type = StalledDownloading;
107 else if (filter == u"checking")
108 type = Checking;
109 else if (filter == u"moving")
110 type = Moving;
111 else if (filter == u"errored")
112 type = Errored;
114 return setType(type);
117 bool TorrentFilter::setTorrentIDSet(const std::optional<TorrentIDSet> &idSet)
119 if (m_idSet != idSet)
121 m_idSet = idSet;
122 return true;
125 return false;
128 bool TorrentFilter::setCategory(const std::optional<QString> &category)
130 if (m_category != category)
132 m_category = category;
133 return true;
136 return false;
139 bool TorrentFilter::setTag(const std::optional<QString> &tag)
141 if (m_tag != tag)
143 m_tag = tag;
144 return true;
147 return false;
150 bool TorrentFilter::match(const Torrent *const torrent) const
152 if (!torrent) return false;
154 return (matchState(torrent) && matchHash(torrent) && matchCategory(torrent) && matchTag(torrent));
157 bool TorrentFilter::matchState(const BitTorrent::Torrent *const torrent) const
159 switch (m_type)
161 case All:
162 default:
163 return true;
164 case Downloading:
165 return torrent->isDownloading();
166 case Seeding:
167 return torrent->isUploading();
168 case Completed:
169 return torrent->isCompleted();
170 case Paused:
171 return torrent->isPaused();
172 case Resumed:
173 return torrent->isResumed();
174 case Active:
175 return torrent->isActive();
176 case Inactive:
177 return torrent->isInactive();
178 case Stalled:
179 return (torrent->state() == BitTorrent::TorrentState::StalledUploading)
180 || (torrent->state() == BitTorrent::TorrentState::StalledDownloading);
181 case StalledUploading:
182 return torrent->state() == BitTorrent::TorrentState::StalledUploading;
183 case StalledDownloading:
184 return torrent->state() == BitTorrent::TorrentState::StalledDownloading;
185 case Checking:
186 return (torrent->state() == BitTorrent::TorrentState::CheckingUploading)
187 || (torrent->state() == BitTorrent::TorrentState::CheckingDownloading)
188 || (torrent->state() == BitTorrent::TorrentState::CheckingResumeData);
189 case Moving:
190 return torrent->isMoving();
191 case Errored:
192 return torrent->isErrored();
196 bool TorrentFilter::matchHash(const BitTorrent::Torrent *const torrent) const
198 if (!m_idSet)
199 return true;
201 return m_idSet->contains(torrent->id());
204 bool TorrentFilter::matchCategory(const BitTorrent::Torrent *const torrent) const
206 if (!m_category)
207 return true;
209 return (torrent->belongsToCategory(*m_category));
212 bool TorrentFilter::matchTag(const BitTorrent::Torrent *const torrent) const
214 if (!m_tag)
215 return true;
217 // Empty tag is a special value to indicate we're filtering for untagged torrents.
218 if (m_tag->isEmpty())
219 return torrent->tags().isEmpty();
221 return torrent->hasTag(*m_tag);