Fix the boiler plate codes
[framework/osp/social.git] / src / FScl_CalendarImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16 /**
17  * @file                FScl_CalendarImpl.cpp
18  * @brief               This is the implementation for _CalendarImpl class.
19  *
20  * This file contains definitions of @e _CalendarImpl class.
21  */
22
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"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Utility;
35 using namespace Tizen::Graphics;
36
37 namespace Tizen { namespace Social
38 {
39
40 static const wchar_t* _COLOR_STRING_DELIMITER = L".";
41 static const int _NUM_OF_COLOR_FACTOR = 3;
42
43 _CalendarImpl::_CalendarImpl(CalendarItemType itemType)
44 {
45         int errorCode = CALENDAR_ERROR_NONE;
46         calendar_record_h calendarHandle = null;
47
48         result r = _CalendarbookDbConnector::Connect();
49         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
50
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.");
53
54         errorCode = calendar_record_set_int(calendarHandle, _calendar_book.store_type, _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(itemType));
55
56         __calendarRecord.ResetHandle(calendarHandle);
57
58 }
59
60 _CalendarImpl::_CalendarImpl(const _CalendarImpl& rhs)
61 {
62         int errorCode = CALENDAR_ERROR_NONE;
63         calendar_record_h calendarHandle = null;
64
65         result r = _CalendarbookDbConnector::Connect();
66         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
67
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.");
70
71         __calendarRecord.ResetHandle(calendarHandle);
72 }
73
74 _CalendarImpl::~_CalendarImpl(void)
75 {
76         int errorCode = CALENDAR_ERROR_NONE;
77
78         errorCode = calendar_record_destroy(__calendarRecord.ReleaseHandle(), true);
79
80         result r = _CalendarbookDbConnector::Disconnect();
81         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
82 }
83
84 _CalendarImpl&
85 _CalendarImpl::operator =(const _CalendarImpl& rhs)
86 {
87         if (this == &rhs)
88         {
89                 return *this;
90         }
91
92         int errorCode = CALENDAR_ERROR_NONE;
93         calendar_record_h calendarHandle = null;
94
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.");
97
98         __calendarRecord.ResetHandle(calendarHandle);
99
100         return *this;
101 }
102
103 String
104 _CalendarImpl::GetName(void) const
105 {
106         int errorCode = CALENDAR_ERROR_NONE;
107         char* pName = null;
108
109         errorCode = calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.name, &pName);
110
111         return String(pName);
112 }
113
114 CalendarItemType
115 _CalendarImpl::GetItemType(void) const
116 {
117         int errorCode = CALENDAR_ERROR_NONE;
118         int storeType = 0;
119         CalendarItemType calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
120
121         errorCode = calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.store_type, &storeType);
122         switch (storeType)
123         {
124
125                 case CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO:
126                         calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
127                         break;
128                 case CALENDAR_BOOK_TYPE_EVENT:
129                         calendarItemType = CALENDAR_ITEM_TYPE_EVENT_ONLY;
130                         break;
131                 case CALENDAR_BOOK_TYPE_TODO:
132                         calendarItemType = CALENDAR_ITEM_TYPE_TODO_ONLY;
133                         break;
134                 default:
135                         calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
136                         break;
137         }
138
139         return calendarItemType;
140 }
141
142 AccountId
143 _CalendarImpl::GetAccountId(void) const
144 {
145         int errorCode = CALENDAR_ERROR_NONE;
146         int accountId = 0;
147         errorCode = calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.account_id, &accountId);
148
149         return accountId;
150 }
151
152 result
153 _CalendarImpl::GetColor(byte& red, byte& green, byte& blue) const
154 {
155         int errorCode = CALENDAR_ERROR_NONE;
156         char* pColor = null;
157         errorCode = calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.color, &pColor);
158
159         if (pColor == null || strlen(pColor) == 0)
160         {
161                 red = 0;
162                 green = 0;
163                 blue = 0;
164
165                 return E_DATA_NOT_FOUND;
166         }
167
168         result r = E_SUCCESS;
169
170         int tmpRed = 0;
171         int tmpGreen = 0;
172         int tmpBlue = 0;
173
174         String tmpString(pColor);
175         String delim(_COLOR_STRING_DELIMITER);
176         String token;
177
178         StringTokenizer strTok(tmpString, delim);
179
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);
186
187         red = tmpRed;
188         green = tmpGreen;
189         blue = tmpBlue;
190
191         return E_SUCCESS;
192 }
193
194 void
195 _CalendarImpl::SetName(const String& name)
196 {
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.");
199
200         int errorCode = CALENDAR_ERROR_NONE;
201         errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.name, pConvertedName.get());
202 }
203
204 void
205 _CalendarImpl::SetColor(byte red, byte green, byte blue)
206 {
207         String color;
208         color.Append(red);
209         color.Append(_COLOR_STRING_DELIMITER);
210         color.Append(green);
211         color.Append(_COLOR_STRING_DELIMITER);
212         color.Append(blue);
213         color.Append(_COLOR_STRING_DELIMITER);
214         color.Append(0);        // alpha
215
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.");
218
219         int errorCode = CALENDAR_ERROR_NONE;
220         errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
221 }
222
223 void
224 _CalendarImpl::ClearColor(void)
225 {
226         String color;
227
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.");
230
231         int errorCode = CALENDAR_ERROR_NONE;
232         errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
233 }
234
235 void
236 _CalendarImpl::SetAccountId(AccountId accountId)
237 {
238         int errorCode = CALENDAR_ERROR_NONE;
239         errorCode = calendar_record_set_int(__calendarRecord.GetHandle(), _calendar_book.account_id, accountId);
240 }
241
242 void
243 _CalendarImpl::SetRecordHandle(calendar_record_h calendarHandle)
244 {
245         __calendarRecord.ResetHandle(calendarHandle);
246 }
247
248 calendar_record_h
249 _CalendarImpl::GetRecordHandle(void) const
250 {
251         return __calendarRecord.GetHandle();
252 }
253
254 Calendar*
255 _CalendarImpl::CreateDefaultInstanceN(void)
256 {
257         return new (std::nothrow) Calendar(CALENDAR_ITEM_TYPE_EVENT_AND_TODO);
258 }
259
260 _CalendarImpl*
261 _CalendarImpl::GetInstance(Calendar& calendar)
262 {
263         return calendar.__pCalendarImpl;
264 }
265
266 const _CalendarImpl*
267 _CalendarImpl::GetInstance(const Calendar& calendar)
268 {
269         return calendar.__pCalendarImpl;
270 }
271
272 }}      // Tizen::Social