2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <BrowserAssert.h>
19 #include <boost/date_time/posix_time/posix_time.hpp>
20 #include <boost/date_time/date.hpp>
21 #include <boost/date_time/date_defs.hpp>
22 #include <boost/date_time/gregorian/gregorian.hpp>
24 #include "ServiceManager.h"
25 #include "HistoryService.h"
26 #include "HistoryItem.h"
27 #include "AbstractWebEngine.h"
33 namespace tizen_browser
38 EXPORT_SERVICE(HistoryService, DOMAIN_HISTORY_SERVICE)
40 HistoryService::HistoryService() : m_testDbMod(false)
42 BROWSER_LOGD("HistoryService");
45 HistoryService::~HistoryService()
49 std::shared_ptr<tizen_browser::services::StorageService> HistoryService::getStorageManager()
51 if (!m_storageManager) {
52 m_storageManager = std::dynamic_pointer_cast <
53 tizen_browser::services::StorageService,
54 tizen_browser::core::AbstractService > (
55 tizen_browser::core::ServiceManager::getInstance().getService(
56 DOMAIN_STORAGE_SERVICE));
59 M_ASSERT(m_storageManager);
60 m_storageManager->init(m_testDbMod);
62 return m_storageManager;
65 void HistoryService::setStorageServiceTestMode(bool testmode) {
66 m_testDbMod = testmode;
69 int HistoryService::getHistoryItemsCount(){
72 bp_history_rows_cond_fmt conds;
73 conds.limit = 20; //no of rows to get negative means no limitation
74 conds.offset = -1; //the first row's index
75 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
76 conds.ordering = 1; //way of ordering 0 asc 1 desc
77 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
78 conds.period_type = BP_HISTORY_DATE_TODAY;
79 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
81 BROWSER_LOGD("Error! Could not get ids!");
84 BROWSER_LOGD("[%s:%d] History Count %d", __PRETTY_FUNCTION__, __LINE__, count);
88 static int __get_duplicated_ids_p(int **ids, int *count, const int limit, const int offset,
89 const bp_history_offset order_column_offset, const int ordering,
90 const bp_history_offset check_column_offset,
91 const char *keyword, const int is_like)
93 bp_history_rows_cond_fmt conds;
95 conds.offset = offset;
96 conds.ordering = ordering;
97 conds.order_offset = order_column_offset;
98 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
99 conds.period_type = BP_HISTORY_DATE_ALL;
101 return bp_history_adaptor_get_cond_ids_p(ids, count, &conds, check_column_offset, keyword, is_like);
104 bool isDuplicate(const char* title)
108 bp_history_rows_cond_fmt conds;
109 conds.limit = 20; //no of rows to get negative means no limitation
110 conds.offset = -1; //the first row's index
111 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
112 conds.ordering = 1; //way of ordering 0 asc 1 desc
113 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
114 conds.period_type = BP_HISTORY_DATE_TODAY;
115 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
117 BROWSER_LOGD("Error! Could not get ids!");
120 bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED);
122 for(int i = 0; i< count; i++){
123 bp_history_info_fmt history_info;
124 bp_history_adaptor_get_info(ids[i],offset,&history_info);
125 if(!strcmp(history_info.title, title)) {
127 bp_history_adaptor_get_frequency(ids[i], &freq);
128 bp_history_adaptor_set_frequency(ids[i], freq + 1);
137 HistoryItemVector& HistoryService::getMostVisitedHistoryItems()
139 history_list.clear();
143 bp_history_rows_cond_fmt conds;
144 conds.limit = 20; //no of rows to get negative means no limitation
145 conds.offset = -1; //the first row's index
146 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
147 conds.ordering = 1; //way of ordering 0 asc 1 desc
148 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
149 conds.period_type = BP_HISTORY_DATE_TODAY;
151 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
153 BROWSER_LOGD("Error! Could not get ids!");
156 bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED);
159 for(int i = 0; i< count; i++){
160 bp_history_info_fmt history_info;
161 bp_history_adaptor_get_info(ids[i],offset,&history_info);
164 if (0 == bp_history_adaptor_get_frequency(ids[i], &freq))
172 int maximum = freq_arr[0];
175 for(int k=1; k<=5;k++){
176 if(k > count || count == 0)
179 maximum = freq_arr[0];
182 for(int i =1;i<count;i++){
183 if (freq_arr[i] > maximum)
185 maximum = freq_arr[i];
189 index_array[j++] = position;
190 freq_arr[position] = -1;
193 for(int i = 0; i < j; i++){
194 bp_history_info_fmt history_info;
195 bp_history_adaptor_get_info(ids[index_array[i]],offset,&history_info);
197 std::shared_ptr<HistoryItem> history = std::make_shared<HistoryItem>(std::string(history_info.url));
198 history->setUrl(std::string(history_info.url ? history_info.url : ""));
199 history->setTitle(std::string(history_info.title ? history_info.title : ""));
202 std::shared_ptr<tizen_browser::tools::BrowserImage> hi = std::make_shared<tizen_browser::tools::BrowserImage>();
203 hi->imageType = tizen_browser::tools::BrowserImage::ImageType::ImageTypePNG;
204 hi->width = history_info.thumbnail_width;
205 hi->height = history_info.thumbnail_height;
206 hi->dataSize = history_info.thumbnail_length;
207 hi->imageData = (void*)malloc(history_info.thumbnail_length);
208 memcpy(hi->imageData, (void*)history_info.thumbnail, history_info.thumbnail_length);
209 history->setThumbnail(hi);
211 history_list.push_back(history);
217 void HistoryService::addHistoryItem(std::shared_ptr<HistoryItem> his,std::shared_ptr<tizen_browser::tools::BrowserImage> thumbnail){
218 BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
219 his->setFavIcon(his->getFavIcon());
220 his->setThumbnail(thumbnail);
222 if(isDuplicate(his->getTitle().c_str()))
226 int ret = bp_history_adaptor_create(&id);
228 BROWSER_LOGE("Error! Could not create new bookmark!");
233 bp_history_rows_cond_fmt conds;
234 conds.limit = 20; //no of rows to get negative means no limitation
235 conds.offset = -1; //the first row's index
236 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
237 conds.ordering = 1; //way of ordering 0 asc 1 desc
238 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
239 conds.period_type = BP_HISTORY_DATE_TODAY;
241 ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
243 BROWSER_LOGE("Error! Could not get ids!");
246 bp_history_adaptor_set_url(id, (his->getUrl()).c_str());
247 bp_history_adaptor_set_title(id, (his->getTitle()).c_str());
248 bp_history_adaptor_set_date_visited(id,-1);
249 bp_history_adaptor_set_frequency(id, 1);
252 std::unique_ptr<tizen_browser::tools::Blob> thumb_blob = tizen_browser::tools::EflTools::getBlobPNG(thumbnail);
253 unsigned char * thumb = std::move((unsigned char*)thumb_blob->getData());
254 bp_history_adaptor_set_snapshot(id, thumbnail->width, thumbnail->height, thumb, thumb_blob->getLength());
257 std::shared_ptr<tizen_browser::tools::BrowserImage> favicon = his->getFavIcon();
259 std::unique_ptr<tizen_browser::tools::Blob> favicon_blob = tizen_browser::tools::EflTools::getBlobPNG(favicon);
260 unsigned char * fav = std::move((unsigned char*)favicon_blob->getData());
261 bp_history_adaptor_set_icon(id, favicon->width, favicon->height, fav, favicon_blob->getLength());
264 BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
269 void HistoryService::insertOrRefresh(std::shared_ptr<HistoryItem> hi) {
271 * No browser- provider implementation till now. Needs to be done
273 //getStorageManager()->insertOrRefresh(hi);
277 * @throws HistoryException on error
279 void HistoryService::clearAllHistory()
281 bp_history_adaptor_reset();
282 history_list.clear();
286 int HistoryService::getHistoryId(const std::string & url)
288 bp_history_rows_cond_fmt conds;
291 conds.order_offset = BP_HISTORY_O_DATE_CREATED;
293 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
294 conds.period_type = BP_HISTORY_DATE_ALL;
297 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&ids_count, &conds, BP_HISTORY_O_URL, url.c_str(), 0);
308 * @throws HistoryException on error
310 void HistoryService::clearURLHistory(const std::string & url)
312 int id = getHistoryId(url);
314 bp_history_adaptor_delete(id);
315 if(0 == getHistoryItemsCount())
321 HistoryItemVector& HistoryService::getHistoryItems(int historyDepthInDays, int maxItems)
323 history_list.clear();
327 bp_history_rows_cond_fmt conds;
328 conds.limit = 20; //no of rows to get negative means no limitation
329 conds.offset = -1; //the first row's index
330 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
331 conds.ordering = 1; //way of ordering 0 asc 1 desc
332 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
333 conds.period_type = BP_HISTORY_DATE_TODAY;
335 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
337 BROWSER_LOGD("Error! Could not get ids!");
340 bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED);
342 for(int i = 0; i< count; i++){
343 bp_history_info_fmt history_info;
344 bp_history_adaptor_get_info(ids[i],offset,&history_info);
347 bp_history_adaptor_get_date_created(ids[i], &date);
349 struct tm *item_time_info;
350 time_t item_time = (time_t)date;
351 item_time_info = localtime(&item_time);
353 int m_year = item_time_info->tm_year;
354 int m_month = item_time_info->tm_mon + 1;
355 int m_day = item_time_info->tm_yday;
356 int m_month_day = item_time_info->tm_mday;
358 int min = item_time_info->tm_min;
359 int hour= item_time_info->tm_hour;
360 int sec = item_time_info->tm_sec;
362 m_year = 2000 + m_year % 100;
364 std::shared_ptr<HistoryItem> history = std::make_shared<HistoryItem>(std::string(history_info.url));
365 boost::gregorian::date d(m_year,m_month,m_month_day);
366 boost::posix_time::ptime t(d,boost::posix_time::time_duration(hour,min,sec));
367 history->setLastVisit(t);
368 history->setUrl(std::string(history_info.url ? history_info.url : ""));
369 history->setTitle(std::string(history_info.title ? history_info.title : ""));
372 std::shared_ptr<tizen_browser::tools::BrowserImage> hi = std::make_shared<tizen_browser::tools::BrowserImage>();
373 hi->imageType = tizen_browser::tools::BrowserImage::ImageType::ImageTypePNG;
374 hi->width = history_info.thumbnail_width;
375 hi->height = history_info.thumbnail_height;
376 hi->dataSize = history_info.thumbnail_length;
377 hi->imageData = (void*)malloc(history_info.thumbnail_length);
378 memcpy(hi->imageData, (void*)history_info.thumbnail, history_info.thumbnail_length);
379 history->setThumbnail(hi);
381 history_list.push_back(history);
387 int HistoryService::getHistoryVisitCounter(const std::string & url)
389 * No browser- provider implementation till now. Needs to be done
391 // return getStorageManager()->getHistoryVisitCounter(url);