2 * Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <json-glib/json-glib.h>
27 static void _json_save_ringtone(JsonNode *root)
29 JsonGenerator *generator = json_generator_new();
30 json_generator_set_root(generator, root);
31 gchar *data = json_generator_to_data(generator, NULL);
32 DBG("ringtone json : %s", data);
34 const char *key = VCONFKEY_SETAPPL_CALL_RINGTONE_USER_LIST;
35 if (vconf_set_str(key, data))
36 ERR("vconf_set_str(%s) Fail", key);
38 g_object_unref(generator);
41 sst_json_h* sst_json_load_ringtone()
44 sst_json_h *handle = calloc(1, sizeof(struct sst_json_s));
49 char *load_data = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_USER_LIST);
51 handle->parser = json_parser_new();
52 json_parser_load_from_data(handle->parser, load_data, -1, &error);
55 ERR("json_parser_load_from_data() Fail(%s)", error->message);
57 g_object_unref(handle->parser);
61 handle->root = json_parser_get_root(handle->parser);
66 void sst_json_unref(sst_json_h *handle)
68 g_object_unref(handle->parser);
72 void sst_json_add_ringtone(sst_json_h *handle, char *nameval, char *pathval)
74 JsonNode *root = handle->root;
75 JsonNode *menu_item = json_node_new(JSON_NODE_OBJECT);
76 JsonObject *object = json_object_new();
77 json_node_take_object(menu_item, object);
78 json_object_set_string_member(object, "name", nameval);
79 json_object_set_string_member(object, "path", pathval);
81 JsonArray *menu = json_node_get_array(root);
82 json_array_add_element(menu, menu_item);
84 _json_save_ringtone(root);
87 void sst_json_get_ringtones(sst_json_h *handle, system_settings_iter_cb cb, void *user_data)
92 RET_IF(NULL == handle);
94 JsonArray *ringtone_list = json_node_get_array(handle->root);
95 int size = json_array_get_length(ringtone_list);
96 for (i = 0; i < size; i++) {
97 JsonObject *ringtone = json_array_get_object_element(ringtone_list, i);
98 //const char *name = json_object_get_string_member(ringtone, "name");
99 const char *path = json_object_get_string_member(ringtone, "path");
103 if (false == cb(i, path, user_data)) {
104 ERR("callback return false");
110 void sst_json_remove_ringtone(sst_json_h *handle, char *path_to_del)
114 RET_IF(NULL == handle);
115 RET_IF(NULL == path_to_del);
117 JsonNode *root = handle->root;
118 int size = json_array_get_length(json_node_get_array(root));
119 DBG("size(%d)", size);
120 for (i = 0; i < size; i++) {
121 JsonObject *ringtone = json_array_get_object_element(json_node_get_array(root), i);
122 //char *nameval = (char*)json_object_get_string_member(ringtone, "name");
123 char *pathval = (char*)json_object_get_string_member(ringtone, "path");
125 if (pathval && SST_EQUAL == strcmp(pathval, path_to_del)) {
126 json_array_remove_element(json_node_get_array(root), i);
127 DBG("removed(%s)", pathval);
131 _json_save_ringtone(root);
134 bool sst_json_contain_ringtone(sst_json_h *handle, char *ringtone_path)
138 RETV_IF(NULL == handle, false);
139 RETV_IF(NULL == ringtone_path, false);
141 JsonNode *root = handle->root;
142 int size = json_array_get_length(json_node_get_array(root));
143 for (i = 0; i < size; i++) {
144 JsonObject *ringtone = json_array_get_object_element(json_node_get_array(root), i);
145 //char *nameval = (char*)json_object_get_string_member(ringtone, "name");
146 char *pathval = (char*)json_object_get_string_member(ringtone, "path");
148 if (pathval && SST_EQUAL == strcmp(pathval, ringtone_path))