qtinternetradio/ui/src/irfavoritesmodel.cpp
changeset 5 0930554dc389
parent 3 ee64f059b8e1
child 8 3b03c28289e6
--- a/qtinternetradio/ui/src/irfavoritesmodel.cpp	Fri May 14 15:43:29 2010 +0300
+++ b/qtinternetradio/ui/src/irfavoritesmodel.cpp	Thu May 27 12:46:34 2010 +0300
@@ -14,6 +14,7 @@
 * Description:
 *
 */
+#include <QtAlgorithms>
 #include <hbicon.h>
 
 #include "irqfavoritesdb.h"
@@ -82,6 +83,20 @@
     emit dataChanged(index(aIndex), index(aIndex));
 }
 
+bool IRFavoritesModel::isLogoReady(int aIndex) const
+{
+    int logoListCount = iLogos.count();
+    if (aIndex >= 0 
+        && aIndex < logoListCount)
+    {
+        return iLogos[aIndex] != NULL;
+    }
+    else
+    {
+        return false;
+    }
+}
+
 QVariant IRFavoritesModel::data(const QModelIndex &aIndex, int aRole) const
 {
     if (!aIndex.isValid())
@@ -166,14 +181,29 @@
     }
     
     int presetSize = iPresetsList->count();
+
+    if(!presetSize)
+    {
+    	return;
+    }
+
     int uniqId = 0;
-         
-    for (int i = 0; i < presetSize; ++i)
+    
+    while(presetSize--)
     {
-        uniqId = iFavoritesDb->getUniqId(i);
+        uniqId = iFavoritesDb->getUniqId(presetSize);
+        
+        //There is the probability that the return value<0, so I added this judgment.
+        if(uniqId < 0 )
+        {
+        	//if here, the Id, which is mapped to preset's item, can't be found. 
+        	//jump out from while 
+        	break; 
+        }
         iFavoritesDb->deletePreset(uniqId);
-    }    
- 
+    	
+    }
+
     clearPresetList();
     clearAndDestroyLogos();
     emit modelChanged();
@@ -209,6 +239,7 @@
     }
     
     beginRemoveRows(QModelIndex(), aIndex, aIndex);
+    delete preset;
     iPresetsList->removeAt(aIndex);
     
     if (aIndex<iLogos.size())
@@ -218,7 +249,59 @@
     }
     iLogos.removeAt(aIndex);
     endRemoveRows();
-    emit modelChanged();
     return true;    
 }
 
+bool IRFavoritesModel::deleteMultiFavorites(const QModelIndexList &aIndexList)
+{
+    if (aIndexList.empty())
+    {
+        return true;
+    }
+
+    int index = 0;
+    bool retVal = true;
+    QList<int> indexToBeDelete;
+    
+    // delete from DB
+    for (int i = 0; i < aIndexList.count(); i++)
+    {
+        index = aIndexList.at(i).row();
+        
+        if (index < 0 || index >= iPresetsList->size())
+        {
+            continue;
+        }
+        
+        if (0 != iFavoritesDb->deletePreset(iPresetsList->at(index)->uniqID))
+        {
+            retVal = false;
+            continue;
+        }
+        indexToBeDelete.append(index);
+    }
+    
+    qSort(indexToBeDelete);
+    
+    
+    // delete from model
+    for (int i = indexToBeDelete.count() - 1; i >= 0; i--)
+    { 
+        index = indexToBeDelete.at(i);
+        
+        beginRemoveRows(QModelIndex(), index, index);
+        IRQPreset *preset = iPresetsList->at(index);
+        delete preset;
+        iPresetsList->removeAt(index);
+        if (index<iLogos.size())
+        {
+            delete iLogos[index];
+            iLogos[index] = NULL;
+        }
+        iLogos.removeAt(index);
+        endRemoveRows();         
+    }
+
+    return retVal;    
+}
+