xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/simplebrowser/webpage.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "browserwindow.h"
52 #include "tabwidget.h"
53 #include "ui_certificateerrordialog.h"
54 #include "ui_passworddialog.h"
55 #include "webpage.h"
56 #include "webview.h"
57 #include <QAuthenticator>
58 #include <QMessageBox>
59 #include <QWebEngineCertificateError>
60 #include <QStyle>
61 
WebPage(QWebEngineProfile * profile,QObject * parent)62 WebPage::WebPage(QWebEngineProfile *profile, QObject *parent)
63     : QWebEnginePage(profile, parent)
64 {
65     connect(this, &QWebEnginePage::authenticationRequired, this, &WebPage::handleAuthenticationRequired);
66     connect(this, &QWebEnginePage::proxyAuthenticationRequired, this, &WebPage::handleProxyAuthenticationRequired);
67 }
68 
certificateError(const QWebEngineCertificateError & error)69 bool WebPage::certificateError(const QWebEngineCertificateError &error)
70 {
71     QWidget *mainWindow = view()->window();
72     if (error.isOverridable()) {
73         QDialog dialog(mainWindow);
74         dialog.setModal(true);
75         dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
76         Ui::CertificateErrorDialog certificateDialog;
77         certificateDialog.setupUi(&dialog);
78         certificateDialog.m_iconLabel->setText(QString());
79         QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, mainWindow));
80         certificateDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
81         certificateDialog.m_errorLabel->setText(error.errorDescription());
82         dialog.setWindowTitle(tr("Certificate Error"));
83         return dialog.exec() == QDialog::Accepted;
84     }
85 
86     QMessageBox::critical(mainWindow, tr("Certificate Error"), error.errorDescription());
87     return false;
88 }
89 
handleAuthenticationRequired(const QUrl & requestUrl,QAuthenticator * auth)90 void WebPage::handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth)
91 {
92     QWidget *mainWindow = view()->window();
93     QDialog dialog(mainWindow);
94     dialog.setModal(true);
95     dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
96 
97     Ui::PasswordDialog passwordDialog;
98     passwordDialog.setupUi(&dialog);
99 
100     passwordDialog.m_iconLabel->setText(QString());
101     QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow));
102     passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
103 
104     QString introMessage(tr("Enter username and password for \"%1\" at %2")
105                          .arg(auth->realm()).arg(requestUrl.toString().toHtmlEscaped()));
106     passwordDialog.m_infoLabel->setText(introMessage);
107     passwordDialog.m_infoLabel->setWordWrap(true);
108 
109     if (dialog.exec() == QDialog::Accepted) {
110         auth->setUser(passwordDialog.m_userNameLineEdit->text());
111         auth->setPassword(passwordDialog.m_passwordLineEdit->text());
112     } else {
113         // Set authenticator null if dialog is cancelled
114         *auth = QAuthenticator();
115     }
116 }
117 
handleProxyAuthenticationRequired(const QUrl &,QAuthenticator * auth,const QString & proxyHost)118 void WebPage::handleProxyAuthenticationRequired(const QUrl &, QAuthenticator *auth, const QString &proxyHost)
119 {
120     QWidget *mainWindow = view()->window();
121     QDialog dialog(mainWindow);
122     dialog.setModal(true);
123     dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
124 
125     Ui::PasswordDialog passwordDialog;
126     passwordDialog.setupUi(&dialog);
127 
128     passwordDialog.m_iconLabel->setText(QString());
129     QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow));
130     passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32));
131 
132     QString introMessage = tr("Connect to proxy \"%1\" using:");
133     introMessage = introMessage.arg(proxyHost.toHtmlEscaped());
134     passwordDialog.m_infoLabel->setText(introMessage);
135     passwordDialog.m_infoLabel->setWordWrap(true);
136 
137     if (dialog.exec() == QDialog::Accepted) {
138         auth->setUser(passwordDialog.m_userNameLineEdit->text());
139         auth->setPassword(passwordDialog.m_passwordLineEdit->text());
140     } else {
141         // Set authenticator null if dialog is cancelled
142         *auth = QAuthenticator();
143     }
144 }
145