diff --git a/src/mirall/application.cpp b/src/mirall/application.cpp index cd8f17bd8..35cc05385 100644 --- a/src/mirall/application.cpp +++ b/src/mirall/application.cpp @@ -695,6 +695,7 @@ void Application::slotSettings() { if (_settingsDialog.isNull()) { _settingsDialog = new SettingsDialog(this); + connect(_settingsDialog, SIGNAL(accepted()), _settingsDialog, SLOT(deleteLater())); _settingsDialog->open(); } else { Utility::raiseDialog(_settingsDialog); diff --git a/src/mirall/generalsettings.cpp b/src/mirall/generalsettings.cpp index 99530f58a..2902a7fc1 100644 --- a/src/mirall/generalsettings.cpp +++ b/src/mirall/generalsettings.cpp @@ -17,6 +17,7 @@ #include "mirall/theme.h" #include "mirall/mirallconfigfile.h" #include "mirall/application.h" +#include "mirall/utility.h" #include @@ -39,7 +40,8 @@ GeneralSettings::GeneralSettings(QWidget *parent) : // not implemented yet _ui->desktopNotificationsCheckBox->setEnabled(false); - _ui->autostartCheckBox->setEnabled(false); + _ui->autostartCheckBox->setChecked(Utility::hasLaunchOnStartup()); + connect(_ui->autostartCheckBox, SIGNAL(toggled(bool)), SLOT(slotToggleLaunchOnStartup(bool))); // setup about section QString about = Theme::instance()->about(); @@ -127,6 +129,11 @@ void GeneralSettings::saveMiscSettings() Theme::instance()->setSystrayUseMonoIcons(isChecked); } +void GeneralSettings::slotToggleLaunchOnStartup(bool enable) +{ + Utility::setLaunchOnStartup(enable); +} + void GeneralSettings::saveProxySettings() { MirallConfigFile cfgFile; diff --git a/src/mirall/generalsettings.h b/src/mirall/generalsettings.h index 1e6af3aa9..26fc76bf2 100644 --- a/src/mirall/generalsettings.h +++ b/src/mirall/generalsettings.h @@ -37,6 +37,7 @@ signals: private slots: void saveProxySettings(); void saveMiscSettings(); + void slotToggleLaunchOnStartup(bool); private: void loadProxySettings(); diff --git a/src/mirall/utility.cpp b/src/mirall/utility.cpp index c2d583ecc..bbc8e94f9 100644 --- a/src/mirall/utility.cpp +++ b/src/mirall/utility.cpp @@ -14,13 +14,14 @@ #include "utility.h" #include "mirall/version.h" +#include "mirall/theme.h" #include +#include #include #include #include #include - #include #ifdef Q_OS_MAC @@ -87,8 +88,6 @@ void Utility::setupFavLink(const QString &folder) gtkBookmarks.reset(); gtkBookmarks.write(places + '\n'); } - - } #endif @@ -161,4 +160,82 @@ void Utility::raiseDialog( QWidget *raiseWidget ) } } +static const char runPathC[] = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + +bool Utility::hasLaunchOnStartup() +{ + QString appName = Theme::instance()->appName(); +#if defined(Q_OS_WIN) + QString runPath(QLatin1String(runPathC)); + QSettings settings(runPath, QSettings::NativeFormat); + return settings.contains(appName); +#elif defined(Q_OS_MAC) +// implement me +#elif defined(Q_OS_UNIX) + QString userAutoStartPath = QDir::homePath()+QLatin1String("/.config/autostart/"); + QString desktopFileLocation = userAutoStartPath+appName+QLatin1String(".desktop"); + return QFile::exists(desktopFileLocation); +#endif +} + +void Utility::setLaunchOnStartup(bool enable) +{ + QString appName = Theme::instance()->appName(); +#if defined(Q_OS_WIN) + QString runPath(QLatin1String(runPathC)); + QSettings settings(runPath, QSettings::NativeFormat); + if (enable) { + settings.setValue(appName, QCoreApplication::applicationFilePath().replace('/','\\')); + } else { + settings.remove(appName); + } +#elif defined(Q_OS_MAC) + if (enable) { + // Finder: Place under "Places"/"Favorites" on the left sidebar + QString filePath = QDir(applicationDirPath+QLatin1String("/../..")).absolutePath(); + CFStringRef folderCFStr = CFStringCreateWithCString(0, filePath.toUtf8().data(), kCFStringEncodingUTF8); + CFURLRef urlRef = CFURLCreateWithFileSystemPath (0, folderCFStr, kCFURLPOSIXPathStyle, true); + + LSSharedFileListRef loginItems = LSSharedFileListCreate(0, kLSSharedFileListSessionLoginItems, 0); + if (loginItems) { + //Insert an item to the list. + LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems, + kLSSharedFileListItemLast, 0, 0, + urlRef, 0, 0); + if (item) + CFRelease(item); + } + CFRelease(loginItems); + CFRelease(folderCFStr); + CFRelease(urlRef); + } else { + // implement me + } +#elif defined(Q_OS_UNIX) + QString userAutoStartPath = QDir::homePath()+QLatin1String("/.config/autostart/"); + QString desktopFileLocation = userAutoStartPath+appName+QLatin1String(".desktop"); + if (enable) { + if (!QDir().exists(userAutoStartPath) && !QDir().mkdir(userAutoStartPath)) { + qDebug() << "Could not create autostart directory"; + return; + } + QSettings desktopFile(desktopFileLocation, QSettings::IniFormat); + desktopFile.beginGroup("Desktop Entry"); + desktopFile.setValue(QLatin1String("Name"), Theme::instance()->appNameGUI()); + desktopFile.setValue(QLatin1String("GenericName"), QLatin1String("File Synchronizer")); + desktopFile.setValue(QLatin1String("Exec"), QCoreApplication::applicationFilePath()); + desktopFile.setValue(QLatin1String("Terminal"), false); + desktopFile.setValue(QLatin1String("Icon"), appName); + desktopFile.setValue(QLatin1String("Categories"), QLatin1String("Network")); + desktopFile.setValue(QLatin1String("StartupNotify"), false); + desktopFile.endGroup(); + } else { + if (!QFile::remove(desktopFileLocation)) { + qDebug() << "Could not remove autostart desktop file"; + } + } + +#endif +} + } // namespace Mirall diff --git a/src/mirall/utility.h b/src/mirall/utility.h index 0858f774c..c978b78d0 100644 --- a/src/mirall/utility.h +++ b/src/mirall/utility.h @@ -29,6 +29,8 @@ namespace Utility QString platform(); QByteArray userAgentString(); void raiseDialog(QWidget *); + bool hasLaunchOnStartup(); + void setLaunchOnStartup(bool launch); } }