From 475c497884feb3aff3143bd7cb4e7a4042db30dc Mon Sep 17 00:00:00 2001 From: Beata Stefaniuk Date: Thu, 18 Dec 2014 16:54:00 +0100 Subject: [PATCH] [system-setting] setProperty() method added Change-Id: Ib6730c350a98e35d32e03cf88d75c0200d48a565 Signed-off-by: Beata Stefaniuk Signed-off-by: Jakub Siewierski --- src/systemsetting/systemsetting_api.js | 43 +++++++--- src/systemsetting/systemsetting_instance.cc | 92 +++++++++++++++++++-- src/systemsetting/systemsetting_instance.h | 5 +- 3 files changed, 119 insertions(+), 21 deletions(-) diff --git a/src/systemsetting/systemsetting_api.js b/src/systemsetting/systemsetting_api.js index e06da487..ed6d6084 100644 --- a/src/systemsetting/systemsetting_api.js +++ b/src/systemsetting/systemsetting_api.js @@ -22,26 +22,18 @@ function callSync_(msg) { return obj.result; } -var SystemSettingType = { - HOME_SCREEN : 'HOME_SCREEN', - LOCK_SCREEN : 'LOCK_SCREEN', - INCOMING_CALL : 'INCOMING_CALL', - NOTIFICATION_EMAIL : 'NOTIFICATION_EMAIL' -}; +var SystemSettingTypeValues = ['HOME_SCREEN', 'LOCK_SCREEN', 'INCOMING_CALL', 'NOTIFICATION_EMAIL']; function SystemSettingManager() { } SystemSettingManager.prototype.getProperty = function() { var args = validator_.validateArgs(arguments, [ - {name: 'type', type: types_.STRING}, - {name: 'successCallback', type: types_.FUNCTION}, - {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true} + { name: 'type', type: types_.ENUM, values: SystemSettingTypeValues }, + { name: 'successCallback', type: types_.FUNCTION }, + { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true } ]); - if (!SystemSettingType.hasOwnProperty(args.type)) - throw new tizen.WebAPIException(0, 'Invalid setting type', 'InvalidValuesError'); - var callback = function(result) { if (result.status === 'error') { if (!type_.isNullOrUndefined(args.errorCallback)) { @@ -60,6 +52,33 @@ SystemSettingManager.prototype.getProperty = function() { native_.call('SystemSettingManager_getProperty', callArgs, callback); }; +SystemSettingManager.prototype.setProperty = function() { + var args = validator_.validateArgs(arguments, [ + { name: 'type', type: types_.ENUM, values: SystemSettingTypeValues }, + { name: 'value', type: types_.STRING }, + { name: 'successCallback', type: types_.FUNCTION }, + { name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true } + ]); + + var callback = function(result) { + if (result.status === 'error') { + if (!type_.isNullOrUndefined(args.errorCallback)) { + args.errorCallback(result.error); + } + } + else { + args.successCallback(); + } + } + + var callArgs = { + type: args.type, + value: args.value + }; + + native_.call('SystemSettingManager_setProperty', callArgs, callback); +}; + // Exports exports = new SystemSettingManager(); diff --git a/src/systemsetting/systemsetting_instance.cc b/src/systemsetting/systemsetting_instance.cc index 6562b508..5fa053d2 100644 --- a/src/systemsetting/systemsetting_instance.cc +++ b/src/systemsetting/systemsetting_instance.cc @@ -35,6 +35,7 @@ SystemSettingInstance::SystemSettingInstance() RegisterHandler(c, std::bind(&SystemSettingInstance::x, this, _1, _2)); REGISTER("SystemSettingManager_getProperty", getProperty); + REGISTER("SystemSettingManager_setProperty", setProperty); #undef REGISTER } @@ -59,41 +60,41 @@ void SystemSettingInstance::getProperty(const picojson::value& args, picojson::o ReportSuccess(result, response->get()); } catch (const PlatformException& e) { ReportError(e, response->get()); - } - }; + } + }; auto get_response = [this, callback_id](const std::shared_ptr& response) -> void { LoggerD("Getting response"); picojson::object& obj = response->get(); obj.insert(std::make_pair("callbackId", callback_id)); PostMessage(response->serialize().c_str()); - }; + }; TaskQueue::GetInstance().Queue (get, get_response, std::shared_ptr(new picojson::value(picojson::object()))); } picojson::value SystemSettingInstance::getPlatformPropertyValue( - const std::string &valueType, int &platformResult) + const std::string &settingType, int &platformResult) { int ret; char *value = NULL; picojson::value result = picojson::value(picojson::object()); picojson::object& result_obj = result.get(); - if (valueType == SETTING_HOME_SCREEN) { + if (settingType == SETTING_HOME_SCREEN) { ret = system_settings_get_value_string( SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, &value); } - else if (valueType == SETTING_LOCK_SCREEN) { + else if (settingType == SETTING_LOCK_SCREEN) { ret = system_settings_get_value_string( SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, &value); } - else if (valueType == SETTING_INCOMING_CALL) { + else if (settingType == SETTING_INCOMING_CALL) { ret = system_settings_get_value_string( SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &value); } - else if (valueType == SETTING_NOTIFICATION_EMAIL) { + else if (settingType == SETTING_NOTIFICATION_EMAIL) { ret = system_settings_get_value_string( SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE, &value); } @@ -118,6 +119,81 @@ picojson::value SystemSettingInstance::getPlatformPropertyValue( return result; } +void SystemSettingInstance::setProperty(const picojson::value& args, picojson::object& out) +{ + LoggerD(""); + const double callback_id = args.get("callbackId").get(); + + const std::string& type = args.get("type").get(); + LoggerD("Type to set: %s ", type.c_str()); + + const std::string& value = args.get("value").get(); + LoggerD("Value to set: %s ", value.c_str()); + + auto get = [this, type, value](const std::shared_ptr& response) -> void { + LoggerD("Setting platform value"); + try { + int platformResult; + picojson::value result = setPlatformPropertyValue(type, value, platformResult); + ReportSuccess(result, response->get()); + } catch (const PlatformException& e) { + ReportError(e, response->get()); + } + }; + + auto get_response = [this, callback_id](const std::shared_ptr& response) -> void { + LoggerD("Getting response"); + picojson::object& obj = response->get(); + obj.insert(std::make_pair("callbackId", callback_id)); + PostMessage(response->serialize().c_str()); + }; + + TaskQueue::GetInstance().Queue + (get, get_response, std::shared_ptr(new picojson::value(picojson::object()))); +} + +picojson::value SystemSettingInstance::setPlatformPropertyValue( + const std::string &settingType, const std::string &settingValue, int &platformResult) +{ + int ret; + picojson::value result = picojson::value(picojson::object()); + picojson::object& result_obj = result.get(); + + if (settingType == SETTING_HOME_SCREEN) { + ret = system_settings_set_value_string( + SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, settingValue.c_str()); + } + else if (settingType == SETTING_LOCK_SCREEN) { + ret = system_settings_set_value_string( + SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, settingValue.c_str()); + } + else if (settingType == SETTING_INCOMING_CALL) { + ret = system_settings_set_value_string( + SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, settingValue.c_str()); + } + else if (settingType == SETTING_NOTIFICATION_EMAIL) { + ret = system_settings_set_value_string( + SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE, settingValue.c_str()); + } + // other values (not specified in the documentation) are handled in JS + + platformResult = ret; + + if (ret == SYSTEM_SETTINGS_ERROR_NONE) { + LoggerD("ret == SYSTEM_SETTINGS_ERROR_NONE"); + } + else if (ret == SYSTEM_SETTINGS_ERROR_CALL_UNSUPPORTED_API) { + LoggerD("ret == SYSTEM_SETTINGS_ERROR_CALL_UNSUPPORTED_API"); + throw NotSupportedException("This property is not supported."); + } + else { + LoggerD("Other error"); + throw UnknownException("Unknown error"); + } + + return result; +} + } // namespace systemsetting } // namespace extension diff --git a/src/systemsetting/systemsetting_instance.h b/src/systemsetting/systemsetting_instance.h index f4bcbf0e..d36d25a1 100644 --- a/src/systemsetting/systemsetting_instance.h +++ b/src/systemsetting/systemsetting_instance.h @@ -17,9 +17,12 @@ public: virtual ~SystemSettingInstance(); private: - void getProperty(const picojson::value& args, picojson::object& out); picojson::value getPlatformPropertyValue(const std::string &valueType, int &platformResult); + + void setProperty(const picojson::value& args, picojson::object& out); + picojson::value setPlatformPropertyValue(const std::string &settingType, + const std::string &settingValue, int &platformResult); }; } // namespace systemsetting -- 2.34.1