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 "filecopyworker.h" |
8 | |
9 | #include <KLocalizedString> |
10 | #include <QFile> |
11 | |
12 | using namespace KNSCore; |
13 | |
14 | class KNSCore::FileCopyWorkerPrivate |
15 | { |
16 | public: |
17 | FileCopyWorkerPrivate() |
18 | { |
19 | } |
20 | QFile source; |
21 | QFile destination; |
22 | }; |
23 | |
24 | FileCopyWorker::FileCopyWorker(const QUrl &source, const QUrl &destination, QObject *parent) |
25 | : QThread(parent) |
26 | , d(new FileCopyWorkerPrivate) |
27 | { |
28 | d->source.setFileName(source.toLocalFile()); |
29 | d->destination.setFileName(destination.toLocalFile()); |
30 | } |
31 | |
32 | FileCopyWorker::~FileCopyWorker() = default; |
33 | |
34 | void FileCopyWorker::run() |
35 | { |
36 | if (d->source.open(flags: QIODevice::ReadOnly)) { |
37 | if (d->destination.open(flags: QIODevice::WriteOnly)) { |
38 | const qint64 totalSize = d->source.size(); |
39 | |
40 | for (qint64 i = 0; i < totalSize; i += 1024) { |
41 | d->destination.write(data: d->source.read(maxlen: 1024)); |
42 | d->source.seek(offset: i); |
43 | d->destination.seek(offset: i); |
44 | |
45 | Q_EMIT progress(current: i, total: totalSize / 1024); |
46 | } |
47 | Q_EMIT completed(); |
48 | } else { |
49 | Q_EMIT error(i18n("Could not open %1 for writing", d->destination.fileName())); |
50 | } |
51 | } else { |
52 | Q_EMIT error(i18n("Could not open %1 for reading", d->source.fileName())); |
53 | } |
54 | } |
55 | |
56 | #include "moc_filecopyworker.cpp" |
57 |