qtinternetradio/ui/src/irsonghistorymodel.cpp
changeset 17 38bbf2dcd608
parent 12 608f67c22514
equal deleted inserted replaced
16:5723da102db1 17:38bbf2dcd608
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 #include <hbglobal.h>
    17 #include <hbglobal.h> 
    18 
       
    19 #include "irqsonghistoryinfo.h"
    18 #include "irqsonghistoryinfo.h"
    20 #include "irqsonghistoryengine.h"
       
    21 #include "irsonghistorymodel.h"
    19 #include "irsonghistorymodel.h"
    22 
    20 #include "songhistorywrapper.h"
    23 IRSongHistoryModel::IRSongHistoryModel(QObject *aParent) : QAbstractListModel(aParent)
    21 #include "irdbviewstruct.h"
    24 {     
    22 
    25     mHistoryEngine = IRQSongHistoryEngine::openInstance();
    23 IRSongHistoryModel::IRSongHistoryModel(QObject *aParent) : QAbstractListModel(aParent),
       
    24                                                            mHistoryWrapper(NULL)
       
    25 {   
       
    26     mHistoryWrapper = new songHistoryWrapper();
    26     getAllList();
    27     getAllList();
    27 }
    28 }
    28 
    29 
    29 IRSongHistoryModel::~IRSongHistoryModel()
    30 IRSongHistoryModel::~IRSongHistoryModel()
    30 {
    31 {
    31     while (!mSongHistoryList.isEmpty())
    32     while (!mSongHistoryList.isEmpty())
    32     {
    33     {
    33         delete mSongHistoryList.takeFirst();
    34         delete mSongHistoryList.takeFirst();
    34     }
    35     }   
    35     
    36     
    36     if (mHistoryEngine)
    37     delete mHistoryWrapper;
    37     {
       
    38         mHistoryEngine->closeInstance();
       
    39         mHistoryEngine = NULL;
       
    40     }
       
    41 }
    38 }
    42 
    39 
    43 int IRSongHistoryModel::rowCount(const QModelIndex &aParent) const
    40 int IRSongHistoryModel::rowCount(const QModelIndex &aParent) const
    44 {
    41 {
    45     Q_UNUSED(aParent);
    42     Q_UNUSED(aParent);
   116 {
   113 {
   117     while (!mSongHistoryList.isEmpty())
   114     while (!mSongHistoryList.isEmpty())
   118     {
   115     {
   119         IRQSongInfo *firstItem = mSongHistoryList.takeFirst();
   116         IRQSongInfo *firstItem = mSongHistoryList.takeFirst();
   120         delete firstItem;
   117         delete firstItem;
   121     }
   118     }    
   122 
   119     mHistoryWrapper->deleteSongHistory();
   123     mHistoryEngine->clearAllSongHistory();
       
   124 
   120 
   125     emit modelChanged();
   121     emit modelChanged();
   126 }
   122 }
   127 
   123 
   128 bool IRSongHistoryModel::checkSongHistoryUpdate()
   124 bool IRSongHistoryModel::checkSongHistoryUpdate()
   129 {     
   125 {   
   130     getAllList();    
   126     getAllList();    
   131     return true;
   127     return true;
   132 }
   128 }
   133  
   129  
   134 
   130 
   135 void IRSongHistoryModel::getAllList()
   131 void IRSongHistoryModel::getAllList()
   136 {
   132 {
   137     mHistoryEngine->getAllSongHistory(mSongHistoryList);
   133     
   138 
   134     QList<QVariant*> *dataSet = mHistoryWrapper->getSongHistory();
       
   135     int dataCount = 0;
       
   136     
       
   137     if( NULL != dataSet )
       
   138     {
       
   139         dataCount = dataSet->count();
       
   140     }
       
   141 
       
   142     while (!mSongHistoryList.isEmpty())
       
   143     {
       
   144         IRQSongInfo *firstItem = mSongHistoryList.takeFirst();
       
   145         delete firstItem;
       
   146     }
       
   147      
       
   148     QString stationsName, songsName, artistsName, status, channelsID;
       
   149 
       
   150     if (0 < dataCount)
       
   151     {
       
   152         for (int i = 0; i < dataCount; ++i)
       
   153         {
       
   154             IRQSongInfo *songInfo = new IRQSongInfo();
       
   155             QVariant *row = dataSet->at(i);
       
   156             stationsName = row[channelNickName].toString();
       
   157             songsName = row[songName].toString();
       
   158             artistsName = row[artistName].toString();
       
   159             status = row[musicStoreStatus].toString();
       
   160             channelsID = row[channelId].toString();
       
   161             songInfo->setAllInfo(stationsName, songsName, artistsName, status, channelsID);
       
   162 
       
   163             delete[] row;
       
   164             mSongHistoryList.append(songInfo);
       
   165         }
       
   166     }
       
   167 
       
   168     delete dataSet;
   139     emit modelChanged();
   169     emit modelChanged();
   140 }
   170 }
   141 
   171 
   142 void IRSongHistoryModel::setOrientation(Qt::Orientation aOrientation)
   172 void IRSongHistoryModel::setOrientation(Qt::Orientation aOrientation)
   143 {
   173 {
   144     mOrientation = aOrientation;
   174     mOrientation = aOrientation;
   145 }
   175 }
   146 
   176 
   147 bool IRSongHistoryModel::deleteOneItem(int aIndex)
   177 bool IRSongHistoryModel::deleteOneItem(int aIndex)
   148 {
   178 {
   149     bool ret = mHistoryEngine->deleteOneSongHistoryItem(aIndex);
   179     if( aIndex < 0 || aIndex >= mSongHistoryList.count() )
   150 
   180     {
       
   181         return false;
       
   182     }
       
   183     
       
   184     IRQSongInfo *currentItem = mSongHistoryList.at(aIndex);   
       
   185      
       
   186     columnMap map;
       
   187     map.insert(songName, currentItem->getSongName());
       
   188     map.insert(artistName, currentItem->getArtistName());
       
   189     map.insert(channelId, currentItem->getChannelID());
       
   190 
       
   191     bool ret = mHistoryWrapper->deleteSongHistory(&map);
       
   192     
   151     if( !ret )
   193     if( !ret )
   152     {
   194     {
   153         return false;                
   195         return false;                
   154     }
   196     }
   155     
   197     
   156     beginRemoveRows(QModelIndex(), aIndex, aIndex);
   198     beginRemoveRows(QModelIndex(), aIndex, aIndex);
       
   199     //delete the current item to avoid memory leaking
       
   200     delete currentItem;
   157     mSongHistoryList.removeAt(aIndex);
   201     mSongHistoryList.removeAt(aIndex);
   158     endRemoveRows(); 
   202     endRemoveRows(); 
   159     
   203     
   160     emit modelChanged();    
   204     emit modelChanged();    
   161     return true;
   205     return true;