From 0fcdff9be24f66a2fcf7b5c3ed30497e0212243a Mon Sep 17 00:00:00 2001 From: Gres Date: Thu, 19 Mar 2020 22:35:42 +0300 Subject: [PATCH] Added ability to toggle autorun --- screen-translator.pro | 2 + src/service/runatsystemstart.cpp | 95 ++++++++++++++++++++++++++++++++ src/service/runatsystemstart.h | 13 +++++ src/settings.cpp | 8 +++ src/settings.h | 1 + src/settingseditor.cpp | 8 +++ src/settingseditor.ui | 19 +++++-- 7 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 src/service/runatsystemstart.cpp create mode 100644 src/service/runatsystemstart.h diff --git a/screen-translator.pro b/screen-translator.pro index 7cb9f15..091fc47 100644 --- a/screen-translator.pro +++ b/screen-translator.pro @@ -43,6 +43,7 @@ HEADERS += \ src/service/apptranslator.h \ src/service/debug.h \ src/service/globalaction.h \ + src/service/runatsystemstart.h \ src/service/singleapplication.h \ src/service/updates.h \ src/service/widgetstate.h \ @@ -71,6 +72,7 @@ SOURCES += \ src/service/apptranslator.cpp \ src/service/debug.cpp \ src/service/globalaction.cpp \ + src/service/runatsystemstart.cpp \ src/service/singleapplication.cpp \ src/service/updates.cpp \ src/service/widgetstate.cpp \ diff --git a/src/service/runatsystemstart.cpp b/src/service/runatsystemstart.cpp new file mode 100644 index 0000000..37f8969 --- /dev/null +++ b/src/service/runatsystemstart.cpp @@ -0,0 +1,95 @@ +#include "runatsystemstart.h" + +#include +#include +#include + +namespace service +{ +#ifdef Q_OS_LINUX +QString desktopFile() +{ + auto name = QCoreApplication::applicationName().toLower(); + name.remove(QLatin1Char(' ')); + const auto result = QDir::homePath() + QLatin1String("/.config/autostart/") + + name + QLatin1String(".desktop"); + return result; +} + +bool RunAtSystemStart::isAvailable() +{ + return true; +} + +bool RunAtSystemStart::isEnabled() +{ + return QFile::exists(desktopFile()); +} + +void RunAtSystemStart::setEnabled(bool isOn) +{ + if (!isOn) { + QFile::remove(desktopFile()); + return; + } + + QFile f(desktopFile()); + if (!f.open(QFile::WriteOnly)) + return; + + const auto contents = QString(R"([Desktop Entry] +Name=%1 +Exec=%2 +)") + .arg(QCoreApplication::applicationName(), + QCoreApplication::applicationFilePath()); + f.write(contents.toUtf8()); +} +#endif + +#ifdef Q_OS_WIN +const auto registryKey = + "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + +bool RunAtSystemStart::isAvailable() +{ + return true; +} + +bool RunAtSystemStart::isEnabled() +{ + QSettings settings(registryKey, QSettings::NativeFormat); + return settings.contains(QCoreApplication::applicationName()); +} + +void RunAtSystemStart::setEnabled(bool isOn) +{ + QSettings settings(registryKey, QSettings::NativeFormat); + if (isOn) { + settings.setValue( + QCoreApplication::applicationName(), + QDir::toNativeSeparators(QCoreApplication::applicationFilePath())); + } else { + settings.remove(QCoreApplication::applicationName()); + } +} +#endif + +#ifdef Q_OS_MAC +bool RunAtSystemStart::isAvailable() +{ + return false; +} + +bool RunAtSystemStart::isEnabled() +{ + return false; +} + +void RunAtSystemStart::setEnabled(bool /*isOn*/) +{ + return; +} +#endif + +} // namespace service diff --git a/src/service/runatsystemstart.h b/src/service/runatsystemstart.h new file mode 100644 index 0000000..bc959a5 --- /dev/null +++ b/src/service/runatsystemstart.h @@ -0,0 +1,13 @@ +#pragma once + +namespace service +{ +class RunAtSystemStart +{ +public: + static bool isAvailable(); + static bool isEnabled(); + static void setEnabled(bool isOn); +}; + +} // namespace service diff --git a/src/settings.cpp b/src/settings.cpp index fbec81c..2cd869b 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,4 +1,5 @@ #include "settings.h" +#include "runatsystemstart.h" #include #include @@ -190,6 +191,11 @@ void Settings::save() const settings.endGroup(); + if (service::RunAtSystemStart::isAvailable()) { + if (runAtSystemStart != service::RunAtSystemStart::isEnabled()) + service::RunAtSystemStart::setEnabled(runAtSystemStart); + } + cleanupOutdated(settings); } @@ -275,6 +281,8 @@ void Settings::load() showRecognized = settings.value(qs_showRecognized, showRecognized).toBool(); showCaptured = settings.value(qs_showCaptured, showCaptured).toBool(); + runAtSystemStart = service::RunAtSystemStart::isEnabled(); + settings.endGroup(); } diff --git a/src/settings.h b/src/settings.h index 3741fb0..15b40a7 100644 --- a/src/settings.h +++ b/src/settings.h @@ -34,6 +34,7 @@ public: QString clipboardHotkey{"Ctrl+Alt+C"}; bool showMessageOnStart{true}; + bool runAtSystemStart{false}; ProxyType proxyType{ProxyType::System}; QString proxyHostName; diff --git a/src/settingseditor.cpp b/src/settingseditor.cpp index 0d097ec..b2ac2cf 100644 --- a/src/settingseditor.cpp +++ b/src/settingseditor.cpp @@ -1,6 +1,7 @@ #include "settingseditor.h" #include "languagecodes.h" #include "manager.h" +#include "runatsystemstart.h" #include "tesseract.h" #include "ui_settingseditor.h" #include "updates.h" @@ -22,6 +23,9 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater) connect(ui->portable, &QCheckBox::toggled, // this, &SettingsEditor::handlePortableChanged); + + ui->runAtSystemStart->setEnabled(service::RunAtSystemStart::isAvailable()); + { auto model = new QStringListModel(this); model->setStringList({tr("General"), tr("Recognition"), tr("Correction"), @@ -97,6 +101,8 @@ Settings SettingsEditor::settings() const Settings settings; settings.setPortable(ui->portable->isChecked()); + settings.runAtSystemStart = ui->runAtSystemStart->isChecked(); + settings.captureHotkey = ui->captureEdit->keySequence().toString(); settings.repeatCaptureHotkey = ui->repeatCaptureEdit->keySequence().toString(); @@ -151,6 +157,8 @@ void SettingsEditor::setSettings(const Settings &settings) wasPortable_ = settings.isPortable(); ui->portable->setChecked(settings.isPortable()); + ui->runAtSystemStart->setChecked(settings.runAtSystemStart); + ui->captureEdit->setKeySequence(settings.captureHotkey); ui->repeatCaptureEdit->setKeySequence(settings.repeatCaptureHotkey); ui->repeatEdit->setKeySequence(settings.showLastHotkey); diff --git a/src/settingseditor.ui b/src/settingseditor.ui index c361989..cb29a99 100644 --- a/src/settingseditor.ui +++ b/src/settingseditor.ui @@ -49,8 +49,8 @@ 0 - - + + Shortcuts @@ -99,7 +99,7 @@ - + Proxy @@ -172,21 +172,28 @@ - + Show message on program start - + + + + Run at system start + + + + Portable (store data in same folder) - + Qt::Vertical