Disable autostartCheckBox if autostart is configured system wide
authorDominik Schmidt <dev@dominik-schmidt.de>
Tue, 16 Oct 2018 17:18:01 +0000 (10:18 -0700)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:14 +0000 (10:58 +0100)
src/common/utility.cpp
src/common/utility.h
src/common/utility_win.cpp
src/gui/generalsettings.cpp

index 3c6fe06b4e29a54eb114d99587b8a5bb5e87a701..1225b6e4a3197ae121b2005b0e7a6eb5f305072d 100644 (file)
@@ -197,6 +197,15 @@ QByteArray Utility::friendlyUserAgentString()
     return userAgent.toUtf8();
 }
 
+bool Utility::hasSystemLaunchOnStartup(const QString &appName)
+{
+#if defined(Q_OS_WIN)
+    return hasSystemLaunchOnStartup_private(appName);
+#else
+    return false;
+#endif
+}
+
 bool Utility::hasLaunchOnStartup(const QString &appName)
 {
     return hasLaunchOnStartup_private(appName);
index fafc1b3bcefc0bd2a49963f07e04011553cff9ce..3985704e39eecaa38166b2c9b0077cd37496fab6 100644 (file)
@@ -58,6 +58,15 @@ namespace Utility {
     OCSYNC_EXPORT QString octetsToString(qint64 octets);
     OCSYNC_EXPORT QByteArray userAgentString();
     OCSYNC_EXPORT QByteArray friendlyUserAgentString();
+    /**
+      * @brief Return whether launch on startup is enabled system wide.
+      *
+      * If this returns true, the checkbox for user specific launch
+      * on startup will be hidden.
+      *
+      * Currently only implemented on Windows.
+      */
+    OCSYNC_EXPORT bool hasSystemLaunchOnStartup(const QString &appName);
     OCSYNC_EXPORT bool hasLaunchOnStartup(const QString &appName);
     OCSYNC_EXPORT void setLaunchOnStartup(const QString &appName, const QString &guiName, bool launch);
     OCSYNC_EXPORT uint convertSizeToUint(size_t &convertVar);
index bfe9d7852e4d6fc245746918ca5f735da5111c5b..d8eae7931327864540189fb6354dd2b3a4cda12b 100644 (file)
@@ -25,6 +25,7 @@
 #include <string>
 #include <QLibrary>
 
+static const char systemRunPathC[] = R"(HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run)";
 static const char runPathC[] = R"(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run)";
 
 namespace OCC {
@@ -67,6 +68,12 @@ static void setupFavLink_private(const QString &folder)
         qCWarning(lcUtility) << "linking" << folder << "to" << linkName << "failed!";
 }
 
+bool hasSystemLaunchOnStartup_private(const QString &appName)
+{
+    QString runPath = QLatin1String(systemRunPathC);
+    QSettings settings(runPath, QSettings::NativeFormat);
+    return settings.contains(appName);
+}
 
 bool hasLaunchOnStartup_private(const QString &appName)
 {
index 2b37a205332c9a6171350629b9815d2e1e3f4f4e..bb98f1db175ebb5c1481fd6818aaa20fe74485ce 100644 (file)
@@ -157,8 +157,14 @@ GeneralSettings::GeneralSettings(QWidget *parent)
     _ui->showInExplorerNavigationPaneCheckBox->setText(txt);
 #endif
 
-    _ui->autostartCheckBox->setChecked(Utility::hasLaunchOnStartup(Theme::instance()->appName()));
-    connect(_ui->autostartCheckBox, &QAbstractButton::toggled, this, &GeneralSettings::slotToggleLaunchOnStartup);
+    if(Utility::hasSystemLaunchOnStartup(Theme::instance()->appName())) {
+        _ui->autostartCheckBox->setChecked(true);
+        _ui->autostartCheckBox->setDisabled(true);
+        _ui->autostartCheckBox->setToolTip(tr("You cannot disable autostart because system-wide autostart is enabled."));
+    } else {
+        _ui->autostartCheckBox->setChecked(Utility::hasLaunchOnStartup(Theme::instance()->appName()));
+        connect(_ui->autostartCheckBox, &QAbstractButton::toggled, this, &GeneralSettings::slotToggleLaunchOnStartup);
+    }
 
     // setup about section
     QString about = Theme::instance()->about();