diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index db546e9eb..95ea78765 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,6 +43,7 @@ add_library(neochat STATIC actionsmodel.cpp serverlistmodel.cpp statemodel.cpp + filetransferpseudojob.cpp ) add_executable(neochat-app diff --git a/src/filetransferpseudojob.cpp b/src/filetransferpseudojob.cpp new file mode 100644 index 000000000..78c52bf9e --- /dev/null +++ b/src/filetransferpseudojob.cpp @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2022 Tobias Fella +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "filetransferpseudojob.h" +#include +#include +#include + +FileTransferPseudoJob::FileTransferPseudoJob(Operation operation, const QString &path, const QString &eventId) + : KJob() + , m_path(path) + , m_eventId(eventId) + , m_operation(operation) +{ +} + +void FileTransferPseudoJob::fileTransferProgress(QString id, qint64 progress, qint64 total) +{ + if (id != m_eventId) { + return; + } + setProcessedAmount(Unit::Bytes, progress); + setTotalAmount(Unit::Bytes, total); +} + +void FileTransferPseudoJob::fileTransferCompleted(QString id, QUrl localFile) +{ + if (id != m_eventId) { + return; + } + emitResult(); +} + +void FileTransferPseudoJob::fileTransferFailed(QString id, QString errorMessage) +{ + if (id != m_eventId) { + return; + } + setErrorText(errorMessage); + emitResult(); +} + +void FileTransferPseudoJob::start() +{ + setTotalAmount(Unit::Files, 1); + Q_EMIT description(this, + m_operation == Download ? i18nc("Job heading, like 'Copying'", "Downloading") : i18nc("Job heading, like 'Copying'", "Uploading"), + {i18nc("The URL being downloaded/uploaded", "Source"), m_path}, + {i18nc("The location being downloaded to", "Destination"), m_path}); +} diff --git a/src/filetransferpseudojob.h b/src/filetransferpseudojob.h new file mode 100644 index 000000000..a577ee593 --- /dev/null +++ b/src/filetransferpseudojob.h @@ -0,0 +1,28 @@ +// SPDX-FileCopyrightText: 2022 Tobias Fella +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +class FileTransferPseudoJob : public KJob +{ +public: + enum Operation { + Download, + Upload, + }; + Q_ENUM(Operation); + FileTransferPseudoJob(Operation operation, const QString &srcDest, const QString &path); + void fileTransferProgress(QString id, qint64 progress, qint64 total); + void fileTransferCompleted(QString id, QUrl localFile); + void fileTransferFailed(QString id, QString errorMessage = {}); + + void start() override; + +private: + QString m_path; + QString m_eventId; + Operation m_operation; +}; diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 853b83173..d3edb1021 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -42,9 +42,14 @@ #include "pollevent.h" #include "pollhandler.h" #endif +#include "filetransferpseudojob.h" #include "stickerevent.h" #include "utils.h" +#ifndef Q_OS_ANDROID +#include +#endif +#include #include using namespace Quotient; @@ -154,6 +159,14 @@ QCoro::Task NeoChatRoom::doUploadFile(QUrl url, QString body) setFileUploadingProgress(int(float(progress) / float(total) * 100)); } }); +#ifndef Q_OS_ANDROID + auto job = new FileTransferPseudoJob(FileTransferPseudoJob::Upload, url.toLocalFile(), txnId); + connect(this, &Room::fileTransferProgress, job, &FileTransferPseudoJob::fileTransferProgress); + connect(this, &Room::fileTransferCompleted, job, &FileTransferPseudoJob::fileTransferCompleted); + connect(this, &Room::fileTransferFailed, job, &FileTransferPseudoJob::fileTransferFailed); + KIO::getJobTracker()->registerJob(job); + job->start(); +#endif } void NeoChatRoom::acceptInvitation() @@ -1266,6 +1279,19 @@ bool NeoChatRoom::downloadTempFile(const QString &eventId) return false; } - downloadFile(eventId, QUrl::fromLocalFile(file.fileName())); + download(eventId, QUrl::fromLocalFile(file.fileName())); return true; } + +void NeoChatRoom::download(const QString &eventId, const QUrl &localFilename) +{ + downloadFile(eventId, localFilename); +#ifndef Q_OS_ANDROID + auto job = new FileTransferPseudoJob(FileTransferPseudoJob::Download, localFilename.toLocalFile(), eventId); + connect(this, &Room::fileTransferProgress, job, &FileTransferPseudoJob::fileTransferProgress); + connect(this, &Room::fileTransferCompleted, job, &FileTransferPseudoJob::fileTransferCompleted); + connect(this, &Room::fileTransferFailed, job, &FileTransferPseudoJob::fileTransferFailed); + KIO::getJobTracker()->registerJob(job); + job->start(); +#endif +} diff --git a/src/neochatroom.h b/src/neochatroom.h index 48b9d16c0..475297923 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -168,6 +168,8 @@ public: Q_INVOKABLE void setPushNotificationState(PushNotificationState::State state); + Q_INVOKABLE void download(const QString &eventId, const QUrl &localFilename = {}); + QString chatBoxText() const; void setChatBoxText(const QString &text); diff --git a/src/qml/Component/Timeline/FileDelegate.qml b/src/qml/Component/Timeline/FileDelegate.qml index cd0139a9f..86dd37190 100644 --- a/src/qml/Component/Timeline/FileDelegate.qml +++ b/src/qml/Component/Timeline/FileDelegate.qml @@ -160,10 +160,10 @@ TimelineContainer { FileDialog { fileMode: FileDialog.SaveFile folder: StandardPaths.writableLocation(StandardPaths.DownloadLocation) - onAccepted: if (openSavedFile) { + onAccepted: if (autoOpenFile) { UrlHelper.copyTo(progressInfo.localPath, file) } else { - currentRoom.downloadFile(eventId, file); + currentRoom.download(eventId, file); } } }