browsercore/appfw/Api/Managers/BookmarksManager.cpp
changeset 3 0954f5dd2cd0
parent 0 1450b09d0cfd
child 9 b39122337a00
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
     4 *
     9 * Initial Contributors:
     5 * This program is free software: you can redistribute it and/or modify
    10 * Nokia Corporation - initial contribution.
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
    11 *
    13 *
    12 * Contributors:
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
    13 *
    17 *
    14 * Description: 
    18 * Description:
    15 *
    19 *
    16 */
    20 */
    17 
       
    18 
    21 
    19 
    22 
    20 #include <QtCore/QDir>
    23 #include <QtCore/QDir>
    21 #include <QtCore/QFile>
    24 #include <QtCore/QFile>
    22 #include <QtGui/QIcon>
    25 #include <QtGui/QIcon>
    23 #include <QtGui>
    26 #include <QtGui>
    24 #include <QtCore/QDebug>
    27 #include <QtCore/QDebug>
    25 #include <QtCore/QUrl>
    28 #include <QtCore/QUrl>
    26 
       
    27 #include <QString>
    29 #include <QString>
       
    30 #include "actionjsobject.h"
    28 #include "BookmarksManager_p.h"
    31 #include "BookmarksManager_p.h"
    29 #include "BookmarksManager.h"
    32 #include "BookmarksManager.h"
    30 
    33 
    31 //#include "wrtsettings.h"
    34 //#include "wrtsettings.h"
    32 #include "bedrockprovisioning.h"
    35 #include "bedrockprovisioning.h"
    33 
       
    34 #include "bookmarks.h"
    36 #include "bookmarks.h"
       
    37 #include <browsercontentdll.h>
    35 #include "xbel.h"
    38 #include "xbel.h"
    36 #include <browsercontentdll.h>
       
    37 #include "webpagecontroller.h"
    39 #include "webpagecontroller.h"
    38 #include "wrtbrowsercontainer.h"
    40 #include "wrtbrowsercontainer.h"
    39 
    41 
    40 
    42 
    41 namespace WRT {
    43 namespace WRT {
    42 
       
    43 #define SETTINGMAXURLS "MaxRecentUrls"
       
    44 
    44 
    45 BookmarksManagerPrivate::BookmarksManagerPrivate(BookmarksManager * mgr, QWidget *parent) :
    45 BookmarksManagerPrivate::BookmarksManagerPrivate(BookmarksManager * mgr, QWidget *parent) :
    46     q(mgr),
    46     q(mgr),
    47     m_connectedToBookmarks(false),
    47     m_connectedToBookmarks(false),
    48     m_loadedBookmarks(false),
       
    49     m_loadedHistory(false),
       
    50     m_bookmarkRootNode(0),
       
    51     m_historyRootNode(0),
       
    52     m_historyRootNodeProxy(0),
       
    53     m_maxUrls(10) // TODO: read from settings
    48     m_maxUrls(10) // TODO: read from settings
    54 {
    49 {
    55 	  m_import = false;
    50     m_import = false;
    56 	  QFileInfo dbFile("browserContent.db");
    51     QFileInfo dbFile("browserContent.db");
    57     
    52     
    58    	if (dbFile.exists()){
    53     if (dbFile.exists()){
    59 	    m_import = false;
    54       m_import = false;
    60 	  }
    55     }
    61 	  else {
    56     else {
    62 		  m_import = true;
    57       m_import = true;
    63 	  }
    58     }
    64 
    59 
    65    	m_bookmarkSession=new BrowserContent("Bedrock");
    60     m_bookmarkSession=new BrowserContent("Bedrock");
    66     if (m_bookmarkSession) {
    61     if (m_bookmarkSession) {
    67         m_connectedToBookmarks = true;
    62         m_connectedToBookmarks = true;
    68     } else {
    63     } else {
    69         qDebug() << "BookmarksManagerPrivate: Failed to connect to bookmarks";
    64         qDebug() << "BookmarksManagerPrivate: Failed to connect to bookmarks";
    70     }
    65     }
       
    66 
       
    67     m_actionsParent = new QObject(mgr);
       
    68     m_actionsParent->setObjectName("actions");
       
    69     
       
    70     m_actionClearHistory = new QAction("clearHistory", m_actionsParent);
       
    71     
       
    72     m_actionClearJSO  = new ActionJSObject(m_actionsParent, m_actionClearHistory);
       
    73     
       
    74     m_actionClearHistory->setObjectName("clearHistory");
       
    75 
    71 }
    76 }
    72 
    77 
    73 BookmarksManagerPrivate::~BookmarksManagerPrivate()
    78 BookmarksManagerPrivate::~BookmarksManagerPrivate()
    74 {
    79 {
    75     delete m_bookmarkRootNode;
       
    76     delete m_historyRootNode;
       
    77 	delete m_historyRootNodeProxy;
       
    78     delete m_bookmarkSession;
    80     delete m_bookmarkSession;
       
    81     delete m_actionClearHistory;
       
    82     delete m_actionClearJSO;
    79 }
    83 }
    80 
    84 
    81 /*!
    85 /*!
    82  * \class BookmarksManager
    86  * \class BookmarksManager
    83  *
    87  *
    94 BookmarksManager::BookmarksManager(QWidget *parent) :
    98 BookmarksManager::BookmarksManager(QWidget *parent) :
    95     d(new BookmarksManagerPrivate(this, parent))
    99     d(new BookmarksManagerPrivate(this, parent))
    96 {
   100 {
    97     if (d->m_import)
   101     if (d->m_import)
    98        importNativeBookmarks();
   102        importNativeBookmarks();
    99 }
   103     
       
   104     m_isBookmarkDbreadRequired=true;
       
   105     //connect(d->m_actionClearHistory, SIGNAL(triggered()), this, SIGNAL(historyCleared()));
       
   106     connect(d->m_actionClearHistory, SIGNAL(triggered()), this, SIGNAL(confirmHistoryClear()));
       
   107      
       
   108 }
       
   109 /*
       
   110 void BookmarksManager::actionClearHistory()
       
   111 {
       
   112     emit confirmClearHistory();
       
   113 }
       
   114 */
       
   115 
   100 BookmarksManager::~BookmarksManager()
   116 BookmarksManager::~BookmarksManager()
   101 {
   117 {
       
   118     disconnect(d->m_actionClearHistory, SIGNAL(triggered()), this, SIGNAL(historyCleared()));
   102     delete d;
   119     delete d;
   103 }
   120 }
   104 
       
   105 
       
   106 /* overwrite settings. called at construction and when settings change
       
   107  */
       
   108 //void BookmarksManager::setSettings(WrtSettings *settings)
       
   109 void BookmarksManager::setSettings(BEDROCK_PROVISIONING::BedrockProvisioning *settings)	
       
   110 {
       
   111     if(settings)   {
       
   112        d->m_maxUrls = settings->valueAsInt(SETTINGMAXURLS);
       
   113     }
       
   114 }
       
   115 
       
   116 
   121 
   117 BookmarksManager* BookmarksManager::getSingleton()
   122 BookmarksManager* BookmarksManager::getSingleton()
   118  {
   123  {
   119     static BookmarksManager* singleton = 0;
   124     static BookmarksManager* singleton = 0;
   120   
   125   
   155     int bookmarkCount = importRootNode->children().count() - 1;
   160     int bookmarkCount = importRootNode->children().count() - 1;
   156     for (int i = bookmarkCount; i >= 0; --i) {
   161     for (int i = bookmarkCount; i >= 0; --i) {
   157         BookmarkNode *node = importRootNode->children()[i];
   162         BookmarkNode *node = importRootNode->children()[i];
   158 
   163 
   159         if (node->type() == BookmarkNode::Bookmark) 
   164         if (node->type() == BookmarkNode::Bookmark) 
   160             addBookmark(node->url,node->title,0);
   165             addBookmark(node->title,node->url,0);
   161         
   166         
   162     } 
   167     } 
   163 #endif    
   168 #endif
       
   169     // add local bookmarks
       
   170     QString localPagesBaseDir(BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsString("LocalPagesBaseDirectory"));
       
   171     QString indexStr;
       
   172     for (int index = 2; index >= 0; index--) {
       
   173         indexStr.setNum(index);
       
   174         QString bookmarkTitle = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsString(tr("Bookmark")+indexStr+tr("Title"));
       
   175         if (bookmarkTitle == "")
       
   176             continue;
       
   177         QString bookmarkUrl = BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsString(tr("Bookmark")+indexStr+tr("Url"));
       
   178         if (bookmarkUrl == "")
       
   179             continue;
       
   180         if (bookmarkUrl.contains("://") || bookmarkUrl.contains("www."))
       
   181             addBookmark(bookmarkTitle, bookmarkUrl, 0);
       
   182         else {
       
   183             QString localBookmarkUrl = QFileInfo(localPagesBaseDir + bookmarkUrl).absoluteFilePath();
       
   184             addBookmark(bookmarkTitle, tr("file:///") + localBookmarkUrl, 0);
       
   185         }
       
   186     }
   164 }
   187 }
   165 
   188 
   166 QString BookmarksManager::getBookmarksJSON()
   189 QString BookmarksManager::getBookmarksJSON()
   167 {
   190     {
   168     QList<BookmarkLeaf*> nodes;
   191     if(m_isBookmarkDbreadRequired==true)
   169     nodes = d->m_bookmarkSession->FetchAllBookmarks();
   192         {
   170     QString bookmakrData = "[";
   193     m_bookmakrData=d->m_bookmarkSession->fetchSerializedBookmarks();
   171     for(int i=0;i<nodes.count();i++) {
   194     m_isBookmarkDbreadRequired=false;
   172         bookmakrData.append("{");
   195        }
   173         bookmakrData.append("\"title\": \"");
   196     return m_bookmakrData;
   174         bookmakrData.append(nodes[i]->getTitle());
   197 
   175         bookmakrData.append("\", \"urlvalue\": \"");
   198     }
   176         bookmakrData.append(nodes[i]->getUrl());
   199 
   177             if(i != (nodes.count()-1))
   200 QString BookmarksManager::normalizeUrl(const QString& url)
   178             bookmakrData.append("\"},");
   201     {
   179         else
   202     // If the URL is relative, add http in front
   180             bookmakrData.append("\"}");
   203     // so that qt doesn't make it absolute using the
   181     }
   204     // local file path
   182     bookmakrData.append("]");
   205     QString updatedUrl = url;
   183 
   206     
   184     qDeleteAll(nodes);
   207     if (!url.contains("://")) {
   185 	nodes.clear();
   208         if (!url.startsWith("www", Qt::CaseInsensitive)) {
   186  
   209             updatedUrl.prepend("http://www.");
   187 	return bookmakrData;
   210         } else {
   188 
   211            updatedUrl.prepend("http://");
   189 }
   212         }
   190 
   213     }
   191 int BookmarksManager::addBookmark(const QString &url, const QString &title, int index)
   214     return updatedUrl;
       
   215     
       
   216     }
       
   217 
       
   218 int BookmarksManager::addBookmark(const QString &title, const QString &url,int index)
   192 {
   219 {
   193     QString updatedTitle = title;
   220     QString updatedTitle = title;
       
   221 	updatedTitle = updatedTitle.trimmed();
   194     //Setting title as url string if title is not available
   222     //Setting title as url string if title is not available
   195     if(url.isEmpty())
   223     if(url.isEmpty())
   196         return ErrBookmarkUrlEmpty;
   224         return ErrBookmarkUrlEmpty;
   197     
   225     
   198     QList<BookmarkLeaf*> nodes;
   226     QVector<QString> nodes;
   199     nodes = d->m_bookmarkSession->FetchAllBookmarks();
   227     d->m_bookmarkSession->fetchAllBookmarkTitles(nodes);
   200     for (int iter = 0 ; iter < nodes.count() ; ++iter)   {
   228     for (int iter = 0 ; iter < nodes.size() ; ++iter)   {
   201      if(nodes.at(iter)->getTitle() == title)   {
   229 		if(0==updatedTitle.compare(nodes.at(iter),Qt::CaseInsensitive)){
   202         //Node already exists no need to add, just return
   230       //Node already exists delete existing
   203         return ErrBookmarkAllReadyPresent;
   231 		 deleteBookmark(updatedTitle); 
   204       }
   232       }
   205     }
   233     }
   206 	
   234 
   207     qDeleteAll(nodes);
   235    
   208 	nodes.clear();
   236     nodes.clear();
       
   237     
       
   238     QString updatedUrl = normalizeUrl(url);
   209     
   239     
   210     //Setting title as url string if title is not available
   240     //Setting title as url string if title is not available
   211     if(title.isEmpty())
   241     if(title.isEmpty())
   212         updatedTitle = url;
   242         updatedTitle = updatedUrl;
   213 
   243     
   214     BookmarkLeaf* leaf=new BookmarkLeaf();
   244     BookmarkLeaf* leaf=new BookmarkLeaf();
   215     leaf->setTitle(updatedTitle);
   245     leaf->setTitle(updatedTitle);
   216     leaf->setUrl(url);
   246     leaf->setUrl(updatedUrl);
   217     //leaf->setDate(bookmark->date);
   247     //leaf->setDate(bookmark->date);
   218     leaf->setTag("unknown");
   248     leaf->setTag("unknown");
   219     leaf->setIndex(index);
   249     leaf->setIndex(index);
   220     
   250     
   221     if(ErrNone == d->m_bookmarkSession->AddBookmark(leaf)) {
   251     if(ErrNone == d->m_bookmarkSession->addBookmark(leaf)) {
   222 		delete leaf;
   252     emit bookmarkEntryAdded(updatedTitle,updatedUrl);
       
   253     delete leaf;
       
   254     m_isBookmarkDbreadRequired=true;
       
   255     return ErrNone;
       
   256     }
       
   257     delete leaf;
       
   258     return ErrGeneral;
       
   259 }
       
   260 
       
   261 void BookmarksManager::deleteBookmark(QString title)
       
   262 {
       
   263     d->m_bookmarkSession->deleteBookmark(title);
       
   264     m_isBookmarkDbreadRequired=true;
       
   265 }
       
   266 
       
   267 void BookmarksManager::clearBookmarks()
       
   268 {
       
   269     d->m_bookmarkSession->clearBookmarks();
       
   270     m_isBookmarkDbreadRequired=true;
       
   271     emit bookmarksCleared();
       
   272 }
       
   273 
       
   274 int BookmarksManager::reorderBokmarks(QString title,int new_index)
       
   275 {
       
   276     d->m_bookmarkSession->reorderBokmarks(title,new_index);
       
   277     m_isBookmarkDbreadRequired=true;
       
   278     return ErrNone;    
       
   279 }
       
   280 
       
   281 int BookmarksManager::modifyBookmark(QString orgTitle, QString newTitle, QString newUrl)
       
   282 {
       
   283     QString updatedTitle = newTitle;
       
   284 	updatedTitle = updatedTitle.trimmed();
       
   285     //Setting title as url string if title is not available
       
   286     if(newUrl.isEmpty())
       
   287         return ErrBookmarkUrlEmpty;
       
   288     
       
   289     QList<BookmarkLeaf*> nodes;
       
   290     nodes = d->m_bookmarkSession->fetchAllBookmarks();
       
   291     for (int iter = 0 ; iter < nodes.count() ; ++iter)   {
       
   292      if((0==updatedTitle.compare(nodes.at(iter)->getTitle(),Qt::CaseInsensitive))
       
   293          && (0 != orgTitle.compare(nodes.at(iter)->getTitle(),Qt::CaseInsensitive))){
       
   294          //Node already exists delete existing
       
   295          deleteBookmark(updatedTitle);
       
   296       }
       
   297     }
       
   298     
       
   299     qDeleteAll(nodes);
       
   300     nodes.clear();
       
   301 
       
   302    QString updatedUrl = normalizeUrl(newUrl);
       
   303    
       
   304   //Setting title as url string if title is not available
       
   305     if(newTitle.isEmpty())
       
   306         updatedTitle = updatedUrl;
       
   307 
       
   308 
       
   309     if(ErrNone == d->m_bookmarkSession->modifyBookmark(orgTitle, updatedTitle, updatedUrl)) {
       
   310         emit bookmarkEntryModified(updatedTitle, updatedUrl);
       
   311         m_isBookmarkDbreadRequired=true;
   223         return ErrNone;
   312         return ErrNone;
   224     }
   313     }
   225 	delete leaf;
       
   226     return ErrGeneral;
   314     return ErrGeneral;
   227 }
   315 }
   228 
   316 
   229 void BookmarksManager::deleteBookmark(QString title)
   317 void BookmarksManager::launchEditBookmark(QString title,QString url)
   230 {
   318 {
   231     d->m_bookmarkSession->DeleteBookmark(title);
   319     emit launchBookmarkEditDailog(title,url);
   232 }
   320 }
   233 
   321 
   234 int BookmarksManager::reorderBokmarks(QString title,int new_index)
   322 QString BookmarksManager::getHistoryFoldersJSON(QString folderName)
   235 {
   323     {
   236     d->m_bookmarkSession->reorderBokmarks(title,new_index);
   324 
   237     return ErrNone;    
   325     bool flag = true;
   238 }
   326   
   239 
       
   240 /*!
       
   241  * Load history from database
       
   242  */
       
   243 void BookmarksManager::loadHistory()
       
   244 {
       
   245     if (d->m_loadedHistory)
       
   246         return;
       
   247 
       
   248     d->m_loadedHistory = true;
       
   249 
       
   250     if(d->m_historyRootNode) {
       
   251         delete d->m_historyRootNode;
       
   252         d->m_historyRootNode = NULL;
       
   253     }
       
   254 
       
   255     d->m_historyRootNode = new BookmarkNode(BookmarkNode::Root, NULL);
       
   256 
       
   257     if (d->m_connectedToBookmarks) {
       
   258         QList<HistoryLeaf*> nodes;
       
   259         nodes = d->m_bookmarkSession->FetchHistory();
       
   260 
       
   261         for(int i=nodes.count()-1;i>=0;i--) {
       
   262             BookmarkNode* node = new BookmarkNode(BookmarkNode::Bookmark, NULL);
       
   263             node->title=nodes[i]->getTitle();
       
   264             node->url=nodes[i]->getUrl();
       
   265             node->date=nodes[i]->getDate();
       
   266             node->lastVisited =nodes[i]->getLastVisited();
       
   267             d->m_historyRootNode->add(node, 0);
       
   268         }
       
   269 
       
   270 		qDeleteAll(nodes);
       
   271 		nodes.clear();
       
   272     }
       
   273 
       
   274        loadHistoryProxy();
       
   275 }
       
   276 
       
   277 /*!
       
   278  * Load history proxy from existing database
       
   279  * This creates new Model class based on existing Model and arranges the items in to groups
       
   280  * "Today", "YesterDay", "This Week",  "This Month" ......... 
       
   281  */
       
   282 void BookmarksManager::loadHistoryProxy()
       
   283 {
       
   284     if(d->m_historyRootNodeProxy) {
       
   285         delete d->m_historyRootNodeProxy;
       
   286         d->m_historyRootNodeProxy = NULL;
       
   287     }
       
   288     
       
   289     d->m_historyRootNodeProxy  = new BookmarkNode(BookmarkNode::Root, NULL);
       
   290 
       
   291     //Loop through the root elements and find the folder in proxy to which the elements belong     
       
   292     for (int i = d->m_historyRootNode->children().count() - 1; i >= 0; --i) {
       
   293         
       
   294         BookmarkNode *node = d->m_historyRootNode->children()[i];
       
   295         int daysToCurrentDate = node->date.daysTo(QDate::currentDate());
       
   296 
       
   297         if(daysToCurrentDate < 0) {
       
   298             continue;
       
   299         }
       
   300         //Find the Folder name to which this node belongs
       
   301         QString strFoldername =  findFolderForDate(node->date);
       
   302         //Add the  node to proxy model
       
   303         addToHistoryProxy(strFoldername,node);
       
   304     }                
       
   305 }
       
   306 
       
   307 QString BookmarksManager::getHistoryFoldersJSON()
       
   308 {
       
   309     loadHistory();
       
   310     loadHistoryProxy();
       
   311 
       
   312     QString historyFolders = "[";
   327     QString historyFolders = "[";
   313 
   328     if (folderName == "")
   314     QList<BookmarkNode *> rootChildren (d->m_historyRootNodeProxy->children());
   329         {
   315 
   330     m_historyMap.clear();
   316     for (int i=0; i < rootChildren.count(); i++) {
   331     m_folderVector.clear();
       
   332     d->m_bookmarkSession->fetchSerializedHistory(m_folderVector, m_historyMap);
       
   333 
       
   334     for (int i = m_folderVector.size() - 1; i >= 0; i--)
       
   335         {
   317         //check for folder nodes
   336         //check for folder nodes
   318         if (rootChildren.at(i)->type() == BookmarkNode::Folder) {
   337         historyFolders.append("\"");
   319             historyFolders.append("\"");
   338         historyFolders.append(m_folderVector[i]);
   320             historyFolders.append (rootChildren.at(i)->title );
   339         historyFolders.append("\"");
   321             historyFolders.append("\"");
   340 
   322             
   341 
   323             if(i != rootChildren.count()-1)
   342         if (i != 0)
   324                historyFolders.append ( ",");
   343             historyFolders.append(",");
   325         }
   344         if (flag)
   326         
   345             {
   327     }
   346             if (m_folderVector[i].count() > 0)
   328     historyFolders.append ("]");
   347                 {
   329 
   348                 d->m_actionClearHistory->setEnabled(true);
   330     return historyFolders;
   349                 flag=false;
   331 }
   350                 }
   332 
   351 
   333 QString BookmarksManager::getHistoryFolderJSON(QString folderName)
   352             }
   334 {
   353         }
   335     QString history = "";
   354      if (flag)
   336 
   355         {
   337     QList<BookmarkNode *> rootChildren (d->m_historyRootNodeProxy->children());
   356         d->m_actionClearHistory->setEnabled(false);
   338 
   357         }
   339     for (int i=0; i < rootChildren.count(); i++) {
   358     historyFolders.append("]");
   340         //check for folder nodes
   359     m_folderVector.clear();
   341         if ((rootChildren.at(i)->type() == BookmarkNode::Folder) && (rootChildren.at(i)->getTitle() == folderName)) {
   360         }
   342 
   361     if (folderName == "")
   343             QList<BookmarkNode *> nodeChildren (rootChildren.at(i)->children());
   362         {
   344             history.append("[");
   363         return historyFolders;
   345     
   364 
   346             for (int j=0; j < nodeChildren.count(); j++) {
   365         }
   347                 //If you encounter a folder node, jump to next sibling since 
   366     else
   348                 //there cann't by any URL at this level
   367         {
   349                 if(nodeChildren.at(j)->type() == BookmarkNode::Folder) {
   368         return m_historyMap[folderName];
   350                     QList<BookmarkNode *> leafChildren (nodeChildren.at(j)->children());
   369         }
   351                     for (int k=0; k < leafChildren.count(); k++) {
   370     }
   352                         QString title,date,time;
       
   353                         history.append("{");
       
   354                          history.append("\"titleVal\": \"");
       
   355                          title = leafChildren.at(k)->getTitle();
       
   356                          history.append(title);
       
   357                          history.append("\", \"dateVal\": \"");
       
   358                          date = nodeChildren.at(j)->getDate().toString("dd.MM.yyyy");
       
   359                          history.append(date);
       
   360                          history.append("\", \"urlVal\": \"");
       
   361                          history.append(leafChildren.at(k)->getUrl());
       
   362 
       
   363                          history.append("\", \"timeVal\": \"");
       
   364                          time =leafChildren.at(k)->getLastVisited().toString("h:mm ap");
       
   365                          history.append(time);
       
   366 
       
   367 //                       qDebug() <<" Title : "<<title << " Date : "<< date <<" Time : "<<time<<'\n';
       
   368                     
       
   369                         if(j != (nodeChildren.count()-1) || k != (leafChildren.count()-1)  )
       
   370                            history.append("\"},");
       
   371                         else
       
   372                            history.append("\"}");
       
   373                         } 
       
   374                     }
       
   375                 else{
       
   376                     history.append("{");
       
   377                     history.append("\"titleVal\": \"");
       
   378                     history.append(nodeChildren.at(j)->getTitle());
       
   379                     history.append("\", \"dateVal\": \"");
       
   380                     history.append(rootChildren.at(i)->getDate().toString("dd.MM.yyyy"));
       
   381                     history.append("\", \"urlVal\": \"");
       
   382                     history.append(nodeChildren.at(j)->getUrl());
       
   383                     history.append("\", \"timeVal\": \"");
       
   384                     QString time =nodeChildren.at(j)->getLastVisited().toString("h:mm ap");
       
   385                     history.append(time);
       
   386                 
       
   387                     if(j != (nodeChildren.count()-1))
       
   388                        history.append("\"},");
       
   389                     else
       
   390                        history.append("\"}");
       
   391                     } 
       
   392             }//EOF for (int j=0; j < nodeChildren.count(); j++) 
       
   393 
       
   394         }//EOF IF if (rootChildren.at(i)->type() == BookmarkNode::Folder && rootChildren.at(i)->title() == folder) {
       
   395         else {
       
   396             //at this level there cann't be URL items.
       
   397         }
       
   398 
       
   399         
       
   400     }
       
   401     history.append ("]");
       
   402 
       
   403     return history;
       
   404 }
       
   405 
   371 
   406 /*!
   372 /*!
   407  * Add the  node to the folder in proxy model. If the folder doesnt exist in proxy, create
   373  * Add the  node to the folder in proxy model. If the folder doesnt exist in proxy, create
   408  * the folder  and add the node to it
   374  * the folder  and add the node to it
   409  */
   375  */
   410 void BookmarksManager::addToHistoryProxy(QString &strFoldername, BookmarkNode*node)
       
   411 {
       
   412     bool bFound = false;
       
   413     BookmarkNode *folderNode= NULL;
       
   414     
       
   415     //Check if the folder exist with name $strFoldername
       
   416     for (int i = d->m_historyRootNodeProxy->children().count() - 1; i >= 0; --i) {
       
   417                folderNode = d->m_historyRootNodeProxy->children()[i];
       
   418                QString title = folderNode->title ;
       
   419                if(folderNode->title == strFoldername){
       
   420                //folder exist
       
   421                    bFound = true;
       
   422                    break;
       
   423                }         
       
   424     }
       
   425 
       
   426     if(!bFound){
       
   427         //Folder doesnt exist. create new folder with name $strFoldername
       
   428         folderNode = new BookmarkNode(BookmarkNode::Folder, d->m_historyRootNodeProxy);
       
   429         folderNode->title = strFoldername;
       
   430         folderNode->date = node->date;
       
   431         
       
   432         int index=0;
       
   433         //Find the index to where the folder needs to be inserted
       
   434         for (; index < d->m_historyRootNodeProxy->children().count(); index++) {
       
   435            BookmarkNode* tmpNode = d->m_historyRootNodeProxy->children()[index];
       
   436            if(folderNode->date  > tmpNode->date){
       
   437              break;
       
   438            }  
       
   439         }
       
   440         d->m_historyRootNodeProxy->add(folderNode,index);
       
   441     }
       
   442     
       
   443     
       
   444 
       
   445     BookmarkNode *newNode = new BookmarkNode(BookmarkNode::Bookmark,folderNode);
       
   446     newNode->date = node->date;
       
   447     newNode->title = node->title;
       
   448     newNode->lastVisited = node->lastVisited;
       
   449     newNode->favicon = node->favicon;
       
   450     newNode->url = node->url;
       
   451     
       
   452     //Find the index to where the node needs to be inserted in to the foder
       
   453     int index=0;
       
   454     for (; index < folderNode->children().count() ;  index++) {
       
   455        BookmarkNode* tmpNode = folderNode->children()[index];
       
   456             if(newNode->lastVisited  > tmpNode->lastVisited){
       
   457                  break;
       
   458                }
       
   459     }
       
   460     
       
   461     folderNode->add(newNode,index);
       
   462 }
       
   463 
       
   464 
       
   465 /*!
       
   466  * Finds the folder to which the date belongs
       
   467  * 
       
   468  */
       
   469 //QString BookmarksManager::findFolderForDate( BookmarkNode *aNode)
       
   470 QString BookmarksManager::findFolderForDate( QDate& nodeDate)const
       
   471 {
       
   472     QDateTime currentDateTime = QDateTime::currentDateTime();
       
   473     int currentDayOfWeek = currentDateTime.date().dayOfWeek(); 
       
   474 
       
   475     int nodeDayOfWeek = nodeDate.dayOfWeek();
       
   476 
       
   477     int daysToCurrentDate = nodeDate.daysTo(currentDateTime.date());
       
   478 
       
   479     //Check if date to belongs to "ToDay" Folder
       
   480     if(nodeDate == currentDateTime.date()){
       
   481         QString folder = qtTrId("txt_browser_history_today");
       
   482         return folder;
       
   483     }
       
   484     //Check if date to belongs to "YesterDay" Folder
       
   485     if(nodeDate.addDays(1) == currentDateTime.date() ){
       
   486         QString folder = qtTrId("txt_browser_history_yesterday");
       
   487         return folder;
       
   488     }
       
   489 
       
   490     //Check if date to belongs to current week folder
       
   491     //Should disply the day for the current week
       
   492     if(daysToCurrentDate < 7  &&  currentDayOfWeek > nodeDayOfWeek ){
       
   493         
       
   494         QString folder = qtTrId("txt_browser_history_this_week");
       
   495         return folder;
       
   496     }
       
   497 
       
   498     if(dateInThisMonth(nodeDate)){
       
   499         QString folder = qtTrId("txt_browser_history_this_month");
       
   500         return folder;
       
   501     }
       
   502 
       
   503     QString folder = nodeDate.toString("dd.MM.yyyy");
       
   504     return folder; 
       
   505 
       
   506 }
       
   507 
       
   508 
       
   509 
       
   510 bool BookmarksManager::dateInThisMonth(QDate &date)const
       
   511 {
       
   512     QDate currentDate = QDateTime::currentDateTime().date();
       
   513     int daysToCurrentDate = currentDate.daysTo(date);
       
   514 
       
   515     int currentMonth = currentDate.month();
       
   516     int nodeMonth = date.month();
       
   517 
       
   518     if(daysToCurrentDate <= 31 && currentMonth == nodeMonth) {
       
   519         return true;
       
   520     }
       
   521     return false;  
       
   522    
       
   523 }
       
   524 
       
   525 
   376 
   526 /*!
   377 /*!
   527  * Add to recent urls
   378  * Add to recent urls
   528  * @param url: url to be added to recent url list
   379  * @param url: url to be added to recent url list
   529  * @param title: title for this item in recent url list
   380  * @param title: title for this item in recent url list
   530  * @param icon: icon to be added for this item in recent url list
   381  * @param icon: icon to be added for this item in recent url list
   531  */
   382  */
   532 Q_DECL_EXPORT void BookmarksManager::addHistory(const QString &url, const QString &title)
   383 Q_DECL_EXPORT void BookmarksManager::addHistory(const QString &url, const QString &title)
   533 {
   384 {
   534     if (!d->m_loadedHistory)
       
   535         loadHistory();
       
   536 
   385 
   537     //Check for a valid history entry
   386     //Check for a valid history entry
   538     if (url.isEmpty() || title.isEmpty())
   387     if (url.isEmpty() || title.isEmpty())
   539         return;
   388         return;
       
   389     
       
   390     bool enabled = (bool) BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsInt("SaveHistory");
       
   391     if(!enabled)
       
   392       return;
   540     
   393     
   541     QDateTime currentDateTime = QDateTime::currentDateTime();
   394     QDateTime currentDateTime = QDateTime::currentDateTime();
   542     
   395     
   543     HistoryLeaf* leaf=new HistoryLeaf();
   396     HistoryLeaf* leaf=new HistoryLeaf();
   544     leaf->setTitle(title);
   397     leaf->setTitle(title);
   545     leaf->setUrl(url);
   398     leaf->setUrl(url);
   546     leaf->setDate(currentDateTime.date());
   399     leaf->setDate(currentDateTime.date());
   547     leaf->setLastVisited(currentDateTime.time());
   400     leaf->setLastVisited(currentDateTime.time());
   548     
   401     
   549     if (d->m_connectedToBookmarks){
   402     if (d->m_connectedToBookmarks){
   550         if(ErrNone == d->m_bookmarkSession->AddHistory(leaf)){
   403         if(ErrNone == d->m_bookmarkSession->addHistory(leaf)){
   551           d->m_loadedHistory = false;
   404           d->m_actionClearHistory->setEnabled(true);
   552         }
   405         }
   553     }
   406     }
   554 	delete leaf;
   407     delete leaf;
   555 }
   408 }
   556 
   409 
   557 /*!
   410 /*!
   558  * Add to recent urls
   411  * Add to recent urls
   559  * @param url: url to be added to recent url list
   412  * @param url: url to be added to recent url list
   564 void BookmarksManager::addHistory(const QUrl &url, const QString &title)
   417 void BookmarksManager::addHistory(const QUrl &url, const QString &title)
   565 {
   418 {
   566     addHistory(url.toString(), title);
   419     addHistory(url.toString(), title);
   567 }
   420 }
   568 
   421 
       
   422 
   569 /*!
   423 /*!
   570  * delete recent urls
   424  * delete recent urls
   571  * clears all the recent url list.
   425  * clears all the recent url list.
   572  */
   426  */
   573 void BookmarksManager::clearHistory()
   427 void BookmarksManager::clearHistory()
   574 {
   428 {
   575     if (!d->m_loadedHistory)
   429 
   576         loadHistory();
       
   577 
       
   578     //Undo Redo is not required for history(recent urls)
       
   579     for (int i = d->m_historyRootNode->children().count() - 1; i >= 0; --i) {
       
   580         BookmarkNode *node = d->m_historyRootNode->children()[i];     
       
   581         d->m_historyRootNode->remove(node);
       
   582     }
       
   583     
       
   584     if (d->m_connectedToBookmarks) {
   430     if (d->m_connectedToBookmarks) {
   585         d->m_bookmarkSession->clearHistory();
   431         d->m_bookmarkSession->clearHistory();
   586     }
   432     }
   587     d->m_loadedHistory = false;
   433     
       
   434     d->m_actionClearHistory->setEnabled(false);
       
   435     
       
   436     emit historyCleared();
       
   437 }
       
   438 
       
   439 QAction * BookmarksManager::getActionClearHistory()
       
   440 {
       
   441     return d->m_actionClearHistory;
   588 }
   442 }
   589 
   443 
   590 int BookmarksManager::getPageRank(const QString &url)
   444 int BookmarksManager::getPageRank(const QString &url)
   591 {
   445 {
   592     if (!d->m_loadedHistory)
   446  //Check for a valid entry
   593         loadHistory();
       
   594 
       
   595     //Check for a valid entry
       
   596     if (url.isNull())
   447     if (url.isNull())
   597         return 0;
   448         return 0;
   598 
   449 
   599     int rank = 0;
   450     int rank = 0;
   600     QList<BookmarkNode *> rootChildren (d->m_historyRootNode->children());
   451 	QList<HistoryLeaf*> historyNodes = d->m_bookmarkSession->fetchHistory();
   601 
   452 
   602 	for (int i=0; i < rootChildren.count(); i++) {
   453     for (int i=0; i < historyNodes.count(); i++) {
   603 	    //Update rank if there is a history for this URL.
   454       //Update rank if there is a history for this URL.
   604 	    if (!rootChildren[i]->getUrl().compare(url))
   455         if (!historyNodes[i]->getUrl().compare(url))
   605 	       rank++;
   456            rank++;
   606 	}
   457     }
       
   458 
       
   459 	while (!historyNodes.isEmpty())
       
   460      delete historyNodes.takeFirst();
       
   461 
   607     return rank;
   462     return rank;
   608 }
   463 }