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