qtinternetradio/ui/src/irfavoritesmodel.cpp
changeset 17 38bbf2dcd608
parent 8 3b03c28289e6
equal deleted inserted replaced
16:5723da102db1 17:38bbf2dcd608
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 #include <QtAlgorithms>
    17 #include <QtAlgorithms>
    18 #include <hbicon.h>
    18 #include <HbIcon>
       
    19 #include <QTimer>
    19 
    20 
    20 #include "irqfavoritesdb.h"
    21 #include "irqfavoritesdb.h"
    21 #include "irqisdsdatastructure.h"
    22 #include "irqisdsdatastructure.h"
    22 #include "irfavoritesmodel.h"
    23 #include "irfavoritesmodel.h"
       
    24 #include "irqisdsclient.h"
       
    25 #include "irlogoprovider.h"
    23 
    26 
    24 IRFavoritesModel::IRFavoritesModel(IRQFavoritesDB *aFavoritesDb, QObject *aParent) 
    27 IRFavoritesModel::IRFavoritesModel(IRQFavoritesDB *aFavoritesDb, QObject *aParent) 
    25                                   : QAbstractListModel(aParent), iFavoritesDb(aFavoritesDb),
    28                                   : QAbstractListModel(aParent), iFavoritesDb(aFavoritesDb),
    26                                     iPresetsList(NULL)
    29                                     iPresetsList(NULL)
    27 {
    30 {
    28     if (aFavoritesDb)
    31     if (aFavoritesDb)
    29     {
    32     {
    30         iPresetsList = aFavoritesDb->getPresets();
    33         iPresetsList = aFavoritesDb->getPresets();
    31     }
    34     }
    32     
    35     
       
    36     iIsdsClient = IRQIsdsClient::openInstance();
       
    37     iLogoProvider = new IRLogoProvider(iIsdsClient);
       
    38     
       
    39     iTimer = new QTimer;
       
    40     iTimer->setInterval(10);
       
    41     connect(iTimer, SIGNAL(timeout()), this, SLOT(downloadNextLogo()));
       
    42     
    33     iStationLogo = new HbIcon("qtg_large_internet_radio");
    43     iStationLogo = new HbIcon("qtg_large_internet_radio");
    34 }
    44 }
    35 
    45 
    36 IRFavoritesModel::~IRFavoritesModel()
    46 IRFavoritesModel::~IRFavoritesModel()
    37 {
    47 {
    38     delete iStationLogo;
    48     delete iStationLogo;
    39     clearPresetList();
    49     clearPresetList();
    40     clearAndDestroyLogos();
    50     clearAndDestroyLogos();
       
    51     
       
    52     delete iLogoProvider;
       
    53     if (iIsdsClient)
       
    54     {
       
    55         iIsdsClient->closeInstance();
       
    56         iIsdsClient = NULL;
       
    57     }
       
    58     delete iTimer;
    41 }
    59 }
    42 
    60 
    43 IRQPreset* IRFavoritesModel::getPreset(int aIndex) const
    61 IRQPreset* IRFavoritesModel::getPreset(int aIndex) const
    44 {
    62 {
    45     if (iPresetsList && aIndex >= 0 && aIndex < iPresetsList->count())
    63     if (iPresetsList && aIndex >= 0 && aIndex < iPresetsList->count())
   112     if (aRole == Qt::DisplayRole)
   130     if (aRole == Qt::DisplayRole)
   113     {
   131     {
   114         QVariantList list;
   132         QVariantList list;
   115         int row = aIndex.row();
   133         int row = aIndex.row();
   116 
   134 
   117         QString primaryText = iPresetsList->at(row)->name;
   135         QString primaryText = iPresetsList->at(row)->nickName;
   118         list.append(primaryText);
   136         list.append(primaryText);
   119         QString secondaryText = iPresetsList->at(row)->shortDesc;
   137         QString secondaryText = iPresetsList->at(row)->shortDesc;
   120         
   138         
   121         // fix bug #9888,if left descriptions as blank,
   139         // fix bug #9888,if left descriptions as blank,
   122         // only one line displays in favorites view
   140         // only one line displays in favorites view
   156 bool IRFavoritesModel::checkFavoritesUpdate()
   174 bool IRFavoritesModel::checkFavoritesUpdate()
   157 {
   175 {
   158     clearPresetList();
   176     clearPresetList();
   159     clearAndDestroyLogos();
   177     clearAndDestroyLogos();
   160     iPresetsList = iFavoritesDb->getPresets();
   178     iPresetsList = iFavoritesDb->getPresets();
       
   179     updateIconIndexArray();
   161     emit modelChanged();
   180     emit modelChanged();
   162     return true;
   181     return true;
   163 }
   182 }
   164 
   183 
   165 void IRFavoritesModel::clearAndDestroyLogos()
   184 void IRFavoritesModel::clearAndDestroyLogos()
   171         iLogos[i] = NULL;
   190         iLogos[i] = NULL;
   172     }
   191     }
   173     iLogos.clear();
   192     iLogos.clear();
   174 }
   193 }
   175 
   194 
   176 void IRFavoritesModel::clearFavoriteDB()
       
   177 {
       
   178     if (NULL == iPresetsList)
       
   179     {
       
   180         return;
       
   181     }
       
   182     
       
   183     int presetSize = iPresetsList->count();
       
   184 
       
   185     if(!presetSize)
       
   186     {
       
   187     	return;
       
   188     }
       
   189 
       
   190     int uniqId = 0;
       
   191     
       
   192     while(presetSize--)
       
   193     {
       
   194         uniqId = iFavoritesDb->getUniqId(presetSize);
       
   195         
       
   196         //There is the probability that the return value<0, so I added this judgment.
       
   197         if(uniqId < 0 )
       
   198         {
       
   199         	//if here, the Id, which is mapped to preset's item, can't be found. 
       
   200         	//jump out from while 
       
   201         	break; 
       
   202         }
       
   203         iFavoritesDb->deletePreset(uniqId);
       
   204     	
       
   205     }
       
   206 
       
   207     clearPresetList();
       
   208     clearAndDestroyLogos();
       
   209     emit modelChanged();
       
   210 }
       
   211 
       
   212 void IRFavoritesModel::clearPresetList()
   195 void IRFavoritesModel::clearPresetList()
   213 {
   196 {
   214     if (NULL == iPresetsList)
   197     if (NULL == iPresetsList)
   215     {
   198     {
   216         return;
   199         return;
   229     if ( aIndex < 0 || aIndex >= iPresetsList->size())
   212     if ( aIndex < 0 || aIndex >= iPresetsList->size())
   230     {
   213     {
   231         return false;
   214         return false;
   232     }
   215     }
   233     
   216     
       
   217     if (!iIconIndexArray.empty())
       
   218     {
       
   219         iIsdsClient->isdsLogoDownCancelTransaction();
       
   220         iTimer->stop();
       
   221     }  
       
   222     
   234     IRQPreset *preset = iPresetsList->at(aIndex);        
   223     IRQPreset *preset = iPresetsList->at(aIndex);        
   235     int ret = iFavoritesDb->deletePreset(preset->uniqID);
   224     int ret = iFavoritesDb->deletePreset(*preset);
   236     if ( 0 != ret )
   225     if ( 0 != ret )
   237     {
   226     {
   238         return false;
   227         return false;
   239     }
   228     }
   240     
   229     
   247         delete iLogos[aIndex];
   236         delete iLogos[aIndex];
   248         iLogos[aIndex] = NULL;
   237         iLogos[aIndex] = NULL;
   249     }
   238     }
   250     iLogos.removeAt(aIndex);
   239     iLogos.removeAt(aIndex);
   251     endRemoveRows();
   240     endRemoveRows();
       
   241     
       
   242     updateIconIndexArray();
       
   243     
       
   244     if (!iIconIndexArray.empty())
       
   245     {
       
   246         iTimer->start();
       
   247     }   
       
   248     
   252     return true;    
   249     return true;    
   253 }
   250 }
   254 
   251 
   255 bool IRFavoritesModel::deleteMultiFavorites(const QModelIndexList &aIndexList)
   252 bool IRFavoritesModel::deleteMultiFavorites(const QModelIndexList &aIndexList)
   256 {
   253 {
   257     if (aIndexList.empty())
   254     if (aIndexList.empty())
   258     {
   255     {
   259         return true;
   256         return true;
   260     }
   257     }
   261 
   258 
       
   259     if (!iIconIndexArray.empty())
       
   260     {
       
   261         iIsdsClient->isdsLogoDownCancelTransaction();
       
   262         iTimer->stop();
       
   263     }
       
   264     
   262     int index = 0;
   265     int index = 0;
   263     bool retVal = true;
   266     bool retVal = true;
   264     QList<int> indexToBeDelete;
   267     QList<int> indexToBeDelete;
   265     
   268     
   266     // delete from DB
   269     // delete from DB
   271         if (index < 0 || index >= iPresetsList->size())
   274         if (index < 0 || index >= iPresetsList->size())
   272         {
   275         {
   273             continue;
   276             continue;
   274         }
   277         }
   275         
   278         
   276         if (0 != iFavoritesDb->deletePreset(iPresetsList->at(index)->uniqID))
   279         IRQPreset *preset = iPresetsList->at(index);
       
   280         if (0 != iFavoritesDb->deletePreset(*preset) )
   277         {
   281         {
   278             retVal = false;
   282             retVal = false;
   279             continue;
   283             continue;
   280         }
   284         }
   281         indexToBeDelete.append(index);
   285         indexToBeDelete.append(index);
   300         }
   304         }
   301         iLogos.removeAt(index);
   305         iLogos.removeAt(index);
   302         endRemoveRows();         
   306         endRemoveRows();         
   303     }
   307     }
   304 
   308 
       
   309     updateIconIndexArray();
       
   310     
       
   311     if (!iIconIndexArray.empty())
       
   312     {
       
   313         iTimer->start();
       
   314     }
       
   315     
   305     return retVal;    
   316     return retVal;    
   306 }
   317 }
   307 
   318 
   308 void IRFavoritesModel::updateFavoriteName(int aIndex, const QString &aNewName)
   319 void IRFavoritesModel::updateFavoriteName(int aIndex, const QString &aNewName)
   309 {
   320 {
   310     if (aIndex >= 0 && aIndex < iPresetsList->count())
   321     if (aIndex >= 0 && aIndex < iPresetsList->count())
   311     {
   322     {
   312         iPresetsList->at(aIndex)->name = aNewName;
   323         iPresetsList->at(aIndex)->nickName = aNewName;
   313         emit dataChanged(index(aIndex), index(aIndex));
   324         emit dataChanged(index(aIndex), index(aIndex));
   314     }
   325     }
   315 }
   326 }
       
   327 
       
   328 void IRFavoritesModel::startDownloadingLogo()
       
   329 {
       
   330     iLogoProvider->activate(this, SLOT(logoData(const QByteArray&)));
       
   331     iTimer->start();
       
   332 }
       
   333 
       
   334 void IRFavoritesModel::stopDownloadingLogo()
       
   335 {
       
   336     iIsdsClient->isdsLogoDownCancelTransaction();
       
   337     iTimer->stop();
       
   338     iIconIndexArray.clear();
       
   339     iLogoProvider->deactivate();
       
   340 }
       
   341 
       
   342 void IRFavoritesModel::downloadNextLogo()
       
   343 {
       
   344     iTimer->stop();
       
   345     int leftCount = iIconIndexArray.count();
       
   346 
       
   347     if (0 != leftCount)
       
   348     {
       
   349          iLogoProvider->getLogo(iPresetsList->at(iIconIndexArray[0]));
       
   350     }
       
   351 }
       
   352 
       
   353 void IRFavoritesModel::logoData(const QByteArray &aLogoData)
       
   354 {
       
   355     if (aLogoData.size() > 0)
       
   356     {
       
   357         QPixmap tempMap;
       
   358         bool ret = tempMap.loadFromData((const unsigned char*)aLogoData.constData(), aLogoData.size());
       
   359         if( ret )
       
   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 void IRFavoritesModel::updateIconIndexArray()
       
   377 {
       
   378     iIconIndexArray.clear();
       
   379     
       
   380     for (int i = 0; i < rowCount(); ++i)
       
   381     {
       
   382         if (getImgUrl(i) != "" && !isLogoReady(i))
       
   383         {
       
   384             iIconIndexArray.append(i);
       
   385         }
       
   386     } 
       
   387 }