Updater UI: Implement auto-update option and update button
authorMichael Schuster <michael@schuster.ms>
Mon, 9 Mar 2020 00:17:02 +0000 (01:17 +0100)
committerMichael Schuster <michael@schuster.ms>
Mon, 9 Mar 2020 01:34:55 +0000 (02:34 +0100)
- Add checkbox and button to UI
- Add new parameter autoUpdateCheck to ConfigFile

Signed-off-by: Michael Schuster <michael@schuster.ms>
src/gui/generalsettings.cpp
src/gui/generalsettings.h
src/gui/generalsettings.ui
src/gui/updater/ocupdater.cpp
src/libsync/configfile.cpp
src/libsync/configfile.h

index 050e574504f002adc74df474f34f33ffc2d853aa..6d0a56d399ff7764afc88f37a0520a191390a9d5 100644 (file)
@@ -162,18 +162,47 @@ void GeneralSettings::slotUpdateInfo()
         connect(updater, &OCUpdater::downloadStateChanged, this, &GeneralSettings::slotUpdateInfo, Qt::UniqueConnection);
         connect(_ui->restartButton, &QAbstractButton::clicked, updater, &OCUpdater::slotStartInstaller, Qt::UniqueConnection);
         connect(_ui->restartButton, &QAbstractButton::clicked, qApp, &QApplication::quit, Qt::UniqueConnection);
+        connect(_ui->updateButton, &QAbstractButton::clicked, this, &GeneralSettings::slotUpdateCheckNow, Qt::UniqueConnection);
+        connect(_ui->autoCheckForUpdatesCheckBox, &QAbstractButton::toggled, this, &GeneralSettings::slotToggleAutoUpdateCheck);
 
         QString status = updater->statusString();
         Theme::replaceLinkColorStringBackgroundAware(status);
         _ui->updateStateLabel->setText(status);
 
         _ui->restartButton->setVisible(updater->downloadState() == OCUpdater::DownloadComplete);
+
+        _ui->updateButton->setEnabled(updater->downloadState() != OCUpdater::CheckingServer &&
+                                      updater->downloadState() != OCUpdater::Downloading &&
+                                      updater->downloadState() != OCUpdater::DownloadComplete);
+
+        _ui->autoCheckForUpdatesCheckBox->setChecked(ConfigFile().autoUpdateCheck());
     } else {
         // can't have those infos from sparkle currently
         _ui->updatesGroupBox->setVisible(false);
     }
 }
 
