From: Youngjae Shin Date: Wed, 11 Sep 2019 03:46:16 +0000 (+0900) Subject: revise architecture of handling plugin Action X-Git-Tag: submit/tizen/20200406.072014~42 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=12ce415a9325296054302b92881d3cc85ecb44a6;p=platform%2Fcore%2Fsystem%2Fmodes-plugins.git revise architecture of handling plugin Action --- diff --git a/app/AppAction.cpp b/app/AppAction.cpp deleted file mode 100644 index 5d56958..0000000 --- a/app/AppAction.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 "AppAction.h" -#include - -MODES_NAMESPACE_USE; - -AppAction::AppAction() -{ -} - -AppAction::~AppAction() -{ -} - -int AppAction::set(const std::string &val) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -int AppAction::undo(const std::string &val) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -int AppAction::get(std::string *val) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -void AppAction::setName(std::string name) -{ - this->name = name; -} - -std::string AppAction::getName() -{ - return name; -} diff --git a/app/AppAction.h b/app/AppAction.h index 7e53fb8..613b6d3 100644 --- a/app/AppAction.h +++ b/app/AppAction.h @@ -16,25 +16,24 @@ #pragma once #include +#include #include "plugin-def.h" MODES_NAMESPACE_BEGIN -class AppAction { +class AppAction : public PluginAction { public: - AppAction(); - virtual ~AppAction(); - - std::string getName(); - virtual int set(const std::string &val); - virtual int get(std::string *val); - virtual int undo(const std::string &val); - -protected: - void setName(std::string name); - -private: - std::string name; + AppAction() = default; + virtual ~AppAction() = default; + + virtual int set(const std::string &val) + { + return MODES_ERROR_NOT_SUPPORTED; + } + virtual int get(std::string *val) + { + return MODES_ERROR_NOT_SUPPORTED; + } }; MODES_NAMESPACE_END diff --git a/app/AppActionLaunch.cpp b/app/AppActionLaunch.cpp index 0c171f3..b8eddb8 100644 --- a/app/AppActionLaunch.cpp +++ b/app/AppActionLaunch.cpp @@ -75,7 +75,7 @@ int AppActionLaunch::set(const std::string &val) int ret = app_control_send_launch_request(service, NULL, NULL); if (APP_CONTROL_ERROR_NONE != ret) { ERR("app_control_send_launch_request() Fail(%s)", get_error_message(ret)); - return MODES_ERROR_IO_ERROR; + return MODES_ERROR_SYSTEM; } app_control_destroy(service); @@ -83,10 +83,10 @@ int AppActionLaunch::set(const std::string &val) DBG("APP_CONTEXT_EVENT_LAUNCHED(appid : %s) added list\n", val.c_str()); - ret = app_manager_set_app_context_status_cb(AppActionLaunch::appContextStatusCallback, val.c_str(), NULL); - if (APP_MANAGER_ERROR_NONE != ret) { - ERR("app_manager_set_app_context_status_cb() Fail(%s)", get_error_message(ret)); - return MODES_ERROR_IO_ERROR; + int err = app_manager_set_app_context_status_cb(AppActionLaunch::appContextStatusCallback, val.c_str(), NULL); + if (APP_MANAGER_ERROR_NONE != err) { + ERR("app_manager_set_app_context_status_cb() Fail(%s)", get_error_message(err)); + return MODES_ERROR_SYSTEM; } appID = val; @@ -102,34 +102,34 @@ int AppActionLaunch::get(std::string *val) return MODES_ERROR_NONE; } -int AppActionLaunch::undo(const std::string &val) +int AppActionLaunch::undo() { bool running; app_context_h runAppContext; - app_manager_is_running(val.c_str(), &running); + app_manager_is_running(appID.c_str(), &running); if (!running) { DBG("It's NOT running"); return MODES_ERROR_NONE; } - int ret = app_manager_unset_app_context_status_cb(AppActionLaunch::appContextStatusCallback, val.c_str()); + int ret = app_manager_unset_app_context_status_cb(AppActionLaunch::appContextStatusCallback, appID.c_str()); if (APP_MANAGER_ERROR_NONE != ret) { ERR("app_manager_unset_app_context_status_cb() Fail(%s)", get_error_message(ret)); - return MODES_ERROR_IO_ERROR; + return MODES_ERROR_SYSTEM; } - appidList.remove(val); + appidList.remove(appID); - ret = app_manager_get_app_context(val.c_str(), &runAppContext); + ret = app_manager_get_app_context(appID.c_str(), &runAppContext); if (APP_MANAGER_ERROR_NONE != ret) { - ERR("app_manager_get_app_context(%s) Fail(%s)", val.c_str(), get_error_message(ret)); - return MODES_ERROR_IO_ERROR; + ERR("app_manager_get_app_context(%s) Fail(%s)", appID.c_str(), get_error_message(ret)); + return MODES_ERROR_SYSTEM; } ret = app_manager_terminate_app(runAppContext); if (APP_MANAGER_ERROR_NONE != ret) { ERR("app_manager_terminate_app() Fail(%s)", get_error_message(ret)); - return MODES_ERROR_IO_ERROR; + return MODES_ERROR_SYSTEM; } return MODES_ERROR_NONE; } diff --git a/app/AppActionLaunch.h b/app/AppActionLaunch.h index 40904e3..e0f8087 100644 --- a/app/AppActionLaunch.h +++ b/app/AppActionLaunch.h @@ -29,7 +29,7 @@ public: virtual int set(const std::string &val) override; virtual int get(std::string *val) override; - virtual int undo(const std::string &val) override; + virtual int undo() override; static const std::string NAME; static void appContextStatusCallback(app_context_h app_context, app_context_status_e status, void *user_data); diff --git a/app/AppPlugin.cpp b/app/AppPlugin.cpp index 8e9afb2..8b78c0b 100644 --- a/app/AppPlugin.cpp +++ b/app/AppPlugin.cpp @@ -26,8 +26,8 @@ public: AppPlugin(); ~AppPlugin(); - int set(const std::string &key, const std::string &val, std::string *oldVal) override; - int undo(const std::string &key, const std::string &val) override; + int set(const std::string &key, const std::string &val, PluginAction **pluginAction) override; + int undo(PluginAction *pluginAction) override; private: AppFactory appFactory; @@ -52,30 +52,31 @@ AppPlugin::~AppPlugin() { } -int AppPlugin::set(const std::string &key, const std::string &val, std::string *oldVal) +int AppPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) { AppAction *action = appFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %s]", key.c_str(), val.c_str()); - int appRet = action->set(val); + int ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + appFactory.destroyAction(action); - if (oldVal) - action->get(oldVal); - - appFactory.destroyAction(action); - return appRet; + return ret; } -int AppPlugin::undo(const std::string &key, const std::string &val) +int AppPlugin::undo(PluginAction *pluginAction) { - AppAction *action = appFactory.createAction(key); - RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); + AppAction *action = static_cast(pluginAction); + RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); - DBG("set [%s, %s]", key.c_str(), val.c_str()); + DBG("Action(%s) undo", action->getName().c_str()); - int appRet = action->undo(val); + int ret = action->undo(); appFactory.destroyAction(action); - return appRet; + + return ret; } diff --git a/bluetooth/BtAction.cpp b/bluetooth/BtAction.cpp index ce18e57..984351d 100644 --- a/bluetooth/BtAction.cpp +++ b/bluetooth/BtAction.cpp @@ -24,8 +24,6 @@ BtAction::BtAction() if (BT_ERROR_NONE != btRet) { ERR("bt_initialize() Fail(%d)", btRet); } - - btAdapterStatus = getAdapterStatus(); } BtAction::~BtAction() @@ -44,37 +42,12 @@ bt_adapter_state_e BtAction::getAdapterStatus() return state; } -int BtAction::set(bool val, int *oldVal) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -int BtAction::set(bool val, double *oldVal) +int BtAction::set(bool val) { return MODES_ERROR_NOT_SUPPORTED; } -int BtAction::set(bool val, bool *oldVal) +int BtAction::set(const std::string &val) { return MODES_ERROR_NOT_SUPPORTED; } - -int BtAction::set(const std::string &val, std::string *oldVal) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -int BtAction::undo(bool val) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -void BtAction::setName(std::string name) -{ - this->name = name; -} - -std::string BtAction::getName() -{ - return name; -} diff --git a/bluetooth/BtAction.h b/bluetooth/BtAction.h index c1b0b18..b901114 100644 --- a/bluetooth/BtAction.h +++ b/bluetooth/BtAction.h @@ -17,30 +17,20 @@ #include #include +#include #include "plugin-log.h" #include "plugin-def.h" MODES_NAMESPACE_BEGIN -class BtAction { +class BtAction : public PluginAction { public: BtAction(); virtual ~BtAction(); + virtual int set(bool val); + virtual int set(const std::string &val); bt_adapter_state_e getAdapterStatus(); - std::string getName(); - - virtual int set(bool val, int *oldVal); - virtual int set(bool val, double *oldVal); - virtual int set(bool val, bool *oldVal); - virtual int set(const std::string &val, std::string *oldVal); - virtual int undo(bool val); - -protected: - void setName(std::string name); - bt_adapter_state_e btAdapterStatus; -private: - std::string name; }; MODES_NAMESPACE_END diff --git a/bluetooth/BtActionAudioConnect.cpp b/bluetooth/BtActionAudioConnect.cpp index 62293b5..0829ac6 100644 --- a/bluetooth/BtActionAudioConnect.cpp +++ b/bluetooth/BtActionAudioConnect.cpp @@ -111,7 +111,7 @@ void BtActionAudioConnect::btDeviceBondCreatedCb(int result, bt_device_info_s *d DBG("BtActionAudioConnect Success! [%s][%s]", device_info->remote_name, device_info->remote_address); } -int BtActionAudioConnect::set(const std::string &val, std::string *oldVal) +int BtActionAudioConnect::set(const std::string &val) { if (!isBtAdapterEnabled()) { ERR("BT is disabled"); @@ -126,7 +126,6 @@ int BtActionAudioConnect::set(const std::string &val, std::string *oldVal) } #endif - *oldVal = std::string(); int btRet; btRet = bt_device_set_bond_created_cb(btDeviceBondCreatedCb, NULL); diff --git a/bluetooth/BtActionAudioConnect.h b/bluetooth/BtActionAudioConnect.h index 6a6d20f..a0e2243 100644 --- a/bluetooth/BtActionAudioConnect.h +++ b/bluetooth/BtActionAudioConnect.h @@ -23,7 +23,7 @@ MODES_NAMESPACE_BEGIN class BtActionAudioConnect : public BtAction { public: BtActionAudioConnect(); - int set(const std::string &val, std::string *oldVal) override; + int set(const std::string &val) override; private: bool isBtAdapterEnabled(); diff --git a/bluetooth/BtActionPower.cpp b/bluetooth/BtActionPower.cpp index f1c01e9..7e329cd 100644 --- a/bluetooth/BtActionPower.cpp +++ b/bluetooth/BtActionPower.cpp @@ -24,29 +24,30 @@ MODES_NAMESPACE_USE; #define SAFE_POINTER_ASSIGN( arg1, arg2) if ( arg1 != NULL ) { *arg1 = arg2; } BtActionPower::BtActionPower() + :undoVal(false) { setName("power"); } -int BtActionPower::set(bool val, bool *oldVal) +int BtActionPower::set(bool val) { if (val) { - SAFE_POINTER_ASSIGN(oldVal, false); + undoVal = false; int btRet = bt_adapter_enable(); if (BT_ERROR_NONE != btRet) { if (BT_ERROR_ALREADY_DONE == btRet) { - SAFE_POINTER_ASSIGN(oldVal, true); + undoVal = true; } else { ERR("bt_adapter_enable() Fail(%d)", btRet); return MODES_ERROR_SYSTEM; } } } else { - SAFE_POINTER_ASSIGN(oldVal, true); + undoVal = true; int btRet = bt_adapter_disable(); if (BT_ERROR_NONE != btRet) { if (BT_ERROR_NOT_ENABLED == btRet) { - SAFE_POINTER_ASSIGN(oldVal, false); + undoVal = false; } else { ERR("bt_adapter_disable() Fail(%d)", btRet); return MODES_ERROR_SYSTEM; @@ -54,11 +55,11 @@ int BtActionPower::set(bool val, bool *oldVal) } } - INFO("Bluetooth power [%s]", val ? "On" : "Off"); + INFO("BT power changed to (%s) from (%s)", val ? "On" : "Off", undoVal ? "On" : "Off"); return MODES_ERROR_NONE; } -int BtActionPower::undo(bool val) +int BtActionPower::undo() { - return set(val, nullptr); + return set(undoVal); } diff --git a/bluetooth/BtActionPower.h b/bluetooth/BtActionPower.h index 1aeebed..24ea19c 100644 --- a/bluetooth/BtActionPower.h +++ b/bluetooth/BtActionPower.h @@ -24,9 +24,10 @@ class BtActionPower : public BtAction { public: BtActionPower(); - int set(bool val, bool *oldVal) override; - int undo(bool val) override; + int set(bool val) override; + int undo() override; private: + bool undoVal; }; MODES_NAMESPACE_END diff --git a/bluetooth/BtPlugin.cpp b/bluetooth/BtPlugin.cpp index 3da5d94..eea6d47 100644 --- a/bluetooth/BtPlugin.cpp +++ b/bluetooth/BtPlugin.cpp @@ -27,12 +27,11 @@ public: BtPlugin(); ~BtPlugin(); - int set(const std::string &key, int val, int *oldVal) override; - int set(const std::string &key, double val, double *oldVal) override; - int set(const std::string &key, bool val, bool *oldVal) override; - int set(const std::string &key, const std::string &val, std::string *oldVal) override; - int undo(const std::string &key, bool val) override; - + int set(const std::string &key, int val, PluginAction **pluginAction) override; + int set(const std::string &key, double val, PluginAction **pluginAction) override; + int set(const std::string &key, bool val, PluginAction **pluginAction) override; + int set(const std::string &key, const std::string &val, PluginAction **pluginAction) override; + int undo(PluginAction *pluginAction) override; private: BtFactory btFactory; }; @@ -56,62 +55,79 @@ BtPlugin::~BtPlugin() { } -int BtPlugin::set(const std::string &key, int val, int *oldVal) +int BtPlugin::set(const std::string &key, int val, PluginAction **pluginAction) { BtAction *action = btFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %d]", key.c_str(), val); - int btRet = action->set(val, oldVal); - btFactory.destroyAction(action); - return btRet; + int ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + btFactory.destroyAction(action); + + return ret; } -int BtPlugin::set(const std::string &key, double val, double *oldVal) +int BtPlugin::set(const std::string &key, double val, PluginAction **pluginAction) { BtAction *action = btFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %f]", key.c_str(), val); - int btRet = action->set(val, oldVal); - btFactory.destroyAction(action); - return btRet; + int ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + btFactory.destroyAction(action); + + return ret; } -int BtPlugin::set(const std::string &key, bool val, bool *oldVal) +int BtPlugin::set(const std::string &key, bool val, PluginAction **pluginAction) { BtAction *action = btFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %s]", key.c_str(), (val ? "on" : "off")); - int btRet = action->set(val, oldVal); - btFactory.destroyAction(action); - return btRet; + int ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + btFactory.destroyAction(action); + + return ret; } -int BtPlugin::set(const std::string &key, const std::string &val, std::string *oldVal) +int BtPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) { BtAction *action = btFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %s]", key.c_str(), val.c_str()); - int btRet = action->set(val, oldVal); - btFactory.destroyAction(action); - return btRet; + int ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + btFactory.destroyAction(action); + + return ret; } -int BtPlugin::undo(const std::string &key, bool val) +int BtPlugin::undo(PluginAction *pluginAction) { - BtAction *action = btFactory.createAction(key); - RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); + BtAction *action = static_cast(pluginAction); + RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); - DBG("set [%s, %s]", key.c_str(), (val ? "on" : "off")); + DBG("Action(%s) undo", action->getName().c_str()); - int appRet = action->undo(val); + int ret = action->undo(); btFactory.destroyAction(action); - return appRet; + + return ret; } diff --git a/pkg/PkgAction.cpp b/pkg/PkgAction.cpp deleted file mode 100644 index ccc03c5..0000000 --- a/pkg/PkgAction.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 "PkgAction.h" -#include - -MODES_NAMESPACE_USE; - -PkgAction::PkgAction() -{ -} - -PkgAction::~PkgAction() -{ -} - -int PkgAction::set(int val) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -int PkgAction::undo(int val) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -int PkgAction::get(int *val) -{ - return MODES_ERROR_NOT_SUPPORTED; -} - -void PkgAction::setName(std::string name) -{ - this->name = name; -} - -std::string PkgAction::getName() -{ - return name; -} diff --git a/pkg/PkgAction.h b/pkg/PkgAction.h index 8f2fc0e..0a014e4 100644 --- a/pkg/PkgAction.h +++ b/pkg/PkgAction.h @@ -16,25 +16,24 @@ #pragma once #include +#include #include "plugin-def.h" MODES_NAMESPACE_BEGIN -class PkgAction { +class PkgAction : public PluginAction { public: - PkgAction(); - virtual ~PkgAction(); - - std::string getName(); - virtual int set(int val); - virtual int get(int *val); - virtual int undo(int val); - -protected: - void setName(std::string name); - -private: - std::string name; + PkgAction() = default; + virtual ~PkgAction() = default; + + virtual int set(int val) + { + return MODES_ERROR_NOT_SUPPORTED; + } + virtual int get(int *val) + { + return MODES_ERROR_NOT_SUPPORTED; + } }; MODES_NAMESPACE_END diff --git a/pkg/PkgEnableSupportMode.cpp b/pkg/PkgEnableSupportMode.cpp index 9a98ecb..dd3808a 100644 --- a/pkg/PkgEnableSupportMode.cpp +++ b/pkg/PkgEnableSupportMode.cpp @@ -109,7 +109,7 @@ int PkgStartSupportMode::get(int *val) } -int PkgStartSupportMode::undo(int val) +int PkgStartSupportMode::undo() { //If the appList is empty, it will be ignored at subroutines. diff --git a/pkg/PkgEnableSupportMode.h b/pkg/PkgEnableSupportMode.h index b9d4f5c..3f51a2c 100644 --- a/pkg/PkgEnableSupportMode.h +++ b/pkg/PkgEnableSupportMode.h @@ -18,6 +18,7 @@ #include #include #include +#include "plugin-def.h" #include "PkgAction.h" MODES_NAMESPACE_BEGIN @@ -26,9 +27,9 @@ class PkgStartSupportMode : public PkgAction { public: PkgStartSupportMode(); - virtual int set(int val) override; - virtual int get(int *val) override; - virtual int undo(int val) override; + virtual int set(int val); + virtual int get(int *val); + virtual int undo() override; static const std::string NAME; private: diff --git a/pkg/PkgFactory.cpp b/pkg/PkgFactory.cpp index 520f833..ff8def4 100644 --- a/pkg/PkgFactory.cpp +++ b/pkg/PkgFactory.cpp @@ -19,39 +19,12 @@ MODES_NAMESPACE_USE; -std::map PkgFactory::savedActionMap; - PkgFactory::PkgFactory() { actionMap[PkgStartSupportMode::NAME] = PKG_ACT_STARTSUPPORTMODE; } -PkgAction* PkgFactory::newStartSupportMode(const std::string &key, bool isUndo) -{ - auto it = savedActionMap.find(key); - if (it != savedActionMap.end()) { - if (isUndo) { - DBG("savedAction(%s) exist", key.c_str()); - PkgAction *action = it->second; - savedActionMap.erase(it); - return action; - } else { - ERR("savedAction(%s) exist", key.c_str()); - return nullptr; - } - } else { - if (isUndo) { - ERR("No savedAction(%s)", key.c_str()); - return nullptr; - } else { - PkgAction *action = new PkgStartSupportMode(); - savedActionMap[key] = action; - return action; - } - } -} - -PkgAction* PkgFactory::createAction(const std::string &key, bool isUndo) +PkgAction* PkgFactory::createAction(const std::string &key) { auto search = actionMap.find(key); if (search == actionMap.end()) { @@ -62,7 +35,7 @@ PkgAction* PkgFactory::createAction(const std::string &key, bool isUndo) PkgAction *action; switch (search->second) { case PKG_ACT_STARTSUPPORTMODE: - action = newStartSupportMode(key, isUndo); + action = new PkgStartSupportMode(); break; default: action = nullptr; @@ -74,13 +47,5 @@ PkgAction* PkgFactory::createAction(const std::string &key, bool isUndo) void PkgFactory::destroyAction(PkgAction *action) { - auto it = savedActionMap.find(action->getName()); - if (it != savedActionMap.end()) { - if (action == it->second) - DBG("savedAction(%s) exist", action->getName().c_str()); - else - ERR("Should not reach"); - } else { - delete action; - } + delete action; } diff --git a/pkg/PkgFactory.h b/pkg/PkgFactory.h index fb251b0..872533f 100644 --- a/pkg/PkgFactory.h +++ b/pkg/PkgFactory.h @@ -26,17 +26,14 @@ public: PkgFactory(); ~PkgFactory() = default; - PkgAction* createAction(const std::string &key, bool isUndo); + PkgAction* createAction(const std::string &key); void destroyAction(PkgAction *action); private: enum actionKey{ PKG_ACT_STARTSUPPORTMODE }; - PkgAction* newStartSupportMode(const std::string &key, bool isUndo); - std::map actionMap; - static std::map savedActionMap; }; MODES_NAMESPACE_END diff --git a/pkg/PkgPlugin.cpp b/pkg/PkgPlugin.cpp index 0348319..bd45830 100644 --- a/pkg/PkgPlugin.cpp +++ b/pkg/PkgPlugin.cpp @@ -17,7 +17,9 @@ #include #include #include "plugin-log.h" +#include "plugin-def.h" #include "PkgFactory.h" +#include "PkgAction.h" MODES_NAMESPACE_USE; @@ -26,8 +28,8 @@ public: PkgPlugin(); ~PkgPlugin(); - int set(const std::string &key, int val, int *oldVal) override; - int undo(const std::string &key, int val) override; + int set(const std::string &key, int val, PluginAction **pluginAction) override; + int undo(PluginAction *pluginAction) override; private: PkgFactory pkgFactory; @@ -52,29 +54,29 @@ PkgPlugin::~PkgPlugin() { } -int PkgPlugin::set(const std::string &key, int val, int *oldVal) +int PkgPlugin::set(const std::string &key, int val, PluginAction **pluginAction) { - PkgAction *action = pkgFactory.createAction(key, false); + PkgAction *action = pkgFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); - DBG("set [%s, %d]", key.c_str(), val); + DBG("Action(%s) set(%d)", key.c_str(), val); int ret = action->set(val); - if (oldVal) - action->get(oldVal); - - pkgFactory.destroyAction(action); + if (pluginAction) + *pluginAction = action; + else + pkgFactory.destroyAction(action); return ret; } -int PkgPlugin::undo(const std::string &key, int val) +int PkgPlugin::undo(PluginAction *pluginAction) { - PkgAction *action = pkgFactory.createAction(key, true); - RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); + PkgAction *action = static_cast(pluginAction); + RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); - DBG("set [%s, %d]", key.c_str(), val); + DBG("Action(%s) undo", action->getName().c_str()); - int ret = action->undo(val); + int ret = action->undo(); pkgFactory.destroyAction(action); return ret; diff --git a/unittests/mdsp_test_app.cpp b/unittests/mdsp_test_app.cpp index cb23059..e5f750d 100644 --- a/unittests/mdsp_test_app.cpp +++ b/unittests/mdsp_test_app.cpp @@ -32,25 +32,28 @@ protected: void SetUp() override { loop = g_main_loop_new(NULL, FALSE); + plugin = objectCreate(); } void TearDown() override { g_main_loop_unref(loop); loop = NULL; + objectDelete(plugin); + plugin = NULL; } static gboolean appPluginUndoConflictIdler(gpointer data) { - app_context_h runAppContext; - Plugin *plugin = objectCreate(); - result = plugin->set("launch", std::string("org.tizen.w-stopwatch"), nullptr); + PluginAction *action; + result = plugin->set("launch", std::string("org.tizen.w-stopwatch"), &action); EXPECT_EQ(MODES_ERROR_NONE, result); + + app_context_h runAppContext; app_manager_get_app_context("org.tizen.w-stopwatch", &runAppContext); app_manager_terminate_app(runAppContext); - result = plugin->undo("launch", std::string("org.tizen.w-stopwatch")); + result = plugin->undo(action); EXPECT_EQ(MODES_ERROR_NONE, result); - objectDelete(plugin); g_main_loop_quit(loop); return G_SOURCE_REMOVE; @@ -58,8 +61,8 @@ protected: static gboolean appPluginSetUndoTimeout(gpointer data) { - Plugin *plugin = (Plugin *)data; - result = plugin->undo("launch", std::string("org.tizen.w-stopwatch")); + PluginAction *action = (PluginAction*)data; + result = plugin->undo(action); EXPECT_EQ(MODES_ERROR_NONE, result); g_main_loop_quit(loop); return false; @@ -67,28 +70,28 @@ protected: static gboolean appPluginSetUndoIdler(gpointer data) { - Plugin *plugin = (Plugin *)data; - result = plugin->set("launch", std::string("org.tizen.w-stopwatch"), nullptr); + PluginAction *action; + result = plugin->set("launch", std::string("org.tizen.w-stopwatch"), &action); EXPECT_EQ(MODES_ERROR_NONE, result); - g_timeout_add(1000, appPluginSetUndoTimeout, plugin); + g_timeout_add(1000, appPluginSetUndoTimeout, action); return G_SOURCE_REMOVE; } static int result; static GMainLoop *loop; static GThread *my_thread; + static Plugin *plugin; }; int PluginTest::result = 0; +Plugin *PluginTest::plugin = NULL; GMainLoop *PluginTest::loop = NULL; TEST_F(PluginTest, setUndoPluginApp) { - Plugin *plugin = objectCreate(); g_idle_add(appPluginSetUndoIdler, plugin); g_main_loop_run(loop); - objectDelete(plugin); } TEST_F(PluginTest, undoConflictPluginApp) @@ -97,7 +100,6 @@ TEST_F(PluginTest, undoConflictPluginApp) g_main_loop_run(loop); } - int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff --git a/unittests/mdsp_test_pkg.cpp b/unittests/mdsp_test_pkg.cpp index 014f109..054ddb4 100644 --- a/unittests/mdsp_test_pkg.cpp +++ b/unittests/mdsp_test_pkg.cpp @@ -41,23 +41,9 @@ protected: plugin = NULL; } - static gboolean pkgPluginTimeout(gpointer data) - { - Plugin *plugin = (Plugin *)data; - result = plugin->undo("startSupportMode", 1); - EXPECT_EQ(MODES_ERROR_NONE, result); - g_main_loop_quit(loop); - - return G_SOURCE_REMOVE; - } - static gboolean pkgPluginIdler(gpointer data) { - Plugin *plugin = (Plugin *)data; - result = plugin->set("startSupportMode", 1, nullptr); - EXPECT_EQ(MODES_ERROR_NONE, result); - - g_timeout_add_seconds(5, pkgPluginTimeout, plugin); + g_main_loop_quit(loop); return G_SOURCE_REMOVE; } @@ -66,11 +52,20 @@ protected: Plugin *plugin; }; -int PkgPluginTest::result = 0; GMainLoop *PkgPluginTest::loop = NULL; TEST_F(PkgPluginTest, setUndoTest) { + PluginAction *action; + int ret = plugin->set("startSupportMode", 1, &action); + EXPECT_EQ(MODES_ERROR_NONE, ret); + + g_idle_add(pkgPluginIdler, plugin); + g_main_loop_run(loop); + + ret = plugin->undo(action); + EXPECT_EQ(MODES_ERROR_NONE, ret); + g_idle_add(pkgPluginIdler, plugin); g_main_loop_run(loop); } diff --git a/unittests/mdsp_test_vconf.cpp b/unittests/mdsp_test_vconf.cpp index a7af18a..9729dff 100644 --- a/unittests/mdsp_test_vconf.cpp +++ b/unittests/mdsp_test_vconf.cpp @@ -39,7 +39,15 @@ protected: objectDelete(plugin); g_main_loop_unref(loop); loop = NULL; - result = -1; + cb1Called = false; + cb2Called = false; + } + + static void shoudNotBeCalled(const std::string &key, void *userData) + { + ERR("This Callback(%s) should not be called", key.c_str()); + g_main_loop_quit(loop); + GTEST_FAIL(); } static void valChangedCb(const std::string &key, void *userData) @@ -48,15 +56,16 @@ protected: g_main_loop_quit(loop); } - static void valChangedResetStoredCb(const std::string &key, void *userData) + static void callback1(const std::string &key, void *userData) { DBG("%s changed callback called!", key.c_str()); - result = MODES_ERROR_NONE; + cb1Called = true; } - static void valChangedResetCb(const std::string &key, void *userData) + static void callback2(const std::string &key, void *userData) { DBG("%s changed callback called!", key.c_str()); + cb2Called = true; g_main_loop_quit(loop); } @@ -67,101 +76,146 @@ protected: return G_SOURCE_REMOVE; } - static int result; static GMainLoop *loop; static Plugin *plugin; - static int oldval; + static bool cb1Called; + static bool cb2Called; }; -int VconfPluginTest::oldval = 0; -int VconfPluginTest::result = -1; +bool VconfPluginTest::cb1Called = false; +bool VconfPluginTest::cb2Called = false; Plugin *VconfPluginTest::plugin = nullptr; GMainLoop *VconfPluginTest::loop = NULL; -TEST_F(VconfPluginTest, setPluginVconfInt) +TEST_F(VconfPluginTest, VconfInt) { int ret; - int oldval; + const char *key = "db.setting.psmode"; + + ret = plugin->set(key, 0, nullptr); + EXPECT_EQ(ret, MODES_ERROR_NONE); + + int val = plugin->getInt(key); + EXPECT_EQ(val, 0); - ret = plugin->set("db.setting.psmode", 3, nullptr); + PluginAction *action; + ret = plugin->set(key, 1, &action); EXPECT_EQ(ret, MODES_ERROR_NONE); - ret = plugin->set("db.setting.psmode", 1, &oldval); + + ret = plugin->undo(action); EXPECT_EQ(ret, MODES_ERROR_NONE); - EXPECT_EQ(oldval, 3); + + val = plugin->getInt(key); + EXPECT_EQ(val, 0); } -TEST_F(VconfPluginTest, setPluginVconfDouble) +TEST_F(VconfPluginTest, VconfDouble) { int ret; - double oldval; + const char *key = "db.system.timechange_external"; - ret = plugin->set("db.system.timechange_external", 1.0, nullptr); + ret = plugin->set(key, 0.0, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - ret = plugin->set("db.system.timechange_external", 0.0, &oldval); + + 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); - EXPECT_EQ(oldval, 1.0); + + ret = plugin->undo(action); + EXPECT_EQ(ret, MODES_ERROR_NONE); + + val = plugin->getDouble(key); + EXPECT_EQ(val, 0.0); } -TEST_F(VconfPluginTest, setPluginVconfBool) +TEST_F(VconfPluginTest, VconfBool) { int ret; - bool oldval; + const char *key = "db.setting.sound.button_sounds"; - ret = plugin->set("db.setting.sound.button_sounds", false, nullptr); + ret = plugin->set(key, true, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - ret = plugin->set("db.setting.sound.button_sounds", true, &oldval); + + double val = plugin->getBool(key); + EXPECT_EQ(val, true); + + PluginAction *action; + ret = plugin->set(key, false, &action); + EXPECT_EQ(ret, MODES_ERROR_NONE); + + ret = plugin->undo(action); EXPECT_EQ(ret, MODES_ERROR_NONE); - EXPECT_EQ(oldval, false); + + val = plugin->getBool(key); + EXPECT_EQ(val, true); } -TEST_F(VconfPluginTest, setPluginVconfStr) +TEST_F(VconfPluginTest, VconfStr) { int ret; - std::string oldval; + const char *key = "db.setting.device_name"; + const std::string devName = "Tizen"; + const std::string tmpName = "ModesTest"; - std::string testVal = "org.tizen.menu-screen.test"; - ret = plugin->set("db.setting.menuscreen.package_name", testVal, nullptr); + ret = plugin->set(key, devName, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - ret = plugin->set("db.setting.menuscreen.package_name", "org.tizen.menu-screen", &oldval); + + 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); - EXPECT_EQ(oldval.compare(testVal), 0); + + ret = plugin->undo(action); + EXPECT_EQ(ret, MODES_ERROR_NONE); + + val = plugin->getString(key); + EXPECT_STREQ(val.c_str(), devName.c_str()); } TEST_F(VconfPluginTest, callbackPluginVconf) { - vconf_get_int("db/setting/psmode", &oldval); - int ret = plugin->setChangedCallback(valChangedCb, - "db.setting.psmode", nullptr); + int oldVal = 0; + vconf_get_int("db/setting/psmode", &oldVal); + int ret = plugin->setChangedCallback(valChangedCb, "db.setting.psmode", nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); g_idle_add(changedCallbackidler, nullptr); g_main_loop_run(loop); ret = plugin->unSetChangedCallback(valChangedCb, "db.setting.psmode", nullptr); EXPECT_EQ(ret, MODES_ERROR_NO_DATA); - vconf_set_int("db/setting/psmode", oldval); + + ret = plugin->setChangedCallback(shoudNotBeCalled, "db.setting.psmode", nullptr); + EXPECT_EQ(ret, MODES_ERROR_NONE); + ret = plugin->unSetChangedCallback(shoudNotBeCalled, "db.setting.psmode", nullptr); + 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, &oldval); + ret = plugin->set("db.setting.psmode", 4, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - ret = plugin->setChangedCallback(valChangedResetStoredCb, "db.setting.psmode", nullptr); + ret = plugin->setChangedCallback(callback1, "db.setting.psmode", nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); ret = plugin->set("db.setting.psmode", 2, nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); - EXPECT_EQ(MODES_ERROR_NONE, result); - ret = plugin->setChangedCallback(valChangedResetCb, "db.setting.psmode", nullptr); + EXPECT_TRUE(cb1Called); + ret = plugin->setChangedCallback(callback2, "db.setting.psmode", nullptr); EXPECT_EQ(ret, MODES_ERROR_NONE); g_idle_add(changedCallbackidler, nullptr); g_main_loop_run(loop); + EXPECT_TRUE(cb2Called); - DBG("loop end and unregister callback start"); - ret = plugin->unSetChangedCallback(valChangedResetCb, "db.setting.psmode", nullptr); + ret = plugin->unSetChangedCallback(callback2, "db.setting.psmode", nullptr); EXPECT_EQ(ret, MODES_ERROR_NO_DATA); - vconf_set_int("db/setting/psmode", oldval); - EXPECT_EQ(MODES_ERROR_NONE, result); + vconf_set_int("db/setting/psmode", 0); } int main(int argc, char **argv) { diff --git a/vconf/VconfActionBool.cpp b/vconf/VconfActionBool.cpp new file mode 100644 index 0000000..27b9ede --- /dev/null +++ b/vconf/VconfActionBool.cpp @@ -0,0 +1,74 @@ +/* + * 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 "VconfActionBool.h" +#include +#include +#include +#include "plugin-log.h" + +MODES_NAMESPACE_USE; + +VconfActionBool::VconfActionBool(const std::string &vconfKey) + :key(vconfKey), oldVal(false) +{ +} + +int VconfActionBool::set(bool val) +{ + int ret; + DBG("setBool(%s, %d)", key.c_str(), val); + + int prev; + ret = vconf_get_bool(key.c_str(), &prev); + if (0 != ret) { + ERR("vconf_get_bool(%s) Fail(%d)", key.c_str(), ret); + return MODES_ERROR_SYSTEM; + } + 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; + } + + return value ? true : false; +} + +int VconfActionBool::undo() +{ + int ret; + DBG("undoBool(%s, %d)", key.c_str(), oldVal); + + 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); + return MODES_ERROR_SYSTEM; + } + + return MODES_ERROR_NONE; +} diff --git a/vconf/VconfActionBool.h b/vconf/VconfActionBool.h new file mode 100644 index 0000000..99ad60d --- /dev/null +++ b/vconf/VconfActionBool.h @@ -0,0 +1,38 @@ +/* + * 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 +#include +#include "plugin-def.h" + +MODES_NAMESPACE_BEGIN + +class VconfActionBool : public PluginAction { +public: + VconfActionBool(const std::string &vconfKey); + virtual ~VconfActionBool() = default; + + int set(bool val); + bool get(); + virtual int undo() override; +private: + std::string key; + bool oldVal; +}; + +MODES_NAMESPACE_END + diff --git a/vconf/VconfActionDbl.cpp b/vconf/VconfActionDbl.cpp new file mode 100644 index 0000000..8c3bebe --- /dev/null +++ b/vconf/VconfActionDbl.cpp @@ -0,0 +1,72 @@ +/* + * 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 "VconfActionDbl.h" +#include +#include +#include +#include "plugin-log.h" + +MODES_NAMESPACE_USE; + +VconfActionDbl::VconfActionDbl(const std::string &vconfKey) + :key(vconfKey), oldVal(0.0) +{ +} + +int VconfActionDbl::set(double val) +{ + int ret; + DBG("setDbl(%s, %f)", key.c_str(), val); + + ret = vconf_get_dbl(key.c_str(), &oldVal); + if (0 != ret) { + ERR("vconf_get_dbl(%s) Fail(%d)", key.c_str(), ret); + 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; + } + + return value; +} + +int VconfActionDbl::undo() +{ + int ret; + DBG("undoDbl(%s, %f)", key.c_str(), oldVal); + + ret = vconf_set_dbl(key.c_str(), oldVal); + if (0 != ret) { + ERR("vconf_set_dbl(%s, %f) Fail(%d)", key.c_str(), oldVal, ret); + return MODES_ERROR_SYSTEM; + } + + return MODES_ERROR_NONE; +} diff --git a/vconf/VconfActionDbl.h b/vconf/VconfActionDbl.h new file mode 100644 index 0000000..35a7c39 --- /dev/null +++ b/vconf/VconfActionDbl.h @@ -0,0 +1,38 @@ +/* + * 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 +#include +#include "plugin-def.h" + +MODES_NAMESPACE_BEGIN + +class VconfActionDbl : public PluginAction { +public: + VconfActionDbl(const std::string &vconfKey); + virtual ~VconfActionDbl() = default; + + int set(double val); + double get(); + virtual int undo() override; +private: + std::string key; + double oldVal; +}; + +MODES_NAMESPACE_END + diff --git a/vconf/VconfActionInt.cpp b/vconf/VconfActionInt.cpp new file mode 100644 index 0000000..39ecfda --- /dev/null +++ b/vconf/VconfActionInt.cpp @@ -0,0 +1,72 @@ +/* + * 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 "VconfActionInt.h" +#include +#include +#include +#include "plugin-log.h" + +MODES_NAMESPACE_USE; + +VconfActionInt::VconfActionInt(const std::string &vconfKey) + :key(vconfKey), oldVal(0) +{ +} + +int VconfActionInt::set(int val) +{ + int ret; + DBG("setInt(%s, %d)", key.c_str(), val); + + ret = vconf_get_int(key.c_str(), &oldVal); + if (0 != ret) { + ERR("vconf_get_int(%s) Fail(%d)", key.c_str(), ret); + 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; + } + + return value; +} + +int VconfActionInt::undo() +{ + int ret; + DBG("undoInt(%s, %d)", key.c_str(), oldVal); + + ret = vconf_set_int(key.c_str(), oldVal); + if (0 != ret) { + ERR("vconf_set_int(%s, %d) Fail(%d)", key.c_str(), oldVal, ret); + return MODES_ERROR_SYSTEM; + } + + return MODES_ERROR_NONE; +} diff --git a/vconf/VconfActionInt.h b/vconf/VconfActionInt.h new file mode 100644 index 0000000..94df5eb --- /dev/null +++ b/vconf/VconfActionInt.h @@ -0,0 +1,38 @@ +/* + * 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 +#include +#include "plugin-def.h" + +MODES_NAMESPACE_BEGIN + +class VconfActionInt : public PluginAction { +public: + VconfActionInt(const std::string &vconfKey); + virtual ~VconfActionInt() = default; + + int set(int val); + int get(); + virtual int undo() override; +private: + std::string key; + int oldVal; +}; + +MODES_NAMESPACE_END + diff --git a/vconf/VconfActionStr.cpp b/vconf/VconfActionStr.cpp new file mode 100644 index 0000000..bfb2319 --- /dev/null +++ b/vconf/VconfActionStr.cpp @@ -0,0 +1,76 @@ +/* + * 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 "VconfActionStr.h" +#include +#include +#include +#include "plugin-log.h" + +MODES_NAMESPACE_USE; + +VconfActionStr::VconfActionStr(const std::string &vconfKey) + :key(vconfKey) +{ +} + +int VconfActionStr::set(const std::string &val) +{ + int ret; + DBG("setStr(%s, %s)", key.c_str(), val.c_str()); + + char *prev = vconf_get_str(key.c_str()); + if (NULL == prev) { + ERR("vconf_get_str(%s) Fail()", key.c_str()); + return MODES_ERROR_SYSTEM; + } + 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; + } + 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; +} + +int VconfActionStr::undo() +{ + int ret; + DBG("undoStr(%s, %s)", key.c_str(), oldVal.c_str()); + + 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); + return MODES_ERROR_SYSTEM; + } + + return MODES_ERROR_NONE; +} diff --git a/vconf/VconfActionStr.h b/vconf/VconfActionStr.h new file mode 100644 index 0000000..7be19af --- /dev/null +++ b/vconf/VconfActionStr.h @@ -0,0 +1,38 @@ +/* + * 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 +#include +#include "plugin-def.h" + +MODES_NAMESPACE_BEGIN + +class VconfActionStr : public PluginAction { +public: + VconfActionStr(const std::string &vconfKey); + ~VconfActionStr() = default; + + int set(const std::string &val); + std::string get(); + virtual int undo() override; +private: + std::string key; + std::string oldVal; +}; + +MODES_NAMESPACE_END + diff --git a/vconf/VconfPlugin.cpp b/vconf/VconfPlugin.cpp index 76d67c7..2bf74c4 100644 --- a/vconf/VconfPlugin.cpp +++ b/vconf/VconfPlugin.cpp @@ -21,7 +21,10 @@ #include #include "plugin-log.h" #include "plugin-def.h" - +#include "VconfActionInt.h" +#include "VconfActionDbl.h" +#include "VconfActionBool.h" +#include "VconfActionStr.h" MODES_NAMESPACE_USE; @@ -29,10 +32,10 @@ class VconfPlugin : public Plugin { public: VconfPlugin(); - int set(const std::string &key, int val, int *oldVal) override; - int set(const std::string &key, double val, double *oldVal) override; - int set(const std::string &key, bool val, bool *oldVal) override; - int set(const std::string &key, const std::string &val, std::string *oldVal) override; + int set(const std::string &key, int val, PluginAction **pluginAction) override; + int set(const std::string &key, double val, PluginAction **pluginAction) override; + int set(const std::string &key, bool val, PluginAction **pluginAction) override; + int set(const std::string &key, const std::string &val, PluginAction **pluginAction) override; int getInt(const std::string &key) override; double getDouble(const std::string &key) override; @@ -70,7 +73,7 @@ VconfPlugin::VconfPlugin() setName("vconf"); } -int VconfPlugin::set(const std::string &key, int val, int *oldVal) +int VconfPlugin::set(const std::string &key, int val, PluginAction **pluginAction) { int ret; DBG("set(%s, %d)", key.c_str(), val); @@ -78,29 +81,22 @@ int VconfPlugin::set(const std::string &key, int val, int *oldVal) std::string newKey(key); std::replace(newKey.begin(), newKey.end(), '.', '/'); - if (oldVal) { - ret = vconf_get_int(newKey.c_str(), oldVal); - if (0 != ret) { - ERR("vconf_get_int(%s) Fail(%d)", newKey.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - } - ret = handleChange(newKey); if (MODES_ERROR_NONE != ret) { ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret); return ret; } - ret = vconf_set_int(newKey.c_str(), val); - if (0 != ret) { - ERR("vconf_set_int(%s, %d) Fail(%d)", newKey.c_str(), val, ret); - return MODES_ERROR_SYSTEM; - } - return MODES_ERROR_NONE; + VconfActionInt *action = new VconfActionInt(newKey); + ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + delete action; + return ret; } -int VconfPlugin::set(const std::string &key, double val, double *oldVal) +int VconfPlugin::set(const std::string &key, double val, PluginAction **pluginAction) { int ret; DBG("set(%s, %lf)", key.c_str(), val); @@ -108,29 +104,22 @@ int VconfPlugin::set(const std::string &key, double val, double *oldVal) std::string newKey(key); std::replace(newKey.begin(), newKey.end(), '.', '/'); - if (oldVal) { - ret = vconf_get_dbl(newKey.c_str(), oldVal); - if (0 != ret) { - ERR("vconf_get_dbl(%s) Fail(%d)", newKey.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - } - ret = handleChange(newKey); if (MODES_ERROR_NONE != ret) { ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret); return ret; } - ret = vconf_set_dbl(newKey.c_str(), val); - if (0 != ret) { - ERR("vconf_set_int(%s, %lf) Fail(%d)", newKey.c_str(), val, ret); - return MODES_ERROR_SYSTEM; - } - return MODES_ERROR_NONE; + VconfActionDbl *action = new VconfActionDbl(newKey); + ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + delete action; + return ret; } -int VconfPlugin::set(const std::string &key, bool val, bool *oldVal) +int VconfPlugin::set(const std::string &key, bool val, PluginAction **pluginAction) { int ret; DBG("set(%s, %d)", key.c_str(), val); @@ -138,60 +127,41 @@ int VconfPlugin::set(const std::string &key, bool val, bool *oldVal) std::string newKey(key); std::replace(newKey.begin(), newKey.end(), '.', '/'); - if (oldVal) { - int prev; - ret = vconf_get_bool(newKey.c_str(), &prev); - if (0 != ret) { - ERR("vconf_get_bool(%s) Fail(%d)", newKey.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - *oldVal = prev; - } - ret = handleChange(newKey); if (MODES_ERROR_NONE != ret) { ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret); return ret; } - ret = vconf_set_bool(newKey.c_str(), val); - if (0 != ret) { - ERR("vconf_set_bool(%s, %d) Fail(%d)", newKey.c_str(), val, ret); - return MODES_ERROR_SYSTEM; - } - return MODES_ERROR_NONE; + VconfActionBool *action = new VconfActionBool(newKey); + ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + delete action; + return ret; } -int VconfPlugin::set(const std::string &key, const std::string &val, std::string *oldVal) +int VconfPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) { DBG("set(%s, %s)", key.c_str(), val.c_str()); std::string newKey(key); std::replace(newKey.begin(), newKey.end(), '.', '/'); - if (oldVal) { - char *prevStr = vconf_get_str(newKey.c_str()); - if (NULL == prevStr) { - ERR("vconf_get_str(%s) Fail()", newKey.c_str()); - return MODES_ERROR_SYSTEM; - } - *oldVal = std::string(prevStr); - free(prevStr); - } - int ret = handleChange(newKey); if (MODES_ERROR_NONE != ret) { ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret); return ret; } - ret = vconf_set_str(newKey.c_str(), val.c_str()); - if (0 != ret) { - ERR("vconf_set_str(%s, %s) Fail(%d)", newKey.c_str(), val.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - - return MODES_ERROR_NONE; + VconfActionStr *action = new VconfActionStr(newKey); + ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + delete action; + return ret; } int VconfPlugin::getInt(const std::string &key) @@ -201,13 +171,9 @@ int VconfPlugin::getInt(const std::string &key) std::string newKey(key); std::replace(newKey.begin(), newKey.end(), '.', '/'); - int value = 0; - int ret = vconf_get_int(newKey.c_str(), &value); - if (0 != ret) { - ERR("vconf_get_int(%s) Fail(%d)", newKey.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - return value; + VconfActionInt action(newKey); + + return action.get(); } double VconfPlugin::getDouble(const std::string &key) @@ -217,13 +183,9 @@ double VconfPlugin::getDouble(const std::string &key) std::string newKey(key); std::replace(newKey.begin(), newKey.end(), '.', '/'); - double value = 0.0; - int ret = vconf_get_dbl(newKey.c_str(), &value); - if (0 != ret) { - ERR("vconf_get_int(%s) Fail(%d)", newKey.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - return value; + VconfActionDbl action(newKey); + + return action.get(); } bool VconfPlugin::getBool(const std::string &key) @@ -233,13 +195,9 @@ bool VconfPlugin::getBool(const std::string &key) std::string newKey(key); std::replace(newKey.begin(), newKey.end(), '.', '/'); - int value = 0; - int ret = vconf_get_bool(newKey.c_str(), &value); - if (0 != ret) { - ERR("vconf_get_bool(%s) Fail(%d)", newKey.c_str(), ret); - return MODES_ERROR_SYSTEM; - } - return (value) ? true : false; + VconfActionBool action(newKey); + + return action.get(); } std::string VconfPlugin::getString(const std::string &key) @@ -249,16 +207,9 @@ std::string VconfPlugin::getString(const std::string &key) std::string newKey(key); std::replace(newKey.begin(), newKey.end(), '.', '/'); - char *value = NULL; - value = vconf_get_str(newKey.c_str()); - if (NULL == value) { - ERR("vconf_get_str(%s) Fail()", newKey.c_str()); - return std::string(); - } - std::string retStr = value; - free(value); + VconfActionStr action(newKey); - return retStr; + return action.get(); } int VconfPlugin::setChangedCallback(valueChangedCb callback, const std::string &key, void *userData) diff --git a/wifi/WifiAction.cpp b/wifi/WifiAction.cpp index dcac333..bc726e1 100644 --- a/wifi/WifiAction.cpp +++ b/wifi/WifiAction.cpp @@ -97,13 +97,3 @@ const std::string& WifiAction::strErr(int err) { return wifiErrorMessage.get(err); } - -void WifiAction::setName(std::string name) -{ - this->name = name; -} - -std::string WifiAction::getName() -{ - return name; -} diff --git a/wifi/WifiAction.h b/wifi/WifiAction.h index 4f0ef9a..c179493 100644 --- a/wifi/WifiAction.h +++ b/wifi/WifiAction.h @@ -17,17 +17,17 @@ #include #include +#include #include "WifiErrorMessage.h" MODES_NAMESPACE_BEGIN -class WifiAction { +class WifiAction : public PluginAction { public: WifiAction(); virtual ~WifiAction(); bool isWifiActivate(); - std::string getName(); virtual int set(int val); virtual int set(double val); virtual int set(bool val); @@ -39,11 +39,8 @@ public: protected: bool isWifiManagerEnable(); const std::string& strErr(int err); - void setName(std::string name); wifi_manager_h wifiManagerHandle; WifiErrorMessage wifiErrorMessage; -private: - std::string name; }; MODES_NAMESPACE_END diff --git a/wifi/WifiActionPower.cpp b/wifi/WifiActionPower.cpp index da4c059..40bbab4 100644 --- a/wifi/WifiActionPower.cpp +++ b/wifi/WifiActionPower.cpp @@ -22,6 +22,7 @@ MODES_NAMESPACE_USE; WifiActionPower::WifiActionPower() + :oldVal(false) { setName("power"); } @@ -46,7 +47,8 @@ int WifiActionPower::set(bool val) { RETV_IF(!isWifiManagerEnable(), MODES_ERROR_NOT_SUPPORTED); - if (isWifiActivate() == val) { + oldVal = isWifiActivate(); + if (oldVal == val) { INFO("Already wifi is [%s]", val ? "On" : "Off"); return MODES_ERROR_NONE; } @@ -80,3 +82,30 @@ int WifiActionPower::get(bool *val) return MODES_ERROR_NONE; } + +int WifiActionPower::undo() +{ + RETV_IF(!isWifiManagerEnable(), MODES_ERROR_NOT_SUPPORTED); + + if (isWifiActivate() == oldVal) { + INFO("Already wifi is [%s]", oldVal ? "On" : "Off"); + return MODES_ERROR_NONE; + } + + if (oldVal) { + int wifiRet = wifi_manager_activate(wifiManagerHandle, activate_cb, this); + if (WIFI_MANAGER_ERROR_NONE != wifiRet) { + ERR("wifi_manager_activate() Fail(%d, %s)", wifiRet, strErr(wifiRet).c_str()); + return MODES_ERROR_SYSTEM; + } + } else { + int wifiRet = wifi_manager_deactivate(wifiManagerHandle, deactivate_cb, this); + if (WIFI_MANAGER_ERROR_NONE != wifiRet) { + ERR("wifi_manager_activate() Fail(%d, %s)", wifiRet, strErr(wifiRet).c_str()); + return MODES_ERROR_SYSTEM; + } + } + + INFO("Wifi power [%s]", oldVal ? "On" : "Off"); + return MODES_ERROR_NONE; +} diff --git a/wifi/WifiActionPower.h b/wifi/WifiActionPower.h index 9c76c12..056961c 100644 --- a/wifi/WifiActionPower.h +++ b/wifi/WifiActionPower.h @@ -26,9 +26,11 @@ public: int set(bool val) override; int get(bool *val) override; + int undo() override; private: static void activate_cb(wifi_manager_error_e result, void *user_data); static void deactivate_cb(wifi_manager_error_e result, void *user_data); + bool oldVal; }; MODES_NAMESPACE_END diff --git a/wifi/WifiFactory.cpp b/wifi/WifiFactory.cpp index 58c13b0..f72e16b 100644 --- a/wifi/WifiFactory.cpp +++ b/wifi/WifiFactory.cpp @@ -46,7 +46,6 @@ WifiAction* WifiFactory::createAction(const std::string &key) void WifiFactory::destroyAction(WifiAction *action) { - RET_IF(action == NULL); delete action; } diff --git a/wifi/WifiPlugin.cpp b/wifi/WifiPlugin.cpp index 39f0cfd..bf82dbc 100644 --- a/wifi/WifiPlugin.cpp +++ b/wifi/WifiPlugin.cpp @@ -23,16 +23,16 @@ MODES_NAMESPACE_USE; -class WifiOperation; class WifiPlugin : public Plugin { public: WifiPlugin(); ~WifiPlugin(); - int set(const std::string &key, int val, int *oldVal) override; - int set(const std::string &key, double val, double *oldVal) override; - int set(const std::string &key, bool val, bool *oldVal) override; - int set(const std::string &key, const std::string &val, std::string *oldVal) override; + int set(const std::string &key, int val, PluginAction **pluginAction) override; + int set(const std::string &key, double val, PluginAction **pluginAction) override; + int set(const std::string &key, bool val, PluginAction **pluginAction) override; + int set(const std::string &key, const std::string &val, PluginAction **pluginAction) override; + int undo(PluginAction *pluginAction) override; private: WifiFactory wifiFactory; }; @@ -56,64 +56,80 @@ WifiPlugin::~WifiPlugin() { } -int WifiPlugin::set(const std::string &key, int val, int *oldVal) +int WifiPlugin::set(const std::string &key, int val, PluginAction **pluginAction) { WifiAction *action = wifiFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %d]", key.c_str(), val); - if (oldVal) - ERR("Not Supported"); - int ret = action->set(val); - wifiFactory.destroyAction(action); + if (pluginAction) + *pluginAction = action; + else + wifiFactory.destroyAction(action); + return ret; } -int WifiPlugin::set(const std::string &key, double val, double *oldVal) +int WifiPlugin::set(const std::string &key, double val, PluginAction **pluginAction) { WifiAction *action = wifiFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %f]", key.c_str(), val); - if (oldVal) - ERR("Not Supported"); - int ret = action->set(val); - wifiFactory.destroyAction(action); + if (pluginAction) + *pluginAction = action; + else + wifiFactory.destroyAction(action); + return ret; } -int WifiPlugin::set(const std::string &key, bool val, bool *oldVal) +int WifiPlugin::set(const std::string &key, bool val, PluginAction **pluginAction) { WifiAction *action = wifiFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %s]", key.c_str(), (val ? "on" : "off")); - if (oldVal) - action->get(oldVal); int ret = action->set(val); - - wifiFactory.destroyAction(action); + if (pluginAction) + *pluginAction = action; + else + wifiFactory.destroyAction(action); return ret; } -int WifiPlugin::set(const std::string &key, const std::string &val, std::string *oldVal) +int WifiPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) { WifiAction *action = wifiFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("set [%s, %s]", key.c_str(), val.c_str()); - if (oldVal) - ERR("Not Supported"); - int ret = action->set(val); + if (pluginAction) + *pluginAction = action; + else + wifiFactory.destroyAction(action); + + return ret; +} + +int WifiPlugin::undo(PluginAction *pluginAction) +{ + WifiAction *action = static_cast(pluginAction); + RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); + + DBG("Action(%s) undo", action->getName().c_str()); + + int ret = action->undo(); wifiFactory.destroyAction(action); + return ret; }