Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / keditbookmarks / updater.cpp
blobbe81b2f36fd1dcda824e0c9e803f08fefdf72e21
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.
22 #include "updater.h"
24 #include "bookmarkiterator.h"
25 #include "toplevel.h"
27 #include <kdebug.h>
28 #include <klocale.h>
29 #include <kapplication.h>
31 #include <kio/job.h>
33 #include <kmimetype.h>
34 #include <kparts/part.h>
35 #include <kparts/browserextension.h>
36 #include <kservicetypetrader.h>
37 #include <assert.h>
39 FavIconUpdater::FavIconUpdater(QObject *parent)
40 : 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)) );
45 m_part = 0;
46 m_webGrabber = 0;
47 m_browserIface = 0;
50 void FavIconUpdater::slotCompleted() {
51 // kDebug() << "FavIconUpdater::slotCompleted";
52 // kDebug() << "emit done(true)";
53 emit done(true);
56 void FavIconUpdater::downloadIcon(const KBookmark &bk) {
57 m_bk = 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)";
65 emit done(true);
67 } else {
68 kDebug()<<"no favicon found "<<endl;
69 webupdate = false;
70 m_favIconModule.downloadHostIcon(url);
74 FavIconUpdater::~FavIconUpdater() {
75 // kDebug() << "~FavIconUpdater";
76 delete m_browserIface;
77 delete m_webGrabber;
78 delete m_part;
81 void FavIconUpdater::downloadIconActual(const KBookmark &bk) {
82 kDebug()<<"FavIconUpdater::downloadIconActual"<<endl;
83 m_bk = bk;
84 webupdate = true;
86 if (!m_part) {
87 KParts::ReadOnlyPart *part
88 = KServiceTypeTrader
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);
105 assert(ext);
107 m_browserIface = new FavIconBrowserInterface(this);
108 ext->setBrowserInterface(m_browserIface);
110 connect(ext, SIGNAL( setIconURL(const KUrl &) ),
111 this, SLOT( setIconURL(const KUrl &) ));
113 m_part = part;
116 m_webGrabber = new FavIconWebGrabber(m_part, bk.url());
119 // khtml callback
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)
128 Q_UNUSED(isHost);
129 Q_UNUSED(hostOrURL);
131 // kDebug() << "FavIconUpdater::notifyChange()";
133 if(iconName.isNull() && !webupdate)
135 // no icon found, try webupdater
136 downloadIconActual(m_bk);
138 else
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
166 sjob->putOnHold();
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) {
176 if (job->error()) {
177 // kDebug() << "FavIconWebGrabber::slotFinished() " << job->errorString();
181 #include "updater.moc"