mirror of
https://github.com/transmission/transmission.git
synced 2026-02-15 07:26:49 +00:00
* fix: warning: declaration shadows a variable in the global namespace [clang-diagnostic-shadow] * fix: warning: use 'contains' to check for membership [readability-container-contains] * fix: warning: variable gl_confdir can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage] * warning: function 'TorrentFilter::match_mode' has a definition with different parameter names [readability-inconsistent-declaration-parameter-name] * build: add sigslot dependency * refactor: use sigslot for tr::Blocklists * refactor: use sigslot for torrent, peer-mgr, wishlist * refactor: remove tr::SimpleObservable * chore: make lint happy warning: method 'make_wishlist' can be made static [readability-convert-member-functions-to-static] warning: invalid case style for function 'make_wishlist' [readability-identifier-naming] warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays] * refactor: remove unused forward declaration of tr_peer * chore: remove slop * refactor: Blocklist::observe_changes() now returns a scoped connection * build: use transmission/sigslot fork * refactor: copyediting * refactor: fix cyclical dependency loop between Wishlist and tr_peerMgr::WishlistMediator
121 lines
3.0 KiB
C++
121 lines
3.0 KiB
C++
// This file Copyright © Mnemosyne LLC.
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
#pragma once
|
|
|
|
#ifndef __TRANSMISSION__
|
|
#error only libtransmission should #include this header.
|
|
#endif
|
|
|
|
#include <algorithm> // std::any_of
|
|
#include <cstddef> // size_t
|
|
#include <numeric>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility> // for std::pair
|
|
#include <vector>
|
|
|
|
#include <sigslot/signal.hpp>
|
|
|
|
#include "libtransmission/net.h" // for tr_address
|
|
|
|
namespace tr
|
|
{
|
|
|
|
class Blocklists
|
|
{
|
|
public:
|
|
Blocklists() = default;
|
|
|
|
[[nodiscard]] bool contains(tr_address const& addr) const noexcept
|
|
{
|
|
return std::any_of(
|
|
std::begin(blocklists_),
|
|
std::end(blocklists_),
|
|
[&addr](auto const& blocklist) { return blocklist.enabled() && blocklist.contains(addr); });
|
|
}
|
|
|
|
[[nodiscard]] constexpr auto num_lists() const noexcept
|
|
{
|
|
return std::size(blocklists_);
|
|
}
|
|
|
|
[[nodiscard]] constexpr auto num_rules() const noexcept
|
|
{
|
|
return std::accumulate(
|
|
std::begin(blocklists_),
|
|
std::end(blocklists_),
|
|
size_t{},
|
|
[](int sum, auto& cur) { return sum + std::size(cur); });
|
|
}
|
|
|
|
void load(std::string_view folder, bool is_enabled);
|
|
void set_enabled(bool is_enabled);
|
|
size_t update_primary_blocklist(std::string_view external_file, bool is_enabled);
|
|
|
|
template<typename Observer>
|
|
[[nodiscard]] sigslot::scoped_connection observe_changes(Observer observer) const
|
|
{
|
|
return changed_.connect_scoped(std::move(observer));
|
|
}
|
|
|
|
private:
|
|
class Blocklist
|
|
{
|
|
public:
|
|
static std::optional<Blocklist> saveNew(std::string_view external_file, std::string_view bin_file, bool is_enabled);
|
|
|
|
Blocklist() = default;
|
|
|
|
Blocklist(std::string_view bin_file, bool is_enabled)
|
|
: bin_file_{ bin_file }
|
|
, is_enabled_{ is_enabled }
|
|
{
|
|
}
|
|
|
|
[[nodiscard]] bool contains(tr_address const& addr) const;
|
|
|
|
[[nodiscard]] size_t size() const
|
|
{
|
|
ensureLoaded();
|
|
|
|
return std::size(rules_);
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool enabled() const noexcept
|
|
{
|
|
return is_enabled_;
|
|
}
|
|
|
|
constexpr void setEnabled(bool is_enabled) noexcept
|
|
{
|
|
is_enabled_ = is_enabled;
|
|
}
|
|
|
|
[[nodiscard]] constexpr auto const& binFile() const noexcept
|
|
{
|
|
return bin_file_;
|
|
}
|
|
|
|
private:
|
|
void ensureLoaded() const;
|
|
|
|
mutable std::vector<std::pair<tr_address, tr_address>> rules_;
|
|
|
|
std::string bin_file_;
|
|
bool is_enabled_ = false;
|
|
};
|
|
|
|
std::vector<Blocklist> blocklists_;
|
|
|
|
std::string folder_;
|
|
|
|
mutable sigslot::signal<> changed_;
|
|
|
|
[[nodiscard]] static std::vector<Blocklist> load_folder(std::string_view folder, bool is_enabled);
|
|
};
|
|
} // namespace tr
|