File indexing completed on 2024-05-12 04:58:29

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "webinspector.h"
0019 #include "mainapplication.h"
0020 #include "networkmanager.h"
0021 #include "settings.h"
0022 #include "webview.h"
0023 #include "webpage.h"
0024 
0025 #include <QJsonArray>
0026 #include <QJsonObject>
0027 #include <QJsonDocument>
0028 #include <QNetworkReply>
0029 #include <QWebEngineSettings>
0030 #include <QtWebEngineWidgetsVersion>
0031 
0032 QList<QWebEngineView*> WebInspector::s_views;
0033 
0034 WebInspector::WebInspector(QWidget *parent)
0035     : QWebEngineView(parent)
0036     , m_view(nullptr)
0037 {
0038     setAttribute(Qt::WA_DeleteOnClose);
0039     setObjectName(QSL("web-inspector"));
0040     setMinimumHeight(80);
0041 
0042     m_height = Settings().value(QSL("Web-Inspector/height"), 80).toInt();
0043     m_windowSize = Settings().value(QSL("Web-Inspector/windowSize"), QSize(640, 480)).toSize();
0044 
0045     registerView(this);
0046 
0047     connect(page(), &QWebEnginePage::windowCloseRequested, this, &WebInspector::deleteLater);
0048     connect(page(), &QWebEnginePage::loadFinished, this, &WebInspector::loadFinished);
0049 }
0050 
0051 WebInspector::~WebInspector()
0052 {
0053     if (m_view && hasFocus()) {
0054         m_view->setFocus();
0055     }
0056 
0057     unregisterView(this);
0058 
0059     if (isWindow()) {
0060         Settings().setValue(QSL("Web-Inspector/windowSize"), size());
0061     } else {
0062         Settings().setValue(QSL("Web-Inspector/height"), height());
0063     }
0064 }
0065 
0066 void WebInspector::setView(WebView *view)
0067 {
0068     m_view = view;
0069     Q_ASSERT(isEnabled());
0070 
0071     page()->setInspectedPage(m_view->page());
0072     connect(m_view, &WebView::pageChanged, this, &WebInspector::deleteLater);
0073 }
0074 
0075 void WebInspector::inspectElement()
0076 {
0077     m_inspectElement = true;
0078 }
0079 
0080 bool WebInspector::isEnabled()
0081 {
0082     if (!mApp->webSettings()->testAttribute(QWebEngineSettings::JavascriptEnabled)) {
0083         return false;
0084     }
0085     return true;
0086 }
0087 
0088 void WebInspector::pushView(QWebEngineView *view)
0089 {
0090     s_views.removeOne(view);
0091     s_views.prepend(view);
0092 }
0093 
0094 void WebInspector::registerView(QWebEngineView *view)
0095 {
0096     s_views.prepend(view);
0097 }
0098 
0099 void WebInspector::unregisterView(QWebEngineView *view)
0100 {
0101     s_views.removeOne(view);
0102 }
0103 
0104 void WebInspector::loadFinished()
0105 {
0106     // Show close button only when docked
0107     if (!isWindow()) {
0108         page()->runJavaScript(QL1S("var button = Components.dockController._closeButton;"
0109                                    "button.setVisible(true);"
0110                                    "button.element.onmouseup = function() {"
0111                                    "    window.close();"
0112                                    "};"));
0113     }
0114 
0115     // Inspect element
0116     if (m_inspectElement) {
0117         m_view->triggerPageAction(QWebEnginePage::InspectElement);
0118         m_inspectElement = false;
0119     }
0120 }
0121 
0122 QSize WebInspector::sizeHint() const
0123 {
0124     if (isWindow()) {
0125         return m_windowSize;
0126     }
0127     QSize s = QWebEngineView::sizeHint();
0128     s.setHeight(m_height);
0129     return s;
0130 }
0131 
0132 void WebInspector::keyPressEvent(QKeyEvent *event)
0133 {
0134     Q_UNUSED(event)
0135     // Stop propagation
0136 }
0137 
0138 void WebInspector::keyReleaseEvent(QKeyEvent *event)
0139 {
0140     Q_UNUSED(event)
0141     // Stop propagation
0142 }