Init Tizen 2.2.1
[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 <FBaseInteger.h>
24 #include <FBaseString.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseUtilStringTokenizer.h>
27 #include <FBase_StringConverter.h>
28 #include <FSclCalendar.h>
29 #include "FScl_CalendarbookDbConnector.h"
30 #include "FScl_CalendarImpl.h"
31 #include "FScl_RecordImpl.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         result r = _CalendarbookDbConnector::Connect();
46         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
47
48         calendar_record_h calendarHandle = null;
49         int errorCode = calendar_record_create(_calendar_book._uri, &calendarHandle);
50         SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
51
52         calendar_record_set_int(calendarHandle, _calendar_book.store_type, _CalendarbookUtil::ConvertCalendarItemTypeToCSCalendarbookType(itemType));
53
54         __calendarRecord.ResetHandle(calendarHandle);
55
56 }
57
58 _CalendarImpl::_CalendarImpl(const _CalendarImpl& rhs)
59 {
60         result r = _CalendarbookDbConnector::Connect();
61         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
62
63         calendar_record_h calendarHandle = null;
64         int errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
65         SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
66
67         __calendarRecord.ResetHandle(calendarHandle);
68 }
69
70 _CalendarImpl::~_CalendarImpl(void)
71 {
72         calendar_record_destroy(__calendarRecord.ReleaseHandle(), true);
73
74         result r = _CalendarbookDbConnector::Disconnect();
75         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
76 }
77
78 _CalendarImpl&
79 _CalendarImpl::operator =(const _CalendarImpl& rhs)
80 {
81         if (this == &rhs)
82         {
83                 return *this;
84         }
85
86         calendar_record_h calendarHandle = null;
87
88         int errorCode = calendar_record_clone(rhs.__calendarRecord.GetHandle(), &calendarHandle);
89         SysTryReturn(NID_SCL, errorCode == CALENDAR_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
90
91         __calendarRecord.ResetHandle(calendarHandle);
92
93         return *this;
94 }
95
96 String
97 _CalendarImpl::GetName(void) const
98 {
99         char* pName = null;
100
101         calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.name, &pName);
102
103         return String(pName);
104 }
105
106 CalendarItemType
107 _CalendarImpl::GetItemType(void) const
108 {
109         int storeType = 0;
110         CalendarItemType calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
111
112         calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.store_type, &storeType);
113         switch (storeType)
114         {
115
116                 case CALENDAR_BOOK_TYPE_EVENT | CALENDAR_BOOK_TYPE_TODO:
117                         calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
118                         break;
119                 case CALENDAR_BOOK_TYPE_EVENT:
120                         calendarItemType = CALENDAR_ITEM_TYPE_EVENT_ONLY;
121                         break;
122                 case CALENDAR_BOOK_TYPE_TODO:
123                         calendarItemType = CALENDAR_ITEM_TYPE_TODO_ONLY;
124                         break;
125                 default:
126                         calendarItemType = CALENDAR_ITEM_TYPE_EVENT_AND_TODO;
127                         break;
128         }
129
130         return calendarItemType;
131 }
132
133 AccountId
134 _CalendarImpl::GetAccountId(void) const
135 {
136         int accountId = 0;
137         calendar_record_get_int(__calendarRecord.GetHandle(), _calendar_book.account_id, &accountId);
138
139         return accountId;
140 }
141
142 result
143 _CalendarImpl::GetColor(byte& red, byte& green, byte& blue) const
144 {
145         char* pColor = null;
146         calendar_record_get_str_p(__calendarRecord.GetHandle(), _calendar_book.color, &pColor);
147
148         if (pColor == null || strlen(pColor) == 0)
149         {
150                 red = 0;
151                 green = 0;
152                 blue = 0;
153
154                 return E_DATA_NOT_FOUND;
155         }
156
157         int tmpRed = 0;
158         int tmpGreen = 0;
159         int tmpBlue = 0;
160
161         String tmpString(pColor);
162         String delim(_COLOR_STRING_DELIMITER);
163         String token;
164
165         StringTokenizer strTok(tmpString, delim);
166
167         strTok.GetNextToken(token);
168         Integer::Parse(token, tmpRed);
169         strTok.GetNextToken(token);
170         Integer::Parse(token, tmpGreen);
171         strTok.GetNextToken(token);
172         Integer::Parse(token, tmpBlue);
173
174         red = tmpRed;
175         green = tmpGreen;
176         blue = tmpBlue;
177
178         return E_SUCCESS;
179 }
180
181 void
182 _CalendarImpl::SetName(const String& name)
183 {
184         std::unique_ptr<char[]> pConvertedName(_StringConverter::CopyToCharArrayN(name));
185         SysTryReturnVoidResult(NID_SCL, pConvertedName != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
186
187         int errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.name, pConvertedName.get());
188         SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
189 }
190
191 void
192 _CalendarImpl::SetColor(byte red, byte green, byte blue)
193 {
194         String color;
195         color.Append(red);
196         color.Append(_COLOR_STRING_DELIMITER);
197         color.Append(green);
198         color.Append(_COLOR_STRING_DELIMITER);
199         color.Append(blue);
200         color.Append(_COLOR_STRING_DELIMITER);
201         color.Append(255);      // alpha
202
203         std::unique_ptr<char[]> pConvertedColor(_StringConverter::CopyToCharArrayN(color));
204         SysTryReturnVoidResult(NID_SCL, pConvertedColor != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
205
206         int errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, pConvertedColor.get());
207         SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
208 }
209
210 void
211 _CalendarImpl::ClearColor(void)
212 {
213         int errorCode = calendar_record_set_str(__calendarRecord.GetHandle(), _calendar_book.color, null);
214         SysTryReturnVoidResult(NID_SCL, errorCode == CALENDAR_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
215 }
216
217 void
218 _CalendarImpl::SetAccountId(AccountId accountId)
219 {
220         calendar_record_set_int(__calendarRecord.GetHandle(), _calendar_book.account_id, accountId);
221 }
222
223 void
224 _CalendarImpl::SetRecordHandle(calendar_record_h calendarHandle)
225 {
226         __calendarRecord.ResetHandle(calendarHandle);
227 }
228
229 calendar_record_h
230 _CalendarImpl::GetRecordHandle(void) const
231 {
232         return __calendarRecord.GetHandle();
233 }
234
235 Calendar*
236 _CalendarImpl::CreateDefaultInstanceN(void)
237 {
238         return new (std::nothrow) Calendar(CALENDAR_ITEM_TYPE_EVENT_AND_TODO);
239 }
240
241 _CalendarImpl*
242 _CalendarImpl::GetInstance(Calendar& calendar)
243 {
244         return calendar.__pCalendarImpl;
245 }
246
247 const _CalendarImpl*
248 _CalendarImpl::GetInstance(const Calendar& calendar)
249 {
250         return calendar.__pCalendarImpl;
251 }
252
253 }}      // Tizen::Social