[N_SE-32914, 33582] Fixed priority converting codes
[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(pByteArray.release(), 0, byteBufferLength, byteBufferLength);
178         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
179
180         return pByteBuffer.release();
181 }
182
183 result
184 _CalendarbookUtil::ConvertEventAlarmsToReminderList(calendar_record_h calendarRecordHandle, ArrayList& reminderList)
185 {
186         int errorCode = CALENDAR_ERROR_NONE;
187
188         unsigned int reminderCount = 0;
189         errorCode = calendar_record_get_child_record_count(calendarRecordHandle, _calendar_event.calendar_alarm, &reminderCount);
190
191         if (reminderCount == 0)
192         {
193                 reminderList.RemoveAll(true);
194                 return E_SUCCESS;
195         }
196
197         for (int i = 0; i< reminderCount; i++)
198         {
199                 calendar_record_h tmpAlarmHandle = null;
200                 int tmpAlarmTickUnit = CALENDAR_ALARM_NONE;
201                 int tmpAlarmTick = 0;
202                 long long tmpAlarmTime = 0;
203                 char* pTmpAlarmTone = null;
204
205                 errorCode = calendar_record_get_child_record_at_p(calendarRecordHandle, _calendar_event.calendar_alarm, i, &tmpAlarmHandle);
206                 errorCode = calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick_unit, &tmpAlarmTickUnit);
207                 errorCode = calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick, &tmpAlarmTick);
208                 errorCode = calendar_record_get_lli(tmpAlarmHandle, _calendar_alarm.time, &tmpAlarmTime);
209                 errorCode = calendar_record_get_str_p(tmpAlarmHandle, _calendar_alarm.tone, &pTmpAlarmTone);
210
211                 std::unique_ptr<Reminder> pReminder(new (std::nothrow) Reminder());
212                 SysTryReturnResult(NID_SCL, pReminder != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
213
214                 pReminder->SetSoundFile(pTmpAlarmTone);
215
216                 switch (tmpAlarmTickUnit)
217                 {
218                 case CALENDAR_ALARM_TIME_UNIT_MINUTE:
219                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_MINUTE, tmpAlarmTick);
220                         break;
221                 case CALENDAR_ALARM_TIME_UNIT_HOUR:
222                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_HOUR, tmpAlarmTick);
223                         break;
224                 case CALENDAR_ALARM_TIME_UNIT_DAY:
225                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick);
226                         break;
227                 case CALENDAR_ALARM_TIME_UNIT_WEEK:
228                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_WEEK, tmpAlarmTick);
229                         break;
230                 case CALENDAR_ALARM_TIME_UNIT_MONTH:
231                         {
232                                 calendar_time_s startCalendarTime;
233                                 DateTime tmpStartTime;
234
235                                 errorCode = calendar_record_get_caltime(calendarRecordHandle, _calendar_event.start_time, &startCalendarTime);
236                                 if (startCalendarTime.type == CALENDAR_TIME_UTIME)
237                                 {
238                                         tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
239                                 }
240                                 else
241                                 {
242                                         tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
243                                 }
244
245                                 tmpStartTime.AddMonths(-1);
246                                 int maxDays = GetMaxDaysOfMonth(tmpStartTime.GetYear(), tmpStartTime.GetMonth());
247
248                                 pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick * maxDays);
249                         }
250                         break;
251                 case CALENDAR_ALARM_TIME_UNIT_SPECIFIC:
252                         if (Tizen::App::_AppInfo::GetApiVersion() > _API_VERSION_2_0)
253                         {
254                                 pReminder->SetAbsoluteTime(_CalendarbookUtil::ConvertEpochTimeToDateTime(tmpAlarmTime));
255                         }
256                         break;
257                 default :
258                         break;
259                 }
260
261                 reminderList.Add(*pReminder.release());
262         }
263
264         return E_SUCCESS;
265 }
266
267 result
268 _CalendarbookUtil::ConvertTodoAlarmsToReminderList(calendar_record_h calendarRecordHandle, ArrayList& reminderList)
269 {
270         int errorCode = CALENDAR_ERROR_NONE;
271
272         unsigned int reminderCount = 0;
273         errorCode = calendar_record_get_child_record_count(calendarRecordHandle, _calendar_todo.calendar_alarm, &reminderCount);
274
275         if (reminderCount == 0)
276         {
277                 reminderList.RemoveAll(true);
278                 return E_SUCCESS;
279         }
280
281         for (int i = 0; i< reminderCount; i++)
282         {
283                 calendar_record_h tmpAlarmHandle = null;
284                 int tmpAlarmTickUnit = CALENDAR_ALARM_NONE;
285                 int tmpAlarmTick = 0;
286                 long long tmpAlarmTime = 0;
287                 char* pTmpAlarmTone = null;
288
289                 errorCode = calendar_record_get_child_record_at_p(calendarRecordHandle, _calendar_todo.calendar_alarm, i, &tmpAlarmHandle);
290                 errorCode = calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick_unit, &tmpAlarmTickUnit);
291                 errorCode = calendar_record_get_int(tmpAlarmHandle, _calendar_alarm.tick, &tmpAlarmTick);
292                 errorCode = calendar_record_get_lli(tmpAlarmHandle, _calendar_alarm.time, &tmpAlarmTime);
293                 errorCode = calendar_record_get_str_p(tmpAlarmHandle, _calendar_alarm.tone, &pTmpAlarmTone);
294
295                 std::unique_ptr<Reminder> pReminder(new (std::nothrow) Reminder());
296                 SysTryReturnResult(NID_SCL, pReminder != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
297
298                 pReminder->SetSoundFile(pTmpAlarmTone);
299
300                 switch (tmpAlarmTickUnit)
301                 {
302                 case CALENDAR_ALARM_TIME_UNIT_MINUTE:
303                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_MINUTE, tmpAlarmTick);
304                         break;
305                 case CALENDAR_ALARM_TIME_UNIT_HOUR:
306                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_HOUR, tmpAlarmTick);
307                         break;
308                 case CALENDAR_ALARM_TIME_UNIT_DAY:
309                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick);
310                         break;
311                 case CALENDAR_ALARM_TIME_UNIT_WEEK:
312                         pReminder->SetTimeOffset(REMINDER_TIME_UNIT_WEEK, tmpAlarmTick);
313                         break;
314                 case CALENDAR_ALARM_TIME_UNIT_MONTH:
315                         {
316                                 calendar_time_s startCalendarTime;
317                                 DateTime tmpStartTime;
318
319                                 errorCode = calendar_record_get_caltime(calendarRecordHandle, _calendar_todo.start_time, &startCalendarTime);
320                                 if (startCalendarTime.type == CALENDAR_TIME_UTIME)
321                                 {
322                                         tmpStartTime = _CalendarbookUtil::ConvertEpochTimeToDateTime(startCalendarTime.time.utime);
323                                 }
324                                 else
325                                 {
326                                         tmpStartTime.SetValue(startCalendarTime.time.date.year, startCalendarTime.time.date.month, startCalendarTime.time.date.mday);
327                                 }
328
329                                 tmpStartTime.AddMonths(-1);
330                                 int maxDays = GetMaxDaysOfMonth(tmpStartTime.GetYear(), tmpStartTime.GetMonth());
331
332                                 pReminder->SetTimeOffset(REMINDER_TIME_UNIT_DAY, tmpAlarmTick * maxDays);
333                         }
334                         break;
335                 case CALENDAR_ALARM_TIME_UNIT_SPECIFIC:
336                         if (Tizen::App::_AppInfo::GetApiVersion() > _API_VERSION_2_0)
337                         {
338                                 pReminder->SetAbsoluteTime(_CalendarbookUtil::ConvertEpochTimeToDateTime(tmpAlarmTime));
339                         }
340                         break;
341                 default :
342                         break;
343                 }
344
345                 reminderList.Add(*pReminder.release());
346         }
347
348         return E_SUCCESS;
349 }
350
351 void
352 _CalendarbookUtil::ConvertDateTimeToCalTime(const DateTime& dateTime, calendar_time_s& convertedCalTime)
353 {
354         convertedCalTime.type = CALENDAR_TIME_UTIME;
355         convertedCalTime.time.utime = ConvertDateTimeToEpochTime(dateTime);
356 }
357
358 void
359 _CalendarbookUtil::ConvertDateToCalTime(const DateTime& dateTime, calendar_time_s& convertedCalTime)
360 {
361         convertedCalTime.type = CALENDAR_TIME_LOCALTIME;
362         convertedCalTime.time.date.year = dateTime.GetYear();
363         convertedCalTime.time.date.month = dateTime.GetMonth();
364         convertedCalTime.time.date.mday = dateTime.GetDay();
365 }
366
367 result
368 _CalendarbookUtil::ConvertRRuleDateTimeStringToDateTime(const String& dateTimeString, DateTime& dateTime, Tizen::Locales::TimeZone& timeZone, bool& isDate)
369 {
370         result r = E_SUCCESS;
371         String dateValue;
372         String timeValue;
373
374         // Split the dateTimeString into date value, time value and time zone
375         if (dateTimeString.GetLength() == _CALENDARBOOK_DATE_VALUE_STRING_LENGTH)
376         {
377                 dateValue = dateTimeString;
378                 isDate = true;
379         }
380         else if (dateTimeString.GetLength() == _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH)
381         {
382                 int timeValueIndex = 0;
383                 int suffixIndex = 0;
384                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_TIME_PREFIX, _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1, timeValueIndex);
385                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no T prefix
386                 SysTryReturnResult(NID_SCL, timeValueIndex == _CALENDARBOOK_DATE_VALUE_STRING_LENGTH
387                                 , E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer()); //The position of T is wrong
388
389                 r = dateTimeString.LastIndexOf(_CALENDARBOOK_UTC_SUFFIX, _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1, suffixIndex);
390                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // There is no Z suffix
391                 SysTryReturnResult(NID_SCL, suffixIndex == _CALENDARBOOK_UTC_DATE_TIME_VALUE_STRING_LENGTH - 1
392                                 , E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer()); //The position of Z is wrong
393
394                 r = dateTimeString.SubString(0, _CALENDARBOOK_DATE_VALUE_STRING_LENGTH, dateValue);
395                 r = dateTimeString.SubString(_CALENDARBOOK_DATE_VALUE_STRING_LENGTH + 1
396                                 , _CALENDARBOOK_TIME_VALUE_STRING_LENGTH, timeValue);
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                 r = dateTimeString.SubString(dateValueIndex +_CALENDARBOOK_DATE_VALUE_STRING_LENGTH + 1
418                                 , _CALENDARBOOK_TIME_VALUE_STRING_LENGTH, timeValue);
419
420                 r = Tizen::Locales::TimeZone::GetTimeZone(timeZoneId, timeZone);
421                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());   // not supported timezone ID
422                 isDate = false;
423         }
424         else
425         {
426                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. date time string = %S", dateTimeString.GetPointer());
427                 return E_INVALID_ARG;
428         }
429
430         int tmpIndex = 0;
431         String tmpString;
432         int yearValue = 0;
433         int monthValue = 0;
434         int dayValue = 0;
435         int hourValue = 0;
436         int minuteValue = 0;
437         int secondValue = 0;
438
439         // Parse date value
440         r = dateValue.SubString(tmpIndex, _CALENDARBOOK_DATE_YEAR_STRING_LENGTH, tmpString);
441         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
442         r = Integer::Parse(tmpString, yearValue);
443         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
444         tmpIndex += _CALENDARBOOK_DATE_YEAR_STRING_LENGTH;
445
446         r = dateValue.SubString(tmpIndex, _CALENDARBOOK_DATE_MONTH_STRING_LENGTH, tmpString);
447         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
448         r = Integer::Parse(tmpString, monthValue);
449         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
450         tmpIndex += _CALENDARBOOK_DATE_MONTH_STRING_LENGTH;
451
452         r = dateValue.SubString(tmpIndex, _CALENDARBOOK_DATE_DAY_STRING_LENGTH, tmpString);
453         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
454         r = Integer::Parse(tmpString, dayValue);
455         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
456         tmpIndex += _CALENDARBOOK_DATE_DAY_STRING_LENGTH;
457
458         if (!isDate)
459         {
460                 // Parse time value
461                 tmpIndex = 0;
462                 r = timeValue.SubString(tmpIndex, _CALENDARBOOK_TIME_HOUR_STRING_LENGTH, tmpString);
463                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
464                 r = Integer::Parse(tmpString, hourValue);
465                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
466                 tmpIndex += _CALENDARBOOK_TIME_HOUR_STRING_LENGTH;
467
468                 r = timeValue.SubString(tmpIndex, _CALENDARBOOK_TIME_MINUTE_STRING_LENGTH, tmpString);
469                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
470                 r = Integer::Parse(tmpString, minuteValue);
471                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
472                 tmpIndex += _CALENDARBOOK_TIME_MINUTE_STRING_LENGTH;
473
474                 r = timeValue.SubString(tmpIndex, _CALENDARBOOK_TIME_SECOND_STRING_LENGTH, tmpString);
475                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
476                 r = Integer::Parse(tmpString, secondValue);
477                 SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
478                 tmpIndex += _CALENDARBOOK_TIME_SECOND_STRING_LENGTH;
479         }
480
481         r = dateTime.SetValue(yearValue, monthValue, dayValue, hourValue, minuteValue, secondValue);
482         SysTryReturnResult(NID_SCL, r == E_SUCCESS, E_INVALID_ARG, "Invalid argument is passed. date time string = %S", dateTimeString.GetPointer());
483
484         return E_SUCCESS;
485 }
486
487 String
488 _CalendarbookUtil::ConvertDateTimeToRRuleDateTimeString(const Tizen::Base::DateTime& dateTime, bool isDate)
489 {
490         result r = E_SUCCESS;
491         String exdateString;
492
493         r = exdateString.Append(dateTime.GetYear());
494
495         if (dateTime.GetMonth() < 10)
496         {
497                 r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
498         }
499         r = exdateString.Append(dateTime.GetMonth());
500
501         if (dateTime.GetDay() < 10)
502         {
503                 r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
504         }
505         r = exdateString.Append(dateTime.GetDay());
506
507         if (!isDate)
508         {
509                 r = exdateString.Append(_CALENDARBOOK_TIME_PREFIX);
510
511                 if (dateTime.GetHour() < 10)
512                 {
513                         r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
514                 }
515                 r = exdateString.Append(dateTime.GetHour());
516
517                 if (dateTime.GetMinute() < 10)
518                 {
519                         r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
520                 }
521                 r = exdateString.Append(dateTime.GetMinute());
522
523                 if (dateTime.GetSecond() < 10)
524                 {
525                         r = exdateString.Append(_CALENDARBOOK_ZERO_STRING);
526                 }
527                 r = exdateString.Append(dateTime.GetSecond());
528
529                 r = exdateString.Append(_CALENDARBOOK_UTC_SUFFIX);
530         }
531
532         return exdateString;
533 }
534
535 calendar_event_status_e
536 _CalendarbookUtil::ConvertEventStatusToCSEventStatus(int status)
537 {
538         calendar_event_status_e convertedEventStatus = CALENDAR_EVENT_STATUS_NONE;
539
540         switch (status)
541         {
542         case EVENT_STATUS_NONE:
543                 convertedEventStatus = CALENDAR_EVENT_STATUS_NONE;
544                 break;
545         case EVENT_STATUS_TENTATIVE:
546                 convertedEventStatus = CALENDAR_EVENT_STATUS_TENTATIVE;
547                 break;
548         case EVENT_STATUS_CONFIRMED:
549                 convertedEventStatus = CALENDAR_EVENT_STATUS_CONFIRMED;
550                 break;
551         case EVENT_STATUS_CANCELLED:
552                 convertedEventStatus = CALENDAR_EVENT_STATUS_CANCELLED;
553                 break;
554         default :
555                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. status = %d", status);
556                 convertedEventStatus = CALENDAR_EVENT_STATUS_NONE;
557                 break;
558         }
559
560         return convertedEventStatus;
561 }
562
563 calendar_event_busy_status_e
564 _CalendarbookUtil::ConvertBusyStatusToCSEventBusyStatus(int busyStatus)
565 {
566         calendar_event_busy_status_e convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
567
568         switch (busyStatus)
569         {
570         case BUSY_STATUS_FREE:
571                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
572                 break;
573         case BUSY_STATUS_BUSY:
574                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_BUSY;
575                 break;
576         case BUSY_STATUS_UNAVAILABLE:
577                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_UNAVAILABLE;
578                 break;
579         case BUSY_STATUS_TENTATIVE:
580                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_TENTATIVE;
581                 break;
582         default :
583                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. busyStatus = %d", busyStatus);
584                 convertedBusyStatus = CALENDAR_EVENT_BUSY_STATUS_FREE;
585                 break;
586         }
587
588         return convertedBusyStatus;
589 }
590
591 calendar_event_priority_e
592 _CalendarbookUtil::ConvertEventPriorityToCSEventPriority(int priority)
593 {
594         calendar_event_priority_e convertedPriority = CALENDAR_EVENT_PRIORITY_NORMAL;
595
596         switch (priority)
597         {
598         case EVENT_PRIORITY_LOW:
599                 convertedPriority = CALENDAR_EVENT_PRIORITY_LOW;
600                 break;
601         case EVENT_PRIORITY_NORMAL:
602                 convertedPriority = CALENDAR_EVENT_PRIORITY_NORMAL;
603                 break;
604         case EVENT_PRIORITY_HIGH:
605                 convertedPriority = CALENDAR_EVENT_PRIORITY_HIGH;
606                 break;
607         default :
608                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. priority = %d", priority);
609                 convertedPriority = CALENDAR_EVENT_PRIORITY_NORMAL;
610                 break;
611         }
612
613         return convertedPriority;
614 }
615
616 calendar_sensitivity_e
617 _CalendarbookUtil::ConvertSensitivityToCSSensitivity(int sensitivity)
618 {
619         calendar_sensitivity_e convertedSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
620
621         switch (sensitivity)
622         {
623         case SENSITIVITY_PUBLIC:
624                 convertedSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
625                 break;
626         case SENSITIVITY_PRIVATE:
627                 convertedSensitivity = CALENDAR_SENSITIVITY_PRIVATE;
628                 break;
629         case SENSITIVITY_CONFIDENTIAL:
630                 convertedSensitivity = CALENDAR_SENSITIVITY_CONFIDENTIAL;
631                 break;
632         default :
633                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. sensitivity = %d", sensitivity);
634                 convertedSensitivity = CALENDAR_SENSITIVITY_PUBLIC;
635                 break;
636         }
637
638         return convertedSensitivity;
639 }
640
641 calendar_todo_status_e
642 _CalendarbookUtil::ConvertTodoStatusToCSTodoStatus(int status)
643 {
644         calendar_todo_status_e convertedTodoStatus = CALENDAR_TODO_STATUS_NONE;
645
646         switch (status)
647         {
648         case TODO_STATUS_NONE:
649                 convertedTodoStatus = CALENDAR_TODO_STATUS_NONE;
650                 break;
651         case TODO_STATUS_NEEDS_ACTION:
652                 convertedTodoStatus = CALENDAR_TODO_STATUS_NEEDS_ACTION;
653                 break;
654         case TODO_STATUS_COMPLETED:
655                 convertedTodoStatus = CALENDAR_TODO_STATUS_COMPLETED;
656                 break;
657         case TODO_STATUS_IN_PROCESS:
658                 convertedTodoStatus = CALENDAR_TODO_STATUS_IN_PROCESS;
659                 break;
660         case TODO_STATUS_CANCELLED:
661                 convertedTodoStatus = CALENDAR_TODO_STATUS_CANCELED;
662                 break;
663         default :
664                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. status = %d", status);
665                 convertedTodoStatus = CALENDAR_TODO_STATUS_NONE;
666                 break;
667         }
668
669         return convertedTodoStatus;
670 }
671
672 calendar_todo_priority_e
673 _CalendarbookUtil::ConvertTodoPriorityToCSTodoPriority(int priority)
674 {
675         calendar_todo_priority_e convertedPriority = CALENDAR_TODO_PRIORITY_NONE;
676
677         switch (priority)
678         {
679         case TODO_PRIORITY_LOW:
680                 convertedPriority = CALENDAR_TODO_PRIORITY_LOW;
681                 break;
682         case TODO_PRIORITY_NORMAL:
683                 convertedPriority = CALENDAR_TODO_PRIORITY_NORMAL;
684                 break;
685         case TODO_PRIORITY_HIGH:
686                 convertedPriority = CALENDAR_TODO_PRIORITY_HIGH;
687                 break;
688         default :
689                 SysLogException(NID_SCL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. priority = %d", priority);
690                 convertedPriority = CALENDAR_TODO_PRIORITY_NONE;
691                 break;
692         }
693
694         return convertedPriority;
695 }
696
697 int
698 _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(int itemType)
699 {
700         int storeType = CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO;
701
702         switch (itemType)
703         {
704         case CALENDAR_ITEM_TYPE_EVENT_ONLY:
705                 storeType = CALENDAR_BOOK_TYPE_EVENT;
706                 break;
707         case CALENDAR_ITEM_TYPE_TODO_ONLY:
708                 storeType = CALENDAR_BOOK_TYPE_TODO;
709                 break;
710         case CALENDAR_ITEM_TYPE_EVENT_AND_TODO:
711                 storeType = CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO;
712                 break;
713         default :
714                 storeType = CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO;
715                 break;
716         }
717
718         return storeType;
719 }
720
721 EventCategory
722 _CalendarbookUtil::ConvertCSCategoriesToEventCategory(const char* pCategories)
723 {
724         result r = E_SUCCESS;
725
726         EventCategory eventCategory = EVENT_CATEGORY_APPOINTMENT;
727         String tmpString(pCategories);
728         String delim(_EVENT_CATEGORY_DELIMITER);
729         String token;
730
731         StringTokenizer strTok(tmpString, delim);
732         while (strTok.HasMoreTokens())
733         {
734                 r = strTok.GetNextToken(token);
735                 SysTryReturn(NID_SCL, r == E_SUCCESS, EVENT_CATEGORY_APPOINTMENT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
736                 if (token == _EVENT_CATEGORY_APPOINTMENT_STRING)
737                 {
738                         eventCategory = EVENT_CATEGORY_APPOINTMENT;
739                         break;
740                 }
741                 else if (token == _EVENT_CATEGORY_ANNIVERSARY_STRING)
742                 {
743                         eventCategory = EVENT_CATEGORY_ANNIVERSARY;
744                         break;
745                 }
746         }
747
748         return eventCategory;
749 }
750
751 int
752 _CalendarbookUtil::GetMaxDaysOfMonth(int year, int month)
753 {
754         int maxDaysOfMonth = 0;
755         Tizen::Locales::Calendar* pGregorianCalendar = Tizen::Locales::Calendar::CreateInstanceN(Tizen::Locales::CALENDAR_GREGORIAN);
756
757         pGregorianCalendar->SetTimeField(Tizen::Locales::TIME_FIELD_YEAR, year);
758         pGregorianCalendar->SetTimeField(Tizen::Locales::TIME_FIELD_MONTH, month);
759
760         maxDaysOfMonth = pGregorianCalendar->GetActualMaxTimeField(Tizen::Locales::TIME_FIELD_DAY_OF_MONTH);
761
762         delete pGregorianCalendar;
763
764         return maxDaysOfMonth;
765 }
766
767 bool
768 _CalendarbookUtil::CheckValidDateTime(const DateTime& dateTime)
769 {
770         if (dateTime < _CalendarbookImpl::GetMinDateTime() || dateTime > _CalendarbookImpl::GetMaxDateTime())
771         {
772                 return false;
773         }
774
775         return true;
776 }
777
778 }}      // Tizen::Social