2010-07-27 19:43:32 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2009-2014 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
|
|
|
*
|
2010-10-01 20:22:51 +00:00
|
|
|
* $Id$
|
2010-07-27 19:43:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
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>
|
|
|
|
|
2010-07-27 19:43:32 +00:00
|
|
|
#include "favicon.h"
|
|
|
|
#include "formatter.h"
|
|
|
|
#include "torrent.h"
|
|
|
|
#include "tracker-delegate.h"
|
|
|
|
#include "tracker-model.h"
|
2015-01-20 23:28:38 +00:00
|
|
|
#include "utils.h"
|
2010-07-27 19:43:32 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
const int mySpacing = 6;
|
2015-01-20 23:28:38 +00:00
|
|
|
const QSize myMargin (10, 10);
|
|
|
|
|
|
|
|
class ItemLayout
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
QTextDocument myTextDocument;
|
|
|
|
|
|
|
|
public:
|
|
|
|
QRect iconRect;
|
|
|
|
QRect textRect;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ItemLayout(const QString& text, bool suppressColors, Qt::LayoutDirection direction,
|
|
|
|
const QPoint& topLeft, int width);
|
|
|
|
|
|
|
|
QSize size () const
|
|
|
|
{
|
|
|
|
return (iconRect | textRect).size ();
|
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractTextDocumentLayout * textLayout () const
|
|
|
|
{
|
|
|
|
return myTextDocument.documentLayout ();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ItemLayout::ItemLayout(const QString& text, bool suppressColors, Qt::LayoutDirection direction,
|
|
|
|
const QPoint& topLeft, int width)
|
|
|
|
{
|
|
|
|
const QStyle * style (qApp->style ());
|
|
|
|
const QSize iconSize = Favicons::getIconSize ();
|
|
|
|
|
|
|
|
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 ());
|
|
|
|
QTextOption textOption;
|
|
|
|
textOption.setTextDirection (direction);
|
|
|
|
if (suppressColors)
|
|
|
|
textOption.setFlags (QTextOption::SuppressColors);
|
|
|
|
myTextDocument.setDefaultTextOption (textOption);
|
|
|
|
myTextDocument.setHtml (text);
|
|
|
|
|
|
|
|
textRect = baseRect;
|
|
|
|
textRect.setSize (myTextDocument.size ().toSize ());
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerDelegate::margin (const QStyle& style) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
Q_UNUSED (style);
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return myMargin;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
QSize
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerDelegate::sizeHint (const QStyleOptionViewItem & option,
|
|
|
|
const TrackerInfo & info) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-01-20 23:28:38 +00:00
|
|
|
const ItemLayout layout (getText (info), true, option.direction, QPoint (0, 0), option.rect.width () - myMargin.width () * 2);
|
|
|
|
return layout.size () + myMargin * 2;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerDelegate::sizeHint (const QStyleOptionViewItem & option,
|
|
|
|
const QModelIndex & index) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
const TrackerInfo trackerInfo = index.data (TrackerModel::TrackerRole).value<TrackerInfo>();
|
|
|
|
return sizeHint (option, trackerInfo);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerDelegate::paint (QPainter * painter,
|
|
|
|
const QStyleOptionViewItem & option,
|
|
|
|
const QModelIndex & index) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
const TrackerInfo trackerInfo = index.data (TrackerModel::TrackerRole).value<TrackerInfo>();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerDelegate::drawTracker (QPainter * painter,
|
|
|
|
const QStyleOptionViewItem & option,
|
|
|
|
const TrackerInfo & inf) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-01-20 23:28:38 +00:00
|
|
|
const bool isItemSelected ((option.state & QStyle::State_Selected) != 0);
|
|
|
|
|
|
|
|
QIcon trackerIcon (inf.st.getFavicon());
|
|
|
|
|
|
|
|
const QRect contentRect (option.rect.adjusted (myMargin.width (), myMargin.height (), -myMargin.width (), -myMargin.height ()));
|
|
|
|
const ItemLayout layout (getText (inf), isItemSelected, option.direction, contentRect.topLeft (), contentRect.width ());
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
painter->save();
|
|
|
|
|
2015-01-20 23:28:38 +00:00
|
|
|
trackerIcon.paint (painter, layout.iconRect, Qt::AlignCenter, isItemSelected ? QIcon::Selected : QIcon::Normal, QIcon::On);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
painter->restore();
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerDelegate::setShowMore (bool b)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
myShowMore = b;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
QString timeToStringRounded (int seconds)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
if (seconds > 60)
|
|
|
|
seconds -= (seconds % 60);
|
|
|
|
|
|
|
|
return Formatter::timeToString (seconds);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2014-12-12 23:05:10 +00:00
|
|
|
TrackerDelegate::getText (const TrackerInfo& inf) const
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
QString key;
|
|
|
|
QString str;
|
|
|
|
const time_t now (time (0));
|
2015-01-29 21:53:05 +00:00
|
|
|
const QString err_markup_begin = QLatin1String ("<span style=\"color:red\">");
|
|
|
|
const QString err_markup_end = QLatin1String ("</span>");
|
|
|
|
const QString timeout_markup_begin = QLatin1String ("<span style=\"color:#224466\">");
|
|
|
|
const QString timeout_markup_end = QLatin1String ("</span>");
|
|
|
|
const QString success_markup_begin = QLatin1String ("<span style=\"color:#008B00\">");
|
|
|
|
const QString success_markup_end = QLatin1String ("</span>");
|
2013-09-14 22:45:04 +00:00
|
|
|
|
|
|
|
// hostname
|
2015-01-29 21:53:05 +00:00
|
|
|
str += inf.st.isBackup ? QLatin1String ("<i>") : QLatin1String ("<b>");
|
2013-09-14 22:45:04 +00:00
|
|
|
char * host = NULL;
|
|
|
|
int port = 0;
|
|
|
|
tr_urlParse (inf.st.announce.toUtf8().constData(), -1, NULL, &host, &port, NULL);
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QString::fromLatin1 ("%1:%2").arg (QString::fromUtf8 (host)).arg (port);
|
2013-09-14 22:45:04 +00:00
|
|
|
tr_free (host);
|
2015-01-29 21:53:05 +00:00
|
|
|
if (!key.isEmpty()) str += QLatin1String (" - ") + key;
|
|
|
|
str += inf.st.isBackup ? QLatin1String ("</i>") : QLatin1String ("</b>");
|
2013-09-14 22:45:04 +00:00
|
|
|
|
|
|
|
// announce & scrape info
|
|
|
|
if (!inf.st.isBackup)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
if (inf.st.hasAnnounced && inf.st.announceState != TR_TRACKER_INACTIVE)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
const QString tstr (timeToStringRounded (now - inf.st.lastAnnounceTime));
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2013-09-14 22:45:04 +00:00
|
|
|
if (inf.st.lastAnnounceSucceeded)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-01-18 02:09:44 +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)
|
2013-09-14 22:45:04 +00:00
|
|
|
.arg (success_markup_begin)
|
|
|
|
.arg (success_markup_end)
|
|
|
|
.arg (tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
else if (inf.st.lastAnnounceTimedOut)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %1 and %2 are replaced with HTML markup, %3 is duration
|
2013-09-14 22:45:04 +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
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
else
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %1 and %3 are replaced with HTML markup, %2 is error message, %4 is duration
|
2013-09-14 22:45:04 +00:00
|
|
|
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)
|
|
|
|
{
|
2010-07-27 19:43:32 +00:00
|
|
|
case TR_TRACKER_INACTIVE:
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2013-09-14 22:45:04 +00:00
|
|
|
str += tr ("No updates scheduled");
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
case TR_TRACKER_WAITING:
|
|
|
|
{
|
|
|
|
const QString tstr (timeToStringRounded (inf.st.nextAnnounceTime - now));
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %1 is duration
|
2013-09-14 22:45:04 +00:00
|
|
|
str += tr ("Asking for more peers in %1").arg (tstr);
|
2010-07-27 19:43:32 +00:00
|
|
|
break;
|
2013-09-14 22:45:04 +00:00
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
|
|
|
|
case TR_TRACKER_QUEUED:
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2013-09-14 22:45:04 +00:00
|
|
|
str += tr ("Queued to ask for more peers");
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
|
|
|
|
case TR_TRACKER_ACTIVE: {
|
2013-09-14 22:45:04 +00:00
|
|
|
const QString tstr (timeToStringRounded (now - inf.st.lastAnnounceStartTime));
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %1 is duration
|
2013-09-14 22:45:04 +00:00
|
|
|
str += tr ("Asking for more peers now... <small>%1</small>").arg (tstr);
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if (myShowMore)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
if (inf.st.hasScraped)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2013-09-14 22:45:04 +00:00
|
|
|
const QString tstr (timeToStringRounded (now - inf.st.lastScrapeTime));
|
|
|
|
if (inf.st.lastScrapeSucceeded)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-01-18 13:19:30 +00:00
|
|
|
if (inf.st.seederCount >= 0 && inf.st.leecherCount >= 0)
|
|
|
|
{
|
|
|
|
//: 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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//: %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);
|
|
|
|
}
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
else
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %1 and %3 are replaced with HTML markup, %2 is error message, %4 is duration
|
2013-09-14 22:45:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
switch (inf.st.scrapeState)
|
2010-07-27 19:43:32 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
case TR_TRACKER_INACTIVE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_WAITING:
|
|
|
|
{
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2013-09-14 22:45:04 +00:00
|
|
|
const QString tstr (timeToStringRounded (inf.st.nextScrapeTime - now));
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %1 is duration
|
2013-09-14 22:45:04 +00:00
|
|
|
str += tr ("Asking for peer counts in %1").arg (tstr);
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
case TR_TRACKER_QUEUED:
|
|
|
|
{
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2013-09-14 22:45:04 +00:00
|
|
|
str += tr ("Queued to ask for peer counts");
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
case TR_TRACKER_ACTIVE:
|
|
|
|
{
|
2015-01-29 21:53:05 +00:00
|
|
|
str += QLatin1String ("<br/>\n");
|
2013-09-14 22:45:04 +00:00
|
|
|
const QString tstr (timeToStringRounded (now - inf.st.lastScrapeStartTime));
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %1 is duration
|
2013-09-14 22:45:04 +00:00
|
|
|
str += tr ("Asking for peer counts now... <small>%1</small>").arg (tstr);
|
|
|
|
break;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
return str;
|
2010-07-27 19:43:32 +00:00
|
|
|
}
|