Add a class to represent a tag

PR #20028.
Closes #19744.
This commit is contained in:
Vladimir Golovnev
2023-12-05 17:01:09 +03:00
committed by GitHub
parent 65771d66fc
commit 7a41192597
38 changed files with 421 additions and 251 deletions

View File

@@ -85,6 +85,7 @@ add_library(qbt_base STATIC
search/searchhandler.h
search/searchpluginmanager.h
settingsstorage.h
tag.h
tagset.h
torrentfileguard.h
torrentfileswatcher.h
@@ -174,6 +175,7 @@ add_library(qbt_base STATIC
search/searchhandler.cpp
search/searchpluginmanager.cpp
settingsstorage.cpp
tag.cpp
tagset.cpp
torrentfileguard.cpp
torrentfileswatcher.cpp

View File

@@ -60,7 +60,7 @@ namespace
{
TagSet tags;
for (const QJsonValue &jsonVal : jsonArr)
tags.insert(jsonVal.toString());
tags.insert(Tag(jsonVal.toString()));
return tags;
}
@@ -68,8 +68,8 @@ namespace
QJsonArray serializeTagSet(const TagSet &tags)
{
QJsonArray arr;
for (const QString &tag : tags)
arr.append(tag);
for (const Tag &tag : tags)
arr.append(tag.toString());
return arr;
}

View File

@@ -85,8 +85,8 @@ namespace
{
ListType entryList;
entryList.reserve(input.size());
for (const QString &setValue : input)
entryList.emplace_back(setValue.toStdString());
for (const Tag &setValue : input)
entryList.emplace_back(setValue.toString().toStdString());
return entryList;
}
}
@@ -263,7 +263,7 @@ BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::loadTorre
{
for (int i = 0; i < tagsNode.list_size(); ++i)
{
const QString tag = fromLTString(tagsNode.list_string_value_at(i));
const Tag tag {fromLTString(tagsNode.list_string_value_at(i))};
torrentParams.tags.insert(tag);
}
}

View File

@@ -854,7 +854,7 @@ namespace
query.bindValue(DB_COLUMN_NAME.placeholder, m_resumeData.name);
query.bindValue(DB_COLUMN_CATEGORY.placeholder, m_resumeData.category);
query.bindValue(DB_COLUMN_TAGS.placeholder, (m_resumeData.tags.isEmpty()
? QString() : m_resumeData.tags.join(u","_s)));
? QString() : QStringList(m_resumeData.tags.cbegin(), m_resumeData.tags.cend()).join(u","_s)));
query.bindValue(DB_COLUMN_CONTENT_LAYOUT.placeholder, Utils::String::fromEnum(m_resumeData.contentLayout));
query.bindValue(DB_COLUMN_RATIO_LIMIT.placeholder, static_cast<int>(m_resumeData.ratioLimit * 1000));
query.bindValue(DB_COLUMN_SEEDING_TIME_LIMIT.placeholder, m_resumeData.seedingTimeLimit);

View File

@@ -33,6 +33,7 @@
#include <QObject>
#include "base/pathfwd.h"
#include "base/tagset.h"
#include "addtorrentparams.h"
#include "categoryoptions.h"
#include "trackerentry.h"
@@ -177,11 +178,10 @@ namespace BitTorrent
virtual Path suggestedSavePath(const QString &categoryName, std::optional<bool> useAutoTMM) const = 0;
virtual Path suggestedDownloadPath(const QString &categoryName, std::optional<bool> useAutoTMM) const = 0;
static bool isValidTag(const QString &tag);
virtual QSet<QString> tags() const = 0;
virtual bool hasTag(const QString &tag) const = 0;
virtual bool addTag(const QString &tag) = 0;
virtual bool removeTag(const QString &tag) = 0;
virtual TagSet tags() const = 0;
virtual bool hasTag(const Tag &tag) const = 0;
virtual bool addTag(const Tag &tag) = 0;
virtual bool removeTag(const Tag &tag) = 0;
// Torrent Management Mode subsystem (TMM)
//
@@ -473,8 +473,8 @@ namespace BitTorrent
void speedLimitModeChanged(bool alternative);
void statsUpdated();
void subcategoriesSupportChanged();
void tagAdded(const QString &tag);
void tagRemoved(const QString &tag);
void tagAdded(const Tag &tag);
void tagRemoved(const Tag &tag);
void torrentAboutToBeRemoved(Torrent *torrent);
void torrentAdded(Torrent *torrent);
void torrentCategoryChanged(Torrent *torrent, const QString &oldCategory);
@@ -487,8 +487,8 @@ namespace BitTorrent
void torrentSavingModeChanged(Torrent *torrent);
void torrentsLoaded(const QVector<Torrent *> &torrents);
void torrentsUpdated(const QVector<Torrent *> &torrents);
void torrentTagAdded(Torrent *torrent, const QString &tag);
void torrentTagRemoved(Torrent *torrent, const QString &tag);
void torrentTagAdded(Torrent *torrent, const Tag &tag);
void torrentTagRemoved(Torrent *torrent, const Tag &tag);
void trackerError(Torrent *torrent, const QString &tracker);
void trackersAdded(Torrent *torrent, const QVector<TrackerEntry> &trackers);
void trackersChanged(Torrent *torrent);

