From e4f47b368fe0c3380636ab8ab0cc5706518f8a2b Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Tue, 5 Jun 2012 11:38:19 +0200 Subject: Added an UrlResolver class to let every url work properly here --- kwebapp/webpage.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 12 deletions(-) (limited to 'kwebapp/webpage.cpp') diff --git a/kwebapp/webpage.cpp b/kwebapp/webpage.cpp index cdca10bd..afe23035 100644 --- a/kwebapp/webpage.cpp +++ b/kwebapp/webpage.cpp @@ -28,19 +28,32 @@ #include "webpage.h" #include "webpage.moc" +// Local Includes +#include "urlresolver.h" + // KDE Includes #include +#include +#include +#include // Qt Includes #include +#include +#include + +// Defines +#define QL1S(x) QLatin1String(x) WebPage::WebPage(QObject *parent) : KWebPage(parent) - , _selfLoading(true) { - connect(this, SIGNAL(loadFinished(bool)), this, SLOT(disableSelfLoading())); + // ----- handling unsupported content... + setForwardUnsupportedContent(true); + connect(this, SIGNAL(unsupportedContent(QNetworkReply*)), this, SLOT(handleUnsupportedContent(QNetworkReply*))); + // downloads connect(this, SIGNAL(unsupportedContent(QNetworkReply*)), this, SLOT(downloadResponse(QNetworkReply*))); connect(this, SIGNAL(downloadRequested(QNetworkRequest)), this, SLOT(downloadRequest(QNetworkRequest))); } @@ -48,21 +61,69 @@ WebPage::WebPage(QObject *parent) bool WebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type) { - return KWebPage::acceptNavigationRequest(frame, request, type); + QUrl reqUrl = request.url(); + QString protocol = reqUrl.scheme(); + + // javascript handling + if (protocol == QL1S("javascript")) + { + QString scriptSource = QUrl::fromPercentEncoding(reqUrl.toString().mid(11).toUtf8()); + mainFrame()->evaluateJavaScript(scriptSource); + return false; + } - // FIXME -// (void)new KRun(request.url(), view(), 0); -// return false; -} + // "mailto" handling: It needs to be handled both here (mail url launched) + // and in handleUnsupportedContent (mail links clicked) + if (protocol == QL1S("mailto")) + { + KToolInvocation::invokeMailer(reqUrl); + return false; + } + if (frame && UrlResolver::isKDEUrl(reqUrl.toString())) + { + QUrl newReqUrl = UrlResolver::urlFromTextTyped(reqUrl.toString()); + frame->load(newReqUrl); + return false; + } -void WebPage::setSelfLoadingEnabled(bool b) -{ - _selfLoading = b; + // don't let webkit try to load an unknown (or missing) protocol... + if (!KProtocolInfo::isKnownProtocol(protocol)) + { + kDebug() << "UNKNOWN PROTOCOL: " << protocol; + return false; + } + + return KWebPage::acceptNavigationRequest(frame, request, type); } -void WebPage::disableSelfLoading() +void WebPage::handleUnsupportedContent(QNetworkReply *reply) { - _selfLoading = false; + Q_ASSERT(reply); + + if (!reply) + { + kDebug() << "NO REPLY. Why????"; + return; + } + + QUrl replyUrl = reply->url(); + QString protocol = replyUrl.scheme(); + + // "http(s)" (fast) handling + if (protocol == QL1S("http") || protocol == QL1S("https")) + { + kDebug() << "Error: " << protocol; + return; + } + + // "mailto" handling. + if (protocol == QL1S("mailto")) + { + KToolInvocation::invokeMailer(replyUrl); + return; + } + + return; } -- cgit v1.2.1