UniversalSwitchSentinel 81/210881/5
authorLukasz Wlazly <l.wlazly@partner.samsung.com>
Thu, 25 Jul 2019 13:27:03 +0000 (15:27 +0200)
committerLukasz Wlazly <l.wlazly@partner.samsung.com>
Tue, 30 Jul 2019 06:49:33 +0000 (08:49 +0200)
Change-Id: I86e2f189ab068c72d7ad8e1f061b038e9912a060

src/model/UniversalSwitchPageModel.cpp
src/model/UniversalSwitchPageModel.hpp
src/model/UniversalSwitchSettingsPageModel.cpp
src/model/UniversalSwitchSettingsPageModel.hpp
src/presenter/UniversalSwitchPagePresenter.cpp
src/presenter/UniversalSwitchPagePresenter.hpp
src/service/UniversalSwitchSentinel.cpp [new file with mode: 0644]
src/service/UniversalSwitchSentinel.hpp [new file with mode: 0644]

index cf1b331..b1f7380 100644 (file)
@@ -4,28 +4,8 @@
 
 UniversalSwitchPageModel::UniversalSwitchPageModel()
 {
-       interactionServiceStateHandle_ = Singleton<VConfInterface>::instance().registerAndGet<bool>(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, false, [this](auto state) {
-               this->interactionServiceState_ = state;
-       });
-
-       interactionServiceState_.attach([](auto val) {
-               Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, val);
-       });
-
-       configurationServiceStateHandle_ = Singleton<VConfInterface>::instance().registerAndGet<bool>(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, false, [this](auto state) {
-               this->configurationServiceState_ = state;
-       });
-
-       configurationServiceState_.attach([this](auto val) {
-               Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, val);
-               if (!val)
-                       this->dbusConfigurationServiceState_ = false;
-       });
-
        connectionPtr_ = DBus::getDBusConnectionByType(DBus::ConnectionType::SESSION);
-       ownerNameCallbackHandle_ = DBus::registerNameOwnerChangedCallback(connectionPtr_, BUS, [this](std::string oldID, std::string newID) {
-               dbusConfigurationServiceState_ = true;
-       });
+       sentinel_ = Singleton<UniversalSwitchSentinelService>::instance().getSentinel();
 }
 
 bool UniversalSwitchPageModel::atLeastOneSwitchConfigured()
index be8fd73..99acedb 100644 (file)
@@ -3,21 +3,17 @@
 
 #include "DBus.hpp"
 #include "ObservableProperty.hpp"
+#include "UniversalSwitchSentinel.hpp"
 #include "VConf.hpp"
 
 class UniversalSwitchPageModel
 {
        public:
        UniversalSwitchPageModel();
-       ObservableProperty<bool> interactionServiceState_;
-       ObservableProperty<bool> configurationServiceState_;
-       ObservableProperty<bool> dbusConfigurationServiceState_;
        bool atLeastOneSwitchConfigured();
+       UniversalSwitchSentinel sentinel_;
 
        private:
-       VConfInterface::CallbackHandle interactionServiceStateHandle_;
-       VConfInterface::CallbackHandle configurationServiceStateHandle_;
-       DBus::OwnerNameCallbackHandle ownerNameCallbackHandle_;
        DBus::DBusWrapper::ConnectionPtr connectionPtr_;
 };
 
index ad1be51..ebc9f2d 100644 (file)
@@ -12,6 +12,7 @@ UniversalSwitchSettingsPageModel::UniversalSwitchSettingsPageModel()
        dBusClient_.addSignal<void()>(std::string{"switchesConfigurationChanged"}, [this]() {
                this->getConfiguredSwitchesCount();
        });
+       sentinel_ = Singleton<UniversalSwitchSentinelService>::instance().getSentinel();
        getConfiguredSwitchesCount();
 }
 
index 54301a3..24ea2a6 100644 (file)
@@ -4,6 +4,7 @@
 #include "DBus.hpp"
 #include "Geometry.hpp"
 #include "ObservableProperty.hpp"
+#include "UniversalSwitchSentinel.hpp"
 #include "UniversalSwitchTypes.hpp"
 #include "VConf.hpp"
 
@@ -13,8 +14,8 @@ class UniversalSwitchSettingsPageModel
        UniversalSwitchSettingsPageModel();
 
        ObservableProperty<int> configuredSwitchesCount_;
