Sync with tizen 2.4
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / SmsPluginStorage.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 <errno.h>
18
19 #include "MsgDebug.h"
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22 #include "MsgContact.h"
23 #include "MsgUtilFile.h"
24 #include "MsgUtilStorage.h"
25 #include "MsgUtilFunction.h"
26 #include "MsgGconfWrapper.h"
27 #include "MsgNotificationWrapper.h"
28 #include "SmsPluginMain.h"
29 #include "SmsPluginSimMsg.h"
30 #include "SmsPluginStorage.h"
31
32
33 /*==================================================================================================
34                                      IMPLEMENTATION OF SmsPluginStorage - Member Functions
35 ==================================================================================================*/
36 SmsPluginStorage* SmsPluginStorage::pInstance = NULL;
37
38
39 SmsPluginStorage::SmsPluginStorage()
40 {
41 /*** No need to connect DB anymore.
42         if (dbHandle->connect() != MSG_SUCCESS) {
43                 MSG_DEBUG("DB Connect Fail");
44         }
45 ***/
46 }
47
48
49 SmsPluginStorage::~SmsPluginStorage()
50 {
51
52 }
53
54
55 SmsPluginStorage* SmsPluginStorage::instance()
56 {
57         if (!pInstance) {
58                 MSG_DEBUG("pInstance is NULL. Now creating instance.");
59                 pInstance = new SmsPluginStorage();
60         }
61
62         return pInstance;
63 }
64
65
66 msg_error_t SmsPluginStorage::insertMsgRef(MSG_MESSAGE_INFO_S *pMsg, unsigned char msgRef, int index)
67 {
68         MSG_BEGIN();
69
70         time_t curTime = time(NULL);
71
72         MsgDbHandler *dbHandle = getDbHandle();
73
74         char sqlQuery[MAX_QUERY_LEN+1];
75         char *normalNum = NULL;
76
77         memset(sqlQuery, 0x00, sizeof(sqlQuery));
78
79         normalNum = msg_normalize_number(pMsg->addressList[index].addressVal);
80
81         MSG_SEC_DEBUG("Insert MsgID=[%d], Address=[%s], MsgRef=[%d], Time=[%d]", \
82                         pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
83
84         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %s, %d, 0, -1, %d);",
85                         MSGFW_SMS_REPORT_TABLE_NAME, pMsg->msgId, normalNum, (int)msgRef, (int)curTime);
86
87         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
88                 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
89                 return MSG_ERR_DB_EXEC;
90         }
91
92         MSG_END();
93
94         return MSG_SUCCESS;
95
96 }
97
98
99 msg_error_t SmsPluginStorage::updateMsgDeliverStatus(MSG_MESSAGE_INFO_S *pMsgInfo, unsigned char msgRef)
100 {
101         MSG_BEGIN();
102
103         MsgDbHandler *dbHandle = getDbHandle();
104
105         char sqlQuery[MAX_QUERY_LEN+1];
106
107         msg_message_id_t msgId = 0;
108         int rowCnt = 0;
109         char *normalNum = NULL;
110
111         normalNum = msg_normalize_number(pMsgInfo->addressList[0].addressVal);
112
113         memset(sqlQuery, 0x00, sizeof(sqlQuery));
114         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE ADDRESS_VAL = %s AND MSG_REF > 0 ORDER BY TIME ASC;",
115                         MSGFW_SMS_REPORT_TABLE_NAME, normalNum);
116         MSG_DEBUG("[SQL Query] %s", sqlQuery);
117
118         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
119                 return MSG_ERR_DB_PREPARE;
120
121         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
122                 msgId = dbHandle->columnInt(0);
123
124         dbHandle->finalizeQuery();
125
126         pMsgInfo->msgId = msgId;
127
128         /** Update Status - MSG_MESSAGE_TABLE */
129         memset(sqlQuery, 0x00, sizeof(sqlQuery));
130         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE MSG_ID = %d AND MSG_REF > 0;",
131                         MSGFW_SMS_REPORT_TABLE_NAME, msgId);
132
133         if (dbHandle->getTable(sqlQuery, &rowCnt) != MSG_SUCCESS) {
134                 dbHandle->freeTable();
135                 return MSG_ERR_DB_GETTABLE;
136         }
137
138         dbHandle->freeTable();
139
140         MSG_DEBUG("Selected row count = [%d]", rowCnt);
141
142         if (rowCnt == 1 && pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
143                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
144                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d WHERE MSG_ID = %d;",
145                                 MSGFW_MESSAGE_TABLE_NAME, (int)pMsgInfo->networkStatus, msgId);
146                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
147
148                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
149                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
150                         return MSG_ERR_DB_EXEC;
151                 }
152         }
153
154         /** Update Status - MSG_REPORT_TABLE */
155         if (pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_SUCCESS) {
156                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
157                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
158                                 MSGFW_SMS_REPORT_TABLE_NAME, 1, (int)pMsgInfo->displayTime, msgId, normalNum);
159                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
160
161                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
162                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
163                         return MSG_ERR_DB_EXEC;
164                 }
165         } else if(pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_EXPIRED) {
166                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
167                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
168                                 MSGFW_SMS_REPORT_TABLE_NAME, 0, (int)pMsgInfo->displayTime, msgId, normalNum);
169                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
170
171                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
172                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
173                         return MSG_ERR_DB_EXEC;
174                 }
175         } else if(pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_PENDING) {
176                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
177                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
178                                 MSGFW_SMS_REPORT_TABLE_NAME, 3, (int)pMsgInfo->displayTime, msgId, normalNum);
179                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
180
181                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
182                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
183                         return MSG_ERR_DB_EXEC;
184                 }
185         } else if(pMsgInfo->networkStatus == MSG_NETWORK_DELIVER_FAIL) {
186                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
187                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, STATUS = %d, TIME = %d WHERE MSG_ID = %d and ADDRESS_VAL = '%s';",
188                                 MSGFW_SMS_REPORT_TABLE_NAME, 8, (int)pMsgInfo->displayTime, msgId, normalNum);
189                 MSG_DEBUG("[SQL Query] %s", sqlQuery);
190
191                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
192                         MSG_DEBUG("Query Failed : [%s]", sqlQuery);
193                         return MSG_ERR_DB_EXEC;
194                 }
195         }
196
197         MSG_END();
198
199         return MSG_SUCCESS;
200 }
201
202
203 msg_error_t SmsPluginStorage::updateSentMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_network_status_t status)
204 {
205         MSG_BEGIN();
206
207         if (!pMsgInfo || (pMsgInfo && pMsgInfo->msgId <= 0)) {
208                 MSG_DEBUG("Invalid message id");
209                 return MSG_ERR_INVALID_MESSAGE_ID;
210         }
211
212 /***  Comment below line to not save the time value after sent status (it could be used later.)
213         time_t curTime = time(NULL);
214 ***/
215         MsgDbHandler *dbHandle = getDbHandle();
216
217         char sqlQuery[MAX_QUERY_LEN+1];
218
219         memset(sqlQuery, 0x00, sizeof(sqlQuery));
220
221         MSG_DEBUG("Update Msg ID : [%d], Network Status : [%d] ", pMsgInfo->msgId, status);
222
223         /** Move Msg to SENTBOX */
224         if (status == MSG_NETWORK_SEND_SUCCESS) {
225                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, FOLDER_ID = %d WHERE MSG_ID = %d;",
226                                         MSGFW_MESSAGE_TABLE_NAME, status, MSG_SENTBOX_ID, pMsgInfo->msgId);
227         } else {
228                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET NETWORK_STATUS = %d, READ_STATUS = 0 WHERE MSG_ID = %d;",
229                                         MSGFW_MESSAGE_TABLE_NAME, status, pMsgInfo->msgId);
230         }
231
232         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
233                 MSG_DEBUG("MsgExecQuery() : [%s]", sqlQuery);
234                 return MSG_ERR_DB_EXEC;
235         }
236         //contacts-service is not used for gear
237 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
238         if (status == MSG_NETWORK_SEND_SUCCESS) {
239                 MSG_DEBUG("MsgAddPhoneLog() : folderId [%d]", pMsgInfo->folderId);
240                 MsgAddPhoneLog(pMsgInfo);
241         }
242 #endif //MSG_CONTACTS_SERVICE_NOT_SUPPORTED
243         MSG_END();
244
245         return MSG_SUCCESS;
246 }
247
248 #ifdef SMS_REPORT_OPERATION
249 msg_error_t SmsPluginStorage::updateMsgRef(msg_message_id_t MsgId, unsigned char MsgRef)
250 {
251         MSG_BEGIN();
252
253         MsgDbHandler *dbHandle = getDbHandle();
254
255         char sqlQuery[MAX_QUERY_LEN+1];
256
257         memset(sqlQuery, 0x00, sizeof(sqlQuery));
258
259         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = %d WHERE MSG_ID = %d;",
260                                 MSGFW_REPORT_TABLE_NAME, (int)MsgRef, MsgId);
261
262         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
263                 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
264                 return MSG_ERR_DB_EXEC;
265         }
266
267         /** Set Message Reference for updating report table */
268         tmpMsgRef = MsgRef;
269
270         MSG_DEBUG("MsgRef : %d", MsgRef);
271
272         MSG_END();
273
274         return MSG_SUCCESS;
275 }
276
277
278 msg_error_t SmsPluginStorage::updateStatusReport(unsigned char MsgRef, msg_delivery_report_status_t Status, time_t DeliveryTime)
279 {
280         MSG_BEGIN();
281
282         MSG_DEBUG("tmpMsgRef : %d", tmpMsgRef);
283
284         MsgDbHandler *dbHandle = getDbHandle();
285
286         char sqlQuery[MAX_QUERY_LEN+1];
287
288         /** Get Msg Id for Quickpanel Noti */
289         msg_message_id_t msgId = 0;
290
291         memset(sqlQuery, 0x00, sizeof(sqlQuery));
292         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE MSG_REF = %d;",
293                                         MSGFW_REPORT_TABLE_NAME, (int)tmpMsgRef);
294
295         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
296                 return MSG_ERR_DB_PREPARE;
297
298         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW)
299                 msgId = dbHandle->columnInt(0);
300
301         dbHandle->finalizeQuery();
302
303         /** Update Status */
304         memset(sqlQuery, 0x00, sizeof(sqlQuery));
305         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET MSG_REF = -1, DELIVERY_REPORT_STATUS = %d, DELIVERY_REPORT_TIME = %lu WHERE MSG_REF = %d;",
306                                         MSGFW_REPORT_TABLE_NAME, Status, DeliveryTime, (int)tmpMsgRef);
307
308         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
309                 MSG_DEBUG("Query Failed : [%s]", sqlQuery);
310                 return MSG_ERR_DB_EXEC;
311         }
312
313         /** Insert Quickpanel Noti */
314         msg_error_t ret = MSG_SUCCESS;
315
316         ret = MsgInsertSmsNotiToQuickpanel(dbHandle, msgId, Status);
317
318         if (ret != MSG_SUCCESS) {
319                 MSG_DEBUG("MsgInsertSmsNotiToQuickpanel() Failed : [%d]", ret);
320                 return ret;
321         }
322
323         MSG_END();
324
325         return MSG_SUCCESS;
326 }
327 #endif
328
329 msg_error_t SmsPluginStorage::addSimMessage(MSG_MESSAGE_INFO_S *pSimMsgInfo, int *simIdList)
330 {
331         msg_error_t err = MSG_SUCCESS;
332
333         unsigned int simId = 0;
334         MsgDbHandler *dbHandle = getDbHandle();
335
336         char sqlQuery[MAX_QUERY_LEN+1];
337
338 //      dbHandle->beginTrans();
339
340 # if 0
341         msg_message_id_t msgId = 0;
342         msg_thread_id_t convId = 0;
343
344         err = MsgStoAddAddress(dbHandle, pSimMsgInfo, &convId);
345
346         if (err != MSG_SUCCESS) {
347                 dbHandle->endTrans(false);
348                 return err;
349         }
350
351         pSimMsgInfo->threadId = convId;
352
353         err = dbHandle->getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
354
355         if (err != MSG_SUCCESS) {
356                 dbHandle->endTrans(false);
357                 return err;
358         }
359
360         //simId = pSimMsgInfo->msgId;
361         pSimMsgInfo->msgId = msgId;
362
363         /**  Add Message */
364         memset(sqlQuery, 0x00, sizeof(sqlQuery));
365
366         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, '', '', ?, 0, 0, 0);",
367                         MSGFW_MESSAGE_TABLE_NAME, msgId, convId, pSimMsgInfo->folderId, pSimMsgInfo->storageId,
368                         pSimMsgInfo->msgType.mainType, pSimMsgInfo->msgType.subType, pSimMsgInfo->displayTime, pSimMsgInfo->dataSize,
369                         pSimMsgInfo->networkStatus, pSimMsgInfo->bRead, pSimMsgInfo->bProtected, pSimMsgInfo->priority,
370                         pSimMsgInfo->direction, 0, pSimMsgInfo->bBackup);
371
372         MSG_DEBUG("QUERY : %s", sqlQuery);
373
374         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
375                 dbHandle->endTrans(false);
376                 return MSG_ERR_DB_PREPARE;
377         }
378
379         dbHandle->bindText(pSimMsgInfo->subject, 1);
380
381         dbHandle->bindText(pSimMsgInfo->msgText, 2);
382
383         if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
384                 dbHandle->endTrans(false);
385                 return MSG_ERR_DB_STEP;
386         }
387
388         dbHandle->finalizeQuery();
389 #endif
390         if(simIdList) {
391                 MSG_DEBUG("simIdList exist.");
392                 for(int i=0; i < MAX_SIM_SMS_NUM; ++i)
393                 {
394                         if(simIdList[i]) {
395                                 simId = simIdList[i] - 1;
396                                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
397                                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);",
398                                                 MSGFW_SIM_MSG_TABLE_NAME, simId, pSimMsgInfo->msgId);
399
400                                 MSG_DEBUG("QUERY : %s", sqlQuery);
401
402                                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
403 //                                      dbHandle->endTrans(false);
404                                         return MSG_ERR_DB_EXEC;
405                                 }
406                         } else
407                                 break;
408                 }
409         }
410
411 //      dbHandle->endTrans(true);
412
413         return err;
414
415 }
416
417
418 msg_error_t SmsPluginStorage::insertSimMessage(int simId, int msgId)
419 {
420         MSG_BEGIN();
421
422         MsgDbHandler *dbHandle = getDbHandle();
423
424         char sqlQuery[MAX_QUERY_LEN+1];
425
426         dbHandle->beginTrans();
427
428         /** Insert Message into msg_sim table */
429         memset(sqlQuery, 0x00, sizeof(sqlQuery));
430         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d);", MSGFW_SIM_MSG_TABLE_NAME, simId, msgId);
431         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
432                 dbHandle->endTrans(false);
433                 return MSG_ERR_DB_EXEC;
434         }
435
436         dbHandle->endTrans(true);
437
438         return MSG_SUCCESS;
439 }
440
441
442 msg_error_t SmsPluginStorage::deleteSimMessage(int sim_idx, int simId)
443 {
444         MSG_BEGIN();
445
446         MsgDbHandler *dbHandle = getDbHandle();
447
448         char sqlQuery[MAX_QUERY_LEN+1];
449
450         dbHandle->beginTrans();
451
452         /** Delete Message from msg_sim table */
453         memset(sqlQuery, 0x00, sizeof(sqlQuery));
454         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE SIM_ID = %d AND SIM_SLOT_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, simId, sim_idx);
455         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
456                 dbHandle->endTrans(false);
457                 return MSG_ERR_DB_EXEC;
458         }
459
460         dbHandle->endTrans(true);
461
462         return MSG_SUCCESS;
463 }
464
465
466 msg_error_t SmsPluginStorage::checkMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
467 {
468         MSG_BEGIN();
469
470         msg_error_t err = MSG_SUCCESS;
471
472         /**  Check whether storage is full or not */
473         err = checkStorageStatus(pMsgInfo);
474
475         if (err != MSG_SUCCESS) {
476                 if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
477                         pMsgInfo->folderId = 0;
478                         err = MSG_SUCCESS;
479                 }
480                 else if(pMsgInfo->msgType.classType == MSG_CLASS_2 &&
481                                 (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS)) {
482                         err = addClass2Message(pMsgInfo);
483                 }
484                 else if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS){
485                         err = MSG_SUCCESS;
486                 }
487                 return err;
488         }
489
490         /**  Amend message information for type **/
491         if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS || pMsgInfo->msgType.subType == MSG_REJECT_SMS) {
492
493                 MSG_DEBUG("Normal SMS");
494
495                 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
496                         err = addClass2Message(pMsgInfo);
497                 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
498                         /** Class 0 Msg should be saved in hidden folder */
499                         pMsgInfo->folderId = 0;
500                 }
501
502         } else if ((pMsgInfo->msgType.subType >= MSG_REPLACE_TYPE1_SMS) && (pMsgInfo->msgType.subType <= MSG_REPLACE_TYPE7_SMS)) {
503                 MSG_DEBUG("Replace SM Type [%d]", pMsgInfo->msgType.subType-3);
504
505                 if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
506                         err = addClass2Message(pMsgInfo);
507                 } else if (pMsgInfo->msgType.classType == MSG_CLASS_0) {
508                         /** Class 0 Msg should be saved in hidden folder */
509                         pMsgInfo->folderId = 0;
510                         pMsgInfo->msgType.subType = MSG_NORMAL_SMS;
511                 }
512
513         } else if ((pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS) && (pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS)) {
514                 if (pMsgInfo->bStore == true) {
515                         MSG_DEBUG("MWI Message");
516
517                         if (pMsgInfo->msgType.classType == MSG_CLASS_2) {
518                                 err = addClass2Message(pMsgInfo);
519                         }
520                 }
521         } else {
522                 MSG_DEBUG("No matching type [%d]", pMsgInfo->msgType.subType);
523         }
524
525         if (err == MSG_SUCCESS) {
526                 MSG_DEBUG("Success to check message !!");
527         } else {
528                 MSG_DEBUG("fail to check message !! : [%d]", err);
529         }
530
531         return err;
532 }
533
534
535 msg_error_t SmsPluginStorage::addSmsMessage(MSG_MESSAGE_INFO_S *pMsgInfo)
536 {
537         msg_error_t err = MSG_SUCCESS;
538
539         unsigned int rowId = 0;
540         msg_thread_id_t convId = 0;
541
542         MsgDbHandler *dbHandle = getDbHandle();
543
544         dbHandle->beginTrans();
545
546         if (pMsgInfo->nAddressCnt > 0) {
547
548                 err = MsgStoAddAddress(dbHandle, pMsgInfo, &convId);
549
550                 if (err != MSG_SUCCESS) {
551                         dbHandle->endTrans(false);
552                         return err;
553                 }
554
555                 pMsgInfo->threadId = convId;
556         }
557
558         /**  Add Message Table */
559         rowId = MsgStoAddMessageTable(dbHandle, pMsgInfo);
560
561         if (rowId <= 0) {
562                 dbHandle->endTrans(false);
563                 return MSG_ERR_DB_ROW;
564         }
565
566         /** Update conversation table */
567         err = MsgStoUpdateConversation(dbHandle, convId);
568
569         if (err != MSG_SUCCESS) {
570                 dbHandle->endTrans(false);
571                 return err;
572         }
573
574         err = dbHandle->endTrans(true);
575         if (err != MSG_SUCCESS) {
576                 return err;
577         }
578
579         pMsgInfo->msgId = (msg_message_id_t)rowId;
580
581         MSG_END();
582
583         return MSG_SUCCESS;
584 }
585
586
587 msg_error_t SmsPluginStorage::deleteSmsMessage(msg_message_id_t msgId)
588 {
589         MSG_BEGIN();
590
591         MsgDbHandler *dbHandle = getDbHandle();
592
593         char sqlQuery[MAX_QUERY_LEN+1];
594
595          /**  Get SUB_TYPE, STORAGE_ID */
596         memset(sqlQuery, 0x00, sizeof(sqlQuery));
597         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, FOLDER_ID, CONV_ID, SIM_INDEX \
598                         FROM %s WHERE MSG_ID = %d;",
599                         MSGFW_MESSAGE_TABLE_NAME, msgId);
600
601         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
602                 return MSG_ERR_DB_PREPARE;
603
604         MSG_MESSAGE_TYPE_S msgType;
605         msg_folder_id_t folderId;
606
607         msg_thread_id_t convId;
608         msg_sim_slot_id_t simIndex;
609
610         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
611                 msgType.mainType = dbHandle->columnInt(0);
612                 msgType.subType = dbHandle->columnInt(1);
613                 folderId = dbHandle->columnInt(2);
614                 convId = dbHandle->columnInt(3);
615                 simIndex = dbHandle->columnInt(4);
616
617                 MSG_DEBUG("Main Type:[%d] SubType:[%d] FolderId:[%d] ConversationId:[%d]", msgType.mainType, msgType.subType, folderId, convId);
618         } else {
619                 MSG_DEBUG("MsgStepQuery() Error [%s]", sqlQuery);
620                 dbHandle->finalizeQuery();
621                 return MSG_ERR_DB_STEP;
622         }
623
624         dbHandle->finalizeQuery();
625
626         dbHandle->beginTrans();
627
628         memset(sqlQuery, 0x00, sizeof(sqlQuery));
629         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_SENDOPT_TABLE_NAME, msgId);
630
631         /**  Delete SMS Send Option */
632         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
633                 dbHandle->endTrans(false);
634                 return MSG_ERR_DB_EXEC;
635         }
636
637         if (msgType.subType == MSG_CB_SMS || msgType.subType == MSG_JAVACB_SMS) {
638                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
639                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_CB_MSG_TABLE_NAME, msgId);
640
641                 /** Delete Push Message from push table */
642                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
643                         dbHandle->endTrans(false);
644                         return MSG_ERR_DB_EXEC;
645                 }
646         } else if (msgType.subType >= MSG_WAP_SI_SMS && msgType.subType <= MSG_WAP_CO_SMS) {
647                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
648                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_PUSH_MSG_TABLE_NAME, msgId);
649
650                 /**  Delete Push Message from push table */
651                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
652                         dbHandle->endTrans(false);
653                         return MSG_ERR_DB_EXEC;
654                 }
655         } else if (msgType.subType == MSG_SYNCML_CP) {
656                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
657                 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SYNCML_MSG_TABLE_NAME, msgId);
658
659                 /**  Delete SyncML Message from syncML table */
660                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
661                         dbHandle->endTrans(false);
662                         return MSG_ERR_DB_EXEC;
663                 }
664         }
665
666         /** Delete Message from msg table */
667         memset(sqlQuery, 0x00, sizeof(sqlQuery));
668         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_MESSAGE_TABLE_NAME, msgId);
669         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
670                 dbHandle->endTrans(false);
671                 return MSG_ERR_DB_EXEC;
672         }
673
674         /** Delete Message from msg_report table */
675         memset(sqlQuery, 0x00, sizeof(sqlQuery));
676         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SMS_REPORT_TABLE_NAME, msgId);
677         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
678                 dbHandle->endTrans(false);
679                 return MSG_ERR_DB_EXEC;
680         }
681
682         /** Delete Message from msg_sim table */
683         memset(sqlQuery, 0x00, sizeof(sqlQuery));
684         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;", MSGFW_SIM_MSG_TABLE_NAME, msgId);
685         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
686                 dbHandle->endTrans(false);
687                 return MSG_ERR_DB_EXEC;
688         }
689
690         /**  Clear Conversation table */
691         if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
692                 dbHandle->endTrans(false);
693                 return MSG_ERR_DB_EXEC;
694         }
695
696         /**  Update conversation table.*/
697         if (MsgStoUpdateConversation(dbHandle, convId) != MSG_SUCCESS) {
698                 dbHandle->endTrans(false);
699                 return MSG_ERR_STORAGE_ERROR;
700         }
701
702         dbHandle->endTrans(true);
703
704         if (folderId == MSG_INBOX_ID) {
705                 msgType.classType = MSG_CLASS_NONE;
706
707                 /**  Set memory status in SIM */
708                 if (MsgStoCheckMsgCntFull(dbHandle, &msgType, folderId) == MSG_SUCCESS) {
709                         MSG_DEBUG("Set Memory Status");
710                         SmsPlgSetMemoryStatus(simIndex, MSG_SUCCESS);
711                 }
712         }
713
714         MsgRefreshAllNotification(true, false, false);
715
716         return MSG_SUCCESS;
717 }
718
719
720 msg_error_t SmsPluginStorage::addClass2Message(MSG_MESSAGE_INFO_S *pMsgInfo)
721 {
722         MSG_BEGIN();
723
724         msg_error_t err = MSG_SUCCESS;
725         pthread_t thd;
726         memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
727         memset(&addrInfo, 0, sizeof(MSG_ADDRESS_INFO_S));
728         memcpy(&msgInfo, pMsgInfo, sizeof(MSG_MESSAGE_INFO_S));
729         memcpy(&addrInfo, pMsgInfo->addressList, sizeof(MSG_ADDRESS_INFO_S));
730         msgInfo.addressList = &addrInfo;
731
732         if(pthread_create(&thd, NULL, &class2_thread, (void *)&msgInfo) < 0)
733         {
734                 MSG_DEBUG("pthread_create() error");
735
736         }
737         //pthread_join(thd, (void **)&err);
738         pthread_detach(thd);
739
740 #if 0
741         err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
742
743
744         if (err == MSG_SUCCESS) {
745                 MSG_DEBUG("Success to saveClass2Message.");
746         } else {
747                 MSG_DEBUG("Fail to saveClass2Message : [%d]", err);
748         }
749 #endif
750
751         MSG_END();
752
753         return err;
754 }
755
756 void* SmsPluginStorage::class2_thread(void *data)
757 {
758         MSG_BEGIN();
759
760         msg_error_t err = MSG_SUCCESS;
761         MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)data;
762
763         err = SmsPluginSimMsg::instance()->saveClass2Message(pMsgInfo);
764         if (err == MSG_SUCCESS) {
765                 MSG_DEBUG("Success to saveClass2Message.");
766         } else {
767                 MSG_DEBUG("Fail to saveClass2Message : [%d]", err);
768         }
769
770         MSG_END();
771         return (void *)err;
772 }
773
774
775 msg_error_t SmsPluginStorage::getReplaceSimMsg(const MSG_MESSAGE_INFO_S *pMsg, int *pMsgId, int *pSimId)
776 {
777         MSG_BEGIN();
778
779         MsgDbHandler *dbHandle = getDbHandle();
780
781         char sqlQuery[MAX_QUERY_LEN+1];
782         msg_thread_id_t convId = 0;
783         msg_message_id_t msgId = 0;
784
785         dbHandle->beginTrans();
786
787         if (MsgExistAddress(dbHandle, pMsg, &convId) == true) {
788                 MSG_DEBUG("Address Info. exists [%d]", convId);
789
790                 /**  Find Replace Type Msg : Same Replace Type, Same Origin Address, Same Storage ID */
791                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
792                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s \
793                                                         WHERE CONV_ID = %d AND SUB_TYPE = %d AND STORAGE_ID = %d \
794                                                         ORDER BY DISPLAY_TIME ASC;",
795                                 MSGFW_MESSAGE_TABLE_NAME, (int)convId, pMsg->msgType.subType, MSG_STORAGE_SIM);
796
797                 MSG_DEBUG("Query=[%s]", sqlQuery);
798                 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
799                         dbHandle->endTrans(false);
800                         return MSG_ERR_DB_PREPARE;
801                 }
802
803                 if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
804                         *pMsgId = dbHandle->columnInt(0);
805                 } else {
806                         dbHandle->finalizeQuery();
807                         dbHandle->endTrans(false);
808                         return MSG_ERR_DB_STEP;
809                 }
810
811                 dbHandle->finalizeQuery();
812
813         } else {
814                 dbHandle->endTrans(false);
815                 return MSG_ERR_DB_NORECORD;
816         }
817
818         memset(sqlQuery, 0x00, sizeof(sqlQuery));
819
820         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT SIM_ID FROM %s \
821                         WHERE MSG_ID = %d;",
822                         MSGFW_SIM_MSG_TABLE_NAME, *pMsgId);
823
824         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS) {
825                 dbHandle->endTrans(false);
826                 return MSG_ERR_DB_PREPARE;
827         }
828
829         if (dbHandle->stepQuery() == MSG_ERR_DB_ROW) {
830                 *pSimId = dbHandle->columnInt(0);
831         } else {
832                 dbHandle->finalizeQuery();
833                 dbHandle->endTrans(false);
834                 return MSG_ERR_DB_STEP;
835         }
836
837         MSG_DEBUG("Replace Msg Id=[%d], Sim Id=[%d]", *pMsgId, *pSimId);
838
839         dbHandle->finalizeQuery();
840
841         dbHandle->endTrans(true);
842
843         MSG_END();
844
845         return msgId;
846 }
847
848 msg_error_t SmsPluginStorage::addSmsSendOption(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo)
849 {
850         MSG_BEGIN();
851
852         msg_error_t err = MSG_SUCCESS;
853
854         if (pSendOptInfo->bSetting == false) {
855                 MsgSettingGetBool(SMS_SEND_DELIVERY_REPORT, &pSendOptInfo->bDeliverReq);
856                 MsgSettingGetBool(SMS_SEND_REPLY_PATH, &pSendOptInfo->option.smsSendOptInfo.bReplyPath);
857
858 //              if (pSendOptInfo->bDeliverReq || pSendOptInfo->option.smsSendOptInfo.bReplyPath) {
859 //                      pSendOptInfo->bSetting = true;
860                         MsgSettingGetBool(MSG_KEEP_COPY, &pSendOptInfo->bKeepCopy);
861 //              }
862         }
863
864 //      if (pSendOptInfo->bSetting == true) {
865                 MsgDbHandler *dbHandle = getDbHandle();
866
867                 char sqlQuery[MAX_QUERY_LEN+1];
868
869                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
870                 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
871                                 MSGFW_SMS_SENDOPT_TABLE_NAME, pMsg->msgId, pSendOptInfo->bDeliverReq,
872                                 pSendOptInfo->bKeepCopy, pSendOptInfo->option.smsSendOptInfo.bReplyPath, pMsg->encodeType);
873
874                 MSG_DEBUG("Query = [%s]", sqlQuery);
875
876                 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
877                         err = MSG_ERR_DB_EXEC;
878                 }
879 //      }
880
881         MSG_END();
882
883         return err;
884 }
885
886
887 msg_error_t SmsPluginStorage::checkStorageStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
888 {
889         msg_error_t err = MSG_SUCCESS;
890
891         MsgDbHandler *dbHandle = getDbHandle();
892
893         err = MsgStoCheckMsgCntFull(dbHandle, &(pMsgInfo->msgType), pMsgInfo->folderId);
894
895         if (err != MSG_SUCCESS) {
896
897                 if (err == MSG_ERR_MESSAGE_COUNT_FULL) {
898                         bool bAutoErase = false;
899
900                         MsgSettingGetBool(MSG_AUTO_ERASE, &bAutoErase);
901
902                         MSG_DEBUG("bAutoErase: %d", bAutoErase);
903
904                         if (bAutoErase == true) {
905                                 msg_message_id_t msgId;
906
907                                 /** Find the oldest message's msgId */
908                                 err = MsgStoGetOldestMessage(dbHandle, pMsgInfo, &msgId);
909
910                                 if (err != MSG_SUCCESS)
911                                         return err;
912
913                                 /** Delete the corresponding message. */
914                                 err = deleteSmsMessage(msgId);
915                         }
916                 }
917
918                 return err;
919         }
920
921         return err;
922 }
923
924
925 #if 0
926
927 msg_error_t SmsPluginStorage::isReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
928 {
929         msg_error_t err = MSG_SUCCESS;
930
931         int rowCnt = 0;
932
933         MsgDbHandler *dbHandle = getDbHandle();
934
935         char sqlQuery[MAX_QUERY_LEN+1] = {0, };
936
937         memset(sqlQuery, 0x00, sizeof(sqlQuery));
938
939         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT * FROM %s WHERE GEO_SCOPE = %d AND MSG_CODE = %d AND MESSAGE_ID = %d AND UPDATE_NUM = %d",
940                         MSGFW_RECEIVED_CB_MSG_TABLE_NAME, CbPage.pageHeader.serialNum.geoScope,
941                         CbPage.pageHeader.serialNum.msgCode,CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
942
943         err = dbHandle->getTable(sqlQuery, &rowCnt);
944         MSG_DEBUG("rowCnt: %d", rowCnt);
945
946         dbHandle->freeTable();
947         return err;
948 }
949
950 msg_error_t SmsPluginStorage::insertReceivedCBMessage(SMS_CBMSG_PAGE_S CbPage)
951 {
952
953         msg_error_t err = MSG_SUCCESS;
954
955         unsigned int rowId = 0;
956
957         MsgDbHandler *dbHandle = getDbHandle();
958
959         char sqlQuery[MAX_QUERY_LEN+1];
960
961         err = dbHandle->getRowId(MSGFW_RECEIVED_CB_MSG_TABLE_NAME, &rowId);
962
963         if (err != MSG_SUCCESS)
964                 return err;
965
966         // Add Folder
967         memset(sqlQuery, 0x00, sizeof(sqlQuery));
968         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d);",
969                         MSGFW_RECEIVED_CB_MSG_TABLE_NAME, rowId, CbPage.pageHeader.serialNum.geoScope,
970                         CbPage.pageHeader.serialNum.msgCode,CbPage.pageHeader.msgId, CbPage.pageHeader.serialNum.updateNum);
971
972         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
973                 return MSG_ERR_DB_EXEC;
974
975         return MSG_SUCCESS;
976
977 }
978
979 #endif
980
981 msg_error_t SmsPluginStorage::getRegisteredPushEvent(char* pPushHeader, int *count, char *application_id, int app_id_len, char *content_type, int content_type_len)
982 {
983         msg_error_t err = MSG_SUCCESS;
984
985         int rowCnt = 0, index = 3;
986
987         MsgDbHandler *dbHandle = getDbHandle();
988
989         char sqlQuery[MAX_QUERY_LEN+1] = {0, };
990
991         memset(sqlQuery, 0x00, sizeof(sqlQuery));
992
993         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT CONTENT_TYPE, APP_ID, APPCODE FROM %s", MSGFW_PUSH_CONFIG_TABLE_NAME);
994
995         err = dbHandle->getTable(sqlQuery, &rowCnt);
996         MSG_DEBUG("rowCnt: %d", rowCnt);
997
998         if (err == MSG_ERR_DB_NORECORD) {
999                 dbHandle->freeTable();
1000                 return MSG_SUCCESS;
1001         }
1002         else if ( err != MSG_SUCCESS) {
1003                 dbHandle->freeTable();
1004                 return err;
1005         }
1006
1007         char contentType[MAX_WAPPUSH_CONTENT_TYPE_LEN + 1] = {0,};
1008         char appId[MAX_WAPPUSH_ID_LEN + 1] = {0,};
1009         int appcode = 0, default_appcode = 0;
1010         bool found = false;
1011         char *_content_type = NULL, *_app_id = NULL;
1012         *count = 0;
1013
1014
1015         for (int i = 0; i < rowCnt; i++) {
1016                 memset(contentType, 0, MAX_WAPPUSH_CONTENT_TYPE_LEN);
1017                 memset(appId, 0, MAX_WAPPUSH_ID_LEN);
1018
1019                 dbHandle->getColumnToString(index++, MAX_WAPPUSH_CONTENT_TYPE_LEN + 1, contentType);
1020                 dbHandle->getColumnToString(index++, MAX_WAPPUSH_ID_LEN + 1, appId);
1021                 appcode = dbHandle->getColumnToInt(index++);
1022
1023                 //MSG_DEBUG("content_type: %s, app_id: %s", content_type, app_id);
1024                 _content_type = strcasestr(pPushHeader, contentType);
1025                 if(_content_type) {
1026                         _app_id = strcasestr(pPushHeader, appId);
1027                         if(appcode)
1028                                 default_appcode = appcode;
1029
1030                         if(_app_id) {
1031                                 PUSH_APPLICATION_INFO_S pInfo = {0, };
1032                                 pInfo.appcode = appcode;
1033                                 MSG_SEC_DEBUG("appcode: %d, app_id: %s", pInfo.appcode, appId);
1034                                 snprintf(application_id, app_id_len, "%s", appId);
1035                                 snprintf(content_type, content_type_len, "%s", contentType);
1036                                 pushAppInfoList.push_back(pInfo);
1037                                 (*count)++;
1038                                 found = true;
1039                         }
1040                 }
1041         }
1042
1043         if(!found && default_appcode != SMS_WAP_APPLICATION_LBS)
1044         {
1045                 // perform default action.
1046                 PUSH_APPLICATION_INFO_S pInfo = {0, };
1047                 pInfo.appcode = default_appcode;
1048                 memset(appId, 0, MAX_WAPPUSH_ID_LEN + 1);
1049                 snprintf(application_id, app_id_len, "%s", appId);
1050                 snprintf(content_type, content_type_len, "%s", contentType);
1051                 pushAppInfoList.push_back(pInfo);
1052                 *count = 1;
1053         }
1054         dbHandle->freeTable();
1055
1056         return err;
1057 }
1058
1059
1060 msg_error_t SmsPluginStorage::getnthPushEvent(int index, int *appcode)
1061 {
1062         msg_error_t err = MSG_SUCCESS;
1063         if((unsigned int)index > pushAppInfoList.size() - 1)
1064                 return MSG_ERR_INVALID_PARAMETER;
1065
1066         std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1067         int count = 0;
1068         for (; it != pushAppInfoList.end(); it++)
1069         {
1070                 if(index == count){
1071                         *appcode = it->appcode;
1072                         break;
1073                 }
1074                 count++;
1075         }
1076
1077         return err;
1078 }
1079
1080
1081 msg_error_t SmsPluginStorage::releasePushEvent()
1082 {
1083         msg_error_t err = MSG_SUCCESS;
1084         std::list<PUSH_APPLICATION_INFO_S>::iterator it = pushAppInfoList.begin();
1085
1086         for (; it != pushAppInfoList.end(); it++)
1087                 it = pushAppInfoList.erase(it);
1088
1089         return err;
1090 }