Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_IconListItemProviderAdaptor.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 /**
19  * @file                FUiCtrl_IconListItemProviderAdaptor.cpp
20  * @brief               This is the implementation file for the _IconListItemProviderAdaptor class.
21  */
22
23 //Includes
24 #include <FBaseSysLog.h>
25 #include <FBaseObject.h>
26 #include <FUiCtrlIconListViewItem.h>
27
28 #include "FUiAnim_VisualElement.h"
29 #include "FUiCtrl_IconListItem.h"
30 #include "FUiCtrl_IconListItemProviderAdaptor.h"
31 #include "FUiCtrl_IconListViewItemImpl.h"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Ui;
36 using namespace Tizen::Ui::Animations;
37
38 namespace Tizen { namespace Ui { namespace Controls
39 {
40
41 _IconListItemProviderAdaptor::_IconListItemProviderAdaptor(void)
42         : __pItemProvider(null)
43         , __pBlankItem(null)
44         , __pRoot(null)
45 {
46         // Do nothing
47 }
48
49 _IconListItemProviderAdaptor::~_IconListItemProviderAdaptor(void)
50 {
51         __pItemProvider = null;
52
53         delete __pBlankItem;
54         __pBlankItem = null;
55
56         __pRoot = null;
57 }
58
59 // Operation
60 _IListItemCommon*
61 _IconListItemProviderAdaptor::GetBlankItem(void) const
62 {
63         if (__pBlankItem == null)
64         {
65                 // Create Default Blank Item.
66                 _IconListItem* pBlankItem = _IconListItem::CreateIconListItemN(null);
67                 SysTryReturn(NID_UI_CTRL, (pBlankItem != null), null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
68
69                 const_cast<_IconListItemProviderAdaptor*>(this)->__pBlankItem = pBlankItem;
70         }
71
72         return __pBlankItem;
73 }
74
75 // Accessor
76 void
77 _IconListItemProviderAdaptor::SetItemProvider(void* pProvider)
78 {
79         IIconListViewItemProvider* pItemProvider = static_cast<IIconListViewItemProvider*>(pProvider);
80         if (pItemProvider != null)
81         {
82                 __pItemProvider = pItemProvider;
83         }
84 }
85
86 _IListItemCommon*
87 _IconListItemProviderAdaptor::LoadItem(int groupIndex, int itemIndex)
88 {
89         // Argument Check
90         SysTryReturn(NID_UI_CTRL, (groupIndex == DEFAULT_GROUP_INDEX), null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Index of group must be 0.");
91         SysTryReturn(NID_UI_CTRL, (itemIndex >= GROUP_TITLE_INDEX), null, E_OUT_OF_RANGE,
92                                 "[E_OUT_OF_RANGE] Index must be a non-negative integer.");
93
94         SysAssertf(__pItemProvider != null, "Not yet initialized. SetItemProvider() should be called before use.");
95
96         ClearLastResult();
97         // Return Group Item
98         if (itemIndex == GROUP_TITLE_INDEX)
99         {
100                 return GetBlankItem();
101         }
102
103         // Valid Check
104         _IconListItem* pIconListItem = null;
105         IconListViewItem* pItem = __pItemProvider->CreateItem(itemIndex);
106         SysTryCatch(NID_UI_CTRL, (pItem != null), , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Can't get new item.");
107         SysTryCatch(NID_UI_CTRL, (pItem->__pImpl != null), , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Can't get new item.");
108
109         pIconListItem = pItem->__pImpl->__pIconListItem;
110         SysTryCatch(NID_UI_CTRL, (pIconListItem != null), , E_SYSTEM, "[E_SYSTEM] A system error has been occurred. Can't get new item.");
111
112         pIconListItem->ClearVisualElement();
113         pIconListItem->AddRef();
114
115         // Attach _VisualElement
116         if (__pRoot != null)
117         {
118                 _VisualElement* pBase = pIconListItem->GetVisualElement();
119                 if (pBase != null)
120                 {
121                         __pRoot->AttachChild(*pBase);
122                         pBase->SetShowState(false);
123                         pBase->SetClipToParent(true);
124                 }
125         }
126
127         return pIconListItem;
128
129 CATCH:
130         if (pItem != null)
131         {
132                 if (!__pItemProvider->DeleteItem(itemIndex, pItem))
133                 {
134                         delete pItem;
135                 }
136         }
137
138         return GetBlankItem();
139 }
140
141 result
142 _IconListItemProviderAdaptor::UnloadItem(_IListItemCommon* pListItem, int groupIndex, int itemIndex)
143 {
144         // Argument Check
145         SysTryReturn(NID_UI_CTRL, (pListItem != null), E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. Item must not be null.");
146         SysTryReturn(NID_UI_CTRL, (groupIndex == DEFAULT_GROUP_INDEX), E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Index of group must be 0.");
147         SysTryReturn(NID_UI_CTRL, (itemIndex >= GROUP_TITLE_INDEX), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
148                                 "[E_OUT_OF_RANGE] Index must be a non-negative integer.");
149
150         // Group Item
151         if (itemIndex == GROUP_TITLE_INDEX)
152         {
153                 return E_SUCCESS;
154         }
155
156         // Is the Blank Item?
157         if (pListItem == GetBlankItem())
158         {
159                 return E_SUCCESS;
160         }
161
162         _IconListItem* pIconListItem = dynamic_cast<_IconListItem*>(pListItem);
163         if (pIconListItem != null)
164         {
165                 _VisualElement* pBase = pIconListItem->GetVisualElement();
166                 if (pBase != null)
167                 {
168                         pBase->SetShowState(false);
169                         pIconListItem->ClearVisualElement();
170                         __pRoot->DetachChild(*pBase);
171                 }
172         }
173
174         IconListViewItem* pItem = static_cast<IconListViewItem*>(pIconListItem->GetAppInfo());
175         pIconListItem->Release();
176         if (pItem == null)
177         {
178                 return E_SUCCESS;
179         }
180
181         SysAssertf(__pItemProvider != null, "Not yet initialized. SetItemProvider() should be called before use.");
182
183         if (!__pItemProvider->DeleteItem(itemIndex, pItem))
184         {
185                 delete pItem;
186         }
187
188         return E_SUCCESS;
189 }
190
191 result
192 _IconListItemProviderAdaptor::DeleteItem(_IListItemCommon* pListItem, int groupIndex, int itemIndex)
193 {
194         // Argument Check
195         SysTryReturn(NID_UI_CTRL, (pListItem != null), E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. Item must not be null.");
196         SysTryReturn(NID_UI_CTRL, (groupIndex == DEFAULT_GROUP_INDEX), E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Index of group must be 0.");
197         SysTryReturn(NID_UI_CTRL, (itemIndex >= GROUP_TITLE_INDEX), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
198                                 "[E_OUT_OF_RANGE] Index must be a non-negative integer.");
199
200         // Group Item
201         if (itemIndex == GROUP_TITLE_INDEX)
202         {
203                 return E_SUCCESS;
204         }
205
206         // Is the Blank Item?
207         if (pListItem == GetBlankItem())
208         {
209                 return E_SUCCESS;
210         }
211
212         _IconListItem* pIconListItem = dynamic_cast <_IconListItem*>(pListItem);
213         if (pIconListItem != null)
214         {
215                 _VisualElement* pBase = pIconListItem->GetVisualElement();
216                 if (pBase != null)
217                 {
218                         pBase->SetShowState(false);
219                         pIconListItem->ClearVisualElement();
220                         __pRoot->DetachChild(*pBase);
221                 }
222         }
223
224         IconListViewItem* pItem = static_cast <IconListViewItem*>(pIconListItem->GetAppInfo());
225         pIconListItem->Release();
226         if (pItem != null)
227         {
228                 delete pItem;
229         }
230
231         return E_SUCCESS;
232 }
233
234 int
235 _IconListItemProviderAdaptor::GetItemCount(int groupIndex) const
236 {
237         // Argument Check
238         SysTryReturn(NID_UI_CTRL, (groupIndex == DEFAULT_GROUP_INDEX), -1, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Index of group must be 0.");
239
240         SysAssertf(__pItemProvider != null, "Not yet initialized. SetItemProvider() should be called before use.");
241
242         ClearLastResult();
243
244         int itemCount = __pItemProvider->GetItemCount();
245         return (itemCount < 0) ? 0 : itemCount;
246 }
247
248 int
249 _IconListItemProviderAdaptor::GetGroupCount(void) const
250 {
251         return DEFAULT_GROUP_COUNT;
252 }
253
254 void
255 _IconListItemProviderAdaptor::SetVisualElement(Tizen::Ui::Animations::_VisualElement* pRoot)
256 {
257         SysTryReturnVoidResult(NID_UI_CTRL, (pRoot != null), E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used.");
258
259         ClearLastResult();
260
261         __pRoot = pRoot;
262 }
263
264 }}} // Tizen::Ui::Controls