Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140434
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/kazv-io-manager.cpp b/src/kazv-io-manager.cpp
index 7348618..af3ca4a 100644
--- a/src/kazv-io-manager.cpp
+++ b/src/kazv-io-manager.cpp
@@ -1,108 +1,109 @@
/*
* 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 {
QPointer<KIO::TransferJob> job;
QPointer<QSaveFile> file;
};
struct KazvIOManagerPrivate {
QVector<KazvIODownloadJob> jobs;
};
KazvIOManager::KazvIOManager(QObject *parent)
: QObject(parent)
, m_d(new KazvIOManagerPrivate)
{
}
KazvIOManager::~KazvIOManager() = default;
QPointer<KIO::TransferJob> KazvIOManager::startNewDownloadJob(QUrl &url, 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;
}
QPointer<KIO::TransferJob> KazvIOManager::getJob(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; });
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);
}
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);
}
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.erase(targetJob);
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Jan 19, 8:54 PM (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55446
Default Alt Text
(3 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment