Page MenuHomePhorge

kazv-io-job.hpp
No OneTemporary

Size
4 KB
Referenced Files
None
Subscribers
None

kazv-io-job.hpp

/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2022-2023 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include "matrix-room.hpp"
#include <QObject>
#include <QPointer>
#include <QSaveFile>
#include <QFile>
#include <QString>
#include <QByteArray>
#include <QUrl>
#include <KIO/TransferJob>
#include <memory>
#include <optional>
struct KazvIOBaseJobPrivate;
/**
* This is considered for an object to perform a job only once,
* but it is also designed to be able to use an object to perform multiple job (just when the previous job has ended.)
* to deal with possible future needs.
* Resetting file, job, etc. will reset the object to prepare for the next job.
* As a suggestion, make sure the file is opened normally before calling setJob to avoid strange problems.
*/
class KazvIOBaseJob : public QObject
{
Q_OBJECT
Q_PROPERTY(QPointer<KIO::TransferJob> job READ job WRITE setJob NOTIFY jobChanged)
Q_PROPERTY(float progress READ progress NOTIFY progressChanged)
std::unique_ptr<KazvIOBaseJobPrivate> m_d;
public:
enum ErrorCode {
/**
* No Error or the signal result has not been emitted yet.
*/
NoError = 0,
/**
* The user actively canceled the job.
*/
UserCancel,
/**
* Errors caused by various reasons such as file read and write failures, open failures, etc.
*/
QtError,
/**
* Errors caused by KIO, such network error.
*/
KIOError,
/**
* Errors caused by bad code, means the bug is somewhere.
*/
KazvError
};
Q_ENUM(ErrorCode)
KazvIOBaseJob(QObject *parent = 0);
~KazvIOBaseJob();
QPointer<KIO::TransferJob> job();
/**
* Before calling this function, make sure to use the setFile of the subclass to set a file that has been opened normally
*/
void setJob(QPointer<KIO::TransferJob> job);
float progress();
/**
* Only use it after emitted the result signal
*/
Q_INVOKABLE void suspend();
Q_INVOKABLE void resume();
Q_INVOKABLE void cancel();
Q_INVOKABLE bool isSuspended();
Q_INVOKABLE bool isResulted();
Q_INVOKABLE ErrorCode error();
Q_SIGNALS:
void jobChanged();
void progressChanged();
void result(ErrorCode ec);
protected Q_SLOTS:
virtual void connectJob();
protected:
/**
* If m_d->job needs to be killed before this function is called, then this function should be called first
* Unless you know what you are doing, it is recommended to use the isResulted function to check whether
* the object has emitted a result signal before calling this function
*/
void emitResult(ErrorCode ec);
void resetResult();
};
struct KazvIODownloadJobPrivate;
class KazvIODownloadJob : public KazvIOBaseJob
{
Q_OBJECT
Q_PROPERTY(QPointer<QSaveFile> file READ file WRITE setFile NOTIFY fileChanged)
std::unique_ptr<KazvIODownloadJobPrivate> m_d;
public:
KazvIODownloadJob(QObject *parent = 0);
~KazvIODownloadJob();
QPointer<QSaveFile> file();
void setFile(QPointer<QSaveFile> file);
Q_INVOKABLE QString fileName();
Q_SIGNALS:
void fileChanged();
protected Q_SLOTS:
void connectJob() override;
protected:
void emitResult(ErrorCode ec);
void writeFile(KJob *job, const QByteArray &data);
void closeFile(KJob *job);
};
struct KazvIOUploadJobPrivate;
class KazvIOUploadJob : public KazvIOBaseJob
{
Q_OBJECT
Q_PROPERTY(QPointer<QFile> file READ file WRITE setFile NOTIFY fileChanged)
Q_PROPERTY(QString response READ response NOTIFY responseChanged)
Q_PROPERTY(QPointer<MatrixRoom> room READ room WRITE setRoom NOTIFY roomChanged)
Q_PROPERTY(QUrl localFileUrl READ localFileUrl WRITE setLocalFileUrl NOTIFY localFileUrlChanged)
std::unique_ptr<KazvIOUploadJobPrivate> m_d;
public:
KazvIOUploadJob(QObject *parent = 0);
~KazvIOUploadJob();
QPointer<QFile> file();
void setFile(QPointer<QFile> file);
QString response();
QPointer<MatrixRoom> room();
void setRoom(QPointer<MatrixRoom> room);
QUrl localFileUrl();
void setLocalFileUrl(QUrl localFileUrl);
Q_INVOKABLE QString fileName();
Q_SIGNALS:
void fileChanged();
void responseChanged();
void roomChanged();
void localFileUrlChanged();
protected Q_SLOTS:
void connectJob() override;
void writeFile(KJob *job, const QByteArray &data);
void closeFile(KJob *job);
};

File Metadata

Mime Type
text/x-c++
Expires
Thu, Oct 2, 4:52 AM (18 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
487343
Default Alt Text
kazv-io-job.hpp (4 KB)

Event Timeline