diff ui/mainwindow.cpp @ 1252:9a482182f80f

(issue54) Monitor update installation and restart afterwards on Linux
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 25 Sep 2014 15:57:25 +0200
parents 8748223990eb
children 8ceaa189570d f1e461340ede
line wrap: on
line diff
--- a/ui/mainwindow.cpp	Thu Sep 25 15:56:37 2014 +0200
+++ b/ui/mainwindow.cpp	Thu Sep 25 15:57:25 2014 +0200
@@ -342,6 +342,23 @@
     return targetPath;
 }
 
+void MainWindow::updaterFinished(int exitCode, QProcess::ExitStatus status)
+{
+    if (status != QProcess::NormalExit) {
+        syslog_error_printf("Update failed.\n");
+        qDebug() << "Failed to install update.";
+        return;
+    }
+    if (exitCode != 0) {
+        qDebug() << "Update not installed with error: " << exitCode;
+        return;
+    }
+    qDebug() << "Restarting";
+    ProcessHelp::cleanUp();
+    QProcess::startDetached(qApp->applicationFilePath());
+    qApp->quit();
+}
+
 void MainWindow::installNewSW(const QString& fileName, const QDateTime& modDate) {
     QFileInfo instProcInfo = QFileInfo(fileName);
     QString filePath = QDir::toNativeSeparators(instProcInfo.absoluteFilePath());
@@ -410,26 +427,44 @@
     parameters << "--update";
     bool sudo_started = false;
     bool use_sudo = is_admin() && is_system_install();
+
+    QProcess *updaterProcess = new QProcess();
+
     if (use_sudo) {
         QStringList sudoPrograms;
         sudoPrograms << "pkexec" << "kdesudo" << "sudo";
         QStringList sudoParams;
-        sudoParams << filePath + " " + parameters.join(" ");
+        sudoParams << filePath;
+        sudoParams << parameters;
+        updaterProcess->setArguments(sudoParams);
 
         foreach (const QString &sProg, sudoPrograms) {
             qDebug() << "Starting process " << sProg <<" params: " << sudoParams;
-            if (!QProcess::startDetached(sProg, sudoParams)) {
+            updaterProcess->setProgram(sProg);
+            updaterProcess->start();
+            if (!updaterProcess->waitForStarted() ||
+                updaterProcess->state() == QProcess::NotRunning) {
                 continue;
             } else {
                 sudo_started = true;
+                connect(updaterProcess, SIGNAL(finished(int, QProcess::ExitStatus)),
+                        this, SLOT(updaterFinished(int, QProcess::ExitStatus)));
                 break;
             }
         }
     }
-    qDebug() << "Starting process " << filePath <<" params: " << parameters;
-    if (!sudo_started && !QProcess::startDetached(filePath, parameters)) {
-        qDebug() << "Failed to start process.";
-        fclose(vres.fptr);
+    if (!sudo_started) {
+        qDebug() << "Starting process " << filePath <<" params: " << parameters;
+        updaterProcess->setArguments(parameters);
+        updaterProcess->setProgram(filePath);
+        updaterProcess->start();
+
+        if (!updaterProcess->waitForStarted() ||
+            updaterProcess->state() == QProcess::NotRunning) {
+            qDebug() << "Failed to start process.";
+        }
+        connect(updaterProcess, SIGNAL(finished(int, QProcess::ExitStatus)),
+                this, SLOT(restart()));
         return;
     }
 
@@ -442,7 +477,9 @@
     syslog_info_printf ("Installing update: %s\n", fileName.toUtf8().constData());
     /* Installer process should now be running. We exit */
     fclose(vres.fptr);
+#ifdef WIN32
     closeApp();
+#endif
 }
 
 void MainWindow::checkUpdates(bool downloadSW)

http://wald.intevation.org/projects/trustbridge/