mmappcomponents/harvester/filehandler/src/mpxplaylistscanner.cpp
changeset 0 a2952bb97e68
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Class to handle playlist extraction
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <mpxplaylistengine.h>
       
    21 #include <mpxmedia.h>
       
    22 #include <mpxmediaarray.h>
       
    23 #include <mpxcollectiontype.h>
       
    24 #include <mpxmediageneraldefs.h>
       
    25 #include "mpxplaylistscanner.h"
       
    26 #include "mpxplaylistscanobserver.h"
       
    27 #include "mpxfilescanstateobserver.h"
       
    28 
       
    29 // CONSTANTS
       
    30 const TInt KExtractionCount = 2;
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // Default constructor
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CMPXPlaylistScanner::CMPXPlaylistScanner( MMPXPlaylistScanObserver& aObs,
       
    39                                           MMPXFileScanStateObserver& aStateObs,
       
    40                                           RPointerArray<CMPXCollectionType>& aTypes ) : 
       
    41                                           iObserver( aObs ),
       
    42                                           iStateObserver( aStateObs ),
       
    43                                           iSupportedTypes(aTypes)
       
    44     {
       
    45     }
       
    46 
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // 2nd phase constructor
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 void CMPXPlaylistScanner::ConstructL()
       
    53     {
       
    54     iPlEngine = CMPXPlaylistEngine::NewL( *this );
       
    55     iPlList = new(ELeave) CDesCArrayFlat(1); // granularity
       
    56     iPlUpdateList = new(ELeave) CDesCArrayFlat(1); // granularity
       
    57     
       
    58     iPropArray = CMPXMediaArray::NewL();
       
    59     iUpdatePropArray = CMPXMediaArray::NewL();
       
    60     }
       
    61 
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Two Phased Constructor
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CMPXPlaylistScanner* CMPXPlaylistScanner::NewL(
       
    68                                        MMPXPlaylistScanObserver&  aObs,
       
    69                                        MMPXFileScanStateObserver& aStateObs,
       
    70                                        RPointerArray<CMPXCollectionType>& aTypes )
       
    71     {
       
    72     CMPXPlaylistScanner* self = new(ELeave) CMPXPlaylistScanner( aObs,
       
    73                                                                  aStateObs,
       
    74                                                                  aTypes );
       
    75     CleanupStack::PushL( self );
       
    76     self->ConstructL();
       
    77     CleanupStack::Pop( self );
       
    78     return self;
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // Virtual destructor
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 CMPXPlaylistScanner::~CMPXPlaylistScanner()
       
    86     {
       
    87     if( iPlList )
       
    88         {
       
    89         iPlList->Reset();
       
    90         delete iPlList;
       
    91         }
       
    92     if( iPlUpdateList )
       
    93         {
       
    94         iPlUpdateList->Reset();
       
    95         delete iPlUpdateList;
       
    96         }
       
    97     
       
    98     if( iPropArray )
       
    99         {
       
   100         iPropArray->Reset();
       
   101         }
       
   102     delete iPropArray;
       
   103     
       
   104     if( iUpdatePropArray )
       
   105         {
       
   106         iUpdatePropArray->Reset();
       
   107         }
       
   108     delete iUpdatePropArray;
       
   109     
       
   110     if( iPlEngine )
       
   111         {
       
   112         iPlEngine->CancelRequests();     
       
   113         }
       
   114     delete iPlEngine;
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // Start the playlist scanning procedure
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 void CMPXPlaylistScanner::ScanL()
       
   122     {
       
   123     iNewPlPending = iPlList->Count();
       
   124     iUpdatePlPending = iPlUpdateList->Count();
       
   125     
       
   126     // Add all files in the list to the playlist engine queue.
       
   127     //
       
   128     if( iNewPlPending || iUpdatePlPending )
       
   129         {
       
   130         for( TInt i=0; i<iNewPlPending; ++i )
       
   131             {
       
   132             iPlEngine->InternalizePlaylistL( (*iPlList)[i] );
       
   133             }
       
   134         for( TInt i=0; i<iUpdatePlPending; ++i )
       
   135             {
       
   136             iPlEngine->InternalizePlaylistL( (*iPlUpdateList)[i] );
       
   137             }
       
   138         iExtracting = ETrue;
       
   139         }
       
   140     // No files to extract
       
   141     // 
       
   142     else
       
   143         {
       
   144         iStateObserver.HandleScanStateCompleteL( MMPXFileScanStateObserver::EScanPlaylists,
       
   145                                                   KErrNone );    
       
   146         }   
       
   147     }
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // Cancel the playlist scanning procedure
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 void CMPXPlaylistScanner::Cancel()
       
   154     {
       
   155     if( iExtracting )
       
   156         {
       
   157         iPlEngine->CancelRequests(); 
       
   158         iExtracting = EFalse;
       
   159         TRAP_IGNORE(iStateObserver.HandleScanStateCompleteL( MMPXFileScanStateObserver::EScanPlaylists, 
       
   160                                                              KErrCancel );  
       
   161                     );      
       
   162         }
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // Reset the object
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CMPXPlaylistScanner::Reset()
       
   170     {
       
   171     iPlList->Reset();
       
   172     iPlUpdateList->Reset();
       
   173     iPropArray->Reset();
       
   174     iUpdatePropArray->Reset();
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // Add a file to the list to be extracted
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 void CMPXPlaylistScanner::AddPlaylistToScanL( const TDesC& aFile )
       
   182     {
       
   183     iPlList->AppendL( aFile );
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // Add a file to the list to be updated
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CMPXPlaylistScanner::AddUpdatedPlaylistToScanL( const TDesC& aFile )
       
   191     {
       
   192     iPlUpdateList->AppendL( aFile );
       
   193     }
       
   194     
       
   195 // ---------------------------------------------------------------------------
       
   196 // Handles playlist import
       
   197 // Note: aCompleted is not in use for Increment 8, for future usage.
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 void CMPXPlaylistScanner::HandlePlaylistL( CMPXMedia* aPlaylist,
       
   201                                            const TInt aError,
       
   202                                            const TBool /*aCompleted*/ )
       
   203     {
       
   204     // Only add parsable playlists
       
   205     //     
       
   206     if( aError == KErrNone )
       
   207         {
       
   208         // We take ownership of the media object
       
   209         //
       
   210         CleanupStack::PushL( aPlaylist );
       
   211         
       
   212         // Find what collection this belongs to
       
   213         //
       
   214         const TDesC& file = aPlaylist->ValueText( 
       
   215                      TMPXAttribute( KMPXMediaIdGeneral, EMPXMediaGeneralUri));
       
   216         TParsePtrC parse( file );
       
   217         TInt index(KErrNotFound); 
       
   218         TInt count( iSupportedTypes.Count() );
       
   219         for (TInt i=0; i <count; ++i)
       
   220             {
       
   221             TInt index2(KErrNotFound);    
       
   222             const CDesCArray& exts = iSupportedTypes[i]->Extensions();
       
   223             if (!exts.FindIsq(parse.Ext(), index2))
       
   224                 { // found 
       
   225                 index = i;
       
   226                 break;
       
   227                 }
       
   228             }
       
   229         
       
   230         // Found?
       
   231         if( index == KErrNotFound )
       
   232             {
       
   233             // Unsupported by a collection
       
   234             //
       
   235             CleanupStack::PopAndDestroy( aPlaylist );      
       
   236             }
       
   237         else
       
   238             {
       
   239             // Set collection ID and add to array
       
   240             //
       
   241             aPlaylist->SetTObjectValueL( TMPXAttribute( KMPXMediaIdGeneral, 
       
   242                                              EMPXMediaGeneralCollectionId),
       
   243                                          iSupportedTypes[index]->Uid() );
       
   244             if( iNewPlPending )
       
   245                 {
       
   246                 iPropArray->AppendL( aPlaylist );  // owned by array
       
   247                 }
       
   248             else
       
   249                 {
       
   250                 iUpdatePropArray->AppendL( aPlaylist ); // owned by array
       
   251                 } 
       
   252             CleanupStack::Pop( aPlaylist );
       
   253             }
       
   254         }
       
   255     else // an error occured
       
   256         {
       
   257         // Playlist engine should have set this to NULL
       
   258         // (Just in case)
       
   259         delete aPlaylist; 
       
   260         }
       
   261         
       
   262     if( iNewPlPending )
       
   263         {
       
   264         iNewPlPending--;
       
   265         }
       
   266     else // Some update ones pending
       
   267         {
       
   268         iUpdatePlPending--;    
       
   269         }
       
   270         
       
   271     // All done!
       
   272     //
       
   273     TInt pending = iNewPlPending + iUpdatePlPending;  
       
   274     if( pending == 0 && iExtracting )
       
   275         {
       
   276         CompleteFilesL();
       
   277         iExtracting = EFalse;
       
   278         
       
   279         iStateObserver.HandleScanStateCompleteL( MMPXFileScanStateObserver::EScanPlaylists, 
       
   280                                                  KErrNone );
       
   281         }
       
   282     // Enough to send to the collection
       
   283     //
       
   284     else if( iPropArray->Count() + iUpdatePropArray->Count() 
       
   285              == KExtractionCount )
       
   286         {
       
   287         CompleteFilesL();
       
   288         }
       
   289     } //lint !e961
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // Handles Playlist export to a file
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 void CMPXPlaylistScanner::HandlePlaylistL(const TDesC& /*aPlaylistUri*/,
       
   296                                           const TInt /*aError*/)
       
   297     {
       
   298     // not used
       
   299     }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // Checks if a file is a playlist file 
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 TBool CMPXPlaylistScanner::IsPlaylistFileL( const TDesC& aFile )
       
   306     {
       
   307     return iPlEngine->IsPlaylistL( aFile );
       
   308     }
       
   309 
       
   310 // ---------------------------------------------------------------------------
       
   311 // Upload the next list of files
       
   312 // ---------------------------------------------------------------------------
       
   313 //
       
   314 void CMPXPlaylistScanner::CompleteFilesL()
       
   315     {
       
   316     TInt err(KErrNone);
       
   317     if( iPropArray->Count() )
       
   318         {
       
   319         TRAP( err, iObserver.AddPlaylistToCollectionL( *iPropArray ) );
       
   320         }
       
   321     if( iUpdatePropArray->Count() )
       
   322         {
       
   323         TRAP( err, iObserver.UpdatePlaylistToCollectionL( *iUpdatePropArray ) );
       
   324         }
       
   325         
       
   326     if( err != KErrNone )
       
   327         {
       
   328         // Cancel requests and notify end
       
   329         iStateObserver.HandleScanStateCompleteL( MMPXFileScanStateObserver::EScanPlaylists, 
       
   330                                                   err );        
       
   331         iPlEngine->CancelRequests();        
       
   332         }
       
   333     Reset();
       
   334     }
       
   335