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