Fixed sometimes focus ring is hidden when context menu is shown by longTap
[framework/web/webkit-efl.git] / Tools / QtTestBrowser / webpage.cpp
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 <QAction>
38 #include <QApplication>
39 #include <QAuthenticator>
40 #ifndef QT_NO_DESKTOPSERVICES
41 #include <QDesktopServices>
42 #endif
43 #include <QDialog>
44 #include <QDialogButtonBox>
45 #include <QLabel>
46 #include <QLayout>
47 #ifndef QT_NO_LINEEDIT
48 #include <QLineEdit>
49 #endif
50 #include <QProgressBar>
51 #include <QtNetwork/QNetworkReply>
52 #include <QtNetwork/QNetworkRequest>
53 #include <QtNetwork/QNetworkProxy>
54
55 WebPage::WebPage(QObject* parent)
56     : QWebPage(parent)
57     , m_userAgent()
58     , m_interruptingJavaScriptEnabled(false)
59 {
60     applyProxy();
61
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)));
66 }
67
68 void WebPage::applyProxy()
69 {
70     QUrl proxyUrl(qgetenv("http_proxy"));
71
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));
75     }
76 }
77
78 bool WebPage::supportsExtension(QWebPage::Extension extension) const
79 {
80     if (extension == QWebPage::ErrorPageExtension)
81         return true;
82     return false;
83 }
84
85 bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
86 {
87     const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
88     QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
89
90     errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>")
91         .arg(info->errorString).toUtf8();
92
93     return true;
94 }
95
96 bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
97 {
98     QObject* view = parent();
99
100     QVariant value = view->property("keyboardModifiers");
101
102     if (!value.isNull()) {
103         Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(value.toInt());
104
105         if (modifiers & Qt::ShiftModifier) {
106             QWebPage* page = createWindow(QWebPage::WebBrowserWindow);
107             page->mainFrame()->load(request);
108             return false;
109         }
110
111         if (modifiers & Qt::AltModifier) {
112             openUrlInDefaultBrowser(request.url());
113             return false;
114         }
115     }
116
117     return QWebPage::acceptNavigationRequest(frame, request, type);
118 }
119
120 void WebPage::openUrlInDefaultBrowser(const QUrl& url)
121 {
122 #ifndef QT_NO_DESKTOPSERVICES
123     if (QAction* action = qobject_cast<QAction*>(sender()))
124         QDesktopServices::openUrl(action->data().toUrl());
125     else
126         QDesktopServices::openUrl(url);
127 #endif
128 }
129
130 QString WebPage::userAgentForUrl(const QUrl& url) const
131 {
132     if (!m_userAgent.isEmpty())
133         return m_userAgent;
134     return QWebPage::userAgentForUrl(url);
135 }
136
137 bool WebPage::shouldInterruptJavaScript()
138 {
139     if (!m_interruptingJavaScriptEnabled)
140         return false;
141     return QWebPage::shouldInterruptJavaScript();
142 }
143
144 void WebPage::authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
145 {
146     QDialog* dialog = new QDialog(QApplication::activeWindow());
147     dialog->setWindowTitle("HTTP Authentication");
148
149     QGridLayout* layout = new QGridLayout(dialog);
150     dialog->setLayout(layout);
151
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);
157
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);
163
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);
169 #endif
170
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);
176
177     if (dialog->exec() == QDialog::Accepted) {
178 #ifndef QT_NO_LINEEDIT
179         authenticator->setUser(userInput->text());
180         authenticator->setPassword(passInput->text());
181 #endif
182     }
183
184     delete dialog;
185 }
186
187 void WebPage::requestPermission(QWebFrame* frame, QWebPage::Feature feature)
188 {
189     setFeaturePermission(frame, feature, PermissionGrantedByUser);
190 }
191
192 void WebPage::featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)
193 {
194 }
195
196 QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)
197 {
198     LauncherWindow* mw = new LauncherWindow;
199     if (type == WebModalDialog)
200         mw->setWindowModality(Qt::ApplicationModal);
201     mw->show();
202     return mw->page();
203 }
204
205 QObject* WebPage::createPlugin(const QString &classId, const QUrl&, const QStringList&, const QStringList&)
206 {
207     if (classId == "alien_QLabel") {
208         QLabel* l = new QLabel;
209         l->winId();
210         return l;
211     }
212
213     if (classId == QLatin1String("QProgressBar"))
214         return new QProgressBar(view());
215     if (classId == QLatin1String("QLabel"))
216         return new QLabel(view());
217     return 0;
218 }
219