1 // -*- indent-tabs-mode:nil -*-
2 // vim: set ts=4 sts=4 sw=4 et:
3 /* This file is part of the KDE project
4 Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License version 2 or at your option version 3 as published by
9 the Free Software Foundation.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
24 #include "bookmarkiterator.h"
29 #include <kapplication.h>
33 #include <kmimetype.h>
34 #include <kparts/part.h>
35 #include <kparts/browserextension.h>
36 #include <kservicetypetrader.h>
39 FavIconUpdater::FavIconUpdater(QObject
*parent
)
41 m_favIconModule("org.kde.kded", "/modules/favicons", QDBusConnection::sessionBus())
43 QObject::connect(&m_favIconModule
, SIGNAL(iconChanged(bool,QString
,QString
)),
44 this, SLOT(notifyChange(bool,QString
,QString
)) );
50 void FavIconUpdater::slotCompleted() {
51 // kDebug() << "FavIconUpdater::slotCompleted";
52 // kDebug() << "emit done(true)";
56 void FavIconUpdater::downloadIcon(const KBookmark
&bk
) {
58 const QString
& url
= bk
.url().url();
59 QString favicon
= KMimeType::favIconForUrl(url
);
60 if (!favicon
.isNull()) {
61 // kDebug() << "downloadIcon() - favicon" << favicon;
62 bk
.internalElement().setAttribute("icon", favicon
);
63 KEBApp::self()->notifyCommandExecuted();
64 // kDebug() << "emit done(true)";
68 kDebug()<<"no favicon found "<<endl
;
70 m_favIconModule
.downloadHostIcon(url
);
74 FavIconUpdater::~FavIconUpdater() {
75 // kDebug() << "~FavIconUpdater";
76 delete m_browserIface
;
81 void FavIconUpdater::downloadIconActual(const KBookmark
&bk
) {
82 kDebug()<<"FavIconUpdater::downloadIconActual"<<endl
;
87 KParts::ReadOnlyPart
*part
89 ::createInstanceFromQuery
<KParts::ReadOnlyPart
>("text/html", QString());
91 part
->setProperty("pluginsEnabled", QVariant(false));
92 part
->setProperty("javaScriptEnabled", QVariant(false));
93 part
->setProperty("javaEnabled", QVariant(false));
94 part
->setProperty("autoloadImages", QVariant(false));
96 //FIXME only connect to result?
97 // connect(part, SIGNAL( result(KIO::Job * job)),
98 // this, SLOT( slotCompleted()));
99 connect(part
, SIGNAL( canceled(const QString
&) ),
100 this, SLOT( slotCompleted() ));
101 connect(part
, SIGNAL( completed() ),
102 this, SLOT( slotCompleted() ));
104 KParts::BrowserExtension
*ext
= KParts::BrowserExtension::childObject(part
);
107 m_browserIface
= new FavIconBrowserInterface(this);
108 ext
->setBrowserInterface(m_browserIface
);
110 connect(ext
, SIGNAL( setIconURL(const KUrl
&) ),
111 this, SLOT( setIconURL(const KUrl
&) ));
116 m_webGrabber
= new FavIconWebGrabber(m_part
, bk
.url());
120 void FavIconUpdater::setIconURL(const KUrl
&iconURL
) {
121 m_favIconModule
.setIconForUrl(m_bk
.url().url(), iconURL
.url());
124 void FavIconUpdater::notifyChange(bool isHost
,
125 const QString
& hostOrURL
,
126 const QString
& iconName
)
131 // kDebug() << "FavIconUpdater::notifyChange()";
133 if(iconName
.isNull() && !webupdate
)
135 // no icon found, try webupdater
136 downloadIconActual(m_bk
);
140 // Either we have an icon or we already tried the webupdater
141 m_bk
.internalElement().setAttribute("icon", iconName
);
142 emit
done(!iconName
.isNull());
146 /* -------------------------- */
148 FavIconWebGrabber::FavIconWebGrabber(KParts::ReadOnlyPart
*part
, const KUrl
&url
)
149 : m_part(part
), m_url(url
) {
151 // kDebug() << "FavIconWebGrabber::FavIconWebGrabber starting KIO::get()";
153 // the use of KIO rather than directly using KHTML is to allow silently abort on error
155 KIO::Job
*job
= KIO::get(m_url
, KIO::NoReload
, KIO::HideProgressInfo
);
156 job
->addMetaData( QString("cookies"), QString("none") );
157 connect(job
, SIGNAL( result( KJob
*)),
158 this, SLOT( slotFinished(KJob
*) ));
159 connect(job
, SIGNAL( mimetype( KIO::Job
*, const QString
&) ),
160 this, SLOT( slotMimetype(KIO::Job
*, const QString
&) ));
163 void FavIconWebGrabber::slotMimetype(KIO::Job
*job
, const QString
& /*type*/) {
164 KIO::SimpleJob
*sjob
= static_cast<KIO::SimpleJob
*>(job
);
165 m_url
= sjob
->url(); // allow for redirection
168 // QString typeLocal = typeUncopied; // local copy
169 // kDebug() << "slotMimetype : " << typeLocal;
170 // TODO - what to do if typeLocal is not text/html ??
172 m_part
->openUrl(m_url
);
175 void FavIconWebGrabber::slotFinished(KJob
*job
) {
177 // kDebug() << "FavIconWebGrabber::slotFinished() " << job->errorString();
181 #include "updater.moc"