Task TT-75 Implement "Main page loading UI" view
[profile/tv/apps/web/browser.git] / services / HistoryService / 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 #include <boost/date_time/date.hpp>
21 #include <boost/date_time/date_defs.hpp>
22 #include <boost/date_time/gregorian/gregorian.hpp>
23
24 #include "ServiceManager.h"
25 #include "HistoryService.h"
26 #include "HistoryItem.h"
27 #include "AbstractWebEngine.h"
28 #include "EflTools.h"
29
30
31
32
33 namespace tizen_browser
34 {
35 namespace services
36 {
37
38 EXPORT_SERVICE(HistoryService, DOMAIN_HISTORY_SERVICE)
39
40 HistoryService::HistoryService() : m_testDbMod(false)
41 {
42     BROWSER_LOGD("HistoryService");
43 }
44
45 HistoryService::~HistoryService()
46 {
47 }
48
49 std::shared_ptr<tizen_browser::services::StorageService> HistoryService::getStorageManager()
50 {
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));
57     }
58
59     M_ASSERT(m_storageManager);
60     m_storageManager->init(m_testDbMod);
61
62     return m_storageManager;
63 }
64
65 void HistoryService::setStorageServiceTestMode(bool testmode) {
66         m_testDbMod = testmode;
67 }
68
69 int HistoryService::getHistoryItemsCount(){
70     int *ids = nullptr;
71     int count=0;
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);
80     if (ret<0){
81         BROWSER_LOGD("Error! Could not get ids!");
82     }
83
84     BROWSER_LOGD("[%s:%d] History Count %d", __PRETTY_FUNCTION__, __LINE__, count);
85     return count;
86 }
87
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)
92 {
93     bp_history_rows_cond_fmt conds;
94     conds.limit = limit;
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;
100
101     return bp_history_adaptor_get_cond_ids_p(ids, count, &conds, check_column_offset, keyword, is_like);
102 }
103
104 bool isDuplicate(const char* title)
105 {
106     int *ids=nullptr;
107     int count=0;
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);
116     if (ret<0){
117         BROWSER_LOGD("Error! Could not get ids!");
118     }
119
120     bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED);
121
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)) {
126                 int freq;
127                 bp_history_adaptor_get_frequency(ids[i], &freq);
128                 bp_history_adaptor_set_frequency(ids[i], freq + 1);
129                 return true;
130         }
131     }
132     return false;
133
134 }
135
136
137 HistoryItemVector& HistoryService::getMostVisitedHistoryItems()
138 {
139     history_list.clear();
140
141     int *ids=nullptr;
142     int count=-1;
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;
150
151     int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
152     if (ret<0){
153         BROWSER_LOGD("Error! Could not get ids!");
154     }
155
156     bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED);
157
158     int freq_arr[1000];
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);
162
163         int freq;
164         if (0 == bp_history_adaptor_get_frequency(ids[i], &freq))
165         {
166             freq_arr[i] = freq;
167         }
168     }
169
170     int index_array[6];
171     int j=0;
172     int maximum = freq_arr[0];
173     int position = 0;
174
175     for(int k=1; k<=5;k++){
176         if(k > count || count == 0)
177             break;
178
179     maximum = freq_arr[0];
180     position = 0;
181
182     for(int i =1;i<count;i++){
183         if (freq_arr[i] > maximum)
184             {
185                 maximum  = freq_arr[i];
186                 position = i;
187             }
188          }
189         index_array[j++] = position;
190         freq_arr[position] = -1;
191     }
192
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);
196
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 : ""));
200
201         //thumbail
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);
210
211         history_list.push_back(history);
212     }
213     free(ids);
214     return history_list;
215 }
216
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);
221
222     if(isDuplicate(his->getTitle().c_str()))
223         return;
224
225     int id = -1;
226     int ret = bp_history_adaptor_create(&id);
227     if (ret<0){
228         BROWSER_LOGE("Error! Could not create new bookmark!");
229     }
230
231     int *ids=nullptr;
232     int count=-1;
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;
240
241     ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
242     if (ret<0){
243         BROWSER_LOGE("Error! Could not get ids!");
244     }
245
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);
250
251     if (thumbnail) {
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());
255     }
256
257     std::shared_ptr<tizen_browser::tools::BrowserImage> favicon = his->getFavIcon();
258     if (favicon) {
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());
262     }
263
264     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
265     historyAdded(his);
266 }
267
268
269 void HistoryService::insertOrRefresh(std::shared_ptr<HistoryItem> hi) {
270     /**
271       *  No browser- provider implementation till now. Needs to be done
272      */
273     //getStorageManager()->insertOrRefresh(hi);
274 }
275
276 /**
277  * @throws HistoryException on error
278  */
279 void HistoryService::clearAllHistory()
280 {
281     bp_history_adaptor_reset();
282     history_list.clear();
283     historyAllDeleted();
284 }
285
286 int HistoryService::getHistoryId(const std::string & url)
287 {
288     bp_history_rows_cond_fmt conds;
289     conds.limit = -1;
290     conds.offset = 0;
291     conds.order_offset = BP_HISTORY_O_DATE_CREATED;
292     conds.ordering = 0;
293     conds.period_offset = BP_HISTORY_O_DATE_CREATED;
294     conds.period_type = BP_HISTORY_DATE_ALL;
295     int *ids = nullptr;
296     int ids_count = 0;
297     int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&ids_count, &conds, BP_HISTORY_O_URL, url.c_str(), 0);
298     if (ids_count!=0){
299         int i = *ids;
300         free(ids);
301         return i;
302     }
303     return 0;
304 }
305
306
307 /**
308  * @throws HistoryException on error
309  */
310 void HistoryService::clearURLHistory(const std::string & url)
311 {
312     int id = getHistoryId(url);
313     if (id!=0)
314         bp_history_adaptor_delete(id);
315     if(0 == getHistoryItemsCount())
316         historyEmpty(true);
317     historyDeleted(url);
318 }
319
320
321 HistoryItemVector& HistoryService::getHistoryItems(int historyDepthInDays, int maxItems)
322 {
323     history_list.clear();
324
325     int *ids=nullptr;
326     int count=-1;
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;
334
335     int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&count, &conds, 0, nullptr, 0);
336     if (ret<0){
337         BROWSER_LOGD("Error! Could not get ids!");
338     }
339
340     bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED);
341
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);
345
346         int date;
347         bp_history_adaptor_get_date_created(ids[i], &date);
348
349         struct tm *item_time_info;
350         time_t item_time = (time_t)date;
351         item_time_info = localtime(&item_time);
352
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;
357         int m_date = date;
358         int min = item_time_info->tm_min;
359         int hour= item_time_info->tm_hour;
360         int sec = item_time_info->tm_sec;
361
362         m_year = 2000 + m_year % 100;
363
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 : ""));
370
371         //thumbail
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);
380
381         history_list.push_back(history);
382     }
383     free(ids);
384     return history_list;
385 }
386
387 int HistoryService::getHistoryVisitCounter(const std::string & url)
388 {   /**
389      *  No browser- provider implementation till now. Needs to be done
390      */
391 //    return getStorageManager()->getHistoryVisitCounter(url);
392
393 }
394
395 }
396 }