3 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <libsyscommon/ini-parser.h>
25 #include <libsyscommon/list.h>
28 #include "sound-theme-manager.h"
30 #define INITIAL_DEFAULT_SOUND_THEME_ID -1
31 static int default_sound_theme_id = INITIAL_DEFAULT_SOUND_THEME_ID;
33 static GHashTable *g_hash_table_to_check_id_duplicated;
35 struct sound_theme_info {
41 static bool is_sound_theme_id_duplicated(int sound_theme_id)
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)) {
50 g_hash_table_insert(g_hash_table_to_check_id_duplicated, hash_key, NULL);
54 static bool is_default_theme_id_set(void)
56 if (default_sound_theme_id == INITIAL_DEFAULT_SOUND_THEME_ID)
61 static int parse_sound_theme_property(gpointer data, gpointer user_data)
63 struct section_property *prop = (struct section_property *) data;
64 struct sound_theme_info *sound_theme_elem = (struct sound_theme_info *)user_data;
66 if (!prop || !sound_theme_elem)
69 if (MATCH(prop->key, "SoundThemeId")) {
70 sscanf(prop->value, "%d", (&sound_theme_elem->id));
71 if (sound_theme_elem->id < 0)
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;
88 static int parse_sound_theme_section(const struct parse_result *result, void *data)
90 struct sound_theme_info sound_theme_elem = {
93 .conf_file_path = NULL
95 struct section_property *extracted_section_prop = NULL;
96 GList *temp_glist = NULL;
99 if (!result || !result->props)
102 if (!MATCH("SoundTheme", result->section))
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;
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;
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;
120 default_sound_theme_id = sound_theme_elem.id;
121 sound_thememan_set_default_sound_theme_id(default_sound_theme_id);
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);
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);
139 int sound_parser_init(const char* sound_file_path)
143 g_hash_table_to_check_id_duplicated = g_hash_table_new_full(g_int_hash, g_int_equal,
146 ret = libsys_config_parse_by_section(sound_file_path, parse_sound_theme_section, NULL);
148 if (!is_default_theme_id_set()) {
149 _E("Failed to parse sound conf file. There is no default sound theme id");
153 if (g_hash_table_to_check_id_duplicated)
154 g_hash_table_destroy(g_steal_pointer(&g_hash_table_to_check_id_duplicated));
162 void sound_parser_exit(void)
164 default_sound_theme_id = INITIAL_DEFAULT_SOUND_THEME_ID;