[refactoring] separate big files
[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 <vconf.h>
17
18 #include "sst.h"
19 #include "sst_core.h"
20 #include "sst_json.h"
21
22 static void ss_json_ringtone_save(JsonNode *root)
23 {
24         // write here
25         JsonGenerator *generator = json_generator_new();
26         json_generator_set_root(generator, root);
27         json_generator_set_pretty(generator, TRUE);
28 #ifdef USE_JSONFILE
29         GError *error = NULL;
30         gboolean ret = json_generator_to_file(generator, filename, &error);
31 #else
32         gchar *result = json_generator_to_data(generator, NULL);
33         vconf_set_str(VCONFKEY_SETAPPL_CALL_RINGTONE_USER_LIST, (char*)result);
34 #endif
35         g_object_unref(generator);
36 }
37
38 JsonParser* ss_json_ringtone_open_file(char *path)
39 {
40         JsonParser *parser;
41         //JsonNode *root;
42         GError *error;
43
44         parser = json_parser_new();
45
46         error = NULL;
47         json_parser_load_from_file(parser, path, &error);
48         if (error) {
49                 SST_SECURE_TRACE("Unable to parse `%s': %s\n", path, error->message);
50                 g_error_free(error);
51                 g_object_unref(parser);
52                 return NULL;
53         }
54
55         return parser;
56 }
57
58 JsonParser* ss_json_ringtone_load_from_data()
59 {
60         JsonParser *parser;
61         //JsonNode *root;
62         GError *error;
63
64         parser = json_parser_new();
65
66         error = NULL;
67         char *load_data = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_USER_LIST);
68         json_parser_load_from_data(parser, load_data, -1, &error);
69         if (error) {
70                 SST_SECURE_TRACE("Unable to load data : %s \n", error->message);
71                 g_error_free(error);
72                 g_object_unref(parser);
73                 return NULL;
74         }
75         return parser;
76 }
77
78 void ss_json_ringtone_add(JsonNode *root, char *filename, char *nameval, char *pathval)
79 {
80         JsonNode *menu_item = json_node_new(JSON_NODE_OBJECT);
81         JsonObject *object = json_object_new();
82         json_node_take_object(menu_item, object);
83         json_object_set_string_member(object, "name", nameval);
84         json_object_set_string_member(object, "path", pathval);
85
86         JsonArray *menu = json_node_get_array(root);
87         json_array_add_element(menu, menu_item);
88
89         ss_json_ringtone_save(root);
90 }
91
92 void ss_json_ringtone_print(JsonNode *root)
93 {
94         JsonNode *node = root;
95
96         JsonGenerator *generator = json_generator_new();
97         json_generator_set_root(generator, node);
98         json_generator_set_pretty(generator, TRUE);
99         gchar *data = json_generator_to_data(generator, NULL);
100         //SETTING_TRACE("%s", (char * )data);
101         SST_SECURE_TRACE("-------------------------------------------------------\n");
102         SST_SECURE_TRACE("%s", data);
103         SST_SECURE_TRACE("-------------------------------------------------------\n");
104
105         SST_SECURE_TRACE("VCONFKEY_SETAPPL_CALL_RINGTONE_USER_LIST SET !!!!\n");
106         vconf_set_str(VCONFKEY_SETAPPL_CALL_RINGTONE_USER_LIST, (char*)data);
107         g_free(data);
108         g_object_unref(generator);
109 }
110
111 void ss_json_ringtone_remove(JsonNode *root, char *filename, char *path_to_del)
112 {
113         int size = json_array_get_length(json_node_get_array(root));
114
115         SST_SECURE_TRACE("BBB size : (%d) \n", size);
116         int i = 0;
117         for (i = 0; i < size; i++) {
118                 JsonObject *ringtone = json_array_get_object_element(json_node_get_array(root), i);
119                 //char *nameval = (char*)json_object_get_string_member(ringtone, "name");
120                 char *pathval = (char*)json_object_get_string_member(ringtone, "path");
121
122                 if (pathval && !strcmp(pathval, path_to_del)) {
123                         json_array_remove_element(json_node_get_array(root), i);
124                         SST_SECURE_TRACE("remove BBB : (%s) --- (%s) \n", pathval, path_to_del);
125                 }
126         }
127
128         ss_json_ringtone_save(root);
129 }
130
131 bool ss_json_ringtone_contain(JsonNode *root, char *newfile)
132 {
133         int size = json_array_get_length(json_node_get_array(root));
134
135         bool ret = false;
136
137         int i = 0;
138         for (i = 0; i < size; i++) {
139                 JsonObject *ringtone = json_array_get_object_element(json_node_get_array(root), i);
140                 //char *nameval = (char*)json_object_get_string_member(ringtone, "name");
141                 char *pathval = (char*)json_object_get_string_member(ringtone, "path");
142                 //SETTING_TRACE("(%s) --- (%s) \n", name, path);
143
144                 if (pathval && !strcmp(pathval, newfile)) {
145                         //SETTING_TRACE("FOUND\n");
146                         ret = true;
147                         break;
148                 } else {
149                         //SETTING_TRACE("*NOT* FOUND\n");
150                         ret = false;
151                 }
152         }
153
154         return ret;
155 }
156
157 void ss_json_ringtone_list(JsonNode *root)
158 {
159         int size = json_array_get_length(json_node_get_array(root));
160
161         int i = 0;
162         for (i = 0; i < size; i++) {
163                 JsonObject *ringtone = json_array_get_object_element(json_node_get_array(root), i);
164                 char *name = (char*)json_object_get_string_member(ringtone, "name");
165                 char *path = (char*)json_object_get_string_member(ringtone, "path");
166                 SST_SECURE_TRACE("(%s) --- (%s) \n", name, path);
167         }
168 }