diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index 8876b9a31..c59e0c0b1 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -49,6 +49,7 @@ #include "base/path.h" #include "base/settingvalue.h" +#include "base/tagset.h" #include "base/utils/thread.h" #include "addtorrentparams.h" #include "cachestatus.h" diff --git a/src/base/bittorrent/torrent.h b/src/base/bittorrent/torrent.h index d2eefbabf..c28bbd516 100644 --- a/src/base/bittorrent/torrent.h +++ b/src/base/bittorrent/torrent.h @@ -209,6 +209,7 @@ namespace BitTorrent virtual TagSet tags() const = 0; virtual bool hasTag(const Tag &tag) const = 0; virtual bool addTag(const Tag &tag) = 0; + virtual bool setTags(const TagSet &newTags) = 0; virtual bool removeTag(const Tag &tag) = 0; virtual void removeAllTags() = 0; diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index fc6f70f77..bc1e1f102 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -60,6 +60,7 @@ #include "base/global.h" #include "base/logger.h" #include "base/preferences.h" +#include "base/tagset.h" #include "base/types.h" #include "base/utils/fs.h" #include "base/utils/io.h" @@ -937,6 +938,24 @@ bool TorrentImpl::addTag(const Tag &tag) return true; } +bool TorrentImpl::setTags(const TagSet &newTags) +{ + // Identify tags to add + for (const Tag &tag : newTags) + { + if (!hasTag(tag)) + addTag(tag); + } + + // Identify tags to remove + for (const Tag &tag : asConst(m_tags)) + { + if (!newTags.contains(tag)) + removeTag(tag); + } + return true; +} + bool TorrentImpl::removeTag(const Tag &tag) { if (m_tags.remove(tag)) diff --git a/src/base/bittorrent/torrentimpl.h b/src/base/bittorrent/torrentimpl.h index 5c54b2d00..7966255a5 100644 --- a/src/base/bittorrent/torrentimpl.h +++ b/src/base/bittorrent/torrentimpl.h @@ -131,6 +131,7 @@ namespace BitTorrent TagSet tags() const override; bool hasTag(const Tag &tag) const override; bool addTag(const Tag &tag) override; + bool setTags(const TagSet &newTags) override; bool removeTag(const Tag &tag) override; void removeAllTags() override; diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index dcf9580b6..d9cf602df 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -1475,6 +1475,25 @@ void TorrentsController::addTagsAction() } } +void TorrentsController::setTagsAction() +{ + requireParams({u"hashes"_s, u"tags"_s}); + + const QStringList hashes {params()[u"hashes"_s].split(u'|')}; + const QStringList tags {params()[u"tags"_s].split(u',', Qt::SkipEmptyParts)}; + + // Convert QStringList to TagSet + TagSet newTags; + for (const QString &tagStr : tags) + newTags.insert(Tag(tagStr)); + + // Apply the new tags to the selected torrents + applyToTorrents(hashes, [&newTags](BitTorrent::Torrent *const torrent) + { + torrent->setTags(newTags); + }); +} + void TorrentsController::removeTagsAction() { requireParams({u"hashes"_s}); diff --git a/src/webui/api/torrentscontroller.h b/src/webui/api/torrentscontroller.h index 82e6e01a6..a233048ac 100644 --- a/src/webui/api/torrentscontroller.h +++ b/src/webui/api/torrentscontroller.h @@ -61,6 +61,7 @@ private slots: void removeCategoriesAction(); void categoriesAction(); void addTagsAction(); + void setTagsAction(); void removeTagsAction(); void createTagsAction(); void deleteTagsAction(); diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index 514ed3f14..f60add26d 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -212,6 +212,7 @@ private: {{u"torrents"_s, u"setShareLimits"_s}, Http::METHOD_POST}, {{u"torrents"_s, u"setSSLParameters"_s}, Http::METHOD_POST}, {{u"torrents"_s, u"setSuperSeeding"_s}, Http::METHOD_POST}, + {{u"torrents"_s, u"setTags"_s}, Http::METHOD_POST}, {{u"torrents"_s, u"setUploadLimit"_s}, Http::METHOD_POST}, {{u"transfer"_s, u"setDownloadLimit"_s}, Http::METHOD_POST}, {{u"transfer"_s, u"setSpeedLimitsMode"_s}, Http::METHOD_POST},