radioapp/radiouiengine/src/radiohistorymodel.cpp
changeset 16 f54ebcfc1b80
child 19 afea38384506
equal deleted inserted replaced
14:63aabac4416d 16:f54ebcfc1b80
       
     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 // System includes
       
    19 #include <QStringList>
       
    20 
       
    21 // User includes
       
    22 #include "radiohistorymodel.h"
       
    23 #include "radiohistorymodel_p.h"
       
    24 #include "radiouiengine.h"
       
    25 #include "radiohistoryitem.h"
       
    26 #include "radiostation.h"
       
    27 #include "radiouiengine.h"
       
    28 #include "radio_global.h"
       
    29 #include "radiologger.h"
       
    30 
       
    31 /*!
       
    32  *
       
    33  */
       
    34 RadioHistoryModel::RadioHistoryModel( RadioUiEngine& uiEngine ) :
       
    35     QAbstractListModel( &uiEngine ),
       
    36     d_ptr( new RadioHistoryModelPrivate( this, uiEngine ) )
       
    37 {
       
    38     connectAndTest( &uiEngine,  SIGNAL(tunedToFrequency(uint,int)),
       
    39                     this,       SLOT(resetCurrentSong()) );
       
    40     connectAndTest( &uiEngine,  SIGNAL(seekingStarted(int)),
       
    41                     this,       SLOT(resetCurrentSong()) );
       
    42 }
       
    43 
       
    44 /*!
       
    45  *
       
    46  */
       
    47 RadioHistoryModel::~RadioHistoryModel()
       
    48 {
       
    49     Q_D( RadioHistoryModel );
       
    50     d->mItems.clear();
       
    51     delete d_ptr;
       
    52 }
       
    53 
       
    54 /*!
       
    55  * \reimp
       
    56  */
       
    57 Qt::ItemFlags RadioHistoryModel::flags ( const QModelIndex& index ) const
       
    58 {
       
    59     Qt::ItemFlags flags = QAbstractListModel::flags( index );
       
    60     flags |= Qt::ItemIsEditable;
       
    61     return flags;
       
    62 }
       
    63 
       
    64 /*!
       
    65  * \reimp
       
    66  */
       
    67 int RadioHistoryModel::rowCount( const QModelIndex& parent ) const
       
    68 {
       
    69     Q_UNUSED( parent );
       
    70     Q_D( const RadioHistoryModel );
       
    71     return d->mItems.count();
       
    72 }
       
    73 
       
    74 /*!
       
    75  * \reimp
       
    76  */
       
    77 QVariant RadioHistoryModel::data( const QModelIndex& index, int role ) const
       
    78 {
       
    79     if ( !index.isValid() ) {
       
    80         return QVariant();
       
    81     }
       
    82 
       
    83     Q_D( const RadioHistoryModel );
       
    84     if ( role == Qt::DisplayRole ) {
       
    85         RadioHistoryItem item = d->mItems.at( index.row() );
       
    86 
       
    87         QStringList list;
       
    88         if ( d->mShowDetails ) {
       
    89             list.append( item.artist() + " - " + item.title() );
       
    90             list.append( item.time() + " " + item.station() + " " /*+ RadioUiEngine::parseFrequency( item.frequency() ) */ );
       
    91         } else {
       
    92             list.append( item.artist() );
       
    93             list.append( item.title() );
       
    94         }
       
    95 
       
    96         return list;
       
    97     }
       
    98 
       
    99     return QVariant();
       
   100 }
       
   101 
       
   102 /*!
       
   103  * \reimp
       
   104  */
       
   105 bool RadioHistoryModel::setData( const QModelIndex& index, const QVariant& value, int role )
       
   106 {
       
   107     Q_UNUSED( value );
       
   108     if ( !index.isValid() ) {
       
   109         return false;
       
   110     }
       
   111 
       
   112     if ( role == RadioHistoryModel::SetFavoriteRole ) {
       
   113         Q_D( RadioHistoryModel );
       
   114         RadioHistoryItem item = d->mItems.at( index.row() );
       
   115         item.setFavorite();
       
   116         updateItem( index.row(), item );
       
   117         return true;
       
   118     }
       
   119 
       
   120     return false;
       
   121 }
       
   122 
       
   123 /*!
       
   124  * Public slot
       
   125  */
       
   126 void RadioHistoryModel::resetCurrentSong()
       
   127 {
       
   128     Q_D( RadioHistoryModel );
       
   129     d->mTopItemIsPlaying = false;
       
   130     emit currentSongReset();
       
   131 }
       
   132 
       
   133 /*!
       
   134  * Public slot
       
   135  */
       
   136 void RadioHistoryModel::setFavorite()
       
   137 {
       
   138     Q_D( RadioHistoryModel );
       
   139     RadioHistoryItem item = d->mItems.first();
       
   140     item.setFavorite();
       
   141     updateItem( 0, item );
       
   142 }
       
   143 
       
   144 /*!
       
   145  * Public slot
       
   146  */
       
   147 void RadioHistoryModel::removeAll()
       
   148 {
       
   149     Q_D( RadioHistoryModel );
       
   150 
       
   151     beginRemoveRows( QModelIndex(), 0, rowCount() - 1 );
       
   152 
       
   153     d->mItems.clear();
       
   154 
       
   155     endRemoveRows();
       
   156 }
       
   157 
       
   158 /*!
       
   159  *
       
   160  */
       
   161 bool RadioHistoryModel::isCurrentSongRecognized() const
       
   162 {
       
   163     Q_D( const RadioHistoryModel );
       
   164     return d->mTopItemIsPlaying;
       
   165 }
       
   166 
       
   167 /*!
       
   168  *
       
   169  */
       
   170 void RadioHistoryModel::setShowDetails( bool showDetails )
       
   171 {
       
   172     Q_D( RadioHistoryModel );
       
   173     d->mShowDetails = showDetails;
       
   174     reset();
       
   175 }
       
   176 
       
   177 /*!
       
   178  *
       
   179  */
       
   180 void RadioHistoryModel::addItem( const QString& artist, const QString& title, const RadioStation& station )
       
   181 {
       
   182     Q_D( RadioHistoryModel );
       
   183 
       
   184     RadioHistoryItem item;
       
   185     const int itemIndex = findItem( artist, title, item );
       
   186     if ( itemIndex != -1 ) {
       
   187         item.increasePlayCount();
       
   188         updateItem( itemIndex, item, true );
       
   189     } else {
       
   190         item.setArtist( artist );
       
   191         item.setTitle( title );
       
   192         item.setStation( station.name() );
       
   193         item.setFrequency( station.frequency() );
       
   194         item.setCurrentTime();
       
   195 
       
   196         beginInsertRows( QModelIndex(), 0, 0 );
       
   197 
       
   198         d->mItems.prepend( item );
       
   199 
       
   200         endInsertRows();
       
   201     }
       
   202 
       
   203     d->mTopItemIsPlaying = true;
       
   204     emit itemAdded();
       
   205 }
       
   206 
       
   207 /*!
       
   208  *
       
   209  */
       
   210 void RadioHistoryModel::clearRadioTextPlus()
       
   211 {
       
   212     Q_D( RadioHistoryModel );
       
   213     d->mRtItemHolder = "";
       
   214     resetCurrentSong();
       
   215 }
       
   216 
       
   217 /*!
       
   218  *
       
   219  */
       
   220 void RadioHistoryModel::addRadioTextPlus( int rtClass, const QString& rtItem, const RadioStation& station )
       
   221 {
       
   222     if ( rtClass == RtPlus::Artist || rtClass == RtPlus::Title ) {
       
   223         Q_D( RadioHistoryModel );
       
   224         if ( d->mRtItemHolder.isEmpty() ) {
       
   225             d->mRtItemHolder = rtItem;
       
   226         } else {
       
   227             if ( rtClass == RtPlus::Title ) {
       
   228                 addItem( d->mRtItemHolder, rtItem, station );
       
   229             } else {
       
   230                 addItem( rtItem, d->mRtItemHolder, station );
       
   231             }
       
   232             d->mRtItemHolder = "";
       
   233         }
       
   234     }
       
   235 }
       
   236 
       
   237 /*!
       
   238  *
       
   239  */
       
   240 int RadioHistoryModel::findItem( const QString& artist, const QString& title, RadioHistoryItem& item )
       
   241 {
       
   242     Q_D( RadioHistoryModel );
       
   243     const int itemCount = d->mItems.count();
       
   244     for ( int i = 0; i < itemCount; ++i ) {
       
   245         RadioHistoryItem existingItem = d->mItems.at( i );
       
   246         if ( existingItem.artist().compare( artist ) == 0
       
   247              && existingItem.title().compare( title ) == 0 ) {
       
   248             item = existingItem;
       
   249             return i;
       
   250         }
       
   251     }
       
   252 
       
   253     return -1;
       
   254 }
       
   255 
       
   256 /*!
       
   257  *
       
   258  */
       
   259 void RadioHistoryModel::updateItem( int index, const RadioHistoryItem& item, bool prepend )
       
   260 {
       
   261     Q_D( RadioHistoryModel );
       
   262     d->mItems.removeAt( index );
       
   263 
       
   264     if ( prepend ) {
       
   265         d->mItems.prepend( item );
       
   266     } else {
       
   267         d->mItems.insert( index, item );
       
   268     }
       
   269 }