Modify flora license version.
[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         alarmmgr_set_time(alarm_info, target_time);
203         alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, 0);
204         alarmmgr_set_type(alarm_info, ALARM_TYPE_VOLATILE);
205         alarmmgr_add_alarm_with_localtime(alarm_info, NULL, &g_alarmId);
206
207         retval = alarmmgr_set_cb(MsgSoundRepeatAlarmCB, (void *)alarm_info);
208
209         if (retval != 0)
210         {
211                 MSG_DEBUG("alarmmgr_set_cb() error [%d]", retval);
212                 return false;
213         }
214
215         MSG_DEBUG("Repeat Alarm Time : [%d-%d-%d %d:%d:%d]",
216                 target_time.year,target_time.month,target_time.day,
217                 target_time.hour, target_time.min, target_time.sec);
218
219         MSG_END();
220
221         return true;
222 }
223
224
225 int MsgSoundRepeatAlarmCB(int TimerId, void *pUserParam)
226 {
227         MSG_BEGIN();
228
229         alarm_info_t *alarm_info = (alarm_info_t *)pUserParam;
230
231         g_bRepeat = false;
232
233         if (MsgSoundGetUnreadMsgCnt() <= 0) {
234                 MSG_DEBUG("no unread msg");
235         } else {
236                 MsgSoundPlayStart(false);
237         }
238
239         if (alarmmgr_free_alarm(alarm_info) != ALARMMGR_RESULT_SUCCESS)
240                 MSG_DEBUG("alarmmgr_free_alarm is failed");
241
242         MSG_END();
243
244         return 0;
245 }
246
247
248 int MsgSoundGetUnreadMsgCnt()
249 {
250         int unreadCnt = 0;
251
252         // Get SMS Count
253         unreadCnt = MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE);
254
255         // Get MMS Count
256         unreadCnt += MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE);
257
258         MSG_DEBUG("unread count : [%d]", unreadCnt);
259
260         return unreadCnt;
261 }
262
263 void MsgSoundInitRepeatAlarm()
264 {
265         MSG_BEGIN();
266
267         int nRepeatValue = 0;
268         long    nRepeatTime = 0;
269
270         g_bRepeat = false;
271
272         if (MsgSoundGetUnreadMsgCnt() <= 0) {
273                 MSG_DEBUG("no unread msg");
274                 return;
275         }
276
277         nRepeatValue = MsgSettingGetInt(MSG_ALERT_TONE);
278
279         switch (nRepeatValue)
280         {
281                 case MSG_ALERT_TONE_ONCE:
282                         nRepeatTime = 0;
283                 break;
284
285                 case MSG_ALERT_TONE_2MINS:
286                         nRepeatTime = 2;
287                 break;
288
289                 case MSG_ALERT_TONE_5MINS:
290                         nRepeatTime = 5;
291                 break;
292
293                 case MSG_ALERT_TONE_10MINS:
294                         nRepeatTime = 10;
295                 break;
296
297                 default:
298                         MSG_DEBUG("Invalid Repetition time");
299                 break;
300         }
301
302         MSG_DEBUG("nRepeatTime = %d", nRepeatTime);
303
304         if (nRepeatTime > 0)
305                 MsgSoundPlayStart(false);
306
307         MSG_END();
308 }