File indexing completed on 2024-04-28 03:56:24

0001 /*
0002     SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "filecopyworker.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QFile>
0011 
0012 using namespace KNSCore;
0013 
0014 class KNSCore::FileCopyWorkerPrivate
0015 {
0016 public:
0017     FileCopyWorkerPrivate()
0018     {
0019     }
0020     QFile source;
0021     QFile destination;
0022 };
0023 
0024 FileCopyWorker::FileCopyWorker(const QUrl &source, const QUrl &destination, QObject *parent)
0025     : QThread(parent)
0026     , d(new FileCopyWorkerPrivate)
0027 {
0028     d->source.setFileName(source.toLocalFile());
0029     d->destination.setFileName(destination.toLocalFile());
0030 }
0031 
0032 FileCopyWorker::~FileCopyWorker() = default;
0033 
0034 void FileCopyWorker::run()
0035 {
0036     if (d->source.open(QIODevice::ReadOnly)) {
0037         if (d->destination.open(QIODevice::WriteOnly)) {
0038             const qint64 totalSize = d->source.size();
0039 
0040             for (qint64 i = 0; i < totalSize; i += 1024) {
0041                 d->destination.write(d->source.read(1024));
0042                 d->source.seek(i);
0043                 d->destination.seek(i);
0044 
0045                 Q_EMIT progress(i, totalSize / 1024);
0046             }
0047             Q_EMIT completed();
0048         } else {
0049             Q_EMIT error(i18n("Could not open %1 for writing", d->destination.fileName()));
0050         }
0051     } else {
0052         Q_EMIT error(i18n("Could not open %1 for reading", d->source.fileName()));
0053     }
0054 }
0055 
0056 #include "moc_filecopyworker.cpp"