File indexing completed on 2024-12-01 10:39:49
0001 /* 0002 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "uploadfilemanager.h" 0008 #include "connection.h" 0009 #include "rocketchataccount.h" 0010 #include "ruqola_debug.h" 0011 0012 #include <QFile> 0013 0014 int UploadFileManager::uploadIdentifier = 0; 0015 UploadFileManager::UploadFileManager(RocketChatAccount *account, QObject *parent) 0016 : QObject{parent} 0017 , mRocketChatAccount(account) 0018 { 0019 } 0020 0021 UploadFileManager::~UploadFileManager() = default; 0022 0023 int UploadFileManager::addUpload(const RocketChatRestApi::UploadFileJob::UploadFileInfo &info) 0024 { 0025 QPointer<RocketChatRestApi::UploadFileJob> job = new RocketChatRestApi::UploadFileJob(this); 0026 mRocketChatAccount->restApi()->initializeRestApiJob(job); 0027 job->setUploadFileInfo(info); 0028 uploadIdentifier++; 0029 const int jobIdentifier = uploadIdentifier; 0030 connect(job, 0031 &RocketChatRestApi::UploadFileJob::uploadProgress, 0032 this, 0033 [this, jobIdentifier](const RocketChatRestApi::UploadFileJob::UploadStatusInfo &info) { 0034 Q_EMIT uploadProgress(info, jobIdentifier, mRocketChatAccount->accountName()); 0035 }); 0036 // Need to delete temporary file. 0037 connect(job, &RocketChatRestApi::UploadFileJob::uploadFinished, this, [info, jobIdentifier, this]() { 0038 if (info.deleteTemporaryFile) { 0039 QFile f(info.filenameUrl.toLocalFile()); 0040 if (f.remove()) { 0041 qCWarning(RUQOLA_LOG) << "Impossible to delete file" << f.fileName(); 0042 } 0043 } 0044 mUploadMap.remove(jobIdentifier); 0045 }); 0046 if (!job->start()) { 0047 qCWarning(RUQOLA_LOG) << "Impossible to start UploadFileJob job"; 0048 return -1; 0049 } else { 0050 mUploadMap.insert(uploadIdentifier, job); 0051 return uploadIdentifier; 0052 } 0053 } 0054 0055 void UploadFileManager::removeFile(const RocketChatRestApi::UploadFileJob::UploadFileInfo &info) 0056 { 0057 if (info.deleteTemporaryFile) { 0058 QFile f(info.filenameUrl.toLocalFile()); 0059 if (f.remove()) { 0060 qCWarning(RUQOLA_LOG) << "Impossible to delete file" << f.fileName(); 0061 } 0062 } 0063 } 0064 0065 void UploadFileManager::cancelJob(int identifier) 0066 { 0067 auto job = mUploadMap.take(identifier); 0068 // Abort will remove job too. 0069 if (job) { 0070 removeFile(job->uploadFileInfo()); 0071 job->abort(); 0072 } 0073 } 0074 0075 #include "moc_uploadfilemanager.cpp"