Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / konqueror / sidebar / web_module / web_module.cpp
blob29a799b8d52527437d0cd6b9e194c934b239fd48
1 /* This file is part of the KDE project
2 Copyright (C) 2003, George Staikos <staikos@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "web_module.h"
20 #include "favicon_interface.h"
22 #include <QtCore/QFileInfo>
23 #include <QtGui/QSpinBox>
24 #include <QtCore/QTimer>
26 #include <dom/html_inline.h>
27 #include <kdebug.h>
28 #include <kdialog.h>
29 #include <kglobal.h>
30 #include <klocale.h>
31 #include <kparts/browserextension.h>
32 #include <kstandarddirs.h>
33 #include <khbox.h>
36 KonqSideBarWebModule::KonqSideBarWebModule(const KComponentData &componentData, QObject *parent, QWidget *widgetParent, QString &desktopName, const char* name)
37 : KonqSidebarPlugin(componentData, parent, widgetParent, desktopName, name)
39 _htmlPart = new KHTMLSideBar(universalMode());
40 connect(_htmlPart, SIGNAL(reload()), this, SLOT(reload()));
41 connect(_htmlPart, SIGNAL(completed()), this, SLOT(pageLoaded()));
42 connect(_htmlPart,
43 SIGNAL(setWindowCaption(const QString&)),
44 this,
45 SLOT(setTitle(const QString&)));
46 connect(_htmlPart,
47 SIGNAL(openUrlRequest(QString, KParts::OpenUrlArguments, KParts::BrowserArguments)),
48 this,
49 SLOT(urlClicked(QString, KParts::OpenUrlArguments, KParts::BrowserArguments)));
50 connect(_htmlPart->browserExtension(),
51 SIGNAL(openUrlRequest(QString, const KParts::OpenUrlArguments&, const KParts::BrowserArguments &)),
52 this,
53 SLOT(formClicked(QString, const KParts::OpenUrlArguments&, const KParts::BrowserArguments &)) );
54 connect(_htmlPart,
55 SIGNAL(setAutoReload()), this, SLOT( setAutoReload() ));
56 connect(_htmlPart,
57 SIGNAL(openUrlNewWindow(const QString&, KParts::OpenUrlArguments, KParts::BrowserArguments)),
58 this,
59 SLOT(urlNewWindow(const QString&, KParts::OpenUrlArguments, KParts::BrowserArguments)));
60 connect(_htmlPart,
61 SIGNAL(submitFormRequest(const char*,const QString&,const QByteArray&,const QString&,const QString&,const QString&)),
62 this,
63 SIGNAL(submitFormRequest(const char*,const QString&,const QByteArray&,const QString&,const QString&,const QString&)));
65 _desktopName = desktopName;
67 KConfig _ksc( _desktopName, KConfig::SimpleConfig );
68 KConfigGroup ksc(&_ksc, "Desktop Entry");
69 reloadTimeout = ksc.readEntry("Reload", 0);
70 _url = ksc.readPathEntry("URL", QString());
71 _htmlPart->openUrl(_url );
72 // Must load this delayed
73 QTimer::singleShot(0, this, SLOT(loadFavicon()));
77 KonqSideBarWebModule::~KonqSideBarWebModule() {
78 delete _htmlPart;
79 _htmlPart = 0L;
83 QWidget *KonqSideBarWebModule::getWidget() {
84 return _htmlPart->widget();
87 void KonqSideBarWebModule::setAutoReload(){
88 KDialog dlg( 0 );
89 dlg.setModal( true );
90 dlg.setCaption( i18n("Set Refresh Timeout (0 disables)" ) );
91 dlg.setButtons( KDialog::Ok | KDialog::Cancel );
93 KHBox *hbox = new KHBox( &dlg );
94 dlg.setMainWidget( hbox );
96 QSpinBox *mins = new QSpinBox( hbox );
97 mins->setRange(0, 120);
98 mins->setSuffix( i18n(" min") );
99 QSpinBox *secs = new QSpinBox( 0, 59, 1, hbox );
100 secs->setSuffix( i18n(" sec") );
102 if( reloadTimeout > 0 ) {
103 int seconds = reloadTimeout / 1000;
104 secs->setValue( seconds % 60 );
105 mins->setValue( ( seconds - secs->value() ) / 60 );
108 if( dlg.exec() == KDialog::Accepted ) {
109 int msec = ( mins->value() * 60 + secs->value() ) * 1000;
110 reloadTimeout = msec;
111 KConfig _ksc( _desktopName, KConfig::SimpleConfig );
112 KConfigGroup ksc(&_ksc, "Desktop Entry");
113 ksc.writeEntry("Reload", reloadTimeout);
114 reload();
118 void *KonqSideBarWebModule::provides(const QString &) {
119 return 0L;
123 void KonqSideBarWebModule::handleURL(const KUrl &) {
127 void KonqSideBarWebModule::urlNewWindow(const QString& url, const KParts::OpenUrlArguments& args, const KParts::BrowserArguments& browserArgs)
129 emit createNewWindow(KUrl(url), args, browserArgs);
133 void KonqSideBarWebModule::urlClicked(const QString& url, const KParts::OpenUrlArguments& args, const KParts::BrowserArguments& browserArgs)
135 emit openUrlRequest(KUrl(url), args, browserArgs);
139 void KonqSideBarWebModule::formClicked(const KUrl& url, const KParts::OpenUrlArguments& args, const KParts::BrowserArguments& browserArgs)
141 _htmlPart->setArguments( args );
142 _htmlPart->browserExtension()->setBrowserArguments(browserArgs);
143 _htmlPart->openUrl(url);
146 void KonqSideBarWebModule::loadFavicon() {
147 QString icon = KMimeType::favIconForUrl(_url);
148 if (icon.isEmpty()) {
149 org::kde::FavIcon favicon("org.kde.kded", "/modules/favicons", QDBusConnection::sessionBus());
150 favicon.downloadHostIcon(_url.url());
151 icon = KMimeType::favIconForUrl(_url);
154 if (!icon.isEmpty()) {
155 emit setIcon(icon);
157 KConfig _ksc( _desktopName, KConfig::SimpleConfig );
158 KConfigGroup ksc(&_ksc, "Desktop Entry");
159 if (icon != ksc.readPathEntry("Icon", QString())) {
160 ksc.writePathEntry("Icon", icon);
166 void KonqSideBarWebModule::reload() {
167 _htmlPart->openUrl(_url);
171 void KonqSideBarWebModule::setTitle(const QString& title) {
172 if (!title.isEmpty()) {
173 emit setCaption(title);
175 KConfig _ksc( _desktopName, KConfig::SimpleConfig );
176 KConfigGroup ksc(&_ksc, "Desktop Entry");
177 if (title != ksc.readPathEntry("Name", QString())) {
178 ksc.writePathEntry("Name", title);
184 void KonqSideBarWebModule::pageLoaded() {
185 if( reloadTimeout > 0 ) {
186 QTimer::singleShot( reloadTimeout, this, SLOT( reload() ) );
191 extern "C" {
192 KDE_EXPORT KonqSidebarPlugin* create_konqsidebar_web(const KComponentData &componentData, QObject *parent, QWidget *widget, QString &desktopName, const char *name) {
193 return new KonqSideBarWebModule(componentData, parent, widget, desktopName, name);
198 extern "C" {
199 KDE_EXPORT bool add_konqsidebar_web(QString* fn, QString* param, QMap<QString,QString> *map) {
200 Q_UNUSED(param);
201 KGlobal::dirs()->addResourceType("websidebardata", "data", "konqsidebartng/websidebar");
202 KUrl url;
203 url.setProtocol("file");
204 QStringList paths = KGlobal::dirs()->resourceDirs("websidebardata");
205 for (QStringList::Iterator i = paths.begin(); i != paths.end(); ++i) {
206 if (QFileInfo(*i + "websidebar.html").exists()) {
207 url.setPath(*i + "websidebar.html");
208 break;
212 if (url.path().isEmpty())
213 return false;
214 map->insert("Type", "Link");
215 map->insert("URL", url.url());
216 map->insert("Icon", "netscape");
217 map->insert("Name", i18n("Web SideBar Plugin"));
218 map->insert("Open", "true");
219 map->insert("X-KDE-KonqSidebarModule","konqsidebar_web");
220 fn->setLatin1("websidebarplugin%1.desktop");
221 return true;
226 bool KHTMLSideBar::urlSelected( const QString &url, int button,
227 int state, const QString &_target,
228 const KParts::OpenUrlArguments& args,
229 const KParts::BrowserArguments& browserArgs )
231 if (button == Qt::LeftButton ){
232 if (_target.toLower() == "_self") {
233 openUrl(url);
234 } else if (_target.toLower() == "_blank") {
235 emit openUrlNewWindow(completeURL(url).url(), args);
236 } else { // isEmpty goes here too
237 emit openUrlRequest(completeURL(url).url(), args);
239 return true;
241 if (button == Qt::MidButton) {
242 emit openUrlNewWindow(completeURL(url).url(),
243 args);
244 return true;
246 // A refresh
247 if (button == 0 && _target.toLower() == "_self") {
248 openUrl(completeURL(url));
249 return true;
251 return KHTMLPart::urlSelected(url, button, state, _target, args, browserArgs);
254 #include "web_module.moc"