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 <FBaseString.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseUtilStringTokenizer.h>
26 #include <FBaseInteger.h>
27 #include <FSclCalendar.h>
28 #include <FBase_StringConverter.h>
29 #include "FScl_CalendarImpl.h"
30 #include "FScl_RecordImpl.h"
31 #include "FScl_CalendarbookDbConnector.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 int errorCode = CALENDAR_ERROR_NONE;
46 calendar_record_h calendarHandle = null;
48 result r = _CalendarbookDbConnector::Connect();
49 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
51 errorCode = calendar_record_create(_calendar_book._uri, &calendarHandle);
52 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
54 errorCode = calendar_record_set_int(calendarHandle, _calendar_book.store_type, _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(itemType));
56 __calendarRecord.ResetHandle(calendarHandle);
60 _CalendarImpl::_CalendarImpl(const _CalendarImpl& rhs)
62 int errorCode = CALENDAR_ERROR_NONE;
63 calendar_record_h calendarHandle = null;
65 result r = _CalendarbookDbConnector::Connect();
66 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
68 errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
69 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
71 __calendarRecord.ResetHandle(calendarHandle);
74 _CalendarImpl::~_CalendarImpl(void)
76 int errorCode = CALENDAR_ERROR_NONE;
78 errorCode = calendar_record_destroy(__calendarRecord.ReleaseHandle(), true);
80 result r = _CalendarbookDbConnector::Disconnect();
81 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
85 _CalendarImpl::operator =(const _CalendarImpl& rhs)
92 int errorCode = CALENDAR_ERROR_NONE;
93 calendar_record_h calendarHandle = null;
95 errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
96 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
98 __calendarRecord.ResetHandle(calendarHandle);
104 _CalendarImpl::GetName(void) const
106 int errorCode = CALENDAR_ERROR_NONE;
109 errorCode = calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.name, &pName);
111 return String(pName);
115 _CalendarImpl::GetItemType(void) const
117 int errorCode = CALENDAR_ERROR_NONE;
119 CalendarItemType calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
121 errorCode = calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.store_type, &storeType);
125 case CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO:
126 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
128 case CALENDAR_BOOK_TYPE_EVENT:
129 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_ONLY;
131 case CALENDAR_BOOK_TYPE_TODO:
132 calendarItemType = CALENDAR_ITEM_TYPE_TODO_ONLY;
135 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
139 return calendarItemType;
143 _CalendarImpl::GetAccountId(void) const
145 int errorCode = CALENDAR_ERROR_NONE;
147 errorCode = calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.account_id, &accountId);
153 _CalendarImpl::GetColor(byte& red, byte& green, byte& blue) const
155 int errorCode = CALENDAR_ERROR_NONE;
157 errorCode = calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.color, &pColor);
159 if (pColor == null || strlen(pColor) == 0)
165 return E_DATA_NOT_FOUND;
168 result r = E_SUCCESS;
174 String tmpString(pColor);
175 String delim(_COLOR_STRING_DELIMITER);
178 StringTokenizer strTok(tmpString, delim);
180 r = strTok.GetNextToken(token);
181 r = Integer::Parse(token, tmpRed);
182 r = strTok.GetNextToken(token);
183 r = Integer::Parse(token, tmpGreen);
184 r = strTok.GetNextToken(token);
185 r = Integer::Parse(token, tmpBlue);
195 _CalendarImpl::SetName(const String& name)
197 std::unique_ptr<char[]> pConvertedName(_StringConverter::CopyToCharArrayN(name));
198 SysTryReturnVoidResult(NID_SCL, pConvertedName != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
200 int errorCode = CALENDAR_ERROR_NONE;
201 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.name, pConvertedName.get());
205 _CalendarImpl::SetColor(byte red, byte green, byte blue)
209 color.Append(_COLOR_STRING_DELIMITER);
211 color.Append(_COLOR_STRING_DELIMITER);
213 color.Append(_COLOR_STRING_DELIMITER);
214 color.Append(0); // alpha
216 std::unique_ptr<char[]> pConvertedColor(_StringConverter::CopyToCharArrayN(color));
217 SysTryReturnVoidResult(NID_SCL, pConvertedColor != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
219 int errorCode = CALENDAR_ERROR_NONE;
220 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
224 _CalendarImpl::ClearColor(void)
228 std::unique_ptr<char[]> pConvertedColor(_StringConverter::CopyToCharArrayN(color));
229 SysTryReturnVoidResult(NID_SCL, pConvertedColor != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
231 int errorCode = CALENDAR_ERROR_NONE;
232 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
236 _CalendarImpl::SetAccountId(AccountId accountId)
238 int errorCode = CALENDAR_ERROR_NONE;
239 errorCode = calendar_record_set_int(__calendarRecord.GetHandle(), _calendar_book.account_id, accountId);
243 _CalendarImpl::SetRecordHandle(calendar_record_h calendarHandle)
245 __calendarRecord.ResetHandle(calendarHandle);
249 _CalendarImpl::GetRecordHandle(void) const
251 return __calendarRecord.GetHandle();
255 _CalendarImpl::CreateDefaultInstanceN(void)
257 return new (std::nothrow) Calendar(CALENDAR_ITEM_TYPE_EVENT_AND_TODO);
261 _CalendarImpl::GetInstance(Calendar& calendar)
263 return calendar.__pCalendarImpl;
267 _CalendarImpl::GetInstance(const Calendar& calendar)
269 return calendar.__pCalendarImpl;