browsercore/appfw/Api/Managers/HistoryManager.cpp
changeset 15 73c48011b8c7
equal deleted inserted replaced
13:491a1d15372f 15:73c48011b8c7
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     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.
       
    13 *
       
    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/".
       
    17 *
       
    18 * Description:
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 #include <QtCore/QDir>
       
    24 #include <QtCore/QFile>
       
    25 #include <QtGui/QIcon>
       
    26 #include <QtGui>
       
    27 #include <QtCore/QDebug>
       
    28 #include <QtCore/QUrl>
       
    29 #include <QString>
       
    30 #include "actionjsobject.h"
       
    31 #include "HistoryManager_p.h"
       
    32 #include "HistoryManager.h"
       
    33 
       
    34 //#include "wrtsettings.h"
       
    35 #include "bedrockprovisioning.h"
       
    36 #include <browsercontentdll.h>
       
    37 #include "webpagecontroller.h"
       
    38 #include "wrtbrowsercontainer.h"
       
    39 
       
    40 
       
    41 namespace WRT {
       
    42 
       
    43 HistoryManagerPrivate::HistoryManagerPrivate(HistoryManager * mgr) :
       
    44     q(mgr),
       
    45     m_connectedToHistory(false),
       
    46     m_maxUrls(10) // TODO: read from settings
       
    47 {
       
    48     QFileInfo dbFile("browserContent.db");
       
    49 
       
    50 #ifdef Q_WS_MAEMO_5
       
    51     m_import = false;    
       
    52     if (dbFile.exists()){
       
    53       m_import = false;
       
    54     }
       
    55     else {
       
    56       m_import = true;
       
    57     }
       
    58 #endif
       
    59 
       
    60     m_historySession=new BrowserContent("Bedrock");
       
    61     if (m_historySession) {
       
    62         m_connectedToHistory = true;
       
    63     } else {
       
    64         qDebug() << "HistoryManagerPrivate: Failed to connect to history database";
       
    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 
       
    76 }
       
    77 
       
    78 HistoryManagerPrivate::~HistoryManagerPrivate()
       
    79 {
       
    80     delete m_historySession;
       
    81     delete m_actionClearHistory;
       
    82     delete m_actionClearJSO;
       
    83 }
       
    84 
       
    85 /*!
       
    86  * \class HistoryManager
       
    87  *
       
    88  * This class is responsible for managing history, This class could be used
       
    89  * mainly for Creating, adding history, adding recent history items, deleting
       
    90  * and editing history.
       
    91  *
       
    92  */
       
    93  
       
    94 /*!
       
    95  * Basic constructor
       
    96  * @param parent : parent widget (Defaulted to NULL ) if not specified
       
    97  */
       
    98 HistoryManager::HistoryManager(QWidget *parent) :
       
    99     QObject(parent),
       
   100     d(new HistoryManagerPrivate(this))
       
   101 {
       
   102 
       
   103     m_isHistoryDbreadRequired=true;
       
   104     connect(d->m_actionClearHistory, SIGNAL(triggered()), this, SIGNAL(confirmHistoryClear()));
       
   105      
       
   106 }
       
   107 /*
       
   108 void HistoryManager::actionClearHistory()
       
   109 {
       
   110     emit confirmClearHistory();
       
   111 }
       
   112 */
       
   113 
       
   114 HistoryManager::~HistoryManager()
       
   115 {
       
   116     disconnect(d->m_actionClearHistory, SIGNAL(triggered()), this, SIGNAL(historyCleared()));
       
   117     delete d;
       
   118 }
       
   119 
       
   120 HistoryManager* HistoryManager::getSingleton()
       
   121  {
       
   122     static HistoryManager* singleton = 0;
       
   123   
       
   124     if(!singleton){
       
   125            singleton = new HistoryManager;
       
   126            singleton->setObjectName("historyManager");
       
   127     }
       
   128 
       
   129     //assert(singleton);
       
   130     return singleton;    
       
   131  }
       
   132 }
       
   133 
       
   134 QString HistoryManager::getHistoryFoldersJSON(QString folderName)
       
   135     {
       
   136 
       
   137     bool flag = true;
       
   138   
       
   139     QString historyFolders = "[";
       
   140     if (folderName == "")
       
   141         {
       
   142     m_historyMap.clear();
       
   143     m_folderVector.clear();
       
   144     d->m_historySession->fetchSerializedHistory(m_folderVector, m_historyMap);
       
   145 
       
   146     for (int i = m_folderVector.size() - 1; i >= 0; i--)
       
   147         {
       
   148         //check for folder nodes
       
   149         historyFolders.append("\"");
       
   150         historyFolders.append(m_folderVector[i]);
       
   151         historyFolders.append("\"");
       
   152 
       
   153 
       
   154         if (i != 0)
       
   155             historyFolders.append(",");
       
   156         if (flag)
       
   157             {
       
   158             if (m_folderVector[i].count() > 0)
       
   159                 {
       
   160                 d->m_actionClearHistory->setEnabled(true);
       
   161                 flag=false;
       
   162                 }
       
   163 
       
   164             }
       
   165         }
       
   166      if (flag)
       
   167         {
       
   168         d->m_actionClearHistory->setEnabled(false);
       
   169         }
       
   170     historyFolders.append("]");
       
   171     m_folderVector.clear();
       
   172         }
       
   173     if (folderName == "")
       
   174         {
       
   175         return historyFolders;
       
   176 
       
   177         }
       
   178     else
       
   179         {
       
   180         return m_historyMap[folderName];
       
   181         }
       
   182     }
       
   183 
       
   184 /*!
       
   185  * Add the  node to the folder in proxy model. If the folder doesnt exist in proxy, create
       
   186  * the folder  and add the node to it
       
   187  */
       
   188 
       
   189 /*!
       
   190  * Add to recent urls
       
   191  * @param url: url to be added to recent url list
       
   192  * @param title: title for this item in recent url list
       
   193  * @param icon: icon to be added for this item in recent url list
       
   194  */
       
   195 Q_DECL_EXPORT void HistoryManager::addHistory(const QString &url, const QString &title)
       
   196 {
       
   197 
       
   198     //Check for a valid history entry
       
   199     if (url.isEmpty() || title.isEmpty())
       
   200         return;
       
   201     
       
   202     bool enabled = (bool) BEDROCK_PROVISIONING::BedrockProvisioning::createBedrockProvisioning()->valueAsInt("SaveHistory");
       
   203     if(!enabled)
       
   204       return;
       
   205     
       
   206     QDateTime currentDateTime = QDateTime::currentDateTime();
       
   207     
       
   208     HistoryLeaf* leaf=new HistoryLeaf();
       
   209     leaf->setTitle(title);
       
   210     leaf->setUrl(url);
       
   211     leaf->setDate(currentDateTime.date());
       
   212     leaf->setLastVisited(currentDateTime.time());
       
   213     
       
   214     if (d->m_connectedToHistory){
       
   215         if(ErrNone == d->m_historySession->addHistory(leaf)){
       
   216           d->m_actionClearHistory->setEnabled(true);
       
   217         }
       
   218     }
       
   219     delete leaf;
       
   220 }
       
   221 
       
   222 /*!
       
   223  * Add to recent urls
       
   224  * @param url: url to be added to recent url list
       
   225  * @param title: title for this item in recent url list
       
   226  * @param icon: icon to be added for this item in recent url list
       
   227  */
       
   228 
       
   229 void HistoryManager::addHistory(const QUrl &url, const QString &title)
       
   230 {
       
   231     addHistory(url.toString(), title);
       
   232 }
       
   233 
       
   234 
       
   235 /*!
       
   236  * delete recent urls
       
   237  * clears all the recent url list.
       
   238  */
       
   239 void HistoryManager::clearHistory()
       
   240 {
       
   241 
       
   242     if (d->m_connectedToHistory) {
       
   243         d->m_historySession->clearHistory();
       
   244     }
       
   245     
       
   246     d->m_actionClearHistory->setEnabled(false);
       
   247     
       
   248     emit historyCleared();
       
   249 }
       
   250 
       
   251 QAction * HistoryManager::getActionClearHistory()
       
   252 {
       
   253     return d->m_actionClearHistory;
       
   254 }
       
   255 
       
   256 int HistoryManager::getPageRank(const QString &url)
       
   257 {
       
   258  //Check for a valid entry
       
   259     if (url.isNull())
       
   260         return 0;
       
   261 
       
   262     int rank = 0;
       
   263 	QList<HistoryLeaf*> historyNodes = d->m_historySession->fetchHistory();
       
   264 
       
   265     for (int i=0; i < historyNodes.count(); i++) {
       
   266       //Update rank if there is a history for this URL.
       
   267         if (!historyNodes[i]->getUrl().compare(url))
       
   268            rank++;
       
   269     }
       
   270 
       
   271 	while (!historyNodes.isEmpty())
       
   272      delete historyNodes.takeFirst();
       
   273 
       
   274     return rank;
       
   275 }
       
   276 
       
   277 QMap<QString, QString> HistoryManager::findHistory(QString title)
       
   278 {
       
   279     return d->m_historySession->findSimilarHistoryItems(title);
       
   280 }
       
   281