b376d6c51995bbc6bf0e845f175acb764c7e35ba
[platform/core/api/system-settings.git] / src / sst_json.c
1 /*
2  * Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include "sst_json.h"
17
18 #include <json-glib/json-glib.h>
19 #include <vconf.h>
20 #include "sst.h"
21
22 struct sst_json_s {
23         JsonParser *parser;
24         JsonNode *root;
25 };
26
27 static void _json_save_ringtone(JsonNode *root)
28 {
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);
33
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);
37         g_free(data);
38         g_object_unref(generator);
39 }
40
41 sst_json_h* sst_json_load_ringtone()
42 {
43         GError *error = NULL;
44         sst_json_h *handle = calloc(1, sizeof(struct sst_json_s));
45         if (NULL == handle) {
46                 ERR("calloc() Fail");
47                 return NULL;
48         }
49         char *load_data = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_USER_LIST);
50
51         handle->parser = json_parser_new();
52         json_parser_load_from_data(handle->parser, load_data, -1, &error);
53         free(load_data);
54         if (error) {
55                 ERR("json_parser_load_from_data() Fail(%s)", error->message);
56                 g_error_free(error);
57                 g_object_unref(handle->parser);
58                 free(handle);
59                 return NULL;
60         }
61         handle->root = json_parser_get_root(handle->parser);
62
63         return handle;
64 }
65
66 void sst_json_unref(sst_json_h *handle)
67 {
68         g_object_unref(handle->parser);
69         free(handle);
70 }
71
72 void sst_json_add_ringtone(sst_json_h *handle, char *nameval, char *pathval)
73 {
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);
80
81         JsonArray *menu = json_node_get_array(root);
82         json_array_add_element(menu, menu_item);
83
84         _json_save_ringtone(root);
85 }
86
87 void sst_json_get_ringtones(sst_json_h *handle, system_settings_iter_cb cb, void *user_data)
88 {
89         int i;
90
91         RET_IF(NULL == cb);
92         RET_IF(NULL == handle);
93
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");
100                 if (NULL == path)
101                         continue;
102
103                 if (false == cb(i, path, user_data)) {
104                         ERR("callback return false");
105                         break;
106                 }
107         }
108 }
109
110 void sst_json_remove_ringtone(sst_json_h *handle, char *path_to_del)
111 {
112         int i;
113
114         RET_IF(NULL == handle);
115         RET_IF(NULL == path_to_del);
116
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");
124
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);
128                 }
129         }
130
131         _json_save_ringtone(root);
132 }
133
134 bool sst_json_contain_ringtone(sst_json_h *handle, char *ringtone_path)
135 {
136         int i;
137
138         RETV_IF(NULL == handle, false);
139         RETV_IF(NULL == ringtone_path, false);
140
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");
147
148                 if (pathval && SST_EQUAL == strcmp(pathval, ringtone_path))
149                         return true;
150         }
151
152         return false;
153 }