transmission/qt/tracker-model.cc

167 lines
3.9 KiB
C++
Raw Normal View History

/*
* This file Copyright (C) 2009-2014 Mnemosyne LLC
*
* It may be used under the GNU Public License v2 or v3 licenses,
* or any future license endorsed by Mnemosyne LLC.
*
* $Id$
*/
#include <algorithm> // std::sort()
#include <QUrl>
#include "app.h" // MyApp
#include "tracker-model.h"
int
TrackerModel::rowCount (const QModelIndex& parent) const
{
2013-09-14 22:45:04 +00:00
Q_UNUSED (parent);
2013-09-14 22:45:04 +00:00
return parent.isValid() ? 0 : myRows.size();
}
QVariant
TrackerModel::data (const QModelIndex& index, int role) const
{
2013-09-14 22:45:04 +00:00
QVariant var;
2013-09-14 22:45:04 +00:00
const int row = index.row ();
if ((0<=row) && (row<myRows.size()))
{
2013-09-14 22:45:04 +00:00
const TrackerInfo& trackerInfo = myRows.at (row);
2013-09-14 22:45:04 +00:00
switch (role)
{
2013-09-14 22:45:04 +00:00
case Qt::DisplayRole:
var = QString (trackerInfo.st.announce);
break;
2013-09-14 22:45:04 +00:00
case Qt::DecorationRole:
var = trackerInfo.st.getFavicon ();
break;
2013-09-14 22:45:04 +00:00
case TrackerRole:
var = qVariantFromValue (trackerInfo);
break;
2013-09-14 22:45:04 +00:00
default:
break;
}
}
2013-09-14 22:45:04 +00:00
return var;
}
/***
****
***/
2013-09-14 22:45:04 +00:00
struct CompareTrackers
{
bool operator() (const TrackerInfo& a, const TrackerInfo& b) const
{
if (a.torrentId != b.torrentId )
return a.torrentId < b.torrentId;
if (a.st.tier != b.st.tier)
return a.st.tier < b.st.tier;
if (a.st.isBackup != b.st.isBackup)
return !a.st.isBackup;
return a.st.announce < b.st.announce;
}
};
void
TrackerModel::refresh (const TorrentModel& torrentModel, const QSet<int>& ids)
{
2013-09-14 22:45:04 +00:00
// build a list of the TrackerInfos
QVector<TrackerInfo> trackers;
foreach (int id, ids)
{
const Torrent * tor = torrentModel.getTorrentFromId (id);
if (tor != 0)
{
const TrackerStatsList trackerList = tor->trackerStats ();
foreach (const TrackerStat& st, trackerList)
{
TrackerInfo trackerInfo;
trackerInfo.st = st;
trackerInfo.torrentId = id;
trackers.append (trackerInfo);
}
}
}
2013-09-14 22:45:04 +00:00
// sort 'em
CompareTrackers comp;
std::sort (trackers.begin(), trackers.end(), comp);
2013-09-14 22:45:04 +00:00
// merge 'em with the existing list
int old_index = 0;
int new_index = 0;
2013-09-14 22:45:04 +00:00
while ((old_index < myRows.size()) || (new_index < trackers.size()))
{
2013-09-14 22:45:04 +00:00
if (old_index == myRows.size())
{
2013-09-14 22:45:04 +00:00
// add this new row
beginInsertRows (QModelIndex (), old_index, old_index);
myRows.insert (old_index, trackers.at (new_index));
endInsertRows ();
++old_index;
++new_index;
}
2013-09-14 22:45:04 +00:00
else if (new_index == trackers.size())
{
2013-09-14 22:45:04 +00:00
// remove this old row
beginRemoveRows (QModelIndex (), old_index, old_index);
myRows.remove (old_index);
endRemoveRows ();
}
2013-09-14 22:45:04 +00:00
else if (comp (myRows.at(old_index), trackers.at(new_index)))
{
2013-09-14 22:45:04 +00:00
// remove this old row
beginRemoveRows (QModelIndex (), old_index, old_index);
myRows.remove (old_index);
endRemoveRows ();
}
2013-09-14 22:45:04 +00:00
else if (comp (trackers.at(new_index), myRows.at(old_index)))
{
2013-09-14 22:45:04 +00:00
// add this new row
beginInsertRows (QModelIndex (), old_index, old_index);
myRows.insert (old_index, trackers.at (new_index));
endInsertRows ();
++old_index;
++new_index;
}
2013-09-14 22:45:04 +00:00
else // update existing row
{
2013-09-14 22:45:04 +00:00
myRows[old_index].st = trackers.at(new_index).st;
QModelIndex topLeft;
QModelIndex bottomRight;
dataChanged (index(old_index,0), index(old_index,0));
++old_index;
++new_index;
}
}
}
int
TrackerModel::find (int torrentId, const QString& url) const
{
2013-09-14 22:45:04 +00:00
for (int i=0, n=myRows.size(); i<n; ++i)
{
const TrackerInfo& inf = myRows.at(i);
if ((inf.torrentId == torrentId) && (url == inf.st.announce))
return i;
}
2013-09-14 22:45:04 +00:00
return -1;
}