upnpframework/upnputilities/src/upnpmetadatafetcher.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  UPnP metadata fetching utilities.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 // System
       
    21 #include <e32std.h>
       
    22 #include <e32base.h>
       
    23 
       
    24 // upnp stack api
       
    25 #include <upnpdevice.h>
       
    26 #include <upnpcontainer.h>
       
    27 #include <upnpitem.h>
       
    28 #include <upnpelement.h>
       
    29 #include <upnpstring.h>
       
    30 
       
    31 // upnpframework / avcontroller helper api
       
    32 #include <upnpconstantdefs.h>
       
    33 
       
    34 // utilities internal
       
    35 #include "upnpmetadatautility.h"
       
    36 #include "upnpcommonutils.h" 
       
    37 #include "upnpmetadatafetcher.h"
       
    38 #include "upnpcdsreselementutility.h"
       
    39 
       
    40 
       
    41 // CONSTANTS
       
    42 _LIT8( KMimeAudio,  "audio" );
       
    43 _LIT8( KMimeVideo,  "video" );
       
    44 _LIT8( KMimeImage,  "image" );
       
    45 _LIT8( KMimeApplication,    "application" );
       
    46 
       
    47 _LIT8( KItemCreator,    "None" );
       
    48 _LIT8( KRealMediaVideo,     "application/x-pn-realmedia" );
       
    49 
       
    50 // --------------------------------------------------------------------------
       
    51 // UPnPMetadataFetcher::FetchMetadataL
       
    52 // Resolves the metadata and fills in the CUpnpObject's metadata fields 
       
    53 // accordingly.
       
    54 // --------------------------------------------------------------------------
       
    55 //
       
    56 EXPORT_C void UPnPMetadataFetcher::FetchMetadataL( CUpnpObject& aObject, 
       
    57                                                    const TDesC& aFilePath, 
       
    58                                                    const TDesC8& aMimeType )
       
    59     {
       
    60     CUPnPMetaDataUtility* utility = CUPnPMetaDataUtility::NewL();
       
    61     CleanupStack::PushL( utility );
       
    62 
       
    63     // APa: Continue even if the loading of meta data fails
       
    64     // (in S60 3.2 this leaves)
       
    65     TRAP_IGNORE( utility->LoadMetaDataL( aFilePath ) );
       
    66 
       
    67     // Get the title from metadata is available, if not, use the filename
       
    68     HBufC8* tempBuf = NULL;
       
    69     if( utility->Title().Length() > 0 )
       
    70         {
       
    71         tempBuf = UpnpString::FromUnicodeL( utility->Title() );
       
    72         CleanupStack::PushL( tempBuf );
       
    73         aObject.SetTitleL( *tempBuf );
       
    74         CleanupStack::PopAndDestroy( tempBuf ); tempBuf = NULL;
       
    75         }
       
    76     else
       
    77         {
       
    78         // Remove the path and file extension from the filename
       
    79         TInt lastDot = 0;
       
    80         TInt lastSlash = 0;
       
    81         TInt length = 0;
       
    82         lastDot = aFilePath.LocateReverseF('.');
       
    83         if( KErrNotFound == lastDot ) //if file extension is not found
       
    84             {
       
    85             lastDot = aFilePath.Length();
       
    86             }
       
    87         lastSlash = aFilePath.LocateReverseF('\\');
       
    88         length = lastDot - lastSlash;
       
    89         HBufC* strippedFilename = NULL;
       
    90         strippedFilename = aFilePath.Mid((lastSlash+1), (length-1)).AllocL();
       
    91         CleanupStack::PushL( strippedFilename );
       
    92 
       
    93         // Convert to 8bit, and set the UpnpObject's title value
       
    94         tempBuf = UpnpString::FromUnicodeL( *strippedFilename );
       
    95         CleanupStack::PushL( tempBuf );
       
    96         aObject.SetTitleL( *tempBuf );
       
    97 
       
    98         // Clean up
       
    99         CleanupStack::PopAndDestroy( tempBuf ); 
       
   100         tempBuf = NULL;
       
   101         CleanupStack::PopAndDestroy( strippedFilename );
       
   102         strippedFilename = NULL;
       
   103         }
       
   104 
       
   105     const RUPnPElementsArray& elms = aObject.GetElements();
       
   106     TInt count = elms.Count();
       
   107     TInt i;
       
   108     TBool found;
       
   109     
       
   110     if( aMimeType.Find( KMimeAudio ) >= 0 )
       
   111         {
       
   112         // Set object class
       
   113         aObject.SetObjectClassL( KClassAudioMusicTrack );
       
   114         
       
   115         // Artist
       
   116         // Try to find the artist element and update it if found
       
   117         tempBuf = UpnpString::FromUnicodeL( utility->Artist() );
       
   118         if( tempBuf )
       
   119             {
       
   120             CleanupStack::PushL( tempBuf );
       
   121             found = EFalse;
       
   122             for( i = 0; i < count; i++)
       
   123                 {
       
   124                 if( elms[ i ]->Name() == KElementArtist )
       
   125                     {
       
   126                     elms[ i ]->SetValueL( *tempBuf );
       
   127                     found = ETrue;
       
   128                     i = count;
       
   129                     }
       
   130                 }
       
   131             if( !found )
       
   132                 {
       
   133                 // Not found, create a new artist element and add it to the 
       
   134                 // item
       
   135                 CUpnpElement* element = CUpnpElement::NewLC( KElementArtist );
       
   136                 element->SetValueL( *tempBuf );
       
   137                 aObject.AddElementL( element );
       
   138                 CleanupStack::Pop( element );
       
   139                 }
       
   140             CleanupStack::PopAndDestroy( tempBuf ); tempBuf = NULL;    
       
   141                 
       
   142             }
       
   143         
       
   144         // Genre
       
   145         // Try to find the genre element and update it if found
       
   146         tempBuf = UpnpString::FromUnicodeL( utility->Genre() );
       
   147         if( tempBuf )
       
   148             {
       
   149             CleanupStack::PushL( tempBuf );
       
   150             found = EFalse;
       
   151             for( i = 0; i < count; i++)
       
   152                 {
       
   153                 if( elms[ i ]->Name() == KElementGenre )
       
   154                     {
       
   155                     elms[ i ]->SetValueL( *tempBuf );
       
   156                     found = ETrue;
       
   157                     i = count;
       
   158                     }
       
   159                 }
       
   160             if( !found )
       
   161                 {
       
   162                 // Not found, create a new genre element and add it to the 
       
   163                 // item
       
   164                 CUpnpElement* element = CUpnpElement::NewLC( KElementGenre );
       
   165                 element->SetValueL( *tempBuf );
       
   166                 aObject.AddElementL( element );
       
   167                 CleanupStack::Pop( element );
       
   168                 }
       
   169             CleanupStack::PopAndDestroy( tempBuf ); tempBuf = NULL;
       
   170             }
       
   171         
       
   172         // Album
       
   173         // Try to find the album element and update it if found
       
   174         tempBuf = UpnpString::FromUnicodeL( utility->Album() );
       
   175         if( tempBuf )
       
   176             {
       
   177             CleanupStack::PushL( tempBuf );
       
   178             found = EFalse;
       
   179             for( i = 0; i < count; i++)
       
   180                 {
       
   181                 if( elms[ i ]->Name() == KElementAlbum )
       
   182                     {
       
   183                     elms[ i ]->SetValueL( *tempBuf );
       
   184                     found = ETrue;
       
   185                     i = count;
       
   186                     }
       
   187                 }
       
   188             if( !found )
       
   189                 {
       
   190                 // Not found, create a new album element and add it to the 
       
   191                 // item
       
   192                 CUpnpElement* element = CUpnpElement::NewLC( KElementAlbum );
       
   193                 element->SetValueL( *tempBuf );
       
   194                 aObject.AddElementL( element );
       
   195                 CleanupStack::Pop( element );
       
   196                 }
       
   197             CleanupStack::PopAndDestroy( tempBuf ); tempBuf = NULL;
       
   198             }
       
   199         }
       
   200     else if( aMimeType.Find( KMimeVideo ) >= 0 )
       
   201         {
       
   202         // Set Object class
       
   203         aObject.SetObjectClassL( KClassVideo );
       
   204         }
       
   205     else if( aMimeType.Find( KMimeImage ) >= 0 )
       
   206         {
       
   207         // Set Object class
       
   208         aObject.SetObjectClassL( KClassImage );
       
   209         }
       
   210     else if( aMimeType.Find( KMimeApplication ) >= 0 )
       
   211         {
       
   212         // some special cases here
       
   213         // Fixes EAHN-7ETBH7
       
   214         if( aMimeType.Find( KRealMediaVideo ) >= 0 )
       
   215             {
       
   216             aObject.SetObjectClassL( KClassVideo );
       
   217             }
       
   218         }
       
   219     else
       
   220         {
       
   221         // Do nothing
       
   222         }
       
   223 
       
   224     // Date
       
   225     // Try to find the date element and update it if found
       
   226     tempBuf = UpnpString::FromUnicodeL( utility->Date() );
       
   227     if( tempBuf )
       
   228         {
       
   229         CleanupStack::PushL( tempBuf );
       
   230         found = EFalse;
       
   231         for( i = 0; i < count; i++)
       
   232             {
       
   233             if( elms[ i ]->Name() == KElementDate )
       
   234                 {
       
   235                 elms[ i ]->SetValueL( *tempBuf );
       
   236                 found = ETrue;
       
   237                 i = count;
       
   238                 }
       
   239             }
       
   240         if( !found )
       
   241             {
       
   242             // Not found, create a new date element and add it to the item
       
   243             CUpnpElement* element = CUpnpElement::NewLC( KElementDate );
       
   244             element->SetValueL( *tempBuf );
       
   245             aObject.AddElementL( element );
       
   246             CleanupStack::Pop( element );
       
   247             }
       
   248         CleanupStack::PopAndDestroy( tempBuf ); tempBuf = NULL;
       
   249         }       
       
   250                          
       
   251     CUpnpElement* element = CUpnpElement::NewLC( KElementCreator );
       
   252     element->SetValueL( KItemCreator );
       
   253     aObject.AddElementL( element );
       
   254     CleanupStack::Pop( element );  
       
   255     // Clean up
       
   256     CleanupStack::PopAndDestroy( utility );
       
   257     utility = NULL;
       
   258     }
       
   259 
       
   260 // --------------------------------------------------------------------------
       
   261 // UPnPMetadataFetcher::FetchMetadataL
       
   262 // Resolves the metadata and fills in the CUpnpObject's metadata fields 
       
   263 // accordingly.
       
   264 // --------------------------------------------------------------------------
       
   265 //
       
   266 EXPORT_C void UPnPMetadataFetcher::FetchMetadataL( CUpnpObject& aObject, 
       
   267                                                    const TDesC& aFilePath )
       
   268     {
       
   269     // Resolve the MIME type of the file
       
   270     HBufC8* mimeType = NULL;
       
   271     mimeType = UPnPCommonUtils::ResolveMimeTypeL( aFilePath );
       
   272     if( mimeType )
       
   273         {    
       
   274         CleanupStack::PushL( mimeType );
       
   275         // Fill in the metadata
       
   276         FetchMetadataL( aObject, aFilePath, *mimeType );
       
   277         // Clean up
       
   278         CleanupStack::PopAndDestroy( mimeType );
       
   279         mimeType = NULL;
       
   280         }        
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // UPnPMetadataFetcher::CreateItemFromFileLC
       
   285 // Creates a new CUpnpItem from a local file. Resolves the metadata and fills
       
   286 // in the new CUpnpItem's metadata fields accordingly.
       
   287 // --------------------------------------------------------------------------
       
   288 //
       
   289 EXPORT_C CUpnpItem* UPnPMetadataFetcher::CreateItemFromFileLC( 
       
   290                                                     const TDesC& aFilePath )
       
   291     {
       
   292     CUpnpItem* item = NULL;
       
   293     item = CUpnpItem::NewL();
       
   294     CleanupStack::PushL( item );
       
   295 
       
   296     // Add a res-element to the item
       
   297     UpnpCdsResElementUtility::AddResElementL( *item, aFilePath );
       
   298     // Resolve the metadata
       
   299     FetchMetadataL( *item, aFilePath );
       
   300     
       
   301     return item;
       
   302     }
       
   303 
       
   304 // --------------------------------------------------------------------------
       
   305 // UPnPMetadataFetcher::CreateItemFromFileL
       
   306 // Creates a new CUpnpItem from a local file. Resolves the metadata and fills
       
   307 // in the new CUpnpItem's metadata fields accordingly.
       
   308 // --------------------------------------------------------------------------
       
   309 //
       
   310 EXPORT_C CUpnpItem* UPnPMetadataFetcher::CreateItemFromFileL(
       
   311                                                     const TDesC& aFilePath )
       
   312     {
       
   313     CUpnpItem* item = CreateItemFromFileLC( aFilePath );
       
   314     CleanupStack::Pop( item );
       
   315     return item;
       
   316     }
       
   317 
       
   318 // End of File