File indexing completed on 2024-12-22 04:41:14
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@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 "qmltab.h" 0019 #include "loadrequest.h" 0020 #include "tabbedwebview.h" 0021 #include "webpage.h" 0022 #include "qml/qmlstaticdata.h" 0023 #include <QWebEngineHistory> 0024 #include <QQmlEngine> 0025 0026 QmlTab::QmlTab(WebTab *webTab, QObject *parent) 0027 : QObject(parent) 0028 , m_webTab(webTab) 0029 { 0030 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); 0031 0032 if (!m_webTab) { 0033 return; 0034 } 0035 0036 createConnections(); 0037 } 0038 0039 void QmlTab::detach() 0040 { 0041 if (!m_webTab) { 0042 return; 0043 } 0044 0045 m_webTab->detach(); 0046 } 0047 0048 void QmlTab::setZoomLevel(int zoomLevel) 0049 { 0050 if (!m_webTab) { 0051 return; 0052 } 0053 0054 m_webTab->setZoomLevel(zoomLevel); 0055 } 0056 0057 void QmlTab::stop() 0058 { 0059 if (!m_webTab) { 0060 return; 0061 } 0062 0063 m_webTab->stop(); 0064 } 0065 0066 void QmlTab::reload() 0067 { 0068 if (!m_webTab) { 0069 return; 0070 } 0071 0072 m_webTab->reload(); 0073 } 0074 0075 void QmlTab::unload() 0076 { 0077 if (!m_webTab) { 0078 return; 0079 } 0080 0081 m_webTab->unload(); 0082 } 0083 0084 void QmlTab::load(const QString &url) 0085 { 0086 if (!m_webTab) { 0087 return; 0088 } 0089 0090 LoadRequest req; 0091 req.setUrl(QUrl::fromEncoded(url.toUtf8())); 0092 m_webTab->load(req); 0093 } 0094 0095 void QmlTab::zoomIn() 0096 { 0097 if (!m_webTab) { 0098 return; 0099 } 0100 0101 m_webTab->webView()->zoomIn(); 0102 } 0103 0104 void QmlTab::zoomOut() 0105 { 0106 if (!m_webTab) { 0107 return; 0108 } 0109 0110 m_webTab->webView()->zoomOut(); 0111 } 0112 0113 void QmlTab::zoomReset() 0114 { 0115 if (!m_webTab) { 0116 return; 0117 } 0118 0119 m_webTab->webView()->zoomReset(); 0120 } 0121 0122 void QmlTab::undo() 0123 { 0124 if (!m_webTab) { 0125 return; 0126 } 0127 0128 m_webTab->webView()->editUndo(); 0129 } 0130 0131 void QmlTab::redo() 0132 { 0133 if (!m_webTab) { 0134 return; 0135 } 0136 0137 m_webTab->webView()->editRedo(); 0138 } 0139 0140 void QmlTab::selectAll() 0141 { 0142 if (!m_webTab) { 0143 return; 0144 } 0145 0146 m_webTab->webView()->editSelectAll(); 0147 } 0148 0149 void QmlTab::reloadBypassCache() 0150 { 0151 if (!m_webTab) { 0152 return; 0153 } 0154 0155 m_webTab->webView()->reloadBypassCache(); 0156 } 0157 0158 void QmlTab::back() 0159 { 0160 if (!m_webTab) { 0161 return; 0162 } 0163 0164 m_webTab->webView()->back(); 0165 } 0166 0167 void QmlTab::forward() 0168 { 0169 if (!m_webTab) { 0170 return; 0171 } 0172 0173 m_webTab->webView()->forward(); 0174 } 0175 0176 void QmlTab::printPage() 0177 { 0178 if (!m_webTab) { 0179 return; 0180 } 0181 0182 m_webTab->webView()->printPage(); 0183 } 0184 0185 void QmlTab::showSource() 0186 { 0187 if (!m_webTab) { 0188 return; 0189 } 0190 0191 m_webTab->webView()->showSource(); 0192 } 0193 0194 void QmlTab::sendPageByMail() 0195 { 0196 if (!m_webTab) { 0197 return; 0198 } 0199 0200 m_webTab->webView()->sendPageByMail(); 0201 } 0202 0203 QVariant QmlTab::execJavaScript(const QJSValue &value) 0204 { 0205 if (!m_webPage && !m_webTab) { 0206 return {}; 0207 } 0208 WebPage *webPage = m_webPage; 0209 if (!m_webPage) { 0210 webPage = m_webTab->webView()->page(); 0211 } 0212 return webPage->execJavaScript(value.toString()); 0213 } 0214 0215 QmlWebHitTestResult *QmlTab::hitTestContent(const QPoint &point) 0216 { 0217 if (!m_webPage && !m_webTab) { 0218 return nullptr; 0219 } 0220 WebPage *webPage = m_webPage; 0221 if (!m_webPage) { 0222 webPage = m_webTab->webView()->page(); 0223 } 0224 const WebHitTestResult result = webPage->hitTestContent(point); 0225 return new QmlWebHitTestResult(result); 0226 } 0227 0228 QString QmlTab::url() const 0229 { 0230 if (!m_webTab) { 0231 return {}; 0232 } 0233 0234 return QString::fromUtf8(m_webTab->url().toEncoded()); 0235 } 0236 0237 QString QmlTab::title() const 0238 { 0239 if (!m_webTab) { 0240 return {}; 0241 } 0242 0243 return m_webTab->title(); 0244 } 0245 0246 0247 int QmlTab::zoomLevel() const 0248 { 0249 if (!m_webTab) { 0250 return -1; 0251 } 0252 0253 return m_webTab->zoomLevel(); 0254 } 0255 0256 int QmlTab::index() const 0257 { 0258 if (!m_webTab) { 0259 return -1; 0260 } 0261 0262 return m_webTab->tabIndex(); 0263 } 0264 0265 bool QmlTab::pinned() const 0266 { 0267 if (!m_webTab) { 0268 return false; 0269 } 0270 0271 return m_webTab->isPinned(); 0272 } 0273 0274 bool QmlTab::muted() const 0275 { 0276 if (!m_webTab) { 0277 return false; 0278 } 0279 0280 return m_webTab->isMuted(); 0281 } 0282 0283 bool QmlTab::restored() const 0284 { 0285 if (!m_webTab) { 0286 return false; 0287 } 0288 0289 return m_webTab->isRestored(); 0290 } 0291 0292 bool QmlTab::current() const 0293 { 0294 if (!m_webTab) { 0295 return false; 0296 } 0297 0298 return m_webTab->isCurrentTab(); 0299 } 0300 0301 bool QmlTab::playing() const 0302 { 0303 if (!m_webTab) { 0304 return false; 0305 } 0306 0307 return m_webTab->isPlaying(); 0308 } 0309 0310 QmlWindow *QmlTab::browserWindow() const 0311 { 0312 if (!m_webTab) { 0313 return nullptr; 0314 } 0315 0316 return QmlStaticData::instance().getWindow(m_webTab->browserWindow()); 0317 } 0318 0319 bool QmlTab::loading() const 0320 { 0321 if (!m_webTab) { 0322 return false; 0323 } 0324 0325 return m_webTab->webView()->isLoading(); 0326 } 0327 0328 int QmlTab::loadingProgress() const 0329 { 0330 if (!m_webTab) { 0331 return -1; 0332 } 0333 0334 return m_webTab->webView()->loadingProgress(); 0335 } 0336 0337 bool QmlTab::backgroundActivity() const 0338 { 0339 if (!m_webTab) { 0340 return false; 0341 } 0342 0343 return m_webTab->webView()->backgroundActivity(); 0344 } 0345 0346 bool QmlTab::canGoBack() const 0347 { 0348 if (!m_webTab) { 0349 return false; 0350 } 0351 0352 return m_webTab->webView()->history()->canGoBack(); 0353 } 0354 0355 bool QmlTab::canGoForward() const 0356 { 0357 if (!m_webTab) { 0358 return false; 0359 } 0360 0361 return m_webTab->webView()->history()->canGoForward(); 0362 } 0363 0364 void QmlTab::setWebPage(WebPage *webPage) 0365 { 0366 if (m_webPage) { 0367 removeConnections(); 0368 } 0369 m_webPage = webPage; 0370 auto *tabbedWebView = qobject_cast<TabbedWebView*>(m_webPage->view()); 0371 m_webTab = tabbedWebView->webTab(); 0372 if (m_webTab) { 0373 createConnections(); 0374 } 0375 } 0376 0377 void QmlTab::createConnections() 0378 { 0379 Q_ASSERT(m_lambdaConnections.length() == 0); 0380 0381 auto titleChangedConnection = connect(m_webTab, &WebTab::titleChanged, this, [this](const QString &title){ 0382 Q_EMIT titleChanged(title); 0383 }); 0384 m_lambdaConnections.append(titleChangedConnection); 0385 0386 auto pinnedChangedConnection = connect(m_webTab, &WebTab::pinnedChanged, this, [this](bool pinned){ 0387 Q_EMIT pinnedChanged(pinned); 0388 }); 0389 m_lambdaConnections.append(pinnedChangedConnection); 0390 0391 auto loadingChangedConnection = connect(m_webTab, &WebTab::loadingChanged, this, [this](bool loading){ 0392 Q_EMIT loadingChanged(loading); 0393 }); 0394 m_lambdaConnections.append(loadingChangedConnection); 0395 0396 auto mutedChangedConnection = connect(m_webTab, &WebTab::mutedChanged, this, [this](bool muted){ 0397 Q_EMIT mutedChanged(muted); 0398 }); 0399 m_lambdaConnections.append(mutedChangedConnection); 0400 0401 auto restoredChangedConnection = connect(m_webTab, &WebTab::restoredChanged, this, [this](bool restored){ 0402 Q_EMIT restoredChanged(restored); 0403 }); 0404 m_lambdaConnections.append(restoredChangedConnection); 0405 0406 auto playingChangedConnection = connect(m_webTab, &WebTab::playingChanged, this, [this](bool playing){ 0407 Q_EMIT playingChanged(playing); 0408 }); 0409 m_lambdaConnections.append(playingChangedConnection); 0410 0411 connect(m_webTab->webView(), &TabbedWebView::zoomLevelChanged, this, &QmlTab::zoomLevelChanged); 0412 connect(m_webTab->webView(), &TabbedWebView::backgroundActivityChanged, this, &QmlTab::backgroundActivityChanged); 0413 0414 if (m_webPage) { 0415 connect(m_webPage, &WebPage::navigationRequestAccepted, this, &QmlTab::navigationRequestAccepted); 0416 } 0417 } 0418 0419 void QmlTab::removeConnections() 0420 { 0421 disconnect(this); 0422 }