File indexing completed on 2024-04-28 04:58:28
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 2003 George Staikos <staikos@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #include "web_module.h" 0008 #include <QAction> 0009 #include <KIO/FavIconRequestJob> 0010 0011 #include <QFileInfo> 0012 #include <QTimer> 0013 0014 #include <dom/html_inline.h> 0015 #include <kdialog.h> 0016 #include <kglobal.h> 0017 #include <KLocalizedString> 0018 #include <kpluginfactory.h> 0019 #include "kf5compat.h" //For NavigationExtension 0020 #include <knameandurlinputdialog.h> 0021 0022 #include <QHBoxLayout> 0023 #include <knuminput.h> 0024 0025 KHTMLSideBar::KHTMLSideBar() 0026 : KHTMLPart() 0027 { 0028 setStatusMessagesEnabled(false); 0029 setMetaRefreshEnabled(true); 0030 setJavaEnabled(false); 0031 setPluginsEnabled(false); 0032 0033 setFormNotification(KHTMLPart::Only); 0034 connect(this, 0035 SIGNAL(formSubmitNotification(const char*,QString,QByteArray,QString,QString,QString)), 0036 this, 0037 SLOT(formProxy(const char*,QString,QByteArray,QString,QString,QString)) 0038 ); 0039 0040 _linkMenu = new KMenu(widget()); 0041 0042 QAction *openLinkAction = new QAction(i18n("&Open Link"), this); 0043 _linkMenu->addAction(openLinkAction); 0044 connect(openLinkAction, SIGNAL(triggered()), this, SLOT(loadPage())); 0045 0046 QAction *openWindowAction = new QAction(i18n("Open in New &Window"), this); 0047 _linkMenu->addAction(openWindowAction); 0048 connect(openWindowAction, SIGNAL(triggered()), this, SLOT(loadNewWindow())); 0049 0050 _menu = new KMenu(widget()); 0051 0052 QAction *reloadAction = new QAction(i18n("&Reload"), this); 0053 reloadAction->setIcon(QIcon::fromTheme("view-refresh")); 0054 _menu->addAction(reloadAction); 0055 connect(reloadAction, SIGNAL(triggered()), this, SIGNAL(reload())); 0056 0057 QAction *autoReloadAction = new QAction(i18n("Set &Automatic Reload"), this); 0058 autoReloadAction->setIcon(QIcon::fromTheme("view-refresh")); 0059 _menu->addAction(autoReloadAction); 0060 connect(autoReloadAction, SIGNAL(triggered()), this, SIGNAL(setAutoReload())); 0061 0062 connect(this, SIGNAL(popupMenu(QString,QPoint)), 0063 this, SLOT(showMenu(QString,QPoint))); 0064 0065 } 0066 0067 //// 0068 0069 KonqSideBarWebModule::KonqSideBarWebModule(QWidget *parent, const KConfigGroup &configGroup) 0070 : KonqSidebarModule(parent, configGroup) 0071 { 0072 _htmlPart = new KHTMLSideBar(); 0073 _htmlPart->setAutoDeletePart(false); 0074 connect(_htmlPart, SIGNAL(reload()), this, SLOT(reload())); 0075 connect(_htmlPart, SIGNAL(completed()), this, SLOT(pageLoaded())); 0076 connect(_htmlPart, 0077 SIGNAL(setWindowCaption(QString)), 0078 this, 0079 SLOT(setTitle(QString))); 0080 connect(_htmlPart, 0081 SIGNAL(openUrlRequest(QString,KParts::OpenUrlArguments,BrowserArguments)), 0082 this, 0083 SLOT(urlClicked(QString,KParts::OpenUrlArguments,BrowserArguments))); 0084 connect(_htmlPart->browserExtension(), 0085 SIGNAL(openUrlRequest(QUrl,KParts::OpenUrlArguments,BrowserArguments)), 0086 this, 0087 SLOT(formClicked(QUrl,KParts::OpenUrlArguments,BrowserArguments))); 0088 connect(_htmlPart, 0089 SIGNAL(setAutoReload()), this, SLOT(setAutoReload())); 0090 connect(_htmlPart, 0091 SIGNAL(openUrlNewWindow(QString,KParts::OpenUrlArguments,BrowserArguments,KParts::WindowArgs)), 0092 this, 0093 SLOT(urlNewWindow(QString,KParts::OpenUrlArguments,BrowserArguments,KParts::WindowArgs))); 0094 connect(_htmlPart, 0095 SIGNAL(submitFormRequest(const char*,QString,QByteArray,QString,QString,QString)), 0096 this, 0097 SIGNAL(submitFormRequest(const char*,QString,QByteArray,QString,QString,QString))); 0098 0099 reloadTimeout = configGroup.readEntry("Reload", 0); 0100 _url = QUrl(configGroup.readPathEntry("URL", QString())); 0101 _htmlPart->openUrl(_url); 0102 // Must load this delayed 0103 QTimer::singleShot(0, this, SLOT(loadFavicon())); 0104 } 0105 0106 KonqSideBarWebModule::~KonqSideBarWebModule() 0107 { 0108 delete _htmlPart; 0109 _htmlPart = 0L; 0110 } 0111 0112 QWidget *KonqSideBarWebModule::getWidget() 0113 { 0114 return _htmlPart->widget(); 0115 } 0116 0117 void KonqSideBarWebModule::setAutoReload() 0118 { 0119 KDialog dlg(0); 0120 dlg.setModal(true); 0121 dlg.setCaption(i18nc("@title:window", "Set Refresh Timeout (0 disables)")); 0122 dlg.setButtons(KDialog::Ok | KDialog::Cancel); 0123 0124 QWidget *hbox = new QWidget(&dlg); 0125 QHBoxLayout *hboxHBoxLayout = new QHBoxLayout(hbox); 0126 hboxHBoxLayout->setContentsMargins(0, 0, 0, 0); 0127 dlg.setMainWidget(hbox); 0128 0129 KIntNumInput *mins = new KIntNumInput(hbox); 0130 hboxHBoxLayout->addWidget(mins); 0131 mins->setRange(0, 120); 0132 mins->setSuffix(ki18np(" minute", " minutes")); 0133 KIntNumInput *secs = new KIntNumInput(hbox); 0134 hboxHBoxLayout->addWidget(secs); 0135 secs->setRange(0, 59); 0136 secs->setSuffix(ki18np(" second", " seconds")); 0137 0138 if (reloadTimeout > 0) { 0139 int seconds = reloadTimeout / 1000; 0140 secs->setValue(seconds % 60); 0141 mins->setValue((seconds - secs->value()) / 60); 0142 } 0143 0144 if (dlg.exec() == KDialog::Accepted) { 0145 int msec = (mins->value() * 60 + secs->value()) * 1000; 0146 reloadTimeout = msec; 0147 configGroup().writeEntry("Reload", reloadTimeout); 0148 reload(); 0149 } 0150 } 0151 0152 void KonqSideBarWebModule::handleURL(const QUrl &) 0153 { 0154 } 0155 0156 void KonqSideBarWebModule::urlNewWindow(const QString &url, const KParts::OpenUrlArguments &args, const BrowserArguments &browserArgs, const KParts::WindowArgs &windowArgs) 0157 { 0158 emit createNewWindow(QUrl(url), args, browserArgs, windowArgs); 0159 } 0160 0161 void KonqSideBarWebModule::urlClicked(const QString &url, const KParts::OpenUrlArguments &args, const BrowserArguments &browserArgs) 0162 { 0163 emit openUrlRequest(QUrl(url), args, browserArgs); 0164 } 0165 0166 void KonqSideBarWebModule::formClicked(const QUrl &url, const KParts::OpenUrlArguments &args, const BrowserArguments &browserArgs) 0167 { 0168 _htmlPart->setArguments(args); 0169 _htmlPart->browserExtension()->setBrowserArguments(browserArgs); 0170 _htmlPart->openUrl(url); 0171 } 0172 0173 void KonqSideBarWebModule::loadFavicon() 0174 { 0175 const QString icon = KIO::favIconForUrl(_url); 0176 if (icon.isEmpty()) { 0177 KIO::FavIconRequestJob *job = new KIO::FavIconRequestJob(_url); 0178 connect(job, &KIO::FavIconRequestJob::result, this, [this, job](KJob *) { 0179 if (!job->error()) { 0180 loadFavicon(); 0181 } 0182 }); 0183 return; 0184 } 0185 0186 emit setIcon(icon); 0187 0188 if (icon != configGroup().readEntry("Icon", QString())) { 0189 configGroup().writeEntry("Icon", icon); 0190 } 0191 } 0192 0193 void KonqSideBarWebModule::reload() 0194 { 0195 _htmlPart->openUrl(_url); 0196 } 0197 0198 void KonqSideBarWebModule::setTitle(const QString &title) 0199 { 0200 qCDebug(SIDEBAR_LOG) << title; 0201 if (!title.isEmpty()) { 0202 emit setCaption(title); 0203 0204 if (title != configGroup().readEntry("Name", QString())) { 0205 configGroup().writeEntry("Name", title); 0206 } 0207 } 0208 } 0209 0210 void KonqSideBarWebModule::pageLoaded() 0211 { 0212 if (reloadTimeout > 0) { 0213 QTimer::singleShot(reloadTimeout, this, SLOT(reload())); 0214 } 0215 } 0216 0217 bool KHTMLSideBar::urlSelected(const QString &url, int button, 0218 int state, const QString &_target, 0219 const KParts::OpenUrlArguments &args, 0220 const BrowserArguments &browserArgs) 0221 { 0222 if (button == Qt::LeftButton) { 0223 if (_target.toLower() == "_self") { 0224 openUrl(completeURL(url)); 0225 } else if (_target.toLower() == "_blank") { 0226 emit openUrlNewWindow(completeURL(url).url(), args); 0227 } else { // isEmpty goes here too 0228 emit openUrlRequest(completeURL(url).url(), args); 0229 } 0230 return true; 0231 } 0232 if (button == Qt::MiddleButton) { 0233 emit openUrlNewWindow(completeURL(url).url(), 0234 args); 0235 return true; 0236 } 0237 // A refresh 0238 if (button == 0 && _target.toLower() == "_self") { 0239 openUrl(completeURL(url)); 0240 return true; 0241 } 0242 return KHTMLPart::urlSelected(url, button, state, _target, args, browserArgs); 0243 } 0244 0245 class KonqSidebarWebPlugin : public KonqSidebarPlugin 0246 { 0247 public: 0248 KonqSidebarWebPlugin(QObject *parent, const QVariantList &args) 0249 : KonqSidebarPlugin(parent, args) {} 0250 virtual ~KonqSidebarWebPlugin() {} 0251 0252 virtual KonqSidebarModule *createModule(QWidget *parent, 0253 const KConfigGroup &configGroup, 0254 const QString &desktopname, 0255 const QVariant &unused) 0256 { 0257 Q_UNUSED(unused); 0258 Q_UNUSED(desktopname); 0259 return new KonqSideBarWebModule(parent, configGroup); 0260 } 0261 0262 virtual QList<QAction *> addNewActions(QObject *parent, 0263 const QList<KConfigGroup> &existingModules, 0264 const QVariant &unused) 0265 { 0266 Q_UNUSED(unused); 0267 Q_UNUSED(existingModules); 0268 QAction *action = new QAction(parent); 0269 action->setText(i18nc("@action:inmenu Add", "Web Sidebar Module")); 0270 action->setIcon(QIcon::fromTheme("internet-web-browser")); 0271 return QList<QAction *>() << action; 0272 } 0273 0274 virtual QString templateNameForNewModule(const QVariant &actionData, 0275 const QVariant &unused) const 0276 { 0277 Q_UNUSED(actionData); 0278 Q_UNUSED(unused); 0279 return QString::fromLatin1("websidebarplugin%1.desktop"); 0280 } 0281 0282 virtual bool createNewModule(const QVariant &actionData, KConfigGroup &configGroup, 0283 QWidget *parentWidget, 0284 const QVariant &unused) 0285 { 0286 Q_UNUSED(actionData); 0287 Q_UNUSED(unused); 0288 0289 KNameAndUrlInputDialog dlg(i18nc("@label", "Name:"), i18nc("@label", "Path or URL:"), QUrl(), parentWidget); 0290 dlg.setWindowTitle(i18nc("@title:window", "Add web sidebar module")); 0291 if (!dlg.exec()) { 0292 return false; 0293 } 0294 0295 configGroup.writeEntry("Type", "Link"); 0296 configGroup.writeEntry("Icon", "internet-web-browser"); 0297 configGroup.writeEntry("Name", dlg.name()); 0298 configGroup.writeEntry("URL", dlg.url().url()); 0299 configGroup.writeEntry("X-KDE-KonqSidebarModule", "konqsidebar_web"); 0300 return true; 0301 } 0302 }; 0303 0304 K_PLUGIN_FACTORY(KonqSidebarWebPluginFactory, registerPlugin<KonqSidebarWebPlugin>();) 0305 0306 #include "web_module.moc" 0307