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