1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

Implement download integration with Plasma

This commit is contained in:
Javier Llorente 2023-04-15 22:49:35 +02:00 committed by Juraj Oravec
parent 2091b3d60c
commit db0584f81e
13 changed files with 268 additions and 12 deletions

View File

@ -124,8 +124,10 @@ set_package_properties(KF5Crash PROPERTIES DESCRIPTION "KDE Frameworks Integrati
find_package(KF5CoreAddons ${KF5_MIN_VERSION} CONFIG)
set_package_properties(KF5CoreAddons PROPERTIES DESCRIPTION "KDE Frameworks Integration plugin" TYPE OPTIONAL)
find_package(KF5Purpose ${KF5_MIN_VERSION} CONFIG)
find_package(KF5JobWidgets ${KF5_MIN_VERSION} CONFIG)
set_package_properties(KF5JobWidgets PROPERTIES DESCRIPTION "KDE Frameworks Integration plugin" TYPE OPTIONAL)
set_package_properties(KF5Purpose PROPERTIES DESCRIPTION "KDE Frameworks Integration plugin" TYPE OPTIONAL)
if (KF5Wallet_FOUND AND KF5KIO_FOUND AND KF5Crash_FOUND AND KF5CoreAddons_FOUND AND KF5Purpose_FOUND)
if (KF5Wallet_FOUND AND KF5KIO_FOUND AND KF5Crash_FOUND AND KF5CoreAddons_FOUND AND KF5Purpose_FOUND AND KF5JobWidgets_FOUND)
set(ENABLE_KDE_FRAMEWORKS_INTEGRATION_PLUGIN TRUE)
endif()

View File

@ -102,6 +102,7 @@ set(SRCS ${SRCS}
cookies/cookiemanager.cpp
downloads/downloaditem.cpp
downloads/downloadmanager.cpp
downloads/downloadmanagermodel.cpp
downloads/downloadoptionsdialog.cpp
downloads/downloadsbutton.cpp
history/history.cpp
@ -328,6 +329,7 @@ set(SRCS ${SRCS}
cookies/cookiemanager.h
downloads/downloaditem.h
downloads/downloadmanager.h
downloads/downloadmanagermodel.h
downloads/downloadoptionsdialog.h
downloads/downloadsbutton.h
history/history.h

View File

@ -172,6 +172,7 @@ void DownloadItem::downloadProgress(qint64 received, qint64 total)
m_total = total;
updateDownloadInfo(m_currSpeed, m_received, m_total);
progressChanged(m_currSpeed, m_received, m_total);
}
int DownloadItem::progress()
@ -363,6 +364,21 @@ void DownloadItem::openFolder()
#endif
}
QUrl DownloadItem::url() const
{
return m_downUrl;
}
QString DownloadItem::path() const
{
return m_path;
}
QString DownloadItem::fileName() const
{
return m_fileName;
}
DownloadItem::~DownloadItem()
{
delete ui;

View File

@ -48,6 +48,9 @@ public:
QTime remainingTime() const { return m_remTime; }
double currentSpeed() const { return m_currSpeed; }
int progress();
QUrl url() const;
QString path() const;
QString fileName() const;
~DownloadItem() override;
void setDownTimer(const QTime &timer) { m_downTimer = timer; }
@ -59,6 +62,7 @@ public:
Q_SIGNALS:
void deleteItem(DownloadItem*);
void downloadFinished(bool success);
void progressChanged(double currSpeed, qint64 received, qint64 total);
private Q_SLOTS:
void parentResized(const QSize &size);

View File

@ -21,6 +21,10 @@
#include "mainapplication.h"
#include "downloadoptionsdialog.h"
#include "downloaditem.h"
#include "downloadmanagermodel.h"
#ifdef PLASMA_DOWNLOADS
#include "downloadkjob.h"
#endif
#include "networkmanager.h"
#include "desktopnotificationsfactory.h"
#include "qztools.h"
@ -52,6 +56,7 @@
DownloadManager::DownloadManager(QWidget* parent)
: QWidget(parent)
, ui(new Ui::DownloadManager)
, m_model(new DownloadManagerModel(this))
, m_isClosing(false)
, m_lastDownloadOption(NoOption)
{
@ -73,6 +78,8 @@ DownloadManager::DownloadManager(QWidget* parent)
loadSettings();
QzTools::setWmClass("Download Manager", this);
connect(m_model, &DownloadManagerModel::downloadAdded, this, &DownloadManager::downloadAdded);
}
void DownloadManager::loadSettings()
@ -365,8 +372,10 @@ void DownloadManager::download(QWebEngineDownloadItem *downloadItem)
auto* downItem = new DownloadItem(listItem, downloadItem, QFileInfo(downloadPath).absolutePath(), QFileInfo(downloadPath).fileName(), openFile, this);
downItem->setDownTimer(downloadTimer);
downItem->startDownloading();
connect(downItem, &DownloadItem::deleteItem, this, &DownloadManager::deleteItem);
connect(downItem, &DownloadItem::downloadFinished, this, &DownloadManager::downloadFinished);
connect(downItem, &DownloadItem::deleteItem, m_model, &DownloadManagerModel::removeDownload);
connect(downItem, &DownloadItem::downloadFinished, this, QOverload<bool>::of(&DownloadManager::downloadFinished));
connect(downItem, &DownloadItem::downloadFinished, this, QOverload<>::of(&DownloadManager::downloadFinished));
m_model->addDownload(downItem);
ui->list->setItemWidget(listItem, downItem);
listItem->setSizeHint(downItem->sizeHint());
downItem->show();
@ -377,7 +386,7 @@ void DownloadManager::download(QWebEngineDownloadItem *downloadItem)
int DownloadManager::downloadsCount() const
{
return ui->list->count();
return m_model->count();
}
int DownloadManager::activeDownloadsCount() const
@ -424,13 +433,6 @@ void DownloadManager::downloadFinished(bool success)
}
}
void DownloadManager::deleteItem(DownloadItem* item)
{
if (item && !item->isDownloading()) {
delete item;
}
}
bool DownloadManager::canClose()
{
if (m_isClosing) {

View File

@ -36,6 +36,10 @@ class QWebEngineDownloadItem;
class QWinTaskbarButton;
class DownloadItem;
class DownloadManagerModel;
#ifdef PLASMA_DOWNLOADS
class DownloadKJob;
#endif
class WebPage;
class FALKON_EXPORT DownloadManager : public QWidget
@ -82,12 +86,14 @@ public Q_SLOTS:
private Q_SLOTS:
void clearList();
void deleteItem(DownloadItem* item);
void downloadFinished(bool success);
Q_SIGNALS:
void resized(QSize);
void downloadsCountChanged();
void downloadAdded(DownloadItem *item);
void downloadRemoved(DownloadItem *item);
void downloadFinished();
private:
void timerEvent(QTimerEvent* e) override;
@ -99,6 +105,7 @@ private:
QWinTaskbarButton *taskbarButton();
Ui::DownloadManager* ui;
DownloadManagerModel *m_model;
QBasicTimer m_timer;
QString m_lastDownloadPath;

View File

@ -0,0 +1,65 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Javier Llorente <javier@opensuse.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "downloadmanagermodel.h"
DownloadManagerModel::DownloadManagerModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int DownloadManagerModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return count();
}
QVariant DownloadManagerModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
const DownloadItem *item = m_downloads.at(index.row());
return item;
}
return QVariant();
}
void DownloadManagerModel::addDownload(DownloadItem *item)
{
m_downloads.append(item);
connect(item, &DownloadItem::deleteItem, this, &DownloadManagerModel::removeDownload);
emit downloadAdded(item);
}
void DownloadManagerModel::removeDownload(DownloadItem *item)
{
if (item && !item->isDownloading()) {
delete item;
emit downloadRemoved(item);
}
}
int DownloadManagerModel::count() const
{
return m_downloads.count();
}
DownloadItem *DownloadManagerModel::at(int index)
{
return m_downloads.at(index);
}

