2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file FScl_CalendarImpl.cpp
18 * @brief This is the implementation for _CalendarImpl class.
20 * This file contains definitions of @e _CalendarImpl class.
23 #include <FBaseInteger.h>
24 #include <FBaseString.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseUtilStringTokenizer.h>
27 #include <FBase_StringConverter.h>
28 #include <FSclCalendar.h>
29 #include "FScl_CalendarbookDbConnector.h"
30 #include "FScl_CalendarImpl.h"
31 #include "FScl_RecordImpl.h"
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Utility;
35 using namespace Tizen::Graphics;
37 namespace Tizen { namespace Social
40 static const wchar_t* _COLOR_STRING_DELIMITER = L".";
41 static const int _NUM_OF_COLOR_FACTOR = 3;
43 _CalendarImpl::_CalendarImpl(CalendarItemType itemType)
45 result r = _CalendarbookDbConnector::Connect();
46 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
48 calendar_record_h calendarHandle = null;
49 int errorCode = calendar_record_create(_calendar_book._uri, &calendarHandle);
50 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
52 calendar_record_set_int(calendarHandle, _calendar_book.store_type, _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(itemType));
54 __calendarRecord.ResetHandle(calendarHandle);
58 _CalendarImpl::_CalendarImpl(const _CalendarImpl& rhs)
60 result r = _CalendarbookDbConnector::Connect();
61 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
63 calendar_record_h calendarHandle = null;
64 int errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
65 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
67 __calendarRecord.ResetHandle(calendarHandle);
70 _CalendarImpl::~_CalendarImpl(void)
72 calendar_record_destroy(__calendarRecord.ReleaseHandle(), true);
74 result r = _CalendarbookDbConnector::Disconnect();
75 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
79 _CalendarImpl::operator =(const _CalendarImpl& rhs)
86 calendar_record_h calendarHandle = null;
88 int errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
89 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
91 __calendarRecord.ResetHandle(calendarHandle);
97 _CalendarImpl::GetName(void) const
101 calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.name, &pName);
103 return String(pName);
107 _CalendarImpl::GetItemType(void) const
110 CalendarItemType calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
112 calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.store_type, &storeType);
116 case CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO:
117 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
119 case CALENDAR_BOOK_TYPE_EVENT:
120 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_ONLY;
122 case CALENDAR_BOOK_TYPE_TODO:
123 calendarItemType = CALENDAR_ITEM_TYPE_TODO_ONLY;
126 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
130 return calendarItemType;
134 _CalendarImpl::GetAccountId(void) const
137 calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.account_id, &accountId);
143 _CalendarImpl::GetColor(byte& red, byte& green, byte& blue) const
146 calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.color, &pColor);
148 if (pColor == null || strlen(pColor) == 0)
154 return E_DATA_NOT_FOUND;
161 String tmpString(pColor);
162 String delim(_COLOR_STRING_DELIMITER);
165 StringTokenizer strTok(tmpString, delim);
167 strTok.GetNextToken(token);
168 Integer::Parse(token, tmpRed);
169 strTok.GetNextToken(token);
170 Integer::Parse(token, tmpGreen);
171 strTok.GetNextToken(token);
172 Integer::Parse(token, tmpBlue);
182 _CalendarImpl::SetName(const String& name)
184 std::unique_ptr<char[]> pConvertedName(_StringConverter::CopyToCharArrayN(name));
185 SysTryReturnVoidResult(NID_SCL, pConvertedName != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
187 int errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.name, pConvertedName.get());
188 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
192 _CalendarImpl::SetColor(byte red, byte green, byte blue)
196 color.Append(_COLOR_STRING_DELIMITER);
198 color.Append(_COLOR_STRING_DELIMITER);
200 color.Append(_COLOR_STRING_DELIMITER);
201 color.Append(255); // alpha
203 std::unique_ptr<char[]> pConvertedColor(_StringConverter::CopyToCharArrayN(color));
204 SysTryReturnVoidResult(NID_SCL, pConvertedColor != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
206 int errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
207 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
211 _CalendarImpl::ClearColor(void)
213 int errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, null);
214 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
218 _CalendarImpl::SetAccountId(AccountId accountId)
220 calendar_record_set_int(__calendarRecord.GetHandle(), _calendar_book.account_id, accountId);
224 _CalendarImpl::SetRecordHandle(calendar_record_h calendarHandle)
226 __calendarRecord.ResetHandle(calendarHandle);
230 _CalendarImpl::GetRecordHandle(void) const
232 return __calendarRecord.GetHandle();
236 _CalendarImpl::CreateDefaultInstanceN(void)
238 return new (std::nothrow) Calendar(CALENDAR_ITEM_TYPE_EVENT_AND_TODO);
242 _CalendarImpl::GetInstance(Calendar& calendar)
244 return calendar.__pCalendarImpl;
248 _CalendarImpl::GetInstance(const Calendar& calendar)
250 return calendar.__pCalendarImpl;