tizen 2.4 release
[apps/home/settings.git] / setting-ringtone / src / setting-ringtone-util.c
1 /*
2  * setting
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 "setting-ringtone-util.h"
23
24 inline char *strlower(char *str)
25 {
26         if (!str) return NULL;
27         char *orig = str;
28         for (; *str != '\0 '; str++)
29                 *str = tolower(*str);
30         return orig;
31 };
32
33 /*remove ext name */
34 char *get_filename_from_fullname(const char *fullname)
35 {
36         retvm_if(fullname == NULL, NULL, "fullname == NULL");
37
38         char tmp[512];
39         snprintf(tmp, sizeof(tmp), "%s", fullname);
40
41         char *name = strrchr(tmp, '.');
42         if (name != NULL) {
43                 *name = '\0';
44         }
45         return g_strdup(tmp);
46 }
47
48 char *get_media_basename(const char *dir_path, const char *name)
49 {
50         if (isEmptyStr(dir_path) || isEmptyStr(name)) {
51                 return NULL;
52         }
53
54         char path[512] = {0, };
55         snprintf(path, sizeof(path), "%s/%s", dir_path, name);
56
57         metadata_extractor_h metadata = NULL;
58         char *title = NULL;
59         int ret = metadata_extractor_create(&metadata);
60         if (ret == METADATA_EXTRACTOR_ERROR_NONE && metadata) {
61                 ret = metadata_extractor_set_path(metadata, path);
62                 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
63                         ret = metadata_extractor_get_metadata(metadata, METADATA_TITLE, &title);
64                         metadata_extractor_destroy(metadata);
65                         if (title)
66                                 /*return (char *)g_strdup(title);*/
67                                 return (char *)title;
68                         else
69                                 return g_strdup(name);
70                 } else {
71                         metadata_extractor_destroy(metadata);
72                         return g_strdup(name);
73                 }
74         } else {
75                 return g_strdup(name);
76         }
77 }
78
79 int get_filelist_from_dir_path(char *path, Eina_List **file_list)
80 {
81         SETTING_TRACE_BEGIN;
82         DIR *pDir = NULL;
83         struct dirent *ent;
84
85         retvm_if(path == NULL, -1, "dir path is null");
86         retvm_if(file_list == NULL, -1, "file_list is null");
87
88         pDir = opendir(path);
89
90         if (pDir == NULL) {
91                 return -2;
92         }
93
94         while ((ent = readdir(pDir)) != NULL) {
95                 fileNodeInfo *pNode = NULL;
96
97                 if (strncmp(ent->d_name, ".", 1) == 0 || strcmp(ent->d_name, "..") == 0) {
98                         continue;
99                 }
100
101                 if ((ent->d_type & DT_REG) == 0) {
102                         continue;
103                 }
104
105                 pNode = (fileNodeInfo *) malloc(sizeof(fileNodeInfo));
106                 if (pNode == NULL) {
107                         continue;
108                 }
109                 memset(pNode, 0, sizeof(fileNodeInfo));
110
111                 pNode->path = g_strdup(path);
112                 pNode->name = g_strdup(ent->d_name);
113                 pNode->media_name = get_media_basename(pNode->path, pNode->name);
114
115                 *file_list = eina_list_append(*file_list, pNode);
116         }
117         closedir(pDir);
118         SETTING_TRACE_END;
119
120         return 0;
121 }
122
123 void ringtone_play_sound(const char *sound_file, player_h **mp_handle)
124 {
125         SETTING_TRACE_BEGIN;
126         player_h *player = calloc(1, sizeof(player_h));
127         if (!player) {
128                 SETTING_TRACE_ERROR("failed to calloc player_h");
129                 return;
130         }
131
132         sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
133         sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
134
135         int err = player_create(player);
136         if (err != PLAYER_ERROR_NONE) {
137                 SETTING_TRACE_ERROR("creating the player handle failed[%d]",
138                                     err);
139                 FREE(player);
140                 return;
141         }
142
143         err = player_set_uri(*player, sound_file);
144         if (err != PLAYER_ERROR_NONE) {
145                 SETTING_TRACE_ERROR("error to set attribute---profile_uri[%d]",
146                                     err);
147                 player_destroy(*player);
148                 FREE(player);
149                 return;
150         }
151
152         err = player_prepare(*player);
153         if (err != PLAYER_ERROR_NONE) {
154                 SETTING_TRACE_ERROR("realizing the player handle failed[%d]",
155                                     err);
156                 player_destroy(*player);
157                 FREE(player);
158                 return;
159         }
160         /* sleep(0.001); */
161         player_state_e state = -1;
162         player_get_state(*player, &state);
163         SETTING_TRACE("state:%d", state);
164
165         if (state != PLAYER_STATE_READY) {
166                 SETTING_TRACE_ERROR("state of player is invalid %d", err);
167                 player_unprepare(*player);
168                 player_destroy(*player);
169                 FREE(player);
170                 return;
171         }
172
173         /*player_set_completed_cb(*player, cb, data); */
174
175         err = player_start(*player);
176         if (err != PLAYER_ERROR_NONE) { /* if directly return error.. */
177                 SETTING_TRACE_ERROR("player_start [%d]", err);
178                 player_unset_completed_cb(*player);
179                 player_unprepare(*player);
180                 player_destroy(*player);
181                 FREE(player);
182                 return;
183         }
184
185         *mp_handle = player;
186 }
187
188 /*
189  *@brief the function for sound balance stop the sound.
190  *
191  *@param data
192  */
193 void ringtone_stop_sound(void *data)
194 {
195         SETTING_TRACE_BEGIN;
196         setting_retm_if(data == NULL, "data is NULL");
197         SettingRingtoneUG *ad = (SettingRingtoneUG *) data;
198         setting_retm_if(ad->mp_ringtone == NULL, "ad->mp_ringtone is NULL");
199         player_state_e state = -1;
200         int ret = 0;
201         player_h player = *(ad->mp_ringtone);
202         player_get_state(*ad->mp_ringtone, &state);
203         if (state == PLAYER_STATE_PLAYING) {
204                 if (player_stop(player) != PLAYER_ERROR_NONE) {
205                         SETTING_TRACE("mm player stop failed");
206                         ret = SETTING_MMPLAYER_STOP_ERR;
207                 }
208         }
209
210         /*player_unset_completed_cb(player); */
211
212         if ((ret == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
213                 SETTING_TRACE("mm player unrealize failed");
214                 ret = SETTING_MMPLAYER_UNREALIZE_ERR;
215         }
216
217         if ((ret == 0) && (player_destroy(player) != PLAYER_ERROR_NONE)) {
218                 SETTING_TRACE("mm player destroy failed");
219                 ret = SETTING_MMPLAYER_DESTROY_ERR;
220         }
221
222         sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
223
224         FREE(ad->mp_ringtone);
225         ad->mp_ringtone = NULL;
226 }