+void GeneralSettings::slotUpdateCheckNow()
+{
+    OCUpdater *updater = qobject_cast<OCUpdater *>(Updater::instance());
+    if (ConfigFile().skipUpdateCheck()) {
+        updater = nullptr; // don't show update info if updates are disabled
+    }
+
+    if (updater) {
+        _ui->updateButton->setEnabled(false);
+
+        updater->checkForUpdate();
+    }
+}
+
+void GeneralSettings::slotToggleAutoUpdateCheck()
+{
+    ConfigFile cfgFile;
+    bool isChecked = _ui->autoCheckForUpdatesCheckBox->isChecked();
+    cfgFile.setAutoUpdateCheck(isChecked, QString());
+}
+
 void GeneralSettings::saveMiscSettings()
 {
     if (_currentlyLoading)
index f7a7591612358e7dffae023be67888b7f76dd281..eb8b2e49ba587903e19222a014c0ee7389300b9f 100644 (file)
@@ -47,10 +47,12 @@ private slots:
     void slotToggleLaunchOnStartup(bool);
     void slotToggleOptionalServerNotifications(bool);
     void slotShowInExplorerNavigationPane(bool);
-    void slotUpdateInfo();
     void slotIgnoreFilesEditor();
     void loadMiscSettings();
     void slotShowLegalNotice();
+    void slotUpdateInfo();
+    void slotUpdateCheckNow();
+    void slotToggleAutoUpdateCheck();
 
 private:
     void customizeStyle();
index 1c7c250158933d302434b40a75fb6b478f7ba787..ebc69ebd665fd2c048572bcad4941130cebeb615 100644 (file)
      <property name="title">
       <string>Updates</string>
      </property>
-     <layout class="QHBoxLayout" name="horizontalLayout">
-      <item>
-       <widget class="QLabel" name="updateStateLabel">
-        <property name="text">
-         <string/>
-        </property>
-        <property name="wordWrap">
-         <bool>true</bool>
-        </property>
-        <property name="openExternalLinks">
-         <bool>true</bool>
-        </property>
-       </widget>
-      </item>
-      <item>
-       <widget class="QPushButton" name="restartButton">
-        <property name="sizePolicy">
-         <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
-          <horstretch>0</horstretch>
-          <verstretch>0</verstretch>
-         </sizepolicy>
-        </property>
-        <property name="text">
-         <string>&amp;Restart &amp;&amp; Update</string>
-        </property>
-       </widget>
+     <layout class="QGridLayout" name="gridLayout_4">
+      <item row="0" column="0">
+       <layout class="QHBoxLayout" name="horizontalLayout_11">
+        <item>
+         <widget class="QCheckBox" name="autoCheckForUpdatesCheckBox">
+          <property name="text">
+           <string>&amp;Automatically check for Updates</string>
+          </property>
+          <property name="checked">
+           <bool>true</bool>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="updateButton">
+          <property name="sizePolicy">
+           <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
+            <horstretch>0</horstretch>
+            <verstretch>0</verstretch>
+           </sizepolicy>
+          </property>
+          <property name="text">
+           <string>&amp;Check for Update now</string>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <spacer name="horizontalSpacer_5">
+          <property name="orientation">
+           <enum>Qt::Horizontal</enum>
+          </property>
+          <property name="sizeType">
+           <enum>QSizePolicy::Preferred</enum>
+          </property>
+          <property name="sizeHint" stdset="0">
+           <size>
+            <width>40</width>
+            <height>20</height>
+           </size>
+          </property>
+         </spacer>
+        </item>
+       </layout>
       </item>
-      <item>
-       <spacer name="horizontalSpacer">
-        <property name="orientation">
-         <enum>Qt::Horizontal</enum>
-        </property>
-        <property name="sizeType">
-         <enum>QSizePolicy::Preferred</enum>
-        </property>
-        <property name="sizeHint" stdset="0">
-         <size>
-          <width>40</width>
-          <height>20</height>
-         </size>
-        </property>
-       </spacer>
+      <item row="1" column="0">
+       <layout class="QHBoxLayout" name="horizontalLayout_2">
+        <item>
+         <widget class="QLabel" name="updateStateLabel">
+          <property name="text">
+           <string/>
+          </property>
+          <property name="wordWrap">
+           <bool>true</bool>
+          </property>
+          <property name="openExternalLinks">
+           <bool>true</bool>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="restartButton">
+          <property name="sizePolicy">
+           <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
+            <horstretch>0</horstretch>
+            <verstretch>0</verstretch>
+           </sizepolicy>
+          </property>
+          <property name="text">
+           <string>&amp;Restart &amp;&amp; Update</string>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <spacer name="horizontalSpacer">
+          <property name="orientation">
+           <enum>Qt::Horizontal</enum>
+          </property>
+          <property name="sizeType">
+           <enum>QSizePolicy::Preferred</enum>
+          </property>
+          <property name="sizeHint" stdset="0">
+           <size>
+            <width>40</width>
+            <height>20</height>
+           </size>
+          </property>
+         </spacer>
+        </item>
+       </layout>
       </item>
      </layout>
     </widget>
   <tabstop>newFolderLimitCheckBox</tabstop>
   <tabstop>newFolderLimitSpinBox</tabstop>
   <tabstop>crashreporterCheckBox</tabstop>
+  <tabstop>autoCheckForUpdatesCheckBox</tabstop>
+  <tabstop>updateButton</tabstop>
   <tabstop>restartButton</tabstop>
  </tabstops>
  <resources/>
index 90294712c067d16bf6271c726ef896a70febdcfe..21e57ef800cb25ad8b5750a7c356ed3d63c51436 100644 (file)
@@ -68,8 +68,8 @@ void UpdaterScheduler::slotTimerFired()
         qCInfo(lcUpdater) << "Setting new update check interval " << checkInterval;
     }
 
