harvester/harvesterplugins/AudioPlaylistPlugin/src/harvesterm3uplaylistparser.cpp
branchRCL_3
changeset 21 85f623e1ef41
parent 20 f23c07ec56e2
child 22 29d87345eaeb
equal deleted inserted replaced
20:f23c07ec56e2 21:85f623e1ef41
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <bautils.h>
       
    21 #include <syslangutil.h>
       
    22 #include <PathInfo.h>
       
    23 #include <data_caging_path_literals.hrh>
       
    24 #include <mdsplaylisttopcharacterset.rsg>
       
    25 
       
    26 #include "harvesterm3uplaylistparser.h"
       
    27 
       
    28 #include "mdsutils.h"
       
    29 #include "harvesterlog.h"
       
    30 
       
    31 _LIT( KMDSM3ULineChange, "\n" );
       
    32 _LIT( KMDSM3UTagExtm3u, "#EXTM3U" );
       
    33 _LIT (KMDSM3UTagExtinf, "#EXTINF:" );
       
    34 _LIT( KMDSM3UPoint, ",");
       
    35 _LIT( KMDSM3UTagExt, "#");
       
    36 _LIT( KMDSM3UAbsPath, ":\\");
       
    37 
       
    38 const TInt KMDSM3UCarriageReturn = 13;
       
    39 const TInt KMDSM3UNoOffset = 0;
       
    40 const TInt KPlaylistSampleLength = 10000; 
       
    41 const TUint KUnicodeBOM = 0xFEFF;
       
    42 const TInt KPlaylistMaxSampleLength = 130000;
       
    43 const TInt KMinimumConfidenceRequired = 75;
       
    44 const TInt KMDSM3UPlaylistMaxItemCount = KMaxTInt;
       
    45 const TInt KPathStartingChars = 3;
       
    46 
       
    47 // MODULE DATA STRUCTURES
       
    48 enum TMDSM3UPlaylistLineType
       
    49     {
       
    50     EMDSM3UPlaylistLineTypeExtinf = 1,
       
    51     EMDSM3UPlaylistLineTypePath = 2,
       
    52     EMDSM3UPlaylistLineTypeNotSupported = 3,
       
    53     EMDSM3UPlaylistLineTypeCorrupted = 4
       
    54     };
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CHarvesterM3UPlaylistParser::NewL
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CHarvesterM3UPlaylistParser* CHarvesterM3UPlaylistParser::NewL( RFs& aFs,
       
    61         CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* aAvailableCharacterSet,
       
    62         CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* aTopCharacterSet )
       
    63     {
       
    64     CHarvesterM3UPlaylistParser* self = new ( ELeave ) CHarvesterM3UPlaylistParser(
       
    65             aFs, aAvailableCharacterSet, aTopCharacterSet );
       
    66 
       
    67 	return self;
       
    68     }
       
    69 
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // Destructor
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CHarvesterM3UPlaylistParser::~CHarvesterM3UPlaylistParser()
       
    76     {
       
    77     Reset();
       
    78     }
       
    79 
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CHarvesterM3UPlaylistParser::CHarvesterM3UPlaylistParser
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CHarvesterM3UPlaylistParser::CHarvesterM3UPlaylistParser( RFs& aFs,
       
    86         CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* aAvailableCharacterSet,
       
    87         CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* aTopCharacterSet ) 
       
    88     :iFs( aFs ), iAvailableCharacterSet( aAvailableCharacterSet ),
       
    89      iTopCharacterSet( aTopCharacterSet ), iEndLineNumber( KMDSM3UPlaylistMaxItemCount )
       
    90     {
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CHarvesterM3UPlaylistParser::ParseL
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 TBool CHarvesterM3UPlaylistParser::ParseL( const TDesC& aFileName,
       
    98 		RPointerArray<HBufC>& aUriArray )
       
    99     {
       
   100     iPlaylistFilePath.Set( aFileName );
       
   101     ReadPlaylistFileToBufferL();
       
   102     ParsePlaylistBufferL( aUriArray, iInvalidItems );
       
   103     
       
   104     // If at the moment, we know that there is at least one error parsing
       
   105     // with auto detect encoding, we don't need to proceed until end of
       
   106     // file anymore, this playlist file is concluded to be corrupted
       
   107     if ( iInvalidItems > 0 )
       
   108         {
       
   109         aUriArray.Reset();
       
   110         Reset();
       
   111         User::Leave( KErrCorrupt );
       
   112         }
       
   113     
       
   114     return ETrue;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CHarvesterM3UPlaylistParser::ResetL
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 void CHarvesterM3UPlaylistParser::Reset()
       
   122     {
       
   123     delete iBuffer;
       
   124     iBuffer = NULL;
       
   125     delete iLine;
       
   126     iLine = NULL;
       
   127     iBufferPtr.Set( KNullDesC );
       
   128     iCurrentLineNumber = 0;
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CHarvesterM3UPlaylistParser::ReadPlaylistFileToBufferL
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CHarvesterM3UPlaylistParser::ReadPlaylistFileToBufferL()
       
   136     {
       
   137 #ifdef _DEBUG
       
   138     WRITELOG1( "Before reading playlist to buffer: heap size = %d", User::Heap().Size() );
       
   139 #endif
       
   140     
       
   141     TEntry entry;
       
   142     User::LeaveIfError( iFs.Entry( iPlaylistFilePath, entry ) );
       
   143     
       
   144     HBufC* buffer = HBufC::NewLC( entry.iSize );
       
   145     TPtr ptr = buffer->Des();
       
   146 
       
   147     HBufC8* buf8 = HBufC8::NewLC( entry.iSize );
       
   148     TPtr8 ptr8 = buf8->Des();
       
   149 
       
   150     // Read the first KPlaylistSampleLength bytes of the file
       
   151     TInt sampleLength( KPlaylistSampleLength );
       
   152     if( sampleLength > entry.iSize )
       
   153         {
       
   154         sampleLength = entry.iSize;
       
   155         }
       
   156     User::LeaveIfError( iFs.ReadFileSection(
       
   157                             iPlaylistFilePath, 0, ptr8, sampleLength ) );
       
   158 
       
   159     // auto detect character encoding
       
   160     TUint charSetId( 0 );
       
   161     TInt error = DetectCharacterSetL( *buf8, *iTopCharacterSet, charSetId );
       
   162     WRITELOG2("Encoding detected using top character set is 0x%x, error %d", charSetId, error);
       
   163     
       
   164     // when we fail to detect the encoding, use all available character set in the
       
   165     // system to try again. If that also fails, abandon the operation.
       
   166     if ( error )
       
   167         {
       
   168         User::LeaveIfError( DetectCharacterSetL( *buf8, *iAvailableCharacterSet, charSetId ) );
       
   169         WRITELOG1( "Encoding detected using available character set is 0x%x", charSetId );     
       
   170         }
       
   171 
       
   172     // read the whole file if the sample taken isn't the whole file
       
   173     if ( sampleLength != entry.iSize )
       
   174         {
       
   175         User::LeaveIfError( iFs.ReadFileSection(
       
   176                                 iPlaylistFilePath, 0, ptr8, entry.iSize) );
       
   177         }
       
   178    
       
   179     // perform character conversion using the selected encoding
       
   180     TInt state( CCnvCharacterSetConverter::KStateDefault );
       
   181     TInt numOfUnconvertibleChars( 0 );
       
   182     CCnvCharacterSetConverter* charSetConv = CCnvCharacterSetConverter::NewLC();
       
   183     charSetConv->PrepareToConvertToOrFromL( charSetId, *iAvailableCharacterSet, iFs );
       
   184     TInt retVal = charSetConv->ConvertToUnicode( ptr, *buf8, state, numOfUnconvertibleChars );
       
   185     User::LeaveIfError( retVal );
       
   186 
       
   187     // try again if the character set wasn't detected using the whole file
       
   188     if( (retVal > 0 || numOfUnconvertibleChars > 0) && (sampleLength != entry.iSize) )
       
   189         {
       
   190         WRITELOG3( "retVal = %d, numOfUnconvertibleChars = %d, entry.iSize = %d",
       
   191                 retVal, numOfUnconvertibleChars, entry.iSize );  
       
   192         numOfUnconvertibleChars = 0;
       
   193         retVal = 0;
       
   194         User::LeaveIfError( DetectCharacterSetL( *buf8, *iAvailableCharacterSet, charSetId ) );
       
   195         charSetConv->PrepareToConvertToOrFromL( charSetId, *iAvailableCharacterSet, iFs );
       
   196         retVal = charSetConv->ConvertToUnicode( ptr, *buf8, state, numOfUnconvertibleChars );
       
   197         }
       
   198         
       
   199     if ( retVal > 0 || numOfUnconvertibleChars > 0 )
       
   200         {
       
   201         WRITELOG2( "Unable to find character encoding for the playlist file. retVal = %d, numOfUnconvertibleChars = %d",
       
   202                     retVal, numOfUnconvertibleChars );     
       
   203         User::Leave( KErrNotSupported );
       
   204         }
       
   205     
       
   206     // remove the byte order mark (BOM) character prepended at the beginning
       
   207     // of the stream if encoded with unicode as per Unicode section 2.4
       
   208     if ( (charSetId == KCharacterSetIdentifierUnicodeLittle ||
       
   209          charSetId == KCharacterSetIdentifierUnicodeBig) &&
       
   210         ptr.Length() > 0 &&
       
   211         ptr[0] == KUnicodeBOM )
       
   212         {
       
   213         ptr.Delete( 0,1 );
       
   214         }
       
   215         
       
   216     iBuffer = buffer;
       
   217     iBufferPtr.Set( *iBuffer );
       
   218     
       
   219     CleanupStack::PopAndDestroy (2, buf8 ); // charSetConv & buf8    
       
   220     CleanupStack::Pop( buffer );
       
   221         
       
   222     // brand new buffer which hasn't been read, reset iCurrentLineNumber and
       
   223     // iEndLineNumber
       
   224     iCurrentLineNumber = 0;
       
   225 
       
   226 #ifdef _DEBUG
       
   227     WRITELOG1( "After reading playlist to buffer: heap size = %d", User::Heap().Size() );
       
   228 #endif
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CHarvesterM3UPlaylistParser::DetectCharacterSetL
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 TInt CHarvesterM3UPlaylistParser::DetectCharacterSetL(
       
   236             const TDesC8& aSample,
       
   237             const CArrayFix<CCnvCharacterSetConverter::SCharacterSet>& aCharacterSet,
       
   238             TUint& aCharSetId)
       
   239     {
       
   240     // CCnvCharacterSetConverter::ConvertibleToCharSetL hangs if sample is too big
       
   241     if ( aSample.Size() > KPlaylistMaxSampleLength )
       
   242         {
       
   243         User::Leave( KErrNotSupported );
       
   244         }
       
   245         
       
   246     TInt confidence( 0 );
       
   247     TInt highestConfidence( 0 );
       
   248     TUint charSetId( 0 );
       
   249     TUint highestConfidencecharSetId( 0 );
       
   250 
       
   251     CCnvCharacterSetConverter* charSetConv = CCnvCharacterSetConverter::NewLC();
       
   252     TInt count = aCharacterSet.Count();
       
   253     for ( TInt i=0; i < count; i++ )
       
   254         {
       
   255         charSetId = aCharacterSet.At(i).Identifier();
       
   256         charSetConv->ConvertibleToCharSetL( confidence, charSetId, aCharacterSet, aSample );
       
   257         if ( confidence > highestConfidence )
       
   258             {
       
   259             highestConfidence = confidence;
       
   260             highestConfidencecharSetId = charSetId;
       
   261             }
       
   262         }
       
   263     CleanupStack::PopAndDestroy( charSetConv );
       
   264     WRITELOG2( "CMPXM3uPlaylistImporter::DetectCharacterSetL :-> Confidence[%d] CharSetId[0x%x]",
       
   265             confidence, aCharSetId );
       
   266     if ( highestConfidence == 0 || highestConfidence < KMinimumConfidenceRequired )
       
   267         {
       
   268         return KErrNotFound;
       
   269         }
       
   270     else
       
   271         {
       
   272         aCharSetId = highestConfidencecharSetId;
       
   273         return KErrNone;
       
   274         }
       
   275     }
       
   276     
       
   277 // -----------------------------------------------------------------------------
       
   278 // CHarvesterM3UPlaylistParser::ParsePlaylistBufferL
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 void CHarvesterM3UPlaylistParser::ParsePlaylistBufferL(
       
   282 			RPointerArray<HBufC>& aPlaylist,
       
   283             TInt& aInvalidItemCount)
       
   284     {
       
   285     // Read and process all the lines in the file
       
   286     //
       
   287     // the order of the following conditions is important. ReadNextLineL
       
   288     // should be called last to avoid skipping one line
       
   289     while ( iCurrentLineNumber < iEndLineNumber &&
       
   290                aPlaylist.Count() < KMDSM3UPlaylistMaxItemCount &&
       
   291                ReadNextLineL() )
       
   292         {
       
   293         ProcessLineL( aPlaylist, aInvalidItemCount );  
       
   294         }
       
   295 
       
   296     if ( aPlaylist.Count() == KMDSM3UPlaylistMaxItemCount )
       
   297         {
       
   298         Reset();
       
   299         User::Leave( KErrOverflow );
       
   300         }
       
   301     }
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CHarvesterM3UPlaylistParser::ReadNextLineL
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 TBool CHarvesterM3UPlaylistParser::ReadNextLineL()
       
   308     {
       
   309     // iBuffer should exist when this function is called
       
   310     __ASSERT_DEBUG( iBuffer, User::Leave( KErrBadDescriptor ) );
       
   311 
       
   312     if ( !iBufferPtr.Length() )
       
   313         {
       
   314         return EFalse;
       
   315         }
       
   316 
       
   317     delete iLine;
       
   318     iLine = NULL;
       
   319 
       
   320     // Try to find line change
       
   321     TInt offset = iBufferPtr.FindF( KMDSM3ULineChange );
       
   322   
       
   323     if( offset == KErrNotFound )
       
   324         {
       
   325         // No line change was found --> last line had no line change
       
   326         iLine = iBufferPtr.AllocL();
       
   327         // Set iBufferPtr to the end of buffer
       
   328         iBufferPtr.Set( iBufferPtr.Right(0) );
       
   329         }
       
   330     else
       
   331         {
       
   332         // Found line change
       
   333         TInt length( offset );
       
   334         if ( (offset > KMDSM3UNoOffset) && 
       
   335             (iBufferPtr[length - 1] == KMDSM3UCarriageReturn) )
       
   336             {
       
   337             --length;
       
   338             }
       
   339 
       
   340         iLine = iBufferPtr.Left(length).AllocL();
       
   341 
       
   342         // Move past the line feed
       
   343         iBufferPtr.Set( iBufferPtr.Mid(++offset) );
       
   344         }
       
   345 
       
   346     // Remove leading and trailing space characters from iLine's data.
       
   347     TPtr ptr = iLine->Des();
       
   348     ptr.Trim();
       
   349 
       
   350     iCurrentLineNumber++;
       
   351     return ETrue;
       
   352     }
       
   353     
       
   354 // -----------------------------------------------------------------------------
       
   355 // CHarvesterM3UPlaylistParser::ProcessLineL
       
   356 // -----------------------------------------------------------------------------
       
   357 //
       
   358 void CHarvesterM3UPlaylistParser::ProcessLineL(
       
   359 			RPointerArray<HBufC>& aPlaylist,
       
   360             TInt& aInvalidItemCount)
       
   361     {
       
   362     if ( iCurrentLineNumber == 1 ) // first line
       
   363         {
       
   364         // Check whether the file is in the extented format
       
   365         TInt offset = iLine->Find( KMDSM3UTagExtm3u );
       
   366         if( offset == KErrNotFound || offset != KMDSM3UNoOffset ||
       
   367             iLine->Length() != KMDSM3UTagExtm3u().Length() )
       
   368             {
       
   369             // The file is not in the extented format
       
   370             iExtendedFormat = EFalse;
       
   371             }
       
   372         else
       
   373             {
       
   374             // The file is in the extented format
       
   375             iExtendedFormat = ETrue;
       
   376             return;
       
   377             }        
       
   378         }
       
   379     
       
   380     // Parse line and then decide what to do with it
       
   381     switch( ParseLineL( iItem, aInvalidItemCount ) )
       
   382         {
       
   383         case EMDSM3UPlaylistLineTypeExtinf:
       
   384             // Continue to next round
       
   385             break;
       
   386             
       
   387         case EMDSM3UPlaylistLineTypePath:
       
   388             {
       
   389             // Line was a path => add item to playlist
       
   390             aPlaylist.AppendL( iItem.AllocL() );
       
   391             }
       
   392             break; 
       
   393                        
       
   394         case EMDSM3UPlaylistLineTypeNotSupported:
       
   395         case EMDSM3UPlaylistLineTypeCorrupted:
       
   396         default:
       
   397             {
       
   398             iItem = KNullDesC;
       
   399             }
       
   400             break;
       
   401         }
       
   402     }
       
   403     
       
   404 // -----------------------------------------------------------------------------
       
   405 // CHarvesterM3UPlaylistParser::ParseLineL
       
   406 // -----------------------------------------------------------------------------
       
   407 //
       
   408 TInt CHarvesterM3UPlaylistParser::ParseLineL(
       
   409             TFileName& aItem,
       
   410             TInt& aInvalidItemCount)
       
   411     {
       
   412     __ASSERT_DEBUG( iLine, User::Leave(KErrAbort) );
       
   413 
       
   414     if( !iLine->Length() )
       
   415         {
       
   416         // Empty line => line is invalid
       
   417         return EMDSM3UPlaylistLineTypeNotSupported;
       
   418         }
       
   419 
       
   420     if( iExtendedFormat )
       
   421         {
       
   422         // File is in the extented format => check whether there is extented
       
   423         // info in this line.
       
   424         TInt offset = iLine->Find( KMDSM3UTagExtinf );
       
   425         if( offset != KErrNotFound && offset == KMDSM3UNoOffset )
       
   426             {
       
   427             offset = iLine->Find( KMDSM3UPoint );
       
   428 
       
   429             if( offset != KErrNotFound )
       
   430                 {
       
   431                 return EMDSM3UPlaylistLineTypeExtinf; // line type extinf
       
   432                 }    
       
   433             }
       
   434         }
       
   435 
       
   436     // File is not in the extented format or supported info not found from this
       
   437     // line.
       
   438     switch( iLine->Find(KMDSM3UTagExt) )
       
   439         {
       
   440         case KMDSM3UNoOffset:
       
   441             // Unsupported extended info tag found from this line
       
   442             return EMDSM3UPlaylistLineTypeNotSupported;
       
   443             
       
   444         case KErrNotFound:
       
   445         default:
       
   446             // Extended info not found from the beginning of line => line is
       
   447             // a path.
       
   448             {
       
   449             // Get absolute path
       
   450             TInt error( KErrNone );
       
   451             HBufC* uri = ParseAbsolutePathLC( *iLine, error );
       
   452         
       
   453             if( error )
       
   454                 {
       
   455                 CleanupStack::PopAndDestroy( uri );
       
   456                 ++aInvalidItemCount;
       
   457                 return EMDSM3UPlaylistLineTypeCorrupted;
       
   458                 }
       
   459 
       
   460             aItem = uri->Des();
       
   461 
       
   462             CleanupStack::PopAndDestroy( uri );
       
   463             
       
   464             return EMDSM3UPlaylistLineTypePath; // line type path
       
   465             }
       
   466         }
       
   467     }
       
   468 
       
   469 // -----------------------------------------------------------------------------
       
   470 // CHarvesterM3UPlaylistParser::ParseAbsolutePathLC
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 HBufC* CHarvesterM3UPlaylistParser::ParseAbsolutePathLC(
       
   474             const TDesC& aPath,
       
   475             TInt& aError)
       
   476     {
       
   477     HBufC* path = NULL;
       
   478     
       
   479     TBool isAbsolute( EFalse );
       
   480     
       
   481     if( aPath.Length() > KPathStartingChars && 
       
   482         !aPath.Mid(1, 2).CompareF( KMDSM3UAbsPath ) ) // magic: the 2nd and 3rd chars
       
   483                                                // are always ":\"
       
   484                                                // for absolute paths
       
   485         {
       
   486         isAbsolute = ETrue;
       
   487         }
       
   488 
       
   489     if( aPath.Length() > KMaxFileName ) // Test if path is too long
       
   490         {
       
   491         aError = KErrCorrupt;
       
   492         }
       
   493     else if( isAbsolute )
       
   494         {
       
   495         aError = KErrNone;
       
   496         aError = iFs.IsValidName( aPath ) ? KErrNone : KErrBadName;
       
   497         path = aPath.AllocLC();
       
   498         }
       
   499     else
       
   500         {
       
   501         // Given path could be relative => create absolute path and test it
       
   502         // Playlist file path
       
   503         TParse playlistPath;
       
   504         playlistPath.Set( iPlaylistFilePath, NULL, NULL );
       
   505         // Path to the folder, where playlist file is located to
       
   506         TPtrC currentFolder = playlistPath.DriveAndPath();
       
   507         // Create absolute path
       
   508         path = HBufC::NewLC( currentFolder.Length() + aPath.Length() );
       
   509 
       
   510         TPtr tmpPtr( path->Des() );
       
   511         tmpPtr = currentFolder;
       
   512         tmpPtr += aPath;
       
   513 
       
   514         aError = iFs.IsValidName(*path) ? KErrNone : KErrBadName;
       
   515         }
       
   516     
       
   517     // It is possible that a song exists in the filesystem but isn't added to
       
   518     // the database because it's not a supported type. If such song is included
       
   519     // in a playlist, it will be added to the database when the playlist is added.
       
   520     // Because of this, we cannot rely on whether the song exists in the database
       
   521     // to conclude whether the song is a broken link. We need to check for file
       
   522     // existence here. For the unsupported songs included in the playlist, they
       
   523     // will then be marked as corrupted when user initiates playback of those
       
   524     // songs.
       
   525     if ( !aError &&
       
   526          !BaflUtils::FileExists(iFs, *path) )
       
   527         {
       
   528         aError = KErrPathNotFound;
       
   529         }
       
   530 
       
   531     return path;
       
   532     }
       
   533 
       
   534 // End of file