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 KNSCore;
13
14class KNSCore::FileCopyWorkerPrivate
15{
16public:
17 FileCopyWorkerPrivate()
18 {
19 }
20 QFile source;
21 QFile destination;
22};
23
24FileCopyWorker::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
32FileCopyWorker::~FileCopyWorker() = default;
33
34void FileCopyWorker::run()
35{
36 if (d->source.open(QIODevice::ReadOnly)) {
37 if (d->destination.open(QIODevice::WriteOnly)) {
38 const qint64 totalSize = d->source.size();
39
40 for (qint64 i = 0; i < totalSize; i += 1024) {
41 d->destination.write(d->source.read(1024));
42 d->source.seek(i);
43 d->destination.seek(i);
44
45 Q_EMIT progress(i, 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"
QString i18n(const char *text, const TYPE &arg...)
Q_EMITQ_EMIT
QString toLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:54:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.