Integrate file uploads and downloads with plasma's job tracker
Implements #538
This commit is contained in:
@@ -43,6 +43,7 @@ add_library(neochat STATIC
|
|||||||
actionsmodel.cpp
|
actionsmodel.cpp
|
||||||
serverlistmodel.cpp
|
serverlistmodel.cpp
|
||||||
statemodel.cpp
|
statemodel.cpp
|
||||||
|
filetransferpseudojob.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(neochat-app
|
add_executable(neochat-app
|
||||||
|
|||||||
50
src/filetransferpseudojob.cpp
Normal file
50
src/filetransferpseudojob.cpp
Normal 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});
|
||||||
|
}
|
||||||
28
src/filetransferpseudojob.h
Normal file
28
src/filetransferpseudojob.h
Normal 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;
|
||||||
|
};
|
||||||
@@ -42,9 +42,14 @@
|
|||||||
#include "pollevent.h"
|
#include "pollevent.h"
|
||||||
#include "pollhandler.h"
|
#include "pollhandler.h"
|
||||||
#endif
|
#endif
|
||||||
|
#include "filetransferpseudojob.h"
|
||||||
#include "stickerevent.h"
|
#include "stickerevent.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#ifndef Q_OS_ANDROID
|
||||||
|
#include <KIO/Job>
|
||||||
|
#endif
|
||||||
|
#include <KJobTrackerInterface>
|
||||||
#include <KLocalizedString>
|
#include <KLocalizedString>
|
||||||
|
|
||||||
using namespace Quotient;
|
using namespace Quotient;
|
||||||
@@ -154,6 +159,14 @@ QCoro::Task<void> NeoChatRoom::doUploadFile(QUrl url, QString body)
|
|||||||
setFileUploadingProgress(int(float(progress) / float(total) * 100));
|
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()
|
void NeoChatRoom::acceptInvitation()
|
||||||
@@ -1266,6 +1279,19 @@ bool NeoChatRoom::downloadTempFile(const QString &eventId)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
downloadFile(eventId, QUrl::fromLocalFile(file.fileName()));
|
download(eventId, QUrl::fromLocalFile(file.fileName()));
|
||||||
return true;
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -168,6 +168,8 @@ public:
|
|||||||
|
|
||||||
Q_INVOKABLE void setPushNotificationState(PushNotificationState::State state);
|
Q_INVOKABLE void setPushNotificationState(PushNotificationState::State state);
|
||||||
|
|
||||||
|
Q_INVOKABLE void download(const QString &eventId, const QUrl &localFilename = {});
|
||||||
|
|
||||||
QString chatBoxText() const;
|
QString chatBoxText() const;
|
||||||
void setChatBoxText(const QString &text);
|
void setChatBoxText(const QString &text);
|
||||||
|
|
||||||
|
|||||||
@@ -160,10 +160,10 @@ TimelineContainer {
|
|||||||
FileDialog {
|
FileDialog {
|
||||||
fileMode: FileDialog.SaveFile
|
fileMode: FileDialog.SaveFile
|
||||||
folder: StandardPaths.writableLocation(StandardPaths.DownloadLocation)
|
folder: StandardPaths.writableLocation(StandardPaths.DownloadLocation)
|
||||||
onAccepted: if (openSavedFile) {
|
onAccepted: if (autoOpenFile) {
|
||||||
UrlHelper.copyTo(progressInfo.localPath, file)
|
UrlHelper.copyTo(progressInfo.localPath, file)
|
||||||
} else {
|
} else {
|
||||||
currentRoom.downloadFile(eventId, file);
|
currentRoom.download(eventId, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user