qtinternetradio/ui/src/irhistorymodel.cpp
changeset 17 38bbf2dcd608
parent 3 ee64f059b8e1
--- a/qtinternetradio/ui/src/irhistorymodel.cpp	Fri Sep 17 08:27:59 2010 +0300
+++ b/qtinternetradio/ui/src/irhistorymodel.cpp	Mon Oct 04 00:07:46 2010 +0300
@@ -15,37 +15,58 @@
 *
 */
 
-#include <hbicon.h>
+#include <HbIcon>
+#include <QTimer>
 
-#include "irqsonghistoryinfo.h"
-#include "irqsonghistoryengine.h"
+#include "irqisdsdatastructure.h"
+#include "channelhistorywrapper.h"
+#include "urlinfowrapper.h"
 #include "irhistorymodel.h"
+#include "irqisdsclient.h"
+#include "irlogoprovider.h"
 
-IRHistoryModel::IRHistoryModel(QObject *aParent) : QAbstractListModel(aParent)
+IRHistoryModel::IRHistoryModel(QObject *aParent) : QAbstractListModel(aParent),
+    iHistoryEngine(NULL), iUrlInfoWrapper(NULL), iStationLogo(NULL),
+    iIsdsClient(NULL), iLogoProvider(NULL), iTimer(NULL)
 {
     iStationLogo = new HbIcon("qtg_large_internet_radio");
 
-    iHistoryEngine = IRQSongHistoryEngine::openInstance();
-    getAllList();
+    iHistoryEngine = new channelHistoryWrapper();
+    
+    iUrlInfoWrapper = new urlInfoWrapper();
+    
+    iIsdsClient = IRQIsdsClient::openInstance();
+    iLogoProvider = new IRLogoProvider(iIsdsClient);
+    
+    iTimer = new QTimer();
+    iTimer->setInterval(10);
+    connect(iTimer, SIGNAL(timeout()), this, SLOT(downloadNextLogo()));
+        
+    refreshModel();
 }
 
 IRHistoryModel::~IRHistoryModel()
 {
+    clearModel();
+	
     delete iStationLogo;
     iStationLogo = NULL;
-
-    clearAndDestroyLogos();
-
-    while (!iHistoryList.isEmpty())
+    
+    delete iHistoryEngine;
+    iHistoryEngine = NULL;
+    
+    delete iUrlInfoWrapper;
+    iUrlInfoWrapper = NULL;
+    
+	stopDownloadingLogo();
+	
+    delete iLogoProvider;
+    if (iIsdsClient)
     {
-        delete iHistoryList.takeFirst();
+        iIsdsClient->closeInstance();
+        iIsdsClient = NULL;
     }
-    
-    if (iHistoryEngine)
-    {
-        iHistoryEngine->closeInstance();
-        iHistoryEngine = NULL;
-    }
+    delete iTimer;    
 }
 
 int IRHistoryModel::rowCount(const QModelIndex &aParent) const
@@ -56,11 +77,17 @@
 
 QString IRHistoryModel::getImageUrl(int aRow) const
 {
-    return iHistoryList.at(aRow)->getImageUrl();
+    return iHistoryList.at(aRow)->imgUrl;
 }
 
 void IRHistoryModel::setLogo(HbIcon *aIcon, int aIndex)
 {
+    int elementCountNeedToAdd = aIndex + 1 - iLogos.size();
+    while (elementCountNeedToAdd > 0)
+    {
+        iLogos.append(NULL);
+        elementCountNeedToAdd--;
+    }
     iLogos[aIndex] = aIcon;
     emit dataChanged(index(aIndex), index(aIndex));
 }
@@ -82,10 +109,10 @@
         QVariantList list;
         
         int row = aIndex.row();
-        QString primaryText = iHistoryList.at(row)->getChannelName();
+        QString primaryText = iHistoryList.at(row)->nickName;
         list.append(primaryText);
         // fix bug #9888,if left descriptions as blank, only one line appears
-        QString tempDes = iHistoryList.at(row)->getChannelDesc();
+        QString tempDes = iHistoryList.at(row)->shortDesc;
         if (0 == tempDes.length())
         {
             tempDes = " ";
@@ -98,23 +125,29 @@
     {
         QVariantList list;
         int row = aIndex.row();
-        const HbIcon *icon = iLogos.value(row);
-        if (icon)
+        if(row < iLogos.size())
         {
-            list.append(*icon);
+            const HbIcon *icon = iLogos[row];
+            if (icon)
+            {
+                list.append(*icon);
+            }
+            else
+            {
+                list.append(*iStationLogo);
+            }            
         }
         else
         {
             list.append(*iStationLogo);
-        }
-        
+        }       
         return list;
     }
 
     return QVariant();
 }
 
