Add Ringtone List API
[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
22 #include <system_settings.h>
23 #include <system_settings_private.h>
24 #include "system_settings_ringtones.h"
25 #include "system_settings_json.h"
26
27 inline char *strlower(char *str)
28 {
29         if (!str) return NULL;
30         char *orig = str;
31         for (; *str != '\0'; str++)
32                 *str = tolower(*str);
33         return orig;
34 };
35
36 /*remove ext name */
37 char *get_filename_from_fullname(const char *fullname)
38 {
39         //retvm_if(fullname == NULL, NULL, "fullname == NULL");
40         if (fullname == NULL) return NULL;
41
42         char tmp[512];
43         snprintf(tmp, sizeof(tmp), "%s", fullname);
44
45         char *name = strrchr(tmp, '.');
46         if (name != NULL)
47                 *name = '\0';
48
49         return strdup((char*)tmp);
50 }
51
52 char *get_media_basename(const char *dir_path, const char *name)
53 {
54         //retv_if(isEmptyStr(dir_path) || isEmptyStr(name), NULL);
55         if (dir_path == NULL) return NULL;
56
57         char path[512] = {0, };
58         snprintf(path, sizeof(path), "%s/%s", dir_path, name);
59
60         metadata_extractor_h metadata = NULL;
61         char *title = NULL;
62         int ret = metadata_extractor_create(&metadata);
63         if (ret == METADATA_EXTRACTOR_ERROR_NONE && metadata) {
64                 ret = metadata_extractor_set_path(metadata, path);
65                 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
66                         ret = metadata_extractor_get_metadata(
67                                         metadata, METADATA_TITLE, &title);
68                         metadata_extractor_destroy(metadata);
69                         if (title)
70                                 /*return (char *)g_strdup(title);*/
71                                 return (char *)title;
72                         else
73                                 return strdup(name);
74                 } else {
75                         metadata_extractor_destroy(metadata);
76                         return strdup(name);
77                 }
78         } else {
79                 return strdup(name);
80         }
81 }
82
83 int get_filelist_from_dir_path(char *path, Eina_List **file_list)
84 {
85         SETTING_TRACE_BEGIN;
86         DIR *pDir = NULL;
87         struct dirent ent, *result;
88
89         if (path == NULL) {
90                 SETTING_TRACE("dir path is null");
91                 return -1;
92         }
93
94         if (file_list == NULL) {
95                 SETTING_TRACE("file_list is null");
96                 return -1;
97         }
98
99         pDir = opendir(path);
100
101         if (pDir == NULL)
102                 return -2;
103
104         while (readdir_r(pDir, &ent, &result) == 0) {
105                 if (result == NULL)
106                         break;
107
108                 fileNodeInfo *pNode = NULL;
109
110                 if (strncmp(ent.d_name, ".", 1) == 0
111                                 || strcmp(ent.d_name, "..") == 0)
112                         continue;
113
114                 if ((ent.d_type & DT_REG) == 0)
115                         continue;
116
117                 pNode = (fileNodeInfo *) malloc(sizeof(fileNodeInfo));
118                 if (pNode == NULL)
119                         continue;
120
121                 memset(pNode, 0, sizeof(fileNodeInfo));
122
123                 pNode->path = strdup(path);
124                 pNode->name = strdup(ent.d_name);
125                 pNode->media_name = get_media_basename(
126                                 pNode->path, pNode->name);
127
128                 *file_list = eina_list_append(*file_list, pNode);
129         }
130         closedir(pDir);
131         //SETTING_TRACE_END;
132
133         return 0;
134 }
135