Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginStorage.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 "MsgGconfWrapper.h"
26 #include "MsgNotificationWrapper.h"
27 #include "SmsPluginMain.h"
28 #include "SmsPluginSimMsg.h"
29 #include "SmsPluginStorage.h"
30
31
32 /*==================================================================================================
33                                      IMPLEMENTATION OF SmsPluginStorage - Member Functions
34 ==================================================================================================*/
35 SmsPluginStorage* SmsPluginStorage::pInstance = NULL;
36
37
38 SmsPluginStorage::SmsPluginStorage()
39 {
40 /*** No need to connect DB anymore.
41         if (dbHandle.connect() != MSG_SUCCESS) {
42                 MSG_DEBUG("DB Connect Fail");
43         }
44 ***/
45 }
46
47
48 SmsPluginStorage::~SmsPluginStorage()
49 {
50         if (dbHandle.disconnect() != MSG_SUCCESS) {
51                 MSG_DEBUG("DB Disconnect Fail");
52         }
53 }
54
55
56 SmsPluginStorage* SmsPluginStorage::instance()
57 {
58         if (!pInstance) {
59                 MSG_DEBUG("pInstance is NULL. Now creating instance.");
60                 pInstance = new SmsPluginStorage();
61         }
62
63         return pInstance;
64 }
65
66
67 msg_error_t SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
68 {
69         MSG_BEGIN();
70
71 /***  Comment below line to not save the time value after sent status (it could be used later.)
72         time_t curTime = time(NULL);
73 ***/
74         char sqlQuery[MAX_QUERY_LEN+1];
75
76         memset(sqlQuery, 0x00, sizeof(sqlQuery));
77
78         MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, status);
79
80         /** Move Msg to SENTBOX */
81         if (status == MSG_NETWORK_SEND_SUCCESS) {
82                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
83                                         MSGFW_MESSAGE_TABLE_NAME, status, MSG_SENTBOX_ID, pMsgInfo->msgId);
84         } else {
85                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
86                                         MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
87         }
88
89         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
90                 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
91                 return MSG_ERR_DB_EXEC;
92         }
93
94         if (status == MSG_NETWORK_SEND_SUCCESS) {
95                 MSG_DEBUG("MsgAddPhoneLog() : folderId [%d]", pMsgInfo->folderId);
96                 MsgAddPhoneLog(pMsgInfo);
97         }
98
99
100         MSG_END();
101
102         return MSG_SUCCESS;
103 }
104
105 #ifdef SMS_REPORT_OPERATION
106 msg_error_t SmsPluginStorage::updateMsgRef(msg_message_id_t MsgId, unsigned char MsgRef)
107 {
108         MSG_BEGIN();
109
110         char sqlQuery[MAX_QUERY_LEN+1];
111
112         memset(sqlQuery, 0x00, sizeof(sqlQuery));
113
114         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = %d WHERE MSG_ID = %d;",
115                                 MSGFW_REPORT_TABLE_NAME, (int)MsgRef, MsgId);
116
117         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
118                 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
119                 return MSG_ERR_DB_EXEC;
120         }
121
122         /** Set Message Reference for updating report table */
123         tmpMsgRef = MsgRef;
124
125         MSG_DEBUG("MsgRef : %d", MsgRef);
126
127         MSG_END();
128
129         return MSG_SUCCESS;
130 }
131
132
133 msg_error_t SmsPluginStorage::updateStatusReport(unsigned char MsgRef, msg_delivery_report_status_t Status, time_t DeliveryTime)
134 {
135         MSG_BEGIN();
136
137         MSG_DEBUG("tmpMsgRef : %d", tmpMsgRef);
138
139         char sqlQuery[MAX_QUERY_LEN+1];
140
141         /** Get Msg Id for Quickpanel Noti */
142         msg_message_id_t msgId = 0;
143
144         memset(sqlQuery, 0x00, sizeof(sqlQuery));
145         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MSG_REF = %d;",
146                                         MSGFW_REPORT_TABLE_NAME, (int)tmpMsgRef);
147
148         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
149                 return MSG_ERR_DB_PREPARE;
150
151         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
152                 msgId = dbHandle.columnInt(0);
153
154         dbHandle.finalizeQuery();
155
156         /** Update Status */
157         memset(sqlQuery, 0x00, sizeof(sqlQuery));
158         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, DELIVERY_REPORT_STATUS = %d, DELIVERY_REPORT_TIME = %lu WHERE MSG_REF = %d;",
159                                         MSGFW_REPORT_TABLE_NAME, Status, DeliveryTime, (int)tmpMsgRef);
160
161         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
162                 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
163                 return MSG_ERR_DB_EXEC;
164         }
165
166         /** Insert Quickpanel Noti */
167         msg_error_t ret = MSG_SUCCESS;
168
169         ret = MsgInsertSmsNotiToQuickpanel(&dbHandle, msgId, Status);
170
171         if (ret != MSG_SUCCESS) {
172                 MSG_DEBUG("MsgInsertSmsNotiToQuickpanel() Failed : [%d]", ret);
173                 return ret;
174         }
175
176         MSG_END();
177
178         return MSG_SUCCESS;
179 }
180 #endif
181
182 msg_error_t SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo)
183 {
184         msg_error_t err = MSG_SUCCESS;
185
186         msg_message_id_t msgId = 0;
187         msg_thread_id_t convId = 0;
188         unsigned int simId = 0;
189
190         char sqlQuery[MAX_QUERY_LEN+1];
191
192         dbHandle.beginTrans();
193
194         err = MsgStoAddAddress(&dbHandle, pSimMsgInfo, &convId);
195
196         if (err != MSG_SUCCESS) {
197                 dbHandle.endTrans(false);
198                 return err;
199         }
200
201         pSimMsgInfo->threadId = convId;
202
203         err = dbHandle.getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
204
205         if (err != MSG_SUCCESS) {
206                 dbHandle.endTrans(false);
207                 return err;
208         }
209
210         simId = pSimMsgInfo->msgId;
211         pSimMsgInfo->msgId = msgId;
212
213         SMS_CONCAT_SIM_MSG_S concatSimMsg = {0};
214
215         /** Get Data from Concat SIM Msg */
216         if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
217
218                 int fileSize = 0;
219
220                 char* pFileData = NULL;
221                 AutoPtr<char> buf(&pFileData);
222
223                 if (MsgOpenAndReadFile(pSimMsgInfo->msgData, &pFileData, &fileSize) == false) {
224                         dbHandle.endTrans(false);
225                         return MSG_ERR_STORAGE_ERROR;
226                 }
227
228
229                 memcpy(&concatSimMsg, (SMS_CONCAT_SIM_MSG_S*)pFileData, fileSize);
230
231                 /** Delete temporary file */
232                 MsgDeleteFile(pSimMsgInfo->msgData); /** ipc */
233
234                 MSG_DEBUG("SIM ID [%d], MSG DATA [%s]", concatSimMsg.simIdCnt, concatSimMsg.msgData);
235         }
236
237         /**  Add Message */
238         memset(sqlQuery, 0x00, sizeof(sqlQuery));
239
240         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, '', '', ?, 0, 0, 0);",
241                         MSGFW_MESSAGE_TABLE_NAME, msgId, convId, pSimMsgInfo->folderId, pSimMsgInfo->storageId,
242                         pSimMsgInfo->msgType.mainType, pSimMsgInfo->msgType.subType, pSimMsgInfo->displayTime, pSimMsgInfo->dataSize,
243                         pSimMsgInfo->networkStatus, pSimMsgInfo->bRead, pSimMsgInfo->bProtected, pSimMsgInfo->priority,
244                         pSimMsgInfo->direction, 0, pSimMsgInfo->bBackup);
245
246         MSG_DEBUG("QUERY : %s", sqlQuery);
247
248         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
249                 dbHandle.endTrans(false);
250                 return MSG_ERR_DB_PREPARE;
251         }
252
253         dbHandle.bindText(pSimMsgInfo->subject, 1);
254
255         if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false)
256                 dbHandle.bindText(concatSimMsg.msgData, 2);
257         else
258                 dbHandle.bindText(pSimMsgInfo->msgText, 2);
259
260         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
261                 dbHandle.endTrans(false);
262                 return MSG_ERR_DB_STEP;
263         }
264
265         dbHandle.finalizeQuery();
266
267         /** Insert to Sim table */
268         if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
269
270                 MSG_DEBUG("sim count : %d", concatSimMsg.simIdCnt);
271
272                 for (unsigned int i = 0; i < concatSimMsg.simIdCnt; i++) {
273                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
274                         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
275                                         MSGFW_SIM_MSG_TABLE_NAME, msgId, concatSimMsg.simIdList[i]);
276
277                         MSG_DEBUG("QUERY : %s", sqlQuery);
278
279                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
280                                 dbHandle.endTrans(false);
281                                 return MSG_ERR_DB_EXEC;
282                         }
283                 }
284         } else {
285                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
286                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
287                                 MSGFW_SIM_MSG_TABLE_NAME, msgId, simId);
288
289                 MSG_DEBUG("QUERY : %s", sqlQuery);
290
291                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
292                         dbHandle.endTrans(false);
293                         return MSG_ERR_DB_EXEC;
294                 }
295         }
296
297         /**  Update conversation table. */
298         if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
299                 dbHandle.endTrans(false);
300                 return MSG_ERR_STORAGE_ERROR;
301         }
302
303         dbHandle.endTrans(true);
304
305         return err;
306 }
307
308
309 msg_error_t SmsPluginStorage::addMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
310 {
311         MSG_BEGIN();
312
313         msg_error_t err = MSG_SUCCESS;
314
315         /**  Check whether storage is full or not */
316         err = checkStorageStatus(pMsgInfo);
317
318         if (err != MSG_SUCCESS) {
319                 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
320                         pMsgInfo->folderId = 0;
321                         if (addSmsMessage(pMsgInfo) != MSG_SUCCESS) {
322                                 MSG_DEBUG("addSmsMessage is failed!");
323                         }
324                 }
325                 return err;
326         }
327
328         if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
329
330                 MSG_DEBUG("Add Normal SMS");
331
332                 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
333                         err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
334
335                         if (err == MSG_SUCCESS) {
336                                 MSG_DEBUG("Success to saveSimMessage.");
337                         } else {
338                                 MSG_DEBUG("Fail to saveSimMessage : [%d]", err);
339                         }
340                 } else {
341                         /** Class 0 Msg should be saved in hidden folder */
342                         if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
343                                 pMsgInfo->folderId = 0;
344                         }
345
346                         /**  Add into DB */
347                         err = addSmsMessage(pMsgInfo);
348                 }
349
350         } else if ((pMsgInfo->msgType.subType == MSG_CB_SMS) || (pMsgInfo->msgType.subType == MSG_JAVACB_SMS)) {
351                 /** check add message option */
352                 bool bSave = false;
353                 MsgSettingGetBool(CB_SAVE, &bSave);
354                 if(bSave) {
355                         MSG_DEBUG("Add CB Message");
356                         err = addCbMessage(pMsgInfo);
357                 }
358         } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
359                 MSG_DEBUG("Add Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
360                 err = addReplaceTypeMsg(pMsgInfo);
361         } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
362                 if (pMsgInfo->bStore == true) {
363                         MSG_DEBUG("Add MWI Message");
364                         err = addSmsMessage(pMsgInfo);
365                 }
366         } else if ((pMsgInfo->msgType.subType == MSG_WAP_SI_SMS) || (pMsgInfo->msgType.subType == MSG_WAP_CO_SMS)) {
367                 MSG_DEBUG("Add WAP Push Message");
368                 switch (pMsgInfo->msgType.subType)
369                 {
370                         case MSG_WAP_SI_SMS:
371                         {
372                                 // save push message information
373                                 err = addWAPMessage(pMsgInfo);
374                         }
375                         break;
376
377                         case MSG_WAP_CO_SMS:
378                         {
379                                 err = handleCOWAPMessage(pMsgInfo);
380                         }
381                         break;
382                 }
383         }
384
385         if (err == MSG_SUCCESS) {
386                 MSG_DEBUG("Success to add message !!");
387         } else {
388                 MSG_DEBUG("fail to add message !! : [%d]", err);
389         }
390
391         return err;
392 }
393
394
395 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
396 {
397         msg_error_t err = MSG_SUCCESS;
398
399         unsigned int rowId = 0;
400         msg_thread_id_t convId = 0;
401
402         dbHandle.beginTrans();
403
404         if (pMsgInfo->nAddressCnt > 0) {
405
406                 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
407
408                 if (err != MSG_SUCCESS) {
409                         dbHandle.endTrans(false);
410                         return err;
411                 }
412
413                 pMsgInfo->threadId = convId;
414         }
415
416         /**  Add Message Table */
417         rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
418
419         if (rowId <= 0) {
420                 dbHandle.endTrans(false);
421                 return MSG_ERR_DB_ROW;
422         }
423
424         /** Update conversation table */
425         err = MsgStoUpdateConversation(&dbHandle, convId);
426
427         if (err != MSG_SUCCESS) {
428                 dbHandle.endTrans(false);
429                 return err;
430         }
431
432         dbHandle.endTrans(true);
433
434         pMsgInfo->msgId = (msg_message_id_t)rowId;
435
436         MSG_END();
437
438         return MSG_SUCCESS;
439 }
440
441 msg_error_t SmsPluginStorage::addSmsSendOption(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
442 {
443         MSG_BEGIN();
444
445         msg_error_t err = MSG_SUCCESS;
446
447         if (pSendOptInfo->bSetting == false) {
448                 MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq);
449                 MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath);
450
451                 if (pSendOptInfo->bDeliverReq || pSendOptInfo->option.smsSendOptInfo.bReplyPath) {
452                         pSendOptInfo->bSetting = true;
453                         MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy);
454                 }
455         }
456
457         if (pSendOptInfo->bSetting == true) {
458                 char sqlQuery[MAX_QUERY_LEN+1];
459
460                 dbHandle.beginTrans();
461
462                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
463                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d);",
464                                 MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId, pSendOptInfo->bDeliverReq,
465                                 pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath);
466
467                 MSG_DEBUG("Query = [%s]", sqlQuery);
468
469                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
470                         dbHandle.endTrans(false);
471                         err = MSG_ERR_DB_EXEC;
472                 } else {
473                         dbHandle.endTrans(true);
474                 }
475         }
476
477         MSG_END();
478
479         return err;
480 }
481
482
483 msg_error_t SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsg)
484 {
485         msg_error_t err = MSG_SUCCESS;
486
487         char sqlQuery[MAX_QUERY_LEN+1];
488
489         msg_thread_id_t convId = 0;
490
491         dbHandle.beginTrans();
492
493         if (pMsg->nAddressCnt > 0) {
494
495                 err = MsgStoAddAddress(&dbHandle, pMsg, &convId);
496
497                 if (err != MSG_SUCCESS) {
498                         dbHandle.endTrans(false);
499                         return err;
500                 }
501         }
502
503         int fileSize = 0;
504
505         char* pFileData = NULL;
506         AutoPtr<char> buf(&pFileData);
507
508         /**  Get File Data */
509         if (pMsg->bTextSms == false) {
510                 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
511                         dbHandle.endTrans(false);
512                         return MSG_ERR_STORAGE_ERROR;
513                 }
514         }
515
516         /**  Update Message */
517         memset(sqlQuery, 0x00, sizeof(sqlQuery));
518
519         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET CONV_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
520                         DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, \
521                         BACKUP = %d, SUBJECT = ?, MSG_DATA = ?, THUMB_PATH = ?, MSG_TEXT = ? WHERE MSG_ID = %d;",
522                                 MSGFW_MESSAGE_TABLE_NAME, convId, pMsg->folderId, pMsg->storageId, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->dataSize,
523                                 pMsg->networkStatus, pMsg->bRead, pMsg->bProtected, pMsg->priority, pMsg->direction, pMsg->bBackup, pMsg->msgId);
524
525         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
526                 dbHandle.endTrans(false);
527                 return MSG_ERR_DB_EXEC;
528         }
529
530         dbHandle.bindText(pMsg->subject, 1);
531
532         dbHandle.bindText(pMsg->msgData, 2);
533
534         dbHandle.bindText(pMsg->thumbPath, 3);
535
536         if (pMsg->msgType.mainType == MSG_SMS_TYPE && pMsg->bTextSms == false)
537                 dbHandle.bindText(pFileData, 4);
538         else
539                 dbHandle.bindText(pMsg->msgText, 4);
540
541         MSG_DEBUG("%s", sqlQuery);
542
543         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
544                 dbHandle.endTrans(false);
545                 return MSG_ERR_DB_EXEC;
546         }
547
548         dbHandle.finalizeQuery();
549
550         err = MsgStoUpdateConversation(&dbHandle, convId);
551
552         if (err != MSG_SUCCESS) {
553                 dbHandle.endTrans(false);
554                 return MSG_ERR_STORAGE_ERROR;
555         }
556
557         err = MsgStoClearConversationTable(&dbHandle);
558
559         if (err != MSG_SUCCESS) {
560                 dbHandle.endTrans(false);
561                 return MSG_ERR_STORAGE_ERROR;
562         }
563
564         dbHandle.endTrans(true);
565
566         return MSG_SUCCESS;
567 }
568
569
570 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
571 {
572         MSG_BEGIN();
573
574         char sqlQuery[MAX_QUERY_LEN+1];
575
576          /**  Get SUB_TYPE, STORAGE_ID */
577         memset(sqlQuery, 0x00, sizeof(sqlQuery));
578         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, ADDRESS_ID \
579                         FROM %s WHERE MSG_ID = %d;",
580                         MSGFW_MESSAGE_TABLE_NAME, msgId);
581
582         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
583                 return MSG_ERR_DB_PREPARE;
584
585         MSG_MESSAGE_TYPE_S msgType;
586         msg_folder_id_t folderId;
587
588         msg_thread_id_t convId;
589
590         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
591                 msgType.mainType = dbHandle.columnInt(0);
592                 msgType.subType = dbHandle.columnInt(1);
593                 folderId = dbHandle.columnInt(2);
594                 convId = dbHandle.columnInt(3);
595
596                 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
597         } else {
598                 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
599                 dbHandle.finalizeQuery();
600                 return MSG_ERR_DB_STEP;
601         }
602
603         dbHandle.finalizeQuery();
604
605         dbHandle.beginTrans();
606
607         memset(sqlQuery, 0x00, sizeof(sqlQuery));
608         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
609
610         /**  Delete SMS Send Option */
611         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
612                 dbHandle.endTrans(false);
613                 return MSG_ERR_DB_EXEC;
614         }
615
616         if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
617                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
618                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
619
620                 /** Delete Push Message from push table */
621                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
622                         dbHandle.endTrans(false);
623                         return MSG_ERR_DB_EXEC;
624                 }
625         } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
626                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
627                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
628
629                 /**  Delete Push Message from push table */
630                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
631                         dbHandle.endTrans(false);
632                         return MSG_ERR_DB_EXEC;
633                 }
634         } else if (msgType.subType == MSG_SYNCML_CP) {
635                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
636                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
637
638                 /**  Delete SyncML Message from syncML table */
639                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
640                         dbHandle.endTrans(false);
641                         return MSG_ERR_DB_EXEC;
642                 }
643         }
644
645         memset(sqlQuery, 0x00, sizeof(sqlQuery));
646         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
647
648         /** Delete Message from msg table */
649         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
650                 dbHandle.endTrans(false);
651                 return MSG_ERR_DB_EXEC;
652         }
653
654         /**  Clear Conversation table */
655         if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
656                 dbHandle.endTrans(false);
657                 return MSG_ERR_DB_EXEC;
658         }
659
660         /**  Update conversation table.*/
661         if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
662                 dbHandle.endTrans(false);
663                 return MSG_ERR_STORAGE_ERROR;
664         }
665
666         dbHandle.endTrans(true);
667
668         if (folderId == MSG_INBOX_ID) {
669                 msgType.classType = MSG_CLASS_NONE;
670
671                 /**  Set memory status in SIM */
672                 if (MsgStoCheckMsgCntFull(&dbHandle, &msgType, folderId) == MSG_SUCCESS) {
673                         MSG_DEBUG("Set Memory Status");
674                         SmsPlgSetMemoryStatus(MSG_SUCCESS);
675                 }
676         }
677
678         int smsCnt = 0, mmsCnt = 0;
679
680         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
681         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
682
683         MsgSettingHandleNewMsg(smsCnt, mmsCnt);
684         MsgRefreshNoti(false);
685
686         return MSG_SUCCESS;
687 }
688
689
690 msg_error_t SmsPluginStorage::addCbMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
691 {
692         msg_error_t err = MSG_SUCCESS;
693
694         unsigned int rowId = 0;
695         msg_thread_id_t convId = 0;
696
697         char sqlQuery[MAX_QUERY_LEN+1];
698
699         dbHandle.beginTrans();
700
701         if (pMsgInfo->nAddressCnt > 0) {
702                 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
703
704                 if (err != MSG_SUCCESS) {
705                         dbHandle.endTrans(false);
706                         return err;
707                 }
708
709                 pMsgInfo->threadId = convId;
710         }
711
712         /**  Add Message Table */
713         rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
714
715         if (rowId <= 0) {
716                 dbHandle.endTrans(false);
717                 return MSG_ERR_DB_ROW;
718         }
719
720         /**  Get CB Msg ID */
721         unsigned short cbMsgId = (unsigned short)pMsgInfo->msgId;
722
723         /** Add CB Msg in MSG_CBMSG_TABLE */
724         memset(sqlQuery, 0x00, sizeof(sqlQuery));
725
726         sprintf(sqlQuery, "INSERT INTO %s VALUES (%d, %d);",
727                         MSGFW_CB_MSG_TABLE_NAME, rowId, cbMsgId);
728
729         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
730                 dbHandle.endTrans(false);
731                 return MSG_ERR_DB_EXEC;
732         }
733
734         /**  Update conversation table. */
735         if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
736                 dbHandle.endTrans(false);
737                 return MSG_ERR_STORAGE_ERROR;
738         }
739
740         dbHandle.endTrans(true);
741
742         /** Assign Msg ID */
743         pMsgInfo->msgId = (msg_message_id_t)rowId;
744
745         return err;
746 }
747
748
749 msg_error_t SmsPluginStorage::addReplaceTypeMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
750 {
751         msg_error_t err = MSG_SUCCESS;
752
753         char sqlQuery[MAX_QUERY_LEN+1];
754
755         unsigned int retCnt = 0;
756         msg_thread_id_t convId = 0;
757
758         /** Check if new address or not */
759         if (MsgExistAddress(&dbHandle, pMsgInfo, &convId) == true) {
760                 MSG_DEBUG("Address Info. exists [%d]", convId);
761
762                 /**  Find Replace Type Msg : Same Replace Type, Same Origin Address */
763                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
764                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*), MSG_ID FROM %s WHERE SUB_TYPE = %d AND CONV_ID = %d;",
765                                 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgType.subType, convId);
766
767                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
768                         return MSG_ERR_DB_PREPARE;
769
770                 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
771                         retCnt = dbHandle.columnInt(0);
772                         pMsgInfo->msgId = dbHandle.columnInt(1);
773                 } else {
774                         dbHandle.finalizeQuery();
775                         return MSG_ERR_DB_STEP;
776                 }
777
778                 dbHandle.finalizeQuery();
779         }
780
781         /** Update New Replace Type Msg */
782         if (retCnt == 0) { /** Insert New Replace Type Msg */
783                 MSG_DEBUG("Insert Replace Type Msg");
784                 err = addSmsMessage(pMsgInfo);
785         } else {
786                 MSG_DEBUG("Update Replace Type Msg");
787                 err = updateSmsMessage(pMsgInfo);
788         }
789
790         return err;
791 }
792
793
794 msg_error_t SmsPluginStorage::addWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
795 {
796         msg_error_t err = MSG_SUCCESS;
797
798         MSG_PUSH_MESSAGE_S pushMsg = {};
799
800         char sqlQuery[MAX_QUERY_LEN+1];
801         memset(sqlQuery, 0x00, sizeof(sqlQuery));
802
803         int fileSize = 0;
804
805         char* pFileData = NULL;
806         AutoPtr<char> buf(&pFileData);
807
808         if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
809                 return MSG_ERR_STORAGE_ERROR;
810
811         MSG_DEBUG("fileSize : %d", fileSize);
812
813         memcpy(&pushMsg, pFileData, fileSize);
814
815         /** Delete temporary file */
816         MsgDeleteFile(pMsgInfo->msgData);
817
818         /** check pPushMsg data */
819
820         MSG_DEBUG("check pushMsg data");
821         MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
822         MSG_DEBUG("pushMsg.received : [%d]", pushMsg.received);
823         MSG_DEBUG("pushMsg.created : [%d]", pushMsg.created);
824         MSG_DEBUG("pushMsg.expires : [%d]", pushMsg.expires);
825         MSG_DEBUG("pushMsg.id : [%s]", pushMsg.id);
826         MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
827         MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
828
829         bool bProceed = true;
830
831         /**  check validation of contents */
832         if (checkPushMsgValidation(&pushMsg, &bProceed) != MSG_SUCCESS) {
833                 MSG_DEBUG("Fail to check Push Message validation.");
834         }
835
836         /**  if validation check value is false */
837         /** return and drop message. */
838         if (bProceed == false)
839                 return MSG_ERR_INVALID_MESSAGE;
840
841 #if 0
842         /**  update subject */
843         int len = strlen(pushMsg.contents);
844
845         if (len > MAX_SUBJECT_LEN) {
846                 memcpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
847                 pMsgInfo->subject[MAX_SUBJECT_LEN] = '\0';
848         } else {
849                 strncpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
850         }
851 #endif
852
853         /**  Update Msg Text - remove */
854         strncpy(pMsgInfo->msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
855
856         if (pushMsg.contents[0] != '\0') {
857                 strncat(pMsgInfo->msgText, " ", MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
858                 strncat(pMsgInfo->msgText, pushMsg.contents, MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
859         }
860
861         pMsgInfo->dataSize = strlen(pMsgInfo->msgText);
862
863         pMsgInfo->bTextSms = true;
864         pMsgInfo->folderId = MSG_INBOX_ID;
865         pMsgInfo->storageId = MSG_STORAGE_PHONE;
866
867         msg_thread_id_t convId = 0;
868
869         dbHandle.beginTrans();
870
871         if (pMsgInfo->nAddressCnt > 0) {
872
873                 err = MsgStoAddAddress(&dbHandle, pMsgInfo, &convId);
874
875                 if (err != MSG_SUCCESS) {
876                         dbHandle.endTrans(false);
877                         return err;
878                 }
879
880                 pMsgInfo->threadId = convId;
881         }
882
883         /**  get last row count for Message id */
884         unsigned int rowId = 0;
885
886         /** Add Message Table */
887         rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo);
888
889         if (rowId <= 0) {
890                 dbHandle.endTrans(false);
891                 return MSG_ERR_DB_ROW;
892         }
893
894         /**  add msg_push_table */
895         snprintf (sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %lu, %lu, ?, ?, ?)",
896                         MSGFW_PUSH_MSG_TABLE_NAME, pMsgInfo->msgId, pushMsg.action, pushMsg.created, pushMsg.expires);
897
898         if ((err = dbHandle.prepareQuery(sqlQuery)) != MSG_SUCCESS) {
899                 dbHandle.endTrans(false);
900                 return err;
901         }
902
903         dbHandle.bindText(pushMsg.id, 1);
904
905         dbHandle.bindText(pushMsg.href, 2);
906
907         dbHandle.bindText(pushMsg.contents, 3);
908
909         if ((err = dbHandle.stepQuery()) != MSG_ERR_DB_DONE) {
910                 dbHandle.endTrans(false);
911                 return err;
912         }
913
914         /** Update conversation table. */
915         if (MsgStoUpdateConversation(&dbHandle, convId) != MSG_SUCCESS) {
916                 dbHandle.endTrans(false);
917                 return MSG_ERR_STORAGE_ERROR;
918         }
919
920         dbHandle.endTrans(true);
921
922         pMsgInfo->msgId = (msg_message_id_t)rowId;
923
924         return MSG_SUCCESS;
925 }
926
927
928 msg_error_t SmsPluginStorage::handleCOWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
929 {
930         msg_error_t err = MSG_SUCCESS;
931
932         char href[MAX_PUSH_CACHEOP_MAX_URL_LEN+1];
933         char sqlQuery[MAX_QUERY_LEN+1];
934
935         int fileSize = 0;
936
937         char* pFileData = NULL;
938         AutoPtr<char> buf(&pFileData);
939
940         if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
941                 return MSG_ERR_STORAGE_ERROR;
942
943         MSG_PUSH_CACHEOP_S *pPushMsg;
944
945         pPushMsg = (MSG_PUSH_CACHEOP_S*)pFileData;
946
947         for (int i = 0; i < pPushMsg->invalObjectCnt; i++) {
948
949                 int msgid = -1;
950
951                 memset(href, 0x00, sizeof(href));
952                 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
953
954                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
955                 snprintf (sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%';", MSGFW_PUSH_MSG_TABLE_NAME, href);
956
957                 dbHandle.beginTrans();
958
959                 err = dbHandle.prepareQuery(sqlQuery);
960
961                 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
962
963                         msgid = dbHandle.getColumnToInt(1);
964
965                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
966                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID= %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
967
968                         /** Delete Message from Push table */
969                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
970                                 dbHandle.finalizeQuery();
971                                 dbHandle.endTrans(false);
972                                 continue;
973                         }
974
975                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
976                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
977
978                         /** Delete Message from msg table */
979                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
980                                 dbHandle.finalizeQuery();
981                                 dbHandle.endTrans(false);
982                                 continue;
983                         }
984
985                         /** Update all Address */
986                         if (updateAllAddress() != MSG_SUCCESS) {
987                                 dbHandle.finalizeQuery();
988                                 dbHandle.endTrans(false);
989                                 continue;
990                         }
991
992                         /** Clear Conversation table */
993                         if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
994                                 dbHandle.finalizeQuery();
995                                 dbHandle.endTrans(false);
996                                 continue;
997                         }
998                 }
999
1000                 dbHandle.finalizeQuery();
1001
1002                 dbHandle.endTrans(true);
1003         }
1004
1005         for (int i = 0; i < pPushMsg->invalServiceCnt; i++) {
1006
1007                 int msgid = -1;
1008
1009                 memset(href, 0x00, sizeof(href));
1010                 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
1011
1012                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1013                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%'", MSGFW_PUSH_MSG_TABLE_NAME, href);
1014
1015                 dbHandle.beginTrans();
1016
1017                 err = dbHandle.prepareQuery(sqlQuery);
1018
1019                 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
1020
1021                         msgid = dbHandle.getColumnToInt(1);
1022
1023                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1024                         sprintf(sqlQuery, "DELETE FROM %s WHERE MSG_ID='%d'", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
1025
1026                         /** Delete Message from Push table */
1027                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1028                                 dbHandle.finalizeQuery();
1029                                 dbHandle.endTrans(false);
1030                                 continue;
1031                         }
1032
1033                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1034                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
1035
1036                         /** Delete Message from msg table */
1037                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1038                                 dbHandle.finalizeQuery();
1039                                 dbHandle.endTrans(false);
1040                                 continue;
1041                         }
1042
1043                         /**  Update all Address */
1044                         if (updateAllAddress() != MSG_SUCCESS) {
1045                                 dbHandle.finalizeQuery();
1046                                 dbHandle.endTrans(false);
1047                                 continue;
1048                         }
1049
1050                         /** Clear Address table */
1051                         if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
1052                                 dbHandle.finalizeQuery();
1053                                 dbHandle.endTrans(false);
1054                                 continue;
1055                         }
1056                 }
1057
1058                 dbHandle.finalizeQuery();
1059
1060                 dbHandle.endTrans(true);
1061         }
1062
1063         /** delete temporary file */
1064         MsgDeleteFile(pMsgInfo->msgData);
1065
1066         return MSG_SUCCESS;
1067 }
1068
1069
1070 msg_error_t SmsPluginStorage::checkPushMsgValidation(MSG_PUSH_MESSAGE_S *pPushMsg, bool *pbProceed)
1071 {
1072         msg_error_t err = MSG_SUCCESS;
1073
1074         unsigned long oldExpireTime = 0;
1075         int rowCnt = 0;
1076
1077         char sqlQuery[MAX_QUERY_LEN+1];
1078
1079         /**  is push message is expired?? */
1080         if (pPushMsg->received > pPushMsg->expires) {
1081                 MSG_DEBUG("Push Message is expired.");
1082                 pbProceed = false;
1083                 return err;
1084         }
1085
1086
1087         if (pPushMsg->action == MSG_PUSH_SL_ACTION_EXECUTE_LOW) {
1088                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
1089                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s' AND ACTION = %d",
1090                                 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id, pPushMsg->action);
1091         } else {
1092                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s'",
1093                                 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id);
1094         }
1095
1096         err = dbHandle.getTable(sqlQuery, &rowCnt);
1097
1098         if (rowCnt < 1) {
1099                 dbHandle.freeTable();
1100                 return MSG_SUCCESS;
1101         }
1102
1103         oldExpireTime = dbHandle.getColumnToInt(1);
1104
1105         dbHandle.freeTable();
1106
1107         if (pPushMsg->created < oldExpireTime) {
1108                 MSG_DEBUG("Push Message is expired.");
1109                 pbProceed = false;
1110                 return err;
1111         }
1112
1113         return err;
1114 }
1115
1116
1117 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
1118 {
1119         msg_error_t err = MSG_SUCCESS;
1120
1121         err = MsgStoCheckMsgCntFull(&dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
1122
1123         if (err != MSG_SUCCESS) {
1124
1125                 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
1126                         bool bAutoErase = false;
1127
1128                         MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
1129
1130                         MSG_DEBUG("bAutoErase: %d", bAutoErase);
1131
1132                         if (bAutoErase == true) {
1133                                 msg_message_id_t msgId;
1134
1135                                 /** Find the oldest message's msgId */
1136                                 err = MsgStoGetOldestMessage(&dbHandle, pMsgInfo, &msgId);
1137
1138                                 if (err != MSG_SUCCESS)
1139                                         return err;
1140
1141                                 /** Delete the corresponding message. */
1142                                 err = deleteSmsMessage(msgId);
1143                         }
1144                 }
1145
1146                 return err;
1147         }
1148
1149         return err;
1150 }
1151
1152
1153 msg_error_t SmsPluginStorage::updateAllAddress()
1154 {
1155         msg_error_t err = MSG_SUCCESS;
1156
1157         int rowCnt = 0, index = 1;
1158         char sqlQuery[MAX_QUERY_LEN+1];
1159
1160         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1161
1162         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s", MSGFW_ADDRESS_TABLE_NAME);
1163
1164         err = dbHandle.getTable(sqlQuery, &rowCnt);
1165
1166         if (err == MSG_ERR_DB_NORECORD) {
1167                 dbHandle.freeTable();
1168                 return MSG_SUCCESS;
1169         } else if ( err != MSG_SUCCESS) {
1170                 dbHandle.freeTable();
1171                 return err;
1172         }
1173
1174
1175         for (int i = 0; i < rowCnt; i++) {
1176
1177                 err = MsgStoUpdateConversation(&dbHandle, index++);
1178
1179                 if (err != MSG_SUCCESS)
1180                         break;
1181         }
1182
1183         dbHandle.freeTable();
1184
1185         return err;
1186 }
1187
1188
1189 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id, char *contentType)
1190 {
1191         msg_error_t err = MSG_SUCCESS;
1192
1193         int rowCnt = 0, index = 3;
1194
1195         char sqlQuery[MAX_QUERY_LEN+1] = {0, };
1196
1197         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1198
1199         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
1200
1201         err = dbHandle.getTable(sqlQuery, &rowCnt);
1202         MSG_DEBUG("rowCnt: %d", rowCnt);
1203
1204         if (err == MSG_ERR_DB_NORECORD) {
1205                 dbHandle.freeTable();
1206                 return MSG_SUCCESS;
1207         }
1208         else if ( err != MSG_SUCCESS) {
1209                 dbHandle.freeTable();
1210                 return err;
1211         }
1212
1213         char content_type[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1];
1214         char app_id[MAX_WAPPUSH_ID_LEN + 1];
1215         int appcode = 0, default_appcode = 0;
1216         bool found = false;
1217         char *_content_type = NULL, *_app_id = NULL;
1218         *count = 0;
1219
1220
1221         for (int i = 0; i < rowCnt; i++) {
1222                 memset(content_type, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
1223                 memset(app_id, 0, MAX_WAPPUSH_ID_LEN);
1224
1225                 dbHandle.getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, content_type);
1226                 dbHandle.getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, app_id);
1227                 appcode = dbHandle.getColumnToInt(index++);
1228
1229                 //MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id);
1230                 _content_type = strcasestr(pPushHeader, content_type);
1231                 if(_content_type) {
1232                         _app_id = strcasestr(pPushHeader, app_id);
1233                         if(appcode)
1234                                 default_appcode = appcode;
1235
1236                         if(_app_id) {
1237                                 PUSH_APPLICATION_INFO_S pInfo = {0, };
1238                                 pInfo.appcode = appcode;
1239                                 MSG_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, app_id);
1240                                 strcpy(application_id, app_id);
1241                                 strcpy(contentType, content_type);
1242                                 pushAppInfoList.push_back(pInfo);
1243                                 (*count)++;
1244                                 found = true;
1245                         }
1246                 }
1247         }
1248
1249         if(!found)
1250         {
1251                 // perform default action.
1252                 PUSH_APPLICATION_INFO_S pInfo = {0, };
1253                 pInfo.appcode = default_appcode;
1254                 strcpy(application_id, app_id);
1255                 strcpy(contentType, content_type);
1256                 pushAppInfoList.push_back(pInfo);
1257                 *count = 1;
1258         }
1259         dbHandle.freeTable();
1260
1261         return err;
1262 }
1263
1264
1265 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1266 {
1267         msg_error_t err = MSG_SUCCESS;
1268         if((unsigned int)index > pushAppInfoList.size() - 1)
1269                 return MSG_ERR_INVALID_PARAMETER;
1270
1271         std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1272         int count = 0;
1273         for (; it != pushAppInfoList.end(); it++)
1274         {
1275                 if(index == count){
1276                         *appcode = it->appcode;
1277                         break;
1278                 }
1279                 count++;
1280         }
1281
1282         return err;
1283 }
1284
1285
1286 msg_error_t SmsPluginStorage::releasePushEvent()
1287 {
1288         msg_error_t err = MSG_SUCCESS;
1289         std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1290
1291         for (; it != pushAppInfoList.end(); it++)
1292                 it = pushAppInfoList.erase(it);
1293
1294         return err;
1295 }