upnpavcontrolpoint/avcpengineclient/src/upnpmetadata.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
parent 39 6369bfd1b60d
child 41 b4d83ea1d6e2
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
     1 /** @file
       
     2 * Copyright (c) 2005-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 for fetching and updating MS metadata
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "upnpmetadata.h"
       
    22 #include "upnpavcpenginemsclient.h"
       
    23 #include <upnpdominterface.h>
       
    24 #include <xml/dom/xmlengdomparser.h>
       
    25 #include <XmlEngSerializationOptions.h>
       
    26 #include <Uri16.h>
       
    27 
       
    28 
       
    29 _LIT(KUPnP16,    "upnp");
       
    30 
       
    31 _LIT8(KId,      "id");
       
    32 _LIT8(KUPnP,    "upnp");
       
    33 _LIT8(KDc,      "dc");
       
    34 _LIT8(KArtist,  "artist");
       
    35 _LIT8(KAlbum,   "album");
       
    36 _LIT8(KGenre,   "genre");
       
    37 _LIT8(KClass,   "class");
       
    38 _LIT8(KCreator, "creator");
       
    39 _LIT8(KTitle,   "title");
       
    40 
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CUpnpMetadata::CUpnpMetadata
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CUpnpMetadata::CUpnpMetadata(RUpnpAVCPEngineMSClient& aSession): 
       
    51 	CActive(EPriorityStandard), 
       
    52 	iSession(aSession), 
       
    53 	iMetaDataPtr(NULL,0),	
       
    54 	iIdPckg(iId), 
       
    55 	iSizePckg(iSize)
       
    56     {
       
    57     CActiveScheduler::Add(this);
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CUpnpMetadata::ConstructL
       
    62 // Symbian 2nd phase constructor can leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void CUpnpMetadata::ConstructL()
       
    66     {     		
       
    67 //    User::LeaveIfError( iRsfwControl.Connect() );
       
    68     iDOMImpl.OpenL();
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CUpnpMetadata::NewL
       
    73 // Two-phased constructor.
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 EXPORT_C CUpnpMetadata* CUpnpMetadata::NewL(RUpnpAVCPEngineMSClient& aSession)
       
    77     {
       
    78     CUpnpMetadata* self = new( ELeave ) CUpnpMetadata(aSession);
       
    79     
       
    80     CleanupStack::PushL( self );
       
    81     self->ConstructL();
       
    82     CleanupStack::Pop( self );
       
    83 
       
    84     return self;
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CUpnpMetadata::~CUpnpMetadata
       
    89 // Destructor
       
    90 // -----------------------------------------------------------------------------
       
    91 //    
       
    92 CUpnpMetadata::~CUpnpMetadata()
       
    93     {
       
    94 	delete iUuid;
       
    95 	delete iPathName;
       
    96 	delete iMetaData;
       
    97 	delete iUpdatedMetaData;
       
    98 	iDocument.Close();    
       
    99 	iDOMImpl.Close();
       
   100 	//iRsfwControl.Close();   
       
   101     }
       
   102 
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CUpnpMetadata::FetchL
       
   106 // -----------------------------------------------------------------------------
       
   107 //   
       
   108 EXPORT_C void CUpnpMetadata::FetchL(const TDesC& aPathName ) 
       
   109 	{
       
   110 	
       
   111 	PrepareDataL(aPathName);
       
   112 	GetMetadataL();
       
   113 	}
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CUpnpMetadata::FetchL
       
   117 // -----------------------------------------------------------------------------
       
   118 //   
       
   119 EXPORT_C void CUpnpMetadata::FetchL(const TDesC& aPathName, TRequestStatus& aStatus ) 
       
   120 	{
       
   121 	PrepareDataL(aPathName);
       
   122 	GetMetadataL(aStatus);
       
   123 	}
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CUpnpMetadata::CommitL
       
   127 // -----------------------------------------------------------------------------
       
   128 //   		
       
   129 EXPORT_C void CUpnpMetadata::CommitL() 
       
   130 	{	        
       
   131     //Dump XML tree into buffer
       
   132     RBuf8 xmlBuf;
       
   133     CleanupClosePushL(xmlBuf);
       
   134     TXmlEngSerializationOptions options(TXmlEngSerializationOptions::KOptionIndent);
       
   135       
       
   136     iDocument.SaveL( xmlBuf, iDocument.DocumentElement(), options);
       
   137     
       
   138     iSession.SetMetadataL(*iUuid, *iPathName, *iMetaData, xmlBuf);    	    
       
   139     
       
   140 	CleanupStack::PopAndDestroy(&xmlBuf); 
       
   141 	}
       
   142 	
       
   143 // -----------------------------------------------------------------------------
       
   144 // CUpnpMetadata::CommitL
       
   145 // -----------------------------------------------------------------------------
       
   146 //   		
       
   147 EXPORT_C void CUpnpMetadata::CommitL(TRequestStatus& aStatus) 
       
   148 	{	 
       
   149 	
       
   150 	iClientStatus = &aStatus;
       
   151 	*iClientStatus = KRequestPending;
       
   152 							
       
   153     //Dump XML tree into buffer
       
   154     RBuf8 xmlBuf;
       
   155     CleanupClosePushL(xmlBuf);
       
   156     TXmlEngSerializationOptions options(TXmlEngSerializationOptions::KOptionIndent);
       
   157       
       
   158     iDocument.SaveL( xmlBuf, iDocument.DocumentElement(), options);
       
   159     delete iUpdatedMetaData;
       
   160     iUpdatedMetaData = NULL;
       
   161     
       
   162     iUpdatedMetaData = xmlBuf.AllocL();
       
   163     CleanupStack::PopAndDestroy(&xmlBuf);
       
   164     
       
   165     iSession.SetMetadata(iStatus, *iUuid, *iPathName, *iMetaData, *iUpdatedMetaData);    	    
       
   166     SetActive();
       
   167 	iState = EUploadingMetadata;    	
       
   168 	}	
       
   169 		
       
   170 // -----------------------------------------------------------------------------
       
   171 // CUpnpMetadata::PrepareData
       
   172 // -----------------------------------------------------------------------------
       
   173 //   
       
   174 void CUpnpMetadata::PrepareDataL(const TDesC& aPathName ) 
       
   175 	{
       
   176 
       
   177 	TParse parseFilename;
       
   178 	parseFilename.Set( aPathName, NULL, NULL );	
       
   179 	
       
   180 
       
   181 	TChar drive = parseFilename.Drive()[0];
       
   182 	drive.UpperCase();
       
   183 
       
   184 	CRsfwMountMan* mountman = NULL;
       
   185 			mountman = CRsfwMountMan::NewL(0, NULL);
       
   186 			User::LeaveIfNull(mountman);
       
   187 			CleanupStack::PushL(mountman);
       
   188 	
       
   189 			const CRsfwMountEntry* entry = 
       
   190 				mountman->MountEntryL(drive);
       
   191 
       
   192 			if (!entry)
       
   193 			  {
       
   194 			  CleanupStack::PopAndDestroy(mountman);
       
   195 			  User::Leave(KErrNotFound);
       
   196 			  }
       
   197 			  
       
   198 	HBufC* uri = entry->Item(EMountEntryItemUri)->Alloc();
       
   199 	
       
   200 	if (!uri)
       
   201 		User::Leave(KErrNoMemory);
       
   202 	
       
   203 	CleanupStack::PushL(uri);	
       
   204 	// parse URI 
       
   205 	TUriParser uriParser;
       
   206 	User::LeaveIfError( uriParser.Parse(*uri) );	
       
   207 	
       
   208 	if ( uriParser.Extract( EUriScheme ).Compare(KUPnP16) == 0) 
       
   209 		{
       
   210 		delete iUuid;
       
   211 		iUuid = NULL;
       
   212 		
       
   213 		delete iPathName;
       
   214 		iPathName = NULL;	
       
   215 			
       
   216 		// filename normalize		
       
   217 		iPathName = HBufC8::NewL( parseFilename.FullName().Length());
       
   218 		iPathName->Des().Append(parseFilename.Path());
       
   219 		TPtr8 path = iPathName->Des();
       
   220 		Normalize( path );		
       
   221 		iPathName->Des().Append(parseFilename.NameAndExt());				
       
   222 		
       
   223 		// uuid 
       
   224 		iUuid = HBufC8::NewL(uriParser.Extract( EUriHost ).Length());			
       
   225 		iUuid->Des().Copy(uriParser.Extract( EUriHost ));						
       
   226 		}
       
   227 	else 
       
   228 		{
       
   229 		CleanupStack::PopAndDestroy(uri);
       
   230 		CleanupStack::PopAndDestroy(mountman);
       
   231 		User::Leave( KErrNotFound );
       
   232 		}	
       
   233 	CleanupStack::PopAndDestroy(uri);
       
   234 	CleanupStack::PopAndDestroy(mountman);
       
   235 	}
       
   236 	
       
   237 // -----------------------------------------------------------------------------
       
   238 // CUpnpMetadata::Normalize
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 void CUpnpMetadata::Normalize(TDes8& aData)
       
   242 	{
       
   243    	for (TInt i = 0; i < aData.Length(); i++)
       
   244 		{
       
   245 		if (aData[i] == '\\')
       
   246     		{
       
   247         	aData[i] = '/';
       
   248      		}
       
   249     	}
       
   250  	}
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CUpnpMetadata::ParseMetadataL
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void CUpnpMetadata::ParseMetadataL()
       
   257 	{
       
   258 	iDocument.Close();
       
   259 	// Create a parser
       
   260 	RXmlEngDOMParser parser;
       
   261 	User::LeaveIfError( parser.Open(iDOMImpl) );
       
   262 	CleanupClosePushL(parser);
       
   263 	// Handler to DOM document
       
   264 	iDocument = parser.ParseL( *iMetaData );     	
       
   265 	CleanupStack::PopAndDestroy(&parser);
       
   266  	} 		
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CUpnpMetadata::GetMetadataL
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void CUpnpMetadata::GetMetadataL()
       
   273 	{
       
   274 	iSession.PrepareMetadataL(*iUuid, *iPathName, iIdPckg, iSizePckg);
       
   275 	if (iSize > 0) 
       
   276 		{
       
   277 		delete iMetaData;
       
   278 		iMetaData = NULL;		
       
   279 		iMetaData = HBufC8::NewL(iSize);
       
   280 		iMetaDataPtr.Set(iMetaData->Des());
       
   281 		iSession.DownloadMetadataL(iId, iMetaDataPtr);
       
   282 		ParseMetadataL();
       
   283 		}
       
   284 	}
       
   285 	
       
   286 // -----------------------------------------------------------------------------
       
   287 // CUpnpMetadata::GetMetadataL
       
   288 // -----------------------------------------------------------------------------
       
   289 //
       
   290 void CUpnpMetadata::GetMetadataL(TRequestStatus& aStatus)
       
   291 	{
       
   292 	iClientStatus = &aStatus;
       
   293 	*iClientStatus = KRequestPending;
       
   294 	iSession.PrepareMetadata(iStatus, *iUuid, *iPathName, iIdPckg, iSizePckg);
       
   295 	SetActive();
       
   296 	iState = EPreparingMetadata;	
       
   297 	}
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 // CUpnpMetadata::DoCancel
       
   301 // -----------------------------------------------------------------------------
       
   302 //
       
   303 void CUpnpMetadata::DoCancel()
       
   304 	{
       
   305 	if (iClientStatus) 
       
   306 		{	
       
   307 		User::RequestComplete(iClientStatus, KErrCancel);			
       
   308 		}
       
   309 	}
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CUpnpMetadata::RunError
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 TInt CUpnpMetadata::RunError( TInt aError ) 
       
   316 	{
       
   317 	if (iClientStatus) 
       
   318 		{
       
   319 		User::RequestComplete(iClientStatus, aError);			
       
   320 		}
       
   321 	return KErrNone;
       
   322 	}
       
   323         		
       
   324 // -----------------------------------------------------------------------------
       
   325 // CUpnpMetadata::RunL
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 void CUpnpMetadata::RunL() 
       
   329 	{
       
   330 	if (iStatus.Int() == KErrNone) 
       
   331 		{
       
   332 		switch (iState) 
       
   333 			{
       
   334 			case EPreparingMetadata:
       
   335 			if (iSize > 0) 
       
   336 				{
       
   337 				delete iMetaData;
       
   338 				iMetaData = NULL;
       
   339 				iMetaData = HBufC8::NewL(iSize);
       
   340 				iMetaDataPtr.Set(iMetaData->Des());
       
   341 				iSession.DownloadMetadata(iStatus, iId, iMetaDataPtr);
       
   342 				SetActive();
       
   343 				iState = EDownloadingMetadata;				
       
   344 				}
       
   345 				break;
       
   346 			case EDownloadingMetadata:
       
   347 				ParseMetadataL();
       
   348 				User::RequestComplete(iClientStatus, iStatus.Int());
       
   349 				break;
       
   350 			case EUploadingMetadata:
       
   351 				delete iUpdatedMetaData;
       
   352 				iUpdatedMetaData = NULL;
       
   353 				User::RequestComplete(iClientStatus, iStatus.Int());
       
   354 				break;							
       
   355 			}		
       
   356 		} 
       
   357 	else 
       
   358 		{
       
   359 		User::RequestComplete(iClientStatus, iStatus.Int());		
       
   360 		}
       
   361 	}
       
   362 
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CUpnpMetadata::ObjectL
       
   366 // -----------------------------------------------------------------------------
       
   367 //   
       
   368 TBool CUpnpMetadata::ObjectL(TXmlEngElement& aObject) 
       
   369 	{
       
   370 	TXmlEngElement root = iDocument.DocumentElement();
       
   371 	
       
   372     RXmlEngNodeList<TXmlEngElement> argElems;
       
   373     CleanupClosePushL(argElems);
       
   374     root.GetChildElements( argElems ); 
       
   375                 
       
   376     if ( argElems.Count() == 1 ) 
       
   377     	{
       
   378     	aObject = argElems.Next();
       
   379     	
       
   380     	} 
       
   381     	
       
   382     CleanupStack::PopAndDestroy(&argElems);
       
   383     return !aObject.IsNull();	             	
       
   384 	}
       
   385 	
       
   386 // -----------------------------------------------------------------------------
       
   387 // CUpnpMetadata::GetElementL
       
   388 // -----------------------------------------------------------------------------
       
   389 //   
       
   390 TPtrC8 CUpnpMetadata::GetElementL( const TDesC8& aName) 	 
       
   391 	{
       
   392 	TXmlEngElement result;
       
   393 	TXmlEngElement object;
       
   394 	if (ObjectL(object) &&  UpnpDomInterface::GetElementL(  object, result, aName) ) 
       
   395 		{
       
   396 		return result.Value();	
       
   397 		}
       
   398                                 
       
   399 	return KNullDesC8();	
       
   400 	}
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CUpnpMetadata::SetElementL
       
   404 // -----------------------------------------------------------------------------
       
   405 //   	
       
   406 void CUpnpMetadata::SetElementL( const TDesC8& aNamespace, const TDesC8& aName, const TDesC8& aValue) 	 
       
   407 	{
       
   408 	TXmlEngElement result;
       
   409 	TXmlEngElement object;
       
   410 	if (ObjectL(object))
       
   411 		{
       
   412 		if (!UpnpDomInterface::GetElementL(  object, result, aName) ) 
       
   413 			{			
       
   414 			result = object.AddNewElementUsePrefixL(aName, aNamespace);
       
   415 			}
       
   416 		result.SetTextL( aValue );
       
   417 		}
       
   418 	}	
       
   419 		
       
   420 // -----------------------------------------------------------------------------
       
   421 // CUpnpMetadata::Id
       
   422 // -----------------------------------------------------------------------------
       
   423 //   	
       
   424 EXPORT_C  TPtrC8 CUpnpMetadata::IdL() 
       
   425 	{ 	
       
   426 	TXmlEngElement object;
       
   427 	if (ObjectL(object)) 
       
   428 		{
       
   429 		return UpnpDomInterface::GetAttrValueL( object, KId );
       
   430 		} 
       
   431 	else 
       
   432 		{
       
   433 		return KNullDesC8();		
       
   434 		}
       
   435 	}
       
   436 	
       
   437 // -----------------------------------------------------------------------------
       
   438 // CUpnpMetadata::Title
       
   439 // -----------------------------------------------------------------------------
       
   440 //   	
       
   441 EXPORT_C TPtrC8 CUpnpMetadata::TitleL() 
       
   442 	{
       
   443 	return GetElementL(KTitle);
       
   444 	}
       
   445 
       
   446 // -----------------------------------------------------------------------------
       
   447 // CUpnpMetadata::SetTitleL
       
   448 // -----------------------------------------------------------------------------
       
   449 //   	
       
   450 EXPORT_C void CUpnpMetadata::SetTitleL(const TDesC8& aTitle) 
       
   451 	{
       
   452 	SetElementL(KDc, KTitle, aTitle);
       
   453 	}	
       
   454 
       
   455 // -----------------------------------------------------------------------------
       
   456 // CUpnpMetadata::Creator
       
   457 // -----------------------------------------------------------------------------
       
   458 //   		
       
   459 EXPORT_C TPtrC8 CUpnpMetadata::CreatorL() 
       
   460 	{
       
   461 	return GetElementL(KCreator);
       
   462 	}
       
   463 
       
   464 // -----------------------------------------------------------------------------
       
   465 // CUpnpMetadata::SetCreator
       
   466 // -----------------------------------------------------------------------------
       
   467 //   	
       
   468 EXPORT_C void CUpnpMetadata::SetCreatorL(const TDesC8& aCreator)
       
   469 	{
       
   470 	SetElementL(KDc,KCreator, aCreator);	
       
   471 	}
       
   472 
       
   473 // -----------------------------------------------------------------------------
       
   474 // CUpnpMetadata::Class
       
   475 // -----------------------------------------------------------------------------
       
   476 //   	
       
   477 EXPORT_C TPtrC8 CUpnpMetadata::ClassL() 
       
   478 	{
       
   479 	return GetElementL(KClass);
       
   480 	}
       
   481 
       
   482 // -----------------------------------------------------------------------------
       
   483 // CUpnpMetadata::SetClass
       
   484 // -----------------------------------------------------------------------------
       
   485 //   		
       
   486 EXPORT_C void CUpnpMetadata::SetClassL(const TDesC8& aClass)
       
   487 	{
       
   488 	SetElementL(KUPnP, KClass, aClass);	
       
   489 	}
       
   490 
       
   491 // -----------------------------------------------------------------------------
       
   492 // CUpnpMetadata::Artist
       
   493 // -----------------------------------------------------------------------------
       
   494 //   		
       
   495 EXPORT_C TPtrC8 CUpnpMetadata::ArtistL() 
       
   496 	{
       
   497 	return GetElementL(KArtist);
       
   498 	}
       
   499 		
       
   500 // -----------------------------------------------------------------------------
       
   501 // CUpnpMetadata::SetArtist
       
   502 // -----------------------------------------------------------------------------
       
   503 //   			
       
   504 EXPORT_C void CUpnpMetadata::SetArtistL(const TDesC8& aArtist)
       
   505 	{
       
   506 	SetElementL(KUPnP, KArtist, aArtist);		
       
   507 	}
       
   508 	
       
   509 // -----------------------------------------------------------------------------
       
   510 // CUpnpMetadata::Genre
       
   511 // -----------------------------------------------------------------------------
       
   512 //   		
       
   513 EXPORT_C TPtrC8 CUpnpMetadata::GenreL() 
       
   514 	{
       
   515 	return GetElementL(KGenre);
       
   516 	}
       
   517 	
       
   518 // -----------------------------------------------------------------------------
       
   519 // CUpnpMetadata::SetGenre
       
   520 // -----------------------------------------------------------------------------
       
   521 //   			
       
   522 EXPORT_C void CUpnpMetadata::SetGenreL(const TDesC8& aGenre)
       
   523 	{
       
   524 	SetElementL(KUPnP, KGenre, aGenre);		
       
   525 	}
       
   526 
       
   527 // -----------------------------------------------------------------------------
       
   528 // CUpnpMetadata::Album
       
   529 // -----------------------------------------------------------------------------
       
   530 //   			
       
   531 EXPORT_C TPtrC8 CUpnpMetadata::AlbumL() 
       
   532 	{
       
   533 	return GetElementL(KAlbum);
       
   534 	}		
       
   535 
       
   536 // -----------------------------------------------------------------------------
       
   537 // CUpnpMetadata::SetAlbum
       
   538 // -----------------------------------------------------------------------------
       
   539 //   	
       
   540 EXPORT_C void CUpnpMetadata::SetAlbumL(const TDesC8& aAlbum)
       
   541 	{
       
   542 	SetElementL(KUPnP, KAlbum, aAlbum);	
       
   543 	}
       
   544 
       
   545 // -----------------------------------------------------------------------------
       
   546 // CUpnpMetadata::AsXmlL
       
   547 // -----------------------------------------------------------------------------
       
   548 //   		
       
   549 EXPORT_C RXmlEngDocument& CUpnpMetadata::AsXmlL() 
       
   550 	{
       
   551 	return iDocument;
       
   552 	}
       
   553 
       
   554 	
       
   555 	
       
   556 //  End of File