Page MenuHomePhorge

No OneTemporary

Size
17 KB
Referenced Files
None
Subscribers
None
diff --git a/src/kazv-io-manager.cpp b/src/kazv-io-manager.cpp
index 67d31d0..ab2ea0a 100644
--- a/src/kazv-io-manager.cpp
+++ b/src/kazv-io-manager.cpp
@@ -1,120 +1,101 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2022 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "kazv-io-manager.hpp"
#include <QObject>
#include <QVector>
#include <KIO/TransferJob>
#include <QSaveFile>
#include <QPointer>
#include <QUrl>
#include <QIODevice>
#include <algorithm>
#include <execution>
-struct KazvIODownloadJob {
+struct KazvIOJob {
QPointer<KIO::TransferJob> job;
- QPointer<QSaveFile> file;
+ QString roomId;
+ QPointer<QSaveFile> localFile;
+
+ KazvIOJob(const QString &roomId, const QUrl &serverUrl, const QUrl &localFileUrl)
+ {
+ this->job = KIO::get(serverUrl);
+ this->roomId = roomId;
+ this->localFile = new QSaveFile(localFileUrl.path());
+ }
};
struct KazvIOManagerPrivate {
- QVector<KazvIODownloadJob> jobs;
+ QVector<KazvIOJob> jobs;
};
KazvIOManager::KazvIOManager(QObject *parent)
: QObject(parent)
, m_d(new KazvIOManagerPrivate)
{
}
KazvIOManager::~KazvIOManager() = default;
-QPointer<KIO::TransferJob> KazvIOManager::startNewDownloadJob(QUrl &url, QUrl &localFileUrl)
+QPointer<KIO::TransferJob> KazvIOManager::startNewDownloadJob(const QString &roomId, const QUrl &serverUrl, const QUrl &localFileUrl)
{
- KazvIODownloadJob downloadJob;
- auto job = KIO::get(url);
- downloadJob.job = job;
- downloadJob.file = new QSaveFile(localFileUrl.path());
- downloadJob.file->open(QIODevice::WriteOnly);
- connect(downloadJob.job, &KIO::TransferJob::data, this, &KazvIOManager::writeFile);
- connect(downloadJob.job, &KJob::result, this, &KazvIOManager::cleanJob);
- m_d->jobs.push_back(downloadJob);
- downloadJob.job->start();
- return job;
-}
+ m_d->jobs.push_back(KazvIOJob(roomId, serverUrl, localFileUrl));
+ connect(m_d->jobs.last().job, &KIO::TransferJob::data, this, &KazvIOManager::writeFile);
+ connect(m_d->jobs.last().job, &KJob::result, this, &KazvIOManager::cleanJob);
-QPointer<KIO::TransferJob> KazvIOManager::startNewUploadJob(QFile &file, const QUrl &serverUrl, const QString &token)
-{
- // TODO: Upload File
- auto job = KIO::http_post(serverUrl, &file);
- // connect(job, &KJob::result, this, &KazvIOManager::cleanJob);
- // connect(job, &KIO::TransferJob::data, this, &KazvIOManager::displayResponse);
- // job->addMetaData(QString("Authorization"), token);
- job->start();
- return job;
+ return m_d->jobs.last().job;
}
-QPointer<KIO::TransferJob> KazvIOManager::getJob(QUrl &url)
+/*
+ QPointer<KIO::TransferJob> KazvIOManager::startNewUploadJob(QFile &file, const QUrl &serverUrl, const QString &token)
+ {
+ // TODO: Upload File
+ auto job = KIO::http_post(serverUrl, &file);
+ // connect(job, &KJob::result, this, &KazvIOManager::cleanJob);
+ // connect(job, &KIO::TransferJob::data, this, &KazvIOManager::displayResponse);
+ // job->addMetaData(QString("Authorization"), token);
+ job->start();
+ return job;
+ }
+*/
+
+QPointer<KIO::TransferJob> KazvIOManager::getJob(const QString &roomId, const QUrl &localFileUrl, const QUrl &serverUrl)
{
auto targetJob = std::find_if(std::execution::seq,
- m_d->jobs.begin(),
- m_d->jobs.end(),
- [&url](auto it) { return it.job->url() == url; });
+ m_d->jobs.begin(),
+ m_d->jobs.end(),
+ [&roomId, &localFileUrl, &serverUrl](auto it) {
+ return (it.roomId == roomId) &&
+ (it.localFile->fileName() == localFileUrl.path()) &&
+ (it.job->url() == serverUrl); });
if (targetJob == m_d->jobs.end()) {
return QPointer<KIO::TransferJob>();
}
- return QPointer<KIO::TransferJob>(targetJob->job);
-}
-
-void KazvIOManager::suspendJob(QUrl &url)
-{
- auto targetJob = std::find_if(std::execution::seq,
- m_d->jobs.begin(),
- m_d->jobs.end(),
- [&url](auto it) { return it.job->url() == url; });
- targetJob->job->suspend();
-}
-void KazvIOManager::resumeJob(QUrl &url)
-{
- auto targetJob = std::find_if(std::execution::seq,
- m_d->jobs.begin(),
- m_d->jobs.end(),
- [&url](auto it) { return it.job->url() == url; });
- targetJob->job->resume();
-}
-
-void KazvIOManager::cancelJob(QUrl &url)
-{
- auto targetJob = std::find_if(std::execution::seq,
- m_d->jobs.begin(),
- m_d->jobs.end(),
- [&url](auto it) { return it.job->url() == url; });
- targetJob->job->kill();
- m_d->jobs.erase(targetJob);
+ return targetJob->job;
}
void KazvIOManager::writeFile(KJob *job, const QByteArray &data)
{
auto targetJob = std::find_if(std::execution::seq,
- m_d->jobs.begin(),
- m_d->jobs.end(),
- [job](auto it) { return it.job->url() == qobject_cast<KIO::TransferJob*>(job)->url(); });
- targetJob->file->write(data);
+ m_d->jobs.begin(),
+ m_d->jobs.end(),
+ [job](auto it) { return it.job == job; });
+ targetJob->localFile->write(data);
}
void KazvIOManager::cleanJob(KJob *job)
{
auto targetJob = std::find_if(std::execution::seq,
- m_d->jobs.begin(),
- m_d->jobs.end(),
- [job](auto it) { return it.job->url() == qobject_cast<KIO::TransferJob*>(job)->url(); });
- targetJob->file->commit();
- delete (QSaveFile*)targetJob->file;
+ m_d->jobs.begin(),
+ m_d->jobs.end(),
+ [job](auto it) { return it.job == job; });
+ targetJob->localFile->commit();
+ delete (QSaveFile*)targetJob->localFile;
m_d->jobs.erase(targetJob);
}
diff --git a/src/kazv-io-manager.hpp b/src/kazv-io-manager.hpp
index 879ab5c..f142f8f 100644
--- a/src/kazv-io-manager.hpp
+++ b/src/kazv-io-manager.hpp
@@ -1,39 +1,36 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2022 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <QObject>
#include <QtQml>
#include <QUrl>
#include <KIO/Job>
#include <QByteArray>
#include <QPointer>
#include <memory>
struct KazvIOManagerPrivate;
class KazvIOManager : public QObject {
Q_OBJECT
QML_ELEMENT
std::unique_ptr<KazvIOManagerPrivate> m_d;
public:
explicit KazvIOManager(QObject *parent = 0);
~KazvIOManager() override;
- QPointer<KIO::TransferJob> startNewDownloadJob(QUrl &url, QUrl &localFileUrl);
- QPointer<KIO::TransferJob> startNewUploadJob(QFile &file, const QUrl &serverUrl, const QString &token);
- QPointer<KIO::TransferJob> getJob(QUrl &url);
- void suspendJob(QUrl &url);
- void resumeJob(QUrl &url);
- void cancelJob(QUrl &url);
+ QPointer<KIO::TransferJob> startNewDownloadJob(const QString &roomId, const QUrl &serverUrl, const QUrl &localFileUrl);
+ // QPointer<KIO::TransferJob> startNewUploadJob(QFile &file, const QUrl &serverUrl, const QString &token);
+ QPointer<KIO::TransferJob> getJob(const QString &roomId, const QUrl &localFileUrl, const QUrl &serverUrl);
private Q_SLOTS:
void writeFile(KJob *job, const QByteArray &data);
void cleanJob(KJob *job);
};
diff --git a/src/kazv-io.cpp b/src/kazv-io.cpp
index 43e0fe1..cbfab21 100644
--- a/src/kazv-io.cpp
+++ b/src/kazv-io.cpp
@@ -1,183 +1,185 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2022 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "kazv-io.hpp"
#include "kazv-io-manager.hpp"
#include <QUrl>
#include <QObject>
struct KazvIOPrivate {
QPointer<KazvIOManager> manager;
- float progress;
- QUrl url;
+ QString roomId;
+ QUrl localFileUrl;
QString serverUrl;
- QString token;
QString mxcUri;
+ float progress;
};
KazvIO::KazvIO(QObject *parent)
: QObject(parent)
, m_d(new KazvIOPrivate)
{
- connect(this, &KazvIO::urlChanged, this, &KazvIO::checkJob);
+ // roomId will only be assigned when KazvIO is created, then check the job it manages in the manager
+ connect(this, &KazvIO::roomIdChanged, this, &KazvIO::checkJob);
connect(this, &KazvIO::success, this, &KazvIO::result);
connect(this, &KazvIO::failure, this, &KazvIO::result);
connect(this, &KazvIO::result, this, &KazvIO::clearPercent);
}
KazvIO::~KazvIO() = default;
void KazvIO::connectJob(QPointer<KJob> job)
{
connect(job, &KJob::percentChanged, this, &KazvIO::updatePercent);
connect(job, &KJob::result, this, &KazvIO::checkResult);
}
/*
* This function is only applicable to a single thread,
* if the manager is located in other threads, please modify this function appropriately
*/
-void KazvIO::download(QUrl localFileUrl)
+void KazvIO::download()
{
- auto job = m_d->manager->startNewDownloadJob(m_d->url, localFileUrl);
+ auto job = m_d->manager->startNewDownloadJob(m_d->roomId, m_d->serverUrl, m_d->localFileUrl);
connectJob(qobject_cast<KJob*>(job));
}
/*
* This function is only applicable to a single thread,
* if the manager is located in other threads, please modify this function appropriately
*/
void KazvIO::upload()
{
// TODO: Get mxcUri form response
- auto file = QFile(m_d->url.toLocalFile());
- auto job = m_d->manager->startNewUploadJob(file, m_d->serverUrl, m_d->token);
- connectJob(qobject_cast<KJob*>(job));
- connectJob(job, &KJob::result, this, &KazvIO::setMxcUri);
+ // auto file = QFile(m_d->url.toLocalFile());
+ // auto job = m_d->manager->startNewUploadJob(file, m_d->serverUrl, m_d->token);
+ // connectJob(qobject_cast<KJob*>(job));
+ // connectJob(job, &KJob::result, this, &KazvIO::setMxcUri);
}
/*
* This function is only applicable to a single thread,
* if the manager is located in other threads, please modify this function appropriately
*/
void KazvIO::suspend()
{
- m_d->manager->suspendJob(m_d->url);
+ // m_d->manager->suspendJob(m_d->url);
}
/*
* This function is only applicable to a single thread,
* if the manager is located in other threads, please modify this function appropriately
*/
void KazvIO::resume()
{
- m_d->manager->resumeJob(m_d->url);
+ // m_d->manager->resumeJob(m_d->url);
}
/*
* This function is only applicable to a single thread,
* if the manager is located in other threads, please modify this function appropriately
*/
void KazvIO::cancel()
{
- m_d->manager->cancelJob(m_d->url);
- Q_EMIT failure();
+ // m_d->manager->cancelJob(m_d->url);
+ // Q_EMIT failure();
}
void KazvIO::checkJob()
{
- auto job = m_d->manager->getJob(m_d->url);
- // The job in the manager is automatically destroyed after completion,
- // so you can judge whether the job is not completed by whether the job is empty or not
- if (!job.isNull()) {
- updatePercent(job, job->percent());
- Q_EMIT progressing();
- if (job->isSuspended()) {
- Q_EMIT suspended();
- }
- connectJob(qobject_cast<KJob*>(job));
- }
+ // auto job = m_d->manager->getJob(m_d->url);
+ // // The job in the manager is automatically destroyed after completion,
+ // // so you can judge whether the job is not completed by whether the job is empty or not
+ // if (!job.isNull()) {
+ // updatePercent(job, job->percent());
+ // Q_EMIT progressing();
+ // if (job->isSuspended()) {
+ // Q_EMIT suspended();
+ // }
+ // connectJob(qobject_cast<KJob*>(job));
+ // }
}
-float KazvIO::progress()
+
+KazvIOManager *KazvIO::manager()
{
- return m_d->progress;
+ return m_d->manager;
}
-QUrl KazvIO::url()
+void KazvIO::setManager(KazvIOManager *manager)
{
- return m_d->url;
+ m_d->manager = manager;
+ Q_EMIT managerChanged();
}
-void KazvIO::setUrl(QUrl url)
+QString KazvIO::roomId()
{
- m_d->url = url;
- Q_EMIT urlChanged();
+ return m_d->roomId;
}
-KazvIOManager *KazvIO::manager()
+void KazvIO::setRoomId(QString roomId)
{
- return m_d->manager;
+ m_d->roomId = roomId;
+ Q_EMIT roomIdChanged();
}
-void KazvIO::setManager(KazvIOManager *manager)
+QUrl KazvIO::localFileUrl()
{
- m_d->manager = manager;
- Q_EMIT managerChanged();
+ return m_d->localFileUrl;
+}
+
+void KazvIO::setLocalFileUrl(QUrl localFileUrl)
+{
+ m_d->localFileUrl = localFileUrl;
+ Q_EMIT localFileUrlChanged();
}
QString KazvIO::serverUrl()
{
return m_d->serverUrl;
}
void KazvIO::setServerUrl(QString serverUrl)
{
m_d->serverUrl = serverUrl;
Q_EMIT serverUrlChanged();
}
-QString KazvIO::token()
-{
- return m_d->token;
-}
-
-void KazvIO::setToken(QString token)
+QString KazvIO::mxcUri()
{
- m_d->token = token;
- Q_EMIT tokenChanged();
+ return m_d->mxcUri;
}
-QString KazvIO::mxcUri()
+float KazvIO::progress()
{
- return m_d->mxcUri;
+ return m_d->progress;
}
void KazvIO::updatePercent(KJob * /* job */, unsigned long percent)
{
m_d->progress = 1.0 * percent / 100;
Q_EMIT progressChanged();
}
void KazvIO::checkResult(KJob *job)
{
if (job->error()) {
Q_EMIT failure();
} else {
Q_EMIT success();
}
}
void KazvIO::clearPercent()
{
m_d->progress = 0;
}
void KazvIO::setMxcUri(KJob *job)
{
/* TODO: get the mxcUri from the response, which requires me to understand how to get the response */
}
diff --git a/src/kazv-io.hpp b/src/kazv-io.hpp
index 11e365c..57acaac 100644
--- a/src/kazv-io.hpp
+++ b/src/kazv-io.hpp
@@ -1,113 +1,116 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2022 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include "kazv-io-manager.hpp"
#include <KIO/Job>
#include <QObject>
#include <QUrl>
#include <QtQml>
#include <memory>
struct KazvIOPrivate;
class KazvIO : public QObject {
Q_OBJECT
QML_ELEMENT
- /*
- * Progress in decimal form, from 0 to 1
- */
- Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
- /*
- * url of the file to download or upload.
- */
- Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
- /*
- * The KazvIOManager of the application,
- * KazvIO will look for the download job determined by the url attribute in the manager,
- * and perform operations such as pausing, canceling, etc.
+
+ /**
+ * Global manager for kazvIO.
*/
Q_PROPERTY(KazvIOManager *manager READ manager WRITE setManager NOTIFY managerChanged)
+ Q_PROPERTY(QString roomId READ roomId WRITE setRoomId NOTIFY roomIdChanged)
+ Q_PROPERTY(QUrl localFileUrl READ localFileUrl WRITE setLocalFileUrl NOTIFY localFileUrlChanged)
Q_PROPERTY(QString serverUrl READ serverUrl WRITE setServerUrl NOTIFY serverUrlChanged)
- Q_PROPERTY(QString token READ token WRITE setToken NOTIFY tokenChanged)
- Q_PROPERTY(QString mxcUri READ mxcUri)
+ Q_PROPERTY(QString mxcUri READ mxcUri NOTIFY mxcUriChanged)
+ /**
+ * From zero to one
+ */
+ Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
private:
std::unique_ptr<KazvIOPrivate> m_d;
void connectJob(QPointer<KJob> job);
public:
explicit KazvIO(QObject *parent = 0);
~KazvIO() override;
/**
* QML should call this function to initiate a download.
* url is the file to download.
* localFileUrl is the file to store.
*/
- Q_INVOKABLE void download(QUrl localFileUrl);
+ Q_INVOKABLE void download();
/**
* Upload file to server, return mxcUri
*/
- Q_INVOKABLE QString upload();
+ Q_INVOKABLE void upload();
/**
* Pause the download.
*/
Q_INVOKABLE void suspend();
/**
* Resume the download.
*/
Q_INVOKABLE void resume();
/**
* Cancel the download.
*/
Q_INVOKABLE void cancel();
/**
* Check if a download/upload task is in progress
*/
Q_INVOKABLE void checkJob();
- float progress();
- QUrl url();
- void setUrl(QUrl url);
KazvIOManager *manager();
void setManager(KazvIOManager *manager);
+ QString roomId();
+ void setRoomId(QString roomId);
+ QUrl localFileUrl();
+ void setLocalFileUrl(QUrl localFileUrl);
QString serverUrl();
void setServerUrl(QString serverUrl);
- QString token();
- void setToken(QString token);
QString mxcUri();
-
+ float progress();
+
Q_SIGNALS:
- /*
+
+ /**
+ * Sending this signal means that the job has been destroyed and the download or upload task has ended.
+ * This signal is always emitted once, whether it is a normal exit or an error.
+ */
+ void result();
+ void success();
+ void failure();
+
+ /**
* When KazvIO is created, it will check if the url it manages is being downloaded or uploading,
* and if it is, then it will send this signal.
*/
void processing();
+ /**
+ * This signal is emitted if the upload or download tasks it manages have been paused when KazvIO was created
+ */
void suspended();
- void progressChanged();
- void urlChanged();
+
void managerChanged();
+ void roomIdChanged();
+ void localFileUrlChanged();
void serverUrlChanged();
- void tokenChanged();
- /*
- * Sending this signal means that the job has been destroyed and the download or upload task has ended.
- * This signal is always emitted once, whether it is a normal exit or an error.
- */
- void result();
- void success();
- void failure();
+ void mxcUriChanged();
+ void progressChanged();
private Q_SLOTS:
void updatePercent(KJob *job, unsigned long percent);
void checkResult(KJob *job);
void clearPercent();
void setMxcUri(KJob *job);
};

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 19, 8:34 PM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55440
Default Alt Text
(17 KB)

Event Timeline