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