[MPR-868] Add duplication logic for CB Messages
[platform/core/messaging/msg-service.git] / framework / storage-handler / MsgStorageManager.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 <stdlib.h>
19 #include <string.h>
20 #include <sys/stat.h>
21 #include <errno.h>
22 #include <sys/mman.h>
23 #include <fcntl.h>
24
25 #include <VMessage.h>
26 #include <VCard.h>
27 #include "MsgVMessage.h"
28
29 #include "MsgDebug.h"
30 #include "MsgUtilFile.h"
31 #include "MsgUtilStorage.h"
32 #include "MsgGconfWrapper.h"
33 #include "MsgSqliteWrapper.h"
34 #include "MsgPluginManager.h"
35 #include "MsgStorageHandler.h"
36
37 #define MSG_DB_ACCESS_MAX_COUNT 10
38 #define MSG_DB_VERSION 2
39
40 /*==================================================================================================
41                                      VARIABLES
42 ==================================================================================================*/
43
44 /*==================================================================================================
45                                      FUNCTION IMPLEMENTATION
46 ==================================================================================================*/
47 msg_error_t MsgStoConnectDB()
48 {
49         return MSG_SUCCESS;
50 }
51
52
53 msg_error_t MsgStoDisconnectDB()
54 {
55         MsgDbHandler *dbHandle = getDbHandle();
56         if (dbHandle->disconnect() != MSG_SUCCESS) {
57                 MSG_DEBUG("DB Disconnect Fail");
58                 return MSG_ERR_DB_DISCONNECT;
59         }
60
61         MSG_DEBUG("DB Disconnect Success");
62
63         return MSG_SUCCESS;
64 }
65
66
67 void MsgUpdateDBtoVer1()
68 {
69         MsgDbHandler *dbHandle = getDbHandle();
70         msg_error_t err = MSG_SUCCESS;
71         char sqlQuery[MAX_QUERY_LEN+1];
72
73         if (!dbHandle->checkTableExist(MSGFW_MMS_MULTIPART_TABLE_NAME)) {
74                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
75                 snprintf(sqlQuery, sizeof(sqlQuery),
76                                 "CREATE TABLE %s ( "
77                                 "_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
78                                 "MSG_ID INTEGER NOT NULL , "
79                                 "SEQ INTEGER DEFAULT 0, "
80                                 "CONTENT_TYPE TEXT, "
81                                 "NAME TEXT, "
82                                 "CHARSET INTEGER, "
83                                 "CONTENT_ID TEXT, "
84                                 "CONTENT_LOCATION TEXT, "
85                                 "FILE_PATH TEXT, "
86                                 "TEXT TEXT, "
87                                 "TCS_LEVEL INTEGER DEFAULT -1, "
88                                 "MALWARE_ALLOW INTEGER DEFAULT 0, "
89                                 "THUMB_FILE_PATH TEXT, "
90                                 "FOREIGN KEY(MSG_ID) REFERENCES MSG_MESSAGE_TABLE(MSG_ID));",
91                                 MSGFW_MMS_MULTIPART_TABLE_NAME);
92
93                 err = dbHandle->execQuery(sqlQuery);
94
95                 if (err == MSG_SUCCESS)
96                         MSG_SEC_DEBUG("SUCCESS : create %s.", MSGFW_MMS_MULTIPART_TABLE_NAME);
97                 else
98                         MSG_SEC_DEBUG("FAIL : create %s [%d].", MSGFW_MMS_MULTIPART_TABLE_NAME, err);
99         }
100 }
101
102
103 void MsgUpdateDBtoVer2()
104 {
105         MsgDbHandler *dbHandle = getDbHandle();
106         msg_error_t err = MSG_SUCCESS;
107         char sqlQuery[MAX_QUERY_LEN+1] = {0};
108         snprintf(sqlQuery, sizeof(sqlQuery),
109                         "ALTER TABLE %s "
110                         "ADD COLUMN DPM_RESTRICTED INTEGER DEFAULT 0;",
111                         MSGFW_MESSAGE_TABLE_NAME);
112
113         err = dbHandle->execQuery(sqlQuery);
114
115         if (err == MSG_SUCCESS)
116                 MSG_SEC_DEBUG("SUCCESS : alter %s.", MSGFW_MESSAGE_TABLE_NAME);
117         else
118                 MSG_SEC_DEBUG("FAIL : create %s [%d].", MSGFW_MESSAGE_TABLE_NAME, err);
119
120         memset(sqlQuery, 0x00, sizeof(sqlQuery));
121         snprintf(sqlQuery, sizeof(sqlQuery),
122                         "ALTER TABLE %s "
123                         "ADD COLUMN SERIAL_NUM INTEGER NOT NULL;",
124                         MSGFW_CB_MSG_TABLE_NAME);
125
126         err = dbHandle->execQuery(sqlQuery);
127
128         if (err == MSG_SUCCESS)
129                 MSG_SEC_DEBUG("SUCCESS : alter %s.", MSGFW_CB_MSG_TABLE_NAME);
130         else
131                 MSG_SEC_DEBUG("FAIL : create %s [%d].", MSGFW_CB_MSG_TABLE_NAME, err);
132 }
133
134
135 void MsgStoUpdateDBVersion()
136 {
137         MsgDbHandler *dbHandle = getDbHandle();
138         char sqlQuery[MAX_QUERY_LEN+1];
139
140         snprintf(sqlQuery, sizeof(sqlQuery), "PRAGMA user_version=%d;", MSG_DB_VERSION);
141
142         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
143                 MSG_DEBUG("Fail to prepareQuery.");
144                 return;
145         }
146
147         if (dbHandle->stepQuery() == MSG_ERR_DB_STEP) {
148                 MSG_DEBUG("Fail to stepQuery.");
149                 dbHandle->finalizeQuery();
150                 return;
151         }
152
153         dbHandle->finalizeQuery();
154 }
155
156 msg_error_t MsgStoDBVerCheck()
157 {
158         MsgDbHandler *dbHandle = getDbHandle();
159 #if 1 /* TODO: need to improve this code later */
160         int cnt = MSG_DB_ACCESS_MAX_COUNT;
161         while (cnt--) {
162                 if (dbHandle->connect() == MSG_SUCCESS)
163                         break;
164
165                 if (cnt == 0) {
166                         MSG_ERR("db connect try count over %d", MSG_DB_ACCESS_MAX_COUNT);
167                         return MSG_ERR_DB_CONNECT;
168                 }
169
170                 MSG_DEBUG("waiting for mount /opt/usr/ cnt [%d]", cnt);
171                 sleep(1);
172         }
173 #endif
174         int dbVersion = 0;
175
176         char sqlQuery[MAX_QUERY_LEN+1];
177
178         snprintf(sqlQuery, sizeof(sqlQuery), "PRAGMA user_version;");
179
180         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
181                 MSG_DEBUG("Fail to prepareQuery.");
182                 return MSG_ERR_DB_EXEC;
183         }
184
185         if (dbHandle->stepQuery() == MSG_ERR_DB_STEP) {
186                 MSG_DEBUG("Fail to stepQuery.");
187                 dbHandle->finalizeQuery();
188                 return MSG_ERR_DB_EXEC;
189         }
190
191         dbVersion = dbHandle->columnInt(0);
192
193         dbHandle->finalizeQuery();
194
195         MSG_DEBUG("dbVersion [%d]", dbVersion);
196
197         switch (dbVersion) {
198         case 0 :
199                 MsgUpdateDBtoVer1();
200                 /* no break */
201         case 1 :
202                 MsgUpdateDBtoVer2();
203                 /* no break */
204         default :
205                 MsgStoUpdateDBVersion();
206                 /* no break */
207         }
208
209         return MSG_SUCCESS;
210 }
211
212 void MsgInitMmapMutex(const char *shm_file_name)
213 {
214         MSG_BEGIN();
215
216         if(!shm_file_name) {
217                 MSG_FATAL("EMAIL_ERROR_INVALID_PARAM");
218                 return;
219         }
220
221         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
222
223         if (fd < 0) {
224                 MSG_FATAL("shm_open errno [%d]", errno);
225                 return;
226         }
227
228         fchmod(fd, 0666);
229         MSG_DEBUG("** Create SHM FILE **");
230         if (ftruncate(fd, sizeof(pthread_mutex_t)) != 0) {
231                 MSG_FATAL("ftruncate errno [%d]", errno);
232                 return;
233         }
234
235         pthread_mutex_t *mx = (pthread_mutex_t *)mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
236         if (mx == MAP_FAILED) {
237                 MSG_FATAL("mmap errno [%d]", errno);
238                 return ;
239         }
240
241         /* initialize the data on mmap */
242         pthread_mutexattr_t mattr;
243         pthread_mutexattr_init(&mattr);
244         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
245         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
246         pthread_mutex_init(mx, &mattr);
247         pthread_mutexattr_destroy(&mattr);
248
249         close(fd);
250
251         if (munmap((void *)mx, sizeof(pthread_mutex_t)) != 0) {
252                 MSG_FATAL("munmap() failed! (errno: %d)", errno);
253                 return;
254         }
255
256         MSG_END();
257 }
258
259 msg_error_t MsgStoInitDB(bool bSimChanged)
260 {
261         MSG_BEGIN();
262         /* MsgDbHandler *dbHandle = getDbHandle(); */
263         msg_error_t err = MSG_SUCCESS;
264
265         /* Init mmap mutex for DB access */
266         MsgInitMmapMutex(SHM_FILE_FOR_DB_LOCK);
267
268         /* Check DB version. */
269         MsgStoDBVerCheck();
270
271         /* Delete Msgs in Hidden Folder */
272         MsgStoDeleteAllMessageInFolder(0, true, NULL);
273
274         /* Reset network status */
275         MsgStoResetNetworkStatus();
276
277 #if 0
278         /* Reset Cb Message */
279         MsgStoResetCBMessage();
280 #endif
281
282         /*clear abnormal mms message */
283         MsgStoCleanAbnormalMmsData();
284
285         /* Clear all old Sim data */
286         MsgStoClearSimMessageInDB();
287
288         /*update sim index to 0 for all messages */
289         MsgStoUpdateIMSI(0);
290
291         MSG_END();
292
293         return err;
294 }
295
296
297 msg_error_t MsgAddDefaultFolders()
298 {
299         int nRowCnt = 0;
300         int nResult = 0;
301
302         char sqlQuery[MAX_QUERY_LEN+1];
303         MsgDbHandler *dbHandle = getDbHandle();
304         /* INBOX */
305         memset(sqlQuery, 0x00, sizeof(sqlQuery));
306         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
307                         MSGFW_FOLDER_TABLE_NAME, MSG_INBOX_ID);
308
309         if (dbHandle->getTable(sqlQuery, &nRowCnt, NULL) != MSG_SUCCESS) {
310                 dbHandle->freeTable();
311                 return MSG_ERR_DB_GETTABLE;
312         }
313
314         nResult = dbHandle->getColumnToInt(1);
315
316         dbHandle->freeTable();
317
318         if (nResult == 0) {
319                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
320                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'INBOX', %d);",
321                                 MSGFW_FOLDER_TABLE_NAME, MSG_INBOX_ID, MSG_FOLDER_TYPE_INBOX);
322
323                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
324                         return MSG_ERR_DB_EXEC;
325         }
326
327         /* OUTBOX */
328         memset(sqlQuery, 0x00, sizeof(sqlQuery));
329         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
330                         MSGFW_FOLDER_TABLE_NAME, MSG_OUTBOX_ID);
331
332         if (dbHandle->getTable(sqlQuery, &nRowCnt, NULL) != MSG_SUCCESS) {
333                 dbHandle->freeTable();
334                 return MSG_ERR_DB_GETTABLE;
335         }
336
337         nResult = dbHandle->getColumnToInt(1);
338
339         dbHandle->freeTable();
340
341         if (nResult == 0) {
342                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
343                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'OUTBOX', %d);",
344                                 MSGFW_FOLDER_TABLE_NAME, MSG_OUTBOX_ID, MSG_FOLDER_TYPE_OUTBOX);
345
346                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
347                         return MSG_ERR_DB_EXEC;
348         }
349
350         /* SENTBOX */
351         memset(sqlQuery, 0x00, sizeof(sqlQuery));
352         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
353                         MSGFW_FOLDER_TABLE_NAME, MSG_SENTBOX_ID);
354
355         if (dbHandle->getTable(sqlQuery, &nRowCnt, NULL) != MSG_SUCCESS) {
356                 dbHandle->freeTable();
357                 return MSG_ERR_DB_GETTABLE;
358         }
359
360         nResult = dbHandle->getColumnToInt(1);
361
362         dbHandle->freeTable();
363
364         if (nResult == 0) {
365                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
366                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'SENTBOX', %d);",
367                                 MSGFW_FOLDER_TABLE_NAME, MSG_SENTBOX_ID, MSG_FOLDER_TYPE_OUTBOX);
368
369                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
370                         return MSG_ERR_DB_EXEC;
371         }
372
373         /* DRAFT */
374         memset(sqlQuery, 0x00, sizeof(sqlQuery));
375         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
376                         MSGFW_FOLDER_TABLE_NAME, MSG_DRAFT_ID);
377
378         if (dbHandle->getTable(sqlQuery, &nRowCnt, NULL) != MSG_SUCCESS) {
379                 dbHandle->freeTable();
380                 return MSG_ERR_DB_GETTABLE;
381         }
382
383         nResult = dbHandle->getColumnToInt(1);
384
385         dbHandle->freeTable();
386
387         if (nResult == 0) {
388                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
389                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'DRAFT', %d);",
390                                 MSGFW_FOLDER_TABLE_NAME, MSG_DRAFT_ID, MSG_FOLDER_TYPE_DRAFT);
391
392                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
393                         return MSG_ERR_DB_EXEC;
394         }
395
396         /* CBMSGBOX */
397         memset(sqlQuery, 0x00, sizeof(sqlQuery));
398         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
399                         MSGFW_FOLDER_TABLE_NAME, MSG_CBMSGBOX_ID);
400
401         if (dbHandle->getTable(sqlQuery, &nRowCnt, NULL) != MSG_SUCCESS) {
402                 dbHandle->freeTable();
403                 return MSG_ERR_DB_GETTABLE;
404         }
405
406         nResult = dbHandle->getColumnToInt(1);
407
408         dbHandle->freeTable();
409
410         if (nResult == 0) {
411                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
412                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'CBMSGBOX', %d);",
413                                 MSGFW_FOLDER_TABLE_NAME, MSG_CBMSGBOX_ID, MSG_FOLDER_TYPE_INBOX);
414
415                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
416                         return MSG_ERR_DB_EXEC;
417         }
418
419         /* SPAMBOX */
420         memset(sqlQuery, 0x00, sizeof(sqlQuery));
421         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
422                         MSGFW_FOLDER_TABLE_NAME, MSG_SPAMBOX_ID);
423
424         if (dbHandle->getTable(sqlQuery, &nRowCnt, NULL) != MSG_SUCCESS) {
425                 dbHandle->freeTable();
426                 return MSG_ERR_DB_GETTABLE;
427         }
428
429         nResult = dbHandle->getColumnToInt(1);
430
431         dbHandle->freeTable();
432
433         if (nResult == 0) {
434                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
435                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'SPAMBOX', %d);",
436                                 MSGFW_FOLDER_TABLE_NAME, MSG_SPAMBOX_ID, MSG_FOLDER_TYPE_SPAMBOX);
437
438                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
439                         return MSG_ERR_DB_EXEC;
440         }
441
442         return MSG_SUCCESS;
443 }
444
445
446 msg_error_t MsgAddDefaultAddress()
447 {
448         MSG_BEGIN();
449         MsgDbHandler *dbHandle = getDbHandle();
450         int nRowCnt = 0, nResult = 0;
451
452         char sqlQuery[MAX_QUERY_LEN+1];
453
454         memset(sqlQuery, 0x00, sizeof(sqlQuery));
455         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE ADDRESS_ID = 0;",
456                         MSGFW_ADDRESS_TABLE_NAME);
457
458         if (dbHandle->getTable(sqlQuery, &nRowCnt, NULL) != MSG_SUCCESS) {
459                 dbHandle->freeTable();
460                 return MSG_ERR_DB_GETTABLE;
461         }
462
463         nResult = dbHandle->getColumnToInt(1);
464
465         dbHandle->freeTable();
466
467         if (nResult == 0) {
468                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
469                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (0, 0, 0, 0, '', 0, 0, '', '', '', '', '', '', 0);",
470                                 MSGFW_ADDRESS_TABLE_NAME);
471
472                 MSG_DEBUG("%s", sqlQuery);
473
474                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
475                         return MSG_ERR_DB_EXEC;
476         }
477
478         MSG_END();
479
480         return MSG_SUCCESS;
481 }
482
483
484 msg_error_t MsgStoResetDatabase()
485 {
486         MsgDbHandler *dbHandle = getDbHandle();
487         msg_error_t err = MSG_SUCCESS;
488
489         char sqlQuery[MAX_QUERY_LEN+1];
490
491         const char* tableList[] = {MSGFW_FOLDER_TABLE_NAME, MSGFW_FILTER_TABLE_NAME,
492                         MSGFW_PUSH_MSG_TABLE_NAME, MSGFW_CB_MSG_TABLE_NAME,
493                         MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_SYNCML_MSG_TABLE_NAME,
494                         MSGFW_SMS_SENDOPT_TABLE_NAME};
495
496         int listCnt = sizeof(tableList)/sizeof(char*);
497
498         dbHandle->beginTrans();
499
500         /* Delete Database */
501         for (int i = 0; i < listCnt; i++) {
502                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
503                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s;", tableList[i]);
504
505                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
506                         dbHandle->endTrans(false);
507                         return MSG_ERR_DB_EXEC;
508                 }
509         }
510
511         /* Delete Message Table */
512         memset(sqlQuery, 0x00, sizeof(sqlQuery));
513         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE STORAGE_ID <> %d;",
514                         MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_SIM);
515
516         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
517                 dbHandle->endTrans(false);
518                 return MSG_ERR_DB_EXEC;
519         }
520
521         /* Delete Conversation Table */
522         err = MsgStoClearConversationTable(dbHandle);
523
524         if (err != MSG_SUCCESS) {
525                 dbHandle->endTrans(false);
526                 return err;
527         }
528
529         /* Add Default Folders */
530         if (MsgAddDefaultFolders() != MSG_SUCCESS) {
531                 MSG_DEBUG("Add Default Folders Fail");
532                 dbHandle->endTrans(false);
533                 return MSG_ERR_DB_STORAGE_INIT;
534         }
535
536         /* Add Default Address */
537         if (MsgAddDefaultAddress() != MSG_SUCCESS) {
538                 MSG_DEBUG("Add Default Address Fail");
539                 dbHandle->endTrans(false);
540                 return MSG_ERR_DB_STORAGE_INIT;
541         }
542
543         dbHandle->endTrans(true);
544
545         /* Delete MMS Files */
546         MsgRmRf((char*)MSG_DATA_PATH);
547         MsgRmRf((char*)MSG_SMIL_FILE_PATH);
548
549         /* Reset SMS Count */
550         if (MsgSettingSetIndicator(0, 0) != MSG_SUCCESS) {
551                 MSG_DEBUG("MsgSettingSetIndicator() FAILED");
552                 return MSG_ERR_SET_SETTING;
553         }
554
555         /* Reset MMS Count */
556         if (MsgSettingSetIndicator(0, 0) != MSG_SUCCESS) {
557                 MSG_DEBUG("MsgSettingSetIndicator() FAILED");
558                 return MSG_ERR_SET_SETTING;
559         }
560
561         return MSG_SUCCESS;
562 }
563
564
565 msg_error_t MsgStoBackupMessage(msg_message_backup_type_t type, const char *filepath)
566 {
567         MsgDbHandler *dbHandle = getDbHandle();
568         msg_error_t     err = MSG_SUCCESS;
569
570         char sqlQuery[MAX_QUERY_LEN+1];
571         int rowCnt = 0;
572         MSG_MESSAGE_INFO_S msgInfo = {0, };
573         char* encoded_data = NULL;
574
575         char fileName[MSG_FILENAME_LEN_MAX+1];
576         memset(fileName, 0x00, sizeof(fileName));
577         strncpy(fileName, filepath, MSG_FILENAME_LEN_MAX);
578         if (remove(fileName) != 0) {
579                 MSG_SEC_DEBUG("Fail to delete [%s].", fileName);
580         }
581
582         memset(sqlQuery, 0x00, sizeof(sqlQuery));
583
584         MSG_SEC_DEBUG("backup type = %d, path = %s", type, filepath);
585
586         if (type == MSG_BACKUP_TYPE_SMS) {
587                 snprintf(sqlQuery, sizeof(sqlQuery),
588                                 "SELECT MSG_ID FROM %s "
589                                 "WHERE STORAGE_ID = %d AND MAIN_TYPE = %d;",
590                                 MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_PHONE, MSG_SMS_TYPE);
591         } else if (type == MSG_BACKUP_TYPE_MMS) {
592                 snprintf(sqlQuery, sizeof(sqlQuery),
593                                 "SELECT MSG_ID FROM %s "
594                                 "WHERE STORAGE_ID = %d AND MAIN_TYPE = %d;",
595                                 MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_PHONE, MSG_MMS_TYPE);
596         } else if (type == MSG_BACKUP_TYPE_ALL) {
597                 snprintf(sqlQuery, sizeof(sqlQuery),
598                                 "SELECT MSG_ID FROM %s "
599                                 "WHERE STORAGE_ID = %d;",
600                                 MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_PHONE);
601         }
602
603         err = dbHandle->getTable(sqlQuery, &rowCnt, NULL);
604
605         if (err != MSG_SUCCESS) {
606                 dbHandle->freeTable();
607                 return err;
608         }
609
610         MSG_DEBUG("backup number = %d", rowCnt);
611
612         int msg_id[rowCnt];
613         for (int i = 0; i < rowCnt; i++) {
614                 msg_id[i] = dbHandle->getColumnToInt(i+1);
615         }
616         dbHandle->freeTable();
617
618         for (int i = 0; i < rowCnt; i++) {
619                 msgInfo.addressList = NULL;
620                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
621
622                 err = MsgStoGetMessage(msg_id[i], &msgInfo, NULL);
623                 if(err != MSG_SUCCESS) {
624                         return err;
625                 }
626
627                 encoded_data = MsgVMessageEncode(&msgInfo);
628
629                 if (msgInfo.bTextSms == false)
630                         MsgDeleteFile(msgInfo.msgData); /*ipc */
631
632                 if (encoded_data != NULL) {
633                         if (MsgAppendFile(fileName, encoded_data, strlen(encoded_data)) == false) {
634                                 free(encoded_data);
635                                 return MSG_ERR_STORAGE_ERROR;
636                         }
637
638                         free(encoded_data);
639
640                         if (chmod(fileName, 0666) == -1) {
641                                 MSG_FATAL("chmod: %s", g_strerror(errno));
642                                 return MSG_ERR_UNKNOWN;
643                         }
644                 }
645
646                 memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
647         }
648
649         MSG_END();
650         return MSG_SUCCESS;
651 }
652
653 msg_error_t MsgStoUpdateMms(MSG_MESSAGE_INFO_S *pMsg)
654 {
655         MsgDbHandler *dbHandle = getDbHandle();
656         msg_error_t err = MSG_SUCCESS;
657         char sqlQuery[MAX_QUERY_LEN+1];
658         unsigned int fileSize = 0;
659         char *pFileData = NULL;
660
661         if (pMsg->msgType.subType == MSG_SENDCONF_MMS) {
662                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
663
664                 dbHandle->beginTrans();
665                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET THUMB_PATH = ?, MSG_TEXT = ? WHERE MSG_ID = %d;",
666                                 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgId);
667
668                 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
669                         dbHandle->endTrans(false);
670                         return MSG_ERR_DB_EXEC;
671                 }
672
673                 dbHandle->bindText(pMsg->thumbPath, 1);
674
675                 if (pMsg->msgText[0] != '\0' && g_file_get_contents((gchar*)pMsg->msgText, (gchar**)&pFileData, (gsize*)&fileSize, NULL) == true) {
676                         dbHandle->bindText(pFileData, 2);
677                 }
678
679                 MSG_SEC_DEBUG("thumbPath = %s , msgText = %s" , pMsg->thumbPath, pFileData);
680
681                 MSG_DEBUG("%s", sqlQuery);
682
683                 if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
684                         dbHandle->finalizeQuery();
685                         dbHandle->endTrans(false);
686                         if (pFileData) {
687                                 free(pFileData);
688                                 pFileData = NULL;
689                         }
690
691                         return MSG_ERR_DB_EXEC;
692                 }
693
694                 dbHandle->finalizeQuery();
695                 dbHandle->endTrans(true);
696
697                 if (pFileData) {
698                         free(pFileData);
699                         pFileData = NULL;
700                 }
701         } else {
702                 err = MsgStoUpdateMMSMessage(pMsg);
703                 if (err != MSG_SUCCESS) {
704                         MSG_DEBUG("MsgStoUpdateMMSMessage() error : %d", err);
705                 }
706         }
707
708         return err;
709 }
710
711 msg_error_t MsgStoRestoreMessage(const char *filepath, msg_id_list_s **result_id_list)
712 {
713         if (result_id_list == NULL) {
714                 MSG_ERR("result_id_list is NULL");
715                 return MSG_ERR_NULL_POINTER;
716         }
717
718         msg_error_t err = MSG_SUCCESS;
719         MSG_MESSAGE_INFO_S msgInfo = {0, };
720         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
721
722         VTree* vMsg = NULL;
723         VObject* pObject = NULL;
724         bool isMMS = false;
725
726         msg_id_list_s *msgIdList = NULL;
727         msgIdList = (msg_id_list_s *)calloc(1, sizeof(msg_id_list_s));
728
729         int dataSize = 0;
730
731         char fileName[MSG_FILENAME_LEN_MAX+1];
732         char *pData = NULL;
733         char *pCurrent = NULL;
734         char *pTemp = NULL;
735
736         *result_id_list = NULL;
737
738 #ifdef MSG_FOR_DEBUG
739         char sample[10000] = "BEGIN:VMSG\r\nX-MESSAGE-TYPE:SMS\r\nX-IRMC-BOX:INBOX\r\nX-SS-DT:20100709T155811Z\r\nBEGIN:VBODY\r\nX-BODY-SUBJECT:hekseh\r\nX-BODY-CONTENTS;ENCODING=BASE64:aGVsbG93b3JsZA==\r\nEND:VBODY\r\nBEGIN:VCARD\r\nVERSION:2.1\r\nTEL:01736510664\r\nEND:VCARD\r\nEND:VMSG\r\n";
740         vMsg = vmsg_decode(sample);
741 #else
742         memset(fileName, 0x00, sizeof(fileName));
743         strncpy(fileName, filepath, MSG_FILENAME_LEN_MAX);
744         pData = MsgOpenAndReadMmsFile(fileName, 0, -1, &dataSize);
745         if (pData == NULL) {
746                 if (msgIdList) {
747                         if (msgIdList->msgIdList)
748                                 free(msgIdList->msgIdList);
749                         free(msgIdList);
750                 }
751                 return MSG_ERR_STORAGE_ERROR;
752         }
753
754         pCurrent = pData;
755
756         while ((pTemp = strstr(pCurrent, "END:VMSG")) != NULL) {
757                 isMMS = false;
758                 MSG_DEBUG("Start Position: %s", pCurrent);
759
760                 while (*pCurrent == '\r' || *pCurrent == '\n')
761                         pCurrent++;
762
763                 MSG_DEBUG("Start Position2: %s", pCurrent);
764
765                 /*decodes if it is sms */
766                 err = MsgVMessageDecodeSMS(pCurrent, &msgInfo);
767
768                 /*decode if it is mms */
769                 if (err == MSG_ERR_INVALID_MESSAGE) {
770                         MSG_DEBUG("Vmsg is not an SMS, decoding for MMS...");
771                         vMsg = vmsg_decode(pCurrent);
772                         isMMS = true;
773                 } else if (err != MSG_SUCCESS) {
774                         MSG_ERR("Vmsg is an SMS, but format not supported.");
775                         goto __RETURN;
776                 }
777
778                 if (vMsg == NULL && isMMS) {
779                         MSG_ERR("Vmsg is an MMS, but format not supported.");
780                         err = MSG_ERR_STORAGE_ERROR;
781                         goto __RETURN;
782                 }
783 #endif
784
785                 if (isMMS) {
786                         pObject = vMsg->pTop;
787                         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
788                 }
789
790                 while (1 && isMMS) {
791                         while (1) {
792                                 MSG_DEBUG("pObject type [%d], pObject Value [%s]", pObject->property, pObject->pszValue[0]);
793
794                                 switch (pObject->property) {
795                                 case VMSG_TYPE_MSGTYPE : {
796                                         if (!strncmp(pObject->pszValue[0], "MMS RETRIEVED", strlen("MMS RETRIEVED"))) {
797                                                 msgInfo.msgType.mainType = MSG_MMS_TYPE;
798                                                 msgInfo.msgType.subType = MSG_RETRIEVE_AUTOCONF_MMS;
799                                         } else if (!strncmp(pObject->pszValue[0], "MMS SEND", strlen("MMS SEND"))) {
800                                                 msgInfo.msgType.mainType = MSG_MMS_TYPE;
801                                                 msgInfo.msgType.subType = MSG_SENDCONF_MMS;
802                                         } else if (!strncmp(pObject->pszValue[0], "MMS NOTIFICATION", strlen("MMS NOTIFICATION"))) {
803                                                 msgInfo.msgType.mainType = MSG_MMS_TYPE;
804                                                 msgInfo.msgType.subType = MSG_NOTIFICATIONIND_MMS;
805                                         } else {
806                                                 vmsg_free_vtree_memory(vMsg);
807                                                 err = MSG_ERR_STORAGE_ERROR;
808                                                 goto __RETURN;
809                                         }
810                                 }
811                                 break;
812
813                                 case VMSG_TYPE_MSGBOX : {
814                                         if (!strncmp(pObject->pszValue[0], "INBOX", strlen("INBOX"))) {
815                                                 msgInfo.folderId = MSG_INBOX_ID;
816                                                 msgInfo.direction = MSG_DIRECTION_TYPE_MT;
817
818                                                 if (msgInfo.msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS)
819                                                         msgInfo.networkStatus = MSG_NETWORK_RETRIEVE_SUCCESS;
820                                                 else if (msgInfo.msgType.subType == MSG_SENDCONF_MMS)
821                                                         msgInfo.networkStatus = MSG_NETWORK_SEND_SUCCESS;
822                                                 else
823                                                         msgInfo.networkStatus = MSG_NETWORK_RECEIVED;
824
825                                         } else if (!strncmp(pObject->pszValue[0], "OUTBOX", strlen("OUTBOX"))) {
826                                                 msgInfo.folderId = MSG_OUTBOX_ID;
827                                                 msgInfo.direction = MSG_DIRECTION_TYPE_MO;
828
829                                                 msgInfo.networkStatus = MSG_NETWORK_SEND_FAIL;
830                                         } else if (!strncmp(pObject->pszValue[0], "SENTBOX", strlen("SENTBOX"))) {
831                                                 msgInfo.folderId = MSG_SENTBOX_ID;
832                                                 msgInfo.direction = MSG_DIRECTION_TYPE_MO;
833
834                                                 msgInfo.networkStatus = MSG_NETWORK_SEND_SUCCESS;
835                                         } else if (!strncmp(pObject->pszValue[0], "DRAFTBOX", strlen("DRAFTBOX"))) {
836                                                 msgInfo.folderId = MSG_DRAFT_ID;
837                                                 msgInfo.direction = MSG_DIRECTION_TYPE_MO;
838
839                                                 msgInfo.networkStatus = MSG_NETWORK_NOT_SEND;
840                                         } else {
841                                                 vmsg_free_vtree_memory(vMsg);
842                                                 err = MSG_ERR_STORAGE_ERROR;
843                                                 goto __RETURN;
844                                         }
845                                 }
846                                 break;
847
848                                 case VMSG_TYPE_STATUS : {
849                                         if(!strncmp(pObject->pszValue[0], "READ", strlen("READ"))) {
850                                                 msgInfo.bRead = true;
851                                         } else if (!strncmp(pObject->pszValue[0], "UNREAD", strlen("UNREAD"))) {
852                                                 msgInfo.bRead = false;
853                                         } else {
854                                                 vmsg_free_vtree_memory(vMsg);
855                                                 err = MSG_ERR_STORAGE_ERROR;
856                                                 goto __RETURN;
857                                         }
858                                 }
859                                 break;
860
861                                 case VMSG_TYPE_DATE : {
862                                         struct tm displayTime;
863
864                                         if (!_convert_vdata_str_to_tm(pObject->pszValue[0], &displayTime)) {
865                                                 vmsg_free_vtree_memory(vMsg);
866                                                 err = MSG_ERR_STORAGE_ERROR;
867                                                 goto __RETURN;
868                                         }
869
870                                         msgInfo.displayTime = mktime(&displayTime);
871                                 }
872                                 break;
873
874                                 case VMSG_TYPE_SUBJECT : {
875                                         MSG_DEBUG("subject length is [%d].", strlen(pObject->pszValue[0]));
876
877                                         if(strlen(pObject->pszValue[0]) > 0) {
878                                                 strncpy(msgInfo.subject, pObject->pszValue[0], MAX_SUBJECT_LEN);
879                                                 if ( msgInfo.subject[strlen(pObject->pszValue[0])-1] == '\r' )
880                                                         msgInfo.subject[strlen(pObject->pszValue[0])-1] = '\0';
881                                         }
882                                 }
883                                 break;
884
885                                 case VMSG_TYPE_BODY : {
886                                         if (msgInfo.msgType.mainType == MSG_MMS_TYPE) {
887                                                 msgInfo.bTextSms = true;
888 #if 0
889                                                 if(msgInfo.msgType.subType == MSG_NOTIFICATIONIND_MMS) {
890                                                                 msgInfo.bTextSms = true;
891
892                                                         /* Save Message Data into File */
893                                                         char fileName[MAX_COMMON_INFO_SIZE+1];
894                                                         memset(fileName, 0x00, sizeof(fileName));
895
896                                                         if (MsgCreateFileName(fileName) == false) {
897                                                                 vmsg_free_vtree_memory(vMsg);
898                                                                 return MSG_ERR_STORAGE_ERROR;
899                                                         }
900
901                                                         if (MsgWriteIpcFile(fileName, pObject->pszValue[0], pObject->numOfBiData) == false) {
902                                                                 vmsg_free_vtree_memory(vMsg);
903                                                                 return MSG_ERR_STORAGE_ERROR;
904                                                         }
905                                                         strncpy(msgInfo.msgData, MSG_IPC_DATA_PATH, MAX_MSG_DATA_LEN);
906                                                         strncat(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN-strlen(msgInfo.msgData));
907                                                         msgInfo.dataSize = strlen(fileName);
908                                                         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(msgInfo.msgType.mainType);
909                                                         if (plg == NULL) {
910                                                                 vmsg_free_vtree_memory(vMsg);
911                                                                 return MSG_ERR_NULL_POINTER;
912                                                         }
913                                                         err =  plg->restoreMsg(&msgInfo, pObject->pszValue[0], pObject->numOfBiData, NULL);
914
915                                                 } else {
916 /** From here was avaliable */
917                                                         char    retrievedFilePath[MAX_FULL_PATH_SIZE] = {0, };
918                                                         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(msgInfo.msgType.mainType);
919                                                         if (plg == NULL) {
920                                                                 vmsg_free_vtree_memory(vMsg);
921                                                                 return MSG_ERR_NULL_POINTER;
922                                                         }
923                                                         err =  plg->restoreMsg(&msgInfo, pObject->pszValue[0], pObject->numOfBiData, retrievedFilePath);
924                                                         msgInfo.bTextSms = false;
925
926                                                         char fileName[MAX_COMMON_INFO_SIZE+1];
927                                                         memset(fileName, 0x00, sizeof(fileName));
928
929                                                         if (MsgCreateFileName(fileName) == false) {
930                                                                 vmsg_free_vtree_memory(vMsg);
931                                                                 return MSG_ERR_STORAGE_ERROR;
932                                                         }
933                                                         MSG_SEC_DEBUG("fileName: %s, retrievedFilePath: %s (%d)", fileName, retrievedFilePath, strlen(retrievedFilePath));
934
935                                                         if (MsgWriteIpcFile(fileName, retrievedFilePath, strlen(retrievedFilePath)+ 1) == false) {
936                                                                 vmsg_free_vtree_memory(vMsg);
937                                                                 return MSG_ERR_STORAGE_ERROR;
938                                                         }
939                                                         strncpy(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN);
940                                                         msgInfo.dataSize = strlen(retrievedFilePath) + 1;
941
942                                                         if (err != MSG_SUCCESS)
943                                                                 return vmsg_free_vtree_memory(vMsg);
944                                                 }
945 #else
946
947                                                 msgInfo.bTextSms = false;
948
949                                                 char fileName[MAX_COMMON_INFO_SIZE+1];
950                                                 memset(fileName, 0x00, sizeof(fileName));
951
952                                                 if (MsgCreateFileName(fileName) == false) {
953                                                         vmsg_free_vtree_memory(vMsg);
954                                                         err = MSG_ERR_STORAGE_ERROR;
955                                                         goto __RETURN;
956                                                 }
957
958                                                 if (MsgWriteIpcFile(fileName, pObject->pszValue[0], pObject->numOfBiData) == false) {
959                                                         vmsg_free_vtree_memory(vMsg);
960                                                         err = MSG_ERR_STORAGE_ERROR;
961                                                         goto __RETURN;
962                                                 }
963
964                                                 /*set serialized mms data ipcfilename */
965                                                 strncpy(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN);
966 #endif
967                                         }
968                                 }
969                                 break;
970
971                                 case VCARD_TYPE_TEL : {
972                                         MSG_ADDRESS_INFO_S * addrInfo = NULL;
973
974                                         msgInfo.nAddressCnt++;
975
976                                         if (msgInfo.addressList == NULL) {
977                                                 addrInfo = (MSG_ADDRESS_INFO_S *)calloc(1, sizeof(MSG_ADDRESS_INFO_S));
978                                         } else {
979                                                 addrInfo = (MSG_ADDRESS_INFO_S *)realloc(msgInfo.addressList, msgInfo.nAddressCnt * sizeof(MSG_ADDRESS_INFO_S));
980                                         }
981
982                                         if (addrInfo == NULL) {
983                                                 vmsg_free_vtree_memory(vMsg);
984                                                 err = MSG_ERR_STORAGE_ERROR;
985                                                 goto __RETURN;
986                                         }
987
988                                         msgInfo.addressList = addrInfo;
989
990                                         msgInfo.addressList[msgInfo.nAddressCnt-1].addressType = MSG_ADDRESS_TYPE_PLMN;
991                                         msgInfo.addressList[msgInfo.nAddressCnt-1].recipientType = MSG_RECIPIENTS_TYPE_TO;
992                                         strncpy(msgInfo.addressList[msgInfo.nAddressCnt-1].addressVal, pObject->pszValue[0], MAX_ADDRESS_VAL_LEN);
993                                 }
994                                 break;
995                                 }
996
997                                 if (pObject->pSibling != NULL)
998                                         pObject = pObject->pSibling;
999                                 else
1000                                         break;
1001                         }
1002
1003                         if (vMsg->pNext != NULL) {
1004                                 vMsg = vMsg->pNext;
1005                                 pObject = vMsg->pTop;
1006                         } else {
1007                                 break;
1008                         }
1009                 }
1010
1011                 msgInfo.bBackup = true; /* Set Backup Flag */
1012                 msgInfo.storageId = MSG_STORAGE_PHONE; /* Set Storage Id */
1013                 msgInfo.priority = MSG_MESSAGE_PRIORITY_NORMAL; /* Set Priority */
1014
1015                 err = MsgStoAddMessage(&msgInfo, NULL);
1016                 if (err != MSG_SUCCESS) {
1017                         MSG_DEBUG("MsgStoAddMessage() error : %d", err);
1018                 }
1019
1020                 if (msgInfo.msgType.mainType == MSG_MMS_TYPE)
1021                         MsgStoUpdateMms(&msgInfo);
1022
1023                 if (msgIdList->nCount == 0) {
1024                         msgIdList->msgIdList = (msg_message_id_t*)calloc(1, sizeof(msg_message_id_t));
1025                 } else {
1026                         msg_message_id_t * msg_id_list;
1027                         msg_id_list = (msg_message_id_t*)realloc(msgIdList->msgIdList, sizeof(msg_message_id_t)*(msgIdList->nCount+1));
1028
1029                         if (msg_id_list) {
1030                                 msgIdList->msgIdList = msg_id_list;
1031                         } else {
1032                                 MSG_DEBUG("realloc failed");
1033                                 err = MSG_ERR_UNKNOWN;
1034                                 goto __RETURN;
1035                         }
1036                 }
1037
1038                 msgIdList->msgIdList[msgIdList->nCount] = msgInfo.msgId;
1039                 msgIdList->nCount++;
1040
1041                 vmsg_free_vtree_memory(vMsg);
1042
1043                 vMsg = NULL;
1044                 pCurrent = pTemp + strlen("END:VMSG");
1045 #ifndef MSG_FOR_DEBUG
1046         }
1047 #endif
1048         *result_id_list = msgIdList;
1049
1050 __RETURN:
1051         if (pData) {
1052                 free(pData);
1053                 pData = NULL;
1054                 pCurrent = NULL;
1055         }
1056
1057         if (*result_id_list == NULL && msgIdList) {
1058                 if (msgIdList->msgIdList) {
1059                         free(msgIdList->msgIdList);
1060                 }
1061                 free(msgIdList);
1062         }
1063
1064         return err;
1065 }
1066
1067 msg_error_t MsgStoAddPushEvent(MSG_PUSH_EVENT_INFO_S* pPushEvent)
1068 {
1069         MsgDbHandler *dbHandle = getDbHandle();
1070         char sqlQuery[MAX_QUERY_LEN+1];
1071         unsigned int rowId = 0;
1072
1073         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1074
1075         /* Check whether Same record exists or not. */
1076 #if 0
1077         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE CONTENT_TYPE LIKE '%s' AND APP_ID LIKE '%s' AND (PKG_NAME LIKE '%s' OR SECURE = 1);",
1078                         MSGFW_PUSH_CONFIG_TABLE_NAME, pPushEvent->contentType, pPushEvent->appId, pPushEvent->pkgName);
1079 #else
1080         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE CONTENT_TYPE LIKE '%s' AND APP_ID LIKE '%s';",
1081                         MSGFW_PUSH_CONFIG_TABLE_NAME, pPushEvent->contentType, pPushEvent->appId);
1082 #endif
1083
1084         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1085                 MSG_DEBUG("Query Failed [%s]", sqlQuery);
1086                 return MSG_ERR_DB_PREPARE;
1087         }
1088
1089         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
1090                 dbHandle->finalizeQuery();
1091                 return MSG_ERR_DB_ROW;
1092         }
1093         dbHandle->finalizeQuery();
1094
1095         dbHandle->beginTrans();
1096
1097         if (dbHandle->getRowId(MSGFW_PUSH_CONFIG_TABLE_NAME, &rowId) != MSG_SUCCESS) {
1098                 MSG_DEBUG("getRowId is failed!!!");
1099         }
1100
1101         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1102
1103         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, ?, ?, ?, %d, 0, 0);",
1104                         MSGFW_PUSH_CONFIG_TABLE_NAME, rowId, pPushEvent->bLaunch);
1105
1106
1107         MSG_DEBUG("QUERY : %s", sqlQuery);
1108
1109         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1110                 dbHandle->endTrans(false);
1111                 return MSG_ERR_DB_EXEC;
1112         }
1113
1114         dbHandle->bindText(pPushEvent->contentType, 1);
1115         dbHandle->bindText(pPushEvent->appId, 2);
1116         dbHandle->bindText(pPushEvent->pkgName, 3);
1117
1118         if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1119                 dbHandle->finalizeQuery();
1120                 dbHandle->endTrans(false);
1121                 return MSG_ERR_DB_EXEC;
1122         }
1123
1124         dbHandle->finalizeQuery();
1125         dbHandle->endTrans(true);
1126
1127         return MSG_SUCCESS;
1128 }
1129
1130
1131 msg_error_t MsgStoDeletePushEvent(MSG_PUSH_EVENT_INFO_S* pPushEvent)
1132 {
1133         MsgDbHandler *dbHandle = getDbHandle();
1134         char sqlQuery[MAX_QUERY_LEN+1];
1135         dbHandle->beginTrans();
1136         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1137         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE CONTENT_TYPE LIKE '%s' AND APP_ID LIKE '%s' AND PKG_NAME LIKE '%s';",
1138                         MSGFW_PUSH_CONFIG_TABLE_NAME, pPushEvent->contentType, pPushEvent->appId, pPushEvent->pkgName);
1139
1140         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
1141                 dbHandle->endTrans(false);
1142                 return MSG_ERR_DB_EXEC;
1143         }
1144         dbHandle->endTrans(true);
1145         return MSG_SUCCESS;
1146 }
1147
1148 msg_error_t MsgStoUpdatePushEvent(MSG_PUSH_EVENT_INFO_S* pSrc, MSG_PUSH_EVENT_INFO_S* pDst)
1149 {
1150         MsgDbHandler *dbHandle = getDbHandle();
1151         char sqlQuery[MAX_QUERY_LEN+1];
1152         dbHandle->beginTrans();
1153         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1154         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET CONTENT_TYPE = ?, APP_ID = ?, PKG_NAME = ?, LAUNCH = %d WHERE CONTENT_TYPE LIKE '%s' AND APP_ID LIKE '%s' AND PKG_NAME LIKE '%s';",
1155                         MSGFW_PUSH_CONFIG_TABLE_NAME, pDst->bLaunch, pSrc->contentType, pSrc->appId, pSrc->pkgName);
1156
1157         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
1158                 dbHandle->endTrans(false);
1159                 return MSG_ERR_DB_EXEC;
1160         }
1161
1162         dbHandle->bindText(pDst->contentType, 1);
1163         dbHandle->bindText(pDst->appId, 2);
1164         dbHandle->bindText(pDst->pkgName, 3);
1165
1166         if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
1167                 dbHandle->finalizeQuery();
1168                 dbHandle->endTrans(false);
1169                 return MSG_ERR_DB_EXEC;
1170         }
1171
1172         dbHandle->finalizeQuery();
1173         dbHandle->endTrans(true);
1174
1175         return MSG_SUCCESS;
1176 }