Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140078
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
8 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/contents/ui/MediaFileMenu.qml b/src/contents/ui/MediaFileMenu.qml
index d1b3224..f85897e 100644
--- a/src/contents/ui/MediaFileMenu.qml
+++ b/src/contents/ui/MediaFileMenu.qml
@@ -1,51 +1,51 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2022-2023 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import QtQuick 2.15
import QtQuick.Controls 2.15
import Qt.labs.platform 1.1 as Platform
import org.kde.kirigami 2.13 as Kirigami
import '.' as Types
QtObject {
id: mediaFileMenu
required property var serverUrl
required property var jobId
required property var fileName
signal startDownload
property var viewAction: Kirigami.Action {
text: l10n.get('media-file-menu-option-view')
// shortcut: StandardKey.Open
// TODO: open with default program
}
property var saveAction: Kirigami.Action {
text: l10n.get('media-file-menu-option-save-as')
// Can't think of a suitable shortcut key
// shortcut: StandardKey.Save
onTriggered: {
fileDialog.open()
}
}
property var optionMenu: [viewAction, saveAction]
property var fileDialog: Platform.FileDialog {
fileMode: Platform.FileDialog.SaveFile
folder: Platform.StandardPaths.writableLocation(Platform.StandardPaths.DownloadLocation)
// This is not work
// currentFile: fileName
onAccepted: {
- kazvIOManager.saveFile(mediaFileMenu.serverUrl, file, mediaFileMenu.jobId)
+ kazvIOManager.startNewDownloadJob(mediaFileMenu.serverUrl, file, mediaFileMenu.jobId)
startDownload()
}
}
}
diff --git a/src/kazv-io-manager.cpp b/src/kazv-io-manager.cpp
index 5b06834..788fd59 100644
--- a/src/kazv-io-manager.cpp
+++ b/src/kazv-io-manager.cpp
@@ -1,143 +1,125 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2022-2023 nannanko <nannanko@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "kazv-io-manager.hpp"
#include "kazv-io-job.hpp"
#include "matrix-room.hpp"
#include "upload-job-model.hpp"
#include <api/csapi/content-repo.hpp>
#include <file-desc.hpp>
#include <QObject>
#include <QIODevice>
#include <QFile>
#include <QSaveFile>
#include <QPointer>
#include <QSharedPointer>
#include <QUrl>
#include <QMap>
#include <QMultiMap>
#include <QTemporaryDir>
#include <string>
static const QString ROOMLESS = QStringLiteral("not-a-room");
struct KazvIOManagerPrivate
{
QTemporaryDir cacheDir;
QMap<QString, QSharedPointer<KazvIODownloadJob>> downloadJobs;
QMap<QString, QSharedPointer<UploadJobModel>> uploadJobs;
};
KazvIOManager::KazvIOManager(QObject *parent)
: QObject(parent)
, m_d(new KazvIOManagerPrivate)
{
}
KazvIOManager::~KazvIOManager() = default;
QUrl KazvIOManager::cacheFile(const QUrl &fileUrl, const QString &id)
{
const QString filePath = m_d->cacheDir.filePath(id);
QDir dir(m_d->cacheDir.path());
if (!dir.exists(id)) {
auto job = this->startNewDownloadJob(fileUrl, QUrl::fromLocalFile(filePath), id);
connect(job, &KazvIOBaseJob::result, this,
[this, id](auto ec) {
if (ec == KazvIOBaseJob::NoError) {
m_d->downloadJobs.remove(id);
}
});
}
return QUrl::fromLocalFile(filePath);
}
-KazvIOBaseJob *KazvIOManager::saveFile(const QUrl &serverFileUrl, const QUrl &localFileUrl, const QString &id)
-{
- QUrl fileUrl = this->cacheFile(serverFileUrl, id);
- auto job = this->getDownloadJob(id);
- if (job == nullptr) {
- QFile file(fileUrl.toLocalFile());
- file.copy(localFileUrl.toLocalFile());
- } else {
- connect(job, &KazvIOBaseJob::result, this, [this, fileUrl, localFileUrl](auto ec) {
- if (ec == KazvIOBaseJob::NoError) {
- QFile file(fileUrl.toLocalFile());
- file.copy(localFileUrl.toLocalFile());
- }
- });
- }
- return job;
-}
-
KazvIOBaseJob *KazvIOManager::startNewDownloadJob(const QUrl &serverUrl, const QUrl &localFileUrl, const QString &jobId)
{
auto downloadJob = QSharedPointer<KazvIODownloadJob>(
new KazvIODownloadJob(localFileUrl.toLocalFile(), serverUrl, std::nullopt, this));
m_d->downloadJobs[jobId] = downloadJob;
return downloadJob.data();
}
KazvIOBaseJob *KazvIOManager::startNewUploadJob(const QUrl &serverUrl, const QUrl &localFileUrl, const QString &token, const QString &roomId, MatrixRoomList *roomList)
{
auto uploadJob = QPointer<KazvIOUploadJob>(
new KazvIOUploadJob(localFileUrl.toLocalFile(), serverUrl, roomList, roomId, token, std::nullopt,this));
if (!m_d->uploadJobs.contains(roomId) || m_d->uploadJobs[roomId].isNull()) {
m_d->uploadJobs[roomId].reset(new UploadJobModel(this));
}
m_d->uploadJobs[roomId]->addJob(uploadJob);
return uploadJob.data();
}
KazvIOBaseJob *KazvIOManager::startNewRoomlessUploadJob(const QUrl &serverUrl, const QUrl &localFileUrl, const QString &token)
{
return startNewUploadJob(serverUrl, localFileUrl, token, ROOMLESS, 0);
}
KazvIOBaseJob *KazvIOManager::getDownloadJob(const QString &jobId)
{
auto targetJob = m_d->downloadJobs.find(jobId);
if (targetJob == m_d->downloadJobs.end()) {
return nullptr;
}
return targetJob.value().data();
}
void KazvIOManager::deleteDownloadJob(const QString &jobId)
{
m_d->downloadJobs.remove(jobId);
}
UploadJobModel *KazvIOManager::getUploadJobs(const QString &roomId)
{
if (!m_d->uploadJobs.contains(roomId) || m_d->uploadJobs[roomId].isNull()) {
m_d->uploadJobs[roomId].reset(new UploadJobModel(this));
}
return m_d->uploadJobs[roomId].data();
}
void KazvIOManager::deleteUploadJob(const QString &roomId, KazvIOBaseJob *job)
{
m_d->uploadJobs[roomId]->removeJob(QPointer<KazvIOUploadJob>(qobject_cast<KazvIOUploadJob*>(job)));
}
void KazvIOManager::deleteRoomlessUploadJob(KazvIOBaseJob *job)
{
deleteUploadJob(ROOMLESS, job);
}
void KazvIOManager::deleteModelIfEmpty(const QString &roomId)
{
if (!m_d->uploadJobs.contains(roomId)) {
return;
}
if (m_d->uploadJobs[roomId]->rowCount() == 0) {
m_d->uploadJobs.remove(roomId);
}
}
diff --git a/src/kazv-io-manager.hpp b/src/kazv-io-manager.hpp
index 91b1770..52ec387 100644
--- a/src/kazv-io-manager.hpp
+++ b/src/kazv-io-manager.hpp
@@ -1,50 +1,49 @@
/*
* 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 "kazv-io-job.hpp"
#include "matrix-room.hpp"
#include "upload-job-model.hpp"
#include "matrix-room-list.hpp"
#include <QObject>
#include <QtQml>
#include <QUrl>
#include <QString>
#include <KIO/TransferJob>
#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;
/**
* Cache the file specified by fileUrl into a temporary folder
* Returns the local url of the cached file.
*/
Q_INVOKABLE QUrl cacheFile(const QUrl &fileUrl, const QString &id);
- Q_INVOKABLE KazvIOBaseJob *saveFile(const QUrl &serverFileUrl, const QUrl &localFileUrl, const QString &id);
Q_INVOKABLE KazvIOBaseJob *startNewDownloadJob(const QUrl &serverUrl, const QUrl &localFileName, const QString &jobId);
Q_INVOKABLE KazvIOBaseJob *startNewUploadJob(const QUrl &serverUrl, const QUrl &localFileName, const QString &token, const QString &roomId, MatrixRoomList *roomList);
Q_INVOKABLE KazvIOBaseJob *startNewRoomlessUploadJob(const QUrl &serverUrl, const QUrl &localFileName, const QString &token);
Q_INVOKABLE KazvIOBaseJob *getDownloadJob(const QString &jobId);
Q_INVOKABLE void deleteDownloadJob(const QString &jobId);
Q_INVOKABLE UploadJobModel *getUploadJobs(const QString &roomId);
Q_INVOKABLE void deleteUploadJob(const QString &roomId, KazvIOBaseJob *job);
Q_INVOKABLE void deleteRoomlessUploadJob(KazvIOBaseJob *job);
Q_INVOKABLE void deleteModelIfEmpty(const QString &roomId);
};
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Jan 19, 12:01 PM (2 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55148
Default Alt Text
(8 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment