2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2009-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2015-01-20 23:28:38 +00:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
|
|
|
#include <QApplication>
|
2010-07-27 19:43:32 +00:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
2023-02-12 03:46:57 +00:00
|
|
|
#include <libtransmission/web-utils.h>
|
2023-04-23 02:11:16 +00:00
|
|
|
#include <libtransmission/favicon-cache.h>
|
2023-02-12 03:46:57 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Formatter.h"
|
|
|
|
#include "Torrent.h"
|
|
|
|
#include "TrackerDelegate.h"
|
|
|
|
#include "TrackerModel.h"
|
|
|
|
#include "Utils.h"
|
2010-07-27 19:43:32 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
auto constexpr Spacing = int{ 6 };
|
|
|
|
|
|
|
|
auto constexpr Margin = QSize{ 10, 10 };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
class ItemLayout
|
|
|
|
{
|
|
|
|
public:
|
2020-05-27 21:53:12 +00:00
|
|
|
QRect icon_rect;
|
|
|
|
QRect text_rect;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
ItemLayout(QString const& text, bool suppress_colors, Qt::LayoutDirection direction, QPoint const& top_left, int width);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
[[nodiscard]] QSize size() const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return (icon_rect | text_rect).size();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
[[nodiscard]] QAbstractTextDocumentLayout* textLayout() const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return text_document_.documentLayout();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-11-02 01:13:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QTextDocument text_document_;
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
ItemLayout::ItemLayout(
|
|
|
|
QString const& text,
|
|
|
|
bool suppress_colors,
|
|
|
|
Qt::LayoutDirection direction,
|
|
|
|
QPoint const& top_left,
|
2017-04-19 12:04:45 +00:00
|
|
|
int width)
|
|
|
|
{
|
2023-04-23 02:11:16 +00:00
|
|
|
auto const icon_size = QSize{ FaviconCache<QPixmap>::Width, FaviconCache<QPixmap>::Height };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-07-18 15:20:17 +00:00
|
|
|
QRect base_rect{ top_left, QSize{ width, 0 } };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
icon_rect = QStyle::alignedRect(direction, Qt::AlignLeft | Qt::AlignTop, icon_size, base_rect);
|
2020-06-05 19:02:11 +00:00
|
|
|
Utils::narrowRect(base_rect, icon_size.width() + Spacing, 0, direction);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
text_document_.setDocumentMargin(0);
|
|
|
|
text_document_.setTextWidth(base_rect.width());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QTextOption text_option;
|
|
|
|
text_option.setTextDirection(direction);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (suppress_colors)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
text_option.setFlags(QTextOption::SuppressColors);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
text_document_.setDefaultTextOption(text_option);
|
|
|
|
text_document_.setHtml(text);
|
2015-01-20 23:28:38 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
text_rect = base_rect;
|
|
|
|
text_rect.setSize(text_document_.size().toSize());
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize TrackerDelegate::sizeHint(QStyleOptionViewItem const& option, TrackerInfo const& info) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2023-07-18 15:20:17 +00:00
|
|
|
ItemLayout const layout{ getText(info), true, option.direction, QPoint(0, 0), option.rect.width() - Margin.width() * 2 };
|
2020-06-05 19:02:11 +00:00
|
|
|
return layout.size() + Margin * 2;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize TrackerDelegate::sizeHint(QStyleOptionViewItem const& option, QModelIndex const& index) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
auto const tracker_info = index.data(TrackerModel::TrackerRole).value<TrackerInfo>();
|
|
|
|
return sizeHint(option, tracker_info);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void TrackerDelegate::paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
auto const tracker_info = index.data(TrackerModel::TrackerRole).value<TrackerInfo>();
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->save();
|
|
|
|
painter->setClipRect(option.rect);
|
|
|
|
drawBackground(painter, option, index);
|
2020-05-27 21:53:12 +00:00
|
|
|
drawTracker(painter, option, tracker_info);
|
2017-04-19 12:04:45 +00:00
|
|
|
drawFocus(painter, option, option.rect);
|
|
|
|
painter->restore();
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void TrackerDelegate::drawTracker(QPainter* painter, QStyleOptionViewItem const& option, TrackerInfo const& inf) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const is_item_selected((option.state & QStyle::State_Selected) != 0);
|
|
|
|
bool const is_item_enabled((option.state & QStyle::State_Enabled) != 0);
|
|
|
|
bool const is_item_active((option.state & QStyle::State_Active) != 0);
|
2015-01-20 23:28:38 +00:00
|
|
|
|
2022-07-27 14:03:13 +00:00
|
|
|
QIcon const tracker_icon(inf.st.getFavicon());
|
2015-01-20 23:28:38 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
QRect const content_rect(option.rect.adjusted(Margin.width(), Margin.height(), -Margin.width(), -Margin.height()));
|
2020-05-27 21:53:12 +00:00
|
|
|
ItemLayout const layout(getText(inf), is_item_selected, option.direction, content_rect.topLeft(), content_rect.width());
|
2015-01-20 23:28:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->save();
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_item_selected)
|
2015-06-15 21:07:46 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QPalette::ColorGroup cg = is_item_enabled ? QPalette::Normal : QPalette::Disabled;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (cg == QPalette::Normal && !is_item_active)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
cg = QPalette::Inactive;
|
|
|
|
}
|
2015-06-15 21:07:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
|
2015-06-15 21:07:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tracker_icon
|
|
|
|
.paint(painter, layout.icon_rect, Qt::AlignCenter, is_item_selected ? QIcon::Selected : QIcon::Normal, QIcon::On);
|
2015-01-20 23:28:38 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QAbstractTextDocumentLayout::PaintContext paint_context;
|
|
|
|
paint_context.clip = layout.text_rect.translated(-layout.text_rect.topLeft());
|
2021-08-15 09:41:48 +00:00
|
|
|
paint_context.palette.setColor(
|
|
|
|
QPalette::Text,
|
2020-05-27 21:53:12 +00:00
|
|
|
option.palette.color(is_item_selected ? QPalette::HighlightedText : QPalette::Text));
|
|
|
|
painter->translate(layout.text_rect.topLeft());
|
|
|
|
layout.textLayout()->draw(painter, paint_context);
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->restore();
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void TrackerDelegate::setShowMore(bool b)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
show_more_ = b;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QString timeToStringRounded(int seconds)
|
|
|
|
{
|
|
|
|
if (seconds > 60)
|
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
seconds -= seconds % 60;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
return Formatter::get().timeToString(seconds);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString TrackerDelegate::getText(TrackerInfo const& inf) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QString str;
|
2020-05-29 17:40:07 +00:00
|
|
|
auto const err_markup_begin = QStringLiteral("<span style=\"color:red\">");
|
|
|
|
auto const err_markup_end = QStringLiteral("</span>");
|
|
|
|
auto const timeout_markup_begin = QStringLiteral("<span style=\"color:#224466\">");
|
|
|
|
auto const timeout_markup_end = QStringLiteral("</span>");
|
|
|
|
auto const success_markup_begin = QStringLiteral("<span style=\"color:#008B00\">");
|
|
|
|
auto const success_markup_end = QStringLiteral("</span>");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
auto const now = time(nullptr);
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const time_until = [&now](auto t)
|
|
|
|
{
|
|
|
|
return timeToStringRounded(static_cast<int>(t - now));
|
|
|
|
};
|
|
|
|
auto const time_since = [&now](auto t)
|
|
|
|
{
|
|
|
|
return timeToStringRounded(static_cast<int>(now - t));
|
|
|
|
};
|
2020-11-02 01:13:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// hostname
|
2020-05-29 17:40:07 +00:00
|
|
|
str += inf.st.is_backup ? QStringLiteral("<i>") : QStringLiteral("<b>");
|
2023-02-12 03:46:57 +00:00
|
|
|
auto const announce_url = inf.st.announce.toStdString();
|
|
|
|
if (auto const parsed = tr_urlParse(announce_url); parsed)
|
|
|
|
{
|
|
|
|
str += QStringLiteral("%1:%2")
|
|
|
|
.arg(QString::fromUtf8(std::data(parsed->host), std::size(parsed->host)))
|
|
|
|
.arg(parsed->port);
|
|
|
|
}
|
2020-05-29 17:40:07 +00:00
|
|
|
str += inf.st.is_backup ? QStringLiteral("</i>") : QStringLiteral("</b>");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// announce & scrape info
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!inf.st.is_backup)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (inf.st.has_announced && inf.st.announce_state != TR_TRACKER_INACTIVE)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
auto const tstr = time_since(inf.st.last_announce_time);
|
2020-05-29 17:40:07 +00:00
|
|
|
str += QStringLiteral("<br/>\n");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (inf.st.last_announce_succeeded)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 and %2 are replaced with HTML markup, %3 is duration
|
2021-08-15 09:41:48 +00:00
|
|
|
str += tr("Got a list of%1 %Ln peer(s)%2 %3 ago", nullptr, inf.st.last_announce_peer_count)
|
|
|
|
.arg(success_markup_begin)
|
|
|
|
.arg(success_markup_end)
|
|
|
|
.arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (inf.st.last_announce_timed_out)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 and %2 are replaced with HTML markup, %3 is duration
|
2021-08-15 09:41:48 +00:00
|
|
|
str += tr("Peer list request %1timed out%2 %3 ago; will retry")
|
|
|
|
.arg(timeout_markup_begin)
|
|
|
|
.arg(timeout_markup_end)
|
|
|
|
.arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 and %3 are replaced with HTML markup, %2 is error message, %4 is duration
|
2021-08-15 09:41:48 +00:00
|
|
|
str += tr("Got an error %1\"%2\"%3 %4 ago")
|
|
|
|
.arg(err_markup_begin)
|
|
|
|
.arg(inf.st.last_announce_result)
|
|
|
|
.arg(err_markup_end)
|
|
|
|
.arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
switch (inf.st.announce_state)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
case TR_TRACKER_INACTIVE:
|
2020-05-29 17:40:07 +00:00
|
|
|
str += QStringLiteral("<br/>\n");
|
2017-04-19 12:04:45 +00:00
|
|
|
str += tr("No updates scheduled");
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_WAITING:
|
2020-11-02 01:13:32 +00:00
|
|
|
str += QStringLiteral("<br/>\n");
|
|
|
|
//: %1 is duration
|
|
|
|
str += tr("Asking for more peers in %1").arg(time_until(inf.st.next_announce_time));
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_QUEUED:
|
2020-05-29 17:40:07 +00:00
|
|
|
str += QStringLiteral("<br/>\n");
|
2017-04-19 12:04:45 +00:00
|
|
|
str += tr("Queued to ask for more peers");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_ACTIVE:
|
2020-11-02 01:13:32 +00:00
|
|
|
str += QStringLiteral("<br/>\n");
|
|
|
|
//: %1 is duration
|
2022-10-11 15:39:41 +00:00
|
|
|
str += tr("Asking for more peers now… <small>%1</small>").arg(time_since(inf.st.last_announce_start_time));
|
2020-11-02 01:13:32 +00:00
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
if (!show_more_)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inf.st.has_scraped)
|
|
|
|
{
|
|
|
|
str += QStringLiteral("<br/>\n");
|
|
|
|
auto const tstr = time_since(inf.st.last_scrape_time);
|
|
|
|
|
|
|
|
if (!inf.st.last_scrape_succeeded)
|
|
|
|
{
|
|
|
|
//: %1 and %3 are replaced with HTML markup, %2 is error message, %4 is duration
|
2021-08-15 09:41:48 +00:00
|
|
|
str += tr("Got a scrape error %1\"%2\"%3 %4 ago")
|
|
|
|
.arg(err_markup_begin)
|
|
|
|
.arg(inf.st.last_scrape_result)
|
|
|
|
.arg(err_markup_end)
|
|
|
|
.arg(tstr);
|
2020-11-02 01:13:32 +00:00
|
|
|
}
|
|
|
|
else if (inf.st.seeder_count >= 0 && inf.st.leecher_count >= 0)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: First part of phrase "Tracker had ... seeder(s) and ... leecher(s) ... ago",
|
2020-11-02 01:13:32 +00:00
|
|
|
//: %1 and %2 are replaced with HTML markup
|
2021-08-15 09:41:48 +00:00
|
|
|
str += tr("Tracker had%1 %Ln seeder(s)%2", nullptr, inf.st.seeder_count)
|
|
|
|
.arg(success_markup_begin)
|
|
|
|
.arg(success_markup_end);
|
2020-11-09 03:31:02 +00:00
|
|
|
//: Second part of phrase "Tracker had ... seeder(s) and ... leecher(s) ... ago",
|
2020-11-02 01:13:32 +00:00
|
|
|
//: %1 and %2 are replaced with HTML markup, %3 is duration;
|
|
|
|
//: notice that leading space (before "and") is included here
|
2021-08-15 09:41:48 +00:00
|
|
|
str += tr(" and%1 %Ln leecher(s)%2 %3 ago", nullptr, inf.st.leecher_count)
|
|
|
|
.arg(success_markup_begin)
|
|
|
|
.arg(success_markup_end)
|
|
|
|
.arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2020-11-02 01:13:32 +00:00
|
|
|
else
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
//: %1 and %2 are replaced with HTML markup, %3 is duration
|
2021-08-15 09:41:48 +00:00
|
|
|
str += tr("Tracker had %1no information%2 on peer counts %3 ago")
|
|
|
|
.arg(success_markup_begin)
|
|
|
|
.arg(success_markup_end)
|
|
|
|
.arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 01:13:32 +00:00
|
|
|
|
|
|
|
switch (inf.st.scrape_state)
|
|
|
|
{
|
|
|
|
case TR_TRACKER_INACTIVE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_WAITING:
|
|
|
|
str += QStringLiteral("<br/>\n");
|
|
|
|
//: %1 is duration
|
|
|
|
str += tr("Asking for peer counts in %1").arg(time_until(inf.st.next_scrape_time));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_QUEUED:
|
|
|
|
str += QStringLiteral("<br/>\n");
|
|
|
|
str += tr("Queued to ask for peer counts");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_ACTIVE:
|
|
|
|
str += QStringLiteral("<br/>\n");
|
|
|
|
//: %1 is duration
|
2022-10-11 15:39:41 +00:00
|
|
|
str += tr("Asking for peer counts now… <small>%1</small>").arg(time_since(inf.st.last_scrape_start_time));
|
2020-11-02 01:13:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return str;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|