Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-core / email-core-sound.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@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
23 #include <stdlib.h>
24 #include <pthread.h>
25
26 #include "email-debug-log.h"
27 #include "email-storage.h"
28 #include "email-core-utils.h"
29 #include "email-core-mailbox.h"
30 #include "email-core-sound.h"
31 #include "email-utilities.h"
32
33 #define TIMER 30000   // 30 seconds
34 #define HAPTIC_TEST_ITERATION 1
35
36 static MMHandleType email_mmhandle = 0;
37 static alarm_id_t email_alarm_id = 0;
38 static int setting_noti_status = 0;
39
40 static char *filename;
41 alarm_entry_t *alarm_info = NULL;
42
43 static pthread_mutex_t sound_mutex = PTHREAD_MUTEX_INITIALIZER;
44 static pthread_cond_t sound_condition = PTHREAD_COND_INITIALIZER;
45 static pthread_mutex_t mmhandle_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static thread_t g_alert_thread;
47
48 void  emcore_set_repetition_alarm(int repetition);
49 int   emcore_sound_mp_player_stop();
50 bool  emcore_sound_mp_player_destory();
51 void *start_alert_thread(void *arg);
52
53 bool emcore_set_mp_filepath(const char *key)
54 {
55         filename = vconf_get_str(key);
56         if (filename == NULL)
57                 return false;
58
59         /* initialize the ringtone path */
60         if (vconf_set_str(VCONF_VIP_NOTI_RINGTONE_PATH, filename) != 0) {
61                 EM_DEBUG_EXCEPTION("vconf_set_str failed");
62                 return false;
63         }
64
65         return true;
66 }
67
68 int emcore_alert_sound_init()
69 {
70         int ret = MM_ERROR_NONE;
71         if ((ret = mm_session_init(MM_SESSION_TYPE_NOTIFY)) != MM_ERROR_NONE) 
72                 EM_DEBUG_EXCEPTION("mm_session_int failed");
73
74         return ret;
75 }
76
77 int emcore_alert_alarm_init()
78 {
79         EM_DEBUG_FUNC_BEGIN();
80
81         int ret = ALARMMGR_RESULT_SUCCESS;
82         
83         ret = alarmmgr_init("email-service-0");
84         if (ret != ALARMMGR_RESULT_SUCCESS) 
85                 EM_DEBUG_EXCEPTION("alarmmgr_init failed : [%d]", ret);
86
87         EM_DEBUG_FUNC_END();
88         return ret;
89 }
90
91 int emcore_alert_sound_filepath_init()
92 {
93         filename = (char  *)em_malloc(MAX_PATH);
94         if (filename == NULL) {
95                 EM_DEBUG_EXCEPTION("Memory malloc error");      
96                 return false;
97         }
98
99         if (!emcore_set_mp_filepath(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR)) {
100                 /* TODO : Add code to set default ringtone path */
101                 EM_DEBUG_EXCEPTION("emcore_set_mp_filepath failed.");
102                 return false;
103         }
104
105         return true;
106 }
107
108 void emcore_global_noti_key_changed_cb(keynode_t *key_node, void *data)
109 {
110         EM_DEBUG_FUNC_BEGIN();
111         int ret = 0;
112
113         switch (vconf_keynode_get_type(key_node)) {
114         case VCONF_TYPE_INT:
115                 ret = alarmmgr_remove_alarm(email_alarm_id);
116                 if (ret != ALARMMGR_RESULT_SUCCESS) {
117                         EM_DEBUG_EXCEPTION("delete of alarm id failed");
118                 }
119                 emcore_set_repetition_alarm(vconf_keynode_get_int(key_node));
120                 break;
121         case VCONF_TYPE_STRING:
122                 filename = vconf_keynode_get_str(key_node);
123                 break;
124         default:
125                 EM_DEBUG_EXCEPTION("Invalid key type");
126                 break;
127         }
128
129         EM_DEBUG_FUNC_END();
130 }
131
132 void emcore_email_noti_key_changed_cb(keynode_t *key_node, void *data)
133 {
134         EM_DEBUG_FUNC_BEGIN();
135         int ret = 0;
136
137         switch (vconf_keynode_get_type(key_node)) {
138         case VCONF_TYPE_INT:
139                 ret = alarmmgr_remove_alarm(email_alarm_id);
140                 if (ret != ALARMMGR_RESULT_SUCCESS) {
141                         EM_DEBUG_EXCEPTION("delete of alarm id failed");
142                 }
143                 emcore_set_repetition_alarm(vconf_keynode_get_int(key_node));
144                 break;
145         case VCONF_TYPE_STRING:
146                 filename = vconf_keynode_get_str(key_node);
147                 break;
148         default:
149                 EM_DEBUG_EXCEPTION("Invalid key type");
150                 break;
151         }
152         EM_DEBUG_FUNC_END();
153 }
154
155 bool emcore_update_noti_status()
156 {
157         EM_DEBUG_FUNC_BEGIN();
158         int ticker_noti = 0;
159
160         /* Get the priority noti ticker */
161         if (vconf_get_bool(VCONF_VIP_NOTI_NOTIFICATION_TICKER, &ticker_noti) != 0) {
162                 EM_DEBUG_EXCEPTION("vconf_get_bool failed");
163                 return false;
164         }
165
166         EM_DEBUG_LOG("ticker_noti of vip : [%d]", ticker_noti);
167
168         if (ticker_noti <= 0) {
169                 /* Get the Global noti ticker */
170                 if (vconf_get_bool(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, &ticker_noti) != 0) {
171                                 EM_DEBUG_EXCEPTION("Not display the noti of email");
172                                 return false;
173                 }
174
175                 EM_DEBUG_LOG("ticker_noti of global : [%d]", ticker_noti);
176
177                 if (!ticker_noti) {
178                         EM_DEBUG_LOG("Not use the notification");
179                         setting_noti_status = SETTING_NOTI_STATUS_OFF;
180                         return true;
181                 }
182
183                 setting_noti_status = SETTING_NOTI_STATUS_GLOBAL;
184         } else {
185                 setting_noti_status = SETTING_NOTI_STATUS_EMAIL;
186         }
187
188         EM_DEBUG_FUNC_END();
189         return true;
190 }
191
192 void emcore_noti_status_changed_cb(keynode_t *key_node, void *data)
193 {
194         EM_DEBUG_FUNC_BEGIN();
195         if (!emcore_update_noti_status()) {
196                 EM_DEBUG_EXCEPTION("emcore_update_noti_status failed");
197                 return;
198         }
199         EM_DEBUG_FUNC_END();
200 }
201
202 bool emcore_noti_init(void *data)
203 {
204         EM_DEBUG_FUNC_BEGIN();
205         struct appdata *ap = data;
206
207         if (!emcore_update_noti_status()) {
208                 EM_DEBUG_EXCEPTION("emcore_update_noti_status failed");
209                 return false;
210         }
211
212         /* Noti callback registration */
213         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_EMAIL_ALERT_REP_TYPE_INT, emcore_global_noti_key_changed_cb, ap) < 0) {
214                 EM_DEBUG_EXCEPTION("Register failed : alert type");
215         }
216
217         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR, emcore_global_noti_key_changed_cb, ap) < 0) {
218                 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
219         }
220
221         if (vconf_notify_key_changed(VCONF_VIP_NOTI_REP_TYPE, emcore_email_noti_key_changed_cb, ap) < 0) {
222                 EM_DEBUG_EXCEPTION("Register failed : alert type");
223         }
224
225         if (vconf_notify_key_changed(VCONF_VIP_NOTI_RINGTONE_PATH, emcore_email_noti_key_changed_cb, ap) < 0) {
226                 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
227         }
228
229         if (vconf_notify_key_changed(VCONF_VIP_NOTI_NOTIFICATION_TICKER, emcore_noti_status_changed_cb, ap) < 0) {
230                 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
231         }
232
233         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_TICKER_NOTI_EMAIL_BOOL, emcore_noti_status_changed_cb, ap) < 0) {
234                 EM_DEBUG_EXCEPTION("Register failed : Ringtone path");
235         }
236
237         EM_DEBUG_FUNC_END();
238         return true;
239 }
240
241 int emcore_alert_init()
242 {
243         EM_DEBUG_FUNC_BEGIN();
244
245         int err = 0;
246         
247         if (!emcore_alert_sound_filepath_init()) {
248                 EM_DEBUG_EXCEPTION("emcore_alert_sound_filepath_init failed");
249                 return false;
250         }
251
252         if ((err = emcore_alert_sound_init()) != MM_ERROR_NONE) {
253                 EM_DEBUG_EXCEPTION("emcore_alert_sound_init failed : [%d]", err);
254                 return false;
255         }
256
257         if ((err = emcore_alert_alarm_init()) != ALARMMGR_RESULT_SUCCESS) {
258                 EM_DEBUG_EXCEPTION("emcore_alert_alarm_init failed : [%d]", err);
259                 return false;           
260         }
261
262         if (!emcore_noti_init(NULL)) {
263                 EM_DEBUG_EXCEPTION("emcore_noti_init failed");
264                 return false;           
265         }
266
267         EM_DEBUG_FUNC_END();    
268         return true;
269 }
270
271 int emcore_mp_player_state_cb(int message, void *param, void *user_param)
272 {
273         switch (message)
274         {
275                 case MM_MESSAGE_ERROR:
276                         EM_DEBUG_LOG("Error is happened.");
277                         if (email_mmhandle) {
278                                 emcore_sound_mp_player_destory();
279                         }
280                         break;
281                 case MM_MESSAGE_BEGIN_OF_STREAM:
282                         EM_DEBUG_LOG("Play is started.");
283                         break;
284                 case MM_MESSAGE_END_OF_STREAM:
285                         EM_DEBUG_LOG("End of stream.");
286                         ENTER_CRITICAL_SECTION(mmhandle_mutex);
287                         if (email_mmhandle)
288                         {                       
289                                 emcore_sound_mp_player_stop();
290                                 emcore_sound_mp_player_destory();
291                         }
292                         LEAVE_CRITICAL_SECTION(mmhandle_mutex);
293                         break;
294                 default: 
295                         EM_DEBUG_LOG("Message = %d", message);
296                         break;
297         }
298         return 1;
299 }
300
301 bool emcore_sound_mp_player_create() 
302 {       
303         EM_DEBUG_FUNC_BEGIN();
304         int err = 0;
305         
306         if (email_mmhandle) {
307                 EM_DEBUG_LOG("already create the handle");
308                 return false;
309         }
310
311         if ((err = mm_player_create(&email_mmhandle)) != MM_ERROR_NONE) {
312                 EM_DEBUG_EXCEPTION("mm_player create fail [%d]", err);
313                 return false;
314         }       
315         EM_DEBUG_FUNC_END();
316         return true;
317 }
318
319 bool emcore_alarm_create() 
320 {       
321         EM_DEBUG_FUNC_BEGIN();
322
323         alarm_info = alarmmgr_create_alarm();
324
325         if (alarm_info == NULL) {
326                 EM_DEBUG_EXCEPTION("alarm create failed");
327                 return false;
328         }               
329
330         EM_DEBUG_FUNC_END();
331         return true;
332 }
333
334 bool emcore_alarm_destory()
335 {
336         EM_DEBUG_FUNC_BEGIN();
337
338         int ret;
339         ret = alarmmgr_free_alarm(alarm_info);
340
341         if (ret != ALARMMGR_RESULT_SUCCESS) {
342                 EM_DEBUG_EXCEPTION("alarm free failed");
343                 return false;
344         }               
345
346         EM_DEBUG_FUNC_END();
347         return true;
348 }
349
350 bool emcore_alert_create()
351 {
352         EM_DEBUG_FUNC_BEGIN();
353
354         /* Create the alarm handle */
355         if (!emcore_alarm_create()) {
356                 EM_DEBUG_EXCEPTION("emcore_alarm_create failed.");
357                 return false;
358         }
359 #if 0   
360         /* Set the music file in alert */
361         if (!emcore_set_mp_filepath(VCONFKEY_SETAPPL_NOTI_EMAIL_RINGTONE_PATH_STR)) {
362                 /* TODO : Add code to set default ringtone path */
363                 EM_DEBUG_EXCEPTION("emcore_set_mp_filepath failed.");
364                 return false;
365         }
366 #endif  
367         EM_DEBUG_FUNC_END();
368         return true;
369 }
370
371 bool emcore_alert_destory()
372 {
373         EM_DEBUG_FUNC_BEGIN();
374         int ret = 0;
375
376         /* Destroy the music player handle */
377         if (!emcore_sound_mp_player_destory()) {
378                 EM_DEBUG_EXCEPTION("emcore_sound_mp_player_destory fail");
379                 return false;
380         }                       
381
382         /* Destroy the alarm handle */
383         ret = alarmmgr_free_alarm(alarm_info);
384         if (ret != ALARMMGR_RESULT_SUCCESS) {
385                 EM_DEBUG_EXCEPTION("alarmmgr_free_alarm fail");
386                 return false;
387         }                       
388         
389         /* Set the music file in alert */
390         EM_SAFE_FREE(filename);
391         
392         EM_DEBUG_FUNC_END();
393         return true;
394 }
395
396 gboolean mp_player_timeout_cb(void *data)
397 {
398         EM_DEBUG_FUNC_BEGIN();
399
400         ENTER_CRITICAL_SECTION(mmhandle_mutex); 
401         if (email_mmhandle)
402         {                       
403                 emcore_sound_mp_player_stop();
404                 emcore_sound_mp_player_destory();
405         }
406         LEAVE_CRITICAL_SECTION(mmhandle_mutex);
407         
408         EM_DEBUG_FUNC_END();
409         return false;
410 }
411
412 bool emcore_vibration_start()
413 {
414         EM_DEBUG_FUNC_BEGIN();
415         int ret = false;
416         int error = FEEDBACK_ERROR_NONE;
417         int call_state = 0;
418
419         error = vconf_get_int(VCONFKEY_CALL_STATE, &call_state);
420         if (error == -1) {
421                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
422                 goto FINISH_OFF;
423         }
424
425         error = feedback_initialize();
426         if (error != FEEDBACK_ERROR_NONE) {
427                 EM_DEBUG_EXCEPTION("feedback_initialize failed : [%d]", error);
428                 goto FINISH_OFF;
429         }
430
431         if (call_state > VCONFKEY_CALL_OFF && call_state < VCONFKEY_CALL_STATE_MAX) {   
432                 error = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_EMAIL_ON_CALL);
433         } else {
434                 error = feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_EMAIL);
435         }
436
437         if (error != FEEDBACK_ERROR_NONE) {
438                 EM_DEBUG_EXCEPTION("feedback_play failed : [%d]", error);
439                 goto FINISH_OFF;
440         }
441
442         ret = true;
443
444 FINISH_OFF:
445
446         error = feedback_deinitialize();
447         if (error != FEEDBACK_ERROR_NONE) {
448                 EM_DEBUG_EXCEPTION("feedback_deinitialize failed : [%d]", error);
449         }
450         
451         EM_DEBUG_FUNC_END();
452         return ret;
453 }
454
455 int emcore_sound_mp_player_start(char *filepath)
456 {
457         EM_DEBUG_FUNC_BEGIN();
458
459         int err = MM_ERROR_NONE;
460
461 /*      
462         int volume = -1;
463
464
465         if ((err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_SOUND_VOLUME_INT, &volume)) == -1)
466         {
467                 EM_DEBUG_LOG("vconf_get_int failed \n");
468                 return err;
469         }
470 */
471         mm_player_set_message_callback(email_mmhandle, emcore_mp_player_state_cb, (void  *)email_mmhandle);
472
473         EM_DEBUG_LOG("Before mm_player_set_attribute filepath = %s", filepath);
474         if ((err = mm_player_set_attribute(email_mmhandle, NULL, "sound_volume_type", MM_SOUND_VOLUME_TYPE_NOTIFICATION, "profile_uri", filepath, EM_SAFE_STRLEN(filepath), NULL)) != MM_ERROR_NONE)
475         {
476                 EM_DEBUG_EXCEPTION("mm_player_set_attribute faile [ %d ] ", err);
477                 return err;
478         }
479
480         EM_DEBUG_LOG("After mm_player_set_attribute");  
481
482         if ((err = mm_player_realize(email_mmhandle)) != MM_ERROR_NONE)
483         {
484                 EM_DEBUG_EXCEPTION("mm_player_realize fail [%d]", err);
485                 return err;
486         }
487
488         if ((err = mm_player_start(email_mmhandle)) != MM_ERROR_NONE)
489         {
490                 EM_DEBUG_EXCEPTION("mm_player_start fail [%d]", err);
491                 return err;
492         }
493
494         if ((err = g_timeout_add(TIMER, (GSourceFunc)mp_player_timeout_cb, NULL) <= 0))
495         {
496                 EM_DEBUG_EXCEPTION("g_timeout_add - Failed to start timer");
497                 return err;             
498         }
499         
500         EM_DEBUG_FUNC_END();
501         return err;
502 }
503
504 int emcore_sound_mp_player_stop()
505 {
506         EM_DEBUG_FUNC_BEGIN();
507
508         int err = MM_ERROR_NONE;
509
510         if ((err = mm_player_stop(email_mmhandle)) != MM_ERROR_NONE)
511         {
512                 EM_DEBUG_EXCEPTION("mm_player_stop fail [%d]", err);
513                 return err;
514         }
515
516         if ((err = mm_player_unrealize(email_mmhandle)) != MM_ERROR_NONE)
517         {
518                 EM_DEBUG_EXCEPTION("mm_player_unrealize [%d]", err);
519                 return err;
520         }
521
522         EM_DEBUG_FUNC_END();
523         return err;
524 }
525
526 bool emcore_sound_mp_player_destory()
527 {
528         EM_DEBUG_FUNC_BEGIN();
529
530         int err = MM_ERROR_NONE;
531
532         if ((err = mm_player_destroy(email_mmhandle)) != MM_ERROR_NONE) {
533                 EM_DEBUG_EXCEPTION("mm_player_destory [%d]", err);
534                 return false;
535         }
536
537         email_mmhandle = 0;
538
539         EM_DEBUG_FUNC_END();
540         return true;
541 }       
542
543 int get_vconf_data(int key, int *return_value)
544 {
545         EM_DEBUG_FUNC_BEGIN();
546         int err = -1, value = 0;
547         
548         switch (key)
549         {
550                 case EMAIL_SOUND_STATUS:
551                         err = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &value);
552                         EM_DEBUG_LOG("EMAIL_SOUND_STATUS[%d]", value);
553                         break;
554                 case EMAIL_VIBE_STATUS:
555                         err = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &value);
556                         EM_DEBUG_LOG("EMAIL_VIBRATION_STATUS[%d]", value);
557                         break;
558                 case EMAIL_ALERT_REP_TYPE:
559                         err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_EMAIL_ALERT_REP_TYPE_INT, &value);
560                         EM_DEBUG_LOG("EMAIL_ALERT_REP_TYPE[%d]", value);
561                         break;
562                 case EMAIL_ALERT_VOLUME:
563                         err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_SOUND_VOLUME_INT, &value);
564                         EM_DEBUG_LOG("EMAIL_ALERT_VOLUME[%d]", value);
565                         break;
566                 case EMAIL_ALERT_VIBE_STENGTH:
567                         err = vconf_get_int(VCONFKEY_SETAPPL_NOTI_VIBRATION_LEVEL_INT, &value);
568                         EM_DEBUG_LOG("EMAIL_ALERT_VIBE_STENGTH[%d]", value);
569                         break;
570                 default: 
571                 {
572                         EM_DEBUG_LOG("Uuknown request\n");
573                         return false;                   
574                 }
575         }
576         
577         if (err == -1)
578         {
579                 EM_DEBUG_LOG("Vconf_get_int failed\n");
580                 return false;
581         }
582
583         *return_value = value;
584         EM_DEBUG_FUNC_END();
585         return true;
586 }
587
588 int emcore_get_alert_type()
589 {
590         EM_DEBUG_FUNC_BEGIN();
591         int sound_status = 0, vibe_status = 0;
592         int err;
593         int alert_type = -1;
594
595         if (!(err = get_vconf_data(EMAIL_SOUND_STATUS, &sound_status))) {
596                 EM_DEBUG_EXCEPTION("Don't get sound status");
597                 return err;
598         }
599
600         if (!(err = get_vconf_data(EMAIL_VIBE_STATUS, &vibe_status)))
601         {
602                 EM_DEBUG_EXCEPTION("Don't get vibration status");
603                 return err;
604         }
605
606         if (sound_status && vibe_status)
607                 alert_type = EMAIL_ALERT_TYPE_MELODY_AND_VIB;
608         else if (sound_status)
609                 alert_type = EMAIL_ALERT_TYPE_MELODY;
610         else if (vibe_status)
611                 alert_type = EMAIL_ALERT_TYPE_VIB;
612         else
613                 alert_type = EMAIL_ALERT_TYPE_MUTE;
614
615         return alert_type;
616 }
617
618
619 INTERNAL_FUNC int emcore_start_thread_for_alerting_new_mails(int *err_code)
620 {
621         EM_DEBUG_FUNC_BEGIN();
622
623         int thread_error;
624
625         if (err_code != NULL)
626                 *err_code = EMAIL_ERROR_NONE;
627         
628         if (g_alert_thread)
629         {
630                 EM_DEBUG_EXCEPTION("Alert service is already running...");
631                 if (err_code != NULL)
632                         *err_code = EMAIL_ERROR_UNKNOWN;
633                 
634                 return 1;
635         }
636         
637         THREAD_CREATE(g_alert_thread, start_alert_thread, NULL, thread_error);
638         if (thread_error != 0)
639         {
640                 EM_DEBUG_EXCEPTION("Cannot create alert thread");
641                 if (err_code != NULL)
642                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
643
644                 return -1;
645         }
646                 
647         if (err_code != NULL)
648                 *err_code = EMAIL_ERROR_NONE;
649
650         return 0;
651 }
652
653 int emcore_alarm_timeout_cb(int timer_id, void *user_parm)
654 {
655         EM_DEBUG_FUNC_BEGIN();
656
657         int err = EMAIL_ERROR_NONE;
658         int total_unread_count = 0;
659         int total_mail_count = 0;
660         email_mailbox_t mailbox;
661
662         memset(&mailbox, 0x00, sizeof(email_mailbox_t));
663
664         mailbox.account_id = ALL_ACCOUNT;
665         mailbox.mailbox_name = NULL;
666
667         if (!emcore_get_mail_count(&mailbox, &total_mail_count, &total_unread_count, &err)) {
668                 EM_DEBUG_EXCEPTION("emcore_get_mail_count failed - %d\n", err);
669                 return false;
670         }
671
672         EM_DEBUG_LOG(">>>> total_unread_count : [%d]\n", total_unread_count);
673         
674         if (total_unread_count) {
675                 emcore_start_alert();
676         }
677
678         EM_DEBUG_FUNC_END();    
679         return true;
680 }
681
682 bool set_alarm(int repetition_time)
683 {
684         EM_DEBUG_FUNC_BEGIN();
685
686         int ret = 0;
687         alarm_date_t alarm_date;
688         time_t current_time;
689         struct tm current_tm;
690
691         time(&current_time);
692         localtime_r(&current_time, &current_tm);
693         
694         alarm_date.year = 0;
695         alarm_date.month = 0;
696         alarm_date.day = 0;
697
698         EM_DEBUG_LOG("Current time : [%d]-[%d]-[%d]", current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec);
699         
700         if (current_tm.tm_min + repetition_time < 60) {
701                 alarm_date.hour = current_tm.tm_hour;
702                 alarm_date.min = current_tm.tm_min + repetition_time;
703         } else {
704                 if (current_tm.tm_hour < 12) {
705                         alarm_date.hour = current_tm.tm_hour + 1;
706                 } else {
707                         alarm_date.hour = (current_tm.tm_hour + 1) % 12;
708                 }
709
710                 alarm_date.min = (current_tm.tm_min + repetition_time) % 60;
711         }
712
713         alarm_date.sec = current_tm.tm_sec;
714         
715         alarmmgr_set_time(alarm_info, alarm_date);
716         alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, 0);
717         alarmmgr_set_type(alarm_info, ALARM_TYPE_VOLATILE);
718         alarmmgr_add_alarm_with_localtime(alarm_info, NULL, &email_alarm_id);
719
720         ret = alarmmgr_set_cb(emcore_alarm_timeout_cb, NULL);
721
722         if (ret != ALARMMGR_RESULT_SUCCESS) {
723                 EM_DEBUG_EXCEPTION("Failed : alarmmgr_set_cb() -> error[%d]", ret);
724                 return false;
725         }
726
727         EM_DEBUG_LOG("Alarm time : [%d]-[%d]-[%d]-[%d]-[%d]-[%d]", alarm_date.year, alarm_date.month, alarm_date.day, alarm_date.hour, alarm_date.min, alarm_date.sec);
728         EM_DEBUG_FUNC_END();
729         return true;
730 }
731
732 void emcore_set_repetition_alarm(int repetition)
733 {
734         EM_DEBUG_FUNC_BEGIN();
735
736         int repetition_time = 0;
737         
738         switch (repetition) {
739         case EMAIL_GCONF_VALUE_REPEAT_NONE:
740                 repetition_time = 0;
741                 break;
742         case EMAIL_GCONF_VALUE_REPEAT_2MINS:
743                 repetition_time = 2;
744                 break;
745         case EMAIL_GCONF_VALUE_REPEAT_5MINS:
746                 repetition_time = 5;
747                 break;
748         case EMAIL_GCONF_VALUE_REPEAT_10MINS:           
749                 repetition_time = 10;
750                 break;
751         default:
752                 EM_DEBUG_EXCEPTION("Invalid repetition time");
753                 return;
754         }
755
756         EM_DEBUG_LOG("repetition time is %d", repetition_time);
757
758         if (repetition_time > 0) {
759                 set_alarm(repetition_time);
760         } 
761
762         EM_DEBUG_FUNC_END();
763 }
764
765 void *start_alert_thread(void *arg)
766 {
767         EM_DEBUG_FUNC_BEGIN();
768         
769         int err = 0;
770         int repetition = 0;
771
772         if (!emcore_alert_init())
773         {
774                 EM_DEBUG_EXCEPTION("Error : emcore_alert_init failed");
775                 return 0;
776         }
777
778         while (1) {
779                 if (!emcore_alert_create()) {
780                         EM_DEBUG_EXCEPTION("Error : emcore_alert_create failed");
781                         return 0;
782                 }
783
784                 err = get_vconf_data(EMAIL_ALERT_REP_TYPE, &repetition);
785                 emcore_set_repetition_alarm(repetition);
786
787                 ENTER_CRITICAL_SECTION(sound_mutex);
788                 SLEEP_CONDITION_VARIABLE(sound_condition , sound_mutex);
789
790                 switch (emcore_get_alert_type())
791                 {
792                         case EMAIL_ALERT_TYPE_MELODY:
793                                 if (!emcore_sound_mp_player_create()) {
794                                         EM_DEBUG_LOG("emcore_sound_mp_player_create failed : [%d]", email_mmhandle);
795                                         break;
796                                 }
797                                 emcore_sound_mp_player_start(filename);
798                                 break;
799                         case EMAIL_ALERT_TYPE_VIB:
800                                 emcore_vibration_start();
801                                 break;
802                         case EMAIL_ALERT_TYPE_MELODY_AND_VIB:
803                                 emcore_vibration_start();
804                                 if (!emcore_sound_mp_player_create()) {
805                                         EM_DEBUG_LOG("emcore_sound_mp_player_create failed : [%d]", email_mmhandle);
806                                         break;
807                                 }
808                                 emcore_sound_mp_player_start(filename);
809                                 break;
810                         case EMAIL_ALERT_TYPE_MUTE:
811                                 EM_DEBUG_LOG("Alert type is mute!!");
812                                 break;
813                         default: 
814                                 EM_DEBUG_EXCEPTION("alert type is strange");
815                                 emcore_alert_destory();
816                                 break;
817                 }
818                 LEAVE_CRITICAL_SECTION(sound_mutex);
819                 EM_DEBUG_LOG("Start FINISH");
820                 emcore_alarm_destory();
821         }
822
823         EM_DEBUG_FUNC_END();
824         return 0;
825 }       
826
827 INTERNAL_FUNC void emcore_start_alert()
828 {
829         EM_DEBUG_FUNC_BEGIN("setting_noti_status : [%d]", setting_noti_status);
830
831         if (setting_noti_status == SETTING_NOTI_STATUS_OFF)
832                 return;
833
834         ENTER_CRITICAL_SECTION(sound_mutex);
835         WAKE_CONDITION_VARIABLE(sound_condition);
836         LEAVE_CRITICAL_SECTION(sound_mutex);
837 }