cb2Called = false;
}
- static void shoudNotBeCalled(const std::string &key, void *userData)
+ static void shoudNotBeCalled(void *userData)
{
- ERR("This Callback(%s) should not be called", key.c_str());
+ ERR("This Callback(%s) should not be called", (char*)userData);
g_main_loop_quit(loop);
GTEST_FAIL();
}
- static void valChangedCb(const std::string &key, void *userData)
+ static void valChangedCb(void *userData)
{
- DBG("%s changed callback called!", key.c_str());
+ DBG("%s changed callback called!", (char*)userData);
g_main_loop_quit(loop);
}
- static void callback1(const std::string &key, void *userData)
+ static void callback1(void *userData)
{
- DBG("%s changed callback called!", key.c_str());
+ DBG("%s changed callback called!", (char*)userData);
cb1Called = true;
}
- static void callback2(const std::string &key, void *userData)
+ static void callback2(void *userData)
{
- DBG("%s changed callback called!", key.c_str());
+ DBG("%s changed callback called!", (char*)userData);
cb2Called = true;
g_main_loop_quit(loop);
}
TEST_F(VconfPluginTest, callbackPluginVconf)
{
int oldVal = 0;
+ const char *key = "db.setting.psmode";
int ret = vconf_get_int("db/setting/psmode", &oldVal);
EXPECT_EQ(ret, 0);
- ret = plugin->setChangedCallback(valChangedCb, "db.setting.psmode", nullptr);
+ PluginAction *action;
+ ret = plugin->set(key, 0, &action);
+ EXPECT_EQ(ret, MODES_ERROR_NONE);
+
+ ret = action->setChangedCallback(valChangedCb, (void*)key);
EXPECT_EQ(ret, MODES_ERROR_NONE);
g_idle_add(changedCallbackidler, nullptr);
g_main_loop_run(loop);
- ret = plugin->unSetChangedCallback(valChangedCb, "db.setting.psmode", nullptr);
+ // It should be removed by the changed callback procedure.
+ ret = action->unSetChangedCallback(valChangedCb, (void*)key);
EXPECT_EQ(ret, MODES_ERROR_NO_DATA);
- ret = plugin->setChangedCallback(shoudNotBeCalled, "db.setting.psmode", nullptr);
+ ret = action->setChangedCallback(shoudNotBeCalled, (void*)key);
EXPECT_EQ(ret, MODES_ERROR_NONE);
- ret = plugin->unSetChangedCallback(shoudNotBeCalled, "db.setting.psmode", nullptr);
+ ret = action->unSetChangedCallback(shoudNotBeCalled, (void*)key);
EXPECT_EQ(ret, MODES_ERROR_NONE);
vconf_set_int("db/setting/psmode", oldVal);
}
TEST_F(VconfPluginTest, callbackPluginVconfReset)
{
int ret;
- ret = plugin->set("db.setting.psmode", 4, nullptr);
+ const char *key = "db.setting.psmode";
+
+ PluginAction *action1;
+ ret = plugin->set(key, 4, &action1);
EXPECT_EQ(ret, MODES_ERROR_NONE);
- ret = plugin->setChangedCallback(callback1, "db.setting.psmode", nullptr);
+
+ ret = action1->setChangedCallback(callback1, nullptr);
EXPECT_EQ(ret, MODES_ERROR_NONE);
- ret = plugin->set("db.setting.psmode", 2, nullptr);
+ PluginAction *action2;
+ ret = plugin->set(key, 2, &action2);
EXPECT_EQ(ret, MODES_ERROR_NONE);
EXPECT_TRUE(cb1Called);
- ret = plugin->setChangedCallback(callback2, "db.setting.psmode", nullptr);
+ ret = action2->setChangedCallback(callback2, nullptr);
EXPECT_EQ(ret, MODES_ERROR_NONE);
g_idle_add(changedCallbackidler, nullptr);
g_main_loop_run(loop);
EXPECT_TRUE(cb2Called);
- ret = plugin->unSetChangedCallback(callback2, "db.setting.psmode", nullptr);
+ ret = action2->unSetChangedCallback(callback2, nullptr);
EXPECT_EQ(ret, MODES_ERROR_NO_DATA);
vconf_set_int("db/setting/psmode", 0);
}
--- /dev/null
+/*
+ * Copyright (c) 2019 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 (VCONF_OK != ret) {
+ ERR("vconf_notify_key_changed(%s) Fail(%s)", key.c_str(), get_error_message(ret));
+ return MODES_ERROR_SYSTEM;
+ }
+
+ DBG("setChangedCallback(%s) Success", key.c_str());
+ return MODES_ERROR_NONE;
+}
+
+int VconfAction::unSetChangedCallback(valueChangedCB callback, void * userData)
+{
+ auto found = callbackMap.find(key);
+ if (found == callbackMap.end()) {
+ ERR("No Changed Callback(%s)", key.c_str());
+ return MODES_ERROR_NO_DATA;
+ }
+
+ callbackMap.erase(found);
+ int ret = vconf_ignore_key_changed(key.c_str(), vconfChangedCallback);
+ if (VCONF_OK != ret) {
+ ERR("vconf_ignore_key_changed(%s) Fail(%s)", key.c_str(), get_error_message(ret));
+ return MODES_ERROR_SYSTEM;
+ }
+
+ DBG("unSetChangedCallback(%s) Success", key.c_str());
+ return MODES_ERROR_NONE;
+}
+
+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 (VCONF_OK != ret) {
+ ERR("vconf_ignore_key_changed(%s) Fail(%s)", key.c_str(), get_error_message(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;
+ }
+
+ std::string newKey(node->keyname);
+ std::replace(newKey.begin(), newKey.end(), '/', '.');
+ cbData->callback(cbData->userData);
+ DBG("Action(%s) is Changed", node->keyname);
+
+ callbackMap.erase(found);
+ int ret = vconf_ignore_key_changed(node->keyname, vconfChangedCallback);
+ if (VCONF_OK != ret)
+ ERR("vconf_ignore_key_changed(%s) Fail(%s)", node->keyname, get_error_message(ret));
+}
--- /dev/null
+/*
+ * Copyright (c) 2019 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;
+ int 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
+
MODES_NAMESPACE_USE;
-VconfActionBool::VconfActionBool(const std::string &vconfKey)
- : PluginAction("boolVconf"), key(vconfKey), oldVal(false)
+VconfActionBool::VconfActionBool(const std::string &key)
+ : VconfAction("boolVconf", key), oldVal(false)
{
}
int ret;
DBG("setBool(%s, %d)", key.c_str(), val);
+ ret = handleChange();
+ if (MODES_ERROR_NONE != ret) {
+ ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
+ return ret;
+ }
+
int prev;
ret = vconf_get_bool(key.c_str(), &prev);
if (0 != ret) {
#pragma once
#include <string>
-#include <PluginAction.h>
+#include "VconfAction.h"
#include "plugin-def.h"
MODES_NAMESPACE_BEGIN
-class VconfActionBool : public PluginAction {
+class VconfActionBool : public VconfAction {
public:
- VconfActionBool(const std::string &vconfKey);
+ VconfActionBool(const std::string &key);
virtual ~VconfActionBool() = default;
int set(bool val);
std::string serialize() override;
int parse(const std::string &archive) override;
private:
- std::string key;
bool oldVal;
};
MODES_NAMESPACE_USE;
-VconfActionDbl::VconfActionDbl(const std::string &vconfKey)
- : PluginAction("dblVconf"), key(vconfKey), oldVal(0.0)
+VconfActionDbl::VconfActionDbl(const std::string &key)
+ : VconfAction("dblVconf", key), oldVal(0.0)
{
}
int ret;
DBG("setDbl(%s, %f)", key.c_str(), val);
+ ret = handleChange();
+ if (MODES_ERROR_NONE != ret) {
+ ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
+ return ret;
+ }
+
ret = vconf_get_dbl(key.c_str(), &oldVal);
if (0 != ret) {
ERR("vconf_get_dbl(%s) Fail(%d)", key.c_str(), ret);
#pragma once
#include <string>
-#include <PluginAction.h>
+#include "VconfAction.h"
#include "plugin-def.h"
MODES_NAMESPACE_BEGIN
-class VconfActionDbl : public PluginAction {
+class VconfActionDbl : public VconfAction {
public:
- VconfActionDbl(const std::string &vconfKey);
+ VconfActionDbl(const std::string &key);
virtual ~VconfActionDbl() = default;
int set(double val);
std::string serialize() override;
int parse(const std::string &archive) override;
private:
- std::string key;
double oldVal;
};
MODES_NAMESPACE_USE;
-VconfActionInt::VconfActionInt(const std::string &vconfKey)
- : PluginAction("intVconf"), key(vconfKey), oldVal(0)
+VconfActionInt::VconfActionInt(const std::string &key)
+ : VconfAction("intVconf", key), oldVal(0)
{
}
int ret;
DBG("setInt(%s, %d)", key.c_str(), val);
+ ret = handleChange();
+ if (MODES_ERROR_NONE != ret) {
+ ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
+ return ret;
+ }
+
ret = vconf_get_int(key.c_str(), &oldVal);
if (0 != ret) {
ERR("vconf_get_int(%s) Fail(%d)", key.c_str(), ret);
#pragma once
#include <string>
-#include <PluginAction.h>
+#include "VconfAction.h"
#include "plugin-def.h"
MODES_NAMESPACE_BEGIN
-class VconfActionInt : public PluginAction {
+class VconfActionInt : public VconfAction {
public:
- VconfActionInt(const std::string &vconfKey);
+ VconfActionInt(const std::string &key);
virtual ~VconfActionInt() = default;
int set(int val);
std::string serialize() override;
int parse(const std::string &archive) override;
private:
- std::string key;
int oldVal;
};
MODES_NAMESPACE_USE;
-VconfActionStr::VconfActionStr(const std::string &vconfKey)
- : PluginAction("strVconf"), key(vconfKey)
+VconfActionStr::VconfActionStr(const std::string &key)
+ : VconfAction("strVconf", key)
{
}
int ret;
DBG("setStr(%s, %s)", key.c_str(), val.c_str());
+ ret = handleChange();
+ if (MODES_ERROR_NONE != ret) {
+ ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
+ return ret;
+ }
+
char *prev = vconf_get_str(key.c_str());
if (NULL == prev) {
ERR("vconf_get_str(%s) Fail()", key.c_str());
#pragma once
#include <string>
-#include <PluginAction.h>
+#include "VconfAction.h"
#include "plugin-def.h"
MODES_NAMESPACE_BEGIN
-class VconfActionStr : public PluginAction {
+class VconfActionStr : public VconfAction {
public:
- VconfActionStr(const std::string &vconfKey);
+ VconfActionStr(const std::string &key);
~VconfActionStr() = default;
int set(const std::string &val);
std::string serialize() override;
int parse(const std::string &archive) override;
private:
- std::string key;
std::string oldVal;
};
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include <map>
#include <string>
#include <algorithm>
#include <vconf.h>
double getDouble(const std::string &key) override;
bool getBool(const std::string &key) override;
std::string getString(const std::string &key) override;
-
- int setChangedCallback(valueChangedCb callback, const std::string &key, void *userData);
- int unSetChangedCallback(valueChangedCb callback, const std::string &key, void *userData);
-private:
- struct CallbackData {
- void *userData;
- valueChangedCb *callback;
- };
-
- int handleChange(const std::string &key);
- static void vconfChangedCallback(keynode_t *node, void *userData);
-
- static std::map<std::string, CallbackData> callbackMap;
};
extern "C" API Plugin *objectCreate(void)
delete plugin;
}
-std::map<std::string, VconfPlugin::CallbackData> VconfPlugin::callbackMap;
-
VconfPlugin::VconfPlugin()
{
setName("vconf");
std::string newKey(key);
std::replace(newKey.begin(), newKey.end(), '.', '/');
- ret = handleChange(newKey);
- if (MODES_ERROR_NONE != ret) {
- ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret);
- return ret;
- }
-
VconfActionInt *action = new VconfActionInt(newKey);
ret = action->set(val);
if (piAction)
std::string newKey(key);
std::replace(newKey.begin(), newKey.end(), '.', '/');
- ret = handleChange(newKey);
- if (MODES_ERROR_NONE != ret) {
- ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret);
- return ret;
- }
-
VconfActionDbl *action = new VconfActionDbl(newKey);
ret = action->set(val);
if (piAction)
std::string newKey(key);
std::replace(newKey.begin(), newKey.end(), '.', '/');
- ret = handleChange(newKey);
- if (MODES_ERROR_NONE != ret) {
- ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret);
- return ret;
- }
-
VconfActionBool *action = new VconfActionBool(newKey);
ret = action->set(val);
if (piAction)
std::string newKey(key);
std::replace(newKey.begin(), newKey.end(), '.', '/');
- int ret = handleChange(newKey);
- if (MODES_ERROR_NONE != ret) {
- ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret);
- return ret;
- }
-
VconfActionStr *action = new VconfActionStr(newKey);
- ret = action->set(val);
+ int ret = action->set(val);
if (piAction)
*piAction = action;
else
return action.get();
}
-
-int VconfPlugin::setChangedCallback(valueChangedCb callback, const std::string &key, void *userData)
-{
- std::string newKey(key);
- std::replace(newKey.begin(), newKey.end(), '.', '/');
-
- callbackMap[newKey].userData = userData;
- callbackMap[newKey].callback = callback;
-
- int ret = vconf_notify_key_changed(newKey.c_str(), VconfPlugin::vconfChangedCallback, &callbackMap[newKey]);
- if (VCONF_OK != ret) {
- ERR("vconf_notify_key_changed(%s) Fail(%s)", newKey.c_str(), get_error_message(ret));
- return MODES_ERROR_SYSTEM;
- }
-
- DBG("setChangedCallback(%s) Success", newKey.c_str());
- return MODES_ERROR_NONE;
-}
-
-int VconfPlugin::unSetChangedCallback(valueChangedCb callback, const std::string &key, void *userData)
-{
- std::string newKey(key);
- std::replace(newKey.begin(), newKey.end(), '.', '/');
-
- auto found = callbackMap.find(newKey);
- if (found == callbackMap.end()) {
- ERR("No Changed Callback(%s)", newKey.c_str());
- return MODES_ERROR_NO_DATA;
- }
-
- callbackMap.erase(found);
- int ret = vconf_ignore_key_changed(newKey.c_str(), VconfPlugin::vconfChangedCallback);
- if (VCONF_OK != ret) {
- ERR("vconf_ignore_key_changed(%s) Fail(%s)", newKey.c_str(), get_error_message(ret));
- return MODES_ERROR_SYSTEM;
- }
-
- DBG("unSetChangedCallback(%s) Success", newKey.c_str());
- return MODES_ERROR_NONE;
-}
-
-int VconfPlugin::handleChange(const std::string &key)
-{
- auto found = callbackMap.find(key);
- if (callbackMap.end() != found) {
- DBG("Acition(%s) already exist", key.c_str());
- found->second.callback(key, found->second.userData);
-
- callbackMap.erase(found);
- int ret = vconf_ignore_key_changed(key.c_str(), VconfPlugin::vconfChangedCallback);
- if (VCONF_OK != ret) {
- ERR("vconf_ignore_key_changed(%s) Fail(%s)", key.c_str(), get_error_message(ret));
- return MODES_ERROR_SYSTEM;
- }
- }
- return MODES_ERROR_NONE;
-}
-
-void VconfPlugin::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;
- }
-
- std::string newKey(node->keyname);
- std::replace(newKey.begin(), newKey.end(), '/', '.');
- cbData->callback(node->keyname, cbData->userData);
- DBG("Action(%s) is Changed", node->keyname);
-
- callbackMap.erase(found);
- int ret = vconf_ignore_key_changed(node->keyname, VconfPlugin::vconfChangedCallback);
- if (VCONF_OK != ret)
- ERR("vconf_ignore_key_changed(%s) Fail(%s)", node->keyname, get_error_message(ret));
-}