1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include "precompiled.h"
#include "IconCache.h"
IconCache::IconCache(const QString& path) :
cachePath(path)
{
nam.setParent(this);
connect(&nam, SIGNAL(finished(QNetworkReply*)), SLOT(finished(QNetworkReply*)));
QFileInfo info(path);
if (!info.exists()) {
QDir d;
d.mkdir(path);
}
}
QString IconCache::getIconPath(const QString& site)
{
QString cachedName = site;
if (site.contains("http"))
cachedName = QUrl(site).host();
qDebug() << cachedName;
//cachedName = cachedName.replace("http:", "").replace("https:", "").replace("/", "");
QFileInfo info;
info.setFile(cachePath, cachedName + ".png");
if (info.exists())
return info.size() > 0 ? info.absoluteFilePath() : QString();
info.setFile(cachePath, cachedName + ".ico");
if (info.exists())
return info.size() > 0 ? info.absoluteFilePath() : QString();
if (!site.startsWith("http"))
return "";
// Call the main thread to grab the icon in the background
emit findIcon(QUrl("http://" + QUrl(site).host() + "/favicon.ico"));
return "";
}
void IconCache::query(QUrl url)
{
qDebug() << "Going for" << url;
QNetworkRequest request;
request.setUrl(url);
request.setAttribute(QNetworkRequest::User, url);
nam.get(request);
}
void IconCache::finished(QNetworkReply* reply)
{
QUrl url = reply->url();
if (reply && reply->error() == QNetworkReply::NoError)
{
QFile file(cachePath + url.host() + ".ico");
if (!file.open(QIODevice::WriteOnly))
{
qDebug() << "Could not open icon for writing";
return;
}
QByteArray ba = reply->readAll();
file.write(ba);
}
reply->deleteLater();
/*
QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (redirectionTarget.isValid())
{
delete reply;
reply = depth < 5 ? query(redirectionTarget.toUrl(), depth + 1) : NULL;
}
*/
}
|