Nokia N97 SDK
Example Applications Guide

CCEMusicView.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : CCEMusicView.cpp
00004 *  Part of     : Content Listing Framework Sample Application
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2006 Nokia Corporation.
00010 *  This material, including documentation and any related
00011 *  computer programs, is protected by copyright controlled by
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 
00017 // INCLUDE FILES
00018 #include "CCEMusicView.h"
00019 
00020 #include "CCEAppUi.h"
00021 #include "CCEOperationObserver.h"
00022 #include "CCEMusicContainer.h"
00023 #include "CCECustomSorter.h"
00024 #include "CCESoundPlayer.h"
00025 #include "CCEWaitNote.h"
00026 #include "CLFExample.hrh"
00027 #include "CLFExampleConsts.h"
00028 
00029 #include <akntitle.h>
00030 #include <f32file.h>
00031 #include <stringloader.h>
00032 #include <aknnotewrappers.h>
00033 #include <barsread.h>
00034 #include <barsc.h>
00035 #include <ContentListingFactory.h>
00036 #include <MCLFContentListingEngine.h>
00037 #include <MCLFItemListModel.h>
00038 #include <MCLFModifiableItem.h>
00039 #include <MCLFSortingStyle.h>
00040 #include <CLFContentListing.hrh>
00041 #include <CLFExample.rsg>
00042 
00043 // ============================ MEMBER FUNCTIONS ===============================
00044 
00045 // -----------------------------------------------------------------------------
00046 // CCEMusicView::CCEMusicView
00047 // C++ default constructor can NOT contain any code, that
00048 // might leave.
00049 // -----------------------------------------------------------------------------
00050 //
00051 CCEMusicView::CCEMusicView( MCLFContentListingEngine* aEngine,
00052                             MCLFItemListModel* aModel,
00053                             CCEOperationObserver* aObserver )
00054     : iEngine( aEngine ),
00055       iModel( aModel ),
00056       iObserver( aObserver )
00057     {
00058     }
00059 
00060 // -----------------------------------------------------------------------------
00061 // CCEMusicView::ConstructL
00062 // Symbian 2nd phase constructor can leave.
00063 // -----------------------------------------------------------------------------
00064 //
00065 void CCEMusicView::ConstructL()
00066     {
00067     BaseConstructL( R_CE_MUSIC_VIEW );
00068 
00069     // Load no-tag-info list item from the resource. It will be used for
00070     // items missing artist or title tag info (file name is displayed).
00071     iNoTagInfoItem = StringLoader::LoadL( R_CE_NO_TAG_INFO_LIST_ITEM );
00072 
00073     // Create sound player for playing audio files
00074     iSoundPlayer = CCESoundPlayer::NewL();
00075 
00076     // Create a wait note
00077     iWaitNote = CCEWaitNote::NewL();
00078     }
00079 
00080 // -----------------------------------------------------------------------------
00081 // CCEMusicView::NewL
00082 // Two-phased constructor.
00083 // -----------------------------------------------------------------------------
00084 //
00085 CCEMusicView* CCEMusicView::NewL( MCLFContentListingEngine* aEngine,
00086                                   MCLFItemListModel* aModel,
00087                                   CCEOperationObserver* aObserver )
00088     {
00089     CCEMusicView* self = CCEMusicView::NewLC( aEngine, aModel, aObserver );
00090     CleanupStack::Pop( self );
00091     return self;
00092     }
00093 
00094 // -----------------------------------------------------------------------------
00095 // CCEMusicView::NewLC
00096 // Two-phased constructor, pointer is left to the CleanupStack.
00097 // -----------------------------------------------------------------------------
00098 //
00099 CCEMusicView* CCEMusicView::NewLC( MCLFContentListingEngine* aEngine,
00100                                    MCLFItemListModel* aModel,
00101                                    CCEOperationObserver* aObserver )
00102     {
00103     CCEMusicView* self =
00104         new (ELeave) CCEMusicView( aEngine, aModel, aObserver );
00105     CleanupStack::PushL( self );
00106     self->ConstructL();
00107     return self;
00108     }
00109 
00110 // Destructor
00111 CCEMusicView::~CCEMusicView()
00112     {
00113     if( iObserver )
00114         {
00115         iObserver->RemoveMusicView();
00116         }
00117     if( iContainer )
00118         {
00119         AppUi()->RemoveFromStack( iContainer );
00120         delete iContainer;
00121         }
00122 
00123     delete iWaitNote;
00124     delete iSecondarySortingStyle;
00125     delete iSortingStyle;
00126     delete iCustomSorter;
00127     delete iSoundPlayer;
00128     delete iNoTagInfoItem;
00129     }
00130 
00131 // -----------------------------------------------------------------------------
00132 // CCEMusicView::HandleCommandL
00133 // Handle user's command.
00134 // -----------------------------------------------------------------------------
00135 //
00136 void CCEMusicView::HandleCommandL( TInt aCommand )
00137     {
00138     switch( aCommand )
00139         {
00140         case ECECmdSelect:
00141             {
00142             // Start playback if the player is not playing
00143             if( iSoundPlayer->PlayerState() != ECEPlaying )
00144                 {
00145                 HandleCommandL( ECECmdPlay );
00146                 }
00147 
00148             // Stop current playback if the player is playing.
00149             // After stopping, play the file that is currently selected.
00150             else if( iSoundPlayer->PlayerState() == ECEPlaying )
00151                 {
00152                 HandleCommandL( ECECmdStop );
00153                 }
00154 
00155             break;
00156             }
00157 
00158         case ECECmdPlay:
00159             {
00160             // Which sound file is selected
00161             TInt selectedFile( iContainer->CurrentItemIndex() );
00162 
00163             // Check that there was song files in the list
00164             if( selectedFile >= 0 )
00165                 {
00166                 // Get path and file name of the sound file
00167                 TPtrC fileNameAndPath;
00168                 TInt error( iModel->Item( selectedFile ).GetField(
00169                     ECLFFieldIdFileNameAndPath, fileNameAndPath ) );
00170                 User::LeaveIfError( error );
00171 
00172                 // Opens the file and starts playback
00173                 iSoundPlayer->StartPlaybackL( fileNameAndPath );
00174                 }
00175             break;
00176             }
00177 
00178         case ECECmdStop:
00179             {
00180             // Stop playback
00181             iSoundPlayer->StopPlayback();
00182             break;
00183             }
00184 
00185         case ECECmdSort:
00186             {
00187             // Set sorting options
00188             SetSortingL();
00189             break;
00190             }
00191 
00192         case ECECmdRename:
00193             {
00194             // Rename selected file
00195             TInt selectedFile( iContainer->CurrentItemIndex() );
00196             if( selectedFile >= 0 )
00197                 {
00198                 RenameMusicFileL( iContainer->CurrentItemIndex() );
00199                 }
00200             break;
00201             }
00202 
00203         case EAknSoftkeyBack:
00204             {
00205             // Remove the changed item observer
00206             if( iObserver )
00207                 {
00208                 iObserver->RemoveMusicView();
00209                 }
00210 
00211             AppUi()->ActivateLocalViewL( KCEGroupViewId );
00212             break;
00213             }
00214 
00215         case EAknCmdExit:
00216             {
00217             // Remove the changed item observer
00218             if( iObserver )
00219                 {
00220                 iObserver->RemoveMusicView();
00221                 }
00222 
00223             // FALLTHROUGH
00224             }
00225 
00226 
00227         default:
00228             {
00229             AppUi()->HandleCommandL( aCommand );
00230             break;
00231             }
00232         }
00233     }
00234 
00235 // -----------------------------------------------------------------------------
00236 // CCEMusicView::Id
00237 // Get Id of the view.
00238 // -----------------------------------------------------------------------------
00239 //
00240 TUid CCEMusicView::Id() const
00241     {
00242     return KCEMusicViewId;
00243     }
00244 
00245 // -----------------------------------------------------------------------------
00246 // CCEMusicView::DoActivateL
00247 // Activate the view.
00248 // -----------------------------------------------------------------------------
00249 //
00250 void CCEMusicView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
00251                                      TUid /*aCustomMessageId*/,
00252                                const TDesC8& /*aCustomMessage*/)
00253     {
00254     // Create the container for Music view
00255     if( !iContainer )
00256         {
00257         iContainer = new (ELeave) CCEMusicContainer( this );
00258         iContainer->SetMopParent( this );
00259         iContainer->ConstructL();
00260         }
00261 
00262     AppUi()->AddToViewStackL( *this, iContainer );
00263 
00264     // Generate and populate the list box
00265     PopulateListBoxL();
00266 
00267     iObserver->SetMusicView( this );
00268     }
00269 
00270 // -----------------------------------------------------------------------------
00271 // CCEMusicView::DoDeactivate
00272 // Deactivate the view.
00273 // -----------------------------------------------------------------------------
00274 //
00275 void CCEMusicView::DoDeactivate()
00276     {
00277     if( iContainer )
00278         {
00279         AppUi()->RemoveFromStack( iContainer );
00280         }
00281 
00282     delete iContainer;
00283     iContainer = NULL;
00284     }
00285 
00286 // -----------------------------------------------------------------------------
00287 // CCEMusicView::HandleClientRectChange
00288 // Handle change of rect.
00289 // -----------------------------------------------------------------------------
00290 //
00291 void CCEMusicView::HandleClientRectChange()
00292     {
00293     if( iContainer )
00294         {
00295         iContainer->SetRect( iAvkonAppUi->ClientRect() );
00296         }
00297     }
00298 
00299 // -----------------------------------------------------------------------------
00300 // CCEMusicView::DynInitMenuPaneL
00301 // Dim items of the menu pane.
00302 // -----------------------------------------------------------------------------
00303 //
00304 void CCEMusicView::DynInitMenuPaneL( TInt aResourceId,
00305                                      CEikMenuPane* aMenuPane )
00306         {
00307         // Dim items from the options-menu
00308         if( aResourceId == R_CE_MUSIC_VIEW_MENUPANE )
00309                 {
00310         // Dim "stop" if the player is not playing
00311                 if( iSoundPlayer->PlayerState() != ECEPlaying )
00312                         {
00313                         aMenuPane->SetItemDimmed( ECECmdStop, ETrue );
00314                         }
00315 
00316                 // Dim "play" if the player is playing, or if there are
00317                 // no music files in the list box
00318                 else if( iSoundPlayer->PlayerState() == ECEPlaying )
00319                         {
00320                         aMenuPane->SetItemDimmed( ECECmdPlay, ETrue );
00321                         }
00322 
00323             // Dim "play", "sort" and "rename" if there are
00324             // no music files in the list box
00325             if ( iContainer->CurrentItemIndex() < 0 )
00326                 {
00327                         aMenuPane->SetItemDimmed( ECECmdPlay, ETrue );
00328                         aMenuPane->SetItemDimmed( ECECmdSort, ETrue );
00329                         aMenuPane->SetItemDimmed( ECECmdRename, ETrue );
00330                 }
00331                 }
00332         }
00333 
00334 // -----------------------------------------------------------------------------
00335 // CCEMusicView::ItemsChangedL
00336 // Changed item observer will call this function when some item has changed.
00337 // -----------------------------------------------------------------------------
00338 //
00339 void CCEMusicView::ItemsChangedL()
00340     {
00341     // Refresh the model and wait for refresh to complete while displaying
00342     // a wait note. After refreshing, re-generate the list box.
00343     iObserver->PrepareForRefresh( iWait );
00344 
00345     iWaitNote->OpenWaitNoteL( R_CE_WAIT_NOTE );
00346 
00347     iModel->RefreshL();
00348 
00349     iWait.Start();
00350     PopulateListBoxL();
00351 
00352     iWaitNote->CloseWaitNoteL();
00353     }
00354 
00355 // -----------------------------------------------------------------------------
00356 // CCEMusicView::PopulateListBoxL
00357 // List all files from the model to the list box.
00358 // -----------------------------------------------------------------------------
00359 //
00360 void CCEMusicView::PopulateListBoxL()
00361     {
00362     // Reset the list box
00363     iContainer->ResetListBoxL();
00364 
00365     // Add items from model to the list box
00366     TInt numberOfItems( iModel->ItemCount() );
00367     for( TInt i = 0; i < numberOfItems; ++i )
00368         {
00369         // Get the item
00370         const MCLFItem& myItem = iModel->Item( i );
00371 
00372         // Get title, artist and album tag info of the item
00373         TPtrC songTitle;
00374         TInt errorTitle( myItem.GetField( ECLFFieldIdSongName, songTitle ) );
00375         TPtrC songArtist;
00376         TInt errorArtist( myItem.GetField( ECLFFieldIdArtist, songArtist ) );
00377         TPtrC songAlbum;
00378         TInt errorAlbum( myItem.GetField( ECLFFieldIdAlbum, songAlbum ) );
00379 
00380         // Create text array for creating the list box item
00381         CDesCArray* textItems =
00382             new (ELeave) CDesCArraySeg( KCEArrayGranularity );
00383         CleanupStack::PushL( textItems );
00384 
00385         // If title and artist tags were found
00386         if( errorTitle == KErrNone && errorArtist == KErrNone )
00387             {
00388             textItems->AppendL( songTitle );
00389             textItems->AppendL( songArtist );
00390 
00391             // If an album tag was found too
00392             if( errorAlbum == KErrNone )
00393                 {
00394                 textItems->AppendL( songAlbum );
00395                 }
00396             }
00397 
00398         // If title or artist tag is not found, the file name is used
00399         else
00400             {
00401             TPtrC songFileName;
00402             TInt errorFileName( iModel->Item( i ).GetField(
00403                 ECLFFieldIdFileName, songFileName ) );
00404             User::LeaveIfError( errorFileName );
00405 
00406             textItems->AppendL( songFileName );
00407             textItems->AppendL( *iNoTagInfoItem );
00408             }
00409 
00410         // Add item to the list box
00411         iContainer->AppendListBoxItemL( textItems );
00412 
00413         CleanupStack::PopAndDestroy( textItems );
00414         }
00415 
00416     // Select the topmost item of the list box to current
00417     iContainer->SetCurrentItemIndex( 0 );
00418     }
00419 
00420 // -----------------------------------------------------------------------------
00421 // CCEMusicView::RenameMusicFileL
00422 // Renames music file.
00423 // -----------------------------------------------------------------------------
00424 //
00425 void CCEMusicView::RenameMusicFileL( const TInt aFileIndex )
00426     {
00427     // Get file name of the sound file
00428     TPtrC fileName;
00429     TInt errorName( iModel->Item( aFileIndex ).GetField(
00430         ECLFFieldIdFileNameAndPath, fileName ) );
00431     User::LeaveIfError( errorName );
00432 
00433     // Query new file name
00434     TFileName newFileName = fileName;
00435     CAknTextQueryDialog* dlg = CAknTextQueryDialog::NewL( newFileName );
00436 
00437     if( dlg->ExecuteLD( R_CE_RENAME_QUERY ) )
00438         {
00439         // Open connection to the file system and rename the file
00440         RFs fs;
00441         User::LeaveIfError( fs.Connect() );
00442         CleanupClosePushL( fs );
00443         User::LeaveIfError( fs.Rename( fileName, newFileName ) );
00444         CleanupStack::PopAndDestroy(); // fs
00445 
00446         // Request the content listing engine to refresh its file listing.
00447         // Calling method UpdateItemsL after renaming a file, will cause
00448         // changed item event to be generated.
00449         iEngine->UpdateItemsL();
00450         }
00451     }
00452 
00453 // -----------------------------------------------------------------------------
00454 // CCEMusicView::SetSortingL
00455 // Set sorting options.
00456 // -----------------------------------------------------------------------------
00457 //
00458 void CCEMusicView::SetSortingL()
00459     {
00460     // Remove old sorting styles and custom sorter if any
00461     iModel->SetSortingStyle( NULL );
00462     iModel->SetCustomSorter( NULL );
00463 
00464     delete iSortingStyle;
00465     iSortingStyle = NULL;
00466 
00467     delete iSecondarySortingStyle;
00468     iSecondarySortingStyle = NULL;
00469 
00470     delete iCustomSorter;
00471     iCustomSorter = NULL;
00472 
00473     // Create new sorting style for default sorting
00474     iSortingStyle = ContentListingFactory::NewSortingStyleLC();
00475     CleanupStack::Pop(); // iSortingStyle
00476 
00477     // Query for default sorting parameter
00478     TInt sortBy( 0 );
00479     CAknListQueryDialog* dlg;
00480     dlg = new (ELeave) CAknListQueryDialog( &sortBy );
00481     if( dlg->ExecuteLD( R_CE_DEFAULT_SORTING_PARAM_QUERY ) )
00482         {
00483         // Set default sorting parameter
00484         switch( sortBy )
00485             {
00486             case ECESortByAlbum:
00487                 {
00488                 // Sort items by album name
00489                 iSortingStyle->AddFieldL( ECLFFieldIdAlbum );
00490 
00491                 // You can add multiple parameters for sorting style:
00492                 // In this case, if the item does not have ECLFFieldIdAlbum
00493                 // then ECLFFieldIdArtist is used to sort the item
00494                 iSortingStyle->AddFieldL( ECLFFieldIdArtist );
00495 
00496                 // Fields are string type
00497                 iSortingStyle->SetSortingDataType( ECLFItemDataTypeDesC );
00498                 break;
00499                 }
00500 
00501             case ECESortByArtist:
00502                 {
00503                 // Sort items by artist name
00504                 iSortingStyle->AddFieldL( ECLFFieldIdArtist );
00505 
00506                 // Field is string type
00507                 iSortingStyle->SetSortingDataType( ECLFItemDataTypeDesC );
00508                 break;
00509                 }
00510 
00511             case ECESortByTitle:
00512                 {
00513                 // Sort items by song title
00514                 iSortingStyle->AddFieldL( ECLFFieldIdSongName );
00515 
00516                 // Field is string type
00517                 iSortingStyle->SetSortingDataType( ECLFItemDataTypeDesC );
00518                 break;
00519                 }
00520 
00521             case ECESortByGenre:
00522                 {
00523                 // Sort items by genre
00524                 iSortingStyle->AddFieldL( ECLFFieldIdGenre );
00525 
00526                 // Field is string type
00527                 iSortingStyle->SetSortingDataType( ECLFItemDataTypeDesC );
00528                 break;
00529                 }
00530 
00531             case ECESortByFileDate:
00532                 {
00533                 // Sort items by file date
00534                 iSortingStyle->AddFieldL( ECLFFieldIdFileDate );
00535 
00536                 // Field is TTime type
00537                 iSortingStyle->SetSortingDataType( ECLFItemDataTypeTTime );
00538                 break;
00539                 }
00540 
00541             case ECESortByRandom:
00542                 {
00543                 // Create a custom sorter and set it to the model.
00544                 // This custom sorter will randomize the list model.
00545                 iCustomSorter = CCECustomSorter::NewL();
00546                 iModel->SetCustomSorter( iCustomSorter );
00547 
00548                 // Refresh the model, wait for the refresh to complete
00549                 // and re-generate the list box
00550                 iModel->RefreshL( ECLFRefreshSorting | ECLFRefreshPostFilter );
00551                 PopulateListBoxL();
00552                 return;
00553                 }
00554 
00555             default:
00556                 {
00557                 return;
00558                 }
00559             }
00560         }
00561     else
00562         {
00563         return;
00564         }
00565 
00566     // Query for sorting order
00567     TInt sortingOrder( 0 );
00568     dlg = new (ELeave) CAknListQueryDialog( &sortingOrder );
00569     if( dlg->ExecuteLD( R_CE_ORDERING_SORTING_PARAM_QUERY ) )
00570         {
00571         // Set sorting order
00572         switch( sortingOrder )
00573             {
00574             case ECESortingOrderAscending:
00575                 {
00576                 iSortingStyle->SetOrdering( ECLFOrderingAscending );
00577                 break;
00578                 }
00579 
00580             case ECESortingOrderDescending:
00581                 {
00582                 iSortingStyle->SetOrdering( ECLFOrderingDescending );
00583                 break;
00584                 }
00585 
00586             default:
00587                 {
00588                 return;
00589                 }
00590             }
00591         }
00592     else
00593         {
00594         return;
00595         }
00596 
00597     // Query how to position items that have no tag info
00598     // for default sorting parameter.
00599     TInt sortPosition( 0 );
00600     dlg = new (ELeave) CAknListQueryDialog( &sortPosition );
00601     if( dlg->ExecuteLD( R_CE_POSITION_SORTING_PARAM_QUERY ) )
00602         {
00603         switch( sortPosition )
00604             {
00605             // Items are sent to the end of the list
00606             case ECESortPositionBottom:
00607                 {
00608                 iSortingStyle->SetUndefinedItemPosition(
00609                     ECLFSortingStyleUndefinedEnd );
00610                 break;
00611                 }
00612 
00613             // Items are sent to the top of the list
00614             case ECESortPositionTop:
00615                 {
00616                 iSortingStyle->SetUndefinedItemPosition(
00617                     ECLFSortingStyleUndefinedFirst );
00618                 break;
00619                 }
00620 
00621             default:
00622                 {
00623                 return;
00624                 }
00625             }
00626         }
00627     else
00628         {
00629         return;
00630         }
00631 
00632     // Set the primary sorting style to the model
00633     iModel->SetSortingStyle( iSortingStyle );
00634 
00635     // Sorting styles can be created from the resource too:
00636     // Create a resource reader for creating the secondary sorting style
00637     // from the resource
00638     TResourceReader resourceReader;
00639     iCoeEnv->CreateResourceReaderLC(
00640         resourceReader, R_CE_SECONDARY_SORTING_STYLE );
00641     iSecondarySortingStyle =
00642         ContentListingFactory::NewSortingStyleLC( resourceReader );
00643     CleanupStack::Pop(); // iSecondarySortingStyle
00644 
00645     // Set the secondary sorting style to the model:
00646     // After sorting music files by default sorting parameter/parameters,
00647     // sort remaining files; files that did not have any of those
00648     // parameters. In this case, files that do not have any of the primary
00649     // sorting parameters, are sorted by file name.
00650     // You can add multiple secondary sorting styles too.
00651     iModel->AppendSecondarySortingStyleL( *iSecondarySortingStyle );
00652 
00653     // Refresh the model and re-generate the list box
00654     iModel->RefreshL( ECLFRefreshSorting | ECLFRefreshPostFilter );
00655     PopulateListBoxL();
00656 
00657     CleanupStack::PopAndDestroy(); // resourceReader
00658     }
00659 
00660 //  End of File

© Nokia 2009

Back to top