Initialize Tizen 2.3
[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         __bookmarkList.RemoveAll(true);
76 }
77
78
79 result
80 _WebBookmarkImpl::Construct(void)
81 {
82         result r = E_SUCCESS;
83
84         int ret = favorites_bookmark_get_root_folder_id(&__rootFolderId);
85         SysTryReturnResult(NID_WEB, ret == FAVORITES_ERROR_NONE, E_SYSTEM, "Failed to get root folder id.");
86
87         r = __bookmarkList.Construct();
88         SysTryReturn(NID_WEB, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
89
90         ret = favorites_bookmark_foreach(_BookmarkForEachCb, this);
91         SysTryCatch(NID_WEB, ret == FAVORITES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to set bookmark callback.");
92
93         return E_SUCCESS;
94
95 CATCH:
96         __bookmarkList.RemoveAll(true);
97
98         return r;
99 }
100
101
102 result
103 _WebBookmarkImpl::AddFolder(const String& title, RecordId parentId, RecordId& folderId)
104 {
105         std::unique_ptr<char[]> pTitle(_StringConverter::CopyToCharArrayN(title));
106         SysTryReturn(NID_WEB, pTitle.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
107
108         int realParentId = parentId;
109
110         if (parentId == ROOT_FOLDER_ID)
111         {
112                 realParentId = __rootFolderId;
113         }
114
115         int ret = favorites_bookmark_add(pTitle.get(), null, realParentId, 1, &folderId);
116         SysTryReturn(NID_WEB, ret == FAVORITES_ERROR_NONE, ConvertErrorCode(ret), ConvertErrorCode(ret), "[%s] Propagating.", GetErrorMessage(ConvertErrorCode(ret)));
117
118         return E_SUCCESS;
119 }
120
121
122 result
123 _WebBookmarkImpl::AddBookmark(const String& title, const String& url, RecordId parentId, RecordId& bookmarkId)
124 {
125         std::unique_ptr<char[]> pTitle(_StringConverter::CopyToCharArrayN(title));
126         SysTryReturn(NID_WEB, pTitle.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
127         std::unique_ptr<char[]> pUrl(_StringConverter::CopyToCharArrayN(url));
128         SysTryReturn(NID_WEB, pUrl.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
129
130         int realParentId = parentId;
131
132         if (parentId == ROOT_FOLDER_ID)
133         {
134                 realParentId = __rootFolderId;
135         }
136         
137         int ret = favorites_bookmark_add(pTitle.get(), pUrl.get(), realParentId, 0, &bookmarkId);
138         SysTryReturn(NID_WEB, ret == FAVORITES_ERROR_NONE, ConvertErrorCode(ret), ConvertErrorCode(ret), "[%s] Propagating.", GetErrorMessage(ConvertErrorCode(ret)));
139         
140         return E_SUCCESS;
141 }
142
143
144 result 
145 _WebBookmarkImpl::SetFavicon(RecordId bookmarkId, const Bitmap& favicon)
146 {
147         SysTryReturnResult(NID_WEB, favicon.GetPixelColorFormat() == BITMAP_PIXEL_FORMAT_ARGB8888, E_INVALID_DATA, "The pixel format of a favicon must be ARGB8888");
148
149         result r = E_SUCCESS;
150         Frame* pFrame = null;
151
152         BufferInfo bufferInfo;
153         int ret = 0;
154
155         Evas_Object* pIcon = null;
156
157         Bitmap& temp = const_cast< Bitmap& >(favicon);
158
159         Evas* pEvas = _Utility::GetEvasFromUiApp(pFrame);
160         SysTryCatch(NID_WEB, pEvas, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
161
162          pIcon = evas_object_image_filled_add(pEvas);
163         evas_object_image_colorspace_set(pIcon, EVAS_COLORSPACE_ARGB8888);
164         evas_object_image_filled_set(pIcon, EINA_TRUE);
165         evas_object_image_alpha_set(pIcon,EINA_TRUE);
166
167         r = temp.Lock(bufferInfo);
168         SysTryCatch(NID_WEB, r == E_SUCCESS, , r, "[%s] Propagating.", r);
169
170         evas_object_image_size_set(pIcon, bufferInfo.width, bufferInfo.height);
171         evas_object_image_fill_set(pIcon, 0, 0, bufferInfo.width, bufferInfo.height);
172         evas_object_image_data_set(pIcon, bufferInfo.pPixels);
173
174         r = temp.Unlock();
175         SysTryCatch(NID_WEB, r == E_SUCCESS, , r, "[%s] Propagating.", r);
176
177         ret = favorites_bookmark_set_favicon(bookmarkId, pIcon);
178         SysTryCatch(NID_WEB, ret == FAVORITES_ERROR_NONE, r = ConvertErrorCode(ret), r, "[%s] Propagating.", GetErrorMessage(r));
179
180
181         delete pFrame;
182
183         return E_SUCCESS;
184
185 CATCH:
186         delete pFrame;
187
188         return r;
189 }
190
191
192 const BookmarkItem*
193 _WebBookmarkImpl::GetItemAt(int index) const
194 {
195         SysTryReturn(NID_WEB,
196                            (index >= 0 && index < GetItemCount()), null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
197
198         return dynamic_cast < const BookmarkItem* >(__bookmarkList.GetAt(index));
199 }
200
201
202 int
203 _WebBookmarkImpl::GetItemCount(void) const
204 {
205         return __bookmarkList.GetCount();
206 }
207
208
209 int
210 _WebBookmarkImpl::GetRootFolderId(void) const
211 {
212         int folderId = 0;
213         int ret = favorites_bookmark_get_root_folder_id(&folderId);
214         SysTryReturn(NID_WEB, ret == FAVORITES_ERROR_NONE, -1, ConvertErrorCode(ret), "[%s] Propagating.", GetErrorMessage(ConvertErrorCode(ret)));
215
216         return folderId;
217 }
218
219
220 result
221 _WebBookmarkImpl::AddItem(RecordId id, const String& url, const String& title, RecordId parentId, bool isFolder)
222 {
223         result r = E_SUCCESS;
224
225         std::unique_ptr<BookmarkItem> pBookmarkItem(new (std::nothrow) BookmarkItem());
226         SysTryReturnResult(NID_WEB, pBookmarkItem.get(), E_OUT_OF_MEMORY, "Memory allocation failed");
227
228         _BookmarkItemImpl* pBookmarkItemImpl = _BookmarkItemImpl::GetInstance(pBookmarkItem.get());
229         SysTryReturn(NID_WEB, pBookmarkItemImpl, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
230
231         int realParentId = parentId;
232
233         if (parentId == __rootFolderId)
234         {
235                 realParentId = ROOT_FOLDER_ID;
236         }
237
238         pBookmarkItemImpl->SetBookmarkItem(id, url, title, realParentId, isFolder);
239
240         r = __bookmarkList.Add(*pBookmarkItem.get());
241         SysTryReturn(NID_WEB, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
242         pBookmarkItem.release();
243
244         return E_SUCCESS;
245 }
246
247
248 result
249 _WebBookmarkImpl::RemoveAll(void)
250 {
251         int ret = favorites_bookmark_delete_all_bookmarks();
252         SysTryReturn(NID_WEB, ret == FAVORITES_ERROR_NONE, ConvertErrorCode(ret), ConvertErrorCode(ret), "[%s] Propagating.", GetErrorMessage(ConvertErrorCode(ret)));
253
254         return E_SUCCESS;
255 }
256
257
258 result
259 _WebBookmarkImpl::ConvertErrorCode(int err) const
260 {
261         result r = E_SUCCESS;
262
263         switch (err)
264         {
265         case FAVORITES_ERROR_INVALID_PARAMETER:
266                 r = E_INVALID_ARG;
267                 break;
268
269         case FAVORITES_ERROR_ITEM_ALREADY_EXIST:
270                 r = E_OBJ_ALREADY_EXIST;
271                 break;
272
273         case FAVORITES_ERROR_DB_FAILED:
274         //fall through
275         default:
276                 r = E_SYSTEM;
277         }
278
279         return r;
280 }
281
282
283 _WebBookmarkImpl*
284 _WebBookmarkImpl::GetInstance(WebBookmark* pWebBookmark)
285 {
286         return pWebBookmark->__pWebBookmarkImpl;
287 }
288
289
290 const _WebBookmarkImpl*
291 _WebBookmarkImpl::GetInstance(const WebBookmark* pWebBookmark)
292 {
293         return pWebBookmark->__pWebBookmarkImpl;
294 }
295
296
297 }} // Tizen::Web