1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 18:18:10 +00:00
transmission/qt/TrackerModel.cc
Mike Gelfand be74cb6356
Qt 6 support (#2069)
* Bump minimum Qt version to 5.6

* Switch from QRegExp to QRegularExpression

While still available, QRegExp has been moved to Qt6::Core5Compat module
and is not part of Qt6::Core.

* Use qIsEffectiveTLD instead of QUrl::topLevelDomain

The latter is not part of Qt6::Core. The former is a private utility in
Qt6::Network; using it for now, until (and if) we switch to something
non-Qt-specific.

* Use QStyle::State_Horizontal state when drawing progress bars

Although available for a long time, this state either didn't apply to
progress bars before Qt 6, or was deduced based on bar size. With Qt 6,
failing to specify it results in bad rendering.

* Don't use QStringRef (and associated methods)

While still available, QStringRef has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core. Related method (e.g.
QString::midRef) have been removed in Qt 6.

* Use Qt::ItemIsAutoTristate instead of Qt::ItemIsTristate

The latter was deprecated and replaced with the former in Qt 5.6.

* Don't use QApplication::globalStrut

This property has been deprecated in Qt 5.15 and removed in Qt 6.

* Use QImage::fromHICON instead of QtWin::fromHICON

WinExtras module (providind the latter helper) has been removed in Qt 6.

* Use QStringDecoder instead of QTextCodec

While still available, QTextCodec has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core.

* Don't forward-declare QStringList

Instead of being a standalone class, its definition has changed to
QList<QString> template specialization in Qt 6.

* Use explicit (since Qt 6) QFileInfo constructor

* Use QDateTime's {to,from}SecsSinceEpoch instead of {to,from}Time_t

The latter was deprecated in Qt 5.8 and removed in Qt 6.

* Don't use QFuture<>'s operator==

It has been removed in Qt 6. Since the original issue this code was
solving was caused by future reuse, just don't reuse futures and create
new finished ones when necessary.

* Use std::vector<> instead of QVector<>

The latter has been changed to a typedef for QList<>, which might not be
what one wants, and which also changed behavior a bit leading to
compilation errors.

* Don't use + for flags, cast to int explicitly

Operator+ for enum values has been deleted in Qt 6, so using operator|
instead. Then, there's no conversion from QFlags<> to QVariant, so need
to cast to int.

* Support Qt 6 in CMake and for MSI packaging

* Remove extra (empty) CMake variable use when constructing Qt target names

* Simplify logic in tr_qt_add_translation CMake helper

Co-authored-by: Charles Kerr <charles@charleskerr.com>
2021-11-04 00:20:11 +03:00

156 lines
3.8 KiB
C++

/*
* This file Copyright (C) 2009-2015 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
#include <algorithm> // std::sort()
#include <QUrl>
#include "Application.h" // Application
#include "TorrentModel.h"
#include "TrackerModel.h"
int TrackerModel::rowCount(QModelIndex const& parent) const
{
Q_UNUSED(parent)
return parent.isValid() ? 0 : rows_.size();
}
QVariant TrackerModel::data(QModelIndex const& index, int role) const
{
QVariant var;
if (index.isValid() && index.row() < rowCount())
{
TrackerInfo const& tracker_info = rows_.at(index.row());
switch (role)
{
case Qt::DisplayRole:
var = tracker_info.st.announce;
break;
case Qt::DecorationRole:
var = QIcon(tracker_info.st.getFavicon());
break;
case TrackerRole:
var = QVariant::fromValue(tracker_info);
break;
default:
break;
}
}
return var;
}
/***
****
***/
struct CompareTrackers
{
bool operator()(TrackerInfo const& a, TrackerInfo const& b) const
{
if (a.torrent_id != b.torrent_id)
{
return a.torrent_id < b.torrent_id;
}
if (a.st.tier != b.st.tier)
{
return a.st.tier < b.st.tier;
}
if (a.st.is_backup != b.st.is_backup)
{
return !a.st.is_backup;
}
return a.st.announce < b.st.announce;
}
};
void TrackerModel::refresh(TorrentModel const& torrent_model, torrent_ids_t const& ids)
{
// build a list of the TrackerInfos
std::vector<TrackerInfo> trackers;
for (int const id : ids)
{
Torrent const* tor = torrent_model.getTorrentFromId(id);
if (tor != nullptr)
{
TrackerStatsList const tracker_list = tor->trackerStats();
for (TrackerStat const& st : tracker_list)
{
TrackerInfo tracker_info;
tracker_info.st = st;
tracker_info.torrent_id = id;
trackers.push_back(tracker_info);
}
}
}
// sort 'em
CompareTrackers comp;
std::sort(trackers.begin(), trackers.end(), comp);
// merge 'em with the existing list
unsigned int old_index = 0;
unsigned int new_index = 0;
while (old_index < rows_.size() || new_index < trackers.size())
{
bool const is_end_of_old = old_index == rows_.size();
bool const is_end_of_new = new_index == trackers.size();
if (is_end_of_old || (!is_end_of_new && comp(trackers.at(new_index), rows_.at(old_index))))
{
// add this new row
beginInsertRows(QModelIndex(), old_index, old_index);
rows_.insert(rows_.begin() + old_index, trackers.at(new_index));
endInsertRows();
++old_index;
++new_index;
}
else if (is_end_of_new || (!is_end_of_old && comp(rows_.at(old_index), trackers.at(new_index))))
{
// remove this old row
beginRemoveRows(QModelIndex(), old_index, old_index);
rows_.erase(rows_.begin() + old_index);
endRemoveRows();
}
else // update existing row
{
rows_[old_index].st = trackers.at(new_index).st;
emit dataChanged(index(old_index, 0), index(old_index, 0));
++old_index;
++new_index;
}
}
}
int TrackerModel::find(int torrent_id, QString const& url) const
{
for (int i = 0, n = rows_.size(); i < n; ++i)
{
TrackerInfo const& inf = rows_.at(i);
if (inf.torrent_id == torrent_id && url == inf.st.announce)
{
return i;
}
}
return -1;
}