Modify flora license version.
[platform/core/messaging/msg-service.git] / framework / storage-handler / MsgStorageFilter.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 "MsgDebug.h"
18 #include "MsgSqliteWrapper.h"
19 #include "MsgStorageHandler.h"
20
21
22 /*==================================================================================================
23                                      VARIABLES
24 ==================================================================================================*/
25 extern MsgDbHandler dbHandle;
26
27
28 /*==================================================================================================
29                                      FUNCTION IMPLEMENTATION
30 ==================================================================================================*/
31 msg_error_t MsgStoCheckDuplicatedFilter(const MSG_FILTER_S *pFilter)
32 {
33         MSG_BEGIN();
34
35         msg_error_t err = MSG_SUCCESS;
36         int rowCnt = 0;
37
38         char sqlQuery[MAX_QUERY_LEN+1];
39
40         memset(sqlQuery, 0x00, sizeof(sqlQuery));
41
42         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_ID FROM %s WHERE FILTER_TYPE = %d AND FILTER_VALUE = '%s';",
43                         MSGFW_FILTER_TABLE_NAME, pFilter->filterType, pFilter->filterValue);
44
45         MSG_DEBUG("sql : %s", sqlQuery);
46
47         err = dbHandle.getTable(sqlQuery, &rowCnt);
48
49         if (err == MSG_SUCCESS)
50                 err = MSG_ERR_FILTER_DUPLICATED;
51         else if (err == MSG_ERR_DB_NORECORD)
52                 err = MSG_SUCCESS;
53
54         dbHandle.freeTable();
55
56         MSG_END();
57
58         return err;
59 }
60
61
62 msg_error_t MsgStoAddFilter(const MSG_FILTER_S *pFilter)
63 {
64         MSG_BEGIN();
65
66         msg_error_t err = MSG_SUCCESS;
67
68         //check duplication
69         err = MsgStoCheckDuplicatedFilter(pFilter);
70
71         if (err != MSG_SUCCESS) {
72                 MSG_DEBUG("Filter is duplicated : [%d]", err);
73                 return err;
74         }
75
76         unsigned int rowId = 0;
77
78         char sqlQuery[MAX_QUERY_LEN+1];
79
80         err = dbHandle.getRowId(MSGFW_FILTER_TABLE_NAME, &rowId);
81
82         if (err != MSG_SUCCESS)
83                 return err;
84
85         // Add Filter
86         memset(sqlQuery, 0x00, sizeof(sqlQuery));
87
88         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, ?, 1);",
89                         MSGFW_FILTER_TABLE_NAME, rowId, pFilter->filterType);
90
91         MSG_DEBUG("sql : %s", sqlQuery);
92
93         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
94                 return MSG_ERR_DB_EXEC;
95
96         dbHandle.bindText(pFilter->filterValue, 1);
97
98         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
99                 dbHandle.finalizeQuery();
100                 return MSG_ERR_DB_EXEC;
101         }
102
103         dbHandle.finalizeQuery();
104
105         MSG_END();
106
107         return MSG_SUCCESS;
108 }
109
110
111 msg_error_t MsgStoUpdateFilter(const MSG_FILTER_S *pFilter)
112 {
113         MSG_BEGIN();
114
115         msg_error_t err = MSG_SUCCESS;
116
117         //check duplication
118         err = MsgStoCheckDuplicatedFilter(pFilter);
119
120         if (err != MSG_SUCCESS) {
121                 MSG_DEBUG("Filter is duplicated : [%d]", err);
122                 return err;
123         }
124
125         char sqlQuery[MAX_QUERY_LEN+1];
126
127         // Update Filter
128         memset(sqlQuery, 0x00, sizeof(sqlQuery));
129
130         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FILTER_TYPE = %d, FILTER_VALUE = ? WHERE FILTER_ID = %d;",
131                         MSGFW_FILTER_TABLE_NAME, pFilter->filterType, pFilter->filterId);
132
133         if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS)
134                 return MSG_ERR_DB_PREPARE;
135
136         dbHandle.bindText(pFilter->filterValue, 1);
137
138         if (dbHandle.stepQuery() != MSG_ERR_DB_DONE) {
139                 dbHandle.finalizeQuery();
140                 return MSG_ERR_DB_EXEC;
141         }
142
143         dbHandle.finalizeQuery();
144
145         MSG_END();
146
147         return MSG_SUCCESS;
148 }
149
150
151 msg_error_t MsgStoDeleteFilter(msg_filter_id_t filterId)
152 {
153         MSG_BEGIN();
154
155         char sqlQuery[MAX_QUERY_LEN+1];
156
157         dbHandle.beginTrans();
158
159         memset(sqlQuery, 0x00, sizeof(sqlQuery));
160
161         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FILTER_ID = %d;", MSGFW_FILTER_TABLE_NAME, filterId);
162
163         // Delete Filter
164         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
165                 dbHandle.endTrans(false);
166                 return MSG_ERR_DB_EXEC;
167         }
168
169         dbHandle.endTrans(true);
170
171         MSG_END();
172
173         return MSG_SUCCESS;
174 }
175
176
177 msg_error_t MsgStoGetFilterList(msg_struct_list_s *pFilterList)
178 {
179         MSG_BEGIN();
180
181         if (pFilterList == NULL) {
182                 MSG_DEBUG("pFilterList is NULL");
183                 return MSG_ERR_NULL_POINTER;
184         }
185
186         int rowCnt = 0, index = 4;
187
188         char sqlQuery[MAX_QUERY_LEN+1];
189
190         // Get filters from DB
191         memset(sqlQuery, 0x00, sizeof(sqlQuery));
192
193         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_ID, FILTER_TYPE, FILTER_VALUE, FILTER_ACTIVE FROM %s;", MSGFW_FILTER_TABLE_NAME);
194
195         msg_error_t err = MSG_SUCCESS;
196
197         err = dbHandle.getTable(sqlQuery, &rowCnt);
198
199         if (err == MSG_ERR_DB_NORECORD) {
200                 pFilterList->nCount = 0;
201                 pFilterList->msg_struct_info = NULL;
202
203                 dbHandle.freeTable();
204
205                 return MSG_SUCCESS;
206         } else if (err != MSG_SUCCESS) {
207                 dbHandle.freeTable();
208                 return err;
209         }
210
211         pFilterList->nCount = rowCnt;
212
213         MSG_DEBUG("pMsgCommInfoList->nCount [%d]", pFilterList->nCount);
214
215         pFilterList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FILTER_S *)*rowCnt];
216
217         msg_struct_s* pTmp = NULL;
218
219         for (int i = 0; i < rowCnt; i++)
220         {
221                 pFilterList->msg_struct_info[i] = (msg_struct_t)new char[sizeof(msg_struct_s)];
222
223                 pTmp = (msg_struct_s *)pFilterList->msg_struct_info[i];
224                 pTmp->type = MSG_STRUCT_FILTER;
225                 pTmp->data = new char [sizeof(MSG_FILTER_S)];
226                 MSG_FILTER_S *pFilter = (MSG_FILTER_S *)pTmp->data;
227                 memset(pFilter, 0x00, sizeof(MSG_FILTER_S));
228                 pFilter->filterId = dbHandle.getColumnToInt(index++);
229                 pFilter->filterType = dbHandle.getColumnToInt(index++);
230                 memset(pFilter->filterValue, 0x00, sizeof(pFilter->filterValue));
231                 dbHandle.getColumnToString(index++, MAX_FILTER_VALUE_LEN, pFilter->filterValue);
232                 pFilter->bActive = dbHandle.getColumnToInt(index++);
233         }
234
235
236         dbHandle.freeTable();
237
238         MSG_END();
239
240         return MSG_SUCCESS;
241 }
242
243
244 msg_error_t MsgStoSetFilterActivation(msg_filter_id_t filterId, bool bActive)
245 {
246         MSG_BEGIN();
247
248         char sqlQuery[MAX_QUERY_LEN+1];
249
250         // Set Filter Activation
251         memset(sqlQuery, 0x00, sizeof(sqlQuery));
252
253         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FILTER_ACTIVE = %d WHERE FILTER_ID = %d;",
254                         MSGFW_FILTER_TABLE_NAME, bActive, filterId);
255
256         MSG_DEBUG("sqlQuery [%s]", sqlQuery);
257
258         if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
259                 return MSG_ERR_DB_EXEC;
260         }
261
262         MSG_END();
263
264         return MSG_SUCCESS;
265 }