Commit 983588a9 authored by Ronan's avatar Ronan

Merge branch 'feature/autostart'

parents da77d41e 766add98
......@@ -1599,6 +1599,10 @@ Server URL ist nicht konfiguriert.</translation>
<source>dataTitle</source>
<translation>UI-Daten</translation>
</message>
<message>
<source>autoStartLabel</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -1604,6 +1604,10 @@ your friend&apos;s SIP address or username.</translation>
<source>dataTitle</source>
<translation>UI Data</translation>
</message>
<message>
<source>autoStartLabel</source>
<translation>Autostart app</translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -1602,6 +1602,10 @@ Cliquez ici : &lt;a href=&quot;%1&quot;&gt;%1&lt;/a&gt;
<source>dataTitle</source>
<translation>Données</translation>
</message>
<message>
<source>autoStartLabel</source>
<translation>Démarrer auto. l&apos;app</translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -1599,6 +1599,10 @@
<source>dataTitle</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>autoStartLabel</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -1604,6 +1604,10 @@ Tiesiog, įveskite savo draugo SIP adresą ar naudotojo vardą.</translation>
<source>dataTitle</source>
<translation>Naudotojo sąsajos duomenys</translation>
</message>
<message>
<source>autoStartLabel</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -1604,6 +1604,10 @@ o endereço SIP ou nome de usuário do seu amigo.</translation>
<source>dataTitle</source>
<translation>Dados UI</translation>
</message>
<message>
<source>autoStartLabel</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -1602,6 +1602,10 @@
<source>dataTitle</source>
<translation>Данные пользовательского интерфейса</translation>
</message>
<message>
<source>autoStartLabel</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -1602,6 +1602,10 @@ Klicka här: &lt;a href=&quot;%1&quot;&gt;%1&lt;/a&gt;
<source>dataTitle</source>
<translation>Användargränssnitt data</translation>
</message>
<message>
<source>autoStartLabel</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -1604,6 +1604,10 @@ arkadaşınızın SIP adresini veya kullanıcı adını girin.</translation>
<source>dataTitle</source>
<translation>Kullanıcı Arayüzü Verisi</translation>
</message>
<message>
<source>autoStartLabel</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>SettingsVideo</name>
......
......@@ -31,6 +31,10 @@
#include <QSystemTrayIcon>
#include <QTimer>
#ifdef Q_OS_WIN
#include <QSettings>
#endif // ifdef Q_OS_WIN
#include "config.h"
#include "cli/Cli.hpp"
......@@ -70,8 +74,86 @@ namespace {
constexpr char AboutPath[] = "qrc:/ui/views/App/Main/Dialogs/About.qml";
constexpr char AssistantViewName[] = "Assistant";
#ifdef Q_OS_LINUX
QString AutoStartDirectory(
QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).at(0) + QLatin1String("/autostart/")
);
#elif defined(Q_OS_MACOS)
QString OsascriptExecutable("osascript");
#else
QString AutoStartSettingsFilePath("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
#endif // ifdef Q_OS_LINUX
}
// -----------------------------------------------------------------------------
#ifdef Q_OS_LINUX
static inline bool autoStartEnabled () {
return QDir(AutoStartDirectory).exists() && QFile(AutoStartDirectory + EXECUTABLE_NAME ".desktop").exists();
}
#elif defined(Q_OS_MACOS)
static inline QString getMacOsBundlePath () {
QDir dir(QCoreApplication::applicationDirPath());
if (dir.dirName() != QLatin1String("MacOS"))
return QString();
dir.cdUp();
dir.cdUp();
QString path(dir.path());
if (path.length() > 0 && path.right(1) == "/")
path.chop(1);
return path;
}
static inline QString getMacOsBundleName () {
return QFileInfo(getMacOsBundlePath()).baseName();
}
static inline bool autoStartEnabled () {
const QByteArray expectedWord(getMacOsBundleName().toUtf8());
if (expectedWord.isEmpty()) {
qInfo() << QStringLiteral("Application is not installed. Autostart unavailable.");
return false;
}
QProcess process;
process.start(OsascriptExecutable, { "-e", "tell application \"System Events\" to get the name of every login item" });
if (!process.waitForFinished()) {
qWarning() << QStringLiteral("Unable to execute properly: `%1` (%2).").arg(OsascriptExecutable).arg(process.errorString());
return false;
}
// TODO: Move in utils?
const QByteArray buf(process.readAll());
for (const char *p = buf.data(), *word = p, *end = p + buf.length(); p <= end; ++p) {
switch (*p) {
case ' ':
case '\r':
case '\n':
case '\t':
case '\0':
if (word != p) {
if (!strncmp(word, expectedWord, size_t(p - word)))
return true;
word = p + 1;
}
default:
break;
}
}
return false;
}
#else
static inline bool autoStartEnabled () {
return QSettings(AutoStartSettingsFilePath, QSettings::NativeFormat).value(EXECUTABLE_NAME).isValid();
}
#endif // ifdef Q_OS_LINUX
// -----------------------------------------------------------------------------
static inline bool installLocale (App &app, QTranslator &translator, const QLocale &locale) {
return translator.load(locale, LanguagePath) && app.installTranslator(&translator);
}
......@@ -125,6 +207,8 @@ App::App (int &argc, char *argv[]) : SingleApplication(argc, argv, true, Mode::U
if (mParser->isSet("version"))
mParser->showVersion();
mAutoStart = autoStartEnabled();
qInfo() << QStringLiteral("Starting " APPLICATION_NAME " (bin: " EXECUTABLE_NAME ")");
qInfo() << QStringLiteral("Use locale: %1").arg(mLocale);
}
......@@ -578,6 +662,100 @@ QString App::getLocale () const {
// -----------------------------------------------------------------------------
#ifdef Q_OS_LINUX
void App::setAutoStart (bool enabled) {
if (enabled == mAutoStart)
return;
QDir dir(AutoStartDirectory);
if (!dir.exists() && !dir.mkpath(AutoStartDirectory)) {
qWarning() << QStringLiteral("Unable to build autostart dir path: `%1`.").arg(AutoStartDirectory);
return;
}
QFile file(AutoStartDirectory + EXECUTABLE_NAME ".desktop");
if (!enabled) {
if (file.exists() && !file.remove()) {
qWarning() << QLatin1String("Unable to remove autostart file: `" EXECUTABLE_NAME ".desktop`.");
return;
}
mAutoStart = enabled;
emit autoStartChanged(enabled);
return;
}
if (!file.open(QFile::WriteOnly)) {
qWarning() << "Unable to open autostart file: `" EXECUTABLE_NAME ".desktop`.";
return;
}
QString fileContent(
"[Desktop Entry]\n"
"Name=" APPLICATION_NAME "\n"
"GenericName=SIP Phone\n"
"Comment=" APPLICATION_DESCRIPTION "\n"
"Type=Application\n"
"Exec=" + applicationFilePath() + "\n"
"Icon=\n"
"Terminal=false\n"
"Categories=Network;Telephony;\n"
"MimeType=x-scheme-handler/sip-linphone;x-scheme-handler/sip;x-scheme-handler/sips-linphone;x-scheme-handler/sips;\n"
);
QTextStream out(&file);
out << fileContent;
mAutoStart = enabled;
emit autoStartChanged(enabled);
}
#elif defined(Q_OS_MACOS)
void App::setAutoStart (bool enabled) {
if (enabled == mAutoStart)
return;
if (getMacOsBundlePath().isEmpty()) {
qWarning() << QStringLiteral("Application is not installed. Unable to change autostart state.");
return;
}
if (enabled)
QProcess::execute(OsascriptExecutable, {
"-e", "tell application \"System Events\" to make login item at end with properties"
"{ path: \"" + getMacOsBundlePath() + "\", hidden: false }"
});
else
QProcess::execute(OsascriptExecutable, {
"-e", "tell application \"System Events\" to delete login item \"" + getMacOsBundleName() + "\""
});
mAutoStart = enabled;
emit autoStartChanged(enabled);
}
#else
void App::setAutoStart (bool enabled) {
if (enabled == mAutoStart)
return;
QSettings settings(AutoStartSettingsFilePath, QSettings::NativeFormat);
if (enabled)
settings.setValue(EXECUTABLE_NAME, QDir::toNativeSeparators(applicationFilePath()));
else
settings.remove(EXECUTABLE_NAME);
mAutoStart = enabled;
emit autoStartChanged(enabled);
}
#endif // ifdef Q_OS_LINUX
// -----------------------------------------------------------------------------
void App::openAppAfterInit (bool mustBeIconified) {
qInfo() << QStringLiteral("Open " APPLICATION_NAME " app.");
......
......@@ -50,6 +50,8 @@ class App : public SingleApplication {
Q_PROPERTY(QVariantList availableLocales READ getAvailableLocales CONSTANT);
Q_PROPERTY(QString qtVersion READ getQtVersion CONSTANT);
Q_PROPERTY(bool autoStart READ getAutoStart WRITE setAutoStart NOTIFY autoStartChanged);
public:
App (int &argc, char *argv[]);
~App ();
......@@ -100,6 +102,8 @@ public:
signals:
void configLocaleChanged (const QString &locale);
void autoStartChanged (bool enabled);
private:
void createParser ();
......@@ -122,6 +126,12 @@ private:
return mAvailableLocales;
}
bool getAutoStart () const {
return mAutoStart;
}
void setAutoStart (bool enabled);
void openAppAfterInit (bool mustBeIconified = false);
static void checkForUpdate ();
......@@ -133,6 +143,8 @@ private:
QVariantList mAvailableLocales;
QString mLocale;
bool mAutoStart = false;
QCommandLineParser *mParser = nullptr;
QQmlApplicationEngine *mEngine = nullptr;
......
......@@ -21,6 +21,7 @@
*******************************************************************************/
#cmakedefine APPLICATION_NAME "${APPLICATION_NAME}"
#cmakedefine APPLICATION_DESCRIPTION "${APPLICATION_DESCRIPTION}"
#cmakedefine ENABLE_UPDATE_CHECK 1
#cmakedefine EXECUTABLE_NAME "${EXECUTABLE_NAME}"
#cmakedefine MSPLUGINS_DIR "${MSPLUGINS_DIR}"
......@@ -146,6 +146,16 @@ TabContainer {
onClicked: SettingsModel.exitOnClose = !checked
}
}
FormGroup {
label: qsTr('autoStartLabel')
Switch {
checked: App.autoStart
onClicked: App.autoStart = !checked
}
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment