Modify flora license version.
[platform/core/messaging/msg-service.git] / mapi / msg_filter.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 <errno.h>
18
19 #include "MsgException.h"
20 #include "MsgDebug.h"
21 #include "MsgHandle.h"
22 #include "msg.h"
23
24 /*==================================================================================================
25                                      FUNCTION IMPLEMENTATION
26 ==================================================================================================*/
27 EXPORT_API int msg_add_filter(msg_handle_t handle, const msg_struct_t msg_struct_handle)
28 {
29         msg_error_t err =  MSG_SUCCESS;
30
31         // TODO : check NULL in msg_struct_handle
32         msg_struct_s *msg_struct = (msg_struct_s *) msg_struct_handle;
33
34         if (handle == NULL || msg_struct == NULL)
35         {
36                 return -EINVAL;
37         }
38
39         if(msg_struct->type != MSG_STRUCT_FILTER)
40         {
41                 return -EINVAL;
42         }
43
44         MsgHandle* pHandle = (MsgHandle*)handle;
45
46         try
47         {
48                 err = pHandle->addFilter((const MSG_FILTER_S *)msg_struct->data);
49         }
50         catch (MsgException& e)
51         {
52                 MSG_FATAL("%s", e.what());
53                 return MSG_ERR_FILTER_ERROR;
54         }
55
56         return err;
57 }
58
59
60 EXPORT_API int msg_update_filter(msg_handle_t handle, const msg_struct_t msg_struct_handle)
61 {
62         msg_error_t err =  MSG_SUCCESS;
63
64         // TODO : check NULL in msg_struct_handle
65         msg_struct_s *msg_struct = (msg_struct_s *) msg_struct_handle;
66
67         if (handle == NULL || msg_struct == NULL)
68         {
69                 return -EINVAL;
70         }
71
72
73         if(msg_struct->type != MSG_STRUCT_FILTER)
74         {
75                 return -EINVAL;
76         }
77
78         MsgHandle* pHandle = (MsgHandle*)handle;
79
80         try
81         {
82                 err = pHandle->updateFilter((const MSG_FILTER_S *)msg_struct->data);
83         }
84         catch (MsgException& e)
85         {
86                 MSG_FATAL("%s", e.what());
87                 return MSG_ERR_FILTER_ERROR;
88         }
89
90         return err;
91 }
92
93
94 EXPORT_API int msg_delete_filter(msg_handle_t handle, msg_filter_id_t filter_id)
95 {
96         msg_error_t err =  MSG_SUCCESS;
97
98         if (handle == NULL)
99         {
100                 return -EINVAL;
101         }
102
103         MsgHandle* pHandle = (MsgHandle*)handle;
104
105         try
106         {
107                 err = pHandle->deleteFilter(filter_id);
108         }
109         catch (MsgException& e)
110         {
111                 MSG_FATAL("%s", e.what());
112                 return MSG_ERR_FILTER_ERROR;
113         }
114
115         return err;
116 }
117
118
119 EXPORT_API int msg_get_filter_list(msg_handle_t handle, msg_struct_list_s *filter_list)
120 {
121         msg_error_t err =  MSG_SUCCESS;
122
123         if (handle == NULL || filter_list == NULL)
124         {
125                 return -EINVAL;
126         }
127
128         MsgHandle* pHandle = (MsgHandle*)handle;
129
130         try
131         {
132                 err = pHandle->getFilterList(filter_list);
133         }
134         catch (MsgException& e)
135         {
136                 MSG_FATAL("%s", e.what());
137                 return MSG_ERR_FILTER_ERROR;
138         }
139
140         return err;
141 }
142
143
144 EXPORT_API int msg_set_filter_operation(msg_handle_t handle, bool set_flag)
145 {
146         msg_error_t err =  MSG_SUCCESS;
147
148         if (handle == NULL)
149         {
150                 return -EINVAL;
151         }
152
153         MsgHandle* pHandle = (MsgHandle*)handle;
154
155         try
156         {
157                 err = pHandle->setFilterOperation(set_flag);
158         }
159         catch (MsgException& e)
160         {
161                 MSG_FATAL("%s", e.what());
162                 return MSG_ERR_FILTER_ERROR;
163         }
164
165         return err;
166 }
167
168
169 EXPORT_API int msg_get_filter_operation(msg_handle_t handle, bool *set_flag)
170 {
171         msg_error_t err =  MSG_SUCCESS;
172
173         if (handle == NULL)
174         {
175                 return -EINVAL;
176         }
177
178         MsgHandle* pHandle = (MsgHandle*)handle;
179
180         try
181         {
182                 err = pHandle->getFilterOperation(set_flag);
183         }
184         catch (MsgException& e)
185         {
186                 MSG_FATAL("%s", e.what());
187                 return MSG_ERR_FILTER_ERROR;
188         }
189
190         return err;
191 }
192
193
194 EXPORT_API int msg_set_filter_active(msg_handle_t handle, msg_filter_id_t filter_id, bool active)
195 {
196         msg_error_t err =  MSG_SUCCESS;
197
198         if (handle == NULL)
199         {
200                 return -EINVAL;
201         }
202
203         MsgHandle* pHandle = (MsgHandle*)handle;
204
205         try
206         {
207                 err = pHandle->setFilterActivation(filter_id, active);
208         }
209         catch (MsgException& e)
210         {
211                 MSG_FATAL("%s", e.what());
212                 return MSG_ERR_FILTER_ERROR;
213         }
214
215         return err;
216 }
217
218
219 bool msg_get_filter_info_bool(void *filter, int field)
220 {
221         if (!filter)
222                 return MSG_ERR_NULL_POINTER;
223
224         int ret = 0;
225
226         MSG_FILTER_S *filter_data = (MSG_FILTER_S *)filter;
227
228         switch (field)
229         {
230         case MSG_FILTER_ACTIVE_BOOL :
231                 ret = filter_data->bActive;
232                 break;
233         default :
234                 return MSG_ERR_INVALID_PARAMETER;
235         }
236
237         return ret;
238 }
239
240
241 int msg_get_filter_info_int(void *filter, int field)
242 {
243         if (!filter)
244                 return MSG_ERR_NULL_POINTER;
245
246         int ret = 0;
247
248         MSG_FILTER_S *filter_data = (MSG_FILTER_S *)filter;
249
250         switch (field)
251         {
252         case MSG_FILTER_ID_INT :
253                 ret = filter_data->filterId;
254                 break;
255         case MSG_FILTER_TYPE_INT :
256                 ret = filter_data->filterType;
257                 break;
258         default :
259                 return MSG_ERR_INVALID_PARAMETER;
260         }
261
262         return ret;
263 }
264
265
266 char *msg_get_filter_info_str(void *filter, int field)
267 {
268         if (!filter)
269                 return NULL;
270
271         char *ret_str = NULL;
272
273         MSG_FILTER_S *filter_data = (MSG_FILTER_S *)filter;
274
275         switch (field)
276         {
277         case MSG_FILTER_VALUE_STR :
278                 ret_str = filter_data->filterValue;
279                 break;
280         default :
281                 return NULL;
282         }
283
284         return ret_str;
285 }
286
287 int msg_set_filter_info_bool(void *filter, int field, bool value)
288 {
289         if (!filter)
290                 return MSG_ERR_NULL_POINTER;
291
292         msg_error_t err =  MSG_SUCCESS;
293         MSG_FILTER_S *filter_data = (MSG_FILTER_S *)filter;
294
295         switch (field)
296         {
297         case MSG_FILTER_ACTIVE_BOOL :
298                 filter_data->bActive = value;
299                 break;
300         default :
301                 return MSG_ERR_INVALID_PARAMETER;
302         }
303
304         return err;
305 }
306
307 int msg_set_filter_info_int(void *filter, int field, int value)
308 {
309         if (!filter)
310                 return MSG_ERR_NULL_POINTER;
311
312         msg_error_t err =  MSG_SUCCESS;
313         MSG_FILTER_S *filter_data = (MSG_FILTER_S *)filter;
314
315         switch (field)
316         {
317         case MSG_FILTER_ID_INT :
318                 filter_data->filterId = value;
319                 break;
320         case MSG_FILTER_TYPE_INT :
321                 filter_data->filterType = value;
322                 break;
323         default :
324                 return MSG_ERR_INVALID_PARAMETER;
325         }
326
327         return err;
328 }
329
330 int msg_set_filter_info_str(void *filter, int field, char *value, int size)
331 {
332         if (!filter || !value)
333                 return MSG_ERR_NULL_POINTER;
334
335         msg_error_t err =  MSG_SUCCESS;
336         MSG_FILTER_S *filter_data = (MSG_FILTER_S *)filter;
337
338         switch (field)
339         {
340         case MSG_FILTER_VALUE_STR :
341         {
342                 int len = (size > MAX_FILTER_VALUE_LEN)?MAX_FILTER_VALUE_LEN:size;
343                 strncpy(filter_data->filterValue, value, len);
344                 break;
345         }
346         default :
347                 return MSG_ERR_INVALID_PARAMETER;
348         }
349
350         return err;
351 }
352
353