qtinternetradio/irqfavoritesdb/src/irqfavoritesdb.cpp
changeset 17 38bbf2dcd608
parent 8 3b03c28289e6
--- a/qtinternetradio/irqfavoritesdb/src/irqfavoritesdb.cpp	Fri Sep 17 08:27:59 2010 +0300
+++ b/qtinternetradio/irqfavoritesdb/src/irqfavoritesdb.cpp	Mon Oct 04 00:07:46 2010 +0300
@@ -14,132 +14,157 @@
 * Description:
 *
 */
-#include "irqfavoritesdb.h" 
-#include "irqfavoritesdb_p.h"
+#include <QVariant>
 
-IRQFavoritesDB::IRQFavoritesDB() : d_ptr(new IRQFavoritesDBPrivate(this))
-{    
-    d_ptr->init();
+#include "irqfavoritesdb.h" 
+#include "favoriteswrapper.h"
+#include "channelhistorywrapper.h"
+#include "urlinfowrapper.h"
+#include "irqisdsdatastructure.h"
+#include "irqenums.h"
+
+IRQFavoritesDB::IRQFavoritesDB()
+{
+    iFavoritesWrapper = new favoritesWrapper;
+    iHistoryWrapper = new channelHistoryWrapper;
+    iUrlWrapper = new urlInfoWrapper;
 }
 
 IRQFavoritesDB::~IRQFavoritesDB()
 {
-    delete d_ptr;
+    delete iFavoritesWrapper;
+    delete iHistoryWrapper;
+    delete iUrlWrapper;
 }
 
-//add a preset 
+//add a preset to favorites
 //@param IRQPreset& the preset
 //@return  errcode
 int IRQFavoritesDB::addPreset(const IRQPreset& aPreset)
 {
-    return d_ptr->addPreset(aPreset);
+    bool ret = false;
+    bool newRow = false;
+    
+    columnMap map;
+    map.insert(channelId, QString::number(aPreset.presetId));
+    ret = iFavoritesWrapper->putFavorites(&map, newRow);
+    
+    if (!ret)
+    {
+        return EIRQErrorNotFound;  //presetId is not found
+    }
+    
+    if (newRow)
+    {
+        return EIRQErrorNone;
+    }
+    else
+    {
+        return EIRQErrorAlreadyExist;
+    }
 }
 
-//add a preset manually
-//@return  errcode
-//@param 
-//
-int IRQFavoritesDB::addPreset(const QString& aName, const QString& aURL)
-{
-    return d_ptr->addPreset(aName, aURL);
-}
-
-//get a preset uniq id
-//@return errcode
-//@param
-//
-int IRQFavoritesDB::getUniqId(int aNum) const
-{
-    return d_ptr->getUniqId(aNum);
-}
-
-//delete a preset by uniq id
+//delete a preset
 //@return errcode
 //@param
 //
-int IRQFavoritesDB::deletePreset(int aUniqId)
+int IRQFavoritesDB::deletePreset(const IRQPreset& aPreset)
 {
-    return d_ptr->deletePreset(aUniqId);
-}
+    columnMap map;
+    map.insert(channelId, QString::number(aPreset.presetId));
+    bool ret = iFavoritesWrapper->deleteFavorites(&map, NULL);
 
-//search a preset by uniqpresetId / isdspresetid
-//warning: the function needs further checking
-//@return errcode
-//
-int IRQFavoritesDB::searchPreset(int aUniqPresetId, int aIsdsPresetId)
-{
-    return d_ptr->searchPreset(aUniqPresetId, aIsdsPresetId);
+    if (ret)
+    {
+        return EIRQErrorNone;
+    }
+    else
+    {
+        return EIRQErrorNotFound;
+    }
 }
 
-//
-//get the previouse preset index in the internal list
-//@return the index
-//
-int IRQFavoritesDB::getPreviousPreset(int aIndex)
-{
-    return d_ptr->getPreviousPreset(aIndex);
-}
-
-//
-//get the next preset index
-//@return the index
-//
-int IRQFavoritesDB::getNextPreset(int aIndex)
-{
-    return d_ptr->getNextPreset(aIndex);
-}
- 
 //replace with new preset
 //@return errcode 
 //
-int IRQFavoritesDB::replacePreset(const IRQPreset& aNewPreset)
+void IRQFavoritesDB::replacePreset(const IRQPreset& aNewPreset)
 {
-    return d_ptr->replacePreset(aNewPreset);
-}
-
-//@return errcode
-//
-int IRQFavoritesDB::replaceUserDefinedPreset(const IRQPreset& aNewPreset)
-{
-    return d_ptr->replaceUserDefinedPreset(aNewPreset);
+    //write the preset data to database
+    columnMap map;
+    QString url;
+    
+    if (IRQPreset::EIsds == aNewPreset.type)
+    {
+        map.insert(channelId, QString::number(aNewPreset.presetId));
+    }
+    map.insert(channelName, aNewPreset.name);
+    map.insert(languageCode, aNewPreset.languageCode);
+    map.insert(languageName, aNewPreset.languageName);
+    map.insert(countryCode, aNewPreset.countryCode);
+    map.insert(countryName, aNewPreset.countryName);
+    map.insert(lastModified, aNewPreset.lastModified);
+    map.insert(musicStoreStatus, aNewPreset.musicStoreStatus);
+    map.insert(description, aNewPreset.description);
+    map.insert(shortDesc, aNewPreset.shortDesc);
+    map.insert(genreId, aNewPreset.genreId);
+    map.insert(genreName, aNewPreset.genreName);
+    map.insert(advertisementUrl, aNewPreset.advertisementUrl);
+    map.insert(advertisementInUse, aNewPreset.advertisementInUse);
+    map.insert(imgUrl, aNewPreset.imgUrl);
+    
+    uint cIdResult = 0;
+    if (IRQPreset::EIsds == aNewPreset.type)
+    {
+        cIdResult = aNewPreset.presetId;
+        columnMap condAnd;
+        condAnd.insert(channelId, QString::number(aNewPreset.presetId));
+        iHistoryWrapper->putChannelHistory(&map, NULL, NULL, &condAnd);
+    }
+    else
+    {
+        columnMap condAnd;
+        condAnd.insert(channelType, QString::number(aNewPreset.type));
+        aNewPreset.getChannelUrlAt(0, url);
+        condAnd.insert(channelUrl, url);
+        condAnd.insert(channelName, aNewPreset.name);
+        QList<uint> *ids = iHistoryWrapper->getChannelId(&condAnd);
+        if (ids && ids->count() > 0)
+        {
+            //the user-defined preset is already in database
+            cIdResult = ids->at(0);
+            condAnd.clear();
+            condAnd.insert(channelId, QString::number(cIdResult));
+            iHistoryWrapper->putChannelHistory(&map, NULL, NULL, &condAnd);
+        }
+        else
+        {
+            //the user-defined preset is not in database
+            iHistoryWrapper->putChannelHistory(&map, &cIdResult);
+        }
+        
+        delete ids;
+    }
+    
+    //write url info to database
+    columnUrlInfoInsertMap urlmap;
+    
+    unsigned int bitrate = 0;
+    for (int i = 0; i < aNewPreset.getChannelURLCount(); i++)
+    {
+        aNewPreset.getChannelBitrate(i, bitrate);
+        aNewPreset.getChannelUrlAt(i, url);
+        urlmap.insert(url, bitrate);
+    }
+    
+    iUrlWrapper->resetUrlInfo(&urlmap, cIdResult);
 }
 
 //change the preset type to user defined
 //@return errcode
 //
-int IRQFavoritesDB::makePresetUserDefined(int aChannelId, int aUserDefinedChannelId)
-{
-    return d_ptr->makePresetUserDefined(aChannelId, aUserDefinedChannelId);
-}
-
-//get the empty preset left count
-//@return the count of empty presets left 
-//
-int IRQFavoritesDB::emptyPresetCount() const
+int IRQFavoritesDB::makePresetUserDefined(int /*aChannelId*/, int /*aUserDefinedChannelId*/)
 {
-    return d_ptr->emptyPresetCount();
-}
-
-//get the max preset count supported now
-//@return errcode 
-//
-int IRQFavoritesDB::maxPresetCount()
-{
-    return d_ptr->maxPresetCount();
-}
-
-//the interface is not used currently.
-//
-void IRQFavoritesDB::setMoveStatus(bool aStatus)
-{
-    d_ptr->setMoveStatus(aStatus);  
-}
-
-//the interface is not used currently 
-//
-bool IRQFavoritesDB::getMoveStatus()
-{
-    return d_ptr->getMoveStatus();
+    return 0;
 }
 
 //for CIRPreset is just an interface so we can wrapper it into the IRQPreset.
@@ -148,18 +173,84 @@
 //
 QList<IRQPreset*>* IRQFavoritesDB::getPresets() const
 {
-    return d_ptr->getPresets();
-}
-
-/*
- * Increase the played times of the preset if it's in the favorites
- */
-void IRQFavoritesDB::increasePlayedTimes(const IRQPreset &aPreset)
-{
-    d_ptr->increasePlayedTimes(aPreset);
+    favoritesWrapper favorites;
+    QList<QVariant*> *dataSet = favorites.getFavorites();
+    if (NULL == dataSet)
+    {
+        return NULL;
+    }
+    
+    QList<IRQPreset*> *presetList = NULL;
+    if (dataSet->count() > 0)
+    {
+        presetList = new QList<IRQPreset*>;
+        int dataSize = dataSet->count();
+        for (int i = 0; i < dataSize; ++i)
+        {
+            IRQPreset *preset = new IRQPreset;
+            bool ok = false;
+            QVariant *row = dataSet->at(i);
+            preset->type = row[channelType].toInt(&ok);
+            preset->presetId = row[channelId].toUInt(&ok);
+            preset->name = row[channelName].toString();
+            preset->nickName = row[channelNickName].toString();
+            preset->languageCode = row[languageCode].toString();
+            preset->languageName = row[languageName].toString();
+            preset->countryCode = row[countryCode].toString();
+            preset->countryName = row[countryName].toString();
+            preset->lastModified = row[lastModified].toString();
+            preset->musicStoreStatus = row[musicStoreStatus].toString();
+            preset->description = row[description].toString();
+            preset->shortDesc = row[shortDesc].toString();
+            preset->genreName = row[genreName].toString();
+            preset->genreId = row[genreId].toString();
+            preset->advertisementInUse = row[advertisementInUse].toString();
+            preset->advertisementUrl = row[advertisementUrl].toString();
+            preset->imgUrl = row[imgUrl].toString();
+            
+            delete []row;
+            
+            //get url info and write to preset
+            urlInfoWrapper urlInfo;
+            columnMap cond;
+            cond.insert(channelId, QString::number(preset->presetId));
+            QList<QVariant*> *urlDataSet = urlInfo.getUrlInfo(&cond, NULL, i, dataSize-1);
+            int urlSize =  urlDataSet->count();
+            for (int j = 0; j < urlSize; ++j)
+            {
+                IRQChannelServerURL url;
+                url.url = urlDataSet->at(j)[channelUrl_URL].toString();
+                url.bitrate = urlDataSet->at(j)[bitRate_URL].toInt();
+                preset->insertChannelServer(url);
+                delete []urlDataSet->at(j);
+            }
+            delete urlDataSet;
+            
+            presetList->append(preset);
+        }
+    }
+    
+    delete dataSet;
+    
+    return presetList;
 }
 
 int IRQFavoritesDB::renamePreset(const IRQPreset &aPreset, const QString &aNewName)
 {
-    return d_ptr->renamePreset(aPreset, aNewName);
+    channelHistoryWrapper wrapper;
+    columnMap map;
+
+    map.insert(channelNickName, aNewName);
+    columnMap condAND;
+    condAND.insert(channelId, QString::number(aPreset.presetId));
+    bool ret = wrapper.putChannelHistory(&map, NULL, NULL, &condAND);
+    
+    if (ret)
+    {
+        return EIRQErrorNone;
+    }
+    else
+    {
+        return EIRQErrorGeneral;
+    }
 }