KNewStuff

filecopyjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <[email protected]>
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 
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 
37 FileCopyJob::FileCopyJob(QObject* parent)
38  : KJob(parent)
39  , d(new Private)
40 {
41 }
42 
43 FileCopyJob::~FileCopyJob()
44 {
45  delete d;
46 }
47 
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  d->worker->start();
58 }
59 
60 QUrl FileCopyJob::destUrl() const
61 {
62  return d->destination;
63 }
64 
65 QUrl FileCopyJob::srcUrl() const
66 {
67  return d->source;
68 }
69 
70 FileCopyJob* FileCopyJob::file_copy(const QUrl& source, const QUrl& destination, int permissions, JobFlags flags, QObject* parent)
71 {
72  FileCopyJob* job = nullptr;
73  if(source.isLocalFile() && destination.isLocalFile()) {
74  qCDebug(KNEWSTUFFCORE) << "File copy job is local only";
75  job = new FileCopyJob(source, destination, permissions, flags, parent);
76  }
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 
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 
92 void FileCopyJob::handleCompleted()
93 {
94  d->worker->deleteLater();
95  d->worker = nullptr;
96  emitResult();
97 }
Contains the core functionality for handling interaction with NewStuff providers. ...
DefaultFlags
bool isLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.