qtinternetradio/ui/src/irsearchcriteriadb.cpp
changeset 0 09774dfdd46b
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     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 <QFile>
       
    19 #include <QSqlQuery>
       
    20 #include <QSqlRecord> 
       
    21 #include <QSqlDatabase>
       
    22 #include <QVariant>
       
    23  
       
    24 
       
    25 #include "irsearchcriteriadb.h"
       
    26 
       
    27 
       
    28 IRSearchCriteriaDB::IRSearchCriteriaDB(int aMaxCount):iDB(NULL),iMaxCount(aMaxCount)
       
    29 {
       
    30     createDBConnection();
       
    31 }
       
    32 
       
    33 IRSearchCriteriaDB::~IRSearchCriteriaDB()
       
    34 {
       
    35     if (iDB->isOpen())
       
    36     {
       
    37         iDB->close();
       
    38     }        
       
    39     
       
    40     delete iDB;
       
    41     iDB = NULL;
       
    42 }
       
    43 
       
    44 IRQError IRSearchCriteriaDB::addOneCriteria(const QString& aCriteria)
       
    45 {         
       
    46     if (iDB->isOpen())
       
    47     {
       
    48         if( aCriteria.length() >= CRITERIA_UPLIMIT_LENGTH )
       
    49         {             
       
    50             //the unicode string is too long to store
       
    51             return EIRQErrorBadParameter;
       
    52         }
       
    53         
       
    54         QSqlQuery query("SELECT * FROM criteria");
       
    55         QSqlRecord rec = query.record();         
       
    56  
       
    57         if( rec.count() >= iMaxCount )
       
    58         {
       
    59             bool ret = deleteSomeCriteria();            
       
    60             if( !ret )
       
    61             {
       
    62                 return EIRQErrorGeneral;
       
    63             }
       
    64         }
       
    65          
       
    66         
       
    67         bool ret = query.prepare("INSERT INTO criteria (cricolum) "
       
    68             "VALUES (?)");
       
    69         query.addBindValue(aCriteria,QSql::In);
       
    70         ret = query.exec();        
       
    71          
       
    72         emit dataAdded();
       
    73         return EIRQErrorNone;
       
    74     }
       
    75 
       
    76     return EIRQErrorServiceUnavailable;
       
    77 }
       
    78 
       
    79 bool IRSearchCriteriaDB::getAllCriteria(QStringList& aStrList) const
       
    80 {
       
    81     aStrList.clear();     
       
    82     if (iDB->isOpen())
       
    83     {
       
    84         QSqlQuery query("SELECT * FROM criteria");
       
    85         QSqlRecord rec = query.record();         
       
    86 
       
    87         int criteriaCol = rec.indexOf("cricolum");
       
    88         while (query.next())
       
    89         {
       
    90             aStrList.append(query.value(criteriaCol).toString());
       
    91         }
       
    92         return true;
       
    93     }
       
    94 
       
    95     return false;
       
    96 }
       
    97 
       
    98 void IRSearchCriteriaDB::createDBConnection()
       
    99 {    
       
   100     bool created = QFile::exists("searchcriteria.dat");    
       
   101     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");     
       
   102     iDB = new QSqlDatabase(db);
       
   103 
       
   104     iDB->setDatabaseName("searchcriteria.dat");
       
   105 
       
   106     if (!iDB->open())
       
   107     {         
       
   108         return;
       
   109     }
       
   110      
       
   111 
       
   112     if (!created)
       
   113     {
       
   114     	  bool dbResult = false;
       
   115         QSqlQuery query;         
       
   116         dbResult = query.exec("CREATE TABLE criteria ("
       
   117             "id INTEGER PRIMARY KEY AUTOINCREMENT, "
       
   118             "cricolum VARCHAR(255) NOT NULL)");
       
   119 
       
   120         if (!dbResult)
       
   121         {             
       
   122             return;
       
   123         }
       
   124     }    
       
   125 }
       
   126 
       
   127 bool IRSearchCriteriaDB::isAlreadyExist(const QString& aCriteria) const
       
   128 {         
       
   129     //TODO: change to the sql statement, but now the sqlquery binding 
       
   130     //dose not work well 
       
   131     if( aCriteria.length() >= CRITERIA_UPLIMIT_LENGTH )
       
   132     {    	   
       
   133         return true;
       
   134     }
       
   135     
       
   136     QStringList strList;
       
   137     getAllCriteria(strList);
       
   138     
       
   139     for(int i=0; i<strList.count(); i++)
       
   140     {
       
   141         if( 0 == strList[i].compare(aCriteria, Qt::CaseInsensitive))
       
   142         {            
       
   143             return true;
       
   144         }
       
   145     }     
       
   146      
       
   147     return false;
       
   148 }
       
   149 
       
   150 bool IRSearchCriteriaDB::deleteSomeCriteria()
       
   151 {
       
   152     //TODO: to delete some items when the db is full
       
   153     return true;   
       
   154 }
       
   155 
       
   156