View File

@ -0,0 +1,45 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Javier Llorente <javier@opensuse.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef DOWNLOADMANAGERMODEL_H
#define DOWNLOADMANAGERMODEL_H
#include <QAbstractListModel>
#include "downloaditem.h"
class DownloadManagerModel : public QAbstractListModel
{
Q_OBJECT
public:
DownloadManagerModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
void addDownload(DownloadItem *item);
void removeDownload(DownloadItem *item);
int count() const;
DownloadItem *at(int index);
private:
QList<DownloadItem *> m_downloads;
signals:
void downloadAdded(DownloadItem *item);
void downloadRemoved(DownloadItem *item);
};
#endif // DOWNLOADMANAGERMODEL_H

View File

@ -2,9 +2,11 @@ set(KDEFrameworksIntegration_SRCS
kdeframeworksintegrationplugin.cpp
kwalletpasswordbackend.cpp
kioschemehandler.cpp
downloadkjob.cpp
kdeframeworksintegrationplugin.h
kwalletpasswordbackend.h
kioschemehandler.h
downloadkjob.h
)
ecm_create_qm_loader(KDEFrameworksIntegration_SRCS falkon_kdeframeworksintegration_qt)
@ -24,4 +26,5 @@ target_link_libraries(KDEFrameworksIntegration
KF5::Crash
KF5::CoreAddons
KF5::PurposeWidgets
KF5::JobWidgets
)

