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