History Implementation using Browser Provider.
[profile/tv/apps/web/browser.git] / services / HistoryService / src / HistoryService.cpp
index 95ee009..cd2864e 100644 (file)
@@ -20,8 +20,8 @@
 
 #include "ServiceManager.h"
 #include "HistoryService.h"
+#include "HistoryItem.h"
 #include "AbstractWebEngine.h"
-
 namespace tizen_browser
 {
 namespace services
@@ -58,19 +58,79 @@ void HistoryService::setStorageServiceTestMode(bool testmode) {
        m_testDbMod = testmode;
 }
 
-/**
- * @throws HistoryException on error
- */
-void HistoryService::addHistoryItem(std::shared_ptr<HistoryItem> hi)
+int HistoryService::getHistoryItemsCount(){
+    return 1;
+    //return getStorageManager()->getHistoryItemsCount();
+}
+
+static int __get_duplicated_ids_p(int **ids, int *count, const int limit, const int offset,
+               const bp_history_offset order_column_offset, const int ordering,
+               const bp_history_offset check_column_offset,
+               const char *keyword, const int is_like)
 {
-    getStorageManager()->addHistoryItem(hi);
+    bp_history_rows_cond_fmt conds;
+    memset(&conds, 0x00, sizeof(bp_history_rows_cond_fmt));
+
+    conds.limit = limit;
+    conds.offset = offset;
+    conds.ordering = ordering;
+    conds.order_offset = order_column_offset;
+    conds.period_offset = BP_HISTORY_O_DATE_CREATED;
+    conds.period_type = BP_HISTORY_DATE_ALL;
+
+    return bp_history_adaptor_get_cond_ids_p
+       (ids, count,
+        &conds,
+        check_column_offset,
+        keyword,
+        is_like);
 }
 
-/**
- * If hi->getUrl() exists on a table HISTORY update visit_counter and visit_date, unless insert hi to database.
- *
- * @throws HistoryException on error
- */
+void HistoryService::addHistoryItem(std::shared_ptr<HistoryItem> his){
+
+    BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
+    his->setFavIcon(his->getFavIcon());
+    std::shared_ptr<tizen_browser::tools::BrowserImage> favicon = his->getFavIcon();
+
+    int id = -1;
+    int ret = bp_history_adaptor_create(&id);
+    if (ret<0){
+        BROWSER_LOGE("Error! Could not create new bookmark!");
+    }
+
+    int *ids=NULL;
+    int count=-1;
+    int **id1=&ids;
+    int *count1=&count;
+
+    bp_history_rows_cond_fmt conds;
+    conds.limit = 20;  //no of rows to get negative means no limitation
+
+    conds.offset = -1;   //the first row's index
+    conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
+
+    conds.ordering = 1; //way of ordering 0 asc 1 desc
+
+    conds.period_offset = BP_HISTORY_O_DATE_CREATED;
+    conds.period_type = BP_HISTORY_DATE_TODAY;
+
+    ret = bp_history_adaptor_get_cond_ids_p(id1 ,count1, &conds, 0, NULL, 0);
+    if (ret<0){
+        BROWSER_LOGE("Error! Could not get ids!");
+    }
+
+    bp_history_adaptor_set_url(id, (his->getUrl()).c_str());
+    bp_history_adaptor_set_title(id, (his->getTitle()).c_str());
+
+
+    std::unique_ptr<tizen_browser::tools::Blob> favicon_blob = tizen_browser::tools::EflTools::getBlobPNG(favicon);
+    unsigned char * fav = std::move((unsigned char*)favicon_blob->getData());
+    bp_history_adaptor_set_icon(id, favicon->width, favicon->height, fav, favicon_blob->getLength());
+
+    BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
+}
+
+
 void HistoryService::insertOrRefresh(std::shared_ptr<HistoryItem> hi) {
        getStorageManager()->insertOrRefresh(hi);
 }
@@ -94,33 +154,48 @@ void HistoryService::clearURLHistory(const std::string & url)
     }
 }
 
-/**
- * @throws HistoryException on error
- */
-std::shared_ptr<HistoryItem> HistoryService::getHistoryItem(const std::string & url)
-{
-    return getStorageManager()->getHistoryItem(url);
-}
 
-/**
- * @throws HistoryException on error
- */
 HistoryItemVector& HistoryService::getHistoryItems(int historyDepthInDays, int maxItems)
 {
-    return getStorageManager()->getHistoryItems(historyDepthInDays, maxItems);
-}
+    history_list.clear();
 
-/**
- * @throws HistoryException on error
- */
-int HistoryService::getHistoryItemsCount()
-{
-    return getStorageManager()->getHistoryItemsCount();
+    int *ids=NULL;
+    int count=-1;
+    int **id1=&ids;
+    int *count1=&count;
+
+    bp_history_rows_cond_fmt conds;
+    conds.limit = 20;  //no of rows to get negative means no limitation
+
+    conds.offset = -1;   //the first row's index
+    conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
+
+    conds.ordering = 1; //way of ordering 0 asc 1 desc
+
+    conds.period_offset = BP_HISTORY_O_DATE_CREATED;
+
+    conds.period_type = BP_HISTORY_DATE_TODAY;
+
+    int ret = bp_history_adaptor_get_cond_ids_p(id1 ,count1, &conds, 0, NULL, 0);
+    if (ret<0){
+        BROWSER_LOGD("Error! Could not get ids!");
+    }
+
+    bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON);
+
+    for(int i = 0; i< (*count1); i++){
+        bp_history_info_fmt history_info;
+        bp_history_adaptor_get_info(ids[i],offset,&history_info);
+
+        std::shared_ptr<HistoryItem> history = std::make_shared<HistoryItem>(std::string(history_info.url));
+        history_list.push_back(history);
+    }
+    ids = NULL;
+    free(ids);
+
+    return history_list;
 }
 
-/**
- * @throws HistoryException on error
- */
 int HistoryService::getHistoryVisitCounter(const std::string & url)
 {
     return getStorageManager()->getHistoryVisitCounter(url);