5cd5bd4c42c9b7839593e816bbaf824144f5b649
[platform/core/api/system-settings.git] / src / system_settings_ringtones.c
1 /*
2  * system_settings_ringtones.c
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <pthread.h>
22
23 #include <system_settings.h>
24 #include <system_settings_private.h>
25 #include "system_settings_ringtones.h"
26 #include "system_settings_json.h"
27
28 /* LCOV_EXCL_START */
29 /*remove ext name */
30 char *get_filename_from_fullname(const char *fullname)
31 {
32         //retvm_if(fullname == NULL, NULL, "fullname == NULL");
33         if (fullname == NULL) return NULL;
34
35         char tmp[512];
36         snprintf(tmp, sizeof(tmp), "%s", fullname);
37
38         char *name = strrchr(tmp, '.');
39         if (name != NULL)
40                 *name = '\0';
41
42         return strdup((char*)tmp);
43 }
44 /* LCOV_EXCL_STOP */
45
46 char *get_media_basename(const char *dir_path, const char *name)
47 {
48         //retv_if(isEmptyStr(dir_path) || isEmptyStr(name), NULL);
49         if (dir_path == NULL) return NULL;
50
51         char path[512] = {0, };
52         snprintf(path, sizeof(path), "%s/%s", dir_path, name);
53
54         metadata_extractor_h metadata = NULL;
55         char *title = NULL;
56         int ret = metadata_extractor_create(&metadata);
57         if (ret == METADATA_EXTRACTOR_ERROR_NONE && metadata) {
58                 ret = metadata_extractor_set_path(metadata, path);
59                 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
60                         ret = metadata_extractor_get_metadata(
61                                         metadata, METADATA_TITLE, &title);
62                         metadata_extractor_destroy(metadata);
63                         if (title)
64                                 /*return (char *)g_strdup(title);*/
65                                 return (char *)title;
66                         else
67                                 return strdup(name);
68                 } else {
69                         metadata_extractor_destroy(metadata);
70                         return strdup(name);
71                 }
72         } else {
73                 return strdup(name);
74         }
75 }
76
77 int get_filelist_from_dir_path(char *path, Eina_List **file_list)
78 {
79         SETTING_TRACE_BEGIN;
80         DIR *pDir = NULL;
81         struct dirent *ent;
82         static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
83
84         if (path == NULL) {
85                 SETTING_TRACE("dir path is null");
86                 return -1;
87         }
88
89         if (file_list == NULL) {
90                 SETTING_TRACE("file_list is null");
91                 return -1;
92         }
93
94         pDir = opendir(path);
95
96         if (pDir == NULL)
97                 return -2;
98
99         pthread_mutex_lock(&mutex);
100         while ((ent = readdir(pDir)) != NULL) {
101                 fileNodeInfo *pNode = NULL;
102
103                 if (strncmp(ent->d_name, ".", 1) == 0 || strcmp(ent->d_name, "..") == 0)
104                         continue;
105
106                 if ((ent->d_type & DT_REG) == 0)
107                         continue;
108
109                 pNode = calloc(1, sizeof(fileNodeInfo));
110                 if (pNode == NULL)
111                         continue;
112
113                 pNode->path = strdup(path);
114                 pNode->name = strdup(ent->d_name);
115                 pNode->media_name = get_media_basename(
116                                 pNode->path, pNode->name);
117
118                 *file_list = eina_list_append(*file_list, pNode);
119         }
120         pthread_mutex_unlock(&mutex);
121
122         closedir(pDir);
123         //SETTING_TRACE_END;
124
125         return 0;
126 }
127