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