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