From 3d48125371309ca3be32fdb58b4a938cc3266168 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Mon, 23 Nov 2020 00:32:00 +0200 Subject: [PATCH] Add MceInterface object --- harbour-fernschreiber.pro | 2 ++ src/harbour-fernschreiber.cpp | 6 ++-- src/mceinterface.cpp | 54 +++++++++++++++++++++++++++++++++++ src/mceinterface.h | 37 ++++++++++++++++++++++++ src/notificationmanager.cpp | 15 +++++----- src/notificationmanager.h | 6 ++-- src/tdlibwrapper.cpp | 13 +++------ src/tdlibwrapper.h | 4 ++- 8 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 src/mceinterface.cpp create mode 100644 src/mceinterface.h diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index a3d7524..c1d46ae 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -28,6 +28,7 @@ SOURCES += src/harbour-fernschreiber.cpp \ src/dbusinterface.cpp \ src/emojisearchworker.cpp \ src/fernschreiberutils.cpp \ + src/mceinterface.cpp \ src/notificationmanager.cpp \ src/processlauncher.cpp \ src/stickermanager.cpp \ @@ -150,6 +151,7 @@ HEADERS += \ src/debuglogjs.h \ src/emojisearchworker.h \ src/fernschreiberutils.h \ + src/mceinterface.h \ src/notificationmanager.h \ src/processlauncher.h \ src/stickermanager.h \ diff --git a/src/harbour-fernschreiber.cpp b/src/harbour-fernschreiber.cpp index b7a14bd..1c54a3e 100644 --- a/src/harbour-fernschreiber.cpp +++ b/src/harbour-fernschreiber.cpp @@ -36,6 +36,7 @@ #include "chatlistmodel.h" #include "chatmodel.h" #include "notificationmanager.h" +#include "mceinterface.h" #include "dbusadaptor.h" #include "processlauncher.h" #include "stickermanager.h" @@ -69,7 +70,8 @@ int main(int argc, char *argv[]) context->setContextProperty("appSettings", appSettings); qmlRegisterUncreatableType(uri, 1, 0, "AppSettings", QString()); - TDLibWrapper *tdLibWrapper = new TDLibWrapper(appSettings, view.data()); + MceInterface *mceInterface = new MceInterface(view.data()); + TDLibWrapper *tdLibWrapper = new TDLibWrapper(appSettings, mceInterface, view.data()); context->setContextProperty("tdLibWrapper", tdLibWrapper); qmlRegisterUncreatableType(uri, 1, 0, "TelegramAPI", QString()); @@ -85,7 +87,7 @@ int main(int argc, char *argv[]) ChatModel chatModel(tdLibWrapper); context->setContextProperty("chatModel", &chatModel); - NotificationManager notificationManager(tdLibWrapper, appSettings, &chatModel); + NotificationManager notificationManager(tdLibWrapper, appSettings, mceInterface, &chatModel); context->setContextProperty("notificationManager", ¬ificationManager); ProcessLauncher processLauncher; diff --git a/src/mceinterface.cpp b/src/mceinterface.cpp new file mode 100644 index 0000000..87393b5 --- /dev/null +++ b/src/mceinterface.cpp @@ -0,0 +1,54 @@ +/* + Copyright (C) 2020 Slava Monich et al. + + This file is part of Fernschreiber. + + Fernschreiber is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Fernschreiber is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fernschreiber. If not, see . +*/ + +#include "mceinterface.h" +#include + +#define DEBUG_MODULE MceInterface +#include "debuglog.h" + +MceInterface::MceInterface(QObject *parent) : + QDBusInterface("com.nokia.mce", "/com/nokia/mce/request", "com.nokia.mce.request", + QDBusConnection::systemBus(), parent) +{ +} + +void MceInterface::ledPatternActivate(const QString &pattern) +{ + LOG("Activating pattern" << pattern); + call(QStringLiteral("req_led_pattern_activate"), pattern); +} + +void MceInterface::ledPatternDeactivate(const QString &pattern) +{ + LOG("Deactivating pattern" << pattern); + call(QStringLiteral("req_led_pattern_deactivate"), pattern); +} + +void MceInterface::displayCancelBlankingPause() +{ + LOG("Enabling display blanking"); + call(QStringLiteral("req_display_cancel_blanking_pause")); +} + +void MceInterface::displayBlankingPause() +{ + LOG("Disabling display blanking"); + call(QStringLiteral("req_display_blanking_pause")); +} diff --git a/src/mceinterface.h b/src/mceinterface.h new file mode 100644 index 0000000..8d0ee64 --- /dev/null +++ b/src/mceinterface.h @@ -0,0 +1,37 @@ +/* + Copyright (C) 2020 Slava Monich et al. + + This file is part of Fernschreiber. + + Fernschreiber is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Fernschreiber is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fernschreiber. If not, see . +*/ + +#ifndef MCE_INTERFACE_H +#define MCE_INTERFACE_H + +#include + +class MceInterface : public QDBusInterface +{ +public: + MceInterface(QObject *parent = Q_NULLPTR); + + void ledPatternActivate(const QString &pattern); + void ledPatternDeactivate(const QString &pattern); + void displayCancelBlankingPause(); + void displayBlankingPause(); +}; + +#endif // MCE_INTERFACE_H + diff --git a/src/notificationmanager.cpp b/src/notificationmanager.cpp index d87bc91..69ec7c1 100644 --- a/src/notificationmanager.cpp +++ b/src/notificationmanager.cpp @@ -120,14 +120,13 @@ NotificationManager::NotificationGroup::~NotificationGroup() delete nemoNotification; } -NotificationManager::NotificationManager(TDLibWrapper *tdLibWrapper, AppSettings *appSettings, ChatModel *chatModel) : - mceInterface("com.nokia.mce", "/com/nokia/mce/request", "com.nokia.mce.request", QDBusConnection::systemBus()), +NotificationManager::NotificationManager(TDLibWrapper *tdLibWrapper, AppSettings *appSettings, MceInterface *mceInterface, ChatModel *chatModel) : appIconFile(SailfishApp::pathTo("images/fernschreiber-notification.png").toLocalFile()) { LOG("Initializing..."); this->tdLibWrapper = tdLibWrapper; this->appSettings = appSettings; - this->appSettings = appSettings; + this->mceInterface = mceInterface; this->chatModel = chatModel; connect(this->tdLibWrapper, SIGNAL(activeNotificationsUpdated(QVariantList)), this, SLOT(handleUpdateActiveNotifications(QVariantList))); @@ -383,9 +382,9 @@ QString NotificationManager::getNotificationText(const QVariantMap ¬ification void NotificationManager::controlLedNotification(bool enabled) { static const QString PATTERN("PatternCommunicationIM"); - static const QString ACTIVATE("req_led_pattern_activate"); - static const QString DEACTIVATE("req_led_pattern_deactivate"); - - LOG("Controlling notification LED" << enabled); - mceInterface.call(enabled ? ACTIVATE : DEACTIVATE, PATTERN); + if (enabled) { + mceInterface->ledPatternActivate(PATTERN); + } else { + mceInterface->ledPatternDeactivate(PATTERN); + } } diff --git a/src/notificationmanager.h b/src/notificationmanager.h index 79e242f..57c66eb 100644 --- a/src/notificationmanager.h +++ b/src/notificationmanager.h @@ -21,10 +21,10 @@ #define NOTIFICATIONMANAGER_H #include -#include #include #include "tdlibwrapper.h" #include "appsettings.h" +#include "mceinterface.h" class ChatModel; @@ -36,7 +36,7 @@ class NotificationManager : public QObject public: - NotificationManager(TDLibWrapper *tdLibWrapper, AppSettings *appSettings, ChatModel *chatModel); + NotificationManager(TDLibWrapper *tdLibWrapper, AppSettings *appSettings, MceInterface *mceInterface, ChatModel *chatModel); ~NotificationManager() override; public slots: @@ -61,10 +61,10 @@ private: TDLibWrapper *tdLibWrapper; AppSettings *appSettings; + MceInterface *mceInterface; ChatModel *chatModel; QMap chatMap; QMap notificationGroups; - QDBusInterface mceInterface; QString appIconFile; }; diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index df6f71c..6de724c 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -41,10 +41,11 @@ namespace { const QString _EXTRA("@extra"); } -TDLibWrapper::TDLibWrapper(AppSettings *appSettings, QObject *parent) : QObject(parent), joinChatRequested(false) +TDLibWrapper::TDLibWrapper(AppSettings *appSettings, MceInterface *mceInterface, QObject *parent) : QObject(parent), joinChatRequested(false) { LOG("Initializing TD Lib..."); this->appSettings = appSettings; + this->mceInterface = mceInterface; this->tdLibClient = td_json_client_create(); this->tdLibReceiver = new TDLibReceiver(this->tdLibClient, this); @@ -892,16 +893,10 @@ void TDLibWrapper::openFileOnDevice(const QString &filePath) void TDLibWrapper::controlScreenSaver(bool enabled) { - LOG("Controlling device screen saver" << enabled); - QDBusConnection dbusConnection = QDBusConnection::connectToBus(QDBusConnection::SystemBus, "system"); - QDBusInterface dbusInterface("com.nokia.mce", "/com/nokia/mce/request", "com.nokia.mce.request", dbusConnection); - if (enabled) { - LOG("Enabling screensaver"); - dbusInterface.call("req_display_cancel_blanking_pause"); + mceInterface->displayCancelBlankingPause(); } else { - LOG("Disabling screensaver"); - dbusInterface.call("req_display_blanking_pause"); + mceInterface->displayBlankingPause(); } } diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 0f02595..93d9f34 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -26,12 +26,13 @@ #include "dbusinterface.h" #include "emojisearchworker.h" #include "appsettings.h" +#include "mceinterface.h" class TDLibWrapper : public QObject { Q_OBJECT public: - explicit TDLibWrapper(AppSettings *appSettings, QObject *parent = nullptr); + explicit TDLibWrapper(AppSettings *appSettings, MceInterface *mceInterface, QObject *parent = nullptr); ~TDLibWrapper(); enum AuthorizationState { @@ -251,6 +252,7 @@ private: private: void *tdLibClient; AppSettings *appSettings; + MceInterface *mceInterface; TDLibReceiver *tdLibReceiver; DBusInterface *dbusInterface; QString version;