KNewStuff

filecopyjob.cpp
1 /*
2  Copyright (C) 2016 Dan Leinir Turthra Jensen <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "filecopyjob.h"
19 
20 #include "downloadjob.h"
21 #include "filecopyworker.h"
22 
23 #include "knewstuffcore_debug.h"
24 
25 using namespace KNSCore;
26 
27 class FileCopyJob::Private
28 {
29 public:
30  Private()
31  : permissions(-1)
32  , flags(DefaultFlags)
33  , worker(nullptr)
34  {}
35  QUrl source;
36  QUrl destination;
37  int permissions;
38  JobFlags flags;
39 
40  FileCopyWorker* worker;
41 };
42 
43 FileCopyJob::FileCopyJob(const QUrl& source, const QUrl& destination, int permissions, JobFlags flags, QObject* parent)
44  : KJob(parent)
45  , d(new Private)
46 {
47  d->source = source;
48  d->destination = destination;
49  d->permissions = permissions;
50  d->flags = flags;
51 }
52 
53 FileCopyJob::FileCopyJob(QObject* parent)
54  : KJob(parent)
55  , d(new Private)
56 {
57 }
58 
59 FileCopyJob::~FileCopyJob()
60 {
61  delete d;
62 }
63 
64 void FileCopyJob::start()
65 {
66  if(d->worker) {
67  // already started...
68  return;
69  }
70  d->worker = new FileCopyWorker(d->source, d->destination, this);
71  connect(d->worker, &FileCopyWorker::progress, this, &FileCopyJob::handleProgressUpdate);
72  connect(d->worker, &FileCopyWorker::completed, this, &FileCopyJob::handleCompleted);
73  d->worker->start();
74 }
75 
76 QUrl FileCopyJob::destUrl() const
77 {
78  return d->destination;
79 }
80 
81 QUrl FileCopyJob::srcUrl() const
82 {
83  return d->source;
84 }
85 
86 FileCopyJob* FileCopyJob::file_copy(const QUrl& source, const QUrl& destination, int permissions, JobFlags flags, QObject* parent)
87 {
88  FileCopyJob* job = nullptr;
89  if(source.isLocalFile() && destination.isLocalFile()) {
90  qCDebug(KNEWSTUFFCORE) << "File copy job is local only";
91  job = new FileCopyJob(source, destination, permissions, flags, parent);
92  }
93  else {
94  qCDebug(KNEWSTUFFCORE) << "File copy job is from (or to) a remote URL";
95  job = new DownloadJob(source, destination, permissions, flags, parent);
96  }
97  job->start();
98  return job;
99 }
100 
101 void FileCopyJob::handleProgressUpdate(qlonglong current, qlonglong total)
102 {
103  setTotalAmount(KJob::Bytes, total);
104  setProcessedAmount(KJob::Bytes, current);
105  emitPercent(current, total);
106 }
107 
108 void FileCopyJob::handleCompleted()
109 {
110  d->worker->deleteLater();
111  d->worker = nullptr;
112  emitResult();
113 }
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-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.