Nokia N97 SDK
Example Applications Guide

CCEGroupView.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : CCEGroupView.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 "CCEGroupView.h"
00019 
00020 #include "CCEAppUi.h"
00021 #include "CCEOperationObserver.h"
00022 #include "CCEGroupContainer.h"
00023 #include "CCECustomGrouper.h"
00024 #include "CCEPostFilter.h"
00025 #include "CCEWaitNote.h"
00026 #include "CLFExample.hrh"
00027 #include "CLFExampleConsts.h"
00028 
00029 #include <akntitle.h>
00030 #include <stringloader.h>
00031 #include <aknnotewrappers.h>
00032 #include <ContentListingFactory.h>
00033 #include <MCLFItem.h>
00034 #include <MCLFItemListModel.h>
00035 #include <CLFContentListing.hrh>
00036 #include <CLFExample.rsg>
00037 
00038 // ============================ MEMBER FUNCTIONS ===============================
00039 
00040 // -----------------------------------------------------------------------------
00041 // CCEGroupView::CCEGroupView
00042 // C++ default constructor can NOT contain any code, that
00043 // might leave.
00044 // -----------------------------------------------------------------------------
00045 //
00046 CCEGroupView::CCEGroupView( MCLFContentListingEngine* aEngine,
00047                             MCLFItemListModel* aModel,
00048                             CCEOperationObserver* aObserver )
00049     : iEngine( aEngine ),
00050       iModel( aModel ),
00051       iObserver( aObserver ),
00052       iGrouping( ECENoGrouping )
00053     {
00054     }
00055 
00056 // -----------------------------------------------------------------------------
00057 // CCEGroupView::ConstructL
00058 // Symbian 2nd phase constructor can leave.
00059 // -----------------------------------------------------------------------------
00060 //
00061 void CCEGroupView::ConstructL()
00062     {
00063     BaseConstructL( R_CE_GROUP_VIEW );
00064 
00065     // Create a wait note
00066     iWaitNote = CCEWaitNote::NewL();
00067     }
00068 
00069 // -----------------------------------------------------------------------------
00070 // CCEGroupView::NewL
00071 // Two-phased constructor.
00072 // -----------------------------------------------------------------------------
00073 //
00074 CCEGroupView* CCEGroupView::NewL( MCLFContentListingEngine* aEngine,
00075                                   MCLFItemListModel* aModel,
00076                                   CCEOperationObserver* aObserver )
00077     {
00078     CCEGroupView* self =
00079         CCEGroupView::NewLC( aEngine, aModel, aObserver );
00080     CleanupStack::Pop( self );
00081     return self;
00082     }
00083 
00084 // -----------------------------------------------------------------------------
00085 // CCEGroupView::NewLC
00086 // Two-phased constructor, pointer is left to the CleanupStack.
00087 // -----------------------------------------------------------------------------
00088 //
00089 CCEGroupView* CCEGroupView::NewLC( MCLFContentListingEngine* aEngine,
00090                                    MCLFItemListModel* aModel,
00091                                    CCEOperationObserver* aObserver )
00092     {
00093     CCEGroupView* self =
00094         new (ELeave) CCEGroupView( aEngine, aModel, aObserver );
00095     CleanupStack::PushL( self );
00096     self->ConstructL();
00097     return self;
00098     }
00099 
00100 // Destructor
00101 CCEGroupView::~CCEGroupView()
00102     {
00103     delete iWaitNote;
00104     delete iArtistGrouper;
00105     delete iPostFilter;
00106 
00107     if( iContainer )
00108         {
00109         AppUi()->RemoveFromStack( iContainer );
00110         delete iContainer;
00111         }
00112     }
00113 
00114 // -----------------------------------------------------------------------------
00115 // CCEGroupView::HandleCommandL
00116 // Handle user's commands.
00117 // -----------------------------------------------------------------------------
00118 //
00119 void CCEGroupView::HandleCommandL( TInt aCommand )
00120     {
00121     // Diplay a wait note if user tries to do something while refresh operation
00122     // of the list model has not finished yet.
00123     if( iObserver->IsRefreshOngoing() )
00124         {
00125         // View the wait note
00126         iWaitNote->OpenWaitNoteL( R_CE_WAIT_NOTE );
00127 
00128         // Wait for the refresh operation to complete. Operation Observer will
00129         // stop this wait when the refresh operation has compeleted.
00130         iWait.Start();
00131 
00132         // Close the wait note
00133         iWaitNote->CloseWaitNoteL();
00134         }
00135 
00136     switch( aCommand )
00137         {
00138         case ECECmdGroupByAllMusicFiles:
00139             {
00140             // Reset grouping state
00141             iGrouping = ECENoGrouping;
00142 
00143             // Group items in the list model
00144             GroupListItemsL( ECEGroupedByAll );
00145 
00146             // Reset the list box
00147             iContainer->ResetListBoxL();
00148 
00149             if( iModel->ItemCount() <= 0 )
00150                 {
00151                 NoMusicFilesFoundL();
00152                 break;
00153                 }
00154 
00155             // Load "all files" list item from the resource and add it to
00156             // the list box if there are music files in the model
00157             HBufC* defaultAllFilesItem = StringLoader::LoadLC(
00158                 R_CE_GROUP_ALL_FILES_ITEM );
00159             iContainer->AppendListBoxItemL( *defaultAllFilesItem );
00160             CleanupStack::PopAndDestroy( defaultAllFilesItem );
00161 
00162             // Focus the topmost item of the list box
00163             iContainer->SetCurrentItemIndex( 0 );
00164             break;
00165             }
00166 
00167         case ECECmdGroupByAlbum:
00168             {
00169             // Reset the grouping state
00170             iGrouping = ECENoGrouping;
00171 
00172             // Group items in the list model
00173             GroupListItemsL( ECEGroupedByAlbum );
00174 
00175             if( iModel->ItemCount() <= 0 )
00176                 {
00177                 NoMusicFilesFoundL();
00178                 }
00179 
00180             // Re-generate the list box with album names
00181             PopulateListBoxL( ECLFFieldIdAlbum );
00182 
00183             // Focus the topmost item of the list box
00184             iContainer->SetCurrentItemIndex( 0 );
00185             break;
00186             }
00187 
00188         case ECECmdGroupByArtist:
00189             {
00190             // Reset the grouping state
00191             iGrouping = ECENoGrouping;
00192 
00193             // Group items in the list model
00194             GroupListItemsL( ECEGroupedByArtist );
00195 
00196             // Remove custom grouper from the model
00197             iModel->SetCustomGrouper( NULL );
00198 
00199             if( iModel->ItemCount() <= 0 )
00200                 {
00201                 NoMusicFilesFoundL();
00202                 }
00203 
00204             // Re-generate the list box with artist names
00205             PopulateListBoxL( ECLFFieldIdArtist );
00206 
00207             // Select the topmost item from the list box
00208             iContainer->SetCurrentItemIndex( 0 );
00209             break;
00210             }
00211 
00212         case ECECmdSelect:
00213         case ECECmdOpen:
00214             {
00215             // Prepare to get all music files to the model
00216             iModel->SetGroupingStyle( ECLFNoGrouping );
00217             iModel->SetCustomGrouper( NULL );
00218             iModel->SetPostFilter( NULL );
00219 
00220             // Delete old post filter if any
00221             if( iPostFilter )
00222                 {
00223                 delete iPostFilter;
00224                 iPostFilter = NULL;
00225                 }
00226 
00227             // Check the grouping state of the application
00228             // The list is grouped by ..
00229             // .. all files
00230             if( iGrouping == ECEGroupedByAll )
00231                 {
00232                 // Do nothing, because the model has been prepared to get
00233                 // all music files on the refresh.
00234                 }
00235 
00236             // .. albums
00237             else if( iGrouping == ECEGroupedByAlbum )
00238                 {
00239                 // Which album is selected
00240                 TInt selectedAlbum( iContainer->CurrentItemIndex() );
00241 
00242                 // Get name of the album
00243                 TPtrC nameOfAlbum;
00244                 TInt error( iModel->Item( selectedAlbum ).GetField(
00245                     ECLFFieldIdAlbum, nameOfAlbum ) );
00246                 User::LeaveIfError( error );
00247 
00248                 // Create and activate a post filter for album filtering
00249                 // so that the model will contain only sound files
00250                 // from selected album
00251                 iPostFilter =
00252                     CCEPostFilter::NewL( ECLFFieldIdAlbum, nameOfAlbum );
00253                 iModel->SetPostFilter( iPostFilter );
00254                 }
00255 
00256             // .. artists
00257             else if( iGrouping == ECEGroupedByArtist )
00258                 {
00259                 // Which artist is selected
00260                 TInt selectedArtist( iContainer->CurrentItemIndex() );
00261 
00262                 // Get name of the artist
00263                 TPtrC nameOfArtist;
00264                 TInt error( iModel->Item( selectedArtist ).GetField(
00265                     ECLFFieldIdArtist, nameOfArtist ) );
00266                 User::LeaveIfError( error );
00267 
00268                 // Create and activate a post filter for album filtering
00269                 // so that the model will contain only sound files
00270                 // from selected artist
00271                 iPostFilter =
00272                     CCEPostFilter::NewL( ECLFFieldIdArtist, nameOfArtist );
00273                 iModel->SetPostFilter( iPostFilter );
00274                 }
00275 
00276             // .. files have not been grouped yet
00277             else
00278                 {
00279                 return;
00280                 }
00281 
00282             // Refresh the model and activate the music files view
00283             iModel->RefreshL( ECLFRefreshAll );
00284             AppUi()->ActivateLocalViewL( KCEMusicViewId );
00285             break;
00286             }
00287 
00288         default:
00289             {
00290             AppUi()->HandleCommandL( aCommand );
00291             break;
00292             }
00293         }
00294     }
00295 
00296 // -----------------------------------------------------------------------------
00297 // CCEGroupView::Id
00298 // Get Id of the view.
00299 // -----------------------------------------------------------------------------
00300 //
00301 TUid CCEGroupView::Id() const
00302     {
00303     return KCEGroupViewId;
00304     }
00305 
00306 // -----------------------------------------------------------------------------
00307 // CCEGroupView::DoActivateL
00308 // Activate the view.
00309 // -----------------------------------------------------------------------------
00310 //
00311 void CCEGroupView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
00312                                      TUid /*aCustomMessageId*/,
00313                                const TDesC8& /*aCustomMessage*/)
00314     {
00315     // Create a container and refresh the list model for Group view in the
00316     // first activate
00317     if( !iContainer )
00318         {
00319         iContainer = new (ELeave) CCEGroupContainer( this );
00320         iContainer->SetMopParent( this );
00321         iContainer->ConstructL();
00322 
00323         // Send reference of CActiveSchedulerWait to the CLF Operation Observer
00324         iObserver->PrepareForRefresh( iWait );
00325 
00326         // Call refresh to get all music files from the file system to the model
00327         iModel->RefreshL();
00328 
00329                 // At this point, other initialization operations can be done as the
00330                 // list model refresh is an asychronous operation.
00331         }
00332 
00333     AppUi()->AddToViewStackL( *this, iContainer );
00334 
00335     // Group files, if they were grouped when the view was deactivated
00336     if( iGrouping != ECENoGrouping )
00337         {
00338         GroupListItemsL( iGrouping );
00339         }
00340     }
00341 
00342 // -----------------------------------------------------------------------------
00343 // CCEGroupView::DoDeactivate
00344 // Deactivate the view.
00345 // -----------------------------------------------------------------------------
00346 //
00347 void CCEGroupView::DoDeactivate()
00348     {
00349     if( iContainer )
00350         {
00351         AppUi()->RemoveFromStack( iContainer );
00352         }
00353     }
00354 
00355 // -----------------------------------------------------------------------------
00356 // CCEGroupView::HandleClientRectChange
00357 // Handle change of the client rect.
00358 // -----------------------------------------------------------------------------
00359 //
00360 void CCEGroupView::HandleClientRectChange()
00361     {
00362     if( iContainer )
00363         {
00364         iContainer->SetRect( iAvkonAppUi->ClientRect() );
00365         }
00366     }
00367 
00368 // -----------------------------------------------------------------------------
00369 // CCEGroupView::DynInitMenuPaneL
00370 // Dim items of the menu pane.
00371 // -----------------------------------------------------------------------------
00372 //
00373 void CCEGroupView::DynInitMenuPaneL( TInt aResourceId,
00374                                      CEikMenuPane* aMenuPane )
00375         {
00376         // Dim open-option from the options-menu
00377         // if there are no items in the list box
00378         if( aResourceId == R_CE_GROUP_VIEW_MENUPANE )
00379                 {
00380                 if( iContainer->CurrentItemIndex() < 0 )
00381                         {
00382                         aMenuPane->SetItemDimmed( ECECmdOpen, ETrue );
00383                         }
00384                 }
00385         }
00386 
00387 // -----------------------------------------------------------------------------
00388 // CCEGroupView::GroupListItemsL
00389 // Group items in the list model if they were grouped when the view
00390 // was deactivated.
00391 // -----------------------------------------------------------------------------
00392 //
00393 void CCEGroupView::GroupListItemsL( const TCEGrouping aGroupingState )
00394     {
00395     // Remove existing post filter and sorting style from the model
00396     iModel->SetPostFilter( NULL );
00397     iModel->SetSortingStyle( NULL );
00398 
00399     switch( aGroupingState )
00400         {
00401         case ECENoGrouping:
00402             {
00403             break;
00404             }
00405 
00406         case ECEGroupedByAll:
00407             {
00408             // Remove grouping
00409             iModel->SetGroupingStyle( ECLFNoGrouping );
00410             break;
00411             }
00412 
00413         case ECEGroupedByAlbum:
00414             {
00415             // Set album grouping
00416             iModel->SetGroupingStyle( ECLFMusicAlbumGrouping );
00417             break;
00418             }
00419 
00420         case ECEGroupedByArtist:
00421             {
00422             // Delete old and create new custom grouper for artist grouping
00423             if( iArtistGrouper )
00424                 {
00425                 delete iArtistGrouper;
00426                 iArtistGrouper = NULL;
00427                 }
00428 
00429             iArtistGrouper = CCECustomGrouper::NewL( ECLFFieldIdArtist );
00430             iModel->SetCustomGrouper( iArtistGrouper );
00431             break;
00432             }
00433         }
00434 
00435     // Refresh the model
00436     iModel->RefreshL( ECLFRefreshAll );
00437 
00438     // Set new grouping state if items were found
00439     if( iModel->ItemCount() > 0 )
00440         {
00441         iGrouping = aGroupingState;
00442         }
00443     else
00444         {
00445         iGrouping = ECENoGrouping;
00446         }
00447     }
00448 
00449 // -----------------------------------------------------------------------------
00450 // CCEGroupView::NoMusicFilesFoundL
00451 // Show user a warning note that no music files were found.
00452 // -----------------------------------------------------------------------------
00453 //
00454 void CCEGroupView::NoMusicFilesFoundL()
00455     {
00456     HBufC* noteText = StringLoader::LoadLC( R_CE_NO_MUSIC_FILES_FOUND );
00457     CAknWarningNote* note = new (ELeave) CAknWarningNote;
00458     note->ExecuteLD( *noteText );
00459     CleanupStack::PopAndDestroy( noteText );
00460     }
00461 
00462 // -----------------------------------------------------------------------------
00463 // CCEGroupView::PopulateListBoxL
00464 // List all files from the model to the list box.
00465 // -----------------------------------------------------------------------------
00466 //
00467 void CCEGroupView::PopulateListBoxL( const TCLFDefaultFieldId aFieldId )
00468     {
00469     // Reset the list box
00470     iContainer->ResetListBoxL();
00471 
00472     // Add all items to the list box
00473     TInt countItems( iModel->ItemCount() );
00474     for( TInt i = 0; i < countItems; ++i )
00475         {
00476         // Get value of requested field and add item to the list box
00477         TPtrC fieldValue;
00478         TInt error( iModel->Item( i ).GetField( aFieldId, fieldValue ) );
00479 
00480         if( error == KErrNone )
00481             {
00482             iContainer->AppendListBoxItemL( fieldValue );
00483             }
00484         }
00485     }
00486 
00487 //  End of File

© Nokia 2009

Back to top