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

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2018-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
@@ -29,6 +29,7 @@
#include "serialize_torrent.h"
#include <QDateTime>
#include <QStringList>
#include <QVector>
#include "base/bittorrent/infohash.h"
@@ -36,7 +37,6 @@
#include "base/bittorrent/trackerentry.h"
#include "base/path.h"
#include "base/tagset.h"
#include "base/utils/fs.h"
namespace
{
@@ -106,6 +106,8 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
: (QDateTime::currentDateTime().toSecsSinceEpoch() - timeSinceActivity);
};
const TagSet &tags = torrent.tags();
return {
{KEY_TORRENT_ID, torrent.id().toString()},
{KEY_TORRENT_INFOHASHV1, torrent.infoHash().v1().toString()},
@@ -128,7 +130,7 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
{KEY_TORRENT_FIRST_LAST_PIECE_PRIO, torrent.hasFirstLastPiecePriority()},
{KEY_TORRENT_CATEGORY, torrent.category()},
{KEY_TORRENT_TAGS, torrent.tags().join(u", "_s)},
{KEY_TORRENT_TAGS, QStringList(tags.cbegin(), tags.cend()).join(u", "_s)},
{KEY_TORRENT_SUPER_SEEDING, torrent.superSeeding()},
{KEY_TORRENT_FORCE_START, torrent.isForced()},
{KEY_TORRENT_SAVE_PATH, torrent.savePath().toString()},

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2018-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

View File

@@ -540,8 +540,8 @@ void SyncController::makeMaindataSnapshot()
m_maindataSnapshot.categories[categoryName] = category.toVariantMap();
}
for (const QString &tag : asConst(session->tags()))
m_maindataSnapshot.tags.append(tag);
for (const Tag &tag : asConst(session->tags()))
m_maindataSnapshot.tags.append(tag.toString());
for (auto trackersIter = m_knownTrackers.cbegin(); trackersIter != m_knownTrackers.cend(); ++trackersIter)
{
@@ -815,16 +815,16 @@ void SyncController::onSubcategoriesSupportChanged()
}
}
void SyncController::onTagAdded(const QString &tag)
void SyncController::onTagAdded(const Tag &tag)
{
m_removedTags.remove(tag);
m_addedTags.insert(tag);
m_removedTags.remove(tag.toString());
m_addedTags.insert(tag.toString());
}
void SyncController::onTagRemoved(const QString &tag)
void SyncController::onTagRemoved(const Tag &tag)
{
m_addedTags.remove(tag);
m_removedTags.insert(tag);
m_addedTags.remove(tag.toString());
m_removedTags.insert(tag.toString());
}
void SyncController::onTorrentAdded(BitTorrent::Torrent *torrent)
@@ -902,12 +902,12 @@ void SyncController::onTorrentSavingModeChanged(BitTorrent::Torrent *torrent)
m_updatedTorrents.insert(torrent->id());
}
void SyncController::onTorrentTagAdded(BitTorrent::Torrent *torrent, [[maybe_unused]] const QString &tag)
void SyncController::onTorrentTagAdded(BitTorrent::Torrent *torrent, [[maybe_unused]] const Tag &tag)
{
m_updatedTorrents.insert(torrent->id());
}
void SyncController::onTorrentTagRemoved(BitTorrent::Torrent *torrent, [[maybe_unused]] const QString &tag)
void SyncController::onTorrentTagRemoved(BitTorrent::Torrent *torrent, [[maybe_unused]] const Tag &tag)
{
m_updatedTorrents.insert(torrent->id());
}

View File

@@ -32,6 +32,7 @@
#include <QVariantMap>
#include "base/bittorrent/infohash.h"
#include "base/tag.h"
#include "apicontroller.h"
namespace BitTorrent
@@ -64,8 +65,8 @@ private:
void onCategoryRemoved(const QString &categoryName);
void onCategoryOptionsChanged(const QString &categoryName);
void onSubcategoriesSupportChanged();
void onTagAdded(const QString &tag);
void onTagRemoved(const QString &tag);
void onTagAdded(const Tag &tag);
void onTagRemoved(const Tag &tag);
void onTorrentAdded(BitTorrent::Torrent *torrent);
void onTorrentAboutToBeRemoved(BitTorrent::Torrent *torrent);
void onTorrentCategoryChanged(BitTorrent::Torrent *torrent, const QString &oldCategory);
@@ -74,8 +75,8 @@ private:
void onTorrentResumed(BitTorrent::Torrent *torrent);
void onTorrentSavePathChanged(BitTorrent::Torrent *torrent);
void onTorrentSavingModeChanged(BitTorrent::Torrent *torrent);
void onTorrentTagAdded(BitTorrent::Torrent *torrent, const QString &tag);
void onTorrentTagRemoved(BitTorrent::Torrent *torrent, const QString &tag);
void onTorrentTagAdded(BitTorrent::Torrent *torrent, const Tag &tag);
void onTorrentTagRemoved(BitTorrent::Torrent *torrent, const Tag &tag);
void onTorrentsUpdated(const QVector<BitTorrent::Torrent *> &torrents);
void onTorrentTrackersChanged(BitTorrent::Torrent *torrent);

