[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / process_copy.cpp
blob16d18ab4641f275392294f992c14be27a62cb6de
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "process_copy.h"
22 #include "progresswidget.h"
23 #include <QEventLoop>
24 #include <QFile>
25 #include <QMessageBox>
26 #include <QTimer>
28 #define BLKSIZE 512
30 CopyProcess::CopyProcess(const QString &source, const QString &destination, ProgressWidget *progress):
31 progress(progress),
32 source(source),
33 destination(destination),
34 result(true)
38 bool CopyProcess::run()
40 progress->lock(true);
41 // progress->setInfo(tr("Copying file..."));
43 QEventLoop loop;
44 connect(this, SIGNAL(finished()), &loop, SLOT(quit()));
45 QTimer::singleShot(500, this, SLOT(onTimer()));
46 loop.exec();
48 return result;
51 void CopyProcess::onTimer()
53 char buf[BLKSIZE];
55 QFile sourceFile(source);
56 int blocks = (sourceFile.size() + BLKSIZE - 1) / BLKSIZE;
57 progress->setMaximum(blocks-1);
59 if (sourceFile.open(QIODevice::ReadOnly)) {
60 QFile destinationFile(destination);
61 if (destinationFile.open(QIODevice::ReadWrite)) {
62 // progress->addText(tr("Writing file: "));
63 for (int i=0; i<blocks; i++) {
64 int read = sourceFile.read(buf, BLKSIZE);
65 if (destinationFile.write(buf, read) == read) {
66 destinationFile.flush();
67 progress->setValue(i);
69 else {
70 QMessageBox::warning(NULL, tr("Error"), tr("Write error"));
71 result = false;
72 break;
75 destinationFile.close();
77 else {
78 QMessageBox::warning(NULL, tr("Error"),tr("Cannot write %1 (reason: %2)").arg(destinationFile.fileName()).arg(sourceFile.errorString()));
79 result = false;
82 else {
83 QMessageBox::warning(NULL, tr("Error"),tr("Cannot open %1 (reason: %2)").arg(sourceFile.fileName()).arg(sourceFile.errorString()));
84 result = false;
87 sourceFile.close();
89 progress->lock(false);
90 emit finished();