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"
29 #include "Tools/GeneralTools.h"
34 namespace tizen_browser
39 EXPORT_SERVICE(HistoryService, DOMAIN_HISTORY_SERVICE)
41 const int SEARCH_LIKE = 1;
43 HistoryService::HistoryService() : m_testDbMod(false)
45 BROWSER_LOGD("HistoryService");
48 HistoryService::~HistoryService()
52 std::shared_ptr<tizen_browser::services::StorageService> HistoryService::getStorageManager()
54 if (!m_storageManager) {
55 m_storageManager = std::dynamic_pointer_cast <
56 tizen_browser::services::StorageService,
57 tizen_browser::core::AbstractService > (
58 tizen_browser::core::ServiceManager::getInstance().getService(
59 DOMAIN_STORAGE_SERVICE));
62 M_ASSERT(m_storageManager);
63 m_storageManager->init(m_testDbMod);
65 return m_storageManager;
68 void HistoryService::setStorageServiceTestMode(bool testmode) {
69 m_testDbMod = testmode;
72 int HistoryService::getHistoryItemsCount(){
75 bp_history_rows_cond_fmt conds;
76 conds.limit = 20; //no of rows to get negative means no limitation
77 conds.offset = -1; //the first row's index
78 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
79 conds.ordering = 1; //way of ordering 0 asc 1 desc
80 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
81 conds.period_type = BP_HISTORY_DATE_TODAY;
82 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
84 BROWSER_LOGD("Error! Could not get ids!");
87 BROWSER_LOGD("[%s:%d] History Count %d", __PRETTY_FUNCTION__, __LINE__, count);
91 bool HistoryService::isDuplicate(const char* url) const
96 bp_history_rows_cond_fmt conds;
97 conds.limit = 20; //no of rows to get negative means no limitation
98 conds.offset = -1; //the first row's index
99 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
100 conds.ordering = 1; //way of ordering 0 asc 1 desc
101 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
102 conds.period_type = BP_HISTORY_DATE_TODAY;
103 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
105 BROWSER_LOGD("Error! Could not get ids!");
108 bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_DATE_CREATED);
110 for (int i = 0; i < count; i++) {
111 bp_history_info_fmt history_info;
112 bp_history_adaptor_get_info(ids[i], offset, &history_info);
113 if (!history_info.url) {
114 BROWSER_LOGD("Warning: history entry without url!");
115 } else if (!strcmp(history_info.url, url)) {
117 bp_history_adaptor_get_frequency(ids[i], &freq);
118 bp_history_adaptor_set_frequency(ids[i], freq + 1);
119 bp_history_adaptor_set_date_visited(ids[i],-1);
126 std::shared_ptr<HistoryItemVector> HistoryService::getHistoryAll()
128 return getHistoryItems(BP_HISTORY_DATE_ALL);
131 std::shared_ptr<HistoryItemVector> HistoryService::getHistoryToday()
133 return getHistoryItems(BP_HISTORY_DATE_TODAY);
136 std::shared_ptr<HistoryItemVector> HistoryService::getHistoryYesterday()
138 return getHistoryItems(BP_HISTORY_DATE_YESTERDAY);
141 std::shared_ptr<HistoryItemVector> HistoryService::getHistoryLastWeek()
143 return getHistoryItems(BP_HISTORY_DATE_LAST_7_DAYS);
146 std::shared_ptr<HistoryItemVector> HistoryService::getHistoryLastMonth()
148 return getHistoryItems(BP_HISTORY_DATE_LAST_MONTH);
150 std::shared_ptr<HistoryItemVector> HistoryService::getHistoryOlder()
152 return getHistoryItems(BP_HISTORY_DATE_OLDER);
155 std::shared_ptr<HistoryItemVector> HistoryService::getMostVisitedHistoryItems()
157 std::shared_ptr<HistoryItemVector> ret_history_list(new HistoryItemVector);
161 bp_history_rows_cond_fmt conds;
162 conds.limit = 20; //no of rows to get negative means no limitation
163 conds.offset = -1; //the first row's index
164 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
165 conds.ordering = 1; //way of ordering 0 asc 1 desc
166 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
167 conds.period_type = BP_HISTORY_DATE_TODAY;
169 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
171 BROWSER_LOGD("Error! Could not get ids!");
174 bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED | BP_HISTORY_O_THUMBNAIL);
177 for(int i = 0; i< count; i++){
179 if (0 == bp_history_adaptor_get_frequency(ids[i], &freq))
187 int maximum = freq_arr[0];
190 for(int k=1; k<=5;k++){
191 if(k > count || count == 0)
194 maximum = freq_arr[0];
197 for(int i =1;i<count;i++){
198 if (freq_arr[i] > maximum)
200 maximum = freq_arr[i];
204 index_array[j++] = position;
205 freq_arr[position] = -1;
208 for(int i = 0; i < j; i++){
209 bp_history_info_fmt history_info;
210 bp_history_adaptor_get_info(ids[index_array[i]],offset,&history_info);
212 std::shared_ptr<HistoryItem> history = std::make_shared<HistoryItem>(std::string(history_info.url));
213 history->setUrl(std::string(history_info.url ? history_info.url : ""));
214 history->setTitle(std::string(history_info.title ? history_info.title : ""));
217 if (history_info.thumbnail_length != -1) {
218 std::shared_ptr<tizen_browser::tools::BrowserImage> hi = std::make_shared<tizen_browser::tools::BrowserImage>();
219 hi->imageType = tools::BrowserImage::ImageTypePNG;
220 hi->width = history_info.thumbnail_width;
221 hi->height = history_info.thumbnail_height;
222 hi->dataSize = history_info.thumbnail_length;
223 hi->imageData = (void*)malloc(history_info.thumbnail_length);
224 memcpy(hi->imageData, (void*)history_info.thumbnail, history_info.thumbnail_length);
225 history->setThumbnail(hi);
227 BROWSER_LOGD("history thumbnail lenght is -1");
230 ret_history_list->push_back(history);
233 return ret_history_list;
236 std::shared_ptr<HistoryItemVector> HistoryService::getHistoryItemsByURL(const std::string& url, int maxItems)
238 std::string search("%" + tools::extractDomain(url) + "%"); // add SQL 'any character' signs
240 std::shared_ptr<HistoryItemVector> items(new HistoryItemVector);
243 bp_history_rows_cond_fmt conds;
244 conds.limit = maxItems; //no of rows to get negative means no limitation
245 conds.offset = -1; //the first row's index
246 conds.order_offset = BP_HISTORY_O_DATE_VISITED; // property to sort
247 conds.ordering = 1; //way of ordering 0 asc 1 desc
248 conds.period_offset = BP_HISTORY_O_DATE_VISITED;
249 conds.period_type = BP_HISTORY_DATE_ALL;
251 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, BP_HISTORY_O_URL, search.c_str(), SEARCH_LIKE);
253 BROWSER_LOGD("Error! Could not get ids!");
257 bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_DATE_VISITED);
258 for(int i = 0; i < count; i++) {
259 bp_history_info_fmt history_info;
260 bp_history_adaptor_get_info(ids[i], offset, &history_info);
262 std::shared_ptr<HistoryItem> history = std::make_shared<HistoryItem>(std::string(history_info.url));
263 history->setTitle(std::string(history_info.title ? history_info.title : ""));
265 items->push_back(history);
272 void HistoryService::addHistoryItem(std::shared_ptr<HistoryItem> his,std::shared_ptr<tizen_browser::tools::BrowserImage> thumbnail){
273 BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
274 his->setFavIcon(his->getFavIcon());
275 his->setThumbnail(thumbnail);
277 if (isDuplicate(his->getUrl().c_str()))
281 int ret = bp_history_adaptor_create(&id);
283 BROWSER_LOGE("Error! Could not create new bookmark!");
288 bp_history_rows_cond_fmt conds;
289 conds.limit = 20; //no of rows to get negative means no limitation
290 conds.offset = -1; //the first row's index
291 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
292 conds.ordering = 1; //way of ordering 0 asc 1 desc
293 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
294 conds.period_type = BP_HISTORY_DATE_TODAY;
296 ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
298 BROWSER_LOGE("Error! Could not get ids!");
301 bp_history_adaptor_set_url(id, (his->getUrl()).c_str());
302 bp_history_adaptor_set_title(id, (his->getTitle()).c_str());
303 bp_history_adaptor_set_date_visited(id,-1);
304 bp_history_adaptor_set_frequency(id, 1);
307 std::unique_ptr<tizen_browser::tools::Blob> thumb_blob = tizen_browser::tools::EflTools::getBlobPNG(thumbnail);
308 unsigned char * thumb = std::move((unsigned char*)thumb_blob->getData());
309 bp_history_adaptor_set_snapshot(id, thumbnail->width, thumbnail->height, thumb, thumb_blob->getLength());
312 std::shared_ptr<tizen_browser::tools::BrowserImage> favicon = his->getFavIcon();
314 std::unique_ptr<tizen_browser::tools::Blob> favicon_blob = tizen_browser::tools::EflTools::getBlobPNG(favicon);
315 unsigned char * fav = std::move((unsigned char*)favicon_blob->getData());
316 bp_history_adaptor_set_icon(id, favicon->width, favicon->height, fav, favicon_blob->getLength());
319 BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
323 void HistoryService::clearAllHistory()
325 bp_history_adaptor_reset();
326 history_list.clear();
330 int HistoryService::getHistoryId(const std::string & url)
332 bp_history_rows_cond_fmt conds;
335 conds.order_offset = BP_HISTORY_O_DATE_CREATED;
337 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
338 conds.period_type = BP_HISTORY_DATE_ALL;
341 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&ids_count, &conds, BP_HISTORY_O_URL, url.c_str(), 0);
343 BROWSER_LOGD("Error! Could not get ids!");
344 } else if (ids_count != 0) {
352 void HistoryService::clearURLHistory(const std::string & url)
354 int id = getHistoryId(url);
356 bp_history_adaptor_delete(id);
357 if(0 == getHistoryItemsCount())
362 std::shared_ptr<HistoryItem> HistoryService::getHistoryItem(int * ids, int idNumber)
364 bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_VISITED);
365 bp_history_info_fmt history_info;
366 bp_history_adaptor_get_info(ids[idNumber], offset, &history_info);
369 bp_history_adaptor_get_date_created(ids[idNumber], &date);
371 struct tm *item_time_info;
372 time_t item_time = (time_t) date;
373 item_time_info = localtime(&item_time);
375 int m_year = item_time_info->tm_year;
376 int m_month = item_time_info->tm_mon + 1;
377 int m_month_day = item_time_info->tm_mday;
378 int min = item_time_info->tm_min;
379 int hour = item_time_info->tm_hour;
380 int sec = item_time_info->tm_sec;
382 m_year = 2000 + m_year % 100;
384 std::shared_ptr<HistoryItem> history = std::make_shared <HistoryItem> (std::string(history_info.url));
385 boost::gregorian::date d(m_year, m_month, m_month_day);
386 boost::posix_time::ptime t(d, boost::posix_time::time_duration(hour, min, sec));
387 history->setLastVisit(t);
388 history->setUrl(std::string(history_info.url ? history_info.url : ""));
389 history->setTitle(std::string(history_info.title ? history_info.title : ""));
392 std::shared_ptr<tizen_browser::tools::BrowserImage> hi = std::make_shared<tizen_browser::tools::BrowserImage>();
393 hi->imageType = tizen_browser::tools::BrowserImage::ImageType::ImageTypePNG;
394 hi->width = history_info.thumbnail_width;
395 hi->height = history_info.thumbnail_height;
396 hi->dataSize = history_info.thumbnail_length;
397 hi->imageData = (void*) malloc(history_info.thumbnail_length);
398 memcpy(hi->imageData, (void*) history_info.thumbnail, history_info.thumbnail_length);
399 history->setThumbnail(hi);
404 std::shared_ptr<HistoryItem> HistoryService::getCurrentTab()
408 bp_history_rows_cond_fmt conds;
409 conds.limit = 20; //no of rows to get negative means no limitation
410 conds.offset = -1; //the first row's index
411 conds.order_offset = BP_HISTORY_O_DATE_VISITED; // property to sort
412 conds.ordering = 1; //way of ordering 0 asc 1 desc
413 conds.period_offset = BP_HISTORY_O_DATE_VISITED;
414 conds.period_type = BP_HISTORY_DATE_TODAY;
416 int ret = bp_history_adaptor_get_cond_ids_p(&ids , &count, &conds, 0, nullptr, 0);
418 BROWSER_LOGD("Error! Could not get ids!");
421 return getHistoryItem(ids);
424 std::shared_ptr<HistoryItemVector> HistoryService::getHistoryItems(bp_history_date_defs period)
426 std::shared_ptr<HistoryItemVector> ret_history_list(new HistoryItemVector);
430 bp_history_rows_cond_fmt conds;
431 conds.limit = 20; //no of rows to get negative means no limitation
432 conds.offset = -1; //the first row's index
433 conds.order_offset = BP_HISTORY_O_DATE_VISITED; // property to sort
434 conds.ordering = 1; //way of ordering 0 asc 1 desc
435 conds.period_offset = BP_HISTORY_O_DATE_VISITED;
436 conds.period_type = period;
438 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
440 BROWSER_LOGD("Error! Could not get ids!");
443 for(int i = 0; i< count; i++) {
444 ret_history_list->push_back(getHistoryItem(ids, i));
447 return ret_history_list;