browsercore/appfw/Api/Managers/geolocationManager.cpp
changeset 16 3c88a81ff781
equal deleted inserted replaced
14:6aeb7a756187 16:3c88a81ff781
       
     1 /*
       
     2  * Copyright (c) 2010 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: This implements the geolocation database API's.
       
    19  *
       
    20  */
       
    21 
       
    22 #include<QString>
       
    23 #include<QFile>
       
    24 #include<QFileInfo>
       
    25 #include<QDebug>
       
    26 #include<QSqlQuery>
       
    27 #include<QSqlError>
       
    28 #include<QWidget>
       
    29 #include<QDateTime>
       
    30 #include<QtGui>
       
    31 
       
    32 #include "geolocationManager.h"
       
    33 
       
    34 /* Declare the user defined meta type for use with QVariant in geolocation. */
       
    35 Q_DECLARE_METATYPE(QWebPage::PermissionPolicy);
       
    36 
       
    37 GeolocationManager* GeolocationManager::getSingleton() 
       
    38 {
       
    39     static GeolocationManager* s_instance;
       
    40     if(!s_instance) {
       
    41         s_instance = new GeolocationManager();
       
    42     }
       
    43     Q_ASSERT(s_instance);
       
    44     return s_instance;    
       
    45 }
       
    46 
       
    47 GeolocationManager::GeolocationManager(QWidget *parent) :
       
    48     QObject(parent)
       
    49 {
       
    50     setObjectName("geolocationManager");
       
    51     
       
    52     /* Register user defined Meta Types used in geolocation API. */
       
    53     qRegisterMetaType<QWebPage::PermissionPolicy>("QWebPage::PermissionPolicy");
       
    54     
       
    55     m_geo = QSqlDatabase::database(GEOLOCATION_DB_NAME);
       
    56     if (!m_geo.isValid()) {
       
    57         m_geo = QSqlDatabase::addDatabase("QSQLITE", GEOLOCATION_DB_NAME);
       
    58         m_geo.setDatabaseName(GEOLOCATION_DB_FILE);
       
    59     }
       
    60     if (m_geo.open()) {
       
    61         if(!doesTableExist(GEOLOCATION_TABLE_NAME)) {
       
    62             createGeolocationSchema();
       
    63         }
       
    64     }
       
    65 }
       
    66 
       
    67 GeolocationManager::~GeolocationManager()
       
    68 {
       
    69     m_geo.close();
       
    70     QSqlDatabase::removeDatabase(GEOLOCATION_DB_NAME);
       
    71 }
       
    72 
       
    73 bool GeolocationManager::doesTableExist(QString tableName)
       
    74 {
       
    75     bool retVal = false;
       
    76        
       
    77     if (!m_geo.isValid() || !m_geo.isOpen())
       
    78          return false;
       
    79        
       
    80     QSqlQuery query(m_geo);
       
    81     query.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name=:tableName");
       
    82     query.bindValue(":tableName", QVariant(tableName));
       
    83     if (query.exec()) {
       
    84         if (query.next()) 
       
    85             retVal = true;
       
    86         query.finish();
       
    87     }
       
    88     return retVal;
       
    89 }
       
    90    
       
    91 void GeolocationManager::createGeolocationSchema()
       
    92 {
       
    93     if (!doQuery("CREATE TABLE IF NOT EXISTS geolocationdata("
       
    94         "domain text PRIMARY KEY,"
       
    95         "creationtime int, "
       
    96         "permission text)")) {
       
    97         // TODO: do some error handling here!
       
    98         return;
       
    99     }
       
   100     // We do lookups by domain
       
   101     if (!doQuery("CREATE INDEX IF NOT EXISTS geo_domain_idx ON geolocationdata(domain ASC)")) {
       
   102         // TODO: do some error handling here!
       
   103         return;
       
   104     }
       
   105 }
       
   106 
       
   107 // TODO refactor this - nothing except the schema creation can use it as is
       
   108 bool GeolocationManager::doQuery(QString query)
       
   109 {
       
   110     QSqlQuery db_query(m_geo);
       
   111     bool ok = db_query.exec(query);
       
   112     if (!ok) {
       
   113         qDebug() << "GeolocationManager::doQuery" << QString("ERR: %1 %2").arg(
       
   114                 db_query.lastError().type()).arg(db_query.lastError().text())
       
   115                 << " Query: " << db_query.lastQuery();
       
   116     }
       
   117     return ok;
       
   118 }
       
   119 
       
   120 /**===================================================================================
       
   121  * Description: Converts database permission string into enum
       
   122  * Returns: permission enum
       
   123  ======================================================================================*/
       
   124 QWebPage::PermissionPolicy GeolocationManager::convertStringIntoPermission(QString permission)
       
   125 {
       
   126     QWebPage::PermissionPolicy permissionPolicy = QWebPage::PermissionUnknown;
       
   127     	
       
   128     if (permission == "G") 
       
   129         permissionPolicy = QWebPage::PermissionGranted;
       
   130     else if (permission == "D") 
       
   131         permissionPolicy = QWebPage::PermissionDenied;
       
   132     else if (permission == "U") 
       
   133         permissionPolicy = QWebPage::PermissionUnknown;
       
   134     
       
   135     return permissionPolicy;
       
   136 }
       
   137 
       
   138 /**===================================================================================
       
   139  * Description: Converts permission enum into database string
       
   140  * Returns: String
       
   141  ======================================================================================*/
       
   142 QString GeolocationManager::convertPermissionIntoString(QWebPage::PermissionPolicy permission)
       
   143 {
       
   144     QString retVal;
       
   145 
       
   146     switch (permission) {
       
   147       case QWebPage::PermissionGranted:
       
   148          retVal = "G";
       
   149          break;
       
   150       case QWebPage::PermissionDenied:
       
   151          retVal = "D";
       
   152          break;
       
   153       case QWebPage::PermissionUnknown:
       
   154          retVal = "U";
       
   155          break;
       
   156       default:
       
   157          retVal = "";    
       
   158     }
       
   159     
       
   160     return retVal;
       
   161 }
       
   162 
       
   163 /**===================================================================================
       
   164  * Description: Delete the geolocation item.
       
   165  * Returns: SUCCESS(0) or Failure(-1 or -2)
       
   166  ======================================================================================*/
       
   167 int GeolocationManager::deleteGeodomain(QString domain)
       
   168 {
       
   169    int retVal = SUCCESS;
       
   170        
       
   171     if (domain.isEmpty())
       
   172         retVal = FAILURE;
       
   173        
       
   174     if (retVal == SUCCESS) {
       
   175         if (m_geo.isOpen()) {
       
   176         QSqlQuery query(m_geo);
       
   177                
       
   178         query.prepare("DELETE FROM geolocationdata WHERE domain=:geodomain");
       
   179         query.bindValue(":geodomain", domain);
       
   180         if (!query.exec()) {
       
   181             lastErrMsg(query);
       
   182             retVal = DATABASEERROR;
       
   183         }
       
   184         } else 
       
   185                retVal = FAILURE;
       
   186     }
       
   187        
       
   188     return retVal;
       
   189 }
       
   190  
       
   191 /**===================================================================================
       
   192  * Description: Delete all records from the geolocation table.
       
   193  * Returns: SUCCESS(0) or FAILURE(-1 or -2)
       
   194  ======================================================================================*/
       
   195 int GeolocationManager::clearAllGeodata()
       
   196 {
       
   197     int retVal = SUCCESS;
       
   198     
       
   199     if (m_geo.isOpen()) {
       
   200         QSqlQuery query(m_geo);
       
   201         
       
   202         if (!query.exec("DELETE FROM geolocationdata")) {
       
   203             lastErrMsg(query);
       
   204             retVal = DATABASEERROR;
       
   205         }
       
   206     } else
       
   207         retVal = FAILURE;
       
   208     
       
   209     return retVal;
       
   210 }
       
   211 
       
   212 
       
   213 
       
   214 /**================================================================
       
   215  * Description: Adds a domain to the database. 
       
   216  * Returns: SUCCESS(0) or FAILURE (-1 or -2)
       
   217  ==================================================================*/
       
   218 int GeolocationManager::addGeodomain(QString domainToAdd, QWebPage::PermissionPolicy permission)
       
   219 {
       
   220     int retVal = SUCCESS;
       
   221     
       
   222     if (domainToAdd.isEmpty() || permission == QWebPage::PermissionUnknown)
       
   223         retVal = FAILURE;
       
   224     
       
   225     if (retVal == SUCCESS) {
       
   226         QString permissionPolicy(convertPermissionIntoString(permission));
       
   227         
       
   228         QDateTime dt  = QDateTime::currentDateTime();
       
   229         int timestamp = dt.toTime_t();
       
   230     
       
   231         if (m_geo.isOpen()) {
       
   232             QSqlQuery query(m_geo);
       
   233             m_geo.transaction();
       
   234             query.prepare("SELECT domain FROM geolocationdata WHERE domain=:domain");
       
   235             query.bindValue(":domain",domainToAdd);
       
   236             if (!query.exec()) {
       
   237                  lastErrMsg(query);
       
   238                  m_geo.rollback();
       
   239                 return DATABASEERROR;
       
   240             }
       
   241             if(!query.next()) {
       
   242                 QSqlQuery query2(m_geo);
       
   243                 query2.prepare("INSERT INTO geolocationdata (domain, creationtime, permission) "
       
   244                         "VALUES (:domain, :ctime, :ppermisson)");
       
   245                 query2.bindValue(":domain", QVariant(domainToAdd));
       
   246                 query2.bindValue(":ctime", QVariant(timestamp));
       
   247                 query2.bindValue(":ppermission", QVariant(permissionPolicy));
       
   248                 if (!query2.exec()) {
       
   249                     lastErrMsg(query);
       
   250                     m_geo.rollback();
       
   251                     retVal = DATABASEERROR;
       
   252                 }
       
   253             }
       
   254             query.finish();
       
   255             if (!m_geo.commit()) {
       
   256                  qDebug() << m_geo.lastError().text();
       
   257                  m_geo.rollback();
       
   258                  return DATABASEERROR;
       
   259             }
       
   260         } else
       
   261             retVal = FAILURE;
       
   262     }
       
   263     
       
   264     return retVal;
       
   265 }
       
   266 
       
   267 /**==============================================================
       
   268  * Description: Finds attributes (row) including permission policy for a domain.
       
   269  * Returns: empty list (not found) or list of attributes
       
   270  ===============================================================*/
       
   271 QList<QVariant> GeolocationManager::findGeodomain(QString domainToFind)
       
   272 { 
       
   273     QList<QVariant> retValue;
       
   274     
       
   275     if (m_geo.isOpen()) {
       
   276         QSqlQuery query(m_geo);
       
   277         query.prepare(
       
   278                 "SELECT permission FROM geolocationdata WHERE domain=:domain");
       
   279         query.bindValue(":domain",domainToFind);
       
   280         if (query.exec()) {
       
   281             if (query.next()) {
       
   282                 QWebPage::PermissionPolicy permission = convertStringIntoPermission(query.value(0).toString());
       
   283                 QVariant v;
       
   284                 v.setValue(permission);
       
   285                 retValue << v;
       
   286                 return retValue;
       
   287             }
       
   288         } else {
       
   289             lastErrMsg(query);
       
   290         }
       
   291     }
       
   292     
       
   293     return retValue;
       
   294 }
       
   295 
       
   296 /**==============================================================
       
   297  * Description: Prints a last error message from the query.
       
   298  * Returns: Nothing.
       
   299  ===============================================================*/
       
   300 void GeolocationManager::lastErrMsg(QSqlQuery& query)
       
   301 {
       
   302     qDebug() << "GeolocationManager::lastErrMsg" << QString("ERR: %1 %2").arg(
       
   303             query.lastError().type()).arg(query.lastError().text())
       
   304             << " Query: " << query.lastQuery();
       
   305 }
       
   306