mpdata/src/mpmpxcollectiondata_p.cpp
changeset 22 ecf06a08d4d9
child 29 8192e5b5c935
equal deleted inserted replaced
20:82baf59ce8dd 22:ecf06a08d4d9
       
     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: Music Player collection data - private implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <thumbnaildata.h>
       
    19 #include <thumbnailobjectsource.h>
       
    20 
       
    21 #include <mpxmedia.h>
       
    22 #include <mpxmediacontainerdefs.h>
       
    23 #include <mpxmediaarray.h>
       
    24 #include <mpxmediageneraldefs.h>
       
    25 #include <mpxmediamusicdefs.h>
       
    26 
       
    27 #include "mpmpxcollectiondata_p.h"
       
    28 #include "mptrace.h"
       
    29 
       
    30 _LIT( KSong, " song" );
       
    31 _LIT( KSongs, " songs" );
       
    32 
       
    33 /*!
       
    34     \class MpMpxCollectionDataPrivate
       
    35     \brief Music Player collection data - private implementation.
       
    36 
       
    37     This is a private implementation of the collection data interface.
       
    38 */
       
    39 
       
    40 /*!
       
    41  \internal
       
    42  */
       
    43 MpMpxCollectionDataPrivate::MpMpxCollectionDataPrivate( MpMpxCollectionData *wrapper )
       
    44     : q_ptr( wrapper ),
       
    45       iContainerMedia(0),
       
    46       iMediaArray(0),
       
    47       iContext( ECollectionContextUnknown ),
       
    48       iCachedRemovedItem ( 0 )
       
    49 {
       
    50     TX_LOG
       
    51 }
       
    52 
       
    53 /*!
       
    54  \internal
       
    55  */
       
    56 MpMpxCollectionDataPrivate::~MpMpxCollectionDataPrivate()
       
    57 {
       
    58     TX_ENTRY
       
    59     delete iContainerMedia;
       
    60     delete iCachedRemovedItem;
       
    61     TX_EXIT
       
    62 }
       
    63 
       
    64 /*!
       
    65  \internal
       
    66  */
       
    67 TCollectionContext MpMpxCollectionDataPrivate::context() const
       
    68 {
       
    69     return iContext;
       
    70 }
       
    71 
       
    72 /*!
       
    73  \internal
       
    74  */
       
    75 int MpMpxCollectionDataPrivate::count() const
       
    76 {
       
    77     if ( iMediaArray ) {
       
    78         return iMediaArray->Count();
       
    79     }
       
    80     return 0;
       
    81 }
       
    82 
       
    83 /*!
       
    84  \internal
       
    85  */
       
    86 QString MpMpxCollectionDataPrivate::collectionTitle() const
       
    87 {
       
    88     TX_ENTRY
       
    89     QString title;
       
    90     if ( iContainerMedia && iContainerMedia->IsSupported( KMPXMediaGeneralTitle ) ) {
       
    91         const TDesC& titleText = iContainerMedia->ValueText( KMPXMediaGeneralTitle );
       
    92         if ( titleText.Compare( KNullDesC ) != 0 ) {
       
    93             title = QString::fromUtf16( titleText.Ptr(), titleText.Length() );
       
    94         }
       
    95     }
       
    96     TX_EXIT_ARGS("title =" << title);
       
    97     return title;
       
    98 }
       
    99 
       
   100 /*!
       
   101  \internal
       
   102  */
       
   103 QString MpMpxCollectionDataPrivate::itemData( int index, MpMpxCollectionData::DataType type ) const
       
   104 {
       
   105     TX_ENTRY_ARGS("index=" << index << ", type=" << type);
       
   106     QString data;
       
   107     TRAPD(err, DoGetDataL(index, type, data));
       
   108     if ( err != KErrNone ) {
       
   109         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   110     }
       
   111     TX_EXIT
       
   112     return data;
       
   113 }
       
   114 
       
   115 /*!
       
   116  \internal
       
   117  */
       
   118 bool MpMpxCollectionDataPrivate::isAutoPlaylist()
       
   119 {
       
   120     TX_ENTRY
       
   121     if ( iContext != ECollectionContextPlaylistSongs ) {
       
   122         TX_EXIT
       
   123         return false;
       
   124     }
       
   125 
       
   126     bool isAuto = false;
       
   127     TRAPD(err, isAuto = DoIsAutoPlaylistL());
       
   128     if ( err == KErrNone ) {
       
   129         TX_LOG_ARGS("isAuto=" << isAuto);
       
   130     }
       
   131     else {
       
   132         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   133     }
       
   134     TX_EXIT
       
   135     return isAuto;
       
   136 }
       
   137 
       
   138 /*!
       
   139  \internal
       
   140  */
       
   141 bool MpMpxCollectionDataPrivate::isAutoPlaylist( int index )
       
   142 {
       
   143     TX_ENTRY_ARGS("index=" << index);
       
   144     if ( iContext != ECollectionContextPlaylists ) {
       
   145         TX_EXIT
       
   146         return false;
       
   147     }
       
   148 
       
   149     bool isAuto = false;
       
   150     TRAPD(err, isAuto = DoIsAutoPlaylistL(index));
       
   151     if ( err == KErrNone ) {
       
   152         TX_LOG_ARGS("isAuto=" << isAuto);
       
   153     }
       
   154     else {
       
   155         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   156     }
       
   157     TX_EXIT
       
   158     return isAuto;
       
   159 }
       
   160 
       
   161 /*!
       
   162  \internal
       
   163  */
       
   164 int MpMpxCollectionDataPrivate::itemCount( int index )
       
   165 {
       
   166     TX_ENTRY_ARGS("index=" << index);
       
   167     int count = 0;
       
   168     TRAPD(err, count = DoGetItemCountL(index));
       
   169     if ( err == KErrNone ) {
       
   170         TX_LOG_ARGS("count=" << count);
       
   171     }
       
   172     else {
       
   173         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   174     }
       
   175     TX_EXIT
       
   176     return count;
       
   177 }
       
   178 
       
   179 /*!
       
   180  \internal
       
   181  */
       
   182 int MpMpxCollectionDataPrivate::containerId()
       
   183 {
       
   184     int id = -1;
       
   185     TRAPD( err, id = DoGetContainerIdL() );
       
   186     if ( err == KErrNone ) {
       
   187         TX_LOG_ARGS("id=" << id);
       
   188     }
       
   189     else {
       
   190         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   191     }
       
   192     TX_EXIT
       
   193     return id;
       
   194 }
       
   195 
       
   196 /*!
       
   197  \internal
       
   198  */
       
   199 int MpMpxCollectionDataPrivate::itemId(int index)
       
   200 {
       
   201     TX_ENTRY_ARGS("index=" << index);
       
   202     int id = -1;
       
   203     TRAPD(err, id = DoGetItemIdL(index));
       
   204     if ( err == KErrNone ) {
       
   205         TX_LOG_ARGS("id=" << id);
       
   206     }
       
   207     else {
       
   208         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   209     }
       
   210     TX_EXIT
       
   211     return id;
       
   212 }
       
   213 
       
   214 /*!
       
   215  \internal
       
   216  */
       
   217 void MpMpxCollectionDataPrivate::removeItem(int index)
       
   218 {
       
   219     TX_ENTRY_ARGS("index=" << index);
       
   220      TRAPD(err,  DoRemoveItemL(index));
       
   221     if ( err != KErrNone ) {
       
   222         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   223     }
       
   224     TX_EXIT
       
   225 }
       
   226 
       
   227 /*!
       
   228  \internal
       
   229  */
       
   230 bool MpMpxCollectionDataPrivate::testCachedItem( int itemId )
       
   231 {
       
   232     TX_ENTRY_ARGS( "itemId=" << itemId);
       
   233     bool match = false;
       
   234     TRAPD( err, match = DoTestCachedItemL( itemId ) );
       
   235     if ( err == KErrNone ) {
       
   236         TX_LOG_ARGS("match=" << match);
       
   237     }
       
   238     else {
       
   239         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   240     }
       
   241     TX_EXIT
       
   242     return match;
       
   243 }
       
   244 
       
   245 /*!
       
   246  \internal
       
   247  */
       
   248 void MpMpxCollectionDataPrivate::insertCachedItem(int index)
       
   249 {
       
   250     TX_ENTRY_ARGS("index=" << index);
       
   251     iMediaArray->Insert( iCachedRemovedItem, index );
       
   252     iCachedRemovedItem = 0; //ownership tranferred above.
       
   253     TX_EXIT
       
   254 }
       
   255 
       
   256 /*!
       
   257  \internal
       
   258  */
       
   259 void MpMpxCollectionDataPrivate::setMpxMedia( const CMPXMedia& entries )
       
   260 {
       
   261     TX_ENTRY
       
   262     TRAPD(err, DoSetMpxMediaL(entries));
       
   263     if ( err == KErrNone ) {
       
   264         TX_LOG_ARGS("Context changed: iContext=" << iContext);
       
   265         emit q_ptr->contextChanged(iContext);
       
   266     }
       
   267     else {
       
   268         TX_LOG_ARGS("Error: " << err << "; should never get here.");
       
   269     }
       
   270     TX_EXIT
       
   271 }
       
   272 
       
   273 /*!
       
   274  \internal
       
   275  */
       
   276 void MpMpxCollectionDataPrivate::DoGetDataL( int index, MpMpxCollectionData::DataType type, QString& data ) const
       
   277 {
       
   278     TX_ENTRY
       
   279     CMPXMedia* currentMedia( iMediaArray->AtL( index ) );
       
   280 
       
   281     TBuf<256> countBuf;
       
   282     TBuf<20> temp;
       
   283     TInt count = 0;
       
   284     switch ( type ) {
       
   285         case MpMpxCollectionData::Title:
       
   286             if ( currentMedia->IsSupported( KMPXMediaGeneralTitle ) ) {
       
   287                 const TDesC& title = currentMedia->ValueText( KMPXMediaGeneralTitle );
       
   288                 if ( title.Compare( KNullDesC ) != 0 ) {
       
   289                     data = QString::fromUtf16( title.Ptr(), title.Length() );
       
   290                 }
       
   291             }
       
   292             break;
       
   293         case MpMpxCollectionData::Artist:
       
   294             if ( currentMedia->IsSupported( KMPXMediaMusicArtist ) ) {
       
   295                 const TDesC& artist = currentMedia->ValueText( KMPXMediaMusicArtist );
       
   296                 if ( artist.Compare( KNullDesC ) != 0 ) {
       
   297                     data = QString::fromUtf16( artist.Ptr(), artist.Length() );
       
   298                 }
       
   299             }
       
   300             break;
       
   301         case MpMpxCollectionData::Album:
       
   302             if ( currentMedia->IsSupported( KMPXMediaMusicAlbum ) ) {
       
   303                 const TDesC& album = currentMedia->ValueText( KMPXMediaMusicAlbum );
       
   304                 if ( album.Compare( KNullDesC ) != 0 ) {
       
   305                     data = QString::fromUtf16( album.Ptr(), album.Length() );
       
   306                 }
       
   307             }
       
   308             break;
       
   309         case MpMpxCollectionData::Count:
       
   310             if ( currentMedia->IsSupported( KMPXMediaGeneralCount ) ) {
       
   311                 count = currentMedia->ValueTObjectL<TInt>( KMPXMediaGeneralCount );
       
   312             }
       
   313             temp.AppendNum( count );
       
   314             //AknTextUtils::LanguageSpecificNumberConversion( temp );
       
   315             countBuf.Append( temp );
       
   316             countBuf.Append( (count > 1 ) ? KSongs() : KSong() );
       
   317             data = QString::fromUtf16( countBuf.Ptr(), countBuf.Length() );
       
   318             break;
       
   319         case MpMpxCollectionData::AlbumArtUri:
       
   320             if ( currentMedia->IsSupported( KMPXMediaMusicAlbumArtFileName ) ) {
       
   321                 const TDesC& album = currentMedia->ValueText( KMPXMediaMusicAlbumArtFileName );
       
   322                 if ( album.Compare( KNullDesC ) != 0 ) {
       
   323                     data = QString::fromUtf16( album.Ptr(), album.Length() );
       
   324                 }
       
   325             }
       
   326             break;
       
   327         case MpMpxCollectionData::Uri:
       
   328             if ( currentMedia->IsSupported( KMPXMediaGeneralUri ) ) {
       
   329                 const TDesC& uri = currentMedia->ValueText( KMPXMediaGeneralUri );
       
   330                 if ( uri.Compare( KNullDesC ) != 0 ) {
       
   331                     data = QString::fromUtf16( uri.Ptr(), uri.Length() );
       
   332                 }
       
   333             }
       
   334             break;
       
   335         default:
       
   336             break;
       
   337     }
       
   338     TX_EXIT
       
   339 }
       
   340 
       
   341 /*!
       
   342  \internal
       
   343  */
       
   344 bool MpMpxCollectionDataPrivate::DoIsAutoPlaylistL()
       
   345 {
       
   346     if ( iContainerMedia->IsSupported(KMPXMediaGeneralNonPermissibleActions) ) {
       
   347         TMPXGeneralNonPermissibleActions attr(
       
   348                 iContainerMedia->ValueTObjectL<TMPXGeneralNonPermissibleActions>(
       
   349                 KMPXMediaGeneralNonPermissibleActions ) );
       
   350         if ( attr & EMPXWrite ) {
       
   351             return true;
       
   352         }
       
   353     }
       
   354     return false;
       
   355 }
       
   356 
       
   357 /*!
       
   358  \internal
       
   359  */
       
   360 bool MpMpxCollectionDataPrivate::DoIsAutoPlaylistL( int index )
       
   361 {
       
   362     const CMPXMediaArray* containerArray = iContainerMedia->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   363     User::LeaveIfNull( const_cast<CMPXMediaArray*>( containerArray ));
       
   364     CMPXMedia* media( containerArray->AtL(index) );
       
   365 
       
   366     if ( media->IsSupported(KMPXMediaGeneralNonPermissibleActions) ) {
       
   367         TMPXGeneralNonPermissibleActions attr(
       
   368             media->ValueTObjectL<TMPXGeneralNonPermissibleActions>(
       
   369                 KMPXMediaGeneralNonPermissibleActions ) );
       
   370         if ( attr & EMPXWrite ) {
       
   371             return true;
       
   372         }
       
   373     }
       
   374     return false;
       
   375 }
       
   376 
       
   377 /*!
       
   378  \internal
       
   379  */
       
   380 int MpMpxCollectionDataPrivate::DoGetItemCountL( int index )
       
   381 {
       
   382     CMPXMedia* currentMedia( iMediaArray->AtL( index ) );
       
   383     int count = 0;
       
   384     if ( currentMedia->IsSupported( KMPXMediaGeneralCount ) ) {
       
   385         count = currentMedia->ValueTObjectL<TInt>( KMPXMediaGeneralCount );
       
   386     }
       
   387     return count;
       
   388 }
       
   389 
       
   390 /*!
       
   391  \internal
       
   392  */
       
   393 int MpMpxCollectionDataPrivate::DoGetContainerIdL()
       
   394 {
       
   395     if ( !iContainerMedia->IsSupported( KMPXMediaGeneralId ) ) {
       
   396         User::Leave(KErrNotFound);
       
   397     }
       
   398     return iContainerMedia->ValueTObjectL<TInt>( KMPXMediaGeneralId );
       
   399 }
       
   400 
       
   401 /*!
       
   402  \internal
       
   403  */
       
   404 int MpMpxCollectionDataPrivate::DoGetItemIdL( int index )
       
   405 {
       
   406     CMPXMedia* currentMedia( iMediaArray->AtL( index ) );
       
   407     if ( !currentMedia->IsSupported( KMPXMediaGeneralId ) ) {
       
   408         User::Leave(KErrNotFound);
       
   409     }
       
   410     return currentMedia->ValueTObjectL<TInt>( KMPXMediaGeneralId );
       
   411 }
       
   412 
       
   413 /*!
       
   414  \internal
       
   415  */
       
   416 void MpMpxCollectionDataPrivate::DoRemoveItemL( int index )
       
   417 {
       
   418     delete iCachedRemovedItem;
       
   419     iCachedRemovedItem = 0;
       
   420     iCachedRemovedItem = CMPXMedia::NewL( *iMediaArray->AtL( index ) );
       
   421     iMediaArray->Remove( index );
       
   422 }
       
   423 
       
   424 /*!
       
   425  \internal
       
   426  */
       
   427 bool MpMpxCollectionDataPrivate::DoTestCachedItemL( int itemId )
       
   428 {
       
   429     if ( !iCachedRemovedItem && !iCachedRemovedItem->IsSupported( KMPXMediaGeneralId ) ) {
       
   430         User::Leave(KErrNotFound);
       
   431     }
       
   432     return ( itemId == iCachedRemovedItem->ValueTObjectL<TInt>( KMPXMediaGeneralId ) );
       
   433 }
       
   434 
       
   435 
       
   436 /*!
       
   437  \internal
       
   438  */
       
   439 void MpMpxCollectionDataPrivate::SetCollectionContextL()
       
   440 {
       
   441     TX_ENTRY
       
   442     TMPXGeneralType containerType( EMPXNoType );
       
   443     if ( iContainerMedia->IsSupported( KMPXMediaGeneralType ) ) {
       
   444         containerType = iContainerMedia->ValueTObjectL<TMPXGeneralType>( KMPXMediaGeneralType );
       
   445     }
       
   446 
       
   447     TMPXGeneralCategory containerCategory( EMPXNoCategory );
       
   448     if( iContainerMedia->IsSupported( KMPXMediaGeneralCategory ) ) {
       
   449         containerCategory = iContainerMedia->ValueTObjectL<TMPXGeneralCategory>( KMPXMediaGeneralCategory );
       
   450     }
       
   451     TX_LOG_ARGS("type=" << containerType << ", category=" << containerCategory );
       
   452 
       
   453     iContext = ECollectionContextUnknown;
       
   454     if ( containerType == EMPXGroup ) {
       
   455         switch (containerCategory) {
       
   456             case EMPXSong:
       
   457                 iContext = ECollectionContextAllSongs;
       
   458                 break;
       
   459             case EMPXArtist:
       
   460                 iContext = ECollectionContextArtists;
       
   461                 break;
       
   462             case EMPXAlbum:
       
   463                 iContext = ECollectionContextAlbums;
       
   464                 break;
       
   465             case EMPXPlaylist:
       
   466                 iContext = ECollectionContextPlaylists;
       
   467                 break;
       
   468             case EMPXGenre:
       
   469                 iContext = ECollectionContextGenres;
       
   470                 break;
       
   471         }
       
   472     }
       
   473     else if ( containerType == EMPXItem ) {
       
   474         switch (containerCategory) {
       
   475             case EMPXArtist:
       
   476                 iContext = ECollectionContextArtistAlbums;
       
   477                 break;
       
   478             case EMPXAlbum:
       
   479                 iContext = ECollectionContextAlbumSongs;
       
   480                 break;
       
   481             case EMPXSong:
       
   482                 iContext = ECollectionContextArtistSongs;
       
   483                 break;
       
   484             case EMPXPlaylist:
       
   485                 iContext = ECollectionContextPlaylistSongs;
       
   486                 break;
       
   487             case EMPXGenre:
       
   488                 iContext = ECollectionContextGenreSongs;
       
   489                 break;
       
   490         }
       
   491     }
       
   492     TX_EXIT
       
   493 }
       
   494 
       
   495 /*!
       
   496  \internal
       
   497  */
       
   498 void MpMpxCollectionDataPrivate::DoSetMpxMediaL( const CMPXMedia& entries )
       
   499 {
       
   500     TX_ENTRY
       
   501     delete iContainerMedia;
       
   502     iContainerMedia = 0;
       
   503     iContainerMedia = CMPXMedia::NewL(entries);
       
   504     iMediaArray = const_cast<CMPXMediaArray*>(iContainerMedia->Value<CMPXMediaArray>( KMPXMediaArrayContents ) );
       
   505     TX_LOG_ARGS("media count=" << iMediaArray->Count() );
       
   506 
       
   507     SetCollectionContextL();
       
   508     TX_EXIT
       
   509 }
       
   510 
       
   511 /*!
       
   512  \internal
       
   513  */
       
   514 const CMPXMedia& MpMpxCollectionDataPrivate::containerMedia()
       
   515 {
       
   516     return *iContainerMedia;
       
   517 }
       
   518