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 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 calendar_record_destroy(__calendarRecord.ReleaseHandle(), true);
78 result r = _CalendarbookDbConnector::Disconnect();
79 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
83 _CalendarImpl::operator =(const _CalendarImpl& rhs)
90 int errorCode = CALENDAR_ERROR_NONE;
91 calendar_record_h calendarHandle = null;
93 errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
94 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
96 __calendarRecord.ResetHandle(calendarHandle);
102 _CalendarImpl::GetName(void) const
106 calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.name, &pName);
108 return String(pName);
112 _CalendarImpl::GetItemType(void) const
115 CalendarItemType calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
117 calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.store_type, &storeType);
121 case CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO:
122 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
124 case CALENDAR_BOOK_TYPE_EVENT:
125 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_ONLY;
127 case CALENDAR_BOOK_TYPE_TODO:
128 calendarItemType = CALENDAR_ITEM_TYPE_TODO_ONLY;
131 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
135 return calendarItemType;
139 _CalendarImpl::GetAccountId(void) const
142 calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.account_id, &accountId);
148 _CalendarImpl::GetColor(byte& red, byte& green, byte& blue) const
151 calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.color, &pColor);
153 if (pColor == null || strlen(pColor) == 0)
159 return E_DATA_NOT_FOUND;
166 String tmpString(pColor);
167 String delim(_COLOR_STRING_DELIMITER);
170 StringTokenizer strTok(tmpString, delim);
172 strTok.GetNextToken(token);
173 Integer::Parse(token, tmpRed);
174 strTok.GetNextToken(token);
175 Integer::Parse(token, tmpGreen);
176 strTok.GetNextToken(token);
177 Integer::Parse(token, tmpBlue);
187 _CalendarImpl::SetName(const String& name)
189 std::unique_ptr<char[]> pConvertedName(_StringConverter::CopyToCharArrayN(name));
190 SysTryReturnVoidResult(NID_SCL, pConvertedName != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
192 int errorCode = CALENDAR_ERROR_NONE;
193 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.name, pConvertedName.get());
194 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
198 _CalendarImpl::SetColor(byte red, byte green, byte blue)
202 color.Append(_COLOR_STRING_DELIMITER);
204 color.Append(_COLOR_STRING_DELIMITER);
206 color.Append(_COLOR_STRING_DELIMITER);
207 color.Append(255); // alpha
209 std::unique_ptr<char[]> pConvertedColor(_StringConverter::CopyToCharArrayN(color));
210 SysTryReturnVoidResult(NID_SCL, pConvertedColor != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
212 int errorCode = CALENDAR_ERROR_NONE;
213 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
214 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
218 _CalendarImpl::ClearColor(void)
222 std::unique_ptr<char[]> pConvertedColor(_StringConverter::CopyToCharArrayN(color));
223 SysTryReturnVoidResult(NID_SCL, pConvertedColor != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
225 int errorCode = CALENDAR_ERROR_NONE;
226 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
227 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
231 _CalendarImpl::SetAccountId(AccountId accountId)
233 calendar_record_set_int(__calendarRecord.GetHandle(), _calendar_book.account_id, accountId);
237 _CalendarImpl::SetRecordHandle(calendar_record_h calendarHandle)
239 __calendarRecord.ResetHandle(calendarHandle);
243 _CalendarImpl::GetRecordHandle(void) const
245 return __calendarRecord.GetHandle();
249 _CalendarImpl::CreateDefaultInstanceN(void)
251 return new (std::nothrow) Calendar(CALENDAR_ITEM_TYPE_EVENT_AND_TODO);
255 _CalendarImpl::GetInstance(Calendar& calendar)
257 return calendar.__pCalendarImpl;
261 _CalendarImpl::GetInstance(const Calendar& calendar)
263 return calendar.__pCalendarImpl;