WebKitTools/QtTestBrowser/webpage.cpp
changeset 2 303757a437d3
parent 0 4f2f89ce4247
equal deleted inserted replaced
0:4f2f89ce4247 2:303757a437d3
     1 /*
       
     2  * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies)
       
     3  * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
       
     4  * Copyright (C) 2006 George Staikos <staikos@kde.org>
       
     5  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
       
     6  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
       
     7  * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
       
     8  *
       
     9  * All rights reserved.
       
    10  *
       
    11  * Redistribution and use in source and binary forms, with or without
       
    12  * modification, are permitted provided that the following conditions
       
    13  * are met:
       
    14  * 1. Redistributions of source code must retain the above copyright
       
    15  *    notice, this list of conditions and the following disclaimer.
       
    16  * 2. Redistributions in binary form must reproduce the above copyright
       
    17  *    notice, this list of conditions and the following disclaimer in the
       
    18  *    documentation and/or other materials provided with the distribution.
       
    19  *
       
    20  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
       
    21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
       
    24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    28  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    31  */
       
    32 
       
    33 #include "webpage.h"
       
    34 
       
    35 #include "launcherwindow.h"
       
    36 
       
    37 #include <QAuthenticator>
       
    38 #include <QDesktopServices>
       
    39 #include <QtGui>
       
    40 #include <QtNetwork/QNetworkReply>
       
    41 #include <QtNetwork/QNetworkRequest>
       
    42 #include <QtNetwork/QNetworkProxy>
       
    43 
       
    44 WebPage::WebPage(QObject* parent)
       
    45     : QWebPage(parent)
       
    46     , m_userAgent()
       
    47     , m_interruptingJavaScriptEnabled(false)
       
    48 {
       
    49     applyProxy();
       
    50 
       
    51     connect(networkAccessManager(), SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
       
    52             this, SLOT(authenticationRequired(QNetworkReply*, QAuthenticator*)));
       
    53     connect(this, SIGNAL(requestPermissionFromUser(QWebFrame*, QWebPage::PermissionDomain)), this, SLOT(requestPermission(QWebFrame*, QWebPage::PermissionDomain)));
       
    54     connect(this, SIGNAL(checkPermissionFromUser(QWebFrame*, QWebPage::PermissionDomain, QWebPage::PermissionPolicy&)), this, SLOT(checkPermission(QWebFrame*, QWebPage::PermissionDomain, QWebPage::PermissionPolicy&)));
       
    55     connect(this, SIGNAL(cancelRequestsForPermission(QWebFrame*, QWebPage::PermissionDomain)), this, SLOT(cancelRequestsForPermission(QWebFrame*, QWebPage::PermissionDomain)));
       
    56 }
       
    57 
       
    58 void WebPage::applyProxy()
       
    59 {
       
    60     QUrl proxyUrl(qgetenv("http_proxy"));
       
    61 
       
    62     if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
       
    63         int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
       
    64         networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort));
       
    65     }
       
    66 }
       
    67 
       
    68 bool WebPage::supportsExtension(QWebPage::Extension extension) const
       
    69 {
       
    70     if (extension == QWebPage::ErrorPageExtension)
       
    71         return true;
       
    72     return false;
       
    73 }
       
    74 
       
    75 bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
       
    76 {
       
    77     const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
       
    78     QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
       
    79 
       
    80     errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>")
       
    81         .arg(info->errorString).toUtf8();
       
    82 
       
    83     return true;
       
    84 }
       
    85 
       
    86 bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
       
    87 {
       
    88     QObject* view = parent();
       
    89 
       
    90     QVariant value = view->property("keyboardModifiers");
       
    91 
       
    92     if (!value.isNull()) {
       
    93         Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(value.toInt());
       
    94 
       
    95         if (modifiers & Qt::ShiftModifier) {
       
    96             QWebPage* page = createWindow(QWebPage::WebBrowserWindow);
       
    97             page->mainFrame()->load(request);
       
    98             return false;
       
    99         }
       
   100 
       
   101         if (modifiers & Qt::AltModifier) {
       
   102             openUrlInDefaultBrowser(request.url());
       
   103             return false;
       
   104         }
       
   105     }
       
   106 
       
   107     return QWebPage::acceptNavigationRequest(frame, request, type);
       
   108 }
       
   109 
       
   110 void WebPage::openUrlInDefaultBrowser(const QUrl& url)
       
   111 {
       
   112     if (QAction* action = qobject_cast<QAction*>(sender()))
       
   113         QDesktopServices::openUrl(action->data().toUrl());
       
   114     else
       
   115         QDesktopServices::openUrl(url);
       
   116 }
       
   117 
       
   118 QString WebPage::userAgentForUrl(const QUrl& url) const
       
   119 {
       
   120     if (!m_userAgent.isEmpty())
       
   121         return m_userAgent;
       
   122     return QWebPage::userAgentForUrl(url);
       
   123 }
       
   124 
       
   125 bool WebPage::shouldInterruptJavaScript()
       
   126 {
       
   127     if (!m_interruptingJavaScriptEnabled)
       
   128         return false;
       
   129     return QWebPage::shouldInterruptJavaScript();
       
   130 }
       
   131 
       
   132 void WebPage::authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
       
   133 {
       
   134     QDialog* dialog = new QDialog(QApplication::activeWindow());
       
   135     dialog->setWindowTitle("HTTP Authentication");
       
   136 
       
   137     QGridLayout* layout = new QGridLayout(dialog);
       
   138     dialog->setLayout(layout);
       
   139 
       
   140     QLabel* messageLabel = new QLabel(dialog);
       
   141     messageLabel->setWordWrap(true);
       
   142     QString messageStr = QString("Enter with username and password for: %1");
       
   143     messageLabel->setText(messageStr.arg(reply->url().toString()));
       
   144     layout->addWidget(messageLabel, 0, 1);
       
   145 
       
   146     QLabel* userLabel = new QLabel("Username:", dialog);
       
   147     layout->addWidget(userLabel, 1, 0);
       
   148     QLineEdit* userInput = new QLineEdit(dialog);
       
   149     layout->addWidget(userInput, 1, 1);
       
   150 
       
   151     QLabel* passLabel = new QLabel("Password:", dialog);
       
   152     layout->addWidget(passLabel, 2, 0);
       
   153     QLineEdit* passInput = new QLineEdit(dialog);
       
   154     passInput->setEchoMode(QLineEdit::Password);
       
   155     layout->addWidget(passInput, 2, 1);
       
   156 
       
   157     QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
       
   158             | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
       
   159     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
       
   160     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
       
   161     layout->addWidget(buttonBox, 3, 1);
       
   162 
       
   163     if (dialog->exec() == QDialog::Accepted) {
       
   164         authenticator->setUser(userInput->text());
       
   165         authenticator->setPassword(passInput->text());
       
   166     }
       
   167 
       
   168     delete dialog;
       
   169 }
       
   170 
       
   171 void WebPage::requestPermission(QWebFrame* frame, QWebPage::PermissionDomain domain)
       
   172 {
       
   173     setUserPermission(frame, domain, PermissionGranted);
       
   174 }
       
   175 
       
   176 void WebPage::checkPermission(QWebFrame* frame, QWebPage::PermissionDomain domain, QWebPage::PermissionPolicy& policy)
       
   177 {
       
   178     switch (domain) {
       
   179     case NotificationsPermissionDomain:
       
   180         policy = PermissionGranted;
       
   181         break;
       
   182     default:
       
   183         break;
       
   184     }
       
   185 }
       
   186 
       
   187 void WebPage::cancelRequestsForPermission(QWebFrame*, QWebPage::PermissionDomain)
       
   188 {
       
   189 }
       
   190 
       
   191 QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)
       
   192 {
       
   193     LauncherWindow* mw = new LauncherWindow;
       
   194     if (type == WebModalDialog)
       
   195         mw->setWindowModality(Qt::ApplicationModal);
       
   196     mw->show();
       
   197     return mw->page();
       
   198 }
       
   199 
       
   200 QObject* WebPage::createPlugin(const QString &classId, const QUrl&, const QStringList&, const QStringList&)
       
   201 {
       
   202     if (classId == "alien_QLabel") {
       
   203         QLabel* l = new QLabel;
       
   204         l->winId();
       
   205         return l;
       
   206     }
       
   207 
       
   208 #ifndef QT_NO_UITOOLS
       
   209     QUiLoader loader;
       
   210     return loader.createWidget(classId, view());
       
   211 #else
       
   212     Q_UNUSED(classId);
       
   213     return 0;
       
   214 #endif
       
   215 }
       
   216