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