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