Remove hardcoded path for multiuser support
[platform/core/messaging/msg-service.git] / utils / MsgSoundPlayer.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include <wait.h>
18
19 #include "MsgHelper.h"
20 #include "MsgDebug.h"
21 #include "MsgGconfWrapper.h"
22 #include "MsgSettingTypes.h"
23 #include "MsgSoundPlayer.h"
24
25 #include <alarm.h>
26
27 /*==================================================================================================
28                                      VARIABLES
29 ==================================================================================================*/
30 static bool g_bRepeat = false;
31 static alarm_id_t g_alarmId = 0;
32
33
34 /*==================================================================================================
35                                      FUNCTION IMPLEMENTATION
36 ==================================================================================================*/
37
38
39 void MsgSoundPlayStart(bool isEmergency)
40 {
41         MSG_BEGIN();
42
43         // Run helper App
44         pid_t childpid;
45         childpid = fork();
46
47         if (childpid == -1)
48         {
49                 MSG_DEBUG("Failed to fork");
50         }
51
52         if (childpid == 0)
53         {
54                 MSG_DEBUG("Child Process - Run helper app for Sound");
55
56                 if (isEmergency)
57                         execl("/usr/bin/msg-helper", MSG_EMERGENCY_SOUND_START, NULL);
58                 else
59                         execl("/usr/bin/msg-helper", MSG_NORMAL_SOUND_START, NULL);
60
61                 MSG_DEBUG("Faild to run helper app for Sound");
62
63                 exit(0);
64         }
65         else if (childpid != 0)
66         {
67                 MSG_DEBUG("Parent Process - Mms Plugin Storage.");
68         }
69
70         if (g_bRepeat == false)
71                 g_bRepeat = MsgSoundSetRepeatAlarm();
72
73         MSG_END();
74 }
75
76 void MsgSoundPlayStop()
77 {
78         MSG_BEGIN();
79
80         // Run helper App
81         pid_t childpid;
82         childpid = fork();
83
84         if (childpid == -1)
85         {
86                 MSG_DEBUG("Failed to fork");
87         }
88
89         if (childpid == 0)
90         {
91                 MSG_DEBUG("Child Process - Run helper app for Sound");
92
93                 execl("/usr/bin/msg-helper", MSG_SOUND_STOP, NULL);
94
95                 MSG_DEBUG("Faild to run helper app for Sound");
96
97                 exit(0);
98         }
99         else if (childpid != 0)
100         {
101                 MSG_DEBUG("Parent Process - Mms Plugin Storage.");
102         }
103
104         MSG_END();
105 }
106
107
108 bool MsgSoundSetRepeatAlarm()
109 {
110         bool bRet = false;
111
112         int nRepeatValue = 0;
113         long    nRepeatTime = 0;
114
115         nRepeatValue = MsgSettingGetInt(MSG_ALERT_TONE);
116
117         switch (nRepeatValue)
118         {
119                 case MSG_ALERT_TONE_ONCE:
120                         nRepeatTime = 0;
121                 break;
122
123                 case MSG_ALERT_TONE_2MINS:
124                         nRepeatTime = 2;
125                 break;
126
127                 case MSG_ALERT_TONE_5MINS:
128                         nRepeatTime = 5;
129                 break;
130
131                 case MSG_ALERT_TONE_10MINS:
132                         nRepeatTime = 10;
133                 break;
134
135                 default:
136                         MSG_DEBUG("Invalid Repetition time");
137                 break;
138         }
139
140         MSG_DEBUG("nRepeatTime = %d", nRepeatTime);
141
142         if (nRepeatTime > 0)
143         {
144                 bRet = MsgSoundCreateRepeatAlarm(nRepeatTime);
145         }
146
147         return bRet;
148 }
149
150
151 bool MsgSoundCreateRepeatAlarm(int RepeatTime)
152 {
153         MSG_BEGIN();
154
155         time_t current_time;
156         struct tm current_tm;
157
158         time(&current_time);
159
160         localtime_r(&current_time, &current_tm);
161
162         int retval =0;
163
164         retval = alarmmgr_init("msg-service-tools");
165
166         if (retval != 0)
167         {
168                 MSG_FATAL("alarmmgr_init() error [%d]", retval);
169                 return false;
170         }
171
172         alarm_info_t* alarm_info;
173         alarm_date_t target_time;
174
175         alarm_info = alarmmgr_create_alarm();
176
177         target_time.year = 0;
178         target_time.month = 0;
179         target_time.day = 0;
180
181         if (current_tm.tm_min+RepeatTime < 60)
182         {
183                 target_time.hour = current_tm.tm_hour;
184                 target_time.min = current_tm.tm_min+RepeatTime;
185         }
186         else
187         {
188                 if (current_tm.tm_hour < 12)
189                 {
190                         target_time.hour = current_tm.tm_hour+1;
191                 }
192                 else
193                 {
194                         target_time.hour = (current_tm.tm_hour+1)%12;
195                 }
196
197                 target_time.min = (current_tm.tm_min+RepeatTime)%60;
198         }
199
200         target_time.sec = current_tm.tm_sec;
201
202         retval = alarmmgr_set_time(alarm_info, target_time);
203         MSG_DEBUG("alarmmgr_set_time return = [%d]", retval);
204         retval = alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, 0);
205         MSG_DEBUG("alarmmgr_set_repeat_mode return = [%d]", retval);
206         retval = alarmmgr_set_type(alarm_info, ALARM_TYPE_VOLATILE);
207         MSG_DEBUG("alarmmgr_set_type return = [%d]", retval);
208         retval = alarmmgr_add_alarm_with_localtime(alarm_info, NULL, &g_alarmId);
209         MSG_DEBUG("alarmmgr_add_alarm_with_localtime return = [%d]", retval);
210
211         retval = alarmmgr_set_cb(MsgSoundRepeatAlarmCB, (void *)alarm_info);
212
213         if (retval != 0)
214         {
215                 MSG_DEBUG("alarmmgr_set_cb() error [%d]", retval);
216                 return false;
217         }
218
219         MSG_DEBUG("Repeat Alarm Time : [%d-%d-%d %d:%d:%d]",
220                 target_time.year,target_time.month,target_time.day,
221                 target_time.hour, target_time.min, target_time.sec);
222
223         MSG_END();
224
225         return true;
226 }
227
228
229 int MsgSoundRepeatAlarmCB(int TimerId, void *pUserParam)
230 {
231         MSG_BEGIN();
232
233         alarm_info_t *alarm_info = (alarm_info_t *)pUserParam;
234
235         g_bRepeat = false;
236
237         if (MsgSoundGetUnreadMsgCnt() <= 0) {
238                 MSG_DEBUG("no unread msg");
239         } else {
240                 MsgSoundPlayStart(false);
241         }
242
243         if (alarmmgr_free_alarm(alarm_info) != ALARMMGR_RESULT_SUCCESS)
244                 MSG_DEBUG("alarmmgr_free_alarm is failed");
245
246         MSG_END();
247
248         return 0;
249 }
250
251
252 int MsgSoundGetUnreadMsgCnt()
253 {
254         int unreadCnt = 0;
255
256         // Get SMS Count
257         unreadCnt = MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE);
258
259         // Get MMS Count
260         unreadCnt += MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE);
261
262         MSG_DEBUG("unread count : [%d]", unreadCnt);
263
264         return unreadCnt;
265 }
266
267 void MsgSoundInitRepeatAlarm()
268 {
269         MSG_BEGIN();
270
271         int nRepeatValue = 0;
272         long    nRepeatTime = 0;
273
274         g_bRepeat = false;
275
276         if (MsgSoundGetUnreadMsgCnt() <= 0) {
277                 MSG_DEBUG("no unread msg");
278                 return;
279         }
280
281         nRepeatValue = MsgSettingGetInt(MSG_ALERT_TONE);
282
283         switch (nRepeatValue)
284         {
285                 case MSG_ALERT_TONE_ONCE:
286                         nRepeatTime = 0;
287                 break;
288
289                 case MSG_ALERT_TONE_2MINS:
290                         nRepeatTime = 2;
291                 break;
292
293                 case MSG_ALERT_TONE_5MINS:
294                         nRepeatTime = 5;
295                 break;
296
297                 case MSG_ALERT_TONE_10MINS:
298                         nRepeatTime = 10;
299                 break;
300
301                 default:
302                         MSG_DEBUG("Invalid Repetition time");
303                 break;
304         }
305
306         MSG_DEBUG("nRepeatTime = %d", nRepeatTime);
307
308         if (nRepeatTime > 0)
309                 MsgSoundPlayStart(false);
310
311         MSG_END();
312 }