diff --git a/widgets/settings/settingsdialog.cpp b/widgets/settings/settingsdialog.cpp index 6c56982..e1d54a7 100644 --- a/widgets/settings/settingsdialog.cpp +++ b/widgets/settings/settingsdialog.cpp @@ -46,6 +46,8 @@ #include #elif defined(PLATFORM_WINDOWS) #include +#elif defined(PLATFORM_MAC) +#include #endif #include #include @@ -618,6 +620,9 @@ QWidget *AutostartOptionPage::setupWidget() ui()->platformNoteLabel->setText(QCoreApplication::translate("QtGui::AutostartOptionPage", "This is achieved by adding a registry key under HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run so the setting " "only affects the current user. Note that the startup entry is invalidated when moving syncthingtray.exe.")); +#elif defined(PLATFORM_MAC) + ui()->platformNoteLabel->setText(QCoreApplication::translate("QtGui::AutostartOptionPage", + "This is achieved by adding a *.plist file under ~/Library/LaunchAgents so the setting only affects the current user.")); #else ui()->platformNoteLabel->setText( QCoreApplication::translate("QtGui::AutostartOptionPage", "This feature has not been implemented for your platform (yet).")); @@ -636,7 +641,7 @@ bool isAutostartEnabled() { #if defined(PLATFORM_LINUX) && !defined(Q_OS_ANDROID) QFile desktopFile(QStandardPaths::locate(QStandardPaths::ConfigLocation, QStringLiteral("autostart/" PROJECT_NAME ".desktop"))); - // check whether the file can be opeed and whether it is enabled but prevent reading large files + // check whether the file can be opened and whether it is enabled but prevent reading large files if (desktopFile.open(QFile::ReadOnly) && (desktopFile.size() > (5 * 1024) || !desktopFile.readAll().contains("Hidden=true"))) { return true; } @@ -644,6 +649,8 @@ bool isAutostartEnabled() #elif defined(PLATFORM_WINDOWS) QSettings settings(QStringLiteral("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), QSettings::NativeFormat); return settings.contains(QStringLiteral(PROJECT_NAME)); +#elif defined(PLATFORM_MAC) + return QFileInfo(QDir::home(), QStringLiteral("Library/LaunchAgents/" PROJECT_NAME ".plist")).isReadable(); #else return false; #endif @@ -710,6 +717,37 @@ bool setAutostartEnabled(bool enabled) } settings.sync(); return true; +#elif defined(PLATFORM_MAC) + const QString libraryPath(QDir::home().filePath("Library")); + if (enabled && !QDir().mkpath(libraryPath + QStringLiteral("/LaunchAgents"))) { + return false; + } + QFile launchdPlistFile(libraryPath + QStringLiteral("/LaunchAgents/" PROJECT_NAME ".plist")); + if (enabled) { + if (!launchdPlistFile.open(QFile::WriteOnly | QFile::Truncate)) { + return false; + } + launchdPlistFile.write("\n" + "\n" + "\n" + " \n" + " Label\n" + " " PROJECT_NAME "\n" + " ProgramArguments\n" + " \n" + " "); + launchdPlistFile.write(QCoreApplication::applicationFilePath().toUtf8().data()); + launchdPlistFile.write("\n" + " \n" + " KeepAlive\n" + " \n" + " \n" + "\n"); + return launchdPlistFile.error() == QFile::NoError && launchdPlistFile.flush(); + + } else { + return !launchdPlistFile.exists() || launchdPlistFile.remove(); + } #endif }