History Implementation using Browser Provider.
[profile/tv/apps/web/browser.git] / services / HistoryService / src / HistoryService.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <string>
18 #include <BrowserAssert.h>
19 #include <boost/date_time/posix_time/posix_time.hpp>
20
21 #include "ServiceManager.h"
22 #include "HistoryService.h"
23 #include "HistoryItem.h"
24 #include "AbstractWebEngine.h"
25 namespace tizen_browser
26 {
27 namespace services
28 {
29
30 EXPORT_SERVICE(HistoryService, DOMAIN_HISTORY_SERVICE)
31
32 HistoryService::HistoryService() : m_testDbMod(false)
33 {
34     BROWSER_LOGD("HistoryService");
35 }
36
37 HistoryService::~HistoryService()
38 {
39 }
40
41 std::shared_ptr<tizen_browser::services::StorageService> HistoryService::getStorageManager()
42 {
43     if (!m_storageManager) {
44         m_storageManager = std::dynamic_pointer_cast <
45                            tizen_browser::services::StorageService,
46                            tizen_browser::core::AbstractService > (
47                                tizen_browser::core::ServiceManager::getInstance().getService(
48                                    DOMAIN_STORAGE_SERVICE));
49     }
50
51     M_ASSERT(m_storageManager);
52     m_storageManager->init(m_testDbMod);
53
54     return m_storageManager;
55 }
56
57 void HistoryService::setStorageServiceTestMode(bool testmode) {
58         m_testDbMod = testmode;
59 }
60
61 int HistoryService::getHistoryItemsCount(){
62     return 1;
63     //return getStorageManager()->getHistoryItemsCount();
64 }
65
66 static int __get_duplicated_ids_p(int **ids, int *count, const int limit, const int offset,
67                 const bp_history_offset order_column_offset, const int ordering,
68                 const bp_history_offset check_column_offset,
69                 const char *keyword, const int is_like)
70 {
71     bp_history_rows_cond_fmt conds;
72     memset(&conds, 0x00, sizeof(bp_history_rows_cond_fmt));
73
74     conds.limit = limit;
75     conds.offset = offset;
76     conds.ordering = ordering;
77     conds.order_offset = order_column_offset;
78     conds.period_offset = BP_HISTORY_O_DATE_CREATED;
79     conds.period_type = BP_HISTORY_DATE_ALL;
80
81     return bp_history_adaptor_get_cond_ids_p
82         (ids, count,
83          &conds,
84          check_column_offset,
85          keyword,
86          is_like);
87 }
88
89 void HistoryService::addHistoryItem(std::shared_ptr<HistoryItem> his){
90
91     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
92     his->setFavIcon(his->getFavIcon());
93     std::shared_ptr<tizen_browser::tools::BrowserImage> favicon = his->getFavIcon();
94
95     int id = -1;
96     int ret = bp_history_adaptor_create(&id);
97     if (ret<0){
98         BROWSER_LOGE("Error! Could not create new bookmark!");
99     }
100
101     int *ids=NULL;
102     int count=-1;
103     int **id1=&ids;
104     int *count1=&count;
105
106     bp_history_rows_cond_fmt conds;
107     conds.limit = 20;  //no of rows to get negative means no limitation
108
109     conds.offset = -1;   //the first row's index
110     conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
111
112     conds.ordering = 1; //way of ordering 0 asc 1 desc
113
114     conds.period_offset = BP_HISTORY_O_DATE_CREATED;
115     conds.period_type = BP_HISTORY_DATE_TODAY;
116
117     ret = bp_history_adaptor_get_cond_ids_p(id1 ,count1, &conds, 0, NULL, 0);
118     if (ret<0){
119         BROWSER_LOGE("Error! Could not get ids!");
120     }
121
122     bp_history_adaptor_set_url(id, (his->getUrl()).c_str());
123     bp_history_adaptor_set_title(id, (his->getTitle()).c_str());
124
125
126     std::unique_ptr<tizen_browser::tools::Blob> favicon_blob = tizen_browser::tools::EflTools::getBlobPNG(favicon);
127     unsigned char * fav = std::move((unsigned char*)favicon_blob->getData());
128     bp_history_adaptor_set_icon(id, favicon->width, favicon->height, fav, favicon_blob->getLength());
129
130     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
131 }
132
133
134 void HistoryService::insertOrRefresh(std::shared_ptr<HistoryItem> hi) {
135         getStorageManager()->insertOrRefresh(hi);
136 }
137
138 /**
139  * @throws HistoryException on error
140  */
141 void HistoryService::clearAllHistory()
142 {
143     getStorageManager()->deleteHistory();
144 }
145
146 /**
147  * @throws HistoryException on error
148  */
149 void HistoryService::clearURLHistory(const std::string & url)
150 {
151     getStorageManager()->deleteHistory(url);
152     if(0 == getHistoryItemsCount()){
153         historyEmpty(true);
154     }
155 }
156
157
158 HistoryItemVector& HistoryService::getHistoryItems(int historyDepthInDays, int maxItems)
159 {
160     history_list.clear();
161
162     int *ids=NULL;
163     int count=-1;
164     int **id1=&ids;
165     int *count1=&count;
166
167     bp_history_rows_cond_fmt conds;
168     conds.limit = 20;  //no of rows to get negative means no limitation
169
170     conds.offset = -1;   //the first row's index
171     conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
172
173     conds.ordering = 1; //way of ordering 0 asc 1 desc
174
175     conds.period_offset = BP_HISTORY_O_DATE_CREATED;
176
177     conds.period_type = BP_HISTORY_DATE_TODAY;
178
179     int ret = bp_history_adaptor_get_cond_ids_p(id1 ,count1, &conds, 0, NULL, 0);
180     if (ret<0){
181         BROWSER_LOGD("Error! Could not get ids!");
182     }
183
184     bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON);
185
186     for(int i = 0; i< (*count1); i++){
187         bp_history_info_fmt history_info;
188         bp_history_adaptor_get_info(ids[i],offset,&history_info);
189
190         std::shared_ptr<HistoryItem> history = std::make_shared<HistoryItem>(std::string(history_info.url));
191         history_list.push_back(history);
192     }
193     ids = NULL;
194     free(ids);
195
196     return history_list;
197 }
198
199 int HistoryService::getHistoryVisitCounter(const std::string & url)
200 {
201     return getStorageManager()->getHistoryVisitCounter(url);
202 }
203
204 }
205 }