Modify the spec file for secure log
[framework/osp/social.git] / src / FScl_CalendarbookImpl.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_CalendarbookImpl.cpp
18  * @brief               This is the implementation for _CalendarbookImpl class.
19  *
20  * This file contains definitions of @e _CalendarbookImpl class.
21  */
22
23 #include <new>
24 #include <unique_ptr.h>
25 #include <FBaseColArrayList.h>
26 #include <FBaseColHashMapT.h>
27 #include <FBaseDateTime.h>
28 #include <FBaseSysLog.h>
29 #include <FBase_StringConverter.h>
30 #include <FBaseUtilStringUtil.h>
31 #include <FIoFile.h>
32 #include <FLclTimeZone.h>
33 #include <FSclCalendar.h>
34 #include <FSclCalendarbook.h>
35 #include <FSclCalEvent.h>
36 #include <FSclCalEventChangeInfo.h>
37 #include <FSclCalEventInstance.h>
38 #include <FSclCalTodo.h>
39 #include <FSclCalTodoChangeInfo.h>
40 #include <FSclICalendarbookEventListener.h>
41 #include <FSclIRecordEventListener.h>
42 #include <FSclIRecordListener.h>
43 #include "FScl_CalendarbookImpl.h"
44 #include "FScl_CalendarbookFilterImpl.h"
45 #include "FScl_CalendarbookDbConnector.h"
46 #include "FScl_CalendarbookDbMonitor.h"
47 #include "FScl_CalendarbookRecordRetrivalEvent.h"
48 #include "FScl_CalendarbookRecordRetrivalThread.h"
49 #include "FScl_CalendarbookUtil.h"
50 #include "FScl_CalEventChangeInfoImpl.h"
51 #include "FScl_CalendarImpl.h"
52 #include "FScl_CalEventImpl.h"
53 #include "FScl_CalEventInstanceImpl.h"
54 #include "FScl_CalTodoChangeInfoImpl.h"
55 #include "FScl_CalTodoImpl.h"
56 #include "FScl_RecordImpl.h"
57
58 using namespace Tizen::Base;
59 using namespace Tizen::Base::Collection;
60 using namespace Tizen::Base::Utility;
61 using namespace Tizen::Io;
62
63 namespace Tizen { namespace Social
64 {
65
66 RequestId _CalendarbookImpl::__requestId = 0;
67 _CalendarbookRecordRetrivalThread* _CalendarbookImpl::__pRecordRetrivalThread = null;
68
69 static const int _DEFAULT_EVENT_CALENDAR_DB_ID = 1;
70 static const int _DEFAULT_TODO_CALENDAR_DB_ID = 2;
71 static const wchar_t* _RECURRENCE_DELIMITER = L",";
72 static const int _NUMBER_OF_TODO_STATUS = 5;
73 static const int _NUMBER_OF_TODO_PRIORITY = 3;
74 static const int _NUMBER_OF_EVENT_CATEGORY_MAP_PROJECTION = 2;
75
76 static const int _INVALID_CALENDARBOOK_DB_ID = -1;      // TODO : This value should be checked.
77 static const int _INVALID_EVENT_DB_ID = -1;
78
79 class _CalendarList
80 {
81 public :
82         _CalendarList(calendar_list_h calendarListHandle, bool deleteRecord = true)
83                 : __calendarListHandle(calendarListHandle)
84                 , __deleteRecord(deleteRecord)
85         {
86         }
87
88         ~_CalendarList(void)
89         {
90                 if (__calendarListHandle)
91                 {
92                         calendar_list_destroy(__calendarListHandle, __deleteRecord);
93                 }
94         }
95
96         calendar_list_h GetHandle(void)
97         {
98                 return __calendarListHandle;
99         }
100
101         calendar_list_h ReleaseHandle(void)
102         {
103                 calendar_list_h calendarListHandle = __calendarListHandle;
104                 __calendarListHandle = null;
105                 return calendarListHandle;
106         }
107
108 private :
109         calendar_list_h __calendarListHandle;
110         bool __deleteRecord;
111 };
112
113 class _CalendarFilter
114 {
115 public :
116         _CalendarFilter(calendar_filter_h calendarFilterHandle)
117                 : __calendarFilterHandle(calendarFilterHandle)
118         {
119         }
120
121         ~_CalendarFilter(void)
122         {
123                 if (__calendarFilterHandle)
124                 {
125                         calendar_filter_destroy(__calendarFilterHandle);
126                 }
127         }
128
129         calendar_filter_h GetHandle(void)
130         {
131                 return __calendarFilterHandle;
132         }
133
134         calendar_filter_h ReleaseHandle(void)
135         {
136                 calendar_filter_h calendarFilterHandle = __calendarFilterHandle;
137                 __calendarFilterHandle = null;
138                 return calendarFilterHandle;
139         }
140
141         void ResetHandle(calendar_filter_h calendarFilterHandle)
142         {
143                 if (__calendarFilterHandle)
144                 {
145                         calendar_filter_destroy(__calendarFilterHandle);
146                 }
147                 __calendarFilterHandle = calendarFilterHandle;
148         }
149
150 private :
151         calendar_filter_h __calendarFilterHandle;
152 };
153
154 class _CalendarConnector
155 {
156 public :
157         _CalendarConnector()
158                 : __r(E_SUCCESS)
159         {
160                 __r = _CalendarbookDbConnector::Connect();
161         }
162
163         ~_CalendarConnector(void)
164         {
165                 if (__r == E_SUCCESS)
166                 {
167                         _CalendarbookDbConnector::Disconnect();
168                 }
169         }
170
171         result GetResult(void)
172         {
173                 return __r;
174         }
175
176 private :
177         result __r;
178 };
179
180 class _CalendarQuery
181 {
182 public :
183         _CalendarQuery(calendar_query_h calendarQueryHandle)
184                 : __calendarQueryHandle(calendarQueryHandle)
185         {
186         }
187
188         ~_CalendarQuery(void)
189         {
190                 if (__calendarQueryHandle)
191                 {
192                         calendar_query_destroy(__calendarQueryHandle);
193                 }
194         }
195
196         calendar_query_h GetHandle(void)
197         {
198                 return __calendarQueryHandle;
199         }
200
201         calendar_query_h ReleaseHandle(void)
202         {
203                 calendar_query_h calendarQueryHandle = __calendarQueryHandle;
204                 __calendarQueryHandle = null;
205                 return calendarQueryHandle;
206         }
207
208         void ResetHandle(calendar_query_h calendarQueryHandle)
209         {
210                 if (__calendarQueryHandle)
211                 {
212                         calendar_query_destroy(__calendarQueryHandle);
213                 }
214                 __calendarQueryHandle = calendarQueryHandle;
215         }
216
217 private :
218         calendar_query_h __calendarQueryHandle;
219 };
220
221 _CalendarbookImpl::_CalendarbookImpl(void)
222         : __pIRecordEventListener(null)
223         , __pICalendarbookEventListener(null)
224         , __dbVersionForEvent(0)
225         , __dbVersionForTodo(0)
226         , __pCalendarbookDbMonitor(null)
227 {
228 }
229
230 _CalendarbookImpl::~_CalendarbookImpl(void)
231 {
232         if (__pCalendarbookDbMonitor != null)
233         {
234                 if (__pIRecordEventListener != null || __pICalendarbookEventListener != null)
235                 {
236                         __pCalendarbookDbMonitor->RemoveListener(*this);
237                 }
238
239                 __pCalendarbookDbMonitor->Release();
240         }
241         else
242         {
243                 _CalendarbookDbConnector::Disconnect();
244         }
245 }
246
247 result
248 _CalendarbookImpl::Construct(void)
249 {
250         result r = _CalendarbookDbConnector::Connect();
251         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
252
253         return E_SUCCESS;
254 }
255
256 result
257 _CalendarbookImpl::Construct(IRecordEventListener* pListener)
258 {
259         result r = E_SUCCESS;
260         int dbVersion = -1;
261
262         ClearLastResult();
263
264         _CalendarbookDbMonitor* pCalendarbookDbMonitor = _CalendarbookDbMonitor::GetInstance();
265         SysTryReturnResult(NID_SCL, pCalendarbookDbMonitor != null, E_SYSTEM, "A system error has been occurred.");
266
267         if (pListener != null)
268         {
269                 dbVersion = GetLatestVersion();
270                 SysTryCatch(NID_SCL, dbVersion != -1, r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
271
272                 r = pCalendarbookDbMonitor->AddListener(*this);
273                 SysTryCatch(NID_SCL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
274
275                 __pIRecordEventListener = pListener;
276         }
277
278         __pCalendarbookDbMonitor = pCalendarbookDbMonitor;
279
280         __dbVersionForEvent = dbVersion;
281         __dbVersionForTodo = dbVersion;
282
283         return E_SUCCESS;
284
285 CATCH:
286         pCalendarbookDbMonitor->Release();
287
288         return r;
289 }
290
291 result
292 _CalendarbookImpl::Construct(ICalendarbookEventListener& listener)
293 {
294         ClearLastResult();
295
296         _CalendarbookDbMonitor* pCalendarbookDbMonitor = _CalendarbookDbMonitor::GetInstance();
297         SysTryReturnResult(NID_SCL, pCalendarbookDbMonitor != null, E_SYSTEM, "A system error has been occurred.");
298
299         result r = E_SUCCESS;
300
301         int dbVersion = GetLatestVersion();
302         SysTryCatch(NID_SCL, dbVersion != -1, r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
303
304         r = pCalendarbookDbMonitor->AddListener(*this);
305         SysTryCatch(NID_SCL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
306
307         __pICalendarbookEventListener = &listener;
308         __pCalendarbookDbMonitor = pCalendarbookDbMonitor;
309
310         __dbVersionForEvent = dbVersion;
311         __dbVersionForTodo = dbVersion;
312
313         return E_SUCCESS;
314
315 CATCH:
316         pCalendarbookDbMonitor->Release();
317
318         return r;
319 }
320
321 result
322 _CalendarbookImpl::AddEvent(CalEvent& event)
323 {
324         SysTryReturnResult(NID_SCL, event.GetRecordId() == INVALID_RECORD_ID, E_INVALID_ARG
325                         , "Invalid argument is used. The specified recordId is not INVALID_RECORD_ID.");
326         SysTryReturnResult(NID_SCL, !event.IsInstance(), E_INVALID_ARG
327                         , "Invalid argument is used. The event is not an entry type instance.");
328
329         calendar_record_h eventHandle = _CalEventImpl::GetInstance(event)->GetRecordHandle();
330
331         int errorCode  = calendar_record_set_int(eventHandle, _calendar_event.calendar_book_id, _DEFAULT_EVENT_CALENDAR_DB_ID);
332         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
333
334         int dbIndex = -1;
335         errorCode = calendar_db_insert_record(eventHandle, &dbIndex);
336         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, "The storage is insufficient.");
337         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
338         _RecordImpl::GetInstance(event)->SetRecordId(dbIndex);
339
340         calendar_record_h tmpEventHandle = null;
341         errorCode = calendar_db_get_record(_calendar_event._uri, dbIndex, &tmpEventHandle);
342         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
343         _CalEventImpl::GetInstance(event)->SetRecordHandle(tmpEventHandle);
344
345         return E_SUCCESS;
346 }
347
348 result
349 _CalendarbookImpl::AddEvent(CalEvent& event, RecordId calendarId)
350 {
351         SysTryReturnResult(NID_SCL, event.GetRecordId() == INVALID_RECORD_ID, E_INVALID_ARG
352                         , "Invalid argument is used. The specified recordId is not INVALID_RECORD_ID.");
353         SysTryReturnResult(NID_SCL, !event.IsInstance(), E_INVALID_ARG
354                         , "Invalid argument is used. The event is not an entry type instance.");
355         SysTryReturnResult(NID_SCL, calendarId != INVALID_RECORD_ID, E_INVALID_ARG
356                         , "Invalid argument is used. calendarId = %d", calendarId);
357         CalendarItemType itemType = GetCalendarItemTypeByCalendarId(calendarId);
358         SysTryReturnResult(NID_SCL, GetLastResult() == E_SUCCESS, E_OBJ_NOT_FOUND, "The specified calendarId is not found.");
359         SysTryReturnResult(NID_SCL,(itemType == CALENDAR_ITEM_TYPE_EVENT_ONLY || itemType == CALENDAR_ITEM_TYPE_EVENT_AND_TODO)
360                         , E_INVALID_ARG, "Invalid argument is used. The calendar is created for CALENDAR_ITEM_TYPE_TODO_ONLY");
361
362         calendar_record_h eventHandle = _CalEventImpl::GetInstance(event)->GetRecordHandle();
363
364         int errorCode = calendar_record_set_int(eventHandle, _calendar_event.calendar_book_id, calendarId);
365         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
366
367         int dbIndex = -1;
368         errorCode = calendar_db_insert_record(eventHandle, &dbIndex);
369         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, "The storage is insufficient.");
370         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
371         _RecordImpl::GetInstance(event)->SetRecordId(dbIndex);
372
373         calendar_record_h tmpEventHandle = null;
374         errorCode = calendar_db_get_record(_calendar_event._uri, dbIndex, &tmpEventHandle);
375         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
376         _CalEventImpl::GetInstance(event)->SetRecordHandle(tmpEventHandle);
377
378         return E_SUCCESS;
379 }
380
381 result
382 _CalendarbookImpl::AddTodo(CalTodo& todo)
383 {
384         SysTryReturnResult(NID_SCL, todo.GetRecordId() == INVALID_RECORD_ID, E_INVALID_ARG
385                         , "Invalid argument is used. The specified recordId is not INVALID_RECORD_ID.");
386
387         calendar_record_h todoHandle = _CalTodoImpl::GetInstance(todo)->GetRecordHandle();
388
389         int errorCode = calendar_record_set_int(todoHandle, _calendar_todo.calendar_book_id, _DEFAULT_TODO_CALENDAR_DB_ID);
390         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
391
392         int dbIndex = -1;
393         errorCode = calendar_db_insert_record(todoHandle, &dbIndex);
394         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, "The storage is insufficient.");
395         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
396         _RecordImpl::GetInstance(todo)->SetRecordId(dbIndex);
397
398         calendar_record_h tmpTodoHandle = null;
399         errorCode = calendar_db_get_record(_calendar_todo._uri, dbIndex, &tmpTodoHandle);
400         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
401         _CalTodoImpl::GetInstance(todo)->SetRecordHandle(tmpTodoHandle);
402
403         return E_SUCCESS;
404 }
405
406 result
407 _CalendarbookImpl::AddTodo(CalTodo& todo, RecordId calendarId)
408 {
409         SysTryReturnResult(NID_SCL, todo.GetRecordId() == INVALID_RECORD_ID, E_INVALID_ARG
410                         , "Invalid argument is used. The specified recordId is not INVALID_RECORD_ID.");
411         SysTryReturnResult(NID_SCL, calendarId != INVALID_RECORD_ID, E_INVALID_ARG
412                         , "Invalid argument is used. calendarId = %d", calendarId);
413         CalendarItemType itemType = GetCalendarItemTypeByCalendarId(calendarId);
414         SysTryReturnResult(NID_SCL, GetLastResult() == E_SUCCESS, E_OBJ_NOT_FOUND, "The specified calendarId is not found.");
415         SysTryReturnResult(NID_SCL, (itemType == CALENDAR_ITEM_TYPE_TODO_ONLY || itemType == CALENDAR_ITEM_TYPE_EVENT_AND_TODO)
416                         , E_INVALID_ARG, "Invalid argument is used. The calendar is created for CALENDAR_ITEM_TYPE_EVENT_ONLY");
417
418         calendar_record_h todoHandle = _CalTodoImpl::GetInstance(todo)->GetRecordHandle();
419
420         int errorCode = calendar_record_set_int(todoHandle, _calendar_todo.calendar_book_id, calendarId);
421
422         int dbIndex = -1;
423         errorCode = calendar_db_insert_record(todoHandle, &dbIndex);
424         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, "The storage is insufficient.");
425         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
426         _RecordImpl::GetInstance(todo)->SetRecordId(dbIndex);
427
428         calendar_record_h tmpTodoHandle = null;
429         errorCode = calendar_db_get_record(_calendar_todo._uri, dbIndex, &tmpTodoHandle);
430         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
431         _CalTodoImpl::GetInstance(todo)->SetRecordHandle(tmpTodoHandle);
432
433         return E_SUCCESS;
434 }
435
436 result
437 _CalendarbookImpl::AddCalendar(Calendar& calendar)
438 {
439         SysTryReturnResult(NID_SCL, calendar.GetRecordId() == INVALID_RECORD_ID, E_INVALID_ARG
440                         , "Invalid argument is used. The specified recordId is not INVALID_RECORD_ID.");
441
442         calendar_record_h calendarHandle = _CalendarImpl::GetInstance(calendar)->GetRecordHandle();
443
444         int dbIndex = -1;
445         int errorCode = calendar_db_insert_record(calendarHandle, &dbIndex);
446         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, "The storage is insufficient.");
447         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
448         _RecordImpl::GetInstance(calendar)->SetRecordId(dbIndex);
449
450         calendar_record_h tmpCalendarHandle = null;
451         errorCode = calendar_db_get_record(_calendar_book._uri, dbIndex, &tmpCalendarHandle);
452         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
453         _CalendarImpl::GetInstance(calendar)->SetRecordHandle(tmpCalendarHandle);
454
455         return E_SUCCESS;
456 }
457
458 result
459 _CalendarbookImpl::AddCalendar(Calendar& calendar, AccountId accountId)
460 {
461         SysTryReturnResult(NID_SCL, calendar.GetRecordId() == INVALID_RECORD_ID, E_INVALID_ARG
462                         , "Invalid argument is used. The specified recordId is not INVALID_RECORD_ID.");
463         SysTryReturnResult(NID_SCL, accountId >= 0, E_INVALID_ARG, "Invalid argument is used.");
464
465         calendar_record_h calendarHandle = _CalendarImpl::GetInstance(calendar)->GetRecordHandle();
466
467         int errorCode = calendar_record_set_int(calendarHandle, _calendar_book.account_id, accountId);
468         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
469
470         int dbIndex = -1;
471         errorCode = calendar_db_insert_record(calendarHandle, &dbIndex);
472         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, "The storage is insufficient.");
473         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
474         _RecordImpl::GetInstance(calendar)->SetRecordId(dbIndex);
475
476         calendar_record_h tmpCalendarHandle = null;
477         errorCode = calendar_db_get_record(_calendar_book._uri, dbIndex, &tmpCalendarHandle);
478         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
479         _CalendarImpl::GetInstance(calendar)->SetRecordHandle(tmpCalendarHandle);
480
481         return E_SUCCESS;
482 }
483
484 result
485 _CalendarbookImpl::RemoveEvent(CalEvent& event)
486 {
487         SysTryReturnResult(NID_SCL, event.GetRecordId() != INVALID_RECORD_ID, E_INVALID_ARG
488                         , "Invalid argument is used. The specified eventId is INVALID_RECORD_ID.");
489
490         calendar_record_h eventHandle = _CalEventImpl::GetInstance(event)->GetRecordHandle();
491
492         int baseEventId = _INVALID_EVENT_DB_ID;
493         int errorCode = calendar_record_get_int(eventHandle, _calendar_event.original_event_id, &baseEventId);
494         SysTryReturnResult(NID_SCL, baseEventId == _INVALID_EVENT_DB_ID, E_INVALID_OPERATION
495                         , "Invalid argument is used. The event has the base event id.");
496
497         errorCode = calendar_db_delete_record(_calendar_event._uri, event.GetRecordId());
498         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_DB_RECORD_NOT_FOUND, E_OBJ_NOT_FOUND, "The specified record is not found.");
499         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
500
501         _RecordImpl* pRecordImpl = _RecordImpl::GetInstance(event);
502         pRecordImpl->SetRecordId(INVALID_RECORD_ID);
503
504         return E_SUCCESS;
505 }
506
507 result
508 _CalendarbookImpl::RemoveEvent(RecordId eventId)
509 {
510         SysTryReturnResult(NID_SCL, eventId != INVALID_RECORD_ID, E_INVALID_ARG
511                         , "Invalid argument is used. The specified eventId is INVALID_RECORD_ID.");
512
513         calendar_record_h eventHandle = null;
514
515         int errorCode = calendar_db_get_record(_calendar_event._uri, eventId, &eventHandle);
516         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OBJ_NOT_FOUND, "The specified record is not found.");
517
518         int baseEventId = _INVALID_EVENT_DB_ID;
519         calendar_record_get_int(eventHandle, _calendar_event.original_event_id, &baseEventId);
520
521         calendar_record_destroy(eventHandle, true);
522
523         SysTryReturnResult(NID_SCL, baseEventId == _INVALID_EVENT_DB_ID, E_INVALID_OPERATION
524                         , "Invalid argument is used. The event of eventId has the base event id.");
525
526         errorCode = calendar_db_delete_record(_calendar_event._uri, eventId);
527         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
528
529         return E_SUCCESS;
530 }
531
532 result
533 _CalendarbookImpl::RemoveTodo(CalTodo& todo)
534 {
535         _RecordImpl* pRecordImpl = _RecordImpl::GetInstance(todo);
536
537         result r = RemoveTodo(todo.GetRecordId());
538         SysTryReturnResult(NID_SCL, !IsFailed(r), r, "Failed to remove the todo.");
539         pRecordImpl->SetRecordId(INVALID_RECORD_ID);
540
541         return r;
542 }
543
544 result
545 _CalendarbookImpl::RemoveTodo(RecordId todoId)
546 {
547         SysTryReturnResult(NID_SCL, todoId != INVALID_RECORD_ID, E_INVALID_ARG
548                         , "Invalid argument is used. The specified todoId is INVALID_RECORD_ID.");
549
550         int errorCode = calendar_db_delete_record(_calendar_todo._uri, todoId);
551         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_DB_RECORD_NOT_FOUND, E_OBJ_NOT_FOUND, "The specified record is not found.");
552         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
553
554         return E_SUCCESS;
555 }
556
557 result
558 _CalendarbookImpl::RemoveCalendar(RecordId calendarId)
559 {
560         SysTryReturnResult(NID_SCL, calendarId != INVALID_RECORD_ID, E_INVALID_ARG
561                         , "The specified calendarId is INVALID_RECORD_ID.");
562         SysTryReturnResult(NID_SCL, calendarId != _DEFAULT_EVENT_CALENDAR_DB_ID
563                         , E_INVALID_ARG, "Invalid argument is used. The calendar represents default calendar.");
564         SysTryReturnResult(NID_SCL, calendarId != _DEFAULT_TODO_CALENDAR_DB_ID
565                         , E_INVALID_ARG, "Invalid argument is used. The calendar represents default calendar.");
566         SysTryReturnResult(NID_SCL, CheckCalendarExistance(calendarId), E_OBJ_NOT_FOUND, "The specified record is not found.");
567
568         int errorCode = calendar_db_delete_record(_calendar_book._uri, calendarId);
569         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
570
571         return E_SUCCESS;
572 }
573
574 result
575 _CalendarbookImpl::UpdateEvent(const CalEvent& event)
576 {
577         SysTryReturnResult(NID_SCL, event.GetRecordId() != INVALID_RECORD_ID, E_INVALID_ARG
578                         , "Invalid argument is used. The specified recordId is INVALID_RECORD_ID.");
579         SysTryReturnResult(NID_SCL, !event.IsInstance(), E_INVALID_ARG
580                         , "Invalid argument is used. The event is not an entry type instance.");
581
582         calendar_record_h eventHandle = _CalEventImpl::GetInstance(event)->GetRecordHandle();
583
584         int baseEventId = _INVALID_EVENT_DB_ID;
585         int errorCode = calendar_record_get_int(eventHandle, _calendar_event.original_event_id, &baseEventId);
586         SysTryReturnResult(NID_SCL, baseEventId == _INVALID_EVENT_DB_ID, E_INVALID_OPERATION
587                         , "Invalid argument is used. The event has the base event id.");
588
589         SysTryReturnResult(NID_SCL, CheckEventExistance(event.GetRecordId()), E_OBJ_NOT_FOUND, "The specified record is not found.");
590
591         errorCode = calendar_db_update_record(eventHandle);
592         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, E_SYSTEM, "A system error has been occurred.");
593         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OBJ_NOT_FOUND, "The specified record is not found.");
594
595         return E_SUCCESS;
596 }
597
598 result
599 _CalendarbookImpl::UpdateTodo(const CalTodo& todo)
600 {
601         SysTryReturnResult(NID_SCL, todo.GetRecordId() != INVALID_RECORD_ID, E_INVALID_ARG
602                         , "Invalid argument is used. The specified recordId is INVALID_RECORD_ID.");
603         SysTryReturnResult(NID_SCL, CheckTodoExistance(todo.GetRecordId()), E_OBJ_NOT_FOUND, "The specified record is not found.");
604
605         calendar_record_h todoHandle = _CalTodoImpl::GetInstance(todo)->GetRecordHandle();
606         int errorCode = calendar_db_update_record(todoHandle);
607         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
608
609         return E_SUCCESS;
610 }
611
612 result
613 _CalendarbookImpl::UpdateCalendar(const Calendar& calendar)
614 {
615         SysTryReturnResult(NID_SCL, calendar.GetRecordId() != INVALID_RECORD_ID, E_INVALID_ARG
616                         , "The specified recordId is INVALID_RECORD_ID.");
617         SysTryReturnResult(NID_SCL, CheckCalendarExistance(calendar.GetRecordId()), E_OBJ_NOT_FOUND, "The specified record is not found.");
618
619         calendar_record_h calendarHandle = _CalendarImpl::GetInstance(calendar)->GetRecordHandle();
620         int errorCode = calendar_db_update_record(calendarHandle);
621         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
622
623         return E_SUCCESS;
624 }
625
626 CalEvent*
627 _CalendarbookImpl::GetEventN(RecordId eventId) const
628 {
629         ClearLastResult();
630
631         SysTryReturn(NID_SCL, eventId != INVALID_RECORD_ID, null, E_INVALID_ARG
632                         , "[%s] Invalid argument is used. The specified recordId is INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG));
633
634         std::unique_ptr<CalEvent> pEvent(new (std::nothrow) CalEvent());
635         SysTryReturn(NID_SCL, pEvent != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
636
637         calendar_record_h eventHandle = null;
638         int errorCode = calendar_db_get_record(_calendar_event._uri, eventId, &eventHandle);
639         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
640         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OBJ_NOT_FOUND, "[%s] The specified record is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
641
642         _CalEventImpl::GetInstance(*pEvent.get())->SetRecordHandle(eventHandle);
643         _RecordImpl::GetInstance(*pEvent.get())->SetRecordId(eventId);
644
645         return pEvent.release();
646 }
647
648 CalTodo*
649 _CalendarbookImpl::GetTodoN(RecordId todoId) const
650 {
651         ClearLastResult();
652
653         SysTryReturn(NID_SCL, todoId != INVALID_RECORD_ID, null, E_INVALID_ARG
654                         , "[%s] Invalid argument is used. The specified recordId is INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG));
655
656         std::unique_ptr<CalTodo> pTodo(new (std::nothrow) CalTodo());
657         SysTryReturn(NID_SCL, pTodo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
658
659         calendar_record_h todoHandle = null;
660         int errorCode = calendar_db_get_record(_calendar_todo._uri, todoId, &todoHandle);
661         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
662         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OBJ_NOT_FOUND, "[%s] The specified record is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
663
664         _CalTodoImpl::GetInstance(*pTodo.get())->SetRecordHandle(todoHandle);
665         _RecordImpl::GetInstance(*pTodo.get())->SetRecordId(todoId);
666
667         return pTodo.release();
668 }
669
670 Calendar*
671 _CalendarbookImpl::GetCalendarN(RecordId calendarId) const
672 {
673         ClearLastResult();
674
675         SysTryReturn(NID_SCL, calendarId != INVALID_RECORD_ID, null, E_INVALID_ARG
676                         , "[%s] Invalid argument is used. The specified recordId is INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG));
677
678         std::unique_ptr<Calendar> pCalendar(new (std::nothrow) Calendar(CALENDAR_ITEM_TYPE_EVENT_AND_TODO));
679         SysTryReturn(NID_SCL, pCalendar != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
680
681         calendar_record_h calendarHandle = null;
682         int errorCode = calendar_db_get_record(_calendar_book._uri, calendarId, &calendarHandle);
683         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
684         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OBJ_NOT_FOUND, "[%s] The specified record is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
685
686         _CalendarImpl::GetInstance(*pCalendar.get())->SetRecordHandle(calendarHandle);
687         _RecordImpl::GetInstance(*pCalendar.get())->SetRecordId(calendarId);
688
689         return pCalendar.release();
690 }
691
692 IList*
693 _CalendarbookImpl::GetTodosN(const DateTime& start, const DateTime& end, int pageNo, int countPerPage, unsigned long status,
694                                                          unsigned long priority) const
695 {
696         ClearLastResult();
697
698         SysTryReturn(NID_SCL, pageNo > 0 && countPerPage > 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. The pageNo or countPerPage is less than 1.", GetErrorMessage(E_INVALID_ARG));
699         SysTryReturn(NID_SCL, start <= end, null, E_INVALID_ARG, "[%s] Invalid argument is used. The start time is later than the end date.", GetErrorMessage(E_INVALID_ARG));
700         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(start), null, E_INVALID_ARG, "[%s] Invalid argument is used. start = %S", GetErrorMessage(E_INVALID_ARG), start.ToString().GetPointer());
701         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(end), null, E_INVALID_ARG, "[%s] Invalid argument is used. end = %S", GetErrorMessage(E_INVALID_ARG), end.ToString().GetPointer());
702         SysTryReturn(NID_SCL, CheckValidTodoStatus(status), null, E_INVALID_ARG, "[%s] Invalid argument is used. status = %ld", GetErrorMessage(E_INVALID_ARG), status);
703         SysTryReturn(NID_SCL, CheckValidTodoPriority(priority), null, E_INVALID_ARG, "[%s] Invalid argument is used. priority = %ld", GetErrorMessage(E_INVALID_ARG), priority);
704
705         DateTime tmpStart;
706         DateTime tmpEnd;
707         tmpStart.SetValue(start.GetYear(), start.GetMonth(), start.GetDay());
708         tmpEnd.SetValue(end.GetYear(), end.GetMonth(), end.GetDay(), 23, 59, 59);
709         calendar_time_s convertedStartTime;
710         calendar_time_s convertedEndTime;
711         _CalendarbookUtil::ConvertDateTimeToCalTime(tmpStart, convertedStartTime);
712         _CalendarbookUtil::ConvertDateTimeToCalTime(tmpEnd, convertedEndTime);
713
714         calendar_filter_h mainFilterHandle = null;
715         int errorCode = calendar_filter_create(_calendar_todo._uri, &mainFilterHandle);
716         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
717         _CalendarFilter mainFilter(mainFilterHandle);
718
719         // Condition : Due time of the item >= start time of filter
720         errorCode = calendar_filter_add_caltime(mainFilterHandle, _calendar_todo.due_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, convertedStartTime);
721         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
722
723         // Condition : Due time of the item <= end time of filter
724         errorCode = calendar_filter_add_operator(mainFilterHandle, CALENDAR_FILTER_OPERATOR_AND);
725         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
726         errorCode = calendar_filter_add_caltime(mainFilterHandle, _calendar_todo.due_time, CALENDAR_MATCH_LESS_THAN_OR_EQUAL, convertedEndTime);
727         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
728
729         // Condition : status
730         calendar_filter_h subFilterHandle = null;
731         errorCode = calendar_filter_create(_calendar_todo._uri, &subFilterHandle);
732         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
733         _CalendarFilter subFilter(subFilterHandle);
734         subFilterHandle = null;
735
736         int tmpStatus = TODO_STATUS_NONE;
737         bool alreadyAdded = false;
738         for (int i = 0; i < _NUMBER_OF_TODO_STATUS; i++)
739         {
740                 if (status & tmpStatus)
741                 {
742                         if (alreadyAdded)
743                         {
744                                 errorCode = calendar_filter_add_operator(subFilter.GetHandle(), CALENDAR_FILTER_OPERATOR_OR);
745                                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
746                         }
747
748                         errorCode = calendar_filter_add_int(subFilter.GetHandle(), _calendar_todo.todo_status, CALENDAR_MATCH_EQUAL, ConvertTodoStatusToCalendarTodoStatus(tmpStatus));
749                         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
750
751                         alreadyAdded = true;
752                 }
753
754                 tmpStatus <<= 1;
755         }
756
757         errorCode = calendar_filter_add_operator(mainFilterHandle, CALENDAR_FILTER_OPERATOR_AND);
758         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
759         errorCode = calendar_filter_add_filter(mainFilterHandle, subFilter.GetHandle());
760         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
761
762         // Condition : priority
763         errorCode = calendar_filter_create(_calendar_todo._uri, &subFilterHandle);
764         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
765         subFilter.ResetHandle(subFilterHandle);
766         subFilterHandle = null;
767
768         int tmpPriority = TODO_PRIORITY_LOW;
769         alreadyAdded = false;
770         for (int i = 0; i < _NUMBER_OF_TODO_PRIORITY; i++)
771         {
772                 if (priority & tmpPriority)
773                 {
774                         if (alreadyAdded)
775                         {
776                                 errorCode = calendar_filter_add_operator(subFilter.GetHandle(), CALENDAR_FILTER_OPERATOR_OR);
777                                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
778                         }
779
780                         errorCode = calendar_filter_add_int(subFilter.GetHandle(), _calendar_todo.priority, CALENDAR_MATCH_EQUAL, ConvertTodoPriorityToCalendarTodoPriority(tmpPriority));
781                         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
782
783                         alreadyAdded = true;
784                 }
785                 tmpPriority <<= 1;
786         }
787
788         errorCode = calendar_filter_add_operator(mainFilterHandle, CALENDAR_FILTER_OPERATOR_AND);
789         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
790         errorCode = calendar_filter_add_filter(mainFilterHandle, subFilter.GetHandle());
791         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
792
793         // Create query
794         calendar_query_h queryHandle = null;
795         errorCode = calendar_query_create(_calendar_todo._uri, &queryHandle);
796         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
797         _CalendarQuery query(queryHandle);
798
799         calendar_query_set_sort(queryHandle, _calendar_todo.due_time, true);
800         calendar_query_set_filter(queryHandle, mainFilterHandle);
801
802         calendar_list_h calendarListHandle = null;
803         errorCode = calendar_db_get_records_with_query(queryHandle, (pageNo - 1) * countPerPage, countPerPage, &calendarListHandle);
804         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
805         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
806         _CalendarList calendarList(calendarListHandle);
807
808         // TODO:- Should we use SingleObjectDeleter instead ?
809         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
810         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
811         result r = pList->Construct();
812         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
813
814         int count = 0;
815         calendar_list_get_count(calendarListHandle, &count);
816
817         for (int i = 0; i < count; i++)
818         {
819                 std::unique_ptr<CalTodo> pTmpTodo(new (std::nothrow) CalTodo());
820                 SysTryReturn(NID_SCL, pTmpTodo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
821
822                 r = pList->Add(pTmpTodo.get());
823                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
824                 pTmpTodo.release();
825         }
826
827         calendar_list_first(calendarListHandle);
828         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
829         while (pEnum->MoveNext() == E_SUCCESS)
830         {
831                 CalTodo* pTmpTodo = static_cast<CalTodo*>(pEnum->GetCurrent());
832
833                 calendar_record_h tmpRecordHandle = null;
834                 errorCode = calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
835                 SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
836
837                 _CalTodoImpl::GetInstance(*pTmpTodo)->SetRecordHandle(tmpRecordHandle);
838
839                 int dbIndex = -1;
840                 errorCode = calendar_record_get_int(tmpRecordHandle, _calendar_todo.id, &dbIndex);
841                 SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
842                 _RecordImpl::GetInstance(*pTmpTodo)->SetRecordId(dbIndex);
843
844                 calendar_list_next(calendarListHandle);
845         }
846
847         calendar_list_destroy(calendarList.ReleaseHandle(), false);
848
849         return pList.release();
850 }
851
852 int
853 _CalendarbookImpl::GetTodoCount(const DateTime& start, const DateTime& end, unsigned long status, unsigned long priority) const
854 {
855         ClearLastResult();
856
857         SysTryReturn(NID_SCL, start <= end, _INVALID_COUNT
858                         , E_INVALID_ARG, "[%s] Invalid argument is used. The start time is later than the end date.", GetErrorMessage(E_INVALID_ARG));
859         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(start), _INVALID_COUNT
860                         , E_INVALID_ARG, "[%s] Invalid argument is used. start = %S", GetErrorMessage(E_INVALID_ARG), start.ToString().GetPointer());
861         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(end), _INVALID_COUNT
862                         , E_INVALID_ARG, "[%s] Invalid argument is used. end = %S", GetErrorMessage(E_INVALID_ARG), end.ToString().GetPointer());
863         SysTryReturn(NID_SCL, CheckValidTodoStatus(status), _INVALID_COUNT
864                         , E_INVALID_ARG, "[%s] Invalid argument is used. status = %ld", GetErrorMessage(E_INVALID_ARG), status);
865         SysTryReturn(NID_SCL, CheckValidTodoPriority(priority), _INVALID_COUNT
866                         , E_INVALID_ARG, "[%s] Invalid argument is used. priority = %ld", GetErrorMessage(E_INVALID_ARG), priority);
867
868         DateTime tmpStart;
869         DateTime tmpEnd;
870         tmpStart.SetValue(start.GetYear(), start.GetMonth(), start.GetDay());
871         tmpEnd.SetValue(end.GetYear(), end.GetMonth(), end.GetDay(), 23, 59, 59);
872         calendar_time_s convertedStartTime;
873         calendar_time_s convertedEndTime;
874         _CalendarbookUtil::ConvertDateTimeToCalTime(tmpStart, convertedStartTime);
875         _CalendarbookUtil::ConvertDateTimeToCalTime(tmpEnd, convertedEndTime);
876
877         calendar_filter_h mainFilterHandle = null;
878         int errorCode = calendar_filter_create(_calendar_todo._uri, &mainFilterHandle);
879         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, _INVALID_COUNT, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
880         _CalendarFilter mainFilter(mainFilterHandle);
881
882         // Condition : Due time of the item => start time of filter
883         errorCode = calendar_filter_add_caltime(mainFilterHandle, _calendar_todo.due_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, convertedStartTime);
884         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
885
886         // Condition : Due time of the item <= end time of filter
887         errorCode = calendar_filter_add_operator(mainFilterHandle, CALENDAR_FILTER_OPERATOR_AND);
888         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
889         errorCode = calendar_filter_add_caltime(mainFilterHandle, _calendar_todo.due_time, CALENDAR_MATCH_LESS_THAN_OR_EQUAL, convertedEndTime);
890         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
891
892         // Condition : status
893         calendar_filter_h subFilterHandle = null;
894         errorCode = calendar_filter_create(_calendar_todo._uri, &subFilterHandle);
895         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, _INVALID_COUNT, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
896         _CalendarFilter subFilter(subFilterHandle);
897         subFilterHandle = null;
898
899         int tmpStatus = TODO_STATUS_NONE;
900         bool alreadyAdded = false;
901         for (int i = 0; i < _NUMBER_OF_TODO_STATUS; i++)
902         {
903                 if (status & tmpStatus)
904                 {
905                         if (alreadyAdded)
906                         {
907                                 errorCode = calendar_filter_add_operator(subFilter.GetHandle(), CALENDAR_FILTER_OPERATOR_OR);
908                                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
909                         }
910
911                         errorCode = calendar_filter_add_int(subFilter.GetHandle(), _calendar_todo.todo_status, CALENDAR_MATCH_EQUAL, ConvertTodoStatusToCalendarTodoStatus(tmpStatus));
912                         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
913
914                         alreadyAdded = true;
915                 }
916
917                 tmpStatus <<= 1;
918         }
919         errorCode = calendar_filter_add_operator(mainFilterHandle, CALENDAR_FILTER_OPERATOR_AND);
920         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
921         errorCode = calendar_filter_add_filter(mainFilterHandle, subFilter.GetHandle());
922         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
923
924         // Condition : priority
925         errorCode = calendar_filter_create(_calendar_todo._uri, &subFilterHandle);
926         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, _INVALID_COUNT, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
927         subFilter.ResetHandle(subFilterHandle);
928         subFilterHandle = null;
929
930         int tmpPriority = TODO_PRIORITY_LOW;
931         alreadyAdded = false;
932         for (int i = 0; i < _NUMBER_OF_TODO_PRIORITY; i++)
933         {
934                 if (priority & tmpPriority)
935                 {
936                         if (alreadyAdded)
937                         {
938                                 errorCode = calendar_filter_add_operator(subFilter.GetHandle(), CALENDAR_FILTER_OPERATOR_OR);
939                                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
940                         }
941
942                         errorCode = calendar_filter_add_int(subFilter.GetHandle(), _calendar_todo.priority, CALENDAR_MATCH_EQUAL, ConvertTodoPriorityToCalendarTodoPriority(tmpPriority));
943                         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
944
945                         alreadyAdded = true;
946                 }
947                 tmpPriority <<= 1;
948         }
949
950         errorCode = calendar_filter_add_operator(mainFilterHandle, CALENDAR_FILTER_OPERATOR_AND);
951         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
952         errorCode = calendar_filter_add_filter(mainFilterHandle, subFilter.GetHandle());
953         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
954
955         // Create query
956         calendar_query_h queryHandle = null;
957         errorCode = calendar_query_create(_calendar_todo._uri, &queryHandle);
958         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, _INVALID_COUNT, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
959         _CalendarQuery query(queryHandle);
960
961         calendar_query_set_filter(queryHandle, mainFilterHandle);
962
963         int count = _INVALID_COUNT;
964
965         errorCode = calendar_db_get_count_with_query(queryHandle, &count);
966         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
967
968         return count;
969 }
970
971 IList*
972 _CalendarbookImpl::GetEventInstancesN(const DateTime& start, const DateTime& end,
973                                                                           const Tizen::Locales::TimeZone& timeZone, int pageNo, int countPerPage,
974                                                                           unsigned long category) const
975 {
976         ClearLastResult();
977
978         SysTryReturn(NID_SCL, pageNo > 0 && countPerPage > 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. The pageNo or countPerPage is less than 1.", GetErrorMessage(E_INVALID_ARG));
979         SysTryReturn(NID_SCL, start <= end, null, E_INVALID_ARG, "[%s] Invalid argument is used. The start time is later than the end date.", GetErrorMessage(E_INVALID_ARG));
980         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(start), null, E_INVALID_ARG, "[%s] Invalid argument is used. start = %S", GetErrorMessage(E_INVALID_ARG), start.ToString().GetPointer());
981         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(end), null, E_INVALID_ARG, "[%s] Invalid argument is used. end = %S", GetErrorMessage(E_INVALID_ARG), end.ToString().GetPointer());
982         SysTryReturn(NID_SCL, (category & EVENT_CATEGORY_APPOINTMENT) || (category & EVENT_CATEGORY_ANNIVERSARY), null, E_INVALID_ARG, "[%s] Invalid argument is used. category = %ld", GetErrorMessage(E_INVALID_ARG), category);
983
984         std::unique_ptr<IList, AllElementsDeleter> pList(GetEventInstancesCommonN(start, end, timeZone, pageNo, countPerPage, category));
985         result r = GetLastResult();
986         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
987
988         return pList.release();
989 }
990
991 result
992 _CalendarbookImpl::GetEventInstances(const DateTime& start, const DateTime& end,
993                                                                          const Tizen::Locales::TimeZone& timeZone, int pageNo, int countPerPage, unsigned long category,
994                                                                          RequestId& reqId, const IRecordListener& listener) const
995 {
996         __requestId++;
997         reqId = __requestId;
998
999         SysTryReturn(NID_SCL, pageNo > 0 && countPerPage > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The pageNo or countPerPage is less than 1.", GetErrorMessage(E_INVALID_ARG));
1000         SysTryReturn(NID_SCL, start <= end, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The start time is later than the end date.", GetErrorMessage(E_INVALID_ARG));
1001         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(start), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. start = %S", GetErrorMessage(E_INVALID_ARG), start.ToString().GetPointer());
1002         SysTryReturn(NID_SCL, _CalendarbookUtil::CheckValidDateTime(end), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. end = %S", GetErrorMessage(E_INVALID_ARG), end.ToString().GetPointer());
1003         SysTryReturn(NID_SCL, (category & EVENT_CATEGORY_APPOINTMENT) || (category & EVENT_CATEGORY_ANNIVERSARY), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. category = %ld", GetErrorMessage(E_INVALID_ARG), category);
1004
1005         std::unique_ptr<_CalendarbookRecordRetrivalEvent> pEvent(new (std::nothrow) _CalendarbookRecordRetrivalEvent());
1006         SysTryReturnResult(NID_SCL, pEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1007         result r = pEvent->Construct();
1008         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
1009         r = pEvent->AddListener(listener);
1010         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
1011
1012         if (__pRecordRetrivalThread != null)
1013         {
1014                 __pRecordRetrivalThread->Join();
1015                 delete __pRecordRetrivalThread;
1016                 __pRecordRetrivalThread = null;
1017         }
1018
1019         std::unique_ptr<_CalendarbookRecordRetrivalThread> pRecordRetrivalThread(new (std::nothrow) _CalendarbookRecordRetrivalThread());
1020         SysTryReturnResult(NID_SCL, pRecordRetrivalThread != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1021         r = pRecordRetrivalThread->Construct(__requestId, *pEvent.release(), start, end, timeZone, pageNo, countPerPage, category);
1022         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
1023
1024         r = pRecordRetrivalThread->Start();
1025         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "A system error has been occurred.");
1026
1027         __pRecordRetrivalThread = pRecordRetrivalThread.release();
1028
1029         return E_SUCCESS;
1030 }
1031
1032 IList*
1033 _CalendarbookImpl::GetAllEventsN(void) const
1034 {
1035         calendar_list_h calendarListHandle = null;
1036         int errorCode = calendar_db_get_all_records(_calendar_event._uri, 0, 0, &calendarListHandle);
1037         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1038         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1039         _CalendarList calendarList(calendarListHandle);
1040
1041         result r = E_SUCCESS;
1042         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1043         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1044         r = pList->Construct();
1045         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1046
1047         int count = 0;
1048         calendar_list_get_count(calendarListHandle, &count);
1049
1050         for (int i = 0; i < count; i++)
1051         {
1052                 std::unique_ptr<CalEvent> pTmpEvent(new (std::nothrow) CalEvent());
1053                 SysTryReturn(NID_SCL, pTmpEvent != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1054
1055                 r = pList->Add(pTmpEvent.get());
1056                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1057                 pTmpEvent.release();
1058         }
1059
1060         calendar_list_first(calendarListHandle);
1061         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
1062         while (pEnum->MoveNext() == E_SUCCESS)
1063         {
1064                 CalEvent* pTmpEvent = static_cast<CalEvent*>(pEnum->GetCurrent());
1065
1066                 calendar_record_h tmpRecordHandle = null;
1067                 errorCode = calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1068                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1069
1070                 _CalEventImpl::GetInstance(*pTmpEvent)->SetRecordHandle(tmpRecordHandle);
1071
1072                 int dbIndex = -1;
1073                 calendar_record_get_int(tmpRecordHandle, _calendar_event.id, &dbIndex);
1074                 _RecordImpl::GetInstance(*pTmpEvent)->SetRecordId(dbIndex);
1075
1076                 calendar_list_next(calendarListHandle);
1077         }
1078
1079         calendar_list_destroy(calendarList.ReleaseHandle(), false);
1080
1081         return pList.release();
1082 }
1083
1084 IList*
1085 _CalendarbookImpl::GetAllTodosN(void) const
1086 {
1087         calendar_list_h calendarListHandle = null;
1088         int errorCode = calendar_db_get_all_records(_calendar_todo._uri, 0, 0, &calendarListHandle);
1089         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1090         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1091         _CalendarList calendarList(calendarListHandle);
1092
1093         result r = E_SUCCESS;
1094         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1095         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1096         r = pList->Construct();
1097         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1098
1099         int count = 0;
1100         calendar_list_get_count(calendarListHandle, &count);
1101
1102         for (int i = 0; i < count; i++)
1103         {
1104                 std::unique_ptr<CalTodo> pTmpTodo(new (std::nothrow) CalTodo());
1105                 SysTryReturn(NID_SCL, pTmpTodo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1106
1107                 r = pList->Add(pTmpTodo.get());
1108                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1109                 pTmpTodo.release();
1110         }
1111
1112         calendar_list_first(calendarListHandle);
1113         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
1114         while (pEnum->MoveNext() == E_SUCCESS)
1115         {
1116                 CalTodo* pTmpTodo = static_cast<CalTodo*>(pEnum->GetCurrent());
1117
1118                 calendar_record_h tmpRecordHandle = null;
1119                 errorCode = calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1120                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1121
1122                 _CalTodoImpl::GetInstance(*pTmpTodo)->SetRecordHandle(tmpRecordHandle);
1123
1124                 int dbIndex = -1;
1125                 calendar_record_get_int(tmpRecordHandle, _calendar_todo.id, &dbIndex);
1126                 _RecordImpl::GetInstance(*pTmpTodo)->SetRecordId(dbIndex);
1127
1128                 calendar_list_next(calendarListHandle);
1129         }
1130
1131         calendar_list_destroy(calendarList.ReleaseHandle(), false);
1132
1133         return pList.release();
1134 }
1135
1136 IList*
1137 _CalendarbookImpl::GetAllCalendarsN(void) const
1138 {
1139         calendar_list_h calendarListHandle = null;
1140         int errorCode = calendar_db_get_all_records(_calendar_book._uri, 0, 0, &calendarListHandle);
1141         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1142         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1143         _CalendarList calendarList(calendarListHandle);
1144
1145         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1146         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1147         result r = pList->Construct();
1148         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1149
1150         int count = 0;
1151         calendar_list_get_count(calendarListHandle, &count);
1152
1153         for (int i = 0; i < count; i++)
1154         {
1155                 std::unique_ptr<Calendar> pTmpCalendar(_CalendarImpl::CreateDefaultInstanceN());
1156                 SysTryReturn(NID_SCL, pTmpCalendar != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1157
1158                 r = pList->Add(pTmpCalendar.get());
1159                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1160                 pTmpCalendar.release();
1161         }
1162
1163         calendar_list_first(calendarListHandle);
1164         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
1165         while (pEnum->MoveNext() == E_SUCCESS)
1166         {
1167                 Calendar* pTmpCalendar = static_cast<Calendar*>(pEnum->GetCurrent());
1168
1169                 calendar_record_h tmpRecordHandle = null;
1170                 errorCode = calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1171                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1172                 _CalendarImpl::GetInstance(*pTmpCalendar)->SetRecordHandle(tmpRecordHandle);
1173
1174                 int dbIndex = -1;
1175                 calendar_record_get_int(tmpRecordHandle, _calendar_book.id, &dbIndex);
1176                 _RecordImpl::GetInstance(*pTmpCalendar)->SetRecordId(dbIndex);
1177
1178                 calendar_list_next(calendarListHandle);
1179         }
1180
1181         calendar_list_destroy(calendarList.ReleaseHandle(), false);
1182
1183         return pList.release();
1184 }
1185
1186 IList*
1187 _CalendarbookImpl::GetChangedEventsAfterN(int version, int& latestVersion) const
1188 {
1189         ClearLastResult();
1190
1191         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. version = %d", version);
1192
1193         calendar_list_h calendarListHandle = null;
1194         int tmpLatestVersion = 0;
1195         int errorCode = calendar_db_get_changes_by_version(_calendar_event._uri, CALENDAR_BOOK_FILTER_ALL, version
1196                         , &calendarListHandle, &tmpLatestVersion);
1197         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null
1198                         , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1199         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1200
1201         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1202         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1203         result r = pList->Construct();
1204         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1205
1206         int count = 0;
1207         calendar_list_get_count(calendarListHandle, &count);
1208
1209         calendar_list_first(calendarListHandle);
1210         for (int i = 0; i < count; i++)
1211         {
1212                 calendar_record_h tmpRecordHandle = null;
1213                 errorCode = calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1214                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1215
1216                 std::unique_ptr<CalEventChangeInfo> pChangedInfo(_CalendarbookImpl::ConvertModifiedEventToCalEventChangeInfoN(tmpRecordHandle));
1217                 SysTryReturn(NID_SCL, pChangedInfo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1218
1219                 r = pList->Add(pChangedInfo.get());
1220                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1221                 pChangedInfo.release();
1222
1223                 calendar_list_next(calendarListHandle);
1224         }
1225
1226         latestVersion = tmpLatestVersion;
1227
1228         return pList.release();
1229 }
1230
1231 IList*
1232 _CalendarbookImpl::GetChangedTodosAfterN(int version, int& latestVersion) const
1233 {
1234         ClearLastResult();
1235
1236         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. version = %d", GetErrorMessage(E_INVALID_ARG), version);
1237
1238         calendar_list_h calendarListHandle = null;
1239         int tmpLatestVersion = 0;
1240         int errorCode = calendar_db_get_changes_by_version(_calendar_todo._uri, CALENDAR_BOOK_FILTER_ALL, version
1241                         , &calendarListHandle, &tmpLatestVersion);
1242         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null
1243                         , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1244         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1245         _CalendarList calendarList(calendarListHandle);
1246
1247         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1248         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1249         result r = pList->Construct();
1250         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1251
1252         int count = 0;
1253         calendar_list_get_count(calendarListHandle, &count);
1254
1255         calendar_list_first(calendarListHandle);
1256         for (int i = 0; i < count; i++)
1257         {
1258                 calendar_record_h tmpRecordHandle = null;
1259                 errorCode = calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1260                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1261
1262                 std::unique_ptr<CalTodoChangeInfo> pChangedInfo(_CalendarbookImpl::ConvertModifiedTodoToCalTodoChangeInfoN(tmpRecordHandle));
1263                 SysTryReturn(NID_SCL, pChangedInfo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1264
1265                 r = pList->Add(pChangedInfo.get());
1266                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1267                 pChangedInfo.release();
1268
1269                 calendar_list_next(calendarListHandle);
1270         }
1271
1272         latestVersion = tmpLatestVersion;
1273
1274         return pList.release();
1275 }
1276
1277 result
1278 _CalendarbookImpl::RemoveEventInstance(const CalEventInstance& eventInstance)
1279 {
1280         calendar_record_h eventHandle = null;
1281
1282         // Get event handle from DB
1283         int errorCode = calendar_db_get_record(_calendar_event._uri, eventInstance.GetOriginalEventId(), &eventHandle);
1284         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, E_SYSTEM, "A system error has been occurred.");
1285         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OBJ_NOT_FOUND, "The specified record is not found.");
1286         _CalendarRecord eventRecord(eventHandle);
1287
1288         int baseEventId = _INVALID_EVENT_DB_ID;
1289         calendar_record_get_int(eventHandle, _calendar_event.original_event_id, &baseEventId);
1290
1291         if (baseEventId != _INVALID_EVENT_DB_ID)
1292         {
1293                 errorCode = calendar_db_delete_record(_calendar_event._uri, eventInstance.GetOriginalEventId());
1294                 SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
1295         }
1296         else
1297         {
1298                 char* pExdate = null;
1299
1300                 // Append exdate
1301                 calendar_record_get_str_p(eventHandle, _calendar_event.exdate, &pExdate);
1302
1303                 String exdate;
1304                 if (pExdate != null && strlen(pExdate) > 0)
1305                 {
1306                         exdate.Append(pExdate);
1307                         exdate.Append(_RECURRENCE_DELIMITER);
1308                 }
1309
1310                 exdate.Append(_CalendarbookUtil::ConvertDateTimeToRRuleDateTimeString(eventInstance.GetStartTime(), eventInstance.IsAllDayEvent()));
1311
1312                 std::unique_ptr<char[]> pAppendedExdate(_StringConverter::CopyToCharArrayN(exdate));
1313                 SysTryReturnResult(NID_SCL, pAppendedExdate != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1314
1315                 errorCode = calendar_record_set_str(eventHandle, _calendar_event.exdate, pAppendedExdate.get());
1316                 SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1317
1318                 // Update event handle
1319                 errorCode = calendar_db_update_record(eventHandle);
1320                 SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
1321         }
1322
1323         return E_SUCCESS;
1324 }
1325
1326 result
1327 _CalendarbookImpl::UpdateEventInstance(const CalEventInstance& eventInstance, CalEvent& event)
1328 {
1329         SysTryReturnResult(NID_SCL, event.GetRecordId() != INVALID_RECORD_ID, E_INVALID_ARG
1330                         , "Invalid argument is used. The event's recordId is INVALID_RECORD_ID.");
1331         SysTryReturnResult(NID_SCL, eventInstance.GetOriginalEventId() == event.GetRecordId(), E_INVALID_ARG
1332                         , "[%s] Invalid argument is used. The event's recordId is not equal to eventInstance's original event ID.", GetErrorMessage(E_INVALID_ARG));
1333         SysTryReturnResult(NID_SCL, CheckEventExistance(event.GetRecordId()), E_OBJ_NOT_FOUND, "The specified event is not found.");
1334
1335         calendar_record_h eventHandle = null;
1336         int errorCode = calendar_record_clone(_CalEventImpl::GetInstance(event)->GetRecordHandle(), &eventHandle);
1337         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1338         _CalendarRecord eventRecord(eventHandle);
1339
1340         int baseEventId = _INVALID_EVENT_DB_ID;
1341         calendar_record_get_int(eventHandle, _calendar_event.original_event_id, &baseEventId);
1342
1343         calendar_record_h baseEventHandle = null;
1344         if (baseEventId == _INVALID_EVENT_DB_ID)
1345         {
1346                 errorCode = calendar_db_get_record(_calendar_event._uri, eventInstance.GetOriginalEventId(), &baseEventHandle);
1347         }
1348         else
1349         {
1350                 errorCode = calendar_db_get_record(_calendar_event._uri, baseEventId, &baseEventHandle);
1351         }
1352         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1353         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OBJ_NOT_FOUND, "[%s] The base event is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1354         _CalendarRecord baseEventRecord(baseEventHandle);
1355
1356         int tmpIntValue = 0;
1357         calendar_record_get_int(baseEventHandle, _calendar_event.freq, &tmpIntValue);
1358         calendar_record_set_int(eventHandle, _calendar_event.freq, tmpIntValue);
1359         calendar_record_get_int(baseEventHandle, _calendar_event.range_type, &tmpIntValue);
1360         calendar_record_set_int(eventHandle, _calendar_event.range_type, tmpIntValue);
1361         calendar_record_get_int(baseEventHandle, _calendar_event.count, &tmpIntValue);
1362         calendar_record_set_int(eventHandle, _calendar_event.count, tmpIntValue);
1363         calendar_record_get_int(baseEventHandle, _calendar_event.interval, &tmpIntValue);
1364         calendar_record_set_int(eventHandle, _calendar_event.interval, tmpIntValue);
1365         calendar_record_get_int(baseEventHandle, _calendar_event.wkst, &tmpIntValue);
1366         calendar_record_set_int(eventHandle, _calendar_event.wkst, tmpIntValue);
1367
1368         calendar_time_s tmpTimeValue;
1369         calendar_record_get_caltime(baseEventHandle, _calendar_event.until_time, &tmpTimeValue);
1370
1371         calendar_time_s tmpStartTimeValue;
1372         DateTime convertedTime;
1373         calendar_record_get_caltime(eventHandle, _calendar_event.start_time, &tmpStartTimeValue);
1374
1375         if (tmpTimeValue.type != tmpStartTimeValue.type)
1376         {
1377                 if (tmpStartTimeValue.type == CALENDAR_TIME_UTIME)
1378                 {
1379                         convertedTime.SetValue(tmpTimeValue.time.date.year, tmpTimeValue.time.date.month, tmpTimeValue.time.date.mday);
1380                         tmpTimeValue.type = CALENDAR_TIME_UTIME;
1381                         tmpTimeValue.time.utime = _CalendarbookUtil::ConvertDateTimeToEpochTime(convertedTime);
1382                 }
1383                 else
1384                 {
1385                         convertedTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(tmpTimeValue.time.utime);
1386                         tmpTimeValue.type = CALENDAR_TIME_LOCALTIME;
1387                         tmpTimeValue.time.date.year = convertedTime.GetYear();
1388                         tmpTimeValue.time.date.month = convertedTime.GetMonth();
1389                         tmpTimeValue.time.date.mday = convertedTime.GetDay();
1390                 }
1391         }
1392         calendar_record_set_caltime(eventHandle, _calendar_event.until_time, tmpTimeValue);
1393
1394         char* tmpStrValue = null;
1395         calendar_record_get_str_p(baseEventHandle, _calendar_event.byday, &tmpStrValue);
1396         errorCode = calendar_record_set_str(eventHandle, _calendar_event.byday, tmpStrValue);
1397         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1398
1399         calendar_record_get_str_p(baseEventHandle, _calendar_event.bymonthday, &tmpStrValue);
1400         errorCode = calendar_record_set_str(eventHandle, _calendar_event.bymonthday, tmpStrValue);
1401         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1402
1403         calendar_record_get_str_p(baseEventHandle, _calendar_event.bymonth, &tmpStrValue);
1404         errorCode = calendar_record_set_str(eventHandle, _calendar_event.bymonth, tmpStrValue);
1405         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1406
1407         calendar_record_get_str_p(baseEventHandle, _calendar_event.exdate, &tmpStrValue);
1408         errorCode = calendar_record_set_str(eventHandle, _calendar_event.exdate, tmpStrValue);
1409         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1410
1411         if (baseEventId == _INVALID_EVENT_DB_ID)
1412         {
1413                 calendar_record_set_int(eventHandle, _calendar_event.original_event_id, eventInstance.GetOriginalEventId());
1414
1415                 std::unique_ptr<char[]> pConvertedRecurrenceId(_StringConverter::CopyToCharArrayN(_CalendarbookUtil::ConvertDateTimeToRRuleDateTimeString(eventInstance.GetStartTime(), eventInstance.IsAllDayEvent())));
1416                 errorCode = calendar_record_set_str(eventHandle, _calendar_event.recurrence_id, pConvertedRecurrenceId.get());
1417                 SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1418
1419                 int dbIndex = -1;
1420                 errorCode = calendar_db_insert_record(eventHandle, &dbIndex);
1421                 SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
1422                 _RecordImpl::GetInstance(event)->SetRecordId(dbIndex);
1423         }
1424         else
1425         {
1426                 errorCode = calendar_db_update_record(eventHandle);
1427                 SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_DB_FAILED, E_SYSTEM, "A system error has been occurred.");
1428                 SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OBJ_NOT_FOUND, "The base event of the event is not found.");
1429         }
1430
1431         calendar_record_h tmpEventHandle = null;
1432         errorCode = calendar_db_get_record(_calendar_event._uri, event.GetRecordId(), &tmpEventHandle);
1433         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
1434         _CalEventImpl::GetInstance(event)->SetRecordHandle(tmpEventHandle);
1435
1436         return E_SUCCESS;
1437 }
1438
1439 int
1440 _CalendarbookImpl::GetLatestVersion(void) const
1441 {
1442         ClearLastResult();
1443
1444         int dbVersion = 0;
1445         int errorCode = calendar_db_get_current_version(&dbVersion);
1446         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, 0, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1447
1448         return dbVersion;
1449 }
1450
1451 IList*
1452 _CalendarbookImpl::SearchN(const CalendarbookFilter& filter, unsigned long propertySortedBy, SortOrder sortOrder, int offset, int maxCount) const
1453 {
1454         CalendarbookFilterType type = _CalendarbookFilterImpl::GetInstance(filter)->GetType();
1455         calendar_filter_h filterHandle = _CalendarbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1456
1457         SysTryReturn(NID_SCL, offset >= 0 && maxCount >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified offset or maxCount is less than 0.", GetErrorMessage(E_INVALID_ARG));
1458         SysTryReturn(NID_SCL, propertySortedBy == 0 || _CalendarbookFilterImpl::IsValidProperty(type, propertySortedBy), null, E_INVALID_ARG
1459                         , "[%s] Invalid argument is used. propertySortedBy = %d", GetErrorMessage(E_INVALID_ARG), propertySortedBy);
1460
1461         const char* pViewUri = _CalendarbookFilterImpl::GetUriFromType(type);
1462
1463         // Create query
1464         calendar_query_h queryHandle = null;
1465         int errorCode = calendar_query_create(pViewUri, &queryHandle);
1466         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1467         _CalendarQuery query(queryHandle);
1468
1469         if (propertySortedBy != 0 && sortOrder != SORT_ORDER_NONE)
1470         {
1471                 unsigned int viewSortPropertyId = _CalendarbookFilterImpl::GetViewPropertyId(type, propertySortedBy);
1472                 bool ascending = (sortOrder == SORT_ORDER_ASCENDING) ? true : false;
1473
1474                 calendar_query_set_sort(queryHandle, viewSortPropertyId, ascending);
1475         }
1476
1477         if (filterHandle)
1478         {
1479                 calendar_query_set_filter(queryHandle, filterHandle);
1480         }
1481
1482         calendar_list_h resultListHandle = null;
1483         errorCode = calendar_db_get_records_with_query(queryHandle, offset, maxCount, &resultListHandle);
1484         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1485         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1486         _CalendarList calendarList(resultListHandle);
1487
1488         std::unique_ptr<IList, AllElementsDeleter> pList(null);
1489
1490         switch(type)
1491         {
1492         case CB_FI_TYPE_EVENT:
1493                 {
1494                         pList.reset(ConvertRecordListN<CalEvent, _CalEventImpl, _calendar_event_property_ids>(resultListHandle, _calendar_event));
1495                         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1496                         calendar_list_destroy(calendarList.ReleaseHandle(), false);
1497                 }
1498                 break;
1499         case CB_FI_TYPE_TODO:
1500                 {
1501                         pList.reset(ConvertRecordListN<CalTodo, _CalTodoImpl, _calendar_todo_property_ids>(resultListHandle, _calendar_todo));
1502                         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1503                         calendar_list_destroy(calendarList.ReleaseHandle(), false);
1504                 }
1505                 break;
1506         case CB_FI_TYPE_CALENDAR:
1507                 {
1508                         pList.reset(ConvertRecordListN<Calendar, _CalendarImpl, _calendar_book_property_ids>(resultListHandle, _calendar_book));
1509                         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1510                         calendar_list_destroy(calendarList.ReleaseHandle(), false);
1511                 }
1512                 break;
1513         case CB_FI_TYPE_ALL_DAY_EVENT_INSTANCE:
1514                 {
1515                         pList.reset(ConvertEventInstanceListN(resultListHandle, true));
1516                         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1517                 }
1518                 break;
1519         case CB_FI_TYPE_NON_ALL_DAY_EVENT_INSTANCE:
1520                 {
1521                         pList.reset(ConvertEventInstanceListN(resultListHandle, false));
1522                         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1523                 }
1524                 break;
1525         default:
1526                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of filter is (%d)", GetErrorMessage(E_INVALID_ARG), type);
1527                 break;
1528         };
1529
1530         return pList.release();
1531 }
1532
1533 int
1534 _CalendarbookImpl::GetMatchedItemCount(const CalendarbookFilter& filter) const
1535 {
1536         CalendarbookFilterType type = _CalendarbookFilterImpl::GetInstance(filter)->GetType();
1537         calendar_filter_h filterHandle = _CalendarbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1538
1539         const char* pViewUri = _CalendarbookFilterImpl::GetUriFromType(type);
1540
1541         // Create query
1542         calendar_query_h queryHandle = null;
1543         int errorCode = calendar_query_create(pViewUri, &queryHandle);
1544         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1545         _CalendarQuery query(queryHandle);
1546
1547         if (filterHandle)
1548         {
1549                 calendar_query_set_filter(queryHandle, filterHandle);
1550         }
1551
1552         int count = _INVALID_COUNT;
1553
1554         errorCode = calendar_db_get_count_with_query(queryHandle, &count);
1555         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1556
1557         return count;
1558 }
1559
1560 IList*
1561 _CalendarbookImpl::ParseEventsFromVcalendarN(const String& vCalFilePath)
1562 {
1563         ClearLastResult();
1564
1565         File vCalFile;
1566         result r = vCalFile.Construct(vCalFilePath, L"r");
1567         SysTryReturn(NID_SCL, r != E_INVALID_ARG && r != E_ILLEGAL_ACCESS && r != E_FILE_NOT_FOUND && r != E_OUT_OF_MEMORY
1568                         , null, r, "[%s] Propagating.", GetErrorMessage(r));
1569         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1570
1571         r = vCalFile.Seek(FILESEEKPOSITION_END, 0);
1572         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1573         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1574
1575         int fileLength = vCalFile.Tell();
1576         SysTryReturn(NID_SCL, fileLength > 0, null, E_INVALID_ARG, "[%s] Invalid argument is used.  The vCal file is empty.", GetErrorMessage(E_INVALID_ARG));
1577
1578         r = vCalFile.Seek(FILESEEKPOSITION_BEGIN, 0);
1579         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1580         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1581
1582         std::unique_ptr<ByteBuffer> pBuffer(new (std::nothrow) ByteBuffer());
1583         SysTryReturn(NID_SCL, pBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1584         r = pBuffer->Construct(fileLength);
1585         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1586
1587         r = vCalFile.Read(*pBuffer);
1588         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1589         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1590
1591         _CalendarConnector connector;
1592         r = connector.GetResult();
1593         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s]A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1594
1595         pBuffer->Rewind();
1596         calendar_list_h calendarListHandle = null;
1597         int errorCode = calendar_vcalendar_parse_to_calendar(reinterpret_cast<char*>(const_cast<byte*>(pBuffer->GetPointer())), &calendarListHandle);
1598         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1599         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.  The vCal file is invalid.", GetErrorMessage(E_INVALID_ARG));
1600         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE || errorCode == CALENDAR_ERROR_NO_DATA, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1601         _CalendarList calendarList(calendarListHandle);
1602
1603         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1604         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1605         r = pList->Construct();
1606         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1607
1608         int count = 0;
1609         calendar_list_get_count(calendarListHandle, &count);
1610
1611         calendar_list_first(calendarListHandle);
1612         for (int i = 0; i < count; i++)
1613         {
1614                 calendar_record_h tmpRecordHandle = null;
1615                 calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1616                 calendar_list_next(calendarListHandle);
1617
1618                 char* pUri = null;
1619                 calendar_record_get_uri_p(tmpRecordHandle, &pUri);
1620
1621                 if (strcmp(pUri, _calendar_event._uri) == 0)
1622                 {
1623                         std::unique_ptr<CalEvent> pTmpEvent(new (std::nothrow) CalEvent());
1624                         SysTryReturn(NID_SCL, pTmpEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1625
1626                         r = pList->Add(pTmpEvent.get());
1627                         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1628                         pTmpEvent.release();
1629                 }
1630                 else
1631                 {
1632                         calendar_list_remove(calendarListHandle, tmpRecordHandle);
1633                         calendar_record_destroy(tmpRecordHandle, true);
1634                 }
1635         }
1636
1637         calendar_list_first(calendarListHandle);
1638         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
1639         while (pEnum->MoveNext() == E_SUCCESS)
1640         {
1641                 CalEvent* pTmpEvent = static_cast<CalEvent*>(pEnum->GetCurrent());
1642
1643                 calendar_record_h tmpRecordHandle = null;
1644                 calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1645                 _CalEventImpl::GetInstance(*pTmpEvent)->SetRecordHandle(tmpRecordHandle);
1646
1647                 calendar_list_next(calendarListHandle);
1648         }
1649
1650         calendar_list_destroy(calendarList.ReleaseHandle(), false);
1651
1652         return pList.release();
1653 }
1654
1655 IList*
1656 _CalendarbookImpl::ParseTodosFromVcalendarN(const String& vCalFilePath)
1657 {
1658         ClearLastResult();
1659
1660         File vCalFile;
1661         result r = vCalFile.Construct(vCalFilePath, L"r");
1662         SysTryReturn(NID_SCL, r != E_INVALID_ARG && r != E_ILLEGAL_ACCESS && r != E_FILE_NOT_FOUND && r != E_OUT_OF_MEMORY
1663                         , null, r, "[%s] Propagating.", GetErrorMessage(r));
1664         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1665
1666         r = vCalFile.Seek(FILESEEKPOSITION_END, 0);
1667         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1668         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1669
1670         int fileLength = vCalFile.Tell();
1671         SysTryReturn(NID_SCL, fileLength > 0, null, E_INVALID_ARG, "[%s] Invalid argument is used.  The vCal file is empty.", GetErrorMessage(E_INVALID_ARG));
1672
1673         r = vCalFile.Seek(FILESEEKPOSITION_BEGIN, 0);
1674         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1675         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1676
1677         std::unique_ptr<ByteBuffer> pBuffer(new (std::nothrow) ByteBuffer());
1678         SysTryReturn(NID_SCL, pBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1679         r = pBuffer->Construct(fileLength);
1680         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1681
1682         r = vCalFile.Read(*pBuffer);
1683         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1684         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1685
1686         _CalendarConnector connector;
1687         r = connector.GetResult();
1688         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1689
1690         pBuffer->Rewind();
1691         calendar_list_h calendarListHandle = null;
1692         int errorCode = calendar_vcalendar_parse_to_calendar(reinterpret_cast<char*>(const_cast<byte*>(pBuffer->GetPointer())), &calendarListHandle);
1693         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1694         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.  The vCal file is invalid.", GetErrorMessage(E_INVALID_ARG));
1695         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE || errorCode == CALENDAR_ERROR_NO_DATA, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1696         _CalendarList calendarList(calendarListHandle);
1697
1698         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1699         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1700         r = pList->Construct();
1701         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1702
1703         int count = 0;
1704         calendar_list_get_count(calendarListHandle, &count);
1705
1706         calendar_list_first(calendarListHandle);
1707         for (int i = 0; i < count; i++)
1708         {
1709                 calendar_record_h tmpRecordHandle = null;
1710                 calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1711                 calendar_list_next(calendarListHandle);
1712
1713                 char* pUri = null;
1714                 calendar_record_get_uri_p(tmpRecordHandle, &pUri);
1715
1716                 if (strcmp(pUri, _calendar_todo._uri) == 0)
1717                 {
1718                         std::unique_ptr<CalTodo> pTmpTodo(new (std::nothrow) CalTodo());
1719                         SysTryReturn(NID_SCL, pTmpTodo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1720
1721                         r = pList->Add(pTmpTodo.get());
1722                         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1723                         pTmpTodo.release();
1724                 }
1725                 else
1726                 {
1727                         calendar_list_remove(calendarListHandle, tmpRecordHandle);
1728                         calendar_record_destroy(tmpRecordHandle, true);
1729                 }
1730         }
1731
1732         calendar_list_first(calendarListHandle);
1733         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
1734         while (pEnum->MoveNext() == E_SUCCESS)
1735         {
1736                 CalTodo* pTmpTodo = static_cast<CalTodo*>(pEnum->GetCurrent());
1737
1738                 calendar_record_h tmpRecordHandle = null;
1739                 calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
1740                 _CalTodoImpl::GetInstance(*pTmpTodo)->SetRecordHandle(tmpRecordHandle);
1741
1742                 calendar_list_next(calendarListHandle);
1743         }
1744
1745         calendar_list_destroy(calendarList.ReleaseHandle(), false);
1746
1747         return pList.release();
1748 }
1749
1750 result
1751 _CalendarbookImpl::ExportEventsToVcalendar(const IList& eventList, const String& vCalFilePath)
1752 {
1753         SysTryReturnResult(NID_SCL, !File::IsFileExist(vCalFilePath), E_FILE_ALREADY_EXIST, "The vCalendar file already exists.");
1754         result r = GetLastResult();
1755         SysTryReturn(NID_SCL, r != E_INVALID_ARG && r != E_ILLEGAL_ACCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1756
1757         _CalendarConnector connector;
1758         r = connector.GetResult();
1759         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1760
1761         calendar_list_h calendarListHandle = null;
1762         int errorCode = calendar_list_create(&calendarListHandle);
1763         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1764         _CalendarList calendarList(calendarListHandle, false);
1765
1766         std::unique_ptr<IEnumerator> pEnum(eventList.GetEnumeratorN());
1767         while (pEnum->MoveNext() == E_SUCCESS)
1768         {
1769                 Record* pTmpRecord = static_cast<Record*>(pEnum->GetCurrent());
1770                 SysTryReturnResult(NID_SCL, pTmpRecord->GetRecordType() == RECORD_TYPE_EVENT, E_INVALID_ARG, "Invalid argument is used. The eventList contains invalid record.");
1771
1772                 CalEvent* pTmpEvent = static_cast<CalEvent*>(pTmpRecord);
1773                 errorCode = calendar_list_add(calendarListHandle, _CalEventImpl::GetInstance(*pTmpEvent)->GetRecordHandle());
1774                 SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1775         }
1776
1777         char* pVCalBuffer = null;
1778         errorCode = calendar_vcalendar_make_from_records(calendarListHandle, &pVCalBuffer);
1779         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "Memory allocation failed.");
1780         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
1781         std::unique_ptr<char[]> pVCal(pVCalBuffer);
1782
1783         File vCalFile;
1784         r = vCalFile.Construct(vCalFilePath, L"w");
1785         SysTryReturn(NID_SCL, r != E_INVALID_ARG && r != E_ILLEGAL_ACCESS && r != E_STORAGE_FULL && r != E_FILE_NOT_FOUND && r != E_OUT_OF_MEMORY
1786                         , r, r, "[%s] Propagating.", GetErrorMessage(r));
1787         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1788
1789         int pVCalBufferLength = strlen(pVCalBuffer);
1790         r = vCalFile.Write(static_cast<void*>(pVCalBuffer), pVCalBufferLength);
1791         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS && r != E_STORAGE_FULL, r, r, "[%s] Propagating.", GetErrorMessage(r));
1792         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1793
1794         return E_SUCCESS;
1795 }
1796
1797 result
1798 _CalendarbookImpl::ExportTodosToVcalendar(const IList& todoList, const String& vCalFilePath)
1799 {
1800         SysTryReturnResult(NID_SCL, !File::IsFileExist(vCalFilePath), E_FILE_ALREADY_EXIST, "The vCalendar file already exists.");
1801         result r = GetLastResult();
1802         SysTryReturn(NID_SCL, r != E_INVALID_ARG && r != E_ILLEGAL_ACCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
1803
1804         _CalendarConnector connector;
1805         r = connector.GetResult();
1806         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1807
1808         calendar_list_h calendarListHandle = null;
1809         int errorCode = calendar_list_create(&calendarListHandle);
1810         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1811         _CalendarList calendarList(calendarListHandle, false);
1812
1813         std::unique_ptr<IEnumerator> pEnum(todoList.GetEnumeratorN());
1814         while (pEnum->MoveNext() == E_SUCCESS)
1815         {
1816                 Record* pTmpRecord = static_cast<Record*>(pEnum->GetCurrent());
1817                 SysTryReturnResult(NID_SCL, pTmpRecord->GetRecordType() == RECORD_TYPE_TODO, E_INVALID_ARG, "Invalid argument is used. The todoList contains invalid record.");
1818
1819                 CalTodo* pTmpTodo = static_cast<CalTodo*>(pTmpRecord);
1820                 errorCode = calendar_list_add(calendarListHandle, _CalTodoImpl::GetInstance(*pTmpTodo)->GetRecordHandle());
1821                 SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
1822         }
1823
1824         char* pVCalBuffer = null;
1825         errorCode = calendar_vcalendar_make_from_records(calendarListHandle, &pVCalBuffer);
1826         SysTryReturnResult(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "Memory allocation failed.");
1827         SysTryReturnResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_SYSTEM, "A system error has been occurred.");
1828         std::unique_ptr<char[]> pVCal(pVCalBuffer);
1829
1830         File vCalFile;
1831         r = vCalFile.Construct(vCalFilePath, L"w");
1832         SysTryReturn(NID_SCL, r != E_INVALID_ARG && r != E_ILLEGAL_ACCESS && r != E_STORAGE_FULL && r != E_FILE_NOT_FOUND && r != E_OUT_OF_MEMORY
1833                         , r, r, "[%s] Propagating.", GetErrorMessage(r));
1834         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1835
1836         int pVCalBufferLength = strlen(pVCalBuffer);
1837         r = vCalFile.Write(static_cast<void*>(pVCalBuffer), pVCalBufferLength);
1838         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS && r != E_STORAGE_FULL, r, r, "[%s] Propagating.", GetErrorMessage(r));
1839         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1840
1841         return E_SUCCESS;
1842 }
1843
1844 DateTime
1845 _CalendarbookImpl::GetMaxDateTime(void)
1846 {
1847         DateTime maxDateTime;
1848         maxDateTime.SetValue(2100, 12, 31, 23, 59, 59);
1849
1850         return maxDateTime;
1851 }
1852
1853 DateTime
1854 _CalendarbookImpl::GetMinDateTime(void)
1855 {
1856         DateTime minDateTime;
1857         minDateTime.SetValue(1900, 1, 1, 0, 0, 0);
1858
1859         return minDateTime;
1860 }
1861
1862 IList*
1863 _CalendarbookImpl::GetEventInstancesCommonN(const DateTime& start, const DateTime& end,
1864                                                                          const Tizen::Locales::TimeZone& timeZone, int pageNo, int countPerPage, unsigned long category) const
1865 {
1866         ClearLastResult();
1867
1868         calendar_time_s convertedStartTime;
1869         calendar_time_s convertedEndTime;
1870         _CalendarbookUtil::ConvertDateTimeToCalTime(start, convertedStartTime);
1871         _CalendarbookUtil::ConvertDateTimeToCalTime(end, convertedEndTime);
1872
1873         Tizen::Locales::TimeZone tmpTimeZone(timeZone);
1874         DateTime localStartTime = tmpTimeZone.UtcTimeToWallTime(start);
1875         DateTime localEndTime = tmpTimeZone.UtcTimeToWallTime(end);
1876
1877         calendar_time_s convertedLocalStartTime;
1878         calendar_time_s convertedLocalEndTime;
1879         _CalendarbookUtil::ConvertDateToCalTime(localStartTime, convertedLocalStartTime);
1880         _CalendarbookUtil::ConvertDateToCalTime(localEndTime, convertedLocalEndTime);
1881
1882         std::unique_ptr<IList, AllElementsDeleter> pList;
1883
1884         if (category == EVENT_CATEGORY_ALL)
1885         {
1886                 pList.reset(GetEventInstancesOfAllCategoriesN(convertedStartTime, convertedEndTime, convertedLocalStartTime, convertedLocalEndTime, pageNo, countPerPage));
1887         }
1888         else
1889         {
1890                 pList.reset(GetEventInstancesOfCategoryN(convertedStartTime, convertedEndTime, convertedLocalStartTime, convertedLocalEndTime, pageNo, countPerPage, category));
1891         }
1892
1893         return pList.release();
1894 }
1895
1896 IList*
1897 _CalendarbookImpl::GetEventInstancesOfAllCategoriesN(const calendar_time_s& startTime, const calendar_time_s& endTime,
1898                 const calendar_time_s& localStartTime, const calendar_time_s& localEndTime, int pageNo, int countPerPage) const
1899 {
1900         ClearLastResult();
1901
1902         calendar_filter_h allDayEventInstanceMainFilterHandle = null;
1903         int errorCode = calendar_filter_create(_calendar_instance_allday_calendar_book._uri, &allDayEventInstanceMainFilterHandle);
1904         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1905         _CalendarFilter allDayEventInstanceMainFilter(allDayEventInstanceMainFilterHandle);
1906
1907         // Condition : End time of the item > start time of filter
1908         errorCode = calendar_filter_add_caltime(allDayEventInstanceMainFilterHandle, _calendar_instance_allday_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, localStartTime);
1909         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1910
1911         // Condition : Start time of the item < end time of filter
1912         errorCode = calendar_filter_add_operator(allDayEventInstanceMainFilterHandle, CALENDAR_FILTER_OPERATOR_AND);
1913         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1914         errorCode = calendar_filter_add_caltime(allDayEventInstanceMainFilterHandle, _calendar_instance_allday_calendar_book.start_time, CALENDAR_MATCH_LESS_THAN_OR_EQUAL, localEndTime);
1915         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1916
1917         // Create query
1918         calendar_query_h allDayEventInstanceQueryHandle = null;
1919         errorCode = calendar_query_create(_calendar_instance_allday_calendar_book._uri, &allDayEventInstanceQueryHandle);
1920         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1921         _CalendarQuery allDayEventInstanceQuery(allDayEventInstanceQueryHandle);
1922
1923         calendar_query_set_sort(allDayEventInstanceQueryHandle, _calendar_instance_allday_calendar_book.start_time, true);
1924         calendar_query_set_filter(allDayEventInstanceQueryHandle, allDayEventInstanceMainFilterHandle);
1925
1926         int startIndex = (pageNo - 1) * countPerPage;
1927
1928         calendar_list_h allDayEventInstanceListHandle = null;
1929         errorCode = calendar_db_get_records_with_query(allDayEventInstanceQueryHandle, startIndex, countPerPage, &allDayEventInstanceListHandle);
1930         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1931         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1932         _CalendarList allDayEventList(allDayEventInstanceListHandle);
1933
1934         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1935         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1936         result r = pList->Construct();
1937         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1938
1939         int allDayEventInstanceCount = 0;
1940         errorCode = calendar_db_get_count_with_query(allDayEventInstanceQueryHandle, &allDayEventInstanceCount);
1941         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1942
1943         int count = 0;
1944         calendar_list_get_count(allDayEventInstanceListHandle, &count);
1945
1946         int pageCount = allDayEventInstanceCount / countPerPage;
1947         int remainingCount = countPerPage - count;
1948
1949         calendar_list_first(allDayEventInstanceListHandle);
1950         for (int i = 0; i < count; i++)
1951         {
1952                 calendar_record_h tmpRecordHandle = null;
1953                 calendar_list_get_current_record_p(allDayEventInstanceListHandle, &tmpRecordHandle);
1954
1955                 std::unique_ptr<CalEvent> pTmpEventInstance(_CalendarbookImpl::ConvertAllDayEventInstanceHandleToCalEventN(tmpRecordHandle));
1956                 SysTryReturn(NID_SCL, pTmpEventInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1957
1958                 r = pList->Add(*pTmpEventInstance.release());
1959                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1960
1961                 calendar_list_next(allDayEventInstanceListHandle);
1962         }
1963
1964         if (remainingCount > 0)
1965         {
1966                 calendar_filter_h nonAllDayEventInstanceMainFilterHandle = null;
1967                 errorCode = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &nonAllDayEventInstanceMainFilterHandle);
1968                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1969                 _CalendarFilter nonAllDayEventInstanceMainFilter(nonAllDayEventInstanceMainFilterHandle);
1970
1971                 calendar_filter_h subFilterHandle = null;
1972                 errorCode = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &subFilterHandle);
1973                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1974                 _CalendarFilter subFilter(subFilterHandle);
1975                 subFilterHandle = null;
1976
1977                 // Condition : End time of the item > start time of filter
1978                 errorCode = calendar_filter_add_caltime(subFilter.GetHandle(), _calendar_instance_normal_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN, startTime);
1979                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1980
1981                 // Condition : Start time of the item < end time of filter
1982                 errorCode = calendar_filter_add_operator(subFilter.GetHandle(), CALENDAR_FILTER_OPERATOR_AND);
1983                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1984                 errorCode = calendar_filter_add_caltime(subFilter.GetHandle(), _calendar_instance_normal_calendar_book.start_time, CALENDAR_MATCH_LESS_THAN, endTime);
1985                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1986
1987                 errorCode = calendar_filter_add_filter(nonAllDayEventInstanceMainFilterHandle, subFilter.GetHandle());
1988                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1989
1990                 // Condition : Start time of the item = start time of filter AND Start time of the item = end time of filter
1991                 errorCode = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &subFilterHandle);
1992                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1993                 subFilter.ResetHandle(subFilterHandle);
1994                 subFilterHandle = null;
1995
1996                 errorCode = calendar_filter_add_caltime(subFilter.GetHandle(), _calendar_instance_normal_calendar_book.start_time, CALENDAR_MATCH_EQUAL, startTime);
1997                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1998                 errorCode = calendar_filter_add_operator(subFilter.GetHandle(), CALENDAR_FILTER_OPERATOR_AND);
1999                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2000                 errorCode = calendar_filter_add_caltime(subFilter.GetHandle(), _calendar_instance_normal_calendar_book.start_time, CALENDAR_MATCH_EQUAL, endTime);
2001                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2002
2003                 errorCode = calendar_filter_add_operator(nonAllDayEventInstanceMainFilterHandle, CALENDAR_FILTER_OPERATOR_OR);
2004                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2005                 errorCode = calendar_filter_add_filter(nonAllDayEventInstanceMainFilterHandle, subFilter.GetHandle());
2006                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2007
2008                 // Create query
2009                 calendar_query_h nonAllDayEventInstanceQueryHandle = null;
2010                 errorCode = calendar_query_create(_calendar_instance_normal_calendar_book._uri, &nonAllDayEventInstanceQueryHandle);
2011                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2012                 _CalendarQuery nonAllDayEventInstanceQuery(nonAllDayEventInstanceQueryHandle);
2013
2014                 calendar_query_set_sort(nonAllDayEventInstanceQueryHandle, _calendar_instance_normal_calendar_book.start_time, true);
2015                 calendar_query_set_filter(nonAllDayEventInstanceQueryHandle, nonAllDayEventInstanceMainFilterHandle);
2016
2017                 if (remainingCount == countPerPage)
2018                 {
2019                         startIndex = (((pageNo - pageCount) - 1) * countPerPage) - (allDayEventInstanceCount % countPerPage);
2020                 }
2021                 else
2022                 {
2023                         startIndex = 0;
2024                 }
2025
2026                 calendar_list_h nonAllDayEventInstanceListHandle = null;
2027                 errorCode = calendar_db_get_records_with_query(nonAllDayEventInstanceQueryHandle, startIndex, remainingCount, &nonAllDayEventInstanceListHandle);
2028                 SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2029                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2030                 _CalendarList nonAllDayEventList(nonAllDayEventInstanceListHandle);
2031
2032                 count = 0;
2033                 calendar_list_get_count(nonAllDayEventInstanceListHandle, &count);
2034                 calendar_list_first(nonAllDayEventInstanceListHandle);
2035                 for (int i = 0; i < count; i++)
2036                 {
2037                         calendar_record_h tmpRecordHandle = null;
2038                         calendar_list_get_current_record_p(nonAllDayEventInstanceListHandle, &tmpRecordHandle);
2039
2040                         std::unique_ptr<CalEvent> pTmpEventInstance(_CalendarbookImpl::ConvertNonAllDayEventInstanceHandleToCalEventN(tmpRecordHandle));
2041                         SysTryReturn(NID_SCL, pTmpEventInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2042
2043                         r = pList->Add(pTmpEventInstance.get());
2044                         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2045
2046                         pTmpEventInstance.release();
2047
2048                         calendar_list_next(nonAllDayEventInstanceListHandle);
2049                 }
2050         }
2051
2052         return pList.release();
2053 }
2054
2055 IList*
2056 _CalendarbookImpl::GetEventInstancesOfCategoryN(const calendar_time_s& startTime, const calendar_time_s& endTime,
2057                 const calendar_time_s& localStartTime, const calendar_time_s& localEndTime, int pageNo, int countPerPage, unsigned long category) const
2058 {
2059         ClearLastResult();
2060
2061         calendar_filter_h allDayEventInstanceMainFilterHandle = null;
2062         int errorCode = calendar_filter_create(_calendar_instance_allday_calendar_book._uri, &allDayEventInstanceMainFilterHandle);
2063         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2064         _CalendarFilter allDayEventInstanceMainFilter(allDayEventInstanceMainFilterHandle);
2065
2066         // Condition : End time of the item > start time of filter
2067         errorCode = calendar_filter_add_caltime(allDayEventInstanceMainFilterHandle, _calendar_instance_allday_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, localStartTime);
2068         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2069
2070         // Condition : Start time of the item < end time of filter
2071         errorCode = calendar_filter_add_operator(allDayEventInstanceMainFilterHandle, CALENDAR_FILTER_OPERATOR_AND);
2072         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2073         errorCode = calendar_filter_add_caltime(allDayEventInstanceMainFilterHandle, _calendar_instance_allday_calendar_book.start_time, CALENDAR_MATCH_LESS_THAN_OR_EQUAL, localEndTime);
2074         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2075
2076         // Create query
2077         calendar_query_h allDayEventInstanceQueryHandle = null;
2078         errorCode = calendar_query_create(_calendar_instance_allday_calendar_book._uri, &allDayEventInstanceQueryHandle);
2079         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2080         _CalendarQuery allDayEventInstanceQuery(allDayEventInstanceQueryHandle);
2081
2082         calendar_query_set_sort(allDayEventInstanceQueryHandle, _calendar_instance_allday_calendar_book.start_time, true);
2083         calendar_query_set_filter(allDayEventInstanceQueryHandle, allDayEventInstanceMainFilterHandle);
2084
2085         // Generate event : category map
2086         std::unique_ptr< HashMapT<int, int> > pEventCategoryMap(GenerateEventCategoryMapN());
2087         SysTryReturn(NID_SCL, pEventCategoryMap != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2088
2089         int startIndex = (pageNo - 1) * countPerPage;
2090
2091         calendar_list_h allDayEventInstanceListHandle = null;
2092         errorCode = calendar_db_get_records_with_query(allDayEventInstanceQueryHandle, 0, 0, &allDayEventInstanceListHandle);
2093         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2094         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2095         _CalendarList allDayEventList(allDayEventInstanceListHandle);
2096
2097         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2098         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2099         result r = pList->Construct();
2100         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
2101
2102         int remainingCount = countPerPage;
2103         int candidateCount = 0;
2104         int count = 0;
2105         calendar_list_get_count(allDayEventInstanceListHandle, &count);
2106         calendar_list_first(allDayEventInstanceListHandle);
2107         for (int i = 0; i < count && remainingCount > 0; i++)
2108         {
2109                 calendar_record_h tmpRecordHandle = null;
2110                 calendar_list_get_current_record_p(allDayEventInstanceListHandle, &tmpRecordHandle);
2111
2112                 int tmpCategory = (int) EVENT_CATEGORY_ALL;
2113                 int eventId = -1;
2114                 calendar_record_get_int(tmpRecordHandle, _calendar_instance_allday_calendar_book.event_id, &eventId);
2115                 pEventCategoryMap->GetValue(eventId, tmpCategory);
2116
2117                 if (tmpCategory == (int) category)
2118                 {
2119                         if (candidateCount >= startIndex)
2120                         {
2121                                 std::unique_ptr<CalEvent> pTmpEventInstance(_CalendarbookImpl::ConvertAllDayEventInstanceHandleToCalEventN(tmpRecordHandle));
2122                                 SysTryReturn(NID_SCL, pTmpEventInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2123
2124                                 r = pList->Add(pTmpEventInstance.get());
2125                                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2126                                 pTmpEventInstance.release();
2127
2128                                 remainingCount--;
2129                         }
2130
2131                         candidateCount++;
2132                 }
2133
2134                 calendar_list_next(allDayEventInstanceListHandle);
2135         }
2136
2137         if (remainingCount > 0)
2138         {
2139                 calendar_filter_h nonAllDayEventInstanceMainFilterHandle = null;
2140                 errorCode = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &nonAllDayEventInstanceMainFilterHandle);
2141                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2142                 _CalendarFilter nonAllDayEventInstanceMainFilter(nonAllDayEventInstanceMainFilterHandle);
2143
2144                 calendar_filter_h subFilterHandle = null;
2145                 errorCode = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &subFilterHandle);
2146                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2147                 _CalendarFilter subFilter(subFilterHandle);
2148                 subFilterHandle = null;
2149
2150                 // Condition : End time of the item > start time of filter
2151                 errorCode = calendar_filter_add_caltime(subFilter.GetHandle(), _calendar_instance_normal_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN, startTime);
2152                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2153
2154                 // Condition : Start time of the item < end time of filter
2155                 errorCode = calendar_filter_add_operator(subFilter.GetHandle(), CALENDAR_FILTER_OPERATOR_AND);
2156                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2157                 errorCode = calendar_filter_add_caltime(subFilter.GetHandle(), _calendar_instance_normal_calendar_book.start_time, CALENDAR_MATCH_LESS_THAN, endTime);
2158                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2159
2160                 errorCode = calendar_filter_add_filter(nonAllDayEventInstanceMainFilterHandle, subFilter.GetHandle());
2161                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2162
2163                 // Condition : Start time of the item = start time of filter AND Start time of the item = end time of filter
2164                 errorCode = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &subFilterHandle);
2165                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2166                 subFilter.ResetHandle(subFilterHandle);
2167                 subFilterHandle = null;
2168
2169                 errorCode = calendar_filter_add_caltime(subFilter.GetHandle(), _calendar_instance_normal_calendar_book.start_time, CALENDAR_MATCH_EQUAL, startTime);
2170                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2171                 errorCode = calendar_filter_add_operator(subFilter.GetHandle(), CALENDAR_FILTER_OPERATOR_AND);
2172                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2173                 errorCode = calendar_filter_add_caltime(subFilter.GetHandle(), _calendar_instance_normal_calendar_book.start_time, CALENDAR_MATCH_EQUAL, endTime);
2174                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2175
2176                 errorCode = calendar_filter_add_operator(nonAllDayEventInstanceMainFilterHandle, CALENDAR_FILTER_OPERATOR_OR);
2177                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2178                 errorCode = calendar_filter_add_filter(nonAllDayEventInstanceMainFilterHandle, subFilter.GetHandle());
2179                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2180
2181                 // Create query
2182                 calendar_query_h nonAllDayEventInstanceQueryHandle = null;
2183                 errorCode = calendar_query_create(_calendar_instance_normal_calendar_book._uri, &nonAllDayEventInstanceQueryHandle);
2184                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2185                 _CalendarQuery nonAllDayEventInstanceQuery(nonAllDayEventInstanceQueryHandle);
2186
2187                 calendar_query_set_sort(nonAllDayEventInstanceQueryHandle, _calendar_instance_normal_calendar_book.start_time, true);
2188                 calendar_query_set_filter(nonAllDayEventInstanceQueryHandle, nonAllDayEventInstanceMainFilterHandle);
2189
2190                 if (remainingCount == countPerPage)
2191                 {
2192                         int pageCount = candidateCount / countPerPage;
2193                         startIndex = (((pageNo - pageCount) - 1) * countPerPage) - (candidateCount % countPerPage);
2194                 }
2195                 else
2196                 {
2197                         startIndex = 0;
2198                 }
2199
2200                 calendar_list_h nonAllDayEventInstanceListHandle = null;
2201                 errorCode = calendar_db_get_records_with_query(nonAllDayEventInstanceQueryHandle, 0, 0, &nonAllDayEventInstanceListHandle);
2202                 SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2203                 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2204                 _CalendarList nonAllDayEventList(nonAllDayEventInstanceListHandle);
2205
2206                 candidateCount = 0;
2207                 count = 0;
2208
2209                 calendar_list_get_count(nonAllDayEventInstanceListHandle, &count);
2210                 calendar_list_first(nonAllDayEventInstanceListHandle);
2211                 for (int i = 0; i < count && remainingCount > 0; i++)
2212                 {
2213                         calendar_record_h tmpRecordHandle = null;
2214                         calendar_list_get_current_record_p(nonAllDayEventInstanceListHandle, &tmpRecordHandle);
2215
2216                         int tmpCategory = (int) EVENT_CATEGORY_ALL;
2217                         int eventId = -1;
2218                         calendar_record_get_int(tmpRecordHandle, _calendar_instance_normal_calendar_book.event_id, &eventId);
2219                         pEventCategoryMap->GetValue(eventId, tmpCategory);
2220
2221                         if (tmpCategory == (int) category)
2222                         {
2223                                 if (candidateCount >= startIndex)
2224                                 {
2225                                         std::unique_ptr<CalEvent> pTmpEventInstance(_CalendarbookImpl::ConvertNonAllDayEventInstanceHandleToCalEventN(tmpRecordHandle));
2226                                         SysTryReturn(NID_SCL, pTmpEventInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2227
2228                                         r = pList->Add(*pTmpEventInstance.release());
2229                                         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2230
2231                                         remainingCount--;
2232                                 }
2233
2234                                 candidateCount++;
2235                         }
2236
2237                         calendar_list_next(nonAllDayEventInstanceListHandle);
2238                 }
2239         }
2240
2241         return pList.release();
2242 }
2243
2244 HashMapT<int, int>*
2245 _CalendarbookImpl::GenerateEventCategoryMapN(void) const
2246 {
2247         result r = E_SUCCESS;
2248         int errorCode = CALENDAR_ERROR_NONE;
2249
2250         unsigned int projectionList[_NUMBER_OF_EVENT_CATEGORY_MAP_PROJECTION] = {_calendar_event.id, _calendar_event.categories};
2251
2252         // Create query
2253         calendar_query_h queryHandle = null;
2254         errorCode = calendar_query_create(_calendar_event._uri, &queryHandle);
2255         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2256         _CalendarQuery query(queryHandle);
2257
2258         calendar_query_set_projection(queryHandle, projectionList, _NUMBER_OF_EVENT_CATEGORY_MAP_PROJECTION);
2259         calendar_query_set_sort(queryHandle, _calendar_event.id, true);
2260
2261         calendar_list_h calendarListHandle = null;
2262         errorCode = calendar_db_get_records_with_query(queryHandle, 0, 0, &calendarListHandle);
2263         SysTryReturn(NID_SCL, errorCode != CALENDAR_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2264         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2265         _CalendarList calendarList(calendarListHandle);
2266
2267         std::unique_ptr< HashMapT<int, int> > pEventCategoryMap(new HashMapT<int, int>());
2268         SysTryReturn(NID_SCL, pEventCategoryMap != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2269         r = pEventCategoryMap->Construct();
2270         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2271
2272         int count = 0;
2273         calendar_list_get_count(calendarListHandle, &count);
2274         calendar_list_first(calendarListHandle);
2275         for (int i = 0; i < count; i++)
2276         {
2277                 calendar_record_h tmpRecordHandle = null;
2278                 calendar_list_get_current_record_p(calendarListHandle, &tmpRecordHandle);
2279
2280                 int eventId = -1;
2281                 char* pCategories = null;
2282
2283                 calendar_record_get_int(tmpRecordHandle, _calendar_event.id, &eventId);
2284                 calendar_record_get_str_p(tmpRecordHandle, _calendar_event.categories, &pCategories);
2285
2286                 int category = _CalendarbookUtil::ConvertCSCategoriesToEventCategory(pCategories);
2287
2288                 pEventCategoryMap->Add(eventId, category);
2289
2290                 calendar_list_next(calendarListHandle);
2291         }
2292
2293         return pEventCategoryMap.release();
2294 }
2295
2296 CalendarItemType
2297 _CalendarbookImpl::GetCalendarItemTypeByCalendarId(RecordId calendarId) const
2298 {
2299         CalendarItemType calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
2300
2301         calendar_record_h calendarHandle = null;
2302         int storeType = CALENDAR_BOOK_TYPE_NONE;
2303
2304         int errorCode = calendar_db_get_record(_calendar_book._uri, calendarId, &calendarHandle);
2305         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, CALENDAR_ITEM_TYPE_EVENT_AND_TODO, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2306
2307         calendar_record_get_int(calendarHandle, _calendar_book.store_type, &storeType);
2308
2309         if (storeType == (CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO))
2310         {
2311                 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
2312         }
2313         else if (storeType == CALENDAR_BOOK_TYPE_EVENT)
2314         {
2315                 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_ONLY;
2316         }
2317         else if (storeType == CALENDAR_BOOK_TYPE_TODO)
2318         {
2319                 calendarItemType = CALENDAR_ITEM_TYPE_TODO_ONLY;
2320         }
2321         else
2322         {
2323                 SetLastResult(E_SYSTEM);
2324                 SysLogException(NID_SCL, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2325                 calendar_record_destroy(calendarHandle, true);
2326                 return CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
2327         }
2328
2329         calendar_record_destroy(calendarHandle, true);
2330
2331         return calendarItemType;
2332 }
2333
2334 bool
2335 _CalendarbookImpl::CheckEventExistance(RecordId eventId) const
2336 {
2337         calendar_record_h eventHandle = null;
2338
2339         int errorCode = calendar_db_get_record(_calendar_event._uri, eventId, &eventHandle);
2340         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, false, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2341
2342         calendar_record_destroy(eventHandle, true);
2343
2344         return true;
2345 }
2346
2347 bool
2348 _CalendarbookImpl::CheckTodoExistance(RecordId todoId) const
2349 {
2350         calendar_record_h todoHandle = null;
2351
2352         int errorCode = calendar_db_get_record(_calendar_todo._uri, todoId, &todoHandle);
2353         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, false, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2354
2355         calendar_record_destroy(todoHandle, true);
2356
2357         return true;
2358 }
2359
2360 bool
2361 _CalendarbookImpl::CheckCalendarExistance(RecordId calendarId) const
2362 {
2363         calendar_record_h calendarHandle = null;
2364
2365         int errorCode = calendar_db_get_record(_calendar_book._uri, calendarId, &calendarHandle);
2366         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, false, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2367
2368         calendar_record_destroy(calendarHandle, true);
2369
2370         return true;
2371 }
2372
2373 bool
2374 _CalendarbookImpl::CheckValidTodoPriority(unsigned long priority) const
2375 {
2376         if ((priority & TODO_PRIORITY_LOW) || (priority & TODO_PRIORITY_NORMAL) || (priority & TODO_PRIORITY_HIGH))
2377         {
2378                 return true;
2379         }
2380
2381         return false;
2382 }
2383
2384 bool
2385 _CalendarbookImpl::CheckValidTodoStatus(unsigned long status) const
2386 {
2387         if ((status & TODO_STATUS_NONE) || (status & TODO_STATUS_NEEDS_ACTION) || (status & TODO_STATUS_COMPLETED)
2388                         || (status & TODO_STATUS_IN_PROCESS) || (status & TODO_STATUS_CANCELLED))
2389         {
2390                 return true;
2391         }
2392
2393         return false;
2394 }
2395
2396 int
2397 _CalendarbookImpl::ConvertTodoStatusToCalendarTodoStatus(unsigned long todoStatus) const
2398 {
2399         return todoStatus << 8;
2400 }
2401
2402 int
2403 _CalendarbookImpl::ConvertTodoPriorityToCalendarTodoPriority(unsigned long todoPriority) const
2404 {
2405         int calendarTodoPriority = CALENDAR_TODO_PRIORITY_NONE;
2406
2407         if (todoPriority == TODO_PRIORITY_LOW)
2408         {
2409                 calendarTodoPriority = CALENDAR_TODO_PRIORITY_LOW;
2410         }
2411         else if (todoPriority == TODO_PRIORITY_NORMAL)
2412         {
2413                 calendarTodoPriority = CALENDAR_TODO_PRIORITY_NORMAL;
2414         }
2415         else if (todoPriority == TODO_PRIORITY_HIGH)
2416         {
2417                 calendarTodoPriority = CALENDAR_TODO_PRIORITY_HIGH;
2418         }
2419
2420         return calendarTodoPriority;
2421 }
2422
2423 CalEvent*
2424 _CalendarbookImpl::ConvertAllDayEventInstanceHandleToCalEventN(calendar_record_h instanceHandle)
2425 {
2426         int originalEventDBId = _INVALID_EVENT_DB_ID;
2427         calendar_record_h eventHandle = null;
2428
2429         int errorCode = calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.event_id, &originalEventDBId);
2430         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. The instanceHandle is invalid record.");
2431
2432         std::unique_ptr<CalEvent> pEvent(new (std::nothrow) CalEvent());
2433         SysTryReturn(NID_SCL, pEvent != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2434         _CalEventImpl* pEventImpl = _CalEventImpl::GetInstance(*pEvent.get());
2435
2436         errorCode = calendar_db_get_record(_calendar_event._uri, originalEventDBId, &eventHandle);
2437         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2438
2439         calendar_time_s startCalendarTime;
2440         calendar_time_s endCalendarTime;
2441         calendar_record_get_caltime(instanceHandle, _calendar_instance_allday_calendar_book.start_time, &startCalendarTime);
2442         calendar_record_get_caltime(instanceHandle, _calendar_instance_allday_calendar_book.end_time, &endCalendarTime);
2443         calendar_record_set_caltime(eventHandle, _calendar_event.start_time, startCalendarTime);
2444         calendar_record_set_caltime(eventHandle, _calendar_event.end_time, endCalendarTime);
2445
2446         pEventImpl->SetOriginalCalEventId(originalEventDBId);
2447         pEventImpl->SetRecordHandle(eventHandle);
2448
2449         return pEvent.release();
2450 }
2451
2452 CalEvent*
2453 _CalendarbookImpl::ConvertNonAllDayEventInstanceHandleToCalEventN(calendar_record_h instanceHandle)
2454 {
2455         int originalEventDBId = _INVALID_EVENT_DB_ID;
2456         calendar_record_h eventHandle = null;
2457
2458         int errorCode = calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.event_id, &originalEventDBId);
2459         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_INVALID_ARG, "[%s] Invalid argument is used. The instanceHandle is invalid record.", GetErrorMessage(E_INVALID_ARG));
2460
2461         std::unique_ptr<CalEvent> pEvent(new (std::nothrow) CalEvent());
2462         SysTryReturn(NID_SCL, pEvent != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2463         _CalEventImpl* pEventImpl = _CalEventImpl::GetInstance(*pEvent.get());
2464
2465         errorCode = calendar_db_get_record(_calendar_event._uri, originalEventDBId, &eventHandle);
2466         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2467
2468         calendar_time_s startCalendarTime;
2469         calendar_time_s endCalendarTime;
2470         calendar_record_get_caltime(instanceHandle, _calendar_instance_normal_calendar_book.start_time, &startCalendarTime);
2471         calendar_record_get_caltime(instanceHandle, _calendar_instance_normal_calendar_book.end_time, &endCalendarTime);
2472         calendar_record_set_caltime(eventHandle, _calendar_event.start_time, startCalendarTime);
2473         calendar_record_set_caltime(eventHandle, _calendar_event.end_time, endCalendarTime);
2474
2475         pEventImpl->SetOriginalCalEventId(originalEventDBId);
2476         pEventImpl->SetRecordHandle(eventHandle);
2477
2478         return pEvent.release();
2479 }
2480
2481 CalEventInstance*
2482 _CalendarbookImpl::ConvertAllDayEventInstanceHandleToCalEventInstanceN(calendar_record_h instanceHandle)
2483 {
2484         ClearLastResult();
2485
2486         std::unique_ptr<CalEventInstance> pEventInstance(new (std::nothrow) CalEventInstance());
2487         SysTryReturn(NID_SCL, pEventInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2488         _CalEventInstanceImpl* pEventInstanceImpl = _CalEventInstanceImpl::GetInstance(*pEventInstance.get());
2489
2490         int originalEventDBId = _INVALID_EVENT_DB_ID;
2491         calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.event_id, &originalEventDBId);
2492         pEventInstanceImpl->SetOriginalEventId(originalEventDBId);
2493
2494         int calendarDBId = _INVALID_CALENDARBOOK_DB_ID;
2495         calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.calendar_book_id, &calendarDBId);
2496         pEventInstanceImpl->SetCalendarId(calendarDBId);
2497
2498         int srcEventBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
2499         calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.busy_status, &srcEventBusyStatus);
2500         BusyStatus busyStatus = BUSY_STATUS_FREE;
2501         switch (srcEventBusyStatus)
2502         {
2503         case CALENDAR_EVENT_BUSY_STATUS_FREE:
2504                 busyStatus = BUSY_STATUS_FREE;
2505                 break;
2506         case CALENDAR_EVENT_BUSY_STATUS_BUSY:
2507                 busyStatus = BUSY_STATUS_BUSY;
2508                 break;
2509         case CALENDAR_EVENT_BUSY_STATUS_UNAVAILABLE:
2510                 busyStatus = BUSY_STATUS_UNAVAILABLE;
2511                 break;
2512         case CALENDAR_EVENT_BUSY_STATUS_TENTATIVE:
2513                 busyStatus = BUSY_STATUS_TENTATIVE;
2514                 break;
2515         default :
2516                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. busy status = %d", GetErrorMessage(E_INVALID_ARG), srcEventBusyStatus);
2517                 busyStatus = BUSY_STATUS_FREE;
2518                 break;
2519         }
2520         pEventInstanceImpl->SetBusyStatus(busyStatus);
2521
2522         int srcEventStatus = CALENDAR_EVENT_STATUS_NONE;
2523         calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.event_status, &srcEventStatus);
2524         EventStatus eventStatus = EVENT_STATUS_NONE;
2525         switch (srcEventStatus)
2526         {
2527         case CALENDAR_EVENT_STATUS_NONE:
2528                 eventStatus = EVENT_STATUS_NONE;
2529                 break;
2530         case CALENDAR_EVENT_STATUS_TENTATIVE:
2531                 eventStatus = EVENT_STATUS_TENTATIVE;
2532                 break;
2533         case CALENDAR_EVENT_STATUS_CONFIRMED:
2534                 eventStatus = EVENT_STATUS_CONFIRMED;
2535                 break;
2536         case CALENDAR_EVENT_STATUS_CANCELLED:
2537                 eventStatus = EVENT_STATUS_CANCELLED;
2538                 break;
2539         default :
2540                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. status = %d", GetErrorMessage(E_INVALID_ARG), srcEventStatus);
2541                 eventStatus = EVENT_STATUS_NONE;
2542         }
2543         pEventInstanceImpl->SetStatus(eventStatus);
2544
2545         int srcPriority = CALENDAR_EVENT_PRIORITY_NORMAL;
2546         calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.priority, &srcPriority);
2547         EventPriority priority = EVENT_PRIORITY_NORMAL;
2548         switch (srcPriority)
2549         {
2550         case CALENDAR_EVENT_PRIORITY_LOW:
2551                 priority = EVENT_PRIORITY_LOW;
2552                 break;
2553         case CALENDAR_EVENT_PRIORITY_NONE:
2554                 // fall through
2555         case CALENDAR_EVENT_PRIORITY_NORMAL:
2556                 priority = EVENT_PRIORITY_NORMAL;
2557                 break;
2558         case CALENDAR_EVENT_PRIORITY_HIGH:
2559                 priority = EVENT_PRIORITY_HIGH;
2560                 break;
2561         default :
2562                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. priority = %d", GetErrorMessage(E_INVALID_ARG), srcPriority);
2563                 priority = EVENT_PRIORITY_NORMAL;
2564         }
2565         pEventInstanceImpl->SetPriority(priority);
2566
2567         int srcSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
2568         calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.sensitivity, &srcSensitivity);
2569         RecordSensitivity sensitivity = SENSITIVITY_PUBLIC;
2570         switch (srcSensitivity)
2571         {
2572         case CALENDAR_SENSITIVITY_PUBLIC:
2573                 sensitivity = SENSITIVITY_PUBLIC;
2574                 break;
2575         case CALENDAR_SENSITIVITY_PRIVATE:
2576                 sensitivity = SENSITIVITY_PRIVATE;
2577                 break;
2578         case CALENDAR_SENSITIVITY_CONFIDENTIAL:
2579                 sensitivity = SENSITIVITY_CONFIDENTIAL;
2580                 break;
2581         default :
2582                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. sensitivity = %d", GetErrorMessage(E_INVALID_ARG), srcSensitivity);
2583                 sensitivity = SENSITIVITY_PUBLIC;
2584         }
2585         pEventInstanceImpl->SetSensitivity(sensitivity);
2586
2587         calendar_time_s startCalendarTime;
2588         DateTime tmpStartTime;
2589         calendar_record_get_caltime(instanceHandle, _calendar_instance_allday_calendar_book.start_time, &startCalendarTime);
2590         tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
2591         pEventInstanceImpl->SetStartTime(tmpStartTime);
2592
2593         calendar_time_s endCalendarTime;
2594         DateTime tmpEndTime;
2595         calendar_record_get_caltime(instanceHandle, _calendar_instance_allday_calendar_book.end_time, &endCalendarTime);
2596         tmpEndTime.SetValue(endCalendarTime.time.date.year, endCalendarTime.time.date.month, endCalendarTime.time.date.mday);
2597         pEventInstanceImpl->SetEndTime(tmpEndTime);
2598
2599         pEventInstanceImpl->SetAllDayEvent(true);
2600
2601         int isRecurring = 0;
2602         calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.has_rrule, &isRecurring);
2603         pEventInstanceImpl->SetRecurring(isRecurring);
2604
2605         int hasReminder = 0;
2606         calendar_record_get_int(instanceHandle, _calendar_instance_allday_calendar_book.has_alarm, &hasReminder);
2607         pEventInstanceImpl->SetHasReminder(hasReminder);
2608
2609         char* pSubject = null;
2610         calendar_record_get_str_p(instanceHandle, _calendar_instance_allday_calendar_book.summary, &pSubject);
2611         pEventInstanceImpl->SetSubject(pSubject);
2612
2613         char* pDescription = null;
2614         calendar_record_get_str_p(instanceHandle, _calendar_instance_allday_calendar_book.description, &pDescription);
2615         pEventInstanceImpl->SetDescription(pDescription);
2616
2617         char* pLocation = null;
2618         calendar_record_get_str_p(instanceHandle, _calendar_instance_allday_calendar_book.location, &pLocation);
2619         pEventInstanceImpl->SetLocation(pLocation);
2620
2621         return pEventInstance.release();
2622 }
2623
2624 CalEventInstance*
2625 _CalendarbookImpl::ConvertNonAllDayEventInstanceHandleToCalEventInstanceN(calendar_record_h instanceHandle)
2626 {
2627         ClearLastResult();
2628
2629         std::unique_ptr<CalEventInstance> pEventInstance(new (std::nothrow) CalEventInstance());
2630         SysTryReturn(NID_SCL, pEventInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2631         _CalEventInstanceImpl* pEventInstanceImpl = _CalEventInstanceImpl::GetInstance(*pEventInstance.get());
2632
2633         int originalEventDBId = _INVALID_EVENT_DB_ID;
2634         calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.event_id, &originalEventDBId);
2635         pEventInstanceImpl->SetOriginalEventId(originalEventDBId);
2636
2637         int calendarDBId = _INVALID_CALENDARBOOK_DB_ID;
2638         calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.calendar_book_id, &calendarDBId);
2639         pEventInstanceImpl->SetCalendarId(calendarDBId);
2640
2641         int srcEventBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
2642         calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.busy_status, &srcEventBusyStatus);
2643         BusyStatus busyStatus = BUSY_STATUS_FREE;
2644         switch (srcEventBusyStatus)
2645         {
2646         case CALENDAR_EVENT_BUSY_STATUS_FREE:
2647                 busyStatus = BUSY_STATUS_FREE;
2648                 break;
2649         case CALENDAR_EVENT_BUSY_STATUS_BUSY:
2650                 busyStatus = BUSY_STATUS_BUSY;
2651                 break;
2652         case CALENDAR_EVENT_BUSY_STATUS_UNAVAILABLE:
2653                 busyStatus = BUSY_STATUS_UNAVAILABLE;
2654                 break;
2655         case CALENDAR_EVENT_BUSY_STATUS_TENTATIVE:
2656                 busyStatus = BUSY_STATUS_TENTATIVE;
2657                 break;
2658         default :
2659                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. busy status = %d", GetErrorMessage(E_INVALID_ARG), srcEventBusyStatus);
2660                 busyStatus = BUSY_STATUS_FREE;
2661                 break;
2662         }
2663         pEventInstanceImpl->SetBusyStatus(busyStatus);
2664
2665         int srcEventStatus = CALENDAR_EVENT_STATUS_NONE;
2666         calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.event_status, &srcEventStatus);
2667
2668         EventStatus eventStatus = EVENT_STATUS_NONE;
2669         switch (srcEventStatus)
2670         {
2671         case CALENDAR_EVENT_STATUS_NONE:
2672                 eventStatus = EVENT_STATUS_NONE;
2673                 break;
2674         case CALENDAR_EVENT_STATUS_TENTATIVE:
2675                 eventStatus = EVENT_STATUS_TENTATIVE;
2676                 break;
2677         case CALENDAR_EVENT_STATUS_CONFIRMED:
2678                 eventStatus = EVENT_STATUS_CONFIRMED;
2679                 break;
2680         case CALENDAR_EVENT_STATUS_CANCELLED:
2681                 eventStatus = EVENT_STATUS_CANCELLED;
2682                 break;
2683         default :
2684                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. status = %d", GetErrorMessage(E_INVALID_ARG), srcEventStatus);
2685                 eventStatus = EVENT_STATUS_NONE;
2686         }
2687         pEventInstanceImpl->SetStatus(eventStatus);
2688
2689         int srcPriority = CALENDAR_EVENT_PRIORITY_NORMAL;
2690         calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.priority, &srcPriority);
2691         EventPriority priority = EVENT_PRIORITY_NORMAL;
2692         switch (srcPriority)
2693         {
2694         case CALENDAR_EVENT_PRIORITY_LOW:
2695                 priority = EVENT_PRIORITY_LOW;
2696                 break;
2697         case CALENDAR_EVENT_PRIORITY_NONE:
2698                 // fall through
2699         case CALENDAR_EVENT_PRIORITY_NORMAL:
2700                 priority = EVENT_PRIORITY_NORMAL;
2701                 break;
2702         case CALENDAR_EVENT_PRIORITY_HIGH:
2703                 priority = EVENT_PRIORITY_HIGH;
2704                 break;
2705         default :
2706                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. priority = %d", GetErrorMessage(E_INVALID_ARG), srcPriority);
2707                 priority = EVENT_PRIORITY_NORMAL;
2708         }
2709         pEventInstanceImpl->SetPriority(priority);
2710
2711         int srcSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
2712         calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.sensitivity, &srcSensitivity);
2713         RecordSensitivity sensitivity = SENSITIVITY_PUBLIC;
2714         switch (srcSensitivity)
2715         {
2716         case CALENDAR_SENSITIVITY_PUBLIC:
2717                 sensitivity = SENSITIVITY_PUBLIC;
2718                 break;
2719         case CALENDAR_SENSITIVITY_PRIVATE:
2720                 sensitivity = SENSITIVITY_PRIVATE;
2721                 break;
2722         case CALENDAR_SENSITIVITY_CONFIDENTIAL:
2723                 sensitivity = SENSITIVITY_CONFIDENTIAL;
2724                 break;
2725         default :
2726                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. sensitivity = %d", GetErrorMessage(E_INVALID_ARG), srcSensitivity);
2727                 sensitivity = SENSITIVITY_PUBLIC;
2728         }
2729         pEventInstanceImpl->SetSensitivity(sensitivity);
2730
2731         calendar_time_s startCalendarTime;
2732         DateTime tmpStartTime;
2733         calendar_record_get_caltime(instanceHandle, _calendar_instance_normal_calendar_book.start_time, &startCalendarTime);
2734         tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
2735         pEventInstanceImpl->SetStartTime(tmpStartTime);
2736
2737         calendar_time_s endCalendarTime;
2738         DateTime tmpEndTime;
2739         calendar_record_get_caltime(instanceHandle, _calendar_instance_normal_calendar_book.end_time, &endCalendarTime);
2740         tmpEndTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(endCalendarTime.time.utime);
2741         pEventInstanceImpl->SetEndTime(tmpEndTime);
2742
2743         pEventInstanceImpl->SetAllDayEvent(false);
2744
2745         int isRecurring = 0;
2746         calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.has_rrule, &isRecurring);
2747         pEventInstanceImpl->SetRecurring(isRecurring);
2748
2749         int hasReminder = 0;
2750         calendar_record_get_int(instanceHandle, _calendar_instance_normal_calendar_book.has_alarm, &hasReminder);
2751         pEventInstanceImpl->SetHasReminder(hasReminder);
2752
2753         char* pSubject = null;
2754         calendar_record_get_str_p(instanceHandle, _calendar_instance_normal_calendar_book.summary, &pSubject);
2755         pEventInstanceImpl->SetSubject(pSubject);
2756
2757         char* pDescription = null;
2758         calendar_record_get_str_p(instanceHandle, _calendar_instance_normal_calendar_book.description, &pDescription);
2759         pEventInstanceImpl->SetDescription(pDescription);
2760
2761         char* pLocation = null;
2762         calendar_record_get_str_p(instanceHandle, _calendar_instance_normal_calendar_book.location, &pLocation);
2763         pEventInstanceImpl->SetLocation(pLocation);
2764
2765         return pEventInstance.release();
2766 }
2767
2768 CalEventChangeInfo*
2769 _CalendarbookImpl::ConvertModifiedEventToCalEventChangeInfoN(calendar_record_h modifiedEventHandle)
2770 {
2771         ClearLastResult();
2772
2773         std::unique_ptr<CalEventChangeInfo> pEventChangeInfo(new (std::nothrow) CalEventChangeInfo());
2774         SysTryReturn(NID_SCL, pEventChangeInfo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2775         _CalEventChangeInfoImpl* pEventChangeInfoImpl = _CalEventChangeInfoImpl::GetInstance(*pEventChangeInfo.get());
2776
2777         int eventId = 0;
2778         calendar_record_get_int(modifiedEventHandle, _calendar_updated_info.id, &eventId);
2779         pEventChangeInfoImpl->SetEventId(eventId);
2780
2781         int calendarbookId = 0;
2782         calendar_record_get_int(modifiedEventHandle, _calendar_updated_info.calendar_book_id, &calendarbookId);
2783         pEventChangeInfoImpl->SetCalendarId(calendarbookId);
2784
2785         int modifiedStatus = CALENDAR_RECORD_MODIFIED_STATUS_INSERTED;
2786         calendar_record_get_int(modifiedEventHandle, _calendar_updated_info.modified_status, &modifiedStatus);
2787         RecordChangeType recordChangeType = RECORD_CHANGE_TYPE_ADDED;
2788         switch (modifiedStatus)
2789         {
2790         case CALENDAR_RECORD_MODIFIED_STATUS_INSERTED:
2791                 recordChangeType = RECORD_CHANGE_TYPE_ADDED;
2792                 break;
2793         case CALENDAR_RECORD_MODIFIED_STATUS_UPDATED:
2794                 recordChangeType = RECORD_CHANGE_TYPE_UPDATED;
2795                 break;
2796         case CALENDAR_RECORD_MODIFIED_STATUS_DELETED:
2797                 recordChangeType = RECORD_CHANGE_TYPE_REMOVED;
2798                 break;
2799         default:
2800                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. modified status = %d", GetErrorMessage(E_INVALID_ARG), modifiedStatus);
2801                 return null;
2802         }
2803         pEventChangeInfoImpl->SetChangeType(recordChangeType);
2804
2805         int version = 0;
2806         calendar_record_get_int(modifiedEventHandle, _calendar_updated_info.version, &version);
2807         pEventChangeInfoImpl->SetVersion(version);
2808
2809         return pEventChangeInfo.release();
2810 }
2811
2812 CalTodoChangeInfo*
2813 _CalendarbookImpl::ConvertModifiedTodoToCalTodoChangeInfoN(calendar_record_h modifiedTodoHandle)
2814 {
2815         ClearLastResult();
2816
2817         std::unique_ptr<CalTodoChangeInfo> pTodoChangeInfo(new (std::nothrow) CalTodoChangeInfo());
2818         SysTryReturn(NID_SCL, pTodoChangeInfo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2819         _CalTodoChangeInfoImpl* pTodoChangeInfoImpl = _CalTodoChangeInfoImpl::GetInstance(*pTodoChangeInfo.get());
2820
2821         int todoId = 0;
2822         calendar_record_get_int(modifiedTodoHandle, _calendar_updated_info.id, &todoId);
2823         pTodoChangeInfoImpl->SetTodoId(todoId);
2824
2825         int calendarbookId = 0;
2826         calendar_record_get_int(modifiedTodoHandle, _calendar_updated_info.calendar_book_id, &calendarbookId);
2827         pTodoChangeInfoImpl->SetCalendarId(calendarbookId);
2828
2829         int modifiedStatus = CALENDAR_RECORD_MODIFIED_STATUS_INSERTED;
2830         calendar_record_get_int(modifiedTodoHandle, _calendar_updated_info.modified_status, &modifiedStatus);
2831         RecordChangeType recordChangeType = RECORD_CHANGE_TYPE_ADDED;
2832
2833         switch (modifiedStatus)
2834         {
2835         case    CALENDAR_RECORD_MODIFIED_STATUS_INSERTED:
2836                 recordChangeType = RECORD_CHANGE_TYPE_ADDED;
2837                 break;
2838         case CALENDAR_RECORD_MODIFIED_STATUS_UPDATED:
2839                 recordChangeType = RECORD_CHANGE_TYPE_UPDATED;
2840                 break;
2841         case CALENDAR_RECORD_MODIFIED_STATUS_DELETED:
2842                 recordChangeType = RECORD_CHANGE_TYPE_REMOVED;
2843                 break;
2844         default:
2845                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. modified status = %d", GetErrorMessage(E_INVALID_ARG), modifiedStatus);
2846                 return null;
2847         }
2848         pTodoChangeInfoImpl->SetChangeType(recordChangeType);
2849
2850         int version = 0;
2851         calendar_record_get_int(modifiedTodoHandle, _calendar_updated_info.version, &version);
2852         pTodoChangeInfoImpl->SetVersion(version);
2853
2854         return pTodoChangeInfo.release();
2855 }
2856
2857 template<typename RecordType, typename RecordTypeImpl, typename RecordView>
2858 IList*
2859 _CalendarbookImpl::ConvertRecordListN(calendar_list_h resultListHandle, RecordView recordView)
2860 {
2861         result r = E_SUCCESS;
2862
2863         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2864         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2865         r = pList->Construct();
2866         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
2867
2868         int count = 0;
2869         calendar_list_get_count(resultListHandle, &count);
2870
2871         for (int i = 0; i < count; i++)
2872         {
2873                 std::unique_ptr<RecordType> pTmpRecord(RecordTypeImpl::CreateDefaultInstanceN());
2874                 SysTryReturn(NID_SCL, pTmpRecord != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2875
2876                 r = pList->Add(pTmpRecord.get());
2877                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2878                 pTmpRecord.release();
2879         }
2880
2881         calendar_list_first(resultListHandle);
2882         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
2883         while (pEnum->MoveNext() == E_SUCCESS)
2884         {
2885                 RecordType* pTmpRecord = static_cast<RecordType*>(pEnum->GetCurrent());
2886
2887                 calendar_record_h tmpRecordHandle = null;
2888                 calendar_list_get_current_record_p(resultListHandle, &tmpRecordHandle);
2889                 RecordTypeImpl::GetInstance(*pTmpRecord)->SetRecordHandle(tmpRecordHandle);
2890
2891                 int dbIndex = -1;
2892                 calendar_record_get_int(tmpRecordHandle, recordView.id, &dbIndex);
2893                 _RecordImpl::GetInstance(*pTmpRecord)->SetRecordId(dbIndex);
2894
2895                 calendar_list_next(resultListHandle);
2896         }
2897
2898         return pList.release();
2899 }
2900
2901 IList*
2902 _CalendarbookImpl::ConvertEventInstanceListN(calendar_list_h resultListHandle, bool isAllDay)
2903 {
2904         result r = E_SUCCESS;
2905
2906         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2907         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2908         r = pList->Construct();
2909         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
2910
2911         int count = 0;
2912         calendar_list_get_count(resultListHandle, &count);
2913         calendar_list_first(resultListHandle);
2914         for (int i = 0; i < count; i++)
2915         {
2916                 calendar_record_h tmpRecordHandle = null;
2917                 calendar_list_get_current_record_p(resultListHandle, &tmpRecordHandle);
2918
2919                 std::unique_ptr<CalEventInstance> pTmpEventInstance;
2920
2921                 if (isAllDay)
2922                 {
2923                         pTmpEventInstance.reset(_CalendarbookImpl::ConvertAllDayEventInstanceHandleToCalEventInstanceN(tmpRecordHandle));
2924                         SysTryReturn(NID_SCL, pTmpEventInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2925                 }
2926                 else
2927                 {
2928                         pTmpEventInstance.reset(_CalendarbookImpl::ConvertNonAllDayEventInstanceHandleToCalEventInstanceN(tmpRecordHandle));
2929                         SysTryReturn(NID_SCL, pTmpEventInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2930                 }
2931
2932                 r = pList->Add(pTmpEventInstance.get());
2933                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2934                 pTmpEventInstance.release();
2935
2936                 calendar_list_next(resultListHandle);
2937         }
2938
2939         return pList.release();
2940 }
2941
2942 void
2943 _CalendarbookImpl::OnCalEventChanged(void)
2944 {
2945         if (__pICalendarbookEventListener == null && __pIRecordEventListener == null)
2946         {
2947                 return;
2948         }
2949
2950         _CalendarConnector connector;
2951         result r = connector.GetResult();
2952         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2953
2954         std::unique_ptr<IList, AllElementsDeleter> pChangedEventList(GetChangedEventsAfterN(__dbVersionForEvent, __dbVersionForEvent));
2955         SysTryReturnVoidResult(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2956         SysTryReturnVoidResult(NID_SCL, GetLastResult() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2957
2958         if (pChangedEventList->GetCount() > 0)
2959         {
2960                 if (__pICalendarbookEventListener != null)
2961                 {
2962                         __pICalendarbookEventListener->OnCalendarEventsChanged(*pChangedEventList.get());
2963                 }
2964                 else
2965                 {
2966                         RecordEventType recordEventType = RECORD_ADDED;
2967                         IEnumerator* pEnum = pChangedEventList->GetEnumeratorN();
2968                         CalEvent* pEvent = null;
2969
2970                         while (pEnum->MoveNext() == E_SUCCESS)
2971                         {
2972                                 CalEventChangeInfo* pEventChagneInfo = static_cast<CalEventChangeInfo*>(pEnum->GetCurrent());
2973
2974                                 if (pEventChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
2975                                 {
2976                                         recordEventType = RECORD_ADDED;
2977                                         pEvent = GetEventN(pEventChagneInfo->GetEventId());
2978                                         if (pEvent == null)
2979                                         {
2980                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2981                                                 {
2982                                                         continue;
2983                                                 }
2984                                                 else
2985                                                 {
2986                                                         break;
2987                                                 }
2988                                         }
2989                                 }
2990                                 else if (pEventChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
2991                                 {
2992                                         recordEventType = RECORD_UPDATED;
2993                                         pEvent = GetEventN(pEventChagneInfo->GetEventId());
2994                                         if (pEvent == null)
2995                                         {
2996                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2997                                                 {
2998                                                         continue;
2999                                                 }
3000                                                 else
3001                                                 {
3002                                                         break;
3003                                                 }
3004                                         }
3005                                 }
3006                                 else
3007                                 {
3008                                         recordEventType = RECORD_REMOVED;
3009                                         pEvent = new (std::nothrow) CalEvent();
3010                                         if (pEvent == null)
3011                                         {
3012                                                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3013                                                 break;
3014                                         }
3015
3016                                         _RecordImpl::GetInstance(*pEvent)->SetRecordId(pEventChagneInfo->GetEventId());
3017                                 }
3018
3019                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_EVENT, *pEvent, null, null);
3020
3021                                 delete pEvent;
3022                         }
3023
3024                         delete pEnum;
3025                 }
3026         }
3027 }
3028
3029 void
3030 _CalendarbookImpl::OnCalTodoChanged(void)
3031 {
3032         if (__pICalendarbookEventListener == null && __pIRecordEventListener == null)
3033         {
3034                 return;
3035         }
3036
3037         _CalendarConnector connector;
3038         result r = connector.GetResult();
3039         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
3040
3041         std::unique_ptr<IList, AllElementsDeleter> pChangedTodoList(GetChangedTodosAfterN(__dbVersionForTodo, __dbVersionForTodo));
3042         SysTryReturnVoidResult(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3043         SysTryReturnVoidResult(NID_SCL, GetLastResult() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
3044
3045         if (pChangedTodoList->GetCount() > 0)
3046         {
3047                 if (__pICalendarbookEventListener != null)
3048                 {
3049                         __pICalendarbookEventListener->OnCalendarTodosChanged(*pChangedTodoList.get());
3050                 }
3051                 else
3052                 {
3053                         RecordEventType recordEventType = RECORD_ADDED;
3054                         IEnumerator* pEnum = pChangedTodoList->GetEnumeratorN();
3055                         CalTodo* pTodo = null;
3056
3057                         while (pEnum->MoveNext() == E_SUCCESS)
3058                         {
3059                                 CalTodoChangeInfo* pTodoChagneInfo = static_cast<CalTodoChangeInfo*>(pEnum->GetCurrent());
3060
3061                                 if (pTodoChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
3062                                 {
3063                                         recordEventType = RECORD_ADDED;
3064                                         pTodo = GetTodoN(pTodoChagneInfo->GetTodoId());
3065                                         if (pTodo == null)
3066                                         {
3067                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
3068                                                 {
3069                                                         continue;
3070                                                 }
3071                                                 else
3072                                                 {
3073                                                         break;
3074                                                 }
3075                                         }
3076                                 }
3077                                 else if (pTodoChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
3078                                 {
3079                                         recordEventType = RECORD_UPDATED;
3080                                         pTodo = GetTodoN(pTodoChagneInfo->GetTodoId());
3081                                         if (pTodo == null)
3082                                         {
3083                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
3084                                                 {
3085                                                         continue;
3086                                                 }
3087                                                 else
3088                                                 {
3089                                                         break;
3090                                                 }
3091                                         }
3092                                 }
3093                                 else
3094                                 {
3095                                         recordEventType = RECORD_REMOVED;
3096                                         pTodo = new (std::nothrow) CalTodo();
3097                                         if (pTodo == null)
3098                                         {
3099                                                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3100                                                 break;
3101                                         }
3102
3103                                         _RecordImpl::GetInstance(*pTodo)->SetRecordId(pTodoChagneInfo->GetTodoId());
3104                                 }
3105
3106                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_TODO, *pTodo, null, null);
3107
3108                                 delete pTodo;
3109                         }
3110
3111                         delete pEnum;
3112                 }
3113         }
3114 }
3115
3116 _CalendarbookImpl*
3117 _CalendarbookImpl::GetInstance(Calendarbook& calendarbook)
3118 {
3119         return calendarbook.__pCalendarbookImpl;
3120 }
3121
3122 const _CalendarbookImpl*
3123 _CalendarbookImpl::GetInstance(const Calendarbook& calendarbook)
3124 {
3125         return calendarbook.__pCalendarbookImpl;
3126 }
3127
3128 }}      //OSP::Social