View File

@@ -152,6 +152,15 @@ namespace
return it.value();
}
std::optional<Tag> getOptionalTag(const StringMap &params, const QString &name)
{
const auto it = params.constFind(name);
if (it == params.cend())
return std::nullopt;
return Tag(it.value());
}
QJsonArray getStickyTrackers(const BitTorrent::Torrent *const torrent)
{
int seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, leechesDHT = 0, leechesPeX = 0, leechesLSD = 0;
@@ -273,7 +282,7 @@ void TorrentsController::infoAction()
{
const QString filter {params()[u"filter"_s]};
const std::optional<QString> category = getOptionalString(params(), u"category"_s);
const std::optional<QString> tag = getOptionalString(params(), u"tag"_s);
const std::optional<Tag> tag = getOptionalTag(params(), u"tag"_s);
const QString sortedColumn {params()[u"sort"_s]};
const bool reverse {parseBool(params()[u"reverse"_s]).value_or(false)};
int limit {params()[u"limit"_s].toInt()};
@@ -1317,12 +1326,11 @@ void TorrentsController::addTagsAction()
const QStringList hashes {params()[u"hashes"_s].split(u'|')};
const QStringList tags {params()[u"tags"_s].split(u',', Qt::SkipEmptyParts)};
for (const QString &tag : tags)
for (const QString &tagStr : tags)
{
const QString tagTrimmed {tag.trimmed()};
applyToTorrents(hashes, [&tagTrimmed](BitTorrent::Torrent *const torrent)
applyToTorrents(hashes, [&tagStr](BitTorrent::Torrent *const torrent)
{
torrent->addTag(tagTrimmed);
torrent->addTag(Tag(tagStr));
});
}
}
@@ -1334,12 +1342,11 @@ void TorrentsController::removeTagsAction()
const QStringList hashes {params()[u"hashes"_s].split(u'|')};
const QStringList tags {params()[u"tags"_s].split(u',', Qt::SkipEmptyParts)};
for (const QString &tag : tags)
for (const QString &tagStr : tags)
{
const QString tagTrimmed {tag.trimmed()};
applyToTorrents(hashes, [&tagTrimmed](BitTorrent::Torrent *const torrent)
applyToTorrents(hashes, [&tagStr](BitTorrent::Torrent *const torrent)
{
torrent->removeTag(tagTrimmed);
torrent->removeTag(Tag(tagStr));
});
}
@@ -1358,8 +1365,8 @@ void TorrentsController::createTagsAction()
const QStringList tags {params()[u"tags"_s].split(u',', Qt::SkipEmptyParts)};
for (const QString &tag : tags)
BitTorrent::Session::instance()->addTag(tag.trimmed());
for (const QString &tagStr : tags)
BitTorrent::Session::instance()->addTag(Tag(tagStr));
}
void TorrentsController::deleteTagsAction()
@@ -1367,15 +1374,15 @@ void TorrentsController::deleteTagsAction()
requireParams({u"tags"_s});
const QStringList tags {params()[u"tags"_s].split(u',', Qt::SkipEmptyParts)};
for (const QString &tag : tags)
BitTorrent::Session::instance()->removeTag(tag.trimmed());
for (const QString &tagStr : tags)
BitTorrent::Session::instance()->removeTag(Tag(tagStr));
}
void TorrentsController::tagsAction()
{
QJsonArray result;
for (const QString &tag : asConst(BitTorrent::Session::instance()->tags()))
result << tag;
for (const Tag &tag : asConst(BitTorrent::Session::instance()->tags()))
result << tag.toString();
setResult(result);
}