tizen 2.3.1 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, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "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                         else
68                                 return g_strdup(name);
69                 } else {
70                         metadata_extractor_destroy(metadata);
71                         return g_strdup(name);
72                 }
73         } else {
74                 return g_strdup(name);
75         }
76 }
77
78 int get_filelist_from_dir_path(char *path, Eina_List **file_list)
79 {
80         SETTING_TRACE_BEGIN;
81         DIR *pDir = NULL;
82         struct dirent *ent;
83
84         retvm_if(path == NULL, -1, "dir path is null");
85         retvm_if(file_list == NULL, -1, "file_list is null");
86
87         pDir = opendir(path);
88
89         if (pDir == NULL) {
90                 return -2;
91         }
92
93         while ((ent = readdir(pDir)) != NULL) {
94                 fileNodeInfo *pNode = NULL;
95
96                 if (strncmp(ent->d_name, ".", 1) == 0 || strcmp(ent->d_name, "..") == 0) {
97                         continue;
98                 }
99
100                 if ((ent->d_type & DT_REG) == 0) {
101                         continue;
102                 }
103
104                 pNode = (fileNodeInfo *) malloc(sizeof(fileNodeInfo));
105                 if (pNode == NULL) {
106                         continue;
107                 }
108                 memset(pNode, 0, sizeof(fileNodeInfo));
109
110                 pNode->path = g_strdup(path);
111                 pNode->name = g_strdup(ent->d_name);
112                 pNode->media_name = get_media_basename(pNode->path, pNode->name);
113
114                 *file_list = eina_list_append(*file_list, pNode);
115         }
116         closedir(pDir);
117         SETTING_TRACE_END;
118
119         return 0;
120 }
121
122 void ringtone_play_sound(const char *sound_file, player_h **mp_handle)
123 {
124         SETTING_TRACE_BEGIN;
125         player_h *player = calloc(1, sizeof(player_h));
126         if (!player) {
127                 SETTING_TRACE_ERROR("failed to calloc player_h");
128                 return;
129         }
130
131         sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
132         sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
133
134         int err = player_create(player);
135         if (err != PLAYER_ERROR_NONE) {
136                 SETTING_TRACE_ERROR("creating the player handle failed[%d]",
137                                     err);
138                 FREE(player);
139                 return;
140         }
141
142         err = player_set_uri(*player, sound_file);
143         if (err != PLAYER_ERROR_NONE) {
144                 SETTING_TRACE_ERROR("error to set attribute---profile_uri[%d]",
145                                     err);
146                 player_destroy(*player);
147                 FREE(player);
148                 return;
149         }
150
151         err = player_prepare(*player);
152         if (err != PLAYER_ERROR_NONE) {
153                 SETTING_TRACE_ERROR("realizing the player handle failed[%d]",
154                                     err);
155                 player_destroy(*player);
156                 FREE(player);
157                 return;
158         }
159         /* sleep(0.001); */
160         player_state_e state = -1;
161         player_get_state(*player, &state);
162         SETTING_TRACE("state:%d", state);
163
164         if (state != PLAYER_STATE_READY) {
165                 SETTING_TRACE_ERROR("state of player is invalid %d", err);
166                 player_unprepare(*player);
167                 player_destroy(*player);
168                 FREE(player);
169                 return;
170         }
171
172         /*player_set_completed_cb(*player, cb, data); */
173
174         err = player_start(*player);
175         if (err != PLAYER_ERROR_NONE) { /* if directly return error.. */
176                 SETTING_TRACE_ERROR("player_start [%d]", err);
177                 player_unset_completed_cb(*player);
178                 player_unprepare(*player);
179                 player_destroy(*player);
180                 FREE(player);
181                 return;
182         }
183
184         *mp_handle = player;
185 }
186
187 /*
188  *@brief the function for sound balance stop the sound.
189  *
190  *@param data
191  */
192 void ringtone_stop_sound(void *data)
193 {
194         SETTING_TRACE_BEGIN;
195         setting_retm_if(data == NULL, "data is NULL");
196         SettingRingtoneUG *ad = (SettingRingtoneUG *) data;
197         setting_retm_if(ad->mp_ringtone == NULL, "ad->mp_ringtone is NULL");
198         player_state_e state = -1;
199         int ret = 0;
200         player_h player = *(ad->mp_ringtone);
201         player_get_state(*ad->mp_ringtone, &state);
202         if (state == PLAYER_STATE_PLAYING) {
203                 if (player_stop(player) != PLAYER_ERROR_NONE) {
204                         SETTING_TRACE("mm player stop failed");
205                         ret = SETTING_MMPLAYER_STOP_ERR;
206                 }
207         }
208
209         /*player_unset_completed_cb(player); */
210
211         if ((ret == 0) && (player_unprepare(player) != PLAYER_ERROR_NONE)) {
212                 SETTING_TRACE("mm player unrealize failed");
213                 ret = SETTING_MMPLAYER_UNREALIZE_ERR;
214         }
215
216         if ((ret == 0) && (player_destroy(player) != PLAYER_ERROR_NONE)) {
217                 SETTING_TRACE("mm player destroy failed");
218                 ret = SETTING_MMPLAYER_DESTROY_ERR;
219         }
220
221         sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
222
223         FREE(ad->mp_ringtone);
224         ad->mp_ringtone = NULL;
225 }