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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
35 #include "launcherwindow.h"
38 #include <QApplication>
39 #include <QAuthenticator>
40 #ifndef QT_NO_DESKTOPSERVICES
41 #include <QDesktopServices>
44 #include <QDialogButtonBox>
47 #ifndef QT_NO_LINEEDIT
50 #include <QProgressBar>
51 #include <QtNetwork/QNetworkReply>
52 #include <QtNetwork/QNetworkRequest>
53 #include <QtNetwork/QNetworkProxy>
55 WebPage::WebPage(QObject* parent)
58 , m_interruptingJavaScriptEnabled(false)
62 connect(networkAccessManager(), SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
63 this, SLOT(authenticationRequired(QNetworkReply*, QAuthenticator*)));
64 connect(this, SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), this, SLOT(requestPermission(QWebFrame*, QWebPage::Feature)));
65 connect(this, SIGNAL(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)), this, SLOT(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)));
68 void WebPage::applyProxy()
70 QUrl proxyUrl(qgetenv("http_proxy"));
72 if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
73 int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
74 networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort));
78 bool WebPage::supportsExtension(QWebPage::Extension extension) const
80 if (extension == QWebPage::ErrorPageExtension)
85 bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
87 const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
88 QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
90 errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>")
91 .arg(info->errorString).toUtf8();
96 bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
98 QObject* view = parent();
100 QVariant value = view->property("keyboardModifiers");
102 if (!value.isNull()) {
103 Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(value.toInt());
105 if (modifiers & Qt::ShiftModifier) {
106 QWebPage* page = createWindow(QWebPage::WebBrowserWindow);
107 page->mainFrame()->load(request);
111 if (modifiers & Qt::AltModifier) {
112 openUrlInDefaultBrowser(request.url());
117 return QWebPage::acceptNavigationRequest(frame, request, type);
120 void WebPage::openUrlInDefaultBrowser(const QUrl& url)
122 #ifndef QT_NO_DESKTOPSERVICES
123 if (QAction* action = qobject_cast<QAction*>(sender()))
124 QDesktopServices::openUrl(action->data().toUrl());
126 QDesktopServices::openUrl(url);
130 QString WebPage::userAgentForUrl(const QUrl& url) const
132 if (!m_userAgent.isEmpty())
134 return QWebPage::userAgentForUrl(url);
137 bool WebPage::shouldInterruptJavaScript()
139 if (!m_interruptingJavaScriptEnabled)
141 return QWebPage::shouldInterruptJavaScript();
144 void WebPage::authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
146 QDialog* dialog = new QDialog(QApplication::activeWindow());
147 dialog->setWindowTitle("HTTP Authentication");
149 QGridLayout* layout = new QGridLayout(dialog);
150 dialog->setLayout(layout);
152 QLabel* messageLabel = new QLabel(dialog);
153 messageLabel->setWordWrap(true);
154 QString messageStr = QString("Enter with username and password for: %1");
155 messageLabel->setText(messageStr.arg(reply->url().toString()));
156 layout->addWidget(messageLabel, 0, 1);
158 #ifndef QT_NO_LINEEDIT
159 QLabel* userLabel = new QLabel("Username:", dialog);
160 layout->addWidget(userLabel, 1, 0);
161 QLineEdit* userInput = new QLineEdit(dialog);
162 layout->addWidget(userInput, 1, 1);
164 QLabel* passLabel = new QLabel("Password:", dialog);
165 layout->addWidget(passLabel, 2, 0);
166 QLineEdit* passInput = new QLineEdit(dialog);
167 passInput->setEchoMode(QLineEdit::Password);
168 layout->addWidget(passInput, 2, 1);
171 QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
172 | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
173 connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
174 connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
175 layout->addWidget(buttonBox, 3, 1);
177 if (dialog->exec() == QDialog::Accepted) {
178 #ifndef QT_NO_LINEEDIT
179 authenticator->setUser(userInput->text());
180 authenticator->setPassword(passInput->text());
187 void WebPage::requestPermission(QWebFrame* frame, QWebPage::Feature feature)
189 setFeaturePermission(frame, feature, PermissionGrantedByUser);
192 void WebPage::featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)
196 QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)
198 LauncherWindow* mw = new LauncherWindow;
199 if (type == WebModalDialog)
200 mw->setWindowModality(Qt::ApplicationModal);
205 QObject* WebPage::createPlugin(const QString &classId, const QUrl&, const QStringList&, const QStringList&)
207 if (classId == "alien_QLabel") {
208 QLabel* l = new QLabel;
213 if (classId == QLatin1String("QProgressBar"))
214 return new QProgressBar(view());
215 if (classId == QLatin1String("QLabel"))
216 return new QLabel(view());