Transmission: update to 2.82
[tomato.git] / release / src / router / transmission / qt / favicon.cc
blob72c29d3d4402fae930c1d8484c60312d83de9137
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: favicon.cc 14150 2013-07-27 21:58:14Z jordan $
13 #include <QDir>
14 #include <QNetworkAccessManager>
15 #include <QNetworkReply>
16 #include <QNetworkRequest>
17 #include <QStandardPaths>
19 #include "favicon.h"
21 /***
22 ****
23 ***/
25 Favicons :: Favicons( )
27 myNAM = new QNetworkAccessManager( );
28 connect( myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)) );
31 Favicons :: ~Favicons( )
33 delete myNAM;
36 /***
37 ****
38 ***/
40 QString
41 Favicons :: getCacheDir( )
43 const QString base = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
44 return QDir( base ).absoluteFilePath( "favicons" );
47 void
48 Favicons :: ensureCacheDirHasBeenScanned( )
50 static bool hasBeenScanned = false;
52 if( !hasBeenScanned )
54 hasBeenScanned = true;
56 QDir cacheDir( getCacheDir( ) );
57 cacheDir.mkpath( cacheDir.absolutePath( ) );
59 QStringList files = cacheDir.entryList( QDir::Files|QDir::Readable );
60 foreach( QString file, files ) {
61 QPixmap pixmap;
62 pixmap.load( cacheDir.absoluteFilePath( file ) );
63 if( !pixmap.isNull( ) )
64 myPixmaps.insert( file, pixmap );
69 QString
70 Favicons :: getHost( const QUrl& url )
72 QString host = url.host( );
73 const int first_dot = host.indexOf( '.' );
74 const int last_dot = host.lastIndexOf( '.' );
76 if( ( first_dot != -1 ) && ( last_dot != -1 ) && ( first_dot != last_dot ) )
77 host.remove( 0, first_dot + 1 );
79 return host;
82 QPixmap
83 Favicons :: find( const QUrl& url )
85 return findFromHost( getHost( url ) );
88 namespace
90 const QSize rightSize( 16, 16 );
93 QPixmap
94 Favicons :: findFromHost( const QString& host )
96 ensureCacheDirHasBeenScanned( );
98 const QPixmap pixmap = myPixmaps[ host ];
99 return pixmap.size()==rightSize ? pixmap : pixmap.scaled(rightSize);
102 void
103 Favicons :: add( const QUrl& url )
105 ensureCacheDirHasBeenScanned( );
107 const QString host = getHost( url );
109 if( !myPixmaps.contains( host ) )
111 // add a placholder s.t. we only ping the server once per session
112 QPixmap tmp( rightSize );
113 tmp.fill( Qt::transparent );
114 myPixmaps.insert( host, tmp );
116 // try to download the favicon
117 const QString path = "http://" + host + "/favicon.";
118 QStringList suffixes;
119 suffixes << "ico" << "png" << "gif" << "jpg";
120 foreach( QString suffix, suffixes )
121 myNAM->get( QNetworkRequest( path + suffix ) );
125 void
126 Favicons :: onRequestFinished( QNetworkReply * reply )
128 const QString host = reply->url().host();
130 QPixmap pixmap;
132 const QByteArray content = reply->readAll( );
133 if( !reply->error( ) )
134 pixmap.loadFromData( content );
136 if( !pixmap.isNull( ) )
138 // save it in memory...
139 myPixmaps.insert( host, pixmap );
141 // save it on disk...
142 QDir cacheDir( getCacheDir( ) );
143 cacheDir.mkpath( cacheDir.absolutePath( ) );
144 QFile file( cacheDir.absoluteFilePath( host ) );
145 file.open( QIODevice::WriteOnly );
146 file.write( content );
147 file.close( );
149 // notify listeners
150 emit pixmapReady( host );