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