-    // consider the skipUpdateCheck flag in the config.
-    if (cfg.skipUpdateCheck()) {
+    // consider the skipUpdateCheck and !autoUpdateCheck flags in the config.
+    if (cfg.skipUpdateCheck() || !cfg.autoUpdateCheck()) {
         qCInfo(lcUpdater) << "Skipping update check because of config file";
         return;
     }
index 69de66cf4a16be85eddba329bbcc8a5fc17fcfe0..874585d41bb5c0e83e4bb9daa6722ce4e8fbea45 100644 (file)
@@ -63,6 +63,7 @@ static const char crashReporterC[] = "crashReporter";
 static const char optionalServerNotificationsC[] = "optionalServerNotifications";
 static const char showInExplorerNavigationPaneC[] = "showInExplorerNavigationPane";
 static const char skipUpdateCheckC[] = "skipUpdateCheck";
+static const char autoUpdateCheckC[] = "autoUpdateCheck";
 static const char updateCheckIntervalC[] = "updateCheckInterval";
 static const char updateSegmentC[] = "updateSegment";
 static const char geometryC[] = "geometry";
@@ -577,6 +578,32 @@ void ConfigFile::setSkipUpdateCheck(bool skip, const QString &connection)
     settings.sync();
 }
 
+bool ConfigFile::autoUpdateCheck(const QString &connection) const
+{
+    QString con(connection);
+    if (connection.isEmpty())
+        con = defaultConnection();
+
+    QVariant fallback = getValue(QLatin1String(autoUpdateCheckC), con, true);
+    fallback = getValue(QLatin1String(autoUpdateCheckC), QString(), fallback);
+
+    QVariant value = getPolicySetting(QLatin1String(autoUpdateCheckC), fallback);
+    return value.toBool();
+}
+
+void ConfigFile::setAutoUpdateCheck(bool autoCheck, const QString &connection)
+{
+    QString con(connection);
+    if (connection.isEmpty())
+        con = defaultConnection();
+
+    QSettings settings(configFile(), QSettings::IniFormat);
+    settings.beginGroup(con);
+
+    settings.setValue(QLatin1String(autoUpdateCheckC), QVariant(autoCheck));
+    settings.sync();
+}
+
 int ConfigFile::updateSegment() const
 {
     QSettings settings(configFile(), QSettings::IniFormat);
index 2fb911c9b14df650dc9009ff4f877b00ef6f8397..7b84ef89c3d6274635549906939cfee03e9f6f76 100644 (file)
@@ -146,9 +146,14 @@ public:
     // how often the check about new versions runs
     std::chrono::milliseconds updateCheckInterval(const QString &connection = QString()) const;
 
+    // skipUpdateCheck completely disables the updater and hides its UI
     bool skipUpdateCheck(const QString &connection = QString()) const;
     void setSkipUpdateCheck(bool, const QString &);
 
+    // autoUpdateCheck allows the user to make the choice in the UI
+    bool autoUpdateCheck(const QString &connection = QString()) const;
+    void setAutoUpdateCheck(bool, const QString &);
+
     /** Query-parameter 'updatesegment' for the update check, value between 0 and 99.
         Used to throttle down desktop release rollout in order to keep the update servers alive at peak times.
         See: https://github.com/nextcloud/client_updater_server/pull/36 */