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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int TrackerModel::rowCount(QModelIndex const& parent) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
Q_UNUSED(parent)
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
return parent.isValid() ? 0 : rows_.size();
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QVariant TrackerModel::data(QModelIndex const& index, int role) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QVariant var;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const row = index.row();
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (0 <= row && row < rows_.size())
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
TrackerInfo const& tracker_info = rows_.at(row);
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (role)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case Qt::DisplayRole:
|
2020-05-27 21:53:12 +00:00
|
|
|
var = tracker_info.st.announce;
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case Qt::DecorationRole:
|
2020-05-27 21:53:12 +00:00
|
|
|
var = QIcon(tracker_info.st.getFavicon());
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TrackerRole:
|
2020-05-27 21:53:12 +00:00
|
|
|
var = QVariant::fromValue(tracker_info);
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return var;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
struct CompareTrackers
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool operator ()(TrackerInfo const& a, TrackerInfo const& b) const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (a.torrent_id != b.torrent_id)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return a.torrent_id < b.torrent_id;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (a.st.tier != b.st.tier)
|
|
|
|
{
|
|
|
|
return a.st.tier < b.st.tier;
|
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (a.st.is_backup != b.st.is_backup)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return !a.st.is_backup;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return a.st.announce < b.st.announce;
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
};
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void TrackerModel::refresh(TorrentModel const& torrent_model, torrent_ids_t const& ids)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// build a list of the TrackerInfos
|
|
|
|
QVector<TrackerInfo> trackers;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (int const id : ids)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
Torrent const* tor = torrent_model.getTorrentFromId(id);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 09:29:58 +00:00
|
|
|
if (tor != nullptr)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
TrackerStatsList const tracker_list = tor->trackerStats();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (TrackerStat const& st : tracker_list)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
TrackerInfo tracker_info;
|
|
|
|
tracker_info.st = st;
|
|
|
|
tracker_info.torrent_id = id;
|
|
|
|
trackers.append(tracker_info);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// sort 'em
|
|
|
|
CompareTrackers comp;
|
|
|
|
std::sort(trackers.begin(), trackers.end(), comp);
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// merge 'em with the existing list
|
2020-05-27 21:53:12 +00:00
|
|
|
int old_index = 0;
|
|
|
|
int new_index = 0;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
while (old_index < rows_.size() || new_index < trackers.size())
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const is_end_of_old = old_index == rows_.size();
|
|
|
|
bool const is_end_of_new = new_index == trackers.size();
|
2015-08-06 20:28:44 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_end_of_old || (!is_end_of_new && comp(trackers.at(new_index), rows_.at(old_index))))
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// add this new row
|
2020-05-27 21:53:12 +00:00
|
|
|
beginInsertRows(QModelIndex(), old_index, old_index);
|
|
|
|
rows_.insert(old_index, trackers.at(new_index));
|
2017-04-19 12:04:45 +00:00
|
|
|
endInsertRows();
|
2020-05-27 21:53:12 +00:00
|
|
|
++old_index;
|
|
|
|
++new_index;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (is_end_of_new || (!is_end_of_old && comp(rows_.at(old_index), trackers.at(new_index))))
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// remove this old row
|
2020-05-27 21:53:12 +00:00
|
|
|
beginRemoveRows(QModelIndex(), old_index, old_index);
|
|
|
|
rows_.remove(old_index);
|
2017-04-19 12:04:45 +00:00
|
|
|
endRemoveRows();
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else // update existing row
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
rows_[old_index].st = trackers.at(new_index).st;
|
|
|
|
emit dataChanged(index(old_index, 0), index(old_index, 0));
|
|
|
|
++old_index;
|
|
|
|
++new_index;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
int TrackerModel::find(int torrent_id, QString const& url) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
for (int i = 0, n = rows_.size(); i < n; ++i)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
TrackerInfo const& inf = rows_.at(i);
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (inf.torrent_id == torrent_id && url == inf.st.announce)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return -1;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|