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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
2010-07-28 14:43:47 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "FaviconCache.h"
|
|
|
|
#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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const mySpacing = 6;
|
|
|
|
QSize const myMargin(10, 10);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
class ItemLayout
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
QTextDocument myTextDocument;
|
|
|
|
|
|
|
|
public:
|
|
|
|
QRect iconRect;
|
|
|
|
QRect textRect;
|
|
|
|
|
|
|
|
public:
|
2017-04-20 16:02:19 +00:00
|
|
|
ItemLayout(QString const& text, bool suppressColors, Qt::LayoutDirection direction, QPoint const& topLeft, int width);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QSize size() const
|
|
|
|
{
|
|
|
|
return (iconRect | textRect).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractTextDocumentLayout* textLayout() const
|
|
|
|
{
|
|
|
|
return myTextDocument.documentLayout();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
ItemLayout::ItemLayout(QString const& text, bool suppressColors, Qt::LayoutDirection direction, QPoint const& topLeft,
|
2017-04-19 12:04:45 +00:00
|
|
|
int width)
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QStyle const* style(qApp->style());
|
|
|
|
QSize const iconSize = FaviconCache::getIconSize();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QRect baseRect(topLeft, QSize(width, 0));
|
|
|
|
|
|
|
|
iconRect = style->alignedRect(direction, Qt::AlignLeft | Qt::AlignTop, iconSize, baseRect);
|
|
|
|
Utils::narrowRect(baseRect, iconSize.width() + mySpacing, 0, direction);
|
|
|
|
|
|
|
|
myTextDocument.setDocumentMargin(0);
|
|
|
|
myTextDocument.setTextWidth(baseRect.width());
|
|
|
|
|
2015-01-20 23:28:38 +00:00
|
|
|
QTextOption textOption;
|
2017-04-19 12:04:45 +00:00
|
|
|
textOption.setTextDirection(direction);
|
|
|
|
|
2015-01-20 23:28:38 +00:00
|
|
|
if (suppressColors)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
textOption.setFlags(QTextOption::SuppressColors);
|
|
|
|
}
|
|
|
|
|
|
|
|
myTextDocument.setDefaultTextOption(textOption);
|
|
|
|
myTextDocument.setHtml(text);
|
2015-01-20 23:28:38 +00:00
|
|
|
|
|
|
|
textRect = baseRect;
|
2017-04-19 12:04:45 +00:00
|
|
|
textRect.setSize(myTextDocument.size().toSize());
|
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
|
|
|
QSize TrackerDelegate::margin(QStyle const& style) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
Q_UNUSED(style);
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return myMargin;
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
ItemLayout const layout(getText(info), true, option.direction, QPoint(0, 0), option.rect.width() - myMargin.width() * 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
return layout.size() + myMargin * 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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
TrackerInfo const trackerInfo = index.data(TrackerModel::TrackerRole).value<TrackerInfo>();
|
2017-04-19 12:04:45 +00:00
|
|
|
return sizeHint(option, trackerInfo);
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
TrackerInfo const trackerInfo = index.data(TrackerModel::TrackerRole).value<TrackerInfo>();
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->save();
|
|
|
|
painter->setClipRect(option.rect);
|
|
|
|
drawBackground(painter, option, index);
|
|
|
|
drawTracker(painter, option, trackerInfo);
|
|
|
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isItemSelected((option.state & QStyle::State_Selected) != 0);
|
|
|
|
bool const isItemEnabled((option.state & QStyle::State_Enabled) != 0);
|
|
|
|
bool const isItemActive((option.state & QStyle::State_Active) != 0);
|
2015-01-20 23:28:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QIcon trackerIcon(inf.st.getFavicon());
|
2015-01-20 23:28:38 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QRect const contentRect(option.rect.adjusted(myMargin.width(), myMargin.height(), -myMargin.width(), -myMargin.height()));
|
|
|
|
ItemLayout const layout(getText(inf), isItemSelected, option.direction, contentRect.topLeft(), contentRect.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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isItemSelected)
|
2015-06-15 21:07:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QPalette::ColorGroup cg = isItemEnabled ? QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
|
|
|
if (cg == QPalette::Normal && !isItemActive)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
trackerIcon.paint(painter, layout.iconRect, Qt::AlignCenter, isItemSelected ? QIcon::Selected : QIcon::Normal, QIcon::On);
|
2015-01-20 23:28:38 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QAbstractTextDocumentLayout::PaintContext paintContext;
|
|
|
|
paintContext.clip = layout.textRect.translated(-layout.textRect.topLeft());
|
|
|
|
paintContext.palette.setColor(QPalette::Text,
|
|
|
|
option.palette.color(isItemSelected ? QPalette::HighlightedText : QPalette::Text));
|
|
|
|
painter->translate(layout.textRect.topLeft());
|
|
|
|
layout.textLayout()->draw(painter, paintContext);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myShowMore = 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)
|
|
|
|
{
|
|
|
|
seconds -= (seconds % 60);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
return Formatter::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 key;
|
|
|
|
QString str;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now(time(0));
|
|
|
|
QString const err_markup_begin = QLatin1String("<span style=\"color:red\">");
|
|
|
|
QString const err_markup_end = QLatin1String("</span>");
|
|
|
|
QString const timeout_markup_begin = QLatin1String("<span style=\"color:#224466\">");
|
|
|
|
QString const timeout_markup_end = QLatin1String("</span>");
|
|
|
|
QString const success_markup_begin = QLatin1String("<span style=\"color:#008B00\">");
|
|
|
|
QString const success_markup_end = QLatin1String("</span>");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// hostname
|
|
|
|
str += inf.st.isBackup ? QLatin1String("<i>") : QLatin1String("<b>");
|
|
|
|
char* host = NULL;
|
|
|
|
int port = 0;
|
|
|
|
tr_urlParse(inf.st.announce.toUtf8().constData(), TR_BAD_SIZE, NULL, &host, &port, NULL);
|
|
|
|
str += QString::fromLatin1("%1:%2").arg(QString::fromUtf8(host)).arg(port);
|
|
|
|
tr_free(host);
|
|
|
|
|
|
|
|
if (!key.isEmpty())
|
|
|
|
{
|
|
|
|
str += QLatin1String(" - ") + key;
|
|
|
|
}
|
|
|
|
|
|
|
|
str += inf.st.isBackup ? QLatin1String("</i>") : QLatin1String("</b>");
|
|
|
|
|
|
|
|
// announce & scrape info
|
|
|
|
if (!inf.st.isBackup)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (inf.st.hasAnnounced && inf.st.announceState != TR_TRACKER_INACTIVE)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const tstr(timeToStringRounded(now - inf.st.lastAnnounceTime));
|
2017-04-19 12:04:45 +00:00
|
|
|
str += QLatin1String("<br/>\n");
|
|
|
|
|
|
|
|
if (inf.st.lastAnnounceSucceeded)
|
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
|
|
|
|
str += tr("Got a list of%1 %Ln peer(s)%2 %3 ago", 0, inf.st.lastAnnouncePeerCount).arg(success_markup_begin).
|
|
|
|
arg(success_markup_end).arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (inf.st.lastAnnounceTimedOut)
|
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
|
|
|
|
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
|
|
|
|
str += tr("Got an error %1\"%2\"%3 %4 ago").arg(err_markup_begin).arg(inf.st.lastAnnounceResult).
|
|
|
|
arg(err_markup_end).arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
switch (inf.st.announceState)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
case TR_TRACKER_INACTIVE:
|
|
|
|
str += QLatin1String("<br/>\n");
|
|
|
|
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:
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const tstr(timeToStringRounded(inf.st.nextAnnounceTime - now));
|
2017-04-19 12:04:45 +00:00
|
|
|
str += QLatin1String("<br/>\n");
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %1 is duration
|
2017-04-19 12:04:45 +00:00
|
|
|
str += tr("Asking for more peers in %1").arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_QUEUED:
|
|
|
|
str += QLatin1String("<br/>\n");
|
|
|
|
str += tr("Queued to ask for more peers");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_ACTIVE:
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const tstr(timeToStringRounded(now - inf.st.lastAnnounceStartTime));
|
2017-04-19 12:04:45 +00:00
|
|
|
str += QLatin1String("<br/>\n");
|
|
|
|
//: %1 is duration
|
|
|
|
str += tr("Asking for more peers now... <small>%1</small>").arg(tstr);
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (myShowMore)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (inf.st.hasScraped)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
str += QLatin1String("<br/>\n");
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const tstr(timeToStringRounded(now - inf.st.lastScrapeTime));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (inf.st.lastScrapeSucceeded)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (inf.st.seederCount >= 0 && inf.st.leecherCount >= 0)
|
2015-01-18 13:19:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
//: First part of phrase "Tracker had ... seeder(s) and ... leecher(s) ... ago";
|
|
|
|
//: %1 and %2 are replaced with HTML markup
|
|
|
|
str += tr("Tracker had%1 %Ln seeder(s)%2", 0, inf.st.seederCount).arg(success_markup_begin).
|
|
|
|
arg(success_markup_end);
|
|
|
|
//: Second part of phrase "Tracker had ... seeder(s) and ... leecher(s) ... ago";
|
|
|
|
//: %1 and %2 are replaced with HTML markup, %3 is duration;
|
|
|
|
//: notice that leading space (before "and") is included here
|
|
|
|
str += tr(" and%1 %Ln leecher(s)%2 %3 ago", 0, inf.st.leecherCount).arg(success_markup_begin).
|
|
|
|
arg(success_markup_end).arg(tstr);
|
2015-01-18 13:19:30 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-01-18 13:19:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 and %2 are replaced with HTML markup, %3 is duration
|
|
|
|
str += tr("Tracker had %1no information%2 on peer counts %3 ago").arg(success_markup_begin).
|
|
|
|
arg(success_markup_end).arg(tstr);
|
2015-01-18 13:19:30 +00:00
|
|
|
}
|
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
|
|
|
|
str += tr("Got a scrape error %1\"%2\"%3 %4 ago").arg(err_markup_begin).arg(inf.st.lastScrapeResult).
|
|
|
|
arg(err_markup_end).arg(tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (inf.st.scrapeState)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_INACTIVE:
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_WAITING:
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
str += QLatin1String("<br/>\n");
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const tstr(timeToStringRounded(inf.st.nextScrapeTime - now));
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 is duration
|
|
|
|
str += tr("Asking for peer counts in %1").arg(tstr);
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_QUEUED:
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
str += QLatin1String("<br/>\n");
|
|
|
|
str += tr("Queued to ask for peer counts");
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_TRACKER_ACTIVE:
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
str += QLatin1String("<br/>\n");
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const tstr(timeToStringRounded(now - inf.st.lastScrapeStartTime));
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 is duration
|
|
|
|
str += tr("Asking for peer counts now... <small>%1</small>").arg(tstr);
|
|
|
|
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
|
|
|
}
|