• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
WebPage(QObject * parent)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(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), this, SLOT(requestPermission(QWebFrame*, QWebPage::Feature)));
54     connect(this, SIGNAL(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)), this, SLOT(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)));
55 }
56 
applyProxy()57 void WebPage::applyProxy()
58 {
59     QUrl proxyUrl(qgetenv("http_proxy"));
60 
61     if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
62         int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
63         networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort));
64     }
65 }
66 
supportsExtension(QWebPage::Extension extension) const67 bool WebPage::supportsExtension(QWebPage::Extension extension) const
68 {
69     if (extension == QWebPage::ErrorPageExtension)
70         return true;
71     return false;
72 }
73 
extension(Extension extension,const ExtensionOption * option,ExtensionReturn * output)74 bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
75 {
76     const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
77     QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
78 
79     errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>")
80         .arg(info->errorString).toUtf8();
81 
82     return true;
83 }
84 
acceptNavigationRequest(QWebFrame * frame,const QNetworkRequest & request,NavigationType type)85 bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
86 {
87     QObject* view = parent();
88 
89     QVariant value = view->property("keyboardModifiers");
90 
91     if (!value.isNull()) {
92         Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(value.toInt());
93 
94         if (modifiers & Qt::ShiftModifier) {
95             QWebPage* page = createWindow(QWebPage::WebBrowserWindow);
96             page->mainFrame()->load(request);
97             return false;
98         }
99 
100         if (modifiers & Qt::AltModifier) {
101             openUrlInDefaultBrowser(request.url());
102             return false;
103         }
104     }
105 
106     return QWebPage::acceptNavigationRequest(frame, request, type);
107 }
108 
openUrlInDefaultBrowser(const QUrl & url)109 void WebPage::openUrlInDefaultBrowser(const QUrl& url)
110 {
111 #ifndef QT_NO_DESKTOPSERVICES
112     if (QAction* action = qobject_cast<QAction*>(sender()))
113         QDesktopServices::openUrl(action->data().toUrl());
114     else
115         QDesktopServices::openUrl(url);
116 #endif
117 }
118 
userAgentForUrl(const QUrl & url) const119 QString WebPage::userAgentForUrl(const QUrl& url) const
120 {
121     if (!m_userAgent.isEmpty())
122         return m_userAgent;
123     return QWebPage::userAgentForUrl(url);
124 }
125 
shouldInterruptJavaScript()126 bool WebPage::shouldInterruptJavaScript()
127 {
128     if (!m_interruptingJavaScriptEnabled)
129         return false;
130     return QWebPage::shouldInterruptJavaScript();
131 }
132 
authenticationRequired(QNetworkReply * reply,QAuthenticator * authenticator)133 void WebPage::authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
134 {
135     QDialog* dialog = new QDialog(QApplication::activeWindow());
136     dialog->setWindowTitle("HTTP Authentication");
137 
138     QGridLayout* layout = new QGridLayout(dialog);
139     dialog->setLayout(layout);
140 
141     QLabel* messageLabel = new QLabel(dialog);
142     messageLabel->setWordWrap(true);
143     QString messageStr = QString("Enter with username and password for: %1");
144     messageLabel->setText(messageStr.arg(reply->url().toString()));
145     layout->addWidget(messageLabel, 0, 1);
146 
147 #ifndef QT_NO_LINEEDIT
148     QLabel* userLabel = new QLabel("Username:", dialog);
149     layout->addWidget(userLabel, 1, 0);
150     QLineEdit* userInput = new QLineEdit(dialog);
151     layout->addWidget(userInput, 1, 1);
152 
153     QLabel* passLabel = new QLabel("Password:", dialog);
154     layout->addWidget(passLabel, 2, 0);
155     QLineEdit* passInput = new QLineEdit(dialog);
156     passInput->setEchoMode(QLineEdit::Password);
157     layout->addWidget(passInput, 2, 1);
158 #endif
159 
160     QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
161             | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
162     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
163     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
164     layout->addWidget(buttonBox, 3, 1);
165 
166     if (dialog->exec() == QDialog::Accepted) {
167 #ifndef QT_NO_LINEEDIT
168         authenticator->setUser(userInput->text());
169         authenticator->setPassword(passInput->text());
170 #endif
171     }
172 
173     delete dialog;
174 }
175 
requestPermission(QWebFrame * frame,QWebPage::Feature feature)176 void WebPage::requestPermission(QWebFrame* frame, QWebPage::Feature feature)
177 {
178     setFeaturePermission(frame, feature, PermissionGrantedByUser);
179 }
180 
featurePermissionRequestCanceled(QWebFrame *,QWebPage::Feature)181 void WebPage::featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)
182 {
183 }
184 
createWindow(QWebPage::WebWindowType type)185 QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)
186 {
187     LauncherWindow* mw = new LauncherWindow;
188     if (type == WebModalDialog)
189         mw->setWindowModality(Qt::ApplicationModal);
190     mw->show();
191     return mw->page();
192 }
193 
createPlugin(const QString & classId,const QUrl &,const QStringList &,const QStringList &)194 QObject* WebPage::createPlugin(const QString &classId, const QUrl&, const QStringList&, const QStringList&)
195 {
196     if (classId == "alien_QLabel") {
197         QLabel* l = new QLabel;
198         l->winId();
199         return l;
200     }
201 
202 #ifndef QT_NO_UITOOLS
203     QUiLoader loader;
204     return loader.createWidget(classId, view());
205 #else
206     Q_UNUSED(classId);
207     return 0;
208 #endif
209 }
210 
211