Commit e3441be1 authored by Dennis Rassmann's avatar Dennis Rassmann

update_notif: remove old files & extract update & remove old binary on startup

Signed-off-by: Dennis Rassmann's avatarshowp1984 <showp1984@gmail.com>
parent 07f2d9bf
......@@ -27,6 +27,27 @@ int main(int argc, char *argv[])
QApplication a(argc, argv);
update_notif un;
QString oldname;
#ifdef Q_WS_X11
oldname = qApp->applicationDirPath() + "/" + "Bricked-Installer";
#endif
#ifdef Q_WS_MAC
oldname = qApp->applicationDirPath() + "/" + "Bricked-Installer";
#endif
#ifdef Q_WS_WIN
oldname = qApp->applicationDirPath() + "\\" + "Bricked-Installer.exe";
#endif
#ifdef Q_WS_WIN
oldname.chop(4);
oldname += "_old.exe";
#else
oldname += "_old";
#endif
if(QFile::exists(oldname)) {
QFile::remove(oldname);
}
a.setApplicationVersion(APP_VERSION);
un.show();
......
......@@ -17,6 +17,10 @@
#include <QDebug>
#include <QTimer>
#include "quazip/quazip.h"
#include "quazip/quazipfile.h"
#include "quazip/JlCompress.h"
#include "update_notif.h"
#include "ui_update_notif.h"
......@@ -173,33 +177,28 @@ 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;
downfilepath = qApp->applicationDirPath() + "/" + filename;
if(QFile::exists(basepath)) {
if(QFile::exists(downfilepath)) {
if(QMessageBox::question(this, tr("Updater: Download"),
tr("There already exists a file called %1 in "
"the current directory. Overwrite?").arg(basepath),
"the current directory. Overwrite?").arg(downfilepath),
QMessageBox::Yes|QMessageBox::No, QMessageBox::No)
== QMessageBox::No) {
cancelDownload();
return;
}
QFile::remove(basepath);
QFile::remove(downfilepath);
}
file = new QFile(basepath);
file = new QFile(downfilepath);
if(!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, "Updater: Download",
tr("Unable to save the file %1: %2.")
.arg(basepath).arg(file->errorString()));
.arg(downfilepath).arg(file->errorString()));
cancelDownload();
delete file;
file = NULL;
......@@ -285,24 +284,24 @@ void update_notif::downloadFinished()
rename_installer();
}
void update_notif::rename_installer()
void update_notif::rename_installer(void)
{
QDir dir(qApp->applicationDirPath());
QString basepath;
QString binarypath;
#ifdef Q_WS_X11
basepath = qApp->applicationDirPath() + "/" + "Bricked-Installer";
binarypath = qApp->applicationDirPath() + "/" + "Bricked-Installer";
#endif
#ifdef Q_WS_MAC
basepath = qApp->applicationDirPath() + "/" + "Bricked-Installer";
binarypath = qApp->applicationDirPath() + "/" + "Bricked-Installer";
#endif
#ifdef Q_WS_WIN
basepath = qApp->applicationDirPath() + "\\" + "Bricked-Installer.exe";
binarypath = qApp->applicationDirPath() + "\\" + "Bricked-Installer.exe";
#endif
QString fPath = basepath;
QString fPath = binarypath;
QString fPath_ren = fPath;
#ifdef Q_WS_WIN
qDebug() << fPath_ren;
fPath_ren.chop(3);
fPath_ren.chop(4);
qDebug() << fPath_ren;
fPath_ren += "_old.exe";
qDebug() << fPath_ren;
......@@ -311,5 +310,170 @@ void update_notif::rename_installer()
fPath_ren += "_old";
qDebug() << fPath_ren;
#endif
oldname = fPath_ren;
dir.rename(fPath, fPath_ren);
extract_files();
}
bool update_notif::rmdir_recursive_notparent(const QString &dirName)
{
bool result = true;
QDir dir(dirName);
QString namedir;
#ifdef Q_WS_X11
namedir = qApp->applicationDirPath() + "/" + "Bricked-Installer";
#endif
#ifdef Q_WS_MAC
namedir = qApp->applicationDirPath() + "/" + "Bricked-Installer";
#endif
#ifdef Q_WS_WIN
namedir = qApp->applicationDirPath() + "\\" + "Bricked-Installer.exe";
#endif
#ifdef Q_WS_WIN
namedir.chop(4);
namedir += "_old.exe";
#else
namedir += "_old";
#endif
if (dir.exists(dirName)) {
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
if (info.isDir()) {
if (info.absoluteFilePath() != dirName)
result = rmdir_recursive_notparent(info.absoluteFilePath());
}
else {
if ((info.absoluteFilePath() != namedir) && (info.absoluteFilePath() != downfilepath))
result = QFile::remove(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
if (QString(qApp->applicationDirPath() + "/") != dirName)
result = dir.rmdir(dirName);
else
result = true;
}
return result;
}
bool update_notif::extract_zip(const QString & filePath)
{
ui->bar2_xtr->show();
QuaZip zip(filePath);
if (!zip.open(QuaZip::mdUnzip)) {
qWarning("testRead(): zip.open(): %d", zip.getZipError());
return false;
}
zip.setFileNameCodec("IBM866");
qWarning("%d entries\n", zip.getEntriesCount());
qWarning("Global comment: %s\n", zip.getComment().toLocal8Bit().constData());
QuaZipFileInfo info;
QuaZipFile file(&zip);
QFile out;
QString name;
QString basepath = qApp->applicationDirPath();
QByteArray cnt;
QDir filedir;
QString temppath;
QStringList splitpath = QStringList() << "" << "" << "" << "" << "" << "" << "" << "" << "" << "" << "" << "" << "" << "";
int filenr = 0;
ui->bar2_xtr->setMaximum(zip.getEntriesCount());
for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
if (!zip.getCurrentFileInfo(&info)) {
qWarning("testRead(): getCurrentFileInfo(): %d\n", zip.getZipError());
return false;
}
if (!file.open(QuaZipFile::ReadOnly)) {
qWarning("testRead(): file.open(): %d", file.getZipError());
return false;
}
splitpath = file.getActualFileName().split("/");
temppath = splitpath.takeLast();
temppath = splitpath.join("/");
abspath = QString("%1/%2").arg(basepath).arg(temppath);
name = QString("%1/%2").arg(basepath).arg(file.getActualFileName());
if (file.getZipError() != UNZ_OK) {
qWarning("testRead(): file.getFileName(): %d", file.getZipError());
return false;
}
filedir.setPath(abspath);
if (!filedir.exists()) {
filedir.mkpath(abspath);
}
out.setFileName(name);
cnt=file.readAll();
qDebug()<<"name: "<<name;
qDebug()<<"writing "<<cnt.size()<<" bytes";
out.open(QIODevice::WriteOnly);
out.write(cnt);
out.close();
if (file.getZipError() != UNZ_OK) {
qWarning("testRead(): file.getFileName(): %d", file.getZipError());
return false;
}
if (!file.atEnd()) {
qWarning("testRead(): read all but not EOF");
return false;
}
file.close();
if (file.getZipError() != UNZ_OK) {
qWarning("testRead(): file.close(): %d", file.getZipError());
return false;
}
ui->bar2_xtr->setValue(filenr);
filenr++;
}
zip.close();
if (zip.getZipError() != UNZ_OK) {
qWarning("testRead(): zip.close(): %d", zip.getZipError());
return false;
}
return true;
}
void update_notif::extract_files(void)
{
bool ret = false;
ret = rmdir_recursive_notparent(QString(qApp->applicationDirPath() + "/"));
if (!ret) {
QMessageBox::information(this, "Update failed!", tr("Error! Could not delete old files! You are required to redownload the application from www.bricked.de"));
this->close();
}
ret = false;
ui->lbl2_xtr->show();
ret = extract_zip(downfilepath);
if (ret) {
ui->bar2_xtr->setMinimum(0);
ui->bar2_xtr->setMaximum(1);
ui->bar2_xtr->setValue(1);
QMessageBox::information(this, "Update sucessfull!", tr("The update was successfull. I will now restart to the new version."));
} else {
QMessageBox::information(this, "Update failed", tr("The update failed. You are required to redownload the application from www.bricked.de"));
this->close();
}
}
/*
ui->lbl2_restart->show();
*/
......@@ -55,11 +55,17 @@ private:
dragons *d;
QTimer *timer_tout;
QString url;
QString abspath;
QString downfilepath;
QString oldname;
QNetworkAccessManager manager;
QFile *file;
QNetworkReply *reply;
bool downloadRequestAborted;
void rename_installer();
void rename_installer(void);
void extract_files(void);
bool extract_zip(const QString&);
bool rmdir_recursive_notparent(const QString&);
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