[system-setting] setProperty() method added
authorBeata Stefaniuk <b.stefaniuk@samsung.com>
Thu, 18 Dec 2014 15:54:00 +0000 (16:54 +0100)
committerJakub Siewierski <j.siewierski@samsung.com>
Wed, 28 Jan 2015 09:39:06 +0000 (10:39 +0100)
Change-Id: Ib6730c350a98e35d32e03cf88d75c0200d48a565
Signed-off-by: Beata Stefaniuk <b.stefaniuk@samsung.com>
Signed-off-by: Jakub Siewierski <j.siewierski@samsung.com>
src/systemsetting/systemsetting_api.js
src/systemsetting/systemsetting_instance.cc
src/systemsetting/systemsetting_instance.h

index e06da487ec5e4857331021ee4ed99ed40cb427f0..ed6d6084aa2cd1749eb95aaee53226c748c97da0 100644 (file)
@@ -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();
 
index 6562b50873acf538ba56a718b0dfbd45e92a065d..5fa053d219eb1c583fd703dbfd9a51fb09248490 100644 (file)
@@ -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<picojson::object>());
         } catch (const PlatformException& e) {
             ReportError(e, response->get<picojson::object>());
-        }   
-    };  
+        }
+    };
 
     auto get_response = [this, callback_id](const std::shared_ptr<picojson::value>& response) -> void {
         LoggerD("Getting response");
         picojson::object& obj = response->get<picojson::object>();
         obj.insert(std::make_pair("callbackId", callback_id));
         PostMessage(response->serialize().c_str());
-    };  
+    };
 
     TaskQueue::GetInstance().Queue<picojson::value>
         (get, get_response, std::shared_ptr<picojson::value>(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<picojson::object>();
 
-    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<double>();
+
+    const std::string& type = args.get("type").get<std::string>();
+    LoggerD("Type to set: %s ", type.c_str());
+
+    const std::string& value = args.get("value").get<std::string>();
+    LoggerD("Value to set: %s ", value.c_str());
+
+    auto get = [this, type, value](const std::shared_ptr<picojson::value>& response) -> void {
+        LoggerD("Setting platform value");
+        try {
+            int platformResult;
+            picojson::value result = setPlatformPropertyValue(type, value, platformResult);
+            ReportSuccess(result, response->get<picojson::object>());
+        } catch (const PlatformException& e) {
+            ReportError(e, response->get<picojson::object>());
+        }
+    };
+
+    auto get_response = [this, callback_id](const std::shared_ptr<picojson::value>& response) -> void {
+        LoggerD("Getting response");
+        picojson::object& obj = response->get<picojson::object>();
+        obj.insert(std::make_pair("callbackId", callback_id));
+        PostMessage(response->serialize().c_str());
+    };
+
+    TaskQueue::GetInstance().Queue<picojson::value>
+        (get, get_response, std::shared_ptr<picojson::value>(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<picojson::object>();
+
+    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
index f4bcbf0ec52a5f77dff2e614ad9d44f2ec9979e4..d36d25a1409bec2d064e5bd7845eb0d97c8708f3 100644 (file)
@@ -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