View File

@ -0,0 +1,44 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Javier Llorente <javier@opensuse.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "downloadkjob.h"
DownloadKJob::DownloadKJob(const QUrl &url, const QString &path, const QString &fileName, QObject *parent)
: KJob(parent),
m_url(url),
m_path(path),
m_fileName(fileName)
{
}
void DownloadKJob::start()
{
}
void DownloadKJob::updateDescription()
{
emit description(this, tr("Downloading"),
qMakePair<QString, QString>(tr("Source"), m_url.toDisplayString()),
qMakePair<QString, QString>(tr("Destination"), QString("%1/%2").arg(m_path, m_fileName)));
}
void DownloadKJob::progress(double currSpeed, qint64 received, qint64 total)
{
setProcessedAmount(Bytes, received);
setTotalAmount(Bytes, total);
emit speed(this, currSpeed);
}

View File

@ -0,0 +1,44 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2019 Javier Llorente <javier@opensuse.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef DOWNLOADKJOB_H
#define DOWNLOADKJOB_H
#include "qzcommon.h"
#include <KJob>
#include <QUrl>
class FALKON_EXPORT DownloadKJob : public KJob
{
Q_OBJECT
public:
DownloadKJob(const QUrl &url, const QString &path, const QString &fileName, QObject *parent = nullptr);
void start();
void updateDescription();
public slots:
void progress(double currSpeed, qint64 received, qint64 total);
private:
QUrl m_url;
QString m_path;
QString m_fileName;
};
#endif // DOWNLOADKJOB_H

View File

@ -23,14 +23,18 @@
#include "mainapplication.h"
#include "autofill.h"
#include "passwordmanager.h"
#include "downloadmanager.h"
#include "kioschemehandler.h"
#include "webpage.h"
#include "webview.h"
#include "downloadkjob.h"
#include "downloaditem.h"
#include <KCrash>
#include <KAboutData>
#include <KProtocolInfo>
#include <Purpose/AlternativesModel>
#include <KUiServerJobTracker>
#include <QWebEngineProfile>
#include <QMenu>
@ -54,6 +58,22 @@ void KDEFrameworksIntegrationPlugin::init(InitState state, const QString &settin
mApp->autoFill()->passwordManager()->switchBackend(QSL("KWallet"));
}
m_jobTracker = new KUiServerJobTracker(this);
auto manager = mApp->downloadManager();
connect(manager, &DownloadManager::downloadAdded, [=](DownloadItem *item) {
auto job = new DownloadKJob(item->url(), item->path(), item->fileName(), this);
m_jobTracker->registerJob(job);
job->start();
job->updateDescription();
connect(item, &DownloadItem::progressChanged, job, &DownloadKJob::progress);
connect(manager, QOverload<>::of(&DownloadManager::downloadFinished), m_jobTracker, [=]() {
m_jobTracker->unregisterJob(job);
});
});
const auto protocols = KProtocolInfo::protocols();
for (const QString &protocol : protocols) {
if (WebPage::internalSchemes().contains(protocol)) {

View File

@ -23,6 +23,7 @@
class KWalletPasswordBackend;
class KIOSchemeHandler;
class KUiServerJobTracker;
class KDEFrameworksIntegrationPlugin : public QObject, public PluginInterface
{
@ -42,4 +43,5 @@ private:
KWalletPasswordBackend *m_backend = nullptr;
QVector<KIOSchemeHandler*> m_kioSchemeHandlers;
Purpose::Menu *m_sharePageMenu = nullptr;
KUiServerJobTracker *m_jobTracker = nullptr;
};