Get Ringtone path from system setting api
[platform/core/system/libstorage.git] / src / storage.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <system_settings.h>
23
24 #include "common.h"
25 #include "list.h"
26 #include "log.h"
27
28 const char *dir_path[STORAGE_DIRECTORY_MAX] = {
29         [STORAGE_DIRECTORY_IMAGES] = "Images",
30         [STORAGE_DIRECTORY_SOUNDS] = "Sounds",
31         [STORAGE_DIRECTORY_VIDEOS] = "Videos",
32         [STORAGE_DIRECTORY_CAMERA] = "Camera",
33         [STORAGE_DIRECTORY_DOWNLOADS] = "Downloads",
34         [STORAGE_DIRECTORY_MUSIC] = "Music",
35         [STORAGE_DIRECTORY_DOCUMENTS] = "Documents",
36         [STORAGE_DIRECTORY_OTHERS] = "Others",
37         [STORAGE_DIRECTORY_SYSTEM_RINGTONES] = "",
38 };
39
40 static dd_list *st_head;
41
42 void add_device(const struct storage_ops *st)
43 {
44         DD_LIST_APPEND(st_head, st);
45 }
46
47 void remove_device(const struct storage_ops *st)
48 {
49         DD_LIST_REMOVE(st_head, st);
50 }
51
52 API int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data)
53 {
54         const struct storage_ops *st;
55         dd_list *elem;
56         int storage_id = 0, ret;
57
58         if (!callback) {
59                 _E("Invalid parameter");
60                 return STORAGE_ERROR_INVALID_PARAMETER;
61         }
62
63         DD_LIST_FOREACH(st_head, elem, st) {
64                 ret = callback(storage_id, st->type, st->get_state(),
65                                 st->root(), user_data);
66                 /* if the return value is false, will be stop to iterate */
67                 if (!ret)
68                         break;
69                 storage_id++;
70         }
71
72         return STORAGE_ERROR_NONE;
73 }
74
75 API int storage_get_root_directory(int storage_id, char **path)
76 {
77         const struct storage_ops *st;
78
79         if (!path) {
80                 _E("Invalid parameger");
81                 return STORAGE_ERROR_INVALID_PARAMETER;
82         }
83
84         st = DD_LIST_NTH(st_head, storage_id);
85         if (!st) {
86                 _E("Not supported storage : id(%d)", storage_id);
87                 return STORAGE_ERROR_NOT_SUPPORTED;
88         }
89
90         *path = strdup(st->root());
91         if (!*path) {
92                 _E("Failed to copy the root string : %d", errno);
93                 return STORAGE_ERROR_OUT_OF_MEMORY;
94         }
95
96         return STORAGE_ERROR_NONE;
97 }
98
99 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
100 {
101         const struct storage_ops *st;
102         const char *root;
103         char temp[PATH_MAX];
104         char *temp2, *end;
105         int ret;
106
107         if (!path) {
108                 _E("Invalid parameger");
109                 return STORAGE_ERROR_INVALID_PARAMETER;
110         }
111
112         if (type < 0 || type >= STORAGE_DIRECTORY_MAX) {
113                 _E("Invalid parameter");
114                 return STORAGE_ERROR_INVALID_PARAMETER;
115         }
116
117         st = DD_LIST_NTH(st_head, storage_id);
118         if (!st) {
119                 _E("Not supported storage : id(%d)", storage_id);
120                 return STORAGE_ERROR_NOT_SUPPORTED;
121         }
122
123         if (st->type != STORAGE_TYPE_INTERNAL
124             && type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
125                 _E("Not support directory : id(%d) type(%d)", storage_id, type);
126                 return STORAGE_ERROR_NOT_SUPPORTED;
127         }
128
129         root = st->root();
130         if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
131                 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &temp2);
132                 if (ret < 0) {
133                         _E("Failed to get ringtone path : %d", ret);
134                         return STORAGE_ERROR_OPERATION_FAILED;
135                 }
136                 end = strrchr(temp2, '/');
137                 if (end)
138                         *end = '\0';
139                 snprintf(temp, PATH_MAX, "%s", temp2);
140                 free(temp2);
141         } else
142                 snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
143
144         *path = strdup(temp);
145         if (!*path) {
146                 _E("Failed to copy the directory(%d) string : %d", type, errno);
147                 return STORAGE_ERROR_OUT_OF_MEMORY;
148         }
149
150         return STORAGE_ERROR_NONE;
151 }
152
153 API int storage_get_type(int storage_id, storage_type_e *type)
154 {
155         const struct storage_ops *st;
156
157         if (!type) {
158                 _E("Invalid parameger");
159                 return STORAGE_ERROR_INVALID_PARAMETER;
160         }
161
162         st = DD_LIST_NTH(st_head, storage_id);
163         if (!st) {
164                 _E("Not supported storage : id(%d)", storage_id);
165                 return STORAGE_ERROR_NOT_SUPPORTED;
166         }
167
168         *type = st->type;
169
170         return STORAGE_ERROR_NONE;
171 }
172
173 API int storage_get_state(int storage_id, storage_state_e *state)
174 {
175         const struct storage_ops *st;
176
177         if (!state) {
178                 _E("Invalid parameger");
179                 return STORAGE_ERROR_INVALID_PARAMETER;
180         }
181
182         st = DD_LIST_NTH(st_head, storage_id);
183         if (!st) {
184                 _E("Not supported storage : id(%d)", storage_id);
185                 return STORAGE_ERROR_NOT_SUPPORTED;
186         }
187
188         *state = st->get_state();
189
190         return STORAGE_ERROR_NONE;
191 }
192
193 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
194 {
195         const struct storage_ops *st;
196         struct storage_cb_info info;
197         int ret;
198
199         if (!callback) {
200                 _E("Invalid parameger");
201                 return STORAGE_ERROR_INVALID_PARAMETER;
202         }
203
204         st = DD_LIST_NTH(st_head, storage_id);
205         if (!st) {
206                 _E("Not supported storage : id(%d)", storage_id);
207                 return STORAGE_ERROR_NOT_SUPPORTED;
208         }
209
210         /* do not register changed callback in case of internal memory */
211         if (st->type == STORAGE_TYPE_INTERNAL)
212                 return STORAGE_ERROR_NONE;
213
214         info.id = storage_id;
215         info.state_cb = callback;
216         info.user_data = user_data;
217
218         ret = st->register_cb(STORAGE_CALLBACK_STATE, &info);
219         if (ret < 0) {
220                 _E("Failed to register callback : id(%d)", storage_id);
221                 return STORAGE_ERROR_OPERATION_FAILED;
222         }
223
224         return STORAGE_ERROR_NONE;
225 }
226
227 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
228 {
229         const struct storage_ops *st;
230         struct storage_cb_info info;
231         int ret;
232
233         if (!callback) {
234                 _E("Invalid parameger");
235                 return STORAGE_ERROR_INVALID_PARAMETER;
236         }
237
238         st = DD_LIST_NTH(st_head, storage_id);
239         if (!st) {
240                 _E("Not supported storage : id(%d)", storage_id);
241                 return STORAGE_ERROR_NOT_SUPPORTED;
242         }
243
244         /* in case of internal memory, it does not register changed callback */
245         if (st->type == STORAGE_TYPE_INTERNAL)
246                 return STORAGE_ERROR_NONE;
247
248         info.id = storage_id;
249         info.state_cb = callback;
250
251         ret = st->unregister_cb(STORAGE_CALLBACK_STATE, &info);
252         if (ret < 0) {
253                 _E("Failed to unregister callback : id(%d)", storage_id);
254                 return STORAGE_ERROR_OPERATION_FAILED;
255         }
256
257         return STORAGE_ERROR_NONE;
258 }
259
260 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
261 {
262         const struct storage_ops *st;
263         unsigned long long total;
264         int ret;
265
266         if (!bytes) {
267                 _E("Invalid parameger");
268                 return STORAGE_ERROR_INVALID_PARAMETER;
269         }
270
271         st = DD_LIST_NTH(st_head, storage_id);
272         if (!st) {
273                 _E("Not supported storage : id(%d)", storage_id);
274                 return STORAGE_ERROR_NOT_SUPPORTED;
275         }
276
277         ret = st->get_space(&total, NULL);
278         if (ret < 0) {
279                 _E("Failed to get total memory : id(%d)", storage_id);
280                 return STORAGE_ERROR_OPERATION_FAILED;
281         }
282
283         *bytes = total;
284
285         return STORAGE_ERROR_NONE;
286 }
287
288 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
289 {
290         const struct storage_ops *st;
291         unsigned long long avail;
292         int ret;
293
294         if (!bytes) {
295                 _E("Invalid parameger");
296                 return STORAGE_ERROR_INVALID_PARAMETER;
297         }
298
299         st = DD_LIST_NTH(st_head, storage_id);
300         if (!st) {
301                 _E("Not supported storage : id(%d)", storage_id);
302                 return STORAGE_ERROR_NOT_SUPPORTED;
303         }
304
305         ret = st->get_space(NULL, &avail);
306         if (ret < 0) {
307                 _E("Failed to get available memory : id(%d)", storage_id);
308                 return STORAGE_ERROR_OPERATION_FAILED;
309         }
310
311         *bytes = avail;
312
313         return STORAGE_ERROR_NONE;
314 }