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