/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote * * SPDX-License-Identifier: GPL-3.0 */ #include "webview.h" #include "subwindow/subwindow.h" #include "webpage.h" #include "webprofile.h" #include "webprofilemanager.h" #include "webviewcontextmenu.h" #include #include #include WebView::WebView(QWidget *parent) : QWebEngineView(parent) { m_parentWindow = qobject_cast(parent); // load status and progress connect(this, &QWebEngineView::loadStarted, this, [this]() { m_loaded = false; }); connect(this, &QWebEngineView::loadFinished, this, [this]() { m_loaded = true; }); // TODO for Qt 5.15, check for fix on QTBUG 65223 connect(this, &QWebEngineView::loadProgress, this, [this](int progress) { if(progress == 100) { emit loadFinished(true); } }); } WebView::WebView(WebProfile *profile, QWidget *parent) : WebView(parent) { Q_CHECK_PTR(profile); m_profile = profile; setPage(new WebPage(profile, this)); } WebView::WebView(const Session::WebView &data, QWidget *parent) : WebView(parent) { const auto *profileManager = WebProfileManager::instance(); Q_CHECK_PTR(profileManager); auto *profile = profileManager->profile(data.profile); if(profile != nullptr) { setProfile(profile); } if(!data.url.isEmpty()) load(QUrl::fromUserInput(data.url)); else { QByteArray copy(data.history); QDataStream historyStream(©, QIODevice::ReadOnly); historyStream >> *history(); } } void WebView::setProfile(WebProfile *profile) { m_profile = profile; const auto url = this->url(); setPage(new WebPage(profile, this)); this->load(url); } Session::WebView WebView::serialize() const { const auto *profileManager = WebProfileManager::instance(); Q_CHECK_PTR(profileManager); QByteArray historyData; QDataStream historyStream(&historyData, QIODevice::WriteOnly); historyStream << *history(); return { profileManager->id(profile()), QString(), historyData }; } bool WebView::isLoaded() const { return m_loaded; } void WebView::search(const QString &term) { const QString searchUrl = m_profile->search().arg(QString(QUrl::toPercentEncoding(term))); load(searchUrl); } WebView *WebView::createWindow(QWebEnginePage::WebWindowType type) { Q_CHECK_PTR(m_parentWindow); // parent Window has been found auto index = m_parentWindow->addTab(); WebView *view = m_parentWindow->view(index); switch(type) { case QWebEnginePage::WebBrowserWindow: // a complete web browser window break; case QWebEnginePage::WebBrowserTab: // a web browser tab m_parentWindow->setCurrentTab(index); break; case QWebEnginePage::WebDialog: // a window without decorations break; case QWebEnginePage::WebBrowserBackgroundTab: // a web browser tab, but don't swap to it break; } return view; } void WebView::contextMenuEvent(QContextMenuEvent *event) { auto *menu = new WebViewContextMenu(this); //const auto ctxdata = page()->contextMenuData(); menu->exec(event->globalPos()); }