Commit 4b1ceacb authored by Dennis Rassmann's avatar Dennis Rassmann

update_notif: replace qt downmanager with own implementation

Signed-off-by: Dennis Rassmann's avatarshowp1984 <showp1984@gmail.com>
parent 8c6de3e4
......@@ -19,16 +19,14 @@ SOURCES += main.cpp \
flasher.cpp \
openfile.cpp \
update_notif.cpp \
database.cpp \
downmanager.cpp
database.cpp
HEADERS += mainwindow.h \
dragons.h \
info.h \
detect.h \
flasher.h \
openfile.h \
update_notif.h \
downmanager.h
update_notif.h
FORMS += mainwindow.ui \
dragons.ui \
info.ui \
......
#############################################################################
# Makefile for building: Bricked-Installer
# Generated by qmake (2.01a) (Qt 4.6.2) on: Sat Jul 7 02:59:37 2012
# Generated by qmake (2.01a) (Qt 4.6.2) on: Sat Jul 7 06:37:34 2012
# Project: Bricked-Installer.pro
# Template: app
# Command: /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ -unix -o Makefile Bricked-Installer.pro
......@@ -10,13 +10,13 @@
CC = gcc
CXX = g++
DEFINES = -DAPP_VERSION=\"0.1\" -DQT_NO_DEBUG -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
DEFINES = -DAPP_VERSION=\"0.1\" -DQT_NO_DEBUG -DQT_SQL_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED
CFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql -I/usr/include/qt4 -I. -I.
INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtNetwork -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql -I/usr/include/qt4 -I. -I.
LINK = g++
LFLAGS = -Wl,-O1
LIBS = $(SUBLIBS) -L/usr/lib -lQtSql -lQtGui -lQtCore -lpthread
LIBS = $(SUBLIBS) -L/usr/lib -lQtSql -lQtGui -lQtNetwork -lQtCore -lpthread
AR = ar cqs
RANLIB =
QMAKE = /usr/bin/qmake-qt4
......@@ -168,6 +168,7 @@ Makefile: Bricked-Installer.pro /usr/share/qt4/mkspecs/linux-g++/qmake.conf /us
/usr/share/qt4/mkspecs/features/include_source_dir.prf \
/usr/lib/libQtSql.prl \
/usr/lib/libQtGui.prl \
/usr/lib/libQtNetwork.prl \
/usr/lib/libQtCore.prl
$(QMAKE) -spec /usr/share/qt4/mkspecs/linux-g++ -unix -o Makefile Bricked-Installer.pro
/usr/share/qt4/mkspecs/common/g++.conf:
......@@ -191,6 +192,7 @@ Makefile: Bricked-Installer.pro /usr/share/qt4/mkspecs/linux-g++/qmake.conf /us
/usr/share/qt4/mkspecs/features/include_source_dir.prf:
/usr/lib/libQtSql.prl:
/usr/lib/libQtGui.prl:
/usr/lib/libQtNetwork.prl:
/usr/lib/libQtCore.prl:
qmake: FORCE
@$(QMAKE) -spec /usr/share/qt4/mkspecs/linux-g++ -unix -o Makefile Bricked-Installer.pro
......@@ -252,7 +254,8 @@ moc_openfile.cpp: info.h \
openfile.h
/usr/bin/moc-qt4 $(DEFINES) $(INCPATH) openfile.h -o moc_openfile.cpp
moc_update_notif.cpp: dragons.h \
moc_update_notif.cpp: ui_update_notif.h \
dragons.h \
mainwindow.h \
info.h \
detect.h \
......@@ -318,6 +321,7 @@ compiler_clean: compiler_moc_header_clean compiler_rcc_clean compiler_uic_clean
####### Compile
main.o: main.cpp update_notif.h \
ui_update_notif.h \
dragons.h \
mainwindow.h \
info.h \
......@@ -377,13 +381,13 @@ openfile.o: openfile.cpp openfile.h \
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o openfile.o openfile.cpp
update_notif.o: update_notif.cpp update_notif.h \
ui_update_notif.h \
dragons.h \
mainwindow.h \
info.h \
detect.h \
openfile.h \
flasher.h \
ui_update_notif.h
flasher.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o update_notif.o update_notif.cpp
database.o: database.cpp
......
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#include "downmanager.h"
DownloadManager::DownloadManager()
{
connect(&manager, SIGNAL(finished(QNetworkReply*)),
SLOT(downloadFinished(QNetworkReply*)));
}
void DownloadManager::doDownload(const QUrl &url)
{
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
currentDownloads.append(reply);
}
QString DownloadManager::saveFileName(const QUrl &url)
{
QString path = url.path();
QString basename = QFileInfo(path).fileName();
if (basename.isEmpty())
basename = "download";
if (QFile::exists(basename)) {
// already exists, don't overwrite
int i = 0;
basename += '.';
while (QFile::exists(basename + QString::number(i)))
++i;
basename += QString::number(i);
}
return basename;
}
bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
fprintf(stderr, "Could not open %s for writing: %s\n",
qPrintable(filename),
qPrintable(file.errorString()));
return false;
}
file.write(data->readAll());
file.close();
return true;
}
void DownloadManager::downloadFinished(QNetworkReply *reply)
{
QUrl url = reply->url();
if (reply->error()) {
fprintf(stderr, "Download of %s failed: %s\n",
url.toEncoded().constData(),
qPrintable(reply->errorString()));
} else {
QString filename = saveFileName(url);
if (saveToDisk(filename, reply))
printf("Download of %s succeeded (saved to %s)\n",
url.toEncoded().constData(), qPrintable(filename));
}
currentDownloads.removeAll(reply);
reply->deleteLater();
if (currentDownloads.isEmpty())
// all downloads finished
QCoreApplication::instance()->quit();
}
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef DOWNMANAGER_H
#define DOWNMANAGER_H
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QStringList>
#include <QTimer>
#include <QUrl>
#include <stdio.h>
class DownloadManager: public QObject
{
Q_OBJECT
QNetworkAccessManager manager;
QList<QNetworkReply *> currentDownloads;
public:
DownloadManager();
QString saveFileName(const QUrl &url);
bool saveToDisk(const QString &filename, QIODevice *data);
public slots:
void doDownload(const QUrl &url);
void downloadFinished(QNetworkReply *reply);
};
#endif // DOWNMANAGER_H
......@@ -114,6 +114,7 @@ void getDBupd::run(void)
{
QSqlDatabase db = connectDB();
QSqlQuery query;
QString ver;
qDebug() << "Local: " << qApp->applicationVersion();
......@@ -121,10 +122,30 @@ void getDBupd::run(void)
query.exec();
while (query.next()) {
version = query.value(0).toFloat();
ver = QString::number(version);
branch = query.value(1).toString();
changelog = query.value(2).toString();
date = query.value(3).toString();
url = query.value(4).toString();
url = url.replace(QString("%%%VERSION%%%"), ver);
#ifdef Q_WS_X11
url = url.replace(QString("%%%SHORT_PLATFORM%%%"), QString("lnx"));
#endif
#ifdef Q_WS_MAC
url = url.replace(QString("%%%SHORT_PLATFORM%%%"), QString("mac"));
#endif
#ifdef Q_WS_WIN
url = url.replace(QString("%%%SHORT_PLATFORM%%%"), QString("win"));
#endif
#ifdef Q_WS_X11
url = url.replace(QString("%%%PLATFORM%%%"), QString("LINUX"));
#endif
#ifdef Q_WS_MAC
url = url.replace(QString("%%%PLATFORM%%%"), QString("MACOS"));
#endif
#ifdef Q_WS_WIN
url = url.replace(QString("%%%PLATFORM%%%"), QString("WINDOWS"));
#endif
qDebug() << version << branch << date << url;// << changelog;
}
db.close();
......@@ -152,7 +173,112 @@ void update_notif::on_btn_now_clicked()
ui->lbl2_updating->show();
ui->bar2_downl->show();
/*
ui->lbl2_xtr->show();
ui->bar2_xtr->show();
ui->lbl2_restart->show();
*/
QUrl qurl(p_dbupd->url);
QString filename = QFileInfo(qurl.path()).fileName();
QString basepath = qApp->applicationDirPath() + "/" + filename;
if(QFile::exists(basepath)) {
if(QMessageBox::question(this, tr("Updater: Download"),
tr("There already exists a file called %1 in "
"the current directory. Overwrite?").arg(basepath),
QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
== QMessageBox::No) {
cancelDownload();
return;
}
QFile::remove(basepath);
}
file = new QFile(basepath);
if(!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, "Updater: Download",
tr("Unable to save the file %1: %2.")
.arg(basepath).arg(file->errorString()));
cancelDownload();
delete file;
file = NULL;
return;
}
downloadRequestAborted = false;
reply = manager.get(QNetworkRequest(qurl));
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
connect(reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
}
void update_notif::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
if(downloadRequestAborted)
return;
ui->bar2_downl->setMaximum(bytesTotal);
ui->bar2_downl->setValue(bytesReceived);
}
void update_notif::downloadReadyRead()
{
if(file)
file->write(reply->readAll());
}
void update_notif::cancelDownload()
{
downloadRequestAborted = true;
reply->abort();
ui->btn_now->show();
}
void update_notif::downloadFinished()
{
if(downloadRequestAborted) {
if(file) {
file->close();
file->remove();
delete file;
file = NULL;
}
reply->deleteLater();
ui->btn_later->show();
ui->btn_now->show();
ui->btn_quit->show();
ui->lbl_branch->show();
ui->lbl_change->show();
ui->lbl_date->show();
ui->lbl_edt_branch->show();
ui->lbl_edt_date->show();
ui->lbl_edt_vers->show();
ui->lbl_updnot->show();
ui->lbl_vers->show();
ui->txt_change->show();
ui->lbl2_restart->hide();
ui->lbl2_updating->hide();
ui->lbl2_warn->hide();
ui->lbl2_xtr->hide();
ui->bar2_xtr->hide();
ui->bar2_downl->hide();
return;
}
downloadReadyRead();
file->flush();
file->close();
if(reply->error()) {
//Download failed
QMessageBox::information(this, "Download failed", tr("Failed: %1").arg(reply->errorString()));
cancelDownload();
downloadFinished();
}
reply->deleteLater();
reply = NULL;
delete file;
file = NULL;
}
......@@ -21,7 +21,13 @@
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QThread>
#include <QtGui/QMainWindow>
#include <QProgressDialog>
#include <QFile>
#include <QtNetwork>
#include <QMessageBox>
#include "ui_update_notif.h"
#include "dragons.h"
namespace Ui {
......@@ -35,6 +41,12 @@ public:
~update_notif();
getDBupd *p_dbupd;
public slots:
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void downloadFinished();
void downloadReadyRead();
void cancelDownload();
protected:
void changeEvent(QEvent *e);
......@@ -43,6 +55,10 @@ private:
dragons *d;
QTimer *timer_tout;
QString url;
QNetworkAccessManager manager;
QFile *file;
QNetworkReply *reply;
bool downloadRequestAborted;
private slots:
void on_btn_now_clicked();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment