Merge "Export internal APIs needed by samsung-socil" into tizen_2.1
[framework/osp/social.git] / src / FScl_CalendarbookFilterImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 * @file         FScl_CalendarbookFilterImpl.cpp
18 * @brief        This is the implementation for _CalendarbookFilterImpl class.
19 *
20 * This file contains definitions of @e _CalendarbookFilterImpl class.
21 */
22
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseDateTime.h>
26 #include <FBase_StringConverter.h>
27 #include <FSclCalendarbookFilter.h>
28 #include "FScl_CalendarbookFilterImpl.h"
29 #include "FScl_CalendarbookUtil.h"
30
31 using namespace Tizen::Base;
32
33 namespace Tizen { namespace Social
34 {
35
36 static const long long _CALENDAR_TODO_NO_START_DATE = -0x7fffffffffffffffLL;
37 static const long long _CALENDAR_TODO_NO_DUE_DATE = 0x7fffffffffffffffLL;
38
39 // CB_FI_TYPE_CALENDAR
40 // The entries must be ordered same as CalendarFilterProperty
41 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__calendarFilterPropertyInfos[] =
42 {
43         {CALENDAR_FI_PR_CALENDAR_ID, _calendar_book.id, DATA_TYPE_INT},
44         {CALENDAR_FI_PR_ACCOUNT_ID, _calendar_book.account_id, DATA_TYPE_INT},
45         {CALENDAR_FI_PR_NAME, _calendar_book.name, DATA_TYPE_STRING},
46         {CALENDAR_FI_PR_ITEM_TYPE, _calendar_book.store_type, DATA_TYPE_INT},
47 };
48
49 // CB_FI_TYPE_EVENT
50 // The entries must be ordered same as EventFilterProperty
51 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__eventFilterPropertyInfos[] =
52 {
53         {EVENT_FI_PR_EVENT_ID, _calendar_event.id, DATA_TYPE_INT},
54         {EVENT_FI_PR_CALENDAR_ID, _calendar_event.calendar_book_id, DATA_TYPE_INT},
55         {EVENT_FI_PR_SUBJECT, _calendar_event.summary, DATA_TYPE_STRING},
56         {EVENT_FI_PR_DESCRIPTION, _calendar_event.description, DATA_TYPE_STRING},
57         {EVENT_FI_PR_LOCATION, _calendar_event.location, DATA_TYPE_STRING},
58         {EVENT_FI_PR_BUSY_STATUS, _calendar_event.busy_status, DATA_TYPE_INT},
59         {EVENT_FI_PR_PRIORITY, _calendar_event.priority, DATA_TYPE_INT},
60         {EVENT_FI_PR_SENSITIVITY, _calendar_event.sensitivity, DATA_TYPE_INT},
61         {EVENT_FI_PR_STATUS, _calendar_event.event_status, DATA_TYPE_INT},
62         {EVENT_FI_PR_UID, _calendar_event.uid, DATA_TYPE_STRING},
63         {EVENT_FI_PR_LATITUDE, _calendar_event.latitude, DATA_TYPE_DOUBLE},
64         {EVENT_FI_PR_LONGITUDE, _calendar_event.longitude, DATA_TYPE_DOUBLE},
65         {EVENT_FI_PR_LAST_REVISED_TIME, _calendar_event.last_modified_time, DATA_TYPE_DATE_TIME},
66         {EVENT_FI_PR_HAS_REMINDER, _calendar_event.has_alarm, DATA_TYPE_BOOL},
67         {EVENT_FI_PR_HAS_ATTENDEE, _calendar_event.has_attendee, DATA_TYPE_BOOL},
68 };
69
70 // CB_FI_TYPE_TODO
71 // The entries must be ordered same as TodoFilterProperty
72 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__todoFilterPropertyInfos[] =
73 {
74                 {TODO_FI_PR_TODO_ID, _calendar_todo.id, DATA_TYPE_INT},
75                 {TODO_FI_PR_CALENDAR_ID, _calendar_todo.calendar_book_id, DATA_TYPE_INT},
76                 {TODO_FI_PR_START_DATE, _calendar_todo.start_time, DATA_TYPE_DATE_TIME},
77                 {TODO_FI_PR_DUE_DATE, _calendar_todo.due_time, DATA_TYPE_DATE_TIME},
78                 {TODO_FI_PR_SUBJECT, _calendar_todo.summary, DATA_TYPE_STRING},
79                 {TODO_FI_PR_DESCRIPTION, _calendar_todo.description, DATA_TYPE_STRING},
80                 {TODO_FI_PR_LOCATION, _calendar_todo.location, DATA_TYPE_STRING},
81                 {TODO_FI_PR_PRIORITY, _calendar_todo.priority, DATA_TYPE_INT},
82                 {TODO_FI_PR_SENSITIVITY, _calendar_todo.sensitivity, DATA_TYPE_INT},
83                 {TODO_FI_PR_STATUS, _calendar_todo.todo_status, DATA_TYPE_INT},
84                 {TODO_FI_PR_LATITUDE, _calendar_todo.latitude, DATA_TYPE_DOUBLE},
85                 {TODO_FI_PR_LONGITUDE, _calendar_todo.longitude, DATA_TYPE_DOUBLE},
86                 {TODO_FI_PR_LAST_REVISED_TIME, _calendar_todo.last_modified_time, DATA_TYPE_DATE_TIME},
87                 {TODO_FI_PR_HAS_REMINDER, _calendar_todo.has_alarm, DATA_TYPE_BOOL},
88 };
89
90 // CB_FI_TYPE_ALL_DAY_EVENT_INSTANCE
91 // The entries must be ordered same as EventInstanceFilterProperty
92 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__allDayEventInstanceFilterPropertyInfos[] =
93 {
94                 {EVENT_INST_FI_PR_ORIGINAL_EVENT_ID, _calendar_instance_allday_calendar_book.event_id, DATA_TYPE_INT},
95                 {EVENT_INST_FI_PR_CALENDAR_ID, _calendar_instance_allday_calendar_book.calendar_book_id, DATA_TYPE_INT},
96                 {EVENT_INST_FI_PR_START_TIME, _calendar_instance_allday_calendar_book.start_time, DATA_TYPE_DATE_TIME},
97                 {EVENT_INST_FI_PR_END_TIME, _calendar_instance_allday_calendar_book.end_time, DATA_TYPE_DATE_TIME},
98                 {EVENT_INST_FI_PR_SUBJECT, _calendar_instance_allday_calendar_book.summary, DATA_TYPE_STRING},
99                 {EVENT_INST_FI_PR_DESCRIPTION, _calendar_instance_allday_calendar_book.description, DATA_TYPE_STRING},
100                 {EVENT_INST_FI_PR_LOCATION, _calendar_instance_allday_calendar_book.location, DATA_TYPE_STRING},
101                 {EVENT_INST_FI_PR_BUSY_STATUS, _calendar_instance_allday_calendar_book.busy_status, DATA_TYPE_INT},
102                 {EVENT_INST_FI_PR_PRIORITY, _calendar_instance_allday_calendar_book.priority, DATA_TYPE_INT},
103                 {EVENT_INST_FI_PR_SENSITIVITY, _calendar_instance_allday_calendar_book.sensitivity, DATA_TYPE_INT},
104                 {EVENT_INST_FI_PR_STATUS, _calendar_instance_allday_calendar_book.event_status, DATA_TYPE_INT},
105                 {EVENT_INST_FI_PR_LATITUDE, _calendar_instance_allday_calendar_book.latitude, DATA_TYPE_DOUBLE},
106                 {EVENT_INST_FI_PR_LONGITUDE, _calendar_instance_allday_calendar_book.longitude, DATA_TYPE_DOUBLE},
107                 {EVENT_INST_FI_PR_IS_RECURRING, _calendar_instance_allday_calendar_book.has_rrule, DATA_TYPE_BOOL},
108                 {EVENT_INST_FI_PR_HAS_REMINDER, _calendar_instance_allday_calendar_book.has_alarm, DATA_TYPE_BOOL},
109 };
110
111 // CB_FI_TYPE_NON_ALL_DAY_EVENT_INSTANCE
112 // The entries must be ordered same as EventInstanceFilterProperty
113 const _CalendarbookFilterImpl::__FilterPropertyInfoEntry _CalendarbookFilterImpl::__nonAllDayEventInstanceFilterPropertyInfos[] =
114 {
115                 {EVENT_INST_FI_PR_ORIGINAL_EVENT_ID, _calendar_instance_normal_calendar_book.event_id, DATA_TYPE_INT},
116                 {EVENT_INST_FI_PR_CALENDAR_ID, _calendar_instance_normal_calendar_book.calendar_book_id, DATA_TYPE_INT},
117                 {EVENT_INST_FI_PR_START_TIME, _calendar_instance_normal_calendar_book.start_time, DATA_TYPE_DATE_TIME},
118                 {EVENT_INST_FI_PR_END_TIME, _calendar_instance_normal_calendar_book.end_time, DATA_TYPE_DATE_TIME},
119                 {EVENT_INST_FI_PR_SUBJECT, _calendar_instance_normal_calendar_book.summary, DATA_TYPE_STRING},
120                 {EVENT_INST_FI_PR_DESCRIPTION, _calendar_instance_normal_calendar_book.description, DATA_TYPE_STRING},
121                 {EVENT_INST_FI_PR_LOCATION, _calendar_instance_normal_calendar_book.location, DATA_TYPE_STRING},
122                 {EVENT_INST_FI_PR_BUSY_STATUS, _calendar_instance_normal_calendar_book.busy_status, DATA_TYPE_INT},
123                 {EVENT_INST_FI_PR_PRIORITY, _calendar_instance_normal_calendar_book.priority, DATA_TYPE_INT},
124                 {EVENT_INST_FI_PR_SENSITIVITY, _calendar_instance_normal_calendar_book.sensitivity, DATA_TYPE_INT},
125                 {EVENT_INST_FI_PR_STATUS, _calendar_instance_normal_calendar_book.event_status, DATA_TYPE_INT},
126                 {EVENT_INST_FI_PR_LATITUDE, _calendar_instance_normal_calendar_book.latitude, DATA_TYPE_DOUBLE},
127                 {EVENT_INST_FI_PR_LONGITUDE, _calendar_instance_normal_calendar_book.longitude, DATA_TYPE_DOUBLE},
128                 {EVENT_INST_FI_PR_IS_RECURRING, _calendar_instance_normal_calendar_book.has_rrule, DATA_TYPE_BOOL},
129                 {EVENT_INST_FI_PR_HAS_REMINDER, _calendar_instance_normal_calendar_book.has_alarm, DATA_TYPE_BOOL},
130 };
131
132 const _CalendarbookFilterImpl::__FilterInfoEntry _CalendarbookFilterImpl::__filterInfos[] =
133 {
134         {CB_FI_TYPE_EVENT, _calendar_event._uri, EVENT_FI_PR_EVENT_ID, sizeof(__eventFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __eventFilterPropertyInfos},
135         {CB_FI_TYPE_TODO, _calendar_todo._uri, TODO_FI_PR_TODO_ID, sizeof(__todoFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __todoFilterPropertyInfos},
136         {CB_FI_TYPE_CALENDAR, _calendar_book._uri, CALENDAR_FI_PR_CALENDAR_ID, sizeof(__calendarFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __calendarFilterPropertyInfos},
137         {CB_FI_TYPE_ALL_DAY_EVENT_INSTANCE, _calendar_instance_allday_calendar_book._uri, EVENT_INST_FI_PR_ORIGINAL_EVENT_ID, sizeof(__allDayEventInstanceFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __allDayEventInstanceFilterPropertyInfos},
138         {CB_FI_TYPE_NON_ALL_DAY_EVENT_INSTANCE, _calendar_instance_normal_calendar_book._uri, EVENT_INST_FI_PR_ORIGINAL_EVENT_ID, sizeof(__nonAllDayEventInstanceFilterPropertyInfos)/sizeof(_CalendarbookFilterImpl::__FilterPropertyInfoEntry), __nonAllDayEventInstanceFilterPropertyInfos},
139 };
140
141 bool
142 _CalendarbookFilterImpl::IsValidProperty(CalendarbookFilterType filterType, unsigned long property)
143 {
144         int filterInfoCount = sizeof(__filterInfos)/sizeof(__FilterInfoEntry);
145
146         if (filterType < 0 || filterType > (filterInfoCount - 1))
147         {
148                 return false;
149         }
150
151         unsigned long baseIndex = __filterInfos[filterType].baseIndex;
152         int propertyInfoCount = __filterInfos[filterType].propertyInfoCount;
153
154         if (property < baseIndex || property >= (baseIndex + propertyInfoCount))
155         {
156                 return false;
157         }
158
159         return true;
160 }
161
162 // IsValidProperty must be called before calling this. If IsValidProperty returns false
163 // this should not be called.
164 const char*
165 _CalendarbookFilterImpl::GetUriFromType(CalendarbookFilterType filterType)
166 {
167         return __filterInfos[filterType].pViewUri;
168 }
169
170 // IsValidProperty must be called before calling this. If IsValidProperty returns false
171 // this should not be called.
172 unsigned int
173 _CalendarbookFilterImpl::GetViewPropertyId(CalendarbookFilterType filterType, unsigned long property)
174 {
175         const __FilterPropertyInfoEntry* propertyInfos = __filterInfos[filterType].propertyInfos;
176         unsigned long baseIndex = __filterInfos[filterType].baseIndex;
177
178         return propertyInfos[property - baseIndex].viewPropertyId;
179 }
180
181 // IsValidProperty must be called before calling this. If IsValidProperty returns__nonAllDayEventInstanceFilterPropertyInfos false
182 // this should not be called.
183 __DataType
184 _CalendarbookFilterImpl::GetPropertyType(CalendarbookFilterType filterType, unsigned long property)
185 {
186         const __FilterPropertyInfoEntry* propertyInfos = __filterInfos[filterType].propertyInfos;
187         unsigned long baseIndex = __filterInfos[filterType].baseIndex;
188
189         return propertyInfos[property - baseIndex].dataType;
190 }
191
192 _CalendarbookFilterImpl::_CalendarbookFilterImpl(CalendarbookFilterType type)
193         : __filterHandle(null)
194         , __filterType(type)
195         , __isEmpty(true)
196 {
197         calendar_filter_h filterHandle = null;
198         const char* pUri = GetUriFromType(type);
199
200         int ret = calendar_filter_create(pUri, &filterHandle);
201         SysTryReturnVoidResult(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(GetLastResult()));
202
203         __filterHandle = filterHandle;
204 }
205
206 _CalendarbookFilterImpl::~_CalendarbookFilterImpl(void)
207 {
208         if (__filterHandle != null)
209         {
210                 calendar_filter_destroy(__filterHandle);
211         }
212 }
213
214 bool
215 _CalendarbookFilterImpl::Equals(const Object& rhs) const
216 {
217         const _CalendarbookFilterImpl* pCalendarbookFilter = dynamic_cast<const _CalendarbookFilterImpl*>(&rhs);
218         if (this == pCalendarbookFilter)
219         {
220                 return true;
221         }
222
223         return false;
224 }
225
226 int
227 _CalendarbookFilterImpl::GetHashCode(void) const
228 {
229         return (int)__filterHandle;
230 }
231
232 calendar_match_int_flag_e
233 _CalendarbookFilterImpl::GetMatchIntFlag(FilterComparisonOperator comparisonOperator)
234 {
235         calendar_match_int_flag_e match = CALENDAR_MATCH_NONE;
236         switch(comparisonOperator)
237         {
238         case FI_CMP_OP_EQUAL:
239                 match = CALENDAR_MATCH_EQUAL;
240                 break;
241         case FI_CMP_OP_LESS_THAN:
242                 match = CALENDAR_MATCH_LESS_THAN;
243                 break;
244         case FI_CMP_OP_LESS_THAN_OR_EQUAL:
245                 match = CALENDAR_MATCH_LESS_THAN_OR_EQUAL;
246                 break;
247         case FI_CMP_OP_GREATER_THAN:
248                 match = CALENDAR_MATCH_GREATER_THAN;
249                 break;
250         case FI_CMP_OP_GREATER_THAN_OR_EQUAL:
251                 match = CALENDAR_MATCH_GREATER_THAN_OR_EQUAL;
252                 break;
253         case FI_CMP_OP_IS_NULL:
254                 match = CALENDAR_MATCH_NONE;
255                 break;
256         default:
257                 match = CALENDAR_MATCH_NONE;
258                 break;
259         };
260
261         return match;
262 }
263
264 calendar_match_str_flag_e
265 _CalendarbookFilterImpl::GetMatchStrFlag(FilterStringOperator stringOperstor)
266 {
267         calendar_match_str_flag_e match = CALENDAR_MATCH_EXISTS;
268         switch(stringOperstor)
269         {
270         case FI_STR_OP_EQUAL:
271                 match = CALENDAR_MATCH_EXACTLY;
272                 break;
273         case FI_STR_OP_FULL_STRING:
274                 match = CALENDAR_MATCH_FULLSTRING;
275                 break;
276         case FI_STR_OP_START_WITH:
277                 match = CALENDAR_MATCH_STARTSWITH;
278                 break;
279         case FI_STR_OP_END_WITH:
280                 match = CALENDAR_MATCH_ENDSWITH;
281                 break;
282         case FI_STR_OP_CONTAIN:
283                 match = CALENDAR_MATCH_CONTAINS;
284                 break;
285         case FI_STR_OP_IS_NOT_NULL:
286                 match = CALENDAR_MATCH_EXISTS;
287                 break;
288         default:
289                 match = CALENDAR_MATCH_EXISTS;
290                 break;
291         };
292
293         return match;
294 }
295
296 result
297 _CalendarbookFilterImpl::AppendInt(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, int value)
298 {
299         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
300         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_INT, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be int.", GetErrorMessage(E_INVALID_ARG), filterProperty);
301         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
302                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
303                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
304         SysTryReturn(NID_SCL, !(!__isEmpty && (conjunctiveOperator == FI_CONJ_OP_NONE)),\
305                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
306                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
307
308         unsigned int id = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
309         calendar_match_int_flag_e match = GetMatchIntFlag(comparisonOperator);
310
311         int convertedValue = 0;
312         switch (filterProperty)
313         {
314         case EVENT_FI_PR_BUSY_STATUS:
315                 convertedValue = _CalendarbookUtil::ConvertBusyStatusToCSEventBusyStatus(value);
316                 break;
317         case EVENT_FI_PR_PRIORITY:
318                 convertedValue = _CalendarbookUtil::ConvertEventPriorityToCSEventPriority(value);
319                 break;
320         case EVENT_FI_PR_SENSITIVITY:
321                 convertedValue = _CalendarbookUtil::ConvertSensitivityToCSSensitivity(value);
322                 break;
323         case EVENT_FI_PR_STATUS:
324                 convertedValue = _CalendarbookUtil::ConvertEventStatusToCSEventStatus(value);
325                 break;
326         case TODO_FI_PR_PRIORITY:
327                 convertedValue = _CalendarbookUtil::ConvertTodoPriorityToCSTodoPriority(value);
328                 break;
329         case TODO_FI_PR_SENSITIVITY:
330                 convertedValue = _CalendarbookUtil::ConvertSensitivityToCSSensitivity(value);
331                 break;
332         case TODO_FI_PR_STATUS:
333                 convertedValue = _CalendarbookUtil::ConvertTodoStatusToCSTodoStatus(value);
334                 break;
335         case CALENDAR_FI_PR_ITEM_TYPE:
336                 convertedValue = _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(value);
337                 break;
338         case EVENT_INST_FI_PR_BUSY_STATUS:
339                 convertedValue = _CalendarbookUtil::ConvertBusyStatusToCSEventBusyStatus(value);
340                 break;
341         case EVENT_INST_FI_PR_PRIORITY:
342                 convertedValue = _CalendarbookUtil::ConvertEventPriorityToCSEventPriority(value);
343                 break;
344         case EVENT_INST_FI_PR_SENSITIVITY:
345                 convertedValue = _CalendarbookUtil::ConvertSensitivityToCSSensitivity(value);
346                 break;
347         case EVENT_INST_FI_PR_STATUS:
348                 convertedValue = _CalendarbookUtil::ConvertEventStatusToCSEventStatus(value);
349                 break;
350         default:
351                 convertedValue = value;
352                 break;
353         }
354
355         if (!__isEmpty)
356         {
357                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
358
359                 calendar_filter_add_operator(__filterHandle, filterOperator);
360         }
361
362         int ret = calendar_filter_add_int(__filterHandle, id, match, convertedValue);
363         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
364
365         __isEmpty = false;
366
367         return E_SUCCESS;
368 }
369
370 result
371 _CalendarbookFilterImpl::AppendDouble(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, double value)
372 {
373         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
374         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_DOUBLE, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be double.", GetErrorMessage(E_INVALID_ARG), filterProperty);
375         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
376                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
377                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
378         SysTryReturn(NID_SCL, !(!__isEmpty && (conjunctiveOperator == FI_CONJ_OP_NONE)),\
379                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
380                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
381
382         unsigned int id = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
383         calendar_match_int_flag_e match = GetMatchIntFlag(comparisonOperator);
384
385         if (!__isEmpty)
386         {
387                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
388
389                 calendar_filter_add_operator(__filterHandle, filterOperator);
390         }
391
392         int ret = calendar_filter_add_double(__filterHandle, id, match, value);
393         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
394
395         __isEmpty = false;
396
397         return E_SUCCESS;
398 }
399
400 result
401 _CalendarbookFilterImpl::AppendBool(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, bool value)
402 {
403         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
404         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_BOOL, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be bool.", GetErrorMessage(E_INVALID_ARG), filterProperty);
405         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
406                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
407                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
408         SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
409                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
410                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
411         SysTryReturn(NID_SCL, comparisonOperator == FI_CMP_OP_EQUAL, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. comparisonOperator %d must be FI_CMP_OP_EQUAL.", GetErrorMessage(E_INVALID_ARG), comparisonOperator);
412
413         unsigned int viewPropertyId = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
414
415         if (!__isEmpty)
416         {
417                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
418
419                 calendar_filter_add_operator(__filterHandle, filterOperator);
420         }
421
422         int ret = calendar_filter_add_int(__filterHandle, viewPropertyId, CALENDAR_MATCH_EQUAL, value);
423         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
424
425         __isEmpty = false;
426
427         return E_SUCCESS;
428 }
429
430 result
431 _CalendarbookFilterImpl::AppendDateTime(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, const DateTime& value)
432 {
433         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
434         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_DATE_TIME, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be DateTime.", GetErrorMessage(E_INVALID_ARG), filterProperty);
435         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
436                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
437                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
438         SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
439                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
440                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
441         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(value), null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. The value is not in a valid range.");
442
443         unsigned int id = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
444         calendar_match_int_flag_e match = GetMatchIntFlag(comparisonOperator);
445
446         if (!__isEmpty)
447         {
448                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
449
450                 calendar_filter_add_operator(__filterHandle, filterOperator);
451         }
452
453
454         if (filterProperty == EVENT_FI_PR_LAST_REVISED_TIME || filterProperty == TODO_FI_PR_LAST_REVISED_TIME)
455         {
456                 int ret = calendar_filter_add_lli(__filterHandle, id, match, _CalendarbookUtil::ConvertDateTimeToEpochTime(value));
457                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
458         }
459         else if (filterProperty == TODO_FI_PR_START_DATE)
460         {
461                 calendar_time_s convertedTime;
462
463                 if (comparisonOperator == FI_CMP_OP_IS_NULL)
464                 {
465                         convertedTime.type = CALENDAR_TIME_UTIME;
466                         convertedTime.time.utime = _CALENDAR_TODO_NO_START_DATE;
467                         match = CALENDAR_MATCH_EQUAL;
468                 }
469                 else
470                 {
471                         _CalendarbookUtil::ConvertDateTimeToCalTime(value, convertedTime);
472                 }
473
474                 int ret = calendar_filter_add_caltime(__filterHandle, id, match, convertedTime);
475                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
476         }
477         else if (filterProperty == TODO_FI_PR_DUE_DATE)
478         {
479                 calendar_time_s convertedTime;
480
481                 if (comparisonOperator == FI_CMP_OP_IS_NULL)
482                 {
483                         convertedTime.type = CALENDAR_TIME_UTIME;
484                         convertedTime.time.utime = _CALENDAR_TODO_NO_DUE_DATE;
485                         match = CALENDAR_MATCH_EQUAL;
486                 }
487                 else
488                 {
489                         _CalendarbookUtil::ConvertDateTimeToCalTime(value, convertedTime);
490                 }
491
492                 int ret = calendar_filter_add_caltime(__filterHandle, id, match, convertedTime);
493                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
494         }
495         else if ((filterProperty == EVENT_INST_FI_PR_START_TIME || filterProperty == EVENT_INST_FI_PR_END_TIME) && __filterType == CB_FI_TYPE_NON_ALL_DAY_EVENT_INSTANCE)
496         {
497                 calendar_time_s convertedTime;
498                 _CalendarbookUtil::ConvertDateTimeToCalTime(value, convertedTime);
499
500                 int ret = calendar_filter_add_caltime(__filterHandle, id, match, convertedTime);
501                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
502         }
503         else if ((filterProperty == EVENT_INST_FI_PR_START_TIME || filterProperty == EVENT_INST_FI_PR_END_TIME) && (__filterType == CB_FI_TYPE_ALL_DAY_EVENT_INSTANCE))
504         {
505                 calendar_time_s convertedTime;
506                 _CalendarbookUtil::ConvertDateToCalTime(value, convertedTime);
507
508                 int ret = calendar_filter_add_caltime(__filterHandle, id, match, convertedTime);
509                 SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
510         }
511         else
512         {
513                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be DateTime.", GetErrorMessage(E_INVALID_ARG), filterProperty);
514                 return E_INVALID_ARG;
515         }
516
517         __isEmpty = false;
518
519         return E_SUCCESS;
520 }
521
522 result
523 _CalendarbookFilterImpl::AppendString(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterStringOperator comparisonOperator, const String& value)
524 {
525         SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. filterProperty = %ld", GetErrorMessage(E_INVALID_ARG), filterProperty);
526         SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_STRING, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be String.", GetErrorMessage(E_INVALID_ARG), filterProperty);
527         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND  || conjunctiveOperator == FI_CONJ_OP_OR)),\
528                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
529                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
530         SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
531                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
532                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
533
534         unsigned int viewPropertyId = _CalendarbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
535
536         calendar_match_str_flag_e match = GetMatchStrFlag(comparisonOperator);
537         std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(value));
538         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
539
540         if (!__isEmpty)
541         {
542                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
543
544                 calendar_filter_add_operator(__filterHandle, filterOperator);
545         }
546
547         int ret = calendar_filter_add_str(__filterHandle, viewPropertyId, match, pCharArray.get());
548         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
549
550         __isEmpty = false;
551
552         return E_SUCCESS;
553 }
554
555 result
556 _CalendarbookFilterImpl::AppendFilter(FilterConjunctiveOperator conjunctiveOperator, const _CalendarbookFilterImpl& filter)
557 {
558         SysTryReturn(NID_SCL, !filter.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified filter should not be empty.", GetErrorMessage(E_INVALID_ARG));
559         SysTryReturn(NID_SCL, __filterType == filter.__filterType, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter must be same with the type of this filter.", GetErrorMessage(E_INVALID_ARG));
560         SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
561                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
562                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
563         SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
564                         E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
565                         GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
566
567         if (!__isEmpty)
568         {
569                 calendar_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CALENDAR_FILTER_OPERATOR_AND : CALENDAR_FILTER_OPERATOR_OR;
570                 calendar_filter_add_operator(__filterHandle, filterOperator);
571         }
572
573         int ret = calendar_filter_add_filter(__filterHandle, filter.__filterHandle);
574         SysTryReturn(NID_SCL, ret == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
575
576         __isEmpty = false;
577
578         return E_SUCCESS;
579 }
580
581 calendar_filter_h
582 _CalendarbookFilterImpl::GetFilterHandle(void) const
583 {
584         if (!__isEmpty)
585         {
586                 return __filterHandle;
587         }
588
589         return null;
590 }
591
592 CalendarbookFilterType
593 _CalendarbookFilterImpl::GetType(void) const
594 {
595         return __filterType;
596 }
597
598 bool
599 _CalendarbookFilterImpl::IsEmpty(void) const
600 {
601         return __isEmpty;
602 }
603
604 const _CalendarbookFilterImpl*
605 _CalendarbookFilterImpl::GetInstance(const CalendarbookFilter& filter)
606 {
607         return filter.__pCalendarbookFilterImpl;
608 }
609
610 _CalendarbookFilterImpl*
611 _CalendarbookFilterImpl::GetInstance(CalendarbookFilter& filter)
612 {
613         return filter.__pCalendarbookFilterImpl;
614 }
615
616 }} // Tizen::Social