update tizen source
[framework/messaging/msg-service.git] / utils / MsgSoundPlayer.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #include <wait.h>
32
33 #include "MsgHelper.h"
34 #include "MsgDebug.h"
35 #include "MsgGconfWrapper.h"
36 #include "MsgSettingTypes.h"
37 #include "MsgSoundPlayer.h"
38
39 #include <alarm.h>
40
41
42  /*==================================================================================================
43                                      VARIABLES
44 ==================================================================================================*/
45 static bool g_bRepeat = false;
46 static alarm_id_t g_alarmId = 0;
47
48
49 /*==================================================================================================
50                                      FUNCTION IMPLEMENTATION
51 ==================================================================================================*/
52
53
54 void MsgSoundPlayStart()
55 {
56         MSG_BEGIN();
57
58         // Run helper App
59         pid_t childpid;
60         childpid = fork();
61
62         if (childpid == -1)
63         {
64                 MSG_DEBUG("Failed to fork");
65         }
66
67         if (childpid == 0)
68         {
69                 MSG_DEBUG("Child Process - Run helper app for Sound");
70
71                 execl("/usr/bin/msg-helper", MSG_SOUND_START, NULL);
72
73                 MSG_DEBUG("Faild to run helper app for Sound");
74
75                 exit(0);
76         }
77         else if (childpid != 0)
78         {
79                 MSG_DEBUG("Parent Process - Mms Plugin Storage.");
80         }
81
82         if (g_bRepeat == false)
83                 g_bRepeat = MsgSoundSetRepeatAlarm();
84
85         MSG_END();
86 }
87
88 void MsgSoundPlayStop()
89 {
90         MSG_BEGIN();
91
92         // Run helper App
93         pid_t childpid;
94         childpid = fork();
95
96         if (childpid == -1)
97         {
98                 MSG_DEBUG("Failed to fork");
99         }
100
101         if (childpid == 0)
102         {
103                 MSG_DEBUG("Child Process - Run helper app for Sound");
104
105                 execl("/usr/bin/msg-helper", MSG_SOUND_STOP, NULL);
106
107                 MSG_DEBUG("Faild to run helper app for Sound");
108
109                 exit(0);
110         }
111         else if (childpid != 0)
112         {
113                 MSG_DEBUG("Parent Process - Mms Plugin Storage.");
114         }
115
116         MSG_END();
117 }
118
119
120 bool MsgSoundSetRepeatAlarm()
121 {
122         bool bRet = false;
123
124         int nRepeatValue = 0;
125         long    nRepeatTime = 0;
126
127         nRepeatValue = MsgSettingGetInt(MSG_ALERT_TONE);
128
129         switch (nRepeatValue)
130         {
131                 case MSG_ALERT_TONE_ONCE:
132                         nRepeatTime = 0;
133                 break;
134
135                 case MSG_ALERT_TONE_2MINS:
136                         nRepeatTime = 2;
137                 break;
138
139                 case MSG_ALERT_TONE_5MINS:
140                         nRepeatTime = 5;
141                 break;
142
143                 case MSG_ALERT_TONE_10MINS:
144                         nRepeatTime = 10;
145                 break;
146
147                 default:
148                         MSG_DEBUG("Invalid Repetition time");
149                 break;
150         }
151
152         MSG_DEBUG("nRepeatTime = %d", nRepeatTime);
153
154         if (nRepeatTime > 0)
155         {
156                 bRet = MsgSoundCreateRepeatAlarm(nRepeatTime);
157         }
158
159         return bRet;
160 }
161
162
163 bool MsgSoundCreateRepeatAlarm(int RepeatTime)
164 {
165         MSG_BEGIN();
166
167         time_t current_time;
168         struct tm current_tm;
169
170         time(&current_time);
171
172         localtime_r(&current_time, &current_tm);
173
174         int retval =0;
175
176         retval = alarmmgr_init("msg-service-tools");
177
178         if (retval != 0)
179         {
180                 MSG_FATAL("alarmmgr_init() error [%d]", retval);
181                 return false;
182         }
183
184         alarm_info_t* alarm_info;
185         alarm_date_t target_time;
186
187         alarm_info = alarmmgr_create_alarm();
188
189         target_time.year = 0;
190         target_time.month = 0;
191         target_time.day = 0;
192
193         if (current_tm.tm_min+RepeatTime < 60)
194         {
195                 target_time.hour = current_tm.tm_hour;
196                 target_time.min = current_tm.tm_min+RepeatTime;
197         }
198         else
199         {
200                 if (current_tm.tm_hour < 12)
201                 {
202                         target_time.hour = current_tm.tm_hour+1;
203                 }
204                 else
205                 {
206                         target_time.hour = (current_tm.tm_hour+1)%12;
207                 }
208
209                 target_time.min = (current_tm.tm_min+RepeatTime)%60;
210         }
211
212         target_time.sec = current_tm.tm_sec;
213
214         alarmmgr_set_time(alarm_info, target_time);
215         alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, 0);
216         alarmmgr_set_type(alarm_info, ALARM_TYPE_VOLATILE);
217         alarmmgr_add_alarm_with_localtime(alarm_info, NULL, &g_alarmId);
218
219         retval = alarmmgr_set_cb(MsgSoundRepeatAlarmCB, NULL);
220
221         if (retval != 0)
222         {
223                 MSG_DEBUG("alarmmgr_set_cb() error [%d]", retval);
224                 return false;
225         }
226
227         MSG_DEBUG("Repeat Alarm Time : [%d-%d-%d %d:%d:%d]",
228                 target_time.year,target_time.month,target_time.day,
229                 target_time.hour, target_time.min, target_time.sec);
230
231         MSG_END();
232
233         return true;
234 }
235
236
237 int MsgSoundRepeatAlarmCB(int TimerId, void *pUserParam)
238 {
239         MSG_BEGIN();
240
241         g_bRepeat = false;
242
243         if (MsgSoundGetUnreadMsgCnt() <= 0)
244         {
245                 MSG_DEBUG("no unread msg");
246
247                 return 0;
248         }
249
250         MsgSoundPlayStart();
251
252         MSG_END();
253
254         return 0;
255 }
256
257
258 int MsgSoundGetUnreadMsgCnt()
259 {
260         int unreadCnt = 0;
261
262         // Get SMS Count
263         unreadCnt = MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_SMS_STATE);
264
265         // Get MMS Count
266         unreadCnt += MsgSettingGetInt(VCONFKEY_MESSAGE_RECV_MMS_STATE);
267
268         MSG_DEBUG("unread count : [%d]", unreadCnt);
269
270         return unreadCnt;
271 }
272