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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#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
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void TorrentModel::clear()
|
2009-05-03 17:37:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
beginResetModel();
|
2013-07-27 21:58:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
qDeleteAll(myTorrents);
|
|
|
|
myTorrents.clear();
|
2013-07-27 21:58:14 +00:00
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentModel::addTorrent(Torrent* t)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
torrents_t::iterator const torrentIt = qLowerBound(myTorrents.begin(), myTorrents.end(), t, TorrentIdLessThan());
|
|
|
|
int const row = torrentIt == myTorrents.end() ? myTorrents.size() : torrentIt - myTorrents.begin();
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
beginInsertRows(QModelIndex(), row, row);
|
|
|
|
myTorrents.insert(torrentIt, t);
|
|
|
|
endInsertRows();
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentModel::addTorrents(torrents_t&& torrents, QSet<int>& addIds)
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (myTorrents.isEmpty())
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
qSort(torrents.begin(), torrents.end(), TorrentIdLessThan());
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
beginInsertRows(QModelIndex(), 0, torrents.size() - 1);
|
|
|
|
myTorrents.swap(torrents);
|
|
|
|
endInsertRows();
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
addIds += getIds();
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (Torrent* const tor : torrents)
|
2016-04-24 07:56:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
addTorrent(tor);
|
|
|
|
addIds.insert(tor->id());
|
2016-04-24 07:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
TorrentModel::TorrentModel(Prefs const& prefs) :
|
2017-04-19 12:04:45 +00:00
|
|
|
myPrefs(prefs)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
TorrentModel::~TorrentModel()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
clear();
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
torrents_t::const_iterator const torrentIt = qBinaryFind(myTorrents.begin(), myTorrents.end(), id, TorrentIdLessThan());
|
2017-04-19 12:04:45 +00:00
|
|
|
return torrentIt == myTorrents.end() ? nullptr : *torrentIt;
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
torrents_t::const_iterator const torrentIt = qBinaryFind(myTorrents.begin(), myTorrents.end(), id, TorrentIdLessThan());
|
2017-04-19 12:04:45 +00:00
|
|
|
return torrentIt == myTorrents.end() ? nullptr : *torrentIt;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentModel::onTorrentChanged(int torrentId)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
torrents_t::iterator const torrentIt = qBinaryFind(myTorrents.begin(), myTorrents.end(), torrentId, TorrentIdLessThan());
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (torrentIt == myTorrents.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const row = torrentIt - myTorrents.begin();
|
|
|
|
QModelIndex const qmi(index(row, 0));
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit dataChanged(qmi, qmi);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentModel::removeTorrents(tr_variant* torrents)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int i = 0;
|
|
|
|
tr_variant* child;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((child = tr_variantListChild(torrents, i++)) != nullptr)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t intVal;
|
|
|
|
|
|
|
|
if (tr_variantGetInt(child, &intVal))
|
|
|
|
{
|
|
|
|
removeTorrent(intVal);
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentModel::updateTorrents(tr_variant* torrents, bool isCompleteList)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrents_t newTorrents;
|
|
|
|
QSet<int> oldIds;
|
|
|
|
QSet<int> addIds;
|
|
|
|
QSet<int> newIds;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isCompleteList)
|
|
|
|
{
|
|
|
|
oldIds = getIds();
|
|
|
|
}
|
2012-12-29 21:05:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantIsList(torrents))
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t i(0);
|
|
|
|
tr_variant* child;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((child = tr_variantListChild(torrents, i++)) != nullptr)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t id;
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(child, TR_KEY_id, &id))
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isCompleteList)
|
|
|
|
{
|
|
|
|
newIds.insert(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Torrent* tor = getTorrentFromId(id);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-30 09:29:58 +00:00
|
|
|
if (tor == nullptr)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor = new Torrent(myPrefs, id);
|
|
|
|
tor->update(child);
|
|
|
|
|
|
|
|
if (!tor->hasMetadata())
|
|
|
|
{
|
|
|
|
tor->setMagnet(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
newTorrents.append(tor);
|
|
|
|
connect(tor, SIGNAL(torrentChanged(int)), this, SLOT(onTorrentChanged(int)));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->update(child);
|
|
|
|
|
|
|
|
if (tor->isMagnet() && tor->hasMetadata())
|
2010-06-16 03:02:17 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
addIds.insert(tor->id());
|
|
|
|
tor->setMagnet(false);
|
2010-06-16 03:02:17 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!newTorrents.isEmpty())
|
|
|
|
{
|
|
|
|
addTorrents(std::move(newTorrents), addIds);
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!addIds.isEmpty())
|
|
|
|
{
|
|
|
|
emit torrentsAdded(addIds);
|
|
|
|
}
|
2010-06-16 03:02:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isCompleteList)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QSet<int> removedIds(oldIds);
|
|
|
|
removedIds -= newIds;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (int const id : removedIds)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
removeTorrent(id);
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentModel::removeTorrent(int id)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
torrents_t::iterator const torrentIt = qBinaryFind(myTorrents.begin(), myTorrents.end(), id, TorrentIdLessThan());
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (torrentIt == myTorrents.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Torrent* const tor = *torrentIt;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const row = torrentIt - myTorrents.begin();
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
myTorrents.remove(row);
|
|
|
|
endRemoveRows();
|
2016-04-24 07:56:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
delete tor;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TorrentModel::getTransferSpeed(Speed& uploadSpeed, size_t& uploadPeerCount, Speed& downloadSpeed,
|
|
|
|
size_t& downloadPeerCount)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
Speed upSpeed;
|
|
|
|
Speed downSpeed;
|
|
|
|
size_t upCount = 0;
|
|
|
|
size_t downCount = 0;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (Torrent const* const tor : myTorrents)
|
2013-02-07 21:07:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
upSpeed += tor->uploadSpeed();
|
|
|
|
upCount += tor->peersWeAreUploadingTo();
|
|
|
|
downSpeed += tor->downloadSpeed();
|
|
|
|
downCount += tor->webseedsWeAreDownloadingFrom();
|
|
|
|
downCount += tor->peersWeAreDownloadingFrom();
|
2013-02-07 21:07:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
uploadSpeed = upSpeed;
|
|
|
|
uploadPeerCount = upCount;
|
|
|
|
downloadSpeed = downSpeed;
|
|
|
|
downloadPeerCount = downCount;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QSet<int> TorrentModel::getIds() const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QSet<int> ids;
|
2012-12-29 21:05:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ids.reserve(myTorrents.size());
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (Torrent const* const tor : myTorrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ids.insert(tor->id());
|
|
|
|
}
|
2012-12-29 21:05:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ids;
|
2009-04-09 18:55:47 +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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
for (Torrent const* const tor : myTorrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (tor->hashString() == hashString)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|