diff options
Diffstat (limited to 'src/webpage.cpp')
-rw-r--r-- | src/webpage.cpp | 141 |
1 files changed, 79 insertions, 62 deletions
diff --git a/src/webpage.cpp b/src/webpage.cpp index 160294e2..c81d93da 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -48,7 +48,7 @@ #include <KStandardDirs> #include <KUrl> #include <KDebug> - +#include <KToolInvocation> #include <KDE/KParts/BrowserRun> #include <KDE/KMimeTypeTrader> @@ -72,6 +72,7 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) , m_keyboardModifiers(Qt::NoModifier) , m_pressedButtons(Qt::NoButton) + , m_requestedUrl() { setForwardUnsupportedContent(true); @@ -83,8 +84,21 @@ WebPage::WebPage(QObject *parent) } +WebPage::~WebPage() +{ +} + + bool WebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type) { + m_requestedUrl = request.url(); + + if (m_requestedUrl.scheme() == QLatin1String("mailto")) + { + KToolInvocation::invokeMailer(m_requestedUrl); + return false; + } + if (m_keyboardModifiers & Qt::ControlModifier || m_pressedButtons == Qt::MidButton) { Application::instance()->loadUrl(request.url(), Rekonq::SettingOpenTab); @@ -118,52 +132,77 @@ WebPage *WebPage::newWindow(WebWindowType type) } -// FIXME: dear slot, you need to handle unsupported content a bit better.. void WebPage::slotHandleUnsupportedContent(QNetworkReply *reply) { - - const KUrl url(reply->request().url()); - kDebug() << "title:" << url; - kDebug() << "error:" << reply->errorString(); - - QString filename = url.fileName(); - QString mimetype = reply->header(QNetworkRequest::ContentTypeHeader).toString(); - KService::Ptr offer = KMimeTypeTrader::self()->preferredService(mimetype); - - KParts::BrowserRun::AskSaveResult res = KParts::BrowserRun::askSave( - url, - offer, - mimetype, - filename - ); - switch (res) + if (reply->error() == QNetworkReply::NoError) { - case KParts::BrowserRun::Save: - slotDownloadRequested(reply->request()); - return; - case KParts::BrowserRun::Cancel: - return; - default: // non extant case - break; - } + const KUrl url(reply->request().url()); + QString filename = url.fileName(); + QString mimetype = reply->header(QNetworkRequest::ContentTypeHeader).toString(); + KService::Ptr offer = KMimeTypeTrader::self()->preferredService(mimetype); - KUrl::List list; - list.append(url); - KRun::run(*offer,url,0); + if( offer.isNull() ) // no service can handle this. We can just download it.. + { + slotDownloadRequested(reply->request()); + } + else + { + switch ( KParts::BrowserRun::askSave( url, offer, mimetype, filename ) ) + { + case KParts::BrowserRun::Save: + slotDownloadRequested(reply->request()); + return; + case KParts::BrowserRun::Cancel: + return; + default: // non extant case + break; + } + // case KParts::BrowserRun::Open + KUrl::List list; + list.append(url); + KRun::run(*offer,url,0); + } + } return; } void WebPage::manageNetworkErrors(QNetworkReply* reply) { - if(reply->error() == QNetworkReply::NoError) + if( reply->error() == QNetworkReply::NoError ) return; - - viewErrorPage(reply); + + if( reply->url() != m_requestedUrl ) // prevent favicon loading + return; + + if( reply->error() == QNetworkReply::ContentNotFoundError ) + { + QList<QWebFrame*> frames; + frames.append(mainFrame()); + while (!frames.isEmpty()) + { + QWebFrame *firstFrame = frames.takeFirst(); + + if (firstFrame->url() == reply->url()) + { + firstFrame->setHtml(errorPage(reply), reply->url()); + return; + } + QList<QWebFrame *> children = firstFrame->childFrames(); + foreach(QWebFrame *frame, children) + { + frames.append(frame); + } + } + } + else + { + mainFrame()->setHtml(errorPage(reply), reply->url()); + } } -void WebPage::viewErrorPage(QNetworkReply *reply) +QString WebPage::errorPage(QNetworkReply *reply) { // display "not found" page QString notfoundFilePath = KStandardDirs::locate("data", "rekonq/htmls/notfound.html"); @@ -172,35 +211,18 @@ void WebPage::viewErrorPage(QNetworkReply *reply) if (!isOpened) { kWarning() << "Couldn't open the notfound.html file"; - return; + return QString(""); } QString title = i18n("Error loading page: ") + reply->url().toString(); QString imagePath = KIconLoader::global()->iconPath("rekonq", KIconLoader::NoGroup, false); QString html = QString(QLatin1String(file.readAll())) - .arg(title) - .arg("file://" + imagePath) - .arg(reply->errorString()) - .arg(reply->url().toString()); - - // test - QList<QWebFrame*> frames; - frames.append(mainFrame()); - while (!frames.isEmpty()) - { - QWebFrame *firstFrame = frames.takeFirst(); - if (firstFrame->url() == reply->url()) - { - firstFrame->setHtml(html, reply->url()); - return; - } - QList<QWebFrame *> children = firstFrame->childFrames(); - foreach(QWebFrame *frame, children) - { - frames.append(frame); - } - } + .arg(title) + .arg("file://" + imagePath) + .arg(reply->errorString()) + .arg(reply->url().toString()); + return html; } @@ -237,6 +259,7 @@ QObject *WebPage::createPlugin(const QString &classId, const QUrl &url, const QS } +// TODO FIXME: sometimes url.fileName() fails to retrieve url file name void WebPage::slotDownloadRequested(const QNetworkRequest &request) { const KUrl url(request.url()); @@ -250,9 +273,3 @@ void WebPage::slotDownloadRequested(const QNetworkRequest &request) job->addMetaData("cache", "cache"); // Use entry from cache if available. job->uiDelegate()->setAutoErrorHandlingEnabled(true); } - - -QString WebPage::chooseFile(QWebFrame *frame, const QString &suggestedFile) -{ - return KFileDialog::getOpenFileName(suggestedFile, QString(), frame->page()->view()); -} |