transmission/qt/TorrentModel.cc

482 lines
11 KiB
C++
Raw Normal View History

// This file Copyright © 2009-2022 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.
2009-04-09 18:55:47 +00:00
#include <algorithm>
#include <cassert>
#include <string_view>
#include <utility>
2009-04-09 18:55:47 +00:00
#include <libtransmission/transmission.h>
#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"
#include "TorrentDelegate.h"
#include "TorrentModel.h"
#include "VariantHelpers.h"
using ::trqt::variant_helpers::getValue;
2009-04-09 18:55:47 +00:00
/***
****
***/
namespace
{
struct TorrentIdLessThan
{
bool operator()(Torrent const* left, Torrent const* right) const
{
return left->id() < right->id();
}
bool operator()(int left_id, Torrent const* right) const
{
return left_id < right->id();
}
bool operator()(Torrent const* left, int right_id) const
{
return left->id() < right_id;
}
};
template<typename Iter>
auto getIds(Iter it, Iter end)
{
torrent_ids_t ids;
for (; it != end; ++it)
{
ids.insert((*it)->id());
}
return ids;
}
} // namespace
/***
****
***/
TorrentModel::TorrentModel(Prefs const& prefs)
: prefs_(prefs)
{
}
TorrentModel::~TorrentModel()
{
clear();
}
void TorrentModel::clear()
{
beginResetModel();
qDeleteAll(torrents_);
torrents_.clear();
endResetModel();
}
int TorrentModel::rowCount(QModelIndex const& parent) const
2009-04-09 18:55:47 +00:00
{
Q_UNUSED(parent)
2009-04-09 18:55:47 +00:00
return torrents_.size();
2009-04-09 18:55:47 +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
if (t != nullptr)
2009-04-09 18:55:47 +00:00
{
switch (role)
{
case Qt::DisplayRole:
return t->name();
2009-04-09 18:55:47 +00:00
case Qt::DecorationRole:
return t->getMimeTypeIcon();
2009-04-09 18:55:47 +00:00
case TorrentRole:
return QVariant::fromValue(t);
2009-04-09 18:55:47 +00:00
default:
2009-04-09 18:55:47 +00:00
break;
}
2009-04-09 18:55:47 +00:00
}
return {};
2009-04-09 18:55:47 +00:00
}
/***
****
***/
void TorrentModel::removeTorrents(tr_variant* list)
2009-04-09 18:55:47 +00:00
{
torrents_t torrents;
torrents.reserve(tr_variantListSize(list));
int i = 0;
tr_variant const* child = nullptr;
while ((child = tr_variantListChild(list, i++)) != nullptr)
{
if (auto const id = getValue<int>(child); id)
{
if (auto* const torrent = getTorrentFromId(*id); torrent != nullptr)
{
torrents.push_back(torrent);
}
}
}
if (!torrents.empty())
{
rowsRemove(torrents);
}
}
void TorrentModel::updateTorrents(tr_variant* torrents, bool is_complete_list)
{
auto const old = is_complete_list ? torrents_ : torrents_t{};
auto added = torrent_ids_t{};
auto changed = torrent_ids_t{};
auto completed = torrent_ids_t{};
auto edited = torrent_ids_t{};
auto instantiated = torrents_t{};
auto needinfo = torrent_ids_t{};
auto processed = torrents_t{};
auto changed_fields = Torrent::fields_t{};
auto const now = time(nullptr);
auto const recently_added = [&now](auto const& tor)
{
static auto constexpr MaxAge = 60;
auto const date = tor->dateAdded();
return (date != 0) && (difftime(now, date) < MaxAge);
};
// build a list of the property keys
tr_variant* const first_child = tr_variantListChild(torrents, 0);
bool const table = tr_variantIsList(first_child);
std::vector<tr_quark> keys;
if (table)
{
// 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.
auto sv = std::string_view{};
size_t i = 0;
keys.reserve(tr_variantListSize(first_child));
while (tr_variantGetStrView(tr_variantListChild(first_child, i++), &sv))
{
keys.push_back(tr_quark_new(sv));
}
}
else if (first_child != nullptr)
{
// In 'object' format, every entry is an object with the same set of properties
auto key = tr_quark{};
tr_variant* value = nullptr;
for (size_t i = 0; tr_variantDictChild(first_child, i, &key, &value); ++i)
{
keys.push_back(key);
}
}
// 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;
processed.reserve(tr_variantListSize(torrents));
tr_variant* v = nullptr;
while ((v = tr_variantListChild(torrents, tor_index++)))
{
// Build an array of values
values.clear();
if (table)
{
// In table mode, v is already a list of values
size_t i = 0;
tr_variant* val = nullptr;
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;
auto key = tr_quark{};
tr_variant* value = nullptr;
while (tr_variantDictChild(v, i++, &key, &value))
{
values.push_back(value);
}
}
// Find the torrent id
auto const id = getValue<int>(values[id_pos]);
if (!id)
{
continue;
}
Torrent* tor = getTorrentFromId(*id);
bool is_new = false;
if (tor == nullptr)
{
tor = new Torrent(prefs_, *id);
instantiated.push_back(tor);
is_new = true;
}
auto const fields = tor->update(keys.data(), values.data(), keys.size());
if (fields.any())
{
changed_fields |= fields;
changed.insert(*id);
}
if (fields.test(Torrent::EDIT_DATE))
{
edited.insert(*id);
}
if (is_new && !tor->hasName())
{
needinfo.insert(*id);
}
if (recently_added(tor) && tor->hasName() && !already_added_.count(*id))
{
added.insert(*id);
already_added_.insert(*id);
}
if (fields.test(Torrent::LEFT_UNTIL_DONE) && (tor->leftUntilDone() == 0) && (tor->downloadedEver() > 0))
{
completed.insert(*id);
}
processed.push_back(tor);
}
// model upkeep
if (!instantiated.empty())
{
rowsAdd(instantiated);
}
if (!edited.empty())
{
emit torrentsEdited(edited);
}
if (!changed.empty())
{
rowsEmitChanged(changed);
}
// emit signals
if (!added.empty())
{
emit torrentsAdded(added);
}
if (!needinfo.empty())
{
emit torrentsNeedInfo(needinfo);
}
if (!changed.empty())
{
emit torrentsChanged(changed, changed_fields);
}
if (!completed.empty())
{
emit torrentsCompleted(completed);
}
// model upkeep
if (is_complete_list)
{
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);
}
2009-04-09 18:55:47 +00:00
}
/***
****
***/
std::optional<int> TorrentModel::getRow(int id) const
2009-04-09 18:55:47 +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)
{
2022-01-24 00:53:35 +00:00
row = std::distance(torrents_.begin(), begin);
assert(torrents_[*row]->id() == id);
}
return row;
2009-04-09 18:55:47 +00:00
}
Torrent* TorrentModel::getTorrentFromId(int id)
2009-04-09 18:55:47 +00:00
{
auto const row = getRow(id);
return row ? torrents_[*row] : nullptr;
2009-04-09 18:55:47 +00:00
}
Torrent const* TorrentModel::getTorrentFromId(int id) const
2009-04-09 18:55:47 +00:00
{
auto const row = getRow(id);
return row ? torrents_[*row] : nullptr;
2009-04-09 18:55:47 +00:00
}
/***
****
***/
std::vector<TorrentModel::span_t> TorrentModel::getSpans(torrent_ids_t const& ids) const
2009-04-09 18:55:47 +00:00
{
// ids -> rows
std::vector<int> rows;
rows.reserve(ids.size());
for (auto const& id : ids)
{
auto const row = getRow(id);
if (row)
{
rows.push_back(*row);
}
}
std::sort(rows.begin(), rows.end());
// 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
{
if (in_span)
{
if (span.second + 1 == row)
{
span.second = row;
}
else
{
spans.push_back(span);
in_span = false;
}
}
if (!in_span)
{
span.first = span.second = row;
in_span = true;
}
2009-04-09 18:55:47 +00:00
}
if (in_span)
{
spans.push_back(span);
}
return spans;
}
/***
****
***/
2009-04-09 18:55:47 +00:00
void TorrentModel::rowsEmitChanged(torrent_ids_t const& ids)
{
2022-01-24 00:53:35 +00:00
for (auto const& [first, last] : getSpans(ids))
{
2022-01-24 00:53:35 +00:00
emit dataChanged(index(first), index(last));
}
}
void TorrentModel::rowsAdd(torrents_t const& torrents)
{
auto const compare = TorrentIdLessThan();
2009-04-09 18:55:47 +00:00
if (torrents_.empty())
{
beginInsertRows(QModelIndex(), 0, torrents.size() - 1);
torrents_ = torrents;
std::sort(torrents_.begin(), torrents_.end(), TorrentIdLessThan());
endInsertRows();
}
else
2009-04-09 18:55:47 +00:00
{
for (auto const& tor : torrents)
{
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);
auto const row = static_cast<int>(std::distance(torrents_.begin(), it));
beginInsertRows(QModelIndex(), row, row);
torrents_.insert(it, tor);
endInsertRows();
}
2009-04-09 18:55:47 +00:00
}
}
void TorrentModel::rowsRemove(torrents_t const& torrents)
2009-04-09 18:55:47 +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)
{
2022-01-24 00:53:35 +00:00
auto const& [first, last] = *it;
2022-01-24 00:53:35 +00:00
beginRemoveRows(QModelIndex(), first, last);
torrents_.erase(torrents_.begin() + first, torrents_.begin() + last + 1);
endRemoveRows();
}
qDeleteAll(torrents);
2009-04-09 18:55:47 +00:00
}
/***
****
***/
bool TorrentModel::hasTorrent(TorrentHash const& hash) const
2009-04-09 18:55:47 +00:00
{
auto test = [hash](auto const& tor)
{
return tor->hash() == hash;
};
return std::any_of(torrents_.cbegin(), torrents_.cend(), test);
2009-04-09 18:55:47 +00:00
}