qtinternetradio/ui/src/irchannelmodel.cpp
changeset 17 38bbf2dcd608
parent 11 f683e24efca3
equal deleted inserted replaced
16:5723da102db1 17:38bbf2dcd608
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 #include <hbicon.h>
    18 #include <HbIcon>
       
    19 #include <QTimer>
    19 
    20 
    20 #include "irchannelmodel.h"
    21 #include "irchannelmodel.h"
    21 #include "irqisdsdatastructure.h"
    22 #include "irqisdsdatastructure.h"
    22 #include "irsearchresultdb.h"
    23 #include "irsearchresultdb.h"
       
    24 #include "irqisdsclient.h"
       
    25 #include "irlogoprovider.h"
    23 
    26 
    24 IrChannelModel::IrChannelModel(QObject *aParent): QAbstractListModel(aParent)
    27 IrChannelModel::IrChannelModel(QObject *aParent): QAbstractListModel(aParent)
    25                                                 , iChannelList(NULL),iDB(NULL)
    28                                                 , iChannelList(NULL),iDB(NULL)
    26 {
    29 {
    27     iStationLogo = new HbIcon("qtg_large_internet_radio");   
    30     iIsdsClient = IRQIsdsClient::openInstance();
       
    31     iLogoProvider = new IRLogoProvider(iIsdsClient);
       
    32     
       
    33     iTimer = new QTimer;
       
    34     iTimer->setInterval(10);
       
    35     connect(iTimer, SIGNAL(timeout()), this, SLOT(downloadNextLogo()));
       
    36     
       
    37     iStationLogo = new HbIcon("qtg_large_internet_radio");
       
    38 
       
    39     if( NULL == iDB )
       
    40     {
       
    41         iDB = new IRSearchResultDB();
       
    42     }
    28 }
    43 }
    29 
    44 
    30 IrChannelModel::~IrChannelModel()
    45 IrChannelModel::~IrChannelModel()
    31 {
    46 {
    32     clearAndDestroyItems();
    47     clearAndDestroyItems();
    34     delete iStationLogo;
    49     delete iStationLogo;
    35     iStationLogo = NULL;
    50     iStationLogo = NULL;
    36     
    51     
    37     clearAndDestroyLogos();
    52     clearAndDestroyLogos();
    38 
    53 
       
    54     stopDownloadingLogo();
       
    55 
    39     if( iDB )
    56     if( iDB )
    40     {
    57     {
    41         delete iDB;
    58         delete iDB;
    42         iDB = NULL;
    59         iDB = NULL;
    43     }
    60     }
       
    61 
       
    62     delete iLogoProvider;
       
    63     if (iIsdsClient)
       
    64     {
       
    65         iIsdsClient->closeInstance();
       
    66         iIsdsClient = NULL;
       
    67     }
       
    68     delete iTimer;
    44 }
    69 }
    45 
    70 
    46 int IrChannelModel::rowCount(const QModelIndex &aParent) const
    71 int IrChannelModel::rowCount(const QModelIndex &aParent) const
    47 {
    72 {
    48     Q_UNUSED(aParent);
    73     Q_UNUSED(aParent);
    51     if(iChannelList)
    76     if(iChannelList)
    52     {
    77     {
    53         count = iChannelList->count();
    78         count = iChannelList->count();
    54     }
    79     }
    55     return count;
    80     return count;
    56 }
       
    57 
       
    58 QString IrChannelModel::imageUrl(int aRow)
       
    59 {
       
    60     if (iChannelList)
       
    61     {
       
    62         return iChannelList->at(aRow)->imageURL;
       
    63     }
       
    64     else
       
    65     {
       
    66         return "";
       
    67     }
       
    68 }
    81 }
    69 
    82 
    70 void IrChannelModel::setLogo(HbIcon *aIcon, int aIndex)
    83 void IrChannelModel::setLogo(HbIcon *aIcon, int aIndex)
    71 {
    84 {
    72     iLogos[aIndex] = aIcon;
    85     iLogos[aIndex] = aIcon;
   117     {
   130     {
   118         return QVariant();
   131         return QVariant();
   119     }
   132     }
   120 }
   133 }
   121 
   134 
   122 void IrChannelModel::updateData(QList<IRQChannelItem*> *aPushItemsList)
   135 void IrChannelModel::updateData(QList<IRQChannelItem*> *aPushItemsList, bool bInit)
   123 {
   136 {
   124     if (iChannelList != aPushItemsList)
   137     if (iChannelList != aPushItemsList)
   125     {
   138     {
   126         clearAndDestroyItems();
   139         clearAndDestroyItems();
   127         iChannelList = aPushItemsList;
   140         iChannelList = aPushItemsList;
   128     }
   141     }
       
   142     if(false == bInit)
       
   143     {
       
   144         save2Cache();
       
   145     
       
   146     }
   129     
   147     
   130     clearAndDestroyLogos();
   148     clearAndDestroyLogos();
   131     
   149 
       
   150     updateIconIndexArray();
       
   151 
   132     emit dataAvailable();
   152     emit dataAvailable();
   133 }
   153 }
   134 
   154 
   135 void IrChannelModel::initWithCache()
   155 void IrChannelModel::initWithCache()
   136 {
   156 {
   137     if( NULL == iDB )
   157     updateData(iDB->getCahcedChannelList(), true);
   138     {
       
   139         iDB = new IRSearchResultDB();
       
   140     }
       
   141     
       
   142     QList<IRQChannelItem*> *channelList = iDB->getCahcedChannelList();
       
   143    
       
   144     if( NULL == channelList )
       
   145     {
       
   146         //some error happens
       
   147         return;
       
   148     }
       
   149     
       
   150     clearAndDestroyItems();
       
   151     clearAndDestroyLogos();
       
   152     iChannelList = channelList;    
       
   153 }
   158 }
   154 
   159 
   155 void IrChannelModel::save2Cache()
   160 void IrChannelModel::save2Cache()
   156 {
   161 {
   157     if( NULL == iChannelList )
   162     if( NULL == iChannelList )
   176 void IrChannelModel::cleanupDatabase()
   181 void IrChannelModel::cleanupDatabase()
   177 {
   182 {
   178     clearAndDestroyItems();
   183     clearAndDestroyItems();
   179     clearAndDestroyLogos();
   184     clearAndDestroyLogos();
   180     iDB->clearCache();
   185     iDB->clearCache();
   181     
   186     iIconIndexArray.clear();
       
   187 
   182     emit dataAvailable();
   188     emit dataAvailable();
   183 }
   189 }
   184 
   190 
   185 void IrChannelModel::clearAndDestroyLogos()
   191 void IrChannelModel::clearAndDestroyLogos()
   186 {
   192 {
   202         }
   208         }
   203         delete iChannelList;
   209         delete iChannelList;
   204         iChannelList = NULL;
   210         iChannelList = NULL;
   205     }
   211     }
   206 }
   212 }
       
   213 
       
   214 void IrChannelModel::startDownloadingLogo()
       
   215 {
       
   216     iLogoProvider->activate(this, SLOT(logoData(const QByteArray&)));
       
   217     iTimer->start();
       
   218 }
       
   219 
       
   220 void IrChannelModel::stopDownloadingLogo()
       
   221 {
       
   222     iIsdsClient->isdsLogoDownCancelTransaction();
       
   223     iTimer->stop();
       
   224     iLogoProvider->deactivate();
       
   225 }
       
   226 
       
   227 void IrChannelModel::downloadNextLogo()
       
   228 {
       
   229     iTimer->stop();
       
   230     int leftCount = iIconIndexArray.count();
       
   231 
       
   232     if (0 != leftCount)
       
   233     {
       
   234          int row = iIconIndexArray[0];
       
   235          IRQPreset preset;
       
   236          preset.name = iChannelList->at(row)->channelName;
       
   237          preset.shortDesc = iChannelList->at(row)->shortDescription;
       
   238          preset.type = IRQPreset::EIsds;
       
   239          preset.presetId = iChannelList->at(row)->channelID;
       
   240 		 preset.imgUrl = iChannelList->at(row)->imageURL;
       
   241          iLogoProvider->getLogo(&preset);
       
   242     }
       
   243 }
       
   244 
       
   245 void IrChannelModel::logoData(const QByteArray &aLogoData)
       
   246 {
       
   247     if (aLogoData.size() > 0)
       
   248     {
       
   249         QPixmap tempMap;
       
   250         bool ret = tempMap.loadFromData((const unsigned char*)aLogoData.constData(), aLogoData.size());
       
   251         if( ret )
       
   252         {
       
   253             QIcon convertIcon(tempMap);
       
   254             HbIcon *hbIcon = new HbIcon(convertIcon);
       
   255             int index = iIconIndexArray[0];
       
   256             setLogo(hbIcon, index);  
       
   257         }
       
   258     }
       
   259     
       
   260     iIconIndexArray.removeAt(0);
       
   261     int leftCount = iIconIndexArray.count();
       
   262     if( leftCount > 0 )
       
   263     {
       
   264         iTimer->start();  
       
   265     }
       
   266 }
       
   267 
       
   268 void IrChannelModel::updateIconIndexArray()
       
   269 {
       
   270     iIconIndexArray.clear();
       
   271     
       
   272     for (int i = 0; i < rowCount(); ++i)
       
   273     {
       
   274         if (iChannelList->at(i)->imageURL != "" && !iLogos.contains(i))
       
   275         {
       
   276             iIconIndexArray.append(i);
       
   277         }
       
   278     }
       
   279 
       
   280     if (iIconIndexArray.size() > 0)
       
   281     {
       
   282         startDownloadingLogo();
       
   283     }
       
   284 }