qtinternetradio/ui/src/irchanneldataprovider.cpp
changeset 15 065198191975
child 16 5723da102db1
equal deleted inserted replaced
14:896e9dbc5f19 15:065198191975
       
     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 <HbIcon>
       
    19 #include <QTimer>
       
    20 
       
    21 #include "irchanneldataprovider.h"
       
    22 #include "irqisdsdatastructure.h"
       
    23 #include "irqisdsclient.h"
       
    24 #include "irsearchresultdb.h"
       
    25 
       
    26 const int KBitmapSize = 59;
       
    27 const int KDownloadLogoInterval = 10; //msec
       
    28 
       
    29 IRChannelDataProvider::IRChannelDataProvider(QObject *aParent) : HgDataProviderModel(aParent),
       
    30                        iChannelList(NULL), iLogoPreset(NULL), iActivated(false)
       
    31 {
       
    32     iDefaultLogo = HbIcon("qtg_large_internet_radio");
       
    33     
       
    34     iLogoDownloadTimer = new QTimer(this);
       
    35     iLogoDownloadTimer->setInterval(KDownloadLogoInterval);
       
    36     connect(iLogoDownloadTimer, SIGNAL(timeout()), this, SLOT(downloadNextLogo()));
       
    37     
       
    38     iIsdsClient = IRQIsdsClient::openInstance();
       
    39 }
       
    40 
       
    41 IRChannelDataProvider::~IRChannelDataProvider()
       
    42 {
       
    43     if (iIsdsClient)
       
    44     {
       
    45         iIsdsClient->closeInstance();
       
    46         iIsdsClient = NULL;
       
    47     }
       
    48     
       
    49     delete iLogoPreset;
       
    50     iLogoPreset = NULL;
       
    51     
       
    52     clearAndDestroyItems();
       
    53     clearAndDestroyLogos();
       
    54 }
       
    55 
       
    56 void IRChannelDataProvider::activate()
       
    57 {
       
    58     iActivated = true;
       
    59     connect(iIsdsClient, SIGNAL(presetLogoDownloaded(IRQPreset* )),
       
    60             this, SLOT(presetLogoDownload(IRQPreset* )));
       
    61     connect(iIsdsClient, SIGNAL(presetLogoDownloadError()),
       
    62             this, SLOT(presetLogoDownloadError()));
       
    63     
       
    64     if (iRequestedItems.count() > 0)
       
    65     {
       
    66         iLogoDownloadTimer->start();
       
    67     }
       
    68 }
       
    69 
       
    70 void IRChannelDataProvider::deactivate()
       
    71 {
       
    72     iActivated = false;
       
    73     disconnect(iIsdsClient, SIGNAL(presetLogoDownloaded(IRQPreset*)),
       
    74                this, SLOT(presetLogoDownload(IRQPreset* )));
       
    75     disconnect(iIsdsClient, SIGNAL(presetLogoDownloadError()),
       
    76                this, SLOT(presetLogoDownloadError()));
       
    77     
       
    78     iRequestedItems.clear();
       
    79     iIsdsClient->isdsLogoDownCancelTransaction();
       
    80     iLogoDownloadTimer->stop();
       
    81 }
       
    82 
       
    83 IRQChannelItem* IRChannelDataProvider::getChannelItemByIndex(int aIndex)
       
    84 {
       
    85     if( aIndex < 0 || aIndex >= iChannelList->count() )
       
    86     {
       
    87         return NULL;
       
    88     }
       
    89     
       
    90     return iChannelList->at(aIndex);
       
    91 }
       
    92 
       
    93 void IRChannelDataProvider::doReleaseData(QList<int> aList, bool aSilent)
       
    94 {
       
    95     Q_UNUSED(aSilent);
       
    96     iIsdsClient->isdsLogoDownCancelTransaction();
       
    97     iLogoDownloadTimer->stop();
       
    98     
       
    99     int count = aList.count();
       
   100     int index = 0;
       
   101     for (int idx = 0; idx < count; ++idx)
       
   102     {
       
   103         index = aList[idx];
       
   104         releasePixmap(index);
       
   105         //if the index is saved in iRequestedItems, remove it
       
   106         iRequestedItems.removeOne(index);
       
   107     }
       
   108 }
       
   109 
       
   110 void IRChannelDataProvider::doRequestData(QList<int> aList, bool aSilent)
       
   111 {
       
   112     Q_UNUSED(aSilent);
       
   113     int count = aList.count();
       
   114     int index = 0;
       
   115     for (int idx = 0; idx < count; ++idx)
       
   116     {
       
   117         index = aList[idx];
       
   118         if (index < iChannelList->count())
       
   119         {
       
   120             if (iChannelList->at(index)->imageURL != "")
       
   121             {
       
   122                 if (NULL == iLogos[index])
       
   123                 {
       
   124                     //logo is not downloaded, add to requested items
       
   125                     iRequestedItems.append(index);
       
   126                 }
       
   127                 else
       
   128                 {
       
   129                     //logo is already downloaded, update icon
       
   130                     updateIcon(index, *iLogos[index]);
       
   131                 }
       
   132             }
       
   133         }
       
   134     }
       
   135     
       
   136     if (iActivated && iRequestedItems.count() > 0)
       
   137     {
       
   138         iLogoDownloadTimer->start();
       
   139     }
       
   140 }
       
   141 
       
   142 QVariant IRChannelDataProvider::defaultIcon() const
       
   143 {
       
   144     return iDefaultLogo;
       
   145 }
       
   146 
       
   147 void IRChannelDataProvider::doResetModel()
       
   148 {
       
   149     int count = iChannelList->count();
       
   150     for (int i = 0; i < count; ++i)
       
   151     {
       
   152         QList< QPair< QVariant, int > > item;
       
   153         QVariantList list;
       
   154         list.append(iChannelList->at(i)->channelName);
       
   155         QString tempDes = iChannelList->at(i)->shortDescription;        
       
   156         if( 0 == tempDes.length() )
       
   157         {
       
   158             tempDes = " ";
       
   159         }
       
   160         list.append(tempDes); 
       
   161         item.append( QPair<QVariant, int>(list, Qt::DisplayRole));
       
   162         
       
   163         newItem(&item);
       
   164     }
       
   165 }
       
   166 
       
   167 //data received from ISDS server, add items
       
   168 void IRChannelDataProvider::updateData(QList<IRQChannelItem*> *aPushItemsList)
       
   169 {
       
   170     if (NULL == aPushItemsList)
       
   171     {
       
   172         return;
       
   173     }
       
   174     
       
   175     if (iChannelList != aPushItemsList)
       
   176     {
       
   177         clearAndDestroyItems();
       
   178         clearAndDestroyLogos();
       
   179         
       
   180         iChannelList = aPushItemsList;
       
   181         iRequestedItems.clear();
       
   182         clearCache();
       
   183         resetModel();
       
   184         emit dataAvailable();
       
   185     }
       
   186 }
       
   187 
       
   188 void IRChannelDataProvider::presetLogoDownload(IRQPreset* aPreset)
       
   189 {
       
   190     if( NULL == aPreset )
       
   191     {
       
   192         presetLogoDownloadError();
       
   193         return;
       
   194     } 
       
   195     
       
   196     delete iLogoPreset;            
       
   197     iLogoPreset = aPreset;    
       
   198 
       
   199     if (iLogoPreset->logoData != KNullDesC8)
       
   200     {         
       
   201         QPixmap tempMap;  
       
   202         const unsigned char * logoData = iLogoPreset->logoData.Ptr();     
       
   203         bool ret = tempMap.loadFromData(logoData, iLogoPreset->logoData.Length());
       
   204        
       
   205         if( ret )
       
   206         {            
       
   207             QIcon convertIcon(tempMap);
       
   208             HbIcon *hbIcon = new HbIcon(convertIcon);
       
   209             int index = iRequestedItems[0];
       
   210             iLogos[index] = hbIcon;
       
   211             updateIcon(index, *hbIcon);
       
   212             iRequestedItems.removeAt(0); 
       
   213             if ( iRequestedItems.count() > 0 )
       
   214             {
       
   215                 iLogoDownloadTimer->start();  
       
   216             }
       
   217             return;
       
   218         }     
       
   219     }    
       
   220     
       
   221     presetLogoDownloadError(); 
       
   222 }
       
   223 
       
   224 void IRChannelDataProvider::presetLogoDownloadError()
       
   225 {
       
   226     // if the logo download fails, try to download the next
       
   227     iRequestedItems.removeAt(0);
       
   228     if ( iRequestedItems.count() > 0 )
       
   229     {
       
   230         iLogoDownloadTimer->start();
       
   231     }   
       
   232 }
       
   233 
       
   234 void IRChannelDataProvider::downloadNextLogo()
       
   235 {
       
   236     iLogoDownloadTimer->stop();
       
   237     
       
   238     if (iRequestedItems.count() > 0)
       
   239     {
       
   240         startDownloading(iRequestedItems[0]);   
       
   241     }
       
   242 }
       
   243 
       
   244 void IRChannelDataProvider::startDownloading(int aIndex)
       
   245 {
       
   246     QString url = iChannelList->at(aIndex)->imageURL;
       
   247     IRQPreset tempPreset;
       
   248     tempPreset.imgUrl = url;
       
   249     tempPreset.type = IRQPreset::EIsds;
       
   250     
       
   251     iIsdsClient->isdsLogoDownSendRequest(&tempPreset, 0, KBitmapSize, KBitmapSize); 
       
   252 }
       
   253 
       
   254 void IRChannelDataProvider::clearAndDestroyItems()
       
   255 {
       
   256     if (iChannelList)
       
   257     {
       
   258         for (QList<IRQChannelItem*>::iterator it = iChannelList->begin(); it != iChannelList->end(); ++it)
       
   259         {
       
   260             delete *it;
       
   261         }
       
   262         delete iChannelList;
       
   263         iChannelList = NULL;
       
   264     }
       
   265 }
       
   266 
       
   267 void IRChannelDataProvider::clearAndDestroyLogos()
       
   268 {
       
   269     for (QMap<int, HbIcon*>::iterator it = iLogos.begin(); it != iLogos.end(); ++it)
       
   270     {
       
   271         delete it.value();
       
   272     }
       
   273     
       
   274     iLogos.clear();
       
   275 }