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