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