Tizen 2.2.1
[framework/osp/web.git] / src / FWeb_WebHistoryImpl.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                FWeb_WebHistoryImplcpp
20  * @brief               The file contains the definition of _WebHistoryImpl class.
21  *
22  * This file contains the definition of _WebHistoryImpl class.
23  */
24 #include <new>
25 #include <favorites.h>
26 #include <unique_ptr.h>
27 #include <FWebHistoryItem.h>
28 #include <FWebWebHistory.h>
29 #include <FBaseSysLog.h>
30 #include "FWeb_HistoryItemImpl.h"
31 #include "FWeb_WebHistoryImpl.h"
32
33
34 using namespace Tizen::Base;
35
36
37 namespace Tizen { namespace Web
38 {
39
40
41 bool
42 _HistoryForEachCb(favorites_history_entry_s* pItem, void* pUserData)
43 {
44         SysAssertf(pItem && pUserData, "Failed to request");
45
46         result r = E_SUCCESS;
47
48         _WebHistoryImpl* pImpl = reinterpret_cast< _WebHistoryImpl* >(pUserData);
49
50         String url(pItem->address);
51         String title(pItem->title);
52
53         r = pImpl->AddItem(url, title, pItem->id);
54         SysTryReturn(NID_WEB, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
55
56         return true;
57 }
58
59
60 _WebHistoryImpl::_WebHistoryImpl(void)
61 {
62 }
63
64
65 _WebHistoryImpl::~_WebHistoryImpl(void)
66 {
67         __historyList.RemoveAll(true);
68 }
69
70
71 result
72 _WebHistoryImpl::Construct(void)
73 {
74         result r = E_SUCCESS;
75
76         r = __historyList.Construct();
77         SysTryReturn(NID_WEB, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
78
79         int ret = favorites_history_foreach(_HistoryForEachCb, this);
80         SysTryCatch(NID_WEB, ret == FAVORITES_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to Set history Callback.");
81
82         return E_SUCCESS;
83
84 CATCH:
85         __historyList.RemoveAll(true);
86
87         return r;
88 }
89
90
91 const HistoryItem*
92 _WebHistoryImpl::GetItemAt(int index) const
93 {
94         SysTryReturn(NID_WEB,
95                            (index >= 0 && index < GetItemCount()), null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index is out of range.");
96
97         return dynamic_cast < const HistoryItem* >(__historyList.GetAt(index));
98 }
99
100
101 int
102 _WebHistoryImpl::GetItemCount(void) const
103 {
104         return __historyList.GetCount();
105 }
106
107
108 result
109 _WebHistoryImpl::AddItem(const Tizen::Base::String& url, const Tizen::Base::String& title, int id)
110 {
111         result r = E_SUCCESS;
112
113         std::unique_ptr<HistoryItem> pHistoryItem(new (std::nothrow) HistoryItem());
114         SysTryReturnResult(NID_WEB, pHistoryItem.get(), E_OUT_OF_MEMORY, "Memory allocation failed");
115
116         _HistoryItemImpl* pHistoryItemImpl = _HistoryItemImpl::GetInstance(pHistoryItem.get());
117         SysTryReturn(NID_WEB_CTRL, pHistoryItemImpl, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
118
119         pHistoryItemImpl->SetHistoryItem(url, title, id);
120
121         r = __historyList.Add(*pHistoryItem.get());
122         SysTryReturn(NID_WEB, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
123         pHistoryItem.release();
124
125         return E_SUCCESS;
126 }
127
128
129 _WebHistoryImpl*
130 _WebHistoryImpl::GetInstance(WebHistory* pWebHistory)
131 {
132         return pWebHistory->__pWebHistoryImpl;
133 }
134
135
136 const _WebHistoryImpl*
137 _WebHistoryImpl::GetInstance(const WebHistory* pWebHistory)
138 {
139         return pWebHistory->__pWebHistoryImpl;
140 }
141
142
143 }} // Tizen::Web