harvester/common/src/harvesterpluginfactory.cpp
changeset 0 c53acadfccc6
child 8 6752808b2036
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2006-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:  Harvester plugin factory
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <e32std.h>
       
    21 #include <apmrec.h>
       
    22 #include <harvesterplugin.h>
       
    23 #include <mdeobject.h>
       
    24 #include <harvesterdata.h>
       
    25 
       
    26 #include "harvesterpluginfactory.h"
       
    27 #include "harvesterplugininfo.h"
       
    28 #include "mdsutils.h"
       
    29 #include "harvesterlog.h"
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // Constructor
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CHarvesterPluginFactory::CHarvesterPluginFactory() :
       
    36     iBlacklist( NULL )
       
    37 	{
       
    38 	WRITELOG( "CHarvesterPluginFactory::CHarvesterPluginFactory()" );
       
    39 	}
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // NewL
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C CHarvesterPluginFactory* CHarvesterPluginFactory::NewL()
       
    46 	{
       
    47 	WRITELOG( "CHarvesterPluginFactory::NewL()" );
       
    48 	CHarvesterPluginFactory* self = new (ELeave) CHarvesterPluginFactory();
       
    49 	CleanupStack::PushL ( self);
       
    50 	self->ConstructL ();
       
    51 	CleanupStack::Pop ( self);
       
    52 
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // Destructor
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CHarvesterPluginFactory::~CHarvesterPluginFactory()
       
    61 	{
       
    62 	WRITELOG( "CHarvesterPluginFactory::~CHarvesterPluginFactory()" );
       
    63 	iHarvesterPluginInfoArray.ResetAndDestroy();
       
    64 	iHarvesterPluginInfoArray.Close();
       
    65 	REComSession::FinalClose();
       
    66 	}
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // ConstructL
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 void CHarvesterPluginFactory::ConstructL()
       
    73 	{
       
    74 	WRITELOG( "CHarvesterPluginFactory::ConstructL()" );
       
    75 	SetupHarvesterPluginInfoL();
       
    76 	}
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // GetObjectDef
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 EXPORT_C void CHarvesterPluginFactory::GetObjectDefL( CHarvesterData& aHD, TDes& aObjectDef )
       
    83 	{
       
    84 	TPtrC extPtr;
       
    85 	if( MdsUtils::GetExt( aHD.Uri(), extPtr ) )
       
    86 		{
       
    87 		RPointerArray<CHarvesterPluginInfo> supportedPlugins;
       
    88 		CleanupClosePushL( supportedPlugins );
       
    89 		GetSupportedPluginsL( supportedPlugins, extPtr );
       
    90 		
       
    91 		const TInt sCount = supportedPlugins.Count();
       
    92 		if( sCount == 1 )
       
    93 			{
       
    94 			CHarvesterPluginInfo* info = supportedPlugins[0];
       
    95 			if( info->iObjectTypes.Count() == 1 )
       
    96 				{
       
    97 				aObjectDef.Copy( *(info->iObjectTypes[0]) );
       
    98 				aHD.SetHarvesterPluginInfo( info );
       
    99 				CleanupStack::PopAndDestroy( &supportedPlugins );
       
   100 				return;
       
   101 				}
       
   102 			}
       
   103 		for( TInt i = 0; i < sCount; i++ )
       
   104 			{
       
   105 			CHarvesterPluginInfo* info = supportedPlugins[i];
       
   106 			if ( !(info->iPlugin) )
       
   107 				{
       
   108 				info->iPlugin = CHarvesterPlugin::NewL( info->iPluginUid );
       
   109 				info->iPlugin->SetQueue( info->iQueue );
       
   110 				}
       
   111 			info->iPlugin->GetObjectType( aHD.Uri(), aObjectDef );
       
   112 			if( aObjectDef.Length() > 0 )
       
   113 				{
       
   114 				aHD.SetHarvesterPluginInfo( info );
       
   115 				break;
       
   116 				}
       
   117 			}
       
   118 		CleanupStack::PopAndDestroy( &supportedPlugins );
       
   119 		}
       
   120 	else
       
   121 		{
       
   122 		aObjectDef.Zero();
       
   123 		}
       
   124 	}
       
   125 	
       
   126 // ---------------------------------------------------------------------------
       
   127 // GetMimeType
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 EXPORT_C void CHarvesterPluginFactory::GetMimeType(const TDesC& /*aUri*/, TDes& aMimeType)
       
   131 	{
       
   132 	_LIT( KJPGMimeType, "image/jpeg" );
       
   133 	aMimeType.Copy( KJPGMimeType );
       
   134 	}
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // HarvestL
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C TInt CHarvesterPluginFactory::HarvestL( CHarvesterData* aHD )
       
   141 	{
       
   142 #ifdef _DEBUG
       
   143 	WRITELOG1("CHarvesterPluginFactory::HarvestL - aHD->Uri: %S", &aHD->Uri() );
       
   144 #endif
       
   145 	CHarvesterPluginInfo* hpi = aHD->HarvesterPluginInfo();
       
   146 		
       
   147 	if ( hpi )
       
   148 		{
       
   149 		if ( ! hpi->iPlugin )
       
   150 	   		{
       
   151 	   		hpi->iPlugin = CHarvesterPlugin::NewL( hpi->iPluginUid );
       
   152 	   		hpi->iPlugin->SetQueue( hpi->iQueue );
       
   153 	   		hpi->iPlugin->SetBlacklist( *iBlacklist );
       
   154 	   		}
       
   155 			
       
   156 		if( aHD->ObjectType() == EFastHarvest || aHD->Origin() == MdeConstants::Object::ECamera )
       
   157 		   	{
       
   158 		   	hpi->iQueue.Insert( aHD, 0 );
       
   159 		    	}
       
   160 	    else
       
   161 			{
       
   162 			hpi->iQueue.AppendL( aHD );
       
   163 			}
       
   164 		    
       
   165 		hpi->iPlugin->StartHarvest();
       
   166 			
       
   167 		return KErrNone;
       
   168 		}
       
   169 		
       
   170 	return KErrNotFound;
       
   171 	}
       
   172 
       
   173 // ---------------------------------------------------------------------------
       
   174 // GetPluginInfos
       
   175 // ---------------------------------------------------------------------------
       
   176 //
       
   177 EXPORT_C RPointerArray<CHarvesterPluginInfo>& CHarvesterPluginFactory::GetPluginInfos()
       
   178 	{
       
   179 	return iHarvesterPluginInfoArray;
       
   180 	}
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // SetBlacklist
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 EXPORT_C void CHarvesterPluginFactory::SetBlacklist( CHarvesterBlacklist& aBlacklist )
       
   187 	{
       
   188 	WRITELOG( "CHarvesterPluginFactory::SetBlacklist()" );
       
   189 	iBlacklist = &aBlacklist;
       
   190 	
       
   191 	TInt count = iHarvesterPluginInfoArray.Count();
       
   192 	for ( TInt i = 0; i < count; i++ )
       
   193 		{
       
   194 		iHarvesterPluginInfoArray[i]->iPlugin->SetBlacklist( *iBlacklist );
       
   195 		}
       
   196 	
       
   197 	}
       
   198 
       
   199 // ---------------------------------------------------------------------------
       
   200 // SetupHarvesterPluginInfoL
       
   201 // ---------------------------------------------------------------------------
       
   202 //
       
   203 void CHarvesterPluginFactory::SetupHarvesterPluginInfoL()
       
   204     {
       
   205     WRITELOG( "CHarvesterPluginFactory::SetupHarvesterPluginInfo()" );
       
   206     
       
   207     RImplInfoPtrArray infoArray;
       
   208     TCleanupItem cleanupItem( MdsUtils::CleanupEComArray, &infoArray );
       
   209     CleanupStack::PushL( cleanupItem );
       
   210  
       
   211     CHarvesterPlugin::ListImplementationsL( infoArray );
       
   212 
       
   213     for ( TInt i = infoArray.Count(); --i >= 0; )
       
   214         {
       
   215         // Parse the MIME types and resolve plug-in's uids from infoArray to iDataTypeArray
       
   216         CImplementationInformation* info = infoArray[i];
       
   217         const TBufC8<KMaxFileName>& type = info->DataType();
       
   218         const TBufC8<KMaxFileName>& opaque = info->OpaqueData();
       
   219         TUid implUID = info->ImplementationUid(); 
       
   220                 
       
   221         AddNewPluginL( type, opaque, implUID );   
       
   222         }
       
   223     
       
   224     CleanupStack::PopAndDestroy( &infoArray );
       
   225     }
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // AddNewPluginL
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 void CHarvesterPluginFactory::AddNewPluginL( const TDesC8& aType,
       
   232 		const TDesC8& aOpaque, TUid aPluginUid )
       
   233     {      
       
   234     WRITELOG( "CHarvesterPluginFactory::AddNewPluginL" );
       
   235 
       
   236     CHarvesterPluginInfo* pluginInfo = new (ELeave) CHarvesterPluginInfo;
       
   237     CleanupStack::PushL( pluginInfo );
       
   238 
       
   239     // get file extensions
       
   240     TLex8 lex( aOpaque );
       
   241     while ( !lex.Eos() )
       
   242         {
       
   243         /* Tokenizing file extensions using TLex8 */
       
   244         lex.SkipSpaceAndMark();
       
   245 
       
   246         TPtrC8 extToken = lex.NextToken();
       
   247 
       
   248         HBufC* str = HBufC::NewLC( extToken.Length() );
       
   249         str->Des().Copy( extToken );
       
   250         pluginInfo->iExtensions.AppendL( str );
       
   251         CleanupStack::Pop( str );
       
   252         }
       
   253 
       
   254     // get object types
       
   255     TLex8 lexObjectTypes( aType );
       
   256     while ( !lexObjectTypes.Eos() )
       
   257         {
       
   258         /* Tokenizing object types using TLex8 */
       
   259         lexObjectTypes.SkipSpaceAndMark();
       
   260         
       
   261         TPtrC8 objectTypeToken = lexObjectTypes.NextToken();
       
   262 
       
   263         HBufC* str = HBufC::NewLC( objectTypeToken.Length() );
       
   264         str->Des().Copy( objectTypeToken );
       
   265         pluginInfo->iObjectTypes.AppendL( str );
       
   266         CleanupStack::Pop( str );
       
   267         }
       
   268     
       
   269     pluginInfo->iPluginUid = aPluginUid;
       
   270     
       
   271     // Load plugin
       
   272     pluginInfo->iPlugin = CHarvesterPlugin::NewL( pluginInfo->iPluginUid );
       
   273     pluginInfo->iPlugin->SetQueue( pluginInfo->iQueue );    
       
   274     
       
   275     iHarvesterPluginInfoArray.AppendL( pluginInfo );
       
   276     CleanupStack::Pop( pluginInfo );
       
   277     }
       
   278 
       
   279 void CHarvesterPluginFactory::GetSupportedPluginsL(
       
   280 		RPointerArray<CHarvesterPluginInfo>& aSupportedPlugins, const TDesC& aExt )
       
   281 	{
       
   282 	TInt pluginInfoCount = iHarvesterPluginInfoArray.Count();
       
   283 	TInt extCount = 0;
       
   284 	for ( TInt i = pluginInfoCount; --i >= 0; )
       
   285         {
       
   286         CHarvesterPluginInfo* info = iHarvesterPluginInfoArray[i];
       
   287         
       
   288         extCount = info->iExtensions.Count();
       
   289         for ( TInt k = extCount; --k >= 0; )
       
   290             {
       
   291             TDesC* ext = info->iExtensions[k];
       
   292             
       
   293             // checking against supported plugin file extensions
       
   294             const TInt result = ext->CompareF( aExt );
       
   295             if ( result == 0 )
       
   296                 {
       
   297                 aSupportedPlugins.AppendL( info );
       
   298                 break;
       
   299                 }
       
   300             }
       
   301         }	
       
   302 	}
       
   303 
       
   304 EXPORT_C TBool CHarvesterPluginFactory::IsSupportedFileExtension( const TDesC& aFileName )
       
   305 	{
       
   306 	TPtrC extPtr;
       
   307 	if( MdsUtils::GetExt( aFileName, extPtr ) )
       
   308 		{
       
   309 		TInt pluginInfoCount = iHarvesterPluginInfoArray.Count();
       
   310 		TInt extCount = 0;
       
   311 		for ( TInt i = pluginInfoCount; --i >= 0; )
       
   312 			{
       
   313 			CHarvesterPluginInfo* info = iHarvesterPluginInfoArray[i];
       
   314 			extCount = info->iExtensions.Count();
       
   315 			for ( TInt k = extCount; --k >= 0; )
       
   316 				{
       
   317 				TDesC* ext = info->iExtensions[k];
       
   318 				// checking against supported plugin file extensions
       
   319 				TInt result = MdsUtils::Compare( *ext, extPtr );
       
   320 				if ( result == 0 )
       
   321 					{
       
   322 					return ETrue;
       
   323 					}
       
   324 				}
       
   325 			}
       
   326 		}
       
   327 	return EFalse;
       
   328 	}
       
   329 
       
   330 EXPORT_C TBool CHarvesterPluginFactory::IsContainerFileL( const TDesC& aURI )
       
   331 	{
       
   332 	TBool isContainerFile = EFalse;
       
   333 	TPtrC extPtr;
       
   334 	
       
   335 	if( MdsUtils::GetExt( aURI, extPtr ) )
       
   336 		{
       
   337 		RPointerArray<CHarvesterPluginInfo> supportedPlugins;
       
   338 		CleanupClosePushL( supportedPlugins );
       
   339 		GetSupportedPluginsL( supportedPlugins, extPtr );
       
   340 		const TInt sCount = supportedPlugins.Count();
       
   341 		for( TInt i = 0; i < sCount; i++ )
       
   342 			{
       
   343 			CHarvesterPluginInfo* info = supportedPlugins[i];
       
   344 			if( info->iObjectTypes.Count() >  1 )
       
   345 				{
       
   346 				isContainerFile = ETrue;
       
   347 				break;
       
   348 				}
       
   349 			}
       
   350 		CleanupStack::PopAndDestroy( &supportedPlugins );
       
   351 		}
       
   352 	return isContainerFile;
       
   353 	}