From af2a4b0be7c7783affbe8234c8fbb71abe1ae310 Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Wed, 9 Dec 2020 23:57:09 +0100 Subject: [PATCH] First experiments with keeping me alive in the background --- qml/pages/SettingsPage.qml | 10 ++++++ src/appsettings.cpp | 35 +++++++++++++++++++++ src/appsettings.h | 8 +++++ src/dbusadaptor.cpp | 12 +++++++ src/dbusadaptor.h | 7 +++++ src/harbour-fernschreiber.cpp | 17 ++++++++-- src/tdlibwrapper.cpp | 6 ---- src/tdlibwrapper.h | 3 -- 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 +++++ 19 files changed, 174 insertions(+), 12 deletions(-) diff --git a/qml/pages/SettingsPage.qml b/qml/pages/SettingsPage.qml index e9abe19..eb93de1 100644 --- a/qml/pages/SettingsPage.qml +++ b/qml/pages/SettingsPage.qml @@ -62,6 +62,16 @@ Page { } } + TextSwitch { + checked: appSettings.stayInBackground + text: qsTr("Stay in background") + description: qsTr("Fernschreiber will stay active in the background after the app was closed") + automaticCheck: false + onClicked: { + appSettings.stayInBackground = !checked + } + } + ComboBox { id: feedbackComboBox label: qsTr("Notification feedback") diff --git a/src/appsettings.cpp b/src/appsettings.cpp index 497578f..d91a1c1 100644 --- a/src/appsettings.cpp +++ b/src/appsettings.cpp @@ -20,9 +20,13 @@ #define DEBUG_MODULE AppSettings #include "debuglog.h" +#include +#include + namespace { const QString KEY_SEND_BY_ENTER("sendByEnter"); const QString KEY_USE_OPEN_WITH("useOpenWith"); + const QString KEY_STAY_IN_BACKGROUND("stayInBackground"); const QString KEY_SHOW_STICKERS_AS_IMAGES("showStickersAsImages"); const QString KEY_ANIMATE_STICKERS("animateStickers"); const QString KEY_NOTIFICATION_TURNS_DISPLAY_ON("notificationTurnsDisplayOn"); @@ -62,6 +66,20 @@ void AppSettings::setUseOpenWith(bool useOpenWith) } } +bool AppSettings::getStayInBackground() const +{ + return settings.value(KEY_STAY_IN_BACKGROUND, false).toBool(); +} + +void AppSettings::setStayInBackground(bool stayInBackground) +{ + if (getStayInBackground() != stayInBackground) { + LOG(KEY_STAY_IN_BACKGROUND << stayInBackground); + settings.setValue(KEY_STAY_IN_BACKGROUND, stayInBackground); + emit stayInBackgroundChanged(); + } +} + bool AppSettings::showStickersAsImages() const { return settings.value(KEY_SHOW_STICKERS_AS_IMAGES, true).toBool(); @@ -131,3 +149,20 @@ void AppSettings::setStorageOptimizer(bool enable) emit storageOptimizerChanged(); } } + +bool AppSettings::isAppRunning() +{ + LOG("Checking via D-Bus if app is already running..."); + QDBusInterface dBusInterface("de.ygriega.fernschreiber", "/de/ygriega/fernschreiber", "", QDBusConnection::sessionBus()); + if (dBusInterface.isValid()) { + QDBusReply reply = dBusInterface.call("showUI"); + if (reply.isValid()) { + return reply.value(); + } + LOG("D-Bus call to show UI failed. App doesn't seem to be running (properly)!" << reply.error().message()); + return false; + } else { + LOG("Fernschreiber D-Bus session interface is not existing. App doesn't seem to be running!"); + return false; + } +} diff --git a/src/appsettings.h b/src/appsettings.h index bb63cdf..5d0e63a 100644 --- a/src/appsettings.h +++ b/src/appsettings.h @@ -20,11 +20,13 @@ #include #include +#include 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 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) Q_PROPERTY(bool notificationTurnsDisplayOn READ notificationTurnsDisplayOn WRITE setNotificationTurnsDisplayOn NOTIFY notificationTurnsDisplayOnChanged) @@ -48,6 +50,9 @@ public: bool getUseOpenWith() const; void setUseOpenWith(bool useOpenWith); + bool getStayInBackground() const; + void setStayInBackground(bool stayInBackground); + bool showStickersAsImages() const; void setShowStickersAsImages(bool showAsImages); @@ -63,9 +68,12 @@ public: bool storageOptimizer() const; void setStorageOptimizer(bool enable); + bool isAppRunning(); + signals: void sendByEnterChanged(); void useOpenWithChanged(); + void stayInBackgroundChanged(); void showStickersAsImagesChanged(); void animateStickersChanged(); void notificationTurnsDisplayOnChanged(); diff --git a/src/dbusadaptor.cpp b/src/dbusadaptor.cpp index 16e17ed..01fa14c 100644 --- a/src/dbusadaptor.cpp +++ b/src/dbusadaptor.cpp @@ -26,6 +26,11 @@ DBusAdaptor::DBusAdaptor(QObject *parent): QDBusAbstractAdaptor(parent) { } +void DBusAdaptor::setAppView(QQuickView *appView) +{ + this->appView = appView; +} + void DBusAdaptor::openMessage(const QString &chatId, const QString &messageId) { LOG("Open Message" << chatId << messageId); @@ -39,3 +44,10 @@ void DBusAdaptor::openUrl(const QStringList &arguments) emit pleaseOpenUrl(arguments.first()); } } + +bool DBusAdaptor::showUI() +{ + LOG("UI shall wake up!"); + this->appView->showFullScreen(); + return true; +} diff --git a/src/dbusadaptor.h b/src/dbusadaptor.h index 750775f..25998e6 100644 --- a/src/dbusadaptor.h +++ b/src/dbusadaptor.h @@ -21,6 +21,7 @@ #define DBUSADAPTOR_H #include +#include class DBusAdaptor : public QDBusAbstractAdaptor { @@ -31,6 +32,8 @@ class DBusAdaptor : public QDBusAbstractAdaptor public: DBusAdaptor(QObject *parent); + void setAppView(QQuickView* appView); + signals: void pleaseOpenMessage(const QString &chatId, const QString &messageId); void pleaseOpenUrl(const QString &url); @@ -38,6 +41,10 @@ signals: public slots: void openMessage(const QString &chatId, const QString &messageId); void openUrl(const QStringList &arguments); + bool showUI(); + +private: + QQuickView *appView; }; diff --git a/src/harbour-fernschreiber.cpp b/src/harbour-fernschreiber.cpp index a2efd4f..ab9aa57 100644 --- a/src/harbour-fernschreiber.cpp +++ b/src/harbour-fernschreiber.cpp @@ -39,6 +39,7 @@ #include "namedaction.h" #include "notificationmanager.h" #include "mceinterface.h" +#include "dbusinterface.h" #include "dbusadaptor.h" #include "processlauncher.h" #include "stickermanager.h" @@ -75,6 +76,19 @@ int main(int argc, char *argv[]) context->setContextProperty("appSettings", appSettings); qmlRegisterUncreatableType(uri, 1, 0, "AppSettings", QString()); +// if (appSettings->isAppRunning()) { +// return 0; +// } + + if (appSettings->getStayInBackground()) { + app.data()->setQuitOnLastWindowClosed(false); + } + + DBusInterface *dBusInterface = new DBusInterface(view.data()); + DBusAdaptor *dBusAdaptor = dBusInterface->getDBusAdaptor(); + dBusAdaptor->setAppView(view.data()); + context->setContextProperty("dBusAdaptor", dBusAdaptor); + MceInterface *mceInterface = new MceInterface(view.data()); TDLibWrapper *tdLibWrapper = new TDLibWrapper(appSettings, mceInterface, view.data()); context->setContextProperty("tdLibWrapper", tdLibWrapper); @@ -83,9 +97,6 @@ int main(int argc, char *argv[]) FernschreiberUtils *fernschreiberUtils = new FernschreiberUtils(view.data()); context->setContextProperty("fernschreiberUtils", fernschreiberUtils); - DBusAdaptor *dBusAdaptor = tdLibWrapper->getDBusAdaptor(); - context->setContextProperty("dBusAdaptor", dBusAdaptor); - ChatListModel chatListModel(tdLibWrapper); context->setContextProperty("chatListModel", &chatListModel); diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 8128178..cc6c15c 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -63,7 +63,6 @@ TDLibWrapper::TDLibWrapper(AppSettings *appSettings, MceInterface *mceInterface, tdLibDatabaseDirectory.mkpath(tdLibDatabaseDirectoryPath); } - this->dbusInterface = new DBusInterface(this); if (this->appSettings->getUseOpenWith()) { this->initializeOpenWith(); } else { @@ -1043,11 +1042,6 @@ void TDLibWrapper::registerJoinChat() this->joinChatRequested = false; } -DBusAdaptor *TDLibWrapper::getDBusAdaptor() -{ - return this->dbusInterface->getDBusAdaptor(); -} - void TDLibWrapper::handleVersionDetected(const QString &version) { this->version = version; diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index fd9520e..ff0728c 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -117,8 +117,6 @@ public: Q_INVOKABLE bool getJoinChatRequested(); Q_INVOKABLE void registerJoinChat(); - DBusAdaptor *getDBusAdaptor(); - // Direct TDLib functions Q_INVOKABLE void sendRequest(const QVariantMap &requestObject); Q_INVOKABLE void setAuthenticationPhoneNumber(const QString &phoneNumber); @@ -278,7 +276,6 @@ private: AppSettings *appSettings; MceInterface *mceInterface; TDLibReceiver *tdLibReceiver; - DBusInterface *dbusInterface; QString version; TDLibWrapper::AuthorizationState authorizationState; QVariantMap authorizationStateData; diff --git a/translations/harbour-fernschreiber-de.ts b/translations/harbour-fernschreiber-de.ts index 7a6cd82..c4f99ae 100644 --- a/translations/harbour-fernschreiber-de.ts +++ b/translations/harbour-fernschreiber-de.ts @@ -1294,6 +1294,14 @@ Enable storage optimizer Speicheroptimierer einschalten + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-en.ts b/translations/harbour-fernschreiber-en.ts index 5d5ce04..36c8b6d 100644 --- a/translations/harbour-fernschreiber-en.ts +++ b/translations/harbour-fernschreiber-en.ts @@ -1294,6 +1294,14 @@ Enable storage optimizer Enable storage optimizer + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-es.ts b/translations/harbour-fernschreiber-es.ts index d03fc54..b5606c7 100644 --- a/translations/harbour-fernschreiber-es.ts +++ b/translations/harbour-fernschreiber-es.ts @@ -1275,6 +1275,14 @@ Enable storage optimizer Optimizador de almacenamiento + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-fi.ts b/translations/harbour-fernschreiber-fi.ts index 852b3db..e77ab2c 100644 --- a/translations/harbour-fernschreiber-fi.ts +++ b/translations/harbour-fernschreiber-fi.ts @@ -1295,6 +1295,14 @@ Enable storage optimizer Käytä tallennustilan optimointia + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-hu.ts b/translations/harbour-fernschreiber-hu.ts index 1fe058b..ccc73b6 100644 --- a/translations/harbour-fernschreiber-hu.ts +++ b/translations/harbour-fernschreiber-hu.ts @@ -1275,6 +1275,14 @@ Enable storage optimizer + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-it.ts b/translations/harbour-fernschreiber-it.ts index 462b213..7b72825 100644 --- a/translations/harbour-fernschreiber-it.ts +++ b/translations/harbour-fernschreiber-it.ts @@ -1294,6 +1294,14 @@ Enable storage optimizer Abilita ottimizzazione memoria + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-pl.ts b/translations/harbour-fernschreiber-pl.ts index b288bf1..d720271 100644 --- a/translations/harbour-fernschreiber-pl.ts +++ b/translations/harbour-fernschreiber-pl.ts @@ -1313,6 +1313,14 @@ Enable storage optimizer Włącz optymalizację pamięci + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-ru.ts b/translations/harbour-fernschreiber-ru.ts index db06186..09ee1e3 100644 --- a/translations/harbour-fernschreiber-ru.ts +++ b/translations/harbour-fernschreiber-ru.ts @@ -1313,6 +1313,14 @@ Enable storage optimizer Включить оптимизацию хранилища + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-sv.ts b/translations/harbour-fernschreiber-sv.ts index 94844e1..18aa3fd 100644 --- a/translations/harbour-fernschreiber-sv.ts +++ b/translations/harbour-fernschreiber-sv.ts @@ -1294,6 +1294,14 @@ Enable storage optimizer Aktivera lagringsoptimering + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber-zh_CN.ts b/translations/harbour-fernschreiber-zh_CN.ts index 1e922cc..873aca5 100644 --- a/translations/harbour-fernschreiber-zh_CN.ts +++ b/translations/harbour-fernschreiber-zh_CN.ts @@ -1275,6 +1275,14 @@ Enable storage optimizer 启用储存加速器 + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker diff --git a/translations/harbour-fernschreiber.ts b/translations/harbour-fernschreiber.ts index 6251eb4..54be337 100644 --- a/translations/harbour-fernschreiber.ts +++ b/translations/harbour-fernschreiber.ts @@ -1294,6 +1294,14 @@ Enable storage optimizer + + Stay in background + + + + Fernschreiber will stay active in the background after the app was closed + + StickerPicker