KNewStuff

filecopyjob.cpp
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
14using namespace KNSCore;
15
16class KNSCore::FileCopyJobPrivate
17{
18public:
19 QUrl source;
20 QUrl destination;
21 int permissions = -1;
22 JobFlags flags = DefaultFlags;
23
24 FileCopyWorker *worker = nullptr;
25};
26
27FileCopyJob::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
37FileCopyJob::FileCopyJob(QObject *parent)
38 : KJob(parent)
39 , d(new FileCopyJobPrivate)
40{
41}
42
43FileCopyJob::~FileCopyJob() = default;
44
45void 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(d->worker, &FileCopyWorker::progress, this, &FileCopyJob::handleProgressUpdate);
53 connect(d->worker, &FileCopyWorker::completed, this, &FileCopyJob::handleCompleted);
54 connect(d->worker, &FileCopyWorker::error, this, &FileCopyJob::handleError);
55 d->worker->start();
56}
57
58QUrl FileCopyJob::destUrl() const
59{
60 return d->destination;
61}
62
63QUrl FileCopyJob::srcUrl() const
64{
65 return d->source;
66}
67
68FileCopyJob *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
82void FileCopyJob::handleProgressUpdate(qlonglong current, qlonglong total)
83{
86 emitPercent(current, total);
87}
88
89void FileCopyJob::handleCompleted()
90{
91 d->worker->deleteLater();
92 d->worker = nullptr;
93 emitResult();
94}
95
96void 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"
void setErrorText(const QString &errorText)
void setTotalAmount(Unit unit, qulonglong amount)
void emitResult()
void setError(int errorCode)
void emitPercent(qulonglong processedAmount, qulonglong totalAmount)
void setProcessedAmount(Unit unit, qulonglong amount)
DefaultFlags
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool isLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.