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