2 * Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "sst_sound.h"
23 #include <metadata_extractor.h>
26 #include "sst_vconf.h"
28 #include "sst_interface.h"
30 typedef struct _file_Info {
35 static char* get_media_basename(const char *dir_path, const char *name)
37 RETV_IF(NULL == name, NULL);
38 RETV_IF(NULL == dir_path, NULL);
41 snprintf(path, sizeof(path), "%s/%s", dir_path, name);
43 metadata_extractor_h metadata = NULL;
44 int ret = metadata_extractor_create(&metadata);
45 if (ret != METADATA_EXTRACTOR_ERROR_NONE || NULL == metadata) {
46 ERR("metadata_extractor_create() Fail(%d)", ret);
50 ret = metadata_extractor_set_path(metadata, path);
51 if (METADATA_EXTRACTOR_ERROR_NONE != ret) {
52 ERR("metadata_extractor_set_path(%s) Fail(%d)", path, ret);
53 metadata_extractor_destroy(metadata);
58 ret = metadata_extractor_get_metadata(metadata, METADATA_TITLE, &title);
59 metadata_extractor_destroy(metadata);
66 static int _get_filelist_in_dir(const char *path, GList **file_list)
70 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
72 RETV_IF(NULL == path, -1);
73 RETV_IF(NULL == file_list, -1);
77 ERR("opendir(%s) Fail(%d)", path, errno);
81 pthread_mutex_lock(&mutex);
82 while ((ent = readdir(dir)) != NULL) {
83 if (strncmp(ent->d_name, ".", 1) == 0 || strcmp(ent->d_name, "..") == 0)
86 if ((ent->d_type & DT_REG) == 0)
89 sst_file_info *info = calloc(1, sizeof(sst_file_info));
91 ERR("calloc() Fail(%d)", errno);
95 info->name = strdup(ent->d_name);
96 info->media_name = get_media_basename(path, info->name);
98 *file_list = g_list_append(*file_list, info);
100 pthread_mutex_unlock(&mutex);
106 static gint _ringtone_compare_cb(gconstpointer a, gconstpointer b)
108 const sst_file_info *info1 = a;
109 const sst_file_info *info2 = b;
111 return strcmp(info1->media_name, info2->media_name);
114 static void _get_default_ringtones(system_settings_iter_cb callback, void *data)
116 RET_IF(NULL == callback);
118 GList *filelist = NULL;
122 const char *ringtone_dir = SST_DEFAULT_RINGTONE_DIR;
123 int ret = _get_filelist_in_dir(ringtone_dir, &filelist);
125 ERR("_get_filelist_in_dir(%s) Fail(%d)", ringtone_dir, ret);
129 filelist = g_list_sort(filelist, _ringtone_compare_cb);
132 for (cur = filelist; cur; cur = cur->next) {
135 char fullpath[PATH_MAX];
136 snprintf(fullpath, sizeof(fullpath), "%s/%s", ringtone_dir, info->name);
137 bool ret = callback(idx, fullpath, data);
139 ERR("callback return false");
144 for (cur = filelist; cur; cur = cur->next) {
147 free(info->media_name);
150 g_list_free(filelist);
153 static void _get_user_ringtones(system_settings_iter_cb callback, void *user_data)
155 RET_IF(NULL == callback);
157 sst_json_h *handle = sst_json_load_ringtone();
158 sst_json_get_ringtones(handle, callback, user_data);
159 sst_json_unref(handle);
161 int sst_sound_add_call_ringtone(system_settings_key_e key, const char *value)
163 const char *path = value;
164 sst_json_h *handle = sst_json_load_ringtone();
166 if (sst_json_contain_ringtone(handle, path)) {
167 ERR("ringtone(%s) is duplicated", path);
168 sst_json_unref(handle);
169 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
172 if (false == sst_misc_file_exists(path)) {
173 ERR("sst_misc_file_exists(%s) Fail", path);
174 sst_json_unref(handle);
175 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
178 sst_json_add_ringtone(handle, path, path);
179 sst_json_unref(handle);
181 return SYSTEM_SETTINGS_ERROR_NONE;
184 int sst_sound_del_call_ringtone(system_settings_key_e key, const char *value)
186 sst_json_h *handle = sst_json_load_ringtone();
187 sst_json_remove_ringtone(handle, value);
188 sst_json_unref(handle);
190 return SYSTEM_SETTINGS_ERROR_NONE;
193 int sst_sound_get_call_ringtone_list(system_settings_key_e key, system_settings_iter_cb callback, void *data)
195 RETV_IF(NULL == callback, SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER);
197 _get_default_ringtones(callback, data);
198 _get_user_ringtones(callback, data);
200 return SYSTEM_SETTINGS_ERROR_NONE;
203 int sst_sound_set_call_ringtone(sst_interface *iface, const char *value)
205 bool is_valid = sst_misc_file_exists(value);
206 if (false == is_valid) {
207 ERR("sst_misc_file_exists(%s) Fail", value);
208 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
211 if (vconf_set_str(iface->vconf_key, value)) {
212 ERR("vconf_set_str(%s, %s) Fail", iface->vconf_key, value);
213 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
216 return SYSTEM_SETTINGS_ERROR_NONE;
219 int sst_sound_set_email_alert(sst_interface *iface, const char *value)
221 bool is_valid = sst_misc_file_exists(value);
222 if (false == is_valid) {
223 ERR("sst_misc_file_exists(%s) Fail", value);
224 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
227 if (vconf_set_str(iface->vconf_key, value)) {
228 ERR("vconf_set_str(%s, %s) Fail", iface->vconf_key, value);
229 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
232 return SYSTEM_SETTINGS_ERROR_NONE;
236 * VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR has a path of the ringtone file which user choose
237 * @return the ringtone file path specified by user in normal case
238 * if it's not accessable, return the default ringtone path
240 int sst_sound_get_call_ringtone(sst_interface *iface, char **value)
242 char *vconf_value = NULL;
243 if (sst_vconf_get_str_ally(iface->vconf_key, &vconf_value))
244 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
246 /* check to see if it's accessable -> OK */
247 /* no --> default ringtone path VCONFKEY_SETAPPL_CALL_RINGTONE_DEFAULT_PATH_STR */
248 bool is_valid = sst_misc_file_exists(vconf_value);
249 if (true == is_valid) {
250 *value = vconf_value;
252 *value = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_DEFAULT_PATH_STR);
256 return SYSTEM_SETTINGS_ERROR_NONE;
259 int sst_sound_get_email_alert(sst_interface *iface, char **value)
261 char *vconf_value = NULL;
262 if (sst_vconf_get_str_ally(iface->vconf_key, &vconf_value))
263 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
265 /* check to see if it's accessable -> OK */
266 /* no --> default ringtone path VCONFKEY_SETAPPL_NOTI_RINGTONE_DEFAULT_PATH_STR */
267 bool is_valid = sst_misc_file_exists(vconf_value);
268 if (true == is_valid) {
269 *value = vconf_value;
271 *value = vconf_get_str(VCONFKEY_SETAPPL_NOTI_RINGTONE_DEFAULT_PATH_STR);
275 return SYSTEM_SETTINGS_ERROR_NONE;
279 * sound == false, vibration == false --> silent mode
280 * sound == true, vibration == false --> sound mode
281 * sound == false, vibration == true --> vibration mode
283 int sst_sound_get_silent_mode(sst_interface *iface, bool *value)
285 int sound_status = 0;
286 if (VCONF_OK != vconf_get_bool(iface->vconf_key, &sound_status)) {
287 ERR("vconf_get_bool(%s) Fail", iface->vconf_key);
288 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
291 const char *vibration_key = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
293 if (VCONF_OK != vconf_get_bool(vibration_key, &vib_status)) {
294 ERR("vconf_get_bool(%s) Fail", vibration_key);
295 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
298 bool result = (sound_status == FALSE && vib_status == FALSE);
301 return SYSTEM_SETTINGS_ERROR_NONE;
305 * sound == false, vibration == false --> silent mode
306 * sound == true, vibration == false --> sound mode
308 //Todo: It should return to the old status.
309 int sst_sound_set_silent_mode(sst_interface *iface, bool value)
311 int sound_state = !value;
312 if (vconf_set_bool(iface->vconf_key, sound_state)) {
313 ERR("vconf_set_bool(%s, %d) Fail", iface->vconf_key, sound_state);
314 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
317 int vib_state = FALSE;
318 const char *vibration_key = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
319 if (vconf_set_bool(vibration_key, vib_state)) {
320 ERR("vconf_set_bool(%s) Fail", vibration_key);
321 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
324 return SYSTEM_SETTINGS_ERROR_NONE;
327 int sst_sound_set_notification(sst_interface *iface, const char *value)
329 bool is_valid = sst_misc_file_exists(value);
330 if (true == is_valid) {
331 if (vconf_set_str(iface->vconf_key, value)) {
332 ERR("vconf_set_str(%s, %s) Fail", iface->vconf_key, value);
333 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
336 ERR("sst_misc_file_exists(%s) Fail", value);
337 return SYSTEM_SETTINGS_ERROR_IO_ERROR;
340 return SYSTEM_SETTINGS_ERROR_NONE;