2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
#include <algorithm>
|
2020-07-29 19:53:55 +00:00
|
|
|
#include <cassert>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <iterator> // for std::back_inserter
|
|
|
|
#include <set>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-12 22:12:12 +00:00
|
|
|
#include "Speed.h"
|
|
|
|
#include "Torrent.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "TorrentDelegate.h"
|
|
|
|
#include "TorrentModel.h"
|
2020-07-27 04:30:58 +00:00
|
|
|
#include "VariantHelpers.h"
|
|
|
|
|
|
|
|
using ::trqt::variant_helpers::getValue;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2016-04-24 07:56:41 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
struct TorrentIdLessThan
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
bool operator()(Torrent const* left, Torrent const* right) const
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return left->id() < right->id();
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
bool operator()(int left_id, Torrent const* right) const
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return left_id < right->id();
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
bool operator()(Torrent const* left, int right_id) const
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return left->id() < right_id;
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
template<typename Iter>
|
2019-11-12 01:37:05 +00:00
|
|
|
auto getIds(Iter it, Iter end)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
torrent_ids_t ids;
|
2019-11-09 14:44:40 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (; it != end; ++it)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
ids.insert((*it)->id());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
TorrentModel::TorrentModel(Prefs const& prefs)
|
|
|
|
: prefs_(prefs)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentModel::~TorrentModel()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentModel::clear()
|
2009-05-03 17:37:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
beginResetModel();
|
2020-05-27 21:53:12 +00:00
|
|
|
qDeleteAll(torrents_);
|
|
|
|
torrents_.clear();
|
2017-04-19 12:04:45 +00:00
|
|
|
endResetModel();
|
2009-05-03 17:37:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int TorrentModel::rowCount(QModelIndex const& parent) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
Q_UNUSED(parent)
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
return torrents_.size();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QVariant TorrentModel::data(QModelIndex const& index, int role) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
Qt 6 support (#2069)
* Bump minimum Qt version to 5.6
* Switch from QRegExp to QRegularExpression
While still available, QRegExp has been moved to Qt6::Core5Compat module
and is not part of Qt6::Core.
* Use qIsEffectiveTLD instead of QUrl::topLevelDomain
The latter is not part of Qt6::Core. The former is a private utility in
Qt6::Network; using it for now, until (and if) we switch to something
non-Qt-specific.
* Use QStyle::State_Horizontal state when drawing progress bars
Although available for a long time, this state either didn't apply to
progress bars before Qt 6, or was deduced based on bar size. With Qt 6,
failing to specify it results in bad rendering.
* Don't use QStringRef (and associated methods)
While still available, QStringRef has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core. Related method (e.g.
QString::midRef) have been removed in Qt 6.
* Use Qt::ItemIsAutoTristate instead of Qt::ItemIsTristate
The latter was deprecated and replaced with the former in Qt 5.6.
* Don't use QApplication::globalStrut
This property has been deprecated in Qt 5.15 and removed in Qt 6.
* Use QImage::fromHICON instead of QtWin::fromHICON
WinExtras module (providind the latter helper) has been removed in Qt 6.
* Use QStringDecoder instead of QTextCodec
While still available, QTextCodec has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core.
* Don't forward-declare QStringList
Instead of being a standalone class, its definition has changed to
QList<QString> template specialization in Qt 6.
* Use explicit (since Qt 6) QFileInfo constructor
* Use QDateTime's {to,from}SecsSinceEpoch instead of {to,from}Time_t
The latter was deprecated in Qt 5.8 and removed in Qt 6.
* Don't use QFuture<>'s operator==
It has been removed in Qt 6. Since the original issue this code was
solving was caused by future reuse, just don't reuse futures and create
new finished ones when necessary.
* Use std::vector<> instead of QVector<>
The latter has been changed to a typedef for QList<>, which might not be
what one wants, and which also changed behavior a bit leading to
compilation errors.
* Don't use + for flags, cast to int explicitly
Operator+ for enum values has been deleted in Qt 6, so using operator|
instead. Then, there's no conversion from QFlags<> to QVariant, so need
to cast to int.
* Support Qt 6 in CMake and for MSI packaging
* Remove extra (empty) CMake variable use when constructing Qt target names
* Simplify logic in tr_qt_add_translation CMake helper
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2021-11-03 21:20:11 +00:00
|
|
|
auto const* const t = (index.isValid() && index.row() < rowCount()) ? torrents_.at(index.row()) : nullptr;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-30 09:29:58 +00:00
|
|
|
if (t != nullptr)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (role)
|
2012-12-29 01:20:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case Qt::DisplayRole:
|
2020-09-08 16:51:33 +00:00
|
|
|
return t->name();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case Qt::DecorationRole:
|
2020-09-08 16:51:33 +00:00
|
|
|
return t->getMimeTypeIcon();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TorrentRole:
|
2020-09-08 16:51:33 +00:00
|
|
|
return QVariant::fromValue(t);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
2012-12-29 01:20:22 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:51:33 +00:00
|
|
|
return {};
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void TorrentModel::removeTorrents(tr_variant* torrent_list)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
auto torrents = torrents_t{};
|
|
|
|
torrents.reserve(tr_variantListSize(torrent_list));
|
2019-11-12 01:37:05 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
int i = 0;
|
2022-04-02 22:42:51 +00:00
|
|
|
tr_variant const* child = nullptr;
|
2022-09-08 23:26:18 +00:00
|
|
|
while ((child = tr_variantListChild(torrent_list, i++)) != nullptr)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
if (auto const id = getValue<int>(child); id)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
if (auto* const torrent = getTorrentFromId(*id); torrent != nullptr)
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
|
|
|
torrents.push_back(torrent);
|
|
|
|
}
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!torrents.empty())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
rowsRemove(torrents);
|
|
|
|
}
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void TorrentModel::updateTorrents(tr_variant* torrent_list, bool is_complete_list)
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
auto const old = is_complete_list ? torrents_ : torrents_t{};
|
2019-11-12 01:37:05 +00:00
|
|
|
auto added = torrent_ids_t{};
|
|
|
|
auto changed = torrent_ids_t{};
|
|
|
|
auto completed = torrent_ids_t{};
|
2020-06-18 20:34:11 +00:00
|
|
|
auto edited = torrent_ids_t{};
|
2019-11-09 14:44:40 +00:00
|
|
|
auto instantiated = torrents_t{};
|
2019-11-12 01:37:05 +00:00
|
|
|
auto needinfo = torrent_ids_t{};
|
|
|
|
auto processed = torrents_t{};
|
2020-06-23 21:11:16 +00:00
|
|
|
auto changed_fields = Torrent::fields_t{};
|
2019-11-09 14:44:40 +00:00
|
|
|
|
|
|
|
auto const now = time(nullptr);
|
2020-11-02 01:13:32 +00:00
|
|
|
auto const recently_added = [&now](auto const& tor)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
static auto constexpr MaxAge = 60;
|
|
|
|
auto const date = tor->dateAdded();
|
|
|
|
return (date != 0) && (difftime(now, date) < MaxAge);
|
|
|
|
};
|
2019-11-09 14:44:40 +00:00
|
|
|
|
2019-11-09 23:02:23 +00:00
|
|
|
// build a list of the property keys
|
2022-09-08 23:26:18 +00:00
|
|
|
tr_variant* const first_child = tr_variantListChild(torrent_list, 0);
|
2023-08-21 21:16:54 +00:00
|
|
|
bool const table = first_child != nullptr && first_child->holds_alternative<tr_variant::Vector>();
|
2019-11-09 23:02:23 +00:00
|
|
|
std::vector<tr_quark> keys;
|
|
|
|
if (table)
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2019-11-09 23:02:23 +00:00
|
|
|
// In 'table' format, the first entry in 'torrents' is an array of keys.
|
|
|
|
// All the other entries are an array of the values for one torrent.
|
2021-11-14 23:28:37 +00:00
|
|
|
auto sv = std::string_view{};
|
2019-11-09 23:02:23 +00:00
|
|
|
size_t i = 0;
|
2020-05-27 21:53:12 +00:00
|
|
|
keys.reserve(tr_variantListSize(first_child));
|
2021-11-14 23:28:37 +00:00
|
|
|
while (tr_variantGetStrView(tr_variantListChild(first_child, i++), &sv))
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
2021-11-14 23:28:37 +00:00
|
|
|
keys.push_back(tr_quark_new(sv));
|
2019-11-09 23:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 22:46:21 +00:00
|
|
|
else if (first_child != nullptr)
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
|
|
|
// In 'object' format, every entry is an object with the same set of properties
|
2022-02-08 03:56:04 +00:00
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* value = nullptr;
|
2020-11-05 22:46:21 +00:00
|
|
|
for (size_t i = 0; tr_variantDictChild(first_child, i, &key, &value); ++i)
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
|
|
|
keys.push_back(key);
|
|
|
|
}
|
|
|
|
}
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2019-11-09 23:02:23 +00:00
|
|
|
// Find the position of TR_KEY_id so we can do torrent lookup
|
|
|
|
auto const id_it = std::find(std::begin(keys), std::end(keys), TR_KEY_id);
|
|
|
|
if (id_it == std::end(keys)) // no ids provided; we can't proceed
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const id_pos = std::distance(std::begin(keys), id_it);
|
|
|
|
|
|
|
|
// Loop through the torrent records...
|
|
|
|
std::vector<tr_variant*> values;
|
|
|
|
values.reserve(keys.size());
|
|
|
|
size_t tor_index = table ? 1 : 0;
|
2022-09-08 23:26:18 +00:00
|
|
|
processed.reserve(tr_variantListSize(torrent_list));
|
2022-02-08 03:56:04 +00:00
|
|
|
tr_variant* v = nullptr;
|
2022-09-08 23:26:18 +00:00
|
|
|
while ((v = tr_variantListChild(torrent_list, tor_index++)))
|
2019-11-09 23:02:23 +00:00
|
|
|
{
|
|
|
|
// Build an array of values
|
|
|
|
values.clear();
|
|
|
|
if (table)
|
|
|
|
{
|
|
|
|
// In table mode, v is already a list of values
|
|
|
|
size_t i = 0;
|
2022-02-08 03:56:04 +00:00
|
|
|
tr_variant* val = nullptr;
|
2019-11-09 23:02:23 +00:00
|
|
|
while ((val = tr_variantListChild(v, i++)))
|
|
|
|
{
|
|
|
|
values.push_back(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// In object mode, v is an object of torrent property key/vals
|
|
|
|
size_t i = 0;
|
2022-02-08 03:56:04 +00:00
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* value = nullptr;
|
2019-11-09 23:02:23 +00:00
|
|
|
while (tr_variantDictChild(v, i++, &key, &value))
|
|
|
|
{
|
|
|
|
values.push_back(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the torrent id
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const id = getValue<int>(values[id_pos]);
|
|
|
|
if (!id)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
Torrent* tor = getTorrentFromId(*id);
|
2019-11-12 01:37:05 +00:00
|
|
|
bool is_new = false;
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
if (tor == nullptr)
|
|
|
|
{
|
2023-07-18 15:20:17 +00:00
|
|
|
tor = new Torrent{ prefs_, *id };
|
2019-11-09 14:44:40 +00:00
|
|
|
instantiated.push_back(tor);
|
2019-11-12 01:37:05 +00:00
|
|
|
is_new = true;
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 21:11:16 +00:00
|
|
|
auto const fields = tor->update(keys.data(), values.data(), keys.size());
|
2020-06-18 20:34:11 +00:00
|
|
|
|
2020-06-23 21:11:16 +00:00
|
|
|
if (fields.any())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2020-06-23 21:11:16 +00:00
|
|
|
changed_fields |= fields;
|
2020-07-27 04:30:58 +00:00
|
|
|
changed.insert(*id);
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 21:11:16 +00:00
|
|
|
if (fields.test(Torrent::EDIT_DATE))
|
2020-06-18 20:34:11 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
edited.insert(*id);
|
2020-06-18 20:34:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (is_new && !tor->hasName())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
needinfo.insert(*id);
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
if (recently_added(tor) && tor->hasName() && !already_added_.count(*id))
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
added.insert(*id);
|
|
|
|
already_added_.insert(*id);
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 21:11:16 +00:00
|
|
|
if (fields.test(Torrent::LEFT_UNTIL_DONE) && (tor->leftUntilDone() == 0) && (tor->downloadedEver() > 0))
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
completed.insert(*id);
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
processed.push_back(tor);
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
2019-11-09 14:44:40 +00:00
|
|
|
|
|
|
|
// model upkeep
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!instantiated.empty())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
rowsAdd(instantiated);
|
|
|
|
}
|
2020-06-18 20:34:11 +00:00
|
|
|
|
|
|
|
if (!edited.empty())
|
|
|
|
{
|
|
|
|
emit torrentsEdited(edited);
|
|
|
|
}
|
2019-11-09 14:44:40 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!changed.empty())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
rowsEmitChanged(changed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// emit signals
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!added.empty())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
emit torrentsAdded(added);
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!needinfo.empty())
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
emit torrentsNeedInfo(needinfo);
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!changed.empty())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2020-06-23 21:11:16 +00:00
|
|
|
emit torrentsChanged(changed, changed_fields);
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!completed.empty())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
emit torrentsCompleted(completed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// model upkeep
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_complete_list)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
std::sort(processed.begin(), processed.end(), TorrentIdLessThan());
|
|
|
|
torrents_t removed;
|
|
|
|
removed.reserve(old.size());
|
|
|
|
std::set_difference(old.begin(), old.end(), processed.begin(), processed.end(), std::back_inserter(removed));
|
|
|
|
rowsRemove(removed);
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
std::optional<int> TorrentModel::getRow(int id) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
std::optional<int> row;
|
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
auto const [begin, end] = std::equal_range(torrents_.begin(), torrents_.end(), id, TorrentIdLessThan());
|
|
|
|
if (begin != end)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2022-01-24 00:53:35 +00:00
|
|
|
row = std::distance(torrents_.begin(), begin);
|
2020-05-27 21:53:12 +00:00
|
|
|
assert(torrents_[*row]->id() == id);
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return row;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
Torrent* TorrentModel::getTorrentFromId(int id)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
auto const row = getRow(id);
|
2020-05-27 21:53:12 +00:00
|
|
|
return row ? torrents_[*row] : nullptr;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
Torrent const* TorrentModel::getTorrentFromId(int id) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
auto const row = getRow(id);
|
2020-05-27 21:53:12 +00:00
|
|
|
return row ? torrents_[*row] : nullptr;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
std::vector<TorrentModel::span_t> TorrentModel::getSpans(torrent_ids_t const& ids) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
// ids -> rows
|
|
|
|
std::vector<int> rows;
|
|
|
|
rows.reserve(ids.size());
|
|
|
|
for (auto const& id : ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
auto const row = getRow(id);
|
|
|
|
if (row)
|
|
|
|
{
|
|
|
|
rows.push_back(*row);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
std::sort(rows.begin(), rows.end());
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
// rows -> spans
|
|
|
|
std::vector<span_t> spans;
|
|
|
|
spans.reserve(rows.size());
|
|
|
|
span_t span;
|
|
|
|
bool in_span = false;
|
|
|
|
for (auto const& row : rows)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
if (in_span)
|
|
|
|
{
|
|
|
|
if (span.second + 1 == row)
|
|
|
|
{
|
|
|
|
span.second = row;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spans.push_back(span);
|
|
|
|
in_span = false;
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
if (!in_span)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
span.first = span.second = row;
|
|
|
|
in_span = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
if (in_span)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
spans.push_back(span);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-29 21:05:05 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
return spans;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void TorrentModel::rowsEmitChanged(torrent_ids_t const& ids)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2022-01-24 00:53:35 +00:00
|
|
|
for (auto const& [first, last] : getSpans(ids))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-24 00:53:35 +00:00
|
|
|
emit dataChanged(index(first), index(last));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentModel::rowsAdd(torrents_t const& torrents)
|
|
|
|
{
|
|
|
|
auto const compare = TorrentIdLessThan();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (torrents_.empty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-07-18 15:20:17 +00:00
|
|
|
beginInsertRows(QModelIndex{}, 0, torrents.size() - 1);
|
2020-05-27 21:53:12 +00:00
|
|
|
torrents_ = torrents;
|
2023-07-18 15:20:17 +00:00
|
|
|
std::sort(torrents_.begin(), torrents_.end(), TorrentIdLessThan{});
|
2019-11-09 14:44:40 +00:00
|
|
|
endInsertRows();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2019-11-09 14:44:40 +00:00
|
|
|
else
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
for (auto const& tor : torrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
Qt 6 support (#2069)
* Bump minimum Qt version to 5.6
* Switch from QRegExp to QRegularExpression
While still available, QRegExp has been moved to Qt6::Core5Compat module
and is not part of Qt6::Core.
* Use qIsEffectiveTLD instead of QUrl::topLevelDomain
The latter is not part of Qt6::Core. The former is a private utility in
Qt6::Network; using it for now, until (and if) we switch to something
non-Qt-specific.
* Use QStyle::State_Horizontal state when drawing progress bars
Although available for a long time, this state either didn't apply to
progress bars before Qt 6, or was deduced based on bar size. With Qt 6,
failing to specify it results in bad rendering.
* Don't use QStringRef (and associated methods)
While still available, QStringRef has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core. Related method (e.g.
QString::midRef) have been removed in Qt 6.
* Use Qt::ItemIsAutoTristate instead of Qt::ItemIsTristate
The latter was deprecated and replaced with the former in Qt 5.6.
* Don't use QApplication::globalStrut
This property has been deprecated in Qt 5.15 and removed in Qt 6.
* Use QImage::fromHICON instead of QtWin::fromHICON
WinExtras module (providind the latter helper) has been removed in Qt 6.
* Use QStringDecoder instead of QTextCodec
While still available, QTextCodec has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core.
* Don't forward-declare QStringList
Instead of being a standalone class, its definition has changed to
QList<QString> template specialization in Qt 6.
* Use explicit (since Qt 6) QFileInfo constructor
* Use QDateTime's {to,from}SecsSinceEpoch instead of {to,from}Time_t
The latter was deprecated in Qt 5.8 and removed in Qt 6.
* Don't use QFuture<>'s operator==
It has been removed in Qt 6. Since the original issue this code was
solving was caused by future reuse, just don't reuse futures and create
new finished ones when necessary.
* Use std::vector<> instead of QVector<>
The latter has been changed to a typedef for QList<>, which might not be
what one wants, and which also changed behavior a bit leading to
compilation errors.
* Don't use + for flags, cast to int explicitly
Operator+ for enum values has been deleted in Qt 6, so using operator|
instead. Then, there's no conversion from QFlags<> to QVariant, so need
to cast to int.
* Support Qt 6 in CMake and for MSI packaging
* Remove extra (empty) CMake variable use when constructing Qt target names
* Simplify logic in tr_qt_add_translation CMake helper
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2021-11-03 21:20:11 +00:00
|
|
|
auto const it = std::lower_bound(torrents_.begin(), torrents_.end(), tor, compare);
|
2020-11-02 15:16:12 +00:00
|
|
|
auto const row = static_cast<int>(std::distance(torrents_.begin(), it));
|
2019-11-09 14:44:40 +00:00
|
|
|
|
2023-07-18 15:20:17 +00:00
|
|
|
beginInsertRows(QModelIndex{}, row, row);
|
2020-05-27 21:53:12 +00:00
|
|
|
torrents_.insert(it, tor);
|
2019-11-09 14:44:40 +00:00
|
|
|
endInsertRows();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void TorrentModel::rowsRemove(torrents_t const& torrents)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
// must walk in reverse to avoid invalidating row numbers
|
|
|
|
auto const& spans = getSpans(getIds(torrents.begin(), torrents.end()));
|
|
|
|
for (auto it = spans.rbegin(), end = spans.rend(); it != end; ++it)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-24 00:53:35 +00:00
|
|
|
auto const& [first, last] = *it;
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2023-07-18 15:20:17 +00:00
|
|
|
beginRemoveRows(QModelIndex{}, first, last);
|
2022-01-24 00:53:35 +00:00
|
|
|
torrents_.erase(torrents_.begin() + first, torrents_.begin() + last + 1);
|
2019-11-09 14:44:40 +00:00
|
|
|
endRemoveRows();
|
|
|
|
}
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
qDeleteAll(torrents);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-09-07 21:19:10 +00:00
|
|
|
bool TorrentModel::hasTorrent(TorrentHash const& hash) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto test = [hash](auto const& tor)
|
|
|
|
{
|
|
|
|
return tor->hash() == hash;
|
|
|
|
};
|
2020-05-27 21:53:12 +00:00
|
|
|
return std::any_of(torrents_.cbegin(), torrents_.cend(), test);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|