From c4abab8cfc785e4f3f07c2c04feb5c074129c663 Mon Sep 17 00:00:00 2001 From: Mark Watkins Date: Sat, 17 Dec 2011 04:15:57 +1000 Subject: [PATCH] Updater stuff. Some Windows build Fixes, file replacement logic --- SleepyHeadQT.pro | 10 ++-- UpdateWindow.cpp => UpdaterWindow.cpp | 75 ++++++++++----------------- UpdateWindow.h => UpdaterWindow.h | 10 ++-- UpdateWindow.ui => UpdaterWindow.ui | 5 +- mainwindow.cpp | 4 +- 5 files changed, 42 insertions(+), 62 deletions(-) rename UpdateWindow.cpp => UpdaterWindow.cpp (87%) rename UpdateWindow.h => UpdaterWindow.h (87%) rename UpdateWindow.ui => UpdaterWindow.ui (98%) diff --git a/SleepyHeadQT.pro b/SleepyHeadQT.pro index 80e91b98..89ddae34 100644 --- a/SleepyHeadQT.pro +++ b/SleepyHeadQT.pro @@ -70,7 +70,6 @@ SOURCES += main.cpp\ common_gui.cpp \ SleepLib/loader_plugins/intellipap_loader.cpp \ SleepLib/calcs.cpp \ - UpdateWindow.cpp \ updateparser.cpp \ quazip/zip.c \ quazip/unzip.c \ @@ -80,7 +79,8 @@ SOURCES += main.cpp\ quazip/quacrc32.cpp \ quazip/quaadler32.cpp \ quazip/qioapi.cpp \ - quazip/JlCompress.cpp + quazip/JlCompress.cpp \ + UpdaterWindow.cpp unix:SOURCES += qextserialport/posix_qextserialport.cpp unix:!macx:SOURCES += qextserialport/qextserialenumerator_unix.cpp @@ -139,7 +139,6 @@ HEADERS += \ SleepLib/loader_plugins/intellipap_loader.h \ SleepLib/calcs.h \ version.h \ - UpdateWindow.h \ updateparser.h \ quazip/zip.h \ quazip/unzip.h \ @@ -153,7 +152,8 @@ HEADERS += \ quazip/quaadler32.h \ quazip/JlCompress.h \ quazip/ioapi.h \ - quazip/crypt.h + quazip/crypt.h \ + UpdaterWindow.h FORMS += \ @@ -166,7 +166,7 @@ FORMS += \ profileselect.ui \ newprofile.ui \ exportcsv.ui \ - UpdateWindow.ui + UpdaterWindow.ui RESOURCES += \ Resources.qrc \ diff --git a/UpdateWindow.cpp b/UpdaterWindow.cpp similarity index 87% rename from UpdateWindow.cpp rename to UpdaterWindow.cpp index 3cd7175c..40daba7e 100644 --- a/UpdateWindow.cpp +++ b/UpdaterWindow.cpp @@ -14,17 +14,17 @@ #include "SleepLib/preferences.h" #include "quazip/quazip.h" #include "quazip/quazipfile.h" -#include "UpdateWindow.h" -#include "ui_UpdateWindow.h" +#include "UpdaterWindow.h" +#include "ui_UpdaterWindow.h" #include "version.h" #include "mainwindow.h" #include "common_gui.h" extern MainWindow * mainwin; -UpdateWindow::UpdateWindow(QWidget *parent) : +UpdaterWindow::UpdaterWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::UpdateWindow) + ui(new Ui::UpdaterWindow) { ui->setupUi(this); @@ -55,17 +55,16 @@ UpdateWindow::UpdateWindow(QWidget *parent) : ui->stackedWidget->setCurrentIndex(0); } -UpdateWindow::~UpdateWindow() +UpdaterWindow::~UpdaterWindow() { disconnect(netmanager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); delete ui; } -void UpdateWindow::checkForUpdates() +void UpdaterWindow::checkForUpdates() { QString filename=QApplication::applicationDirPath()+QDir::separator()+"update.xml"; - qDebug() << filename; // Check updates.xml file if it's still recent.. if (QFile::exists(filename)) { QFileInfo fi(filename); @@ -90,7 +89,7 @@ void UpdateWindow::checkForUpdates() dltime.start(); } -void UpdateWindow::dataReceived() +void UpdaterWindow::dataReceived() { //HttpStatusCodeAttribute QString rs=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString(); @@ -102,7 +101,7 @@ void UpdateWindow::dataReceived() file.write(read); } -void UpdateWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) +void UpdaterWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { QString rs=reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString(); if (rs!="200") return; @@ -119,7 +118,7 @@ void UpdateWindow::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) int elapsed=dltime.elapsed(); } -void UpdateWindow::requestFile() +void UpdaterWindow::requestFile() { if (!update) return; QProgressBar *bar=qobject_cast(ui->tableWidget->cellWidget(current_row,3)); @@ -159,7 +158,7 @@ void UpdateWindow::requestFile() connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this, SLOT(downloadProgress(qint64,qint64))); } -void UpdateWindow::ParseUpdateXML(QIODevice * dev) +void UpdaterWindow::ParseUpdateXML(QIODevice * dev) { QXmlInputSource src(dev); QXmlSimpleReader reader; @@ -256,7 +255,7 @@ void UpdateWindow::ParseUpdateXML(QIODevice * dev) } } -void UpdateWindow::replyFinished(QNetworkReply * reply) +void UpdaterWindow::replyFinished(QNetworkReply * reply) { netmanager->disconnect(reply,SIGNAL(downloadProgress(qint64,qint64)),this, SLOT(downloadProgress(qint64,qint64))); if (reply->error()==QNetworkReply::NoError) { @@ -332,15 +331,18 @@ void UpdateWindow::replyFinished(QNetworkReply * reply) int fsize=files.size(); QByteArray ba; QStringList update_txt; - QStringList unzipped; - QStringList original; + + QString apppath=QApplication::applicationDirPath()+QDir::separator(); + QString backups=apppath+"Backups"+QDir::separator(); + QString downloads=apppath+"Downloads"+QDir::separator(); + QDir().mkpath(backups); for (int i=0;iplainTextEdit->appendPlainText("Extracting "+files.at(i)); QuaZipFile qzf(file.fileName(),files.at(i)); qzf.open(QuaZipFile::ReadOnly); - QString path=QApplication::applicationDirPath()+QDir::separator()+"Download"+QDir::separator()+files.at(i); + QString path=downloads+files.at(i); if (path.endsWith(QDir::separator())) { QDir().mkpath(path); if (update->unzipped_path.isEmpty()) @@ -357,8 +359,12 @@ void UpdateWindow::replyFinished(QNetworkReply * reply) if (!line.isNull()) update_txt.append(line); } while (!line.isNull()); } else { - unzipped.append(path); - original.append(QApplication::applicationDirPath()+QDir::separator()+files.at(i).section("/",-1)); + QString fn=files.at(i).section("/",-1); + if (f.exists(backups+fn)) f.remove(backups+fn); + if (!f.rename(apppath+fn,backups+fn)) { + errors++; + } + f.setFileName(path); f.open(QFile::WriteOnly); f.write(ba); @@ -373,33 +379,6 @@ void UpdateWindow::replyFinished(QNetworkReply * reply) } zip.close(); if (!errors) { - for (int i=0;iplainTextEdit->appendPlainText("Copied "+unzipped[i]+" to "+original[i]); - } else { - QFile f(original[i]); - if (f.exists()) { - if (!f.remove()) { - f.rename(original[i],original[i]+".bak"); - ui->plainTextEdit->appendPlainText("Renaming "+original[i]); - } else { - ui->plainTextEdit->appendPlainText("Removing "+original[i]); - } - } - if (QFile::copy(unzipped[i],original[i])) { - ui->plainTextEdit->appendPlainText("Copied "+unzipped[i]+" to "+original[i]); - } else { - ui->plainTextEdit->appendPlainText("Copy Failed"); - failed=true; - } - } - - } - // Parse replacement instructions file? or just dump zip over the top? - - - // To clean up or not to clean up... is a reasonably valid question. - //file.remove(); } } @@ -438,12 +417,12 @@ void UpdateWindow::replyFinished(QNetworkReply * reply) } } -void UpdateWindow::on_CloseButton_clicked() +void UpdaterWindow::on_CloseButton_clicked() { close(); } -void UpdateWindow::upgradeNext() +void UpdaterWindow::upgradeNext() { QTableWidgetItem *item; bool fnd=false; @@ -486,7 +465,7 @@ void UpdateWindow::upgradeNext() } -void UpdateWindow::on_upgradeButton_clicked() +void UpdaterWindow::on_upgradeButton_clicked() { if (!updates.size()) return; ui->tableWidget->clearContents(); @@ -519,7 +498,7 @@ void UpdateWindow::on_upgradeButton_clicked() upgradeNext(); } -void UpdateWindow::on_FinishedButton_clicked() +void UpdaterWindow::on_FinishedButton_clicked() { if (success) mainwin->RestartApplication(); diff --git a/UpdateWindow.h b/UpdaterWindow.h similarity index 87% rename from UpdateWindow.h rename to UpdaterWindow.h index 588e3471..5ece6dab 100644 --- a/UpdateWindow.h +++ b/UpdaterWindow.h @@ -11,18 +11,18 @@ #include "updateparser.h" namespace Ui { -class UpdateWindow; +class UpdaterWindow; } enum RequestMode { RM_None, RM_CheckUpdates, RM_GetFile, RM_UpdateQT }; -class UpdateWindow : public QMainWindow +class UpdaterWindow : public QMainWindow { Q_OBJECT public: - explicit UpdateWindow(QWidget *parent = 0); - ~UpdateWindow(); + explicit UpdaterWindow(QWidget *parent = 0); + ~UpdaterWindow(); void checkForUpdates(); void ParseUpdateXML(QIODevice * dev); @@ -44,7 +44,7 @@ private slots: private: UpdateParser updateparser; - Ui::UpdateWindow *ui; + Ui::UpdaterWindow *ui; QSystemTrayIcon *systray; QMenu *systraymenu; RequestMode requestmode; diff --git a/UpdateWindow.ui b/UpdaterWindow.ui similarity index 98% rename from UpdateWindow.ui rename to UpdaterWindow.ui index 67f081b7..47f5910f 100644 --- a/UpdateWindow.ui +++ b/UpdaterWindow.ui @@ -1,7 +1,7 @@ - UpdateWindow - + UpdaterWindow + 0 @@ -365,6 +365,7 @@ + diff --git a/mainwindow.cpp b/mainwindow.cpp index c613a37c..bba69560 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -30,7 +30,7 @@ #include "exportcsv.h" #include "SleepLib/schema.h" #include "Graphs/glcommon.h" -#include "UpdateWindow.h" +#include "UpdaterWindow.h" QProgressBar *qprogress; QLabel *qstatus; @@ -517,7 +517,7 @@ void MainWindow::CheckForUpdates() void MainWindow::on_actionCheck_for_Updates_triggered() { - UpdateWindow *w=new UpdateWindow(this); + UpdaterWindow *w=new UpdaterWindow(this); w->checkForUpdates();