revise vconf changed callbacks
authorYoungjae Shin <yj99.shin@samsung.com>
Wed, 4 Mar 2020 03:11:30 +0000 (12:11 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 19 Mar 2020 04:30:37 +0000 (13:30 +0900)
27 files changed:
CMakeLists.txt
common/VconfCbHandler.cpp [new file with mode: 0644]
display/CMakeLists.txt
display/DisplayActVconf.cpp [new file with mode: 0644]
display/DisplayActVconf.h [new file with mode: 0644]
display/DisplayAutoBrightness.cpp [deleted file]
display/DisplayAutoBrightness.h [deleted file]
display/DisplayFactory.cpp
display/DisplayFactory.h
display/DisplayTimeout.cpp [deleted file]
display/DisplayTimeout.h [deleted file]
include/VconfCbHandler.h [new file with mode: 0644]
include/VconfChangeAction.h [new file with mode: 0644]
unittests/CMakeLists.txt
unittests/mdsp_test_vconf.cpp
vconf/CMakeLists.txt
vconf/VconfAction.cpp [deleted file]
vconf/VconfAction.h [deleted file]
vconf/VconfActionBool.cpp
vconf/VconfActionBool.h
vconf/VconfActionDbl.cpp
vconf/VconfActionDbl.h
vconf/VconfActionInt.cpp
vconf/VconfActionInt.h
vconf/VconfActionStr.cpp
vconf/VconfActionStr.h
vconf/VconfPlugin.cpp

index fd4247e5b1ffbde59cb509b0f81e0e1920715883..cf8ccb2d933bb762dc0d5af63eb1f5745870090a 100644 (file)
@@ -15,6 +15,8 @@ IF(NOT DEFINED MODES_PLUGIN_DEFAULT_DIR)
        SET(MODES_PLUGIN_DEFAULT_DIR "/usr/lib/modes-plugins/")
 ENDIF(NOT DEFINED MODES_PLUGIN_DEFAULT_DIR)
 
+SET(VCONF_COMMON_SRC ${CMAKE_SOURCE_DIR}/common/VconfCbHandler.cpp)
+
 ADD_SUBDIRECTORY(wifi)
 ADD_SUBDIRECTORY(vconf)
 ADD_SUBDIRECTORY(app)
diff --git a/common/VconfCbHandler.cpp b/common/VconfCbHandler.cpp
new file mode 100644 (file)
index 0000000..e4577ca
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "VconfCbHandler.h"
+
+#include <string>
+#include <algorithm>
+#include <vconf.h>
+#include <modes_errors.h>
+#include "plugin-log.h"
+#include "VconfChangeAction.h"
+
+MODES_NAMESPACE_USE;
+
+std::map<std::string, VconfChangeAction*> VconfCbHandler::callbackMap;
+
+int VconfCbHandler::handleSubscription(const std::string &key)
+{
+       auto found = callbackMap.find(key);
+       if (callbackMap.end() != found) {
+               WARN("Acition(%s) already exist", key.c_str());
+               found->second->vconfChangedCB();
+
+               callbackMap.erase(found);
+               int ret = vconf_ignore_key_changed(key.c_str(), vconfCallback);
+               if (0 != ret) {
+                       ERR("vconf_ignore_key_changed(%s) Fail(%d)", key.c_str(), ret);
+                       return MODES_ERROR_SYSTEM;
+               }
+       }
+       return MODES_ERROR_NONE;
+}
+
+int VconfCbHandler::setChangedCB(const std::string &key, VconfChangeAction *action)
+{
+       RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER);
+
+       int ret = vconf_notify_key_changed(key.c_str(), vconfCallback, NULL);
+       if (0 != ret) {
+               ERR("vconf_notify_key_changed(%s) Fail(%d)", key.c_str(), ret);
+               return MODES_ERROR_SYSTEM;
+       }
+
+       callbackMap[key] = action;
+
+       return MODES_ERROR_NONE;
+}
+
+void VconfCbHandler::unSetChangedCB(const std::string &key)
+{
+       auto found = callbackMap.find(key);
+       if (callbackMap.end() == found) {
+               ERR("No Changed Callback(%s)", key.c_str());
+               return;
+       }
+
+       callbackMap.erase(found);
+       int ret = vconf_ignore_key_changed(key.c_str(), vconfCallback);
+       if (0 != ret) {
+               ERR("vconf_ignore_key_changed(%s) Fail(%d)", key.c_str(), ret);
+               return;
+       }
+}
+
+void VconfCbHandler::vconfCallback(keynode_t *node, void *userData)
+{
+       auto found = callbackMap.find(node->keyname);
+       if (callbackMap.end() == found) {
+               ERR("No VconfAction(%s)", node->keyname);
+               return;
+       }
+
+       found->second->vconfChangedCB(node);
+       DBG("Action(%s) is Changed", node->keyname);
+}
index 85bf7cc385ac8a3173f7a6bc2ee1ebb224ceea33..e6add3606d900a7523a4209580aead79eb4a10c3 100644 (file)
@@ -7,7 +7,7 @@ PKG_CHECK_MODULES(DISPLAY_pkgs REQUIRED modes dlog capi-base-common capi-system-
 INCLUDE_DIRECTORIES(${DISPLAY_pkgs_INCLUDE_DIRS})
 LINK_DIRECTORIES(${DISPLAY_pkgs_LIBRARY_DIRS})
 
-ADD_LIBRARY(${DISPLAY_PLUGIN} SHARED ${DISPLAY_SRCS})
+ADD_LIBRARY(${DISPLAY_PLUGIN} SHARED ${DISPLAY_SRCS} ${VCONF_COMMON_SRC})
 TARGET_LINK_LIBRARIES(${DISPLAY_PLUGIN} ${DISPLAY_pkgs_LIBRARIES})
 SET_TARGET_PROPERTIES(${DISPLAY_PLUGIN} PROPERTIES NO_SONAME 1 )
 INSTALL(TARGETS ${DISPLAY_PLUGIN} DESTINATION ${MODES_PLUGIN_DEFAULT_DIR})
diff --git a/display/DisplayActVconf.cpp b/display/DisplayActVconf.cpp
new file mode 100644 (file)
index 0000000..a9e8d07
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "DisplayActVconf.h"
+
+#include <sstream>
+#include <string>
+#include <vconf.h>
+#include <modes_errors.h>
+#include "plugin-log.h"
+#include "VconfCbHandler.h"
+
+MODES_NAMESPACE_USE;
+
+// It should align with actionKey in DisplayFactory
+const std::string DisplayActVconf::NAME[2] = {
+       "autoBrightness",
+       "timeout",
+};
+const char* const DisplayActVconf::KEY[2] = {
+       VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT,
+       VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL,
+};
+
+DisplayActVconf::DisplayActVconf(int type)
+       :DisplayAction(NAME[type]), keyType(type),
+       requestVal(-1), oldVal(-1), cb(nullptr), cbData(nullptr)
+{
+}
+
+int DisplayActVconf::set(int val)
+{
+       requestVal = val;
+
+       int ret = cbHandler.handleSubscription(KEY[keyType]);
+       if (MODES_ERROR_NONE != ret) {
+               ERR("handleChange(%s) Fail(%d)", KEY[keyType], ret);
+               return ret;
+       }
+
+       ret = vconf_get_int(KEY[keyType], &oldVal);
+       if (0 != ret) {
+               ERR("vconf_get_int(%s) Fail(%d)", KEY[keyType], ret);
+               return MODES_ERROR_SYSTEM;
+       }
+
+       if (oldVal == val) {
+               INFO("%s already set(%d)", NAME[keyType].c_str(), val);
+       } else {
+               ret = vconf_set_int(KEY[keyType], val);
+               if (0 != ret) {
+                       ERR("vconf_set_int(%s, %d) Fail(%d)", KEY[keyType], val, ret);
+                       return MODES_ERROR_SYSTEM;
+               }
+       }
+
+       return MODES_ERROR_NONE;
+}
+
+void DisplayActVconf::undo()
+{
+       RET_IF(-1 == oldVal);
+
+       int ret = vconf_set_int(KEY[keyType], oldVal);
+       if (0 != ret)
+               ERR("vconf_set_int(%s, %d) Fail(%d)", KEY[keyType], oldVal, ret);
+}
+
+std::string DisplayActVconf::serialize()
+{
+       std::ostringstream ostr;
+
+       ostr << oldVal;
+       return ostr.str();
+}
+
+int DisplayActVconf::parse(const std::string &archive)
+{
+       std::istringstream iss(archive);
+       iss >> oldVal;
+
+       return MODES_ERROR_NONE;
+}
+
+int DisplayActVconf::setChangedCallback(valueChangedCB callback, void *userData)
+{
+       RETV_IF(nullptr == callback, MODES_ERROR_INVALID_PARAMETER);
+
+       int ret = cbHandler.setChangedCB(KEY[keyType], this);
+       if (MODES_ERROR_NONE != ret) {
+               ERR("setChangedCB(%s) Fail(%d)", KEY[keyType], ret);
+               return ret;
+       }
+
+       cb = callback;
+       cbData = userData;
+
+       return MODES_ERROR_NONE;
+}
+
+void DisplayActVconf::unSetChangedCallback(valueChangedCB callback, void *userData)
+{
+       RET_IF(nullptr == callback);
+
+       cbHandler.unSetChangedCB(KEY[keyType]);
+
+       cbData = nullptr;
+       cb = nullptr;
+}
+
+void DisplayActVconf::vconfChangedCB()
+{
+       //The value will be changed by Modes. That's why the callback is always called.
+       if (cb)
+               cb(cbData);
+}
+
+void DisplayActVconf::vconfChangedCB(keynode_t *node)
+{
+       const char *vconfKey = vconf_keynode_get_name(node);
+       if (vconfKey != KEY[keyType]) {
+               WARN("Unknown vconf(%s) notification", vconfKey);
+               return;
+       }
+
+       int val = vconf_keynode_get_int(node);
+
+       if (cb && (val != requestVal))
+               cb(cbData);
+}
diff --git a/display/DisplayActVconf.h b/display/DisplayActVconf.h
new file mode 100644 (file)
index 0000000..2d2d7cc
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <string>
+#include "plugin-def.h"
+#include "DisplayAction.h"
+#include "VconfCbHandler.h"
+
+MODES_NAMESPACE_BEGIN
+
+class DisplayActVconf : public DisplayAction, public VconfChangeAction {
+public:
+       DisplayActVconf(int type);
+
+       int set(int val) override;
+       void undo() override;
+       std::string serialize() override;
+       int parse(const std::string &archive) override;
+       int setChangedCallback(valueChangedCB callback, void *userData) override;
+       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
+
+       void vconfChangedCB() override;
+       void vconfChangedCB(keynode_t *node) override;
+
+       static const std::string NAME[2];
+       static const char* const KEY[2];
+private:
+       int keyType;
+       int requestVal;
+       int oldVal;
+       VconfCbHandler cbHandler;
+       valueChangedCB cb;
+       void *cbData;
+};
+
+MODES_NAMESPACE_END
diff --git a/display/DisplayAutoBrightness.cpp b/display/DisplayAutoBrightness.cpp
deleted file mode 100644 (file)
index cb24efb..0000000
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "DisplayAutoBrightness.h"
-#include <sstream>
-#include <string>
-#include <vconf.h>
-#include <modes_errors.h>
-#include "plugin-log.h"
-
-MODES_NAMESPACE_USE;
-
-const std::string DisplayAutoBrightness::NAME = "autoBrightness";
-const char* const DisplayAutoBrightness::key = VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT;
-
-DisplayAutoBrightness::DisplayAutoBrightness()
-       :DisplayAction(NAME), oldVal(-1), cb(nullptr), cbData(nullptr)
-{
-}
-
-DisplayAutoBrightness::~DisplayAutoBrightness()
-{
-}
-
-int DisplayAutoBrightness::set(int val)
-{
-       int ret = vconf_get_int(key, &oldVal);
-       if (0 != ret) {
-               ERR("vconf_get_int(%s) Fail(%d)", key, ret);
-               return MODES_ERROR_SYSTEM;
-       }
-
-       if (oldVal == val) {
-               INFO("Auto brightness already set(%d)", val);
-               return MODES_ERROR_NONE;
-       }
-
-       ret = vconf_set_int(key, val);
-       if (0 != ret) {
-               ERR("vconf_set_int(%s, %d) Fail(%d)", key, val, ret);
-               return MODES_ERROR_SYSTEM;
-       }
-
-       return MODES_ERROR_NONE;
-}
-
-void DisplayAutoBrightness::undo()
-{
-       RET_IF(-1 == oldVal);
-
-       int ret = vconf_set_int(key, oldVal);
-       if (0 != ret)
-               ERR("vconf_set_int(%s, %d) Fail(%d)", key, oldVal, ret);
-}
-
-std::string DisplayAutoBrightness::serialize()
-{
-       std::ostringstream ostr;
-
-       ostr << oldVal;
-       return ostr.str();
-}
-
-int DisplayAutoBrightness::parse(const std::string &archive)
-{
-       std::istringstream iss(archive);
-       iss >> oldVal;
-
-       return MODES_ERROR_NONE;
-}
-
-int DisplayAutoBrightness::setChangedCallback(valueChangedCB callback, void *userData)
-{
-       cbData = userData;
-       cb = callback;
-       int ret = vconf_notify_key_changed(key, autoBrightnessChangedCB, this);
-       if (0 != ret) {
-               ERR("vconf_notify_key_changed(%s) Fail(%d)", key, ret);
-               return MODES_ERROR_SYSTEM;
-       }
-       return MODES_ERROR_NONE;
-}
-
-void DisplayAutoBrightness::unSetChangedCallback(valueChangedCB callback, void *userData)
-{
-       int ret = vconf_ignore_key_changed(key, autoBrightnessChangedCB);
-       if (0 != ret)
-               ERR("vconf_ignore_key_changed(%s) Fail(%d)", key, ret);
-
-       cbData = NULL;
-       cb = NULL;
-}
-
-void DisplayAutoBrightness::autoBrightnessChangedCB(keynode_t *node, void *userData)
-{
-       DisplayAutoBrightness *action = (DisplayAutoBrightness*)userData;
-
-       RET_IF(NULL == userData);
-
-       int val = 0;
-       int ret = vconf_get_int(key, &val);
-       if (0 != ret) {
-               ERR("vconf_get_int(%s) Fail(%d)", key, ret);
-               return;
-       }
-
-       if (action->cb && (val != action->oldVal))
-               action->cb(action->cbData);
-}
diff --git a/display/DisplayAutoBrightness.h b/display/DisplayAutoBrightness.h
deleted file mode 100644 (file)
index 528664e..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <string>
-#include <vconf.h>
-#include "plugin-def.h"
-#include "DisplayAction.h"
-
-MODES_NAMESPACE_BEGIN
-
-class DisplayAutoBrightness : public DisplayAction {
-public:
-       DisplayAutoBrightness();
-       ~DisplayAutoBrightness();
-
-       int set(int val) override;
-       void undo() override;
-       std::string serialize() override;
-       int parse(const std::string &archive) override;
-
-       int setChangedCallback(valueChangedCB callback, void *userData) override;
-       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
-
-       static const std::string NAME;
-
-private:
-       static void autoBrightnessChangedCB(keynode_t *node, void *userData);
-
-       static const char* const key;
-       int oldVal;
-       valueChangedCB cb;
-       void *cbData;
-};
-
-MODES_NAMESPACE_END
index b3e2ddaf1238d110654979e6dc9b6b8beb5f1ed9..8497b8afb87782757eec5300fb39c7d1743afaa5 100644 (file)
 #include <string>
 #include <device/power.h>
 #include "plugin-log.h"
+#include "DisplayActVconf.h"
 #include "DisplayBrightness.h"
-#include "DisplayAutoBrightness.h"
-#include "DisplayTimeout.h"
 #include "DisplayAllowPalmTouch.h"
 
 MODES_NAMESPACE_USE;
 
 DisplayFactory::DisplayFactory()
 {
+       actionMap[DisplayActVconf::NAME[AUTO_BRIGHTNESS_VCONF]] = AUTO_BRIGHTNESS_VCONF;
+       actionMap[DisplayActVconf::NAME[TIMEOUT_VCONF]] = TIMEOUT_VCONF;
        actionMap[DisplayBrightness::NAME] = BRIGHTNESS;
-       actionMap[DisplayAutoBrightness::NAME] = AUTO_BRIGHTNESS;
-       actionMap[DisplayTimeout::NAME] = TIMEOUT;
        actionMap[DisplayAllowPalmTouch::NAME] = ALLOW_PALM_TOUCH;
 }
 
@@ -42,14 +41,14 @@ DisplayAction* DisplayFactory::createAction(const std::string &key)
 
        DisplayAction *action;
        switch (search->second) {
-       case BRIGHTNESS:
-               action = new DisplayBrightness();
+       case AUTO_BRIGHTNESS_VCONF:
+               action = new DisplayActVconf(AUTO_BRIGHTNESS_VCONF);
                break;
-       case AUTO_BRIGHTNESS:
-               action = new DisplayAutoBrightness();
+       case TIMEOUT_VCONF:
+               action = new DisplayActVconf(TIMEOUT_VCONF);
                break;
-       case TIMEOUT:
-               action = new DisplayTimeout();
+       case BRIGHTNESS:
+               action = new DisplayBrightness();
                break;
        case ALLOW_PALM_TOUCH:
                action = new DisplayAllowPalmTouch();
index a440e3d2b54932c29e53e08ce738849b64f46a91..f0602a9d4e6a5da55fe67aea109f8dcf65eee54f 100644 (file)
@@ -30,9 +30,9 @@ public:
        void destroyAction(DisplayAction *action);
 private:
        enum actionKey{
+               AUTO_BRIGHTNESS_VCONF,
+               TIMEOUT_VCONF,
                BRIGHTNESS,
-               AUTO_BRIGHTNESS,
-               TIMEOUT,
                ALLOW_PALM_TOUCH,
        };
 
diff --git a/display/DisplayTimeout.cpp b/display/DisplayTimeout.cpp
deleted file mode 100644 (file)
index d68f0e9..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "DisplayTimeout.h"
-#include <sstream>
-#include <string>
-#include <vconf.h>
-#include <modes_errors.h>
-#include "plugin-log.h"
-
-MODES_NAMESPACE_USE;
-
-const std::string DisplayTimeout::NAME = "timeout";
-DisplayTimeout::DisplayTimeout()
-       :DisplayAction(NAME), key(VCONFKEY_SETAPPL_LCD_TIMEOUT_NORMAL), oldDisplayTimeout(-1)
-{
-}
-
-DisplayTimeout::~DisplayTimeout()
-{
-}
-
-int DisplayTimeout::set(int val)
-{
-       int ret = vconf_get_int(key, &oldDisplayTimeout);
-       if (0 != ret) {
-               ERR("vconf_get_int(%s) Fail(%d)", key, ret);
-               return MODES_ERROR_SYSTEM;
-       }
-       ret = vconf_set_int(key, val);
-       if (0 != ret) {
-               ERR("vconf_set_int(%s, %d) Fail(%d)", key, val, ret);
-               return MODES_ERROR_SYSTEM;
-       }
-
-       return MODES_ERROR_NONE;
-}
-
-void DisplayTimeout::undo()
-{
-       DBG("undo Display Timeout(%s, %d)", key, oldDisplayTimeout);
-       int ret = vconf_set_int(key, oldDisplayTimeout);
-       if (0 != ret)
-               ERR("vconf_set_int(%s, %d) Fail(%d)", key, oldDisplayTimeout, ret);
-}
-
-std::string DisplayTimeout::serialize()
-{
-       std::ostringstream ostr;
-
-       ostr << oldDisplayTimeout;
-       return ostr.str();
-}
-
-int DisplayTimeout::parse(const std::string &archive)
-{
-       std::istringstream iss(archive);
-       iss >> oldDisplayTimeout;
-
-       return MODES_ERROR_NONE;
-}
diff --git a/display/DisplayTimeout.h b/display/DisplayTimeout.h
deleted file mode 100644 (file)
index c908e15..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <string>
-#include "plugin-def.h"
-#include "DisplayAction.h"
-
-MODES_NAMESPACE_BEGIN
-
-class DisplayTimeout : public DisplayAction {
-public:
-       DisplayTimeout();
-       ~DisplayTimeout();
-
-       int set(int val) override;
-       void undo() override;
-       std::string serialize() override;
-       int parse(const std::string &archive) override;
-
-       static const std::string NAME;
-private:
-       const char* const key;
-       int oldDisplayTimeout;
-};
-
-MODES_NAMESPACE_END
diff --git a/include/VconfCbHandler.h b/include/VconfCbHandler.h
new file mode 100644 (file)
index 0000000..7e0d379
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <map>
+#include <string>
+#include <vconf.h>
+#include "plugin-def.h"
+#include "VconfChangeAction.h"
+
+MODES_NAMESPACE_BEGIN
+
+class VconfCbHandler {
+public:
+       int handleSubscription(const std::string &key);
+       int setChangedCB(const std::string &key, VconfChangeAction *action);
+       void unSetChangedCB(const std::string &key);
+private:
+       static void vconfCallback(keynode_t *node, void *userData);
+       static std::map<std::string, VconfChangeAction*> callbackMap;
+};
+
+MODES_NAMESPACE_END
+
diff --git a/include/VconfChangeAction.h b/include/VconfChangeAction.h
new file mode 100644 (file)
index 0000000..108a08b
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <vconf.h>
+#include "plugin-def.h"
+
+MODES_NAMESPACE_BEGIN
+
+class VconfChangeAction {
+public:
+       virtual void vconfChangedCB() = 0;
+       virtual void vconfChangedCB(keynode_t *node) = 0;
+};
+
+MODES_NAMESPACE_END
index 65d9a7f206d9362663734aefd5cd2b4594edefe7..48927dde9549b6842ab5753ee77a569b9d21f151 100644 (file)
@@ -60,7 +60,7 @@ SET(VCONF_PLUGIN_TEST "modes-plugintest-vconf")
 FILE(GLOB VCONF_SRCS ${VCONF_SRC_DIR}/*.cpp)
 SET(VCONF_SRCS ${VCONF_SRCS} "mdsp_test_vconf.cpp")
 
-ADD_EXECUTABLE(${VCONF_PLUGIN_TEST} ${VCONF_SRCS})
+ADD_EXECUTABLE(${VCONF_PLUGIN_TEST} ${VCONF_SRCS} ${VCONF_COMMON_SRC})
 TARGET_LINK_LIBRARIES(${VCONF_PLUGIN_TEST} ${test_pkgs_LIBRARIES})
 INSTALL(TARGETS ${VCONF_PLUGIN_TEST} DESTINATION ${TEST_INSTALL_DIR})
 #===================================================================#
@@ -78,6 +78,6 @@ SET(DISPLAY_PLUGIN_TEST "modes-plugintest-display")
 FILE(GLOB DISPLAY_SRCS ${DISPLAY_SRC_DIR}/*.cpp)
 SET(DISPLAY_SRCS ${DISPLAY_SRCS} "mdsp_test_display.cpp")
 
-ADD_EXECUTABLE(${DISPLAY_PLUGIN_TEST} ${DISPLAY_SRCS})
+ADD_EXECUTABLE(${DISPLAY_PLUGIN_TEST} ${DISPLAY_SRCS} ${VCONF_COMMON_SRC})
 TARGET_LINK_LIBRARIES(${DISPLAY_PLUGIN_TEST} ${test_pkgs_LIBRARIES})
 INSTALL(TARGETS ${DISPLAY_PLUGIN_TEST} DESTINATION ${TEST_INSTALL_DIR})
index e364e1175f9d32827f521cf40536d435a653ea75..7feb4f1d8696aebc23d571f1e4b2a5db375f2c3f 100644 (file)
@@ -71,7 +71,7 @@ protected:
 
        static gboolean changedCallbackidler(gpointer data)
        {
-               DBG("start change the value db/setting/mode");
+               DBG("start change the value db/setting/psmode");
                vconf_set_int("db/setting/psmode", 7);
                return G_SOURCE_REMOVE;
        }
@@ -96,17 +96,11 @@ TEST_F(VconfPluginTest, VconfInt)
        ret = plugin->set(key, 0, nullptr);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
 
-       int val = plugin->getInt(key);
-       EXPECT_EQ(val, 0);
-
        PluginAction *action;
        ret = plugin->set(key, 1, &action);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
 
        plugin->undo(action);
-
-       val = plugin->getInt(key);
-       EXPECT_EQ(val, 0);
 }
 
 TEST_F(VconfPluginTest, VconfDouble)
@@ -117,17 +111,11 @@ TEST_F(VconfPluginTest, VconfDouble)
        ret = plugin->set(key, 0.0, nullptr);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
 
-       double val = plugin->getDouble(key);
-       EXPECT_EQ(val, 0.0);
-
        PluginAction *action;
        ret = plugin->set(key, 1.0, &action);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
 
        plugin->undo(action);
-
-       val = plugin->getDouble(key);
-       EXPECT_EQ(val, 0.0);
 }
 
 TEST_F(VconfPluginTest, VconfBool)
@@ -138,17 +126,11 @@ TEST_F(VconfPluginTest, VconfBool)
        ret = plugin->set(key, true, nullptr);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
 
-       double val = plugin->getBool(key);
-       EXPECT_EQ(val, true);
-
        PluginAction *action;
        ret = plugin->set(key, false, &action);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
 
        plugin->undo(action);
-
-       val = plugin->getBool(key);
-       EXPECT_EQ(val, true);
 }
 
 TEST_F(VconfPluginTest, VconfStr)
@@ -161,17 +143,11 @@ TEST_F(VconfPluginTest, VconfStr)
        ret = plugin->set(key, devName, nullptr);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
 
-       std::string val = plugin->getString(key);
-       EXPECT_STREQ(val.c_str(), devName.c_str());
-
        PluginAction *action;
        ret = plugin->set(key, tmpName, &action);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
 
        plugin->undo(action);
-
-       val = plugin->getString(key);
-       EXPECT_STREQ(val.c_str(), devName.c_str());
 }
 
 TEST_F(VconfPluginTest, callbackPluginVconf)
index 6a83822523959d328d3b2a84cc118ea2148b8d53..cb97410c7e353c7b7a9983e32a5d12a2e5f395be 100644 (file)
@@ -2,19 +2,11 @@ SET(VCONF_PLUGIN "modes-plugin-vconf")
 
 FILE(GLOB VCONF_SRCS *.cpp)
 
-pkg_check_modules(vconf_pkgs REQUIRED modes dlog capi-base-common vconf)
+PKG_CHECK_MODULES(vconf_pkgs REQUIRED modes dlog capi-base-common vconf)
 INCLUDE_DIRECTORIES(${vconf_pkgs_INCLUDE_DIRS})
 LINK_DIRECTORIES(${vconf_pkgs_LIBRARY_DIRS})
 
-# ==== WifiErrorMessage Header genertor ====
-#ADD_CUSTOM_TARGET(
-#    genWifiErrorMessage
-#    COMMAND echo "Run WifiErrorMessage Generator"
-#    COMMAND tool/WifiErrorMessageGenerator.sh
-#)
-#ADD_DEPENDENCIES(${VCONF_PLUGIN} genWifiErrorMessage)
-
-ADD_LIBRARY(${VCONF_PLUGIN} SHARED ${VCONF_SRCS})
+ADD_LIBRARY(${VCONF_PLUGIN} SHARED ${VCONF_SRCS} ${VCONF_COMMON_SRC})
 TARGET_LINK_LIBRARIES(${VCONF_PLUGIN} ${vconf_pkgs_LIBRARIES})
 SET_TARGET_PROPERTIES(${VCONF_PLUGIN} PROPERTIES NO_SONAME 1 )
 INSTALL(TARGETS ${VCONF_PLUGIN} DESTINATION ${MODES_PLUGIN_DEFAULT_DIR})
diff --git a/vconf/VconfAction.cpp b/vconf/VconfAction.cpp
deleted file mode 100644 (file)
index 32a551f..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "VconfAction.h"
-
-#include <string>
-#include <algorithm>
-#include <vconf.h>
-#include "plugin-log.h"
-
-MODES_NAMESPACE_USE;
-
-std::map<std::string, VconfAction::CallbackData> VconfAction::callbackMap;
-
-VconfAction::VconfAction(const std::string &name, const std::string &vconfKey)
-       : PluginAction(name), key(vconfKey)
-{
-}
-
-int VconfAction::setChangedCallback(valueChangedCB callback, void *userData)
-{
-       callbackMap[key].userData = userData;
-       callbackMap[key].callback = callback;
-
-       int ret = vconf_notify_key_changed(key.c_str(), vconfChangedCallback, &callbackMap[key]);
-       if (0 != ret) {
-               ERR("vconf_notify_key_changed(%s) Fail(%d)", key.c_str(), ret);
-               return MODES_ERROR_SYSTEM;
-       }
-
-       DBG("setChangedCallback(%s) Success", key.c_str());
-       return MODES_ERROR_NONE;
-}
-
-void VconfAction::unSetChangedCallback(valueChangedCB callback, void *userData)
-{
-       auto found = callbackMap.find(key);
-       if (found == callbackMap.end()) {
-               ERR("No Changed Callback(%s)", key.c_str());
-               return;
-       }
-
-       callbackMap.erase(found);
-       int ret = vconf_ignore_key_changed(key.c_str(), vconfChangedCallback);
-       if (0 != ret) {
-               ERR("vconf_ignore_key_changed(%s) Fail(%d)", key.c_str(), ret);
-               return;
-       }
-
-       DBG("unSetChangedCallback(%s) Success", key.c_str());
-}
-
-int VconfAction::handleChange()
-{
-       auto found = callbackMap.find(key);
-       if (callbackMap.end() != found) {
-               DBG("Acition(%s) already exist", key.c_str());
-               found->second.callback(found->second.userData);
-
-               callbackMap.erase(found);
-               int ret = vconf_ignore_key_changed(key.c_str(), vconfChangedCallback);
-               if (0 != ret) {
-                       ERR("vconf_ignore_key_changed(%s) Fail(%d)", key.c_str(), ret);
-                       return MODES_ERROR_SYSTEM;
-               }
-       }
-       return MODES_ERROR_NONE;
-}
-
-void VconfAction::vconfChangedCallback(keynode_t *node, void *userData)
-{
-       CallbackData *cbData = (CallbackData*)userData;
-
-       RET_IF(NULL == userData);
-
-       auto found = callbackMap.find(node->keyname);
-       if (&(found->second) != cbData) {
-               ERR("Unknown callbackData(%s)", node->keyname);
-               return;
-       }
-
-       cbData->callback(cbData->userData);
-       DBG("Action(%s) is Changed", node->keyname);
-}
diff --git a/vconf/VconfAction.h b/vconf/VconfAction.h
deleted file mode 100644 (file)
index 5bc0af3..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2019-2020 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <map>
-#include <string>
-#include <vconf.h>
-#include <PluginAction.h>
-#include "plugin-def.h"
-
-MODES_NAMESPACE_BEGIN
-
-class VconfAction : public PluginAction {
-public:
-       VconfAction(const std::string &name, const std::string &vconfKey);
-       virtual ~VconfAction() = default;
-
-       int setChangedCallback(valueChangedCB callback, void *userData) override;
-       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
-protected:
-       int handleChange();
-       std::string key;
-private:
-       struct CallbackData {
-               void *userData;
-               valueChangedCB callback;
-       };
-       static void vconfChangedCallback(keynode_t *node, void *userData);
-       static std::map<std::string, CallbackData> callbackMap;
-};
-
-MODES_NAMESPACE_END
-
index c58b06eedf25c948eb290cf52a5b79e548eec02b..23209add7b50e7989057a34a8e231365506ab749 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "VconfActionBool.h"
+
 #include <string>
 #include <sstream>
 #include <vconf.h>
 
 MODES_NAMESPACE_USE;
 
-VconfActionBool::VconfActionBool(const std::string &key)
-       : VconfAction("boolVconf", key), oldVal(false)
+VconfActionBool::VconfActionBool(const std::string &vconfKey)
+       : PluginAction("boolVconf"), key(vconfKey), requestVal(false), oldVal(false), cb(nullptr), cbData(nullptr)
 {
 }
 
 int VconfActionBool::set(bool val)
 {
-       int ret;
-       DBG("setBool(%s, %d)", key.c_str(), val);
+       requestVal = val;
 
-       ret = handleChange();
+       int ret = cbHandler.handleSubscription(key);
        if (MODES_ERROR_NONE != ret) {
                ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
                return ret;
@@ -45,32 +45,22 @@ int VconfActionBool::set(bool val)
        }
        oldVal = prev;
 
-       ret = vconf_set_bool(key.c_str(), val);
-       if (0 != ret) {
-               ERR("vconf_set_bool(%s, %d) Fail(%d)", key.c_str(), val, ret);
-               return MODES_ERROR_SYSTEM;
-       }
-       return MODES_ERROR_NONE;
-}
-
-bool VconfActionBool::get()
-{
-       int value = 0;
-       int ret = vconf_get_bool(key.c_str(), &value);
-       if (0 != ret) {
-               ERR("vconf_get_bool(%s) Fail(%d)", key.c_str(), ret);
-               return false;
+       if (oldVal == val) {
+               INFO("vconf(%s) already set(%d)", key.c_str(), val);
+       } else {
+               ret = vconf_set_bool(key.c_str(), val);
+               if (0 != ret) {
+                       ERR("vconf_set_bool(%s, %d) Fail(%d)", key.c_str(), val, ret);
+                       return MODES_ERROR_SYSTEM;
+               }
        }
 
-       return value ? true : false;
+       return MODES_ERROR_NONE;
 }
 
 void VconfActionBool::undo()
 {
-       int ret;
-       DBG("undoBool(%s, %d)", key.c_str(), oldVal);
-
-       ret = vconf_set_bool(key.c_str(), oldVal ? 1 : 0);
+       int ret = vconf_set_bool(key.c_str(), oldVal ? 1 : 0);
        if (0 != ret)
                ERR("vconf_set_bool(%s, %d) Fail(%d)", key.c_str(), oldVal, ret);
 }
@@ -90,3 +80,49 @@ int VconfActionBool::parse(const std::string &archive)
 
        return MODES_ERROR_NONE;
 }
+
+int VconfActionBool::setChangedCallback(valueChangedCB callback, void *userData)
+{
+       RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER);
+
+       int ret = cbHandler.setChangedCB(key, this);
+       if (MODES_ERROR_NONE != ret) {
+               ERR("setChangedCB(%s) Fail(%d)", key.c_str(), ret);
+               return ret;
+       }
+
+       cb = callback;
+       cbData = userData;
+
+       return MODES_ERROR_NONE;
+}
+
+void VconfActionBool::unSetChangedCallback(valueChangedCB callback, void *userData)
+{
+       RET_IF(NULL == callback);
+
+       cbHandler.unSetChangedCB(key);
+
+       cbData = NULL;
+       cb = NULL;
+}
+
+void VconfActionBool::vconfChangedCB()
+{
+       //The value will be changed by Modes. That's why the callback is always called.
+       if (cb)
+               cb(cbData);
+}
+
+void VconfActionBool::vconfChangedCB(keynode_t *node)
+{
+       const char *vconfKey = vconf_keynode_get_name(node);
+       if (vconfKey != key) {
+               WARN("Unknown vconf(%s) notification", vconfKey);
+               return;
+       }
+
+       bool val = vconf_keynode_get_bool(node);
+       if (cb && (val != requestVal))
+               cb(cbData);
+}
index 90644aeb7a86489cf7e7abbf9b629f21be35fa34..2e8c989620a94a9289a6d4699e12f56fdceeed92 100644 (file)
 #pragma once
 
 #include <string>
-#include "VconfAction.h"
+#include <PluginAction.h>
 #include "plugin-def.h"
+#include "VconfCbHandler.h"
 
 MODES_NAMESPACE_BEGIN
 
-class VconfActionBool : public VconfAction {
+class VconfActionBool : public PluginAction, public VconfChangeAction {
 public:
-       VconfActionBool(const std::string &key);
-       virtual ~VconfActionBool() = default;
+       VconfActionBool(const std::string &vconfKey);
 
        int set(bool val);
-       bool get();
        void undo() override;
        std::string serialize() override;
        int parse(const std::string &archive) override;
+       int setChangedCallback(valueChangedCB callback, void *userData) override;
+       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
+
+       void vconfChangedCB() override;
+       void vconfChangedCB(keynode_t *node) override;
 private:
+       std::string key;
+       bool requestVal;
        bool oldVal;
+       VconfCbHandler cbHandler;
+       valueChangedCB cb;
+       void *cbData;
 };
 
 MODES_NAMESPACE_END
-
index e5765f062455f81f67480042c278521a64c4ba2d..15bca080deda7318a1300716d094f3437429b44d 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "VconfActionDbl.h"
+
 #include <string>
 #include <sstream>
 #include <vconf.h>
 
 MODES_NAMESPACE_USE;
 
-VconfActionDbl::VconfActionDbl(const std::string &key)
-       : VconfAction("dblVconf", key), oldVal(0.0)
+VconfActionDbl::VconfActionDbl(const std::string &vconfKey)
+       : PluginAction("dblVconf"), key(vconfKey), requestVal(0.0), oldVal(0.0), cb(nullptr), cbData(nullptr)
 {
 }
 
 int VconfActionDbl::set(double val)
 {
-       int ret;
-       DBG("setDbl(%s, %f)", key.c_str(), val);
+       requestVal = val;
 
-       ret = handleChange();
+       int ret = cbHandler.handleSubscription(key);
        if (MODES_ERROR_NONE != ret) {
                ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
                return ret;
@@ -43,32 +43,22 @@ int VconfActionDbl::set(double val)
                return MODES_ERROR_SYSTEM;
        }
 
-       ret = vconf_set_dbl(key.c_str(), val);
-       if (0 != ret) {
-               ERR("vconf_set_dbl(%s, %f) Fail(%d)", key.c_str(), val, ret);
-               return MODES_ERROR_SYSTEM;
-       }
-       return MODES_ERROR_NONE;
-}
-
-double VconfActionDbl::get()
-{
-       double value = 0.0;
-       int ret = vconf_get_dbl(key.c_str(), &value);
-       if (0 != ret) {
-               ERR("vconf_get_dbl(%s) Fail(%d)", key.c_str(), ret);
-               return 0.0;
+       if (oldVal == val) {
+               INFO("vconf(%s) already set(%f)", key.c_str(), val);
+       } else {
+               ret = vconf_set_dbl(key.c_str(), val);
+               if (0 != ret) {
+                       ERR("vconf_set_dbl(%s, %f) Fail(%d)", key.c_str(), val, ret);
+                       return MODES_ERROR_SYSTEM;
+               }
        }
 
-       return value;
+       return MODES_ERROR_NONE;
 }
 
 void VconfActionDbl::undo()
 {
-       int ret;
-       DBG("undoDbl(%s, %f)", key.c_str(), oldVal);
-
-       ret = vconf_set_dbl(key.c_str(), oldVal);
+       int ret = vconf_set_dbl(key.c_str(), oldVal);
        if (0 != ret)
                ERR("vconf_set_dbl(%s, %f) Fail(%d)", key.c_str(), oldVal, ret);
 }
@@ -88,3 +78,49 @@ int VconfActionDbl::parse(const std::string &archive)
 
        return MODES_ERROR_NONE;
 }
+
+int VconfActionDbl::setChangedCallback(valueChangedCB callback, void *userData)
+{
+       RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER);
+
+       int ret = cbHandler.setChangedCB(key, this);
+       if (MODES_ERROR_NONE != ret) {
+               ERR("setChangedCB(%s) Fail(%d)", key.c_str(), ret);
+               return ret;
+       }
+
+       cb = callback;
+       cbData = userData;
+
+       return MODES_ERROR_NONE;
+}
+
+void VconfActionDbl::unSetChangedCallback(valueChangedCB callback, void *userData)
+{
+       RET_IF(NULL == callback);
+
+       cbHandler.unSetChangedCB(key);
+
+       cbData = NULL;
+       cb = NULL;
+}
+
+void VconfActionDbl::vconfChangedCB()
+{
+       //The value will be changed by Modes. That's why the callback is always called.
+       if (cb)
+               cb(cbData);
+}
+
+void VconfActionDbl::vconfChangedCB(keynode_t *node)
+{
+       const char *vconfKey = vconf_keynode_get_name(node);
+       if (vconfKey != key) {
+               WARN("Unknown vconf(%s) notification", vconfKey);
+               return;
+       }
+
+       double val = vconf_keynode_get_dbl(node);
+       if (cb && (val != requestVal))
+               cb(cbData);
+}
index 0fd0bdf6dc0cb5336e543dcf41cf1c0e6cb9f8f2..3a98ebef5f63d869995d46ba57eefeef9adc536b 100644 (file)
 #pragma once
 
 #include <string>
-#include "VconfAction.h"
+#include <PluginAction.h>
 #include "plugin-def.h"
+#include "VconfCbHandler.h"
 
 MODES_NAMESPACE_BEGIN
 
-class VconfActionDbl : public VconfAction {
+class VconfActionDbl : public PluginAction, public VconfChangeAction {
 public:
-       VconfActionDbl(const std::string &key);
-       virtual ~VconfActionDbl() = default;
+       VconfActionDbl(const std::string &vconfKey);
 
        int set(double val);
-       double get();
        void undo() override;
        std::string serialize() override;
        int parse(const std::string &archive) override;
+       int setChangedCallback(valueChangedCB callback, void *userData) override;
+       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
+
+       void vconfChangedCB() override;
+       void vconfChangedCB(keynode_t *node) override;
 private:
+       std::string key;
+       double requestVal;
        double oldVal;
+       VconfCbHandler cbHandler;
+       valueChangedCB cb;
+       void *cbData;
 };
 
 MODES_NAMESPACE_END
-
index b6e2bb86fabb8f079dc1395790c4a85ba81887b3..08504f56eed9d32994a86269cc56864882143ad6 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 #include "VconfActionInt.h"
+
 #include <string>
 #include <sstream>
 #include <vconf.h>
 
 MODES_NAMESPACE_USE;
 
-VconfActionInt::VconfActionInt(const std::string &key)
-       : VconfAction("intVconf", key), oldVal(0)
+VconfActionInt::VconfActionInt(const std::string &vconfKey)
+       : PluginAction("intVconf"), key(vconfKey), requestVal(0), oldVal(0), cb(nullptr), cbData(nullptr)
 {
 }
 
 int VconfActionInt::set(int val)
 {
-       int ret;
-       DBG("setInt(%s, %d)", key.c_str(), val);
+       requestVal = val;
 
-       ret = handleChange();
+       int ret = cbHandler.handleSubscription(key);
        if (MODES_ERROR_NONE != ret) {
                ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
                return ret;
@@ -43,32 +43,22 @@ int VconfActionInt::set(int val)
                return MODES_ERROR_SYSTEM;
        }
 
-       ret = vconf_set_int(key.c_str(), val);
-       if (0 != ret) {
-               ERR("vconf_set_int(%s, %d) Fail(%d)", key.c_str(), val, ret);
-               return MODES_ERROR_SYSTEM;
-       }
-       return MODES_ERROR_NONE;
-}
-
-int VconfActionInt::get()
-{
-       int value = 0;
-       int ret = vconf_get_int(key.c_str(), &value);
-       if (0 != ret) {
-               ERR("vconf_get_int(%s) Fail(%d)", key.c_str(), ret);
-               return 0;
+       if (oldVal == val) {
+               INFO("vconf(%s) already set(%d)", key.c_str(), val);
+       } else {
+               ret = vconf_set_int(key.c_str(), val);
+               if (0 != ret) {
+                       ERR("vconf_set_int(%s, %d) Fail(%d)", key.c_str(), val, ret);
+                       return MODES_ERROR_SYSTEM;
+               }
        }
 
-       return value;
+       return MODES_ERROR_NONE;
 }
 
 void VconfActionInt::undo()
 {
-       int ret;
-       DBG("undoInt(%s, %d)", key.c_str(), oldVal);
-
-       ret = vconf_set_int(key.c_str(), oldVal);
+       int ret = vconf_set_int(key.c_str(), oldVal);
        if (0 != ret)
                ERR("vconf_set_int(%s, %d) Fail(%d)", key.c_str(), oldVal, ret);
 }
@@ -88,3 +78,49 @@ int VconfActionInt::parse(const std::string &archive)
 
        return MODES_ERROR_NONE;
 }
+
+int VconfActionInt::setChangedCallback(valueChangedCB callback, void *userData)
+{
+       RETV_IF(NULL == callback, MODES_ERROR_INVALID_PARAMETER);
+
+       int ret = cbHandler.setChangedCB(key, this);
+       if (MODES_ERROR_NONE != ret) {
+               ERR("setChangedCB(%s) Fail(%d)", key.c_str(), ret);
+               return ret;
+       }
+
+       cb = callback;
+       cbData = userData;
+
+       return MODES_ERROR_NONE;
+}
+
+void VconfActionInt::unSetChangedCallback(valueChangedCB callback, void *userData)
+{
+       RET_IF(NULL == callback);
+
+       cbHandler.unSetChangedCB(key);
+
+       cbData = NULL;
+       cb = NULL;
+}
+
+void VconfActionInt::vconfChangedCB()
+{
+       //The value will be changed by Modes. That's why the callback is always called.
+       if (cb)
+               cb(cbData);
+}
+
+void VconfActionInt::vconfChangedCB(keynode_t *node)
+{
+       const char *vconfKey = vconf_keynode_get_name(node);
+       if (vconfKey != key) {
+               WARN("Unknown vconf(%s) notification", vconfKey);
+               return;
+       }
+
+       int val = vconf_keynode_get_int(node);
+       if (cb && (val != requestVal))
+               cb(cbData);
+}
index 086665d6c8f847b4cac70c3fedd8fe93e75de133..49108e8fe1032e41a32f126b3ecadf4cd73e09e2 100644 (file)
 #pragma once
 
 #include <string>
-#include "VconfAction.h"
+#include <PluginAction.h>
 #include "plugin-def.h"
+#include "VconfCbHandler.h"
 
 MODES_NAMESPACE_BEGIN
 
-class VconfActionInt : public VconfAction {
+class VconfActionInt : public PluginAction, public VconfChangeAction {
 public:
-       VconfActionInt(const std::string &key);
-       virtual ~VconfActionInt() = default;
+       VconfActionInt(const std::string &vconfKey);
 
        int set(int val);
-       int get();
        void undo() override;
        std::string serialize() override;
        int parse(const std::string &archive) override;
+       int setChangedCallback(valueChangedCB callback, void *userData) override;
+       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
+
+       void vconfChangedCB() override;
+       void vconfChangedCB(keynode_t *node) override;
 private:
+       std::string key;
+       int requestVal;
        int oldVal;
+       VconfCbHandler cbHandler;
+       valueChangedCB cb;
+       void *cbData;
 };
 
 MODES_NAMESPACE_END
-
index 024e128ee4b7e713e6bc429bbbde30c714703e74..b4832619791bc7bf0a67d4e606325702756a868d 100644 (file)
  * limitations under the License.
  */
 #include "VconfActionStr.h"
+
 #include <string>
 #include <vconf.h>
 #include "plugin-log.h"
 
 MODES_NAMESPACE_USE;
 
-VconfActionStr::VconfActionStr(const std::string &key)
-       : VconfAction("strVconf", key)
+VconfActionStr::VconfActionStr(const std::string &vconfKey)
+       : PluginAction("strVconf"), key(vconfKey), cb(nullptr), cbData(nullptr)
 {
 }
 
 int VconfActionStr::set(const std::string &val)
 {
-       int ret;
-       DBG("setStr(%s, %s)", key.c_str(), val.c_str());
+       requestVal = val;
 
-       ret = handleChange();
+       int ret = cbHandler.handleSubscription(key);
        if (MODES_ERROR_NONE != ret) {
                ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
                return ret;
@@ -44,34 +44,22 @@ int VconfActionStr::set(const std::string &val)
        oldVal = prev;
        free(prev);
 
-       ret = vconf_set_str(key.c_str(), val.c_str());
-       if (0 != ret) {
-               ERR("vconf_set_str(%s, %s) Fail(%d)", key.c_str(), val.c_str(), ret);
-               return MODES_ERROR_SYSTEM;
+       if (oldVal == val) {
+               INFO("vconf(%s) already set(%s)", key.c_str(), val.c_str());
+       } else {
+               ret = vconf_set_str(key.c_str(), val.c_str());
+               if (0 != ret) {
+                       ERR("vconf_set_str(%s, %s) Fail(%d)", key.c_str(), val.c_str(), ret);
+                       return MODES_ERROR_SYSTEM;
+               }
        }
-       return MODES_ERROR_NONE;
-}
-
-std::string VconfActionStr::get()
-{
-       char *value = vconf_get_str(key.c_str());
-       if (NULL == value) {
-               ERR("vconf_get_str(%s) Fail()", key.c_str());
-               return std::string();
-       }
-
-       std::string retStr = value;
-       free(value);
 
-       return retStr;
+       return MODES_ERROR_NONE;
 }
 
 void VconfActionStr::undo()
 {
-       int ret;
-       DBG("undoStr(%s, %s)", key.c_str(), oldVal.c_str());
-
-       ret = vconf_set_str(key.c_str(), oldVal.c_str());
+       int ret = vconf_set_str(key.c_str(), oldVal.c_str());
        if (0 != ret)
                ERR("vconf_set_str(%s, %s) Fail(%d)", key.c_str(), oldVal.c_str(), ret);
 }
@@ -86,3 +74,49 @@ int VconfActionStr::parse(const std::string &archive)
        oldVal = archive;
        return MODES_ERROR_NONE;
 }
+
+int VconfActionStr::setChangedCallback(valueChangedCB callback, void *userData)
+{
+       RETV_IF(nullptr == callback, MODES_ERROR_INVALID_PARAMETER);
+
+       int ret = cbHandler.setChangedCB(key, this);
+       if (MODES_ERROR_NONE != ret) {
+               ERR("setChangedCB(%s) Fail(%d)", key.c_str(), ret);
+               return ret;
+       }
+
+       cb = callback;
+       cbData = userData;
+
+       return MODES_ERROR_NONE;
+}
+
+void VconfActionStr::unSetChangedCallback(valueChangedCB callback, void *userData)
+{
+       RET_IF(nullptr == callback);
+
+       cbHandler.unSetChangedCB(key);
+
+       cbData = nullptr;
+       cb = nullptr;
+}
+
+void VconfActionStr::vconfChangedCB()
+{
+       //The value will be changed by Modes. That's why the callback is always called.
+       if (cb)
+               cb(cbData);
+}
+
+void VconfActionStr::vconfChangedCB(keynode_t *node)
+{
+       const char *vconfKey = vconf_keynode_get_name(node);
+       if (vconfKey != key) {
+               WARN("Unknown vconf(%s) notification", vconfKey);
+               return;
+       }
+
+       char *val = vconf_keynode_get_str(node);
+       if (cb && (val != requestVal))
+               cb(cbData);
+}
index 38af35c6dca424d34de36ca61389df2d6f84d5ac..ad841541e6a58f9958f697fa3c7f54917eceb96f 100644 (file)
 #pragma once
 
 #include <string>
-#include "VconfAction.h"
+#include <PluginAction.h>
 #include "plugin-def.h"
+#include "VconfCbHandler.h"
 
 MODES_NAMESPACE_BEGIN
 
-class VconfActionStr : public VconfAction {
+class VconfActionStr : public PluginAction, public VconfChangeAction {
 public:
-       VconfActionStr(const std::string &key);
-       ~VconfActionStr() = default;
+       VconfActionStr(const std::string &vconfKey);
 
        int set(const std::string &val);
-       std::string get();
        void undo() override;
        std::string serialize() override;
        int parse(const std::string &archive) override;
+       int setChangedCallback(valueChangedCB callback, void *userData) override;
+       void unSetChangedCallback(valueChangedCB callback, void *userData) override;
+
+       void vconfChangedCB() override;
+       void vconfChangedCB(keynode_t *node) override;
 private:
+       std::string key;
+       std::string requestVal;
        std::string oldVal;
+       VconfCbHandler cbHandler;
+       valueChangedCB cb;
+       void *cbData;
 };
 
 MODES_NAMESPACE_END
index e0739e4c8e7f839afbb0e72ef6db006f28b1c963..c74889354d3bb5075ec96c019e2f2fa8c34fc50f 100644 (file)
@@ -37,11 +37,6 @@ public:
        int set(const std::string &key, const std::string &val, PluginAction **piAction) override;
        PluginAction* getUndoAction(const std::string &key, const std::string &info) override;
        std::string serializeAction(PluginAction *piAction) override;
-
-       int getInt(const std::string &key) override;
-       double getDouble(const std::string &key) override;
-       bool getBool(const std::string &key) override;
-       std::string getString(const std::string &key) override;
 };
 
 extern "C" API Plugin *objectCreate(void)
@@ -166,51 +161,3 @@ std::string VconfPlugin::serializeAction(PluginAction *piAction)
        std::string rawStr = piAction->serialize();
        return piAction->getName().front() + rawStr;
 }
-
-int VconfPlugin::getInt(const std::string &key)
-{
-       DBG("getInt(%s)", key.c_str());
-
-       std::string newKey(key);
-       std::replace(newKey.begin(), newKey.end(), '.', '/');
-
-       VconfActionInt action(newKey);
-
-       return action.get();
-}
-
-double VconfPlugin::getDouble(const std::string &key)
-{
-       DBG("getDouble(%s)", key.c_str());
-
-       std::string newKey(key);
-       std::replace(newKey.begin(), newKey.end(), '.', '/');
-
-       VconfActionDbl action(newKey);
-
-       return action.get();
-}
-
-bool VconfPlugin::getBool(const std::string &key)
-{
-       DBG("getBool(%s)", key.c_str());
-
-       std::string newKey(key);
-       std::replace(newKey.begin(), newKey.end(), '.', '/');
-
-       VconfActionBool action(newKey);
-
-       return action.get();
-}
-
-std::string VconfPlugin::getString(const std::string &key)
-{
-       DBG("getString(%s)", key.c_str());
-
-       std::string newKey(key);
-       std::replace(newKey.begin(), newKey.end(), '.', '/');
-
-       VconfActionStr action(newKey);
-
-       return action.get();
-}