update tizen source
[framework/messaging/msg-service.git] / framework / storage-handler / MsgStorageUtil.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 <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "MsgDebug.h"
36 #include "MsgCppTypes.h"
37 #include "MsgUtilFile.h"
38 #include "MsgContact.h"
39 #include "MsgSoundPlayer.h"
40 #include "MsgGconfWrapper.h"
41 #include "MsgSqliteWrapper.h"
42 #include "MsgPluginManager.h"
43 #include "MsgUtilStorage.h"
44 #include "MsgStorageHandler.h"
45
46
47 /*==================================================================================================
48                                      VARIABLES
49 ==================================================================================================*/
50 extern MsgDbHandler dbHandle;
51
52
53 /*==================================================================================================
54                                      FUNCTION IMPLEMENTATION
55 ==================================================================================================*/
56 MSG_ERROR_T MsgMakeSortRule(const MSG_SORT_RULE_S *pSortRule, char *pSqlSort)
57 {
58         char sql[128];
59         char order[6];
60
61         memset(sql, 0x00, sizeof(sql));
62         memset(order, 0x00, sizeof(order));
63
64         if (pSortRule->bAscending == true)
65                 strncpy(order, "ASC", 5);
66         else
67                 strncpy(order, "DESC", 5);
68
69         int nameOrder = MsgGetContactNameOrder();
70
71         switch (pSortRule->sortType)
72         {
73                 case MSG_SORT_BY_DISPLAY_FROM :
74                         if (nameOrder == 0)
75                                 snprintf(sql, sizeof(sql), "ORDER BY B.FIRST_NAME %s, B.LAST_NAME %s, B.ADDRESS_VAL, A.DISPLAY_TIME DESC;", order, order);
76                         else
77                                 snprintf(sql, sizeof(sql), "ORDER BY B.LAST_NAME %s, B.FIRST_NAME %s, B.ADDRESS_VAL, A.DISPLAY_TIME DESC;", order, order);
78                         break;
79                 case MSG_SORT_BY_DISPLAY_TO :
80                         if (nameOrder == 0)
81                                 snprintf(sql, sizeof(sql), "ORDER BY B.FIRST_NAME %s, B.LAST_NAME %s, B.ADDRESS_VAL, A.DISPLAY_TIME DESC;", order, order);
82                         else
83                                 snprintf(sql, sizeof(sql), "ORDER BY B.LAST_NAME %s, B.FIRST_NAME %s, B.ADDRESS_VAL, A.DISPLAY_TIME DESC;", order, order);
84                         break;
85                 case MSG_SORT_BY_DISPLAY_TIME :
86                         snprintf(sql, sizeof(sql), "ORDER BY A.DISPLAY_TIME %s;", order);
87                         break;
88                 case MSG_SORT_BY_MSG_TYPE :
89                         snprintf(sql, sizeof(sql), "ORDER BY A.MAIN_TYPE %s, A.DISPLAY_TIME DESC;", order);
90                         break;
91                 case MSG_SORT_BY_READ_STATUS :
92                         snprintf(sql, sizeof(sql), "ORDER BY A.READ_STATUS %s, A.DISPLAY_TIME DESC;", order);
93                         break;
94                 case MSG_SORT_BY_STORAGE_TYPE :
95                         snprintf(sql, sizeof(sql), "ORDER BY A.STORAGE_ID %s, A.DISPLAY_TIME DESC;", order);
96                         break;
97                 case MSG_SORT_BY_THREAD_NAME :
98                         if (nameOrder == 0)
99                                 snprintf(sql, sizeof(sql), "ORDER BY FIRST_NAME %s, LAST_NAME %s;", order, order);
100                         else
101                                 snprintf(sql, sizeof(sql), "ORDER BY LAST_NAME %s, FIRST_NAME %s;", order, order);
102                         break;
103                 case MSG_SORT_BY_THREAD_DATE :
104                         snprintf(sql, sizeof(sql), "ORDER BY MSG_TIME %s;", order);
105                         break;
106                 case MSG_SORT_BY_THREAD_COUNT :
107                         snprintf(sql, sizeof(sql), "ORDER BY UNREAD_CNT %s;", order);
108                         break;
109                 default :
110                         snprintf(sql, sizeof(sql), "ORDER BY A.DISPLAY_TIME %s;", order);
111                         break;
112         }
113
114         memcpy(pSqlSort, sql, strlen(sql));
115
116         return MSG_SUCCESS;
117 }
118
119
120 MSG_ERROR_T MsgStoGetSmsSendOpt(MSG_MESSAGE_ID_T MsgId, MSG_SENDINGOPT_INFO_S* pSendOpt)
121 {
122         char sqlQuery[MAX_QUERY_LEN+1];
123
124         memset(sqlQuery, 0x00, sizeof(sqlQuery));
125
126         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DELREP_REQ, KEEP_COPY, REPLY_PATH FROM %s WHERE MSG_ID = %d;",
127                                 MSGFW_SMS_SENDOPT_TABLE_NAME, MsgId);
128
129         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
130                 return MSG_ERR_DB_PREPARE;
131
132         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
133         {
134                 pSendOpt->bSetting = true;
135                 pSendOpt->bDeliverReq = (bool)dbHandle.columnInt(0);
136                 pSendOpt->bKeepCopy = (bool)dbHandle.columnInt(1);
137                 pSendOpt->option.smsSendOptInfo.bReplyPath = (bool)dbHandle.columnInt(2);
138         }
139         else
140         {
141                 dbHandle.finalizeQuery();
142                 return MSG_ERR_DB_STEP;
143         }
144
145         dbHandle.finalizeQuery();
146
147         return MSG_SUCCESS;
148 }
149
150
151 MSG_ERROR_T MsgStoGetMmsSendOpt(MSG_MESSAGE_ID_T MsgId, MSG_SENDINGOPT_INFO_S* pSendOpt)
152 {
153         char sqlQuery[MAX_QUERY_LEN+1];
154
155         memset(sqlQuery, 0x00, sizeof(sqlQuery));
156
157         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT ASK_DELIVERY_REPORT, KEEP_COPY, ASK_READ_REPLY, EXPIRY_TIME, PRIORITY FROM %s WHERE MSG_ID = %d;",
158                                 MMS_PLUGIN_ATTRIBUTE_TABLE_NAME, MsgId);
159
160         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
161                 return MSG_ERR_DB_PREPARE;
162
163         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
164         {
165                 pSendOpt->bSetting = true;
166                 pSendOpt->bDeliverReq = (bool)dbHandle.columnInt(0);
167                 pSendOpt->bKeepCopy = (bool)dbHandle.columnInt(1);
168                 pSendOpt->option.mmsSendOptInfo.bReadReq = (bool)dbHandle.columnInt(2);
169                 pSendOpt->option.mmsSendOptInfo.expiryTime.time = (unsigned int)dbHandle.columnInt(3);
170                 pSendOpt->option.mmsSendOptInfo.priority = (MSG_PRIORITY_TYPE_T)dbHandle.columnInt(4);
171         }
172         else
173         {
174                 dbHandle.finalizeQuery();
175                 return MSG_ERR_DB_STEP;
176         }
177
178         dbHandle.finalizeQuery();
179
180         return MSG_SUCCESS;
181 }
182
183
184 MSG_ERROR_T MsgStoAddScheduledMessage(MSG_MESSAGE_ID_T MsgID, int AlarmId, int ListenerFd)
185 {
186         char sqlQuery[MAX_QUERY_LEN+1];
187
188         memset(sqlQuery, 0x00, sizeof(sqlQuery));
189
190         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, %d);",
191                                 MSGFW_SCHEDULED_MSG_TABLE_NAME, MsgID, AlarmId, ListenerFd);
192
193         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
194         {
195                 return MSG_ERR_DB_EXEC;
196         }
197
198         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE)
199         {
200                 return MSG_ERR_DB_EXEC;
201         }
202
203         dbHandle.finalizeQuery();
204
205         return MSG_SUCCESS;
206 }
207
208
209 MSG_ERROR_T MsgStoDeleteScheduledMessage(MSG_MESSAGE_ID_T MsgId)
210 {
211         char sqlQuery[MAX_QUERY_LEN+1];
212
213         memset(sqlQuery, 0x00, sizeof(sqlQuery));
214
215         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID = %d;",
216                                 MSGFW_SCHEDULED_MSG_TABLE_NAME, MsgId);
217
218         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
219         {
220                 MSG_DEBUG("Delete Scheduled Msg Fail!!");
221                 return MSG_ERR_DB_EXEC;
222         }
223
224         return MSG_SUCCESS;
225 }
226
227
228 MSG_ERROR_T MsgStoGetScheduledMessage(int AlarmId, MSG_REQUEST_INFO_S *pReqInfo, int *pListenerFd)
229 {
230         MSG_BEGIN();
231
232         MSG_ERROR_T     err = MSG_SUCCESS;
233
234         char sqlQuery[MAX_QUERY_LEN+1];
235
236         memset(sqlQuery, 0x00, sizeof(sqlQuery));
237
238         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID, LISTENER_FD FROM %s WHERE ALARM_ID = %d;",
239                                 MSGFW_SCHEDULED_MSG_TABLE_NAME, AlarmId);
240
241         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
242                 return MSG_ERR_DB_PREPARE;
243
244         if (dbHandle.stepQuery() == MSG_ERR_DB_ROW)
245         {
246                 pReqInfo->msgInfo.msgId = dbHandle.columnInt(0);
247                 *pListenerFd = dbHandle.columnInt(1);
248         }
249
250         dbHandle.finalizeQuery();
251
252         err = MsgStoGetMessage(pReqInfo->msgInfo.msgId, &(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
253
254         if (err != MSG_SUCCESS)
255         {
256                 MSG_DEBUG("MsgStoGetMessage() Error!! [%d]", err);
257                 return err;
258         }
259
260         MSG_END();
261
262         return MSG_SUCCESS;
263 }
264
265
266 bool MsgStoCheckSyncMLMsgInThread(MSG_THREAD_ID_T threadId)
267 {
268         MSG_ERROR_T err = MSG_SUCCESS;
269         int rowCnt = 0;
270         bool isSyncMLMsg = false;
271
272         char sqlQuery[MAX_QUERY_LEN+1];
273
274         memset(sqlQuery, 0x00, sizeof(sqlQuery));
275
276         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE SUB_TYPE = %d AND ADDRESS_ID = %d;",
277                                 MSGFW_MESSAGE_TABLE_NAME, MSG_SYNCML_CP, threadId);
278
279         MSG_DEBUG("sqlQuery [%s]", sqlQuery);
280
281         err = dbHandle.getTable(sqlQuery, &rowCnt);
282
283         if (rowCnt > 0) isSyncMLMsg = true;
284
285         MSG_DEBUG("rowCnt [%d]", rowCnt);
286
287         dbHandle.freeTable();
288
289         return isSyncMLMsg;
290 }
291
292
293 MSG_ERROR_T MsgStoResetNetworkStatus()
294 {
295         MSG_BEGIN();
296
297         char sqlQuery[MAX_QUERY_LEN+1];
298
299         memset(sqlQuery, 0x00, sizeof(sqlQuery));
300
301         snprintf(sqlQuery, sizeof(sqlQuery),
302                 "UPDATE %s SET NETWORK_STATUS = %d WHERE NETWORK_STATUS = %d; UPDATE %s SET NETWORK_STATUS = %d WHERE NETWORK_STATUS = %d;"
303                 , MSGFW_MESSAGE_TABLE_NAME, MSG_NETWORK_SEND_FAIL, MSG_NETWORK_SENDING
304                 , MSGFW_MESSAGE_TABLE_NAME, MSG_NETWORK_RETRIEVE_FAIL, MSG_NETWORK_RETRIEVING);
305
306         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
307                 return MSG_ERR_DB_EXEC;
308
309         MSG_END();
310
311         return MSG_SUCCESS;
312 }
313
314
315 MSG_ERROR_T MsgStoCleanAbnormalMmsData()
316 {
317         MSG_BEGIN();
318
319         int rowCnt = 0, index = 2; // numbers of index
320
321         MSG_MESSAGE_ID_T msgId;
322
323         char sqlQuery[MAX_QUERY_LEN+1];
324         char    filePath[MSG_FILEPATH_LEN_MAX];
325
326         memset(sqlQuery, 0x00, sizeof(sqlQuery));
327
328         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT A.MSG_ID, A.FILE_PATH FROM %s A, %s B WHERE A.MSG_ID = B.MSG_ID AND (B.SUB_TYPE = %d OR B.SUB_TYPE = %d OR B.SUB_TYPE = %d);",
329                 MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_MESSAGE_TABLE_NAME, MSG_SENDCONF_MMS, MSG_RETRIEVE_AUTOCONF_MMS, MSG_RETRIEVE_MANUALCONF_MMS);
330
331         MSG_ERROR_T  err = dbHandle.getTable(sqlQuery, &rowCnt);
332
333         if (err == MSG_ERR_DB_NORECORD)
334         {
335                 dbHandle.freeTable();
336
337                 return MSG_SUCCESS;
338         }
339         else if (err != MSG_SUCCESS)
340         {
341                 MSG_DEBUG("%s", sqlQuery);
342
343                 dbHandle.freeTable();
344
345                 return err;
346         }
347
348
349         for (int i = 0; i < rowCnt; i++)
350         {
351                 memset(filePath, 0x00, sizeof(filePath));
352
353                 msgId = dbHandle.getColumnToInt(index++);
354
355                 dbHandle.getColumnToString(index++, MSG_FILEPATH_LEN_MAX, filePath);
356
357                 if(strlen(filePath) > 1)
358                 {
359                         MSG_DEBUG("strlen(filePath) [%d]", strlen(filePath));
360                         MSG_DEBUG("filePath [%s]", filePath);
361                         if(MsgGetFileSize(filePath) < 0)
362                         {
363                                 // abnormal mms message
364                                 MSG_DEBUG("abnormal mms message [%d]", msgId);
365
366                                 // delete mms message
367                                 MsgStoDeleteMessage(msgId, false);
368                         }
369                 }
370
371         }
372         dbHandle.freeTable();
373
374         MSG_END();
375
376         return MSG_SUCCESS;
377 }
378
379 MSG_ERROR_T MsgStoCheckReadReportStatus(MSG_MESSAGE_ID_T msgId)
380 {
381         MSG_BEGIN();
382
383         bool    bReadReportRequested;
384         bool    bReadReportIsSent;
385
386         bReadReportRequested = MsgStoCheckReadReportRequested(&dbHandle, msgId);
387         if(bReadReportRequested == false)
388                 return MSG_ERR_READREPORT_NOT_REQUESTED;
389
390         bReadReportIsSent = MsgStoCheckReadReportIsSent(&dbHandle, msgId);
391         if(bReadReportIsSent == true)
392                 return MSG_ERR_READREPORT_ALEADY_SENT;
393
394         MSG_END();
395
396         return MSG_SUCCESS;
397 }
398