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"
30 namespace tizen_browser
35 EXPORT_SERVICE(HistoryService, DOMAIN_HISTORY_SERVICE)
37 HistoryService::HistoryService() : m_testDbMod(false)
39 BROWSER_LOGD("HistoryService");
42 HistoryService::~HistoryService()
46 std::shared_ptr<tizen_browser::services::StorageService> HistoryService::getStorageManager()
48 if (!m_storageManager) {
49 m_storageManager = std::dynamic_pointer_cast <
50 tizen_browser::services::StorageService,
51 tizen_browser::core::AbstractService > (
52 tizen_browser::core::ServiceManager::getInstance().getService(
53 DOMAIN_STORAGE_SERVICE));
56 M_ASSERT(m_storageManager);
57 m_storageManager->init(m_testDbMod);
59 return m_storageManager;
62 void HistoryService::setStorageServiceTestMode(bool testmode) {
63 m_testDbMod = testmode;
66 int HistoryService::getHistoryItemsCount(){
67 return history_list.size();
70 static int __get_duplicated_ids_p(int **ids, int *count, const int limit, const int offset,
71 const bp_history_offset order_column_offset, const int ordering,
72 const bp_history_offset check_column_offset,
73 const char *keyword, const int is_like)
75 bp_history_rows_cond_fmt conds;
76 memset(&conds, 0x00, sizeof(bp_history_rows_cond_fmt));
79 conds.offset = offset;
80 conds.ordering = ordering;
81 conds.order_offset = order_column_offset;
82 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
83 conds.period_type = BP_HISTORY_DATE_ALL;
85 return bp_history_adaptor_get_cond_ids_p
93 void HistoryService::addHistoryItem(std::shared_ptr<HistoryItem> his){
95 BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
96 his->setFavIcon(his->getFavIcon());
97 std::shared_ptr<tizen_browser::tools::BrowserImage> favicon = his->getFavIcon();
100 int ret = bp_history_adaptor_create(&id);
102 BROWSER_LOGE("Error! Could not create new bookmark!");
110 bp_history_rows_cond_fmt conds;
111 conds.limit = 20; //no of rows to get negative means no limitation
113 conds.offset = -1; //the first row's index
114 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
116 conds.ordering = 1; //way of ordering 0 asc 1 desc
118 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
119 conds.period_type = BP_HISTORY_DATE_TODAY;
121 ret = bp_history_adaptor_get_cond_ids_p(id1 ,count1, &conds, 0, NULL, 0);
123 BROWSER_LOGE("Error! Could not get ids!");
126 bp_history_adaptor_set_url(id, (his->getUrl()).c_str());
127 bp_history_adaptor_set_title(id, (his->getTitle()).c_str());
128 bp_history_adaptor_set_date_visited(id,-1);
130 std::unique_ptr<tizen_browser::tools::Blob> favicon_blob = tizen_browser::tools::EflTools::getBlobPNG(favicon);
131 unsigned char * fav = std::move((unsigned char*)favicon_blob->getData());
132 bp_history_adaptor_set_icon(id, favicon->width, favicon->height, fav, favicon_blob->getLength());
133 history_list.push_back(his);
135 BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
139 void HistoryService::insertOrRefresh(std::shared_ptr<HistoryItem> hi) {
140 getStorageManager()->insertOrRefresh(hi);
144 * @throws HistoryException on error
146 void HistoryService::clearAllHistory()
148 bp_history_adaptor_reset();
149 history_list.clear();
152 int HistoryService::getHistoryId(const std::string & url)
154 bp_history_rows_cond_fmt conds;
157 conds.order_offset = BP_HISTORY_O_DATE_CREATED;
159 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
160 conds.period_type = BP_HISTORY_DATE_ALL;
163 int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&ids_count, &conds, BP_HISTORY_O_URL, url.c_str(), 0);
173 * @throws HistoryException on error
175 void HistoryService::clearURLHistory(const std::string & url)
177 int id = getHistoryId(url);
179 bp_history_adaptor_delete(id);
180 if (0 == (getHistoryItemsCount() - 1)) {
182 history_list.clear();
187 HistoryItemVector& HistoryService::getHistoryItems(int historyDepthInDays, int maxItems)
189 history_list.clear();
196 bp_history_rows_cond_fmt conds;
197 conds.limit = 20; //no of rows to get negative means no limitation
199 conds.offset = -1; //the first row's index
200 conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
202 conds.ordering = 1; //way of ordering 0 asc 1 desc
204 conds.period_offset = BP_HISTORY_O_DATE_CREATED;
206 conds.period_type = BP_HISTORY_DATE_TODAY;
208 int ret = bp_history_adaptor_get_cond_ids_p(id1 ,count1, &conds, 0, NULL, 0);
210 BROWSER_LOGD("Error! Could not get ids!");
213 bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED);
215 for(int i = 0; i< (*count1); i++){
216 bp_history_info_fmt history_info;
217 bp_history_adaptor_get_info(ids[i],offset,&history_info);
220 bp_history_adaptor_get_date_created(ids[i], &date);
222 struct tm *item_time_info;
223 time_t item_time = (time_t)date;
224 item_time_info = localtime(&item_time);
226 int m_year = item_time_info->tm_year;
227 int m_month = item_time_info->tm_mon + 1;
228 int m_day = item_time_info->tm_yday;
229 int m_month_day = item_time_info->tm_mday;
231 int min = item_time_info->tm_min;
232 int hour= item_time_info->tm_hour;
233 int sec = item_time_info->tm_sec;
234 m_year = 2000 + m_year % 100;
236 std::shared_ptr<HistoryItem> history = std::make_shared<HistoryItem>(std::string(history_info.url));
237 boost::gregorian::date d(m_year,m_month,m_month_day);
238 boost::posix_time::ptime t(d,boost::posix_time::time_duration(hour,min,sec));
239 history->setLastVisit(t);
240 history->setUrl(std::string(history_info.url ? history_info.url : ""));
241 history->setTitle(std::string(history_info.title ? history_info.title : ""));
242 history_list.push_back(history);
250 int HistoryService::getHistoryVisitCounter(const std::string & url)
252 return getStorageManager()->getHistoryVisitCounter(url);