Sync with tizen 2.4
[platform/core/messaging/msg-service.git] / framework / storage-handler / MsgStorageUtil.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <queue>
21
22 #include "MsgDebug.h"
23 #include "MsgCppTypes.h"
24 #include "MsgUtilFile.h"
25 #include "MsgContact.h"
26 #include "MsgSoundPlayer.h"
27 #include "MsgGconfWrapper.h"
28 #include "MsgSqliteWrapper.h"
29 #include "MsgPluginManager.h"
30 #include "MsgUtilStorage.h"
31 #include "MsgStorageHandler.h"
32
33 using namespace std;
34
35 /*==================================================================================================
36                                      VARIABLES
37 ==================================================================================================*/
38
39
40 /*==================================================================================================
41                                      FUNCTION IMPLEMENTATION
42 ==================================================================================================*/
43 msg_error_t MsgStoGetSmsSendOpt(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S* pSendOpt)
44 {
45         MsgDbHandler *dbHandle = getDbHandle();
46         char sqlQuery[MAX_QUERY_LEN+1];
47
48         memset(sqlQuery, 0x00, sizeof(sqlQuery));
49
50         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DELREP_REQ, KEEP_COPY, REPLY_PATH, ENCODE_TYPE FROM %s WHERE MSG_ID = %d;",
51                         MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId);
52
53         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
54                 return MSG_ERR_DB_PREPARE;
55
56         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
57                 pSendOpt->bSetting = true;
58                 pSendOpt->bDeliverReq = (bool)dbHandle->columnInt(0);
59                 pSendOpt->bKeepCopy = (bool)dbHandle->columnInt(1);
60                 pSendOpt->option.smsSendOptInfo.bReplyPath = (bool)dbHandle->columnInt(2);
61                 pMsg->encodeType = dbHandle->columnInt(3);
62         } else {
63                 dbHandle->finalizeQuery();
64                 return MSG_ERR_DB_STEP;
65         }
66
67         dbHandle->finalizeQuery();
68
69         return MSG_SUCCESS;
70 }
71
72
73 msg_error_t MsgStoGetMmsSendOpt(msg_message_id_t msgId, MSG_SENDINGOPT_INFO_S* pSendOpt)
74 {
75         MsgDbHandler *dbHandle = getDbHandle();
76         char sqlQuery[MAX_QUERY_LEN+1];
77
78         memset(sqlQuery, 0x00, sizeof(sqlQuery));
79
80         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ASK_DELIVERY_REPORT, KEEP_COPY, ASK_READ_REPLY, EXPIRY_TIME, PRIORITY FROM %s WHERE MSG_ID = %d;",
81                         MMS_PLUGIN_MESSAGE_TABLE_NAME, msgId);
82
83         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
84                 return MSG_ERR_DB_PREPARE;
85
86         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
87                 pSendOpt->bSetting = true;
88                 pSendOpt->bDeliverReq = (bool)dbHandle->columnInt(0);
89                 pSendOpt->bKeepCopy = (bool)dbHandle->columnInt(1);
90                 pSendOpt->option.mmsSendOptInfo.bReadReq = (bool)dbHandle->columnInt(2);
91                 pSendOpt->option.mmsSendOptInfo.expiryTime.time = (unsigned int)dbHandle->columnInt(3);
92                 pSendOpt->option.mmsSendOptInfo.priority = (msg_priority_type_t)dbHandle->columnInt(4);
93         } else {
94                 dbHandle->finalizeQuery();
95                 return MSG_ERR_DB_STEP;
96         }
97
98         dbHandle->finalizeQuery();
99
100         return MSG_SUCCESS;
101 }
102
103
104 bool MsgStoCheckSyncMLMsgInThread(msg_thread_id_t threadId)
105 {
106         int rowCnt = 0;
107         bool isSyncMLMsg = false;
108         MsgDbHandler *dbHandle = getDbHandle();
109         char sqlQuery[MAX_QUERY_LEN+1];
110
111         memset(sqlQuery, 0x00, sizeof(sqlQuery));
112
113         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE SUB_TYPE = %d AND CONV_ID = %d;",
114                         MSGFW_MESSAGE_TABLE_NAME, MSG_SYNCML_CP, threadId);
115
116         if (dbHandle->getTable(sqlQuery, &rowCnt) != MSG_SUCCESS) {
117                 MSG_DEBUG("getTable is failed!!!");
118         }
119
120         if (rowCnt > 0) isSyncMLMsg = true;
121
122         MSG_DEBUG("rowCnt [%d]", rowCnt);
123
124         dbHandle->freeTable();
125
126         return isSyncMLMsg;
127 }
128
129
130 msg_error_t MsgStoResetNetworkStatus()
131 {
132         MSG_BEGIN();
133         MsgDbHandler *dbHandle = getDbHandle();
134         char sqlQuery[MAX_QUERY_LEN+1];
135
136         memset(sqlQuery, 0x00, sizeof(sqlQuery));
137
138         snprintf(sqlQuery, sizeof(sqlQuery),
139                         "UPDATE %s SET NETWORK_STATUS = %d WHERE NETWORK_STATUS = %d;",
140                         MSGFW_MESSAGE_TABLE_NAME, MSG_NETWORK_SEND_FAIL, MSG_NETWORK_SENDING);
141
142         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
143                 //continue reset process for MSG_NETWORK_RETRIEVE_FAIL case
144                 MSG_INFO("execQuery is failed");
145         }
146
147         memset(sqlQuery, 0x00, sizeof(sqlQuery));
148
149         snprintf(sqlQuery, sizeof(sqlQuery),
150                         "UPDATE %s SET NETWORK_STATUS = %d WHERE NETWORK_STATUS = %d;",
151                         MSGFW_MESSAGE_TABLE_NAME, MSG_NETWORK_RETRIEVE_FAIL, MSG_NETWORK_RETRIEVING);
152
153         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
154                 return MSG_ERR_DB_EXEC;
155
156         MSG_END();
157
158         return MSG_SUCCESS;
159 }
160
161 #if 0
162 msg_error_t MsgStoResetCBMessage()
163 {
164         MSG_BEGIN();
165         char sqlQuery[MAX_QUERY_LEN+1];
166
167         memset(sqlQuery, 0x00, sizeof(sqlQuery));
168
169         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s",
170                         MSGFW_RECEIVED_CB_MSG_TABLE_NAME);
171
172         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
173                 MSG_DEBUG("Delete Received CB Msg Fail!!");
174                 return MSG_ERR_DB_EXEC;
175         }
176
177         MSG_END();
178         return MSG_SUCCESS;
179 }
180 #endif
181
182
183 msg_error_t MsgStoCleanAbnormalMmsData()
184 {
185         MSG_BEGIN();
186
187         int rowCnt = 0, index = 2; // numbers of index
188         MsgDbHandler *dbHandle = getDbHandle();
189         msg_message_id_t msgId;
190
191         char sqlQuery[MAX_QUERY_LEN+1];
192         char    filePath[MSG_FILEPATH_LEN_MAX];
193
194         memset(sqlQuery, 0x00, sizeof(sqlQuery));
195
196         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MSG_ID, A.FILE_PATH FROM %s A, %s B WHERE A.MSG_ID = B.MSG_ID AND (B.SUB_TYPE = %d OR B.SUB_TYPE = %d OR B.SUB_TYPE = %d);",
197                         MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MSG_SENDCONF_MMS, MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS);
198
199         msg_error_t  err = dbHandle->getTable(sqlQuery, &rowCnt);
200
201         if (err == MSG_ERR_DB_NORECORD) {
202                 dbHandle->freeTable();
203                 return MSG_SUCCESS;
204         } else if (err != MSG_SUCCESS) {
205                 MSG_DEBUG("Fail to getTable().");
206                 dbHandle->freeTable();
207                 return err;
208         }
209
210
211         for (int i = 0; i < rowCnt; i++)
212         {
213                 memset(filePath, 0x00, sizeof(filePath));
214
215                 msgId = dbHandle->getColumnToInt(index++);
216
217                 dbHandle->getColumnToString(index++, MSG_FILEPATH_LEN_MAX, filePath);
218
219                 if(strlen(filePath) > 1) {
220                         MSG_DEBUG("strlen(filePath) [%d]", strlen(filePath));
221                         MSG_SEC_DEBUG("filePath [%s]", filePath);
222
223                         if(MsgGetFileSize(filePath) < 0) {
224                                 // abnormal mms message
225                                 MSG_DEBUG("abnormal mms message [%d]", msgId);
226
227                                 // delete mms message
228                                 MsgStoDeleteMessage(msgId, false);
229                         }
230                 }
231
232         }
233         dbHandle->freeTable();
234
235         MSG_END();
236
237         return MSG_SUCCESS;
238 }
239
240
241 msg_error_t MsgStoCheckReadReportStatus(msg_message_id_t msgId)
242 {
243         MSG_BEGIN();
244
245         bool    bReadReportRequested;
246         bool    bReadReportIsSent;
247         MsgDbHandler *dbHandle = getDbHandle();
248         bReadReportRequested = MsgStoCheckReadReportRequested(dbHandle, msgId);
249         if(bReadReportRequested == false)
250                 return MSG_ERR_READREPORT_NOT_REQUESTED;
251
252         bReadReportIsSent = MsgStoCheckReadReportIsSent(dbHandle, msgId);
253         if(bReadReportIsSent == true)
254                 return MSG_ERR_READREPORT_ALEADY_SENT;
255
256         MSG_END();
257
258         return MSG_SUCCESS;
259 }
260
261
262 msg_error_t MsgStoAutoDeleteConversation(msg_thread_id_t threadId, msg_id_list_s *msgIdList)
263 {
264         MSG_BEGIN();
265         MsgDbHandler *dbHandle = getDbHandle();
266         msg_error_t err = MSG_SUCCESS;
267
268         bool bAutoErase = false;
269         MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
270
271         if (bAutoErase) {
272
273                 MSG_DEBUG("threadId [%d]", threadId);
274
275                 //msg_id_list_s msgIdList;
276                 int currentSmsCnt = 0;
277                 int currentMmsCnt = 0;
278                 int limitSmsCnt = 0;
279                 int limitMmsCnt = 0;
280
281                 char sqlQuery[MAX_QUERY_LEN+1];
282
283                 int rowCnt = 0;
284                 unsigned int index = 1;
285
286                 //memset(msgIdList, 0x00, sizeof(msg_id_list_s));
287
288                 // Get current count of messages
289                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
290
291 #ifdef MSG_NOTI_INTEGRATION
292                 snprintf(sqlQuery, sizeof(sqlQuery),
293                                 "SELECT COUNT(MSG_ID) FROM %s "
294                                 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND MAIN_TYPE = %d "
295                                 "UNION ALL "
296                                 "SELECT COUNT(MSG_ID) FROM %s "
297                                 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d);",
298                                 MSGFW_MESSAGE_TABLE_NAME,
299                                 threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE, MSG_SMS_TYPE,
300                                 MSGFW_MESSAGE_TABLE_NAME,
301                                 threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE, MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS);
302 #else
303                 snprintf(sqlQuery, sizeof(sqlQuery),
304                                 "SELECT COUNT(MSG_ID) FROM %s "
305                                 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND MAIN_TYPE = %d "
306                                 "UNION ALL "
307                                 "SELECT COUNT(MSG_ID) FROM %s "
308                                 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d);",
309                                 MSGFW_MESSAGE_TABLE_NAME,
310                                 threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE, MSG_SMS_TYPE,
311                                 MSGFW_MESSAGE_TABLE_NAME,
312                                 threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE, MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS);
313 #endif
314
315                 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
316                         MSG_DEBUG("sqlQuery [%s]", sqlQuery);
317                         return MSG_ERR_DB_PREPARE;
318                 }
319
320                 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
321                         currentSmsCnt = dbHandle->columnInt(0);
322                 } else {
323                         dbHandle->finalizeQuery();
324                         return MSG_ERR_DB_STEP;
325                 }
326
327                 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
328                         currentMmsCnt = dbHandle->columnInt(0);
329                 } else {
330                         dbHandle->finalizeQuery();
331                         return MSG_ERR_DB_STEP;
332                 }
333
334                 dbHandle->finalizeQuery();
335
336                 MSG_DEBUG("currentSmsCnt [%d], currentMmsCnt [%d]", currentSmsCnt, currentMmsCnt);
337
338                 if (currentSmsCnt > 0 || currentMmsCnt > 0) {
339                         limitSmsCnt = MsgSettingGetInt(MSG_SMS_LIMIT);
340                         limitMmsCnt = MsgSettingGetInt(MSG_MMS_LIMIT);
341
342                         if (limitSmsCnt < 0 || limitMmsCnt < 0) {
343                                 MSG_DEBUG("limitSmsCnt [%d], limitMmsCnt [%d]", limitSmsCnt, limitMmsCnt);
344                                 return MSG_SUCCESS;
345                         }
346                 } else { // !(currentSmsCnt > 0 || currentMmsCnt > 0)
347                         return MSG_SUCCESS;
348                 }
349
350                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
351
352 #ifdef MSG_NOTI_INTEGRATION
353                 snprintf(sqlQuery, sizeof(sqlQuery),
354                                 "SELECT DISTINCT(MSG_ID) FROM %s "
355                                 "WHERE MSG_ID IN "
356                                 "(SELECT MSG_ID FROM %s "
357                                 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND PROTECTED = 0 "
358                                 "AND MAIN_TYPE = %d ORDER BY MSG_ID ASC LIMIT %d) "
359                                 "OR MSG_ID IN "
360                                 "(SELECT MSG_ID FROM %s "
361                                 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND PROTECTED = 0 "
362                                 "AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d) ORDER BY MSG_ID ASC LIMIT %d);",
363                                 MSGFW_MESSAGE_TABLE_NAME,
364                                 MSGFW_MESSAGE_TABLE_NAME,
365                                 threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE,
366                                 MSG_SMS_TYPE, ((currentSmsCnt-limitSmsCnt)>0)?(currentSmsCnt-limitSmsCnt):0,
367                                 MSGFW_MESSAGE_TABLE_NAME,
368                                 threadId, MSG_ALLBOX_ID, MSG_SPAMBOX_ID, MSG_STORAGE_PHONE,
369                                 MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS, ((currentMmsCnt-limitMmsCnt)>0)?(currentMmsCnt-limitMmsCnt):0);
370 #else
371                 snprintf(sqlQuery, sizeof(sqlQuery),
372                                 "SELECT DISTINCT(MSG_ID) FROM %s "
373                                 "WHERE MSG_ID IN "
374                                 "(SELECT MSG_ID FROM %s "
375                                 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND PROTECTED = 0 "
376                                 "AND MAIN_TYPE = %d ORDER BY MSG_ID ASC LIMIT %d) "
377                                 "OR MSG_ID IN "
378                                 "(SELECT MSG_ID FROM %s "
379                                 "WHERE CONV_ID = %d AND FOLDER_ID > %d AND FOLDER_ID < %d AND STORAGE_ID = %d AND PROTECTED = 0 "
380                                 "AND MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d) ORDER BY MSG_ID ASC LIMIT %d);",
381                                 MSGFW_MESSAGE_TABLE_NAME,
382                                 MSGFW_MESSAGE_TABLE_NAME,
383                                 threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE,
384                                 MSG_SMS_TYPE, ((currentSmsCnt-limitSmsCnt)>0)?(currentSmsCnt-limitSmsCnt):0,
385                                 MSGFW_MESSAGE_TABLE_NAME,
386                                 threadId, MSG_ALLBOX_ID, MSG_CBMSGBOX_ID, MSG_STORAGE_PHONE,
387                                 MSG_MMS_TYPE, MSG_DELIVERYIND_MMS, MSG_READRECIND_MMS, MSG_READORGIND_MMS, ((currentMmsCnt-limitMmsCnt)>0)?(currentMmsCnt-limitMmsCnt):0);
388 #endif
389                 err = dbHandle->getTable(sqlQuery, &rowCnt);
390
391                 if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
392                         MSG_DEBUG("Fail to getTable().");
393                         dbHandle->freeTable();
394                         return err;
395                 }
396
397                 if (rowCnt <= 0) {
398                         MSG_DEBUG("rowCnt <= 0");
399                         dbHandle->freeTable();
400                         return MSG_SUCCESS;
401                 }
402
403                 msgIdList->nCount = rowCnt;
404
405                 MSG_DEBUG("msgIdList.nCount [%d]", msgIdList->nCount);
406
407                 msgIdList->msgIdList = (msg_message_id_t *)new char[sizeof(msg_message_id_t) * rowCnt];
408
409                 for (int i = 0; i < rowCnt; i++) {
410                         msgIdList->msgIdList[i] = dbHandle->getColumnToInt(index++);
411                 }
412
413                 dbHandle->freeTable();
414
415                 // delete message
416                 err = MsgStoDeleteMessageByList(msgIdList);
417
418                 //delete [] (char*)msgIdList.msgIdList;
419
420         } else {
421                 MSG_DEBUG("bAutoErase [%d]", bAutoErase);
422         }
423
424         MSG_END();
425
426         return err;
427 }
428
429
430 msg_error_t MsgStoGetReplaceMsgId(MSG_MESSAGE_INFO_S *pMsgInfo)
431 {
432         msg_error_t err = MSG_SUCCESS;
433         MsgDbHandler *dbHandle = getDbHandle();
434         char sqlQuery[MAX_QUERY_LEN+1];
435
436         msg_thread_id_t convId = 0;
437
438         /** Check if new address or not */
439         if (MsgExistAddress(dbHandle, pMsgInfo, &convId) == true) {
440                 MSG_DEBUG("Address Info. exists [%d]", convId);
441
442                 /**  Find Replace Type Msg : Same Replace Type, Same Origin Address, Same Storage ID */
443                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
444                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE SUB_TYPE = %d AND CONV_ID = %d AND STORAGE_ID = %d;",
445                                 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgType.subType, convId, pMsgInfo->storageId);
446
447                 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
448                         return MSG_ERR_DB_PREPARE;
449
450                 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
451                         pMsgInfo->msgId = dbHandle->columnInt(0);
452                 } else {
453                         dbHandle->finalizeQuery();
454                         return MSG_ERR_DB_STEP;
455                 }
456
457                 dbHandle->finalizeQuery();
458         }
459
460         return err;
461 }
462
463
464 msg_error_t MsgStoAddWAPMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
465 {
466         msg_error_t err = MSG_SUCCESS;
467         MsgDbHandler *dbHandle = getDbHandle();
468         MSG_PUSH_MESSAGE_S pushMsg = {};
469
470         char sqlQuery[MAX_QUERY_LEN+1];
471         memset(sqlQuery, 0x00, sizeof(sqlQuery));
472
473         int fileSize = 0;
474
475         char* pFileData = NULL;
476         AutoPtr<char> buf(&pFileData);
477
478         if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
479                 return MSG_ERR_STORAGE_ERROR;
480
481         MSG_DEBUG("fileSize : %d", fileSize);
482
483         memcpy(&pushMsg, pFileData, fileSize);
484
485         /** Delete temporary file */
486         MsgDeleteFile(pMsgInfo->msgData);
487
488         /** check pPushMsg data */
489
490         MSG_DEBUG("check pushMsg data");
491         MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
492         MSG_DEBUG("pushMsg.received : [%d]", pushMsg.received);
493         MSG_DEBUG("pushMsg.created : [%d]", pushMsg.created);
494         MSG_DEBUG("pushMsg.expires : [%d]", pushMsg.expires);
495         MSG_SEC_DEBUG("pushMsg.id : [%s]", pushMsg.id);
496         MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
497         MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
498
499         bool bProceed = true;
500
501         /**  check validation of contents */
502         if (MsgStoCheckPushMsgValidation(&pushMsg, &bProceed) != MSG_SUCCESS) {
503                 MSG_DEBUG("Fail to check Push Message validation.");
504         }
505
506         /**  if validation check value is false */
507         /** return and drop message. */
508         if (bProceed == false)
509                 return MSG_ERR_INVALID_MESSAGE;
510
511
512         /**  Update Msg Text - remove */
513         strncpy(pMsgInfo->msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
514
515         if (pushMsg.contents[0] != '\0') {
516                 strncat(pMsgInfo->msgText, " ", MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
517                 strncat(pMsgInfo->msgText, pushMsg.contents, MAX_MSG_TEXT_LEN - strlen(pMsgInfo->msgText));
518         }
519
520         pMsgInfo->dataSize = strlen(pMsgInfo->msgText);
521
522         pMsgInfo->bTextSms = true;
523         pMsgInfo->folderId = MSG_INBOX_ID;
524         pMsgInfo->storageId = MSG_STORAGE_PHONE;
525
526         msg_thread_id_t convId = 0;
527
528         dbHandle->beginTrans();
529
530         if (pMsgInfo->nAddressCnt > 0) {
531
532                 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
533
534                 if (err != MSG_SUCCESS) {
535                         dbHandle->endTrans(false);
536                         return err;
537                 }
538
539                 pMsgInfo->threadId = convId;
540         }
541
542         /**  get last row count for Message id */
543         unsigned int rowId = 0;
544
545         /** Add Message Table */
546         rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
547
548         if (rowId <= 0) {
549                 dbHandle->endTrans(false);
550                 return MSG_ERR_DB_ROW;
551         }
552
553         pMsgInfo->msgId = (msg_message_id_t)rowId;
554
555         /**  add msg_push_table */
556         snprintf (sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %lu, %lu, ?, ?, ?)",
557                         MSGFW_PUSH_MSG_TABLE_NAME, pMsgInfo->msgId, pushMsg.action, pushMsg.created, pushMsg.expires);
558
559         if ((err = dbHandle->prepareQuery(sqlQuery)) != MSG_SUCCESS) {
560                 dbHandle->endTrans(false);
561                 return err;
562         }
563
564         dbHandle->bindText(pushMsg.id, 1);
565
566         dbHandle->bindText(pushMsg.href, 2);
567
568         dbHandle->bindText(pushMsg.contents, 3);
569
570         if ((err = dbHandle->stepQuery()) != MSG_ERR_DB_DONE) {
571                 dbHandle->finalizeQuery();
572                 dbHandle->endTrans(false);
573                 return err;
574         }
575
576         dbHandle->finalizeQuery();
577
578         /** Update conversation table. */
579         if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
580                 dbHandle->endTrans(false);
581                 return MSG_ERR_STORAGE_ERROR;
582         }
583
584         dbHandle->endTrans(true);
585
586         return MSG_SUCCESS;
587 }
588
589
590 msg_error_t MsgStoAddCOWAPMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
591 {
592         msg_error_t err = MSG_SUCCESS;
593         MsgDbHandler *dbHandle = getDbHandle();
594         char href[MAX_PUSH_CACHEOP_MAX_URL_LEN+1];
595         char sqlQuery[MAX_QUERY_LEN+1];
596
597         int fileSize = 0;
598
599         char* pFileData = NULL;
600         AutoPtr<char> buf(&pFileData);
601
602         if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
603                 return MSG_ERR_STORAGE_ERROR;
604
605         MSG_PUSH_CACHEOP_S *pPushMsg;
606
607         pPushMsg = (MSG_PUSH_CACHEOP_S*)pFileData;
608
609         for (int i = 0; i < pPushMsg->invalObjectCnt; i++) {
610
611                 int msgid = -1;
612
613                 memset(href, 0x00, sizeof(href));
614                 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
615
616                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
617                 snprintf (sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%';", MSGFW_PUSH_MSG_TABLE_NAME, href);
618
619                 dbHandle->beginTrans();
620
621                 err = dbHandle->prepareQuery(sqlQuery);
622
623                 if ((dbHandle->stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
624
625                         msgid = dbHandle->getColumnToInt(1);
626
627                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
628                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID= %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
629
630                         /** Delete Message from Push table */
631                         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
632                                 dbHandle->finalizeQuery();
633                                 dbHandle->endTrans(false);
634                                 continue;
635                         }
636
637                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
638                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
639
640                         /** Delete Message from msg table */
641                         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
642                                 dbHandle->finalizeQuery();
643                                 dbHandle->endTrans(false);
644                                 continue;
645                         }
646
647                         /** Update all Address */
648                         if (MsgStoUpdateAllAddress() != MSG_SUCCESS) {
649                                 dbHandle->finalizeQuery();
650                                 dbHandle->endTrans(false);
651                                 continue;
652                         }
653
654                         /** Clear Conversation table */
655                         if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
656                                 dbHandle->finalizeQuery();
657                                 dbHandle->endTrans(false);
658                                 continue;
659                         }
660                 }
661
662                 dbHandle->finalizeQuery();
663
664                 dbHandle->endTrans(true);
665         }
666
667         for (int i = 0; i < pPushMsg->invalServiceCnt; i++) {
668
669                 int msgid = -1;
670
671                 memset(href, 0x00, sizeof(href));
672                 strncpy(href, &(pPushMsg->invalObjectUrl[i][0]), MAX_PUSH_CACHEOP_MAX_URL_LEN);
673
674                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
675                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE HREF LIKE '%%%s%%'", MSGFW_PUSH_MSG_TABLE_NAME, href);
676
677                 dbHandle->beginTrans();
678
679                 err = dbHandle->prepareQuery(sqlQuery);
680
681                 if ((dbHandle->stepQuery() == MSG_ERR_DB_ROW) && err == MSG_SUCCESS) {
682
683                         msgid = dbHandle->getColumnToInt(1);
684
685                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
686                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID='%d'", MSGFW_PUSH_MSG_TABLE_NAME, msgid);
687
688                         /** Delete Message from Push table */
689                         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
690                                 dbHandle->finalizeQuery();
691                                 dbHandle->endTrans(false);
692                                 continue;
693                         }
694
695                         memset(sqlQuery, 0x00, sizeof(sqlQuery));
696                         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgid);
697
698                         /** Delete Message from msg table */
699                         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
700                                 dbHandle->finalizeQuery();
701                                 dbHandle->endTrans(false);
702                                 continue;
703                         }
704
705                         /**  Update all Address */
706                         if (MsgStoUpdateAllAddress() != MSG_SUCCESS) {
707                                 dbHandle->finalizeQuery();
708                                 dbHandle->endTrans(false);
709                                 continue;
710                         }
711
712                         /** Clear Address table */
713                         if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
714                                 dbHandle->finalizeQuery();
715                                 dbHandle->endTrans(false);
716                                 continue;
717                         }
718                 }
719
720                 dbHandle->finalizeQuery();
721
722                 dbHandle->endTrans(true);
723         }
724
725         /** delete temporary file */
726         MsgDeleteFile(pMsgInfo->msgData);
727
728         return MSG_SUCCESS;
729 }
730
731
732 msg_error_t MsgStoAddCBMsg(MSG_MESSAGE_INFO_S *pMsgInfo)
733 {
734         msg_error_t err = MSG_SUCCESS;
735         MsgDbHandler *dbHandle = getDbHandle();
736         unsigned int rowId = 0;
737         msg_thread_id_t convId = 0;
738
739         char sqlQuery[MAX_QUERY_LEN+1];
740
741         dbHandle->beginTrans();
742
743         if (pMsgInfo->nAddressCnt > 0) {
744                 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
745
746                 if (err != MSG_SUCCESS) {
747                         dbHandle->endTrans(false);
748                         return err;
749                 }
750
751                 pMsgInfo->threadId = convId;
752         }
753
754         /**  Add Message Table */
755         rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
756
757         if (rowId <= 0) {
758                 dbHandle->endTrans(false);
759                 return MSG_ERR_DB_ROW;
760         }
761
762         /**  Get CB Msg ID */
763         unsigned short cbMsgId = (unsigned short)pMsgInfo->msgId;
764
765         /** Add CB Msg in MSG_CBMSG_TABLE */
766         memset(sqlQuery, 0x00, sizeof(sqlQuery));
767
768         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
769                         MSGFW_CB_MSG_TABLE_NAME, rowId, cbMsgId);
770
771         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
772                 dbHandle->endTrans(false);
773                 return MSG_ERR_DB_EXEC;
774         }
775
776         /**  Update conversation table. */
777         if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
778                 dbHandle->endTrans(false);
779                 return MSG_ERR_STORAGE_ERROR;
780         }
781
782         dbHandle->endTrans(true);
783
784         /** Assign Msg ID */
785         pMsgInfo->msgId = (msg_message_id_t)rowId;
786
787         return err;
788 }
789
790
791 msg_error_t MsgStoCheckPushMsgValidation(MSG_PUSH_MESSAGE_S *pPushMsg, bool *pbProceed)
792 {
793         msg_error_t err = MSG_SUCCESS;
794         MsgDbHandler *dbHandle = getDbHandle();
795         unsigned long oldExpireTime = 0;
796         int rowCnt = 0;
797
798         char sqlQuery[MAX_QUERY_LEN+1];
799
800         /**  is push message is expired?? */
801         if (pPushMsg->received > pPushMsg->expires) {
802                 MSG_DEBUG("Push Message is expired.");
803                 *pbProceed = false;
804                 return err;
805         }
806
807         if (pPushMsg->action == MSG_PUSH_SL_ACTION_EXECUTE_LOW) {
808                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
809                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s' AND ACTION = %d",
810                                 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id, pPushMsg->action);
811         } else {
812                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT EXPIRES FROM %s WHERE ID = '%s'",
813                                 MSGFW_PUSH_MSG_TABLE_NAME, pPushMsg->id);
814         }
815
816         err = dbHandle->getTable(sqlQuery, &rowCnt);
817
818         if (rowCnt < 1) {
819                 dbHandle->freeTable();
820                 return MSG_SUCCESS;
821         }
822
823         oldExpireTime = dbHandle->getColumnToInt(1);
824
825         dbHandle->freeTable();
826
827         if (pPushMsg->created < oldExpireTime) {
828                 MSG_DEBUG("Push Message is expired.");
829                 *pbProceed = false;
830                 return err;
831         }
832
833         return err;
834 }
835
836
837 msg_error_t MsgStoUpdateAllAddress()
838 {
839         msg_error_t err = MSG_SUCCESS;
840         MsgDbHandler *dbHandle = getDbHandle();
841         int rowCnt = 0, index = 1;
842         char sqlQuery[MAX_QUERY_LEN+1];
843
844         memset(sqlQuery, 0x00, sizeof(sqlQuery));
845
846         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s", MSGFW_ADDRESS_TABLE_NAME);
847
848         err = dbHandle->getTable(sqlQuery, &rowCnt);
849
850         if (err == MSG_ERR_DB_NORECORD) {
851                 dbHandle->freeTable();
852                 return MSG_SUCCESS;
853         } else if ( err != MSG_SUCCESS) {
854                 dbHandle->freeTable();
855                 return err;
856         }
857
858         for (int i = 0; i < rowCnt; i++) {
859
860                 err = MsgStoUpdateConversation(dbHandle, index++);
861
862                 if (err != MSG_SUCCESS)
863                         break;
864         }
865
866         dbHandle->freeTable();
867
868         return err;
869 }
870