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