Merge "Export internal APIs needed by samsung-socil" into tizen_2.1
[platform/framework/native/social.git] / src / FScl_CalendarbookUtil.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 /**
17  * @file                FScl_CalendarbookUtil.cpp
18  * @brief               This is the implementation for _CalendarbookUtil class.
19  *
20  * This file contains definitions of @e _CalendarbookUtil class.
21  */
22
23 #include <new>
24 #include <string.h>
25 #include <unique_ptr.h>
26 #include <FBaseResult.h>
27 #include <FBaseDateTime.h>
28 #include <FBaseColIList.h>
29 #include <FBaseUtilStringUtil.h>
30 #include <FLclTimeZone.h>
31 #include <FLclGregorianCalendar.h>
32 #include <FSclReminder.h>
33 #include <FBaseSysLog.h>
34 #include <FBase_StringConverter.h>
35 #include <FApp_AppInfo.h>
36 #include "FScl_CalendarbookImpl.h"
37 #include "FScl_CalendarbookUtil.h"
38
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Base::Collection;
42
43 namespace Tizen { namespace Social
44 {
45
46 static const byte _CALENDARBOOK_BYTE_VALUE_9 = 0x09;
47 static const int CALENDARBOOK_DEC_ASCII_MASK = 0x30;
48 static const int CALENDARBOOK_CHAR_ASCII_DIFF = 55;
49
50 static const int _CALENDARBOOK_MSEC_UNIT = 1000;
51 static const long long _CALENDARBOOK_EPOCH_YEAR_INTERVAL_SECONDS = 62135596800;
52
53 static const wchar_t* _EVENT_CATEGORY_DELIMITER = L",";
54 static const wchar_t* _EVENT_CATEGORY_APPOINTMENT_STRING = L"Appointment";
55 static const wchar_t* _EVENT_CATEGORY_ANNIVERSARY_STRING = L"Anniversary";
56
57 static const wchar_t _CALENDARBOOK_UTC_SUFFIX = L'Z';
58 static const wchar_t _CALENDARBOOK_TIME_PREFIX = L'T';
59 static const wchar_t* _CALENDARBOOK_UTC_TZID = L"TZID=";
60 static const wchar_t _CALENDARBOOK_DATE_TIME_DELIMITER = L':';
61 static const int _CALENDARBOOK_TZID_INDEX = 5;
62 static const int _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH = 16;  // ex :20120629T193000Z
63 static const int _CALENDARBOOK_DATE_VALUE_STRING_LENGTH = 8;    // ex :20120629
64 static const int _CALENDARBOOK_TIME_VALUE_STRING_LENGTH = 6;    // ex :193000
65 static const int _CALENDARBOOK_DATE_YEAR_STRING_LENGTH = 4;
66 static const int _CALENDARBOOK_DATE_MONTH_STRING_LENGTH = 2;
67 static const int _CALENDARBOOK_DATE_DAY_STRING_LENGTH = 2;
68 static const int _CALENDARBOOK_TIME_HOUR_STRING_LENGTH = 2;
69 static const int _CALENDARBOOK_TIME_MINUTE_STRING_LENGTH = 2;
70 static const int _CALENDARBOOK_TIME_SECOND_STRING_LENGTH = 2;
71 static const wchar_t* _CALENDARBOOK_ZERO_STRING = L"0";
72
73 long long int
74 _CalendarbookUtil::ConvertDateTimeToEpochTime(const DateTime& dateTime)
75 {
76         return (dateTime.GetTime().GetTicks() / _CALENDARBOOK_MSEC_UNIT) - _CALENDARBOOK_EPOCH_YEAR_INTERVAL_SECONDS;
77 }
78
79 DateTime
80 _CalendarbookUtil::ConvertEpochTimeToDateTime(long long int dateTime)
81 {
82         DateTime tmpDateTime;
83         tmpDateTime.SetValue(TimeSpan((dateTime + _CALENDARBOOK_EPOCH_YEAR_INTERVAL_SECONDS) * _CALENDARBOOK_MSEC_UNIT));
84         return tmpDateTime;
85 }
86
87 char*
88 _CalendarbookUtil::ConvertByteBufferToCharArrayN(const ByteBuffer& byteBuffer)
89 {
90         int byteBufferLength = byteBuffer.GetCapacity();
91         char* pCharArray = new (std::nothrow) char[byteBufferLength * 2 + 1];
92         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
93
94         const byte* pByteBufferPtr = byteBuffer.GetPointer();
95
96         byte tmpByte = 0;
97
98         for (int i = 0; i < byteBufferLength; i++)
99         {
100                 tmpByte = pByteBufferPtr[i] >> 4;
101
102                 if (tmpByte <= _CALENDARBOOK_BYTE_VALUE_9)
103                 {
104                         pCharArray[i * 2] = tmpByte | CALENDARBOOK_DEC_ASCII_MASK;
105                 }
106                 else
107                 {
108                         pCharArray[i * 2] = tmpByte + CALENDARBOOK_CHAR_ASCII_DIFF;
109                 }
110
111                 tmpByte = pByteBufferPtr[i] & 0x0F;
112
113                 if (tmpByte <= _CALENDARBOOK_BYTE_VALUE_9)
114                 {
115                         pCharArray[i * 2 + 1] = tmpByte | CALENDARBOOK_DEC_ASCII_MASK;
116                 }
117                 else
118                 {
119                         pCharArray[i * 2 + 1] = tmpByte + CALENDARBOOK_CHAR_ASCII_DIFF;
120                 }
121         }
122
123         pCharArray[byteBufferLength * 2] = 0;
124
125         return pCharArray;
126 }
127
128 ByteBuffer*
129 _CalendarbookUtil::ConvertCharArrayToByteBufferN(const char* pCharArray)
130 {
131         int charArrayLength = strlen(pCharArray);
132         SysTryReturn(NID_SCL, charArrayLength > 0, null, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
133
134         int byteBufferLength = 0;
135         std::unique_ptr<byte[]> pByteArray(new (std::nothrow) byte[charArrayLength/2]);
136         SysTryReturn(NID_SCL, pByteArray != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
137
138         bool inProgress = false;
139         char tmpChar = 0;
140         byte tmpByte = 0;
141
142         for (int i = 0; i < charArrayLength; i++)
143         {
144                 tmpChar = pCharArray[i];
145
146                 if (tmpChar >= '0' && tmpChar <= '9')
147                 {
148                         tmpChar &= 0x0F;
149                 }
150                 else if (tmpChar >= 'A' && tmpChar <= 'F')
151                 {
152                         tmpChar -= CALENDARBOOK_CHAR_ASCII_DIFF;
153                 }
154                 else
155                 {
156                         continue;
157                 }
158
159                 if (!inProgress)
160                 {
161                         tmpByte = tmpChar << 4;
162                         inProgress = true;
163                 }
164                 else
165                 {
166                         pByteArray[byteBufferLength] = tmpByte | tmpChar;
167                         byteBufferLength++;
168                         tmpByte = 0;
169                         inProgress = false;
170                 }
171         }
172
173         result r = E_SUCCESS;
174         std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer());
175         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
176         r = pByteBuffer->Construct(byteBufferLength);
177         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
178         r = pByteBuffer->SetArray(pByteArray.get(), 0, byteBufferLength);
179         pByteBuffer->Rewind();
180
181         return pByteBuffer.release();
182 }
183
184 result
185 _CalendarbookUtil::ConvertEventAlarmsToReminderList(calendar_record_h calendarRecordHandle, ArrayList& reminderList)
186 {
187         int errorCode = CALENDAR_ERROR_NONE;
188
189         unsigned int reminderCount = 0;
190         errorCode = calendar_record_get_child_record_count(calendarRecordHandle, _calendar_event.calendar_alarm, &reminderCount);
191
192         if (reminderCount == 0)
193         {
194                 reminderList.RemoveAll(true);
195                 return E_SUCCESS;
196         }
197
198         for (int i = 0; i< reminderCount; i++)
199         {
200                 calendar_record_h tmpAlarmHandle = null;
201                 int tmpAlarmTickUnit = CALENDAR_ALARM_NONE;
202                 int tmpAlarmTick = 0;
203                 long long tmpAlarmTime = 0;
204                 char* pTmpAlarmTone = null;
205
206                 errorCode = calendar_record_get_child_record_at_p(calendarRecordHandle, _calendar_event.calendar_alarm, i, &tmpAlarmHandle);
207                 errorCode = calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick_unit, &tmpAlarmTickUnit);
208                 errorCode = calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick, &tmpAlarmTick);
209                 errorCode = calendar_record_get_lli(tmpAlarmHandle, _calendar_alarm.time, &tmpAlarmTime);
210                 errorCode = calendar_record_get_str_p(tmpAlarmHandle, _calendar_alarm.tone, &pTmpAlarmTone);
211
212                 std::unique_ptr<Reminder> pReminder(new (std::nothrow) Reminder());
213                 SysTryReturnResult(NID_SCL, pReminder != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
214
215                 pReminder->SetSoundFile(pTmpAlarmTone);
216
217                 switch (tmpAlarmTickUnit)
218                 {
219                 case CALENDAR_ALARM_TIME_UNIT_MINUTE:
220                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_MINUTE, tmpAlarmTick);
221                         break;
222                 case CALENDAR_ALARM_TIME_UNIT_HOUR:
223                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_HOUR, tmpAlarmTick);
224                         break;
225                 case CALENDAR_ALARM_TIME_UNIT_DAY:
226                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick);
227                         break;
228                 case CALENDAR_ALARM_TIME_UNIT_WEEK:
229                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_WEEK, tmpAlarmTick);
230                         break;
231                 case CALENDAR_ALARM_TIME_UNIT_MONTH:
232                         {
233                                 calendar_time_s startCalendarTime;
234                                 DateTime tmpStartTime;
235
236                                 errorCode = calendar_record_get_caltime(calendarRecordHandle, _calendar_event.start_time, &startCalendarTime);
237                                 if (startCalendarTime.type == CALENDAR_TIME_UTIME)
238                                 {
239                                         tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
240                                 }
241                                 else
242                                 {
243                                         tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
244                                 }
245
246                                 tmpStartTime.AddMonths(-1);
247                                 int maxDays = GetMaxDaysOfMonth(tmpStartTime.GetYear(), tmpStartTime.GetMonth());
248
249                                 pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick * maxDays);
250                         }
251                         break;
252                 case CALENDAR_ALARM_TIME_UNIT_SPECIFIC:
253                         if (Tizen::App::_AppInfo::GetApiVersion() > _API_VERSION_2_0)
254                         {
255                                 pReminder->SetAbsoluteTime(_CalendarbookUtil::ConvertEpochTimeToDateTime(tmpAlarmTime));
256                         }
257                         break;
258                 default :
259                         break;
260                 }
261
262                 reminderList.Add(*pReminder.release());
263         }
264
265         return E_SUCCESS;
266 }
267
268 result
269 _CalendarbookUtil::ConvertTodoAlarmsToReminderList(calendar_record_h calendarRecordHandle, ArrayList& reminderList)
270 {
271         int errorCode = CALENDAR_ERROR_NONE;
272
273         unsigned int reminderCount = 0;
274         errorCode = calendar_record_get_child_record_count(calendarRecordHandle, _calendar_todo.calendar_alarm, &reminderCount);
275
276         if (reminderCount == 0)
277         {
278                 reminderList.RemoveAll(true);
279                 return E_SUCCESS;
280         }
281
282         for (int i = 0; i< reminderCount; i++)
283         {
284                 calendar_record_h tmpAlarmHandle = null;
285                 int tmpAlarmTickUnit = CALENDAR_ALARM_NONE;
286                 int tmpAlarmTick = 0;
287                 long long tmpAlarmTime = 0;
288                 char* pTmpAlarmTone = null;
289
290                 errorCode = calendar_record_get_child_record_at_p(calendarRecordHandle, _calendar_todo.calendar_alarm, i, &tmpAlarmHandle);
291                 errorCode = calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick_unit, &tmpAlarmTickUnit);
292                 errorCode = calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick, &tmpAlarmTick);
293                 errorCode = calendar_record_get_lli(tmpAlarmHandle, _calendar_alarm.time, &tmpAlarmTime);
294                 errorCode = calendar_record_get_str_p(tmpAlarmHandle, _calendar_alarm.tone, &pTmpAlarmTone);
295
296                 std::unique_ptr<Reminder> pReminder(new (std::nothrow) Reminder());
297                 SysTryReturnResult(NID_SCL, pReminder != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
298
299                 pReminder->SetSoundFile(pTmpAlarmTone);
300
301                 switch (tmpAlarmTickUnit)
302                 {
303                 case CALENDAR_ALARM_TIME_UNIT_MINUTE:
304                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_MINUTE, tmpAlarmTick);
305                         break;
306                 case CALENDAR_ALARM_TIME_UNIT_HOUR:
307                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_HOUR, tmpAlarmTick);
308                         break;
309                 case CALENDAR_ALARM_TIME_UNIT_DAY:
310                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick);
311                         break;
312                 case CALENDAR_ALARM_TIME_UNIT_WEEK:
313                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_WEEK, tmpAlarmTick);
314                         break;
315                 case CALENDAR_ALARM_TIME_UNIT_MONTH:
316                         {
317                                 calendar_time_s startCalendarTime;
318                                 DateTime tmpStartTime;
319
320                                 errorCode = calendar_record_get_caltime(calendarRecordHandle, _calendar_todo.start_time, &startCalendarTime);
321                                 if (startCalendarTime.type == CALENDAR_TIME_UTIME)
322                                 {
323                                         tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
324                                 }
325                                 else
326                                 {
327                                         tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
328                                 }
329
330                                 tmpStartTime.AddMonths(-1);
331                                 int maxDays = GetMaxDaysOfMonth(tmpStartTime.GetYear(), tmpStartTime.GetMonth());
332
333                                 pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick * maxDays);
334                         }
335                         break;
336                 case CALENDAR_ALARM_TIME_UNIT_SPECIFIC:
337                         if (Tizen::App::_AppInfo::GetApiVersion() > _API_VERSION_2_0)
338                         {
339                                 pReminder->SetAbsoluteTime(_CalendarbookUtil::ConvertEpochTimeToDateTime(tmpAlarmTime));
340                         }
341                         break;
342                 default :
343                         break;
344                 }
345
346                 reminderList.Add(*pReminder.release());
347         }
348
349         return E_SUCCESS;
350 }
351
352 void
353 _CalendarbookUtil::ConvertDateTimeToCalTime(const DateTime& dateTime, calendar_time_s& convertedCalTime)
354 {
355         convertedCalTime.type = CALENDAR_TIME_UTIME;
356         convertedCalTime.time.utime = ConvertDateTimeToEpochTime(dateTime);
357 }
358
359 void
360 _CalendarbookUtil::ConvertDateToCalTime(const DateTime& dateTime, calendar_time_s& convertedCalTime)
361 {
362         convertedCalTime.type = CALENDAR_TIME_LOCALTIME;
363         convertedCalTime.time.date.year = dateTime.GetYear();
364         convertedCalTime.time.date.month = dateTime.GetMonth();
365         convertedCalTime.time.date.mday = dateTime.GetDay();
366 }
367
368 result
369 _CalendarbookUtil::ConvertRRuleDateTimeStringToDateTime(const String& dateTimeString, DateTime& dateTime, Tizen::Locales::TimeZone& timeZone, bool& isDate)
370 {
371         result r = E_SUCCESS;
372         String dateValue;
373         String timeValue;
374
375         // Split the dateTimeString into date value, time value and time zone
376         if (dateTimeString.GetLength() == _CALENDARBOOK_DATE_VALUE_STRING_LENGTH)
377         {
378                 dateValue = dateTimeString;
379                 isDate = true;
380         }
381         else if (dateTimeString.GetLength() == _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH)
382         {
383                 int timeValueIndex = 0;
384                 int suffixIndex = 0;
385                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_TIME_PREFIX, _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1, timeValueIndex);
386                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no T prefix
387                 SysTryReturnResult(NID_SCL, timeValueIndex == _CALENDARBOOK_DATE_VALUE_STRING_LENGTH
388                                 , E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer()); //The position of T is wrong
389
390                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_UTC_SUFFIX, _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1, suffixIndex);
391                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no Z suffix
392                 SysTryReturnResult(NID_SCL, suffixIndex == _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1
393                                 , E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer()); //The position of Z is wrong
394
395                 r = dateTimeString.SubString(0, _CALENDARBOOK_DATE_VALUE_STRING_LENGTH, dateValue);
396                 r = dateTimeString.SubString(_CALENDARBOOK_DATE_VALUE_STRING_LENGTH + 1
397                                 , _CALENDARBOOK_TIME_VALUE_STRING_LENGTH, timeValue);
398                 isDate = false;
399         }
400         else if (dateTimeString.GetLength() > _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH)  // including timezone ID
401         {
402                 String timeZoneId;
403                 int dateValueIndex = 0;
404
405                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_UTC_TZID, dateTimeString.GetLength() - 1, dateValueIndex);
406                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no TZID
407                 SysTryReturnResult(NID_SCL, dateValueIndex == 0, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer()); //The position of TZID is wrong
408
409                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_DATE_TIME_DELIMITER, dateTimeString.GetLength() - 1, dateValueIndex);
410                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no TZID
411
412                 r = dateTimeString.SubString(_CALENDARBOOK_TZID_INDEX, dateValueIndex - _CALENDARBOOK_TZID_INDEX, timeZoneId);
413                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
414
415                 dateValueIndex += 1;
416
417                 r = dateTimeString.SubString(dateValueIndex, _CALENDARBOOK_DATE_VALUE_STRING_LENGTH, dateValue);
418                 r = dateTimeString.SubString(dateValueIndex +_CALENDARBOOK_DATE_VALUE_STRING_LENGTH + 1
419                                 , _CALENDARBOOK_TIME_VALUE_STRING_LENGTH, timeValue);
420
421                 r = Tizen::Locales::TimeZone::GetTimeZone(timeZoneId, timeZone);
422                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // not supported timezone ID
423                 isDate = false;
424         }
425         else
426         {
427                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. date time string = %S", dateTimeString.GetPointer());
428                 return E_INVALID_ARG;
429         }
430
431         int tmpIndex = 0;
432         String tmpString;
433         int yearValue = 0;
434         int monthValue = 0;
435         int dayValue = 0;
436         int hourValue = 0;
437         int minuteValue = 0;
438         int secondValue = 0;
439
440         // Parse date value
441         r = dateValue.SubString(tmpIndex, _CALENDARBOOK_DATE_YEAR_STRING_LENGTH, tmpString);
442         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
443         r = Integer::Parse(tmpString, yearValue);
444         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
445         tmpIndex += _CALENDARBOOK_DATE_YEAR_STRING_LENGTH;
446
447         r = dateValue.SubString(tmpIndex, _CALENDARBOOK_DATE_MONTH_STRING_LENGTH, tmpString);
448         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
449         r = Integer::Parse(tmpString, monthValue);
450         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
451         tmpIndex += _CALENDARBOOK_DATE_MONTH_STRING_LENGTH;
452
453         r = dateValue.SubString(tmpIndex, _CALENDARBOOK_DATE_DAY_STRING_LENGTH, tmpString);
454         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
455         r = Integer::Parse(tmpString, dayValue);
456         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
457         tmpIndex += _CALENDARBOOK_DATE_DAY_STRING_LENGTH;
458
459         if (!isDate)
460         {
461                 // Parse time value
462                 tmpIndex = 0;
463                 r = timeValue.SubString(tmpIndex, _CALENDARBOOK_TIME_HOUR_STRING_LENGTH, tmpString);
464                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
465                 r = Integer::Parse(tmpString, hourValue);
466                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
467                 tmpIndex += _CALENDARBOOK_TIME_HOUR_STRING_LENGTH;
468
469                 r = timeValue.SubString(tmpIndex, _CALENDARBOOK_TIME_MINUTE_STRING_LENGTH, tmpString);
470                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
471                 r = Integer::Parse(tmpString, minuteValue);
472                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
473                 tmpIndex += _CALENDARBOOK_TIME_MINUTE_STRING_LENGTH;
474
475                 r = timeValue.SubString(tmpIndex, _CALENDARBOOK_TIME_SECOND_STRING_LENGTH, tmpString);
476                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
477                 r = Integer::Parse(tmpString, secondValue);
478                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
479                 tmpIndex += _CALENDARBOOK_TIME_SECOND_STRING_LENGTH;
480         }
481
482         r = dateTime.SetValue(yearValue, monthValue, dayValue, hourValue, minuteValue, secondValue);
483         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
484
485         return E_SUCCESS;
486 }
487
488 String
489 _CalendarbookUtil::ConvertDateTimeToRRuleDateTimeString(const Tizen::Base::DateTime& dateTime, bool isDate)
490 {
491         result r = E_SUCCESS;
492         String exdateString;
493
494         r = exdateString.Append(dateTime.GetYear());
495
496         if (dateTime.GetMonth() < 10)
497         {
498                 r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
499         }
500         r = exdateString.Append(dateTime.GetMonth());
501
502         if (dateTime.GetDay() < 10)
503         {
504                 r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
505         }
506         r = exdateString.Append(dateTime.GetDay());
507
508         if (!isDate)
509         {
510                 r = exdateString.Append(_CALENDARBOOK_TIME_PREFIX);
511
512                 if (dateTime.GetHour() < 10)
513                 {
514                         r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
515                 }
516                 r = exdateString.Append(dateTime.GetHour());
517
518                 if (dateTime.GetMinute() < 10)
519                 {
520                         r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
521                 }
522                 r = exdateString.Append(dateTime.GetMinute());
523
524                 if (dateTime.GetSecond() < 10)
525                 {
526                         r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
527                 }
528                 r = exdateString.Append(dateTime.GetSecond());
529
530                 r = exdateString.Append(_CALENDARBOOK_UTC_SUFFIX);
531         }
532
533         return exdateString;
534 }
535
536 calendar_event_status_e
537 _CalendarbookUtil::ConvertEventStatusToCSEventStatus(int status)
538 {
539         calendar_event_status_e convertedEventStatus = CALENDAR_EVENT_STATUS_NONE;
540
541         switch (status)
542         {
543         case EVENT_STATUS_NONE:
544                 convertedEventStatus = CALENDAR_EVENT_STATUS_NONE;
545                 break;
546         case EVENT_STATUS_TENTATIVE:
547                 convertedEventStatus = CALENDAR_EVENT_STATUS_TENTATIVE;
548                 break;
549         case EVENT_STATUS_CONFIRMED:
550                 convertedEventStatus = CALENDAR_EVENT_STATUS_CONFIRMED;
551                 break;
552         case EVENT_STATUS_CANCELLED:
553                 convertedEventStatus = CALENDAR_EVENT_STATUS_CANCELLED;
554                 break;
555         default :
556                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. status = %d", status);
557                 convertedEventStatus = CALENDAR_EVENT_STATUS_NONE;
558                 break;
559         }
560
561         return convertedEventStatus;
562 }
563
564 calendar_event_busy_status_e
565 _CalendarbookUtil::ConvertBusyStatusToCSEventBusyStatus(int busyStatus)
566 {
567         calendar_event_busy_status_e convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
568
569         switch (busyStatus)
570         {
571         case BUSY_STATUS_FREE:
572                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
573                 break;
574         case BUSY_STATUS_BUSY:
575                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_BUSY;
576                 break;
577         case BUSY_STATUS_UNAVAILABLE:
578                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_UNAVAILABLE;
579                 break;
580         case BUSY_STATUS_TENTATIVE:
581                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_TENTATIVE;
582                 break;
583         default :
584                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. busyStatus = %d", busyStatus);
585                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
586                 break;
587         }
588
589         return convertedBusyStatus;
590 }
591
592 calendar_event_priority_e
593 _CalendarbookUtil::ConvertEventPriorityToCSEventPriority(int priority)
594 {
595         calendar_event_priority_e convertedPriority = CALENDAR_EVENT_PRIORITY_NORMAL;
596
597         switch (priority)
598         {
599         case EVENT_PRIORITY_LOW:
600                 convertedPriority = CALENDAR_EVENT_PRIORITY_LOW;
601                 break;
602         case EVENT_PRIORITY_NORMAL:
603                 convertedPriority = CALENDAR_EVENT_PRIORITY_NORMAL;
604                 break;
605         case EVENT_PRIORITY_HIGH:
606                 convertedPriority = CALENDAR_EVENT_PRIORITY_HIGH;
607                 break;
608         default :
609                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. priority = %d", priority);
610                 convertedPriority = CALENDAR_EVENT_PRIORITY_NORMAL;
611                 break;
612         }
613
614         return convertedPriority;
615 }
616
617 calendar_sensitivity_e
618 _CalendarbookUtil::ConvertSensitivityToCSSensitivity(int sensitivity)
619 {
620         calendar_sensitivity_e convertedSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
621
622         switch (sensitivity)
623         {
624         case SENSITIVITY_PUBLIC:
625                 convertedSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
626                 break;
627         case SENSITIVITY_PRIVATE:
628                 convertedSensitivity = CALENDAR_SENSITIVITY_PRIVATE;
629                 break;
630         case SENSITIVITY_CONFIDENTIAL:
631                 convertedSensitivity = CALENDAR_SENSITIVITY_CONFIDENTIAL;
632                 break;
633         default :
634                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. sensitivity = %d", sensitivity);
635                 convertedSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
636                 break;
637         }
638
639         return convertedSensitivity;
640 }
641
642 calendar_todo_status_e
643 _CalendarbookUtil::ConvertTodoStatusToCSTodoStatus(int status)
644 {
645         calendar_todo_status_e convertedTodoStatus = CALENDAR_TODO_STATUS_NONE;
646
647         switch (status)
648         {
649         case TODO_STATUS_NONE:
650                 convertedTodoStatus = CALENDAR_TODO_STATUS_NONE;
651                 break;
652         case TODO_STATUS_NEEDS_ACTION:
653                 convertedTodoStatus = CALENDAR_TODO_STATUS_NEEDS_ACTION;
654                 break;
655         case TODO_STATUS_COMPLETED:
656                 convertedTodoStatus = CALENDAR_TODO_STATUS_COMPLETED;
657                 break;
658         case TODO_STATUS_IN_PROCESS:
659                 convertedTodoStatus = CALENDAR_TODO_STATUS_IN_PROCESS;
660                 break;
661         case TODO_STATUS_CANCELLED:
662                 convertedTodoStatus = CALENDAR_TODO_STATUS_CANCELED;
663                 break;
664         default :
665                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. status = %d", status);
666                 convertedTodoStatus = CALENDAR_TODO_STATUS_NONE;
667                 break;
668         }
669
670         return convertedTodoStatus;
671 }
672
673 calendar_todo_priority_e
674 _CalendarbookUtil::ConvertTodoPriorityToCSTodoPriority(int priority)
675 {
676         calendar_todo_priority_e convertedPriority = CALENDAR_TODO_PRIORITY_NONE;
677
678         switch (priority)
679         {
680         case TODO_PRIORITY_LOW:
681                 convertedPriority = CALENDAR_TODO_PRIORITY_LOW;
682                 break;
683         case TODO_PRIORITY_NORMAL:
684                 convertedPriority = CALENDAR_TODO_PRIORITY_NORMAL;
685                 break;
686         case TODO_PRIORITY_HIGH:
687                 convertedPriority = CALENDAR_TODO_PRIORITY_HIGH;
688                 break;
689         default :
690                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. priority = %d", priority);
691                 convertedPriority = CALENDAR_TODO_PRIORITY_NONE;
692                 break;
693         }
694
695         return convertedPriority;
696 }
697
698 int
699 _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(int itemType)
700 {
701         int storeType = CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO;
702
703         switch (itemType)
704         {
705         case CALENDAR_ITEM_TYPE_EVENT_ONLY:
706                 storeType = CALENDAR_BOOK_TYPE_EVENT;
707                 break;
708         case CALENDAR_ITEM_TYPE_TODO_ONLY:
709                 storeType = CALENDAR_BOOK_TYPE_TODO;
710                 break;
711         case CALENDAR_ITEM_TYPE_EVENT_AND_TODO:
712                 storeType = CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO;
713                 break;
714         default :
715                 storeType = CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO;
716                 break;
717         }
718
719         return storeType;
720 }
721
722 EventCategory
723 _CalendarbookUtil::ConvertCSCategoriesToEventCategory(const char* pCategories)
724 {
725         result r = E_SUCCESS;
726
727         EventCategory eventCategory = EVENT_CATEGORY_APPOINTMENT;
728         String tmpString(pCategories);
729         String delim(_EVENT_CATEGORY_DELIMITER);
730         String token;
731
732         StringTokenizer strTok(tmpString, delim);
733         while (strTok.HasMoreTokens())
734         {
735                 r = strTok.GetNextToken(token);
736                 SysTryReturn(NID_SCL, r == E_SUCCESS, EVENT_CATEGORY_APPOINTMENT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
737                 if (token == _EVENT_CATEGORY_APPOINTMENT_STRING)
738                 {
739                         eventCategory = EVENT_CATEGORY_APPOINTMENT;
740                         break;
741                 }
742                 else if (token == _EVENT_CATEGORY_ANNIVERSARY_STRING)
743                 {
744                         eventCategory = EVENT_CATEGORY_ANNIVERSARY;
745                         break;
746                 }
747         }
748
749         return eventCategory;
750 }
751
752 int
753 _CalendarbookUtil::GetMaxDaysOfMonth(int year, int month)
754 {
755         int maxDaysOfMonth = 0;
756         Tizen::Locales::Calendar* pGregorianCalendar = Tizen::Locales::Calendar::CreateInstanceN(Tizen::Locales::CALENDAR_GREGORIAN);
757
758         pGregorianCalendar->SetTimeField(Tizen::Locales::TIME_FIELD_YEAR, year);
759         pGregorianCalendar->SetTimeField(Tizen::Locales::TIME_FIELD_MONTH, month);
760
761         maxDaysOfMonth = pGregorianCalendar->GetActualMaxTimeField(Tizen::Locales::TIME_FIELD_DAY_OF_MONTH);
762
763         delete pGregorianCalendar;
764
765         return maxDaysOfMonth;
766 }
767
768 bool
769 _CalendarbookUtil::CheckValidDateTime(const DateTime& dateTime)
770 {
771         if (dateTime < _CalendarbookImpl::GetMinDateTime() || dateTime > _CalendarbookImpl::GetMaxDateTime())
772         {
773                 return false;
774         }
775
776         return true;
777 }
778
779 }}      // Tizen::Social