Browse Source

Disable autostartCheckBox if autostart is configured system wide

Dominik Schmidt 7 years ago
parent
commit
5d9370594d
4 changed files with 33 additions and 2 deletions
  1. 9 0
      src/common/utility.cpp
  2. 9 0
      src/common/utility.h
  3. 7 0
      src/common/utility_win.cpp
  4. 8 2
      src/gui/generalsettings.cpp

+ 9 - 0
src/common/utility.cpp

@@ -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);

+ 9 - 0
src/common/utility.h

@@ -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);

+ 7 - 0
src/common/utility_win.cpp

@@ -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)
 {

+ 8 - 2
src/gui/generalsettings.cpp

@@ -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();