From 749b73cbb6d13422a5793f113f0bcb1db520c3ba Mon Sep 17 00:00:00 2001 From: lupoDharkael Date: Sat, 30 Dec 2017 14:35:22 +0100 Subject: [PATCH] Add autostart option --- src/config/geneneralconf.cpp | 24 ++++++++++++++++++++-- src/config/geneneralconf.h | 11 ++++++---- src/utils/confighandler.cpp | 40 ++++++++++++++++++++++++++++++++++++ src/utils/confighandler.h | 3 +++ 4 files changed, 72 insertions(+), 6 deletions(-) diff --git a/src/config/geneneralconf.cpp b/src/config/geneneralconf.cpp index 2c84cf76..17850ef1 100644 --- a/src/config/geneneralconf.cpp +++ b/src/config/geneneralconf.cpp @@ -35,6 +35,8 @@ GeneneralConf::GeneneralConf(QWidget *parent) : QGroupBox(parent) { initShowHelp(); initShowDesktopNotification(); initShowTrayIcon(); + initAutostart(); + initConfingButtons(); updateComponents(); } @@ -43,6 +45,7 @@ void GeneneralConf::updateComponents() { ConfigHandler config; m_helpMessage->setChecked(config.showHelpValue()); m_sysNotifications->setChecked(config.desktopNotificationValue()); + m_autostart->setChecked(config.startupLaunchValue()); #ifdef Q_OS_LINUX m_showTray->setChecked(!config.disabledTrayIconValue()); #endif @@ -62,7 +65,11 @@ void GeneneralConf::showTrayIconChanged(bool checked) { controller->enableTrayIcon(); } else { controller->disableTrayIcon(); - } + } +} + +void GeneneralConf::autostartChanged(bool checked) { + ConfigHandler().setStartupLaunch(checked); } void GeneneralConf::importConfiguration() { @@ -164,5 +171,18 @@ void GeneneralConf::initConfingButtons() { m_resetButton = new QPushButton(tr("Reset")); buttonLayout->addWidget(m_resetButton); connect(m_resetButton, &QPushButton::clicked, this, - &GeneneralConf::resetConfiguration); + &GeneneralConf::resetConfiguration); +} + +void GeneneralConf::initAutostart() { + m_autostart = + new QCheckBox(tr("Launch at startup"), this); + ConfigHandler config; + bool checked = config.startupLaunchValue(); + m_autostart->setChecked(checked); + m_autostart->setToolTip(tr("Launch Flameshot ")); + m_layout->addWidget(m_autostart); + + connect(m_autostart, &QCheckBox::clicked, this, + &GeneneralConf::autostartChanged); } diff --git a/src/config/geneneralconf.h b/src/config/geneneralconf.h index 0f17ee6b..a1191da5 100644 --- a/src/config/geneneralconf.h +++ b/src/config/geneneralconf.h @@ -36,6 +36,7 @@ private slots: void showHelpChanged(bool checked); void showDesktopNotificationChanged(bool checked); void showTrayIconChanged(bool checked); + void autostartChanged(bool checked); void importConfiguration(); void exportConfiguration(); void resetConfiguration(); @@ -45,14 +46,16 @@ private: QCheckBox *m_sysNotifications; QCheckBox *m_showTray; QCheckBox *m_helpMessage; - QPushButton *m_importButton; - QPushButton *m_exportButton; - QPushButton *m_resetButton; + QCheckBox *m_autostart; + QPushButton *m_importButton; + QPushButton *m_exportButton; + QPushButton *m_resetButton; void initShowHelp(); void initShowDesktopNotification(); void initShowTrayIcon(); - void initConfingButtons(); + void initConfingButtons(); + void initAutostart(); }; diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index ba8951af..9a9635cf 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -17,6 +17,8 @@ #include "confighandler.h" #include +#include +#include ConfigHandler::ConfigHandler(){ m_settings.setDefaultFormat(QSettings::IniFormat); @@ -117,6 +119,44 @@ void ConfigHandler::setKeepOpenAppLauncher(const bool keepOpen) { m_settings.setValue("keepOpenAppLauncher", keepOpen); } +bool ConfigHandler::startupLaunchValue() { + bool res = false; +#if defined(Q_OS_LINUX) + QString path = QDir::homePath() + "/.config/autostart/Flameshot.desktop"; + res = QFile(path).exists(); +#elif defined(Q_OS_WIN) + QSettings bootUpSettings( + "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", + QSettings::NativeFormat); + res = !bootUpSettings.value("Flameshot").toString().isEmpty(); +#endif + return res; +} + +void ConfigHandler::setStartupLaunch(const bool start) { +#if defined(Q_OS_LINUX) + QString path = QDir::homePath() + "/.config/autostart/Flameshot.desktop"; + QFile file(path); + if (start) { + if (file.open(QIODevice::WriteOnly)) { + file.write("[Desktop Entry]\nIcon=system-run\nExec=flameshot\nTerminal=false"); + } + } else { + file.remove(); + } +#elif defined(Q_OS_WIN) + QSettings bootUpSettings( + "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", + QSettings::NativeFormat); + if (start) { + QString app_path = QCoreApplication::applicationFilePath(); + bootUpSettings.setValue("Flameshot", app_path); + } else { + bootUpSettings.remove("Flameshot"); + } +#endif +} + bool ConfigHandler::initiatedIsSet() { return m_settings.value("initiated").toBool(); } diff --git a/src/utils/confighandler.h b/src/utils/confighandler.h index 6623ba92..8902f73b 100644 --- a/src/utils/confighandler.h +++ b/src/utils/confighandler.h @@ -60,6 +60,9 @@ public: bool keepOpenAppLauncherValue(); void setKeepOpenAppLauncher(const bool); + bool startupLaunchValue(); + void setStartupLaunch(const bool); + bool initiatedIsSet(); void setInitiated(); void setNotInitiated();