update tizen source
[framework/messaging/msg-service.git] / framework / storage-handler / MsgStorageMessage.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <tr1/unordered_set>
35 #include <queue>
36 #include <locale>
37 #include <glib.h>
38
39 #include "MsgDebug.h"
40 #include "MsgCppTypes.h"
41 #include "MsgContact.h"
42 #include "MsgUtilFile.h"
43 #include "MsgMutex.h"
44 #include "MsgUtilStorage.h"
45 #include "MsgSoundPlayer.h"
46 #include "MsgGconfWrapper.h"
47 #include "MsgSqliteWrapper.h"
48 #include "MsgPluginManager.h"
49 #include "MsgStorageHandler.h"
50 #include "MsgNotificationWrapper.h"
51 #include "MsgMmsMessage.h"
52
53 using namespace std;
54
55
56 /*==================================================================================================
57                                      VARIABLES
58 ==================================================================================================*/
59 extern MsgDbHandler dbHandle;
60
61 static unsigned int refId = 0;
62
63 Mutex delNotiMx;
64 CndVar delNoticv;
65 bool delNotiRunning = false;
66
67 Mutex delLogMx;
68 CndVar delLogcv;
69 bool delLogRunning = false;
70
71
72 /*==================================================================================================
73                                      FUNCTION FOR THREAD
74 ==================================================================================================*/
75 static gboolean startToDeleteNoti(void *pVoid)
76 {
77         MSG_BEGIN();
78
79         MSG_MSGID_LIST_S *pMsgIdList = (MSG_MSGID_LIST_S *)pVoid;
80
81         MSG_DEBUG("pMsgIdList->nCount [%d]", pMsgIdList->nCount);
82
83         delNotiMx.lock();
84
85         while (delNotiRunning) {
86                 delNoticv.wait(delNotiMx.pMutex());
87         }
88
89         delNotiRunning = true;
90
91         for (int i = 0; i < pMsgIdList->nCount; i++) {
92                 MsgDeleteNotiByMsgId(pMsgIdList->msgIdList[i]);
93
94                 /** sleep for moment */
95                 if ((i%100 == 0) && (i != 0))
96                         usleep(70000);
97         }
98
99         delNotiRunning = false;
100
101         delNoticv.signal();
102         delNotiMx.unlock();
103
104         // memory free
105         if (pMsgIdList != NULL) {
106                 //free peer info list
107                 if (pMsgIdList->msgIdList != NULL)
108                         delete [] pMsgIdList->msgIdList;
109
110                 delete [] pMsgIdList;
111         }
112
113         MSG_END();
114
115         return FALSE;
116 }
117
118
119 static gboolean updateUnreadMsgCount(void *pVoid)
120 {
121         MSG_BEGIN();
122
123         int smsCnt = 0;
124         int mmsCnt = 0;
125
126         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
127         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
128
129         MsgSettingSetIndicator(smsCnt, mmsCnt);
130
131         MSG_END();
132
133         return FALSE;
134 }
135
136
137 /*==================================================================================================
138                                      FUNCTION IMPLEMENTATION
139 ==================================================================================================*/
140 MSG_ERROR_T MsgStoAddMessage(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo, int addrIdx)
141 {
142         MSG_BEGIN();
143
144         MSG_ERROR_T err = MSG_SUCCESS;
145
146         unsigned int rowId = 0;
147         unsigned int addrId = 0;
148
149         char sqlQuery[MAX_QUERY_LEN+1];
150
151         dbHandle.beginTrans();
152
153         if (pMsg->nAddressCnt > 0) {
154                 err = MsgStoAddAddress(&dbHandle, &(pMsg->addressList[addrIdx]), &addrId);
155
156                 if (err != MSG_SUCCESS) {
157                         dbHandle.endTrans(false);
158                         return err;
159                 }
160
161                 pMsg->addressList[addrIdx].threadId = (MSG_THREAD_ID_T)addrId;
162         }
163
164         err = dbHandle.getRowId(MSGFW_MESSAGE_TABLE_NAME, &rowId);
165
166         if (err != MSG_SUCCESS) {
167                 dbHandle.endTrans(false);
168                 return err;
169         }
170
171         pMsg->msgId = (MSG_MESSAGE_ID_T)rowId;
172
173         if (addrIdx == 0)
174                 refId = pMsg->msgId;
175
176         pMsg->referenceId = refId;
177
178         int fileSize = 0;
179
180         char *pFileData = NULL;
181         AutoPtr<char> buf(&pFileData);
182
183         // Get File Data
184         if (pMsg->bTextSms == false) {
185                 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
186                         dbHandle.endTrans(false);
187                         return MSG_ERR_STORAGE_ERROR;
188                 }
189                 MSG_DEBUG("file size [%d]", fileSize);
190         }
191
192         // Add Message
193         memset(sqlQuery, 0x00, sizeof(sqlQuery));
194
195         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, ?, ?, ?, %d, 0, %d, 0, 0);",
196                                 MSGFW_MESSAGE_TABLE_NAME, rowId, addrId, pMsg->folderId, pMsg->referenceId, pMsg->storageId, pMsg->msgType.mainType, pMsg->msgType.subType,
197                                 pMsg->displayTime, pMsg->dataSize, pMsg->networkStatus, pMsg->bRead, pMsg->bProtected, pMsg->priority, pMsg->direction,
198                                 pMsg->scheduledTime, pMsg->bBackup, MSG_DELIVERY_REPORT_NONE, MSG_READ_REPORT_NONE);
199
200         MSG_DEBUG("QUERY : %s", sqlQuery);
201
202         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
203                 dbHandle.endTrans(false);
204                 return MSG_ERR_DB_EXEC;
205         }
206
207         dbHandle.bindText(pMsg->subject, 1);
208
209         dbHandle.bindText(pMsg->msgData, 2);
210
211         dbHandle.bindText(pMsg->thumbPath, 3);
212
213         if (pMsg->bTextSms == false)
214                 dbHandle.bindText(pFileData, 4);
215         else
216                 dbHandle.bindText(pMsg->msgText, 4);
217
218         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
219                 dbHandle.finalizeQuery();
220                 dbHandle.endTrans(false);
221                 return MSG_ERR_DB_EXEC;
222         }
223
224         dbHandle.finalizeQuery();
225
226         if (pMsg->msgType.subType != MSG_SENDREQ_MMS) {
227                 err = MsgStoUpdateAddress(&dbHandle, addrId);
228
229                 if (err != MSG_SUCCESS) {
230                         dbHandle.endTrans(false);
231                         return err;
232                 }
233         }
234
235         dbHandle.endTrans(true);
236
237         /* In the case of MMS Message, load the MMS plugin to save MMS PDU */
238         if (pMsg->msgType.mainType == MSG_MMS_TYPE) {
239                 MMS_MESSAGE_DATA_S mmsMsg;
240                 memset(&mmsMsg, 0x00, sizeof(MMS_MESSAGE_DATA_S));
241
242                 if (pMsg->dataSize == 0) {
243                         MSG_DEBUG("pMsg->dataSize == 0, So Making emtpy MMS body.");
244                         char * tempMmsBody = _MsgMmsSerializeMessageData(&mmsMsg, &(pMsg->dataSize));
245                         memcpy(&pMsg->msgText, tempMmsBody, pMsg->dataSize);
246                         free(tempMmsBody);
247                 }
248
249                 if (pMsg->msgType.subType != MSG_DELIVERYIND_MMS && pMsg->msgType.subType != MSG_READORGIND_MMS) {
250                         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(MSG_MMS_TYPE);
251
252                         //pMsg->msgId = pMsg->refernceId;
253
254                         err = plg->addMessage(pMsg, pSendOptInfo, pFileData);
255
256                         if (err != MSG_SUCCESS)
257                                 return MSG_ERR_STORAGE_ERROR;
258
259                         if (pMsg->msgType.subType == MSG_SENDREQ_MMS) {
260                                 MSG_DEBUG("pMsg->msgText: %s, pMsg->thumbPath: %s ", pMsg->msgText, pMsg->thumbPath);
261
262                                 err = MsgStoUpdateMMSMessage(pMsg);
263
264                                 if (err != MSG_SUCCESS)
265                                         return MSG_ERR_STORAGE_ERROR;
266
267                         }
268                 }
269         }
270
271         MSG_END();
272
273         return MSG_SUCCESS;
274 }
275
276
277 MSG_ERROR_T MsgStoUpdateMessage(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo, int addrIdx)
278 {
279         MSG_ERROR_T err = MSG_SUCCESS;
280
281         char sqlQuery[MAX_QUERY_LEN+1];
282
283         unsigned int addrId = 0;
284
285         dbHandle.beginTrans();
286
287         if (pMsg->nAddressCnt > 0) {
288                 err = MsgStoAddAddress(&dbHandle, &(pMsg->addressList[addrIdx]), &addrId);
289
290                 if (err != MSG_SUCCESS) {
291                         dbHandle.endTrans(false);
292                         return err;
293                 }
294         }
295
296         int fileSize = 0;
297
298         char *pFileData = NULL;
299         AutoPtr<char> buf(&pFileData);
300
301         // Get File Data
302         if (pMsg->bTextSms == false) {
303                 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
304                         dbHandle.endTrans(false);
305                         return MSG_ERR_STORAGE_ERROR;
306                 }
307         }
308
309         if (pSendOptInfo != NULL) {
310                 // Get Global setting value if bSetting == false
311                 if (pSendOptInfo->bSetting == false) {
312                         MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy);
313
314                         if (pMsg->msgType.mainType == MSG_SMS_TYPE) {
315                                 MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq);
316                                 MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath);
317                         } else if (pMsg->msgType.mainType == MSG_MMS_TYPE) {
318                                 MsgSettingGetBool(MMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq);
319                                 MsgSettingGetBool(MMS_SEND_READ_REPLY, &pSendOptInfo->option.mmsSendOptInfo.bReadReq);
320                                 pSendOptInfo->option.mmsSendOptInfo.expiryTime.time = (unsigned int)MsgSettingGetInt(MMS_SEND_EXPIRY_TIME);
321
322                                 MSG_MMS_DELIVERY_TIME_T deliveryTime = (MSG_MMS_DELIVERY_TIME_T)MsgSettingGetInt(MMS_SEND_DELIVERY_TIME);
323
324                                 if (deliveryTime == MSG_DELIVERY_TIME_CUSTOM) {
325                                         pSendOptInfo->option.mmsSendOptInfo.bUseDeliveryCustomTime = true;
326                                         pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time = (unsigned int)MsgSettingGetInt(MMS_SEND_CUSTOM_DELIVERY);
327                                 } else {
328                                         pSendOptInfo->option.mmsSendOptInfo.bUseDeliveryCustomTime = false;
329                                         pSendOptInfo->option.mmsSendOptInfo.deliveryTime.time = (unsigned int)deliveryTime;
330                                 }
331
332                                 pSendOptInfo->option.mmsSendOptInfo.priority = (MSG_PRIORITY_TYPE_T)MsgSettingGetInt(MMS_SEND_PRIORITY);
333                         }
334                 }
335         }
336
337         // Update Message
338         memset(sqlQuery, 0x00, sizeof(sqlQuery));
339
340         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET ADDRESS_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
341                                                                 DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, \
342                                                                 PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, SCHEDULED_TIME = %lu, BACKUP = %d, SUBJECT = ?, MSG_DATA = ?, THUMB_PATH = ?, MSG_TEXT = ? \
343                                                                 WHERE MSG_ID = %d;",
344                         MSGFW_MESSAGE_TABLE_NAME, addrId, pMsg->folderId, pMsg->storageId, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->dataSize,
345                         pMsg->networkStatus, pMsg->bRead, pMsg->bProtected, pMsg->priority, pMsg->direction, pMsg->scheduledTime, pMsg->bBackup, pMsg->msgId);
346
347         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
348                 dbHandle.endTrans(false);
349                 return MSG_ERR_DB_EXEC;
350         }
351
352         dbHandle.bindText(pMsg->subject, 1);
353
354         dbHandle.bindText(pMsg->msgData, 2);
355
356         dbHandle.bindText(pMsg->thumbPath, 3);
357
358         if (pMsg->msgType.mainType == MSG_SMS_TYPE && pMsg->bTextSms == false)
359                 dbHandle.bindText(pFileData, 4);
360         else
361                 dbHandle.bindText(pMsg->msgText, 4);
362
363         MSG_DEBUG("%s", sqlQuery);
364
365         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
366                 dbHandle.finalizeQuery();
367                 dbHandle.endTrans(false);
368                 return MSG_ERR_DB_EXEC;
369         }
370
371         dbHandle.finalizeQuery();
372
373         if (pMsg->msgType.mainType == MSG_SMS_TYPE && pSendOptInfo != NULL) {
374                 if (pSendOptInfo->bSetting == true) {
375                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
376                         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET DELREP_REQ = %d, KEEP_COPY = %d, REPLY_PATH = %d \
377                                                                                 WHERE MSG_ID = %d;",
378                                         MSGFW_SMS_SENDOPT_TABLE_NAME, pSendOptInfo->bDeliverReq, pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath, pMsg->msgId);
379
380                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
381                                 dbHandle.endTrans(false);
382                                 return MSG_ERR_DB_EXEC;
383                         }
384                 }
385         } else if (pMsg->msgType.mainType == MSG_MMS_TYPE) {
386                 MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(MSG_MMS_TYPE);
387
388                 err = plg->updateMessage(pMsg, pSendOptInfo, pFileData);
389
390                 if (err != MSG_SUCCESS) {
391                         dbHandle.endTrans(false);
392                         return MSG_ERR_STORAGE_ERROR;
393                 }
394         }
395
396         err = MsgStoUpdateAddress(&dbHandle, addrId);
397
398         if (err != MSG_SUCCESS) {
399                 dbHandle.endTrans(false);
400                 return MSG_ERR_STORAGE_ERROR;
401         }
402
403         err = MsgStoClearAddressTable(&dbHandle);
404
405         if (err != MSG_SUCCESS) {
406                 dbHandle.endTrans(false);
407                 return MSG_ERR_STORAGE_ERROR;
408         }
409
410         dbHandle.endTrans(true);
411
412         return MSG_SUCCESS;
413 }
414
415
416 MSG_ERROR_T MsgStoUpdateReadStatus(MSG_MESSAGE_ID_T MsgId, bool bRead)
417 {
418         char sqlQuery[MAX_QUERY_LEN+1];
419
420         MSG_STORAGE_ID_T storageId;
421
422         if (MsgStoSetReadStatus(&dbHandle, MsgId, bRead) != MSG_SUCCESS) {
423                 MSG_DEBUG("MsgStoUpdateAddress() Error");
424                 return MSG_ERR_STORAGE_ERROR;
425         }
426
427         // Get STORAGE_ID
428         memset(sqlQuery, 0x00, sizeof(sqlQuery));
429         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT STORAGE_ID \
430                                         FROM %s \
431                                      WHERE MSG_ID = %d;",
432                         MSGFW_MESSAGE_TABLE_NAME, MsgId);
433
434         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
435                 return MSG_ERR_DB_PREPARE;
436
437         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
438                 storageId = dbHandle.columnInt(0);
439         } else {
440                 dbHandle.finalizeQuery();
441                 return MSG_ERR_DB_STEP;
442         }
443
444         dbHandle.finalizeQuery();
445
446         MSG_DEBUG("StorageId:[%d]", storageId);
447
448         // Update Read Status for SIM Msg
449         if (storageId == MSG_STORAGE_SIM) {
450                 MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(MSG_SMS_TYPE);
451
452                 if (plg == NULL) {
453                         MSG_DEBUG("SMS Plug-in is NULL");
454                         return MSG_ERR_NULL_POINTER;
455                 }
456
457                 // Get SIM Msg ID
458                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
459                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s WHERE MSG_ID = %d;",
460                                                 MSGFW_SIM_MSG_TABLE_NAME, MsgId);
461
462                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
463                         return MSG_ERR_DB_PREPARE;
464
465                 MSG_SIM_ID_T simId;
466
467                 while (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
468                         simId = dbHandle.columnInt(0);
469
470                         if (plg->setReadStatus(simId) != MSG_SUCCESS) {
471                                 MSG_DEBUG("Fail to Set Read Status for SIM SMS");
472                                 continue;
473                         }
474                 }
475
476                 dbHandle.finalizeQuery();
477         }
478
479         return MSG_SUCCESS;
480 }
481
482
483 MSG_ERROR_T MsgStoUpdateThreadReadStatus(MSG_THREAD_ID_T ThreadId)
484 {
485         MSG_BEGIN();
486
487         MSG_ERROR_T err = MSG_SUCCESS;
488
489         int rowCnt = 0;
490         int index = 1;
491         MSG_MSGID_LIST_S *pUnreadMsgIdList = NULL;
492
493         pUnreadMsgIdList = (MSG_MSGID_LIST_S *)new char[sizeof(MSG_MSGID_LIST_S)];
494         memset(pUnreadMsgIdList, 0x00, sizeof(MSG_MSGID_LIST_S));
495
496         char sqlQuery[MAX_QUERY_LEN+1];
497
498         // Get MSG_ID List
499         memset(sqlQuery, 0x00, sizeof(sqlQuery));
500         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID \
501                                                 FROM %s A \
502                                                 WHERE ADDRESS_ID = %d AND READ_STATUS = 0;",
503                                         MSGFW_MESSAGE_TABLE_NAME, ThreadId);
504
505         err = dbHandle.getTable(sqlQuery, &rowCnt);
506
507         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
508                 dbHandle.freeTable();
509                 return err;
510         }
511
512         pUnreadMsgIdList->nCount = rowCnt;
513
514         MSG_DEBUG("unreadMsgIdList.nCount [%d]", pUnreadMsgIdList->nCount);
515
516         pUnreadMsgIdList->msgIdList = (MSG_MESSAGE_ID_T *)new char[sizeof(MSG_MESSAGE_ID_T) * rowCnt];
517
518         for (int i = 0; i < rowCnt; i++)
519                 pUnreadMsgIdList->msgIdList[i] = dbHandle.getColumnToInt(index++);
520
521         dbHandle.freeTable();
522
523
524         // Get sim MSG_ID
525         memset(sqlQuery, 0x00, sizeof(sqlQuery));
526         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID \
527                                         FROM %s A \
528                                      WHERE ADDRESS_ID = %d AND READ_STATUS = 0 AND STORAGE_ID = %d;",
529                                 MSGFW_MESSAGE_TABLE_NAME, ThreadId, MSG_STORAGE_SIM);
530
531         rowCnt = 0;
532         err = dbHandle.getTable(sqlQuery, &rowCnt);
533
534         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
535                 dbHandle.freeTable();
536                 return err;
537         }
538
539         for (int i = 1; i <= rowCnt; i++) {
540                 MsgStoUpdateReadStatus(dbHandle.getColumnToInt(i), true);
541         }
542
543         dbHandle.freeTable();
544
545         // set read status
546         memset(sqlQuery, 0x00, sizeof(sqlQuery));
547         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET READ_STATUS = %d \
548                                         WHERE ADDRESS_ID = %d AND READ_STATUS = 0;",
549                                         MSGFW_MESSAGE_TABLE_NAME, 1, ThreadId);
550
551         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
552                 return MSG_ERR_DB_EXEC;
553
554
555         if (MsgStoUpdateAddress(&dbHandle, ThreadId) != MSG_SUCCESS) {
556                 MSG_DEBUG("MsgStoUpdateAddress() Error");
557                 return MSG_ERR_STORAGE_ERROR;
558         }
559
560         if (g_idle_add(updateUnreadMsgCount, NULL) == 0) {
561                 MSG_DEBUG("updateUnreadMsgCount() Error");
562         }
563
564         if (pUnreadMsgIdList->nCount > 0) {
565                 if (g_idle_add(startToDeleteNoti, (void *)pUnreadMsgIdList) == 0) {
566                         MSG_DEBUG("startToDeleteNoti not invoked: %s", strerror(errno));
567                         // memory free
568                         if (pUnreadMsgIdList != NULL) {
569                                 //free peer info list
570                                 if (pUnreadMsgIdList->msgIdList != NULL)
571                                         delete [] pUnreadMsgIdList->msgIdList;
572
573                                 delete [] pUnreadMsgIdList;
574                         }
575                         err = MSG_ERR_UNKNOWN;
576                 }
577         }
578         MSG_END();
579
580         return MSG_SUCCESS;
581 }
582
583
584 MSG_ERROR_T MsgStoUpdateProtectedStatus(MSG_MESSAGE_ID_T MsgId, bool bProtected)
585 {
586         char sqlQuery[MAX_QUERY_LEN+1];
587
588         memset(sqlQuery, 0x00, sizeof(sqlQuery));
589         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET PROTECTED = %d WHERE MSG_ID = %d;",
590                                         MSGFW_MESSAGE_TABLE_NAME, (int)bProtected, MsgId);
591
592         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
593                 return MSG_ERR_DB_EXEC;
594
595         return MSG_SUCCESS;
596 }
597
598
599 MSG_ERROR_T MsgStoDeleteMessage(MSG_MESSAGE_ID_T MsgId, bool bCheckIndication)
600 {
601         MSG_BEGIN();
602
603         MSG_DEBUG("Msg Id : %d", MsgId);
604
605         MSG_ERROR_T err = MSG_SUCCESS;
606
607         char sqlQuery[MAX_QUERY_LEN+1];
608
609         // Get SUB_TYPE, STORAGE_ID
610         memset(sqlQuery, 0x00, sizeof(sqlQuery));
611         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MAIN_TYPE, A.SUB_TYPE, A.FOLDER_ID, A.STORAGE_ID, A.READ_STATUS, B.CONTACT_ID, B.ADDRESS_ID \
612                                         FROM %s A, %s B \
613                                      WHERE A.MSG_ID = %d AND A.ADDRESS_ID = B.ADDRESS_ID;",
614                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, MsgId);
615
616         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
617                 return MSG_ERR_DB_PREPARE;
618
619         MSG_MESSAGE_TYPE_S msgType;
620         MSG_FOLDER_ID_T folderId;
621         MSG_STORAGE_ID_T storageId;
622         MSG_CONTACT_ID_T contactId;
623
624         bool bRead;
625         unsigned int addrId;
626
627         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
628                 msgType.mainType = dbHandle.columnInt(0);
629                 msgType.subType = dbHandle.columnInt(1);
630                 folderId = dbHandle.columnInt(2);
631                 storageId = dbHandle.columnInt(3);
632                 bRead = dbHandle.columnInt(4);
633                 contactId = dbHandle.columnInt(5);
634                 addrId = dbHandle.columnInt(6);
635
636                 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] StorageId:[%d] ContactId:[%d] AddressId:[%d]", msgType.mainType, msgType.subType, folderId, storageId, contactId, addrId);
637         } else {
638                 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
639
640                 dbHandle.finalizeQuery();
641
642                 return MSG_ERR_DB_STEP;
643         }
644
645         dbHandle.finalizeQuery();
646
647         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(MSG_SMS_TYPE);
648
649         if (plg == NULL) {
650                 MSG_DEBUG("SMS Plug-in is NULL");
651
652                 return MSG_ERR_NULL_POINTER;
653         }
654
655         dbHandle.beginTrans();
656
657         // Check sim message
658         if (storageId == MSG_STORAGE_SIM) {
659                 // get sim message id
660                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
661                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s WHERE MSG_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, MsgId);
662
663                 MSG_DEBUG("sqlQuery is [%s]", sqlQuery);
664
665                 MSG_SIM_ID_T simMsgId;
666
667                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
668                         dbHandle.endTrans(false);
669                         return MSG_ERR_DB_PREPARE;
670                 }
671
672                 while (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
673                         simMsgId = dbHandle.columnInt(0);
674
675                         MSG_DEBUG("SIM Msg Id : [%d]", simMsgId);
676
677                         err = plg->deleteSimMessage(simMsgId);
678
679                         if (err != MSG_SUCCESS) {
680                                 dbHandle.finalizeQuery();
681                                 dbHandle.endTrans(false);
682                                 return err;
683                         }
684
685                         //Sim message delete in db table
686                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
687                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE SIM_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, simMsgId);
688
689                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
690                                 dbHandle.finalizeQuery();
691                                 dbHandle.endTrans(false);
692                                 return MSG_ERR_DB_EXEC;
693                         }
694                 }
695
696                 dbHandle.finalizeQuery();
697         }
698
699         memset(sqlQuery, 0x00, sizeof(sqlQuery));
700         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SCHEDULED_MSG_TABLE_NAME, MsgId);
701
702         // Delete Message from scheduled msg table
703         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
704                 dbHandle.endTrans(false);
705                 return MSG_ERR_DB_EXEC;
706         }
707
708         if (msgType.mainType == MSG_SMS_TYPE) {
709                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
710                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, MsgId);
711
712                 // Delete SMS Send Option
713                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
714                         dbHandle.endTrans(false);
715                         return MSG_ERR_DB_EXEC;
716                 }
717
718                 if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
719                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
720                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, MsgId);
721
722                         // Delete Push Message from push table
723                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
724                                 dbHandle.endTrans(false);
725                                 return MSG_ERR_DB_EXEC;
726                         }
727                 } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
728                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
729                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, MsgId);
730
731                         // Delete Push Message from push table
732                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
733                                 dbHandle.endTrans(false);
734                                 return MSG_ERR_DB_EXEC;
735                         }
736                 } else if (msgType.subType == MSG_SYNCML_CP) {
737                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
738                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, MsgId);
739
740                         // Delete SyncML Message from syncML table
741                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
742                                 dbHandle.endTrans(false);
743                                 return MSG_ERR_DB_EXEC;
744                         }
745                 }
746         } else if (msgType.mainType == MSG_MMS_TYPE) {
747                 //get multiple recipient message count
748                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
749                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE REFERENCE_ID IN \
750                                                                         (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
751                                                 MSGFW_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MsgId);
752
753                 int rowCnt = 0;
754
755                 err = dbHandle.getTable(sqlQuery, &rowCnt);
756
757                 if (err != MSG_SUCCESS) {
758                         dbHandle.freeTable();
759                         return err;
760                 }
761
762                 dbHandle.freeTable();
763                 MSG_DEBUG("rowCnt = %d", rowCnt);
764
765                 if (rowCnt == 1) {
766                         char filePath[MSG_FILEPATH_LEN_MAX] = {0,};
767                         char thumbnailpath[MSG_FILEPATH_LEN_MAX] = {0,};
768                         char dirPath[MSG_FILEPATH_LEN_MAX]= {0,};
769
770                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
771                         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILE_PATH FROM %s WHERE REFERENCE_ID IN \
772                                                                         (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
773                                                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MsgId);
774
775                         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
776                                 dbHandle.endTrans(false);
777                                 return MSG_ERR_DB_PREPARE;
778                         }
779
780                         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
781                                 strncpy(filePath, (char *)dbHandle.columnText(0), MSG_FILEPATH_LEN_MAX);
782
783                                 snprintf(dirPath, MSG_FILEPATH_LEN_MAX, "%s.dir", filePath);
784
785                                 if (remove(filePath) == -1)
786                                         MSG_DEBUG("Fail to delete file [%s]", filePath);
787                                 else
788                                         MSG_DEBUG("Success to delete file [%s]", filePath);
789
790                                 MsgRmRf(dirPath);
791                                 rmdir(dirPath);
792
793                                 // remove thumbnail file
794                                 char *fileName = NULL;
795                                 fileName = strrchr(filePath, '/');
796
797                                 snprintf(thumbnailpath, MSG_FILEPATH_LEN_MAX, MSG_THUMBNAIL_PATH"%s.jpg", fileName+1);
798                                 if(remove(thumbnailpath) == -1)
799                                         MSG_DEBUG("Fail to delete thumbnail [%s]", thumbnailpath);
800                                 else
801                                         MSG_DEBUG("Success to delete thumbnail [%s]", thumbnailpath);
802                         } else {
803                                 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
804                                 dbHandle.finalizeQuery();
805                                 dbHandle.endTrans(false);
806                                 return MSG_ERR_DB_STEP;
807                         }
808
809                         dbHandle.finalizeQuery();
810
811                         const char *tableList[] = {MMS_PLUGIN_MESSAGE_TABLE_NAME, MMS_PLUGIN_ATTRIBUTE_TABLE_NAME};
812
813                         int listCnt = sizeof(tableList)/sizeof(char *);
814
815                         for (int i = 0; i < listCnt; i++) {
816                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
817                                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE REFERENCE_ID IN \
818                                                                         (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
819                                                                                 tableList[i], MSGFW_MESSAGE_TABLE_NAME, MsgId);
820
821                                 // Delete Data from MMS table
822                                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
823                                         dbHandle.endTrans(false);
824                                         return MSG_ERR_DB_EXEC;
825                                 }
826                         }
827                 } else {
828                         MSG_DEBUG("There is more multi-recipient mms. MMS DB table is not destroyed");
829                 }
830         }
831
832         memset(sqlQuery, 0x00, sizeof(sqlQuery));
833         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, MsgId);
834
835         // Delete Message from msg table
836         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
837                 dbHandle.endTrans(false);
838                 return MSG_ERR_DB_EXEC;
839         }
840
841         // Clear Address table
842         if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS) {
843                 dbHandle.endTrans(false);
844                 return MSG_ERR_DB_EXEC;
845         }
846
847         // Update Address Info.
848         if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
849                 dbHandle.endTrans(false);
850                 return MSG_ERR_STORAGE_ERROR;
851         }
852
853         dbHandle.endTrans(true);
854
855         if (msgType.mainType == MSG_SMS_TYPE && folderId == MSG_INBOX_ID) {
856                 msgType.classType = MSG_CLASS_NONE;
857
858                 // Set memory status in SIM
859                 if (MsgStoCheckMsgCntFull(&dbHandle, &msgType, folderId) == MSG_SUCCESS) {
860                         MSG_DEBUG("Set Memory Status");
861
862                         plg->setMemoryStatus(MSG_SUCCESS);
863                 }
864         }
865
866         if (bCheckIndication == true) {
867                 MSG_DEBUG("bCheckIndication is true.");
868
869                 int smsCnt = 0;
870                 int mmsCnt = 0;
871
872                 smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
873                 mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
874
875                 MsgSettingSetIndicator(smsCnt, mmsCnt);
876
877                 MsgDeleteNotiByMsgId(MsgId);
878         }
879
880         return MSG_SUCCESS;
881 }
882
883
884 MSG_ERROR_T MsgStoDeleteAllMessageInFolder(MSG_FOLDER_ID_T FolderId, bool bOnlyDB, MSG_MSGID_LIST_S *pMsgIdList)
885 {
886         MSG_ERROR_T err = MSG_SUCCESS;
887
888         char sqlQuery[MAX_QUERY_LEN+1];
889
890         queue<MSG_THREAD_ID_T> threadList;
891
892         const char *tableList[] = {MSGFW_PUSH_MSG_TABLE_NAME, MSGFW_CB_MSG_TABLE_NAME,
893                                                 MSGFW_SYNCML_MSG_TABLE_NAME, MSGFW_SCHEDULED_MSG_TABLE_NAME,
894                                                 MSGFW_SMS_SENDOPT_TABLE_NAME, MMS_PLUGIN_MESSAGE_TABLE_NAME,
895                                                 MSGFW_MESSAGE_TABLE_NAME};
896
897         int listCnt = sizeof(tableList)/sizeof(char *);
898         int rowCnt = 0;
899
900         // Get Address ID in Folder
901         memset(sqlQuery, 0x00, sizeof(sqlQuery));
902
903         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DISTINCT(B.ADDRESS_ID) \
904                                         FROM %s A, %s B \
905                                      WHERE A.ADDRESS_ID = B.ADDRESS_ID \
906                                           AND A.FOLDER_ID = %d",
907                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, FolderId);
908
909         err = dbHandle.getTable(sqlQuery, &rowCnt);
910
911         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
912                 MSG_DEBUG("sql query is %s.", sqlQuery);
913
914                 dbHandle.freeTable();
915                 return err;
916         }
917
918         if (rowCnt <= 0) {
919                 dbHandle.freeTable();
920
921                 return MSG_SUCCESS;
922         }
923
924         for (int i = 1; i <= rowCnt; i++) {
925                 MSG_DEBUG("thread ID : %d", dbHandle.getColumnToInt(i));
926                 threadList.push((MSG_THREAD_ID_T)dbHandle.getColumnToInt(i));
927         }
928
929         dbHandle.freeTable();
930
931         /*** Get msg id list **/
932         MSG_MSGID_LIST_S *pToDeleteMsgIdList = NULL;
933         pToDeleteMsgIdList = (MSG_MSGID_LIST_S *)new char[sizeof(MSG_MSGID_LIST_S)];
934         memset(pToDeleteMsgIdList, 0x00, sizeof(MSG_MSGID_LIST_S));
935
936         memset(sqlQuery, 0x00, sizeof(sqlQuery));
937         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE FOLDER_ID = %d",
938                         MSGFW_MESSAGE_TABLE_NAME, FolderId);
939
940         rowCnt = 0;
941         int index = 1;
942
943         err = dbHandle.getTable(sqlQuery, &rowCnt);
944
945         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
946                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
947
948                 dbHandle.freeTable();
949
950                 goto FREE_MEMORY;
951         }
952
953         if (rowCnt <= 0) {
954                 dbHandle.freeTable();
955                 err = MSG_SUCCESS;
956
957                 goto FREE_MEMORY;
958         }
959
960         pToDeleteMsgIdList->nCount = rowCnt;
961
962         MSG_DEBUG("pToDeleteMsgIdList->nCount [%d]", pToDeleteMsgIdList->nCount);
963
964         pToDeleteMsgIdList->msgIdList = (MSG_MESSAGE_ID_T *)new char[sizeof(MSG_MESSAGE_ID_T) * rowCnt];
965
966         for (int i = 0; i < rowCnt; i++)
967                 pToDeleteMsgIdList->msgIdList[i] = dbHandle.getColumnToInt(index++);
968
969         dbHandle.freeTable();
970         /*** **/
971
972         /*** Delete Sim Message In Folder **/
973         if (FolderId >= MSG_INBOX_ID) {
974                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
975
976                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s \
977                                              WHERE FOLDER_ID = %d AND STORAGE_ID = %d",
978                                 MSGFW_MESSAGE_TABLE_NAME, FolderId, MSG_STORAGE_SIM);
979
980                 MSG_DEBUG("sql query is %s.", sqlQuery);
981
982                 rowCnt = 0;
983
984                 err = dbHandle.getTable(sqlQuery, &rowCnt);
985
986                 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
987                         dbHandle.freeTable();
988
989                         goto FREE_MEMORY;
990                 }
991
992                 for (int i = 1; i <= rowCnt; i++) {
993                         err = MsgStoDeleteMessage(dbHandle.getColumnToInt(i), false);
994
995                         if (err != MSG_SUCCESS) {
996                                 MSG_DEBUG("MsgStoDeleteMessage() Error!!!");
997
998                                 dbHandle.freeTable();
999
1000                                 goto FREE_MEMORY;
1001                         }
1002                 }
1003
1004                 dbHandle.freeTable();
1005         }
1006
1007         dbHandle.beginTrans();
1008
1009         for (int i = 0; i < listCnt; i++) {
1010                 if (!strcmp(tableList[i], MMS_PLUGIN_MESSAGE_TABLE_NAME)) {
1011                         //MMS_PLUGIN_MESSAGE_TABLE and MMS_PLUGIN_ATTRIBUTE_TABLE will be updated together
1012                         int rowCnt = 0;
1013                         int msgCnt = 0;
1014                         int index = 2;
1015
1016                         //get mms msg id list
1017                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1018                         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID, REFERENCE_ID FROM %s \
1019                                                                 WHERE FOLDER_ID = %d AND MAIN_TYPE = %d",
1020                                                                 MSGFW_MESSAGE_TABLE_NAME, FolderId, MSG_MMS_TYPE);
1021
1022                         MSG_DEBUG("sqlQuery [%s]", sqlQuery);
1023
1024                         err = dbHandle.getTable(sqlQuery, &rowCnt);
1025                         MSG_DEBUG("rowCnt %d", rowCnt);
1026
1027                         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
1028                                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
1029
1030                                 dbHandle.freeTable();
1031                                 dbHandle.endTrans(false);
1032
1033                                 goto FREE_MEMORY;
1034                         }
1035
1036                         for (int i = 1; i <= rowCnt; i++) {
1037                                 int msgId = 0;
1038                                 int refId = 0;
1039
1040                                 msgId = dbHandle.getColumnToInt(index++);
1041                                 refId = dbHandle.getColumnToInt(index++);
1042
1043                                 //get multiple recipient message count
1044                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1045                                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE REFERENCE_ID = %d;",
1046                                                                 MSGFW_MESSAGE_TABLE_NAME, refId);
1047
1048                                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
1049                                         dbHandle.freeTable();
1050                                         dbHandle.endTrans(false);
1051                                         err = MSG_ERR_DB_PREPARE;
1052
1053                                         goto FREE_MEMORY;
1054                                 }
1055
1056                                 msgCnt = 0;
1057
1058                                 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1059                                         msgCnt = dbHandle.columnInt(0);
1060                                 } else {
1061                                         dbHandle.finalizeQuery();
1062                                         dbHandle.freeTable();
1063                                         dbHandle.endTrans(false);
1064                                         err = MSG_ERR_DB_STEP;
1065
1066                                         goto FREE_MEMORY;
1067                                 }
1068
1069                                 dbHandle.finalizeQuery();
1070
1071                                 //delete mms raw file and db(mms_table and mms_attr_table) data
1072                                 if (msgCnt == 1) {
1073                                         char filePath[MSG_FILEPATH_LEN_MAX] = {0,};
1074                                         char dirPath[MSG_FILEPATH_LEN_MAX] = {0,};
1075                                         char thumbnailPath[MSG_FILEPATH_LEN_MAX] = {0,};
1076
1077                                         //get raw file_path
1078                                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1079                                         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILE_PATH FROM %s WHERE REFERENCE_ID = %d;",
1080                                                                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, refId);
1081
1082                                         MSG_DEBUG("sqlQuery [%s]", sqlQuery);
1083                                         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
1084                                                 dbHandle.freeTable();
1085                                                 dbHandle.endTrans(false);
1086                                                 err = MSG_ERR_DB_PREPARE;
1087
1088                                                 goto FREE_MEMORY;
1089                                         }
1090
1091                                         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1092                                                 strncpy(filePath, (char *)dbHandle.columnText(0), MSG_FILEPATH_LEN_MAX);
1093                                         } else {
1094                                                 dbHandle.finalizeQuery();
1095                                                 dbHandle.freeTable();
1096                                                 dbHandle.endTrans(false);
1097                                                 err = MSG_ERR_DB_STEP;
1098
1099                                                 goto FREE_MEMORY;
1100                                         }
1101
1102                                         dbHandle.finalizeQuery();
1103
1104                                         MSG_DEBUG("filePath [%s]", filePath);
1105
1106                                         //delete raw file
1107                                         snprintf(dirPath, sizeof(dirPath), "%s.dir", filePath);
1108
1109                                         if (remove(filePath) == -1)
1110                                                 MSG_DEBUG("Fail to delete file [%s]", filePath);
1111                                         else
1112                                                 MSG_DEBUG("Success to delete file [%s]", filePath);
1113
1114                                         MsgRmRf(dirPath);
1115                                         rmdir(dirPath);
1116                                         // delete thumbanil
1117
1118                                         char *fileName = NULL;
1119                                         fileName = strrchr(filePath, '/');
1120
1121                                         snprintf(thumbnailPath, sizeof(thumbnailPath), MSG_THUMBNAIL_PATH"%s.jpg", fileName+1);
1122
1123                                         if (remove(thumbnailPath) == -1)
1124                                                 MSG_DEBUG("Fail to delete thumbnail [%s]", thumbnailPath);
1125                                         else
1126                                                 MSG_DEBUG("Success to delete thumbnail [%s]", thumbnailPath);
1127
1128                                         //delete MMS_PLUGIN_MESSAGE_TABLE_NAME table data
1129                                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1130                                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE REFERENCE_ID = %d;",
1131                                                                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, refId);
1132
1133                                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1134                                                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
1135                                                 dbHandle.freeTable();
1136                                                 dbHandle.endTrans(false);
1137                                                 err = MSG_ERR_DB_EXEC;
1138
1139                                                 goto FREE_MEMORY;
1140                                         }
1141
1142                                         //delete  MMS_PLUGIN_ATTRIBUTE_TABLE_NAME table data
1143                                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1144                                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE REFERENCE_ID = %d;",
1145                                                                                 MMS_PLUGIN_ATTRIBUTE_TABLE_NAME, refId);
1146
1147                                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1148                                                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
1149                                                 dbHandle.freeTable();
1150                                                 dbHandle.endTrans(false);
1151                                                 err = MSG_ERR_DB_EXEC;
1152
1153                                                 goto FREE_MEMORY;
1154                                         }
1155                                 } else {
1156                                         MSG_DEBUG("There is remained multi-recipient mms for this msg = %d", msgId);
1157                                 }
1158                         }
1159
1160                         dbHandle.freeTable();
1161                 } else {
1162                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1163
1164                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID IN \
1165                                                                 (SELECT MSG_ID FROM %s WHERE FOLDER_ID = %d);",
1166                                                                 tableList[i], MSGFW_MESSAGE_TABLE_NAME, FolderId);
1167
1168                         // Delete Message in specific folder from table
1169                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1170                                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
1171                                 dbHandle.endTrans(false);
1172                                 err = MSG_ERR_DB_EXEC;
1173
1174                                 goto FREE_MEMORY;
1175                         }
1176                 }
1177         }
1178
1179         // Clear Address table
1180         if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS) {
1181                 dbHandle.endTrans(false);
1182                 err = MSG_ERR_DB_EXEC;
1183
1184                 goto FREE_MEMORY;
1185         }
1186
1187         // Update Address
1188         while (!threadList.empty()) {
1189                 err = MsgStoUpdateAddress(&dbHandle, threadList.front());
1190
1191                 threadList.pop();
1192
1193                 if (err != MSG_SUCCESS) {
1194                         dbHandle.endTrans(false);
1195
1196                         goto FREE_MEMORY;
1197                 }
1198         }
1199
1200         dbHandle.endTrans(true);
1201
1202         if (FolderId == MSG_INBOX_ID) {
1203                 int smsCnt = 0;
1204                 int mmsCnt = 0;
1205
1206                 smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
1207                 mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
1208
1209                 MsgSettingSetIndicator(smsCnt, mmsCnt);
1210         }
1211
1212 /*** Set pMsgIdList **/
1213         if (pMsgIdList != NULL && pToDeleteMsgIdList->nCount > 0) {
1214                 pMsgIdList->nCount = pToDeleteMsgIdList->nCount;
1215
1216                 pMsgIdList->msgIdList = (MSG_MESSAGE_ID_T *)new char[sizeof(MSG_MESSAGE_ID_T)*pToDeleteMsgIdList->nCount];
1217                 memcpy(pMsgIdList->msgIdList, pToDeleteMsgIdList->msgIdList, sizeof(MSG_MESSAGE_ID_T)*pToDeleteMsgIdList->nCount);
1218         }
1219 /*** **/
1220
1221 /*** Create thread  for noti delete. **/
1222         if (!bOnlyDB) {
1223                 if (pToDeleteMsgIdList->nCount > 0) {
1224
1225                         if (g_idle_add(startToDeleteNoti, (void *)pToDeleteMsgIdList) == 0) {
1226                                 MSG_DEBUG("startToDeleteNoti not invoked: %s", strerror(errno));
1227                                 // memory free
1228                                 if (pToDeleteMsgIdList != NULL) {
1229                                         //free peer info list
1230                                         if (pToDeleteMsgIdList->msgIdList != NULL)
1231                                                 delete [] pToDeleteMsgIdList->msgIdList;
1232
1233                                         delete [] pToDeleteMsgIdList;
1234                                 }
1235                                 err = MSG_ERR_UNKNOWN;
1236                         }
1237                 }
1238         }
1239 /*** **/
1240
1241         return MSG_SUCCESS;
1242
1243 FREE_MEMORY:
1244         MSG_DEBUG("Error case Free Memory");
1245
1246         while (!threadList.empty()) {
1247                 threadList.front();
1248                 threadList.pop();
1249         }
1250
1251         // memory free
1252         if (pToDeleteMsgIdList != NULL) {
1253                 //free peer info list
1254                 if (pToDeleteMsgIdList->msgIdList != NULL)
1255                         delete [] pToDeleteMsgIdList->msgIdList;
1256
1257                 delete [] pToDeleteMsgIdList;
1258         }
1259
1260         return err;
1261
1262 }
1263
1264
1265 MSG_ERROR_T MsgStoMoveMessageToFolder(MSG_MESSAGE_ID_T MsgId, MSG_FOLDER_ID_T DestFolderId)
1266 {
1267         MSG_MESSAGE_TYPE_S msgType;
1268
1269         MsgStoGetMsgType(MsgId, &msgType);
1270
1271         char sqlQuery[MAX_QUERY_LEN+1];
1272
1273         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1274
1275         if (msgType.mainType == MSG_SMS_TYPE) {
1276                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FOLDER_ID = %d WHERE MSG_ID = %d;",
1277                                         MSGFW_MESSAGE_TABLE_NAME, DestFolderId, MsgId);
1278         } else {
1279                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FOLDER_ID = %d WHERE REFERENCE_ID = %d;",
1280                                         MSGFW_MESSAGE_TABLE_NAME, DestFolderId, MsgId);
1281         }
1282
1283         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
1284                 return MSG_ERR_DB_EXEC;
1285
1286         unsigned int addrId = 0;
1287         /* get address id */
1288         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1289
1290         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s WHERE MSG_ID = %d;",
1291                                                         MSGFW_MESSAGE_TABLE_NAME, MsgId);
1292
1293         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1294                 return MSG_ERR_DB_PREPARE;
1295
1296         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1297                 addrId = dbHandle.columnInt(0);
1298         }
1299         MSG_DEBUG("addrId : %d",  addrId);
1300
1301         dbHandle.finalizeQuery();
1302
1303         /* update address table */
1304         MsgStoUpdateAddress(&dbHandle, addrId);
1305
1306         return MSG_SUCCESS;
1307 }
1308
1309
1310 MSG_ERROR_T MsgStoMoveMessageToStorage(MSG_MESSAGE_ID_T MsgId, MSG_STORAGE_ID_T DestStorageId)
1311 {
1312         MSG_BEGIN();
1313
1314         MSG_ERROR_T err = MSG_SUCCESS;
1315
1316         char sqlQuery[MAX_QUERY_LEN+1];
1317
1318         MSG_DEBUG("MsgId : %d, DestStorageId : %d", MsgId, DestStorageId);
1319
1320         switch (DestStorageId) {
1321         case MSG_STORAGE_SIM : // Move message to sim card
1322                 {
1323                         MSG_MESSAGE_INFO_S msgInfo;
1324                         SMS_SIM_ID_LIST_S simIdList;
1325
1326                         memset(&simIdList, 0x00, sizeof(SMS_SIM_ID_LIST_S));
1327
1328                         if ((err = MsgStoGetMessage(MsgId, &msgInfo, NULL)) != MSG_SUCCESS)
1329                                 return err;
1330
1331                         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(MSG_SMS_TYPE);
1332
1333                         if ((err = plg->saveSimMessage(&msgInfo, &simIdList)) != MSG_SUCCESS)
1334                                 return err;
1335
1336                         dbHandle.beginTrans();
1337
1338                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1339                         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET STORAGE_ID = %d WHERE MSG_ID = %d;",
1340                                 MSGFW_MESSAGE_TABLE_NAME, DestStorageId, MsgId);
1341
1342                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1343                                 dbHandle.endTrans(false);
1344                                 return MSG_ERR_DB_EXEC;
1345                         }
1346
1347                         for (unsigned int i = 0; i < simIdList.count; i++) {
1348                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1349                                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
1350                                                                 MSGFW_SIM_MSG_TABLE_NAME, MsgId, simIdList.simId[i]);
1351
1352                                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1353                                         dbHandle.endTrans(false);
1354                                         return MSG_ERR_DB_EXEC;
1355                                 }
1356                         }
1357
1358                         dbHandle.endTrans(true);
1359                 }
1360         break;
1361
1362         default: //Moving message to memory (when destination storage id is MSG_STORAGE_PHONE)
1363                 {
1364                         bool bSimMsg = false;
1365                         int rowCnt = 0;
1366
1367                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1368                         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT STORAGE_ID FROM %s WHERE MSG_ID = %d;",
1369                                         MSGFW_MESSAGE_TABLE_NAME, MsgId);
1370
1371                         err = dbHandle.getTable(sqlQuery, &rowCnt);
1372
1373                         if (err != MSG_SUCCESS) {
1374                                 dbHandle.freeTable();
1375                                 return err;
1376                         }
1377
1378                         if (dbHandle.getColumnToInt(1) == MSG_STORAGE_SIM) {
1379                                 MSG_DEBUG("It is SIM Message");
1380                                 bSimMsg = true;
1381                         }
1382
1383                         dbHandle.freeTable();
1384
1385                         if (bSimMsg == true) {
1386                                 MSG_SIM_ID_T simMsgId;
1387
1388                                 // get sim message id
1389                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1390
1391                                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s WHERE MSG_ID = %d;",
1392                                         MSGFW_SIM_MSG_TABLE_NAME, MsgId);
1393
1394                                 MSG_DEBUG("sqlQuery is %s.", sqlQuery);
1395
1396                                 err = dbHandle.getTable(sqlQuery, &rowCnt);
1397
1398                                 if (err != MSG_SUCCESS) {
1399                                         dbHandle.freeTable();
1400                                         return err;
1401                                 }
1402
1403                                 //Delete messages in sim card
1404                                 MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(MSG_SMS_TYPE);
1405
1406                                 for (int i = 0; i < rowCnt; i++) {
1407                                         simMsgId = dbHandle.getColumnToInt(i+1);
1408
1409                                         MSG_DEBUG("simMsgId is %d.", simMsgId);
1410
1411                                         if ((err = plg->deleteSimMessage(simMsgId)) != MSG_SUCCESS) {
1412                                                 dbHandle.freeTable();
1413                                                 return err;
1414                                         }
1415                                 }
1416
1417                                 dbHandle.freeTable();
1418
1419                                 dbHandle.beginTrans();
1420
1421                                 //Delete Messages in SIM Msg table
1422                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1423                                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;",
1424                                                         MSGFW_SIM_MSG_TABLE_NAME, MsgId);
1425                                 MSG_DEBUG("sqlQuery is %s.", sqlQuery);
1426
1427                                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1428                                         dbHandle.endTrans(false);
1429                                         return MSG_ERR_DB_EXEC;
1430                                 }
1431
1432                                 //Move storage id
1433                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1434                                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET STORAGE_ID = %d WHERE MSG_ID = %d;",
1435                                         MSGFW_MESSAGE_TABLE_NAME, DestStorageId, MsgId);
1436
1437                                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1438                                         dbHandle.endTrans(false);
1439                                         return MSG_ERR_DB_EXEC;
1440                                 }
1441
1442                                 dbHandle.endTrans(true);
1443                         }
1444                 }
1445         }
1446
1447         return MSG_SUCCESS;
1448 }
1449
1450
1451 MSG_ERROR_T MsgStoCountMessage(MSG_FOLDER_ID_T FolderId, MSG_COUNT_INFO_S *pCountInfo)
1452 {
1453         if (pCountInfo == NULL) {
1454                 MSG_DEBUG("pCountInfo is NULL");
1455                 return MSG_ERR_NULL_POINTER;
1456         }
1457
1458         char sqlQuery[MAX_QUERY_LEN+1];
1459
1460         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1461         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE FOLDER_ID = %d AND READ_STATUS = 1 \
1462                                                                     UNION ALL SELECT COUNT(MSG_ID) FROM %s WHERE FOLDER_ID = %d AND READ_STATUS = 0 \
1463                                                                     UNION ALL SELECT COUNT(MSG_ID) FROM %s WHERE FOLDER_ID = %d AND MAIN_TYPE = %d \
1464                                                                     UNION ALL SELECT COUNT(MSG_ID) FROM %s WHERE FOLDER_ID = %d AND MAIN_TYPE = %d ;",
1465                                                         MSGFW_MESSAGE_TABLE_NAME, FolderId,
1466                                                         MSGFW_MESSAGE_TABLE_NAME, FolderId,
1467                                                         MSGFW_MESSAGE_TABLE_NAME, FolderId, MSG_SMS_TYPE,
1468                                                         MSGFW_MESSAGE_TABLE_NAME, FolderId, MSG_MMS_TYPE);
1469
1470         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1471                 return MSG_ERR_DB_PREPARE;
1472
1473         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1474                 pCountInfo->nReadCnt = dbHandle.columnInt(0);
1475         } else {
1476                 dbHandle.finalizeQuery();
1477                 return MSG_ERR_DB_STEP;
1478         }
1479
1480         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1481                 pCountInfo->nUnreadCnt = dbHandle.columnInt(0);
1482         } else {
1483                 dbHandle.finalizeQuery();
1484                 return MSG_ERR_DB_STEP;
1485         }
1486
1487         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1488                 pCountInfo->nSms = dbHandle.columnInt(0);
1489         } else {
1490                 dbHandle.finalizeQuery();
1491                 return MSG_ERR_DB_STEP;
1492         }
1493
1494         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1495                 pCountInfo->nMms = dbHandle.columnInt(0);
1496         } else {
1497                 dbHandle.finalizeQuery();
1498                 return MSG_ERR_DB_STEP;
1499         }
1500
1501         dbHandle.finalizeQuery();
1502
1503         return MSG_SUCCESS;
1504 }
1505
1506
1507 MSG_ERROR_T MsgStoCountMsgByType(const MSG_MESSAGE_TYPE_S *pMsgType, int *pMsgCount)
1508 {
1509         if (pMsgType == NULL) {
1510                 MSG_DEBUG("pMsgType is NULL");
1511                 return MSG_ERR_NULL_POINTER;
1512         }
1513
1514         *pMsgCount = 0;
1515
1516         char sqlQuery[MAX_QUERY_LEN+1];
1517         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1518         // SMS
1519         if ((pMsgType->mainType == MSG_SMS_TYPE) &&
1520                 (pMsgType->subType == MSG_NORMAL_SMS || pMsgType->subType == MSG_STATUS_REPORT_SMS || pMsgType->subType == MSG_CONCAT_SIM_SMS)) {
1521                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d, %d);",
1522                                 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_NORMAL_SMS, MSG_STATUS_REPORT_SMS, MSG_CONCAT_SIM_SMS);
1523         } else if ((pMsgType->mainType == MSG_SMS_TYPE) &&
1524                                 (pMsgType->subType == MSG_WAP_SI_SMS || pMsgType->subType == MSG_WAP_SL_SMS)) {
1525          // PUSH
1526                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d);",
1527                                 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_WAP_SI_SMS, MSG_WAP_SL_SMS);
1528         } else if ((pMsgType->mainType == MSG_MMS_TYPE) &&
1529                                 (pMsgType->subType == MSG_SENDREQ_MMS || pMsgType->subType == MSG_SENDCONF_MMS || pMsgType->subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsgType->subType == MSG_RETRIEVE_MANUALCONF_MMS)) {
1530          // MMS
1531                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d, %d, %d);",
1532                                 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_SENDREQ_MMS, MSG_SENDCONF_MMS, MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS);
1533         } else {
1534                 return MSG_ERR_INVALID_PARAMETER;
1535         }
1536
1537         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1538                 return MSG_ERR_DB_PREPARE;
1539
1540         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1541                 *pMsgCount = dbHandle.columnInt(0);
1542         } else {
1543                 dbHandle.finalizeQuery();
1544                 return MSG_ERR_DB_STEP;
1545         }
1546
1547         dbHandle.finalizeQuery();
1548
1549         return MSG_SUCCESS;
1550 }
1551
1552
1553 MSG_ERROR_T MsgStoGetMessage(MSG_MESSAGE_ID_T MsgId, MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
1554 {
1555         char sqlQuery[MAX_QUERY_LEN+1];
1556
1557         unsigned int addrId = 0;
1558
1559         char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
1560
1561         memset(firstName, 0x00, sizeof(firstName));
1562         memset(lastName, 0x00, sizeof(lastName));
1563
1564         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1565         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MSG_ID, A.ADDRESS_ID, A.FOLDER_ID, A.REFERENCE_ID, A.STORAGE_ID, A.MAIN_TYPE, A.SUB_TYPE, A.DISPLAY_TIME, A.DATA_SIZE, \
1566                                                   A.NETWORK_STATUS, A.READ_STATUS, A.PROTECTED, A.BACKUP, A.PRIORITY, A.MSG_DIRECTION, A.SCHEDULED_TIME, A.SUBJECT, A.MSG_TEXT, \
1567                                                   B.ADDRESS_TYPE, B.RECIPIENT_TYPE, B.CONTACT_ID, B.ADDRESS_VAL, B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME, A.THUMB_PATH \
1568                                         FROM %s A, %s B WHERE A.MSG_ID = %d AND A.ADDRESS_ID = B.ADDRESS_ID;",
1569                                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, MsgId);
1570
1571         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
1572                 return MSG_ERR_DB_PREPARE;
1573
1574         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1575                 pMsg->msgId = dbHandle.columnInt(0);
1576                 addrId = dbHandle.columnInt(1);
1577                 pMsg->folderId = dbHandle.columnInt(2);
1578                 pMsg->referenceId = dbHandle.columnInt(3);
1579                 pMsg->storageId = dbHandle.columnInt(4);
1580                 pMsg->msgType.mainType = dbHandle.columnInt(5);
1581                 pMsg->msgType.subType = dbHandle.columnInt(6);
1582                 pMsg->displayTime = (time_t)dbHandle.columnInt(7);
1583                 pMsg->dataSize = dbHandle.columnInt(8);
1584                 pMsg->networkStatus = dbHandle.columnInt(9);
1585                 pMsg->bRead = dbHandle.columnInt(10);
1586                 pMsg->bProtected = dbHandle.columnInt(11);
1587                 pMsg->bBackup = dbHandle.columnInt(12);
1588                 pMsg->priority = dbHandle.columnInt(13);
1589                 pMsg->direction= dbHandle.columnInt(14);
1590                 pMsg->scheduledTime = (time_t)dbHandle.columnInt(15);
1591
1592                 strncpy(pMsg->subject, (char *)dbHandle.columnText(16), MAX_SUBJECT_LEN);
1593
1594                 /* Temp_File_Handling */
1595                 if (pMsg->msgType.mainType == MSG_SMS_TYPE || pMsg->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
1596                         if (pMsg->dataSize > MAX_MSG_TEXT_LEN) {
1597                                 char msgData[pMsg->dataSize+1];
1598                                 memset(msgData, 0x00, sizeof(msgData));
1599
1600                                 strncpy(msgData, (char *)dbHandle.columnText(17), pMsg->dataSize);
1601
1602                                 // Save Message Data into File
1603                                 char fileName[MAX_COMMON_INFO_SIZE+1];
1604                                 memset(fileName, 0x00, sizeof(fileName));
1605
1606                                 if (MsgCreateFileName(fileName) == false) {
1607                                         dbHandle.finalizeQuery();
1608                                         return MSG_ERR_STORAGE_ERROR;
1609                                 }
1610
1611                                 MSG_DEBUG("Save Message Data into file : size[%d] name[%s]\n", pMsg->dataSize, fileName);
1612
1613                                 if (MsgWriteIpcFile(fileName, msgData, pMsg->dataSize) == false) {
1614                                         dbHandle.finalizeQuery();
1615                                         return MSG_ERR_STORAGE_ERROR;
1616                                 }
1617
1618                                 strncpy(pMsg->msgData, fileName, MAX_MSG_DATA_LEN);
1619
1620                                 pMsg->bTextSms = false;
1621                         } else {
1622                                 memset(pMsg->msgText, 0x00, sizeof(pMsg->msgText));
1623                                 strncpy(pMsg->msgText, (char *)dbHandle.columnText(17), pMsg->dataSize);
1624
1625                                 // For WAP PUSH SI Message
1626                                 if (pMsg->msgType.subType == MSG_WAP_SI_SMS) {
1627                                         strncat(pMsg->msgText, "\n", MAX_MSG_TEXT_LEN-strlen(pMsg->msgText));
1628                                         strncat(pMsg->msgText, pMsg->subject, MAX_MSG_TEXT_LEN-strlen(pMsg->msgText));
1629                                         MSG_DEBUG("pMsg->msgText : [%s]", pMsg->msgText);
1630                                         pMsg->dataSize = sizeof(pMsg->msgText);
1631                                 }
1632
1633                                 pMsg->bTextSms = true;
1634                         }
1635                 } else {
1636                         if (dbHandle.columnText(17) != NULL)
1637                                 strncpy(pMsg->msgText, (char *)dbHandle.columnText(17), MAX_MSG_TEXT_LEN);
1638                 }
1639
1640                 if (addrId > 0)
1641                         pMsg->nAddressCnt = 1;
1642                 else
1643                         pMsg->nAddressCnt = 0;
1644
1645                 pMsg->addressList[0].threadId = addrId;
1646                 pMsg->addressList[0].addressType = dbHandle.columnInt(18);
1647                 pMsg->addressList[0].recipientType = dbHandle.columnInt(19);
1648                 pMsg->addressList[0].contactId = dbHandle.columnInt(20);
1649
1650                 if (dbHandle.columnText(21) != NULL)
1651                         strncpy(pMsg->addressList[0].addressVal, (char *)dbHandle.columnText(21), MAX_ADDRESS_VAL_LEN);
1652
1653                 if (dbHandle.columnText(22) != NULL && ((char *)dbHandle.columnText(22))[0]!='\0') {
1654                         MSG_DEBUG("displayName  : [%s]", dbHandle.columnText(22));
1655                         strncpy(pMsg->addressList[0].displayName, (char *)dbHandle.columnText(22), MAX_DISPLAY_NAME_LEN);
1656                 } else {
1657                         if (dbHandle.columnText(23) != NULL)
1658                                 strncpy(firstName, (char *)dbHandle.columnText(23), MAX_DISPLAY_NAME_LEN);
1659
1660                         if (dbHandle.columnText(24) != NULL)
1661                                 strncpy(lastName, (char *)dbHandle.columnText(24), MAX_DISPLAY_NAME_LEN);
1662
1663                         int order = MsgGetContactNameOrder();
1664
1665                         if (order == 0) {
1666                                 if (strlen(firstName) > 0) {
1667                                         strncpy(pMsg->addressList[0].displayName, firstName, MAX_DISPLAY_NAME_LEN);
1668                                 }
1669
1670                                 if (strlen(lastName) > 0) {
1671                                         strncat(pMsg->addressList[0].displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(pMsg->addressList[0].displayName));
1672                                         strncat(pMsg->addressList[0].displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(pMsg->addressList[0].displayName));
1673                                 }
1674                         } else if (order == 1) {
1675                                 if (strlen(lastName) > 0) {
1676                                         strncpy(pMsg->addressList[0].displayName, lastName, MAX_DISPLAY_NAME_LEN);
1677                                         strncat(pMsg->addressList[0].displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(pMsg->addressList[0].displayName));
1678                                 }
1679
1680                                 if (strlen(firstName) > 0) {
1681                                         strncat(pMsg->addressList[0].displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(pMsg->addressList[0].displayName));
1682                                 }
1683                         }
1684                 }
1685
1686                 // thumbnail path
1687                 if (dbHandle.columnText(25)!=NULL && ((char *)dbHandle.columnText(25))[0] != '\0') {
1688                         strncpy(pMsg->thumbPath, (char *)dbHandle.columnText(25), MSG_FILEPATH_LEN_MAX);
1689                         MSG_DEBUG("pMsg->thumbPath : [%s]", pMsg->thumbPath);
1690                 }
1691         } else {
1692                 dbHandle.finalizeQuery();
1693                 MSG_DEBUG("%s", sqlQuery);
1694                 return MSG_ERR_DB_STEP;
1695         }
1696
1697         dbHandle.finalizeQuery();
1698
1699         // Get MMS body if it is MMS.
1700         if ((pMsg->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS && (pMsg->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsg->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS)) ||
1701                 pMsg->msgType.subType == MSG_SENDREQ_MMS ||
1702                 pMsg->msgType.subType == MSG_SENDCONF_MMS) {
1703                 MSG_ERROR_T err = MSG_SUCCESS;
1704                 MMS_MESSAGE_DATA_S      mmsMsg;
1705                 char *pDestMsg = NULL;
1706
1707                 // call mms plugin to get mms specific message data
1708                 MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(pMsg->msgType.mainType);
1709                 memset(&mmsMsg, 0, sizeof(MMS_MESSAGE_DATA_S));
1710                 err =  plg->getMmsMessage(pMsg, pSendOptInfo, &mmsMsg, &pDestMsg);
1711
1712                 if (err != MSG_SUCCESS) {
1713                         if (pDestMsg) {
1714                                 free(pDestMsg);
1715                                 pDestMsg = NULL;
1716                         }
1717                         return MSG_ERR_STORAGE_ERROR;
1718                 }
1719
1720                 memset(pMsg->msgData, 0, MAX_MSG_DATA_LEN+1);
1721
1722                 // Encode MMS specific data to MMS_MESSAGE_DATA_S
1723                 if (pMsg->dataSize > MAX_MSG_DATA_LEN) {
1724                         // Save Message Data into File
1725                         char tempFileName[MAX_COMMON_INFO_SIZE+1];
1726                         memset(tempFileName, 0x00, sizeof(tempFileName));
1727
1728                         if (MsgCreateFileName(tempFileName) == false) {
1729                                 if(pDestMsg) {
1730                                         free (pDestMsg);
1731                                         pDestMsg = NULL;
1732                                 }
1733                                 return MSG_ERR_STORAGE_ERROR;
1734                         }
1735                         MSG_DEBUG("Save Message Data into file : size[%d] name[%s]\n", pMsg->dataSize, tempFileName);
1736
1737                         if (MsgWriteIpcFile(tempFileName, pDestMsg, pMsg->dataSize) == false) {
1738                                 if(pDestMsg) {
1739                                         free (pDestMsg);
1740                                         pDestMsg = NULL;
1741                                 }
1742                                 return MSG_ERR_STORAGE_ERROR;
1743                         }
1744                         strncpy(pMsg->msgData, tempFileName, MAX_MSG_DATA_LEN);
1745                         //pMsg->dataSize = strlen(fileName);
1746                         pMsg->bTextSms = false;
1747                 } else {
1748                         strncpy(pMsg->msgData, pDestMsg, pMsg->dataSize);
1749                         pMsg->bTextSms = true;
1750                 }
1751
1752                 if (pDestMsg) {
1753                         free(pDestMsg);
1754                         pDestMsg = NULL;
1755                 }
1756         }
1757
1758         // Get SMS Sending Options
1759         if (pMsg->msgType.mainType == MSG_SMS_TYPE && pSendOptInfo != NULL) {
1760                 MsgStoGetSmsSendOpt(pMsg->msgId, pSendOptInfo);
1761         }
1762
1763         return MSG_SUCCESS;
1764 }
1765
1766
1767 MSG_ERROR_T MsgStoGetFolderViewList(MSG_FOLDER_ID_T FolderId, const MSG_SORT_RULE_S *pSortRule, MSG_LIST_S *pMsgFolderViewList)
1768 {
1769         if (pMsgFolderViewList == NULL) {
1770                 MSG_DEBUG("pMsgFolderViewList is NULL");
1771                 return MSG_ERR_NULL_POINTER;
1772         }
1773
1774         int rowCnt = 0;
1775         int index = 27; // numbers of index
1776
1777         unsigned int addrId = 0;
1778
1779         char sqlQuery[MAX_QUERY_LEN+1];
1780         char sqlSort[64];
1781         char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
1782         char displayName[MAX_DISPLAY_NAME_LEN+1];
1783         // Get Name Order
1784         int order = MsgGetContactNameOrder();
1785
1786         // Get Message In Folder
1787         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1788
1789         if (FolderId == MSG_ALLBOX_ID) {
1790                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MSG_ID, A.ADDRESS_ID, A.FOLDER_ID, A.REFERENCE_ID, A.STORAGE_ID, A.MAIN_TYPE, A.SUB_TYPE, \
1791                                                         A.DISPLAY_TIME, A.DATA_SIZE, A.NETWORK_STATUS, A.READ_STATUS, A.PROTECTED, A.BACKUP, A.PRIORITY, \
1792                                                         A.MSG_DIRECTION, A.SCHEDULED_TIME, A.SUBJECT, A.MSG_TEXT, B.ADDRESS_TYPE, B.RECIPIENT_TYPE, \
1793                                                         B.CONTACT_ID, B.ADDRESS_VAL, B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME, A.ATTACHMENT_COUNT, A.THUMB_PATH \
1794                                                                 FROM %s A, %s B \
1795                                                              WHERE A.ADDRESS_ID = B.ADDRESS_ID \
1796                                                                   AND A.FOLDER_ID < %d ",
1797                                                 MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, MSG_CBMSGBOX_ID);
1798         } else {
1799                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MSG_ID, A.ADDRESS_ID, A.FOLDER_ID, A.REFERENCE_ID, A.STORAGE_ID, A.MAIN_TYPE, A.SUB_TYPE, \
1800                                                         A.DISPLAY_TIME, A.DATA_SIZE, A.NETWORK_STATUS, A.READ_STATUS, A.PROTECTED, A.BACKUP, A.PRIORITY, \
1801                                                         A.MSG_DIRECTION, A.SCHEDULED_TIME, A.SUBJECT, A.MSG_TEXT, B.ADDRESS_TYPE, B.RECIPIENT_TYPE, \
1802                                                         B.CONTACT_ID, B.ADDRESS_VAL, B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME, A.ATTACHMENT_COUNT, A.THUMB_PATH \
1803                                                                 FROM %s A, %s B \
1804                                                              WHERE A.ADDRESS_ID = B.ADDRESS_ID \
1805                                                                   AND A.FOLDER_ID = %d ",
1806                                                 MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, FolderId);
1807         }
1808
1809         memset(sqlSort, 0x00, sizeof(sqlSort));
1810         MsgMakeSortRule(pSortRule, sqlSort);
1811         strncat(sqlQuery, sqlSort, strlen(sqlSort));
1812
1813         MSG_ERROR_T  err = dbHandle.getTable(sqlQuery, &rowCnt);
1814
1815         if (err == MSG_ERR_DB_NORECORD) {
1816                 pMsgFolderViewList->nCount = 0;
1817                 pMsgFolderViewList->msgInfo = NULL;
1818
1819                 dbHandle.freeTable();
1820
1821                 return MSG_SUCCESS;
1822         } else if (err != MSG_SUCCESS) {
1823                 MSG_DEBUG("sqlQuery is - %s", sqlQuery);
1824                 dbHandle.freeTable();
1825                 return err;
1826         }
1827
1828         pMsgFolderViewList->nCount = rowCnt;
1829
1830         MSG_DEBUG("pMsgCommInfoList->nCount [%d]", pMsgFolderViewList->nCount);
1831
1832         pMsgFolderViewList->msgInfo = (msg_message_t *)new char[sizeof(MSG_MESSAGE_S *) * rowCnt];
1833
1834         MSG_MESSAGE_S *pTmp = NULL;
1835
1836         for (int i = 0; i < rowCnt; i++) {
1837                 pMsgFolderViewList->msgInfo[i] = (msg_message_t)new char[sizeof(MSG_MESSAGE_S)];
1838
1839                 pTmp = (MSG_MESSAGE_S *)pMsgFolderViewList->msgInfo[i];
1840
1841                 memset(pTmp, 0x00, sizeof(pTmp));
1842
1843                 pTmp->pData = NULL;
1844                 pTmp->pMmsData = NULL;
1845
1846                 pTmp->msgId = dbHandle.getColumnToInt(index++);
1847                 addrId = dbHandle.getColumnToInt(index++);
1848                 pTmp->folderId = dbHandle.getColumnToInt(index++);
1849                 pTmp->referenceId = dbHandle.getColumnToInt(index++);
1850                 pTmp->storageId = dbHandle.getColumnToInt(index++);
1851                 pTmp->msgType.mainType = dbHandle.getColumnToInt(index++);
1852                 pTmp->msgType.subType = dbHandle.getColumnToInt(index++);
1853                 pTmp->displayTime = (time_t)dbHandle.getColumnToInt(index++);
1854                 pTmp->dataSize = dbHandle.getColumnToInt(index++);
1855                 pTmp->networkStatus = dbHandle.getColumnToInt(index++);
1856                 pTmp->bRead = dbHandle.getColumnToInt(index++);
1857                 pTmp->bProtected = dbHandle.getColumnToInt(index++);
1858                 pTmp->bBackup = dbHandle.getColumnToInt(index++);
1859                 pTmp->priority = dbHandle.getColumnToInt(index++);
1860                 pTmp->direction= dbHandle.getColumnToInt(index++);
1861                 pTmp->scheduledTime = (time_t)dbHandle.getColumnToInt(index++);
1862
1863                 dbHandle.getColumnToString(index++, MAX_SUBJECT_LEN, pTmp->subject);
1864
1865                 if (pTmp->msgType.mainType == MSG_MMS_TYPE &&
1866                         (pTmp->networkStatus == MSG_NETWORK_RETRIEVING || pTmp->networkStatus == MSG_NETWORK_RETRIEVE_FAIL || pTmp->msgType.subType == MSG_NOTIFICATIONIND_MMS)) {
1867                         pTmp->pData = NULL;
1868                         index++;
1869                 } else {
1870                         pTmp->pData = (void *)new char[pTmp->dataSize + 2];
1871                         memset(pTmp->pData, 0x00, sizeof(pTmp->pData));
1872
1873                         dbHandle.getColumnToString(index++, pTmp->dataSize+1, (char *)pTmp->pData);
1874                 }
1875
1876                 if (addrId > 0)
1877                         pTmp->nAddressCnt = 1;
1878                 else
1879                         pTmp->nAddressCnt = 0;
1880
1881                 pTmp->addressList[0].threadId = addrId;
1882                 pTmp->addressList[0].addressType = dbHandle.getColumnToInt(index++);
1883                 pTmp->addressList[0].recipientType = dbHandle.getColumnToInt(index++);
1884                 pTmp->addressList[0].contactId = dbHandle.getColumnToInt(index++);
1885
1886                 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pTmp->addressList[0].addressVal);
1887
1888                 memset(displayName, 0x00, sizeof(displayName));
1889                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, displayName);
1890
1891                 memset(firstName, 0x00, sizeof(firstName));
1892                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, firstName);
1893
1894                 memset(lastName, 0x00, sizeof(lastName));
1895                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, lastName);
1896
1897
1898                 if (strlen(displayName) <= 0) {
1899                         if (order == 0) {
1900                                 if (firstName[0] != '\0') {
1901                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
1902                                 }
1903
1904                                 if (lastName[0] != '\0') {
1905                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
1906                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
1907                                 }
1908                         } else if (order == 1) {
1909                                 if (lastName[0] != '\0') {
1910                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
1911                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
1912                                 }
1913
1914                                 if (firstName[0] != '\0') {
1915                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
1916                                 }
1917                         }
1918                 }
1919
1920                 strncpy(pTmp->addressList[0].displayName, displayName, MAX_DISPLAY_NAME_LEN);
1921
1922                 pTmp->attachCount = dbHandle.getColumnToInt(index++);
1923
1924                 dbHandle.getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pTmp->thumbPath);
1925         }
1926
1927         dbHandle.freeTable();
1928
1929         return MSG_SUCCESS;
1930 }
1931
1932
1933 MSG_ERROR_T MsgStoAddSyncMLMessage(MSG_MESSAGE_INFO_S *pMsgInfo, int ExtId, int PinCode)
1934 {
1935         MSG_BEGIN();
1936
1937         MSG_ERROR_T err = MSG_SUCCESS;
1938
1939         char sqlQuery[MAX_QUERY_LEN+1];
1940
1941         unsigned int rowId = 0;
1942         unsigned int addrId = 0;
1943
1944         dbHandle.beginTrans();
1945
1946         if (pMsgInfo->nAddressCnt > 0) {
1947                 err = MsgStoAddAddress(&dbHandle, &(pMsgInfo->addressList[0]), &addrId);
1948
1949                 if (err != MSG_SUCCESS) {
1950                         dbHandle.endTrans(false);
1951                         return err;
1952                 }
1953         }
1954
1955         // Add Message Table
1956         rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo, addrId);
1957
1958         if (rowId <= 0) {
1959                 dbHandle.endTrans(false);
1960                 return MSG_ERR_DB_ROW;
1961         }
1962
1963         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1964         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d);",
1965                                         MSGFW_SYNCML_MSG_TABLE_NAME, rowId, ExtId, PinCode);
1966
1967         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1968                 dbHandle.endTrans(false);
1969                 return MSG_ERR_DB_EXEC;
1970         }
1971
1972         if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
1973                 dbHandle.endTrans(false);
1974                 return MSG_ERR_STORAGE_ERROR;
1975         }
1976
1977         dbHandle.endTrans(true);
1978
1979         pMsgInfo->msgId = (MSG_MESSAGE_ID_T)rowId;
1980         pMsgInfo->referenceId = (MSG_REFERENCE_ID_T)rowId;
1981
1982         MsgSoundPlayStart();
1983
1984         int smsCnt = 0;
1985         int mmsCnt = 0;
1986
1987         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
1988         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
1989
1990         MsgSettingHandleNewMsg(smsCnt, mmsCnt);
1991
1992         MsgInsertNoti(&dbHandle, pMsgInfo);
1993
1994         MSG_END();
1995
1996         return MSG_SUCCESS;
1997 }
1998
1999
2000 MSG_ERROR_T MsgStoGetMsgType(MSG_MESSAGE_ID_T msgId, MSG_MESSAGE_TYPE_S *pMsgType)
2001 {
2002         char sqlQuery[MAX_QUERY_LEN+1];
2003
2004         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2005
2006         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE FROM %s WHERE MSG_ID = %d;",
2007                                                         MSGFW_MESSAGE_TABLE_NAME, msgId);
2008
2009         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
2010                 return MSG_ERR_DB_PREPARE;
2011
2012         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
2013                 pMsgType->mainType = dbHandle.columnInt(0);
2014                 pMsgType->subType = dbHandle.columnInt(1);
2015         }
2016
2017         dbHandle.finalizeQuery();
2018
2019         return MSG_SUCCESS;
2020 }
2021
2022
2023 MSG_ERROR_T MsgStoGetQuickPanelData(MSG_QUICKPANEL_TYPE_T Type, MSG_MESSAGE_INFO_S *pMsg)
2024 {
2025         MSG_ERROR_T     err = MSG_SUCCESS;
2026
2027         char sqlQuery[MAX_QUERY_LEN+1];
2028
2029         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2030
2031         if (Type == MSG_QUICKPANEL_SMS) {
2032                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE FOLDER_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE = %d AND READ_STATUS = 0 ORDER BY DISPLAY_TIME DESC;",
2033                         MSGFW_MESSAGE_TABLE_NAME, MSG_INBOX_ID, MSG_SMS_TYPE, MSG_NORMAL_SMS);
2034         } else if (Type == MSG_QUICKPANEL_MMS) {
2035                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE FOLDER_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d) AND READ_STATUS = 0 ORDER BY DISPLAY_TIME DESC;",
2036                         MSGFW_MESSAGE_TABLE_NAME, MSG_INBOX_ID, MSG_MMS_TYPE, MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS);
2037         } else if (Type == MSG_QUICKPANEL_DELIVER_REP) {
2038                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE SUB_TYPE IN (%d, %d) AND READ_STATUS = 0 ORDER BY DISPLAY_TIME DESC;",
2039                         MSGFW_MESSAGE_TABLE_NAME, MSG_STATUS_REPORT_SMS, MSG_DELIVERYIND_MMS);
2040         } else if (Type == MSG_QUICKPANEL_READ_REP) {
2041                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE = %d AND READ_STATUS = 0 ORDER BY DISPLAY_TIME DESC;",
2042                         MSGFW_MESSAGE_TABLE_NAME, MSG_MMS_TYPE, MSG_READORGIND_MMS);
2043         } else if (Type == MSG_QUICKPANEL_VOICEMAIL) {
2044                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE FOLDER_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE = %d AND READ_STATUS = 0 ORDER BY DISPLAY_TIME DESC;",
2045                         MSGFW_MESSAGE_TABLE_NAME, MSG_INBOX_ID, MSG_SMS_TYPE, MSG_MWI_VOICE_SMS);
2046         } else if (Type == MSG_QUICKPANEL_MMS_NOTI) {
2047                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE FOLDER_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE = %d AND READ_STATUS = 0 ORDER BY DISPLAY_TIME DESC;",
2048                         MSGFW_MESSAGE_TABLE_NAME, MSG_INBOX_ID, MSG_MMS_TYPE, MSG_NOTIFICATIONIND_MMS);
2049         }
2050
2051         // Get Message ID
2052         MSG_MESSAGE_ID_T msgId;
2053
2054         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
2055                 return MSG_ERR_DB_PREPARE;
2056
2057         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
2058                 msgId = dbHandle.columnInt(0);
2059         } else {
2060                 dbHandle.finalizeQuery();
2061                 return MSG_ERR_DB_STEP;
2062         }
2063
2064         dbHandle.finalizeQuery();
2065
2066         // Get Message Info
2067         err = MsgStoGetMessage(msgId, pMsg, NULL);
2068
2069         return err;
2070 }
2071
2072
2073 MSG_ERROR_T MsgStoGetThreadViewList(const MSG_SORT_RULE_S *pSortRule, MSG_THREAD_VIEW_LIST_S *pThreadViewList)
2074 {
2075         pThreadViewList->nCount = 0;
2076         pThreadViewList->msgThreadInfo = NULL;
2077
2078         int rowCnt = 0;
2079         int index = 15; // numbers of index
2080
2081         char sqlQuery[MAX_QUERY_LEN+1];
2082         char sqlSort[64];
2083
2084         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2085
2086         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID, CONTACT_ID, ADDRESS_VAL, \
2087                         DISPLAY_NAME, FIRST_NAME, LAST_NAME, IMAGE_PATH, \
2088                         UNREAD_CNT, SMS_CNT, MMS_CNT, MAIN_TYPE, SUB_TYPE, \
2089                         MSG_DIRECTION, MSG_TIME, MSG_TEXT \
2090                         FROM %s \
2091                         WHERE ADDRESS_ID <> 0 AND (SMS_CNT > 0 OR MMS_CNT > 0) ",
2092                         MSGFW_ADDRESS_TABLE_NAME);
2093
2094         memset(sqlSort, 0x00, sizeof(sqlSort));
2095         MsgMakeSortRule(pSortRule, sqlSort);
2096         strncat(sqlQuery, sqlSort, sizeof(sqlQuery) - strlen(sqlQuery) - 1);
2097
2098         MSG_ERROR_T  err = dbHandle.getTable(sqlQuery, &rowCnt);
2099
2100         if (err == MSG_ERR_DB_NORECORD) {
2101                 dbHandle.freeTable();
2102
2103                 return MSG_SUCCESS;
2104         } else if (err != MSG_SUCCESS) {
2105                 MSG_DEBUG("%s", sqlQuery);
2106
2107                 dbHandle.freeTable();
2108
2109                 return err;
2110         }
2111
2112         char firstName[MAX_THREAD_NAME_LEN+1];
2113         char lastName[MAX_THREAD_NAME_LEN+1];
2114         char displayName[MAX_DISPLAY_NAME_LEN+1];
2115
2116         int order = MsgGetContactNameOrder();
2117
2118         pThreadViewList->nCount = rowCnt;
2119
2120         MSG_DEBUG("pThreadViewList->nCount [%d]", pThreadViewList->nCount);
2121
2122         pThreadViewList->msgThreadInfo = (msg_thread_view_t *)new char[sizeof(MSG_THREAD_VIEW_S *)*rowCnt];
2123
2124         MSG_THREAD_VIEW_S *pTmp = NULL;
2125
2126         for (int i = 0; i < rowCnt; i++) {
2127                 pThreadViewList->msgThreadInfo[i] = (msg_thread_view_t)new char[sizeof(MSG_THREAD_VIEW_S)];
2128
2129                 pTmp = (MSG_THREAD_VIEW_S *)pThreadViewList->msgThreadInfo[i];
2130
2131                 pTmp->threadId = dbHandle.getColumnToInt(index++);
2132                 pTmp->contactId = dbHandle.getColumnToInt(index++);
2133
2134                 memset(pTmp->threadAddr, 0x00, sizeof(pTmp->threadAddr));
2135                 dbHandle.getColumnToString(index++, MAX_THREAD_ADDR_LEN, pTmp->threadAddr);
2136
2137                 memset(displayName, 0x00, sizeof(displayName));
2138                 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, displayName);
2139
2140                 memset(firstName, 0x00, sizeof(firstName));
2141                 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, firstName);
2142
2143                 memset(lastName, 0x00, sizeof(lastName));
2144                 dbHandle.getColumnToString(index++, MAX_THREAD_NAME_LEN, lastName);
2145
2146                 if (strlen(displayName) <= 0) {
2147                         if (order == 0) {
2148                                 if (firstName[0] != '\0') {
2149                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
2150                                 }
2151
2152                                 if (lastName[0] != '\0') {
2153                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
2154                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
2155                                 }
2156                         } else if (order == 1) {
2157                                 if (lastName[0] != '\0') {
2158                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
2159                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
2160                                 }
2161
2162                                 if (firstName[0] != '\0') {
2163                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
2164                                 }
2165                         }
2166                 }
2167
2168                 memset(pTmp->threadName, 0x00, sizeof(pTmp->threadName));
2169                 strncpy(pTmp->threadName, displayName, MAX_THREAD_NAME_LEN);
2170
2171                 memset(pTmp->threadImagePath, 0x00, sizeof(pTmp->threadImagePath));
2172                 dbHandle.getColumnToString(index++, MAX_IMAGE_PATH_LEN, pTmp->threadImagePath);
2173
2174                 pTmp->unreadCnt = dbHandle.getColumnToInt(index++);
2175                 pTmp->smsCnt = dbHandle.getColumnToInt(index++);
2176                 pTmp->mmsCnt = dbHandle.getColumnToInt(index++);
2177
2178                 pTmp->threadType.mainType = dbHandle.getColumnToInt(index++);
2179                 pTmp->threadType.subType = dbHandle.getColumnToInt(index++);
2180
2181                 pTmp->direction = dbHandle.getColumnToInt(index++);
2182                 pTmp->threadTime = (time_t)dbHandle.getColumnToInt(index++);
2183
2184                 memset(pTmp->threadData, 0x00, sizeof(pTmp->threadData));
2185                 dbHandle.getColumnToString(index++, MAX_THREAD_DATA_LEN, pTmp->threadData);
2186         }
2187
2188         dbHandle.freeTable();
2189
2190         return MSG_SUCCESS;
2191 }
2192
2193
2194 MSG_ERROR_T MsgStoGetConversationViewList(MSG_THREAD_ID_T ThreadId, MSG_LIST_S *pConvViewList)
2195 {
2196         MSG_BEGIN();
2197
2198         pConvViewList->nCount = 0;
2199         pConvViewList->msgInfo = NULL;
2200
2201         int rowCnt = 0;
2202         int index = 20; /** numbers of index */
2203
2204         unsigned int addrId = 0;
2205
2206         char sqlQuery[MAX_QUERY_LEN+1];
2207
2208         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2209
2210         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID, ADDRESS_ID, FOLDER_ID, REFERENCE_ID, STORAGE_ID, MAIN_TYPE, SUB_TYPE, \
2211                                                         DISPLAY_TIME, DATA_SIZE, NETWORK_STATUS, READ_STATUS, PROTECTED, BACKUP, PRIORITY, \
2212                                                         MSG_DIRECTION, SCHEDULED_TIME, SUBJECT, MSG_TEXT, ATTACHMENT_COUNT, THUMB_PATH \
2213                                                         FROM %s \
2214                                                         WHERE ADDRESS_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d \
2215                                                         ORDER BY DISPLAY_TIME, MSG_ID ASC;",
2216                                         MSGFW_MESSAGE_TABLE_NAME, ThreadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID);
2217
2218         MSG_ERROR_T err = dbHandle.getTable(sqlQuery, &rowCnt);
2219
2220         if (err == MSG_ERR_DB_NORECORD) {
2221                 dbHandle.freeTable();
2222                 return MSG_SUCCESS;
2223         } else if (err != MSG_SUCCESS) {
2224                 MSG_DEBUG("%s", sqlQuery);
2225                 dbHandle.freeTable();
2226                 return err;
2227         }
2228
2229         pConvViewList->nCount = rowCnt;
2230
2231         MSG_DEBUG("pConvViewList->nCount [%d]", pConvViewList->nCount);
2232
2233         pConvViewList->msgInfo = (msg_message_t *)new char[sizeof(MSG_MESSAGE_S *) * rowCnt];
2234
2235         MSG_MESSAGE_S *pTmp = NULL;
2236
2237         for (int i = 0; i < rowCnt; i++) {
2238                 pConvViewList->msgInfo[i] = (msg_message_t)new char[sizeof(MSG_MESSAGE_S)];
2239
2240                 pTmp = (MSG_MESSAGE_S *)pConvViewList->msgInfo[i];
2241
2242                 memset(pTmp, 0x00, sizeof(pTmp));
2243
2244                 pTmp->pData = NULL;
2245                 pTmp->pMmsData = NULL;
2246
2247                 pTmp->msgId = dbHandle.getColumnToInt(index++);
2248                 addrId = dbHandle.getColumnToInt(index++);
2249                 pTmp->folderId = dbHandle.getColumnToInt(index++);
2250                 pTmp->referenceId = dbHandle.getColumnToInt(index++);
2251                 pTmp->storageId = dbHandle.getColumnToInt(index++);
2252                 pTmp->msgType.mainType = dbHandle.getColumnToInt(index++);
2253                 pTmp->msgType.subType = dbHandle.getColumnToInt(index++);
2254                 pTmp->displayTime = (time_t)dbHandle.getColumnToInt(index++);
2255                 pTmp->dataSize = dbHandle.getColumnToInt(index++);
2256                 pTmp->networkStatus = dbHandle.getColumnToInt(index++);
2257                 pTmp->bRead = dbHandle.getColumnToInt(index++);
2258                 pTmp->bProtected = dbHandle.getColumnToInt(index++);
2259                 pTmp->bBackup = dbHandle.getColumnToInt(index++);
2260                 pTmp->priority = dbHandle.getColumnToInt(index++);
2261                 pTmp->direction = dbHandle.getColumnToInt(index++);
2262                 pTmp->scheduledTime = (time_t)dbHandle.getColumnToInt(index++);
2263
2264                 dbHandle.getColumnToString(index++, MAX_SUBJECT_LEN, pTmp->subject);
2265
2266                 if (pTmp->msgType.mainType == MSG_MMS_TYPE &&
2267                         (pTmp->networkStatus == MSG_NETWORK_RETRIEVING || pTmp->networkStatus == MSG_NETWORK_RETRIEVE_FAIL || pTmp->msgType.subType == MSG_NOTIFICATIONIND_MMS)) {
2268                         pTmp->pData = NULL;
2269                         index++;
2270                 } else {
2271                         pTmp->pData = (void *)new char[pTmp->dataSize+2];
2272                         memset(pTmp->pData, 0x00, pTmp->dataSize+2);
2273
2274                         dbHandle.getColumnToString(index++, pTmp->dataSize+1, (char *)pTmp->pData);
2275                 }
2276
2277                 pTmp->attachCount = dbHandle.getColumnToInt(index++);
2278
2279                 dbHandle.getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pTmp->thumbPath);
2280
2281         }
2282
2283         dbHandle.freeTable();
2284
2285         MSG_END();
2286
2287         return MSG_SUCCESS;
2288 }
2289
2290
2291 MSG_ERROR_T MsgStoDeleteThreadMessageList(MSG_THREAD_ID_T ThreadId, MSG_MSGID_LIST_S *pMsgIdList)
2292 {
2293         MSG_ERROR_T err = MSG_SUCCESS;
2294
2295         char sqlQuery[MAX_QUERY_LEN+1];
2296         /*** Get msg id list **/
2297         MSG_MSGID_LIST_S *pToDeleteMsgIdList = NULL;
2298
2299         int rowCnt = 0;
2300         int index = 1;
2301         // Set Indicator
2302         int smsCnt = 0;
2303         int mmsCnt = 0;
2304
2305         const char *tableList[] = {MSGFW_PUSH_MSG_TABLE_NAME, MSGFW_CB_MSG_TABLE_NAME,
2306                                         MSGFW_SYNCML_MSG_TABLE_NAME, MSGFW_SCHEDULED_MSG_TABLE_NAME,
2307                                         MSGFW_SMS_SENDOPT_TABLE_NAME, MMS_PLUGIN_MESSAGE_TABLE_NAME,
2308                                         MSGFW_MESSAGE_TABLE_NAME};
2309
2310         int listCnt = sizeof(tableList)/sizeof(char *);
2311
2312         pToDeleteMsgIdList = (MSG_MSGID_LIST_S *)new char[sizeof(MSG_MSGID_LIST_S)];
2313         memset(pToDeleteMsgIdList, 0x00, sizeof(MSG_MSGID_LIST_S));
2314
2315         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2316         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE ADDRESS_ID = %d",
2317                         MSGFW_MESSAGE_TABLE_NAME, ThreadId);
2318
2319         err = dbHandle.getTable(sqlQuery, &rowCnt);
2320
2321         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
2322                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
2323
2324                 dbHandle.freeTable();
2325
2326                 goto FREE_MEMORY;
2327         }
2328
2329         if (rowCnt <= 0) {
2330                 dbHandle.freeTable();
2331                 err = MSG_SUCCESS;
2332
2333                 goto FREE_MEMORY;
2334         }
2335
2336         pToDeleteMsgIdList->nCount = rowCnt;
2337
2338         MSG_DEBUG("pToDeleteMsgIdList->nCount [%d]", pToDeleteMsgIdList->nCount);
2339
2340         pToDeleteMsgIdList->msgIdList = (MSG_MESSAGE_ID_T *)new char[sizeof(MSG_MESSAGE_ID_T) * rowCnt];
2341
2342         for (int i = 0; i < rowCnt; i++)
2343                 pToDeleteMsgIdList->msgIdList[i] = dbHandle.getColumnToInt(index++);
2344
2345         dbHandle.freeTable();
2346         /*** **/
2347
2348         /*** Delete Sim Message **/
2349         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2350
2351         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID \
2352                                         FROM %s \
2353                                      WHERE ADDRESS_ID = %d \
2354                                           AND STORAGE_ID = %d",
2355                         MSGFW_MESSAGE_TABLE_NAME, ThreadId, MSG_STORAGE_SIM);
2356
2357         rowCnt = 0;
2358
2359         err = dbHandle.getTable(sqlQuery, &rowCnt);
2360
2361         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
2362                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
2363
2364                 dbHandle.freeTable();
2365
2366                 goto FREE_MEMORY;
2367         }
2368
2369         for (int i = 1; i <= rowCnt; i++) {
2370                 err = MsgStoDeleteMessage(dbHandle.getColumnToInt(i), false);
2371
2372                 if (err != MSG_SUCCESS) {
2373                         MSG_DEBUG("MsgStoDeleteMessage() Error!!!");
2374
2375                         dbHandle.freeTable();
2376
2377                         goto FREE_MEMORY;
2378                 }
2379         }
2380
2381         dbHandle.freeTable();
2382         /*** **/
2383
2384         dbHandle.beginTrans();
2385
2386         for (int i = 0; i < listCnt; i++) {
2387                 if (!strcmp(tableList[i], MMS_PLUGIN_MESSAGE_TABLE_NAME)) {
2388                         //MMS_PLUGIN_MESSAGE_TABLE_NAME and MMS_PLUGIN_ATTRIBUTE_TABLE_NAME will be updated together
2389                         int rowCnt = 0;
2390                         int msgCnt = 0;
2391                         int index = 2;
2392
2393                         //get mms msg id list
2394                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2395                         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID, REFERENCE_ID FROM %s \
2396                                                                 WHERE ADDRESS_ID = %d AND MAIN_TYPE = %d",
2397                                                                 MSGFW_MESSAGE_TABLE_NAME, ThreadId, MSG_MMS_TYPE);
2398
2399                         err = dbHandle.getTable(sqlQuery, &rowCnt);
2400                         MSG_DEBUG("rowCnt %d", rowCnt);
2401
2402                         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
2403                                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
2404
2405                                 dbHandle.freeTable();
2406                                 dbHandle.endTrans(false);
2407
2408                                 goto FREE_MEMORY;
2409                         }
2410
2411                         for (int i = 1; i <= rowCnt; i++) {
2412                                 int msgId = 0;
2413                                 int refId = 0;
2414
2415                                 msgId = dbHandle.getColumnToInt(index++);
2416                                 refId = dbHandle.getColumnToInt(index++);
2417
2418                                 //get multiple recipient message count
2419                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
2420                                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE REFERENCE_ID = %d;",
2421                                                                 MSGFW_MESSAGE_TABLE_NAME, refId);
2422
2423                                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
2424                                         dbHandle.freeTable();
2425                                         dbHandle.endTrans(false);
2426                                         err = MSG_ERR_DB_PREPARE;
2427
2428                                         goto FREE_MEMORY;
2429                                 }
2430
2431                                 msgCnt = 0;
2432
2433                                 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
2434                                         msgCnt = dbHandle.columnInt(0);
2435                                 } else {
2436                                         dbHandle.finalizeQuery();
2437                                         dbHandle.freeTable();
2438                                         dbHandle.endTrans(false);
2439                                         err = MSG_ERR_DB_STEP;
2440
2441                                         goto FREE_MEMORY;
2442                                 }
2443
2444                                 dbHandle.finalizeQuery();
2445
2446                                 //delete mms raw file and db(mms_table and mms_attr_table) data
2447                                 if (msgCnt == 1) {
2448                                         char filePath[MSG_FILEPATH_LEN_MAX] = {0,};
2449                                         char dirPath[MSG_FILEPATH_LEN_MAX] = {0,};
2450                                         char thumbnailPath[MSG_FILEPATH_LEN_MAX] = {0,};
2451
2452                                         //get raw file_path
2453                                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2454                                         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILE_PATH FROM %s WHERE REFERENCE_ID = %d;",
2455                                                                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, refId);
2456
2457                                         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
2458                                                 dbHandle.freeTable();
2459                                                 dbHandle.endTrans(false);
2460                                                 err = MSG_ERR_DB_PREPARE;
2461
2462                                                 goto FREE_MEMORY;
2463                                         }
2464
2465                                         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
2466                                                 strncpy(filePath, (char *)dbHandle.columnText(0), MSG_FILEPATH_LEN_MAX);
2467                                         } else {
2468                                                 dbHandle.finalizeQuery();
2469                                                 dbHandle.freeTable();
2470                                                 dbHandle.endTrans(false);
2471                                                 err = MSG_ERR_DB_STEP;
2472
2473                                                 goto FREE_MEMORY;
2474                                         }
2475
2476                                         dbHandle.finalizeQuery();
2477
2478                                         MSG_DEBUG("filePath [%s]", filePath);
2479
2480                                         //delete raw file
2481                                         snprintf(dirPath, sizeof(dirPath), "%s.dir", filePath);
2482
2483                                         if (remove(filePath) == -1)
2484                                                 MSG_DEBUG("Fail to delete file [%s]", filePath);
2485                                         else
2486                                                 MSG_DEBUG("Success to delete file [%s]", filePath);
2487
2488                                         MsgRmRf(dirPath);
2489                                         // remove directory also
2490                                         rmdir(dirPath);
2491
2492                                         // delete thumbnail
2493                                         char *fileName = NULL;
2494                                         fileName = strrchr(filePath, '/');
2495
2496                                         snprintf(thumbnailPath, sizeof(thumbnailPath), MSG_THUMBNAIL_PATH"%s.jpg", fileName+1);
2497
2498                                         if (remove(thumbnailPath) == -1)
2499                                                 MSG_DEBUG("Fail to delete thumbnail [%s]", thumbnailPath);
2500                                         else
2501                                                 MSG_DEBUG("Success to delete thumbnail [%s]", thumbnailPath);
2502
2503                                         //delete MMS_PLUGIN_MESSAGE_TABLE_NAME table data
2504                                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2505                                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE REFERENCE_ID = %d;",
2506                                                                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, refId);
2507
2508                                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
2509                                                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
2510                                                 dbHandle.freeTable();
2511                                                 dbHandle.endTrans(false);
2512                                                 err = MSG_ERR_DB_EXEC;
2513
2514                                                 goto FREE_MEMORY;
2515                                         }
2516
2517                                         //delete  MMS_PLUGIN_ATTRIBUTE_TABLE_NAME table data
2518                                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2519                                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE REFERENCE_ID = %d;",
2520                                                                                 MMS_PLUGIN_ATTRIBUTE_TABLE_NAME, refId);
2521
2522                                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
2523                                                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
2524                                                 dbHandle.freeTable();
2525                                                 dbHandle.endTrans(false);
2526                                                 err = MSG_ERR_DB_EXEC;
2527
2528                                                 goto FREE_MEMORY;
2529                                         }
2530                                 } else {
2531                                         MSG_DEBUG("There is remained multi-recipient mms for this msg = %d", msgId);
2532                                 }
2533                         }
2534
2535                         dbHandle.freeTable();
2536                 } else {
2537                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2538                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID IN \
2539                                                                         (SELECT MSG_ID FROM %s WHERE ADDRESS_ID = %d);",
2540                                                                         tableList[i], MSGFW_MESSAGE_TABLE_NAME, ThreadId);
2541
2542                         // Delete Message in specific folder from table
2543                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
2544                                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
2545                                 dbHandle.endTrans(false);
2546                                 err = MSG_ERR_DB_EXEC;
2547
2548                                 goto FREE_MEMORY;
2549                         }
2550                 }
2551         }
2552
2553         // Delete Address table
2554         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2555
2556         if (ThreadId > 0) {
2557                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s \
2558                                              WHERE ADDRESS_ID = %d;",
2559                                         MSGFW_ADDRESS_TABLE_NAME, ThreadId);
2560
2561                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
2562                         MSG_DEBUG("sqlQuery [%s]", sqlQuery);
2563                         dbHandle.endTrans(false);
2564                         err = MSG_ERR_DB_EXEC;
2565
2566                         goto FREE_MEMORY;
2567                 }
2568         }
2569
2570         dbHandle.endTrans(true);
2571
2572         MSG_MESSAGE_TYPE_S msgType;
2573
2574         msgType.mainType = MSG_SMS_TYPE;
2575         msgType.subType = MSG_NORMAL_SMS;
2576         msgType.classType = MSG_CLASS_NONE;
2577
2578         // Set memory status in SIM
2579         if (MsgStoCheckMsgCntFull(&dbHandle, &msgType, MSG_INBOX_ID) == MSG_SUCCESS) {
2580                 MSG_DEBUG("Set Memory Status");
2581
2582                 MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(MSG_SMS_TYPE);
2583
2584                 if (plg == NULL) {
2585                         MSG_DEBUG("SMS Plug-in is NULL");
2586                         err = MSG_ERR_NULL_POINTER;
2587
2588                         goto FREE_MEMORY;
2589                 }
2590
2591                 plg->setMemoryStatus(MSG_SUCCESS);
2592         }
2593
2594         // Set Indicator
2595         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
2596         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
2597
2598         MsgSettingSetIndicator(smsCnt, mmsCnt);
2599
2600 /*** Set pMsgIdList **/
2601         if (pMsgIdList != NULL && pToDeleteMsgIdList->nCount > 0) {
2602                 pMsgIdList->nCount = pToDeleteMsgIdList->nCount;
2603
2604                 pMsgIdList->msgIdList = (MSG_MESSAGE_ID_T *)new char[sizeof(MSG_MESSAGE_ID_T)*pToDeleteMsgIdList->nCount];
2605                 memcpy(pMsgIdList->msgIdList, pToDeleteMsgIdList->msgIdList, sizeof(MSG_MESSAGE_ID_T)*pToDeleteMsgIdList->nCount);
2606         }
2607 /*** **/
2608
2609         /*** Create thread  for noti delete. **/
2610         if (pToDeleteMsgIdList->nCount > 0) {
2611                 if (g_idle_add(startToDeleteNoti, (void *)pToDeleteMsgIdList) == 0) {
2612                         MSG_DEBUG("startToDeleteNoti not invoked: %s", strerror(errno));
2613                         // memory free
2614                         if (pToDeleteMsgIdList != NULL) {
2615                                 //free peer info list
2616                                 if (pToDeleteMsgIdList->msgIdList != NULL)
2617                                         delete [] pToDeleteMsgIdList->msgIdList;
2618
2619                                 delete [] pToDeleteMsgIdList;
2620                         }
2621                         err = MSG_ERR_UNKNOWN;
2622                 }
2623         }
2624         /*** **/
2625
2626         return MSG_SUCCESS;
2627
2628 FREE_MEMORY:
2629         MSG_DEBUG("Error case Free Memory");
2630         // memory free
2631         if (pToDeleteMsgIdList != NULL) {
2632                 //free peer info list
2633                 if (pToDeleteMsgIdList->msgIdList != NULL) {
2634                         delete [] pToDeleteMsgIdList->msgIdList;
2635                         pToDeleteMsgIdList->msgIdList = NULL;
2636                 }
2637
2638                 delete [] pToDeleteMsgIdList;
2639                 pToDeleteMsgIdList = NULL;
2640         }
2641
2642         return err;
2643
2644 }
2645
2646
2647 MSG_ERROR_T MsgStoCountMsgByContact(const MSG_THREAD_LIST_INDEX_S *pAddrInfo, MSG_THREAD_COUNT_INFO_S *pThreadCountInfo)
2648 {
2649         char sqlQuery[MAX_QUERY_LEN+1];
2650
2651         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2652
2653         if (pAddrInfo->contactId > 0) {
2654                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) AS TOTAL, \
2655                                                 SUM(CASE WHEN READ_STATUS=0 AND FOLDER_ID=%d THEN 1 ELSE 0 END), \
2656                                                 SUM(CASE WHEN MAIN_TYPE=%d THEN 1 ELSE 0 END), \
2657                                                 SUM(CASE WHEN MAIN_TYPE=%d THEN 1 ELSE 0 END) \
2658                                                 FROM (SELECT * FROM %s A  JOIN %s B ON A.ADDRESS_ID = B.ADDRESS_ID WHERE B.CONTACT_ID = %d)",
2659                                         MSG_INBOX_ID, MSG_SMS_TYPE, MSG_MMS_TYPE,
2660                                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, pAddrInfo->contactId);
2661         } else {
2662                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) AS TOTAL, \
2663                                                 SUM(CASE WHEN READ_STATUS=0 AND FOLDER_ID=%d THEN 1 ELSE 0 END), \
2664                                                 SUM(CASE WHEN MAIN_TYPE=%d THEN 1 ELSE 0 END), \
2665                                                 SUM(CASE WHEN MAIN_TYPE=%d THEN 1 ELSE 0 END) \
2666                                                 FROM (SELECT * FROM %s A  JOIN %s B ON A.ADDRESS_ID = B.ADDRESS_ID WHERE B.ADDRESS_VAL = '%s')",
2667                                         MSG_INBOX_ID, MSG_SMS_TYPE, MSG_MMS_TYPE,
2668                                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, pAddrInfo->msgAddrInfo.addressVal);
2669         }
2670
2671         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
2672                 return MSG_ERR_DB_PREPARE;
2673
2674         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
2675                 pThreadCountInfo->totalCount = dbHandle.columnInt(0);
2676                 pThreadCountInfo->unReadCount = dbHandle.columnInt(1);
2677                 pThreadCountInfo->smsMsgCount = dbHandle.columnInt(2);
2678                 pThreadCountInfo->mmsMsgCount = dbHandle.columnInt(3);
2679         } else {
2680                 dbHandle.finalizeQuery();
2681                 return MSG_ERR_DB_STEP;
2682         }
2683
2684         dbHandle.finalizeQuery();
2685
2686         return MSG_SUCCESS;
2687 }
2688
2689
2690 MSG_ERROR_T MsgStoSearchMessage(const char *pSearchString, MSG_THREAD_VIEW_LIST_S *pThreadViewList)
2691 {
2692         if (!pSearchString)
2693                 return MSG_ERR_NULL_POINTER;
2694
2695         // Clear Out Parameter
2696         pThreadViewList->nCount = 0;
2697         pThreadViewList->msgThreadInfo = NULL;
2698
2699         tr1::unordered_set<MSG_THREAD_ID_T> IdList;
2700         queue<MSG_THREAD_VIEW_S> searchList;
2701
2702         MSG_THREAD_VIEW_S threadView;
2703
2704         char sqlQuery[MAX_QUERY_LEN+1];
2705
2706         char firstName[MAX_DISPLAY_NAME_LEN+1];
2707         char lastName[MAX_DISPLAY_NAME_LEN+1];
2708         char displayName[MAX_DISPLAY_NAME_LEN+1];
2709
2710         // Get Name Order
2711         int order = MsgGetContactNameOrder();
2712
2713         // Replace string for '%' and '_' character
2714         char *ext1_str = NULL;
2715         char *ext2_str = NULL;
2716
2717         ext1_str = MsgStoReplaceString(pSearchString, "_", "\\_");
2718         ext2_str = MsgStoReplaceString(ext1_str, "%", "\\%");
2719
2720         // Search - Address, Name
2721         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2722
2723         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.ADDRESS_ID, A.CONTACT_ID, A.ADDRESS_VAL, \
2724                                                 A.DISPLAY_NAME, A.FIRST_NAME, A.LAST_NAME, A.IMAGE_PATH, \
2725                                                 A.UNREAD_CNT, A.SMS_CNT, A.MMS_CNT, \
2726                                                 A.MSG_DIRECTION, B.DISPLAY_TIME, B.MSG_TEXT \
2727                                                 FROM %s A, %s B \
2728                                         WHERE A.ADDRESS_ID = B.ADDRESS_ID AND A.ADDRESS_ID <> 0 \
2729                                                 AND B.FOLDER_ID > 0 AND B.FOLDER_ID < %d \
2730                                                 AND ( B.MSG_TEXT LIKE '%%%s%%' ESCAPE '\\' \
2731                                                 OR B.SUBJECT LIKE '%%%s%%' ESCAPE '\\' \
2732                                                 OR A.ADDRESS_VAL LIKE '%%%s%%' ESCAPE '\\' \
2733                                                 OR A.DISPLAY_NAME LIKE '%%%s%%' ESCAPE '\\' \
2734                                                 OR A.FIRST_NAME LIKE '%%%s%%' ESCAPE '\\' \
2735                                                 OR A.LAST_NAME LIKE '%%%s%%' ESCAPE '\\') \
2736                                         ORDER BY B.DISPLAY_TIME DESC;",
2737                                                 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MSG_CBMSGBOX_ID,
2738                                                 ext2_str, ext2_str, ext2_str, ext2_str, ext2_str, ext2_str);
2739
2740
2741         if (ext1_str) {
2742                 free(ext1_str);
2743                 ext1_str = NULL;
2744         }
2745
2746         if (ext2_str) {
2747                 free(ext2_str);
2748                 ext2_str = NULL;
2749         }
2750
2751         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
2752                 return MSG_ERR_DB_PREPARE;
2753
2754         while (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
2755                 memset(&threadView, 0x00, sizeof(threadView));
2756
2757                 threadView.threadId = dbHandle.columnInt(0);
2758                 threadView.contactId = dbHandle.columnInt(1);
2759
2760                 memset(threadView.threadAddr, 0x00, sizeof(threadView.threadAddr));
2761                 strncpy(threadView.threadAddr, (char *)dbHandle.columnText(2), MAX_THREAD_ADDR_LEN);
2762
2763                 memset(displayName, 0x00, sizeof(displayName));
2764                 strncpy(displayName, (char *)dbHandle.columnText(3), MAX_DISPLAY_NAME_LEN);
2765
2766                 memset(firstName, 0x00, sizeof(firstName));
2767                 strncpy(firstName, (char *)dbHandle.columnText(4), MAX_DISPLAY_NAME_LEN);
2768
2769                 memset(lastName, 0x00, sizeof(lastName));
2770                 strncpy(lastName, (char *)dbHandle.columnText(5), MAX_DISPLAY_NAME_LEN);
2771
2772                 if (strlen(displayName) <= 0) {
2773                         if (order == 0) {
2774                                 if (firstName[0] != '\0') {
2775                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
2776                                 }
2777
2778                                 if (lastName[0] != '\0') {
2779                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
2780                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
2781                                 }
2782                         } else if (order == 1) {
2783                                 if (lastName[0] != '\0') {
2784                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
2785                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
2786                                 }
2787
2788                                 if (firstName[0] != '\0') {
2789                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
2790                                 }
2791                         }
2792                 }
2793
2794                 strncpy(threadView.threadName, displayName, MAX_THREAD_NAME_LEN);
2795
2796                 strncpy(threadView.threadImagePath, (char *)dbHandle.columnText(6), MAX_IMAGE_PATH_LEN);
2797
2798                 threadView.unreadCnt = dbHandle.columnInt(7);
2799                 threadView.smsCnt = dbHandle.columnInt(8);
2800                 threadView.mmsCnt = dbHandle.columnInt(9);
2801
2802                 threadView.direction = dbHandle.columnInt(10);
2803                 threadView.threadTime = (time_t)dbHandle.columnInt(11);
2804
2805                 strncpy(threadView.threadData, (char *)dbHandle.columnText(12), MAX_THREAD_DATA_LEN);
2806
2807                 tr1::unordered_set<MSG_THREAD_ID_T>::iterator it;
2808
2809                 it = IdList.find(threadView.threadId);
2810
2811                 if (it == IdList.end()) {
2812                         IdList.insert(threadView.threadId);
2813                         searchList.push(threadView);
2814                 }
2815
2816         }
2817
2818         dbHandle.finalizeQuery();
2819
2820         // Add data to Out Parameter
2821         pThreadViewList->nCount = searchList.size();
2822         pThreadViewList->msgThreadInfo = (msg_thread_view_t *)new char[sizeof(MSG_THREAD_VIEW_S *) * searchList.size()];
2823
2824         MSG_THREAD_VIEW_S *pTmp = NULL;
2825
2826         int index = 0;
2827
2828         while (!searchList.empty()) {
2829                 pThreadViewList->msgThreadInfo[index] = (msg_thread_view_t)new char[sizeof(MSG_THREAD_VIEW_S)];
2830
2831                 pTmp = (MSG_THREAD_VIEW_S *)pThreadViewList->msgThreadInfo[index];
2832
2833                 memcpy(pTmp, &(searchList.front()), sizeof(MSG_THREAD_VIEW_S));
2834
2835                 searchList.pop();
2836
2837                 index++;
2838         }
2839
2840         return MSG_SUCCESS;
2841 }
2842
2843
2844 MSG_ERROR_T MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int offset, int limit, MSG_LIST_S *pMsgList)
2845 {
2846         // Clear Out Parameter
2847         pMsgList->nCount = 0;
2848         pMsgList->msgInfo = NULL;
2849
2850         int rowCnt = 0;
2851         int index = 27; // numbers of index
2852
2853         unsigned int addrId = 0;
2854
2855         char sqlQuery[MAX_QUERY_LEN+1];
2856         char sqlQuerySubset[(MAX_QUERY_LEN/5)+1];
2857
2858         char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
2859         char displayName[MAX_DISPLAY_NAME_LEN+1];
2860
2861         char *ext1_str = NULL;
2862         char *ext2_str = NULL;
2863
2864         // Get Name Order
2865         int order = MsgGetContactNameOrder();
2866
2867         memset(sqlQuery, 0x00, sizeof(sqlQuery));
2868
2869         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MSG_ID, A.ADDRESS_ID, A.FOLDER_ID, A.REFERENCE_ID, A.STORAGE_ID, A.MAIN_TYPE, A.SUB_TYPE, \
2870                                                         A.DISPLAY_TIME, A.DATA_SIZE, A.NETWORK_STATUS, A.READ_STATUS, A.PROTECTED, A.BACKUP, A.PRIORITY, \
2871                                                         A.MSG_DIRECTION, A.SCHEDULED_TIME, A.SUBJECT, A.MSG_TEXT, B.ADDRESS_TYPE, B.RECIPIENT_TYPE, \
2872                                                         B.CONTACT_ID, B.ADDRESS_VAL, B.DISPLAY_NAME, B.FIRST_NAME, B.LAST_NAME, A.ATTACHMENT_COUNT, A.THUMB_PATH \
2873                                                         FROM %s A, %s B \
2874                                                         WHERE A.ADDRESS_ID = B.ADDRESS_ID AND B.ADDRESS_ID <> 0 ",
2875                                                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME);
2876
2877         //// folder
2878         memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2879
2880         if (pSearchCon->folderId == MSG_ALLBOX_ID)
2881                 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID > 0 AND A.FOLDER_ID < %d ", MSG_CBMSGBOX_ID);
2882         else if (pSearchCon->folderId == MSG_IOSBOX_ID)
2883                 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID > 0 AND A.FOLDER_ID < %d ", MSG_DRAFT_ID);
2884         else
2885                 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID = %d ", pSearchCon->folderId);
2886
2887         strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2888
2889
2890         //// msg type
2891         memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2892
2893         switch (pSearchCon->msgType) {
2894                 case MSG_TYPE_SMS:
2895                         snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_NORMAL_SMS);
2896                         break;
2897
2898                 case MSG_TYPE_MMS:
2899                         snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d ", MSG_MMS_TYPE);
2900                         break;
2901
2902                 case MSG_TYPE_MMS_JAVA:
2903                         snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_MMS_TYPE, MSG_SENDREQ_JAVA_MMS);
2904                         break;
2905
2906                 case MSG_TYPE_SMS_SYNCML:
2907                         snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_SYNCML_CP);
2908                         break;
2909
2910                 case MSG_TYPE_SMS_REJECT:
2911                         snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_REJECT_SMS);
2912                         break;
2913
2914                 default:
2915                         MSG_DEBUG("msg type is not set.");
2916         }
2917
2918         strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2919
2920         /// string
2921         if (pSearchCon->pSearchVal != NULL) {
2922
2923                 // Replace string for '%' and '_' character
2924                 ext1_str = MsgStoReplaceString(pSearchCon->pSearchVal, "_", "\\_");
2925                 ext2_str = MsgStoReplaceString(ext1_str, "%", "\\%");
2926
2927                 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2928                 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND ( A.MSG_TEXT LIKE '%%%s%%' ESCAPE '\\' \
2929                                                                 OR A.SUBJECT LIKE '%%%s%%' ESCAPE '\\' \
2930                                                                 OR B.ADDRESS_VAL LIKE '%%%s%%' ESCAPE '\\' \
2931                                                                 OR B.DISPLAY_NAME LIKE '%%%s%%' ESCAPE '\\' \
2932                                                                 OR B.FIRST_NAME LIKE '%%%s%%' ESCAPE '\\' \
2933                                                                 OR B.LAST_NAME LIKE '%%%s%%' ESCAPE '\\') ",
2934                                                                 ext2_str, ext2_str, ext2_str, ext2_str, ext2_str, ext2_str);
2935                 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2936
2937                 if (ext1_str) {
2938                         free(ext1_str);
2939                         ext1_str = NULL;
2940                 }
2941
2942                 if (ext2_str) {
2943                         free(ext2_str);
2944                         ext2_str = NULL;
2945                 }
2946         }
2947
2948         /// address
2949         if (pSearchCon->pAddressVal != NULL) {
2950
2951                 // Replace string for '%' and '_' character
2952                 ext1_str = MsgStoReplaceString(pSearchCon->pAddressVal, "_", "\\_");
2953                 ext2_str = MsgStoReplaceString(ext1_str, "%", "\\%");
2954
2955                 memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2956                 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND B.ADDRESS_VAL LIKE '%%%s%%' ESCAPE '\\'", ext2_str);
2957                 strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2958
2959                 if (ext1_str) {
2960                         free(ext1_str);
2961                         ext1_str = NULL;
2962                 }
2963
2964                 if (ext2_str) {
2965                         free(ext2_str);
2966                         ext2_str = NULL;
2967                 }
2968         }
2969
2970         /// limit, offset
2971         memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
2972
2973         if (offset >= 0 && limit > 0)
2974                 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "ORDER BY A.DISPLAY_TIME DESC LIMIT %d OFFSET %d;", limit, offset);
2975         else
2976                 snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "ORDER BY A.DISPLAY_TIME DESC;");
2977
2978         strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
2979
2980         MSG_ERROR_T err = dbHandle.getTable(sqlQuery, &rowCnt);
2981
2982         if (err == MSG_ERR_DB_NORECORD) {
2983                 dbHandle.freeTable();
2984
2985                 return MSG_SUCCESS;
2986         } else if (err != MSG_SUCCESS) {
2987                 MSG_DEBUG("%s", sqlQuery);
2988
2989                 dbHandle.freeTable();
2990
2991                 return err;
2992         }
2993
2994         pMsgList->nCount = rowCnt;
2995
2996         MSG_DEBUG("pMsgList->nCount [%d]", pMsgList->nCount);
2997
2998         pMsgList->msgInfo = (msg_message_t *)new char[sizeof(MSG_MESSAGE_S *) * rowCnt];
2999
3000         MSG_MESSAGE_S *pTmp = NULL;
3001
3002         for (int i = 0; i < rowCnt; i++) {
3003                 pMsgList->msgInfo[i] = (msg_message_t)new char[sizeof(MSG_MESSAGE_S)];
3004
3005                 pTmp = (MSG_MESSAGE_S *)pMsgList->msgInfo[i];
3006
3007                 memset(pTmp, 0x00, sizeof(pTmp));
3008
3009                 pTmp->pData = NULL;
3010                 pTmp->pMmsData = NULL;
3011
3012                 pTmp->msgId = dbHandle.getColumnToInt(index++);
3013                 addrId = dbHandle.getColumnToInt(index++);
3014                 pTmp->folderId = dbHandle.getColumnToInt(index++);
3015                 pTmp->referenceId = dbHandle.getColumnToInt(index++);
3016                 pTmp->storageId = dbHandle.getColumnToInt(index++);
3017                 pTmp->msgType.mainType = dbHandle.getColumnToInt(index++);
3018                 pTmp->msgType.subType = dbHandle.getColumnToInt(index++);
3019                 pTmp->displayTime = (time_t)dbHandle.getColumnToInt(index++);
3020                 pTmp->dataSize = dbHandle.getColumnToInt(index++);
3021                 pTmp->networkStatus = dbHandle.getColumnToInt(index++);
3022                 pTmp->bRead = dbHandle.getColumnToInt(index++);
3023                 pTmp->bProtected = dbHandle.getColumnToInt(index++);
3024                 pTmp->bBackup = dbHandle.getColumnToInt(index++);
3025                 pTmp->priority = dbHandle.getColumnToInt(index++);
3026                 pTmp->direction= dbHandle.getColumnToInt(index++);
3027                 pTmp->scheduledTime = (time_t)dbHandle.getColumnToInt(index++);
3028
3029                 dbHandle.getColumnToString(index++, MAX_SUBJECT_LEN, pTmp->subject);
3030
3031                 if (pTmp->msgType.mainType == MSG_MMS_TYPE &&
3032                         (pTmp->networkStatus == MSG_NETWORK_RETRIEVING || pTmp->networkStatus == MSG_NETWORK_RETRIEVE_FAIL || pTmp->msgType.subType == MSG_NOTIFICATIONIND_MMS)) {
3033                         pTmp->pData = NULL;
3034                         index++;
3035                 } else {
3036                         MSG_DEBUG("pTmp->dataSize [%d]", pTmp->dataSize);
3037                         pTmp->pData = (void *)new char[pTmp->dataSize + 2];
3038                         memset(pTmp->pData, 0x00, sizeof(pTmp->pData));
3039
3040                         dbHandle.getColumnToString(index++, pTmp->dataSize+1, (char *)pTmp->pData);
3041                 }
3042
3043                 if (addrId > 0)
3044                         pTmp->nAddressCnt = 1;
3045                 else
3046                         pTmp->nAddressCnt = 0;
3047
3048                 pTmp->addressList[0].threadId = addrId;
3049                 pTmp->addressList[0].addressType = dbHandle.getColumnToInt(index++);
3050                 pTmp->addressList[0].recipientType = dbHandle.getColumnToInt(index++);
3051                 pTmp->addressList[0].contactId = dbHandle.getColumnToInt(index++);
3052
3053                 dbHandle.getColumnToString(index++, MAX_ADDRESS_VAL_LEN, pTmp->addressList[0].addressVal);
3054
3055                 memset(displayName, 0x00, sizeof(displayName));
3056                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, displayName);
3057
3058                 memset(firstName, 0x00, sizeof(firstName));
3059                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, firstName);
3060
3061                 memset(lastName, 0x00, sizeof(lastName));
3062                 dbHandle.getColumnToString(index++, MAX_DISPLAY_NAME_LEN, lastName);
3063
3064                 if (strlen(displayName) <= 0) {
3065                         if (order == 0) {
3066                                 if (firstName[0] != '\0') {
3067                                         strncpy(displayName, firstName, MAX_DISPLAY_NAME_LEN);
3068                                 }
3069
3070                                 if (lastName[0] != '\0') {
3071                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
3072                                         strncat(displayName, lastName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
3073                                 }
3074                         } else if (order == 1) {
3075                                 if (lastName[0] != '\0') {
3076                                         strncpy(displayName, lastName, MAX_DISPLAY_NAME_LEN);
3077                                         strncat(displayName, " ", MAX_DISPLAY_NAME_LEN-strlen(displayName));
3078                                 }
3079
3080                                 if (firstName[0] != '\0') {
3081                                         strncat(displayName, firstName, MAX_DISPLAY_NAME_LEN-strlen(displayName));
3082                                 }
3083                         }
3084                 }
3085
3086                 strncpy(pTmp->addressList[0].displayName, displayName, MAX_DISPLAY_NAME_LEN);
3087
3088                 pTmp->attachCount = dbHandle.getColumnToInt(index++);
3089
3090                 dbHandle.getColumnToString(index++, MSG_FILEPATH_LEN_MAX, pTmp->thumbPath);
3091
3092         }
3093         dbHandle.freeTable();
3094
3095         return MSG_SUCCESS;
3096 }
3097
3098
3099
3100 MSG_ERROR_T MsgStoGetMsgIdList(MSG_REFERENCE_ID_T RefId, MSG_MSGID_LIST_S *pMsgIdList)
3101 {
3102         // Clear Out Parameter
3103         pMsgIdList->nCount = 0;
3104         pMsgIdList->msgIdList = NULL;
3105
3106         int rowCnt = 0;
3107         int index = 1; // numbers of index
3108
3109         char sqlQuery[MAX_QUERY_LEN+1];
3110
3111         // Search - Address, Name
3112         memset(sqlQuery, 0x00, sizeof(sqlQuery));
3113
3114         snprintf(sqlQuery, sizeof(sqlQuery),
3115                 "SELECT MSG_ID \
3116                                         FROM %s \
3117                                      WHERE REFERENCE_ID = %d \
3118                                       ORDER BY MSG_ID ASC;",
3119                                       MSGFW_MESSAGE_TABLE_NAME, RefId);
3120
3121         MSG_ERROR_T err = dbHandle.getTable(sqlQuery, &rowCnt);
3122
3123         if (err != MSG_SUCCESS) {
3124                 MSG_DEBUG("%s", sqlQuery);
3125
3126                 dbHandle.freeTable();
3127
3128                 return err;
3129         }
3130
3131         pMsgIdList->nCount = rowCnt;
3132
3133         MSG_DEBUG("pMsgIdList->nCount [%d]", pMsgIdList->nCount);
3134
3135         pMsgIdList->msgIdList = (MSG_MESSAGE_ID_T *)new char[sizeof(MSG_MESSAGE_ID_T *) * rowCnt];
3136
3137         for (int i = 0; i < rowCnt; i++) {
3138                 pMsgIdList->msgIdList[i] = (MSG_MESSAGE_ID_T)new char[sizeof(MSG_MESSAGE_ID_T)];
3139
3140                 pMsgIdList->msgIdList[i] = dbHandle.getColumnToInt(index++);
3141         }
3142
3143         dbHandle.freeTable();
3144
3145         return MSG_SUCCESS;
3146 }
3147
3148
3149 void MsgConvertNumber(const char *pSrcNum, char *pDestNum)
3150 {
3151         int overLen = 0;
3152         int i = 0;
3153
3154         overLen = strlen(pSrcNum) - MAX_PRECONFIG_NUM;
3155
3156         for (i = 0; i < MAX_PRECONFIG_NUM; i++)
3157                 pDestNum[i] = pSrcNum[i+overLen];
3158
3159         pDestNum[i] = '\0';
3160 }
3161
3162
3163 MSG_ERROR_T MsgStoGetRejectMsgList(const char *pNumber, MSG_REJECT_MSG_LIST_S *pRejectMsgList)
3164 {
3165         // Clear Out Parameter
3166         pRejectMsgList->nCount = 0;
3167         pRejectMsgList->rejectMsgInfo = NULL;
3168
3169         int rowCnt = 0;
3170         int index = 3; // numbers of index
3171
3172         char sqlQuery[MAX_QUERY_LEN+1];
3173
3174         // Search Reject Msg
3175         memset(sqlQuery, 0x00, sizeof(sqlQuery));
3176
3177         if (pNumber != NULL) {
3178                 char phoneNumber[MAX_PRECONFIG_NUM+1];
3179                 memset(phoneNumber, 0x00, sizeof(phoneNumber));
3180
3181                 if (strlen(pNumber) > MAX_PRECONFIG_NUM) {
3182                         MsgConvertNumber(pNumber, phoneNumber);
3183                 } else {
3184                         strncpy(phoneNumber, pNumber, MAX_PRECONFIG_NUM);
3185                 }
3186
3187                 snprintf(sqlQuery, sizeof(sqlQuery),
3188                         "SELECT B.MSG_ID, B.MSG_TEXT, B.DISPLAY_TIME \
3189                                                 FROM %s A, %s B \
3190                                              WHERE A.ADDRESS_ID = B.ADDRESS_ID \
3191                                                   AND B.MAIN_TYPE = %d \
3192                                                   AND B.SUB_TYPE = %d \
3193                                                   AND A.ADDRESS_VAL LIKE '%%%s' \
3194                                               ORDER BY B.DISPLAY_TIME DESC;",
3195                                               MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME,
3196                                               MSG_SMS_TYPE, MSG_REJECT_SMS, phoneNumber);
3197         } else {
3198                 snprintf(sqlQuery, sizeof(sqlQuery),
3199                         "SELECT B.MSG_ID, B.MSG_TEXT, B.DISPLAY_TIME \
3200                                                 FROM %s A, %s B \
3201                                              WHERE A.ADDRESS_ID = B.ADDRESS_ID \
3202                                                   AND B.MAIN_TYPE = %d \
3203                                                   AND B.SUB_TYPE = %d \
3204                                               ORDER BY B.DISPLAY_TIME DESC;",
3205                                               MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME,
3206                                               MSG_SMS_TYPE, MSG_REJECT_SMS);
3207         }
3208
3209         MSG_ERROR_T  err = dbHandle.getTable(sqlQuery, &rowCnt);
3210
3211         if (err != MSG_SUCCESS) {
3212                 MSG_DEBUG("%s", sqlQuery);
3213
3214                 dbHandle.freeTable();
3215
3216                 return err;
3217         }
3218
3219         pRejectMsgList->nCount = rowCnt;
3220
3221         MSG_DEBUG("pRejectMsgList->nCount [%d]", pRejectMsgList->nCount);
3222
3223         pRejectMsgList->rejectMsgInfo = (MSG_REJECT_MSG_INFO_S *)new char[sizeof(MSG_REJECT_MSG_INFO_S)*rowCnt];
3224
3225         MSG_REJECT_MSG_INFO_S *pTmp = pRejectMsgList->rejectMsgInfo;
3226
3227         for (int i = 0; i < rowCnt; i++) {
3228                 pTmp->msgId = dbHandle.getColumnToInt(index++);
3229
3230                 memset(pTmp->msgText, 0x00, sizeof(pTmp->msgText));
3231                 dbHandle.getColumnToString(index++, MAX_MSG_TEXT_LEN, pTmp->msgText);
3232
3233                 pTmp->displayTime = (time_t)dbHandle.getColumnToInt(index++);
3234
3235                 pTmp++;
3236         }
3237
3238         dbHandle.freeTable();
3239
3240         return MSG_SUCCESS;
3241 }
3242
3243
3244 MSG_ERROR_T MsgStoGetSyncMLExtId(MSG_MESSAGE_ID_T msgId, int *extId)
3245 {
3246         char sqlQuery[MAX_QUERY_LEN+1];
3247
3248         memset(sqlQuery, 0x00, sizeof(sqlQuery));
3249
3250         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXT_ID FROM %s WHERE MSG_ID = %d;",
3251                                 MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
3252
3253         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
3254                 return MSG_ERR_DB_PREPARE;
3255
3256         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
3257                 *extId = dbHandle.columnInt(0);
3258         } else {
3259                 dbHandle.finalizeQuery();
3260                 return MSG_ERR_DB_STEP;
3261         }
3262
3263         dbHandle.finalizeQuery();
3264
3265         return MSG_SUCCESS;
3266 }
3267
3268
3269 MSG_ERROR_T MsgStoGetReportStatus(MSG_MESSAGE_ID_T msgId, MSG_REPORT_STATUS_INFO_S *pReportStatus)
3270 {
3271         char sqlQuery[MAX_QUERY_LEN+1];
3272
3273         memset(sqlQuery, 0x00, sizeof(sqlQuery));
3274
3275         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DELIVERY_REPORT_STATUS, DELIVERY_REPORT_TIME, READ_REPORT_STATUS, READ_REPORT_TIME \
3276                                                                         FROM %s WHERE MSG_ID = %d;",
3277                                                         MSGFW_MESSAGE_TABLE_NAME, msgId);
3278
3279         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
3280                 return MSG_ERR_DB_PREPARE;
3281
3282         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
3283                 pReportStatus->deliveryStatus = (MSG_DELIVERY_REPORT_STATUS_T)dbHandle.columnInt(0);
3284                 pReportStatus->deliveryStatusTime = (time_t)dbHandle.columnInt(1);
3285                 pReportStatus->readStatus = (MSG_READ_REPORT_STATUS_T)dbHandle.columnInt(2);
3286                 pReportStatus->readStatusTime = (time_t)dbHandle.columnInt(3);
3287         }
3288
3289         dbHandle.finalizeQuery();
3290
3291         return MSG_SUCCESS;
3292 }
3293
3294
3295 MSG_ERROR_T MsgStoGetThreadUnreadCnt(MSG_THREAD_ID_T ThreadId, int *cnt)
3296 {
3297         MSG_BEGIN();
3298
3299         int msgCnt = 0;
3300         *cnt = 0;
3301
3302         char sqlQuery[MAX_QUERY_LEN+1];
3303
3304         memset(sqlQuery, 0x00, sizeof(sqlQuery));
3305
3306         // Get MSG_ID
3307         memset(sqlQuery, 0x00, sizeof(sqlQuery));
3308         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s A \
3309                                      WHERE ADDRESS_ID = %d AND READ_STATUS = 0;",
3310                                 MSGFW_MESSAGE_TABLE_NAME, ThreadId);
3311
3312         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
3313                 return MSG_ERR_DB_PREPARE;
3314
3315         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
3316                 msgCnt = dbHandle.columnInt(0);
3317         }
3318
3319         dbHandle.finalizeQuery();
3320
3321         *cnt = msgCnt;
3322
3323         MSG_END();
3324
3325         return MSG_SUCCESS;
3326 }
3327