-
        ObservableProperty<bool> autoTapKeyboardState_;
+       UniversalSwitchSentinel sentinel_;
 
        private:
        template <typename T>
index fc103b1..3022415 100644 (file)
@@ -10,13 +10,13 @@ UniversalSwitchPagePresenter::UniversalSwitchPagePresenter()
        groups_.emplace_back("");
        auto &items = groups_.back().items_;
        items.push_back(std::make_unique<ListItem>(
-               model_.interactionServiceState_.value() ? "IDS_ST_BODY_ON" : "IDS_ST_BODY_OFF",
+               model_.sentinel_->interactionServiceState_.value() ? "IDS_ST_BODY_ON" : "IDS_ST_BODY_OFF",
                std::string{},
                [this](auto item) {
                        if (!item->widgetState_.value())
-                               model_.interactionServiceState_ = false;
+                               model_.sentinel_->setInteractionServiceState(false);
                        else if (model_.atLeastOneSwitchConfigured())
-                               model_.interactionServiceState_ = true;
+                               model_.sentinel_->setInteractionServiceState(true);
                        else {
                                item->widgetState_ = false;
                                DEBUG("No switch configured");
@@ -26,8 +26,8 @@ UniversalSwitchPagePresenter::UniversalSwitchPagePresenter()
                ListItem::WidgetType::toggle,
                std::function<void(ListItem * item)>{},
                std::function<void(ListItem * item)>{},
-               model_.interactionServiceState_.value()));
-       items.back()->enabled_ = model_.dbusConfigurationServiceState_.value();
+               model_.sentinel_->interactionServiceState_.value()));
+       items.back()->enabled_ = model_.sentinel_->configurationServiceState_.value();
 
        items.push_back(std::make_unique<ListItem>(
                std::string{},
@@ -37,21 +37,17 @@ UniversalSwitchPagePresenter::UniversalSwitchPagePresenter()
        addAction(std::make_unique<Action>("titleRightAction", "IDS_ACCS_SETTINGS_CAPS", [this](auto action) {
                Singleton<AppContext>::instance().push(std::make_unique<UniversalSwitchSettingsPagePresenter>());
        },
-               model_.dbusConfigurationServiceState_.value()));
+               model_.sentinel_->configurationServiceState_.value()));
 
