qtinternetradio/ui/src/irhistorymodel.cpp
changeset 17 38bbf2dcd608
parent 3 ee64f059b8e1
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 
    19 #include <QTimer>
    20 #include "irqsonghistoryinfo.h"
    20 
    21 #include "irqsonghistoryengine.h"
    21 #include "irqisdsdatastructure.h"
       
    22 #include "channelhistorywrapper.h"
       
    23 #include "urlinfowrapper.h"
    22 #include "irhistorymodel.h"
    24 #include "irhistorymodel.h"
    23 
    25 #include "irqisdsclient.h"
    24 IRHistoryModel::IRHistoryModel(QObject *aParent) : QAbstractListModel(aParent)
    26 #include "irlogoprovider.h"
       
    27 
       
    28 IRHistoryModel::IRHistoryModel(QObject *aParent) : QAbstractListModel(aParent),
       
    29     iHistoryEngine(NULL), iUrlInfoWrapper(NULL), iStationLogo(NULL),
       
    30     iIsdsClient(NULL), iLogoProvider(NULL), iTimer(NULL)
    25 {
    31 {
    26     iStationLogo = new HbIcon("qtg_large_internet_radio");
    32     iStationLogo = new HbIcon("qtg_large_internet_radio");
    27 
    33 
    28     iHistoryEngine = IRQSongHistoryEngine::openInstance();
    34     iHistoryEngine = new channelHistoryWrapper();
    29     getAllList();
    35     
       
    36     iUrlInfoWrapper = new urlInfoWrapper();
       
    37     
       
    38     iIsdsClient = IRQIsdsClient::openInstance();
       
    39     iLogoProvider = new IRLogoProvider(iIsdsClient);
       
    40     
       
    41     iTimer = new QTimer();
       
    42     iTimer->setInterval(10);
       
    43     connect(iTimer, SIGNAL(timeout()), this, SLOT(downloadNextLogo()));
       
    44         
       
    45     refreshModel();
    30 }
    46 }
    31 
    47 
    32 IRHistoryModel::~IRHistoryModel()
    48 IRHistoryModel::~IRHistoryModel()
    33 {
    49 {
       
    50     clearModel();
       
    51 	
    34     delete iStationLogo;
    52     delete iStationLogo;
    35     iStationLogo = NULL;
    53     iStationLogo = NULL;
    36 
    54     
    37     clearAndDestroyLogos();
    55     delete iHistoryEngine;
    38 
    56     iHistoryEngine = NULL;
    39     while (!iHistoryList.isEmpty())
    57     
    40     {
    58     delete iUrlInfoWrapper;
    41         delete iHistoryList.takeFirst();
    59     iUrlInfoWrapper = NULL;
    42     }
    60     
    43     
    61 	stopDownloadingLogo();
    44     if (iHistoryEngine)
    62 	
    45     {
    63     delete iLogoProvider;
    46         iHistoryEngine->closeInstance();
    64     if (iIsdsClient)
    47         iHistoryEngine = NULL;
    65     {
    48     }
    66         iIsdsClient->closeInstance();
       
    67         iIsdsClient = NULL;
       
    68     }
       
    69     delete iTimer;    
    49 }
    70 }
    50 
    71 
    51 int IRHistoryModel::rowCount(const QModelIndex &aParent) const
    72 int IRHistoryModel::rowCount(const QModelIndex &aParent) const
    52 {
    73 {
    53     Q_UNUSED(aParent);
    74     Q_UNUSED(aParent);
    54     return iHistoryList.count();
    75     return iHistoryList.count();
    55 }
    76 }
    56 
    77 
    57 QString IRHistoryModel::getImageUrl(int aRow) const
    78 QString IRHistoryModel::getImageUrl(int aRow) const
    58 {
    79 {
    59     return iHistoryList.at(aRow)->getImageUrl();
    80     return iHistoryList.at(aRow)->imgUrl;
    60 }
    81 }
    61 
    82 
    62 void IRHistoryModel::setLogo(HbIcon *aIcon, int aIndex)
    83 void IRHistoryModel::setLogo(HbIcon *aIcon, int aIndex)
    63 {
    84 {
       
    85     int elementCountNeedToAdd = aIndex + 1 - iLogos.size();
       
    86     while (elementCountNeedToAdd > 0)
       
    87     {
       
    88         iLogos.append(NULL);
       
    89         elementCountNeedToAdd--;
       
    90     }
    64     iLogos[aIndex] = aIcon;
    91     iLogos[aIndex] = aIcon;
    65     emit dataChanged(index(aIndex), index(aIndex));
    92     emit dataChanged(index(aIndex), index(aIndex));
    66 }
    93 }
    67 
    94 
    68 QVariant IRHistoryModel::data(const QModelIndex &aIndex, int aRole) const
    95 QVariant IRHistoryModel::data(const QModelIndex &aIndex, int aRole) const
    80     if (aRole == Qt::DisplayRole)
   107     if (aRole == Qt::DisplayRole)
    81     {
   108     {
    82         QVariantList list;
   109         QVariantList list;
    83         
   110         
    84         int row = aIndex.row();
   111         int row = aIndex.row();
    85         QString primaryText = iHistoryList.at(row)->getChannelName();
   112         QString primaryText = iHistoryList.at(row)->nickName;
    86         list.append(primaryText);
   113         list.append(primaryText);
    87         // fix bug #9888,if left descriptions as blank, only one line appears
   114         // fix bug #9888,if left descriptions as blank, only one line appears
    88         QString tempDes = iHistoryList.at(row)->getChannelDesc();
   115         QString tempDes = iHistoryList.at(row)->shortDesc;
    89         if (0 == tempDes.length())
   116         if (0 == tempDes.length())
    90         {
   117         {
    91             tempDes = " ";
   118             tempDes = " ";
    92         }
   119         }
    93         list.append(tempDes);
   120         list.append(tempDes);
    96     }
   123     }
    97     else if (aRole == Qt::DecorationRole)
   124     else if (aRole == Qt::DecorationRole)
    98     {
   125     {
    99         QVariantList list;
   126         QVariantList list;
   100         int row = aIndex.row();
   127         int row = aIndex.row();
   101         const HbIcon *icon = iLogos.value(row);
   128         if(row < iLogos.size())
   102         if (icon)
   129         {
   103         {
   130             const HbIcon *icon = iLogos[row];
   104             list.append(*icon);
   131             if (icon)
       
   132             {
       
   133                 list.append(*icon);
       
   134             }
       
   135             else
       
   136             {
       
   137                 list.append(*iStationLogo);
       
   138             }            
   105         }
   139         }
   106         else
   140         else
   107         {
   141         {
   108             list.append(*iStationLogo);
   142             list.append(*iStationLogo);
   109         }
   143         }       
   110         
       
   111         return list;
   144         return list;
   112     }
   145     }
   113 
   146 
   114     return QVariant();
   147     return QVariant();
   115 }
   148 }
   116 
   149 
   117 IRQSongHistoryInfo* IRHistoryModel::getHistoryInfo(int aIndex)
   150 IRQPreset* IRHistoryModel::getHistoryInfo(int aIndex)
   118 {
   151 {
   119     if (aIndex >= 0 && aIndex < iHistoryList.count())
   152     if (aIndex >= 0 && aIndex < iHistoryList.count())
   120     {
   153     {
   121         return iHistoryList.at(aIndex);
   154         return iHistoryList.at(aIndex);
   122     }
   155     }
   123 
   156 
   124     return NULL;
   157     return NULL;
   125 }
   158 }
   126 
   159 
   127 void IRHistoryModel::clearAllList()
   160 void IRHistoryModel::clearAllHistory()
       
   161 {
       
   162     if (iHistoryEngine->deleteChannelHistory())
       
   163     {
       
   164         clearModel();
       
   165         emit modelChanged();
       
   166     }
       
   167 }
       
   168 
       
   169 bool IRHistoryModel::checkHistoryUpdate()
       
   170 {
       
   171     refreshModel();    
       
   172     return true;
       
   173 }
       
   174 
       
   175 void IRHistoryModel::clearAndDestroyLogos()
       
   176 {
       
   177     int size = iLogos.size(); 
       
   178     for (int i = 0; i < size; i ++)
       
   179     {
       
   180         delete iLogos[i];
       
   181         iLogos[i] = NULL;
       
   182     }
       
   183     iLogos.clear();
       
   184 }
       
   185 
       
   186 void IRHistoryModel::refreshModel()
       
   187 {
       
   188     clearModel();
       
   189     
       
   190     QList<QVariant*>* historySet = NULL;
       
   191     historySet = iHistoryEngine->getChannelHistory();
       
   192     
       
   193     if (NULL == historySet)
       
   194     {
       
   195         return;
       
   196     }
       
   197     int dataSize = historySet->size();
       
   198     for (int i = 0; i < dataSize; i++)
       
   199     {
       
   200         IRQPreset* preset = new IRQPreset();
       
   201         preset->presetId = (*(historySet->at(i) + channelId)).toUInt();  
       
   202         preset->name = (*(historySet->at(i) + channelName)).toString();
       
   203         preset->nickName = (*(historySet->at(i) + channelNickName)).toString();
       
   204         
       
   205         preset->genreName = (*(historySet->at(i) + genreName)).toString();
       
   206         preset->genreId = (*(historySet->at(i) + genreId)).toString();
       
   207         preset->languageName = (*(historySet->at(i) + languageName)).toString();
       
   208         preset->languageCode = (*(historySet->at(i) + languageCode)).toString(); 
       
   209         
       
   210         preset->countryName = (*(historySet->at(i) + countryName)).toString();
       
   211         preset->countryCode = (*(historySet->at(i) + countryCode)).toString();
       
   212         preset->description = (*(historySet->at(i) + description)).toString();
       
   213         preset->shortDesc = (*(historySet->at(i) + shortDesc)).toString();
       
   214         
       
   215         preset->lastModified = (*(historySet->at(i) + lastModified)).toString();
       
   216         preset->type = (*(historySet->at(i) + channelType)).toInt();
       
   217         preset->musicStoreStatus = (*(historySet->at(i) + musicStoreStatus)).toString(); 
       
   218         
       
   219         preset->imgUrl = (*(historySet->at(i) + imgUrl)).toString();
       
   220         
       
   221         preset->advertisementUrl = (*(historySet->at(i) + advertisementUrl)).toString();
       
   222         preset->advertisementInUse = (*(historySet->at(i) + advertisementInUse)).toString();
       
   223        
       
   224         columnMap selectCriteria;
       
   225         selectCriteria.insert(channelId, QString::number(preset->presetId));
       
   226         
       
   227         QList<QVariant*>* urlInfoList = iUrlInfoWrapper->getUrlInfo(&selectCriteria, NULL, i, dataSize-1);        
       
   228         if (NULL == urlInfoList)
       
   229         {
       
   230             delete preset;
       
   231             preset = NULL;
       
   232             continue;
       
   233         }
       
   234         int urlSize = urlInfoList->size();
       
   235         for (int j = 0; j < urlSize; j++)
       
   236         {
       
   237             IRQChannelServerURL urlInfo;
       
   238             urlInfo.serverName = preset->name;
       
   239             urlInfo.url = (*(urlInfoList->at(j) + channelUrl_URL)).toString();
       
   240             urlInfo.bitrate = (*(urlInfoList->at(j) + bitRate_URL)).toInt();
       
   241             preset->insertChannelServer(urlInfo);
       
   242         }        
       
   243         
       
   244         while(false == urlInfoList->isEmpty())
       
   245         {
       
   246             delete []urlInfoList->takeFirst();
       
   247         }
       
   248         urlInfoList->clear();
       
   249         
       
   250         delete urlInfoList;
       
   251         urlInfoList = NULL;
       
   252         
       
   253         iHistoryList.append(preset);
       
   254     }
       
   255     
       
   256     while(false == historySet->isEmpty())
       
   257     {
       
   258         delete []historySet->takeFirst();
       
   259     }
       
   260     historySet->clear();
       
   261     
       
   262     delete historySet;
       
   263     historySet = NULL;
       
   264     
       
   265     updateIconIndexArray();
       
   266     
       
   267     emit modelChanged();
       
   268 }
       
   269 
       
   270 void IRHistoryModel::clearModel()
   128 {
   271 {
   129     while (!iHistoryList.isEmpty())
   272     while (!iHistoryList.isEmpty())
   130     {
   273     {
   131         IRQSongHistoryInfo *firstItem = iHistoryList.takeFirst();
   274         IRQPreset *firstItem = iHistoryList.takeFirst();
   132         delete firstItem;
   275         delete firstItem;
   133     }
   276     }
   134     iHistoryEngine->clearAllHistory();
   277 	
   135     clearAndDestroyLogos();
   278     clearAndDestroyLogos();	
   136     emit modelChanged();
   279 	updateIconIndexArray();    
   137 }
   280 }    
   138 
   281 
   139 bool IRHistoryModel::checkHistoryUpdate()
   282 bool IRHistoryModel::deleteHistory(int aIndex)
   140 {
   283 {
   141     getAllList();    
   284     if (aIndex < 0 && aIndex >= iHistoryList.count())
       
   285     {
       
   286         return false;
       
   287     }
       
   288         
       
   289     if (!iIconIndexArray.empty())
       
   290     {
       
   291         iIsdsClient->isdsLogoDownCancelTransaction();
       
   292         iTimer->stop();
       
   293     }
       
   294             
       
   295     columnMap selectCriteria;
       
   296     selectCriteria.insert(channelId, QString::number(iHistoryList.at(aIndex)->presetId));
       
   297          
       
   298     if (!iHistoryEngine->deleteChannelHistory(&selectCriteria))
       
   299     {
       
   300         return false;                
       
   301     }
       
   302     
       
   303     beginRemoveRows(QModelIndex(), aIndex, aIndex);
       
   304     
       
   305     delete iHistoryList.at(aIndex);
       
   306     iHistoryList.removeAt(aIndex);
       
   307     
       
   308     if (aIndex < iLogos.size())
       
   309     {
       
   310         delete iLogos[aIndex];
       
   311         iLogos.removeAt(aIndex);
       
   312     }
       
   313 	
       
   314     updateIconIndexArray();
       
   315     
       
   316     endRemoveRows(); 
       
   317     
       
   318     emit modelChanged();  
       
   319     
       
   320     if (!iIconIndexArray.empty())
       
   321     {
       
   322         iTimer->start();
       
   323     }
       
   324           
   142     return true;
   325     return true;
   143 }
   326 }
   144 
   327 
   145 void IRHistoryModel::clearAndDestroyLogos()
   328 void IRHistoryModel::startDownloadingLogo()
   146 {
   329 {
   147     for (QMap<int, HbIcon*>::iterator it = iLogos.begin(); it != iLogos.end(); ++it)
   330     iLogoProvider->activate(this, SLOT(logoData(const QByteArray&)));
   148     {
   331     iTimer->start();
   149         delete it.value();
   332 }
   150     }
   333 
   151     
   334 void IRHistoryModel::stopDownloadingLogo()
   152     iLogos.clear();
   335 {
   153 }
   336     iIsdsClient->isdsLogoDownCancelTransaction();
   154 
   337     iTimer->stop();
   155 void IRHistoryModel::getAllList()
   338     iIconIndexArray.clear();
   156 {
   339     iLogoProvider->deactivate();
   157     iHistoryEngine->getAllHistory(iHistoryList);
   340 }
   158 
   341 
   159     emit modelChanged();
   342 void IRHistoryModel::downloadNextLogo()
   160 }
   343 {
   161 
   344     iTimer->stop();
   162 bool IRHistoryModel::deleteOneItem(int aIndex)
   345     int leftCount = iIconIndexArray.count();
   163 {
   346 
   164     bool ret = iHistoryEngine->deleteOneItem(aIndex);
   347     if (0 != leftCount)
   165      
   348     {
   166     if( !ret )
   349          iLogoProvider->getLogo(iHistoryList.at(iIconIndexArray[0]));
   167     {
   350     }
   168         return false;                
   351 }
   169     }
   352 
   170     
   353 void IRHistoryModel::logoData(const QByteArray &aLogoData)
   171     beginRemoveRows(QModelIndex(), aIndex, aIndex);
   354 {
   172     iHistoryList.removeAt(aIndex);
   355     if (aLogoData.size() > 0)
   173     endRemoveRows(); 
   356     {
   174     
   357         QPixmap tempMap;
   175     emit modelChanged();    
   358         bool ret = tempMap.loadFromData((const unsigned char*)aLogoData.constData(), aLogoData.size());
   176     return true;
   359         if( ret )
   177 }
   360         {
       
   361             QIcon convertIcon(tempMap);
       
   362             HbIcon *hbIcon = new HbIcon(convertIcon);
       
   363             int index = iIconIndexArray[0];
       
   364             setLogo(hbIcon, index);  
       
   365         }
       
   366     }
       
   367     
       
   368     iIconIndexArray.removeAt(0);
       
   369     int leftCount = iIconIndexArray.count();
       
   370     if( leftCount > 0 )
       
   371     {
       
   372         iTimer->start();  
       
   373     }
       
   374 }
       
   375 
       
   376 bool IRHistoryModel::isLogoReady(int aIndex) const
       
   377 {
       
   378     int logoListCount = iLogos.count();
       
   379     if (aIndex >= 0 
       
   380         && aIndex < logoListCount)
       
   381     {
       
   382         return iLogos[aIndex] != NULL;
       
   383     }
       
   384     else
       
   385     {
       
   386         return false;
       
   387     }
       
   388 }
       
   389 
       
   390 void IRHistoryModel::updateIconIndexArray()
       
   391 {
       
   392     iIconIndexArray.clear();
       
   393     
       
   394     for (int i = 0; i < rowCount(); ++i)
       
   395     {
       
   396         if (getImageUrl(i) != "" && !isLogoReady(i))
       
   397         {
       
   398             iIconIndexArray.append(i);
       
   399         }
       
   400     } 
       
   401 }
       
   402