Implement Launch on Startup (Win, Linux).

Mac OS started. Untested.
This commit is contained in:
Daniel Molkentin 2013-07-05 00:25:19 +02:00
parent ef03ebe086
commit c06410e726
5 changed files with 92 additions and 4 deletions

View file

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

View file

@ -17,6 +17,7 @@
#include "mirall/theme.h"
#include "mirall/mirallconfigfile.h"
#include "mirall/application.h"
#include "mirall/utility.h"
#include <QNetworkProxy>
@ -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;

View file

@ -37,6 +37,7 @@ signals:
private slots:
void saveProxySettings();
void saveMiscSettings();
void slotToggleLaunchOnStartup(bool);
private:
void loadProxySettings();

View file

@ -14,13 +14,14 @@
#include "utility.h"
#include "mirall/version.h"
#include "mirall/theme.h"
#include <QCoreApplication>
#include <QSettings>
#include <QDir>
#include <QFile>
#include <QUrl>
#include <QWidget>
#include <QDebug>
#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

View file

@ -29,6 +29,8 @@ namespace Utility
QString platform();
QByteArray userAgentString();
void raiseDialog(QWidget *);
bool hasLaunchOnStartup();
void setLaunchOnStartup(bool launch);
}
}