[MPR-868] Add duplication logic for CB Messages
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginStorage.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 <errno.h>
18
19 #include "MsgDebug.h"
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22 #include "MsgContact.h"
23 #include "MsgUtilFile.h"
24 #include "MsgUtilStorage.h"
25 #include "MsgUtilFunction.h"
26 #include "MsgGconfWrapper.h"
27 #include "MsgNotificationWrapper.h"
28 #include "SmsPluginMain.h"
29 #include "SmsPluginSimMsg.h"
30 #include "SmsPluginEventHandler.h"
31 #include "SmsPluginStorage.h"
32
33
34 /*==================================================================================================
35                                      IMPLEMENTATION OF SmsPluginStorage - Member Functions
36 ==================================================================================================*/
37 SmsPluginStorage* SmsPluginStorage::pInstance = NULL;
38
39
40 SmsPluginStorage::SmsPluginStorage()
41 {
42         memset(&msgInfo, 0x00, sizeof(msgInfo));
43         memset(&addrInfo, 0x00, sizeof(addrInfo));
44 }
45
46
47 SmsPluginStorage::~SmsPluginStorage()
48 {
49 }
50
51
52 SmsPluginStorage* SmsPluginStorage::instance()
53 {
54         if (!pInstance) {
55                 MSG_DEBUG("pInstance is NULL. Now creating instance.");
56                 pInstance = new SmsPluginStorage();
57         }
58
59         return pInstance;
60 }
61
62
63 msg_error_t SmsPluginStorage::insertMsgRef(MSG_MESSAGE_INFO_S *pMsg, unsigned char msgRef, int index)
64 {
65         MSG_BEGIN();
66
67         time_t curTime = time(NULL);
68
69         MsgDbHandler *dbHandle = getDbHandle();
70
71         char sqlQuery[MAX_QUERY_LEN+1];
72         char *normalNum = NULL;
73
74         memset(sqlQuery, 0x00, sizeof(sqlQuery));
75
76         normalNum = msg_normalize_number(pMsg->addressList[index].addressVal);
77
78         MSG_SEC_DEBUG("Insert MsgID=[%d], Address=[%s], MsgRef=[%d], Time=[%d]", \
79                         pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
80
81         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %s, %d, 0, -1, %d);",
82                         MSGFW_SMS_REPORT_TABLE_NAME, pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
83
84         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
85                 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
86                 return MSG_ERR_DB_EXEC;
87         }
88
89         MSG_END();
90
91         return MSG_SUCCESS;
92 }
93
94
95 msg_error_t SmsPluginStorage::updateMsgDeliverStatus(MSG_MESSAGE_INFO_S *pMsgInfo, unsigned char msgRef)
96 {
97         MSG_BEGIN();
98
99         MsgDbHandler *dbHandle = getDbHandle();
100
101         char sqlQuery[MAX_QUERY_LEN+1];
102
103         msg_message_id_t msgId = 0;
104         int rowCnt = 0;
105         char *normalNum = NULL;
106
107         normalNum = msg_normalize_number(pMsgInfo->addressList[0].addressVal);
108
109         memset(sqlQuery, 0x00, sizeof(sqlQuery));
110         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE ADDRESS_VAL = %s AND MSG_REF > 0 ORDER BY TIME ASC;",
111                         MSGFW_SMS_REPORT_TABLE_NAME, normalNum);
112         MSG_DEBUG("[SQL Query] %s", sqlQuery);
113
114         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
115                 return MSG_ERR_DB_PREPARE;
116
117         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
118                 msgId = dbHandle->columnInt(0);
119
120         dbHandle->finalizeQuery();
121
122         pMsgInfo->msgId = msgId;
123
124         /** Update Status - MSG_MESSAGE_TABLE */
125         memset(sqlQuery, 0x00, sizeof(sqlQuery));
126         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE MSG_ID = %d AND MSG_REF > 0;",
127                         MSGFW_SMS_REPORT_TABLE_NAME, msgId);
128
129         if (dbHandle->getTable(sqlQuery, &rowCnt, NULL) != MSG_SUCCESS) {
130                 dbHandle->freeTable();
131                 return MSG_ERR_DB_GETTABLE;
132         }
133
134         dbHandle->freeTable();
135
136         MSG_DEBUG("Selected row count = [%d]", rowCnt);
137
138         if (rowCnt == 1 && pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
139                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
140                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
141                                 MSGFW_MESSAGE_TABLE_NAME, (int)pMsgInfo->networkStatus, msgId);
142                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
143
144                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
145                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
146                         return MSG_ERR_DB_EXEC;
147                 }
148         }
149
150         /** Update Status - MSG_REPORT_TABLE */
151         if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
152                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
153                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
154                                 MSGFW_SMS_REPORT_TABLE_NAME, 1, (int)pMsgInfo->displayTime, msgId, normalNum);
155                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
156
157                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
158                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
159                         return MSG_ERR_DB_EXEC;
160                 }
161         } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_EXPIRED) {
162                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
163                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
164                                 MSGFW_SMS_REPORT_TABLE_NAME, 0, (int)pMsgInfo->displayTime, msgId, normalNum);
165                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
166
167                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
168                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
169                         return MSG_ERR_DB_EXEC;
170                 }
171         } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_PENDING) {
172                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
173                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
174                                 MSGFW_SMS_REPORT_TABLE_NAME, 3, (int)pMsgInfo->displayTime, msgId, normalNum);
175                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
176
177                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
178                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
179                         return MSG_ERR_DB_EXEC;
180                 }
181         } else if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_FAIL) {
182                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
183                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
184                                 MSGFW_SMS_REPORT_TABLE_NAME, 8, (int)pMsgInfo->displayTime, msgId, normalNum);
185                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
186
187                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
188                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
189                         return MSG_ERR_DB_EXEC;
190                 }
191         }
192
193         MSG_END();
194
195         return MSG_SUCCESS;
196 }
197
198
199 msg_error_t SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
200 {
201         MSG_BEGIN();
202
203         if (!pMsgInfo || (pMsgInfo && pMsgInfo->msgId <= 0)) {
204                 MSG_DEBUG("Invalid message id");
205                 return MSG_ERR_INVALID_MESSAGE_ID;
206         }
207
208 /***  Comment below line to not save the time value after sent status (it could be used later.)
209         time_t curTime = time(NULL);
210 ***/
211         MsgDbHandler *dbHandle = getDbHandle();
212
213         char sqlQuery[MAX_QUERY_LEN+1];
214
215         memset(sqlQuery, 0x00, sizeof(sqlQuery));
216
217         MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, status);
218
219         /** Move Msg to SENTBOX */
220         if (status == MSG_NETWORK_SEND_SUCCESS) {
221                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
222                                         MSGFW_MESSAGE_TABLE_NAME, status, MSG_SENTBOX_ID, pMsgInfo->msgId);
223         } else {
224                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, READ_STATUS = 0 WHERE MSG_ID = %d;",
225                                         MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
226         }
227
228         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
229                 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
230                 return MSG_ERR_DB_EXEC;
231         }
232
233 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
234         if (status == MSG_NETWORK_SEND_SUCCESS) {
235                 MSG_DEBUG("MsgAddPhoneLog() : folderId [%d]", pMsgInfo->folderId);
236                 MsgAddPhoneLog(pMsgInfo);
237         }
238 #endif /* MSG_CONTACTS_SERVICE_NOT_SUPPORTED */
239         MSG_END();
240
241         return MSG_SUCCESS;
242 }
243
244 #ifdef SMS_REPORT_OPERATION
245 msg_error_t SmsPluginStorage::updateMsgRef(msg_message_id_t MsgId, unsigned char MsgRef)
246 {
247         MSG_BEGIN();
248
249         MsgDbHandler *dbHandle = getDbHandle();
250
251         char sqlQuery[MAX_QUERY_LEN+1];
252
253         memset(sqlQuery, 0x00, sizeof(sqlQuery));
254
255         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = %d WHERE MSG_ID = %d;",
256                                 MSGFW_REPORT_TABLE_NAME, (int)MsgRef, MsgId);
257
258         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
259                 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
260                 return MSG_ERR_DB_EXEC;
261         }
262
263         /** Set Message Reference for updating report table */
264         tmpMsgRef = MsgRef;
265
266         MSG_DEBUG("MsgRef : %d", MsgRef);
267
268         MSG_END();
269
270         return MSG_SUCCESS;
271 }
272
273
274 msg_error_t SmsPluginStorage::updateStatusReport(unsigned char MsgRef, msg_delivery_report_status_t Status, time_t DeliveryTime)
275 {
276         MSG_BEGIN();
277
278         MSG_DEBUG("tmpMsgRef : %d", tmpMsgRef);
279
280         MsgDbHandler *dbHandle = getDbHandle();
281
282         char sqlQuery[MAX_QUERY_LEN+1];
283
284         /** Get Msg Id for Quickpanel Noti */
285         msg_message_id_t msgId = 0;
286
287         memset(sqlQuery, 0x00, sizeof(sqlQuery));
288         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MSG_REF = %d;",
289                                         MSGFW_REPORT_TABLE_NAME, (int)tmpMsgRef);
290
291         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
292                 return MSG_ERR_DB_PREPARE;
293
294         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
295                 msgId = dbHandle->columnInt(0);
296
297         dbHandle->finalizeQuery();
298
299         /** Update Status */
300         memset(sqlQuery, 0x00, sizeof(sqlQuery));
301         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, DELIVERY_REPORT_STATUS = %d, DELIVERY_REPORT_TIME = %lu WHERE MSG_REF = %d;",
302                                         MSGFW_REPORT_TABLE_NAME, Status, DeliveryTime, (int)tmpMsgRef);
303
304         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
305                 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
306                 return MSG_ERR_DB_EXEC;
307         }
308
309         /** Insert Quickpanel Noti */
310         msg_error_t ret = MSG_SUCCESS;
311
312         ret = MsgInsertSmsNotiToQuickpanel(dbHandle, msgId, Status);
313
314         if (ret != MSG_SUCCESS) {
315                 MSG_DEBUG("MsgInsertSmsNotiToQuickpanel() Failed : [%d]", ret);
316                 return ret;
317         }
318
319         MSG_END();
320
321         return MSG_SUCCESS;
322 }
323 #endif
324
325 msg_error_t SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo, int *simIdList)
326 {
327         msg_error_t err = MSG_SUCCESS;
328
329         unsigned int simId = 0;
330         MsgDbHandler *dbHandle = getDbHandle();
331
332         char sqlQuery[MAX_QUERY_LEN+1];
333
334         if (simIdList) {
335                 MSG_DEBUG("simIdList exist.");
336                 for (int i = 0; i < MAX_SIM_SMS_NUM; ++i) {
337                         if (simIdList[i]) {
338                                 simId = simIdList[i] - 1;
339                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
340                                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
341                                                 MSGFW_SIM_MSG_TABLE_NAME, simId, pSimMsgInfo->msgId);
342
343                                 MSG_DEBUG("QUERY : %s", sqlQuery);
344
345                                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
346                                         return MSG_ERR_DB_EXEC;
347                                 }
348                         } else {
349                                 break;
350                         }
351                 }
352         }
353
354         return err;
355 }
356
357
358 msg_error_t SmsPluginStorage::insertSimMessage(int simId, int msgId)
359 {
360         MSG_BEGIN();
361
362         MsgDbHandler *dbHandle = getDbHandle();
363
364         char sqlQuery[MAX_QUERY_LEN+1];
365
366         dbHandle->beginTrans();
367
368         /** Insert Message into msg_sim table */
369         memset(sqlQuery, 0x00, sizeof(sqlQuery));
370         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);", MSGFW_SIM_MSG_TABLE_NAME, simId, msgId);
371         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
372                 dbHandle->endTrans(false);
373                 return MSG_ERR_DB_EXEC;
374         }
375
376         dbHandle->endTrans(true);
377
378         return MSG_SUCCESS;
379 }
380
381
382 msg_error_t SmsPluginStorage::deleteSimMessage(int sim_idx, int simId)
383 {
384         MSG_BEGIN();
385
386         MsgDbHandler *dbHandle = getDbHandle();
387
388         char sqlQuery[MAX_QUERY_LEN+1];
389
390         dbHandle->beginTrans();
391
392         /** Delete Message from msg_sim table */
393         memset(sqlQuery, 0x00, sizeof(sqlQuery));
394         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE SIM_ID = %d AND SIM_SLOT_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, simId, sim_idx);
395         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
396                 dbHandle->endTrans(false);
397                 return MSG_ERR_DB_EXEC;
398         }
399
400         dbHandle->endTrans(true);
401
402         return MSG_SUCCESS;
403 }
404
405
406 msg_error_t SmsPluginStorage::checkMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
407 {
408         MSG_BEGIN();
409
410         msg_error_t err = MSG_SUCCESS;
411
412         /**  Check whether storage is full or not */
413         err = checkStorageStatus(pMsgInfo);
414
415         if (err != MSG_SUCCESS) {
416                 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
417                         pMsgInfo->folderId = 0;
418                         err = MSG_SUCCESS;
419                 } else if (pMsgInfo->msgType.classType == MSG_CLASS_2 &&
420                                 (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS)) {
421                         err = addClass2Message(pMsgInfo);
422                 } else if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
423                         err = MSG_SUCCESS;
424                 }
425                 return err;
426         }
427
428         /**  Amend message information for type **/
429         if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
430                 MSG_DEBUG("Normal SMS");
431
432                 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
433                         err = addClass2Message(pMsgInfo);
434                 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
435                         /** Class 0 Msg should be saved in hidden folder */
436                         pMsgInfo->folderId = 0;
437                 }
438
439         } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
440                 MSG_DEBUG("Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
441
442                 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
443                         err = addClass2Message(pMsgInfo);
444                 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
445                         /** Class 0 Msg should be saved in hidden folder */
446                         pMsgInfo->folderId = 0;
447                         pMsgInfo->msgType.subType = MSG_NORMAL_SMS;
448                 }
449
450         } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
451                 if (pMsgInfo->bStore == true) {
452                         MSG_DEBUG("MWI Message");
453
454                         if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
455                                 err = addClass2Message(pMsgInfo);
456                         }
457                 }
458         } else {
459                 MSG_DEBUG("No matching type [%d]", pMsgInfo->msgType.subType);
460         }
461
462         if (err == MSG_SUCCESS) {
463                 MSG_DEBUG("Success to check message !!");
464         } else {
465                 MSG_DEBUG("fail to check message !! : [%d]", err);
466         }
467
468         return err;
469 }
470
471
472 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
473 {
474         msg_error_t err = MSG_SUCCESS;
475
476         unsigned int rowId = 0;
477         msg_thread_id_t convId = 0;
478
479         MsgDbHandler *dbHandle = getDbHandle();
480
481         dbHandle->beginTrans();
482
483         if (pMsgInfo->nAddressCnt > 0) {
484                 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
485
486                 if (err != MSG_SUCCESS) {
487                         dbHandle->endTrans(false);
488                         return err;
489                 }
490
491                 pMsgInfo->threadId = convId;
492         }
493
494         /**  Add Message Table */
495         rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
496
497         if (rowId <= 0) {
498                 dbHandle->endTrans(false);
499                 return MSG_ERR_DB_ROW;
500         }
501
502         /** Update conversation table */
503         err = MsgStoUpdateConversation(dbHandle, convId);
504
505         if (err != MSG_SUCCESS) {
506                 dbHandle->endTrans(false);
507                 return err;
508         }
509
510         err = dbHandle->endTrans(true);
511         if (err != MSG_SUCCESS) {
512                 return err;
513         }
514
515         pMsgInfo->msgId = (msg_message_id_t)rowId;
516
517         MSG_END();
518
519         return MSG_SUCCESS;
520 }
521
522
523 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
524 {
525         MSG_BEGIN();
526
527         MsgDbHandler *dbHandle = getDbHandle();
528
529         char sqlQuery[MAX_QUERY_LEN+1];
530
531          /**  Get SUB_TYPE, STORAGE_ID */
532         memset(sqlQuery, 0x00, sizeof(sqlQuery));
533         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, CONV_ID, SIM_INDEX \
534                         FROM %s WHERE MSG_ID = %d;",
535                         MSGFW_MESSAGE_TABLE_NAME, msgId);
536
537         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
538                 return MSG_ERR_DB_PREPARE;
539
540         MSG_MESSAGE_TYPE_S msgType;
541         msg_folder_id_t folderId;
542
543         msg_thread_id_t convId;
544         msg_sim_slot_id_t simIndex;
545
546         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
547                 msgType.mainType = dbHandle->columnInt(0);
548                 msgType.subType = dbHandle->columnInt(1);
549                 folderId = dbHandle->columnInt(2);
550                 convId = dbHandle->columnInt(3);
551                 simIndex = dbHandle->columnInt(4);
552
553                 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
554         } else {
555                 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
556                 dbHandle->finalizeQuery();
557                 return MSG_ERR_DB_STEP;
558         }
559
560         dbHandle->finalizeQuery();
561
562         dbHandle->beginTrans();
563
564         memset(sqlQuery, 0x00, sizeof(sqlQuery));
565         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
566
567         /**  Delete SMS Send Option */
568         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
569                 dbHandle->endTrans(false);
570                 return MSG_ERR_DB_EXEC;
571         }
572
573         if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
574                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
575                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
576
577                 /** Delete Push Message from push table */
578                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
579                         dbHandle->endTrans(false);
580                         return MSG_ERR_DB_EXEC;
581                 }
582         } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
583                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
584                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
585
586                 /**  Delete Push Message from push table */
587                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
588                         dbHandle->endTrans(false);
589                         return MSG_ERR_DB_EXEC;
590                 }
591         } else if (msgType.subType == MSG_SYNCML_CP) {
592                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
593                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
594
595                 /**  Delete SyncML Message from syncML table */
596                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
597                         dbHandle->endTrans(false);
598                         return MSG_ERR_DB_EXEC;
599                 }
600         }
601
602         /** Delete Message from msg table */
603         memset(sqlQuery, 0x00, sizeof(sqlQuery));
604         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
605         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
606                 dbHandle->endTrans(false);
607                 return MSG_ERR_DB_EXEC;
608         }
609
610         /** Delete Message from msg_report table */
611         memset(sqlQuery, 0x00, sizeof(sqlQuery));
612         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_REPORT_TABLE_NAME, msgId);
613         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
614                 dbHandle->endTrans(false);
615                 return MSG_ERR_DB_EXEC;
616         }
617
618         /** Delete Message from msg_sim table */
619         memset(sqlQuery, 0x00, sizeof(sqlQuery));
620         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, msgId);
621         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
622                 dbHandle->endTrans(false);
623                 return MSG_ERR_DB_EXEC;
624         }
625
626         /**  Clear Conversation table */
627         if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
628                 dbHandle->endTrans(false);
629                 return MSG_ERR_DB_EXEC;
630         }
631
632         /**  Update conversation table.*/
633         if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
634                 dbHandle->endTrans(false);
635                 return MSG_ERR_STORAGE_ERROR;
636         }
637
638         dbHandle->endTrans(true);
639
640         memset(sqlQuery, 0x00, sizeof(sqlQuery));
641         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONV_ID FROM %s WHERE CONV_ID = %d;", MSGFW_CONVERSATION_TABLE_NAME, convId);
642         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
643                 return MSG_ERR_DB_PREPARE;
644
645         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
646                 SmsPluginEventHandler::instance()->callbackThreadChange(MSG_STORAGE_CHANGE_UPDATE, convId);
647         else
648                 SmsPluginEventHandler::instance()->callbackThreadChange(MSG_STORAGE_CHANGE_DELETE, convId);
649
650         dbHandle->finalizeQuery();
651
652         signed char folder_id = (signed char)folderId;
653         if (folder_id == MSG_INBOX_ID) {
654                 msgType.classType = MSG_CLASS_NONE;
655
656                 /**  Set memory status in SIM */
657                 if (MsgStoCheckMsgCntFull(dbHandle, &msgType, folderId) == MSG_SUCCESS) {
658                         MSG_DEBUG("Set Memory Status");
659                         SmsPlgSetMemoryStatus(simIndex, MSG_SUCCESS);
660                 }
661         }
662
663         MsgRefreshAllNotification(true, false, MSG_ACTIVE_NOTI_TYPE_NONE);
664
665         return MSG_SUCCESS;
666 }
667
668
669 msg_error_t SmsPluginStorage::addClass2Message(MSG_MESSAGE_INFO_S *pMsgInfo)
670 {
671         MSG_BEGIN();
672
673         msg_error_t err = MSG_SUCCESS;
674         bool bSimSst = true;
675
676         char keyName[MAX_VCONFKEY_NAME_LEN];
677         memset(keyName, 0x00, sizeof(keyName));
678         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_SERVICE_TABLE, pMsgInfo->sim_idx);
679
680         if (MsgSettingGetBool(keyName, &bSimSst) != MSG_SUCCESS) {
681                 MSG_ERR("MsgSettingGetBool [%s] failed", keyName);
682         }
683
684         if (bSimSst == false) {
685                 return MSG_ERR_SIM_STORAGE_FULL;
686         }
687         pthread_t thd;
688         memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
689         memset(&addrInfo, 0, sizeof(MSG_ADDRESS_INFO_S));
690         memcpy(&msgInfo, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
691         memcpy(&addrInfo, pMsgInfo->addressList, sizeof(MSG_ADDRESS_INFO_S));
692         msgInfo.addressList = &addrInfo;
693
694         if (pthread_create(&thd, NULL, &class2_thread, (void *)&msgInfo) < 0)
695                 MSG_DEBUG("pthread_create() error");
696
697         pthread_detach(thd);
698
699 #if 0
700         err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
701
702
703         if (err == MSG_SUCCESS) {
704                 MSG_DEBUG("Success to saveClass2Message.");
705         } else {
706                 MSG_DEBUG("Fail to saveClass2Message : [%d]", err);
707         }
708 #endif
709
710         MSG_END();
711
712         return err;
713 }
714
715 void* SmsPluginStorage::class2_thread(void *data)
716 {
717         MSG_BEGIN();
718
719         msg_error_t err = MSG_SUCCESS;
720         MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)data;
721
722         err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
723         if (err == MSG_SUCCESS) {
724                 MSG_DEBUG("Success to saveClass2Message.");
725         } else {
726                 MSG_ERR("Fail to saveClass2Message : [%d]", err);
727         }
728
729         MSG_END();
730         return NULL;
731 }
732
733
734 msg_error_t SmsPluginStorage::getReplaceSimMsg(const MSG_MESSAGE_INFO_S *pMsg, int *pMsgId, int *pSimId)
735 {
736         MSG_BEGIN();
737
738         MsgDbHandler *dbHandle = getDbHandle();
739
740         char sqlQuery[MAX_QUERY_LEN+1];
741         msg_thread_id_t convId = 0;
742         msg_message_id_t msgId = 0;
743
744         dbHandle->beginTrans();
745
746         if (MsgExistAddress(dbHandle, pMsg, &convId) == true) {
747                 MSG_DEBUG("Address Info. exists [%d]", convId);
748
749                 /**  Find Replace Type Msg : Same Replace Type, Same Origin Address, Same Storage ID */
750                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
751                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s \
752                                                         WHERE CONV_ID = %d AND SUB_TYPE = %d AND STORAGE_ID = %d \
753                                                         ORDER BY DISPLAY_TIME ASC;",
754                                 MSGFW_MESSAGE_TABLE_NAME, (int)convId, pMsg->msgType.subType, MSG_STORAGE_SIM);
755
756                 MSG_DEBUG("Query=[%s]", sqlQuery);
757                 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
758                         dbHandle->endTrans(false);
759                         return MSG_ERR_DB_PREPARE;
760                 }
761
762                 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
763                         *pMsgId = dbHandle->columnInt(0);
764                 } else {
765                         dbHandle->finalizeQuery();
766                         dbHandle->endTrans(false);
767                         return MSG_ERR_DB_STEP;
768                 }
769
770                 dbHandle->finalizeQuery();
771
772         } else {
773                 dbHandle->endTrans(false);
774                 return MSG_ERR_DB_NORECORD;
775         }
776
777         memset(sqlQuery, 0x00, sizeof(sqlQuery));
778
779         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s \
780                         WHERE MSG_ID = %d;",
781                         MSGFW_SIM_MSG_TABLE_NAME, *pMsgId);
782
783         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
784                 dbHandle->endTrans(false);
785                 return MSG_ERR_DB_PREPARE;
786         }
787
788         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
789                 *pSimId = dbHandle->columnInt(0);
790         } else {
791                 dbHandle->finalizeQuery();
792                 dbHandle->endTrans(false);
793                 return MSG_ERR_DB_STEP;
794         }
795
796         MSG_DEBUG("Replace Msg Id=[%d], Sim Id=[%d]", *pMsgId, *pSimId);
797
798         dbHandle->finalizeQuery();
799
800         dbHandle->endTrans(true);
801
802         MSG_END();
803
804         return msgId;
805 }
806
807 msg_error_t SmsPluginStorage::addSmsSendOption(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
808 {
809         MSG_BEGIN();
810
811         msg_error_t err = MSG_SUCCESS;
812
813         if (pSendOptInfo->bSetting == false) {
814                 if (MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq) != MSG_SUCCESS)
815                         MSG_INFO("MsgSettingGetBool() is failed");
816
817                 if (MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath) != MSG_SUCCESS)
818                         MSG_INFO("MsgSettingGetBool() is failed");
819
820                 if (MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy) != MSG_SUCCESS)
821                         MSG_INFO("MsgSettingGetBool() is failed");
822         }
823
824         MsgDbHandler *dbHandle = getDbHandle();
825
826         char sqlQuery[MAX_QUERY_LEN+1];
827
828         dbHandle->beginTrans();
829
830         memset(sqlQuery, 0x00, sizeof(sqlQuery));
831         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
832                         MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId, pSendOptInfo->bDeliverReq,
833                         pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath, pMsg->encodeType);
834
835         MSG_DEBUG("Query = [%s]", sqlQuery);
836
837         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
838                 MSG_ERR("execQuery() is failed");
839                 dbHandle->endTrans(false);
840                 return MSG_ERR_DB_EXEC;
841         }
842
843         dbHandle->endTrans(true);
844
845         MSG_END();
846
847         return err;
848 }
849
850
851 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
852 {
853         msg_error_t err = MSG_SUCCESS;
854
855         MsgDbHandler *dbHandle = getDbHandle();
856
857         err = MsgStoCheckMsgCntFull(dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
858
859         if (err != MSG_SUCCESS) {
860                 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
861                         bool bAutoErase = false;
862
863                         if (MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase) != MSG_SUCCESS)
864                                 MSG_INFO("MsgSettingGetBool() is failed");
865
866                         MSG_DEBUG("bAutoErase: %d", bAutoErase);
867
868                         if (bAutoErase == true) {
869                                 msg_message_id_t msgId;
870
871                                 /** Find the oldest message's msgId */
872                                 err = MsgStoGetOldestMessage(dbHandle, pMsgInfo, &msgId);
873
874                                 if (err != MSG_SUCCESS)
875                                         return err;
876
877                                 /** Delete the corresponding message. */
878                                 err = deleteSmsMessage(msgId);
879                         }
880                 }
881
882                 return err;
883         }
884
885         return err;
886 }
887
888
889 #if 0
890
891 msg_error_t SmsPluginStorage::isReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
892 {
893         msg_error_t err = MSG_SUCCESS;
894
895         int rowCnt = 0;
896
897         MsgDbHandler *dbHandle = getDbHandle();
898
899         char sqlQuery[MAX_QUERY_LEN+1] = {0, };
900
901         memset(sqlQuery, 0x00, sizeof(sqlQuery));
902
903         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE GEO_SCOPE = %d AND MSG_CODE = %d AND MESSAGE_ID = %d AND UPDATE_NUM = %d",
904                         MSGFW_RECEIVED_CB_MSG_TABLE_NAME, CbPage.pageHeader.serialNum.geoScope,
905                         CbPage.pageHeader.serialNum.msgCode, CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
906
907         err = dbHandle->getTable(sqlQuery, &rowCnt, NULL);
908         MSG_DEBUG("rowCnt: %d", rowCnt);
909
910         dbHandle->freeTable();
911         return err;
912 }
913
914 msg_error_t SmsPluginStorage::insertReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
915 {
916         msg_error_t err = MSG_SUCCESS;
917
918         unsigned int rowId = 0;
919
920         MsgDbHandler *dbHandle = getDbHandle();
921
922         char sqlQuery[MAX_QUERY_LEN+1];
923
924         err = dbHandle->getRowId(MSGFW_RECEIVED_CB_MSG_TABLE_NAME, &rowId);
925
926         if (err != MSG_SUCCESS)
927                 return err;
928
929         /* Add Folder */
930         memset(sqlQuery, 0x00, sizeof(sqlQuery));
931         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
932                         MSGFW_RECEIVED_CB_MSG_TABLE_NAME, rowId, CbPage.pageHeader.serialNum.geoScope,
933                         CbPage.pageHeader.serialNum.msgCode, CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
934
935         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
936                 return MSG_ERR_DB_EXEC;
937
938         return MSG_SUCCESS;
939 }
940
941 #endif
942
943 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id, int app_id_len, char *content_type, int content_type_len)
944 {
945         msg_error_t err = MSG_SUCCESS;
946
947         int rowCnt = 0, index = 0;
948
949         MsgDbHandler *dbHandle = getDbHandle();
950
951         char sqlQuery[MAX_QUERY_LEN+1] = {0, };
952
953         memset(sqlQuery, 0x00, sizeof(sqlQuery));
954
955         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
956
957         err = dbHandle->getTable(sqlQuery, &rowCnt, &index);
958         MSG_DEBUG("rowCnt: %d", rowCnt);
959
960         if (err != MSG_SUCCESS) {
961                 dbHandle->freeTable();
962
963                 if (err == MSG_ERR_DB_NORECORD)
964                         return MSG_SUCCESS;
965                 else
966                         return err;
967         }
968
969         char contentType[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1] = {0, };
970         char appId[MAX_WAPPUSH_ID_LEN + 1] = {0, };
971         int appcode = 0, default_appcode = 0;
972         bool found = false;
973         char *_content_type = NULL, *_app_id = NULL;
974         *count = 0;
975
976
977         for (int i = 0; i < rowCnt; i++) {
978                 memset(contentType, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
979                 memset(appId, 0, MAX_WAPPUSH_ID_LEN);
980
981                 dbHandle->getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, contentType);
982                 dbHandle->getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, appId);
983                 appcode = dbHandle->getColumnToInt(index++);
984
985                 /* MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id); */
986                 _content_type = strcasestr(pPushHeader, contentType);
987                 if (_content_type) {
988                         _app_id = strcasestr(pPushHeader, appId);
989                         if (appcode)
990                                 default_appcode = appcode;
991
992                         if (_app_id) {
993                                 PUSH_APPLICATION_INFO_S pInfo = {0, };
994                                 pInfo.appcode = appcode;
995                                 MSG_SEC_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, appId);
996                                 snprintf(application_id, app_id_len, "%s", appId);
997                                 snprintf(content_type, content_type_len, "%s", contentType);
998                                 pushAppInfoList.push_back(pInfo);
999                                 (*count)++;
1000                                 found = true;
1001                         }
1002                 }
1003         }
1004
1005         if (!found && default_appcode != SMS_WAP_APPLICATION_LBS) {
1006                 /* perform default action. */
1007                 PUSH_APPLICATION_INFO_S pInfo = {0, };
1008                 pInfo.appcode = default_appcode;
1009                 memset(appId, 0, MAX_WAPPUSH_ID_LEN + 1);
1010                 snprintf(application_id, app_id_len, "%s", appId);
1011                 snprintf(content_type, content_type_len, "%s", contentType);
1012                 pushAppInfoList.push_back(pInfo);
1013                 *count = 1;
1014         }
1015         dbHandle->freeTable();
1016
1017         return err;
1018 }
1019
1020
1021 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1022 {
1023         msg_error_t err = MSG_SUCCESS;
1024         if ((unsigned int)index > pushAppInfoList.size() - 1)
1025                 return MSG_ERR_INVALID_PARAMETER;
1026
1027         std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1028         int count = 0;
1029         for (; it != pushAppInfoList.end(); it++) {
1030                 if (index == count) {
1031                         *appcode = it->appcode;
1032                         break;
1033                 }
1034                 count++;
1035         }
1036
1037         return err;
1038 }
1039
1040
1041 msg_error_t SmsPluginStorage::releasePushEvent()
1042 {
1043         msg_error_t err = MSG_SUCCESS;
1044         std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1045
1046         for (; it != pushAppInfoList.end(); it++)
1047                 it = pushAppInfoList.erase(it);
1048
1049         return err;
1050 }
1051
1052
1053 msg_error_t SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
1054 {
1055         MSG_BEGIN();
1056         MsgDbHandler *dbHandle = getDbHandle();
1057         char sqlQuery[MAX_QUERY_LEN+1] = {0, };
1058         msg_thread_id_t convId = 0;
1059
1060         dbHandle->beginTrans();
1061
1062         if (pMsgInfo->nAddressCnt > 0) {
1063                 pMsgInfo->threadId = 0;
1064                 msg_error_t err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
1065
1066                 if (err != MSG_SUCCESS) {
1067                         dbHandle->endTrans(false);
1068                         return err;
1069                 }
1070         }
1071
1072         int fileSize = 0;
1073
1074         char *pFileData = NULL;
1075         unique_ptr<char*, void(*)(char**)> buf(&pFileData, unique_ptr_deleter);
1076
1077         /* Get File Data */
1078         if (pMsgInfo->bTextSms == false) {
1079                 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false) {
1080                         dbHandle->endTrans(false);
1081                         return MSG_ERR_STORAGE_ERROR;
1082                 }
1083         }
1084
1085         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET CONV_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
1086                         DISPLAY_TIME = %lu, DATA_SIZE = %zu, NETWORK_STATUS = %d, READ_STATUS = %d, PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, \
1087                         BACKUP = %d, SUBJECT = ?, MSG_TEXT = ?, SIM_INDEX = %d \
1088                         WHERE MSG_ID = %d;",
1089                         MSGFW_MESSAGE_TABLE_NAME, convId, pMsgInfo->folderId, pMsgInfo->storageId, pMsgInfo->msgType.mainType, pMsgInfo->msgType.subType, pMsgInfo->displayTime, pMsgInfo->dataSize,
1090                         pMsgInfo->networkStatus, pMsgInfo->bRead, pMsgInfo->bProtected, pMsgInfo->priority, pMsgInfo->direction, pMsgInfo->bBackup, pMsgInfo->sim_idx, pMsgInfo->msgId);
1091
1092         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1093                 dbHandle->endTrans(false);
1094                 return MSG_ERR_DB_EXEC;
1095         }
1096
1097         dbHandle->bindText(pMsgInfo->subject, 1);
1098
1099         if (pMsgInfo->bTextSms == false)
1100                 dbHandle->bindText(pFileData, 2);
1101         else
1102                 dbHandle->bindText(pMsgInfo->msgText, 2);
1103
1104         MSG_DEBUG("%s", sqlQuery);
1105
1106         if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1107                 dbHandle->finalizeQuery();
1108                 dbHandle->endTrans(false);
1109                 return MSG_ERR_DB_EXEC;
1110         }
1111
1112         dbHandle->finalizeQuery();
1113         dbHandle->endTrans(true);
1114
1115         MSG_END();
1116         return MSG_SUCCESS;
1117 }
1118
1119
1120 bool SmsPluginStorage::isDuplicatedCBMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
1121 {
1122         msg_error_t err = MSG_SUCCESS;
1123         MsgDbHandler *dbHandle = getDbHandle();
1124         char sqlQuery[MAX_QUERY_LEN+1] = {0};
1125         int rowCnt = 0;
1126
1127         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE SERIAL_NUM = %d",
1128                         MSGFW_CB_MSG_TABLE_NAME, pMsgInfo->serialNum);
1129
1130         err = dbHandle->getTable(sqlQuery, &rowCnt, NULL);
1131         if (err == MSG_SUCCESS) {
1132                 dbHandle->freeTable();
1133                 return true;
1134         }
1135
1136         dbHandle->freeTable();
1137
1138         return false;
1139 }