apply Tizen 3.0 Message UX Guide
[platform/core/messaging/msg-service.git] / manager / src / msg-manager.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 /*==================================================================================================
18                                          INCLUDE FILES
19 ==================================================================================================*/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <glib.h>
24
25 extern "C"
26 {
27 #include <tizen.h>
28 #include <service_app.h>
29 #include <app_event.h>
30 #include <notification_list.h>
31 #include <notification_text_domain.h>
32 #include <notification_internal.h>
33 #include <vconf.h>
34 #include <call-manager.h>
35 }
36
37 #include "msg.h"
38 #include "msg_storage.h"
39
40 #include "msg-manager-contact.h"
41 #include "msg-manager-debug.h"
42 #include "msg-manager-notification.h"
43 #include "msg-manager-sound.h"
44 #include "msg-manager-util.h"
45
46 /*==================================================================================================
47                                      VARIABLES
48 ==================================================================================================*/
49 msg_handle_t msg_handle = NULL;
50 cm_client_h cm_handle = NULL;
51
52 pthread_mutex_t mx;
53 static int job_cnt = 0;
54 static bool terminated = false;
55
56 /*==================================================================================================
57                             FUNCTION IMPLEMENTATION
58 ==================================================================================================*/
59 static int _service_app_exit(void *data)
60 {
61         MSG_MGR_INFO("kill msg-manager");
62         service_app_exit();
63
64         return 0;
65 }
66
67 static int _check_app_terminate(void *data)
68 {
69         pthread_mutex_lock(&mx);
70
71         job_cnt--;
72         MSG_MGR_DEBUG("job_cnt [%d]", job_cnt);
73         if (job_cnt == 0) {
74                 terminated = true;
75                 g_idle_add(_service_app_exit, NULL);
76         }
77
78         pthread_mutex_unlock(&mx);
79
80         return 0;
81 }
82
83 bool service_app_create(void *data)
84 {
85         MSG_MGR_INFO("app_create");
86
87         int msg_server_ready = 0;
88         for (int i = 0; i < 100; i++) {
89                 vconf_get_bool(VCONFKEY_MSG_SERVER_READY, &msg_server_ready);
90                 if (msg_server_ready == 1) {
91                         int msg_err = msg_open_msg_handle(&msg_handle);
92                         if (msg_err != MSG_SUCCESS)
93                                 MSG_MGR_ERR("msg_open_msg_handle() failed [%d]", msg_err);
94                         else
95                                 MSG_MGR_DEBUG("msg_open_msg_handle() success");
96
97                         break;
98                 } else {
99                         MSG_MGR_DEBUG("msg-server is not ready.");
100                         sleep(1);
101                 }
102         }
103
104         if (msg_handle == NULL)
105                 return false;
106
107         MsgMgrInitNoti();
108         initMsgMgrSoundPlayer();
109         cm_init(&cm_handle);
110
111         return true;
112 }
113
114 void service_app_terminate(void *data)
115 {
116         MSG_MGR_INFO("app_terminate");
117
118         cm_deinit(cm_handle);
119         MsgMgrCloseContactSvc();
120         msg_close_msg_handle(&msg_handle);
121
122         return;
123 }
124
125 void _incoming_msg_func(app_control_h app_control)
126 {
127         MSG_MGR_BEGIN();
128
129         int ret = 0;
130         char *rcv_msg_type = NULL;
131         char *rcv_msg_id = NULL;
132
133         ret = app_control_get_extra_data(app_control, EVENT_KEY_MSG_ID, &rcv_msg_id);
134         if (ret != APP_CONTROL_ERROR_NONE || rcv_msg_id == NULL) {
135                 MSG_MGR_ERR("app_control_get_extra_data failed");
136                 return;
137         }
138
139         ret = app_control_get_extra_data(app_control, EVENT_KEY_MSG_TYPE, &rcv_msg_type);
140         if (ret != APP_CONTROL_ERROR_NONE || rcv_msg_type == NULL) {
141                 MSG_MGR_ERR("app_control_get_extra_data failed");
142                 g_free(rcv_msg_id);
143                 return;
144         }
145
146         MSG_MGR_INFO("rcv_msg_type(%s), rcv_msg_id(%s)", rcv_msg_type, rcv_msg_id);
147
148         int msg_err = MSG_SUCCESS;
149         msg_message_id_t msg_id = atoi(rcv_msg_id);
150         msg_struct_t msg = NULL;
151         msg_struct_t opt = NULL;
152         contactInfo contact_info = {0, };
153         contact_info.msgId = msg_id;
154
155         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
156         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
157         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
158         if (msg_err != MSG_SUCCESS) {
159                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
160                 return;
161         }
162
163         msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &contact_info.msgType);
164         msg_get_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, &contact_info.folderId);
165         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &contact_info.simIndex);
166         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&contact_info.addrList);
167         msg_get_str_value(msg, MSG_MESSAGE_SUBJECT_STR, contact_info.subject, MAX_CONTACT_TEXT_LEN);
168         int msgSize = 0;
169         msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msgSize);
170         if (msgSize > 0)
171                 msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, contact_info.msgText, MAX_CONTACT_TEXT_LEN);
172
173         if ((contact_info.folderId == MSG_INBOX_ID || contact_info.folderId == MSG_SPAMBOX_ID)) {
174                 MsgMgrAddPhoneLog(&contact_info);
175         }
176
177         msg_release_struct(&msg);
178         msg_release_struct(&opt);
179
180         g_free(rcv_msg_id);
181         g_free(rcv_msg_type);
182
183         MSG_MGR_END();
184 }
185
186 void _outgoing_msg_func(app_control_h app_control)
187 {
188         MSG_MGR_BEGIN();
189
190         int ret = 0;
191         char *sent_msg_type = NULL;
192         char *sent_msg_id = NULL;
193
194         ret = app_control_get_extra_data(app_control, EVENT_KEY_OUT_MSG_ID, &sent_msg_id);
195         if (ret != APP_CONTROL_ERROR_NONE || sent_msg_id == NULL) {
196                 MSG_MGR_ERR("app_control_get_extra_data failed");
197                 return;
198         }
199
200         ret = app_control_get_extra_data(app_control, EVENT_KEY_OUT_MSG_TYPE, &sent_msg_type);
201         if (ret != APP_CONTROL_ERROR_NONE || sent_msg_type == NULL) {
202                 MSG_MGR_ERR("app_control_get_extra_data failed");
203                 g_free(sent_msg_id);
204                 return;
205         }
206
207         MSG_MGR_INFO("sent_msg_type(%s) sent_msg_id(%s)", sent_msg_type, sent_msg_id);
208
209         int msg_err = MSG_SUCCESS;
210         msg_message_id_t msg_id = atoi(sent_msg_id);
211         msg_struct_t msg = NULL;
212         msg_struct_t opt = NULL;
213         contactInfo contact_info = {0, };
214         contact_info.msgId = msg_id;
215
216         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
217         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
218         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
219         if (msg_err != MSG_SUCCESS) {
220                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
221                 return;
222         }
223
224         msg_get_int_value(msg, MSG_MESSAGE_TYPE_INT, &contact_info.msgType);
225         msg_get_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, &contact_info.folderId);
226         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &contact_info.simIndex);
227         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&contact_info.addrList);
228         msg_get_str_value(msg, MSG_MESSAGE_SUBJECT_STR, contact_info.subject, 100);
229         int msgSize = 0;
230         msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msgSize);
231         if (msgSize > 0)
232                 msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, contact_info.msgText, 100);
233
234         MsgMgrAddPhoneLog(&contact_info);
235
236         msg_release_struct(&msg);
237         msg_release_struct(&opt);
238
239         g_free(sent_msg_id);
240         g_free(sent_msg_type);
241
242         MSG_MGR_END();
243 }
244
245 void _refresh_noti_func(app_control_h app_control)
246 {
247         char *type = NULL;
248         char *feedback = NULL;
249         char *active_type = NULL;
250         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
251         bool bFeedback = true;
252         msg_mgr_active_notification_type_t active_noti_type = MSG_MGR_ACTIVE_NOTI_TYPE_NONE;
253
254         int ret = app_control_get_extra_data(app_control, "type", &type);
255         if (ret == APP_CONTROL_ERROR_NONE && type) {
256                 MSG_MGR_DEBUG("type [%s]", type);
257                 if (g_strcmp0(type, "normal") == 0)
258                         noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
259                 else if (g_strcmp0(type, "failed") == 0)
260                         noti_type = MSG_MGR_NOTI_TYPE_FAILED;
261
262                 g_free(type);
263         } else {
264                 MSG_MGR_ERR("app_control_get_extra_data failed");
265                 return;
266         }
267
268         ret = app_control_get_extra_data(app_control, "feedback", &feedback);
269         if (ret == APP_CONTROL_ERROR_NONE && feedback) {
270                 MSG_MGR_DEBUG("feedback [%s]", feedback);
271                 if (g_strcmp0(feedback, "false") == 0)
272                         bFeedback = false;
273                 else if (g_strcmp0(feedback, "true") == 0)
274                         bFeedback = true;
275
276                 g_free(feedback);
277         }
278
279         ret = app_control_get_extra_data(app_control, "active_type", &active_type);
280         if (ret == APP_CONTROL_ERROR_NONE && active_type) {
281                 MSG_MGR_DEBUG("active_type [%s]", active_type);
282                 if (g_strcmp0(active_type, "none") == 0)
283                         active_noti_type = MSG_MGR_ACTIVE_NOTI_TYPE_NONE;
284                 else if (g_strcmp0(active_type, "active") == 0)
285                         active_noti_type = MSG_MGR_ACTIVE_NOTI_TYPE_ACTIVE;
286                 else if (g_strcmp0(active_type, "instant") == 0)
287                         active_noti_type = MSG_MGR_ACTIVE_NOTI_TYPE_INSTANT;
288
289                 g_free(active_type);
290         }
291
292         MsgMgrRefreshNotification(noti_type, bFeedback, active_noti_type);
293 }
294
295 void _add_noti_func(app_control_h app_control)
296 {
297         char *type;
298         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_ALL;
299
300         int ret = app_control_get_extra_data(app_control, "type", &type);
301         if (ret == APP_CONTROL_ERROR_NONE && type) {
302                 if (g_strcmp0(type, "voice1") == 0) {
303                         noti_type = MSG_MGR_NOTI_TYPE_VOICE_1;
304                 } else if (g_strcmp0(type, "voice2") == 0) {
305                         noti_type = MSG_MGR_NOTI_TYPE_VOICE_2;
306                 } else if (g_strcmp0(type, "mwi") == 0) {
307                         noti_type = MSG_MGR_NOTI_TYPE_MWI;
308                 } else if (g_strcmp0(type, "class0") == 0) {
309                         noti_type = MSG_MGR_NOTI_TYPE_CLASS0;
310                 } else if (g_strcmp0(type, "read_report_sent") == 0) {
311                         noti_type = MSG_MGR_NOTI_TYPE_MMS_READ_REPORT_SENT;
312                 }
313
314                 g_free(type);
315         } else {
316                 MSG_MGR_ERR("app_control_get_extra_data failed");
317                 return;
318         }
319
320         char *msgId = NULL;
321         ret = app_control_get_extra_data(app_control, "msg_id", &msgId);
322         if (ret != APP_CONTROL_ERROR_NONE || msgId == NULL) {
323                 MSG_MGR_ERR("app_control_get_extra_data failed");
324                 return;
325         }
326
327         int msg_err = MSG_SUCCESS;
328         msg_message_id_t msg_id = atoi(msgId);
329         msg_struct_t msg = NULL;
330         msg_struct_t opt = NULL;
331         msg_list_handle_t addr_list = NULL;
332         MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
333         msg_info.msgId = msg_id;
334
335         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
336         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
337         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
338         if (msg_err != MSG_SUCCESS) {
339                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
340                 return;
341         }
342
343         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &msg_info.sim_idx);
344         msg_get_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, (int *)&msg_info.displayTime);
345         msg_get_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, (int *)&msg_info.networkStatus);
346         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
347
348         msg_struct_t addr_data = msg_list_nth_data(addr_list, 0);
349         msg_get_str_value(addr_data, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, msg_info.addressVal, MAX_ADDRESS_VAL_LEN);
350         msg_get_str_value(addr_data, MSG_ADDRESS_INFO_DISPLAYNAME_STR, msg_info.displayName, MAX_DISPLAY_NAME_LEN);
351
352         if (noti_type == MSG_MGR_NOTI_TYPE_MWI || noti_type == MSG_MGR_NOTI_TYPE_CLASS0) {
353                 int msg_size = 0;
354                 msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msg_size);
355                 if (msg_size > 0)
356                         msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, msg_info.msgText, MAX_MSG_TEXT_LEN);
357         }
358
359         msg_release_struct(&msg);
360         msg_release_struct(&opt);
361
362         g_free(msgId);
363
364         MsgMgrAddNotification(noti_type, &msg_info);
365 }
366
367 void _del_noti_func(app_control_h app_control)
368 {
369         char *type;
370         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_ALL;
371
372         int ret = app_control_get_extra_data(app_control, "type", &type);
373         if (ret == APP_CONTROL_ERROR_NONE && type) {
374                 if (g_strcmp0(type, "all") == 0) {
375                         noti_type = MSG_MGR_NOTI_TYPE_ALL;
376                 } else if (g_strcmp0(type, "normal") == 0) {
377                         noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
378                 } else if (g_strcmp0(type, "sim") == 0) {
379                         noti_type = MSG_MGR_NOTI_TYPE_SIM;
380                 } else if (g_strcmp0(type, "voice1") == 0) {
381                         noti_type = MSG_MGR_NOTI_TYPE_VOICE_1;
382                 } else if (g_strcmp0(type, "voice2") == 0) {
383                         noti_type = MSG_MGR_NOTI_TYPE_VOICE_2;
384                 }
385
386                 g_free(type);
387         } else {
388                 MSG_MGR_ERR("app_control_get_extra_data failed");
389                 return;
390         }
391
392         char *simIndex = NULL;
393         ret = app_control_get_extra_data(app_control, "sim_index", &simIndex);
394         if (ret != APP_CONTROL_ERROR_NONE || simIndex == NULL) {
395                 MSG_MGR_ERR("app_control_get_extra_data failed");
396                 return;
397         }
398
399         int sim_index = atoi(simIndex);
400
401         MsgMgrDeleteNoti(noti_type, sim_index);
402
403         g_free(simIndex);
404 }
405
406 void _add_report_noti_func(app_control_h app_control)
407 {
408         char *type;
409         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_ALL;
410
411         int ret = app_control_get_extra_data(app_control, "type", &type);
412         if (ret == APP_CONTROL_ERROR_NONE && type) {
413                 if (g_strcmp0(type, "sms_delivery") == 0) {
414                         noti_type = MSG_MGR_NOTI_TYPE_SMS_DELIVERY_REPORT;
415                 } else if (g_strcmp0(type, "mms_delivery") == 0) {
416                         noti_type = MSG_MGR_NOTI_TYPE_MMS_DELIVERY_REPORT;
417                 } else if (g_strcmp0(type, "mms_read") == 0) {
418                         noti_type = MSG_MGR_NOTI_TYPE_MMS_READ_REPORT;
419                 }
420
421                 g_free(type);
422         } else {
423                 MSG_MGR_ERR("app_control_get_extra_data failed");
424                 return;
425         }
426
427         char *msgId = NULL;
428         ret = app_control_get_extra_data(app_control, "msg_id", &msgId);
429         if (ret != APP_CONTROL_ERROR_NONE || msgId == NULL) {
430                 MSG_MGR_ERR("app_control_get_extra_data failed");
431                 return;
432         }
433
434         int msg_err = MSG_SUCCESS;
435         msg_message_id_t msg_id = atoi(msgId);
436         msg_struct_t msg = NULL;
437         msg_struct_t opt = NULL;
438         msg_list_handle_t addr_list = NULL;
439         MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
440         msg_info.msgId = msg_id;
441
442         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
443         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
444         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
445         if (msg_err != MSG_SUCCESS) {
446                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
447                 return;
448         }
449
450         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &msg_info.sim_idx);
451         msg_get_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, (int *)&msg_info.displayTime);
452         msg_get_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, (int *)&msg_info.networkStatus);
453         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
454
455         msg_struct_t addr_data = msg_list_nth_data(addr_list, 0);
456         msg_get_str_value(addr_data, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, msg_info.addressVal, MAX_ADDRESS_VAL_LEN);
457         msg_get_str_value(addr_data, MSG_ADDRESS_INFO_DISPLAYNAME_STR, msg_info.displayName, MAX_DISPLAY_NAME_LEN);
458
459         msg_release_struct(&msg);
460         msg_release_struct(&opt);
461
462         g_free(msgId);
463
464         MsgMgrAddReportNotification(noti_type, &msg_info);
465 }
466
467 void _del_report_noti_func(app_control_h app_control)
468 {
469         char *addr = NULL;
470         int ret = app_control_get_extra_data(app_control, "address", &addr);
471         if (ret != APP_CONTROL_ERROR_NONE || addr == NULL) {
472                 MSG_MGR_ERR("app_control_get_extra_data failed");
473                 return;
474         }
475
476         MsgMgrDeleteReportNotification(addr);
477
478         g_free(addr);
479 }
480
481 void _del_sent_read_report_noti_func(app_control_h app_control)
482 {
483         MsgMgrDeleteSentReadReportNotification();
484 }
485
486 void _insert_only_active_noti_func(app_control_h app_control)
487 {
488         char *type;
489         msg_mgr_notification_type_t noti_type = MSG_MGR_NOTI_TYPE_ALL;
490
491         int ret = app_control_get_extra_data(app_control, "type", &type);
492         if (ret == APP_CONTROL_ERROR_NONE && type) {
493                 if (g_strcmp0(type, "normal") == 0) {
494                         noti_type = MSG_MGR_NOTI_TYPE_NORMAL;
495                 } else if (g_strcmp0(type, "class0") == 0) {
496                         noti_type = MSG_MGR_NOTI_TYPE_CLASS0;
497                 }
498
499                 g_free(type);
500         } else {
501                 MSG_MGR_ERR("app_control_get_extra_data failed");
502                 return;
503         }
504
505         char *msgId = NULL;
506         ret = app_control_get_extra_data(app_control, "msg_id", &msgId);
507         if (ret != APP_CONTROL_ERROR_NONE || msgId == NULL) {
508                 MSG_MGR_ERR("app_control_get_extra_data failed");
509                 return;
510         }
511
512         int msg_err = MSG_SUCCESS;
513         msg_message_id_t msg_id = atoi(msgId);
514         msg_struct_t msg = NULL;
515         msg_struct_t opt = NULL;
516         msg_list_handle_t addr_list = NULL;
517         MSG_MGR_MESSAGE_INFO_S msg_info = {0, };
518         msg_info.msgId = msg_id;
519
520         msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
521         opt = msg_create_struct(MSG_STRUCT_SENDOPT);
522         msg_err = msg_get_message(msg_handle, msg_id, msg, opt);
523         if (msg_err != MSG_SUCCESS) {
524                 MSG_MGR_ERR("msg_get_message() failed [%d]", msg_err);
525                 return;
526         }
527
528         msg_get_int_value(msg, MSG_MESSAGE_SIM_INDEX_INT, &msg_info.sim_idx);
529
530         if (noti_type == MSG_MGR_NOTI_TYPE_CLASS0) {
531                 msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
532
533                 msg_struct_t addr_data = msg_list_nth_data(addr_list, 0);
534                 msg_get_str_value(addr_data, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, msg_info.addressVal, MAX_ADDRESS_VAL_LEN);
535                 msg_get_str_value(addr_data, MSG_ADDRESS_INFO_DISPLAYNAME_STR, msg_info.displayName, MAX_DISPLAY_NAME_LEN);
536
537                 int msg_size = 0;
538                 msg_get_int_value(msg, MSG_MESSAGE_DATA_SIZE_INT, &msg_size);
539                 if (msg_size > 0)
540                         msg_get_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, msg_info.msgText, MAX_MSG_TEXT_LEN);
541         }
542
543         msg_release_struct(&msg);
544         msg_release_struct(&opt);
545
546         g_free(msgId);
547
548         MsgMgrInsertOnlyActiveNotification(noti_type, &msg_info);
549 }
550
551 void _insert_ticker_func(app_control_h app_control)
552 {
553         char *ticker_msg = NULL;
554         char *locale_ticker_msg = NULL;
555         char *feedback_str = NULL;
556         bool feedback = false;
557         char *msg_id_str = NULL;
558         int msg_id = 0;
559
560         int ret = app_control_get_extra_data(app_control, "ticker_msg", &ticker_msg);
561         if (ret != APP_CONTROL_ERROR_NONE || ticker_msg == NULL) {
562                 MSG_MGR_ERR("app_control_get_extra_data failed [%d]", ret);
563                 goto _END_OF_INSERT_TICKER;
564         }
565
566         ret = app_control_get_extra_data(app_control, "locale_ticker_msg", &locale_ticker_msg);
567         if (ret != APP_CONTROL_ERROR_NONE || locale_ticker_msg == NULL) {
568                 MSG_MGR_ERR("app_control_get_extra_data failed [%d]", ret);
569                 goto _END_OF_INSERT_TICKER;
570         }
571
572         ret = app_control_get_extra_data(app_control, "feedback", &feedback_str);
573         if (ret != APP_CONTROL_ERROR_NONE || feedback_str == NULL) {
574                 MSG_MGR_ERR("app_control_get_extra_data failed [%d]", ret);
575                 goto _END_OF_INSERT_TICKER;
576         } else {
577                 if (g_strcmp0(feedback_str, "true") == 0)
578                         feedback = true;
579                 else
580                         feedback = false;
581         }
582
583         ret = app_control_get_extra_data(app_control, "msg_id", &msg_id_str);
584         if (ret != APP_CONTROL_ERROR_NONE || msg_id_str == NULL) {
585                 MSG_MGR_ERR("app_control_get_extra_data failed [%d]", ret);
586                 goto _END_OF_INSERT_TICKER;
587         } else {
588                 msg_id = atoi(msg_id_str);
589         }
590
591         ret = MsgMgrInsertTicker(ticker_msg, locale_ticker_msg, feedback, msg_id);
592         if (ret != 0)
593                 MSG_MGR_ERR("MsgMgrInsertTicker failed [%d]", ret);
594
595 _END_OF_INSERT_TICKER:
596         if (ticker_msg)
597                 g_free(ticker_msg);
598
599         if (locale_ticker_msg)
600                 g_free(locale_ticker_msg);
601
602         if (feedback_str)
603                 g_free(feedback_str);
604
605         if (msg_id_str)
606                 g_free(msg_id_str);
607 }
608
609 void _sound_play_start_func(app_control_h app_control)
610 {
611         char *type;
612         MSG_MGR_SOUND_TYPE_T sound_type = MSG_MGR_SOUND_PLAY_DEFAULT;
613
614         int ret = app_control_get_extra_data(app_control, "type", &type);
615         if (ret == APP_CONTROL_ERROR_NONE && type) {
616                 if (g_strcmp0(type, "default") == 0) {
617                         sound_type = MSG_MGR_SOUND_PLAY_DEFAULT;
618                 } else if (g_strcmp0(type, "user") == 0) {
619                         sound_type = MSG_MGR_SOUND_PLAY_USER;
620                 } else if (g_strcmp0(type, "emergency") == 0) {
621                         sound_type = MSG_MGR_SOUND_PLAY_EMERGENCY;
622                 } else if (g_strcmp0(type, "voicemail") == 0) {
623                         sound_type = MSG_MGR_SOUND_PLAY_VOICEMAIL;
624                 }
625
626                 g_free(type);
627         } else {
628                 MSG_MGR_ERR("app_control_get_extra_data failed");
629                 return;
630         }
631
632         char *addr = NULL;
633         ret = app_control_get_extra_data(app_control, "address", &addr);
634
635         if (addr) {
636                 MSG_MGR_ADDRESS_INFO_S addr_info = {0, };
637                 snprintf(addr_info.addressVal, MAX_ADDRESS_VAL_LEN, "%s", addr);
638
639                 MsgMgrSoundPlayStart(&addr_info, sound_type);
640
641                 g_free(addr);
642         } else {
643                 MsgMgrSoundPlayStart(NULL, sound_type);
644         }
645 }
646
647 void _change_pm_state_func(app_control_h app_control)
648 {
649         MsgMgrChangePmState();
650 }
651
652 void _on_boot_func(app_control_h app_control)
653 {
654         MsgMgrOnBoot();
655 }
656
657 void service_app_control(app_control_h app_control, void *data)
658 {
659         MSG_MGR_INFO("service_app_control called");
660
661         pthread_mutex_lock(&mx);
662         job_cnt++;
663         pthread_mutex_unlock(&mx);
664
665         int ret = 0;
666         char *operation = NULL;
667         char *cmd = NULL;
668
669         ret = app_control_get_operation(app_control, &operation);
670         if (ret == APP_CONTROL_ERROR_NONE && operation) {
671                 if (g_strcmp0(operation, APP_CONTROL_OPERATION_DEFAULT) == 0) {
672                         ret = app_control_get_extra_data(app_control, "cmd", &cmd);
673                         if (ret == APP_CONTROL_ERROR_NONE && cmd) {
674                                 MSG_MGR_DEBUG("cmd [%s]", cmd);
675
676                                 if (g_strcmp0(cmd, "incoming_msg") == 0) {
677                                         _incoming_msg_func(app_control);
678                                 } else if (g_strcmp0(cmd, "outgoing_msg") == 0) {
679                                         _outgoing_msg_func(app_control);
680                                 } else if (g_strcmp0(cmd, "refresh_noti") == 0) {
681                                         _refresh_noti_func(app_control);
682                                 } else if (g_strcmp0(cmd, "add_noti") == 0) {
683                                         _add_noti_func(app_control);
684                                 } else if (g_strcmp0(cmd, "del_noti") == 0) {
685                                         _del_noti_func(app_control);
686                                 } else if (g_strcmp0(cmd, "add_report_noti") == 0) {
687                                         _add_report_noti_func(app_control);
688                                 } else if (g_strcmp0(cmd, "del_report_noti") == 0) {
689                                         _del_report_noti_func(app_control);
690                                 } else if (g_strcmp0(cmd, "del_sent_read_report_noti") == 0) {
691                                         _del_sent_read_report_noti_func(app_control);
692                                 } else if (g_strcmp0(cmd, "insert_only_active_noti") == 0) {
693                                         _insert_only_active_noti_func(app_control);
694                                 } else if (g_strcmp0(cmd, "insert_ticker") == 0) {
695                                         _insert_ticker_func(app_control);
696                                 } else if (g_strcmp0(cmd, "sound_play_start") == 0) {
697                                         _sound_play_start_func(app_control);
698                                 } else if (g_strcmp0(cmd, "change_pm_state") == 0) {
699                                         _change_pm_state_func(app_control);
700                                 } else if (g_strcmp0(cmd, "on_boot") == 0) {
701                                         _on_boot_func(app_control);
702                                 }
703
704                                 g_free(cmd);
705                         }
706                 }
707                 g_free(operation);
708         }
709
710         pthread_mutex_lock(&mx);
711         if (!terminated)
712                 g_timeout_add_seconds(60, _check_app_terminate, NULL);
713         pthread_mutex_unlock(&mx);
714
715         return;
716 }
717
718 int main(int argc, char* argv[])
719 {
720         service_app_lifecycle_callback_s event_callback = {0, };
721
722         event_callback.create = service_app_create;
723         event_callback.terminate = service_app_terminate;
724         event_callback.app_control = service_app_control;
725
726         return service_app_main(argc, argv, &event_callback, NULL);
727 }