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