upnpframework/upnputilities/src/upnpcdsreselementutility.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:  Utility class for meta data gathering for audio files
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 // System
       
    21 #include <e32std.h>
       
    22 #include <e32base.h>
       
    23 
       
    24 // upnp stack api
       
    25 #include <upnpitem.h> // CUpnpItem
       
    26 #include <upnpstring.h>
       
    27 #include <upnpelement.h>
       
    28 #include <upnpdlnaprotocolinfo.h>
       
    29 #include <upnpprotocolinfo.h>
       
    30 
       
    31 // upnpframework / avcontroller helper api
       
    32 #include <upnpconstantdefs.h> // for upnp class defs
       
    33 
       
    34 // upnpframework internal api's
       
    35 #include "upnpresresolver.h" // CUpnpResResolver
       
    36 #include "upnpresparameters.h"
       
    37 
       
    38 // utilities internal
       
    39 #include "upnpcommonutils.h"
       
    40 #include "upnpcdsreselementutility.h"
       
    41 
       
    42 _LIT( KHttp, "http://" );
       
    43 const TInt KColon = 58;
       
    44 const TInt KIpAddLen = 25;
       
    45 
       
    46 // ============================ MEMBER FUNCTIONS ============================
       
    47 
       
    48 // --------------------------------------------------------------------------
       
    49 // CUpnpCdsResElementUtility::AddResElementL
       
    50 // 
       
    51 // (other items were commented in a header).
       
    52 // --------------------------------------------------------------------------
       
    53 //
       
    54 EXPORT_C void UpnpCdsResElementUtility::AddResElementL( 
       
    55                                                 CUpnpItem& aItem, 
       
    56                                                 const TDesC& aFilename )
       
    57     {
       
    58 
       
    59     // Create CUpnpElement for res
       
    60     CUpnpElement* elRes = CUpnpElement::NewL( KElementRes() );
       
    61     CleanupStack::PushL( elRes );
       
    62     elRes->SetFilePathL( aFilename );
       
    63  
       
    64     // Create res resolver component and put it to CleanupStack
       
    65     CUpnpResResolver* resolver = CUpnpResResolver::NewLC();
       
    66     
       
    67     // Obtain res element parameters using res resolver component.
       
    68     CUpnpResParameters* resParameters = NULL;
       
    69 	TRAPD(err, resParameters = resolver->GetResParametersL( aFilename ) );
       
    70 
       
    71    if (err == KErrGeneral)	
       
    72     {
       
    73 	// File not recognized, handling it manually by adding
       
    74 	// following res-item:  'http-get:*:MIMETYPE:*'
       
    75 
       
    76    		_LIT8( KProtInfo1, "http-get:");
       
    77     	_LIT8( KStar8, "*");
       
    78     	_LIT8( KColon8, ":");
       
    79     	HBufC8* mimetype = UPnPCommonUtils::ResolveMimeTypeL(aFilename);
       
    80     	mimetype->Des().Trim();		// needs to be trimmed
       
    81 
       
    82         TInt resultLen = KProtInfo1().Length() + KStar8().Length() + 
       
    83     	KColon8().Length() + mimetype->Des().Length() + KColon8().Length() +
       
    84         KStar8().Length();
       
    85     	HBufC8* resValue = HBufC8::NewLC( resultLen );
       
    86 
       
    87     // construct final value
       
    88 	    resValue->Des().Append( KProtInfo1 );
       
    89 	    resValue->Des().Append( KStar8 );
       
    90 	    resValue->Des().Append( KColon8 );
       
    91 	    resValue->Des().Append( *mimetype );
       
    92 	    resValue->Des().Append( KColon8 );
       
    93 	    resValue->Des().Append( KStar8 );
       
    94 
       
    95 		CUpnpProtocolInfo* pInfo = CUpnpProtocolInfo::NewL(*resValue);
       
    96 		CUpnpAttribute* attribute = CUpnpAttribute::NewLC( KAttributeProtocolInfo() );
       
    97 		attribute->SetValueL( pInfo->ProtocolInfoL() );
       
    98 		elRes->AddAttributeL( attribute );    
       
    99     	aItem.AddElementL(elRes);
       
   100 
       
   101 		CleanupStack::Pop( attribute );
       
   102 		CleanupStack::PopAndDestroy( resValue );
       
   103 		CleanupStack::PopAndDestroy( resolver );
       
   104     	CleanupStack::Pop( elRes );
       
   105   		resValue = NULL;
       
   106     	mimetype = NULL;
       
   107     	
       
   108     	return;
       
   109     }
       
   110     // Fixes ESLX-7B7HBL
       
   111     else if( err )
       
   112         {
       
   113         User::Leave( err );
       
   114         }
       
   115 
       
   116     CleanupStack::PushL( resParameters );
       
   117 
       
   118     // Add protocolInfo parameter to res element.
       
   119     if ( resParameters->ProtocolInfo().Length() > 0 ) 
       
   120         {
       
   121         CUpnpAttribute* prInfo = CUpnpAttribute::NewLC( KAttributeProtocolInfo() );
       
   122         prInfo->SetValueL( resParameters->ProtocolInfo() );   
       
   123         elRes->AddAttributeL( prInfo ); //transfer ownership
       
   124         CleanupStack::Pop( prInfo );       
       
   125         }
       
   126 
       
   127     // Add size parameter to res element.
       
   128 
       
   129     HBufC8* sizeString = resParameters->FileSizeL();
       
   130     if ( sizeString ) 
       
   131         {
       
   132         CleanupStack::PushL( sizeString );
       
   133         CUpnpAttribute* size =
       
   134             CUpnpAttribute::NewLC( KAttributeSize() );
       
   135         size->SetValueL( *sizeString );
       
   136 
       
   137         elRes->AddAttributeL( size ); //transfer ownership
       
   138         CleanupStack::Pop( size );        
       
   139         CleanupStack::PopAndDestroy( sizeString );
       
   140         sizeString = NULL;
       
   141         }
       
   142 
       
   143     if ( resParameters->IsImageFile() ) 
       
   144         {
       
   145         // In image case, add resolution parameter to res element.
       
   146         HBufC8* resolutionString = resParameters->ResolutionL();
       
   147         if ( resolutionString ) 
       
   148             {
       
   149             CleanupStack::PushL( resolutionString );
       
   150             CUpnpAttribute* resolution = 
       
   151                 CUpnpAttribute::NewLC( KAttributeResolution() );
       
   152             resolution->SetValueL( *resolutionString );
       
   153             elRes->AddAttributeL( resolution ); //transfer ownership
       
   154             CleanupStack::Pop( resolution );            
       
   155             CleanupStack::PopAndDestroy( resolutionString );
       
   156             resolutionString = NULL;
       
   157             }
       
   158         }
       
   159     else if ( resParameters->IsAudioOrVideoFile() )
       
   160         {
       
   161         // In music or video case, add duration parameter to res element.
       
   162         HBufC8* durationString = resParameters->DurationL();
       
   163         if ( durationString ) 
       
   164             {
       
   165             CleanupStack::PushL( durationString );
       
   166             CUpnpAttribute* duration =
       
   167                 CUpnpAttribute::NewLC( KAttributeDuration() );
       
   168             duration->SetValueL( *durationString );
       
   169             elRes->AddAttributeL( duration ); //transfer ownership
       
   170             CleanupStack::Pop( duration );
       
   171             CleanupStack::PopAndDestroy( durationString );
       
   172             durationString = NULL;
       
   173             }
       
   174         }
       
   175 
       
   176     CleanupStack::PopAndDestroy( resParameters );
       
   177     CleanupStack::PopAndDestroy( resolver );
       
   178 
       
   179     // Attach res element to aItem.
       
   180     aItem.AddElementL( elRes ); //transfer ownership
       
   181     CleanupStack::Pop( elRes );
       
   182     }
       
   183 
       
   184 // --------------------------------------------------------------------------
       
   185 // CUpnpCdsResElementUtility::IsUriAbsolute
       
   186 // 
       
   187 // (other items were commented in a header).
       
   188 // --------------------------------------------------------------------------
       
   189 EXPORT_C TBool UpnpCdsResElementUtility::IsUriAbsolute( const TDesC8& aUri )
       
   190     {
       
   191     TInetAddr addr;
       
   192     TBool retVal = EFalse;
       
   193     TBuf<KIpAddLen> ipAddrBuf;
       
   194     // if aUri.Length is less than KHttp().Length(),aUri is invalid
       
   195     if ( aUri.Length() >= KHttp().Length() )
       
   196 		{
       
   197 		TPtrC8 right = aUri.Right( aUri.Length() - KHttp().Length() );
       
   198 
       
   199 		TInt posit = right.Locate( TChar( KColon ) );
       
   200 		if( posit != KErrNotFound )
       
   201 			{
       
   202 			TPtrC8 ipAddr = right.Left( posit );
       
   203 			if( ipAddr.Length() <= KIpAddLen )
       
   204 				{
       
   205 				ipAddrBuf.Copy( ipAddr );
       
   206 				}
       
   207 			}
       
   208 
       
   209 		if( addr.Input( ipAddrBuf ) == KErrNone )
       
   210 			{
       
   211 			retVal = ETrue;
       
   212 			}
       
   213 		}
       
   214 
       
   215 	return retVal;    
       
   216     }
       
   217 
       
   218 //  End of File