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