View File

@@ -80,12 +80,9 @@
#include "base/net/proxyconfigurationmanager.h"
#include "base/preferences.h"
#include "base/profile.h"
#include "base/torrentfilter.h"
#include "base/unicodestrings.h"
#include "base/utils/bytearray.h"
#include "base/utils/fs.h"
#include "base/utils/io.h"
#include "base/utils/misc.h"
#include "base/utils/net.h"
#include "base/utils/random.h"
#include "base/version.h"
@@ -373,11 +370,6 @@ QString Session::parentCategoryName(const QString &category)
return {};
}
bool Session::isValidTag(const QString &tag)
{
return (!tag.trimmed().isEmpty() && !tag.contains(u','));
}
QStringList Session::expandCategory(const QString &category)
{
QStringList result;
@@ -560,7 +552,8 @@ SessionImpl::SessionImpl(QObject *parent)
}
const QStringList storedTags = m_storedTags.get();
m_tags = {storedTags.cbegin(), storedTags.cend()};
m_tags.insert(storedTags.cbegin(), storedTags.cend());
std::erase_if(m_tags, [](const Tag &tag) { return !tag.isValid(); });
updateSeedingLimitTimer();
populateAdditionalTrackers();
@@ -1022,34 +1015,37 @@ Path SessionImpl::suggestedDownloadPath(const QString &categoryName, std::option
return path;
}
QSet<QString> SessionImpl::tags() const
TagSet SessionImpl::tags() const
{
return m_tags;
}
bool SessionImpl::hasTag(const QString &tag) const
bool SessionImpl::hasTag(const Tag &tag) const
{
return m_tags.contains(tag);
}
bool SessionImpl::addTag(const QString &tag)
bool SessionImpl::addTag(const Tag &tag)
{
if (!isValidTag(tag) || hasTag(tag))
if (!tag.isValid() || hasTag(tag))
return false;
m_tags.insert(tag);
m_storedTags = m_tags.values();
m_storedTags = QStringList(m_tags.cbegin(), m_tags.cend());
emit tagAdded(tag);
return true;
}
bool SessionImpl::removeTag(const QString &tag)
bool SessionImpl::removeTag(const Tag &tag)
{
if (m_tags.remove(tag))
{
for (TorrentImpl *const torrent : asConst(m_torrents))
torrent->removeTag(tag);
m_storedTags = m_tags.values();
m_storedTags = QStringList(m_tags.cbegin(), m_tags.cend());
emit tagRemoved(tag);
return true;
}
@@ -1472,7 +1468,7 @@ void SessionImpl::processNextResumeData(ResumeSessionContext *context)
}
}
std::erase_if(resumeData.tags, [this, &torrentID](const QString &tag)
std::erase_if(resumeData.tags, [this, &torrentID](const Tag &tag)
{
if (hasTag(tag))
return false;
@@ -1481,12 +1477,12 @@ void SessionImpl::processNextResumeData(ResumeSessionContext *context)
{
LogMsg(tr("Detected inconsistent data: tag is missing from the configuration file."
" Tag will be recovered."
" Torrent: \"%1\". Tag: \"%2\"").arg(torrentID.toString(), tag), Log::WARNING);
" Torrent: \"%1\". Tag: \"%2\"").arg(torrentID.toString(), tag.toString()), Log::WARNING);
return false;
}
LogMsg(tr("Detected inconsistent data: invalid tag. Torrent: \"%1\". Tag: \"%2\"")
.arg(torrentID.toString(), tag), Log::WARNING);
.arg(torrentID.toString(), tag.toString()), Log::WARNING);
return true;
});
@@ -2701,7 +2697,7 @@ LoadTorrentParams SessionImpl::initLoadTorrentParams(const AddTorrentParams &add
}
}
for (const QString &tag : addTorrentParams.tags)
for (const Tag &tag : addTorrentParams.tags)
{
if (hasTag(tag) || addTag(tag))
loadTorrentParams.tags.insert(tag);
@@ -4863,12 +4859,12 @@ void SessionImpl::handleTorrentCategoryChanged(TorrentImpl *const torrent, const
emit torrentCategoryChanged(torrent, oldCategory);
}
void SessionImpl::handleTorrentTagAdded(TorrentImpl *const torrent, const QString &tag)
void SessionImpl::handleTorrentTagAdded(TorrentImpl *const torrent, const Tag &tag)
{
emit torrentTagAdded(torrent, tag);
}
void SessionImpl::handleTorrentTagRemoved(TorrentImpl *const torrent, const QString &tag)
void SessionImpl::handleTorrentTagRemoved(TorrentImpl *const torrent, const Tag &tag)
{
emit torrentTagRemoved(torrent, tag);
}

View File

@@ -156,10 +156,10 @@ namespace BitTorrent
Path suggestedSavePath(const QString &categoryName, std::optional<bool> useAutoTMM) const override;
Path suggestedDownloadPath(const QString &categoryName, std::optional<bool> useAutoTMM) const override;
QSet<QString> tags() const override;
bool hasTag(const QString &tag) const override;
bool addTag(const QString &tag) override;
bool removeTag(const QString &tag) override;
TagSet tags() const override;
bool hasTag(const Tag &tag) const override;
bool addTag(const Tag &tag) override;
bool removeTag(const Tag &tag) override;
bool isAutoTMMDisabledByDefault() const override;
void setAutoTMMDisabledByDefault(bool value) override;
@@ -433,8 +433,8 @@ namespace BitTorrent
void handleTorrentNameChanged(TorrentImpl *torrent);
void handleTorrentSavePathChanged(TorrentImpl *torrent);
void handleTorrentCategoryChanged(TorrentImpl *torrent, const QString &oldCategory);
void handleTorrentTagAdded(TorrentImpl *torrent, const QString &tag);
void handleTorrentTagRemoved(TorrentImpl *torrent, const QString &tag);
void handleTorrentTagAdded(TorrentImpl *torrent, const Tag &tag);
void handleTorrentTagRemoved(TorrentImpl *torrent, const Tag &tag);
void handleTorrentSavingModeChanged(TorrentImpl *torrent);
void handleTorrentMetadataReceived(TorrentImpl *torrent);
void handleTorrentPaused(TorrentImpl *torrent);
@@ -755,7 +755,7 @@ namespace BitTorrent
QSet<TorrentID> m_needSaveResumeDataTorrents;
QHash<TorrentID, TorrentID> m_changedTorrentIDs;
QMap<QString, CategoryOptions> m_categories;
QSet<QString> m_tags;
TagSet m_tags;
// This field holds amounts of peers reported by trackers in their responses to announces
// (torrent.tracker_name.tracker_local_endpoint.protocol_version.num_peers)

View File

@@ -202,9 +202,9 @@ namespace BitTorrent
virtual bool setCategory(const QString &category) = 0;
virtual TagSet tags() const = 0;
virtual bool hasTag(const QString &tag) const = 0;
virtual bool addTag(const QString &tag) = 0;
virtual bool removeTag(const QString &tag) = 0;
virtual bool hasTag(const Tag &tag) const = 0;
virtual bool addTag(const Tag &tag) = 0;
virtual bool removeTag(const Tag &tag) = 0;
virtual void removeAllTags() = 0;
virtual int piecesCount() const = 0;

View File

@@ -863,14 +863,14 @@ TagSet TorrentImpl::tags() const
return m_tags;
}
bool TorrentImpl::hasTag(const QString &tag) const
bool TorrentImpl::hasTag(const Tag &tag) const
{
return m_tags.contains(tag);
}
bool TorrentImpl::addTag(const QString &tag)
bool TorrentImpl::addTag(const Tag &tag)
{
if (!m_session->isValidTag(tag))
if (!tag.isValid())
return false;
if (hasTag(tag))
return false;
@@ -886,7 +886,7 @@ bool TorrentImpl::addTag(const QString &tag)
return true;
}
bool TorrentImpl::removeTag(const QString &tag)
bool TorrentImpl::removeTag(const Tag &tag)
{
if (m_tags.remove(tag))
{
@@ -899,7 +899,7 @@ bool TorrentImpl::removeTag(const QString &tag)
void TorrentImpl::removeAllTags()
{
for (const QString &tag : asConst(tags()))
for (const Tag &tag : asConst(tags()))
removeTag(tag);
}

View File

@@ -128,9 +128,9 @@ namespace BitTorrent
bool setCategory(const QString &category) override;
TagSet tags() const override;
bool hasTag(const QString &tag) const override;
bool addTag(const QString &tag) override;
bool removeTag(const QString &tag) override;
bool hasTag(const Tag &tag) const override;
bool addTag(const Tag &tag) override;
bool removeTag(const Tag &tag) override;
void removeAllTags() override;
int filesCount() const override;

92
src/base/tag.cpp Normal file
View File

@@ -0,0 +1,92 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "tag.h"
#include <QDataStream>
#include "base/concepts/stringable.h"
#include "base/global.h"
namespace
{
QString cleanTag(const QString &tag)
{
return tag.trimmed();
}
}
// `Tag` should satisfy `Stringable` concept in order to be stored in settings as string
static_assert(Stringable<Tag>);
Tag::Tag(const QString &tagStr)
: m_tagStr {cleanTag(tagStr)}
{
}
Tag::Tag(const std::string &tagStr)
: Tag(QString::fromStdString(tagStr))
{
}
bool Tag::isValid() const
{
if (isEmpty())
return false;
return !m_tagStr.contains(u',');
}
bool Tag::isEmpty() const
{
return m_tagStr.isEmpty();
}
QString Tag::toString() const noexcept
{
return m_tagStr;
}
Tag::operator QString() const noexcept
{
return toString();
}
QDataStream &operator<<(QDataStream &out, const Tag &tag)
{
out << tag.toString();
return out;
}
QDataStream &operator>>(QDataStream &in, Tag &tag)
{
QString tagStr;
in >> tagStr;
tag = Tag(tagStr);
return in;
}

67
src/base/tag.h Normal file
View File

@@ -0,0 +1,67 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#pragma once
#include <QMetaType>
#include <QString>
class Tag final
{
public:
Tag() = default;
explicit Tag(const QString &tagStr);
explicit Tag(const std::string &tagStr);
bool isValid() const;
bool isEmpty() const;
QString toString() const noexcept;
explicit operator QString() const noexcept;
friend bool operator==(const Tag &, const Tag &) = default;
private:
QString m_tagStr;
};
Q_DECLARE_METATYPE(Tag)
QDataStream &operator<<(QDataStream &out, const Tag &tag);
QDataStream &operator>>(QDataStream &in, Tag &tag);
template <>
struct std::hash<Tag>
{
std::size_t operator()(const Tag &tag, const std::size_t seed = 0) const noexcept
{
return qHash(tag.toString(), seed);
}
};

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2021 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
@@ -28,10 +29,10 @@
#include "tagset.h"
bool TagLessThan::operator()(const QString &left, const QString &right) const
bool TagLessThan::operator()(const Tag &left, const Tag &right) const
{
const int result = m_compare(left, right);
const int result = m_compare(left.toString(), right.toString());
if (result != 0)
return (result < 0);
return (m_subCompare(left, right) < 0);
return (m_subCompare(left.toString(), right.toString()) < 0);
}

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2021 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
@@ -29,21 +30,21 @@
#pragma once
#include <QMetaType>
#include <QString>
#include "base/orderedset.h"
#include "base/utils/compare.h"
#include "orderedset.h"
#include "tag.h"
#include "utils/compare.h"
class TagLessThan
{
public:
bool operator()(const QString &left, const QString &right) const;
bool operator()(const Tag &left, const Tag &right) const;
private:
Utils::Compare::NaturalCompare<Qt::CaseInsensitive> m_compare;
Utils::Compare::NaturalCompare<Qt::CaseSensitive> m_subCompare;
};
using TagSet = OrderedSet<QString, TagLessThan>;
using TagSet = OrderedSet<Tag, TagLessThan>;
Q_DECLARE_METATYPE(TagSet)

View File

@@ -33,7 +33,7 @@
const std::optional<QString> TorrentFilter::AnyCategory;
const std::optional<TorrentIDSet> TorrentFilter::AnyID;
const std::optional<QString> TorrentFilter::AnyTag;
const std::optional<Tag> TorrentFilter::AnyTag;
const TorrentFilter TorrentFilter::DownloadingTorrent(TorrentFilter::Downloading);
const TorrentFilter TorrentFilter::SeedingTorrent(TorrentFilter::Seeding);
@@ -52,19 +52,19 @@ const TorrentFilter TorrentFilter::ErroredTorrent(TorrentFilter::Errored);
using BitTorrent::Torrent;
TorrentFilter::TorrentFilter(const Type type, const std::optional<TorrentIDSet> &idSet
, const std::optional<QString> &category, const std::optional<QString> &tag)
: m_type(type)
, m_category(category)
, m_tag(tag)
, m_idSet(idSet)
, const std::optional<QString> &category, const std::optional<Tag> &tag)
: m_type {type}
, m_category {category}
, m_tag {tag}
, m_idSet {idSet}
{
}
TorrentFilter::TorrentFilter(const QString &filter, const std::optional<TorrentIDSet> &idSet
, const std::optional<QString> &category, const std::optional<QString> &tag)
: m_category(category)
, m_tag(tag)
, m_idSet(idSet)
, const std::optional<QString> &category, const std::optional<Tag> &tag)
: m_category {category}
, m_tag {tag}
, m_idSet {idSet}
{
setTypeByName(filter);
}
@@ -136,7 +136,7 @@ bool TorrentFilter::setCategory(const std::optional<QString> &category)
return false;
}
bool TorrentFilter::setTag(const std::optional<QString> &tag)
bool TorrentFilter::setTag(const std::optional<Tag> &tag)
{
if (m_tag != tag)
{

View File

@@ -34,6 +34,7 @@
#include <QString>
#include "base/bittorrent/infohash.h"
#include "base/tag.h"
namespace BitTorrent
{
@@ -66,7 +67,7 @@ public:
// These mean any permutation, including no category / tag.
static const std::optional<QString> AnyCategory;
static const std::optional<TorrentIDSet> AnyID;
static const std::optional<QString> AnyTag;
static const std::optional<Tag> AnyTag;
static const TorrentFilter DownloadingTorrent;
static const TorrentFilter SeedingTorrent;
@@ -85,15 +86,15 @@ public:
TorrentFilter() = default;
// category & tags: pass empty string for uncategorized / untagged torrents.
TorrentFilter(Type type, const std::optional<TorrentIDSet> &idSet = AnyID
, const std::optional<QString> &category = AnyCategory, const std::optional<QString> &tag = AnyTag);
, const std::optional<QString> &category = AnyCategory, const std::optional<Tag> &tag = AnyTag);
TorrentFilter(const QString &filter, const std::optional<TorrentIDSet> &idSet = AnyID
, const std::optional<QString> &category = AnyCategory, const std::optional<QString> &tags = AnyTag);
, const std::optional<QString> &category = AnyCategory, const std::optional<Tag> &tags = AnyTag);
bool setType(Type type);
bool setTypeByName(const QString &filter);
bool setTorrentIDSet(const std::optional<TorrentIDSet> &idSet);
bool setCategory(const std::optional<QString> &category);
bool setTag(const std::optional<QString> &tag);
bool setTag(const std::optional<Tag> &tag);
bool match(const BitTorrent::Torrent *torrent) const;
@@ -105,6 +106,6 @@ private:
Type m_type {All};
std::optional<QString> m_category;
std::optional<QString> m_tag;
std::optional<Tag> m_tag;
std::optional<TorrentIDSet> m_idSet;
};