mpdata/src/mpplaybackdata.cpp
branchRCL_3
changeset 25 14979e23cb5e
equal deleted inserted replaced
24:26a1709b9fec 25:14979e23cb5e
       
     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: Playback Data provider for playback view.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QIcon>
       
    19 #include <QString>
       
    20 #include <QPixmap>
       
    21 #include <QPainter>
       
    22 #include <hbicon.h>
       
    23 #include <thumbnailmanager_qt.h>
       
    24 
       
    25 #include "mpplaybackdata.h"
       
    26 #include "mptrace.h"
       
    27 
       
    28 const int KUndefined = -1;
       
    29 
       
    30 /*!
       
    31     \class MpPlaybackData
       
    32     \brief Music Player playback data.
       
    33 
       
    34     Playback data provides access to current playing song data, and playback
       
    35     properties, it is designed as an interface between the playback wrapper
       
    36     and the UI, it also provides album art extraction.
       
    37 */
       
    38 
       
    39 /*!
       
    40     \fn void durationChanged()
       
    41 
       
    42     This signal is emitted when duration changes.
       
    43  */
       
    44 
       
    45 /*!
       
    46     \fn void positionChanged()
       
    47 
       
    48     This signal is emitted when position changes.
       
    49  */
       
    50 
       
    51 /*!
       
    52     \fn void albumArtReady()
       
    53 
       
    54     This signal is emitted when album art is ready.
       
    55  */
       
    56 
       
    57 /*!
       
    58     \fn void playbackStateChanged()
       
    59 
       
    60     This signal is emitted when there is a *transition* on the playback state,
       
    61     this is based on a simple state.
       
    62  */
       
    63 
       
    64 /*!
       
    65     \fn void playbackInfoChanged()
       
    66 
       
    67     This signal is emitted when playback info changes. This includes:
       
    68     title, artist and album name.
       
    69  */
       
    70 
       
    71 /*!
       
    72     \fn void fileCorrupted()
       
    73 
       
    74     This signal is emitted when a file is found corrupted.  Call by 
       
    75     PlayBackWrapper.  
       
    76  */
       
    77 
       
    78 
       
    79 
       
    80 /*!
       
    81     Constructs a new MpPlaybackData.
       
    82  */
       
    83 MpPlaybackData::MpPlaybackData( QObject *parent )
       
    84     : QObject( parent ),
       
    85       mThumbnailManager( new ThumbnailManager( this ) ),
       
    86       mReqId( KUndefined ),
       
    87       mDuration(0),
       
    88       mPosition(0),
       
    89       mAlbumId(0),
       
    90       mId(0),
       
    91       mAlbumArt( "qtg_large_album_art" ),
       
    92       mPlaybackState( NotPlaying ),
       
    93       mRealAudio( false )
       
    94 {
       
    95     TX_ENTRY
       
    96     mThumbnailManager->setQualityPreference( ThumbnailManager::OptimizeForQuality );
       
    97     mThumbnailManager->setThumbnailSize( ThumbnailManager::ThumbnailLarge );
       
    98     connect( mThumbnailManager, SIGNAL( thumbnailReady(QPixmap, void *, int, int ) ),
       
    99              this, SLOT(thumbnailReady( QPixmap, void *, int, int ) ) );
       
   100     TX_EXIT
       
   101 }
       
   102 
       
   103 /*!
       
   104  Constructs a new MpPlaybackData.
       
   105  */
       
   106 MpPlaybackData::~MpPlaybackData()
       
   107 {
       
   108     TX_ENTRY
       
   109     delete mThumbnailManager;
       
   110     TX_EXIT
       
   111 }
       
   112 
       
   113 /*!
       
   114  Sets the song \a duration.
       
   115 */
       
   116 void MpPlaybackData::setDuration( int duration )
       
   117 {
       
   118     TX_ENTRY_ARGS( "duration = " << duration )
       
   119     mDuration = duration;
       
   120     emit durationChanged();
       
   121     TX_EXIT
       
   122 }
       
   123 
       
   124 /*!
       
   125  Returns the song duration.
       
   126 */
       
   127 int MpPlaybackData::duration() const
       
   128 {
       
   129     TX_LOG_ARGS( "mDuration = " << mDuration )
       
   130     return mDuration;
       
   131 }
       
   132 
       
   133 /*!
       
   134  Sets the song \a position.
       
   135 */
       
   136 void MpPlaybackData::setPosition( int position )
       
   137 {
       
   138     TX_ENTRY_ARGS( "position = " << position )
       
   139     mPosition = position;
       
   140     emit positionChanged();
       
   141     TX_EXIT
       
   142 }
       
   143 
       
   144 /*!
       
   145  Returns the song position.
       
   146 */
       
   147 int MpPlaybackData::position() const
       
   148 {
       
   149     TX_ENTRY_ARGS( "mPosition = " << mPosition )
       
   150     return mPosition;
       
   151 }
       
   152 
       
   153 /*!
       
   154  Sets the song \a title, returns true if the value is new.
       
   155 */
       
   156 bool MpPlaybackData::setTitle( const QString& title )
       
   157 {
       
   158     TX_ENTRY_ARGS( "title = " << title )
       
   159     bool change = false;
       
   160     if ( title != mTitle ) {
       
   161         change = true;
       
   162         mTitle = title;
       
   163     }
       
   164     TX_EXIT
       
   165     return change;
       
   166 }
       
   167 
       
   168 /*!
       
   169  Returns the song title.
       
   170 */
       
   171 const QString& MpPlaybackData::title() const
       
   172 {
       
   173     TX_ENTRY_ARGS( "mTitle = " << mTitle )
       
   174     return mTitle;
       
   175 }
       
   176 
       
   177 /*!
       
   178  Sets the song \a artist, returns true if the value is new.
       
   179 */
       
   180 bool MpPlaybackData::setArtist( const QString& artist )
       
   181 {
       
   182     TX_ENTRY_ARGS( "artist = " << artist )
       
   183     bool change = false;
       
   184     // data is different or mArtist was not set before or it was reset.
       
   185     if ( artist != mArtist || mArtist.isNull()) {
       
   186         change = true;
       
   187         mArtist = artist;
       
   188     }
       
   189     TX_EXIT
       
   190     return change;
       
   191 }
       
   192 
       
   193 /*!
       
   194  Returns the song artist.
       
   195 */
       
   196 const QString& MpPlaybackData::artist() const
       
   197 {
       
   198     TX_ENTRY_ARGS( "mArtist = " << mArtist )
       
   199     return mArtist;
       
   200 }
       
   201 
       
   202 /*!
       
   203  Sets the song \a album, returns true if the value is new.
       
   204 */
       
   205 bool MpPlaybackData::setAlbum( const QString& album )
       
   206 {
       
   207     TX_ENTRY_ARGS( "album = " << album )
       
   208     bool change = false;
       
   209     // data is different or mAlbum was not set before or it was reset.
       
   210     if ( album != mAlbum || mAlbum.isNull() ) {
       
   211         change = true;
       
   212         mAlbum = album;
       
   213     }
       
   214     TX_EXIT
       
   215     return change;
       
   216 }
       
   217 
       
   218 /*!
       
   219  Returns the song album.
       
   220 */
       
   221 const QString& MpPlaybackData::album() const
       
   222 {
       
   223     TX_ENTRY_ARGS( "mAlbum = " << mAlbum )
       
   224     return mAlbum;
       
   225 }
       
   226 
       
   227 /*!
       
   228  Sets the song \a uri, returns true if the value is new.
       
   229 */
       
   230 bool MpPlaybackData::setUri( const QString& uri )
       
   231 {
       
   232     TX_ENTRY_ARGS( "uri = " << uri )
       
   233     bool change = false;
       
   234     if ( uri != mUri ) {
       
   235         change = true;
       
   236         mUri = uri;
       
   237     }
       
   238     TX_EXIT
       
   239     return change;
       
   240 }
       
   241 
       
   242 /*!
       
   243  Returns the song uri.
       
   244 */
       
   245 const QString& MpPlaybackData::uri() const
       
   246 {
       
   247     TX_ENTRY_ARGS( "mUri = " << mUri )
       
   248     return mUri;
       
   249 }
       
   250 
       
   251 /*!
       
   252  Sets the song \a albumArtUri.
       
   253 */
       
   254 void MpPlaybackData::setAlbumArtUri( const QString& albumArtUri )
       
   255 {
       
   256     TX_ENTRY_ARGS( "albumArtUri = " << albumArtUri )
       
   257     if ( !albumArtUri.isEmpty() ) {
       
   258         bool ok = true;
       
   259         if ( mReqId != KUndefined ) {
       
   260             // There is already an outstanding request. Cancel it first.
       
   261             bool ok = mThumbnailManager->cancelRequest(mReqId);
       
   262         }
       
   263         if ( ok ) {
       
   264             mReqId = mThumbnailManager->getThumbnail( albumArtUri );
       
   265             if ( mReqId == KUndefined ) {
       
   266                 // Request failed. clear the icon.
       
   267                 mAlbumArt.clear();
       
   268                 emit albumArtReady();
       
   269             }
       
   270         }
       
   271     }
       
   272     else {
       
   273         // No album art uri. clear the icon.
       
   274         mAlbumArt.clear();
       
   275         emit albumArtReady();
       
   276     }
       
   277     TX_EXIT
       
   278 }
       
   279 
       
   280 /*!
       
   281  Returns the song album art on \a icon.
       
   282 */
       
   283 void MpPlaybackData::albumArt( HbIcon& icon ) const
       
   284 {
       
   285     TX_ENTRY
       
   286     icon = mAlbumArt ;
       
   287     TX_EXIT
       
   288 }
       
   289 
       
   290 /*!
       
   291  Sets the song's album \a id, returns true if the value is new.
       
   292 */
       
   293 bool MpPlaybackData::setAlbumId( int id )
       
   294 {
       
   295     bool change = false;
       
   296     if (mAlbumId != id) {
       
   297         mAlbumId = id;
       
   298         change = true;
       
   299     }
       
   300     return change;
       
   301 }
       
   302 
       
   303 /*!
       
   304  Returns the id of the album to which the song belongs.
       
   305 */
       
   306 int MpPlaybackData::albumId()
       
   307 {
       
   308     return mAlbumId;
       
   309 }
       
   310 
       
   311 /*!
       
   312  Sets the song's \a id, returns true if the value is new.
       
   313 */
       
   314 bool MpPlaybackData::setId( int id )
       
   315 {
       
   316     bool change = false;
       
   317     if (mId != id) {
       
   318         mId = id;
       
   319         change = true;
       
   320     }
       
   321     return change;
       
   322 }
       
   323 
       
   324 /*!
       
   325  Returns the id the song.
       
   326 */
       
   327 int MpPlaybackData::id()
       
   328 {
       
   329     return mId;
       
   330 }
       
   331 /*!
       
   332 Set Real Audio \a mode.
       
   333 */
       
   334 bool MpPlaybackData::setRealAudio( bool mode )
       
   335 {
       
   336     TX_ENTRY
       
   337     bool change = false;
       
   338     if (mRealAudio != mode) {
       
   339         mRealAudio = mode;
       
   340         change = true;
       
   341     }
       
   342     TX_EXIT
       
   343     return change;
       
   344 }
       
   345 
       
   346 /*!
       
   347  Returns the mode of Real Audio.
       
   348 */
       
   349 bool MpPlaybackData::realAudio()
       
   350 {
       
   351     return mRealAudio;
       
   352 }
       
   353 
       
   354 /*!
       
   355  Sets the playback \a state.
       
   356 */
       
   357 void MpPlaybackData::setPlaybackState( const SimplifiedState state )
       
   358 {
       
   359     TX_ENTRY_ARGS( "state = " << state )
       
   360     if ( state != mPlaybackState ) {
       
   361         mPlaybackState = state;
       
   362         emit playbackStateChanged();
       
   363     }
       
   364     TX_EXIT
       
   365 }
       
   366 
       
   367 /*!
       
   368  Returns the playback state.
       
   369 */
       
   370 MpPlaybackData::SimplifiedState MpPlaybackData::playbackState() const
       
   371 {
       
   372     TX_LOG_ARGS( "mPlaybackState = " << mPlaybackState )
       
   373     return mPlaybackState;
       
   374 }
       
   375 
       
   376 /*!
       
   377 
       
   378 */
       
   379 void MpPlaybackData::commitPlaybackInfo()
       
   380 {
       
   381     TX_ENTRY
       
   382     emit playbackInfoChanged();
       
   383     TX_EXIT
       
   384 }
       
   385 
       
   386 /*!
       
   387  Resets the data, currently called when source is removed.
       
   388 */
       
   389 void MpPlaybackData::resetData()
       
   390 {
       
   391     mDuration = 0;
       
   392     mPosition = 0;
       
   393     mTitle = QString();
       
   394     mAlbum = QString();
       
   395     mArtist = QString();
       
   396     mUri = QString();
       
   397     mAlbumId = 0;
       
   398     mId = 0;
       
   399     mAlbumArt.clear();
       
   400     mRealAudio = false;
       
   401     
       
   402     emit durationChanged();
       
   403     emit positionChanged();
       
   404     emit albumArtReady();
       
   405     emit playbackInfoChanged();
       
   406 }
       
   407 
       
   408 /*!
       
   409  Emit fileCorrupted(id) signal when a file with id is corrupted
       
   410 */
       
   411 void MpPlaybackData::setCorrupted( int id )
       
   412 {
       
   413    emit fileCorrupted( id );
       
   414 }
       
   415 
       
   416 /*!
       
   417  Slot to handle the album art thumb.
       
   418 */
       
   419 void MpPlaybackData::thumbnailReady(
       
   420     const QPixmap& pixmap, void *data, int id, int error )
       
   421 {
       
   422     TX_LOG_ARGS( "error = " << error << ", id = " << id )
       
   423     Q_UNUSED(data);
       
   424     if ( error == 0 && mReqId == id ) {
       
   425         mAlbumArt = HbIcon(QIcon( pixmap ));      
       
   426     }
       
   427     else {
       
   428         mReqId = KUndefined;
       
   429         mAlbumArt.clear();
       
   430     }
       
   431     emit albumArtReady();
       
   432     TX_EXIT
       
   433 }
       
   434