remove return on void function
[framework/osp/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         unsigned int reminderCount = 0;
188         calendar_record_get_child_record_count(calendarRecordHandle, _calendar_event.calendar_alarm, &reminderCount);
189
190         if (reminderCount == 0)
191         {
192                 reminderList.RemoveAll(true);
193                 return E_SUCCESS;
194         }
195
196         for (unsigned int i = 0; i< reminderCount; i++)
197         {
198                 calendar_record_h tmpAlarmHandle = null;
199                 int tmpAlarmTickUnit = CALENDAR_ALARM_NONE;
200                 int tmpAlarmTick = 0;
201                 long long tmpAlarmTime = 0;
202                 char* pTmpAlarmTone = null;
203
204                 calendar_record_get_child_record_at_p(calendarRecordHandle, _calendar_event.calendar_alarm, i, &tmpAlarmHandle);
205                 calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick_unit, &tmpAlarmTickUnit);
206                 calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick, &tmpAlarmTick);
207                 calendar_record_get_lli(tmpAlarmHandle, _calendar_alarm.time, &tmpAlarmTime);
208                 calendar_record_get_str_p(tmpAlarmHandle, _calendar_alarm.tone, &pTmpAlarmTone);
209
210                 std::unique_ptr<Reminder> pReminder(new (std::nothrow) Reminder());
211                 SysTryReturnResult(NID_SCL, pReminder != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
212
213                 pReminder->SetSoundFile(pTmpAlarmTone);
214
215                 switch (tmpAlarmTickUnit)
216                 {
217                 case CALENDAR_ALARM_TIME_UNIT_MINUTE:
218                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_MINUTE, tmpAlarmTick);
219                         break;
220                 case CALENDAR_ALARM_TIME_UNIT_HOUR:
221                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_HOUR, tmpAlarmTick);
222                         break;
223                 case CALENDAR_ALARM_TIME_UNIT_DAY:
224                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick);
225                         break;
226                 case CALENDAR_ALARM_TIME_UNIT_WEEK:
227                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_WEEK, tmpAlarmTick);
228                         break;
229                 case CALENDAR_ALARM_TIME_UNIT_MONTH:
230                         {
231                                 calendar_time_s startCalendarTime;
232                                 DateTime tmpStartTime;
233
234                                 calendar_record_get_caltime(calendarRecordHandle, _calendar_event.start_time, &startCalendarTime);
235                                 if (startCalendarTime.type == CALENDAR_TIME_UTIME)
236                                 {
237                                         tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
238                                 }
239                                 else
240                                 {
241                                         tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
242                                 }
243
244                                 tmpStartTime.AddMonths(-1);
245                                 int maxDays = GetMaxDaysOfMonth(tmpStartTime.GetYear(), tmpStartTime.GetMonth());
246
247                                 pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick * maxDays);
248                         }
249                         break;
250                 case CALENDAR_ALARM_TIME_UNIT_SPECIFIC:
251                         if (Tizen::App::_AppInfo::GetApiVersion() > _API_VERSION_2_0)
252                         {
253                                 pReminder->SetAbsoluteTime(_CalendarbookUtil::ConvertEpochTimeToDateTime(tmpAlarmTime));
254                         }
255                         break;
256                 default :
257                         break;
258                 }
259
260                 reminderList.Add(*pReminder.release());
261         }
262
263         return E_SUCCESS;
264 }
265
266 result
267 _CalendarbookUtil::ConvertTodoAlarmsToReminderList(calendar_record_h calendarRecordHandle, ArrayList& reminderList)
268 {
269         unsigned int reminderCount = 0;
270         calendar_record_get_child_record_count(calendarRecordHandle, _calendar_todo.calendar_alarm, &reminderCount);
271
272         if (reminderCount == 0)
273         {
274                 reminderList.RemoveAll(true);
275                 return E_SUCCESS;
276         }
277
278         for (unsigned int i = 0; i< reminderCount; i++)
279         {
280                 calendar_record_h tmpAlarmHandle = null;
281                 int tmpAlarmTickUnit = CALENDAR_ALARM_NONE;
282                 int tmpAlarmTick = 0;
283                 long long tmpAlarmTime = 0;
284                 char* pTmpAlarmTone = null;
285
286                 calendar_record_get_child_record_at_p(calendarRecordHandle, _calendar_todo.calendar_alarm, i, &tmpAlarmHandle);
287                 calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick_unit, &tmpAlarmTickUnit);
288                 calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick, &tmpAlarmTick);
289                 calendar_record_get_lli(tmpAlarmHandle, _calendar_alarm.time, &tmpAlarmTime);
290                 calendar_record_get_str_p(tmpAlarmHandle, _calendar_alarm.tone, &pTmpAlarmTone);
291
292                 std::unique_ptr<Reminder> pReminder(new (std::nothrow) Reminder());
293                 SysTryReturnResult(NID_SCL, pReminder != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
294
295                 pReminder->SetSoundFile(pTmpAlarmTone);
296
297                 switch (tmpAlarmTickUnit)
298                 {
299                 case CALENDAR_ALARM_TIME_UNIT_MINUTE:
300                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_MINUTE, tmpAlarmTick);
301                         break;
302                 case CALENDAR_ALARM_TIME_UNIT_HOUR:
303                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_HOUR, tmpAlarmTick);
304                         break;
305                 case CALENDAR_ALARM_TIME_UNIT_DAY:
306                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick);
307                         break;
308                 case CALENDAR_ALARM_TIME_UNIT_WEEK:
309                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_WEEK, tmpAlarmTick);
310                         break;
311                 case CALENDAR_ALARM_TIME_UNIT_MONTH:
312                         {
313                                 calendar_time_s startCalendarTime;
314                                 DateTime tmpStartTime;
315
316                                 calendar_record_get_caltime(calendarRecordHandle, _calendar_todo.start_time, &startCalendarTime);
317                                 if (startCalendarTime.type == CALENDAR_TIME_UTIME)
318                                 {
319                                         tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
320                                 }
321                                 else
322                                 {
323                                         tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
324                                 }
325
326                                 tmpStartTime.AddMonths(-1);
327                                 int maxDays = GetMaxDaysOfMonth(tmpStartTime.GetYear(), tmpStartTime.GetMonth());
328
329                                 pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick * maxDays);
330                         }
331                         break;
332                 case CALENDAR_ALARM_TIME_UNIT_SPECIFIC:
333                         if (Tizen::App::_AppInfo::GetApiVersion() > _API_VERSION_2_0)
334                         {
335                                 pReminder->SetAbsoluteTime(_CalendarbookUtil::ConvertEpochTimeToDateTime(tmpAlarmTime));
336                         }
337                         break;
338                 default :
339                         break;
340                 }
341
342                 reminderList.Add(*pReminder.release());
343         }
344
345         return E_SUCCESS;
346 }
347
348 void
349 _CalendarbookUtil::ConvertDateTimeToCalTime(const DateTime& dateTime, calendar_time_s& convertedCalTime)
350 {
351         convertedCalTime.type = CALENDAR_TIME_UTIME;
352         convertedCalTime.time.utime = ConvertDateTimeToEpochTime(dateTime);
353 }
354
355 void
356 _CalendarbookUtil::ConvertDateToCalTime(const DateTime& dateTime, calendar_time_s& convertedCalTime)
357 {
358         convertedCalTime.type = CALENDAR_TIME_LOCALTIME;
359         convertedCalTime.time.date.year = dateTime.GetYear();
360         convertedCalTime.time.date.month = dateTime.GetMonth();
361         convertedCalTime.time.date.mday = dateTime.GetDay();
362 }
363
364 result
365 _CalendarbookUtil::ConvertRRuleDateTimeStringToDateTime(const String& dateTimeString, DateTime& dateTime, Tizen::Locales::TimeZone& timeZone, bool& isDate)
366 {
367         result r = E_SUCCESS;
368         String dateValue;
369         String timeValue;
370
371         // Split the dateTimeString into date value, time value and time zone
372         if (dateTimeString.GetLength() == _CALENDARBOOK_DATE_VALUE_STRING_LENGTH)
373         {
374                 dateValue = dateTimeString;
375                 isDate = true;
376         }
377         else if (dateTimeString.GetLength() == _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH)
378         {
379                 int timeValueIndex = 0;
380                 int suffixIndex = 0;
381                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_TIME_PREFIX, _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1, timeValueIndex);
382                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no T prefix
383                 SysTryReturnResult(NID_SCL, timeValueIndex == _CALENDARBOOK_DATE_VALUE_STRING_LENGTH
384                                 , E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer()); //The position of T is wrong
385
386                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_UTC_SUFFIX, _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1, suffixIndex);
387                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no Z suffix
388                 SysTryReturnResult(NID_SCL, suffixIndex == _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1
389                                 , E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer()); //The position of Z is wrong
390
391                 r = dateTimeString.SubString(0, _CALENDARBOOK_DATE_VALUE_STRING_LENGTH, dateValue);
392                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
393                 r = dateTimeString.SubString(_CALENDARBOOK_DATE_VALUE_STRING_LENGTH + 1
394                                 , _CALENDARBOOK_TIME_VALUE_STRING_LENGTH, timeValue);
395                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
396
397                 isDate = false;
398         }
399         else if (dateTimeString.GetLength() > _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH)  // including timezone ID
400         {
401                 String timeZoneId;
402                 int dateValueIndex = 0;
403
404                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_UTC_TZID, dateTimeString.GetLength() - 1, dateValueIndex);
405                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no TZID
406                 SysTryReturnResult(NID_SCL, dateValueIndex == 0, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer()); //The position of TZID is wrong
407
408                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_DATE_TIME_DELIMITER, dateTimeString.GetLength() - 1, dateValueIndex);
409                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no TZID
410
411                 r = dateTimeString.SubString(_CALENDARBOOK_TZID_INDEX, dateValueIndex - _CALENDARBOOK_TZID_INDEX, timeZoneId);
412                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
413
414                 dateValueIndex += 1;
415
416                 r = dateTimeString.SubString(dateValueIndex, _CALENDARBOOK_DATE_VALUE_STRING_LENGTH, dateValue);
417                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
418                 r = dateTimeString.SubString(dateValueIndex +_CALENDARBOOK_DATE_VALUE_STRING_LENGTH + 1
419                                 , _CALENDARBOOK_TIME_VALUE_STRING_LENGTH, timeValue);
420                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
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         String exdateString;
493
494         exdateString.Append(dateTime.GetYear());
495
496         if (dateTime.GetMonth() < 10)
497         {
498                 exdateString.Append(_CALENDARBOOK_ZERO_STRING);
499         }
500         exdateString.Append(dateTime.GetMonth());
501
502         if (dateTime.GetDay() < 10)
503         {
504                 exdateString.Append(_CALENDARBOOK_ZERO_STRING);
505         }
506         exdateString.Append(dateTime.GetDay());
507
508         if (!isDate)
509         {
510                 exdateString.Append(_CALENDARBOOK_TIME_PREFIX);
511
512                 if (dateTime.GetHour() < 10)
513                 {
514                         exdateString.Append(_CALENDARBOOK_ZERO_STRING);
515                 }
516                 exdateString.Append(dateTime.GetHour());
517
518                 if (dateTime.GetMinute() < 10)
519                 {
520                         exdateString.Append(_CALENDARBOOK_ZERO_STRING);
521                 }
522                 exdateString.Append(dateTime.GetMinute());
523
524                 if (dateTime.GetSecond() < 10)
525                 {
526                         exdateString.Append(_CALENDARBOOK_ZERO_STRING);
527                 }
528                 exdateString.Append(dateTime.GetSecond());
529
530                 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