diff --git a/webenginepart/src/webenginepartdownloadmanager.cpp b/webenginepart/src/webenginepartdownloadmanager.cpp index 1bffcc9c1..5d76a07a9 100644 --- a/webenginepart/src/webenginepartdownloadmanager.cpp +++ b/webenginepart/src/webenginepartdownloadmanager.cpp @@ -1,88 +1,91 @@ /* * This file is part of the KDE project. * * Copyright (C) 2017 Stefano Crocco * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation; either version 2.1 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #include "webenginepartdownloadmanager.h" #include "webenginepage.h" #include #include #include #include -WebEnginePartDownloadManager::WebEnginePartDownloadManager(): QObject() +WebEnginePartDownloadManager::WebEnginePartDownloadManager() + : QObject() { connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePartDownloadManager::performDownload); } WebEnginePartDownloadManager::~WebEnginePartDownloadManager() { m_requests.clear(); } WebEnginePartDownloadManager * WebEnginePartDownloadManager::instance() { static WebEnginePartDownloadManager inst; return &inst; } void WebEnginePartDownloadManager::addPage(WebEnginePage* page) { - if (!m_pages.contains(page)) m_pages.append(page); + if (!m_pages.contains(page)) { + m_pages.append(page); + } connect(page, &WebEnginePage::navigationRequested, this, &WebEnginePartDownloadManager::recordNavigationRequest); connect(page, &QObject::destroyed, this, &WebEnginePartDownloadManager::removePage); } void WebEnginePartDownloadManager::removePage(QObject* page) { const QUrl url = m_requests.key(static_cast(page)); m_requests.remove(url); m_pages.removeOne(static_cast(page)); } void WebEnginePartDownloadManager::performDownload(QWebEngineDownloadItem* it) { WebEnginePage *page = m_requests.take(it->url()); bool forceNew = false; if (!page && !m_pages.isEmpty()) { qDebug() << "downloading" << it->url() << "in new window or tab"; page = m_pages.first(); forceNew = true; - } - else if (!page){ + } else if (!page) { qDebug() << "Couldn't find a part wanting to download" << it->url(); return; } page->download(it->url(), forceNew); } void WebEnginePartDownloadManager::recordNavigationRequest(WebEnginePage *page, const QUrl& url) { - qDebug() << "recordNavigatioRequest for" << url; + qDebug() << url; m_requests.insert(url, page); } - WebEnginePage* WebEnginePartDownloadManager::pageForDownload(QWebEngineDownloadItem* it) { WebEnginePage *page = m_requests.value(it->url()); - if(!page && !m_pages.isEmpty()) page = m_pages.first(); + if (!page && !m_pages.isEmpty()) { + page = m_pages.first(); + } return page; } diff --git a/webenginepart/src/webenginepartdownloadmanager.h b/webenginepart/src/webenginepartdownloadmanager.h index c565dbf11..7393dec9e 100644 --- a/webenginepart/src/webenginepartdownloadmanager.h +++ b/webenginepart/src/webenginepartdownloadmanager.h @@ -1,60 +1,57 @@ /* * This file is part of the KDE project. * * Copyright (C) 2017 Stefano Crocco * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation; either version 2.1 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #ifndef WEBENGINEPARTDOWNLOADMANAGER_H #define WEBENGINEPARTDOWNLOADMANAGER_H #include #include #include class WebEnginePage; class QWebEngineDownloadItem; class WebEnginePartDownloadManager : public QObject { Q_OBJECT public: - static WebEnginePartDownloadManager* instance(); ~WebEnginePartDownloadManager(); public Q_SLOTS: void addPage(WebEnginePage *page); void removePage(QObject *page); private: - WebEnginePartDownloadManager(); WebEnginePage* pageForDownload(QWebEngineDownloadItem *it); private Q_SLOTS: - void performDownload(QWebEngineDownloadItem *it); void recordNavigationRequest(WebEnginePage* page, const QUrl& url); private: QVector m_pages; QHash m_requests; }; #endif // WEBENGINEPARTDOWNLOADMANAGER_H