Remove unused vconf
[platform/core/messaging/msg-service.git] / utils / MsgNotificationWrapper.cpp
1 /*
2  * msg-service
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include "MsgDebug.h"
21 #include "MsgContact.h"
22 #include "MsgStorageTypes.h"
23 #include "MsgUtilStorage.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgNotificationWrapper.h"
26
27 extern "C"
28 {
29         #include <notification.h>
30         #include <appsvc.h>
31 }
32
33 /*==================================================================================================
34                                      FUNCTION IMPLEMENTATION
35 ==================================================================================================*/
36 void MsgSmsClass0Noti(MSG_MESSAGE_INFO_S* pMsg, notification_h noti, bundle* args)
37 {
38         MSG_BEGIN();
39         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
40         char tempId[6];
41
42         memset(tempId, 0x00, sizeof(tempId));
43
44         appsvc_set_appid(args, "org.tizen.msg-ui-class0");
45
46         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
47         if (noti_err != NOTIFICATION_ERROR_NONE) {
48                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
49         }
50
51         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
52         if (noti_err != NOTIFICATION_ERROR_NONE) {
53                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
54         }
55
56         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "CLASS 0 Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
57
58         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New CLASS 0 Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
59
60         if (pMsg->addressList[0].displayName[0] == '\0')
61                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
62         else
63                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
64
65         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, pMsg->msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
66
67         // set time.
68         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
69
70         // set led.
71         notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00);
72
73         bundle_add(args, "type", "new_msg");
74
75         snprintf(tempId, 5, "%d", pMsg->msgId);
76         bundle_add(args, "msgId", tempId);
77
78         MSG_END();
79 }
80
81
82 void MsgSmsMWINoti(MSG_MESSAGE_INFO_S* pMsg, notification_h noti, bundle* args)
83 {
84         MSG_BEGIN();
85
86         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
87
88         appsvc_set_appid(args, "org.tizen.call");
89
90         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
91         if (noti_err != NOTIFICATION_ERROR_NONE) {
92                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
93         }
94
95         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "MWI Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
96
97         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New MWI Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
98
99         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
100         if (noti_err != NOTIFICATION_ERROR_NONE) {
101                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
102         }
103
104         if (pMsg->addressList[0].displayName[0] == '\0')
105                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
106         else
107                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
108
109         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, pMsg->msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
110
111         // set time.
112         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
113
114         // set led.
115         notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00);
116
117         bundle_add(args, "launch-type", "MO");
118         bundle_add(args, "number", pMsg->addressList[0].addressVal);
119
120         MSG_END();
121 }
122
123
124 void MsgSmsVoiceNoti(MSG_MESSAGE_INFO_S* pMsg)
125 {
126         MSG_BEGIN();
127
128         int notiId = 0;
129         notification_h noti = NULL;
130         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
131         bundle* args;
132
133         args = bundle_create();
134
135         notiId = MsgSettingGetInt(VOICE_NOTI_ID_1);
136
137         if (notiId > 0) {
138                 noti = notification_load(NULL, notiId);
139                 if (noti == NULL)
140                         MSG_DEBUG("notification_load is failed.");
141         }
142
143         if (noti == NULL) {
144                 noti = notification_create(NOTIFICATION_TYPE_NOTI);
145                 if (noti == NULL) {
146                         MSG_DEBUG("notification_new is failed.");
147                         if (args != NULL) bundle_free(args);
148                         return;
149                 }
150
151                 notiId = 0;
152         }
153
154         appsvc_set_appid(args, "org.tizen.call");
155
156         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
157         if (noti_err != NOTIFICATION_ERROR_NONE) {
158                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
159         }
160
161         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Voicemail", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
162
163         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Voicemail", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
164
165         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
166         if (noti_err != NOTIFICATION_ERROR_NONE) {
167                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
168         }
169
170         if (pMsg->addressList[0].displayName[0] == '\0')
171                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
172         else
173                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
174
175         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, pMsg->msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
176
177         // set time.
178         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
179
180         // set led.
181         notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00);
182
183         bundle_add(args, "launch-type", "MO");
184         bundle_add(args, "number", pMsg->addressList[0].addressVal);
185
186         if (args != NULL) {
187                 noti_err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, args);
188                 if (noti_err != NOTIFICATION_ERROR_NONE) {
189                         MSG_DEBUG("Fail to notification_set_execute_option : %d", noti_err);
190                 }
191         }
192
193         if (notiId > 0) {
194                 noti_err = notification_update(noti);
195                 if (noti_err != NOTIFICATION_ERROR_NONE) {
196                         MSG_DEBUG("Fail to notification_update");
197                 }
198         } else {
199                 noti_err = notification_insert(noti, &notiId);
200                 if (noti_err != NOTIFICATION_ERROR_NONE) {
201                         MSG_DEBUG("Fail to notification_insert");
202                 }
203
204                 if (MsgSettingSetInt(VOICE_NOTI_ID_1, notiId) == MSG_SUCCESS)
205                         MSG_DEBUG("Insert VOICE_NOTI_ID_1 [%d]", notiId);
206                 else
207                         MSG_DEBUG("MsgSettingSetInt fail: VOICE_NOTI_ID_1");
208         }
209
210         noti_err = notification_free(noti);
211         if (noti_err != NOTIFICATION_ERROR_NONE) {
212                 MSG_DEBUG("Fail to notification_free");
213         }
214
215         if (args != NULL) {
216                 bundle_free(args);
217         }
218
219         MsgChangePmState();
220
221         MSG_END();
222 }
223
224
225 void MsgSmsReportNoti(MSG_MESSAGE_INFO_S* pMsg, notification_h noti, bundle* args)
226 {
227         MSG_BEGIN();
228
229         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
230
231         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
232         if (noti_err != NOTIFICATION_ERROR_NONE) {
233                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
234         }
235
236         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Delivery Report", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
237         if(pMsg->networkStatus == MSG_NETWORK_DELIVER_SUCCESS)
238                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "Message Delivered", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
239         else
240                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "Message delivery failed", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
241
242         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
243         if (noti_err != NOTIFICATION_ERROR_NONE) {
244                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
245         }
246
247         // get contact info.
248         MSG_CONTACT_INFO_S contactInfo;
249         memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
250         MsgGetContactInfo(&(pMsg->addressList[0]), &contactInfo);
251
252         int order = MsgGetContactNameOrder();
253
254         if (order == 0) {
255                 if (contactInfo.firstName[0] != '\0') {
256                         strncpy(pMsg->addressList[0].displayName, contactInfo.firstName, MAX_DISPLAY_NAME_LEN);
257                 }
258
259                 if (contactInfo.lastName[0] != '\0') {
260                         strncat(pMsg->addressList[0].displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(pMsg->addressList[0].displayName));
261                         strncat(pMsg->addressList[0].displayName, contactInfo.lastName, MAX_DISPLAY_NAME_LEN-strlen(pMsg->addressList[0].displayName));
262                 }
263         } else if (order == 1) {
264                 if (contactInfo.lastName[0] != '\0') {
265                         strncpy(pMsg->addressList[0].displayName, contactInfo.lastName, MAX_DISPLAY_NAME_LEN);
266                         strncat(pMsg->addressList[0].displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(pMsg->addressList[0].displayName));
267                 }
268
269                 if (contactInfo.firstName[0] != '\0') {
270                         strncat(pMsg->addressList[0].displayName, contactInfo.firstName, MAX_DISPLAY_NAME_LEN-strlen(pMsg->addressList[0].displayName));
271                 }
272         }
273
274         if (pMsg->addressList[0].displayName[0] == '\0')
275                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
276         else
277                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
278
279         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message delivered.", pMsg->msgText, NOTIFICATION_VARIABLE_TYPE_NONE);
280
281         // set time.
282         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
283
284         // set led.
285         notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00);
286
287         // set launch type
288         noti_err = notification_set_property(noti, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
289         if (noti_err != NOTIFICATION_ERROR_NONE)
290                 MSG_DEBUG("Fail to notification_set_display_applist");
291
292         MSG_END();
293 }
294
295
296 msg_error_t MsgInsertNoti(MSG_MESSAGE_INFO_S* pMsg)
297 {
298         MSG_DEBUG("Start to Insert Notification.");
299         notification_h noti = NULL;
300         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
301         bundle* args = NULL;
302
303         noti = notification_create(NOTIFICATION_TYPE_NOTI);
304         if (noti == NULL) {
305                 MSG_DEBUG("notification_create is failed.");
306                 return MSG_ERR_UNKNOWN;
307         }
308
309         switch(pMsg->msgType.subType)
310         {
311         case MSG_CB_SMS :
312                 noti_err = notification_free(noti);
313                 if (noti_err != NOTIFICATION_ERROR_NONE) {
314                         MSG_DEBUG("Fail to notification_free");
315                 }
316                 MsgRefreshCBNoti(true);
317                 return MSG_SUCCESS;
318                 break;
319         case MSG_MWI_FAX_SMS :
320         case MSG_MWI_EMAIL_SMS :
321         case MSG_MWI_OTHER_SMS :
322                 args = bundle_create();
323                 MsgSmsMWINoti(pMsg, noti, args);
324                 break;
325         case MSG_MWI_VOICE_SMS :
326                 noti_err = notification_free(noti);
327                 if (noti_err != NOTIFICATION_ERROR_NONE) {
328                         MSG_DEBUG("Fail to notification_free");
329                 }
330                 MsgSmsVoiceNoti(pMsg);
331                 return MSG_SUCCESS;
332                 break;
333         case MSG_STATUS_REPORT_SMS :
334                 //args = bundle_create();
335                 MsgSmsReportNoti(pMsg, noti, NULL);
336                 break;
337         case MSG_NORMAL_SMS :
338                 if (pMsg->msgType.classType == MSG_CLASS_0) {
339                         args = bundle_create();
340                         MsgSmsClass0Noti(pMsg, noti, args);
341                         break;
342                 } // Do not break here.
343         default :
344                 noti_err = notification_free(noti);
345                 if (noti_err != NOTIFICATION_ERROR_NONE) {
346                         MSG_DEBUG("Fail to notification_free");
347                 }
348                 MsgRefreshNoti(true);
349                 return MSG_SUCCESS;
350                 break;
351         }
352
353         if (args != NULL) {
354                 // set execute option
355                 noti_err = notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, args);
356                 if (noti_err != NOTIFICATION_ERROR_NONE) {
357                         MSG_DEBUG("Fail to notification_set_execute_option : %d", noti_err);
358                 }
359         }
360
361         noti_err = notification_insert(noti, NULL);
362         if (noti_err != NOTIFICATION_ERROR_NONE) {
363                 MSG_DEBUG("Fail to notification_insert");
364         }
365
366         noti_err = notification_free(noti);
367         if (noti_err != NOTIFICATION_ERROR_NONE) {
368                 MSG_DEBUG("Fail to notification_free");
369         }
370
371         if (args != NULL) {
372                 bundle_free(args);
373         }
374
375         MsgChangePmState();
376
377         return MSG_SUCCESS;
378 }
379
380
381 msg_error_t MsgInsertMmsReportToNoti(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S* pMsg)
382 {
383
384         notification_h noti = NULL;
385         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
386
387         char addressVal[MAX_ADDRESS_VAL_LEN+1];
388         char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
389         char displayName[MAX_DISPLAY_NAME_LEN+1];
390         char sqlQuery[MAX_QUERY_LEN+1];
391
392         memset(addressVal, 0x00, sizeof(addressVal));
393         memset(firstName, 0x00, sizeof(firstName));
394         memset(lastName, 0x00, sizeof(lastName));
395         memset(displayName, 0x00, sizeof(displayName));
396         memset(sqlQuery, 0x00, sizeof(sqlQuery));
397
398         int report_status_type;
399         int report_status_value;
400
401         if (pMsg->msgType.subType == MSG_DELIVERYIND_MMS) {
402                 report_status_type = MSG_REPORT_TYPE_DELIVERY;
403                 MSG_DEBUG("mms subtype is Delivery Report type");
404         } else if (pMsg->msgType.subType == MSG_READORGIND_MMS) {
405                 report_status_type = MSG_REPORT_TYPE_READ;
406                 MSG_DEBUG("mms subtype is Read Report type");
407         } else {
408                 MSG_DEBUG("No matching subtype. subtype [%d]", pMsg->msgType.subType);
409                 return MSG_SUCCESS;
410         }
411
412         MSG_ADDRESS_INFO_S *address_info_s = &pMsg->addressList[0];
413         MSG_DEBUG("address info : %s, type : %d", address_info_s->addressVal, address_info_s->addressType);
414
415         if (address_info_s->addressType == MSG_ADDRESS_TYPE_PLMN) {
416
417                 if (strlen(address_info_s->addressVal) > MAX_PRECONFIG_NUM) {
418                         char newPhoneNum[MAX_PRECONFIG_NUM+1];
419
420                         memset(newPhoneNum, 0x00, sizeof(newPhoneNum));
421
422                         MsgConvertNumber(address_info_s->addressVal, newPhoneNum);
423
424                         snprintf(sqlQuery, sizeof(sqlQuery),
425                                                 "SELECT A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, B.STATUS "
426                                                 "FROM %s A, %s B "
427                                                 "WHERE B.MSG_ID=%d AND B.STATUS_TYPE=%d AND A.ADDRESS_VAL LIKE '%%%s' AND B.ADDRESS_VAL LIKE \'%%%s\';"
428                                                 , MSGFW_ADDRESS_TABLE_NAME, MSGFW_REPORT_TABLE_NAME, pMsg->msgId, report_status_type, newPhoneNum, newPhoneNum);
429                 } else {
430
431                         snprintf(sqlQuery, sizeof(sqlQuery),
432                                 "SELECT A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, B.STATUS "
433                                 "FROM %s A, %s B "
434                                 "WHERE B.MSG_ID=%d AND B.STATUS_TYPE=%d AND A.ADDRESS_VAL LIKE '%s' AND B.ADDRESS_VAL LIKE '%s';"
435                                 , MSGFW_ADDRESS_TABLE_NAME, MSGFW_REPORT_TABLE_NAME, pMsg->msgId, report_status_type, address_info_s->addressVal, address_info_s->addressVal);
436                 }
437
438         } else if (address_info_s->addressType == MSG_ADDRESS_TYPE_EMAIL) {//check full
439                 snprintf(sqlQuery, sizeof(sqlQuery),
440                         "SELECT A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, B.STATUS "
441                         "FROM %s A, %s B "
442                         "WHERE B.MSG_ID=%d AND B.STATUS_TYPE=%d AND A.ADDRESS_VAL=\'%s\' AND B.ADDRESS_VAL LIKE \'%s\';"
443                         , MSGFW_ADDRESS_TABLE_NAME, MSGFW_REPORT_TABLE_NAME, pMsg->msgId, report_status_type, address_info_s->addressVal, address_info_s->addressVal);
444         } else {
445                 snprintf(sqlQuery, sizeof(sqlQuery),
446                         "SELECT A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, B.STATUS "
447                         "FROM %s A, %s B "
448                         "WHERE B.MSG_ID=%d B.STATUS_TYPE=%d;"
449                         , MSGFW_ADDRESS_TABLE_NAME, MSGFW_REPORT_TABLE_NAME, pMsg->msgId, report_status_type);
450         }
451
452         MSG_DEBUG("sqlQuery = [%s]", sqlQuery);
453
454         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
455                 return MSG_ERR_DB_PREPARE;
456
457         if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
458                 if (pDbHandle->columnText(0) != NULL) {
459                         strncpy(addressVal, (char*)pDbHandle->columnText(0), MAX_ADDRESS_VAL_LEN);
460                         MSG_DEBUG("addressVal is [%s]",addressVal);
461                 } else {
462                         MSG_DEBUG("address Val is Null");
463                 }
464
465                 char *pTempDisplayName = (char *)pDbHandle->columnText(1);
466                 if (pTempDisplayName != NULL && pTempDisplayName[0] != '\0') {
467                         strncpy(displayName, pTempDisplayName, MAX_DISPLAY_NAME_LEN);
468                 } else {
469                         if (pDbHandle->columnText(2) != NULL)
470                                 strncpy(firstName, (char*)pDbHandle->columnText(2), MAX_DISPLAY_NAME_LEN);
471
472                         if (pDbHandle->columnText(3) != NULL)
473                                 strncpy(lastName, (char*)pDbHandle->columnText(3), MAX_DISPLAY_NAME_LEN);
474
475                         int order = MsgGetContactNameOrder();
476
477                         if (order == 0) {
478                                 if (firstName[0] != '\0') {
479                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
480                                 }
481
482                                 if (lastName[0] != '\0') {
483                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
484                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
485                                 }
486                         } else if (order == 1) {
487                                 if (lastName[0] != '\0') {
488                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
489                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
490                                 }
491
492                                 if (firstName[0] != '\0') {
493                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
494                                 }
495                         }
496                 }
497
498                 report_status_value = pDbHandle->columnInt(4);
499                 MSG_DEBUG("report status [type = %d, value = %d]", report_status_type, report_status_value);
500         } else {
501                 MSG_DEBUG("DB Query Result Fail");
502                 pDbHandle->finalizeQuery();
503                 return MSG_ERR_DB_STEP;
504         }
505
506         pDbHandle->finalizeQuery();
507
508         noti = notification_create(NOTIFICATION_TYPE_NOTI);
509         if (noti == NULL) {
510                 MSG_DEBUG("notification_create is failed.");
511                 return MSG_ERR_UNKNOWN;
512         }
513
514         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
515         if (noti_err != NOTIFICATION_ERROR_NONE) {
516                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
517         }
518
519         if (pMsg->msgType.subType == MSG_DELIVERYIND_MMS) { // MMS delivery report
520
521                 noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
522                 if (noti_err != NOTIFICATION_ERROR_NONE) {
523                         MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
524                 }
525
526                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "New message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
527
528                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "Delivery Report", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
529
530         } else if (pMsg->msgType.subType == MSG_READORGIND_MMS) {  // MMS read report
531
532                 noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
533                 if (noti_err != NOTIFICATION_ERROR_NONE) {
534                         MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
535                 }
536
537                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "New message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
538
539                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "Read Report", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
540         }
541
542         if (displayName[0] == '\0')
543                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
544         else
545                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
546
547         // set launch type
548         noti_err = notification_set_property(noti, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
549         if (noti_err != NOTIFICATION_ERROR_NONE)
550                 MSG_DEBUG("Fail to notification_set_display_applist");
551
552         if (pMsg->msgType.subType == MSG_DELIVERYIND_MMS) {
553
554                 switch(report_status_value) {
555                 case MSG_DELIVERY_REPORT_NONE:
556                         noti_err = notification_free(noti);
557                         if (noti_err != NOTIFICATION_ERROR_NONE) {
558                                 MSG_DEBUG("Fail to notification_free");
559                         }
560
561                         return MSG_ERR_UNKNOWN;
562
563                 case MSG_DELIVERY_REPORT_EXPIRED:
564                         MSG_DEBUG("Message expired.");
565                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message expired", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
566                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message expired", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
567                         break;
568
569                 case MSG_DELIVERY_REPORT_REJECTED:
570                         MSG_DEBUG("Message rejected.");
571                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message rejected", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
572                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message rejected", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
573                         break;
574
575                 case MSG_DELIVERY_REPORT_DEFERRED:
576                         MSG_DEBUG("Message deferred.");
577                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message deferred", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
578                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message deferred", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
579                         break;
580
581                 case MSG_DELIVERY_REPORT_UNRECOGNISED:
582                         MSG_DEBUG("Message unrecognised.");
583                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message unrecognised", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
584                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message unrecognised", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
585                         break;
586
587                 case MSG_DELIVERY_REPORT_INDETERMINATE:
588                         MSG_DEBUG("Message indeterminate.");
589                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message indeterminate", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
590                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message indeterminate", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
591                         break;
592
593                 case MSG_DELIVERY_REPORT_FORWARDED:
594                         MSG_DEBUG("Message forwarded.");
595                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message forwarded", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
596                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message forwarded", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
597                         break;
598
599                 case MSG_DELIVERY_REPORT_UNREACHABLE:
600                         MSG_DEBUG("Message unreachable.");
601                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message unreachable", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
602                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message unreachable", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
603                         break;
604
605                 case MSG_DELIVERY_REPORT_ERROR:
606                         MSG_DEBUG("Message error.");
607                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message error", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
608                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message error", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
609                         break;
610
611                 default :
612                         MSG_DEBUG("Message delivered.");
613                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message delivered", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
614                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, "Message delivered", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
615                         break;
616                 }
617
618                 noti_err = notification_insert(noti, NULL);
619                 if (noti_err != NOTIFICATION_ERROR_NONE)
620                         MSG_DEBUG("Fail to notification_insert");
621
622         } else if (pMsg->msgType.subType == MSG_READORGIND_MMS) {
623
624                 switch(report_status_value) {
625                 case MSG_READ_REPORT_NONE:
626                         noti_err = notification_free(noti);
627                         if (noti_err != NOTIFICATION_ERROR_NONE)
628                                 MSG_DEBUG("Fail to notification_free");
629
630                         return MSG_ERR_UNKNOWN;
631
632                 case MSG_READ_REPORT_IS_DELETED:
633                         MSG_DEBUG("Message deleted.");
634                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message deleted", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
635                         break;
636
637                 default :
638                         MSG_DEBUG("Message read.");
639                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, "Message read", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
640                         break;
641                 }
642
643                 // set time.
644                 notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
645
646                 // set led.
647                 notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00);
648
649                 noti_err = notification_insert(noti, NULL);
650                 if (noti_err != NOTIFICATION_ERROR_NONE) {
651                         MSG_DEBUG("Fail to notification_insert");
652                 }
653         } else {
654                 MSG_DEBUG("No matching subtype. subtype [%d]", pMsg->msgType.subType);
655
656                 noti_err = notification_free(noti);
657                 if (noti_err != NOTIFICATION_ERROR_NONE) {
658                         MSG_DEBUG("Fail to notification_free");
659                 }
660                 return MSG_SUCCESS;
661         }
662
663         noti_err = notification_free(noti);
664         if (noti_err != NOTIFICATION_ERROR_NONE) {
665                 MSG_DEBUG("Fail to notification_free");
666         }
667
668         return MSG_SUCCESS;
669 }
670
671
672 msg_error_t MsgRefreshNoti(bool bWithTicker)
673 {
674
675         MsgDbHandler dbhandler;
676         MSG_MESSAGE_INFO_S msg = {0,};
677
678         int notiPrivId = MsgSettingGetInt(NOTIFICATION_PRIV_ID);
679
680         notification_h noti = NULL;
681         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
682         bundle* args = NULL;
683         bundle* reply = NULL;
684
685         int contactId = 0;
686         msg_thread_id_t threadId = 0;
687         time_t msgTime = 0;
688         char tempId[6];
689         char addressVal[MAX_ADDRESS_VAL_LEN+1];
690         char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
691         char displayName[MAX_DISPLAY_NAME_LEN+1];
692         char thumbPath[MAX_IMAGE_PATH_LEN+1];
693         char sqlQuery[MAX_QUERY_LEN+1];
694
695         memset(tempId, 0x00, sizeof(tempId));
696         memset(addressVal, 0x00, sizeof(addressVal));
697         memset(firstName, 0x00, sizeof(firstName));
698         memset(lastName, 0x00, sizeof(lastName));
699         memset(displayName, 0x00, sizeof(displayName));
700         memset(thumbPath, 0x00, sizeof(thumbPath));
701         memset(sqlQuery, 0x00, sizeof(sqlQuery));
702
703         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.CONV_ID, A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, "
704                         "B.DISPLAY_TIME, A.CONTACT_ID, A.IMAGE_PATH, B.MSG_ID, B.MSG_TEXT, B.SUBJECT, B.MAIN_TYPE "
705                         "FROM %s A, %s B WHERE A.CONV_ID=B.CONV_ID AND B.READ_STATUS=0 AND B.FOLDER_ID=%d AND "
706                         "(B.STORAGE_ID = %d OR B.STORAGE_ID = %d) "
707                         "ORDER BY B.DISPLAY_TIME DESC;",
708                         MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MSG_INBOX_ID, MSG_STORAGE_PHONE, MSG_STORAGE_SIM);
709
710         if (dbhandler.prepareQuery(sqlQuery) != MSG_SUCCESS)
711                 return MSG_ERR_DB_PREPARE;
712
713         if (dbhandler.stepQuery() == MSG_ERR_DB_ROW) {
714                 threadId = dbhandler.columnInt(0);
715
716                 if (dbhandler.columnText(1) != NULL)
717                         strncpy(addressVal, (char*)dbhandler.columnText(1), MAX_ADDRESS_VAL_LEN);
718
719
720                 char *pTempDisplayName = (char *)dbhandler.columnText(2);
721                 if (pTempDisplayName != NULL && pTempDisplayName[0] != '\0') {
722                         strncpy(displayName, pTempDisplayName, MAX_DISPLAY_NAME_LEN);
723                 } else {
724                         if (dbhandler.columnText(3) != NULL)
725                                 strncpy(firstName, (char*)dbhandler.columnText(3), MAX_DISPLAY_NAME_LEN);
726
727                         if (dbhandler.columnText(4) != NULL)
728                                 strncpy(lastName, (char*)dbhandler.columnText(4), MAX_DISPLAY_NAME_LEN);
729
730                         int order = MsgGetContactNameOrder();
731
732                         if (order == 0) {
733                                 if (firstName[0] != '\0') {
734                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
735                                 }
736
737                                 if (lastName[0] != '\0') {
738                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
739                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
740                                 }
741                         } else if (order == 1) {
742                                 if (lastName[0] != '\0') {
743                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
744                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
745                                 }
746
747                                 if (firstName[0] != '\0') {
748                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
749                                 }
750                         }
751                 }
752
753                 msgTime = (time_t)dbhandler.columnInt(5);
754
755                 contactId = dbhandler.columnInt(6);
756
757                 strncpy(thumbPath, (char*)dbhandler.columnText(7), MAX_IMAGE_PATH_LEN);
758
759                 msg.msgId = dbhandler.columnInt(8);
760
761                 strncpy(msg.msgText, (char*)dbhandler.columnText(9), MAX_MSG_TEXT_LEN);
762
763                 strncpy(msg.subject, (char*)dbhandler.columnText(10), MAX_SUBJECT_LEN);
764
765                 msg.msgType.mainType = dbhandler.columnInt(11);
766
767                 MSG_DEBUG("unread message [%d].", msg.msgId);
768         } else {
769
770                 MSG_DEBUG("No unread message.");
771                 MSG_DEBUG("notiPrivId [%d]", notiPrivId);
772
773                 dbhandler.finalizeQuery();
774
775                 // No unread message.
776                 if (notiPrivId > 0) {
777                         noti_err = notification_delete_by_priv_id("8r4r5ddzzn.Messages", NOTIFICATION_TYPE_NOTI, notiPrivId);
778                         if (noti_err != NOTIFICATION_ERROR_NONE) {
779                                 MSG_DEBUG("Fail to notification_delete_by_priv_id : %d", noti_err);
780                         }
781                 }
782
783                 notiPrivId = 0;
784
785                 if(MsgSettingSetInt(NOTIFICATION_PRIV_ID, notiPrivId) != MSG_SUCCESS)
786                         MSG_DEBUG("MsgSettingSetInt fail : NOTIFICATION_PRIV_ID");
787
788                 return MSG_ERR_DB_STEP;
789         }
790
791         dbhandler.finalizeQuery();
792
793         int unreadMsgCnt = 0;
794 #if 0
795         unreadMsgCnt = MsgStoGetUnreadCnt(&dbhandler, MSG_SMS_TYPE);
796         unreadMsgCnt += MsgStoGetUnreadCnt(&dbhandler, MSG_MMS_TYPE);
797 #else
798         if (dbhandler.getTable(sqlQuery , &unreadMsgCnt) != MSG_SUCCESS) {
799                 MSG_DEBUG("Fail to getTable");
800                 dbhandler.freeTable();
801                 return MSG_ERR_DB_GETTABLE;
802         }
803
804         dbhandler.freeTable();
805 #endif
806
807         MSG_DEBUG("notiPrivId [%d], unreadMsgCnt [%d]", notiPrivId, unreadMsgCnt);
808
809         if (notiPrivId > 0) {
810                 noti = notification_load(NULL, notiPrivId);
811                 if (noti == NULL)
812                         MSG_DEBUG("notification_load is failed.");
813         }
814
815         if (noti == NULL) {
816                 noti = notification_create(NOTIFICATION_TYPE_NOTI);
817                 if (noti == NULL) {
818                         MSG_DEBUG("notification_new is failed.");
819                         return MSG_ERR_UNKNOWN;
820                 }
821
822                 notiPrivId = 0;
823         }
824
825         // set pkg name.
826         noti_err = notification_set_pkgname(noti, "8r4r5ddzzn.Messages");
827         if (noti_err != NOTIFICATION_ERROR_NONE) {
828                 MSG_DEBUG("Fail to notification_set_pkgname : %d", noti_err);
829         }
830
831         // create bundle
832         args = bundle_create();
833
834         appsvc_set_appid(args, "8r4r5ddzzn.Messages");
835
836         // Set bundle values
837         memset(&tempId, 0x00, sizeof(tempId));
838         bundle_add(args, "type", "new_msg");
839         snprintf(tempId, 5, "%d", msg.msgId);
840         bundle_add(args, "msgId", tempId);
841
842         if (unreadMsgCnt > 1) {
843
844                 noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_MULTIPLE);
845                 if (noti_err != NOTIFICATION_ERROR_NONE) {
846                         MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
847                 }
848
849                 noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
850                 if (noti_err != NOTIFICATION_ERROR_NONE) {
851                         MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
852                 }
853
854                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
855
856                 char unreadMsgCntStr[5] = {0,};
857                 snprintf(unreadMsgCntStr, 5, "%d", unreadMsgCnt);
858                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, unreadMsgCntStr, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
859
860                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Messages", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
861
862                 // set execute option
863                 notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH, NULL, NULL, args);
864
865                 notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_RESPONDING, NULL, NULL, NULL);
866
867         } else {
868
869                 noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
870                 if (noti_err != NOTIFICATION_ERROR_NONE) {
871                         MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
872                 }
873
874                 noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
875                 if (noti_err != NOTIFICATION_ERROR_NONE) {
876                         MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
877                 }
878
879                 noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_INDICATOR, NORMAL_MSG_ICON_PATH);
880                 if (noti_err != NOTIFICATION_ERROR_NONE) {
881                         MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
882                 }
883
884                 noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON_FOR_LOCK, NORMAL_MSG_ICON_PATH);
885                 if (noti_err != NOTIFICATION_ERROR_NONE) {
886                         MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
887                 }
888
889                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
890
891                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
892
893                 // set execute option
894                 notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, args);
895
896                 // Set responding option
897                 reply = bundle_create();
898                 appsvc_set_appid(reply, "8r4r5ddzzn.Messages");
899
900                 bundle_add(reply, "type", "reply");
901                 bundle_add(reply, "show_list", "list_show");
902
903                 memset(&tempId, 0x00, sizeof(tempId));
904                 snprintf(tempId, 5, "%d", msg.msgId);
905                 bundle_add(reply, "msgId", tempId);
906
907                 notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_RESPONDING, NULL, NULL, reply);
908         }
909
910
911         if (displayName[0] == '\0')
912                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
913         else
914                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
915
916         if (msg.msgType.mainType == MSG_SMS_TYPE)
917                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2,msg.msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
918         else
919                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, msg.subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
920
921         // set time.
922         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, msgTime);
923
924         // set led.
925         notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00);
926
927         notification_set_property(noti, NOTIFICATION_PROP_DISABLE_AUTO_DELETE);
928
929         if (bWithTicker)
930                 noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_ALL);
931         else
932                 noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_ALL^NOTIFICATION_DISPLAY_APP_TICKER);
933
934         if (noti_err != NOTIFICATION_ERROR_NONE) {
935                 MSG_DEBUG("Fail to notification_set_display_applist : %d", noti_err);
936         }
937
938         if (notiPrivId > 0) {
939                 noti_err = notification_update(noti);
940                 if (noti_err != NOTIFICATION_ERROR_NONE) {
941                         MSG_DEBUG("Fail to notification_update");
942                 }
943         } else {
944                 noti_err = notification_insert(noti, &notiPrivId);
945                 if (noti_err != NOTIFICATION_ERROR_NONE) {
946                         MSG_DEBUG("Fail to notification_insert");
947                 }
948
949                 if (MsgSettingSetInt(NOTIFICATION_PRIV_ID, notiPrivId) != MSG_SUCCESS)
950                         MSG_DEBUG("MsgSettingSetInt fail: NOTIFICATION_PRIV_ID");
951                 MSG_DEBUG("Insert notiPrivId [%d]", notiPrivId);
952         }
953
954         noti_err = notification_free(noti);
955         if (noti_err != NOTIFICATION_ERROR_NONE) {
956                 MSG_DEBUG("Fail to notification_free");
957         }
958
959         if (args != NULL) {
960                 bundle_free(args);
961         }
962
963         if (reply != NULL) {
964                 bundle_free(reply);
965         }
966
967         return MSG_SUCCESS;
968 }
969
970
971 msg_error_t MsgRefreshCBNoti(bool bWithTicker)
972 {
973         MsgDbHandler dbhandler;
974
975         int notiCbId = MsgSettingGetInt(CB_NOTI_PRIV_ID);
976
977         notification_h noti = NULL;
978         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
979         bundle* args = NULL;
980
981         int contactId = 0;
982         msg_thread_id_t threadId = 0;
983         msg_message_id_t msgId = 0;
984         time_t msgTime = 0;
985         char tempId[6];
986         char addressVal[MAX_ADDRESS_VAL_LEN+1];
987         char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
988         char displayName[MAX_DISPLAY_NAME_LEN+1];
989         char thumbPath[MAX_IMAGE_PATH_LEN+1];
990         char msgText[MAX_MSG_TEXT_LEN+1];
991         char sqlQuery[MAX_QUERY_LEN+1];
992
993         memset(tempId, 0x00, sizeof(tempId));
994         memset(addressVal, 0x00, sizeof(addressVal));
995         memset(firstName, 0x00, sizeof(firstName));
996         memset(lastName, 0x00, sizeof(lastName));
997         memset(displayName, 0x00, sizeof(displayName));
998         memset(thumbPath, 0x00, sizeof(thumbPath));
999         memset(msgText, 0x00, sizeof(msgText));
1000         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1001
1002         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.CONV_ID, A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, "
1003                         "B.DISPLAY_TIME, A.CONTACT_ID, A.IMAGE_PATH, B.MSG_ID, B.MSG_TEXT "
1004                         "FROM %s A, %s B WHERE A.CONV_ID=B.CONV_ID AND B.READ_STATUS=0 AND B.FOLDER_ID=%d AND B.STORAGE_ID = %d "
1005                         "ORDER BY B.DISPLAY_TIME DESC;",
1006                         MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE);
1007
1008         if (dbhandler.prepareQuery(sqlQuery) != MSG_SUCCESS)
1009                 return MSG_ERR_DB_PREPARE;
1010
1011         if (dbhandler.stepQuery() == MSG_ERR_DB_ROW) {
1012                 threadId = dbhandler.columnInt(0);
1013
1014                 if (dbhandler.columnText(1) != NULL)
1015                         strncpy(addressVal, (char*)dbhandler.columnText(1), MAX_ADDRESS_VAL_LEN);
1016
1017
1018                 char *pTempDisplayName = (char *)dbhandler.columnText(2);
1019                 if (pTempDisplayName != NULL && pTempDisplayName[0] != '\0') {
1020                         strncpy(displayName, pTempDisplayName, MAX_DISPLAY_NAME_LEN);
1021                 } else {
1022                         if (dbhandler.columnText(3) != NULL)
1023                                 strncpy(firstName, (char*)dbhandler.columnText(3), MAX_DISPLAY_NAME_LEN);
1024
1025                         if (dbhandler.columnText(4) != NULL)
1026                                 strncpy(lastName, (char*)dbhandler.columnText(4), MAX_DISPLAY_NAME_LEN);
1027
1028                         int order = MsgGetContactNameOrder();
1029
1030                         if (order == 0) {
1031                                 if (firstName[0] != '\0') {
1032                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
1033                                 }
1034
1035                                 if (lastName[0] != '\0') {
1036                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
1037                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
1038                                 }
1039                         } else if (order == 1) {
1040                                 if (lastName[0] != '\0') {
1041                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
1042                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
1043                                 }
1044
1045                                 if (firstName[0] != '\0') {
1046                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
1047                                 }
1048                         }
1049                 }
1050
1051                 msgTime = (time_t)dbhandler.columnInt(5);
1052
1053                 contactId = dbhandler.columnInt(6);
1054
1055                 strncpy(thumbPath, (char*)dbhandler.columnText(7), MAX_IMAGE_PATH_LEN);
1056
1057                 msgId = dbhandler.columnInt(8);
1058
1059                 strncpy(msgText, (char*)dbhandler.columnText(9), MAX_MSG_TEXT_LEN);
1060
1061                 MSG_DEBUG("unread CB message [%d].", msgId);
1062         } else {
1063
1064                 MSG_DEBUG("No unread CB message.");
1065                 MSG_DEBUG("notiCbId [%d]", notiCbId);
1066
1067                 dbhandler.finalizeQuery();
1068
1069                 // No unread message.
1070                 if (notiCbId > 0) {
1071                         noti_err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NOTI, notiCbId);
1072                         if (noti_err != NOTIFICATION_ERROR_NONE) {
1073                                 MSG_DEBUG("Fail to notification_delete_by_priv_id : %d", noti_err);
1074                         }
1075                 }
1076
1077                 notiCbId = 0;
1078
1079                 if(MsgSettingSetInt(CB_NOTI_PRIV_ID, notiCbId) != MSG_SUCCESS)
1080                         MSG_DEBUG("MsgSettingSetInt fail : CB_NOTI_PRIV_ID");
1081
1082                 return MSG_ERR_DB_STEP;
1083         }
1084
1085         dbhandler.finalizeQuery();
1086
1087         int unreadCbMsgCnt = 0;
1088
1089         if (dbhandler.getTable(sqlQuery, &unreadCbMsgCnt) != MSG_SUCCESS) {
1090                 MSG_DEBUG("getTable is failed");
1091                 dbhandler.freeTable();
1092                 return MSG_ERR_DB_GETTABLE;
1093         }
1094
1095         MSG_DEBUG("notiCbId [%d], unreadCbMsgCnt [%d]", notiCbId, unreadCbMsgCnt);
1096
1097         if (notiCbId > 0) {
1098                 noti = notification_load(NULL, notiCbId);
1099                 if (noti == NULL)
1100                         MSG_DEBUG("notification_load is failed.");
1101         }
1102
1103         if (noti == NULL) {
1104                 noti = notification_create(NOTIFICATION_TYPE_NOTI);
1105                 if (noti == NULL) {
1106                         MSG_DEBUG("notification_new is failed.");
1107                         return MSG_ERR_UNKNOWN;
1108                 }
1109                 notiCbId = 0;
1110         }
1111
1112         args = bundle_create();
1113
1114         appsvc_set_appid(args, "8r4r5ddzzn.Messages");
1115
1116         // Set bundle values
1117         memset(&tempId, 0x00, sizeof(tempId));
1118         bundle_add(args, "type", "new_msg");
1119         snprintf(tempId, 5, "%d", msgId);
1120         bundle_add(args, "msgId", tempId);
1121
1122         if (unreadCbMsgCnt > 1) {
1123                 noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_MULTIPLE);
1124                 if (noti_err != NOTIFICATION_ERROR_NONE) {
1125                         MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
1126                 }
1127
1128                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Broadcast message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1129
1130                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Messages", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1131
1132                 char unreadCbMsgCntStr[5] = {0,};
1133                 snprintf(unreadCbMsgCntStr, 5, "%d", unreadCbMsgCnt);
1134                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, unreadCbMsgCntStr, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1135
1136                 notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH, NULL, NULL, args);
1137         } else {
1138                 noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
1139                 if (noti_err != NOTIFICATION_ERROR_NONE) {
1140                         MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
1141                 }
1142
1143                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Broadcast message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1144
1145                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1146
1147                 notification_set_execute_option(noti, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, args);
1148         }
1149
1150
1151         if (displayName[0] == '\0')
1152                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1153         else
1154                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1155
1156         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
1157
1158         // set time.
1159         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, msgTime);
1160
1161         // set icon image
1162         notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
1163
1164         // set led.
1165         notification_set_led(noti, NOTIFICATION_LED_OP_ON, 0x00);
1166
1167         if (bWithTicker)
1168                 noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_ALL^NOTIFICATION_DISPLAY_APP_LOCK);
1169         else
1170                 noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_NOTIFICATION_TRAY|NOTIFICATION_DISPLAY_APP_INDICATOR);
1171
1172         if (noti_err != NOTIFICATION_ERROR_NONE) {
1173                 MSG_DEBUG("Fail to notification_set_display_applist : %d", noti_err);
1174         }
1175
1176         if (notiCbId > 0) {
1177                 noti_err = notification_update(noti);
1178                 if (noti_err != NOTIFICATION_ERROR_NONE) {
1179                         MSG_DEBUG("Fail to notification_update");
1180                 }
1181         } else {
1182                 noti_err = notification_insert(noti, &notiCbId);
1183                 if (noti_err != NOTIFICATION_ERROR_NONE) {
1184                         MSG_DEBUG("Fail to notification_insert");
1185                 }
1186
1187                 if (MsgSettingSetInt(CB_NOTI_PRIV_ID, notiCbId) != MSG_SUCCESS)
1188                         MSG_DEBUG("MsgSettingSetInt fail: CB_NOTI_PRIV_ID");
1189                 MSG_DEBUG("Insert notiCbId [%d]", notiCbId);
1190         }
1191
1192         noti_err = notification_free(noti);
1193         if (noti_err != NOTIFICATION_ERROR_NONE) {
1194                 MSG_DEBUG("Fail to notification_free");
1195         }
1196
1197         if (args != NULL) {
1198                 bundle_free(args);
1199         }
1200
1201         return MSG_SUCCESS;
1202 }
1203
1204
1205 msg_error_t MsgCleanAndResetNoti()
1206 {
1207         msg_error_t err = MSG_SUCCESS;
1208         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1209
1210         noti_err = notification_delete_all_by_type("8r4r5ddzzn.Messages", NOTIFICATION_TYPE_NOTI);
1211         if (noti_err != NOTIFICATION_ERROR_NONE) {
1212                 MSG_DEBUG("Fail to notification_delete_all_by_type noti_err [%d]", noti_err);
1213         }
1214
1215         err = MsgRefreshNoti(false);
1216         if (err != MSG_SUCCESS) {
1217                 MSG_DEBUG("Fail to MsgRefreshNoti : [err=%d]", err);
1218         }
1219
1220         err = MsgRefreshCBNoti(false);
1221         if (err != MSG_SUCCESS) {
1222                 MSG_DEBUG("Fail to MsgRefreshCBNoti : [err=%d]", err);
1223         }
1224
1225         return err;
1226 }
1227
1228
1229 msg_error_t MsgInsertTicker(const char* pTickerMsg, const char* pLocaleTickerMsg)
1230 {
1231         notification_h noti = NULL;
1232         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1233
1234         noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
1235         if (noti == NULL) {
1236                 MSG_DEBUG("notification_new is failed.");
1237                 return MSG_ERR_UNKNOWN;
1238         }
1239
1240         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
1241         if (noti_err != NOTIFICATION_ERROR_NONE) {
1242                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
1243         }
1244
1245         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, pTickerMsg, pLocaleTickerMsg, NOTIFICATION_VARIABLE_TYPE_NONE);
1246         if (noti_err != NOTIFICATION_ERROR_NONE) {
1247                 MSG_DEBUG("Fail to notification_set_text : %d", noti_err);
1248         }
1249
1250         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, pTickerMsg, pLocaleTickerMsg, NOTIFICATION_VARIABLE_TYPE_NONE);
1251         if (noti_err != NOTIFICATION_ERROR_NONE) {
1252                 MSG_DEBUG("Fail to notification_set_text : %d", noti_err);
1253         }
1254
1255         noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_TICKER);
1256         if (noti_err != NOTIFICATION_ERROR_NONE) {
1257                 MSG_DEBUG("Fail to notification_set_display_applist : %d", noti_err);
1258         }
1259
1260         noti_err = notification_set_property(noti, NOTIFICATION_PROP_DISABLE_APP_LAUNCH);
1261         if (noti_err != NOTIFICATION_ERROR_NONE)
1262                 MSG_DEBUG("Fail to notification_set_display_applist");
1263
1264         noti_err = notification_insert(noti, NULL);
1265         if (noti_err != NOTIFICATION_ERROR_NONE) {
1266                 MSG_DEBUG("Fail to notification_insert");
1267         }
1268
1269         noti_err = notification_free(noti);
1270         if (noti_err != NOTIFICATION_ERROR_NONE) {
1271                 MSG_DEBUG("Fail to notification_free");
1272         }
1273
1274         return MSG_SUCCESS;
1275 }
1276
1277
1278 msg_error_t MsgInsertBadge(unsigned int unreadMsgCnt)
1279 {
1280         return MSG_SUCCESS;
1281 }
1282
1283
1284 msg_error_t MsgClearVoiceNoti(MSG_SUB_TYPE_T subType)
1285 {
1286         MSG_BEGIN();
1287
1288         msg_error_t err = MSG_SUCCESS;
1289         int notiId = 0;
1290         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
1291
1292         if (subType == MSG_MWI_VOICE_SMS) {
1293                 notiId = MsgSettingGetInt(VOICE_NOTI_ID_1);
1294         } else {
1295                 return MSG_ERR_UNKNOWN;
1296         }
1297
1298         if (notiId>0) {
1299                 noti_err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NOTI, notiId);
1300                 if (noti_err != NOTIFICATION_ERROR_NONE) {
1301                         MSG_DEBUG("Fail to notification_delete_by_priv_id : %d", noti_err);
1302                         err = MSG_ERR_UNKNOWN;
1303                 }
1304         }
1305
1306         notiId = 0;
1307
1308         if (subType == MSG_MWI_VOICE_SMS) {
1309                 if (MsgSettingSetInt(VOICE_NOTI_ID_1, notiId) == MSG_SUCCESS)
1310                         MSG_DEBUG("Insert VOICE_NOTI_ID_1 [%d]", notiId);
1311                 else
1312                         MSG_DEBUG("MsgSettingSetInt fail: VOICE_NOTI_ID_1");
1313         }
1314
1315         MSG_END();
1316
1317         return err;
1318 }