1 /* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #include "tabbedwebview.h"
19 #include "browserwindow.h"
20 #include "webpage.h"
21 #include "tabwidget.h"
22 #include "mainapplication.h"
23 #include "tabbar.h"
24 #include "webtab.h"
25 #include "statusbar.h"
26 #include "progressbar.h"
27 #include "navigationbar.h"
28 #include "iconprovider.h"
29 #include "searchenginesmanager.h"
30 #include "enhancedmenu.h"
31 #include "locationbar.h"
32 #include "webhittestresult.h"
33 #include "webinspector.h"
34 
35 #include <QHostInfo>
36 #include <QContextMenuEvent>
37 
TabbedWebView(WebTab * webTab)38 TabbedWebView::TabbedWebView(WebTab* webTab)
39     : WebView(webTab)
40     , m_window(0)
41     , m_webTab(webTab)
42     , m_menu(new Menu(this))
43 {
44     m_menu->setCloseOnMiddleClick(true);
45 
46     connect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted()));
47     connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));
48     connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished()));
49 }
50 
setPage(WebPage * page)51 void TabbedWebView::setPage(WebPage* page)
52 {
53     WebView::setPage(page);
54 
55     connect(page, &WebPage::linkHovered, this, &TabbedWebView::linkHovered);
56 }
57 
browserWindow() const58 BrowserWindow* TabbedWebView::browserWindow() const
59 {
60     return m_window;
61 }
62 
setBrowserWindow(BrowserWindow * window)63 void TabbedWebView::setBrowserWindow(BrowserWindow* window)
64 {
65     m_window = window;
66 }
67 
inspectElement()68 void TabbedWebView::inspectElement()
69 {
70     if (m_webTab->haveInspector())
71         triggerPageAction(QWebEnginePage::InspectElement);
72     else
73         m_webTab->showWebInspector(true);
74 }
75 
webTab() const76 WebTab* TabbedWebView::webTab() const
77 {
78     return m_webTab;
79 }
80 
getIp() const81 QString TabbedWebView::getIp() const
82 {
83     return m_currentIp;
84 }
85 
slotLoadProgress(int prog)86 void TabbedWebView::slotLoadProgress(int prog)
87 {
88     Q_UNUSED(prog)
89 
90     if (m_webTab->isCurrentTab() && m_window) {
91         m_window->updateLoadingActions();
92     }
93 }
94 
userLoadAction(const LoadRequest & req)95 void TabbedWebView::userLoadAction(const LoadRequest &req)
96 {
97     load(req);
98 }
99 
slotLoadStarted()100 void TabbedWebView::slotLoadStarted()
101 {
102     m_currentIp.clear();
103 }
104 
slotLoadFinished()105 void TabbedWebView::slotLoadFinished()
106 {
107     QHostInfo::lookupHost(url().host(), this, SLOT(setIp(QHostInfo)));
108 
109     if (m_webTab->isCurrentTab() && m_window) {
110         m_window->updateLoadingActions();
111     }
112 }
113 
setIp(const QHostInfo & info)114 void TabbedWebView::setIp(const QHostInfo &info)
115 {
116     if (info.addresses().isEmpty()) {
117         return;
118     }
119 
120     m_currentIp = QString("%1 (%2)").arg(info.hostName(), info.addresses().at(0).toString());
121 
122     if (m_webTab->isCurrentTab()) {
123         emit ipChanged(m_currentIp);
124     }
125 }
126 
linkHovered(const QString & link)127 void TabbedWebView::linkHovered(const QString &link)
128 {
129     if (m_webTab->isCurrentTab() && m_window) {
130         if (link.isEmpty()) {
131             m_window->statusBar()->clearMessage();
132         }
133         else {
134             m_window->statusBar()->showMessage(link);
135         }
136     }
137 }
138 
tabIndex() const139 int TabbedWebView::tabIndex() const
140 {
141     return m_webTab->tabIndex();
142 }
143 
overlayWidget()144 QWidget* TabbedWebView::overlayWidget()
145 {
146     return m_webTab;
147 }
148 
closeView()149 void TabbedWebView::closeView()
150 {
151     emit wantsCloseTab(tabIndex());
152 }
153 
loadInNewTab(const LoadRequest & req,Qz::NewTabPositionFlags position)154 void TabbedWebView::loadInNewTab(const LoadRequest &req, Qz::NewTabPositionFlags position)
155 {
156     if (m_window) {
157         int index = m_window->tabWidget()->addView(QUrl(), position);
158         TabbedWebView *view = m_window->weView(index);
159         webTab()->addChildTab(view->webTab());
160         view->webTab()->locationBar()->showUrl(req.url());
161         view->load(req);
162     }
163 }
164 
isFullScreen()165 bool TabbedWebView::isFullScreen()
166 {
167     return m_window && m_window->isFullScreen();
168 }
169 
requestFullScreen(bool enable)170 void TabbedWebView::requestFullScreen(bool enable)
171 {
172     if (!m_window)
173         return;
174 
175     m_window->requestHtmlFullScreen(this, enable);
176 }
177 
setAsCurrentTab()178 void TabbedWebView::setAsCurrentTab()
179 {
180     if (m_window) {
181         m_window->tabWidget()->setCurrentWidget(m_webTab);
182     }
183 }
184 
_contextMenuEvent(QContextMenuEvent * event)185 void TabbedWebView::_contextMenuEvent(QContextMenuEvent *event)
186 {
187     m_menu->clear();
188 
189     WebHitTestResult hitTest = page()->hitTestContent(event->pos());
190     createContextMenu(m_menu, hitTest);
191 
192     if (WebInspector::isEnabled()) {
193         m_menu->addSeparator();
194         m_menu->addAction(tr("Inspect Element"), this, &TabbedWebView::inspectElement);
195     }
196 
197     if (!m_menu->isEmpty()) {
198         // Prevent choosing first option with double rightclick
199         const QPoint pos = event->globalPos();
200         QPoint p(pos.x(), pos.y() + 1);
201 
202         m_menu->popup(p);
203         return;
204     }
205 
206     WebView::_contextMenuEvent(event);
207 }
208 
_mouseMoveEvent(QMouseEvent * event)209 void TabbedWebView::_mouseMoveEvent(QMouseEvent *event)
210 {
211     if (m_window && m_window->isFullScreen()) {
212         if (m_window->fullScreenNavigationVisible()) {
213             m_window->hideNavigationWithFullScreen();
214         }
215         else if (event->y() < 5) {
216             m_window->showNavigationWithFullScreen();
217         }
218     }
219 
220     WebView::_mouseMoveEvent(event);
221 }
222