-IRQSongHistoryInfo* IRHistoryModel::getHistoryInfo(int aIndex)
+IRQPreset* IRHistoryModel::getHistoryInfo(int aIndex)
 {
     if (aIndex >= 0 && aIndex < iHistoryList.count())
     {
@@ -124,54 +157,246 @@
     return NULL;
 }
 
-void IRHistoryModel::clearAllList()
+void IRHistoryModel::clearAllHistory()
 {
-    while (!iHistoryList.isEmpty())
+    if (iHistoryEngine->deleteChannelHistory())
     {
-        IRQSongHistoryInfo *firstItem = iHistoryList.takeFirst();
-        delete firstItem;
+        clearModel();
+        emit modelChanged();
     }
-    iHistoryEngine->clearAllHistory();
-    clearAndDestroyLogos();
-    emit modelChanged();
 }
 
 bool IRHistoryModel::checkHistoryUpdate()
 {
-    getAllList();    
+    refreshModel();    
     return true;
 }
 
 void IRHistoryModel::clearAndDestroyLogos()
 {
-    for (QMap<int, HbIcon*>::iterator it = iLogos.begin(); it != iLogos.end(); ++it)
+    int size = iLogos.size(); 
+    for (int i = 0; i < size; i ++)
     {
-        delete it.value();
+        delete iLogos[i];
+        iLogos[i] = NULL;
     }
-    
     iLogos.clear();
 }
 
-void IRHistoryModel::getAllList()
+void IRHistoryModel::refreshModel()
 {
-    iHistoryEngine->getAllHistory(iHistoryList);
-
+    clearModel();
+    
+    QList<QVariant*>* historySet = NULL;
+    historySet = iHistoryEngine->getChannelHistory();
+    
+    if (NULL == historySet)
+    {
+        return;
+    }
+    int dataSize = historySet->size();
+    for (int i = 0; i < dataSize; i++)
+    {
+        IRQPreset* preset = new IRQPreset();
+        preset->presetId = (*(historySet->at(i) + channelId)).toUInt();  
+        preset->name = (*(historySet->at(i) + channelName)).toString();
+        preset->nickName = (*(historySet->at(i) + channelNickName)).toString();
+        
+        preset->genreName = (*(historySet->at(i) + genreName)).toString();
+        preset->genreId = (*(historySet->at(i) + genreId)).toString();
+        preset->languageName = (*(historySet->at(i) + languageName)).toString();
+        preset->languageCode = (*(historySet->at(i) + languageCode)).toString(); 
+        
+        preset->countryName = (*(historySet->at(i) + countryName)).toString();
+        preset->countryCode = (*(historySet->at(i) + countryCode)).toString();
+        preset->description = (*(historySet->at(i) + description)).toString();
+        preset->shortDesc = (*(historySet->at(i) + shortDesc)).toString();
+        
+        preset->lastModified = (*(historySet->at(i) + lastModified)).toString();
+        preset->type = (*(historySet->at(i) + channelType)).toInt();
+        preset->musicStoreStatus = (*(historySet->at(i) + musicStoreStatus)).toString(); 
+        
+        preset->imgUrl = (*(historySet->at(i) + imgUrl)).toString();
+        
+        preset->advertisementUrl = (*(historySet->at(i) + advertisementUrl)).toString();
+        preset->advertisementInUse = (*(historySet->at(i) + advertisementInUse)).toString();
+       
+        columnMap selectCriteria;
+        selectCriteria.insert(channelId, QString::number(preset->presetId));
+        
+        QList<QVariant*>* urlInfoList = iUrlInfoWrapper->getUrlInfo(&selectCriteria, NULL, i, dataSize-1);        
+        if (NULL == urlInfoList)
+        {
+            delete preset;
+            preset = NULL;
+            continue;
+        }
+        int urlSize = urlInfoList->size();
+        for (int j = 0; j < urlSize; j++)
+        {
+            IRQChannelServerURL urlInfo;
+            urlInfo.serverName = preset->name;
+            urlInfo.url = (*(urlInfoList->at(j) + channelUrl_URL)).toString();
+            urlInfo.bitrate = (*(urlInfoList->at(j) + bitRate_URL)).toInt();
+            preset->insertChannelServer(urlInfo);
+        }        
+        
+        while(false == urlInfoList->isEmpty())
+        {
+            delete []urlInfoList->takeFirst();
+        }
+        urlInfoList->clear();
+        
+        delete urlInfoList;
+        urlInfoList = NULL;
+        
+        iHistoryList.append(preset);
+    }
+    
+    while(false == historySet->isEmpty())
+    {
+        delete []historySet->takeFirst();
+    }
+    historySet->clear();
+    
+    delete historySet;
+    historySet = NULL;
+    
+    updateIconIndexArray();
+    
     emit modelChanged();
 }
 
-bool IRHistoryModel::deleteOneItem(int aIndex)
+void IRHistoryModel::clearModel()
+{
+    while (!iHistoryList.isEmpty())
+    {
+        IRQPreset *firstItem = iHistoryList.takeFirst();
+        delete firstItem;
+    }
+	
+    clearAndDestroyLogos();	
+	updateIconIndexArray();    
+}    
+
+bool IRHistoryModel::deleteHistory(int aIndex)
 {
-    bool ret = iHistoryEngine->deleteOneItem(aIndex);
-     
-    if( !ret )
+    if (aIndex < 0 && aIndex >= iHistoryList.count())
+    {
+        return false;
+    }
+        
+    if (!iIconIndexArray.empty())
+    {
+        iIsdsClient->isdsLogoDownCancelTransaction();
+        iTimer->stop();
+    }
+            
+    columnMap selectCriteria;
+    selectCriteria.insert(channelId, QString::number(iHistoryList.at(aIndex)->presetId));
+         
+    if (!iHistoryEngine->deleteChannelHistory(&selectCriteria))
     {
         return false;                
     }
     
     beginRemoveRows(QModelIndex(), aIndex, aIndex);
+    
+    delete iHistoryList.at(aIndex);
     iHistoryList.removeAt(aIndex);
+    
+    if (aIndex < iLogos.size())
+    {
+        delete iLogos[aIndex];
+        iLogos.removeAt(aIndex);
+    }
+	
+    updateIconIndexArray();
+    
     endRemoveRows(); 
     
-    emit modelChanged();    
+    emit modelChanged();  
+    
+    if (!iIconIndexArray.empty())
+    {
+        iTimer->start();
+    }
+          
     return true;
 }
+
+void IRHistoryModel::startDownloadingLogo()
+{
+    iLogoProvider->activate(this, SLOT(logoData(const QByteArray&)));
+    iTimer->start();
+}
+
+void IRHistoryModel::stopDownloadingLogo()
+{
+    iIsdsClient->isdsLogoDownCancelTransaction();
+    iTimer->stop();
+    iIconIndexArray.clear();
+    iLogoProvider->deactivate();
+}
+
+void IRHistoryModel::downloadNextLogo()
+{
+    iTimer->stop();
+    int leftCount = iIconIndexArray.count();
+
+    if (0 != leftCount)
+    {
+         iLogoProvider->getLogo(iHistoryList.at(iIconIndexArray[0]));
+    }
+}
+
+void IRHistoryModel::logoData(const QByteArray &aLogoData)
+{
+    if (aLogoData.size() > 0)
+    {
+        QPixmap tempMap;
+        bool ret = tempMap.loadFromData((const unsigned char*)aLogoData.constData(), aLogoData.size());
+        if( ret )
+        {
+            QIcon convertIcon(tempMap);
+            HbIcon *hbIcon = new HbIcon(convertIcon);
+            int index = iIconIndexArray[0];
+            setLogo(hbIcon, index);  
+        }
+    }
+    
+    iIconIndexArray.removeAt(0);
+    int leftCount = iIconIndexArray.count();
+    if( leftCount > 0 )
+    {
+        iTimer->start();  
+    }
+}
+
+bool IRHistoryModel::isLogoReady(int aIndex) const
+{
+    int logoListCount = iLogos.count();
+    if (aIndex >= 0 
+        && aIndex < logoListCount)
+    {
+        return iLogos[aIndex] != NULL;
+    }
+    else
+    {
+        return false;
+    }
+}
+
+void IRHistoryModel::updateIconIndexArray()
+{
+    iIconIndexArray.clear();
+    
+    for (int i = 0; i < rowCount(); ++i)
+    {
+        if (getImageUrl(i) != "" && !isLogoReady(i))
+        {
+            iIconIndexArray.append(i);
+        }
+    } 
+}
+