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