diff --git a/src/lib/downloads/downloadmanager.cpp b/src/lib/downloads/downloadmanager.cpp index 7e108a33d..8d38d7244 100644 --- a/src/lib/downloads/downloadmanager.cpp +++ b/src/lib/downloads/downloadmanager.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #ifdef Q_OS_WIN #include @@ -118,7 +119,7 @@ void DownloadManager::keyPressEvent(QKeyEvent* 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 auto testWebView = [](TabbedWebView *view, const QUrl &url) { @@ -132,6 +133,9 @@ void DownloadManager::closeDownloadTab(const QUrl &url) const if (page->history()->count() != 0) { return false; } +#if QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(5, 12, 0) + return true; +#else if (page->url() != QUrl()) { return false; } @@ -140,15 +144,32 @@ void DownloadManager::closeDownloadTab(const QUrl &url) const tabUrl = QUrl(view->webTab()->locationBar()->text()); } return tabUrl.host() == url.host(); +#endif }; +#if QTWEBENGINEWIDGETS_VERSION >= QT_VERSION_CHECK(5, 12, 0) + if (!item->page()) { + return; + } + WebPage *page = qobject_cast(item->page()); + if (!page) { + return; + } + TabbedWebView *view = qobject_cast(page->view()); + if (!view) { + return; + } + if (testWebView(view, QUrl())) { + view->closeView(); + } +#else BrowserWindow* mainWindow = mApp->getWindow(); // If the main window was closed, there is no need to go further if (mainWindow == nullptr) { return; } - if (testWebView(mainWindow->weView(), url)) { + if (testWebView(mainWindow->weView(), item->url())) { mainWindow->weView()->closeView(); return; } @@ -158,12 +179,13 @@ void DownloadManager::closeDownloadTab(const QUrl &url) const const auto tabs = window->tabWidget()->allTabs(); for (auto *tab : tabs) { auto *view = tab->webView(); - if (testWebView(view, url)) { + if (testWebView(view, item->url())) { view->closeView(); return; } } } +#endif } QWinTaskbarButton *DownloadManager::taskbarButton() @@ -272,7 +294,7 @@ void DownloadManager::download(QWebEngineDownloadItem *downloadItem) QTime downloadTimer; downloadTimer.start(); - closeDownloadTab(downloadItem->url()); + closeDownloadTab(downloadItem); QString downloadPath; bool openFile = false; diff --git a/src/lib/downloads/downloadmanager.h b/src/lib/downloads/downloadmanager.h index 6714564ad..f31649e54 100644 --- a/src/lib/downloads/downloadmanager.h +++ b/src/lib/downloads/downloadmanager.h @@ -95,7 +95,7 @@ private: void resizeEvent(QResizeEvent* e) override; void keyPressEvent(QKeyEvent* e) override; - void closeDownloadTab(const QUrl &url) const; + void closeDownloadTab(QWebEngineDownloadItem *item) const; QWinTaskbarButton *taskbarButton(); Ui::DownloadManager* ui;