Integrate file uploads and downloads with plasma's job tracker

Implements #538
This commit is contained in:
Tobias Fella
2022-11-22 18:21:49 +01:00
parent 37e4d85978
commit ab1b1f6c08
6 changed files with 110 additions and 3 deletions

View File

@@ -43,6 +43,7 @@ add_library(neochat STATIC
actionsmodel.cpp
serverlistmodel.cpp
statemodel.cpp
filetransferpseudojob.cpp
)
add_executable(neochat-app

View File

@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2022 Tobias Fella <fella@posteo.de>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "filetransferpseudojob.h"
#include <KLocalizedString>
#include <QDebug>
#include <QUrl>
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});
}

View File

@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2022 Tobias Fella <fella@posteo.de>
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <KJob>
#include <QString>
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;
};

View File

@@ -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 <KIO/Job>
#endif
#include <KJobTrackerInterface>
#include <KLocalizedString>
using namespace Quotient;
@@ -154,6 +159,14 @@ QCoro::Task<void> 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
}

View File

@@ -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);

View File

@@ -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);
}
}
}