2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FScl_CalendarImpl.cpp
19 * @brief This is the implementation for _CalendarImpl class.
21 * This file contains definitions of @e _CalendarImpl class.
24 #include <FBaseString.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseUtilStringTokenizer.h>
27 #include <FBaseInteger.h>
28 #include <FSclCalendar.h>
29 #include <FBase_StringConverter.h>
30 #include "FScl_CalendarImpl.h"
31 #include "FScl_RecordImpl.h"
32 #include "FScl_CalendarbookDbConnector.h"
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Utility;
36 using namespace Tizen::Graphics;
38 namespace Tizen { namespace Social
41 static const wchar_t* _COLOR_STRING_DELIMITER = L".";
42 static const int _NUM_OF_COLOR_FACTOR = 3;
44 _CalendarImpl::_CalendarImpl(CalendarItemType itemType)
46 int errorCode = CALENDAR_ERROR_NONE;
47 calendar_record_h calendarHandle = null;
49 result r = _CalendarbookDbConnector::Connect();
50 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
52 errorCode = calendar_record_create(_calendar_book._uri, &calendarHandle);
53 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
55 errorCode = calendar_record_set_int(calendarHandle, _calendar_book.store_type, _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(itemType));
57 __calendarRecord.ResetHandle(calendarHandle);
61 _CalendarImpl::_CalendarImpl(const _CalendarImpl& rhs)
63 int errorCode = CALENDAR_ERROR_NONE;
64 calendar_record_h calendarHandle = null;
66 result r = _CalendarbookDbConnector::Connect();
67 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
69 errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
70 SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
72 __calendarRecord.ResetHandle(calendarHandle);
75 _CalendarImpl::~_CalendarImpl(void)
77 int errorCode = CALENDAR_ERROR_NONE;
79 errorCode = calendar_record_destroy(__calendarRecord.ReleaseHandle(), true);
81 result r = _CalendarbookDbConnector::Disconnect();
82 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
86 _CalendarImpl::operator =(const _CalendarImpl& rhs)
93 int errorCode = CALENDAR_ERROR_NONE;
94 calendar_record_h calendarHandle = null;
96 errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
97 SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
99 __calendarRecord.ResetHandle(calendarHandle);
105 _CalendarImpl::GetName(void) const
107 int errorCode = CALENDAR_ERROR_NONE;
110 errorCode = calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.name, &pName);
112 return String(pName);
116 _CalendarImpl::GetItemType(void) const
118 int errorCode = CALENDAR_ERROR_NONE;
120 CalendarItemType calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
122 errorCode = calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.store_type, &storeType);
126 case CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO:
127 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
129 case CALENDAR_BOOK_TYPE_EVENT:
130 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_ONLY;
132 case CALENDAR_BOOK_TYPE_TODO:
133 calendarItemType = CALENDAR_ITEM_TYPE_TODO_ONLY;
136 calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
140 return calendarItemType;
144 _CalendarImpl::GetAccountId(void) const
146 int errorCode = CALENDAR_ERROR_NONE;
148 errorCode = calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.account_id, &accountId);
154 _CalendarImpl::GetColor(byte& red, byte& green, byte& blue) const
156 int errorCode = CALENDAR_ERROR_NONE;
158 errorCode = calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.color, &pColor);
160 if (pColor == null || strlen(pColor) == 0)
166 return E_DATA_NOT_FOUND;
169 result r = E_SUCCESS;
175 String tmpString(pColor);
176 String delim(_COLOR_STRING_DELIMITER);
179 StringTokenizer strTok(tmpString, delim);
181 r = strTok.GetNextToken(token);
182 r = Integer::Parse(token, tmpRed);
183 r = strTok.GetNextToken(token);
184 r = Integer::Parse(token, tmpGreen);
185 r = strTok.GetNextToken(token);
186 r = Integer::Parse(token, tmpBlue);
196 _CalendarImpl::SetName(const String& name)
198 std::unique_ptr<char[]> pConvertedName(_StringConverter::CopyToCharArrayN(name));
199 SysTryReturnVoidResult(NID_SCL, pConvertedName != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
201 int errorCode = CALENDAR_ERROR_NONE;
202 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.name, pConvertedName.get());
206 _CalendarImpl::SetColor(byte red, byte green, byte blue)
210 color.Append(_COLOR_STRING_DELIMITER);
212 color.Append(_COLOR_STRING_DELIMITER);
214 color.Append(_COLOR_STRING_DELIMITER);
215 color.Append(0); // alpha
217 std::unique_ptr<char[]> pConvertedColor(_StringConverter::CopyToCharArrayN(color));
218 SysTryReturnVoidResult(NID_SCL, pConvertedColor != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
220 int errorCode = CALENDAR_ERROR_NONE;
221 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
225 _CalendarImpl::ClearColor(void)
229 std::unique_ptr<char[]> pConvertedColor(_StringConverter::CopyToCharArrayN(color));
230 SysTryReturnVoidResult(NID_SCL, pConvertedColor != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
232 int errorCode = CALENDAR_ERROR_NONE;
233 errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
237 _CalendarImpl::SetAccountId(AccountId accountId)
239 int errorCode = CALENDAR_ERROR_NONE;
240 errorCode = calendar_record_set_int(__calendarRecord.GetHandle(), _calendar_book.account_id, accountId);
244 _CalendarImpl::SetRecordHandle(calendar_record_h calendarHandle)
246 __calendarRecord.ResetHandle(calendarHandle);
250 _CalendarImpl::GetRecordHandle(void) const
252 return __calendarRecord.GetHandle();
256 _CalendarImpl::CreateDefaultInstanceN(void)
258 return new (std::nothrow) Calendar(CALENDAR_ITEM_TYPE_EVENT_AND_TODO);
262 _CalendarImpl::GetInstance(Calendar& calendar)
264 return calendar.__pCalendarImpl;
268 _CalendarImpl::GetInstance(const Calendar& calendar)
270 return calendar.__pCalendarImpl;