From: Youngjae Shin Date: Thu, 24 Oct 2019 03:46:37 +0000 (+0900) Subject: apply action serialization X-Git-Tag: submit/tizen/20200406.072014~38 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1c1a65ac1b332d7417ac672fa0db1d6537ff2f94;p=platform%2Fcore%2Fsystem%2Fmodes-plugins.git apply action serialization --- diff --git a/app/AppAction.h b/app/AppAction.h index 613b6d3..d625245 100644 --- a/app/AppAction.h +++ b/app/AppAction.h @@ -23,7 +23,10 @@ MODES_NAMESPACE_BEGIN class AppAction : public PluginAction { public: - AppAction() = default; + AppAction(const std::string &name) + : PluginAction(name) + { + } virtual ~AppAction() = default; virtual int set(const std::string &val) diff --git a/app/AppActionLaunch.cpp b/app/AppActionLaunch.cpp index b8eddb8..0993e1a 100644 --- a/app/AppActionLaunch.cpp +++ b/app/AppActionLaunch.cpp @@ -26,30 +26,31 @@ MODES_NAMESPACE_USE; const std::string AppActionLaunch::NAME = "launch"; std::list AppActionLaunch::appidList; + +AppActionLaunch::AppActionLaunch() + : AppAction(NAME) +{ +} + void AppActionLaunch::appContextStatusCallback(app_context_h app_context, app_context_status_e status, void *user_data) { - char *appid = NULL; DBG("appContextStatusCallback(status:%d)", status); - if (app_context_get_app_id(app_context, &appid) != APP_MANAGER_ERROR_NONE) { - ERR("Failed to get appid\n"); + if (APP_CONTEXT_STATUS_TERMINATED != status) return; - } - DBG("app_context_cb(appid:%s)", appid); - if (APP_CONTEXT_STATUS_TERMINATED == status) { - DBG("APP_CONTEXT_STATUS_TERMINATED appid : %s\n", appid); - appidList.remove(appid); + char *appid = NULL; + int ret = app_context_get_app_id(app_context, &appid); + if (APP_MANAGER_ERROR_NONE != ret) { + ERR("app_context_get_app_id() Fail(%d)", ret); + return; } + appidList.remove(appid); + free(appid); return; } -AppActionLaunch::AppActionLaunch() -{ - setName(NAME); -} - int AppActionLaunch::set(const std::string &val) { DBG("id(%s)", val.c_str()); @@ -114,10 +115,8 @@ int AppActionLaunch::undo() } int ret = app_manager_unset_app_context_status_cb(AppActionLaunch::appContextStatusCallback, appID.c_str()); - if (APP_MANAGER_ERROR_NONE != ret) { + if (APP_MANAGER_ERROR_NONE != ret) ERR("app_manager_unset_app_context_status_cb() Fail(%s)", get_error_message(ret)); - return MODES_ERROR_SYSTEM; - } appidList.remove(appID); @@ -133,3 +132,14 @@ int AppActionLaunch::undo() } return MODES_ERROR_NONE; } + +std::string AppActionLaunch::serialize() +{ + return appID; +} + +int AppActionLaunch::parse(const std::string &archive) +{ + appID = archive; + return MODES_ERROR_NONE; +} diff --git a/app/AppActionLaunch.h b/app/AppActionLaunch.h index e0f8087..818457d 100644 --- a/app/AppActionLaunch.h +++ b/app/AppActionLaunch.h @@ -25,15 +25,15 @@ MODES_NAMESPACE_BEGIN class AppActionLaunch : public AppAction { public: + static const std::string NAME; AppActionLaunch(); - virtual int set(const std::string &val) override; - virtual int get(std::string *val) override; - virtual int undo() override; - - static const std::string NAME; + int set(const std::string &val) override; + int get(std::string *val) override; + int undo() override; + std::string serialize() override; + int parse(const std::string &data) override; static void appContextStatusCallback(app_context_h app_context, app_context_status_e status, void *user_data); - private: std::string appID; static std::list appidList; diff --git a/app/AppPlugin.cpp b/app/AppPlugin.cpp index 8b78c0b..9204ca8 100644 --- a/app/AppPlugin.cpp +++ b/app/AppPlugin.cpp @@ -26,9 +26,9 @@ public: AppPlugin(); ~AppPlugin(); - int set(const std::string &key, const std::string &val, PluginAction **pluginAction) override; - int undo(PluginAction *pluginAction) override; - + int set(const std::string &key, const std::string &val, PluginAction **piAction) override; + int undo(PluginAction *piAction) override; + PluginAction* getUndoAction(const std::string &key, const std::string &info) override; private: AppFactory appFactory; }; @@ -52,7 +52,7 @@ AppPlugin::~AppPlugin() { } -int AppPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) +int AppPlugin::set(const std::string &key, const std::string &val, PluginAction **piAction) { AppAction *action = appFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -60,17 +60,17 @@ int AppPlugin::set(const std::string &key, const std::string &val, PluginAction DBG("set [%s, %s]", key.c_str(), val.c_str()); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else appFactory.destroyAction(action); return ret; } -int AppPlugin::undo(PluginAction *pluginAction) +int AppPlugin::undo(PluginAction *piAction) { - AppAction *action = static_cast(pluginAction); + AppAction *action = static_cast(piAction); RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); DBG("Action(%s) undo", action->getName().c_str()); @@ -80,3 +80,18 @@ int AppPlugin::undo(PluginAction *pluginAction) return ret; } + +PluginAction* AppPlugin::getUndoAction(const std::string &key, const std::string &info) +{ + AppAction *action = appFactory.createAction(key); + RETVM_IF(nullptr == action, nullptr, "action(%s) is null", key.c_str()); + + int ret = action->parse(info); + if (MODES_ERROR_NONE != ret) { + ERR("Action(%s) parse(%s) Fail(%d)", key.c_str(), info.c_str(), ret); + appFactory.destroyAction(action); + return nullptr; + } + + return action; +} diff --git a/bluetooth/BtAction.cpp b/bluetooth/BtAction.cpp index 984351d..ae784be 100644 --- a/bluetooth/BtAction.cpp +++ b/bluetooth/BtAction.cpp @@ -18,7 +18,8 @@ MODES_NAMESPACE_USE; -BtAction::BtAction() +BtAction::BtAction(const std::string &name) + : PluginAction(name) { int btRet = bt_initialize(); if (BT_ERROR_NONE != btRet) { diff --git a/bluetooth/BtAction.h b/bluetooth/BtAction.h index b901114..d30ade8 100644 --- a/bluetooth/BtAction.h +++ b/bluetooth/BtAction.h @@ -25,7 +25,7 @@ MODES_NAMESPACE_BEGIN class BtAction : public PluginAction { public: - BtAction(); + BtAction(const std::string &name); virtual ~BtAction(); virtual int set(bool val); diff --git a/bluetooth/BtActionAudioConnect.cpp b/bluetooth/BtActionAudioConnect.cpp index 0829ac6..57fe412 100644 --- a/bluetooth/BtActionAudioConnect.cpp +++ b/bluetooth/BtActionAudioConnect.cpp @@ -21,19 +21,11 @@ MODES_NAMESPACE_USE; -void BtActionAudioConnect::connectionStateChangedCb(int result, bool connected, const char *remote_address, bt_audio_profile_type_e type, void *user_data) -{ - INFO("remote_address: %s", remote_address); - - if (BT_ERROR_NONE != result) { - ERR("Connection_state_changed_cb() Fail(%d)", result); - } -} +const std::string BtActionAudioConnect::NAME = "audioConnect"; BtActionAudioConnect::BtActionAudioConnect() + : BtAction(NAME) { - setName("audioConnect"); - bt_audio_initialize(); int btRet = bt_audio_set_connection_state_changed_cb(connectionStateChangedCb, this); if (BT_ERROR_NONE != btRet) { @@ -41,6 +33,16 @@ BtActionAudioConnect::BtActionAudioConnect() } } +void BtActionAudioConnect::connectionStateChangedCb(int result, bool connected, + const char *remote_address, bt_audio_profile_type_e type, void *user_data) +{ + INFO("remote_address: %s", remote_address); + + if (BT_ERROR_NONE != result) { + ERR("Connection_state_changed_cb() Fail(%d)", result); + } +} + bool BtActionAudioConnect::isBtAdapterEnabled() { bt_adapter_state_e state; @@ -163,7 +165,7 @@ bt_device_info_s *BtActionAudioConnect::getBondedDeviceInfo(const std::string &v for (int i = 0; i < deviceInfo->service_count; i++) { bt_get_uuid_name(deviceInfo->service_uuid[i], &str); INFO("[%d / %d] %s (%s)", i, deviceInfo->service_count, - str ? str : "Unknown", deviceInfo->service_uuid[i]); + str ? str : "Unknown", deviceInfo->service_uuid[i]); str = NULL; } } diff --git a/bluetooth/BtActionAudioConnect.h b/bluetooth/BtActionAudioConnect.h index a0e2243..eee8b1b 100644 --- a/bluetooth/BtActionAudioConnect.h +++ b/bluetooth/BtActionAudioConnect.h @@ -22,9 +22,10 @@ MODES_NAMESPACE_BEGIN class BtActionAudioConnect : public BtAction { public: + static const std::string NAME; BtActionAudioConnect(); - int set(const std::string &val) override; + int set(const std::string &val) override; private: bool isBtAdapterEnabled(); bt_device_info_s *getBondedDeviceInfo(const std::string &val); diff --git a/bluetooth/BtActionPower.cpp b/bluetooth/BtActionPower.cpp index 7e329cd..0c87332 100644 --- a/bluetooth/BtActionPower.cpp +++ b/bluetooth/BtActionPower.cpp @@ -15,18 +15,19 @@ */ #include "BtActionPower.h" -#include +#include #include -#include "plugin-log.h" #include +#include "plugin-log.h" MODES_NAMESPACE_USE; #define SAFE_POINTER_ASSIGN( arg1, arg2) if ( arg1 != NULL ) { *arg1 = arg2; } +const std::string BtActionPower::NAME = "power"; + BtActionPower::BtActionPower() - :undoVal(false) + : BtAction(NAME), undoVal(false) { - setName("power"); } int BtActionPower::set(bool val) @@ -63,3 +64,19 @@ int BtActionPower::undo() { return set(undoVal); } + +std::string BtActionPower::serialize() +{ + std::ostringstream ostr; + + ostr << undoVal; + return ostr.str(); +} + +int BtActionPower::parse(const std::string &archive) +{ + std::istringstream iss(archive); + iss >> undoVal; + + return MODES_ERROR_NONE; +} diff --git a/bluetooth/BtActionPower.h b/bluetooth/BtActionPower.h index 24ea19c..dba36ef 100644 --- a/bluetooth/BtActionPower.h +++ b/bluetooth/BtActionPower.h @@ -22,10 +22,13 @@ MODES_NAMESPACE_BEGIN class BtActionPower : public BtAction { public: + static const std::string NAME; BtActionPower(); int set(bool val) override; int undo() override; + std::string serialize() override; + int parse(const std::string & archive) override; private: bool undoVal; }; diff --git a/bluetooth/BtFactory.cpp b/bluetooth/BtFactory.cpp index ef2ac42..dde8e04 100644 --- a/bluetooth/BtFactory.cpp +++ b/bluetooth/BtFactory.cpp @@ -25,8 +25,8 @@ MODES_NAMESPACE_USE; BtFactory::BtFactory() { - registerAction("power", BtActionEnum::POWER); - registerAction("audioConnect", BtActionEnum::AUDIO_CONNECT); + actionMap[BtActionPower::NAME] = POWER; + actionMap[BtActionAudioConnect::NAME] = AUDIO_CONNECT; } BtAction* BtFactory::createAction(const std::string &key) @@ -38,10 +38,10 @@ BtAction* BtFactory::createAction(const std::string &key) } switch (search->second) { - case BtActionEnum::POWER: + case POWER: return (new BtActionPower()); - case BtActionEnum::AUDIO_CONNECT: + case AUDIO_CONNECT: return (new BtActionAudioConnect()); } @@ -52,9 +52,3 @@ void BtFactory::destroyAction(BtAction *action) { delete action; } - -void BtFactory::registerAction(std::string key, BtActionEnum action) -{ - DBG("Register Action key[%s]", key.c_str()); - actionMap[key] = action; -} diff --git a/bluetooth/BtFactory.h b/bluetooth/BtFactory.h index e8b3f92..815ca8f 100644 --- a/bluetooth/BtFactory.h +++ b/bluetooth/BtFactory.h @@ -29,13 +29,12 @@ public: BtAction *createAction(const std::string &key); void destroyAction(BtAction *action); private: - enum class BtActionEnum { + enum actionKey { POWER, AUDIO_CONNECT }; - void registerAction(std::string key, BtActionEnum action); - std::map actionMap; + std::map actionMap; }; MODES_NAMESPACE_END diff --git a/bluetooth/BtPlugin.cpp b/bluetooth/BtPlugin.cpp index eea6d47..ab57774 100644 --- a/bluetooth/BtPlugin.cpp +++ b/bluetooth/BtPlugin.cpp @@ -27,11 +27,12 @@ public: BtPlugin(); ~BtPlugin(); - 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; + int set(const std::string &key, int val, PluginAction **piAction) override; + int set(const std::string &key, double val, PluginAction **piAction) override; + int set(const std::string &key, bool val, PluginAction **piAction) override; + int set(const std::string &key, const std::string &val, PluginAction **piAction) override; + int undo(PluginAction *piAction) override; + PluginAction* getUndoAction(const std::string &key, const std::string &info) override; private: BtFactory btFactory; }; @@ -55,7 +56,7 @@ BtPlugin::~BtPlugin() { } -int BtPlugin::set(const std::string &key, int val, PluginAction **pluginAction) +int BtPlugin::set(const std::string &key, int val, PluginAction **piAction) { BtAction *action = btFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -63,15 +64,15 @@ int BtPlugin::set(const std::string &key, int val, PluginAction **pluginAction) DBG("set [%s, %d]", key.c_str(), val); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else btFactory.destroyAction(action); return ret; } -int BtPlugin::set(const std::string &key, double val, PluginAction **pluginAction) +int BtPlugin::set(const std::string &key, double val, PluginAction **piAction) { BtAction *action = btFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -79,15 +80,15 @@ int BtPlugin::set(const std::string &key, double val, PluginAction **pluginActio DBG("set [%s, %f]", key.c_str(), val); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else btFactory.destroyAction(action); return ret; } -int BtPlugin::set(const std::string &key, bool val, PluginAction **pluginAction) +int BtPlugin::set(const std::string &key, bool val, PluginAction **piAction) { BtAction *action = btFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -95,15 +96,15 @@ int BtPlugin::set(const std::string &key, bool val, PluginAction **pluginAction) DBG("set [%s, %s]", key.c_str(), (val ? "on" : "off")); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else btFactory.destroyAction(action); return ret; } -int BtPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) +int BtPlugin::set(const std::string &key, const std::string &val, PluginAction **piAction) { BtAction *action = btFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -111,17 +112,17 @@ int BtPlugin::set(const std::string &key, const std::string &val, PluginAction * DBG("set [%s, %s]", key.c_str(), val.c_str()); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else btFactory.destroyAction(action); return ret; } -int BtPlugin::undo(PluginAction *pluginAction) +int BtPlugin::undo(PluginAction *piAction) { - BtAction *action = static_cast(pluginAction); + BtAction *action = static_cast(piAction); RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); DBG("Action(%s) undo", action->getName().c_str()); @@ -131,3 +132,18 @@ int BtPlugin::undo(PluginAction *pluginAction) return ret; } + +PluginAction* BtPlugin::getUndoAction(const std::string &key, const std::string &info) +{ + BtAction *action = btFactory.createAction(key); + RETVM_IF(nullptr == action, nullptr, "action(%s) is null", key.c_str()); + + int ret = action->parse(info); + if (MODES_ERROR_NONE != ret) { + ERR("Action(%s) parse(%s) Fail(%d)", key.c_str(), info.c_str(), ret); + btFactory.destroyAction(action); + return nullptr; + } + + return action; +} diff --git a/media/MediaAction.h b/media/MediaAction.h index d0bf7d7..04e1eb3 100644 --- a/media/MediaAction.h +++ b/media/MediaAction.h @@ -23,7 +23,10 @@ MODES_NAMESPACE_BEGIN class MediaAction : public PluginAction { public: - MediaAction() = default; + MediaAction(const std::string &name) + : PluginAction(name) + { + } virtual ~MediaAction() = default; virtual int set(std::string val) diff --git a/media/MediaPlayer.cpp b/media/MediaPlayer.cpp index 0b9cea7..7e5c0df 100644 --- a/media/MediaPlayer.cpp +++ b/media/MediaPlayer.cpp @@ -23,6 +23,10 @@ MODES_NAMESPACE_USE; const std::string MediaPlayer::NAME = "player"; +MediaPlayer::MediaPlayer() + : MediaAction(NAME), player(NULL), stream_info(NULL) +{ +} void MediaPlayer::playerCompletedCb(void *data) { @@ -32,16 +36,6 @@ void MediaPlayer::playerCompletedCb(void *data) h->destroyPlayer(); } -MediaPlayer::MediaPlayer() -:player(NULL), stream_info(NULL) -{ - setName(NAME); -} - -MediaPlayer::~MediaPlayer() -{ -} - int MediaPlayer::createPlayer() { int ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info); diff --git a/media/MediaPlayer.h b/media/MediaPlayer.h index fd0c9b1..b8ccf62 100644 --- a/media/MediaPlayer.h +++ b/media/MediaPlayer.h @@ -24,14 +24,12 @@ MODES_NAMESPACE_BEGIN class MediaPlayer : public MediaAction { public: - MediaPlayer(); - ~MediaPlayer(); - - virtual int set(std::string val); - virtual int undo(); - static const std::string NAME; + MediaPlayer(); + ~MediaPlayer() = default; + int set(std::string val) override; + int undo() override; private: int createPlayer(); int destroyPlayer(); diff --git a/media/MediaPlugin.cpp b/media/MediaPlugin.cpp index 9b3e087..b9ecc55 100644 --- a/media/MediaPlugin.cpp +++ b/media/MediaPlugin.cpp @@ -28,10 +28,10 @@ public: MediaPlugin(); ~MediaPlugin(); - int set(const std::string &key, const std::string &val, PluginAction **pluginAction) override; - + int set(const std::string &key, const std::string &val, PluginAction **piAction) override; + PluginAction* getUndoAction(const std::string &key, const std::string &info) override; private: - MediaFactory pkgFactory; + MediaFactory mediaFactory; }; extern "C" API Plugin *objectCreate(void) @@ -53,17 +53,32 @@ MediaPlugin::~MediaPlugin() { } -int MediaPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) +int MediaPlugin::set(const std::string &key, const std::string &val, PluginAction **piAction) { - MediaAction *action = pkgFactory.createAction(key); + MediaAction *action = mediaFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); DBG("Action(%s) set(%s)", key.c_str(), val.c_str()); int ret = action->set(val); - if ((ret == MODES_ERROR_NONE) && pluginAction) - *pluginAction = action; + if ((ret == MODES_ERROR_NONE) && piAction) + *piAction = action; else - pkgFactory.destroyAction(action); + mediaFactory.destroyAction(action); return ret; } + +PluginAction* MediaPlugin::getUndoAction(const std::string &key, const std::string &info) +{ + MediaAction *action = mediaFactory.createAction(key); + RETVM_IF(nullptr == action, nullptr, "action(%s) is null", key.c_str()); + + int ret = action->parse(info); + if (MODES_ERROR_NONE != ret) { + ERR("Action(%s) parse(%s) Fail(%d)", key.c_str(), info.c_str(), ret); + mediaFactory.destroyAction(action); + return nullptr; + } + + return action; +} diff --git a/pkg/PkgAction.h b/pkg/PkgAction.h index 0a014e4..55f08a9 100644 --- a/pkg/PkgAction.h +++ b/pkg/PkgAction.h @@ -23,7 +23,10 @@ MODES_NAMESPACE_BEGIN class PkgAction : public PluginAction { public: - PkgAction() = default; + PkgAction(const std::string &name) + : PluginAction(name) + { + } virtual ~PkgAction() = default; virtual int set(int val) diff --git a/pkg/PkgEnableSupportMode.cpp b/pkg/PkgEnableSupportMode.cpp index dd3808a..7675b5d 100644 --- a/pkg/PkgEnableSupportMode.cpp +++ b/pkg/PkgEnableSupportMode.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -28,8 +29,8 @@ const std::string PkgStartSupportMode::NAME = "startSupportMode"; int PkgStartSupportMode::appModeVal = 0; PkgStartSupportMode::PkgStartSupportMode() + : PkgAction(NAME) { - setName(NAME); } int PkgStartSupportMode::app_list_cb(const pkgmgrinfo_appinfo_h handle, void *userData) @@ -132,3 +133,23 @@ int PkgStartSupportMode::undo() return MODES_ERROR_NONE; } + +std::string PkgStartSupportMode::serialize() +{ + std::ostringstream ostr; + for (auto it = appList.begin(); it != appList.end(); it++) + ostr << *it << delimiter; + + return ostr.str(); +} + +int PkgStartSupportMode::parse(const std::string &data) +{ + size_t pos; + size_t start = 0; + while ((pos = data.find(delimiter, start)) != std::string::npos) { + appList.push_back(data.substr(start, pos - start)); + start = pos + 1; + } + return MODES_ERROR_NONE; +} diff --git a/pkg/PkgEnableSupportMode.h b/pkg/PkgEnableSupportMode.h index 3f51a2c..b5ccc98 100644 --- a/pkg/PkgEnableSupportMode.h +++ b/pkg/PkgEnableSupportMode.h @@ -25,19 +25,21 @@ MODES_NAMESPACE_BEGIN class PkgStartSupportMode : public PkgAction { public: + static const std::string NAME; PkgStartSupportMode(); - virtual int set(int val); - virtual int get(int *val); - virtual int undo() override; - - static const std::string NAME; + int set(int val) override; + int get(int *val) override; + int undo() override; + std::string serialize() override; + int parse(const std::string &data) override; private: enum AppSupportMode { ULTRA_POWER_SAVING = 1, COOL_DOWN = 2, SCREEN_READER = 4 }; + const char delimiter = '#'; static int app_list_cb(const pkgmgrinfo_appinfo_h handle, void *user_data); static int appModeVal; diff --git a/pkg/PkgPlugin.cpp b/pkg/PkgPlugin.cpp index bd45830..5fdcc6b 100644 --- a/pkg/PkgPlugin.cpp +++ b/pkg/PkgPlugin.cpp @@ -28,9 +28,9 @@ public: PkgPlugin(); ~PkgPlugin(); - int set(const std::string &key, int val, PluginAction **pluginAction) override; - int undo(PluginAction *pluginAction) override; - + int set(const std::string &key, int val, PluginAction **piAction) override; + int undo(PluginAction *piAction) override; + PluginAction* getUndoAction(const std::string &key, const std::string &info) override; private: PkgFactory pkgFactory; }; @@ -54,7 +54,7 @@ PkgPlugin::~PkgPlugin() { } -int PkgPlugin::set(const std::string &key, int val, PluginAction **pluginAction) +int PkgPlugin::set(const std::string &key, int val, PluginAction **piAction) { PkgAction *action = pkgFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -62,16 +62,16 @@ int PkgPlugin::set(const std::string &key, int val, PluginAction **pluginAction) DBG("Action(%s) set(%d)", key.c_str(), val); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else pkgFactory.destroyAction(action); return ret; } -int PkgPlugin::undo(PluginAction *pluginAction) +int PkgPlugin::undo(PluginAction *piAction) { - PkgAction *action = static_cast(pluginAction); + PkgAction *action = static_cast(piAction); RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); DBG("Action(%s) undo", action->getName().c_str()); @@ -81,3 +81,18 @@ int PkgPlugin::undo(PluginAction *pluginAction) return ret; } + +PluginAction* PkgPlugin::getUndoAction(const std::string &key, const std::string &info) +{ + PkgAction *action = pkgFactory.createAction(key); + RETVM_IF(nullptr == action, nullptr, "action(%s) is null", key.c_str()); + + int ret = action->parse(info); + if (MODES_ERROR_NONE != ret) { + ERR("Action(%s) parse(%s) Fail(%d)", key.c_str(), info.c_str(), ret); + pkgFactory.destroyAction(action); + return nullptr; + } + + return action; +} diff --git a/vconf/VconfActionBool.cpp b/vconf/VconfActionBool.cpp index 27b9ede..e61b5ab 100644 --- a/vconf/VconfActionBool.cpp +++ b/vconf/VconfActionBool.cpp @@ -15,14 +15,14 @@ */ #include "VconfActionBool.h" #include -#include +#include #include #include "plugin-log.h" MODES_NAMESPACE_USE; VconfActionBool::VconfActionBool(const std::string &vconfKey) - :key(vconfKey), oldVal(false) + : PluginAction("boolVconf"), key(vconfKey), oldVal(false) { } @@ -72,3 +72,19 @@ int VconfActionBool::undo() return MODES_ERROR_NONE; } + +std::string VconfActionBool::serialize() +{ + std::ostringstream ostr; + + ostr << oldVal; + return ostr.str(); +} + +int VconfActionBool::parse(const std::string &archive) +{ + std::istringstream iss(archive); + iss >> oldVal; + + return MODES_ERROR_NONE; +} diff --git a/vconf/VconfActionBool.h b/vconf/VconfActionBool.h index 99ad60d..9d85615 100644 --- a/vconf/VconfActionBool.h +++ b/vconf/VconfActionBool.h @@ -28,7 +28,9 @@ public: int set(bool val); bool get(); - virtual int undo() override; + int undo() override; + std::string serialize() override; + int parse(const std::string &archive) override; private: std::string key; bool oldVal; diff --git a/vconf/VconfActionDbl.cpp b/vconf/VconfActionDbl.cpp index 8c3bebe..0a41472 100644 --- a/vconf/VconfActionDbl.cpp +++ b/vconf/VconfActionDbl.cpp @@ -15,14 +15,14 @@ */ #include "VconfActionDbl.h" #include -#include +#include #include #include "plugin-log.h" MODES_NAMESPACE_USE; VconfActionDbl::VconfActionDbl(const std::string &vconfKey) - :key(vconfKey), oldVal(0.0) + : PluginAction("dblVconf"), key(vconfKey), oldVal(0.0) { } @@ -70,3 +70,19 @@ int VconfActionDbl::undo() return MODES_ERROR_NONE; } + +std::string VconfActionDbl::serialize() +{ + std::ostringstream ostr; + + ostr << oldVal; + return ostr.str(); +} + +int VconfActionDbl::parse(const std::string &archive) +{ + std::istringstream iss(archive); + iss >> oldVal; + + return MODES_ERROR_NONE; +} diff --git a/vconf/VconfActionDbl.h b/vconf/VconfActionDbl.h index 35a7c39..2e499d7 100644 --- a/vconf/VconfActionDbl.h +++ b/vconf/VconfActionDbl.h @@ -28,7 +28,9 @@ public: int set(double val); double get(); - virtual int undo() override; + int undo() override; + std::string serialize() override; + int parse(const std::string &archive) override; private: std::string key; double oldVal; diff --git a/vconf/VconfActionInt.cpp b/vconf/VconfActionInt.cpp index 39ecfda..dd8c340 100644 --- a/vconf/VconfActionInt.cpp +++ b/vconf/VconfActionInt.cpp @@ -15,14 +15,14 @@ */ #include "VconfActionInt.h" #include -#include +#include #include #include "plugin-log.h" MODES_NAMESPACE_USE; VconfActionInt::VconfActionInt(const std::string &vconfKey) - :key(vconfKey), oldVal(0) + : PluginAction("intVconf"), key(vconfKey), oldVal(0) { } @@ -70,3 +70,19 @@ int VconfActionInt::undo() return MODES_ERROR_NONE; } + +std::string VconfActionInt::serialize() +{ + std::ostringstream ostr; + + ostr << oldVal; + return ostr.str(); +} + +int VconfActionInt::parse(const std::string &archive) +{ + std::istringstream iss(archive); + iss >> oldVal; + + return MODES_ERROR_NONE; +} diff --git a/vconf/VconfActionInt.h b/vconf/VconfActionInt.h index 94df5eb..8bd4542 100644 --- a/vconf/VconfActionInt.h +++ b/vconf/VconfActionInt.h @@ -28,7 +28,9 @@ public: int set(int val); int get(); - virtual int undo() override; + int undo() override; + std::string serialize() override; + int parse(const std::string &archive) override; private: std::string key; int oldVal; diff --git a/vconf/VconfActionStr.cpp b/vconf/VconfActionStr.cpp index bfb2319..d5076e4 100644 --- a/vconf/VconfActionStr.cpp +++ b/vconf/VconfActionStr.cpp @@ -15,14 +15,13 @@ */ #include "VconfActionStr.h" #include -#include #include #include "plugin-log.h" MODES_NAMESPACE_USE; VconfActionStr::VconfActionStr(const std::string &vconfKey) - :key(vconfKey) + : PluginAction("strVconf"), key(vconfKey) { } @@ -74,3 +73,14 @@ int VconfActionStr::undo() return MODES_ERROR_NONE; } + +std::string VconfActionStr::serialize() +{ + return oldVal; +} + +int VconfActionStr::parse(const std::string &archive) +{ + oldVal = archive; + return MODES_ERROR_NONE; +} diff --git a/vconf/VconfActionStr.h b/vconf/VconfActionStr.h index 7be19af..e5080e1 100644 --- a/vconf/VconfActionStr.h +++ b/vconf/VconfActionStr.h @@ -28,7 +28,9 @@ public: int set(const std::string &val); std::string get(); - virtual int undo() override; + int undo() override; + std::string serialize() override; + int parse(const std::string &archive) override; private: std::string key; std::string oldVal; diff --git a/vconf/VconfPlugin.cpp b/vconf/VconfPlugin.cpp index 2bf74c4..718eaae 100644 --- a/vconf/VconfPlugin.cpp +++ b/vconf/VconfPlugin.cpp @@ -32,10 +32,12 @@ class VconfPlugin : public Plugin { public: VconfPlugin(); - 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 set(const std::string &key, int val, PluginAction **piAction) override; + int set(const std::string &key, double val, PluginAction **piAction) override; + int set(const std::string &key, bool val, PluginAction **piAction) override; + 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; @@ -73,7 +75,7 @@ VconfPlugin::VconfPlugin() setName("vconf"); } -int VconfPlugin::set(const std::string &key, int val, PluginAction **pluginAction) +int VconfPlugin::set(const std::string &key, int val, PluginAction **piAction) { int ret; DBG("set(%s, %d)", key.c_str(), val); @@ -89,14 +91,14 @@ int VconfPlugin::set(const std::string &key, int val, PluginAction **pluginActio VconfActionInt *action = new VconfActionInt(newKey); ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else delete action; return ret; } -int VconfPlugin::set(const std::string &key, double val, PluginAction **pluginAction) +int VconfPlugin::set(const std::string &key, double val, PluginAction **piAction) { int ret; DBG("set(%s, %lf)", key.c_str(), val); @@ -112,14 +114,14 @@ int VconfPlugin::set(const std::string &key, double val, PluginAction **pluginAc VconfActionDbl *action = new VconfActionDbl(newKey); ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else delete action; return ret; } -int VconfPlugin::set(const std::string &key, bool val, PluginAction **pluginAction) +int VconfPlugin::set(const std::string &key, bool val, PluginAction **piAction) { int ret; DBG("set(%s, %d)", key.c_str(), val); @@ -135,14 +137,14 @@ int VconfPlugin::set(const std::string &key, bool val, PluginAction **pluginActi VconfActionBool *action = new VconfActionBool(newKey); ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else delete action; return ret; } -int VconfPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) +int VconfPlugin::set(const std::string &key, const std::string &val, PluginAction **piAction) { DBG("set(%s, %s)", key.c_str(), val.c_str()); @@ -157,13 +159,54 @@ int VconfPlugin::set(const std::string &key, const std::string &val, PluginActio VconfActionStr *action = new VconfActionStr(newKey); ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else delete action; return ret; } +PluginAction* VconfPlugin::getUndoAction(const std::string &key, const std::string &info) +{ + std::string newKey(key); + std::replace(newKey.begin(), newKey.end(), '.', '/'); + + PluginAction *piAction; + switch (info.front()) { + case 's': + piAction = new VconfActionStr(newKey); + break; + case 'i': + piAction = new VconfActionInt(newKey); + break; + case 'd': + piAction = new VconfActionDbl(newKey); + break; + case 'b': + piAction = new VconfActionBool(newKey); + break; + default: + ERR("Unknown Undo Information(%s)", info.c_str()); + return nullptr; + } + int ret = piAction->parse(info); + if (MODES_ERROR_NONE != ret) { + ERR("Action(%s) parse(%s) Fail(%d)", key.c_str(), info.c_str(), ret); + delete piAction; + return nullptr; + } + + return piAction; +} + +std::string VconfPlugin::serializeAction(PluginAction *piAction) +{ + RETV_IF(nullptr == piAction, std::string()); + + std::string rawStr = piAction->serialize(); + return piAction->getName().front() + rawStr; +} + int VconfPlugin::getInt(const std::string &key) { DBG("getInt(%s)", key.c_str()); diff --git a/wifi/WifiAction.cpp b/wifi/WifiAction.cpp index bc726e1..368fc61 100644 --- a/wifi/WifiAction.cpp +++ b/wifi/WifiAction.cpp @@ -21,7 +21,8 @@ MODES_NAMESPACE_USE; -WifiAction::WifiAction() +WifiAction::WifiAction(const std::string &name) + : PluginAction(name) { int ret = wifi_manager_initialize(&wifiManagerHandle); if (ret != WIFI_MANAGER_ERROR_NONE) { diff --git a/wifi/WifiAction.h b/wifi/WifiAction.h index c179493..01e22ac 100644 --- a/wifi/WifiAction.h +++ b/wifi/WifiAction.h @@ -24,7 +24,7 @@ MODES_NAMESPACE_BEGIN class WifiAction : public PluginAction { public: - WifiAction(); + WifiAction(const std::string &name); virtual ~WifiAction(); bool isWifiActivate(); diff --git a/wifi/WifiActionPower.cpp b/wifi/WifiActionPower.cpp index 40bbab4..f728000 100644 --- a/wifi/WifiActionPower.cpp +++ b/wifi/WifiActionPower.cpp @@ -15,16 +15,17 @@ */ #include "WifiActionPower.h" -#include +#include #include #include "plugin-log.h" MODES_NAMESPACE_USE; +const std::string WifiActionPower::NAME = "power"; + WifiActionPower::WifiActionPower() - :oldVal(false) + : WifiAction(NAME), oldVal(false) { - setName("power"); } void WifiActionPower::activate_cb(wifi_manager_error_e result, void *user_data) @@ -109,3 +110,19 @@ int WifiActionPower::undo() INFO("Wifi power [%s]", oldVal ? "On" : "Off"); return MODES_ERROR_NONE; } + +std::string WifiActionPower::serialize() +{ + std::ostringstream ostr; + + ostr << oldVal; + return ostr.str(); +} + +int WifiActionPower::parse(const std::string &archive) +{ + std::istringstream iss(archive); + iss >> oldVal; + + return MODES_ERROR_NONE; +} diff --git a/wifi/WifiActionPower.h b/wifi/WifiActionPower.h index 056961c..aa0bafd 100644 --- a/wifi/WifiActionPower.h +++ b/wifi/WifiActionPower.h @@ -22,11 +22,14 @@ MODES_NAMESPACE_BEGIN class WifiActionPower : public WifiAction { public: + static const std::string NAME; WifiActionPower(); int set(bool val) override; int get(bool *val) override; int undo() override; + std::string serialize() override; + int parse(const std::string &archive) 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); diff --git a/wifi/WifiFactory.cpp b/wifi/WifiFactory.cpp index f72e16b..cf637e5 100644 --- a/wifi/WifiFactory.cpp +++ b/wifi/WifiFactory.cpp @@ -23,7 +23,7 @@ MODES_NAMESPACE_USE; WifiFactory::WifiFactory() { - registerAction("power", WifiActionEnum::POWER); + actionMap[WifiActionPower::NAME] = POWER; } WifiAction* WifiFactory::createAction(const std::string &key) @@ -35,7 +35,7 @@ WifiAction* WifiFactory::createAction(const std::string &key) } switch (search->second) { - case WifiActionEnum::POWER: + case POWER: WifiActionPower *wifiActionPower = new WifiActionPower(); return wifiActionPower; break; @@ -48,9 +48,3 @@ void WifiFactory::destroyAction(WifiAction *action) { delete action; } - -void WifiFactory::registerAction(std::string key, WifiActionEnum action) -{ - DBG("Register Action key[%s]", key.c_str()); - actionMap[key] = action; -} diff --git a/wifi/WifiFactory.h b/wifi/WifiFactory.h index 71bb2fe..259305f 100644 --- a/wifi/WifiFactory.h +++ b/wifi/WifiFactory.h @@ -29,12 +29,11 @@ public: WifiAction *createAction(const std::string &key); void destroyAction(WifiAction *action); private: - enum class WifiActionEnum { + enum actionKey { POWER }; - void registerAction(std::string key, WifiActionEnum action); - std::map actionMap; + std::map actionMap; }; MODES_NAMESPACE_END diff --git a/wifi/WifiPlugin.cpp b/wifi/WifiPlugin.cpp index bf82dbc..fea8078 100644 --- a/wifi/WifiPlugin.cpp +++ b/wifi/WifiPlugin.cpp @@ -28,11 +28,12 @@ public: WifiPlugin(); ~WifiPlugin(); - 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; + int set(const std::string &key, int val, PluginAction **piAction) override; + int set(const std::string &key, double val, PluginAction **piAction) override; + int set(const std::string &key, bool val, PluginAction **piAction) override; + int set(const std::string &key, const std::string &val, PluginAction **piAction) override; + int undo(PluginAction *piAction) override; + PluginAction* getUndoAction(const std::string &key, const std::string &info) override; private: WifiFactory wifiFactory; }; @@ -56,7 +57,7 @@ WifiPlugin::~WifiPlugin() { } -int WifiPlugin::set(const std::string &key, int val, PluginAction **pluginAction) +int WifiPlugin::set(const std::string &key, int val, PluginAction **piAction) { WifiAction *action = wifiFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -64,15 +65,15 @@ int WifiPlugin::set(const std::string &key, int val, PluginAction **pluginAction DBG("set [%s, %d]", key.c_str(), val); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else wifiFactory.destroyAction(action); return ret; } -int WifiPlugin::set(const std::string &key, double val, PluginAction **pluginAction) +int WifiPlugin::set(const std::string &key, double val, PluginAction **piAction) { WifiAction *action = wifiFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -80,15 +81,15 @@ int WifiPlugin::set(const std::string &key, double val, PluginAction **pluginAct DBG("set [%s, %f]", key.c_str(), val); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else wifiFactory.destroyAction(action); return ret; } -int WifiPlugin::set(const std::string &key, bool val, PluginAction **pluginAction) +int WifiPlugin::set(const std::string &key, bool val, PluginAction **piAction) { WifiAction *action = wifiFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -96,15 +97,15 @@ int WifiPlugin::set(const std::string &key, bool val, PluginAction **pluginActio DBG("set [%s, %s]", key.c_str(), (val ? "on" : "off")); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else wifiFactory.destroyAction(action); return ret; } -int WifiPlugin::set(const std::string &key, const std::string &val, PluginAction **pluginAction) +int WifiPlugin::set(const std::string &key, const std::string &val, PluginAction **piAction) { WifiAction *action = wifiFactory.createAction(key); RETVM_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER, "action(%s) is null", key.c_str()); @@ -112,17 +113,17 @@ int WifiPlugin::set(const std::string &key, const std::string &val, PluginAction DBG("set [%s, %s]", key.c_str(), val.c_str()); int ret = action->set(val); - if (pluginAction) - *pluginAction = action; + if (piAction) + *piAction = action; else wifiFactory.destroyAction(action); return ret; } -int WifiPlugin::undo(PluginAction *pluginAction) +int WifiPlugin::undo(PluginAction *piAction) { - WifiAction *action = static_cast(pluginAction); + WifiAction *action = static_cast(piAction); RETV_IF(nullptr == action, MODES_ERROR_INVALID_PARAMETER); DBG("Action(%s) undo", action->getName().c_str()); @@ -133,3 +134,18 @@ int WifiPlugin::undo(PluginAction *pluginAction) return ret; } +PluginAction* WifiPlugin::getUndoAction(const std::string &key, const std::string &info) +{ + WifiAction *action = wifiFactory.createAction(key); + RETVM_IF(nullptr == action, nullptr, "action(%s) is null", key.c_str()); + + int ret = action->parse(info); + if (MODES_ERROR_NONE != ret) { + ERR("Action(%s) parse(%s) Fail(%d)", key.c_str(), info.c_str(), ret); + wifiFactory.destroyAction(action); + return nullptr; + } + + return action; +} +