1 | /* |
---|
2 | * This file Copyright (C) 2010 Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <QDesktopServices> |
---|
14 | #include <QDir> |
---|
15 | #include <QNetworkAccessManager> |
---|
16 | #include <QNetworkReply> |
---|
17 | #include <QNetworkRequest> |
---|
18 | |
---|
19 | #include "favicon.h" |
---|
20 | |
---|
21 | /*** |
---|
22 | **** |
---|
23 | ***/ |
---|
24 | |
---|
25 | Favicons :: Favicons( ) |
---|
26 | { |
---|
27 | myNAM = new QNetworkAccessManager( ); |
---|
28 | connect( myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onRequestFinished(QNetworkReply*)) ); |
---|
29 | } |
---|
30 | |
---|
31 | Favicons :: ~Favicons( ) |
---|
32 | { |
---|
33 | delete myNAM; |
---|
34 | } |
---|
35 | |
---|
36 | /*** |
---|
37 | **** |
---|
38 | ***/ |
---|
39 | |
---|
40 | QString |
---|
41 | Favicons :: getCacheDir( ) |
---|
42 | { |
---|
43 | const QString base = QDesktopServices::storageLocation( QDesktopServices::CacheLocation ); |
---|
44 | return QDir( base ).absoluteFilePath( "favicons" ); |
---|
45 | } |
---|
46 | |
---|
47 | void |
---|
48 | Favicons :: ensureCacheDirHasBeenScanned( ) |
---|
49 | { |
---|
50 | static bool hasBeenScanned = false; |
---|
51 | |
---|
52 | if( !hasBeenScanned ) |
---|
53 | { |
---|
54 | hasBeenScanned = true; |
---|
55 | |
---|
56 | QDir cacheDir( getCacheDir( ) ); |
---|
57 | cacheDir.mkpath( cacheDir.absolutePath( ) ); |
---|
58 | |
---|
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 ); |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | QString |
---|
70 | Favicons :: getHost( const QUrl& url ) |
---|
71 | { |
---|
72 | QString host = url.host( ); |
---|
73 | const int first_dot = host.indexOf( '.' ); |
---|
74 | const int last_dot = host.lastIndexOf( '.' ); |
---|
75 | |
---|
76 | if( ( first_dot != -1 ) && ( last_dot != -1 ) && ( first_dot != last_dot ) ) |
---|
77 | host.remove( 0, first_dot + 1 ); |
---|
78 | |
---|
79 | return host; |
---|
80 | } |
---|
81 | |
---|
82 | QPixmap |
---|
83 | Favicons :: find( const QUrl& url ) |
---|
84 | { |
---|
85 | ensureCacheDirHasBeenScanned( ); |
---|
86 | |
---|
87 | return myPixmaps[ getHost(url) ]; |
---|
88 | } |
---|
89 | |
---|
90 | void |
---|
91 | Favicons :: add( const QUrl& url ) |
---|
92 | { |
---|
93 | ensureCacheDirHasBeenScanned( ); |
---|
94 | |
---|
95 | const QString host = getHost( url ); |
---|
96 | if( !myPixmaps.contains( host ) && !myPending.contains( host ) ) |
---|
97 | { |
---|
98 | myPending.append( host ); |
---|
99 | |
---|
100 | const QString path = "http://" + host + "/favicon."; |
---|
101 | QStringList suffixes; |
---|
102 | suffixes << "ico" << "png" << "gif" << "jpg"; |
---|
103 | foreach( QString suffix, suffixes ) |
---|
104 | myNAM->get( QNetworkRequest( path + suffix ) ); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void |
---|
109 | Favicons :: onRequestFinished( QNetworkReply * reply ) |
---|
110 | { |
---|
111 | const QString host = reply->url().host(); |
---|
112 | myPending.removeAll( host ); |
---|
113 | |
---|
114 | QPixmap pixmap; |
---|
115 | |
---|
116 | const QByteArray content = reply->readAll( ); |
---|
117 | if( !reply->error( ) ) |
---|
118 | pixmap.loadFromData( content ); |
---|
119 | |
---|
120 | if( !pixmap.isNull( ) ) |
---|
121 | { |
---|
122 | // save it in memory... |
---|
123 | myPixmaps.insert( host, pixmap ); |
---|
124 | |
---|
125 | // save it on disk... |
---|
126 | QDir cacheDir( getCacheDir( ) ); |
---|
127 | cacheDir.mkpath( cacheDir.absolutePath( ) ); |
---|
128 | QFile file( cacheDir.absoluteFilePath( host ) ); |
---|
129 | file.open( QIODevice::WriteOnly ); |
---|
130 | file.write( content ); |
---|
131 | file.close( ); |
---|
132 | |
---|
133 | // notify listeners |
---|
134 | emit pixmapReady( host ); |
---|
135 | } |
---|
136 | } |
---|