Change license to APLv2.0
[platform/core/messaging/msg-service.git] / framework / storage-handler / MsgStorageManager.cpp
1 /*
2  * msg-service
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <errno.h>
25
26 #include <VMessage.h>
27 #include <VCard.h>
28 #include "MsgVMessage.h"
29
30 #include "MsgDebug.h"
31 #include "MsgUtilFile.h"
32 #include "MsgUtilStorage.h"
33 #include "MsgGconfWrapper.h"
34 #include "MsgSqliteWrapper.h"
35 #include "MsgPluginManager.h"
36 #include "MsgStorageHandler.h"
37
38
39 /*==================================================================================================
40                                      VARIABLES
41 ==================================================================================================*/
42 MsgDbHandler dbHandle;
43
44
45 /*==================================================================================================
46                                      FUNCTION IMPLEMENTATION
47 ==================================================================================================*/
48 msg_error_t MsgStoConnectDB()
49 {
50         return MSG_SUCCESS;
51 }
52
53
54 msg_error_t MsgStoDisconnectDB()
55 {
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 msg_error_t MsgStoInitDB(bool bSimChanged)
68 {
69         MSG_BEGIN();
70
71         msg_error_t err = MSG_SUCCESS;
72
73 #ifdef MSG_DB_CREATE
74         if (MsgCreateConversationTable() != MSG_SUCCESS)
75                 return MSG_ERR_DB_STORAGE_INIT;
76         if (MsgCreateAddressTable() != MSG_SUCCESS)
77                 return MSG_ERR_DB_STORAGE_INIT;
78         if (MsgCreateFolderTable() != MSG_SUCCESS)
79                 return MSG_ERR_DB_STORAGE_INIT;
80         if (MsgCreateMsgTable() != MSG_SUCCESS)
81                 return MSG_ERR_DB_STORAGE_INIT;
82         if (MsgCreateSimMessageTable() != MSG_SUCCESS)
83                 return MSG_ERR_DB_STORAGE_INIT;
84         if (MsgCreateWAPMessageTable() != MSG_SUCCESS)
85                 return MSG_ERR_DB_STORAGE_INIT;
86         if (MsgCreateCBMessageTable() != MSG_SUCCESS)
87                 return MSG_ERR_DB_STORAGE_INIT;
88         if (MsgCreateSyncMLMessageTable() != MSG_SUCCESS)
89                 return MSG_ERR_DB_STORAGE_INIT;
90         if (MsgCreateSmsSendOptTable() != MSG_SUCCESS)
91                 return MSG_ERR_DB_STORAGE_INIT;
92         if (MsgCreateFilterTable() != MSG_SUCCESS)
93                 return MSG_ERR_DB_STORAGE_INIT;
94         if (MsgCreateMmsTable() != MSG_SUCCESS)
95                 return MSG_ERR_DB_STORAGE_INIT;
96
97         // Add Default Folders
98         if (MsgAddDefaultFolders() != MSG_SUCCESS) {
99                 MSG_DEBUG("Add Default Folders Fail");
100                 return MSG_ERR_DB_STORAGE_INIT;
101         }
102
103         // Add Default Address
104         if (MsgAddDefaultAddress() != MSG_SUCCESS) {
105                 MSG_DEBUG("Add Default Address Fail");
106                 return MSG_ERR_DB_STORAGE_INIT;
107         }
108 #endif
109
110         // Delete Msgs in Hidden Folder
111         MsgStoDeleteAllMessageInFolder(0, true, NULL);
112
113         // Reset network status
114         MsgStoResetNetworkStatus();
115
116         //clear abnormal mms message
117         MsgStoCleanAbnormalMmsData();
118
119         // Clear all old Sim data
120         MsgStoClearSimMessageInDB();
121
122         int smsCnt = 0, mmsCnt = 0;
123
124         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
125         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
126
127         // Set Indicator
128         MsgSettingSetIndicator(smsCnt, mmsCnt);
129
130         MSG_END();
131
132         return err;
133 }
134
135
136 msg_error_t MsgCreateConversationTable()
137 {
138         msg_error_t err = MSG_SUCCESS;
139
140         char sqlQuery[MAX_QUERY_LEN+1];
141
142         if (!dbHandle.checkTableExist(MSGFW_ADDRESS_TABLE_NAME)) {
143                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
144
145                 snprintf(sqlQuery, sizeof(sqlQuery),
146                                 "CREATE TABLE %s ( \
147                                 CONV_ID INTEGER NOT NULL , \
148                                 UNREAD_CNT INTEGER DEFAULT 0 , \
149                                 SMS_CNT INTEGER DEFAULT 0 , \
150                                 MMS_CNT INTEGER DEFAULT 0 , \
151                                 MAIN_TYPE INTEGER NOT NULL , \
152                                 SUB_TYPE INTEGER NOT NULL , \
153                                 MSG_DIRECTION INTEGER NOT NULL , \
154                                 DISPLAY_TIME INTEGER , \
155                                 DISPLAY_NAME TEXT , \
156                                 MSG_TEXT TEXT );",
157                                 MSGFW_CONVERSATION_TABLE_NAME);
158
159                 err = dbHandle.execQuery(sqlQuery);
160
161                 if (err == MSG_SUCCESS)
162                         MSG_DEBUG("SUCCESS : create %s.", MSGFW_CONVERSATION_TABLE_NAME);
163                 else
164                         MSG_DEBUG("FAIL : create %s [%d].", MSGFW_CONVERSATION_TABLE_NAME, err);
165         }
166
167         return err;
168 }
169
170
171 msg_error_t MsgCreateAddressTable()
172 {
173         msg_error_t err = MSG_SUCCESS;
174
175         char sqlQuery[MAX_QUERY_LEN+1];
176
177         if (!dbHandle.checkTableExist(MSGFW_ADDRESS_TABLE_NAME)) {
178                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
179
180                 snprintf(sqlQuery, sizeof(sqlQuery),
181                                 "CREATE TABLE %s ( \
182                                 ADDRESS_ID INTEGER PRIMARY KEY , \
183                                 CONV_ID INTEGER  NOT NULL , \
184                                 ADDRESS_TYPE INTEGER , \
185                                 RECIPIENT_TYPE INTEGER , \
186                                 ADDRESS_VAL TEXT , \
187                                 CONTACT_ID INTEGER , \
188                                 DISPLAY_NAME TEXT , \
189                                 FIRST_NAME TEXT , \
190                                 LAST_NAME TEXT , \
191                                 IMAGE_PATH TEXT , \
192                                 SYNC_TIME DATETIME , \
193                                 FOREIGN KEY(CONV_ID) REFERENCES %s (CONV_ID) );",
194                                 MSGFW_ADDRESS_TABLE_NAME, MSGFW_CONVERSATION_TABLE_NAME);
195
196                 err = dbHandle.execQuery(sqlQuery);
197
198                 if (err == MSG_SUCCESS)
199                         MSG_DEBUG("SUCCESS : create %s.", MSGFW_ADDRESS_TABLE_NAME);
200                 else
201                         MSG_DEBUG("FAIL : create %s [%d].", MSGFW_ADDRESS_TABLE_NAME, err);
202         }
203
204         return err;
205 }
206
207
208 msg_error_t MsgCreateFolderTable()
209 {
210         msg_error_t err = MSG_SUCCESS;
211
212         char sqlQuery[MAX_QUERY_LEN+1];
213
214         if (!dbHandle.checkTableExist(MSGFW_FOLDER_TABLE_NAME)) {
215                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
216
217                 snprintf(sqlQuery, sizeof(sqlQuery),
218                                 "CREATE TABLE %s ( \
219                                 FOLDER_ID INTEGER PRIMARY KEY, \
220                                 FOLDER_NAME TEXT NOT NULL, \
221                                 FOLDER_TYPE INTEGER DEFAULT 0 );",
222                                 MSGFW_FOLDER_TABLE_NAME);
223
224                 err = dbHandle.execQuery(sqlQuery);
225
226                 if (err == MSG_SUCCESS)
227                         MSG_DEBUG("SUCCESS : create %s.", MSGFW_FOLDER_TABLE_NAME);
228                 else
229                         MSG_DEBUG("FAIL : create %s [%d].", MSGFW_FOLDER_TABLE_NAME, err);
230         }
231
232         return err;
233 }
234
235
236 msg_error_t MsgCreateMsgTable()
237 {
238         msg_error_t err = MSG_SUCCESS;
239
240         char sqlQuery[MAX_QUERY_LEN+1];
241
242         if (!dbHandle.checkTableExist(MSGFW_MESSAGE_TABLE_NAME)) {
243                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
244
245                 snprintf(sqlQuery, sizeof(sqlQuery),
246                                 "CREATE TABLE %s ( \
247                                 MSG_ID INTEGER PRIMARY KEY , \
248                                 CONV_ID INTEGER NOT NULL , \
249                                 FOLDER_ID INTEGER NOT NULL , \
250                                 STORAGE_ID INTEGER NOT NULL , \
251                                 MAIN_TYPE INTEGER NOT NULL , \
252                                 SUB_TYPE INTEGER NOT NULL , \
253                                 DISPLAY_TIME DATETIME , \
254                                 DATA_SIZE INTEGER DEFAULT 0 , \
255                                 NETWORK_STATUS INTEGER DEFAULT 0 , \
256                                 READ_STATUS INTEGER DEFAULT 0 , \
257                                 PROTECTED INTEGER DEFAULT 0 , \
258                                 PRIORITY INTEGER DEFAULT 0 , \
259                                 MSG_DIRECTION INTEGER NOT NULL , \
260                                 SCHEDULED_TIME DATETIME , \
261                                 BACKUP INTEGER DEFAULT 0 , \
262                                 SUBJECT TEXT , \
263                                 MSG_DATA TEXT , \
264                                 THUMB_PATH TEXT , \
265                                 MSG_TEXT TEXT , \
266                                 ATTACHMENT_COUNT INTEGER DEFAULT 0 , \
267                                 FOREIGN KEY(CONV_ID) REFERENCES %s (CONV_ID) , \
268                                 FOREIGN KEY(FOLDER_ID) REFERENCES %s (FOLDER_ID) );",
269                                 MSGFW_MESSAGE_TABLE_NAME, MSGFW_CONVERSATION_TABLE_NAME, MSGFW_FOLDER_TABLE_NAME);
270
271                 err = dbHandle.execQuery(sqlQuery);
272
273                 if (err == MSG_SUCCESS)
274                         MSG_DEBUG("SUCCESS : create %s.", MSGFW_MESSAGE_TABLE_NAME);
275                 else
276                         MSG_DEBUG("FAIL : create %s [%d].", MSGFW_MESSAGE_TABLE_NAME, err);
277         }
278
279         return err;
280 }
281
282
283 msg_error_t MsgCreateSimMessageTable()
284 {
285         msg_error_t err = MSG_SUCCESS;
286
287         char sqlQuery[MAX_QUERY_LEN+1];
288
289         if (!dbHandle.checkTableExist(MSGFW_SIM_MSG_TABLE_NAME)) {
290                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
291
292                 snprintf(sqlQuery, sizeof(sqlQuery),
293                                 "CREATE TABLE %s ( \
294                                 MSG_ID INTEGER , \
295                                 SIM_ID INTEGER NOT NULL , \
296                                 FOREIGN KEY(MSG_ID) REFERENCES %s (MSG_ID) );",
297                                 MSGFW_SIM_MSG_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
298
299                 err = dbHandle.execQuery(sqlQuery);
300
301                 if (err == MSG_SUCCESS)
302                         MSG_DEBUG("SUCCESS : create %s.", MSGFW_SIM_MSG_TABLE_NAME);
303                 else
304                         MSG_DEBUG("FAIL : create %s [%d].", MSGFW_SIM_MSG_TABLE_NAME, err);
305         }
306
307         return err;
308 }
309
310
311 msg_error_t MsgCreateWAPMessageTable()
312 {
313         msg_error_t err = MSG_SUCCESS;
314
315         char sqlQuery[MAX_QUERY_LEN+1];
316
317         if (!dbHandle.checkTableExist(MSGFW_PUSH_MSG_TABLE_NAME)) {
318                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
319
320                 snprintf(sqlQuery, sizeof(sqlQuery),
321                                 "CREATE TABLE %s ( \
322                                 MSG_ID INTEGER , \
323                                 ACTION INTEGER , \
324                                 CREATED INTEGER , \
325                                 EXPIRES INTEGER , \
326                                 ID TEXT , \
327                                 HREF TEXT , \
328                                 CONTENT TEXT , \
329                                 FOREIGN KEY(MSG_ID) REFERENCES %s(MSG_ID) );",
330                                 MSGFW_PUSH_MSG_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
331
332                 err = dbHandle.execQuery(sqlQuery);
333
334                 if (err == MSG_SUCCESS)
335                         MSG_DEBUG("SUCCESS : create %s.", MSGFW_PUSH_MSG_TABLE_NAME);
336                 else
337                         MSG_DEBUG("FAIL : create %s [%d].", MSGFW_PUSH_MSG_TABLE_NAME, err);
338         }
339
340         return err;
341 }
342
343
344 msg_error_t MsgCreateCBMessageTable()
345 {
346         msg_error_t err = MSG_SUCCESS;
347
348         char sqlQuery[MAX_QUERY_LEN+1];
349
350         if (!dbHandle.checkTableExist(MSGFW_CB_MSG_TABLE_NAME)) {
351                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
352
353                 snprintf(sqlQuery, sizeof(sqlQuery),
354                                 "CREATE TABLE %s ( \
355                                 MSG_ID INTEGER , \
356                                 CB_MSG_ID INTEGER NOT NULL , \
357                                 FOREIGN KEY(MSG_ID) REFERENCES %s (MSG_ID) );",
358                                 MSGFW_CB_MSG_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
359
360                 err = dbHandle.execQuery(sqlQuery);
361
362                 if (err == MSG_SUCCESS)
363                         MSG_DEBUG("SUCCESS : create %s.", MSGFW_CB_MSG_TABLE_NAME);
364                 else
365                         MSG_DEBUG("FAIL : create %s [%d].", MSGFW_CB_MSG_TABLE_NAME, err);
366         }
367
368         return err;
369 }
370
371
372 msg_error_t MsgCreateSyncMLMessageTable()
373 {
374         msg_error_t err = MSG_SUCCESS;
375
376         char sqlQuery[MAX_QUERY_LEN+1];
377
378         if (!dbHandle.checkTableExist(MSGFW_SYNCML_MSG_TABLE_NAME)) {
379                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
380
381                 snprintf(sqlQuery, sizeof(sqlQuery),
382                                 "CREATE TABLE %s ( \
383                                 MSG_ID INTEGER , \
384                                 EXT_ID INTEGER NOT NULL , \
385                                 PINCODE INTEGER NOT NULL , \
386                                 FOREIGN KEY(MSG_ID) REFERENCES %s(MSG_ID) );",
387                                 MSGFW_SYNCML_MSG_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
388
389                 err = dbHandle.execQuery(sqlQuery);
390
391                 if (err == MSG_SUCCESS)
392                         MSG_DEBUG("SUCCESS : create %s.", MSGFW_SYNCML_MSG_TABLE_NAME);
393                 else
394                         MSG_DEBUG("FAIL : create %s [%d].", MSGFW_SYNCML_MSG_TABLE_NAME, err);
395         }
396
397         return err;
398 }
399
400 msg_error_t MsgCreateSmsSendOptTable()
401 {
402         msg_error_t err = MSG_SUCCESS;
403
404         char sqlQuery[MAX_QUERY_LEN+1];
405
406         if (!dbHandle.checkTableExist(MSGFW_SMS_SENDOPT_TABLE_NAME)) {
407                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
408
409                 snprintf(sqlQuery, sizeof(sqlQuery),
410                                 "CREATE TABLE %s ( \
411                                 MSG_ID INTEGER , \
412                                 DELREP_REQ INTEGER NOT NULL , \
413                                 KEEP_COPY INTEGER NOT NULL , \
414                                 REPLY_PATH INTEGER NOT NULL , \
415                                 FOREIGN KEY(MSG_ID) REFERENCES %s (MSG_ID) );",
416                                 MSGFW_SMS_SENDOPT_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
417
418                 err = dbHandle.execQuery(sqlQuery);
419
420                 if (err == MSG_SUCCESS)
421                                 MSG_DEBUG("SUCCESS : create %s.", MSGFW_SMS_SENDOPT_TABLE_NAME);
422                 else
423                                 MSG_DEBUG("FAIL : create %s [%d].", MSGFW_SMS_SENDOPT_TABLE_NAME, err);
424         }
425
426         return err;
427 }
428
429
430 msg_error_t MsgCreateFilterTable()
431 {
432         msg_error_t err = MSG_SUCCESS;
433
434         char sqlQuery[MAX_QUERY_LEN+1];
435
436         if (!dbHandle.checkTableExist(MSGFW_FILTER_TABLE_NAME)) {
437                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
438
439                 snprintf(sqlQuery, sizeof(sqlQuery),
440                                 "CREATE TABLE %s ( \
441                                 FILTER_ID INTEGER PRIMARY KEY , \
442                                 FILTER_TYPE INTEGER NOT NULL , \
443                                 FILTER_VALUE TEXT NOT NULL , \
444                                 FILTER_ACTIVE INTEGER DEFAULT 0);",
445                                 MSGFW_FILTER_TABLE_NAME);
446
447                 err = dbHandle.execQuery(sqlQuery);
448
449                 if (err == MSG_SUCCESS)
450                                 MSG_DEBUG("SUCCESS : create %s.", MSGFW_FILTER_TABLE_NAME);
451                 else
452                                 MSG_DEBUG("FAIL : create %s [%d].", MSGFW_FILTER_TABLE_NAME, err);
453         }
454
455         return err;
456 }
457
458
459 msg_error_t MsgCreateMmsTable()
460 {
461         msg_error_t err = MSG_SUCCESS;
462
463         char sqlQuery[MAX_QUERY_LEN+1];
464
465         if (!dbHandle.checkTableExist(MMS_PLUGIN_MESSAGE_TABLE_NAME)) {
466                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
467
468                 snprintf(sqlQuery, sizeof(sqlQuery),
469                                 "CREATE TABLE %s ( \
470                                 MSG_ID INTEGER , \
471                                 TRANSACTION_ID TEXT , \
472                                 MESSAGE_ID TEXT , \
473                                 FWD_MESSAGE_ID TEXT , \
474                                 CONTENTS_LOCATION TEXT , \
475                                 FILE_PATH TEXT , \
476                                 VERSION INTEGER NOT NULL , \
477                                 DATA_TYPE INTEGER DEFAULT -1 , \
478                                 DATE DATETIME , \
479                                 HIDE_ADDRESS INTEGER DEFAULT 0 , \
480                                 ASK_DELIVERY_REPORT INTEGER DEFAULT 0 , \
481                                 REPORT_ALLOWED INTEGER DEFAULT 0 , \
482                                 READ_REPORT_ALLOWED_TYPE INTEGER DEFAULT 0 , \
483                                 ASK_READ_REPLY INTEGER DEFAULT 0 , \
484                                 READ INTEGER DEFAULT 0 , \
485                                 READ_REPORT_SEND_STATUS INTEGER DEFAULT 0 , \
486                                 READ_REPORT_SENT INTEGER DEFAULT 0 , \
487                                 PRIORITY INTEGER DEFAULT 0 , \
488                                 KEEP_COPY INTEGER DEFAULT 0 , \
489                                 MSG_SIZE INTEGER NOT NULL , \
490                                 MSG_CLASS INTEGER DEFAULT -1 , \
491                                 EXPIRY_TIME DATETIME , \
492                                 CUSTOM_DELIVERY_TIME INTEGER DEFAULT 0 , \
493                                 DELIVERY_TIME DATETIME , \
494                                 MSG_STATUS INTEGER DEFAULT -1 , \
495                                 FOREIGN KEY(MSG_ID) REFERENCES %s (MSG_ID) );",
496                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
497
498                 err = dbHandle.execQuery(sqlQuery);
499
500                 if (err == MSG_SUCCESS)
501                         MSG_DEBUG("SUCCESS : create %s.", MMS_PLUGIN_MESSAGE_TABLE_NAME);
502                 else
503                         MSG_DEBUG("FAIL : create %s [%d].", MMS_PLUGIN_MESSAGE_TABLE_NAME, err);
504         }
505
506         return err;
507 }
508
509
510 msg_error_t MsgAddDefaultFolders()
511 {
512         int nRowCnt = 0;
513         int nResult = 0;
514
515         char sqlQuery[MAX_QUERY_LEN+1];
516
517         // INBOX
518         memset(sqlQuery, 0x00, sizeof(sqlQuery));
519         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
520                         MSGFW_FOLDER_TABLE_NAME, MSG_INBOX_ID);
521
522         if (dbHandle.getTable(sqlQuery, &nRowCnt) != MSG_SUCCESS) {
523                 dbHandle.freeTable();
524                 return MSG_ERR_DB_GETTABLE;
525         }
526
527         nResult = dbHandle.getColumnToInt(1);
528
529         dbHandle.freeTable();
530
531         if (nResult == 0) {
532                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
533                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'INBOX', %d);",
534                                 MSGFW_FOLDER_TABLE_NAME, MSG_INBOX_ID, MSG_FOLDER_TYPE_INBOX);
535
536                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
537                         return MSG_ERR_DB_EXEC;
538         }
539
540         // OUTBOX
541         memset(sqlQuery, 0x00, sizeof(sqlQuery));
542         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
543                         MSGFW_FOLDER_TABLE_NAME, MSG_OUTBOX_ID);
544
545         if (dbHandle.getTable(sqlQuery, &nRowCnt) != MSG_SUCCESS) {
546                 dbHandle.freeTable();
547                 return MSG_ERR_DB_GETTABLE;
548         }
549
550         nResult = dbHandle.getColumnToInt(1);
551
552         dbHandle.freeTable();
553
554         if (nResult == 0) {
555                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
556                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'OUTBOX', %d);",
557                                 MSGFW_FOLDER_TABLE_NAME, MSG_OUTBOX_ID, MSG_FOLDER_TYPE_OUTBOX);
558
559                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
560                         return MSG_ERR_DB_EXEC;
561         }
562
563         // SENTBOX
564         memset(sqlQuery, 0x00, sizeof(sqlQuery));
565         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
566                         MSGFW_FOLDER_TABLE_NAME, MSG_SENTBOX_ID);
567
568         if (dbHandle.getTable(sqlQuery, &nRowCnt) != MSG_SUCCESS) {
569                 dbHandle.freeTable();
570                 return MSG_ERR_DB_GETTABLE;
571         }
572
573         nResult = dbHandle.getColumnToInt(1);
574
575         dbHandle.freeTable();
576
577         if (nResult == 0) {
578                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
579                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'SENTBOX', %d);",
580                                 MSGFW_FOLDER_TABLE_NAME, MSG_SENTBOX_ID, MSG_FOLDER_TYPE_OUTBOX);
581
582                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
583                         return MSG_ERR_DB_EXEC;
584         }
585
586         // DRAFT
587         memset(sqlQuery, 0x00, sizeof(sqlQuery));
588         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
589                         MSGFW_FOLDER_TABLE_NAME, MSG_DRAFT_ID);
590
591         if (dbHandle.getTable(sqlQuery, &nRowCnt) != MSG_SUCCESS) {
592                 dbHandle.freeTable();
593                 return MSG_ERR_DB_GETTABLE;
594         }
595
596         nResult = dbHandle.getColumnToInt(1);
597
598         dbHandle.freeTable();
599
600         if (nResult == 0) {
601                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
602                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'DRAFT', %d);",
603                                 MSGFW_FOLDER_TABLE_NAME, MSG_DRAFT_ID, MSG_FOLDER_TYPE_DRAFT);
604
605                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
606                         return MSG_ERR_DB_EXEC;
607         }
608
609         // CBMSGBOX
610         memset(sqlQuery, 0x00, sizeof(sqlQuery));
611         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
612                         MSGFW_FOLDER_TABLE_NAME, MSG_CBMSGBOX_ID);
613
614         if (dbHandle.getTable(sqlQuery, &nRowCnt) != MSG_SUCCESS) {
615                 dbHandle.freeTable();
616                 return MSG_ERR_DB_GETTABLE;
617         }
618
619         nResult = dbHandle.getColumnToInt(1);
620
621         dbHandle.freeTable();
622
623         if (nResult == 0) {
624                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
625                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'CBMSGBOX', %d);",
626                                 MSGFW_FOLDER_TABLE_NAME, MSG_CBMSGBOX_ID, MSG_FOLDER_TYPE_INBOX);
627
628                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
629                         return MSG_ERR_DB_EXEC;
630         }
631
632         // SPAMBOX
633         memset(sqlQuery, 0x00, sizeof(sqlQuery));
634         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE FOLDER_ID = %d;",
635                         MSGFW_FOLDER_TABLE_NAME, MSG_SPAMBOX_ID);
636
637         if (dbHandle.getTable(sqlQuery, &nRowCnt) != MSG_SUCCESS) {
638                 dbHandle.freeTable();
639                 return MSG_ERR_DB_GETTABLE;
640         }
641
642         nResult = dbHandle.getColumnToInt(1);
643
644         dbHandle.freeTable();
645
646         if (nResult == 0) {
647                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
648                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, 'SPAMBOX', %d);",
649                                 MSGFW_FOLDER_TABLE_NAME, MSG_SPAMBOX_ID, MSG_FOLDER_TYPE_SPAMBOX);
650
651                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
652                         return MSG_ERR_DB_EXEC;
653         }
654
655         return MSG_SUCCESS;
656 }
657
658
659 msg_error_t MsgAddDefaultAddress()
660 {
661         MSG_BEGIN();
662
663         int nRowCnt = 0, nResult = 0;
664
665         char sqlQuery[MAX_QUERY_LEN+1];
666
667         memset(sqlQuery, 0x00, sizeof(sqlQuery));
668         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(*) FROM %s WHERE ADDRESS_ID = 0;",
669                         MSGFW_ADDRESS_TABLE_NAME);
670
671         if (dbHandle.getTable(sqlQuery, &nRowCnt) != MSG_SUCCESS) {
672                 dbHandle.freeTable();
673                 return MSG_ERR_DB_GETTABLE;
674         }
675
676         nResult = dbHandle.getColumnToInt(1);
677
678         dbHandle.freeTable();
679
680         if (nResult == 0) {
681                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
682                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (0, 0, 0, '', 0, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 0, '');",
683                                 MSGFW_ADDRESS_TABLE_NAME);
684
685                 MSG_DEBUG("%s", sqlQuery);
686
687                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
688                         return MSG_ERR_DB_EXEC;
689         }
690
691         MSG_END();
692
693         return MSG_SUCCESS;
694 }
695
696
697 msg_error_t MsgStoResetDatabase()
698 {
699         msg_error_t err = MSG_SUCCESS;
700
701         char sqlQuery[MAX_QUERY_LEN+1];
702
703         const char* tableList[] = {MSGFW_FOLDER_TABLE_NAME, MSGFW_FILTER_TABLE_NAME,
704                         MSGFW_PUSH_MSG_TABLE_NAME, MSGFW_CB_MSG_TABLE_NAME,
705                         MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_SYNCML_MSG_TABLE_NAME,
706                         MSGFW_SMS_SENDOPT_TABLE_NAME};
707
708         int listCnt = sizeof(tableList)/sizeof(char*);
709
710         dbHandle.beginTrans();
711
712         // Delete Database
713         for (int i = 0; i < listCnt; i++)
714         {
715                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
716                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s;", tableList[i]);
717
718                 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
719                         dbHandle.endTrans(false);
720                         return MSG_ERR_DB_EXEC;
721                 }
722         }
723
724         // Delete Message Table
725         memset(sqlQuery, 0x00, sizeof(sqlQuery));
726         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE STORAGE_ID <> %d;",
727                         MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_SIM);
728
729         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
730                 dbHandle.endTrans(false);
731                 return MSG_ERR_DB_EXEC;
732         }
733
734         // Delete Conversation Table
735         err = MsgStoClearConversationTable(&dbHandle);
736
737         if (err != MSG_SUCCESS) {
738                 dbHandle.endTrans(false);
739                 return err;
740         }
741
742         // Add Default Folders
743         if (MsgAddDefaultFolders() != MSG_SUCCESS) {
744                 MSG_DEBUG("Add Default Folders Fail");
745                 dbHandle.endTrans(false);
746                 return MSG_ERR_DB_STORAGE_INIT;
747         }
748
749         // Add Default Address
750         if (MsgAddDefaultAddress() != MSG_SUCCESS) {
751                 MSG_DEBUG("Add Default Address Fail");
752                 dbHandle.endTrans(false);
753                 return MSG_ERR_DB_STORAGE_INIT;
754         }
755
756         dbHandle.endTrans(true);
757
758         // Delete MMS Files
759         MsgRmRf((char*)MSG_DATA_PATH);
760         MsgRmRf((char*)MSG_SMIL_FILE_PATH);
761
762         // Reset SMS Count
763         if (MsgSettingSetIndicator(0, 0) != MSG_SUCCESS) {
764                 MSG_DEBUG("MsgSettingSetIndicator() FAILED");
765                 return MSG_ERR_SET_SETTING;
766         }
767
768         // Reset MMS Count
769         if (MsgSettingSetIndicator(0, 0) != MSG_SUCCESS) {
770                 MSG_DEBUG("MsgSettingSetIndicator() FAILED");
771                 return MSG_ERR_SET_SETTING;
772         }
773
774         return MSG_SUCCESS;
775 }
776
777
778 msg_error_t MsgStoBackupMessage(msg_message_backup_type_t type, const char *filepath)
779 {
780         msg_error_t     err = MSG_SUCCESS;
781
782         char sqlQuery[MAX_QUERY_LEN+1];
783         int rowCnt = 0;
784         int index = 0;
785         MSG_MESSAGE_INFO_S       msgInfo = {0, };
786         char*                   encoded_data = NULL;
787
788         char fileName[MSG_FILENAME_LEN_MAX+1];
789         memset(fileName, 0x00, sizeof(fileName));
790         strncpy(fileName, filepath, MSG_FILENAME_LEN_MAX);
791         if (remove(fileName) != 0) {
792                 MSG_DEBUG("Fail to delete [%s].", fileName);
793         }
794
795         memset(sqlQuery, 0x00, sizeof(sqlQuery));
796
797         MSG_DEBUG("backup type = %d, path = %s", type, filepath);
798
799         if (type == MSG_BACKUP_TYPE_SMS) {
800                 snprintf(sqlQuery, sizeof(sqlQuery),
801                                 "SELECT MSG_ID FROM %s "
802                                 "WHERE STORAGE_ID = %d AND MAIN_TYPE = %d;",
803                                 MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_PHONE, MSG_SMS_TYPE);
804         } else if (type == MSG_BACKUP_TYPE_MMS) {
805                 snprintf(sqlQuery, sizeof(sqlQuery),
806                                 "SELECT MSG_ID FROM %s "
807                                 "WHERE STORAGE_ID = %d AND MAIN_TYPE = %d;",
808                                 MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_PHONE, MSG_MMS_TYPE);
809         } else if (type == MSG_BACKUP_TYPE_ALL) {
810                 snprintf(sqlQuery, sizeof(sqlQuery),
811                                 "SELECT MSG_ID FROM %s "
812                                 "WHERE STORAGE_ID = %d;",
813                                 MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_PHONE);
814
815         }
816
817         err = dbHandle.getTable(sqlQuery, &rowCnt);
818
819         if (err != MSG_SUCCESS) {
820                 dbHandle.freeTable();
821                 return err;
822         }
823         MSG_DEBUG("backup number = %d", rowCnt);
824
825         for (int i = 0; i < rowCnt; i++) {
826                 err = MsgStoGetMessage(dbHandle.getColumnToInt(++index), &msgInfo, NULL);
827                 if(err != MSG_SUCCESS) {
828                         dbHandle.freeTable();
829                         return err;
830                 }
831
832                 encoded_data = MsgVMessageAddRecord(&dbHandle, &msgInfo);
833
834                 if (encoded_data != NULL) {
835                         if (MsgAppendFile(fileName, encoded_data, strlen(encoded_data)) == false) {
836                                 dbHandle.freeTable();
837                                 free(encoded_data);
838                                 return MSG_ERR_STORAGE_ERROR;
839                         }
840
841                         free(encoded_data);
842                 }
843
844                 memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
845         }
846
847         dbHandle.freeTable();
848         MSG_END();
849         return MSG_SUCCESS;
850
851 }
852
853 msg_error_t MsgStoUpdateMms(MSG_MESSAGE_INFO_S *pMsg)
854 {
855         char sqlQuery[MAX_QUERY_LEN+1];
856
857         if (pMsg->msgType.subType == MSG_SENDCONF_MMS) {
858                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
859
860                 dbHandle.beginTrans();
861                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET THUMB_PATH = ?, MSG_TEXT = ? WHERE MSG_ID = %d;",
862                                 MSGFW_MESSAGE_TABLE_NAME, pMsg->msgId);
863
864                 if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
865                         dbHandle.endTrans(false);
866                         return MSG_ERR_DB_EXEC;
867                 }
868
869
870                 dbHandle.bindText(pMsg->thumbPath, 1);
871                 dbHandle.bindText(pMsg->msgText, 2);
872                 MSG_DEBUG("thumbPath = %s , msgText = %s" , pMsg->thumbPath, pMsg->msgText);
873                 MSG_DEBUG("%s", sqlQuery);
874
875                 if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
876                         dbHandle.finalizeQuery();
877                         dbHandle.endTrans(false);
878                         return MSG_ERR_DB_EXEC;
879                 }
880
881                 dbHandle.finalizeQuery();
882                 dbHandle.endTrans(true);
883         } else {
884                 if (MsgStoUpdateMMSMessage(pMsg) != MSG_SUCCESS)
885                         MSG_DEBUG("MsgStoUpdateMMSMessage is failed");
886         }
887         return MSG_SUCCESS;
888 }
889
890 msg_error_t MsgStoRestoreMessage(const char *filepath)
891 {
892         msg_error_t err = MSG_SUCCESS;
893         MSG_MESSAGE_INFO_S msgInfo = {0,};
894
895         VTree* vMsg = NULL;
896         VObject* pObject = NULL;
897
898         int dataSize = 0;
899
900         char fileName[MSG_FILENAME_LEN_MAX+1];
901         char *pData = NULL;
902         char *pCurrent = NULL;
903         char *pTemp = NULL;
904
905 #ifdef MSG_FOR_DEBUG
906         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";
907         vMsg = vmsg_decode(sample);
908 #else
909         memset(fileName, 0x00, sizeof(fileName));
910         strncpy(fileName, filepath, MSG_FILENAME_LEN_MAX);
911         pData = MsgOpenAndReadMmsFile(fileName, 0, -1, &dataSize);
912         if (pData == NULL)
913                 return MSG_ERR_STORAGE_ERROR;
914
915         pCurrent = pData;
916
917         while ((pTemp = strstr(pCurrent, "END:VMSG")) != NULL)
918         {
919                 MSG_DEBUG("Start Position: %s", pCurrent);
920
921                 while (*pCurrent == '\r' || *pCurrent == '\n')
922                         pCurrent++;
923
924                 MSG_DEBUG("Start Position2: %s", pCurrent);
925
926                 vMsg = vmsg_decode(pCurrent);
927 #endif
928
929                 pObject = vMsg->pTop;
930
931                 memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
932
933                 while (1)
934                 {
935                         while (1)
936                         {
937                                 MSG_DEBUG("pObject type [%d], pObject Value [%s]", pObject->property, pObject->pszValue[0]);
938
939                                 switch (pObject->property)
940                                 {
941                                         case VMSG_TYPE_MSGTYPE :
942                                         {
943                                                 if (!strncmp(pObject->pszValue[0], "SMS", strlen("SMS"))) {
944                                                         msgInfo.msgType.mainType = MSG_SMS_TYPE;
945                                                         msgInfo.msgType.subType = MSG_NORMAL_SMS;
946                                                 } else if (!strncmp(pObject->pszValue[0], "MMS RETRIEVED", strlen("MMS RETRIEVED"))) {
947                                                         msgInfo.msgType.mainType = MSG_MMS_TYPE;
948                                                         msgInfo.msgType.subType = MSG_RETRIEVE_AUTOCONF_MMS;
949                                                 } else if (!strncmp(pObject->pszValue[0], "MMS SEND", strlen("MMS SEND"))) {
950                                                         msgInfo.msgType.mainType = MSG_MMS_TYPE;
951                                                         msgInfo.msgType.subType = MSG_SENDCONF_MMS;
952                                                 } else if (!strncmp(pObject->pszValue[0], "MMS NOTIFICATION", strlen("MMS NOTIFICATION"))) {
953                                                         msgInfo.msgType.mainType = MSG_MMS_TYPE;
954                                                         msgInfo.msgType.subType = MSG_NOTIFICATIONIND_MMS;
955                                                 } else {
956                                                         vmsg_free_vtree_memory(vMsg);
957                                                         return MSG_ERR_STORAGE_ERROR;
958                                                 }
959                                         }
960                                         break;
961
962                                         case VMSG_TYPE_MSGBOX :
963                                         {
964                                                 if(!strncmp(pObject->pszValue[0], "INBOX", strlen("INBOX"))) {
965                                                         msgInfo.folderId= MSG_INBOX_ID;
966                                                         msgInfo.direction=MSG_DIRECTION_TYPE_MT;
967
968                                                         msgInfo.networkStatus=MSG_NETWORK_RECEIVED;
969                                                 } else if(!strncmp(pObject->pszValue[0], "OUTBOX", strlen("OUTBOX"))) {
970                                                         msgInfo.folderId= MSG_OUTBOX_ID;
971                                                         msgInfo.direction=MSG_DIRECTION_TYPE_MO;
972
973                                                         msgInfo.networkStatus=MSG_NETWORK_SEND_FAIL;
974                                                 } else if(!strncmp(pObject->pszValue[0], "SENTBOX", strlen("SENTBOX"))) {
975                                                         msgInfo.folderId= MSG_SENTBOX_ID;
976                                                         msgInfo.direction=MSG_DIRECTION_TYPE_MO;
977
978                                                         msgInfo.networkStatus=MSG_NETWORK_SEND_SUCCESS;
979                                                 } else if(!strncmp(pObject->pszValue[0], "DRAFTBOX", strlen("DRAFTBOX"))) {
980                                                         msgInfo.folderId=MSG_DRAFT_ID;
981                                                         msgInfo.direction=MSG_DIRECTION_TYPE_MO;
982
983                                                         msgInfo.networkStatus=MSG_NETWORK_NOT_SEND;
984                                                 } else {
985                                                         vmsg_free_vtree_memory(vMsg);
986                                                         return MSG_ERR_STORAGE_ERROR;
987                                                 }
988                                         }
989                                         break;
990
991                                         case VMSG_TYPE_STATUS :
992                                         {
993                                                 if(!strncmp(pObject->pszValue[0], "READ", strlen("READ"))) {
994                                                         msgInfo.bRead = true;
995                                                 } else if(!strncmp(pObject->pszValue[0], "UNREAD", strlen("UNREAD"))) {
996                                                         msgInfo.bRead = false;
997                                                 } else {
998                                                         vmsg_free_vtree_memory(vMsg);
999                                                         return MSG_ERR_STORAGE_ERROR;
1000                                                 }
1001                                         }
1002                                         break;
1003
1004                                         case VMSG_TYPE_DATE :
1005                                         {
1006                                                 struct tm       displayTime;
1007
1008                                                 if (!_convert_vdata_str_to_tm(pObject->pszValue[0], &displayTime)) {
1009                                                         vmsg_free_vtree_memory( vMsg );
1010                                                         return MSG_ERR_STORAGE_ERROR;
1011                                                 }
1012
1013                                                 msgInfo.displayTime = mktime(&displayTime);
1014                                         }
1015                                         break;
1016
1017                                         case VMSG_TYPE_SUBJECT :
1018                                         {
1019                                                 MSG_DEBUG("subject length is [%d].", strlen(pObject->pszValue[0]));
1020
1021                                                 if(strlen(pObject->pszValue[0]) > 0) {
1022                                                         strncpy(msgInfo.subject, pObject->pszValue[0], MAX_SUBJECT_LEN);
1023                                                         if ( msgInfo.subject[strlen(pObject->pszValue[0])-1] == '\r' )
1024                                                                 msgInfo.subject[strlen(pObject->pszValue[0])-1]= '\0';
1025                                                 }
1026                                         }
1027                                         break;
1028
1029                                         case VMSG_TYPE_BODY :
1030                                         {
1031                                                 if (msgInfo.msgType.mainType == MSG_SMS_TYPE) {
1032                                                         if (pObject->numOfBiData > MAX_MSG_DATA_LEN) {
1033                                                                 msgInfo.bTextSms = false;
1034                                                                 char fileName[MAX_COMMON_INFO_SIZE + 1];
1035                                                                 memset(fileName, 0x00, sizeof(fileName));
1036
1037                                                                 if (MsgCreateFileName(fileName) == false) {
1038                                                                         vmsg_free_vtree_memory(vMsg);
1039                                                                         return MSG_ERR_STORAGE_ERROR;
1040                                                                 }
1041
1042                                                                 if (MsgWriteIpcFile(fileName, pObject->pszValue[0], pObject->numOfBiData) == false) {
1043                                                                         vmsg_free_vtree_memory(vMsg);
1044                                                                         return MSG_ERR_STORAGE_ERROR;
1045                                                                 }
1046
1047                                                                 strncpy(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN);
1048                                                                 msgInfo.dataSize = pObject->numOfBiData;
1049                                                         } else {
1050                                                                 msgInfo.bTextSms = true;
1051
1052                                                                 if(pObject->numOfBiData > 0) {
1053                                                                         memset(msgInfo.msgText, 0x00, sizeof(msgInfo.msgText));
1054                                                                         memcpy(msgInfo.msgText, pObject->pszValue[0], pObject->numOfBiData);
1055
1056                                                                         msgInfo.dataSize = pObject->numOfBiData;
1057                                                                 }
1058                                                         }
1059                                                 } else {
1060                                                         msgInfo.bTextSms = true;
1061                                                         if(msgInfo.msgType.subType == MSG_NOTIFICATIONIND_MMS) {
1062
1063                                                                         msgInfo.bTextSms = true;
1064
1065                                                                 // Save Message Data into File
1066                                                                 char fileName[MAX_COMMON_INFO_SIZE+1];
1067                                                                 memset(fileName, 0x00, sizeof(fileName));
1068
1069                                                                 if (MsgCreateFileName(fileName) == false) {
1070                                                                         vmsg_free_vtree_memory(vMsg);
1071                                                                         return MSG_ERR_STORAGE_ERROR;
1072                                                                 }
1073
1074                                                                 if (MsgWriteIpcFile(fileName, pObject->pszValue[0], pObject->numOfBiData) == false) {
1075                                                                         vmsg_free_vtree_memory(vMsg);
1076                                                                         return MSG_ERR_STORAGE_ERROR;
1077                                                                 }
1078                                                                 strncpy(msgInfo.msgData, MSG_IPC_DATA_PATH, MAX_MSG_DATA_LEN);
1079                                                                 strncat(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN-strlen(msgInfo.msgData));
1080                                                                 msgInfo.dataSize = strlen(fileName);
1081                                                                 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(msgInfo.msgType.mainType);
1082                                                                 err =  plg->restoreMsg(&msgInfo, pObject->pszValue[0], pObject->numOfBiData, NULL);
1083
1084                                                         } else {
1085 //////////////// From here was avaliable
1086                                                                 char    retrievedFilePath[MAX_FULL_PATH_SIZE] = {0,};
1087                                                                 MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(msgInfo.msgType.mainType);
1088                                                                 err =  plg->restoreMsg(&msgInfo, pObject->pszValue[0], pObject->numOfBiData, retrievedFilePath);
1089                                                                 msgInfo.bTextSms = false;
1090
1091                                                                 char fileName[MAX_COMMON_INFO_SIZE+1];
1092                                                                 memset(fileName, 0x00, sizeof(fileName));
1093
1094                                                                 if (MsgCreateFileName(fileName) == false) {
1095                                                                         vmsg_free_vtree_memory(vMsg);
1096                                                                         return MSG_ERR_STORAGE_ERROR;
1097                                                                 }
1098                                                                 MSG_DEBUG("fileName: %s, retrievedFilePath: %s (%d)", fileName, retrievedFilePath, strlen(retrievedFilePath));
1099
1100                                                                 if (MsgWriteIpcFile(fileName, retrievedFilePath, strlen(retrievedFilePath)+ 1) == false) {
1101                                                                         vmsg_free_vtree_memory(vMsg);
1102                                                                         return MSG_ERR_STORAGE_ERROR;
1103                                                                 }
1104                                                                 strncpy(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN);
1105                                                                 msgInfo.dataSize = strlen(retrievedFilePath) + 1;
1106
1107                                                                 if (err != MSG_SUCCESS)
1108                                                                         return vmsg_free_vtree_memory(vMsg);
1109 ///////////////////////////
1110                                                         }
1111                                                 }
1112                                         }
1113                                         break;
1114
1115                                         case VCARD_TYPE_TEL :
1116                                         {
1117                                                 msgInfo.nAddressCnt++;
1118
1119                                                 msgInfo.addressList[msgInfo.nAddressCnt-1].addressType = MSG_ADDRESS_TYPE_PLMN;
1120                                                 msgInfo.addressList[msgInfo.nAddressCnt-1].recipientType = MSG_RECIPIENTS_TYPE_TO;
1121
1122                                                 strncpy(msgInfo.addressList[msgInfo.nAddressCnt-1].addressVal, pObject->pszValue[0], MAX_ADDRESS_VAL_LEN);
1123                                         }
1124                                         break;
1125                                 }
1126
1127                                 if (pObject->pSibling != NULL)
1128                                         pObject = pObject->pSibling;
1129                                 else
1130                                         break;
1131                         }
1132
1133                         if (vMsg->pNext != NULL) {
1134                                 vMsg = vMsg->pNext;
1135                                 pObject = vMsg->pTop;
1136                         } else {
1137                                 break;
1138                         }
1139                 }
1140
1141                 msgInfo.bBackup = true; // Set Backup Flag
1142                 msgInfo.storageId = MSG_STORAGE_PHONE; // Set Storage Id
1143                 msgInfo.priority = MSG_MESSAGE_PRIORITY_NORMAL; // Set Priority
1144
1145                 err = MsgStoAddMessage(&msgInfo, NULL);
1146                 if (err != MSG_SUCCESS) {
1147                         MSG_DEBUG("MsgStoAddMessage() error : %d", err);
1148                 }
1149
1150                 if (msgInfo.msgType.mainType == MSG_MMS_TYPE)
1151                         MsgStoUpdateMms(&msgInfo);
1152
1153                 vmsg_free_vtree_memory(vMsg);
1154
1155                 vMsg = NULL;
1156                 pCurrent = pTemp + strlen("END:VMSG");
1157         }
1158
1159         return MSG_SUCCESS;
1160 }
1161
1162 msg_error_t MsgStoAddPushEvent(MSG_PUSH_EVENT_INFO_S* pPushEvent)
1163 {
1164         msg_error_t err = MSG_SUCCESS;
1165         char sqlQuery[MAX_QUERY_LEN+1];
1166         unsigned int rowId = 0;
1167
1168         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1169
1170         // Check whether Same record exists or not.
1171         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);",
1172                         MSGFW_PUSH_CONFIG_TABLE_NAME, pPushEvent->contentType, pPushEvent->appId, pPushEvent->pkgName);
1173
1174         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
1175                 MSG_DEBUG("Query Failed [%s]", sqlQuery);
1176                 return MSG_ERR_DB_PREPARE;
1177         }
1178
1179         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
1180                 dbHandle.finalizeQuery();
1181                 return MSG_ERR_DB_ROW;
1182         }
1183         dbHandle.finalizeQuery();
1184
1185         dbHandle.beginTrans();
1186         err = dbHandle.getRowId(MSGFW_PUSH_CONFIG_TABLE_NAME, &rowId);
1187         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1188
1189         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, ?, ?, ?, %d, 0, 0);",
1190                         MSGFW_PUSH_CONFIG_TABLE_NAME, rowId, pPushEvent->bLaunch);
1191
1192
1193         MSG_DEBUG("QUERY : %s", sqlQuery);
1194
1195         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
1196                 dbHandle.endTrans(false);
1197                 return MSG_ERR_DB_EXEC;
1198         }
1199
1200         dbHandle.bindText(pPushEvent->contentType, 1);
1201         dbHandle.bindText(pPushEvent->appId, 2);
1202         dbHandle.bindText(pPushEvent->pkgName, 3);
1203
1204         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
1205                 dbHandle.finalizeQuery();
1206                 dbHandle.endTrans(false);
1207                 return MSG_ERR_DB_EXEC;
1208         }
1209
1210         dbHandle.finalizeQuery();
1211         dbHandle.endTrans(true);
1212
1213         return MSG_SUCCESS;
1214 }
1215
1216
1217 msg_error_t MsgStoDeletePushEvent(MSG_PUSH_EVENT_INFO_S* pPushEvent)
1218 {
1219         char sqlQuery[MAX_QUERY_LEN+1];
1220         dbHandle.beginTrans();
1221         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1222         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE CONTENT_TYPE LIKE '%s' AND APP_ID LIKE '%s' AND PKG_NAME LIKE '%s';",
1223                         MSGFW_PUSH_CONFIG_TABLE_NAME, pPushEvent->contentType, pPushEvent->appId, pPushEvent->pkgName);
1224
1225         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
1226                 dbHandle.endTrans(false);
1227                 return MSG_ERR_DB_EXEC;
1228         }
1229         dbHandle.endTrans(true);
1230         return MSG_SUCCESS;
1231 }
1232
1233 msg_error_t MsgStoUpdatePushEvent(MSG_PUSH_EVENT_INFO_S* pSrc, MSG_PUSH_EVENT_INFO_S* pDst)
1234 {
1235         char sqlQuery[MAX_QUERY_LEN+1];
1236         dbHandle.beginTrans();
1237         memset(sqlQuery, 0x00, sizeof(sqlQuery));
1238         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';",
1239                         MSGFW_PUSH_CONFIG_TABLE_NAME, pDst->bLaunch, pSrc->contentType, pSrc->appId, pSrc->pkgName);
1240
1241         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
1242                 dbHandle.endTrans(false);
1243                 return MSG_ERR_DB_EXEC;
1244         }
1245
1246         dbHandle.bindText(pDst->contentType, 1);
1247         dbHandle.bindText(pDst->appId, 2);
1248         dbHandle.bindText(pDst->pkgName, 3);
1249
1250         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
1251                 dbHandle.finalizeQuery();
1252                 dbHandle.endTrans(false);
1253                 return MSG_ERR_DB_EXEC;
1254         }
1255
1256         dbHandle.finalizeQuery();
1257         dbHandle.endTrans(true);
1258
1259         return MSG_SUCCESS;
1260 }