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