Compilation fix
[opentx.git] / companion / src / downloaddialog.cpp
blobaf7bbfd85f56d0e53f2ac50a49bba4776946a138
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 "downloaddialog.h"
22 #include "ui_downloaddialog.h"
23 #include <QTime>
24 #include "helpers.h"
26 downloadDialog::downloadDialog(QWidget *parent, QString src, QString tgt):
27 QDialog(parent),
28 ui(new Ui::downloadDialog),
29 file(NULL)
31 ui->setupUi(this);
32 setWindowIcon(CompanionIcon("fwpreferences.png"));
33 ui->progressBar->setValue(1);
34 ui->progressBar->setMinimum(0);
35 ui->progressBar->setMaximum(0);
37 if (tgt.isEmpty()) {
38 setWindowTitle(src);
39 return; // just show wait dialog.
42 file = new QFile(tgt);
43 if (!file->open(QIODevice::WriteOnly)) {
44 QMessageBox::critical(this, "Companion",
45 tr("Unable to save the file %1: %2.")
46 .arg(tgt).arg(file->errorString()));
47 QTimer::singleShot(0, this, SLOT(fileError()));
49 else {
50 reply = qnam.get(QNetworkRequest(QUrl(src)));
51 connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
52 connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
53 connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDataReadProgress(qint64,qint64)));
57 downloadDialog::~downloadDialog()
59 delete ui;
60 delete file;
63 void downloadDialog::httpFinished()
65 file->flush();
66 file->close();
68 bool ok = true;
69 if (reply->error()) {
70 file->remove();
71 QMessageBox::information(this, tr("Companion"),
72 tr("Download failed: %1.")
73 .arg(reply->errorString()));
74 ok = false;
77 reply->deleteLater();
78 reply = 0;
79 delete file;
80 file = NULL;
82 if (ok)
83 accept();
84 else
85 reject();
88 void downloadDialog::httpReadyRead()
90 if (file) {
91 file->write(reply->readAll());
95 void downloadDialog::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
97 ui->progressBar->setMaximum(totalBytes);
98 ui->progressBar->setValue(bytesRead);
101 void downloadDialog::fileError()
103 delete file;
104 file = NULL;
105 reject();
108 #if 0
109 void downloadDialog::closeEvent( QCloseEvent * event)
111 // Delay closing 2 seconds to avoid unpleasant flashing download dialogs
112 QTime closeTime= QTime::currentTime().addSecs(2);
113 while( QTime::currentTime() < closeTime )
114 QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
116 event->accept();
118 #endif