onlinesearchproviders/isproviderdbmanager/src/isproviderdbmanager.cpp
changeset 12 993ab30e92fc
equal deleted inserted replaced
11:773be20e0a25 12:993ab30e92fc
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     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  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QSqlQuery>
       
    19 #include <QSqlError>
       
    20 #include <qsysteminfo.h>
       
    21 using namespace QtMobility;
       
    22 #include <QVariant>
       
    23 #include <QStringList.h>
       
    24 
       
    25 #include "isproviderdbmanager.h"
       
    26 #include "isproviderdbconstants.h"
       
    27 
       
    28 
       
    29 /*!
       
    30  * \brief Constructor
       
    31  */
       
    32 IsProviderDBManager::IsProviderDBManager( QObject *parent ) :  QObject(parent)
       
    33     {    
       
    34     }
       
    35 
       
    36 /*!
       
    37  * \brief Destructor
       
    38  */
       
    39 IsProviderDBManager::~IsProviderDBManager()
       
    40     {
       
    41     }
       
    42 
       
    43 /*!
       
    44  * \brief Opens the database
       
    45  */
       
    46 bool IsProviderDBManager::OpenDatabase()
       
    47     {
       
    48     m_db = QSqlDatabase::addDatabase(databaseType, connectionName);
       
    49     m_db.setDatabaseName(databaseName);
       
    50     
       
    51     if ( !m_db.isValid())
       
    52         {
       
    53         CloseDatabase();
       
    54         return false;
       
    55         }
       
    56     
       
    57     if (!m_db.isOpen())
       
    58         {
       
    59         if (!m_db.open())
       
    60             {
       
    61             CloseDatabase();
       
    62             return false;
       
    63             }
       
    64         }
       
    65     return true;
       
    66     }
       
    67 
       
    68 /*!
       
    69  * \brief Closes the database
       
    70  */
       
    71 void IsProviderDBManager::CloseDatabase()
       
    72     {    
       
    73   //  if (m_db.isValid() && m_db.isOpen())
       
    74   //      {
       
    75         m_db.close();
       
    76    //     }
       
    77     }
       
    78 
       
    79 /*!
       
    80  * \brief Reads providers allowed in the current country
       
    81  */
       
    82 bool IsProviderDBManager::GetProviders(QList<ServiceProvider>& providers)
       
    83     {
       
    84     bool ok = OpenDatabase();
       
    85 
       
    86     if (ok)
       
    87         {
       
    88         // Create query that gets providers allowed this country
       
    89         QString queryString(providerQuery);        
       
    90         // get country code and append to query
       
    91         queryString.append(GetCountryCode());
       
    92         queryString.append("\'");
       
    93     
       
    94         QSqlQuery query(QSqlDatabase::database(connectionName));    
       
    95         query.prepare(queryString);
       
    96         query.exec();
       
    97         // read all found providers
       
    98         while (query.next())
       
    99             {
       
   100             ServiceProvider* provider = new ServiceProvider();
       
   101 
       
   102             int providerId = query.value(0).toInt();
       
   103             provider->SetId(providerId);
       
   104             
       
   105             QString providerName = query.value(1).toString();
       
   106             provider->SetName(providerName);
       
   107             
       
   108             QString providerDescription = query.value(2).toString();
       
   109             provider->SetDescription(providerDescription);
       
   110 
       
   111             HbIcon icon(query.value(3).toString());            
       
   112             provider->SetIcon(icon);
       
   113             
       
   114             QString pluginName = query.value(10).toString();
       
   115             provider->SetPluginName(pluginName);
       
   116             providers.append(*provider);
       
   117             }    
       
   118         }
       
   119     
       
   120     CloseDatabase();
       
   121     
       
   122     return ok;
       
   123     }
       
   124 
       
   125 /*!
       
   126  * \brief Gets selected provider
       
   127  */
       
   128 ServiceProvider* IsProviderDBManager::SelectedProvider()
       
   129     {
       
   130     ServiceProvider* provider = NULL;
       
   131     
       
   132     bool ok = OpenDatabase();
       
   133     if (ok)
       
   134         {
       
   135         // Create query that gets providers allowed this country       
       
   136         QSqlQuery query(QSqlDatabase::database(connectionName));    
       
   137         query.prepare(selectedProviderQuery);
       
   138         query.exec();  
       
   139 
       
   140         // read all found providers
       
   141         while (query.next())
       
   142             {
       
   143             provider = new ServiceProvider();
       
   144             
       
   145             int providerId = query.value(0).toInt();
       
   146             provider->SetId(providerId);
       
   147             
       
   148             QString providerName = query.value(1).toString();
       
   149             provider->SetName(providerName);
       
   150             
       
   151             QString providerDescription = query.value(2).toString();
       
   152             provider->SetDescription(providerDescription);
       
   153 
       
   154             HbIcon icon(query.value(3).toString());            
       
   155             provider->SetIcon(icon);           
       
   156             
       
   157             QString providerUrl = query.value(4).toString();
       
   158             provider->SetProviderUrl(providerUrl);
       
   159             }    
       
   160         }
       
   161     
       
   162     CloseDatabase();
       
   163     
       
   164     return provider;
       
   165     }
       
   166 
       
   167 /*!
       
   168  * \brief Sets selected provider
       
   169  */
       
   170 bool IsProviderDBManager::SetSelectedProvider(int providerId)
       
   171     {    
       
   172     bool ok = OpenDatabase();
       
   173     if (ok)
       
   174         {
       
   175         // Clear selection status from all providers
       
   176         QSqlQuery query(QSqlDatabase::database(connectionName));    
       
   177         query.prepare(resetProviderSelection);
       
   178         ok = query.exec();
       
   179         
       
   180         if (ok)
       
   181             {
       
   182             // Set selection status to selected provider    
       
   183             QString queryString(setSelectedProvider);
       
   184             queryString.append( QString::number(providerId));        
       
   185        
       
   186             query.clear();
       
   187             query.prepare(queryString);
       
   188             ok = query.exec();        
       
   189             }
       
   190         }
       
   191     
       
   192     CloseDatabase();
       
   193     
       
   194     return ok;
       
   195     }
       
   196 
       
   197 /*!
       
   198  * \brief Reads country list from database
       
   199  */
       
   200 bool IsProviderDBManager::GetCountryList(QStringList& countryList)
       
   201     {
       
   202     bool ok = OpenDatabase();
       
   203     if (ok)
       
   204         {
       
   205         // Create query that gets country list   
       
   206         QSqlQuery query(QSqlDatabase::database(connectionName));    
       
   207         query.prepare(countryQuery);
       
   208         ok = query.exec();
       
   209 
       
   210         // read all found providers
       
   211         while (query.next())
       
   212             {
       
   213                 countryList << query.value(0).toString();
       
   214             }     
       
   215         }
       
   216     
       
   217     CloseDatabase();   
       
   218     
       
   219     return ok;
       
   220     }
       
   221 
       
   222 /*!
       
   223  * \brief Return current country code retrieved from system
       
   224  */
       
   225 QString IsProviderDBManager::GetCountryCode()
       
   226     {
       
   227     QSystemInfo *sysInfo = new QSystemInfo(this);
       
   228     return "FI";//sysInfo->currentCountryCode();
       
   229     }