transmission: update from 2.13 to 2.22
[tomato.git] / release / src / router / transmission / qt / tracker-delegate.cc
blob8595150e48174aa7a537f1f311ef0b3e4a1f3bc2
1 /*
2 * This file Copyright (C) Mnemosyne LLC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 * $Id: tracker-delegate.cc 11522 2010-12-12 16:43:19Z charles $
13 #include <iostream>
15 #include <QPainter>
16 #include <QPixmap>
17 #include <QTextDocument>
19 #include <libtransmission/transmission.h>
20 #include <libtransmission/utils.h>
22 #include "favicon.h"
23 #include "formatter.h"
24 #include "torrent.h"
25 #include "tracker-delegate.h"
26 #include "tracker-model.h"
28 /***
29 ****
30 ***/
32 namespace
34 const int mySpacing = 6;
35 const QSize myMargin( 10, 6 );
38 QSize
39 TrackerDelegate :: margin( const QStyle& style ) const
41 Q_UNUSED( style );
43 return myMargin;
46 /***
47 ****
48 ***/
50 QSize
51 TrackerDelegate :: sizeHint( const QStyleOptionViewItem& option, const TrackerInfo& info ) const
53 Q_UNUSED( option );
55 QPixmap favicon = info.st.getFavicon( );
57 const QString text = TrackerDelegate :: getText( info );
58 QTextDocument textDoc;
59 textDoc.setHtml( text );
60 const QSize textSize = textDoc.size().toSize();
62 return QSize( myMargin.width() + favicon.width() + mySpacing + textSize.width() + myMargin.width(),
63 myMargin.height() + qMax<int>( favicon.height(), textSize.height() ) + myMargin.height() );
66 QSize
67 TrackerDelegate :: sizeHint( const QStyleOptionViewItem & option,
68 const QModelIndex & index ) const
70 const TrackerInfo trackerInfo = index.data( TrackerModel::TrackerRole ).value<TrackerInfo>();
71 return sizeHint( option, trackerInfo );
74 void
75 TrackerDelegate :: paint( QPainter * painter,
76 const QStyleOptionViewItem & option,
77 const QModelIndex & index) const
79 const TrackerInfo trackerInfo = index.data( TrackerModel::TrackerRole ).value<TrackerInfo>();
80 painter->save( );
81 painter->setClipRect( option.rect );
82 drawBackground( painter, option, index );
83 drawTracker( painter, option, trackerInfo );
84 drawFocus(painter, option, option.rect );
85 painter->restore( );
88 void
89 TrackerDelegate :: drawTracker( QPainter * painter,
90 const QStyleOptionViewItem & option,
91 const TrackerInfo & inf ) const
93 painter->save( );
95 QPixmap icon = inf.st.getFavicon( );
96 QRect iconArea( option.rect.x() + myMargin.width(),
97 option.rect.y() + myMargin.height(),
98 icon.width(),
99 icon.height() );
100 painter->drawPixmap( iconArea.x(), iconArea.y()+4, icon );
102 const int textWidth = option.rect.width() - myMargin.width()*2 - mySpacing - icon.width();
103 const int textX = myMargin.width() + icon.width() + mySpacing;
104 const QString text = getText( inf );
105 QTextDocument textDoc;
106 textDoc.setHtml( text );
107 const QRect textRect( textX, iconArea.y(), textWidth, option.rect.height() - myMargin.height()*2 );
108 painter->translate( textRect.topLeft( ) );
109 textDoc.drawContents( painter, textRect.translated( -textRect.topLeft( ) ) );
111 painter->restore( );
114 void
115 TrackerDelegate :: setShowMore( bool b )
117 myShowMore = b;
120 namespace
122 QString timeToStringRounded( int seconds )
124 if( seconds > 60 ) seconds -= ( seconds % 60 );
125 return Formatter::timeToString ( seconds );
129 QString
130 TrackerDelegate :: getText( const TrackerInfo& inf ) const
132 QString key;
133 QString str;
134 const time_t now( time( 0 ) );
135 const QString err_markup_begin = "<span style=\"color:red\">";
136 const QString err_markup_end = "</span>";
137 const QString timeout_markup_begin = "<span style=\"color:#224466\">";
138 const QString timeout_markup_end = "</span>";
139 const QString success_markup_begin = "<span style=\"color:#008B00\">";
140 const QString success_markup_end = "</span>";
142 // hostname
143 str += inf.st.isBackup ? "<i>" : "<b>";
144 char * host = NULL;
145 int port = 0;
146 tr_urlParse( inf.st.announce.toUtf8().constData(), -1, NULL, &host, &port, NULL );
147 str += QString( "%1:%2" ).arg( host ).arg( port );
148 tr_free( host );
149 if( !key.isEmpty( ) ) str += " - " + key;
150 str += inf.st.isBackup ? "</i>" : "</b>";
152 // announce & scrape info
153 if( !inf.st.isBackup )
155 if( inf.st.hasAnnounced && inf.st.announceState != TR_TRACKER_INACTIVE )
157 const QString tstr( timeToStringRounded( now - inf.st.lastAnnounceTime ) );
158 str += "<br/>\n";
159 if( inf.st.lastAnnounceSucceeded )
161 str += tr( "Got a list of %1%2 peers%3 %4 ago" )
162 .arg( success_markup_begin )
163 .arg( inf.st.lastAnnouncePeerCount )
164 .arg( success_markup_end )
165 .arg( tstr );
167 else if( inf.st.lastAnnounceTimedOut )
169 str += tr( "Peer list request %1timed out%2 %3 ago; will retry" )
170 .arg( timeout_markup_begin )
171 .arg( timeout_markup_end )
172 .arg( tstr );
174 else
176 str += tr( "Got an error %1\"%2\"%3 %4 ago" )
177 .arg( err_markup_begin )
178 .arg( inf.st.lastAnnounceResult )
179 .arg( err_markup_end )
180 .arg( tstr );
184 switch( inf.st.announceState )
186 case TR_TRACKER_INACTIVE:
187 str += "<br/>\n";
188 str += tr( "No updates scheduled" );
189 break;
191 case TR_TRACKER_WAITING: {
192 const QString tstr( timeToStringRounded( inf.st.nextAnnounceTime - now ) );
193 str += "<br/>\n";
194 str += tr( "Asking for more peers in %1" ).arg( tstr );
195 break;
198 case TR_TRACKER_QUEUED:
199 str += "<br/>\n";
200 str += tr( "Queued to ask for more peers" );
201 break;
203 case TR_TRACKER_ACTIVE: {
204 const QString tstr( timeToStringRounded( now - inf.st.lastAnnounceStartTime ) );
205 str += "<br/>\n";
206 str += tr( "Asking for more peers now... <small>%1</small>" ).arg( tstr );
207 break;
211 if( myShowMore )
213 if( inf.st.hasScraped )
215 str += "<br/>\n";
216 const QString tstr( timeToStringRounded( now - inf.st.lastScrapeTime ) );
217 if( inf.st.lastScrapeSucceeded )
219 str += tr( "Tracker had %1%2 seeders%3 and %4%5 leechers%6 %7 ago" )
220 .arg( success_markup_begin )
221 .arg( inf.st.seederCount )
222 .arg( success_markup_end )
223 .arg( success_markup_begin )
224 .arg( inf.st.leecherCount )
225 .arg( success_markup_end )
226 .arg( tstr );
228 else
230 str += tr( "Got a scrape error %1\"%2\"%3 %4 ago" )
231 .arg( err_markup_begin )
232 .arg( inf.st.lastScrapeResult )
233 .arg( err_markup_end )
234 .arg( tstr );
238 switch( inf.st.scrapeState )
240 case TR_TRACKER_INACTIVE:
241 break;
243 case TR_TRACKER_WAITING: {
244 str += "<br/>\n";
245 const QString tstr( timeToStringRounded( inf.st.nextScrapeTime - now ) );
246 str += tr( "Asking for peer counts in %1" ).arg( tstr );
247 break;
250 case TR_TRACKER_QUEUED: {
251 str += "<br/>\n";
252 str += tr( "Queued to ask for peer counts" );
253 break;
256 case TR_TRACKER_ACTIVE: {
257 str += "<br/>\n";
258 const QString tstr( timeToStringRounded( now - inf.st.lastScrapeStartTime ) );
259 str += tr( "Asking for peer counts now... <small>%1</small>" ).arg( tstr );
260 break;
266 return str;