Fix doc path
[opentx.git] / companion / src / downloaddialog.cpp
blobe422c3a8511edb1b81c91764c731af5a79971f80
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 "constants.h"
24 #include "helpers.h"
26 #include <QTime>
28 downloadDialog::downloadDialog(QWidget *parent, QString src, QString tgt):
29 QDialog(parent),
30 ui(new Ui::downloadDialog),
31 file(NULL)
33 ui->setupUi(this);
34 setWindowIcon(CompanionIcon("fwpreferences.png"));
35 ui->progressBar->setValue(1);
36 ui->progressBar->setMinimum(0);
37 ui->progressBar->setMaximum(0);
39 if (tgt.isEmpty()) {
40 setWindowTitle(src);
41 return; // just show wait dialog.
44 file = new QFile(tgt);
45 if (!file->open(QIODevice::WriteOnly)) {
46 QMessageBox::critical(this, CPN_STR_APP_NAME,
47 tr("Unable to save the file %1: %2.")
48 .arg(tgt).arg(file->errorString()));
49 QTimer::singleShot(0, this, SLOT(fileError()));
51 else {
52 reply = qnam.get(QNetworkRequest(QUrl(src)));
53 connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
54 connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
55 connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDataReadProgress(qint64,qint64)));
59 downloadDialog::~downloadDialog()
61 delete ui;
62 delete file;
65 void downloadDialog::httpFinished()
67 file->flush();
68 file->close();
70 bool ok = true;
71 if (reply->error()) {
72 file->remove();
73 QMessageBox::information(this, CPN_STR_APP_NAME,
74 tr("Download failed: %1.")
75 .arg(reply->errorString()));
76 ok = false;
79 reply->deleteLater();
80 reply = 0;
81 delete file;
82 file = NULL;
84 if (ok)
85 accept();
86 else
87 reject();
90 void downloadDialog::httpReadyRead()
92 if (file) {
93 file->write(reply->readAll());
97 void downloadDialog::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
99 ui->progressBar->setMaximum(totalBytes);
100 ui->progressBar->setValue(bytesRead);
103 void downloadDialog::fileError()
105 delete file;
106 file = NULL;
107 reject();
110 #if 0
111 void downloadDialog::closeEvent( QCloseEvent * event)
113 // Delay closing 2 seconds to avoid unpleasant flashing download dialogs
114 QTime closeTime= QTime::currentTime().addSecs(2);
115 while( QTime::currentTime() < closeTime )
116 QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
118 event->accept();
120 #endif