2010-07-27 19:43:32 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2010-07-27 19:43:32 +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.
|
2010-07-27 19:43:32 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm> // std::sort()
|
|
|
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Application.h" // Application
|
2015-06-12 22:12:12 +00:00
|
|
|
#include "TorrentModel.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "TrackerModel.h"
|
2010-07-27 19:43:32 +00:00
|
|
|
|
|
|
|
int
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerModel::rowCount (const QModelIndex& parent) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
Q_UNUSED (parent);
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return parent.isValid() ? 0 : myRows.size();
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerModel::data (const QModelIndex& index, int role) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
QVariant var;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
const int row = index.row ();
|
|
|
|
|
|
|
|
if ((0<=row) && (row<myRows.size()))
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
const TrackerInfo& trackerInfo = myRows.at (row);
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
switch (role)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
case Qt::DisplayRole:
|
2014-12-27 14:07:14 +00:00
|
|
|
var = trackerInfo.st.announce;
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
case Qt::DecorationRole:
|
|
|
|
var = trackerInfo.st.getFavicon ();
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
case TrackerRole:
|
|
|
|
var = qVariantFromValue (trackerInfo);
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
default:
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return var;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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;
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerModel::refresh (const TorrentModel& torrentModel, const QSet<int>& ids)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
// build a list of the TrackerInfos
|
|
|
|
QVector<TrackerInfo> trackers;
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const int id: ids)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
|
|
|
const Torrent * tor = torrentModel.getTorrentFromId (id);
|
|
|
|
if (tor != 0)
|
|
|
|
{
|
|
|
|
const TrackerStatsList trackerList = tor->trackerStats ();
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const TrackerStat& st: trackerList)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
|
|
|
TrackerInfo trackerInfo;
|
|
|
|
trackerInfo.st = st;
|
|
|
|
trackerInfo.torrentId = id;
|
|
|
|
trackers.append (trackerInfo);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
// sort 'em
|
|
|
|
CompareTrackers comp;
|
|
|
|
std::sort (trackers.begin(), trackers.end(), comp);
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
// merge 'em with the existing list
|
2015-08-06 20:28:44 +00:00
|
|
|
int oldIndex = 0;
|
|
|
|
int newIndex = 0;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2015-08-06 20:28:44 +00:00
|
|
|
while (oldIndex < myRows.size () || newIndex < trackers.size ())
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-08-06 20:28:44 +00:00
|
|
|
const bool isEndOfOld = oldIndex == myRows.size ();
|
|
|
|
const bool isEndOfNew = newIndex == trackers.size ();
|
|
|
|
|
|
|
|
if (isEndOfOld || (!isEndOfNew && comp (trackers.at (newIndex), myRows.at (oldIndex))))
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
// add this new row
|
2015-08-06 20:28:44 +00:00
|
|
|
beginInsertRows (QModelIndex (), oldIndex, oldIndex);
|
|
|
|
myRows.insert (oldIndex, trackers.at (newIndex));
|
2013-09-14 22:45:04 +00:00
|
|
|
endInsertRows ();
|
2015-08-06 20:28:44 +00:00
|
|
|
++oldIndex;
|
|
|
|
++newIndex;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2015-08-06 20:28:44 +00:00
|
|
|
else if (isEndOfNew || (!isEndOfOld && comp (myRows.at (oldIndex), trackers.at (newIndex))))
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
// remove this old row
|
2015-08-06 20:28:44 +00:00
|
|
|
beginRemoveRows (QModelIndex (), oldIndex, oldIndex);
|
|
|
|
myRows.remove (oldIndex);
|
2013-09-14 22:45:04 +00:00
|
|
|
endRemoveRows ();
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
else // update existing row
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-08-06 20:28:44 +00:00
|
|
|
myRows[oldIndex].st = trackers.at (newIndex).st;
|
|
|
|
emit dataChanged (index (oldIndex, 0), index (oldIndex, 0));
|
|
|
|
++oldIndex;
|
|
|
|
++newIndex;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerModel::find (int torrentId, const QString& url) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
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;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return -1;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|