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