qtinternetradio/ui/src/irchannelmodel.cpp
changeset 17 38bbf2dcd608
parent 11 f683e24efca3
--- a/qtinternetradio/ui/src/irchannelmodel.cpp	Fri Sep 17 08:27:59 2010 +0300
+++ b/qtinternetradio/ui/src/irchannelmodel.cpp	Mon Oct 04 00:07:46 2010 +0300
@@ -15,16 +15,31 @@
 *
 */
 
-#include <hbicon.h>
+#include <HbIcon>
+#include <QTimer>
 
 #include "irchannelmodel.h"
 #include "irqisdsdatastructure.h"
 #include "irsearchresultdb.h"
+#include "irqisdsclient.h"
+#include "irlogoprovider.h"
 
 IrChannelModel::IrChannelModel(QObject *aParent): QAbstractListModel(aParent)
                                                 , iChannelList(NULL),iDB(NULL)
 {
-    iStationLogo = new HbIcon("qtg_large_internet_radio");   
+    iIsdsClient = IRQIsdsClient::openInstance();
+    iLogoProvider = new IRLogoProvider(iIsdsClient);
+    
+    iTimer = new QTimer;
+    iTimer->setInterval(10);
+    connect(iTimer, SIGNAL(timeout()), this, SLOT(downloadNextLogo()));
+    
+    iStationLogo = new HbIcon("qtg_large_internet_radio");
+
+    if( NULL == iDB )
+    {
+        iDB = new IRSearchResultDB();
+    }
 }
 
 IrChannelModel::~IrChannelModel()
@@ -36,11 +51,21 @@
     
     clearAndDestroyLogos();
 
+    stopDownloadingLogo();
+
     if( iDB )
     {
         delete iDB;
         iDB = NULL;
     }
+
+    delete iLogoProvider;
+    if (iIsdsClient)
+    {
+        iIsdsClient->closeInstance();
+        iIsdsClient = NULL;
+    }
+    delete iTimer;
 }
 
 int IrChannelModel::rowCount(const QModelIndex &aParent) const
@@ -55,18 +80,6 @@
     return count;
 }
 
-QString IrChannelModel::imageUrl(int aRow)
-{
-    if (iChannelList)
-    {
-        return iChannelList->at(aRow)->imageURL;
-    }
-    else
-    {
-        return "";
-    }
-}
-
 void IrChannelModel::setLogo(HbIcon *aIcon, int aIndex)
 {
     iLogos[aIndex] = aIcon;
@@ -119,37 +132,29 @@
     }
 }
 
-void IrChannelModel::updateData(QList<IRQChannelItem*> *aPushItemsList)
+void IrChannelModel::updateData(QList<IRQChannelItem*> *aPushItemsList, bool bInit)
 {
     if (iChannelList != aPushItemsList)
     {
         clearAndDestroyItems();
         iChannelList = aPushItemsList;
     }
+    if(false == bInit)
+    {
+        save2Cache();
+    
+    }
     
     clearAndDestroyLogos();
-    
+
+    updateIconIndexArray();
+
     emit dataAvailable();
 }
 
 void IrChannelModel::initWithCache()
 {
-    if( NULL == iDB )
-    {
-        iDB = new IRSearchResultDB();
-    }
-    
-    QList<IRQChannelItem*> *channelList = iDB->getCahcedChannelList();
-   
-    if( NULL == channelList )
-    {
-        //some error happens
-        return;
-    }
-    
-    clearAndDestroyItems();
-    clearAndDestroyLogos();
-    iChannelList = channelList;    
+    updateData(iDB->getCahcedChannelList(), true);
 }
 
 void IrChannelModel::save2Cache()
@@ -178,7 +183,8 @@
     clearAndDestroyItems();
     clearAndDestroyLogos();
     iDB->clearCache();
-    
+    iIconIndexArray.clear();
+
     emit dataAvailable();
 }
 
@@ -204,3 +210,75 @@
         iChannelList = NULL;
     }
 }
+
+void IrChannelModel::startDownloadingLogo()
+{
+    iLogoProvider->activate(this, SLOT(logoData(const QByteArray&)));
+    iTimer->start();
+}
+
+void IrChannelModel::stopDownloadingLogo()
+{
+    iIsdsClient->isdsLogoDownCancelTransaction();
+    iTimer->stop();
+    iLogoProvider->deactivate();
+}
+
+void IrChannelModel::downloadNextLogo()
+{
+    iTimer->stop();
+    int leftCount = iIconIndexArray.count();
+
+    if (0 != leftCount)
+    {
+         int row = iIconIndexArray[0];
+         IRQPreset preset;
+         preset.name = iChannelList->at(row)->channelName;
+         preset.shortDesc = iChannelList->at(row)->shortDescription;
+         preset.type = IRQPreset::EIsds;
+         preset.presetId = iChannelList->at(row)->channelID;
+		 preset.imgUrl = iChannelList->at(row)->imageURL;
+         iLogoProvider->getLogo(&preset);
+    }
+}
+
+void IrChannelModel::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();  
+    }
+}
+
+void IrChannelModel::updateIconIndexArray()
+{
+    iIconIndexArray.clear();
+    
+    for (int i = 0; i < rowCount(); ++i)
+    {
+        if (iChannelList->at(i)->imageURL != "" && !iLogos.contains(i))
+        {
+            iIconIndexArray.append(i);
+        }
+    }
+
+    if (iIconIndexArray.size() > 0)
+    {
+        startDownloadingLogo();
+    }
+}