Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUi_ClipboardImpl.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  * @file                FUi_ClipboardImpl.cpp
19  * @brief               This is the implementation file for the _ClipboardImpl class.
20  */
21 #include <FUiClipboard.h>
22 #include <FUiClipboardItem.h>
23 #include <FUiIClipboardPopupEventListener.h>
24 #include <FBaseColLinkedList.h>
25 #include <FBaseResult.h>
26 #include <FBaseSysLog.h>
27 #include "FUi_ClipboardImpl.h"
28 #include "FUi_Clipboard.h"
29 #include "FUi_ClipboardItemImpl.h"
30 #include "FUi_ClipboardItem.h"
31
32 using namespace Tizen::Base::Collection;
33
34 namespace Tizen { namespace Ui
35 {
36
37 _ClipboardImpl*
38 _ClipboardImpl::CreateInstanceN(void)
39 {
40         _ClipboardImpl* pClipboardImpl = new (std::nothrow) _ClipboardImpl;
41         SysTryReturn(NID_UI, pClipboardImpl, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
42
43         SetLastResult(E_SUCCESS);
44
45         return pClipboardImpl;
46 }
47
48 _ClipboardImpl::~_ClipboardImpl(void)
49 {
50 }
51
52 _Clipboard*
53 _ClipboardImpl::GetCore(void)
54 {
55         return _Clipboard::GetInstance();
56 }
57
58 const _Clipboard*
59 _ClipboardImpl::GetCore(void) const
60 {
61         return _Clipboard::GetInstance();
62 }
63
64 result
65 _ClipboardImpl::CopyItem(const _ClipboardItemImpl& item)
66 {
67         SysAssert(GetCore());
68
69         result r = GetCore()->CopyItem(item.GetCore());
70
71         if ((r == E_INVALID_ARG) || (r == E_SYSTEM))
72         {
73                 SysLogException(NID_UI, r, "[%s] Propagating.", GetErrorMessage(r));
74         }
75         else if (r != E_SUCCESS)
76         {
77                 r = E_SYSTEM;
78                 SysLogException(NID_UI, r, "[E_SYSTEM] A system error occurred.");
79         }
80
81         return r;
82 }
83
84 Tizen::Base::Collection::IList*
85 _ClipboardImpl::RetrieveItemsN(unsigned long dataTypes)
86 {
87         SysTryReturn(NID_UI, IsValidDataType(dataTypes), null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The item of the specified data types is not found.");
88         SysAssert(GetCore());
89
90         // Get items from _Clipboard.
91         IList* pRetrievedList = GetCore()->RetrieveItemsN(dataTypes);
92         result r = GetLastResult();
93
94         if ((r == E_OBJ_NOT_FOUND) || (r == E_OUT_OF_MEMORY) || (r == E_SYSTEM))
95         {
96                 SysLogException(NID_UI, r, "[%s] Propagating.", GetErrorMessage(r));
97                 return null;
98         }
99
100         SysTryReturn(NID_UI, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
101
102         IEnumerator* pEnumerator = null;
103         _ClipboardItem* pItemCore = null;
104         ClipboardItem* pItem = null;
105
106         // Make lists.
107         LinkedList* pList = new (std::nothrow) LinkedList;
108         SysTryCatch(NID_UI, pList, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
109
110         pEnumerator = pRetrievedList->GetEnumeratorN();
111         r = GetLastResult();
112         SysTryCatch(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
113
114         while (pEnumerator->MoveNext() == E_SUCCESS)
115         {
116                 // [ToDo] Use a template list.
117                 pItemCore = dynamic_cast<_ClipboardItem*>(pEnumerator->GetCurrent());
118                 if (pItemCore)
119                 {
120                         pItem = new (std::nothrow) ClipboardItem;
121                         SysTryCatch(NID_UI, pItem, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
122
123                         r = pItem->Construct(pItemCore->GetDataType(), *(pItemCore->GetData()));
124                         if (r != E_SUCCESS)
125                         {
126                                 delete pItem;
127                                 SysLogException(NID_UI, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
128                                 goto CATCH;
129                         }
130
131                         r = pList->Add(*pItem);
132                         if (r != E_SUCCESS)
133                         {
134                                 delete pItem;
135                                 SysLogException(NID_UI, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
136                                 goto CATCH;
137                         }
138                 }
139         }
140
141         delete pEnumerator;
142
143         pRetrievedList->RemoveAll(true);
144         delete pRetrievedList;
145
146         SetLastResult(E_SUCCESS);
147
148         return pList;
149
150 CATCH:
151         pRetrievedList->RemoveAll(true);
152         delete pRetrievedList;
153
154         if (pList)
155         {
156                 pList->RemoveAll(true);
157                 delete pList;
158         }
159
160         if (pEnumerator)
161         {
162                 delete pEnumerator;
163         }
164
165         return null;
166 }
167
168 _ClipboardItemImpl*
169 _ClipboardImpl::RetrieveLatestItemN(unsigned long dataTypes)
170 {
171         SysTryReturn(NID_UI, IsValidDataType(dataTypes), null, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The item of the specified data types is not found.");
172         SysAssert(GetCore());
173
174         const _ClipboardItem* pItemCore = GetCore()->RetrieveLatestItemN(dataTypes);
175         result r = GetLastResult();
176
177         ClipboardItem* pItem = null;
178         _ClipboardItemImpl* pItemImpl = null;
179
180         if ((r == E_OBJ_NOT_FOUND) || (r == E_OUT_OF_MEMORY) || (r == E_SYSTEM))
181         {
182                 SysLogException(NID_UI, r, "[%s] Propagating.", GetErrorMessage(r));
183                 goto CATCH;
184         }
185         else if (r != E_SUCCESS)
186         {
187                 SysLogException(NID_UI, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
188                 goto CATCH;
189         }
190
191         pItem = new (std::nothrow) ClipboardItem;
192         SysTryCatch(NID_UI, pItem, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
193
194         r = pItem->Construct(pItemCore->GetDataType(), *(pItemCore->GetData()));
195         SysTryCatch(NID_UI, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error occurred.");
196
197         delete pItemCore;
198
199         pItemImpl = _ClipboardItemImpl::GetInstance(*pItem);
200
201         SetLastResult(E_SUCCESS);
202
203         return pItemImpl;
204
205 CATCH:
206         if (pItemCore)
207         {
208                 delete pItemCore;
209         }
210
211         if (pItem)
212         {
213                 delete pItem;
214         }
215
216         return null;
217 }
218
219 result
220 _ClipboardImpl::ShowPopup(unsigned long dataTypes, const IClipboardPopupEventListener& listener)
221 {
222         SysTryReturn(NID_UI, IsValidDataType(dataTypes), E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
223         SysTryReturn(NID_UI, GetCore(), E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
224
225         result r = GetCore()->ShowPopup(dataTypes, listener);
226
227         if ((r == E_INVALID_OPERATION) || (r == E_SYSTEM))
228         {
229                 SysLogException(NID_UI, r, "[%s] Propagating.", GetErrorMessage(r));
230         }
231         else if (r != E_SUCCESS)
232         {
233                 r = E_SYSTEM;
234                 SysLogException(NID_UI, r, "[E_SYSTEM] A system error occurred.");
235         }
236
237         return r;
238 }
239
240 result
241 _ClipboardImpl::HidePopup(void)
242 {
243         SysTryReturn(NID_UI, GetCore(), E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
244
245         result r = GetCore()->HidePopup();
246
247         if ((r == E_INVALID_OPERATION) || (r == E_SYSTEM))
248         {
249                 SysLogException(NID_UI, r, "[%s] Propagating.", GetErrorMessage(r));
250         }
251         else if (r != E_SUCCESS)
252         {
253                 r = E_SYSTEM;
254                 SysLogException(NID_UI, r, "[E_SYSTEM] A system error occurred.");
255         }
256
257         return r;
258 }
259
260 bool
261 _ClipboardImpl::IsPopupVisible(void)
262 {
263         if (GetCore() == null)
264         {
265                 return false;
266         }
267
268         return GetCore()->IsPopupVisible();
269 }
270
271 _ClipboardImpl*
272 _ClipboardImpl::GetInstance(Clipboard& clipboard)
273 {
274         return clipboard.__pClipboardImpl;
275 }
276
277 const _ClipboardImpl*
278 _ClipboardImpl::GetInstance(const Clipboard& clipboard)
279 {
280         return clipboard.__pClipboardImpl;
281 }
282
283 bool
284 _ClipboardImpl::IsValidDataType(unsigned long dataTypes)
285 {
286         return ((CLIPBOARD_DATA_TYPE_TEXT & dataTypes) ||
287                 (CLIPBOARD_DATA_TYPE_HTML & dataTypes) ||
288                 (CLIPBOARD_DATA_TYPE_IMAGE & dataTypes) ||
289                 (CLIPBOARD_DATA_TYPE_VIDEO & dataTypes) ||
290                 (CLIPBOARD_DATA_TYPE_AUDIO & dataTypes));
291 }
292
293 _ClipboardImpl::_ClipboardImpl(void)
294 {
295 }
296
297 }} // Tizen::Ui