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"
26 downloadDialog::downloadDialog(QWidget
*parent
, QString src
, QString tgt
):
28 ui(new Ui::downloadDialog
),
32 setWindowIcon(CompanionIcon("fwpreferences.png"));
33 ui
->progressBar
->setValue(1);
34 ui
->progressBar
->setMinimum(0);
35 ui
->progressBar
->setMaximum(0);
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()));
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()
63 void downloadDialog::httpFinished()
71 QMessageBox::information(this, tr("Companion"),
72 tr("Download failed: %1.")
73 .arg(reply
->errorString()));
88 void downloadDialog::httpReadyRead()
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()
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);