update tizen source
[framework/messaging/msg-service.git] / utils / MsgUtilStorage.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #include "MsgDebug.h"
32 #include "MsgUtilFile.h"
33 #include "MsgContact.h"
34 #include "MsgCppTypes.h"
35 #include "MsgGconfWrapper.h"
36 #include "MsgNotificationWrapper.h"
37 #include "MsgUtilStorage.h"
38
39 #include <sys/stat.h>
40 #include <sys/vfs.h>
41
42
43 static int msgCntLimit[MSG_COUNT_LIMIT_MAILBOX_TYPE_MAX][MSG_COUNT_LIMIT_MSG_TYPE_MAX] = {{1500, 500, 0, 50, 50}, {50, 50, 0, 0, 0}, {1000, 250, 0, 0, 0}, {50, 50, 0, 0, 0}, {0, 0, 200, 0, 0}};
44
45
46 /*==================================================================================================
47                                      FUNCTION IMPLEMENTATION
48 ==================================================================================================*/
49 unsigned int MsgStoAddMessageTable(MsgDbHandler *pDbHandle, const MSG_MESSAGE_INFO_S *pMsgInfo, unsigned int AddrId)
50 {
51         MSG_BEGIN();
52
53         MSG_ERROR_T err = MSG_SUCCESS;
54
55         unsigned int msgId = 0;
56
57         err = pDbHandle->getRowId(MSGFW_MESSAGE_TABLE_NAME, &msgId);
58
59         if (err != MSG_SUCCESS)
60         {
61                 return 0;
62         }
63
64         int fileSize = 0;
65
66         char* pFileData = NULL;
67         AutoPtr<char> buf(&pFileData);
68
69         // Get File Data
70         if (pMsgInfo->bTextSms == false)
71         {
72                 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pFileData, &fileSize) == false)
73                         return 0;
74
75                 MSG_DEBUG("file size [%d]", fileSize);
76         }
77
78         // Add Message
79         char sqlQuery[MAX_QUERY_LEN+1];
80
81         memset(sqlQuery, 0x00, sizeof(sqlQuery));
82
83         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, %d, %d, %d, %d, %ld, %d, %d, %d, %d, %d, %d, %ld, %d, ?, ?, ?, ?, %d, 0, %d, 0, 0);",
84                                 MSGFW_MESSAGE_TABLE_NAME, msgId, AddrId, pMsgInfo->folderId, msgId, pMsgInfo->storageId,
85                                 pMsgInfo->msgType.mainType, pMsgInfo->msgType.subType, pMsgInfo->displayTime, pMsgInfo->dataSize,
86                                 pMsgInfo->networkStatus, pMsgInfo->bRead, pMsgInfo->bProtected, pMsgInfo->priority,
87                                 pMsgInfo->direction, pMsgInfo->scheduledTime, pMsgInfo->bBackup, MSG_DELIVERY_REPORT_NONE, MSG_READ_REPORT_NONE);
88
89         MSG_DEBUG("QUERY : %s", sqlQuery);
90
91         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
92         {
93                 return 0;
94         }
95
96         pDbHandle->bindText(pMsgInfo->subject, 1);
97
98         pDbHandle->bindText(pMsgInfo->msgData, 2);
99
100         pDbHandle->bindText(pMsgInfo->thumbPath, 3);
101
102         if (pMsgInfo->bTextSms == false)
103                 pDbHandle->bindText(pFileData, 4);
104         else
105                 pDbHandle->bindText(pMsgInfo->msgText, 4);
106
107         if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE)
108         {
109                 pDbHandle->finalizeQuery();
110                 return 0;
111         }
112
113         pDbHandle->finalizeQuery();
114
115         return msgId;
116 }
117
118
119 MSG_ERROR_T MsgStoSetReadStatus(MsgDbHandler *pDbHandle, MSG_MESSAGE_ID_T MsgId, bool bRead)
120 {
121         char sqlQuery[MAX_QUERY_LEN+1];
122
123         memset(sqlQuery, 0x00, sizeof(sqlQuery));
124         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET READ_STATUS = %d WHERE MSG_ID = %d;",
125                                         MSGFW_MESSAGE_TABLE_NAME, (int)bRead, MsgId);
126
127         if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
128                 return MSG_ERR_DB_EXEC;
129
130         // Get MAIN_TYPE, SUB_TYPE, STORAGE_ID
131         memset(sqlQuery, 0x00, sizeof(sqlQuery));
132         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MAIN_TYPE, A.SUB_TYPE, B.CONTACT_ID, B.ADDRESS_ID \
133                                         FROM %s A, %s B \
134                                      WHERE A.MSG_ID = %d AND A.ADDRESS_ID = B.ADDRESS_ID;",
135                         MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME, MsgId);
136
137         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
138                 return MSG_ERR_DB_PREPARE;
139
140         MSG_MESSAGE_TYPE_S msgType;
141         MSG_CONTACT_ID_T contactId;
142         unsigned int addrId;
143
144         if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW)
145         {
146                 msgType.mainType = pDbHandle->columnInt(0);
147                 msgType.subType = pDbHandle->columnInt(1);
148                 contactId = pDbHandle->columnInt(2);
149                 addrId = pDbHandle->columnInt(3);
150         }
151         else
152         {
153                 pDbHandle->finalizeQuery();
154                 return MSG_ERR_DB_STEP;
155         }
156
157         pDbHandle->finalizeQuery();
158
159         MSG_DEBUG("Main Type:[%d] SubType:[%d] ContactId:[%d] AddrId:[%d]", msgType.mainType, msgType.subType, contactId, addrId);
160
161         if (MsgStoUpdateAddress(pDbHandle, addrId) != MSG_SUCCESS)
162         {
163                 MSG_DEBUG("MsgStoUpdateAddress() Error");
164                 return MSG_ERR_STORAGE_ERROR;
165         }
166
167         int smsCnt = 0, mmsCnt = 0;
168
169         smsCnt = MsgStoGetUnreadCnt(pDbHandle, MSG_SMS_TYPE);
170         mmsCnt = MsgStoGetUnreadCnt(pDbHandle, MSG_MMS_TYPE);
171
172         MsgSettingSetIndicator(smsCnt, mmsCnt);
173
174         MsgDeleteNotiByMsgId(MsgId);
175
176         return MSG_SUCCESS;
177 }
178
179
180 MSG_ERROR_T MsgStoGetOldestMessage(MsgDbHandler *pDbHandle, const MSG_MESSAGE_INFO_S *pMsgInfo, MSG_MESSAGE_ID_T *pMsgId)
181 {
182         char sqlQuery[MAX_QUERY_LEN+1];
183
184         memset(sqlQuery, 0x00, sizeof(sqlQuery));
185
186         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID \
187                                                                        FROM %s \
188                                                                     WHERE SUB_TYPE = %d AND FOLDER_ID = %d AND STORAGE_ID = %d AND PROTECTED = 0 \
189                                                                      ORDER BY DISPLAY_TIME ASC",
190                                 MSGFW_MESSAGE_TABLE_NAME, pMsgInfo->msgType.subType, pMsgInfo->folderId, MSG_STORAGE_PHONE);
191
192         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
193                 return MSG_ERR_DB_PREPARE;
194
195         if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW)
196         {
197                 *pMsgId = pDbHandle->columnInt(0);
198         }
199         else
200         {
201                 pDbHandle->finalizeQuery();
202                 return MSG_ERR_DB_STEP;
203         }
204
205         pDbHandle->finalizeQuery();
206
207         return MSG_SUCCESS;
208 }
209
210
211 MSG_ERROR_T MsgStoCheckMsgCntFull(MsgDbHandler *pDbHandle, const MSG_MESSAGE_TYPE_S* pMsgType, MSG_FOLDER_ID_T FolderId)
212 {
213         MSG_BEGIN();
214
215         MSG_ERROR_T err = MSG_SUCCESS;
216
217         struct statfs buf = {0};
218
219         if (statfs(MSG_DATA_ROOT_PATH, &buf) == -1)
220         {
221                 MSG_DEBUG("statfs(\"%s\") failed - %d", MSG_DATA_ROOT_PATH);
222                 return MSG_ERR_STORAGE_ERROR;
223         }
224
225         unsigned long freeSpace = (buf.f_bfree * buf.f_bsize);
226
227         MSG_DEBUG("f_bfree [%d] f_bsize [%d]", buf.f_bfree, buf.f_bsize);
228         MSG_DEBUG("Free space of storage is [%ul] MB.", freeSpace);
229
230         if (freeSpace < SMS_MINIMUM_SPACE && pMsgType->mainType == MSG_SMS_TYPE)
231                 err = MSG_ERR_MESSAGE_COUNT_FULL;
232         else if(freeSpace < MMS_MINIMUM_SPACE && pMsgType->mainType == MSG_MMS_TYPE)
233                 err = MSG_ERR_MESSAGE_COUNT_FULL;
234
235         return err;
236
237         MSG_END();
238
239         return err;
240 }
241
242
243 MSG_ERROR_T MsgStoCountMsgByLimitCategory(MsgDbHandler *pDbHandle, const MSG_MESSAGE_TYPE_S *pMsgType, int *pMsgCount, MSG_FOLDER_ID_T folderId )
244 {
245         if (pMsgType == NULL)
246         {
247                 MSG_DEBUG("pMsgType is NULL");
248                 return MSG_ERR_NULL_POINTER;
249         }
250
251         *pMsgCount = 0;
252
253         char sqlQuery[MAX_QUERY_LEN+1];
254         memset(sqlQuery, 0x00, sizeof(sqlQuery));
255
256         if ((pMsgType->mainType == MSG_SMS_TYPE) && (pMsgType->subType == MSG_WAP_SI_SMS ||pMsgType->subType == MSG_WAP_SL_SMS)) // PUSH
257         {
258                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d) AND FOLDER_ID = %d;",
259                                 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_WAP_SI_SMS, MSG_WAP_SL_SMS, MSG_INBOX_ID);
260         }
261         else if ((pMsgType->mainType == MSG_SMS_TYPE) && (pMsgType->subType == MSG_CB_SMS)) // CB
262         {
263                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d) AND FOLDER_ID = %d;",
264                                 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_CB_SMS, MSG_CBMSGBOX_ID);
265         }
266         else if ((pMsgType->mainType == MSG_SMS_TYPE) && (pMsgType->subType == MSG_SYNCML_CP)) // Provision
267         {
268                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d) AND FOLDER_ID = %d;",
269                                 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_SYNCML_CP, MSG_INBOX_ID);
270         }
271         else if ((pMsgType->mainType == MSG_SMS_TYPE)) // SMS
272         {
273                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE NOT IN (%d, %d, %d, %d) AND FOLDER_ID = %d;",
274                                 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_WAP_SI_SMS, MSG_WAP_SL_SMS, MSG_CB_SMS, MSG_SYNCML_CP, MSG_INBOX_ID); // etc SMS
275         }
276         else if ((pMsgType->mainType == MSG_MMS_TYPE) && (pMsgType->subType == MSG_SENDREQ_MMS || pMsgType->subType == MSG_SENDCONF_MMS || pMsgType->subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsgType->subType == MSG_RETRIEVE_MANUALCONF_MMS || pMsgType->subType == MSG_NOTIFICATIONIND_MMS)) // MMS
277         {
278                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d, %d, %d, %d) AND FOLDER_ID = %d;",
279                                 MSGFW_MESSAGE_TABLE_NAME, pMsgType->mainType, MSG_SENDREQ_MMS, MSG_SENDCONF_MMS, MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS, MSG_NOTIFICATIONIND_MMS, folderId);
280         }
281         else
282         {
283                 return MSG_ERR_INVALID_PARAMETER;
284         }
285
286         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
287                 return MSG_ERR_DB_PREPARE;
288
289         if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW)
290         {
291                 *pMsgCount = pDbHandle->columnInt(0);
292         }
293         else
294         {
295                 pDbHandle->finalizeQuery();
296                 return MSG_ERR_DB_STEP;
297         }
298
299         pDbHandle->finalizeQuery();
300
301         return MSG_SUCCESS;
302 }
303
304
305 int MsgStoCheckMsgCntLimit(const MSG_MESSAGE_TYPE_S* pMsgType, MSG_FOLDER_ID_T FolderId)
306 {
307         int msgboxType = -1;
308         int msgType= -1;
309
310         switch (FolderId)
311         {
312                 case MSG_INBOX_ID :
313                         msgboxType = MSG_COUNT_LIMIT_INBOX_TYPE;
314                 break;
315
316                 case MSG_OUTBOX_ID :
317                         msgboxType = MSG_COUNT_LIMIT_OUTBOX_TYPE;
318                 break;
319
320                 case MSG_SENTBOX_ID :
321                         msgboxType = MSG_COUNT_LIMIT_SENTBOX_TYPE;
322                 break;
323
324                 case MSG_DRAFT_ID :
325                         msgboxType = MSG_COUNT_LIMIT_DRAFTBOX_TYPE;
326                 break;
327
328                 case MSG_CBMSGBOX_ID :
329                         msgboxType = MSG_COUNT_LIMIT_CBMSGBOX_TYPE;
330                 break;
331
332                 default:
333                         MSG_DEBUG("Unknown mailbox Type [%d]", FolderId);
334                 return -1;
335         }
336
337         switch (pMsgType->subType)
338         {
339                 case MSG_NORMAL_SMS:
340                 case MSG_REPLACE_TYPE1_SMS:
341                 case MSG_REPLACE_TYPE2_SMS:
342                 case MSG_REPLACE_TYPE3_SMS:
343                 case MSG_REPLACE_TYPE4_SMS:
344                 case MSG_REPLACE_TYPE5_SMS:
345                 case MSG_REPLACE_TYPE6_SMS:
346                 case MSG_REPLACE_TYPE7_SMS:
347                 case MSG_MWI_VOICE_SMS:
348                 case MSG_MWI_FAX_SMS:
349                 case MSG_MWI_EMAIL_SMS:
350                 case MSG_MWI_OTHER_SMS:
351                 case MSG_STATUS_REPORT_SMS:
352                         msgType = MSG_COUNT_LIMIT_SMS_TYPE;
353                 break;
354
355                 case MSG_CB_SMS:
356                         msgType = MSG_COUNT_LIMIT_CB_TYPE;
357                 break;
358
359                 case MSG_WAP_SI_SMS:
360                 case MSG_WAP_SL_SMS:
361                         msgType = MSG_COUNT_LIMIT_WAPPUSH_TYPE;
362                 break;
363
364                 case MSG_SYNCML_CP:
365                         msgType = MSG_COUNT_LIMIT_PROVISION_TYPE;
366                 break;
367
368                 case MSG_SENDREQ_MMS:
369                 case MSG_SENDCONF_MMS:
370                 case MSG_NOTIFICATIONIND_MMS:
371                 case MSG_RETRIEVE_AUTOCONF_MMS:
372                 case MSG_RETRIEVE_MANUALCONF_MMS:
373                         msgType = MSG_COUNT_LIMIT_MMS_TYPE;
374                 break;
375
376                 default:
377                         MSG_DEBUG("Unknown Message Type [%d]", pMsgType->subType);
378                 return -1;
379         }
380
381         return msgCntLimit[msgboxType][msgType];
382 }
383
384
385 MSG_ERROR_T MsgStoAddAddress(MsgDbHandler *pDbHandle, const MSG_ADDRESS_INFO_S *pAddrInfo, unsigned int *pAddrId)
386 {
387         MSG_ERROR_T err = MSG_SUCCESS;
388
389         char sqlQuery[MAX_QUERY_LEN+1];
390
391         // Check if new address or not
392         if (MsgExistAddress(pDbHandle, pAddrInfo->addressVal, pAddrId) == true)
393         {
394                 MSG_DEBUG("The address already exists. ID : [%d], Value : [%s]", *pAddrId, pAddrInfo->addressVal);
395                 return err;
396         }
397
398         // Get Contact Info
399         MSG_CONTACT_INFO_S contactInfo = {0};
400
401         err = MsgGetContactInfo(pAddrInfo, &contactInfo);
402
403         if (err != MSG_SUCCESS)
404         {
405                 MSG_DEBUG("MsgGetContactInfo() fail [%d]", err);
406                 return err;
407         }
408
409         if (pDbHandle->getRowId(MSGFW_ADDRESS_TABLE_NAME, pAddrId) != MSG_SUCCESS)
410         {
411                 return err;
412         }
413
414         // Add Address
415         memset(sqlQuery, 0x00, sizeof(sqlQuery));
416         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d, '%s', %d, ?, ?, ?, '%s', 0, 0, 0, 0, 0, 0, 0, 0, '');",
417                                 MSGFW_ADDRESS_TABLE_NAME, *pAddrId, pAddrInfo->addressType, pAddrInfo->recipientType, pAddrInfo->addressVal,
418                                 contactInfo.contactId, contactInfo.imagePath);
419
420         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
421                 return MSG_ERR_DB_PREPARE;
422
423         pDbHandle->bindText(contactInfo.displayName, 1);
424         pDbHandle->bindText(contactInfo.firstName, 2);
425         pDbHandle->bindText(contactInfo.lastName, 3);
426
427         if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE)
428         {
429                 pDbHandle->finalizeQuery();
430                 MSG_DEBUG("Add Address Info. Fail [%s]", sqlQuery);
431                 return MSG_ERR_DB_STEP;
432         }
433
434         pDbHandle->finalizeQuery();
435
436         return err;
437 }
438
439
440 MSG_ERROR_T MsgStoUpdateAddress(MsgDbHandler *pDbHandle, unsigned int AddrId)
441 {
442         MSG_BEGIN();
443
444         MSG_ERROR_T err = MSG_SUCCESS;
445
446         int unreadCnt = 0, smsCnt = 0, mmsCnt = 0;
447
448         char msgText[MAX_THREAD_DATA_LEN+1];
449         char sqlQuery[MAX_QUERY_LEN+1];
450
451         // Get Unread Count
452         memset(sqlQuery, 0x00, sizeof(sqlQuery));
453
454         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s \
455                                                                 WHERE ADDRESS_ID = %d \
456                                                                      AND FOLDER_ID = %d \
457                                                                      AND READ_STATUS = 0;",
458                                 MSGFW_MESSAGE_TABLE_NAME, AddrId, MSG_INBOX_ID);
459
460         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
461                 return MSG_ERR_DB_PREPARE;
462
463         err = pDbHandle->stepQuery();
464
465         if (err == MSG_ERR_DB_ROW)
466         {
467                 unreadCnt = pDbHandle->columnInt(0);
468         }
469         else if (err != MSG_ERR_DB_DONE)
470         {
471                 pDbHandle->finalizeQuery();
472                 return MSG_ERR_DB_STEP;
473         }
474
475         pDbHandle->finalizeQuery();
476
477         // Get SMS Count
478         memset(sqlQuery, 0x00, sizeof(sqlQuery));
479
480         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s \
481                                                                 WHERE ADDRESS_ID = %d \
482                                                                      AND MAIN_TYPE = %d \
483                                                                      AND FOLDER_ID > 0 AND FOLDER_ID < %d;",
484                                 MSGFW_MESSAGE_TABLE_NAME, AddrId, MSG_SMS_TYPE, MSG_CBMSGBOX_ID);
485
486         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
487                 return MSG_ERR_DB_PREPARE;
488
489         err = pDbHandle->stepQuery();
490
491         if (err == MSG_ERR_DB_ROW)
492         {
493                 smsCnt = pDbHandle->columnInt(0);
494         }
495         else if (err != MSG_ERR_DB_DONE)
496         {
497                 pDbHandle->finalizeQuery();
498                 return MSG_ERR_DB_STEP;
499         }
500
501         pDbHandle->finalizeQuery();
502
503         // Get MMS Count
504         memset(sqlQuery, 0x00, sizeof(sqlQuery));
505
506         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s \
507                                                                 WHERE ADDRESS_ID = %d \
508                                                                      AND MAIN_TYPE = %d \
509                                                                      AND SUB_TYPE NOT IN (%d, %d, %d) \
510                                                                      AND FOLDER_ID > 0 AND FOLDER_ID < %d;",
511                                 MSGFW_MESSAGE_TABLE_NAME, AddrId, MSG_MMS_TYPE, MSG_DELIVERYIND_MMS,
512                                 MSG_READRECIND_MMS, MSG_READORGIND_MMS, MSG_CBMSGBOX_ID);
513
514         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
515                 return MSG_ERR_DB_PREPARE;
516
517         err = pDbHandle->stepQuery();
518
519         if (err == MSG_ERR_DB_ROW)
520         {
521                 mmsCnt = pDbHandle->columnInt(0);
522         }
523         else if (err != MSG_ERR_DB_DONE)
524         {
525                 pDbHandle->finalizeQuery();
526                 return MSG_ERR_DB_STEP;
527         }
528
529         pDbHandle->finalizeQuery();
530
531         // Get Latest Msg Data
532         MSG_MAIN_TYPE_T mainType = MSG_UNKNOWN_TYPE;
533         MSG_SUB_TYPE_T subType = MSG_NORMAL_SMS;
534         MSG_DIRECTION_TYPE_T direction = MSG_DIRECTION_TYPE_MO;
535         time_t msgTime = 0;
536
537         memset(sqlQuery, 0x00, sizeof(sqlQuery));
538
539         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MAIN_TYPE, SUB_TYPE, MSG_DIRECTION, DISPLAY_TIME, SUBJECT, MSG_TEXT FROM %s \
540                                       WHERE ADDRESS_ID = %d \
541                                       AND FOLDER_ID > 0 AND FOLDER_ID < %d \
542                                       ORDER BY DISPLAY_TIME DESC;",
543                                 MSGFW_MESSAGE_TABLE_NAME, AddrId, MSG_CBMSGBOX_ID);
544
545         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
546                 return MSG_ERR_DB_PREPARE;
547
548         err = pDbHandle->stepQuery();
549
550         if (err == MSG_ERR_DB_ROW)
551         {
552                 mainType = pDbHandle->columnInt(0);
553                 subType = pDbHandle->columnInt(1);
554                 direction = pDbHandle->columnInt(2);
555
556                 msgTime = (time_t)pDbHandle->columnInt(3);
557
558                 memset(msgText, 0x00, sizeof(msgText));
559
560                 if (mainType == MSG_SMS_TYPE)
561                 {
562                         if (pDbHandle->columnText(5) != NULL)
563                                 strncpy(msgText, (char*)pDbHandle->columnText(5), MAX_THREAD_DATA_LEN);
564                 }
565                 else if (mainType == MSG_MMS_TYPE)
566                 {
567                         if (pDbHandle->columnText(4) != NULL)
568                         {
569                                 strncpy(msgText, (char*)pDbHandle->columnText(4), MAX_THREAD_DATA_LEN);
570                         }
571
572                         if ((strlen(msgText) <= 0) && (pDbHandle->columnText(5) != NULL) && (subType != MSG_NOTIFICATIONIND_MMS))
573                         {
574                                 memset(msgText, 0x00, sizeof(msgText));
575                                 strncpy(msgText, (char*)pDbHandle->columnText(5), MAX_THREAD_DATA_LEN);
576                         }
577                 }
578         }
579         else if (err != MSG_ERR_DB_DONE)
580         {
581                 pDbHandle->finalizeQuery();
582                 return MSG_ERR_DB_STEP;
583         }
584
585         pDbHandle->finalizeQuery();
586
587         // Update Address Table
588         memset(sqlQuery, 0x00, sizeof(sqlQuery));
589
590         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET UNREAD_CNT = %d, SMS_CNT = %d, MMS_CNT = %d, MAIN_TYPE = %d, SUB_TYPE = %d, MSG_DIRECTION = %d, MSG_TIME = %ld, MSG_TEXT = ? \
591                                       WHERE ADDRESS_ID = %d;",
592                                 MSGFW_ADDRESS_TABLE_NAME, unreadCnt, smsCnt, mmsCnt, mainType, subType, direction, msgTime, AddrId);
593
594         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
595                 return MSG_ERR_DB_PREPARE;
596
597         pDbHandle->bindText(msgText, 1);
598
599         if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE)
600         {
601                 pDbHandle->finalizeQuery();
602                 MSG_DEBUG("Update Address Info. Fail [%d] [%s]", err, sqlQuery);
603                 return MSG_ERR_DB_STEP;
604         }
605
606         pDbHandle->finalizeQuery();
607
608         MSG_END();
609
610         return MSG_SUCCESS;
611 }
612
613
614 MSG_ERROR_T MsgStoClearAddressTable(MsgDbHandler *pDbHandle)
615 {
616         MSG_ERROR_T err = MSG_SUCCESS;
617
618         char sqlQuery[MAX_QUERY_LEN+1];
619
620         memset(sqlQuery, 0x00, sizeof(sqlQuery));
621
622         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s \
623                                      WHERE ADDRESS_ID NOT IN (SELECT ADDRESS_ID FROM %s) \
624                                           AND ADDRESS_ID <> 0;",
625                                 MSGFW_ADDRESS_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME);
626
627         err = pDbHandle->execQuery(sqlQuery);
628
629         return err;
630 }
631
632
633 void MsgConvertNumber(const char* pSrcNum, char* pDestNum)
634 {
635         int overLen = 0;
636         int i = 0;
637
638         overLen = strlen(pSrcNum) - MAX_PRECONFIG_NUM;
639
640         for (i = 0; i < MAX_PRECONFIG_NUM; i++)
641                 pDestNum[i] = pSrcNum[i+overLen];
642
643         pDestNum[i] = '\0';
644 }
645
646
647 bool MsgExistAddress(MsgDbHandler *pDbHandle, const char *pAddress, unsigned int *pAddrId)
648 {
649         MSG_ERROR_T err = MSG_SUCCESS;
650
651         char sqlQuery[MAX_QUERY_LEN+1];
652
653         *pAddrId = 0;
654
655         if (strlen(pAddress) > MAX_PRECONFIG_NUM)
656         {
657                 char newPhoneNum[MAX_PRECONFIG_NUM+1];
658
659                 memset(newPhoneNum, 0x00, sizeof(newPhoneNum));
660
661                 MsgConvertNumber(pAddress, newPhoneNum);
662
663                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
664
665                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s WHERE ADDRESS_VAL LIKE '%%%%%s';",
666                                                 MSGFW_ADDRESS_TABLE_NAME, newPhoneNum);
667         }
668         else
669         {
670                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
671
672                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ADDRESS_ID FROM %s WHERE ADDRESS_VAL = '%s';",
673                                                 MSGFW_ADDRESS_TABLE_NAME, pAddress);
674         }
675
676         int rowCnt = 0, addrId = 0;
677
678         err = pDbHandle->getTable(sqlQuery, &rowCnt);
679
680         // No record or other error
681         if (err != MSG_SUCCESS)
682         {
683                 pDbHandle->freeTable();
684                 return false;
685         }
686
687         addrId = pDbHandle->getColumnToInt(1);
688
689         MSG_DEBUG("AddressId : [%d]", addrId);
690
691         if (addrId > 0)
692         {
693                 *pAddrId = addrId;
694
695                 pDbHandle->freeTable();
696                 return true;
697         }
698         else
699         {
700                 pDbHandle->freeTable();
701                 return false;
702         }
703
704         pDbHandle->freeTable();
705
706         return false;
707 }
708
709
710 int MsgStoGetUnreadCnt(MsgDbHandler *pDbHandle, MSG_MAIN_TYPE_T MsgType)
711 {
712         int msgCnt = 0;
713
714         char sqlQuery[MAX_QUERY_LEN+1];
715
716         memset(sqlQuery, 0x00, sizeof(sqlQuery));
717
718         if (MsgType == MSG_SMS_TYPE)
719         {
720                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s \
721                                                                         WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d, %d, %d, %d, %d, %d) AND \
722                                                                         FOLDER_ID = %d AND READ_STATUS = 0;",
723                                                                         MSGFW_MESSAGE_TABLE_NAME, MSG_SMS_TYPE, MSG_NORMAL_SMS, MSG_STATUS_REPORT_SMS, MSG_CONCAT_SIM_SMS, MSG_WAP_SI_SMS, MSG_WAP_SL_SMS, MSG_MWI_VOICE_SMS, MSG_SYNCML_CP, MSG_INBOX_ID);
724         }
725         else if (MsgType == MSG_MMS_TYPE)
726         {
727                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT COUNT(MSG_ID) FROM %s \
728                                                                         WHERE MAIN_TYPE = %d AND SUB_TYPE IN (%d, %d, %d) AND \
729                                                                         FOLDER_ID = %d AND READ_STATUS = 0;",
730                                                                         MSGFW_MESSAGE_TABLE_NAME, MSG_MMS_TYPE, MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS, MSG_NOTIFICATIONIND_MMS, MSG_INBOX_ID);
731         }
732
733         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
734                 return 0;
735
736         if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW)
737         {
738                 msgCnt = pDbHandle->columnInt(0);
739         }
740         else
741         {
742                 pDbHandle->finalizeQuery();
743                 return 0;
744         }
745
746         pDbHandle->finalizeQuery();
747
748         return msgCnt;
749 }
750
751
752 MSG_ERROR_T MsgStoAddContactInfo(MsgDbHandler *pDbHandle, MSG_CONTACT_INFO_S *pContactInfo, const char *pNumber)
753 {
754         char newPhoneNum[MAX_PRECONFIG_NUM+1];
755         char sqlQuery[MAX_QUERY_LEN+1];
756
757         if (strlen(pNumber) > MAX_PRECONFIG_NUM)
758         {
759                 memset(newPhoneNum, 0x00, sizeof(newPhoneNum));
760                 MsgConvertNumber(pNumber, newPhoneNum);
761
762                 MSG_DEBUG("Phone Number to Compare : [%s]", newPhoneNum);
763
764                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
765                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE MSG_ADDRESS_TABLE SET CONTACT_ID = %d, DISPLAY_NAME = ?, FIRST_NAME = ?, LAST_NAME = ?, IMAGE_PATH = '%s' \
766                                                                                 WHERE ADDRESS_VAL LIKE '%%%%%s';",
767                                                                         pContactInfo->contactId,
768                                                                         pContactInfo->imagePath,
769                                                                         newPhoneNum);
770         }
771         else
772         {
773                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
774                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE MSG_ADDRESS_TABLE SET CONTACT_ID = %d, DISPLAY_NAME = ?, FIRST_NAME = ?, LAST_NAME = ?, IMAGE_PATH = '%s' \
775                                                                                 WHERE ADDRESS_VAL = '%s';",
776                                                                         pContactInfo->contactId,
777                                                                         pContactInfo->imagePath,
778                                                                         pNumber);
779         }
780
781         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
782                 return MSG_ERR_DB_PREPARE;
783
784         pDbHandle->bindText(pContactInfo->displayName, 1);
785
786         pDbHandle->bindText(pContactInfo->firstName, 2);
787
788         pDbHandle->bindText(pContactInfo->lastName, 3);
789
790         if (pDbHandle->stepQuery() != MSG_ERR_DB_DONE)
791         {
792                 pDbHandle->finalizeQuery();
793                 MSG_DEBUG("Update contact Info. Fail [%s]", sqlQuery);
794                 return MSG_ERR_DB_STEP;
795         }
796
797         pDbHandle->finalizeQuery();
798
799         return MSG_SUCCESS;
800 }
801
802
803 MSG_ERROR_T MsgStoClearContactInfo(MsgDbHandler *pDbHandle, int ContactId)
804 {
805         char sqlQuery[MAX_QUERY_LEN+1];
806
807         memset(sqlQuery, 0x00, sizeof(sqlQuery));
808
809         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE MSG_ADDRESS_TABLE SET CONTACT_ID = 0, DISPLAY_NAME = '', FIRST_NAME = '', LAST_NAME = '', IMAGE_PATH = '' \
810                                 WHERE CONTACT_ID = %d;", ContactId);
811
812         if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
813         {
814                 MSG_DEBUG("Fail to execute query");
815                 return MSG_ERR_DB_EXEC;
816         }
817
818         return MSG_SUCCESS;
819 }
820
821
822 MSG_ERROR_T MsgStoClearContactInfo(MsgDbHandler *pDbHandle, int ContactId, const char *pNumber)
823 {
824         char newPhoneNum[MAX_PRECONFIG_NUM+1];
825         char sqlQuery[MAX_QUERY_LEN+1];
826
827         memset(newPhoneNum, 0x00, sizeof(newPhoneNum));
828         memset(sqlQuery, 0x00, sizeof(sqlQuery));
829
830         if (strlen(pNumber) > MAX_PRECONFIG_NUM)
831         {
832                 MsgConvertNumber(pNumber, newPhoneNum);
833
834                 MSG_DEBUG("Phone Number to Compare : [%s]", newPhoneNum);
835
836                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE MSG_ADDRESS_TABLE SET CONTACT_ID = 0, DISPLAY_NAME = '', FIRST_NAME = '', LAST_NAME = '', IMAGE_PATH = '' \
837                                 WHERE CONTACT_ID = %d AND ADDRESS_VAL NOT LIKE '%%%s';", ContactId, newPhoneNum);
838         }
839         else
840         {
841                 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE MSG_ADDRESS_TABLE SET CONTACT_ID = 0, DISPLAY_NAME = '', FIRST_NAME = '', LAST_NAME = '', IMAGE_PATH = '' \
842                                 WHERE CONTACT_ID = %d AND ADDRESS_VAL <> '%s';", ContactId, pNumber);
843         }
844
845         if (pDbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
846         {
847                 MSG_DEBUG("Fail to execute query");
848                 return MSG_ERR_DB_EXEC;
849         }
850
851         return MSG_SUCCESS;
852 }
853
854
855 MSG_ERROR_T MsgStoGetMmsRawFilePath(MsgDbHandler *pDbHandle, MSG_MESSAGE_ID_T msgId, char *pFilePath)
856 {
857         char sqlQuery[MAX_QUERY_LEN+1];
858
859         memset(sqlQuery, 0x00, sizeof(sqlQuery));
860
861         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILE_PATH FROM %s WHERE REFERENCE_ID IN \
862                                 (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
863                                 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, msgId);
864
865         if (pDbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
866                 return MSG_ERR_DB_PREPARE;
867
868         if (pDbHandle->stepQuery() == MSG_ERR_DB_ROW)
869         {
870                 if (pDbHandle->columnText(0) != NULL)
871                 {
872                         strncpy(pFilePath, (char*)pDbHandle->columnText(0), MSG_FILEPATH_LEN_MAX);
873                 }
874         }
875         else
876         {
877                 pDbHandle->finalizeQuery();
878                 return MSG_ERR_DB_STEP;
879         }
880
881         pDbHandle->finalizeQuery();
882
883         return MSG_SUCCESS;
884 }
885
886
887 bool MsgStoCheckReadReportRequested(MsgDbHandler *pDbHandle, MSG_MESSAGE_ID_T MsgId)
888 {
889         MSG_ERROR_T err = MSG_SUCCESS;
890
891         char sqlQuery[MAX_QUERY_LEN+1];
892         int rowCnt = 0;
893         bool bReadReportRequested = false;
894
895         memset(sqlQuery, 0x00, sizeof(sqlQuery));
896
897         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ASK_READ_REPLY FROM %s WHERE REFERENCE_ID IN \
898                                 (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
899                                 MMS_PLUGIN_ATTRIBUTE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MsgId);
900
901         err = pDbHandle->getTable(sqlQuery, &rowCnt);
902
903         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD)
904         {
905                 pDbHandle->freeTable();
906                 MSG_DEBUG("[Error]Failed to Get Table");
907                 return bReadReportRequested;
908         }
909
910         if (rowCnt != 1)
911         {
912                 pDbHandle->freeTable();
913                 MSG_DEBUG("[Error]MSG_ERR_DB_NORECORD");
914                 return bReadReportRequested;
915         }
916
917         bReadReportRequested = pDbHandle->getColumnToInt(1);
918
919         pDbHandle->freeTable();
920
921         return bReadReportRequested;
922 }
923
924
925 bool MsgStoCheckReadReportIsSent(MsgDbHandler *pDbHandle, MSG_MESSAGE_ID_T MsgId)
926 {
927         MSG_ERROR_T err = MSG_SUCCESS;
928
929         char sqlQuery[MAX_QUERY_LEN+1];
930
931         int rowCnt = 0;
932         bool bReadReportIsSent = true;
933
934         memset(sqlQuery, 0x00, sizeof(sqlQuery));
935
936         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT READ_REPORT_SENT FROM %s WHERE REFERENCE_ID IN \
937                                 (SELECT REFERENCE_ID FROM %s WHERE MSG_ID = %d);",
938                                 MMS_PLUGIN_ATTRIBUTE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MsgId);
939
940         err = pDbHandle->getTable(sqlQuery, &rowCnt);
941
942         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD)
943         {
944                 pDbHandle->freeTable();
945                 MSG_DEBUG("[Error]Failed to Get Table");
946                 return bReadReportIsSent;
947         }
948
949         if (rowCnt != 1)
950         {
951                 pDbHandle->freeTable();
952                 MSG_DEBUG("[Error]MSG_ERR_DB_NORECORD");
953                 return bReadReportIsSent;
954         }
955
956         bReadReportIsSent = (bool)pDbHandle->getColumnToInt(1);
957
958         pDbHandle->freeTable();
959
960         return bReadReportIsSent;
961 }
962
963 char *MsgStoReplaceString(const char *origStr, const char *oldStr, const char *newStr)
964 {
965         if (origStr == NULL)
966                 return NULL;
967
968         char *replaceStr = NULL;
969         char *pTemp = NULL;
970         int i = 0;
971         int matchedCnt = 0;
972         int oldStrLen = 0;
973         int newStrLen = 0;
974         int replaceSize = 0;
975
976         if (g_strcmp0(oldStr, newStr) != 0) {
977                 oldStrLen = strlen(oldStr);
978                 newStrLen = strlen(newStr);
979
980                 for (i = 0; origStr[i] != '\0';) {
981                         if (memcmp(&origStr[i], oldStr, oldStrLen) == 0) {
982                                 matchedCnt++;
983                                 i += oldStrLen;
984                         } else {
985                                 i++;
986                         }
987                 }
988         } else {
989                 return g_strdup(origStr);
990         }
991
992         replaceStr = (char *)calloc(1, i + sizeof(char) * (matchedCnt * (newStrLen - oldStrLen) + 1));
993         if (replaceStr == NULL)
994                 return NULL;
995
996         pTemp = replaceStr;
997
998         while (*origStr) {
999                 if (memcmp(origStr, oldStr, oldStrLen) == 0) {
1000                         memcpy(pTemp, newStr, newStrLen);
1001                         pTemp += newStrLen;
1002                         origStr += oldStrLen;
1003                 } else {
1004                         *pTemp++ = *origStr++;
1005                 }
1006         }
1007
1008         return replaceStr;
1009 }