KNewStuff

filecopyworker.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 "filecopyworker.h"
8
9#include <KLocalizedString>
10#include <QFile>
11
12using namespace std::chrono_literals;
13using namespace KNSCore;
14
15class KNSCore::FileCopyWorkerPrivate
16{
17public:
18 FileCopyWorkerPrivate()
19 {
20 }
21 QFile source;
22 QFile destination;
23};
24
25FileCopyWorker::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
33FileCopyWorker::~FileCopyWorker()
34{
35 if (isRunning()) {
37 quit();
38 if (!wait(1s)) {
39 terminate();
40 wait();
41 }
42 }
43}
44
45void FileCopyWorker::run()
46{
47 if (d->source.open(QIODevice::ReadOnly)) {
48 if (d->destination.open(QIODevice::WriteOnly)) {
49 const qint64 totalSize = d->source.size();
50
51 for (qint64 i = 0; i < totalSize; i += 1024) {
52 d->destination.write(d->source.read(1024));
53 d->source.seek(i);
54 d->destination.seek(i);
55
56 Q_EMIT progress(i, 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"
QString i18n(const char *text, const TYPE &arg...)
Q_EMITQ_EMIT
bool isRunning() const const
void quit()
void requestInterruption()
void terminate()
bool wait(QDeadlineTimer deadline)
QString toLocalFile() 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.