Merge "fixed tc error in GetEvasFromUiApp" into tizen_2.2
[framework/osp/web.git] / src / FWeb_WebBookmarkImpl.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 /**
19  * @file                FWebWebHistory.cpp
20  * @brief               The file contains the definition of WebHistory class.
21  *
22  * This file contains the definition of WebHistory class.
23  */
24 #include <new>
25 #include <favorites.h>
26 #include <unique_ptr.h>
27 #include <FBaseSysLog.h>
28 #include <FGrpBitmap.h>
29 #include <FWebBookmarkItem.h>
30 #include <FWebWebBookmark.h>
31 #include <FBase_StringConverter.h>
32 #include <FUiCtrlFrame.h>
33 #include "FWeb_BookmarkItemImpl.h"
34 #include "FWeb_WebBookmarkImpl.h"
35 #include "FWebCtrl_Utility.h"
36
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Ui::Controls;
41 using namespace Tizen::Web::Controls;
42
43
44 namespace Tizen { namespace Web
45 {
46
47
48 bool
49 _BookmarkForEachCb(favorites_bookmark_entry_s* pItem, void* pUserData)
50 {
51         SysAssertf(pItem && pUserData, "Failed to request");
52
53         result r = E_SUCCESS;
54
55         _WebBookmarkImpl* pImpl = reinterpret_cast< _WebBookmarkImpl* >(pUserData);
56
57         String url(pItem->address);
58         String title(pItem->title);
59
60         r = pImpl->AddItem(pItem->id, url, title, pItem->folder_id, pItem->is_folder);
61         SysTryReturn(NID_WEB, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
62
63         return true;
64 }
65
66
67 _WebBookmarkImpl::_WebBookmarkImpl(void)
68         : __rootFolderId(ROOT_FOLDER_ID)
69 {
70 }
71
72
73 _WebBookmarkImpl::~_WebBookmarkImpl(void)
74 {
75 }
76
77
78 result
79 _WebBookmarkImpl::Construct(void)
80 {
81         result r = E_SUCCESS;
82
83         int ret = favorites_bookmark_get_root_folder_id(&__rootFolderId);
84         SysTryReturnResult(NID_WEB, ret == FAVORITES_ERROR_NONE, E_SYSTEM, "Failed to get root folder id.");
85
86         r = __bookmarkList.Construct();
87         SysTryReturn(NID_WEB, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
88
89         ret = favorites_bookmark_foreach(_BookmarkForEachCb, this);
90         SysTryCatch(NID_WEB, ret == FAVORITES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to set bookmark callback.");
91
92         return E_SUCCESS;
93
94 CATCH:
95         __bookmarkList.RemoveAll(true);
96
97         return r;
98 }
99
100
101 result
102 _WebBookmarkImpl::AddFolder(const String& title, RecordId parentId, RecordId& folderId)
103 {
104         std::unique_ptr<char[]> pTitle(_StringConverter::CopyToCharArrayN(title));
105         SysTryReturn(NID_WEB, pTitle.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
106
107         int realParentId = parentId;
108
109         if (parentId == ROOT_FOLDER_ID)
110         {
111                 realParentId = __rootFolderId;
112         }
113
114         int ret = favorites_bookmark_add(pTitle.get(), null, realParentId, 1, &folderId);
115         SysTryReturn(NID_WEB, ret == FAVORITES_ERROR_NONE, ConvertErrorCode(ret), ConvertErrorCode(ret), "[%s] Propagating.", GetErrorMessage(ConvertErrorCode(ret)));
116
117         return E_SUCCESS;
118 }
119
120
121 result
122 _WebBookmarkImpl::AddBookmark(const String& title, const String& url, RecordId parentId, RecordId& bookmarkId)
123 {
124         std::unique_ptr<char[]> pTitle(_StringConverter::CopyToCharArrayN(title));
125         SysTryReturn(NID_WEB, pTitle.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
126         std::unique_ptr<char[]> pUrl(_StringConverter::CopyToCharArrayN(url));
127         SysTryReturn(NID_WEB, pUrl.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
128
129         int realParentId = parentId;
130
131         if (parentId == ROOT_FOLDER_ID)
132         {
133                 realParentId = __rootFolderId;
134         }
135         
136         int ret = favorites_bookmark_add(pTitle.get(), pUrl.get(), realParentId, 0, &bookmarkId);
137         SysTryReturn(NID_WEB, ret == FAVORITES_ERROR_NONE, ConvertErrorCode(ret), ConvertErrorCode(ret), "[%s] Propagating.", GetErrorMessage(ConvertErrorCode(ret)));
138         
139         return E_SUCCESS;
140 }
141
142
143 result 
144 _WebBookmarkImpl::SetFavicon(RecordId bookmarkId, const Bitmap& favicon)
145 {
146         SysTryReturnResult(NID_WEB, favicon.GetPixelColorFormat() == BITMAP_PIXEL_FORMAT_ARGB8888, E_INVALID_DATA, "The pixel format of a favicon must be ARGB8888");
147
148         result r = E_SUCCESS;
149         Frame* pFrame = null;
150
151         BufferInfo bufferInfo;
152         int ret = 0;
153
154         Evas_Object* pIcon = null;
155
156         Bitmap& temp = const_cast< Bitmap& >(favicon);;
157
158         Evas* pEvas = _Utility::GetEvasFromUiApp(pFrame);
159         SysTryCatch(NID_WEB, pEvas, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
160
161          pIcon = evas_object_image_filled_add(pEvas);
162         evas_object_image_colorspace_set(pIcon, EVAS_COLORSPACE_ARGB8888);
163         evas_object_image_filled_set(pIcon, EINA_TRUE);
164         evas_object_image_alpha_set(pIcon,EINA_TRUE);
165
166         r = temp.Lock(bufferInfo);
167         SysTryCatch(NID_WEB, r == E_SUCCESS, , r, "[%s] Propagating.", r);
168
169         evas_object_image_size_set(pIcon, bufferInfo.width, bufferInfo.height);
170         evas_object_image_fill_set(pIcon, 0, 0, bufferInfo.width, bufferInfo.height);
171         evas_object_image_data_set(pIcon, bufferInfo.pPixels);
172
173         r = temp.Unlock();
174         SysTryCatch(NID_WEB, r == E_SUCCESS, , r, "[%s] Propagating.", r);
175
176         ret = favorites_bookmark_set_favicon(bookmarkId, pIcon);
177         SysTryCatch(NID_WEB, ret == FAVORITES_ERROR_NONE, r = ConvertErrorCode(ret), r, "[%s] Propagating.", GetErrorMessage(r));
178
179
180         delete pFrame;
181
182         return E_SUCCESS;
183
184 CATCH:
185         delete pFrame;
186
187         return r;
188 }
189
190
191 const BookmarkItem*
192 _WebBookmarkImpl::GetItemAt(int index) const
193 {
194         SysTryReturn(NID_WEB,
195                            (index >= 0 && index < GetItemCount()), null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
196
197         return dynamic_cast < const BookmarkItem* >(__bookmarkList.GetAt(index));
198 }
199
200
201 int
202 _WebBookmarkImpl::GetItemCount(void) const
203 {
204         return __bookmarkList.GetCount();
205 }
206
207
208 int
209 _WebBookmarkImpl::GetRootFolderId(void) const
210 {
211         int folderId = 0;
212         int ret = favorites_bookmark_get_root_folder_id(&folderId);
213         SysTryReturn(NID_WEB, ret == FAVORITES_ERROR_NONE, -1, ConvertErrorCode(ret), "[%s] Propagating.", GetErrorMessage(ConvertErrorCode(ret)));
214
215         return folderId;
216 }
217
218
219 result
220 _WebBookmarkImpl::AddItem(RecordId id, const String& url, const String& title, RecordId parentId, bool isFolder)
221 {
222         result r = E_SUCCESS;
223
224         std::unique_ptr<BookmarkItem> pBookmarkItem(new (std::nothrow) BookmarkItem());
225         SysTryReturnResult(NID_WEB, pBookmarkItem.get(), E_OUT_OF_MEMORY, "Memory allocation failed");
226
227         _BookmarkItemImpl* pBookmarkItemImpl = _BookmarkItemImpl::GetInstance(pBookmarkItem.get());
228         SysTryReturn(NID_WEB, pBookmarkItemImpl, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
229
230         int realParentId = parentId;
231
232         if (parentId == __rootFolderId)
233         {
234                 realParentId = ROOT_FOLDER_ID;
235         }
236
237         pBookmarkItemImpl->SetBookmarkItem(id, url, title, realParentId, isFolder);
238
239         r = __bookmarkList.Add(*pBookmarkItem.get());
240         SysTryReturn(NID_WEB, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
241         pBookmarkItem.release();
242
243         return E_SUCCESS;
244 }
245
246
247 result
248 _WebBookmarkImpl::RemoveAll(void)
249 {
250         int ret = favorites_bookmark_delete_all_bookmarks();
251         SysTryReturn(NID_WEB, ret == FAVORITES_ERROR_NONE, ConvertErrorCode(ret), ConvertErrorCode(ret), "[%s] Propagating.", GetErrorMessage(ConvertErrorCode(ret)));
252
253         return E_SUCCESS;
254 }
255
256
257 result
258 _WebBookmarkImpl::ConvertErrorCode(int err) const
259 {
260         result r = E_SUCCESS;
261
262         switch (err)
263         {
264         case FAVORITES_ERROR_INVALID_PARAMETER:
265                 r = E_INVALID_ARG;
266                 break;
267
268         case FAVORITES_ERROR_ITEM_ALREADY_EXIST:
269                 r = E_OBJ_ALREADY_EXIST;
270                 break;
271
272         case FAVORITES_ERROR_DB_FAILED:
273         //fall through
274         default:
275                 r = E_SYSTEM;
276         }
277
278         return r;
279 }
280
281
282 _WebBookmarkImpl*
283 _WebBookmarkImpl::GetInstance(WebBookmark* pWebBookmark)
284 {
285         return pWebBookmark->__pWebBookmarkImpl;
286 }
287
288
289 const _WebBookmarkImpl*
290 _WebBookmarkImpl::GetInstance(const WebBookmark* pWebBookmark)
291 {
292         return pWebBookmark->__pWebBookmarkImpl;
293 }
294
295
296 }} // Tizen::Web