sound: Fix checking default theme duplication
[platform/core/system/libsvi.git] / src / sound-parser.c
1 /*
2  * libfeedback
3  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <glib.h>
19 #include <stdbool.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23
24 #include <libsyscommon/ini-parser.h>
25 #include <libsyscommon/list.h>
26
27 #include "log.h"
28 #include "sound-theme-manager.h"
29
30 #define INITIAL_DEFAULT_SOUND_THEME_ID                          -1
31 static int default_sound_theme_id = INITIAL_DEFAULT_SOUND_THEME_ID;
32
33 static GHashTable *g_hash_table_to_check_id_duplicated;
34
35 struct sound_theme_info {
36         int id;
37         int is_default;
38         char *conf_file_path;
39 };
40
41 static bool is_sound_theme_id_duplicated(int sound_theme_id)
42 {
43         gint *hash_key = g_new(gint, 1);
44         *hash_key = sound_theme_id;
45         if (g_hash_table_contains(g_hash_table_to_check_id_duplicated, hash_key)) {
46                 g_free(hash_key);
47                 return true;
48         }
49
50         g_hash_table_insert(g_hash_table_to_check_id_duplicated, hash_key, NULL);
51         return false;
52 }
53
54 static bool is_default_theme_id_set(void)
55 {
56         if (default_sound_theme_id == INITIAL_DEFAULT_SOUND_THEME_ID)
57                 return false;
58         return true;
59 }
60
61 static int parse_sound_theme_property(gpointer data, gpointer user_data)
62 {
63         struct section_property *prop = (struct section_property *) data;
64         struct sound_theme_info *sound_theme_elem = (struct sound_theme_info *)user_data;
65
66         if (!prop || !sound_theme_elem)
67                 return 0;
68
69         if (MATCH(prop->key, "SoundThemeId")) {
70                 sscanf(prop->value, "%d", (&sound_theme_elem->id));
71                 if (sound_theme_elem->id < 0)
72                         return -EPERM;
73         } else if (MATCH(prop->key, "SoundThemePath")) {
74                 int str_len = sizeof(prop->value);
75                 sound_theme_elem->conf_file_path = strndup(prop->value, str_len);
76         } else if (MATCH(prop->key, "SoundThemeDefault")) {
77                 if (MATCH(prop->value, "yes"))
78                         sound_theme_elem->is_default = 1;
79                 else if (MATCH(prop->value, "no"))
80                         sound_theme_elem->is_default = 0;
81                 else
82                         return -EPERM;
83         }
84         return 0;
85 }
86
87
88 static int parse_sound_theme_section(const struct parse_result *result, void *data)
89 {
90         struct sound_theme_info sound_theme_elem = {
91                 .id = 0,
92                 .is_default = 0,
93                 .conf_file_path = NULL
94         };
95         struct section_property *extracted_section_prop = NULL;
96         GList *temp_glist = NULL;
97         int ret = 0;
98
99         if (!result || !result->props)
100                 return 0;
101
102         if (!MATCH("SoundTheme", result->section))
103                 return 0;
104
105         SYS_G_LIST_FOREACH(result->props, temp_glist, extracted_section_prop) {
106                 if (parse_sound_theme_property(extracted_section_prop, &sound_theme_elem) < 0)
107                         goto out_parsing_fail;
108         }
109
110         if (is_sound_theme_id_duplicated(sound_theme_elem.id)) {
111                 _E("Failed to parse sound conf file, sound theme id is duplicated.");
112                 goto out_parsing_fail;
113         }
114
115         if (sound_theme_elem.is_default) {
116                 if (is_default_theme_id_set()) {
117                         _E("Failed to parse sound conf file, default sound theme is duplicated.");
118                         goto out_parsing_fail;
119                 }
120                 default_sound_theme_id = sound_theme_elem.id;
121                 sound_thememan_set_default_sound_theme_id(default_sound_theme_id);
122         }
123
124         ret = sound_thememan_add_sound_theme(sound_theme_elem.id, sound_theme_elem.conf_file_path);
125         free(sound_theme_elem.conf_file_path);
126
127         if (ret < 0)
128                 return ret;
129
130         return 0;
131
132 out_parsing_fail:
133         _E("Failed to parse sound conf file, please check conf file description and follow the rules");
134         free(sound_theme_elem.conf_file_path);
135
136         return -EPERM;
137 }
138
139 int sound_parser_init(const char* sound_file_path)
140 {
141         int ret = 0;
142
143         g_hash_table_to_check_id_duplicated = g_hash_table_new_full(g_int_hash, g_int_equal,
144                                                                         g_free, g_free);
145
146         ret = libsys_config_parse_by_section(sound_file_path, parse_sound_theme_section, NULL);
147
148         if (!is_default_theme_id_set()) {
149                 _E("Failed to parse sound conf file. There is no default sound theme id");
150                 ret = -EPERM;
151         }
152
153         if (g_hash_table_to_check_id_duplicated)
154                 g_hash_table_destroy(g_steal_pointer(&g_hash_table_to_check_id_duplicated));
155
156         if (ret < 0)
157                 return ret;
158
159         return 0;
160 }
161
162 void sound_parser_exit(void)
163 {
164         default_sound_theme_id = INITIAL_DEFAULT_SOUND_THEME_ID;
165 }