Add possibility to autorun Fernschreiber at startup

This commit is contained in:
Sebastian Wolf 2020-12-12 00:12:28 +01:00
parent ce8047028d
commit c771f395da
14 changed files with 189 additions and 0 deletions

View file

@ -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 { TextSwitch {
checked: appSettings.stayInBackground checked: appSettings.stayInBackground
text: qsTr("Stay in background") text: qsTr("Stay in background")

View file

@ -23,10 +23,16 @@
#include <QDBusInterface> #include <QDBusInterface>
#include <QDBusReply> #include <QDBusReply>
#include <QGuiApplication> #include <QGuiApplication>
#include <QDir>
#include <QStandardPaths>
#include <QFile>
#include <QFileInfo>
#include <QProcess>
namespace { namespace {
const QString KEY_SEND_BY_ENTER("sendByEnter"); const QString KEY_SEND_BY_ENTER("sendByEnter");
const QString KEY_USE_OPEN_WITH("useOpenWith"); const QString KEY_USE_OPEN_WITH("useOpenWith");
const QString KEY_AUTO_RUN("autoRun");
const QString KEY_STAY_IN_BACKGROUND("stayInBackground"); const QString KEY_STAY_IN_BACKGROUND("stayInBackground");
const QString KEY_SHOW_STICKERS_AS_IMAGES("showStickersAsImages"); const QString KEY_SHOW_STICKERS_AS_IMAGES("showStickersAsImages");
const QString KEY_ANIMATE_STICKERS("animateStickers"); const QString KEY_ANIMATE_STICKERS("animateStickers");
@ -37,6 +43,7 @@ namespace {
AppSettings::AppSettings(QObject *parent) : QObject(parent), settings("harbour-fernschreiber", "settings") AppSettings::AppSettings(QObject *parent) : QObject(parent), settings("harbour-fernschreiber", "settings")
{ {
getAutoRun() ? initializeAutoRun() : disableAutoRun();
} }
bool AppSettings::getSendByEnter() const 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 bool AppSettings::getStayInBackground() const
{ {
return settings.value(KEY_STAY_IN_BACKGROUND, false).toBool(); return settings.value(KEY_STAY_IN_BACKGROUND, false).toBool();
@ -168,3 +190,64 @@ bool AppSettings::isAppRunning()
return false; 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());
}
}

View file

@ -26,6 +26,7 @@ class AppSettings : public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool sendByEnter READ getSendByEnter WRITE setSendByEnter NOTIFY sendByEnterChanged) Q_PROPERTY(bool sendByEnter READ getSendByEnter WRITE setSendByEnter NOTIFY sendByEnterChanged)
Q_PROPERTY(bool useOpenWith READ getUseOpenWith WRITE setUseOpenWith NOTIFY useOpenWithChanged) 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 stayInBackground READ getStayInBackground WRITE setStayInBackground NOTIFY stayInBackgroundChanged)
Q_PROPERTY(bool showStickersAsImages READ showStickersAsImages WRITE setShowStickersAsImages NOTIFY showStickersAsImagesChanged) Q_PROPERTY(bool showStickersAsImages READ showStickersAsImages WRITE setShowStickersAsImages NOTIFY showStickersAsImagesChanged)
Q_PROPERTY(bool animateStickers READ animateStickers WRITE setAnimateStickers NOTIFY animateStickersChanged) Q_PROPERTY(bool animateStickers READ animateStickers WRITE setAnimateStickers NOTIFY animateStickersChanged)
@ -50,6 +51,9 @@ public:
bool getUseOpenWith() const; bool getUseOpenWith() const;
void setUseOpenWith(bool useOpenWith); void setUseOpenWith(bool useOpenWith);
bool getAutoRun() const;
void setAutoRun(bool autoRun);
bool getStayInBackground() const; bool getStayInBackground() const;
void setStayInBackground(bool stayInBackground); void setStayInBackground(bool stayInBackground);
@ -73,6 +77,7 @@ public:
signals: signals:
void sendByEnterChanged(); void sendByEnterChanged();
void useOpenWithChanged(); void useOpenWithChanged();
void autoRunChanged();
void stayInBackgroundChanged(); void stayInBackgroundChanged();
void showStickersAsImagesChanged(); void showStickersAsImagesChanged();
void animateStickersChanged(); void animateStickersChanged();
@ -82,6 +87,9 @@ signals:
private: private:
QSettings settings; QSettings settings;
void initializeAutoRun();
void disableAutoRun();
}; };
#endif // APPSETTINGS_H #endif // APPSETTINGS_H

View file

@ -1302,6 +1302,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1302,6 +1302,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1283,6 +1283,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1303,6 +1303,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1283,6 +1283,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1302,6 +1302,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1321,6 +1321,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1321,6 +1321,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1302,6 +1302,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1283,6 +1283,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>

View file

@ -1302,6 +1302,14 @@
<source>Fernschreiber will stay active in the background after the app was closed</source> <source>Fernschreiber will stay active in the background after the app was closed</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Autorun at startup</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Fernschreiber will automatically be run at startup</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>StickerPicker</name> <name>StickerPicker</name>