Modify flora license version.
[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 }
28
29 /*==================================================================================================
30                                      FUNCTION IMPLEMENTATION
31 ==================================================================================================*/
32 void MsgSmsClass0Noti(MSG_MESSAGE_INFO_S* pMsg, notification_h noti, bundle* args)
33 {
34         MSG_BEGIN();
35         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
36         char tempId[6];
37
38         memset(tempId, 0x00, sizeof(tempId));
39
40         noti_err = notification_set_application(noti, "org.tizen.msg-ui-class0");
41         if (noti_err != NOTIFICATION_ERROR_NONE) {
42                 MSG_DEBUG("Fail to notification_set_application : %d", noti_err);
43         }
44
45         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
46         if (noti_err != NOTIFICATION_ERROR_NONE) {
47                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
48         }
49
50         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
51         if (noti_err != NOTIFICATION_ERROR_NONE) {
52                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
53         }
54
55         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "CLASS 0 Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
56
57         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New CLASS 0 Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
58
59         if (pMsg->addressList[0].displayName[0] == '\0')
60                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
61         else
62                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
63
64         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, pMsg->msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
65
66         // set time.
67         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
68
69         bundle_add(args, "type", "msg_id");
70
71         snprintf(tempId, 5, "%d", pMsg->msgId);
72         bundle_add(args, "msgId", tempId);
73
74         MSG_END();
75 }
76
77 void MsgSmsCBNoti(MSG_MESSAGE_INFO_S* pMsg, notification_h noti, bundle* args)
78 {
79         MSG_BEGIN();
80
81         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
82         char tempId[6];
83
84         memset(tempId, 0x00, sizeof(tempId));
85
86         noti_err = notification_set_application(noti, "8r4r5ddzzn.Messages");
87         if (noti_err != NOTIFICATION_ERROR_NONE) {
88                 MSG_DEBUG("Fail to notification_set_application : %d", noti_err);
89         }
90
91         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
92         if (noti_err != NOTIFICATION_ERROR_NONE) {
93                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
94         }
95
96         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, CB_MSG_ICON_PATH);
97         if (noti_err != NOTIFICATION_ERROR_NONE) {
98                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
99         }
100
101         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "CB Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
102
103         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New CB Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
104
105         if (pMsg->addressList[0].displayName[0] == '\0')
106                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
107         else
108                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
109
110         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, pMsg->msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
111
112         // set time.
113         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
114
115         memset(&tempId, 0x00, sizeof(tempId));
116
117         bundle_add(args, "type", "msg_id");
118
119         snprintf(tempId, 5, "%d", pMsg->msgId);
120         bundle_add(args, "msgId", tempId);
121
122         MSG_END();
123 }
124
125 void MsgSmsVoiceNoti(MSG_MESSAGE_INFO_S* pMsg, notification_h noti, bundle* args)
126 {
127         MSG_BEGIN();
128
129         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
130
131         noti_err = notification_set_application(noti, "org.tizen.call");
132         if (noti_err != NOTIFICATION_ERROR_NONE) {
133                 MSG_DEBUG("Fail to notification_set_application : %d", noti_err);
134         }
135
136         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
137         if (noti_err != NOTIFICATION_ERROR_NONE) {
138                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
139         }
140
141         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Voice Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
142
143         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Voice Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
144
145         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, VOICE_MSG_ICON_PATH);
146         if (noti_err != NOTIFICATION_ERROR_NONE) {
147                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
148         }
149
150         if (pMsg->addressList[0].displayName[0] == '\0')
151                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
152         else
153                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
154
155         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, pMsg->msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
156
157         // set time.
158         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
159
160         //FIXME :: Temp code for voice number, 2012.08.16 sangkoo.kim
161         bundle_add(args, "launch-type", "MO");
162         bundle_add(args, "number", pMsg->addressList[0].addressVal);
163
164         MSG_END();
165 }
166
167
168 void MsgSmsReportNoti(MSG_MESSAGE_INFO_S* pMsg, notification_h noti, bundle* args)
169 {
170         MSG_BEGIN();
171
172         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
173         char tempId[6];
174
175         memset(tempId, 0x00, sizeof(tempId));
176
177         noti_err = notification_set_application(noti, "8r4r5ddzzn.Messages");
178         if (noti_err != NOTIFICATION_ERROR_NONE) {
179                 MSG_DEBUG("Fail to notification_set_application : %d", noti_err);
180         }
181         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
182         if (noti_err != NOTIFICATION_ERROR_NONE) {
183                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
184         }
185
186         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Delivery Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
187
188         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Delivery Message", NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
189
190         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NOTI_MSG_ICON_PATH);
191         if (noti_err != NOTIFICATION_ERROR_NONE) {
192                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
193         }
194
195         if (pMsg->addressList[0].displayName[0] == '\0')
196                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
197         else
198                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, pMsg->addressList[0].displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
199
200         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, pMsg->msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
201
202         // set time.
203         notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, pMsg->displayTime);
204
205         // get msg id
206         MsgDbHandler dbhandler;
207         char sqlQuery[MAX_QUERY_LEN+1];
208         memset(sqlQuery, 0x00, sizeof(sqlQuery));
209         snprintf(sqlQuery, sizeof(sqlQuery),
210                         "SELECT MSG_ID "
211                         "FROM %s "
212                         "WHERE CONV_ID IN (SELECT CONV_ID FROM %s WHERE ADDRESS_VAL LIKE '%%%s') AND FOLDER_ID=%d "
213                         "ORDER BY DISPLAY_TIME DESC;"
214                         , MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, pMsg->addressList[0].addressVal, MSG_SENTBOX_ID);
215
216         MSG_DEBUG("sqlQuery - %s", sqlQuery);
217
218         if (dbhandler.prepareQuery(sqlQuery) == MSG_SUCCESS) {
219
220                 if (dbhandler.stepQuery() == MSG_ERR_DB_ROW) {
221                         memset(&tempId, 0x00, sizeof(tempId));
222
223                         bundle_add(args, "type", "report");
224
225                         snprintf(tempId, 5, "%d", dbhandler.columnInt(0));
226                         bundle_add(args, "msgId", tempId);
227
228                         MSG_DEBUG("msgId [%s] add.", tempId);
229                 }
230
231                 dbhandler.finalizeQuery();
232         }
233
234         MSG_END();
235 }
236
237
238 msg_error_t MsgInsertNoti(MSG_MESSAGE_INFO_S* pMsg)
239 {
240         MSG_DEBUG("Start to Insert Notification.");
241         notification_h noti = NULL;
242         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
243         bundle* args = NULL;
244
245         noti = notification_create(NOTIFICATION_TYPE_NOTI);
246         if (noti == NULL) {
247                 MSG_DEBUG("notification_create is failed.");
248                 return MSG_ERR_UNKNOWN;
249         }
250
251         if (pMsg->msgType.mainType == MSG_SMS_TYPE) {
252                 switch(pMsg->msgType.subType)
253                 {
254                 case MSG_CB_SMS :
255                         args = bundle_create();
256                         MsgSmsCBNoti(pMsg, noti, args);
257                         break;
258                 case MSG_MWI_VOICE_SMS :
259                 case MSG_MWI_FAX_SMS :
260                 case MSG_MWI_EMAIL_SMS :
261                 case MSG_MWI_OTHER_SMS :
262                         args = bundle_create();
263                         MsgSmsVoiceNoti(pMsg, noti, args);
264                         break;
265                 case MSG_STATUS_REPORT_SMS :
266                         args = bundle_create();
267                         MsgSmsReportNoti(pMsg, noti, args);
268                         break;
269                 default :
270                         MsgRefreshNoti(true);
271                         noti_err = notification_free(noti);
272                         if (noti_err != NOTIFICATION_ERROR_NONE) {
273                                 MSG_DEBUG("Fail to notification_free");
274                         }
275                         return MSG_SUCCESS;
276                         break;
277                 }
278         } else if (pMsg->msgType.mainType == MSG_MMS_TYPE) {
279                 switch(pMsg->msgType.subType)
280                 {
281                 default :
282                         MsgRefreshNoti(true);
283                         noti_err = notification_free(noti);
284                         if (noti_err != NOTIFICATION_ERROR_NONE) {
285                                 MSG_DEBUG("Fail to notification_free");
286                         }
287                         return MSG_SUCCESS;
288                         break;
289                 }
290         } else {
291                 MSG_DEBUG("Message type does not match.");
292
293                 noti_err = notification_free(noti);
294                 if (noti_err != NOTIFICATION_ERROR_NONE) {
295                         MSG_DEBUG("Fail to notification_free");
296                 }
297
298                 return MSG_ERR_INVALID_PARAMETER;
299         }
300
301         if (args != NULL) {
302                 noti_err = notification_set_args(noti, args, NULL);
303                 if (noti_err != NOTIFICATION_ERROR_NONE) {
304                         MSG_DEBUG("Fail to notification_set_args : %d", noti_err);
305                 }
306         }
307
308         noti_err = notification_insert(noti, NULL);
309         if (noti_err != NOTIFICATION_ERROR_NONE) {
310                 MSG_DEBUG("Fail to notification_insert");
311         }
312
313         noti_err = notification_free(noti);
314         if (noti_err != NOTIFICATION_ERROR_NONE) {
315                 MSG_DEBUG("Fail to notification_free");
316         }
317
318         if (args != NULL) {
319                 bundle_free(args);
320         }
321
322         MsgChangePmState();
323
324         return MSG_SUCCESS;
325 }
326
327
328 msg_error_t MsgInsertMmsReportToNoti(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S* pMsg)
329 {
330
331         notification_h noti = NULL;
332         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
333
334         char addressVal[MAX_ADDRESS_VAL_LEN+1];
335         char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
336         char displayName[MAX_DISPLAY_NAME_LEN+1];
337         char contents[MAX_DISPLAY_NAME_LEN+1];
338         char sqlQuery[MAX_QUERY_LEN+1];
339
340         memset(addressVal, 0x00, sizeof(addressVal));
341         memset(firstName, 0x00, sizeof(firstName));
342         memset(lastName, 0x00, sizeof(lastName));
343         memset(displayName, 0x00, sizeof(displayName));
344         memset(contents, 0x00, sizeof(contents));
345         memset(sqlQuery, 0x00, sizeof(sqlQuery));
346
347         int report_status_type;
348         int report_status_value;
349
350         if (pMsg->msgType.subType == MSG_DELIVERYIND_MMS) {
351                 report_status_type = MSG_REPORT_TYPE_DELIVERY;
352                 MSG_DEBUG("mms subtype is Delivery Report type");
353         } else if (pMsg->msgType.subType == MSG_READORGIND_MMS) {
354                 report_status_type = MSG_REPORT_TYPE_READ;
355                 MSG_DEBUG("mms subtype is Read Report type");
356         } else {
357                 MSG_DEBUG("No matching subtype. subtype [%d]", pMsg->msgType.subType);
358                 return MSG_SUCCESS;
359         }
360
361         MSG_ADDRESS_INFO_S *address_info_s = &pMsg->addressList[0];
362         MSG_DEBUG("address info : %s, type : %d", address_info_s->addressVal, address_info_s->addressType);
363
364         if (address_info_s->addressType == MSG_ADDRESS_TYPE_PLMN) {
365
366                 if (strlen(address_info_s->addressVal) > MAX_PRECONFIG_NUM) {
367                         char newPhoneNum[MAX_PRECONFIG_NUM+1];
368
369                         memset(newPhoneNum, 0x00, sizeof(newPhoneNum));
370
371                         MsgConvertNumber(address_info_s->addressVal, newPhoneNum);
372
373                         snprintf(sqlQuery, sizeof(sqlQuery),
374                                                 "SELECT A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, B.STATUS "
375                                                 "FROM %s A, %s B "
376                                                 "WHERE B.MSG_ID=%d AND B.STATUS_TYPE=%d AND A.ADDRESS_VAL LIKE '%%%s' AND B.ADDRESS_VAL LIKE \'%%%s\';"
377                                                 , MSGFW_ADDRESS_TABLE_NAME, MSGFW_REPORT_TABLE_NAME, pMsg->msgId, report_status_type, newPhoneNum, newPhoneNum);
378                 } else {
379
380                         snprintf(sqlQuery, sizeof(sqlQuery),
381                                 "SELECT A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, B.STATUS "
382                                 "FROM %s A, %s B "
383                                 "WHERE B.MSG_ID=%d AND B.STATUS_TYPE=%d AND A.ADDRESS_VAL LIKE '%s' AND B.ADDRESS_VAL LIKE '%s';"
384                                 , MSGFW_ADDRESS_TABLE_NAME, MSGFW_REPORT_TABLE_NAME, pMsg->msgId, report_status_type, address_info_s->addressVal, address_info_s->addressVal);
385                 }
386
387         } else if (address_info_s->addressType == MSG_ADDRESS_TYPE_EMAIL) {//check full
388                 snprintf(sqlQuery, sizeof(sqlQuery),
389                         "SELECT A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, B.STATUS "
390                         "FROM %s A, %s B "
391                         "WHERE B.MSG_ID=%d AND B.STATUS_TYPE=%d AND A.ADDRESS_VAL=\'%s\' AND B.ADDRESS_VAL LIKE \'%s\';"
392                         , MSGFW_ADDRESS_TABLE_NAME, MSGFW_REPORT_TABLE_NAME, pMsg->msgId, report_status_type, address_info_s->addressVal, address_info_s->addressVal);
393         } else {
394                 snprintf(sqlQuery, sizeof(sqlQuery),
395                         "SELECT A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, B.STATUS "
396                         "FROM %s A, %s B "
397                         "WHERE B.MSG_ID=%d B.STATUS_TYPE=%d;"
398                         , MSGFW_ADDRESS_TABLE_NAME, MSGFW_REPORT_TABLE_NAME, pMsg->msgId, report_status_type);
399         }
400
401         MSG_DEBUG("sqlQuery = [%s]", sqlQuery);
402
403         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
404                 return MSG_ERR_DB_PREPARE;
405
406         if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW) {
407                 if (pDbHandle->columnText(0) != NULL) {
408                         strncpy(addressVal, (char*)pDbHandle->columnText(0), MAX_ADDRESS_VAL_LEN);
409                         MSG_DEBUG("addressVal is [%s]",addressVal);
410                 } else {
411                         MSG_DEBUG("address Val is Null");
412                 }
413
414                 char *pTempDisplayName = (char *)pDbHandle->columnText(1);
415                 if (pTempDisplayName != NULL && pTempDisplayName[0] != '\0') {
416                         strncpy(displayName, pTempDisplayName, MAX_DISPLAY_NAME_LEN);
417                 } else {
418                         if (pDbHandle->columnText(2) != NULL)
419                                 strncpy(firstName, (char*)pDbHandle->columnText(2), MAX_DISPLAY_NAME_LEN);
420
421                         if (pDbHandle->columnText(3) != NULL)
422                                 strncpy(lastName, (char*)pDbHandle->columnText(3), MAX_DISPLAY_NAME_LEN);
423
424                         int order = MsgGetContactNameOrder();
425
426                         if (order == 0) {
427                                 if (firstName[0] != '\0') {
428                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
429                                 }
430
431                                 if (lastName[0] != '\0') {
432                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
433                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
434                                 }
435                         } else if (order == 1) {
436                                 if (lastName[0] != '\0') {
437                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
438                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
439                                 }
440
441                                 if (firstName[0] != '\0') {
442                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
443                                 }
444                         }
445                 }
446
447                 report_status_value = pDbHandle->columnInt(4);
448                 MSG_DEBUG("report status [type = %d, value = %d]", report_status_type, report_status_value);
449         } else {
450                 MSG_DEBUG("DB Query Result Fail");
451                 pDbHandle->finalizeQuery();
452                 return MSG_ERR_DB_STEP;
453         }
454
455         pDbHandle->finalizeQuery();
456
457         noti = notification_create(NOTIFICATION_TYPE_NOTI);
458         if (noti == NULL) {
459                 MSG_DEBUG("notification_create is failed.");
460                 return MSG_ERR_UNKNOWN;
461         }
462
463         noti_err = notification_set_application(noti, "8r4r5ddzzn.Messages");
464         if (noti_err != NOTIFICATION_ERROR_NONE) {
465                 MSG_DEBUG("Fail to notification_set_application : %d", noti_err);
466         }
467         noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
468         if (noti_err != NOTIFICATION_ERROR_NONE) {
469                 MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
470         }
471
472         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NOTI_MSG_ICON_PATH);
473         if (noti_err != NOTIFICATION_ERROR_NONE) {
474                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
475         }
476
477         if (displayName[0] == '\0')
478                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
479         else
480                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
481
482         // set time.
483 //      notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, msgTime);
484
485
486         if (pMsg->msgType.subType == MSG_DELIVERYIND_MMS) {
487
488                 switch(report_status_value) {
489                 case MSG_DELIVERY_REPORT_NONE:
490                         noti_err = notification_free(noti);
491                         if (noti_err != NOTIFICATION_ERROR_NONE) {
492                                 MSG_DEBUG("Fail to notification_free");
493                         }
494
495                         return MSG_ERR_UNKNOWN;
496
497                 case MSG_DELIVERY_REPORT_EXPIRED:
498                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Expired.");
499                         break;
500
501                 case MSG_DELIVERY_REPORT_REJECTED:
502                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Rejected.");
503                         break;
504
505                 case MSG_DELIVERY_REPORT_DEFERRED:
506                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Deferred.");
507                         break;
508
509                 case MSG_DELIVERY_REPORT_UNRECOGNISED:
510                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Unrecognised.");
511                         break;
512
513                 case MSG_DELIVERY_REPORT_INDETERMINATE:
514                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Indeterminate.");
515                         break;
516
517                 case MSG_DELIVERY_REPORT_FORWARDED:
518                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Forwarded.");
519                         break;
520
521                 case MSG_DELIVERY_REPORT_UNREACHABLE:
522                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Unreachable.");
523                         break;
524
525                 case MSG_DELIVERY_REPORT_ERROR:
526                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Error.");
527                         break;
528
529                 default :
530                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Delivered.");
531                         break;
532                 }
533
534                 MSG_DEBUG("content text : %s", contents);
535
536                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, contents, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
537                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, contents, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
538
539                 noti_err = notification_insert(noti, NULL);
540                 if (noti_err != NOTIFICATION_ERROR_NONE)
541                         MSG_DEBUG("Fail to notification_insert");
542
543         } else if (pMsg->msgType.subType == MSG_READORGIND_MMS) {
544
545                 switch(report_status_value) {
546                 case MSG_READ_REPORT_NONE:
547                         noti_err = notification_free(noti);
548                         if (noti_err != NOTIFICATION_ERROR_NONE)
549                                 MSG_DEBUG("Fail to notification_free");
550
551                         return MSG_ERR_UNKNOWN;
552
553                 case MSG_READ_REPORT_IS_DELETED:
554                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Deleted.");
555                         break;
556
557                 default :
558                         snprintf(contents, MAX_DISPLAY_NAME_LEN, "Read.");
559                         break;
560                 }
561
562                 MSG_DEBUG("content text : %s", contents);
563
564                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, contents, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
565
566                 noti_err = notification_insert(noti, NULL);
567                 if (noti_err != NOTIFICATION_ERROR_NONE) {
568                         MSG_DEBUG("Fail to notification_insert");
569                 }
570         } else {
571                 MSG_DEBUG("No matching subtype. subtype [%d]", pMsg->msgType.subType);
572
573                 noti_err = notification_free(noti);
574                 if (noti_err != NOTIFICATION_ERROR_NONE) {
575                         MSG_DEBUG("Fail to notification_free");
576                 }
577                 return MSG_SUCCESS;
578         }
579
580         noti_err = notification_free(noti);
581         if (noti_err != NOTIFICATION_ERROR_NONE) {
582                 MSG_DEBUG("Fail to notification_free");
583         }
584
585         return MSG_SUCCESS;
586 }
587
588
589 msg_error_t MsgRefreshNoti(bool bWithTicker)
590 {
591
592         MsgDbHandler dbhandler;
593         MSG_MESSAGE_INFO_S msg = {0,};
594
595         int notiPrivId = MsgSettingGetInt(NOTIFICATION_PRIV_ID);
596
597         notification_h noti = NULL;
598         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
599         bundle* args;
600
601         int contactId = 0;
602         msg_thread_id_t threadId = 0;
603         time_t msgTime = 0;
604         char tempId[6];
605         char addressVal[MAX_ADDRESS_VAL_LEN+1];
606         char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
607         char displayName[MAX_DISPLAY_NAME_LEN+1];
608         char thumbPath[MAX_IMAGE_PATH_LEN+1];
609         char sqlQuery[MAX_QUERY_LEN+1];
610
611         memset(tempId, 0x00, sizeof(tempId));
612         memset(addressVal, 0x00, sizeof(addressVal));
613         memset(firstName, 0x00, sizeof(firstName));
614         memset(lastName, 0x00, sizeof(lastName));
615         memset(displayName, 0x00, sizeof(displayName));
616         memset(thumbPath, 0x00, sizeof(thumbPath));
617         memset(sqlQuery, 0x00, sizeof(sqlQuery));
618
619         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.CONV_ID, A.ADDRESS_VAL, A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, \
620                         B.DISPLAY_TIME, A.CONTACT_ID, A.IMAGE_PATH, B.MSG_ID, B.MSG_TEXT, B.SUBJECT, B.MAIN_TYPE \
621                         FROM %s A, %s B WHERE A.CONV_ID=B.CONV_ID AND B.READ_STATUS=0 AND B.FOLDER_ID=%d ORDER BY B.DISPLAY_TIME DESC;",
622                         MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MSG_INBOX_ID);
623
624         if (dbhandler.prepareQuery(sqlQuery) != MSG_SUCCESS)
625                 return MSG_ERR_DB_PREPARE;
626
627         if (dbhandler.stepQuery() == MSG_ERR_DB_ROW) {
628                 threadId = dbhandler.columnInt(0);
629
630                 if (dbhandler.columnText(1) != NULL)
631                         strncpy(addressVal, (char*)dbhandler.columnText(1), MAX_ADDRESS_VAL_LEN);
632
633
634                 char *pTempDisplayName = (char *)dbhandler.columnText(2);
635                 if (pTempDisplayName != NULL && pTempDisplayName[0] != '\0') {
636                         strncpy(displayName, pTempDisplayName, MAX_DISPLAY_NAME_LEN);
637                 } else {
638                         if (dbhandler.columnText(3) != NULL)
639                                 strncpy(firstName, (char*)dbhandler.columnText(3), MAX_DISPLAY_NAME_LEN);
640
641                         if (dbhandler.columnText(4) != NULL)
642                                 strncpy(lastName, (char*)dbhandler.columnText(4), MAX_DISPLAY_NAME_LEN);
643
644                         int order = MsgGetContactNameOrder();
645
646                         if (order == 0) {
647                                 if (firstName[0] != '\0') {
648                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
649                                 }
650
651                                 if (lastName[0] != '\0') {
652                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
653                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
654                                 }
655                         } else if (order == 1) {
656                                 if (lastName[0] != '\0') {
657                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
658                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
659                                 }
660
661                                 if (firstName[0] != '\0') {
662                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
663                                 }
664                         }
665                 }
666
667                 msgTime = (time_t)dbhandler.columnInt(5);
668
669                 contactId = dbhandler.columnInt(6);
670
671                 strncpy(thumbPath, (char*)dbhandler.columnText(7), MAX_IMAGE_PATH_LEN);
672
673                 msg.msgId = dbhandler.columnInt(8);
674
675                 strncpy(msg.msgText, (char*)dbhandler.columnText(9), MAX_MSG_TEXT_LEN);
676
677                 strncpy(msg.subject, (char*)dbhandler.columnText(10), MAX_SUBJECT_LEN);
678
679                 msg.msgType.mainType = dbhandler.columnInt(11);
680
681                 MSG_DEBUG("unread message [%d].", msg.msgId);
682         } else {
683
684                 MSG_DEBUG("No unread message.");
685                 MSG_DEBUG("notiPrivId [%d]", notiPrivId);
686
687                 dbhandler.finalizeQuery();
688
689                 // No unread message.
690                 if (notiPrivId > 0) {
691                         noti_err = notification_delete_by_priv_id(NULL, NOTIFICATION_TYPE_NOTI, notiPrivId);
692                         if (noti_err != NOTIFICATION_ERROR_NONE) {
693                                 MSG_DEBUG("Fail to notification_delete_by_priv_id : %d", noti_err);
694                         }
695                 }
696
697                 notiPrivId = 0;
698
699                 if(MsgSettingSetInt(NOTIFICATION_PRIV_ID, notiPrivId) != MSG_SUCCESS)
700                         MSG_DEBUG("MsgSettingSetInt fail : NOTIFICATION_PRIV_ID");
701
702                 return MSG_ERR_DB_STEP;
703         }
704
705         dbhandler.finalizeQuery();
706
707
708         args = bundle_create();
709
710         int unreadMsgCnt = MsgStoGetUnreadCnt(&dbhandler, MSG_SMS_TYPE);
711         unreadMsgCnt += MsgStoGetUnreadCnt(&dbhandler, MSG_MMS_TYPE);
712
713         MSG_DEBUG("notiPrivId [%d], unreadMsgCnt [%d]", notiPrivId, unreadMsgCnt);
714
715         if (notiPrivId > 0) {
716                 noti = notification_load(NULL, notiPrivId);
717                 if (noti == NULL)
718                         MSG_DEBUG("notification_load is failed.");
719         }
720
721         if (noti == NULL) {
722                 noti = notification_create(NOTIFICATION_TYPE_NOTI);
723                 if (noti == NULL) {
724                         MSG_DEBUG("notification_new is failed.");
725                         bundle_free(args);
726                         return MSG_ERR_UNKNOWN;
727                 }
728
729                 notiPrivId = 0;
730         }
731         noti_err = notification_set_application(noti, "8r4r5ddzzn.Messages");
732         if (noti_err != NOTIFICATION_ERROR_NONE) {
733                 MSG_DEBUG("Fail to notification_set_application : %d", noti_err);
734         }
735         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
736         if (noti_err != NOTIFICATION_ERROR_NONE) {
737                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
738         }
739
740
741         if (unreadMsgCnt > 1) {
742
743                 noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_MULTIPLE);
744                 if (noti_err != NOTIFICATION_ERROR_NONE) {
745                         MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
746                 }
747
748                 noti_err = notification_set_text_domain(noti, MSG_SYS_PACKAGE_NAME, MSG_SYS_LOCALEDIR);
749                 if (noti_err != NOTIFICATION_ERROR_NONE) {
750                         MSG_DEBUG("Fail to notification_set_text_domain.");
751                 }
752
753                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Message", MESSAGE, NOTIFICATION_VARIABLE_TYPE_NONE);
754
755                 char unreadMsgCntStr[5] = {0,};
756                 snprintf(unreadMsgCntStr, 5, "%d", unreadMsgCnt);
757                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_EVENT_COUNT, unreadMsgCntStr, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
758
759                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Messages", NEW_MESSAGES, NOTIFICATION_VARIABLE_TYPE_NONE);
760
761                 if (displayName[0] == '\0')
762                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
763                 else
764                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
765
766                 if (msg.msgType.mainType == MSG_SMS_TYPE)
767                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2,msg.msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
768                 else
769                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, msg.subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
770
771                 // set time.
772                 notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, msgTime);
773
774         } else {
775
776                 noti_err = notification_set_layout(noti, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
777                 if (noti_err != NOTIFICATION_ERROR_NONE) {
778                         MSG_DEBUG("Fail to notification_set_layout : %d", noti_err);
779                 }
780
781                 noti_err = notification_set_text_domain(noti, MSG_SYS_PACKAGE_NAME, MSG_SYS_LOCALEDIR);
782                 if (noti_err != NOTIFICATION_ERROR_NONE) {
783                         MSG_DEBUG("Fail to notification_set_text_domain.");
784                 }
785
786                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, "Message", MESSAGE, NOTIFICATION_VARIABLE_TYPE_NONE);
787
788                 notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, "New Message", NEW_MESSAGE, NOTIFICATION_VARIABLE_TYPE_NONE);
789
790                 if (displayName[0] == '\0')
791                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, addressVal, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
792                 else
793                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_1, displayName, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
794
795                 if (msg.msgType.mainType == MSG_SMS_TYPE)
796                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2,msg.msgText, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
797                 else
798                         notification_set_text(noti, NOTIFICATION_TEXT_TYPE_INFO_2, msg.subject, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
799
800                 // set time.
801                 notification_set_time_to_text(noti, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, msgTime);
802
803         }
804         {
805                 if (bWithTicker)
806                         noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_ALL);
807                 else
808                         noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_ALL^NOTIFICATION_DISPLAY_APP_TICKER);
809
810                 if (noti_err != NOTIFICATION_ERROR_NONE) {
811                         MSG_DEBUG("Fail to notification_set_display_applist : %d", noti_err);
812                 }
813         }
814
815         memset(&tempId, 0x00, sizeof(tempId));
816
817         bundle_add(args, "type", "msg_id");
818
819         snprintf(tempId, 5, "%d", msg.msgId);
820         bundle_add(args, "msgId", tempId);
821
822         if (args != NULL) {
823                 noti_err = notification_set_args(noti, args, NULL);
824                 if (noti_err != NOTIFICATION_ERROR_NONE) {
825                         MSG_DEBUG("Fail to notification_set_args : %d", noti_err);
826                 }
827         }
828
829         if (notiPrivId > 0) {
830                 noti_err = notification_update(noti);
831                 if (noti_err != NOTIFICATION_ERROR_NONE) {
832                         MSG_DEBUG("Fail to notification_update");
833                 }
834         } else {
835                 noti_err = notification_insert(noti, &notiPrivId);
836                 if (noti_err != NOTIFICATION_ERROR_NONE) {
837                         MSG_DEBUG("Fail to notification_insert");
838                 }
839
840                 if (MsgSettingSetInt(NOTIFICATION_PRIV_ID, notiPrivId) != MSG_SUCCESS)
841                         MSG_DEBUG("MsgSettingSetInt fail: NOTIFICATION_PRIV_ID");
842                 MSG_DEBUG("Insert notiPrivId [%d]", notiPrivId);
843         }
844
845         noti_err = notification_free(noti);
846         if (noti_err != NOTIFICATION_ERROR_NONE) {
847                 MSG_DEBUG("Fail to notification_free");
848         }
849
850         bundle_free(args);
851
852         return MSG_SUCCESS;
853 }
854
855 msg_error_t MsgCleanAndResetNoti()
856 {
857         msg_error_t err = MSG_SUCCESS;
858         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
859
860         noti_err = notification_delete_all_by_type("/usr/bin/msg-server", NOTIFICATION_TYPE_NOTI);
861         if (noti_err != NOTIFICATION_ERROR_NONE) {
862                 MSG_DEBUG("Fail to notification_delete_all_by_type noti_err [%d]", noti_err);
863                 return MSG_ERR_UNKNOWN;
864         }
865
866         err = MsgRefreshNoti(false);
867         if (err != MSG_SUCCESS) {
868                         MSG_DEBUG("Fail to MsgRefreshNoti");
869         }
870
871         return err;
872 }
873
874
875 msg_error_t MsgInsertTicker(const char* pTickerMsg, const char* pLocaleTickerMsg)
876 {
877
878         MSG_DEBUG("pTickerMsg [%s]", pTickerMsg);
879         MSG_DEBUG("pLocaleTickerMsg [%s]", pLocaleTickerMsg);
880
881         notification_h noti = NULL;
882         notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
883
884         noti = notification_new(NOTIFICATION_TYPE_NOTI, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE);
885         if (noti == NULL) {
886                 MSG_DEBUG("notification_new is failed.");
887                 return MSG_ERR_UNKNOWN;
888         }
889
890         noti_err = notification_set_application(noti, "8r4r5ddzzn.Messages");
891         if (noti_err != NOTIFICATION_ERROR_NONE) {
892                 MSG_DEBUG("Fail to notification_set_application : %d", noti_err);
893         }
894
895         noti_err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, NORMAL_MSG_ICON_PATH);
896         if (noti_err != NOTIFICATION_ERROR_NONE) {
897                 MSG_DEBUG("Fail to notification_set_image : %d", noti_err);
898         }
899
900         noti_err = notification_set_text_domain(noti, MSG_APP_PACKAGE_NAME, MSG_APP_LOCALEDIR);
901         if (noti_err != NOTIFICATION_ERROR_NONE) {
902                 MSG_DEBUG("Fail to notification_set_text_domain.");
903         }
904
905         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, pTickerMsg, pLocaleTickerMsg, NOTIFICATION_VARIABLE_TYPE_NONE);
906         if (noti_err != NOTIFICATION_ERROR_NONE) {
907                 MSG_DEBUG("Fail to notification_set_text : %d", noti_err);
908         }
909
910         noti_err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT_FOR_DISPLAY_OPTION_IS_OFF, pTickerMsg, pLocaleTickerMsg, NOTIFICATION_VARIABLE_TYPE_NONE);
911         if (noti_err != NOTIFICATION_ERROR_NONE) {
912                 MSG_DEBUG("Fail to notification_set_text : %d", noti_err);
913         }
914
915         noti_err = notification_set_display_applist(noti, NOTIFICATION_DISPLAY_APP_TICKER);
916         if (noti_err != NOTIFICATION_ERROR_NONE) {
917                 MSG_DEBUG("Fail to notification_set_display_applist : %d", noti_err);
918         }
919
920         noti_err = notification_insert(noti, NULL);
921         if (noti_err != NOTIFICATION_ERROR_NONE) {
922                 MSG_DEBUG("Fail to notification_set_text_domain");
923         }
924
925         noti_err = notification_free(noti);
926         if (noti_err != NOTIFICATION_ERROR_NONE) {
927                 MSG_DEBUG("Fail to notification_set_text_domain");
928         }
929
930         return MSG_SUCCESS;
931 }
932 msg_error_t MsgInsertBadge(unsigned int unreadMsgCnt)
933 {
934         return MSG_SUCCESS;
935 }