File indexing completed on 2024-04-28 03:56:24

0001 /*
0002     SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "filecopyjob.h"
0008 
0009 #include "downloadjob.h"
0010 #include "filecopyworker.h"
0011 
0012 #include "knewstuffcore_debug.h"
0013 
0014 using namespace KNSCore;
0015 
0016 class KNSCore::FileCopyJobPrivate
0017 {
0018 public:
0019     QUrl source;
0020     QUrl destination;
0021     int permissions = -1;
0022     JobFlags flags = DefaultFlags;
0023 
0024     FileCopyWorker *worker = nullptr;
0025 };
0026 
0027 FileCopyJob::FileCopyJob(const QUrl &source, const QUrl &destination, int permissions, JobFlags flags, QObject *parent)
0028     : KJob(parent)
0029     , d(new FileCopyJobPrivate)
0030 {
0031     d->source = source;
0032     d->destination = destination;
0033     d->permissions = permissions;
0034     d->flags = flags;
0035 }
0036 
0037 FileCopyJob::FileCopyJob(QObject *parent)
0038     : KJob(parent)
0039     , d(new FileCopyJobPrivate)
0040 {
0041 }
0042 
0043 FileCopyJob::~FileCopyJob() = default;
0044 
0045 void FileCopyJob::start()
0046 {
0047     if (d->worker) {
0048         // already started...
0049         return;
0050     }
0051     d->worker = new FileCopyWorker(d->source, d->destination, this);
0052     connect(d->worker, &FileCopyWorker::progress, this, &FileCopyJob::handleProgressUpdate);
0053     connect(d->worker, &FileCopyWorker::completed, this, &FileCopyJob::handleCompleted);
0054     connect(d->worker, &FileCopyWorker::error, this, &FileCopyJob::handleError);
0055     d->worker->start();
0056 }
0057 
0058 QUrl FileCopyJob::destUrl() const
0059 {
0060     return d->destination;
0061 }
0062 
0063 QUrl FileCopyJob::srcUrl() const
0064 {
0065     return d->source;
0066 }
0067 
0068 FileCopyJob *FileCopyJob::file_copy(const QUrl &source, const QUrl &destination, int permissions, JobFlags flags, QObject *parent)
0069 {
0070     FileCopyJob *job = nullptr;
0071     if (source.isLocalFile() && destination.isLocalFile()) {
0072         qCDebug(KNEWSTUFFCORE) << "File copy job is local only";
0073         job = new FileCopyJob(source, destination, permissions, flags, parent);
0074     } else {
0075         qCDebug(KNEWSTUFFCORE) << "File copy job is from (or to) a remote URL";
0076         job = new DownloadJob(source, destination, permissions, flags, parent);
0077     }
0078     job->start();
0079     return job;
0080 }
0081 
0082 void FileCopyJob::handleProgressUpdate(qlonglong current, qlonglong total)
0083 {
0084     setTotalAmount(KJob::Bytes, total);
0085     setProcessedAmount(KJob::Bytes, current);
0086     emitPercent(current, total);
0087 }
0088 
0089 void FileCopyJob::handleCompleted()
0090 {
0091     d->worker->deleteLater();
0092     d->worker = nullptr;
0093     emitResult();
0094 }
0095 
0096 void FileCopyJob::handleError(const QString &errorMessage)
0097 {
0098     d->worker->deleteLater();
0099     d->worker = nullptr;
0100     setError(UserDefinedError);
0101     setErrorText(errorMessage);
0102     emitResult();
0103 }
0104 
0105 #include "moc_filecopyjob.cpp"