transmission/qt/TorrentModel.cc

332 lines
7.0 KiB
C++
Raw Normal View History

2009-04-09 18:55:47 +00:00
/*
* This file Copyright (C) 2009-2015 Mnemosyne LLC
2009-04-09 18:55:47 +00:00
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
2009-04-09 18:55:47 +00:00
*
*/
#include <iostream>
#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"
2009-04-09 18:55:47 +00:00
namespace
{
struct TorrentIdLessThan
{
bool operator ()(Torrent* left, Torrent* right) const
{
return left->id() < right->id();
}
bool operator ()(int leftId, Torrent* right) const
{
return leftId < right->id();
}
bool operator ()(Torrent* left, int rightId) const
{
return left->id() < rightId;
}
};
} // namespace
void TorrentModel::clear()
{
beginResetModel();
2013-07-27 21:58:14 +00:00
qDeleteAll(myTorrents);
myTorrents.clear();
2013-07-27 21:58:14 +00:00
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 myTorrents.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
{
QVariant var;
Torrent const* t = myTorrents.value(index.row(), 0);
2009-04-09 18:55:47 +00:00
if (t != 0)
2009-04-09 18:55:47 +00:00
{
switch (role)
{
case Qt::DisplayRole:
var.setValue(t->name());
2009-04-09 18:55:47 +00:00
break;
case Qt::DecorationRole:
var.setValue(t->getMimeTypeIcon());
2009-04-09 18:55:47 +00:00
break;
case TorrentRole:
var = qVariantFromValue(t);
2009-04-09 18:55:47 +00:00
break;
default:
// std::cerr << "Unhandled role: " << role << std::endl;
2009-04-09 18:55:47 +00:00
break;
}
2009-04-09 18:55:47 +00:00
}
return var;
2009-04-09 18:55:47 +00:00
}
/***
****
***/
void TorrentModel::addTorrent(Torrent* t)
2009-04-09 18:55:47 +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();
beginInsertRows(QModelIndex(), row, row);
myTorrents.insert(torrentIt, t);
endInsertRows();
}
void TorrentModel::addTorrents(torrents_t&& torrents, QSet<int>& addIds)
{
if (myTorrents.isEmpty())
{
qSort(torrents.begin(), torrents.end(), TorrentIdLessThan());
beginInsertRows(QModelIndex(), 0, torrents.size() - 1);
myTorrents.swap(torrents);
endInsertRows();
addIds += getIds();
}
else
{
for (Torrent* const tor : torrents)
{
addTorrent(tor);
addIds.insert(tor->id());
}
}
2009-04-09 18:55:47 +00:00
}
TorrentModel::TorrentModel(Prefs const& prefs) :
myPrefs(prefs)
2009-04-09 18:55:47 +00:00
{
}
TorrentModel::~TorrentModel()
2009-04-09 18:55:47 +00:00
{
clear();
2009-04-09 18:55:47 +00:00
}
/***
****
***/
Torrent* TorrentModel::getTorrentFromId(int id)
2009-04-09 18:55:47 +00:00
{
torrents_t::const_iterator const torrentIt = qBinaryFind(myTorrents.begin(), myTorrents.end(), id, TorrentIdLessThan());
return torrentIt == myTorrents.end() ? nullptr : *torrentIt;
2009-04-09 18:55:47 +00:00
}
Torrent const* TorrentModel::getTorrentFromId(int id) const
2009-04-09 18:55:47 +00:00
{
torrents_t::const_iterator const torrentIt = qBinaryFind(myTorrents.begin(), myTorrents.end(), id, TorrentIdLessThan());
return torrentIt == myTorrents.end() ? nullptr : *torrentIt;
2009-04-09 18:55:47 +00:00
}
/***
****
***/
void TorrentModel::onTorrentChanged(int torrentId)
2009-04-09 18:55:47 +00:00
{
torrents_t::iterator const torrentIt = qBinaryFind(myTorrents.begin(), myTorrents.end(), torrentId, TorrentIdLessThan());
if (torrentIt == myTorrents.end())
{
return;
}
int const row = torrentIt - myTorrents.begin();
QModelIndex const qmi(index(row, 0));
emit dataChanged(qmi, qmi);
2009-04-09 18:55:47 +00:00
}
void TorrentModel::removeTorrents(tr_variant* torrents)
2009-04-09 18:55:47 +00:00
{
int i = 0;
tr_variant* child;
while ((child = tr_variantListChild(torrents, i++)))
2013-09-14 22:45:04 +00:00
{
int64_t intVal;
if (tr_variantGetInt(child, &intVal))
{
removeTorrent(intVal);
}
2009-04-09 18:55:47 +00:00
}
}
void TorrentModel::updateTorrents(tr_variant* torrents, bool isCompleteList)
2009-04-09 18:55:47 +00:00
{
torrents_t newTorrents;
QSet<int> oldIds;
QSet<int> addIds;
QSet<int> newIds;
2009-04-09 18:55:47 +00:00
if (isCompleteList)
{
oldIds = getIds();
}
if (tr_variantIsList(torrents))
2009-04-09 18:55:47 +00:00
{
size_t i(0);
tr_variant* child;
while ((child = tr_variantListChild(torrents, i++)))
2009-04-09 18:55:47 +00:00
{
int64_t id;
if (tr_variantDictFindInt(child, TR_KEY_id, &id))
2009-04-09 18:55:47 +00:00
{
if (isCompleteList)
{
newIds.insert(id);
}
Torrent* tor = getTorrentFromId(id);
2009-04-09 18:55:47 +00:00
if (tor == 0)
2009-04-09 18:55:47 +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
}
else
2009-04-09 18:55:47 +00:00
{
tor->update(child);
if (tor->isMagnet() && tor->hasMetadata())
{
addIds.insert(tor->id());
tor->setMagnet(false);
}
2009-04-09 18:55:47 +00:00
}
}
}
}
if (!newTorrents.isEmpty())
{
addTorrents(std::move(newTorrents), addIds);
}
2009-04-09 18:55:47 +00:00
if (!addIds.isEmpty())
{
emit torrentsAdded(addIds);
}
if (isCompleteList)
2009-04-09 18:55:47 +00:00
{
QSet<int> removedIds(oldIds);
removedIds -= newIds;
for (int const id : removedIds)
{
removeTorrent(id);
}
2009-04-09 18:55:47 +00:00
}
}
void TorrentModel::removeTorrent(int id)
2009-04-09 18:55:47 +00:00
{
torrents_t::iterator const torrentIt = qBinaryFind(myTorrents.begin(), myTorrents.end(), id, TorrentIdLessThan());
if (torrentIt == myTorrents.end())
{
return;
}
Torrent* const tor = *torrentIt;
int const row = torrentIt - myTorrents.begin();
beginRemoveRows(QModelIndex(), row, row);
myTorrents.remove(row);
endRemoveRows();
delete tor;
2009-04-09 18:55:47 +00:00
}
void TorrentModel::getTransferSpeed(Speed& uploadSpeed, size_t& uploadPeerCount, Speed& downloadSpeed,
size_t& downloadPeerCount)
2009-04-09 18:55:47 +00:00
{
Speed upSpeed, downSpeed;
size_t upCount = 0, downCount = 0;
2009-04-09 18:55:47 +00:00
for (Torrent const* const tor : myTorrents)
{
upSpeed += tor->uploadSpeed();
upCount += tor->peersWeAreUploadingTo();
downSpeed += tor->downloadSpeed();
downCount += tor->webseedsWeAreDownloadingFrom();
downCount += tor->peersWeAreDownloadingFrom();
}
uploadSpeed = upSpeed;
uploadPeerCount = upCount;
downloadSpeed = downSpeed;
downloadPeerCount = downCount;
2009-04-09 18:55:47 +00:00
}
QSet<int> TorrentModel::getIds() const
2009-04-09 18:55:47 +00:00
{
QSet<int> ids;
ids.reserve(myTorrents.size());
for (Torrent const* const tor : myTorrents)
{
ids.insert(tor->id());
}
return ids;
2009-04-09 18:55:47 +00:00
}
bool TorrentModel::hasTorrent(QString const& hashString) const
2009-04-09 18:55:47 +00:00
{
for (Torrent const* const tor : myTorrents)
{
if (tor->hashString() == hashString)
{
return true;
}
}
2013-09-14 22:45:04 +00:00
return false;
2009-04-09 18:55:47 +00:00
}