update tizen source
[framework/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginStorage.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #include <errno.h>
32
33 #include "MsgDebug.h"
34 #include "MsgCppTypes.h"
35 #include "MsgException.h"
36 #include "MsgContact.h"
37 #include "MsgUtilFile.h"
38 #include "MsgUtilStorage.h"
39 #include "MsgGconfWrapper.h"
40 #include "MsgNotificationWrapper.h"
41 #include "SmsPluginMain.h"
42 #include "SmsPluginSimMsg.h"
43 #include "SmsPluginStorage.h"
44
45
46 /*==================================================================================================
47                                      IMPLEMENTATION OF SmsPluginStorage - Member Functions
48 ==================================================================================================*/
49 SmsPluginStorage* SmsPluginStorage::pInstance = NULL;
50
51
52 SmsPluginStorage::SmsPluginStorage()
53 {
54 }
55
56
57 SmsPluginStorage::~SmsPluginStorage()
58 {
59         if (dbHandle.disconnect() != MSG_SUCCESS) {
60                 MSG_DEBUG("DB Disconnect Fail");
61         }
62 }
63
64
65 SmsPluginStorage* SmsPluginStorage::instance()
66 {
67         if (!pInstance) {
68                 MSG_DEBUG("pInstance is NULL. Now creating instance.");
69                 pInstance = new SmsPluginStorage();
70         }
71
72         return pInstance;
73 }
74
75
76 MSG_ERROR_T SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_NETWORK_STATUS_T Status)
77 {
78         MSG_BEGIN();
79
80         char sqlQuery[MAX_QUERY_LEN+1];
81
82         memset(sqlQuery, 0x00, sizeof(sqlQuery));
83
84         MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, Status);
85
86         /** Move Msg to SENTBOX */
87         if (Status == MSG_NETWORK_SEND_SUCCESS) {
88                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
89                                         MSGFW_MESSAGE_TABLE_NAME, Status, MSG_SENTBOX_ID, pMsgInfo->msgId);
90         } else {
91                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
92                                         MSGFW_MESSAGE_TABLE_NAME, Status, pMsgInfo->msgId);
93         }
94
95         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
96                 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
97                 return MSG_ERR_DB_EXEC;
98         }
99
100         MSG_END();
101
102         return MSG_SUCCESS;
103 }
104
105
106 MSG_ERROR_T SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo)
107 {
108         MSG_ERROR_T err = MSG_SUCCESS;
109
110         unsigned int msgId = 0;
111         unsigned int addrId = 0;
112         unsigned int simId = 0;
113
114         char sqlQuery[MAX_QUERY_LEN+1];
115
116         dbHandle.beginTrans();
117
118         err = MsgStoAddAddress(&dbHandle, &(pSimMsgInfo->addressList[0]), &addrId);
119
120         if (err != MSG_SUCCESS) {
121                 dbHandle.endTrans(false);
122                 return err;
123         }
124
125         err = dbHandle.getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
126
127         if (err != MSG_SUCCESS) {
128                 dbHandle.endTrans(false);
129                 return err;
130         }
131
132         simId = pSimMsgInfo->msgId;
133         pSimMsgInfo->msgId = (MSG_MESSAGE_ID_T)msgId;
134
135         SMS_CONCAT_SIM_MSG_S concatSimMsg = {0};
136
137         /** Get Data from Concat SIM Msg */
138         if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
139
140                 int fileSize = 0;
141
142                 char* pFileData = NULL;
143                 AutoPtr<char> buf(&pFileData);
144
145                 if (MsgOpenAndReadFile(pSimMsgInfo->msgData, &pFileData, &fileSize) == false) {
146                         dbHandle.endTrans(false);
147                         return MSG_ERR_STORAGE_ERROR;
148                 }
149
150
151                 memcpy(&concatSimMsg, (SMS_CONCAT_SIM_MSG_S*)pFileData, fileSize);
152
153                 /** Delete temporary file */
154                 MsgDeleteFile(pSimMsgInfo->msgData); /** ipc */
155
156                 MSG_DEBUG("SIM ID [%d], MSG DATA [%s]", concatSimMsg.simIdCnt, concatSimMsg.msgData);
157         }
158
159         /**  Add Message */
160         memset(sqlQuery, 0x00, sizeof(sqlQuery));
161
162         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, '', '', ?, %d, 0, %d, 0, 0);",
163                                 MSGFW_MESSAGE_TABLE_NAME, msgId, addrId, pSimMsgInfo->folderId, 0, pSimMsgInfo->storageId,
164                                 pSimMsgInfo->msgType.mainType, pSimMsgInfo->msgType.subType, pSimMsgInfo->displayTime, pSimMsgInfo->dataSize,
165                                 pSimMsgInfo->networkStatus, pSimMsgInfo->bRead, pSimMsgInfo->bProtected, pSimMsgInfo->priority,
166                                 pSimMsgInfo->direction, pSimMsgInfo->scheduledTime, pSimMsgInfo->bBackup, MSG_DELIVERY_REPORT_NONE, MSG_READ_REPORT_NONE);
167
168         MSG_DEBUG("QUERY : %s", sqlQuery);
169
170         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
171                 dbHandle.endTrans(false);
172                 return MSG_ERR_DB_PREPARE;
173         }
174
175         dbHandle.bindText(pSimMsgInfo->subject, 1);
176
177         if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false)
178                 dbHandle.bindText(concatSimMsg.msgData, 2);
179         else
180                 dbHandle.bindText(pSimMsgInfo->msgText, 2);
181
182         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
183                 dbHandle.endTrans(false);
184                 return MSG_ERR_DB_STEP;
185         }
186
187         dbHandle.finalizeQuery();
188
189         /** Insert to Sim table */
190         if (pSimMsgInfo->msgType.subType == MSG_CONCAT_SIM_SMS && pSimMsgInfo->bTextSms == false) {
191
192                 MSG_DEBUG("sim count : %d", concatSimMsg.simIdCnt);
193
194                 for (unsigned int i = 0; i < concatSimMsg.simIdCnt; i++) {
195                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
196                         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
197                                                 MSGFW_SIM_MSG_TABLE_NAME, msgId, concatSimMsg.simIdList[i]);
198
199                         MSG_DEBUG("QUERY : %s", sqlQuery);
200
201                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
202                                 dbHandle.endTrans(false);
203                                 return MSG_ERR_DB_EXEC;
204                         }
205                 }
206         } else {
207                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
208                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
209                                         MSGFW_SIM_MSG_TABLE_NAME, msgId, simId);
210
211                 MSG_DEBUG("QUERY : %s", sqlQuery);
212
213                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
214                         dbHandle.endTrans(false);
215                         return MSG_ERR_DB_EXEC;
216                 }
217         }
218
219         /**  Update Address Info. */
220         if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
221                 dbHandle.endTrans(false);
222                 return MSG_ERR_STORAGE_ERROR;
223         }
224
225         dbHandle.endTrans(true);
226
227         return err;
228 }
229
230
231 MSG_ERROR_T SmsPluginStorage::addMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
232 {
233         MSG_BEGIN();
234
235         MSG_ERROR_T err = MSG_SUCCESS;
236
237         /**  Check whether storage is full or not */
238         err = checkStorageStatus(pMsgInfo);
239
240         if (err != MSG_SUCCESS) {
241                 return err;
242         }
243
244         if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
245
246                 MSG_DEBUG("Add Normal SMS");
247
248                 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
249                         err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
250
251                         if (err == MSG_SUCCESS) {
252                                 MSG_DEBUG("Success to saveSimMessage.");
253                         } else {
254                                 MSG_DEBUG("Fail to saveSimMessage : [%d]", err);
255                         }
256                 } else {
257                         /** Class 0 Msg should be saved in hidden folder */
258                         if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
259                                 pMsgInfo->folderId = 0;
260                         }
261
262                         /**  Add into DB */
263                         err = addSmsMessage(pMsgInfo);
264                 }
265
266         } else if ((pMsgInfo->msgType.subType == MSG_CB_SMS) || (pMsgInfo->msgType.subType == MSG_JAVACB_SMS)) {
267                 MSG_DEBUG("Add CB Message");
268                 err = addCbMessage(pMsgInfo);
269         } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
270                 MSG_DEBUG("Add Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
271                 err = addReplaceTypeMsg(pMsgInfo);
272         } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
273                 MSG_DEBUG("Add MWI Message");
274                 err = addSmsMessage(pMsgInfo);
275         } else if ((pMsgInfo->msgType.subType == MSG_WAP_SI_SMS) || (pMsgInfo->msgType.subType == MSG_WAP_CO_SMS)) {
276                 MSG_DEBUG("Add WAP Push Message");
277                 switch (pMsgInfo->msgType.subType)
278                 {
279                         case MSG_WAP_SI_SMS:
280                         {
281                                 // save push message information
282                                 err = addWAPMessage(pMsgInfo);
283                         }
284                         break;
285
286                         case MSG_WAP_CO_SMS:
287                         {
288                                 err = handleCOWAPMessage(pMsgInfo);
289                         }
290                         break;
291                 }
292         } else if (pMsgInfo->msgType.subType == MSG_STATUS_REPORT_SMS) {
293                 MSG_DEBUG("Add Status Report");
294                 err = addSmsMessage(pMsgInfo);
295         }
296
297         if (err == MSG_SUCCESS) {
298                 MSG_DEBUG("Success to add message !!");
299         } else {
300                 MSG_DEBUG("fail to add message !! : [%d]", err);
301         }
302
303         return err;
304 }
305
306
307 MSG_ERROR_T SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
308 {
309         MSG_ERROR_T err = MSG_SUCCESS;
310
311         unsigned int rowId = 0;
312         unsigned int addrId = 0;
313
314         char sqlQuery[MAX_QUERY_LEN+1];
315
316         dbHandle.beginTrans();
317
318         if (pMsgInfo->nAddressCnt > 0) {
319
320                 err = MsgStoAddAddress(&dbHandle, &(pMsgInfo->addressList[0]), &addrId);
321
322                 if (err != MSG_SUCCESS) {
323                         dbHandle.endTrans(false);
324                         return err;
325                 }
326
327                 pMsgInfo->addressList[0].threadId = (MSG_THREAD_ID_T)addrId;
328         }
329
330         /**  Add Message Table */
331         rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo, addrId);
332
333         if (rowId <= 0) {
334                 dbHandle.endTrans(false);
335                 return MSG_ERR_DB_ROW;
336         }
337
338         /** Update Address table */
339         err = MsgStoUpdateAddress(&dbHandle, addrId);
340
341         if (err != MSG_SUCCESS) {
342                 dbHandle.endTrans(false);
343                 return err;
344         }
345
346         dbHandle.endTrans(true);
347
348         pMsgInfo->msgId = (MSG_MESSAGE_ID_T)rowId;
349         pMsgInfo->referenceId = (MSG_REFERENCE_ID_T)rowId;
350
351         MSG_END();
352
353         return MSG_SUCCESS;
354 }
355
356
357 MSG_ERROR_T SmsPluginStorage::updateSmsMessage(MSG_MESSAGE_INFO_S *pMsg)
358 {
359         MSG_ERROR_T err = MSG_SUCCESS;
360
361         char sqlQuery[MAX_QUERY_LEN+1];
362
363         unsigned int addrId = 0;
364
365         dbHandle.beginTrans();
366
367         if (pMsg->nAddressCnt > 0) {
368
369                 err = MsgStoAddAddress(&dbHandle, &(pMsg->addressList[0]), &addrId);
370
371                 if (err != MSG_SUCCESS) {
372                         dbHandle.endTrans(false);
373                         return err;
374                 }
375         }
376
377         int fileSize = 0;
378
379         char* pFileData = NULL;
380         AutoPtr<char> buf(&pFileData);
381
382         /**  Get File Data */
383         if (pMsg->bTextSms == false) {
384                 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
385                         dbHandle.endTrans(false);
386                         return MSG_ERR_STORAGE_ERROR;
387                 }
388         }
389
390         /**  Update Message */
391         memset(sqlQuery, 0x00, sizeof(sqlQuery));
392
393         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET ADDRESS_ID = %d, FOLDER_ID = %d, STORAGE_ID = %d, MAIN_TYPE = %d, SUB_TYPE = %d, \
394                                                                         DISPLAY_TIME = %lu, DATA_SIZE = %d, NETWORK_STATUS = %d, READ_STATUS = %d, \
395                                                                         PROTECTED = %d, PRIORITY = %d, MSG_DIRECTION = %d, SCHEDULED_TIME = %lu, BACKUP = %d, SUBJECT = ?, MSG_DATA = ?, THUMB_PATH = ?, MSG_TEXT = ? \
396                                                                         WHERE MSG_ID = %d;",
397                                 MSGFW_MESSAGE_TABLE_NAME, addrId, pMsg->folderId, pMsg->storageId, pMsg->msgType.mainType, pMsg->msgType.subType, pMsg->displayTime, pMsg->dataSize,
398                                 pMsg->networkStatus, pMsg->bRead, pMsg->bProtected, pMsg->priority, pMsg->direction, pMsg->scheduledTime, pMsg->bBackup, pMsg->msgId);
399
400         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
401                 dbHandle.endTrans(false);
402                 return MSG_ERR_DB_EXEC;
403         }
404
405         dbHandle.bindText(pMsg->subject, 1);
406
407         dbHandle.bindText(pMsg->msgData, 2);
408
409         dbHandle.bindText(pMsg->thumbPath, 3);
410
411         if (pMsg->msgType.mainType == MSG_SMS_TYPE && pMsg->bTextSms == false)
412                 dbHandle.bindText(pFileData, 4);
413         else
414                 dbHandle.bindText(pMsg->msgText, 4);
415
416         MSG_DEBUG("%s", sqlQuery);
417
418         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
419                 dbHandle.endTrans(false);
420                 return MSG_ERR_DB_EXEC;
421         }
422
423         dbHandle.finalizeQuery();
424
425         err = MsgStoUpdateAddress(&dbHandle, addrId);
426
427         if (err != MSG_SUCCESS) {
428                 dbHandle.endTrans(false);
429                 return MSG_ERR_STORAGE_ERROR;
430         }
431
432         err = MsgStoClearAddressTable(&dbHandle);
433
434         if (err != MSG_SUCCESS) {
435                 dbHandle.endTrans(false);
436                 return MSG_ERR_STORAGE_ERROR;
437         }
438
439         dbHandle.endTrans(true);
440
441         return MSG_SUCCESS;
442 }
443
444
445 MSG_ERROR_T SmsPluginStorage::deleteSmsMessage(MSG_MESSAGE_ID_T MsgId)
446 {
447         MSG_BEGIN();
448
449         MSG_ERROR_T err = MSG_SUCCESS;
450
451         char sqlQuery[MAX_QUERY_LEN+1];
452
453          /**  Get SUB_TYPE, STORAGE_ID */
454         memset(sqlQuery, 0x00, sizeof(sqlQuery));
455         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, ADDRESS_ID \
456                                         FROM %s WHERE MSG_ID = %d;",
457                         MSGFW_MESSAGE_TABLE_NAME, MsgId);
458
459         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
460                 return MSG_ERR_DB_PREPARE;
461
462         MSG_MESSAGE_TYPE_S msgType;
463         MSG_FOLDER_ID_T folderId;
464
465         unsigned int addrId;
466
467         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
468                 msgType.mainType = dbHandle.columnInt(0);
469                 msgType.subType = dbHandle.columnInt(1);
470                 folderId = dbHandle.columnInt(2);
471                 addrId = dbHandle.columnInt(3);
472
473                 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] AddressId:[%d]", msgType.mainType, msgType.subType, folderId, addrId);
474         } else {
475                 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
476                 dbHandle.finalizeQuery();
477                 return MSG_ERR_DB_STEP;
478         }
479
480         dbHandle.finalizeQuery();
481
482         dbHandle.beginTrans();
483
484         memset(sqlQuery, 0x00, sizeof(sqlQuery));
485         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SCHEDULED_MSG_TABLE_NAME, MsgId);
486
487         /** Delete Message from scheduled msg table */
488         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
489                 dbHandle.endTrans(false);
490                 return MSG_ERR_DB_EXEC;
491         }
492
493         memset(sqlQuery, 0x00, sizeof(sqlQuery));
494         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, MsgId);
495
496         /**  Delete SMS Send Option */
497         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
498                 dbHandle.endTrans(false);
499                 return MSG_ERR_DB_EXEC;
500         }
501
502         if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
503                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
504                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, MsgId);
505
506                 /** Delete Push Message from push table */
507                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
508                         dbHandle.endTrans(false);
509                         return MSG_ERR_DB_EXEC;
510                 }
511         } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
512                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
513                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, MsgId);
514
515                 /**  Delete Push Message from push table */
516                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
517                         dbHandle.endTrans(false);
518                         return MSG_ERR_DB_EXEC;
519                 }
520         } else if (msgType.subType == MSG_SYNCML_CP) {
521                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
522                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, MsgId);
523
524                 /**  Delete SyncML Message from syncML table */
525                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
526                         dbHandle.endTrans(false);
527                         return MSG_ERR_DB_EXEC;
528                 }
529         }
530
531         memset(sqlQuery, 0x00, sizeof(sqlQuery));
532         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, MsgId);
533
534         /** Delete Message from msg table */
535         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
536                 dbHandle.endTrans(false);
537                 return MSG_ERR_DB_EXEC;
538         }
539
540         /**  Clear Address table */
541         if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS) {
542                 dbHandle.endTrans(false);
543                 return MSG_ERR_DB_EXEC;
544         }
545
546         /**  Update Address Info.*/
547         if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
548                 dbHandle.endTrans(false);
549                 return MSG_ERR_STORAGE_ERROR;
550         }
551
552         dbHandle.endTrans(true);
553
554         if (folderId == MSG_INBOX_ID) {
555                 msgType.classType = MSG_CLASS_NONE;
556
557                 /**  Set memory status in SIM */
558                 if (MsgStoCheckMsgCntFull(&dbHandle, &msgType, folderId) == MSG_SUCCESS) {
559                         MSG_DEBUG("Set Memory Status");
560                         SmsPlgSetMemoryStatus(MSG_SUCCESS);
561                 }
562         }
563
564         int smsCnt = 0, mmsCnt = 0;
565
566         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
567         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
568
569         MsgSettingHandleNewMsg(smsCnt, mmsCnt);
570         MsgDeleteNotiByMsgId(MsgId);
571
572         return MSG_SUCCESS;
573 }
574
575
576 MSG_ERROR_T SmsPluginStorage::addCbMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
577 {
578         MSG_ERROR_T err = MSG_SUCCESS;
579
580         unsigned int rowId = 0, addrId = 0;
581
582         char sqlQuery[MAX_QUERY_LEN+1];
583
584         dbHandle.beginTrans();
585
586         if (pMsgInfo->nAddressCnt > 0) {
587                 err = MsgStoAddAddress(&dbHandle, &(pMsgInfo->addressList[0]), &addrId);
588
589                 if (err != MSG_SUCCESS) {
590                         dbHandle.endTrans(false);
591                         return err;
592                 }
593
594                 pMsgInfo->addressList[0].threadId = (MSG_THREAD_ID_T)addrId;
595         }
596
597         /**  Add Message Table */
598         rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo, addrId);
599
600         if (rowId <= 0) {
601                 dbHandle.endTrans(false);
602                 return MSG_ERR_DB_ROW;
603         }
604
605         /**  Get CB Msg ID */
606         unsigned short cbMsgId = (unsigned short)pMsgInfo->msgId;
607
608         /** Add CB Msg in MSG_CBMSG_TABLE */
609         memset(sqlQuery, 0x00, sizeof(sqlQuery));
610
611         sprintf(sqlQuery, "INSERT INTO %s VALUES (%d, %d);",
612                                 MSGFW_CB_MSG_TABLE_NAME, rowId, cbMsgId);
613
614         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
615                 dbHandle.endTrans(false);
616                 return MSG_ERR_DB_EXEC;
617         }
618
619         /**  Update Address Info. */
620         if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
621                 dbHandle.endTrans(false);
622                 return MSG_ERR_STORAGE_ERROR;
623         }
624
625         dbHandle.endTrans(true);
626
627         /** Assign Msg ID */
628         pMsgInfo->msgId = (MSG_MESSAGE_ID_T)rowId;
629         pMsgInfo->referenceId = (MSG_REFERENCE_ID_T)rowId;
630
631         return err;
632 }
633
634
635 MSG_ERROR_T SmsPluginStorage::addReplaceTypeMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
636 {
637         MSG_ERROR_T err = MSG_SUCCESS;
638
639         char sqlQuery[MAX_QUERY_LEN+1];
640
641         unsigned int addrId = 0, retCnt = 0;
642
643         /** Check if new address or not */
644         if (MsgExistAddress(&dbHandle, pMsgInfo->addressList[0].addressVal, &addrId) == true) {
645                 MSG_DEBUG("Address Info. exists [%d] [%s]", addrId, pMsgInfo->addressList[0].addressVal);
646
647                 /**  Find Replace Type Msg : Same Replace Type, Same Origin Address */
648                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
649                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*), A.MSG_ID FROM %s A, %s B \
650                                              WHERE A.ADDRESS_ID = B.ADDRESS_ID AND A.SUB_TYPE = %d AND B.ADDRESS_ID = %d;",
651                                 MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, pMsgInfo->msgType.subType, addrId);
652
653                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
654                         return MSG_ERR_DB_PREPARE;
655
656                 if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
657                         retCnt = dbHandle.columnInt(0);
658                         pMsgInfo->msgId = dbHandle.columnInt(1);
659                 } else {
660                         dbHandle.finalizeQuery();
661                         return MSG_ERR_DB_STEP;
662                 }
663
664                 dbHandle.finalizeQuery();
665         }
666
667         /** Update New Replace Type Msg */
668         if (retCnt == 1) {
669                 MSG_DEBUG("Update Replace Type Msg");
670                 err = updateSmsMessage(pMsgInfo);
671         } else if (retCnt == 0) { /** Insert New Replace Type Msg */
672                 MSG_DEBUG("Insert Replace Type Msg");
673                 err = addSmsMessage(pMsgInfo);
674         }
675
676         return err;
677 }
678
679
680 MSG_ERROR_T SmsPluginStorage::addWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
681 {
682         MSG_ERROR_T err = MSG_SUCCESS;
683
684         MSG_PUSH_MESSAGE_S pushMsg = {};
685
686         char sqlQuery[MAX_QUERY_LEN+1];
687         memset(sqlQuery, 0x00, sizeof(sqlQuery));
688
689         int fileSize = 0;
690
691         char* pFileData = NULL;
692         AutoPtr<char> buf(&pFileData);
693
694         if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
695                 return MSG_ERR_STORAGE_ERROR;
696
697         MSG_DEBUG("fileSize : %d", fileSize);
698
699         memcpy(&pushMsg, pFileData, fileSize);
700
701         /** Delete temporary file */
702         MsgDeleteFile(pMsgInfo->msgData);
703
704         /** check pPushMsg data */
705
706         MSG_DEBUG("check pushMsg data");
707         MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
708         MSG_DEBUG("pushMsg.received : [%d]", pushMsg.received);
709         MSG_DEBUG("pushMsg.created : [%d]", pushMsg.created);
710         MSG_DEBUG("pushMsg.expires : [%d]", pushMsg.expires);
711         MSG_DEBUG("pushMsg.id : [%s]", pushMsg.id);
712         MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
713         MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
714
715         bool bProceed = true;
716
717         /**  check validation of contents */
718         if (checkPushMsgValidation(&pushMsg, &bProceed) != MSG_SUCCESS) {
719                 MSG_DEBUG("Fail to check Push Message validation.");
720         }
721
722         /**  if validation check value is false */
723         /** return and drop message. */
724         if (bProceed == false)
725                 return MSG_ERR_INVALID_MESSAGE;
726
727         /**  update subject */
728         int len = strlen(pushMsg.contents);
729
730         if (len > MAX_SUBJECT_LEN) {
731                 memcpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
732                 pMsgInfo->subject[MAX_SUBJECT_LEN] = '\0';
733         } else {
734                 strncpy(pMsgInfo->subject, pushMsg.contents, MAX_SUBJECT_LEN);
735         }
736
737         /**  Update Msg Text - remove */
738         strncpy(pMsgInfo->msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
739         pMsgInfo->dataSize = strlen(pMsgInfo->msgText);
740
741         pMsgInfo->bTextSms = true;
742         pMsgInfo->folderId = MSG_INBOX_ID;
743         pMsgInfo->storageId = MSG_STORAGE_PHONE;
744
745         unsigned int addrId = 0;
746
747         dbHandle.beginTrans();
748
749         if (pMsgInfo->nAddressCnt > 0) {
750
751                 err = MsgStoAddAddress(&dbHandle, &(pMsgInfo->addressList[0]), &addrId);
752
753                 if (err != MSG_SUCCESS) {
754                         dbHandle.endTrans(false);
755                         return err;
756                 }
757         }
758
759         /**  get last row count for Message id */
760         unsigned int rowId = 0;
761
762         /** Add Message Table */
763         rowId = MsgStoAddMessageTable(&dbHandle, pMsgInfo, addrId);
764
765         if (rowId <= 0) {
766                 dbHandle.endTrans(false);
767                 return MSG_ERR_DB_ROW;
768         }
769
770         /**  add msg_push_table */
771         snprintf (sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %lu, %lu, ?, ?, ?)",
772                                     MSGFW_PUSH_MSG_TABLE_NAME, pMsgInfo->msgId, pushMsg.action, pushMsg.created, pushMsg.expires);
773
774         if ((err = dbHandle.prepareQuery(sqlQuery)) != MSG_SUCCESS) {
775                 dbHandle.endTrans(false);
776                 return err;
777         }
778
779         dbHandle.bindText(pushMsg.id, 1);
780
781         dbHandle.bindText(pushMsg.href, 2);
782
783         dbHandle.bindText(pushMsg.contents, 3);
784
785         if ((err = dbHandle.stepQuery()) != MSG_ERR_DB_DONE) {
786                 dbHandle.endTrans(false);
787                 return err;
788         }
789
790         /** Update Address Info. */
791         if (MsgStoUpdateAddress(&dbHandle, addrId) != MSG_SUCCESS) {
792                 dbHandle.endTrans(false);
793                 return MSG_ERR_STORAGE_ERROR;
794         }
795
796         dbHandle.endTrans(true);
797
798         pMsgInfo->msgId = (MSG_MESSAGE_ID_T)rowId;
799         pMsgInfo->referenceId = (MSG_REFERENCE_ID_T)rowId;
800
801         return MSG_SUCCESS;
802 }
803
804
805 MSG_ERROR_T SmsPluginStorage::handleCOWAPMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
806 {
807         MSG_ERROR_T err = MSG_SUCCESS;
808
809         char href[MAX_PUSH_CACHEOP_MAX_URL_LEN+1];
810         char sqlQuery[MAX_QUERY_LEN+1];
811
812         int fileSize = 0;
813
814         char* pFileData = NULL;
815         AutoPtr<char> buf(&pFileData);
816
817         if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
818                 return MSG_ERR_STORAGE_ERROR;
819
820         MSG_PUSH_CACHEOP_S *pPushMsg;
821
822         pPushMsg = (MSG_PUSH_CACHEOP_S*)pFileData;
823
824         for (int i = 0; i < pPushMsg->invalObjectCnt; i++) {
825
826                 int msgid = -1;
827
828                 memset(href, 0x00, sizeof(href));
829                 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
830
831                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
832                 snprintf (sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%';",
833                                         MSGFW_PUSH_MSG_TABLE_NAME, href);
834
835                 dbHandle.beginTrans();
836
837                 err = dbHandle.prepareQuery(sqlQuery);
838
839                 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
840
841                         msgid = dbHandle.getColumnToInt(1);
842
843                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
844                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID= %d;",
845                                         MSGFW_PUSH_MSG_TABLE_NAME, msgid);
846
847                         /** Delete Message from Push table */
848                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
849                                 dbHandle.finalizeQuery();
850                                 dbHandle.endTrans(false);
851                                 continue;
852                         }
853
854                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
855                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;",
856                                                 MSGFW_MESSAGE_TABLE_NAME, msgid);
857
858                         /** Delete Message from msg table */
859                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
860                                 dbHandle.finalizeQuery();
861                                 dbHandle.endTrans(false);
862                                 continue;
863                         }
864
865                         /** Update all Address */
866                         if (updateAllAddress() != MSG_SUCCESS) {
867                                 dbHandle.finalizeQuery();
868                                 dbHandle.endTrans(false);
869                                 continue;
870                         }
871
872                         /** Clear Address table */
873                         if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS) {
874                                 dbHandle.finalizeQuery();
875                                 dbHandle.endTrans(false);
876                                 continue;
877                         }
878                 }
879
880                 dbHandle.finalizeQuery();
881
882                 dbHandle.endTrans(true);
883         }
884
885         for (int i = 0; i < pPushMsg->invalServiceCnt; i++) {
886
887                 int msgid = -1;
888
889                 memset(href, 0x00, sizeof(href));
890                 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
891
892                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
893                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%'",
894                                         MSGFW_PUSH_MSG_TABLE_NAME, href);
895
896                 dbHandle.beginTrans();
897
898                 err = dbHandle.prepareQuery(sqlQuery);
899
900                 if ((dbHandle.stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
901
902                         msgid = dbHandle.getColumnToInt(1);
903
904                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
905                         sprintf(sqlQuery, "DELETE FROM %s WHERE MSG_ID='%d'",
906                                                 MSGFW_PUSH_MSG_TABLE_NAME, msgid);
907
908                         /** Delete Message from Push table */
909                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
910                                 dbHandle.finalizeQuery();
911                                 dbHandle.endTrans(false);
912                                 continue;
913                         }
914
915                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
916                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;",
917                                                 MSGFW_MESSAGE_TABLE_NAME, msgid);
918
919                         /** Delete Message from msg table */
920                         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
921                                 dbHandle.finalizeQuery();
922                                 dbHandle.endTrans(false);
923                                 continue;
924                         }
925
926                         /**  Update all Address */
927                         if (updateAllAddress() != MSG_SUCCESS) {
928                                 dbHandle.finalizeQuery();
929                                 dbHandle.endTrans(false);
930                                 continue;
931                         }
932
933                         /** Clear Address table */
934                         if (MsgStoClearAddressTable(&dbHandle) != MSG_SUCCESS) {
935                                 dbHandle.finalizeQuery();
936                                 dbHandle.endTrans(false);
937                                 continue;
938                         }
939                 }
940
941                 dbHandle.finalizeQuery();
942
943                 dbHandle.endTrans(true);
944         }
945
946         /** delete temporary file */
947         MsgDeleteFile(pMsgInfo->msgData);
948
949         return MSG_SUCCESS;
950 }
951
952
953 MSG_ERROR_T SmsPluginStorage::checkPushMsgValidation(MSG_PUSH_MESSAGE_S *pPushMsg, bool *pbProceed)
954 {
955         MSG_ERROR_T err = MSG_SUCCESS;
956
957         unsigned long oldExpireTime = 0;
958         int rowCnt = 0;
959
960         char sqlQuery[MAX_QUERY_LEN+1];
961
962         /**  is push message is expired?? */
963         if (pPushMsg->received > pPushMsg->expires) {
964                 MSG_DEBUG("Push Message is expired.");
965                 pbProceed = false;
966                 return err;
967         }
968
969
970         if (pPushMsg->action == MSG_PUSH_SL_ACTION_EXECUTE_LOW) {
971                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
972                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s' AND ACTION = %d",
973                                         MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id, pPushMsg->action);
974         } else {
975                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s'",
976                                         MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id);
977         }
978
979         err = dbHandle.getTable(sqlQuery, &rowCnt);
980
981         if (rowCnt < 1) {
982                 dbHandle.freeTable();
983                 return MSG_SUCCESS;
984         }
985
986         oldExpireTime = dbHandle.getColumnToInt(1);
987
988         dbHandle.freeTable();
989
990         if (pPushMsg->created < oldExpireTime) {
991                 MSG_DEBUG("Push Message is expired.");
992                 pbProceed = false;
993                 return err;
994         }
995
996         return err;
997 }
998
999
1000 MSG_ERROR_T SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
1001 {
1002         MSG_ERROR_T err = MSG_SUCCESS;
1003
1004         err = MsgStoCheckMsgCntFull(&dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
1005
1006         if (err != MSG_SUCCESS) {
1007
1008                 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
1009                         bool bAutoErase = false;
1010
1011                         MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
1012
1013                         MSG_DEBUG("bAutoErase: %d", bAutoErase);
1014
1015                         if (bAutoErase == true) {
1016                                 MSG_MESSAGE_ID_T msgId;
1017
1018                                 /** Find the oldest message's msgId */
1019                                 err = MsgStoGetOldestMessage(&dbHandle, pMsgInfo, &msgId);
1020
1021                                 if (err != MSG_SUCCESS)
1022                                         return err;
1023
1024                                 /** Delete the corresponding message. */
1025                                 err = deleteSmsMessage(msgId);
1026                         }
1027                 }
1028
1029                 return err;
1030         }
1031
1032         return err;
1033 }
1034
1035
1036 MSG_ERROR_T SmsPluginStorage::updateAllAddress()
1037 {
1038         MSG_ERROR_T err = MSG_SUCCESS;
1039
1040         int rowCnt = 0, index = 1;
1041         char sqlQuery[MAX_QUERY_LEN+1];
1042
1043         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1044
1045         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s",
1046                                 MSGFW_ADDRESS_TABLE_NAME);
1047
1048         err = dbHandle.getTable(sqlQuery, &rowCnt);
1049
1050         if (err == MSG_ERR_DB_NORECORD) {
1051                 dbHandle.freeTable();
1052                 return MSG_SUCCESS;
1053         } else if ( err != MSG_SUCCESS) {
1054                 dbHandle.freeTable();
1055                 return err;
1056         }
1057
1058
1059         for (int i = 0; i < rowCnt; i++) {
1060
1061                 err = MsgStoUpdateAddress(&dbHandle, index++);
1062
1063                 if (err != MSG_SUCCESS)
1064                         break;
1065         }
1066
1067         dbHandle.freeTable();
1068
1069         return err;
1070 }