~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/favicons/faviconprovider.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007 Tobias Koenig <tokoe@kde.org>
 
3
 *   Copyright (C) 2008 Marco Martin <notmart@gmail.com>
 
4
 *
 
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.
 
9
 *
 
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
 
14
 *
 
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.
 
19
 */
 
20
 
 
21
 
 
22
#include "faviconprovider.h"
 
23
 
 
24
#include <QtGui/QImage>
 
25
#include <QtCore/QFile>
 
26
 
 
27
#include <KUrl>
 
28
#include <KIO/Job>
 
29
#include <KIO/StoredTransferJob>
 
30
#include <KMimeType>
 
31
#include <KDebug>
 
32
#include <kstandarddirs.h>
 
33
 
 
34
class FaviconProvider::Private
 
35
{
 
36
    public:
 
37
        Private( FaviconProvider *parent )
 
38
          : q(parent)
 
39
        {
 
40
        }
 
41
 
 
42
        void imageRequestFinished( KJob *job );
 
43
 
 
44
        FaviconProvider *q;
 
45
        QImage image;
 
46
        QString cachePath;
 
47
};
 
48
 
 
49
void FaviconProvider::Private::imageRequestFinished(KJob *job)
 
50
{
 
51
    if (job->error()) {
 
52
        emit q->error(q);
 
53
        return;
 
54
    }
 
55
 
 
56
    KIO::StoredTransferJob *storedJob = qobject_cast<KIO::StoredTransferJob*>(job);
 
57
    image = QImage::fromData(storedJob->data());
 
58
    if (!image.isNull()) {
 
59
        image.save(cachePath, "PNG");
 
60
    }
 
61
    emit q->finished(q);
 
62
}
 
63
 
 
64
 
 
65
FaviconProvider::FaviconProvider(QObject *parent, const QString &url)
 
66
    : QObject(parent),
 
67
      m_url(url),
 
68
      d(new Private(this))
 
69
{
 
70
    KUrl faviconUrl(url);
 
71
    if (faviconUrl.protocol().isEmpty()) {
 
72
        faviconUrl = KUrl("http://" + url);
 
73
    }
 
74
 
 
75
    const QString fileName = KMimeType::favIconForUrl(faviconUrl.url());
 
76
 
 
77
    if (!fileName.isEmpty()) {
 
78
        d->cachePath = KStandardDirs::locateLocal("cache",  fileName + ".png");
 
79
        d->image.load(d->cachePath, "PNG");
 
80
    } else {
 
81
        d->cachePath = KStandardDirs::locateLocal("cache",  "favicons/" + faviconUrl.host() + ".png");
 
82
        faviconUrl.setPath("/favicon.ico");
 
83
 
 
84
        if (faviconUrl.isValid()) {
 
85
            KIO::StoredTransferJob *job = KIO::storedGet(faviconUrl, KIO::NoReload, KIO::HideProgressInfo);
 
86
            //job->setProperty("uid", id);
 
87
            connect(job, SIGNAL(result(KJob*)), this, SLOT(imageRequestFinished(KJob*)));
 
88
        }
 
89
    }
 
90
}
 
91
 
 
92
FaviconProvider::~FaviconProvider()
 
93
{
 
94
    delete d;
 
95
}
 
96
 
 
97
QImage FaviconProvider::image() const
 
98
{
 
99
    return d->image;
 
100
}
 
101
 
 
102
QString FaviconProvider::identifier() const
 
103
{
 
104
    return m_url;
 
105
}
 
106
 
 
107
#include "faviconprovider.moc"