From c771f395dad2c2b32729593f667913fd884f6393 Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Sat, 12 Dec 2020 00:12:28 +0100 Subject: [PATCH] Add possibility to autorun Fernschreiber at startup --- qml/pages/SettingsPage.qml | 10 +++ src/appsettings.cpp | 83 +++++++++++++++++++++ src/appsettings.h | 8 ++ translations/harbour-fernschreiber-de.ts | 8 ++ translations/harbour-fernschreiber-en.ts | 8 ++ translations/harbour-fernschreiber-es.ts | 8 ++ translations/harbour-fernschreiber-fi.ts | 8 ++ translations/harbour-fernschreiber-hu.ts | 8 ++ translations/harbour-fernschreiber-it.ts | 8 ++ translations/harbour-fernschreiber-pl.ts | 8 ++ translations/harbour-fernschreiber-ru.ts | 8 ++ translations/harbour-fernschreiber-sv.ts | 8 ++ translations/harbour-fernschreiber-zh_CN.ts | 8 ++ translations/harbour-fernschreiber.ts | 8 ++ 14 files changed, 189 insertions(+) diff --git a/qml/pages/SettingsPage.qml b/qml/pages/SettingsPage.qml index eb93de1..1a198b7 100644 --- a/qml/pages/SettingsPage.qml +++ b/qml/pages/SettingsPage.qml @@ -62,6 +62,16 @@ Page { } } + TextSwitch { + checked: appSettings.autoRun + text: qsTr("Autorun at startup") + description: qsTr("Fernschreiber will automatically be run at startup") + automaticCheck: false + onClicked: { + appSettings.autoRun = !checked + } + } + TextSwitch { checked: appSettings.stayInBackground text: qsTr("Stay in background") diff --git a/src/appsettings.cpp b/src/appsettings.cpp index 0522560..6f02719 100644 --- a/src/appsettings.cpp +++ b/src/appsettings.cpp @@ -23,10 +23,16 @@ #include #include #include +#include +#include +#include +#include +#include namespace { const QString KEY_SEND_BY_ENTER("sendByEnter"); const QString KEY_USE_OPEN_WITH("useOpenWith"); + const QString KEY_AUTO_RUN("autoRun"); const QString KEY_STAY_IN_BACKGROUND("stayInBackground"); const QString KEY_SHOW_STICKERS_AS_IMAGES("showStickersAsImages"); const QString KEY_ANIMATE_STICKERS("animateStickers"); @@ -37,6 +43,7 @@ namespace { AppSettings::AppSettings(QObject *parent) : QObject(parent), settings("harbour-fernschreiber", "settings") { + getAutoRun() ? initializeAutoRun() : disableAutoRun(); } bool AppSettings::getSendByEnter() const @@ -67,6 +74,21 @@ void AppSettings::setUseOpenWith(bool useOpenWith) } } +bool AppSettings::getAutoRun() const +{ + return settings.value(KEY_AUTO_RUN, false).toBool(); +} + +void AppSettings::setAutoRun(bool autoRun) +{ + if (getAutoRun() != autoRun) { + LOG(KEY_AUTO_RUN << autoRun); + settings.setValue(KEY_AUTO_RUN, autoRun); + autoRun ? initializeAutoRun() : disableAutoRun(); + emit autoRunChanged(); + } +} + bool AppSettings::getStayInBackground() const { return settings.value(KEY_STAY_IN_BACKGROUND, false).toBool(); @@ -168,3 +190,64 @@ bool AppSettings::isAppRunning() return false; } } + +void AppSettings::initializeAutoRun() +{ + LOG("Initialize Auto-Run..."); + + QDir scriptsSystemDDir(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/systemd/scripts"); + if (!scriptsSystemDDir.exists()) { + scriptsSystemDDir.mkpath(scriptsSystemDDir.path()); + } + QFile autorunAppFile(scriptsSystemDDir.path() + "/autorun-fernschreiber"); + if (!autorunAppFile.exists()) { + LOG("Creating autorun app file at " << autorunAppFile.fileName()); + if (autorunAppFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + QTextStream appFileOut(&autorunAppFile); + appFileOut.setCodec("UTF-8"); + appFileOut << QString("#!/bin/sh -").toUtf8() << "\n"; + appFileOut << QString("").toUtf8() << "\n"; + appFileOut << QString("/usr/bin/invoker -n -s --type=silica-qt5 /usr/bin/harbour-fernschreiber").toUtf8() << "\n"; + appFileOut.flush(); + autorunAppFile.close(); + QProcess::startDetached("chmod u+x " + QFileInfo(autorunAppFile).filePath()); + } + } + + QDir userSystemDDir(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/systemd/user"); + if (!userSystemDDir.exists()) { + userSystemDDir.mkpath(userSystemDDir.path()); + } + QFile autorunServiceFile(userSystemDDir.path() + "/autorun-fernschreiber.service"); + if (!autorunServiceFile.exists()) { + LOG("Creating autorun service file at " << autorunServiceFile.fileName()); + if (autorunServiceFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + QTextStream serviceFileOut(&autorunServiceFile); + serviceFileOut.setCodec("UTF-8"); + serviceFileOut << QString("[Unit]").toUtf8() << "\n"; + serviceFileOut << QString("Description=Autorun Fernschreiber at startup").toUtf8() << "\n"; + serviceFileOut << QString("Requires=lipstick.service").toUtf8() << "\n"; + serviceFileOut << QString("After=lipstick.service").toUtf8() << "\n"; + serviceFileOut << QString("").toUtf8() << "\n"; + serviceFileOut << QString("[Service]").toUtf8() << "\n"; + serviceFileOut << QString("Type=oneshot").toUtf8() << "\n"; + serviceFileOut << QString("ExecStart=" + QFileInfo(autorunAppFile).filePath()).toUtf8() << "\n"; + serviceFileOut << QString("").toUtf8() << "\n"; + serviceFileOut << QString("[Install]").toUtf8() << "\n"; + serviceFileOut << QString("WantedBy=post-user-session.target").toUtf8() << "\n"; + serviceFileOut.flush(); + autorunServiceFile.close(); + } + } + + QProcess::startDetached("systemctl --user enable " + QFileInfo(autorunServiceFile).filePath()); +} + +void AppSettings::disableAutoRun() +{ + LOG("Disabling Auto-Run..."); + QFile autorunServiceFile(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/systemd/user/autorun-fernschreiber.service"); + if (autorunServiceFile.exists()) { + QProcess::startDetached("systemctl --user disable " + QFileInfo(autorunServiceFile).filePath()); + } +} diff --git a/src/appsettings.h b/src/appsettings.h index 5d0e63a..20b2186 100644 --- a/src/appsettings.h +++ b/src/appsettings.h @@ -26,6 +26,7 @@ class AppSettings : public QObject { Q_OBJECT Q_PROPERTY(bool sendByEnter READ getSendByEnter WRITE setSendByEnter NOTIFY sendByEnterChanged) Q_PROPERTY(bool useOpenWith READ getUseOpenWith WRITE setUseOpenWith NOTIFY useOpenWithChanged) + Q_PROPERTY(bool autoRun READ getAutoRun WRITE setAutoRun NOTIFY autoRunChanged) Q_PROPERTY(bool stayInBackground READ getStayInBackground WRITE setStayInBackground NOTIFY stayInBackgroundChanged) Q_PROPERTY(bool showStickersAsImages READ showStickersAsImages WRITE setShowStickersAsImages NOTIFY showStickersAsImagesChanged) Q_PROPERTY(bool animateStickers READ animateStickers WRITE setAnimateStickers NOTIFY animateStickersChanged) @@ -50,6 +51,9 @@ public: bool getUseOpenWith() const; void setUseOpenWith(bool useOpenWith); + bool getAutoRun() const; + void setAutoRun(bool autoRun); + bool getStayInBackground() const; void setStayInBackground(bool stayInBackground); @@ -73,6 +77,7 @@ public: signals: void sendByEnterChanged(); void useOpenWithChanged(); + void autoRunChanged(); void stayInBackgroundChanged(); void showStickersAsImagesChanged(); void animateStickersChanged(); @@ -82,6 +87,9 @@ signals: private: QSettings settings; + + void initializeAutoRun(); + void disableAutoRun(); }; #endif // APPSETTINGS_H diff --git a/translations/harbour-fernschreiber-de.ts b/translations/harbour-fernschreiber-de.ts index c4f99ae..3e16983 100644 --- a/translations/harbour-fernschreiber-de.ts +++ b/translations/harbour-fernschreiber-de.ts @@ -1302,6 +1302,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-en.ts b/translations/harbour-fernschreiber-en.ts index 36c8b6d..b00d677 100644 --- a/translations/harbour-fernschreiber-en.ts +++ b/translations/harbour-fernschreiber-en.ts @@ -1302,6 +1302,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-es.ts b/translations/harbour-fernschreiber-es.ts index b5606c7..946d855 100644 --- a/translations/harbour-fernschreiber-es.ts +++ b/translations/harbour-fernschreiber-es.ts @@ -1283,6 +1283,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-fi.ts b/translations/harbour-fernschreiber-fi.ts index e77ab2c..1068761 100644 --- a/translations/harbour-fernschreiber-fi.ts +++ b/translations/harbour-fernschreiber-fi.ts @@ -1303,6 +1303,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-hu.ts b/translations/harbour-fernschreiber-hu.ts index ccc73b6..b550a55 100644 --- a/translations/harbour-fernschreiber-hu.ts +++ b/translations/harbour-fernschreiber-hu.ts @@ -1283,6 +1283,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-it.ts b/translations/harbour-fernschreiber-it.ts index 7b72825..025a964 100644 --- a/translations/harbour-fernschreiber-it.ts +++ b/translations/harbour-fernschreiber-it.ts @@ -1302,6 +1302,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-pl.ts b/translations/harbour-fernschreiber-pl.ts index d720271..375ec45 100644 --- a/translations/harbour-fernschreiber-pl.ts +++ b/translations/harbour-fernschreiber-pl.ts @@ -1321,6 +1321,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-ru.ts b/translations/harbour-fernschreiber-ru.ts index 09ee1e3..e26f7d8 100644 --- a/translations/harbour-fernschreiber-ru.ts +++ b/translations/harbour-fernschreiber-ru.ts @@ -1321,6 +1321,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-sv.ts b/translations/harbour-fernschreiber-sv.ts index 18aa3fd..99177b9 100644 --- a/translations/harbour-fernschreiber-sv.ts +++ b/translations/harbour-fernschreiber-sv.ts @@ -1302,6 +1302,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber-zh_CN.ts b/translations/harbour-fernschreiber-zh_CN.ts index 873aca5..b2399ef 100644 --- a/translations/harbour-fernschreiber-zh_CN.ts +++ b/translations/harbour-fernschreiber-zh_CN.ts @@ -1283,6 +1283,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker diff --git a/translations/harbour-fernschreiber.ts b/translations/harbour-fernschreiber.ts index 54be337..26ce04a 100644 --- a/translations/harbour-fernschreiber.ts +++ b/translations/harbour-fernschreiber.ts @@ -1302,6 +1302,14 @@ Fernschreiber will stay active in the background after the app was closed + + Autorun at startup + + + + Fernschreiber will automatically be run at startup + + StickerPicker