-       model_.dbusConfigurationServiceState_.attach([this, &items](auto value) {
+       model_.sentinel_->configurationServiceState_.attach([this, &items](auto value) {
+               ERROR("value: %d", value);
                this->getAction("titleRightAction")->enabled_ = value;
                items.front()->enabled_ = value;
        });
-       model_.configurationServiceState_ = true;
+       model_.sentinel_->setConfigurationServiceState(true);
 
-       model_.interactionServiceState_.attach([this, &items](auto value) {
+       model_.sentinel_->interactionServiceState_.attach([this, &items](auto value) {
                items.front()->title_ = value ? "IDS_ST_BODY_ON" : "IDS_ST_BODY_OFF";
                items.front()->widgetState_ = value;
        });
 }
-
-UniversalSwitchPagePresenter::~UniversalSwitchPagePresenter()
-{
-       model_.configurationServiceState_ = false;
-}
\ No newline at end of file
index e2ff3ee..835a9e1 100644 (file)
@@ -8,7 +8,6 @@ class UniversalSwitchPagePresenter : public ListPresenter
 {
        public:
        UniversalSwitchPagePresenter();
-       ~UniversalSwitchPagePresenter();
 
        private:
        UniversalSwitchPageModel model_;
diff --git a/src/service/UniversalSwitchSentinel.cpp b/src/service/UniversalSwitchSentinel.cpp
new file mode 100644 (file)
index 0000000..e291091
--- /dev/null
@@ -0,0 +1,67 @@
+#include "UniversalSwitchSentinel.hpp"
+
+#include "UniversalSwitchConstants.hpp"
+
+UniversalSwitchState::UniversalSwitchState()
+{
+       interactionServiceStateHandle_ = Singleton<VConfInterface>::instance().registerAndGet<bool>(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, false, [this](auto state) {
+               this->vconfInteractionState_ = state;
+               this->syncInteractionServiceState();
+       });
+
+       configurationServiceStateHandle_ = Singleton<VConfInterface>::instance().registerAndGet<bool>(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, false, [this](auto state) {
+               this->vconfConfigurationState_ = state;
+               this->syncConfigurationServiceState();
+       });
+
+       connectionPtr_ = DBus::getDBusConnectionByType(DBus::ConnectionType::SESSION);
+       ownerNameCallbackHandle_ = DBus::registerNameOwnerChangedCallback(connectionPtr_, BUS, [this](std::string oldID, std::string newID) {
+               dbusConfigurationServiceState_ = true;
+               syncInteractionServiceState();
+               syncConfigurationServiceState();
+       });
+}
+
+UniversalSwitchState::~UniversalSwitchState()
+{
+       Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, false);
+}
+
+void UniversalSwitchState::syncInteractionServiceState()
+{
+       auto v = vconfInteractionState_ && dbusConfigurationServiceState_;
+       if (this->interactionServiceState_.value_ != v) {
+               this->interactionServiceState_.value_ = v;
+               this->interactionServiceState_.notify(v);
+       }
+}
+
+void UniversalSwitchState::syncConfigurationServiceState()
+{
+       auto v = vconfConfigurationState_ && dbusConfigurationServiceState_;
+       if (this->configurationServiceState_.value_ != v) {
+               this->configurationServiceState_.value_ = v;
+               this->configurationServiceState_.notify(v);
+       }
+}
+
+void UniversalSwitchState::setInteractionServiceState(bool state)
+{
+       Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_INTERACTION_SERVICE, state);
+}
+
+void UniversalSwitchState::setConfigurationServiceState(bool state)
+{
+       Singleton<VConfInterface>::instance().set(VCONFKEY_SETAPPL_ACCESSIBILITY_UNIVERSAL_SWITCH_CONFIGURATION_SERVICE, state);
+}
+
+UniversalSwitchSentinel UniversalSwitchSentinelService::getSentinel()
+{
+       if (auto s = sentinel_.lock()) {
+               return s;
+       } else {
+               auto x = std::make_shared<UniversalSwitchState>();
+               sentinel_ = x;
+               return x;
+       }
+}
\ No newline at end of file
diff --git a/src/service/UniversalSwitchSentinel.hpp b/src/service/UniversalSwitchSentinel.hpp
new file mode 100644 (file)
index 0000000..b72a06a
--- /dev/null
@@ -0,0 +1,64 @@
+#ifndef UNIVERSAL_SWITCH_SENTINEL_HPP
+#define UNIVERSAL_SWITCH_SENTINEL_HPP
+
+#include "DBus.hpp"
+#include "ObservableProperty.hpp"
+#include "VConf.hpp"
+
+#include <memory>
+
+class UniversalSwitchState
+{
+       public:
+       class ReadOnlyProperty : public Observable<bool>
+       {
+               friend class UniversalSwitchState;
+
+               public:
+               ReadOnlyProperty() = default;
+               ReadOnlyProperty(bool val)
+                       : value_(val)
+               {}
+
+               bool value() const
+               {
+                       return value_;
+               }
+
+               private:
+               bool value_ = false;
+       };
+
+       UniversalSwitchState();
+       ~UniversalSwitchState();
+       ReadOnlyProperty interactionServiceState_;
+       ReadOnlyProperty configurationServiceState_;
+
+       void setInteractionServiceState(bool state);
+       void setConfigurationServiceState(bool state);
+
+       private:
+       void syncInteractionServiceState();
+       void syncConfigurationServiceState();
+
+       VConfInterface::CallbackHandle interactionServiceStateHandle_;
+       VConfInterface::CallbackHandle configurationServiceStateHandle_;
+       DBus::OwnerNameCallbackHandle ownerNameCallbackHandle_;
+       DBus::DBusWrapper::ConnectionPtr connectionPtr_;
+       bool dbusConfigurationServiceState_ = false;
+       bool vconfConfigurationState_ = false;
+       bool vconfInteractionState_ = false;
+};
+
+using UniversalSwitchSentinel = std::shared_ptr<UniversalSwitchState>;
+
+class UniversalSwitchSentinelService
+{
+       public:
+       UniversalSwitchSentinel getSentinel();
+
+       private:
+       std::weak_ptr<UniversalSwitchState> sentinel_;
+};
+
+#endif
\ No newline at end of file