add more spacing
[personal-kdebase.git] / workspace / plasma / dataengines / favicons / faviconprovider.cpp
blobafd992bc18967bb4664b24534551c5bb6a74f141
1 /*
2 * Copyright (C) 2007 Tobias Koenig <tokoe@kde.org>
3 * Copyright (C) 2008 Marco Martin <notmart@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "faviconprovider.h"
24 #include <QtGui/QImage>
25 #include <QtCore/QFile>
27 #include <KUrl>
28 #include <KIO/Job>
29 #include <KIO/StoredTransferJob>
30 #include <KMimeType>
31 #include <KDebug>
32 #include <kstandarddirs.h>
34 class FaviconProvider::Private
36 public:
37 Private( FaviconProvider *parent )
38 : q(parent)
42 void imageRequestFinished( KJob *job );
44 FaviconProvider *q;
45 QImage image;
46 QString cachePath;
49 void FaviconProvider::Private::imageRequestFinished(KJob *job)
51 if (job->error()) {
52 emit q->error(q);
53 return;
56 KIO::StoredTransferJob *storedJob = qobject_cast<KIO::StoredTransferJob*>(job);
57 image = QImage::fromData(storedJob->data());
58 if (!image.isNull()) {
59 image.save(cachePath, "PNG");
61 emit q->finished(q);
65 FaviconProvider::FaviconProvider(QObject *parent, const QString &url)
66 : QObject(parent),
67 m_url(url),
68 d(new Private(this))
70 KUrl faviconUrl(url);
71 if (faviconUrl.protocol().isEmpty()) {
72 QString host = faviconUrl.host();
73 faviconUrl = KUrl("http://" + url);
76 QString fileName = KMimeType::favIconForUrl(faviconUrl.url());
78 if (!fileName.isEmpty()) {
79 d->cachePath = KStandardDirs::locateLocal("cache", fileName + ".png");
80 d->image.load(d->cachePath, "PNG");
81 } else {
82 d->cachePath = KStandardDirs::locateLocal("cache", "favicons/" + faviconUrl.host() + ".png");
83 faviconUrl.setPath("/favicon.ico");
85 if (faviconUrl.isValid()) {
86 KIO::StoredTransferJob *job = KIO::storedGet(faviconUrl, KIO::NoReload, KIO::HideProgressInfo);
87 //job->setProperty("uid", id);
88 connect(job, SIGNAL(result(KJob*)), this, SLOT(imageRequestFinished(KJob*)));
93 FaviconProvider::~FaviconProvider()
95 delete d;
98 QImage FaviconProvider::image() const
100 return d->image;
103 QString FaviconProvider::identifier() const
105 return m_url;
108 #include "faviconprovider.moc"