Added ability to toggle autorun

This commit is contained in:
Gres 2020-03-19 22:35:42 +03:00
parent 1f478bc48a
commit 0fcdff9be2
7 changed files with 140 additions and 6 deletions

View File

@ -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 \

View File

@ -0,0 +1,95 @@
#include "runatsystemstart.h"
#include <QCoreApplication>
#include <QDir>
#include <QSettings>
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

View File

@ -0,0 +1,13 @@
#pragma once
namespace service
{
class RunAtSystemStart
{
public:
static bool isAvailable();
static bool isEnabled();
static void setEnabled(bool isOn);
};
} // namespace service

View File

@ -1,4 +1,5 @@
#include "settings.h"
#include "runatsystemstart.h"
#include <QFile>
#include <QSettings>
@ -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();
}

View File

@ -34,6 +34,7 @@ public:
QString clipboardHotkey{"Ctrl+Alt+C"};
bool showMessageOnStart{true};
bool runAtSystemStart{false};
ProxyType proxyType{ProxyType::System};
QString proxyHostName;

View File

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

View File

@ -49,8 +49,8 @@
<number>0</number>
</property>
<widget class="QWidget" name="pageGeneral">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_11">
<item row="0" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Shortcuts</string>
@ -99,7 +99,7 @@
</layout>
</widget>
</item>
<item>
<item row="1" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Proxy</string>
@ -172,21 +172,28 @@
</layout>
</widget>
</item>
<item>
<item row="2" column="0">
<widget class="QCheckBox" name="showOnStart">
<property name="text">
<string>Show message on program start</string>
</property>
</widget>
</item>
<item>
<item row="2" column="1">
<widget class="QCheckBox" name="runAtSystemStart">
<property name="text">
<string>Run at system start</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="portable">
<property name="text">
<string>Portable (store data in same folder)</string>
</property>
</widget>
</item>
<item>
<item row="4" column="1">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>