Fixed offline mode handling in file downloader with the QWidget based interface
This commit is contained in:
@@ -9,6 +9,8 @@
|
|||||||
#include <QTemporaryFile>
|
#include <QTemporaryFile>
|
||||||
#ifdef QT_WIDGET
|
#ifdef QT_WIDGET
|
||||||
#include <QProgressDialog>
|
#include <QProgressDialog>
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QCoreApplication>
|
||||||
#else
|
#else
|
||||||
#include <qdeclarative.h>
|
#include <qdeclarative.h>
|
||||||
#endif //QT_WIDGET
|
#endif //QT_WIDGET
|
||||||
@@ -60,19 +62,30 @@ signals:
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void fileDownloaded(){
|
void fileDownloaded(){
|
||||||
if(m_tmp.write(m_downloadReply->readAll()) == -1) return;
|
// let's check some error
|
||||||
if(QFile(m_localPath).exists())
|
if(m_downloadReply->error() != QNetworkReply::NoError) {
|
||||||
QFile::remove(m_localPath);
|
m_state = NETWORK_ERROR;
|
||||||
|
} else {
|
||||||
|
if(m_tmp.write(m_downloadReply->readAll()) == -1) return;
|
||||||
|
if(QFile(m_localPath).exists())
|
||||||
|
QFile::remove(m_localPath);
|
||||||
|
|
||||||
if(!m_tmp.rename(m_localPath)) return;
|
m_tmp.close();
|
||||||
|
computeLocalHash(m_tmp);
|
||||||
computeLocalHash(m_tmp);
|
if(m_localHash == m_remoteHash) {
|
||||||
m_tmp.setAutoRemove(false);
|
if(!m_tmp.rename(m_localPath)) return;
|
||||||
m_state = DOWNLOADED;
|
m_tmp.setAutoRemove(false);
|
||||||
|
m_state = DOWNLOADED;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
m_state = NETWORK_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
emit stateChanged(m_state);
|
emit stateChanged(m_state);
|
||||||
};
|
};
|
||||||
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal){
|
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal){
|
||||||
if(m_tmp.write(m_downloadReply->readAll()) == -1) return;
|
if(m_tmp.write(m_downloadReply->readAll()) == -1) return;
|
||||||
|
if(!bytesTotal) return;
|
||||||
m_received = bytesReceived*100/bytesTotal;
|
m_received = bytesReceived*100/bytesTotal;
|
||||||
emit receivedChanged(m_received);
|
emit receivedChanged(m_received);
|
||||||
};
|
};
|
||||||
@@ -83,8 +96,16 @@ private slots:
|
|||||||
void handleStateChange(DownloadState state){
|
void handleStateChange(DownloadState state){
|
||||||
#ifdef QT_WIDGET
|
#ifdef QT_WIDGET
|
||||||
switch(state) {
|
switch(state) {
|
||||||
case DOWNLOADED:
|
|
||||||
case NETWORK_ERROR:
|
case NETWORK_ERROR:
|
||||||
|
if(m_localHash == "") {
|
||||||
|
setLabelText("Network Error");
|
||||||
|
setCancelButton(new QPushButton("OK"));
|
||||||
|
show();
|
||||||
|
} else {
|
||||||
|
emit finished(0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DOWNLOADED:
|
||||||
emit finished(0);
|
emit finished(0);
|
||||||
break;
|
break;
|
||||||
case DOWNLOADING_HASH:
|
case DOWNLOADING_HASH:
|
||||||
@@ -98,6 +119,11 @@ private slots:
|
|||||||
emit stateStringChanged();
|
emit stateStringChanged();
|
||||||
#endif //QT_WIDGET
|
#endif //QT_WIDGET
|
||||||
};
|
};
|
||||||
|
#ifdef QT_WIDGET
|
||||||
|
void handleCancel(){
|
||||||
|
QCoreApplication::instance()->exit(1);
|
||||||
|
}
|
||||||
|
#endif //QT_WIDGET
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -13,8 +13,9 @@ FileDownloader::FileDownloader(QString localPath, QString targetFile, QObject *p
|
|||||||
{
|
{
|
||||||
#ifdef QT_WIDGET
|
#ifdef QT_WIDGET
|
||||||
setCancelButton(0);
|
setCancelButton(0);
|
||||||
#endif //QT_WIDGET
|
|
||||||
connect(this, SIGNAL(receivedChanged(int)), SLOT(setValue(int)));
|
connect(this, SIGNAL(receivedChanged(int)), SLOT(setValue(int)));
|
||||||
|
connect(this, SIGNAL(canceled()), SLOT(handleCancel()));
|
||||||
|
#endif //QT_WIDGET
|
||||||
connect(this, SIGNAL(stateChanged(DownloadState)), SLOT(handleStateChange(DownloadState)));
|
connect(this, SIGNAL(stateChanged(DownloadState)), SLOT(handleStateChange(DownloadState)));
|
||||||
|
|
||||||
QDir dir(QDir::homePath());
|
QDir dir(QDir::homePath());
|
||||||
@@ -81,18 +82,22 @@ void FileDownloader::requestHash(QUrl url)
|
|||||||
|
|
||||||
void FileDownloader::computeRemoteHash()
|
void FileDownloader::computeRemoteHash()
|
||||||
{
|
{
|
||||||
QString aString = m_hashReply->readAll();
|
if(m_hashReply->error() != QNetworkReply::NoError) {
|
||||||
|
m_state = NETWORK_ERROR;
|
||||||
|
} else {
|
||||||
|
QString aString = m_hashReply->readAll();
|
||||||
|
|
||||||
int index = aString.indexOf("SHA1 Checksum: ");
|
int index = aString.indexOf("SHA1 Checksum: ");
|
||||||
m_remoteHash = aString.mid(index+52, 40);
|
m_remoteHash = aString.mid(index+52, 40);
|
||||||
if(m_localHash != m_remoteHash)
|
if(m_localHash != m_remoteHash)
|
||||||
{ /* We download the real file */
|
{ /* We download the real file */
|
||||||
m_state = DOWNLOADING_FILE;
|
m_state = DOWNLOADING_FILE;
|
||||||
setDownloadUrl(QUrl("http://wagic.googlecode.com/files/" + m_targetFile));
|
setDownloadUrl(QUrl("http://wagic.googlecode.com/files/" + m_targetFile));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_state = DOWNLOADED;
|
m_state = DOWNLOADED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
emit stateChanged(m_state);
|
emit stateChanged(m_state);
|
||||||
}
|
}
|
||||||
@@ -100,9 +105,10 @@ void FileDownloader::computeRemoteHash()
|
|||||||
void FileDownloader::computeLocalHash(QFile& file)
|
void FileDownloader::computeLocalHash(QFile& file)
|
||||||
{
|
{
|
||||||
QCryptographicHash crypto(QCryptographicHash::Sha1);
|
QCryptographicHash crypto(QCryptographicHash::Sha1);
|
||||||
file.open(QFile::ReadOnly);
|
QFile myFile(file.fileName());
|
||||||
while(!file.atEnd()){
|
myFile.open(QFile::ReadOnly);
|
||||||
crypto.addData(file.read(8192));
|
while(!myFile.atEnd()){
|
||||||
|
crypto.addData(myFile.read(8192));
|
||||||
}
|
}
|
||||||
QByteArray hash = crypto.result();
|
QByteArray hash = crypto.result();
|
||||||
m_localHash = hash.toHex();
|
m_localHash = hash.toHex();
|
||||||
|
|||||||
Reference in New Issue
Block a user