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

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 "tabbedwebview.h"
0019 #include "browserwindow.h"
0020 #include "webpage.h"
0021 #include "tabwidget.h"
0022 #include "mainapplication.h"
0023 #include "tabbar.h"
0024 #include "webtab.h"
0025 #include "statusbar.h"
0026 #include "progressbar.h"
0027 #include "navigationbar.h"
0028 #include "iconprovider.h"
0029 #include "searchenginesmanager.h"
0030 #include "enhancedmenu.h"
0031 #include "locationbar.h"
0032 #include "webhittestresult.h"
0033 #include "webinspector.h"
0034 
0035 #include <QHostInfo>
0036 #include <QContextMenuEvent>
0037 
0038 TabbedWebView::TabbedWebView(WebTab* webTab)
0039     : WebView(webTab)
0040     , m_window(nullptr)
0041     , m_webTab(webTab)
0042     , m_menu(new Menu(this))
0043 {
0044     m_menu->setCloseOnMiddleClick(true);
0045 
0046     connect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted()));
0047     connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));
0048     connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished()));
0049 }
0050 
0051 void TabbedWebView::setPage(WebPage* page)
0052 {
0053     WebView::setPage(page);
0054 
0055     connect(page, &WebPage::linkHovered, this, &TabbedWebView::linkHovered);
0056 }
0057 
0058 BrowserWindow* TabbedWebView::browserWindow() const
0059 {
0060     return m_window;
0061 }
0062 
0063 void TabbedWebView::setBrowserWindow(BrowserWindow* window)
0064 {
0065     m_window = window;
0066 }
0067 
0068 void TabbedWebView::inspectElement()
0069 {
0070     if (m_webTab->haveInspector())
0071         triggerPageAction(QWebEnginePage::InspectElement);
0072     else
0073         m_webTab->showWebInspector(true);
0074 }
0075 
0076 WebTab* TabbedWebView::webTab() const
0077 {
0078     return m_webTab;
0079 }
0080 
0081 QString TabbedWebView::getIp() const
0082 {
0083     return m_currentIp;
0084 }
0085 
0086 void TabbedWebView::slotLoadProgress(int prog)
0087 {
0088     Q_UNUSED(prog)
0089 
0090     if (m_webTab->isCurrentTab() && m_window) {
0091         m_window->updateLoadingActions();
0092     }
0093 }
0094 
0095 void TabbedWebView::userLoadAction(const LoadRequest &req)
0096 {
0097     load(req);
0098 }
0099 
0100 void TabbedWebView::slotLoadStarted()
0101 {
0102     m_currentIp.clear();
0103 }
0104 
0105 void TabbedWebView::slotLoadFinished()
0106 {
0107     QHostInfo::lookupHost(url().host(), this, SLOT(setIp(QHostInfo)));
0108 
0109     if (m_webTab->isCurrentTab() && m_window) {
0110         m_window->updateLoadingActions();
0111     }
0112 }
0113 
0114 void TabbedWebView::setIp(const QHostInfo &info)
0115 {
0116     if (info.addresses().isEmpty()) {
0117         return;
0118     }
0119 
0120     m_currentIp = QStringLiteral("%1 (%2)").arg(info.hostName(), info.addresses().at(0).toString());
0121 
0122     if (m_webTab->isCurrentTab()) {
0123         Q_EMIT ipChanged(m_currentIp);
0124     }
0125 }
0126 
0127 void TabbedWebView::linkHovered(const QString &link)
0128 {
0129     if (m_webTab->isCurrentTab() && m_window) {
0130         if (link.isEmpty()) {
0131             m_window->statusBar()->clearMessage();
0132         }
0133         else {
0134             m_window->statusBar()->showMessage(link);
0135         }
0136     }
0137 }
0138 
0139 int TabbedWebView::tabIndex() const
0140 {
0141     return m_webTab->tabIndex();
0142 }
0143 
0144 QWidget* TabbedWebView::overlayWidget()
0145 {
0146     return m_webTab;
0147 }
0148 
0149 void TabbedWebView::closeView()
0150 {
0151     Q_EMIT wantsCloseTab(tabIndex());
0152 }
0153 
0154 void TabbedWebView::loadInNewTab(const LoadRequest &req, Qz::NewTabPositionFlags position)
0155 {
0156     if (m_window) {
0157         int index = m_window->tabWidget()->addView(QUrl(), position);
0158         TabbedWebView *view = m_window->weView(index);
0159         webTab()->addChildTab(view->webTab());
0160         view->webTab()->locationBar()->showUrl(req.url());
0161         view->load(req);
0162     }
0163 }
0164 
0165 bool TabbedWebView::isFullScreen()
0166 {
0167     return m_window && m_window->isFullScreen();
0168 }
0169 
0170 void TabbedWebView::requestFullScreen(bool enable)
0171 {
0172     if (!m_window)
0173         return;
0174 
0175     m_window->requestHtmlFullScreen(this, enable);
0176 }
0177 
0178 void TabbedWebView::setAsCurrentTab()
0179 {
0180     if (m_window) {
0181         m_window->tabWidget()->setCurrentWidget(m_webTab);
0182     }
0183 }
0184 
0185 void TabbedWebView::_contextMenuEvent(QContextMenuEvent *event)
0186 {
0187     m_menu->clear();
0188 
0189     WebHitTestResult hitTest = page()->hitTestContent(event->pos());
0190     createContextMenu(m_menu, hitTest);
0191 
0192     if (WebInspector::isEnabled()) {
0193         m_menu->addSeparator();
0194         m_menu->addAction(tr("Inspect Element"), this, &TabbedWebView::inspectElement);
0195     }
0196 
0197     if (!m_menu->isEmpty()) {
0198         // Prevent choosing first option with double rightclick
0199         const QPoint pos = event->globalPos();
0200         QPoint p(pos.x(), pos.y() + 1);
0201 
0202         m_menu->popup(p);
0203         return;
0204     }
0205 
0206     WebView::_contextMenuEvent(event);
0207 }
0208 
0209 void TabbedWebView::_mouseMoveEvent(QMouseEvent *event)
0210 {
0211     if (m_window && m_window->isFullScreen()) {
0212         if (m_window->fullScreenNavigationVisible()) {
0213             m_window->hideNavigationWithFullScreen();
0214         }
0215         else if (event->position().toPoint().y() < 5) {
0216             m_window->showNavigationWithFullScreen();
0217         }
0218     }
0219 
0220     WebView::_mouseMoveEvent(event);
0221 }