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 FileCopyJob::Private
17 {
18 public:
19 QUrl source;
20 QUrl destination;
21 int permissions = -1;
22 JobFlags flags = DefaultFlags;
23
24 FileCopyWorker *worker = nullptr;
25 };
26
FileCopyJob(const QUrl & source,const QUrl & destination,int permissions,JobFlags flags,QObject * parent)27 FileCopyJob::FileCopyJob(const QUrl &source, const QUrl &destination, int permissions, JobFlags flags, QObject *parent)
28 : KJob(parent)
29 , d(new Private)
30 {
31 d->source = source;
32 d->destination = destination;
33 d->permissions = permissions;
34 d->flags = flags;
35 }
36
FileCopyJob(QObject * parent)37 FileCopyJob::FileCopyJob(QObject *parent)
38 : KJob(parent)
39 , d(new Private)
40 {
41 }
42
~FileCopyJob()43 FileCopyJob::~FileCopyJob()
44 {
45 delete d;
46 }
47
start()48 void FileCopyJob::start()
49 {
50 if (d->worker) {
51 // already started...
52 return;
53 }
54 d->worker = new FileCopyWorker(d->source, d->destination, this);
55 connect(d->worker, &FileCopyWorker::progress, this, &FileCopyJob::handleProgressUpdate);
56 connect(d->worker, &FileCopyWorker::completed, this, &FileCopyJob::handleCompleted);
57 connect(d->worker, &FileCopyWorker::error, this, &FileCopyJob::handleError);
58 d->worker->start();
59 }
60
destUrl() const61 QUrl FileCopyJob::destUrl() const
62 {
63 return d->destination;
64 }
65
srcUrl() const66 QUrl FileCopyJob::srcUrl() const
67 {
68 return d->source;
69 }
70
file_copy(const QUrl & source,const QUrl & destination,int permissions,JobFlags flags,QObject * parent)71 FileCopyJob *FileCopyJob::file_copy(const QUrl &source, const QUrl &destination, int permissions, JobFlags flags, QObject *parent)
72 {
73 FileCopyJob *job = nullptr;
74 if (source.isLocalFile() && destination.isLocalFile()) {
75 qCDebug(KNEWSTUFFCORE) << "File copy job is local only";
76 job = new FileCopyJob(source, destination, permissions, flags, parent);
77 } else {
78 qCDebug(KNEWSTUFFCORE) << "File copy job is from (or to) a remote URL";
79 job = new DownloadJob(source, destination, permissions, flags, parent);
80 }
81 job->start();
82 return job;
83 }
84
handleProgressUpdate(qlonglong current,qlonglong total)85 void FileCopyJob::handleProgressUpdate(qlonglong current, qlonglong total)
86 {
87 setTotalAmount(KJob::Bytes, total);
88 setProcessedAmount(KJob::Bytes, current);
89 emitPercent(current, total);
90 }
91
handleCompleted()92 void FileCopyJob::handleCompleted()
93 {
94 d->worker->deleteLater();
95 d->worker = nullptr;
96 emitResult();
97 }
98
handleError(const QString & errorMessage)99 void FileCopyJob::handleError(const QString &errorMessage)
100 {
101 d->worker->deleteLater();
102 d->worker = nullptr;
103 setError(UserDefinedError);
104 setErrorText(errorMessage);
105 emitResult();
106 }
107