qtinternetradio/ui/src/irhistorymodel.cpp
changeset 0 09774dfdd46b
child 3 ee64f059b8e1
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <hbicon.h>
       
    19 #include <QBrush>
       
    20 
       
    21 #include "irqsonghistoryinfo.h"
       
    22 #include "irqsonghistoryengine.h"
       
    23 #include "irhistorymodel.h"
       
    24 #include "iruidefines.h"
       
    25 
       
    26 IRHistoryModel::IRHistoryModel(QObject *aParent) : QAbstractListModel(aParent)
       
    27 {
       
    28     iStationLogo = new HbIcon(":/stationlist/icon_stationdefault.png");
       
    29 
       
    30     iHistoryEngine = IRQSongHistoryEngine::openInstance();
       
    31     getAllList();
       
    32 }
       
    33 
       
    34 IRHistoryModel::~IRHistoryModel()
       
    35 {
       
    36     delete iStationLogo;
       
    37     iStationLogo = NULL;
       
    38 
       
    39     clearAndDestroyLogos();
       
    40 
       
    41     while (!iHistoryList.isEmpty())
       
    42     {
       
    43         delete iHistoryList.takeFirst();
       
    44     }
       
    45     
       
    46     if (iHistoryEngine)
       
    47     {
       
    48         iHistoryEngine->closeInstance();
       
    49         iHistoryEngine = NULL;
       
    50     }
       
    51 }
       
    52 
       
    53 int IRHistoryModel::rowCount(const QModelIndex &aParent) const
       
    54 {
       
    55     Q_UNUSED(aParent);
       
    56     return iHistoryList.count();
       
    57 }
       
    58 
       
    59 QString IRHistoryModel::getImageUrl(int aRow) const
       
    60 {
       
    61     return iHistoryList.at(aRow)->getImageUrl();
       
    62 }
       
    63 
       
    64 void IRHistoryModel::setLogo(HbIcon *aIcon, int aIndex)
       
    65 {
       
    66     iLogos[aIndex] = aIcon;
       
    67     emit dataChanged(index(aIndex), index(aIndex));
       
    68 }
       
    69 
       
    70 QVariant IRHistoryModel::data(const QModelIndex &aIndex, int aRole) const
       
    71 {
       
    72     if (!aIndex.isValid())
       
    73     {
       
    74         return QVariant();
       
    75     }
       
    76 
       
    77     if (aIndex.row() >= iHistoryList.count())
       
    78     {
       
    79         return QVariant();
       
    80     }
       
    81     
       
    82     if (aRole == Qt::DisplayRole)
       
    83     {
       
    84         QVariantList list;
       
    85         
       
    86         int row = aIndex.row();
       
    87         QString primaryText = QString::number(row+1) + ". " + iHistoryList.at(row)->getChannelName();
       
    88         list.append(primaryText);
       
    89         // fix bug #9888,if left descriptions as blank, only one line appears
       
    90         QString tempDes = iHistoryList.at(row)->getChannelDesc();
       
    91         if (0 == tempDes.length())
       
    92         {
       
    93             tempDes = " ";
       
    94         }
       
    95         list.append(tempDes);
       
    96         
       
    97         return list;
       
    98     }
       
    99     else if (aRole == Qt::DecorationRole)
       
   100     {
       
   101         QVariantList list;
       
   102         int row = aIndex.row();
       
   103         const HbIcon *icon = iLogos.value(row);
       
   104         if (icon)
       
   105         {
       
   106             list.append(*icon);
       
   107         }
       
   108         else
       
   109         {
       
   110             list.append(*iStationLogo);
       
   111         }
       
   112         
       
   113         return list;
       
   114     }
       
   115     else if (aRole == Qt::BackgroundRole)
       
   116     {
       
   117         if (aIndex.row() % 2 == 0)
       
   118         {
       
   119             return QBrush(KListEvenRowColor);
       
   120         }
       
   121         else
       
   122         {
       
   123             return QBrush(KListOddRowColor);
       
   124         }
       
   125     }
       
   126 
       
   127     return QVariant();
       
   128 }
       
   129 
       
   130 IRQSongHistoryInfo* IRHistoryModel::getHistoryInfo(int aIndex)
       
   131 {
       
   132     if (aIndex >= 0 && aIndex < iHistoryList.count())
       
   133     {
       
   134         return iHistoryList.at(aIndex);
       
   135     }
       
   136 
       
   137     return NULL;
       
   138 }
       
   139 
       
   140 void IRHistoryModel::clearAllList()
       
   141 {
       
   142     while (!iHistoryList.isEmpty())
       
   143     {
       
   144         IRQSongHistoryInfo *firstItem = iHistoryList.takeFirst();
       
   145         delete firstItem;
       
   146     }
       
   147     iHistoryEngine->clearAllHistory();
       
   148     clearAndDestroyLogos();
       
   149     emit modelChanged();
       
   150 }
       
   151 
       
   152 bool IRHistoryModel::checkHistoryUpdate()
       
   153 {
       
   154     getAllList();    
       
   155     return true;
       
   156 }
       
   157 
       
   158 void IRHistoryModel::clearAndDestroyLogos()
       
   159 {
       
   160     for (QMap<int, HbIcon*>::iterator it = iLogos.begin(); it != iLogos.end(); ++it)
       
   161     {
       
   162         delete it.value();
       
   163     }
       
   164     
       
   165     iLogos.clear();
       
   166 }
       
   167 
       
   168 void IRHistoryModel::getAllList()
       
   169 {
       
   170     iHistoryEngine->getAllHistory(iHistoryList);
       
   171 
       
   172     emit modelChanged();
       
   173 }
       
   174 
       
   175 bool IRHistoryModel::deleteOneItem(int aIndex)
       
   176 {
       
   177     bool ret = iHistoryEngine->deleteOneItem(aIndex);
       
   178      
       
   179     if( !ret )
       
   180     {
       
   181         return false;                
       
   182     }
       
   183     
       
   184     beginRemoveRows(QModelIndex(), aIndex, aIndex);
       
   185     iHistoryList.removeAt(aIndex);
       
   186     endRemoveRows(); 
       
   187     
       
   188     emit modelChanged();    
       
   189     return true;
       
   190 }