2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
#include <algorithm>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <iostream>
|
2019-11-09 14:44:40 +00:00
|
|
|
#include <utility>
|
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"
|
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
|
|
|
|
{
|
|
|
|
bool operator ()(Torrent* left, Torrent* 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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool operator ()(int leftId, Torrent* right) const
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return leftId < right->id();
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool operator ()(Torrent* left, int rightId) const
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return left->id() < rightId;
|
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
|
|
|
|
|
|
|
for ( ; it != end; ++it)
|
|
|
|
{
|
|
|
|
ids.insert((*it)->id());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
TorrentModel::TorrentModel(Prefs const& prefs) :
|
|
|
|
myPrefs(prefs)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
qDeleteAll(myTorrents);
|
|
|
|
myTorrents.clear();
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return myTorrents.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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QVariant var;
|
|
|
|
|
2017-04-30 09:29:58 +00:00
|
|
|
Torrent const* t = myTorrents.value(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:
|
|
|
|
var.setValue(t->name());
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case Qt::DecorationRole:
|
|
|
|
var.setValue(t->getMimeTypeIcon());
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TorrentRole:
|
2012-12-29 01:20:22 +00:00
|
|
|
var = qVariantFromValue(t);
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
// std::cerr << "Unhandled role: " << role << std::endl;
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return var;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
void TorrentModel::removeTorrents(tr_variant* list)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
torrents_t torrents;
|
|
|
|
torrents.reserve(tr_variantListSize(list));
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
int i = 0;
|
|
|
|
tr_variant* child;
|
|
|
|
while ((child = tr_variantListChild(list, i++)) != nullptr)
|
|
|
|
{
|
|
|
|
int64_t id;
|
|
|
|
Torrent* torrent = nullptr;
|
|
|
|
|
|
|
|
if (tr_variantGetInt(child, &id))
|
|
|
|
{
|
|
|
|
torrent = getTorrentFromId(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (torrent != nullptr)
|
|
|
|
{
|
2019-11-12 01:37:05 +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
|
|
|
}
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
void TorrentModel::updateTorrents(tr_variant* torrents, bool isCompleteList)
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
auto const old = isCompleteList ? myTorrents : torrents_t{};
|
|
|
|
auto added = torrent_ids_t{};
|
|
|
|
auto changed = torrent_ids_t{};
|
|
|
|
auto completed = 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{};
|
2019-11-09 14:44:40 +00:00
|
|
|
|
|
|
|
auto const now = time(nullptr);
|
|
|
|
auto const recently_added = [now](auto const& tor)
|
|
|
|
{
|
|
|
|
static auto constexpr max_age = 60;
|
|
|
|
auto const date = tor->dateAdded();
|
|
|
|
return (date != 0) && (difftime(now, date) < max_age);
|
|
|
|
};
|
|
|
|
|
2019-11-09 23:02:23 +00:00
|
|
|
// build a list of the property keys
|
|
|
|
tr_variant* const firstChild = tr_variantListChild(torrents, 0);
|
|
|
|
bool const table = tr_variantIsList(firstChild);
|
|
|
|
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.
|
|
|
|
char const* str;
|
|
|
|
size_t len;
|
|
|
|
size_t i = 0;
|
2019-11-12 01:37:05 +00:00
|
|
|
keys.reserve(tr_variantListSize(firstChild));
|
2019-11-09 23:02:23 +00:00
|
|
|
while (tr_variantGetStr(tr_variantListChild(firstChild, i++), &str, &len))
|
|
|
|
{
|
|
|
|
keys.push_back(tr_quark_new(str, len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// In 'object' format, every entry is an object with the same set of properties
|
|
|
|
size_t i = 0;
|
|
|
|
tr_quark key;
|
|
|
|
tr_variant* value;
|
|
|
|
while (firstChild && tr_variantDictChild(firstChild, i++, &key, &value))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
tr_variant* v;
|
2019-11-12 01:37:05 +00:00
|
|
|
processed.reserve(tr_variantListSize(torrents));
|
2019-11-09 23:02:23 +00:00
|
|
|
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;
|
|
|
|
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;
|
|
|
|
tr_quark key;
|
|
|
|
tr_variant* value;
|
|
|
|
while (tr_variantDictChild(v, i++, &key, &value))
|
|
|
|
{
|
|
|
|
values.push_back(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the torrent id
|
|
|
|
int64_t id;
|
|
|
|
if (!tr_variantGetInt(values[id_pos], &id))
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Torrent* tor = getTorrentFromId(id);
|
|
|
|
std::optional<uint64_t> leftUntilDone;
|
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)
|
|
|
|
{
|
|
|
|
tor = new Torrent(myPrefs, id);
|
|
|
|
instantiated.push_back(tor);
|
2019-11-12 01:37:05 +00:00
|
|
|
is_new = true;
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
leftUntilDone = tor->leftUntilDone();
|
|
|
|
}
|
|
|
|
|
2019-11-09 23:02:23 +00:00
|
|
|
if (tor->update(keys.data(), values.data(), keys.size()))
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
changed.insert(id);
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (is_new && !tor->hasName())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
needinfo.insert(id);
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (recently_added(tor) && tor->hasName() && !myAlreadyAdded.count(id))
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
added.insert(id);
|
|
|
|
myAlreadyAdded.insert(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leftUntilDone && (*leftUntilDone > 0) && (tor->leftUntilDone() == 0) && (tor->downloadedEver() > 0))
|
|
|
|
{
|
|
|
|
completed.insert(id);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
emit torrentsChanged(changed);
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!completed.empty())
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
emit torrentsCompleted(completed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// model upkeep
|
|
|
|
|
|
|
|
if (isCompleteList)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
auto const it = std::equal_range(myTorrents.begin(), myTorrents.end(), id, TorrentIdLessThan());
|
|
|
|
if (it.first != it.second)
|
|
|
|
{
|
|
|
|
row = std::distance(myTorrents.begin(), it.first);
|
|
|
|
assert(myTorrents[*row]->id() == id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return row;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
std::optional<int> TorrentModel::getRow(Torrent const* tor) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
return getRow(tor->id());
|
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);
|
|
|
|
return row ? myTorrents[*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);
|
|
|
|
return row ? myTorrents[*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
|
|
|
{
|
|
|
|
for (auto const& span : getSpans(ids))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
emit dataChanged(index(span.first), index(span.second));
|
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
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if (myTorrents.empty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
beginInsertRows(QModelIndex(), 0, torrents.size() - 1);
|
|
|
|
myTorrents = torrents;
|
|
|
|
std::sort(myTorrents.begin(), myTorrents.end(), TorrentIdLessThan());
|
|
|
|
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
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
auto const it = std::lower_bound(myTorrents.begin(), myTorrents.end(), tor, compare);
|
|
|
|
auto const row = std::distance(myTorrents.begin(), it);
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), row, row);
|
|
|
|
myTorrents.insert(it, tor);
|
|
|
|
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
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
auto const& span = *it;
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
beginRemoveRows(QModelIndex(), span.first, span.second);
|
|
|
|
auto const n = span.second + 1 - span.first;
|
|
|
|
myTorrents.remove(span.first, n);
|
|
|
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool TorrentModel::hasTorrent(QString const& hashString) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
auto test = [hashString](auto const& tor) { return tor->hashString() == hashString; };
|
|
|
|
return std::any_of(myTorrents.cbegin(), myTorrents.cend(), test);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|