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