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