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"
28 downloadDialog::downloadDialog(QWidget
*parent
, QString src
, QString tgt
):
30 ui(new Ui::downloadDialog
),
34 setWindowIcon(CompanionIcon("fwpreferences.png"));
35 ui
->progressBar
->setValue(1);
36 ui
->progressBar
->setMinimum(0);
37 ui
->progressBar
->setMaximum(0);
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()));
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()
65 void downloadDialog::httpFinished()
73 QMessageBox::information(this, CPN_STR_APP_NAME
,
74 tr("Download failed: %1.")
75 .arg(reply
->errorString()));
90 void downloadDialog::httpReadyRead()
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()
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);