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