diff ui/mainwindow.cpp @ 1339:64f812a63de6

(issue170) Keep checking for updates in intervals When the connection fails in tray mode this will keep the software alive and have it checking for some time until a limit is reached.
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 23 Oct 2014 16:14:18 +0200
parents 5c6294c201c2
children c2b76c8a8b82
line wrap: on
line diff
--- a/ui/mainwindow.cpp	Thu Oct 16 15:00:52 2014 +0200
+++ b/ui/mainwindow.cpp	Thu Oct 23 16:14:18 2014 +0200
@@ -51,12 +51,6 @@
 // something to say
 #define NAG_INTERVAL_MINUTES 70
 
-/** @def The interval in which the update check in tray mode is placed
- *
- * The interval should be given in minutes. The first update check will
- * be placed randomly inside this interval. */
-#define FIRST_CHECK_INTERVAL_MINUTES 180
-
 #define DATETIME_FORMAT "d. MMM yyyy HH:mm"
 
 #ifndef APPNAME
@@ -104,9 +98,36 @@
 static void activateDetailsButton(QPushButton *);
 static void deactivateDetailsButton(QPushButton *);
 
+/** @brief get the interval in milliseconds until the next update try.
+ *
+ * @param [in] tries the number of unsuccessful tries already done.
+ *
+ * @returns a positive value for the time in ms that should be waited.
+ * -1 in case the software should abort. */
+static int getNextUpdateInterval(int tries)
+{
+    if (tries < 2) {
+        // 5-10 minutes for the first two checks
+        return ((5 + (qrand() % 5)) * 60 * 1000);
+    }
+    if (tries < 4) {
+        // 15 minutes
+        return 15 * 60 * 1000;
+    }
+    if (tries < 6) {
+        // 30 minutes
+        return 30 * 60 * 1000;
+    }
+    if (tries < 7) {
+        return 60;
+    }
+    return -1;
+}
+
 MainWindow::MainWindow(bool trayMode):
     mTrayMode(trayMode),
-    mManualDetailsShown(false)
+    mManualDetailsShown(false),
+    mFailedConnections(0)
 {
     createActions();
     createTrayIcon();
@@ -123,13 +144,7 @@
     connect(mMessageTimer, SIGNAL(timeout()), this, SLOT(showMessage()));
     mMessageTimer->setInterval(NAG_INTERVAL_MINUTES * 60 * 1000);
     mMessageTimer->start();
-    if (trayMode) {
-        /* Place the first update in a random interval in the first 3h
-         * after start. */
-        QTimer::singleShot(qrand() % (FIRST_CHECK_INTERVAL_MINUTES * 60 * 1000), this, SLOT(checkUpdates()));
-    } else {
-        checkUpdates();
-    }
+    checkUpdates();
     loadUnselectedCertificates();
     loadCertificateList();
 
@@ -651,10 +666,10 @@
     } else {
         handleLTE(lteNoConnection);
     }
+    setState(TransferError);
 #ifdef IS_TAG_BUILD
     /* During tag build it should never happen that an url checked is not available
      * during development this is normal as each revision produces a new url. */
-    setState(TransferError);
     if (!isVisible()) {
         mCurMessage = message;
         mTrayIcon->show();
@@ -1757,6 +1772,18 @@
         syslog_info_printf(tr("Sucessfully checked for updates.").toUtf8().constData());
         handleLTE(lteNoConnection, true); /* Reset error state */
         handleLTE(lteInvalidCertificate, true);
+        mFailedConnections = 0;
+    } else if (!isVisible()) {
+        int waitInterval = getNextUpdateInterval(mFailedConnections++);
+        if (waitInterval < 0) {
+            qDebug() << "Shutting down after " << mFailedConnections <<
+                " tries to get an update connection.";
+            closeApp();
+            return;
+        }
+        qDebug() << "Waiting for " << waitInterval / 1000 << " seconds until the next try.";
+        QTimer::singleShot(waitInterval, this, SLOT(checkUpdates()));
+        return;
     }
     if ((getState() != NewSoftwareAvailable && getState() != NewListAvailable && mTrayMode)
             && !isVisible()) {

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