• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Engines

faviconprovider.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Tobias Koenig <tokoe@kde.org>
00003  *   Copyright (C) 2008 Marco Martin <notmart@gmail.com>
00004  *
00005  *   This program is free software; you can redistribute it and/or modify  
00006  *   it under the terms of the GNU Library General Public License as published by  
00007  *   the Free Software Foundation; either version 2 of the License, or     
00008  *   (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details
00014  *
00015  *   You should have received a copy of the GNU Library General Public
00016  *   License along with this program; if not, write to the
00017  *   Free Software Foundation, Inc.,
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 
00022 #include "faviconprovider.h"
00023 
00024 #include <QtGui/QImage>
00025 #include <QtCore/QFile>
00026 
00027 #include <KUrl>
00028 #include <KIO/Job>
00029 #include <KIO/StoredTransferJob>
00030 #include <KMimeType>
00031 #include <KDebug>
00032 #include <kstandarddirs.h>
00033 
00034 class FaviconProvider::Private
00035 {
00036     public:
00037         Private( FaviconProvider *parent )
00038           : q(parent)
00039         {
00040         }
00041 
00042         void imageRequestFinished( KJob *job );
00043 
00044         FaviconProvider *q;
00045         QImage image;
00046         QString cachePath;
00047 };
00048 
00049 void FaviconProvider::Private::imageRequestFinished(KJob *job)
00050 {
00051     if (job->error()) {
00052         emit q->error(q);
00053         return;
00054     }
00055 
00056     KIO::StoredTransferJob *storedJob = qobject_cast<KIO::StoredTransferJob*>(job);
00057     image = QImage::fromData(storedJob->data());
00058     if (!image.isNull()) {
00059         image.save(cachePath, "PNG");
00060     }
00061     emit q->finished(q);
00062 }
00063 
00064 
00065 FaviconProvider::FaviconProvider(QObject *parent, const QString &url)
00066     : QObject(parent),
00067       m_url(url),
00068       d(new Private(this))
00069 {
00070     KUrl faviconUrl(url);
00071     if (faviconUrl.protocol().isEmpty()) {
00072         QString host = faviconUrl.host();
00073         faviconUrl = KUrl("http://" + url);
00074     }
00075 
00076     QString fileName = KMimeType::favIconForUrl(faviconUrl.url());
00077 
00078     if (!fileName.isEmpty()) {
00079         d->cachePath = KStandardDirs::locateLocal("cache",  fileName + ".png");
00080         d->image.load(d->cachePath, "PNG");
00081     } else {
00082         d->cachePath = KStandardDirs::locateLocal("cache",  "favicons/" + faviconUrl.host() + ".png");
00083         faviconUrl.setPath("/favicon.ico");
00084 
00085         if (faviconUrl.isValid()) {
00086             KIO::StoredTransferJob *job = KIO::storedGet(faviconUrl, KIO::NoReload, KIO::HideProgressInfo);
00087             //job->setProperty("uid", id);
00088             connect(job, SIGNAL(result(KJob*)), this, SLOT(imageRequestFinished(KJob*)));
00089         }
00090     }
00091 }
00092 
00093 FaviconProvider::~FaviconProvider()
00094 {
00095     delete d;
00096 }
00097 
00098 QImage FaviconProvider::image() const
00099 {
00100     return d->image;
00101 }
00102 
00103 QString FaviconProvider::identifier() const
00104 {
00105     return m_url;
00106 }
00107 
00108 #include "faviconprovider.moc"

Engines

Skip menu "Engines"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal