feedback: Change feedback pattern data from list to GHashTable
[platform/core/system/libsvi.git] / src / sound-theme-manager.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
20 #include <libsyscommon/list.h>
21
22 #include "feedback-config.h"
23 #include "profiles.h"
24 #include "log.h"
25 #include "sound-parser.h"
26 #include "sound-theme-manager.h"
27
28 #define SOUND_NAME                              "Sound"
29
30 struct sound_theme_element {
31         unsigned int id;
32         GHashTable *sound_info;
33 };
34
35 static unsigned int default_sound_theme_id;
36 static GList *g_sound_theme_list;
37
38 bool sound_thememan_is_sound_theme_id_exist(unsigned int sound_theme_id)
39 {
40         struct sound_theme_element *sound_theme_elem = NULL;
41         GList *temp_glist = NULL;
42
43         SYS_G_LIST_FOREACH(g_sound_theme_list, temp_glist, sound_theme_elem) {
44                 if (sound_theme_elem->id == sound_theme_id) {
45                         return true;
46                 }
47         }
48
49         return false;
50 }
51
52 void sound_thememan_set_default_sound_theme_id(unsigned int sound_theme_id)
53 {
54         default_sound_theme_id = sound_theme_id;
55 }
56
57 void sound_thememan_get_default_sound_theme_id(unsigned int *sound_theme_id)
58 {
59         *sound_theme_id = default_sound_theme_id;
60 }
61
62 int sound_thememan_get_number_of_theme(unsigned int *number_of_theme)
63 {
64         if (!number_of_theme) {
65                 _E("Invalid parameter: number_of_theme(NULL)");
66                 return -EINVAL;
67         }
68
69         *number_of_theme = (unsigned int)g_list_length(g_sound_theme_list);
70         return 0;
71 }
72
73 const char* sound_thememan_get_pattern_sound_path(unsigned int sound_theme_id, feedback_pattern_e pattern)
74 {
75         struct sound_theme_element *sound_theme_elem = NULL;
76         GList *temp_glist = NULL;
77
78         SYS_G_LIST_FOREACH(g_sound_theme_list, temp_glist, sound_theme_elem) {
79                 if (sound_theme_elem->id == sound_theme_id)
80                         return g_hash_table_lookup(sound_theme_elem->sound_info, GINT_TO_POINTER(pattern));
81         }
82
83         return NULL;
84 }
85
86 int sound_thememan_add_sound_theme(unsigned int sound_theme_id, const char* conf_file_path)
87 {
88         struct sound_theme_element *sound_theme_elem = NULL;
89         int ret = 0;
90         GHashTable *sound_info = g_hash_table_new_full(g_direct_hash, g_direct_equal,
91                                                                 NULL, g_free);
92
93         if (!sound_info) {
94                 _E("Failed to allocate memory for sound_info.");
95                 return -ENOMEM;
96         }
97
98         sound_theme_elem = (struct sound_theme_element*)calloc(1, sizeof(struct sound_theme_element));
99         if (!sound_theme_elem) {
100                 _E("Failed to allocate memory for sound_theme_elem.");
101                 ret = -ENOMEM;
102                 goto out_add_sound_theme_fail;
103         }
104
105         ret = sound_parser_get_config_info(conf_file_path, sound_info);
106         if (ret < 0) {
107                 _E("Failed to load config file %s", conf_file_path);
108                 goto out_add_sound_theme_fail;
109         }
110
111         sound_theme_elem->id = sound_theme_id;
112         sound_theme_elem->sound_info = sound_info;
113         g_sound_theme_list = g_list_append(g_sound_theme_list, sound_theme_elem);
114         return 0;
115
116 out_add_sound_theme_fail:
117         if (sound_info)
118                 g_hash_table_destroy(g_steal_pointer(&sound_info));
119
120         if (sound_theme_elem)
121                 free(sound_theme_elem);
122
123         return ret;
124 }
125
126 int sound_thememan_get_sound_theme_ids(unsigned int *count_of_theme, unsigned int **sound_theme_ids)
127 {
128         int index = 0;
129         unsigned int *temp_sound_theme_ids = NULL;
130         struct sound_theme_element* sound_theme_elem = NULL;
131         GList *temp_glist = NULL;
132
133         sound_thememan_get_number_of_theme(count_of_theme);
134
135         temp_sound_theme_ids = calloc(*count_of_theme, sizeof(unsigned int));
136         if (!temp_sound_theme_ids)
137                 return -ENOMEM;
138
139         SYS_G_LIST_FOREACH(g_sound_theme_list, temp_glist, sound_theme_elem) {
140                 temp_sound_theme_ids[index++] = sound_theme_elem->id;
141         }
142
143         *sound_theme_ids = g_steal_pointer(&temp_sound_theme_ids);
144         return 0;
145 }
146
147 static void cleanup_sound_theme_element(gpointer data)
148 {
149         struct sound_theme_element *sound_theme_elem = NULL;
150
151         if (!data)
152                 return;
153
154         sound_theme_elem = data;
155         if (sound_theme_elem->sound_info)
156                 g_hash_table_destroy(g_steal_pointer(&sound_theme_elem->sound_info));
157
158         free(sound_theme_elem);
159 }
160
161 int sound_thememan_init(void)
162 {
163         g_sound_theme_list = NULL;
164         return 0;
165 }
166
167 void sound_thememan_exit(void)
168 {
169         if (g_sound_theme_list)
170                 g_list_free_full(g_steal_pointer(&g_sound_theme_list), cleanup_sound_theme_element);
171 }
172