| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "filecopyjob.h" |
| 8 | |
| 9 | #include "downloadjob.h" |
| 10 | #include "filecopyworker.h" |
| 11 | |
| 12 | #include "knewstuffcore_debug.h" |
| 13 | |
| 14 | using namespace KNSCore; |
| 15 | |
| 16 | class KNSCore::FileCopyJobPrivate |
| 17 | { |
| 18 | public: |
| 19 | QUrl source; |
| 20 | QUrl destination; |
| 21 | int permissions = -1; |
| 22 | JobFlags flags = DefaultFlags; |
| 23 | |
| 24 | FileCopyWorker *worker = nullptr; |
| 25 | }; |
| 26 | |
| 27 | FileCopyJob::FileCopyJob(const QUrl &source, const QUrl &destination, int permissions, JobFlags flags, QObject *parent) |
| 28 | : KJob(parent) |
| 29 | , d(new FileCopyJobPrivate) |
| 30 | { |
| 31 | d->source = source; |
| 32 | d->destination = destination; |
| 33 | d->permissions = permissions; |
| 34 | d->flags = flags; |
| 35 | } |
| 36 | |
| 37 | FileCopyJob::FileCopyJob(QObject *parent) |
| 38 | : KJob(parent) |
| 39 | , d(new FileCopyJobPrivate) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | FileCopyJob::~FileCopyJob() = default; |
| 44 | |
| 45 | void FileCopyJob::start() |
| 46 | { |
| 47 | if (d->worker) { |
| 48 | // already started... |
| 49 | return; |
| 50 | } |
| 51 | d->worker = new FileCopyWorker(d->source, d->destination, this); |
| 52 | connect(sender: d->worker, signal: &FileCopyWorker::progress, context: this, slot: &FileCopyJob::handleProgressUpdate); |
| 53 | connect(sender: d->worker, signal: &FileCopyWorker::completed, context: this, slot: &FileCopyJob::handleCompleted); |
| 54 | connect(sender: d->worker, signal: &FileCopyWorker::error, context: this, slot: &FileCopyJob::handleError); |
| 55 | d->worker->start(); |
| 56 | } |
| 57 | |
| 58 | QUrl FileCopyJob::destUrl() const |
| 59 | { |
| 60 | return d->destination; |
| 61 | } |
| 62 | |
| 63 | QUrl FileCopyJob::srcUrl() const |
| 64 | { |
| 65 | return d->source; |
| 66 | } |
| 67 | |
| 68 | FileCopyJob *FileCopyJob::file_copy(const QUrl &source, const QUrl &destination, int permissions, JobFlags flags, QObject *parent) |
| 69 | { |
| 70 | FileCopyJob *job = nullptr; |
| 71 | if (source.isLocalFile() && destination.isLocalFile()) { |
| 72 | qCDebug(KNEWSTUFFCORE) << "File copy job is local only" ; |
| 73 | job = new FileCopyJob(source, destination, permissions, flags, parent); |
| 74 | } else { |
| 75 | qCDebug(KNEWSTUFFCORE) << "File copy job is from (or to) a remote URL" ; |
| 76 | job = new DownloadJob(source, destination, permissions, flags, parent); |
| 77 | } |
| 78 | job->start(); |
| 79 | return job; |
| 80 | } |
| 81 | |
| 82 | void FileCopyJob::handleProgressUpdate(qlonglong current, qlonglong total) |
| 83 | { |
| 84 | setTotalAmount(unit: KJob::Bytes, amount: total); |
| 85 | setProcessedAmount(unit: KJob::Bytes, amount: current); |
| 86 | emitPercent(processedAmount: current, totalAmount: total); |
| 87 | } |
| 88 | |
| 89 | void FileCopyJob::handleCompleted() |
| 90 | { |
| 91 | d->worker->deleteLater(); |
| 92 | d->worker = nullptr; |
| 93 | emitResult(); |
| 94 | } |
| 95 | |
| 96 | void FileCopyJob::handleError(const QString &errorMessage) |
| 97 | { |
| 98 | d->worker->deleteLater(); |
| 99 | d->worker = nullptr; |
| 100 | setError(UserDefinedError); |
| 101 | setErrorText(errorMessage); |
| 102 | emitResult(); |
| 103 | } |
| 104 | |
| 105 | #include "moc_filecopyjob.cpp" |
| 106 | |