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