1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

DownloadManager: Correctly detect originating page

Requires QtWebEngine 5.12
This commit is contained in:
David Rosca 2018-12-25 10:53:52 +01:00
parent 19cce0c838
commit f19f443c1c
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
2 changed files with 27 additions and 5 deletions

View File

@ -40,6 +40,7 @@
#include <QStandardPaths> #include <QStandardPaths>
#include <QWebEngineHistory> #include <QWebEngineHistory>
#include <QWebEngineDownloadItem> #include <QWebEngineDownloadItem>
#include <QtWebEngineWidgetsVersion>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <QtWin> #include <QtWin>
@ -118,7 +119,7 @@ void DownloadManager::keyPressEvent(QKeyEvent* e)
QWidget::keyPressEvent(e); QWidget::keyPressEvent(e);
} }
void DownloadManager::closeDownloadTab(const QUrl &url) const void DownloadManager::closeDownloadTab(QWebEngineDownloadItem *item) const
{ {
// Attempt to close empty tab that was opened only for loading the download url // Attempt to close empty tab that was opened only for loading the download url
auto testWebView = [](TabbedWebView *view, const QUrl &url) { auto testWebView = [](TabbedWebView *view, const QUrl &url) {
@ -132,6 +133,9 @@ void DownloadManager::closeDownloadTab(const QUrl &url) const
if (page->history()->count() != 0) { if (page->history()->count() != 0) {
return false; return false;
} }
#if QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(5, 12, 0)
return true;
#else
if (page->url() != QUrl()) { if (page->url() != QUrl()) {
return false; return false;
} }
@ -140,15 +144,32 @@ void DownloadManager::closeDownloadTab(const QUrl &url) const
tabUrl = QUrl(view->webTab()->locationBar()->text()); tabUrl = QUrl(view->webTab()->locationBar()->text());
} }
return tabUrl.host() == url.host(); return tabUrl.host() == url.host();
#endif
}; };
#if QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(5, 12, 0)
if (!item->page()) {
return;
}
WebPage *page = qobject_cast<WebPage*>(item->page());
if (!page) {
return;
}
TabbedWebView *view = qobject_cast<TabbedWebView*>(page->view());
if (!view) {
return;
}
if (testWebView(view, QUrl())) {
view->closeView();
}
#else
BrowserWindow* mainWindow = mApp->getWindow(); BrowserWindow* mainWindow = mApp->getWindow();
// If the main window was closed, there is no need to go further // If the main window was closed, there is no need to go further
if (mainWindow == nullptr) { if (mainWindow == nullptr) {
return; return;
} }
if (testWebView(mainWindow->weView(), url)) { if (testWebView(mainWindow->weView(), item->url())) {
mainWindow->weView()->closeView(); mainWindow->weView()->closeView();
return; return;
} }
@ -158,12 +179,13 @@ void DownloadManager::closeDownloadTab(const QUrl &url) const
const auto tabs = window->tabWidget()->allTabs(); const auto tabs = window->tabWidget()->allTabs();
for (auto *tab : tabs) { for (auto *tab : tabs) {
auto *view = tab->webView(); auto *view = tab->webView();
if (testWebView(view, url)) { if (testWebView(view, item->url())) {
view->closeView(); view->closeView();
return; return;
} }
} }
} }
#endif
} }
QWinTaskbarButton *DownloadManager::taskbarButton() QWinTaskbarButton *DownloadManager::taskbarButton()
@ -272,7 +294,7 @@ void DownloadManager::download(QWebEngineDownloadItem *downloadItem)
QTime downloadTimer; QTime downloadTimer;
downloadTimer.start(); downloadTimer.start();
closeDownloadTab(downloadItem->url()); closeDownloadTab(downloadItem);
QString downloadPath; QString downloadPath;
bool openFile = false; bool openFile = false;

View File

@ -95,7 +95,7 @@ private:
void resizeEvent(QResizeEvent* e) override; void resizeEvent(QResizeEvent* e) override;
void keyPressEvent(QKeyEvent* e) override; void keyPressEvent(QKeyEvent* e) override;
void closeDownloadTab(const QUrl &url) const; void closeDownloadTab(QWebEngineDownloadItem *item) const;
QWinTaskbarButton *taskbarButton(); QWinTaskbarButton *taskbarButton();
Ui::DownloadManager* ui; Ui::DownloadManager* ui;