genericservices/mimerecognitionfw/apmime/APMFNDR.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // concrete file finders for MIME type recognizers
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "APMFNDR.H"
       
    19 
       
    20 #include <e32uid.h>
       
    21 #include "APMPAN.H"
       
    22 
       
    23 #ifdef USING_ECOM_RECOGS
       
    24 #include <ecom/ecom.h>
       
    25 #endif
       
    26 
       
    27 const TInt KRecognizerDllGranularity=2;
       
    28 
       
    29 #ifdef USING_ECOM_RECOGS
       
    30 const TUid KUidDataRecogInterFaceUid = {0x101F7D87};
       
    31 #endif
       
    32 
       
    33 //
       
    34 // class TRecognizer
       
    35 //
       
    36 
       
    37 CApaScanningDataRecognizer::TRecognizer::TRecognizer(HBufC* aName)
       
    38 	{
       
    39 	iName=aName;
       
    40 	}
       
    41 	
       
    42 //
       
    43 // class CApaRecognizerDll
       
    44 //
       
    45 
       
    46 CApaRecognizerDll::~CApaRecognizerDll()
       
    47 	{
       
    48 	delete iId.iName;
       
    49 	iLibrary.Close();
       
    50 	delete iNext;
       
    51 	}
       
    52 
       
    53 CApaRecognizerDll::CApaRecognizerDll(HBufC* aName) 
       
    54 	: iId(aName)
       
    55 	{
       
    56 	}
       
    57 
       
    58 #ifdef USING_ECOM_RECOGS
       
    59 // Used for cleanup support of locally declared arrays
       
    60 void CleanUpEComInfoArray(TAny* aInfoArray)
       
    61 	{
       
    62 	RImplInfoPtrArray* infoArray = (static_cast<RImplInfoPtrArray*>(aInfoArray));
       
    63 	infoArray->ResetAndDestroy();
       
    64 	infoArray->Close();
       
    65 	}
       
    66 #endif
       
    67 
       
    68 // class CApaBackupOperationObserver
       
    69 CApaScanningDataRecognizer::CApaBackupOperationObserver::CApaBackupOperationObserver(CApaScanningDataRecognizer& aRecognizer)
       
    70 	: iRecognizer(aRecognizer)
       
    71 	{
       
    72 	TRAP_IGNORE(iSession = CBaBackupSessionWrapper::NewL());
       
    73 	}
       
    74 
       
    75 CApaScanningDataRecognizer::CApaBackupOperationObserver::~CApaBackupOperationObserver()
       
    76 	{
       
    77 	if( iSession )
       
    78 		{
       
    79 		iSession->DeRegisterBackupOperationObserver(*this);
       
    80 		delete iSession;
       
    81 		}
       
    82 	}
       
    83 
       
    84 void CApaScanningDataRecognizer::CApaBackupOperationObserver::RegisterObserverL()
       
    85 	{
       
    86 	if(iSession)
       
    87 		{
       
    88 		iSession->RegisterBackupOperationObserverL(*this);
       
    89 		}
       
    90 	}
       
    91 
       
    92 void CApaScanningDataRecognizer::CApaBackupOperationObserver::HandleBackupOperationEventL(const TBackupOperationAttributes& aBackupOperationAttributes)
       
    93 	{
       
    94 	if (!iRecognizer.iExtra->iRecognizersLoaded)
       
    95 		{
       
    96 		// Then the recognizers have been unloaded, so there's nothing to do
       
    97 		return;
       
    98 		}
       
    99 
       
   100 	if ((aBackupOperationAttributes.iFileFlag == MBackupObserver::EReleaseLockReadOnly ||
       
   101 		aBackupOperationAttributes.iFileFlag == MBackupObserver::EReleaseLockNoAccess)
       
   102 		&& aBackupOperationAttributes.iOperation == EStart)
       
   103 		{
       
   104 		// release recognisers not in rom
       
   105 		TInt count = iRecognizer.RecognizerCount();
       
   106 		for (TInt i=count-1; i>=0; i--)
       
   107 			{
       
   108 				CApaRecognizerDll* lib=iRecognizer.iRecognizerLib[i];
       
   109 				TDriveUnit drive(lib->iId.iDrive);
       
   110 				TChar driveName=drive.Name()[0];
       
   111 				if(driveName != TChar('Z'))
       
   112 					{
       
   113 					iRecognizer.RemoveRecognizer(lib); // ignore any errors
       
   114 					}
       
   115 			}
       
   116 		}
       
   117 
       
   118 	if (aBackupOperationAttributes.iFileFlag == MBackupObserver::ETakeLock &&
       
   119 		(aBackupOperationAttributes.iOperation == EEnd ||
       
   120 		aBackupOperationAttributes.iOperation == EAbort))
       
   121 		{
       
   122 		iRecognizer.ScanForRecognizersL();
       
   123 		}
       
   124 	}
       
   125 
       
   126 TInt CApaScanningDataRecognizer::CApaBackupOperationObserver::UpdateCounter() const
       
   127 	{
       
   128 	return iUpdateCounter;
       
   129 	}
       
   130 
       
   131 void CApaScanningDataRecognizer::CApaBackupOperationObserver::SetUpdateCounter(TInt aValue)
       
   132 	{
       
   133 	iUpdateCounter = aValue;
       
   134 	}
       
   135 
       
   136 
       
   137 //
       
   138 // class CApaScanningDataRecognizer
       
   139 // 
       
   140 
       
   141 EXPORT_C CApaScanningDataRecognizer* CApaScanningDataRecognizer::NewL(RFs& aFs)
       
   142 	{
       
   143 	return NewL(aFs,ETrue);
       
   144 	}
       
   145 
       
   146 /**
       
   147 @internalTechnology
       
   148 */
       
   149 EXPORT_C CApaScanningDataRecognizer* CApaScanningDataRecognizer::NewL(RFs& aFs, TBool aLoadDataRecognizers)
       
   150 	{
       
   151 	CApaScanningDataRecognizer* self=new(ELeave) CApaScanningDataRecognizer(aFs);
       
   152 	CleanupStack::PushL(self);
       
   153 	self->ConstructL(aLoadDataRecognizers);
       
   154 	CleanupStack::Pop(); // self
       
   155 	return self;
       
   156 	}
       
   157 
       
   158 EXPORT_C CApaScanningDataRecognizer::~CApaScanningDataRecognizer()
       
   159 	{
       
   160 	UnloadRecognizers();
       
   161 	delete iExtra->iObserver;
       
   162 	delete iExtra;
       
   163 	}
       
   164 
       
   165 EXPORT_C void CApaScanningDataRecognizer::ScanForRecognizersL()
       
   166 	{
       
   167 	// set all recognizers to not present - pending rediscovery
       
   168 	TInt count=iRecognizerLib.Count();
       
   169 	TInt i;
       
   170 	for (i=0; i<count; i++)
       
   171 		iRecognizerLib[i]->iPresent=CApaRecognizerDll::ENotPresent;
       
   172 
       
   173 	ScanForEcomRecognizersL();
       
   174 	//
       
   175 	// remove any recognizers that are no longer present
       
   176 	for (TInt ii=iRecognizerLib.Count()-1;ii>=0;ii--)
       
   177 		{
       
   178 		CApaRecognizerDll* lib=iRecognizerLib[ii];
       
   179 		if (lib->iPresent==CApaRecognizerDll::ENotPresent)
       
   180 			{
       
   181 			RemoveRecognizer(lib); // ignore any errors
       
   182 			}
       
   183 		}
       
   184 	iMaxBufferSize = -1; // Recognizers were re-loaded so need to recalculate the maximum buffer size
       
   185 	}
       
   186 
       
   187 EXPORT_C TInt CApaScanningDataRecognizer::RecognizerCount()
       
   188 	{
       
   189 	return iRecognizerLib.Count();
       
   190 	}
       
   191 
       
   192 EXPORT_C CApaScanningDataRecognizer::CRecognizerArray* CApaScanningDataRecognizer::RecognizerListLC()const
       
   193 	{
       
   194 	CRecognizerArray* list=new(ELeave) CArrayFixFlat<TRecognizer>(2);
       
   195 	CleanupStack::PushL(list);
       
   196 	TInt count=iRecognizerLib.Count();
       
   197 	for (TInt i=0; i<count; i++)
       
   198 		list->AppendL(iRecognizerLib[i]->iId);
       
   199 	return list;
       
   200 	}
       
   201 
       
   202 EXPORT_C const CApaScanningDataRecognizer::TRecognizer& CApaScanningDataRecognizer::operator[](TInt aIndex)const
       
   203 	{
       
   204 	return iRecognizerLib[aIndex]->iId;	
       
   205 	}
       
   206 
       
   207 EXPORT_C TInt CApaScanningDataRecognizer::UpdateCounter()const
       
   208 	{
       
   209 	return iExtra->iObserver->UpdateCounter();
       
   210 	}
       
   211 
       
   212 
       
   213 EXPORT_C CApaScanningDataRecognizer::CApaScanningDataRecognizer(RFs& aFs)
       
   214 	:CApaDataRecognizer(aFs),
       
   215 	iRecognizerLib(KRecognizerDllGranularity)
       
   216 	{
       
   217 	__DECLARE_NAME(_S("CApaScanningDataRecognizer"));
       
   218 	}
       
   219 
       
   220 EXPORT_C void CApaScanningDataRecognizer::ConstructL()
       
   221 	{
       
   222 	ConstructL(ETrue);
       
   223 	}
       
   224 
       
   225 void CApaScanningDataRecognizer::ConstructL(TBool aLoadDataRecognizers)
       
   226 	{
       
   227 	iExtra = new (ELeave) CApaScanningDataRecognizerExtra();
       
   228 	iExtra->iObserver = new (ELeave) CApaBackupOperationObserver( *this );
       
   229 	iExtra->iObserver->RegisterObserverL();
       
   230 	if(aLoadDataRecognizers)
       
   231 		{
       
   232 		LoadRecognizersL();
       
   233 		}
       
   234 	}
       
   235 
       
   236 EXPORT_C void CApaScanningDataRecognizer::SetEcomRecognizersFromListL(const CRecognizerArray& aList)
       
   237 	{
       
   238 	//for ecom style
       
   239 	for (TInt i=0 ; i<aList.Count() ; i++)
       
   240 		{
       
   241 		TDriveUnit drive(aList[i].iDrive);
       
   242 		TRAPD(ret, LoadEcomRecognizerL(drive,aList[i].iUid));
       
   243 		if (ret==KErrNoMemory)
       
   244 			{
       
   245 			User::Leave(ret); 
       
   246 			}
       
   247 		}
       
   248 	}
       
   249 
       
   250 EXPORT_C void CApaScanningDataRecognizer::SetEcomRecognizerL(const TRecognizer& aRecognizer)
       
   251 	{
       
   252 	//for ecom style 
       
   253 	TDriveUnit drive(aRecognizer.iDrive);
       
   254 	LoadEcomRecognizerL(drive,aRecognizer.iUid);
       
   255 	}
       
   256 
       
   257 TInt CApaScanningDataRecognizer::RemoveRecognizer(CApaRecognizerDll* aDll)
       
   258 	{
       
   259 	__ASSERT_DEBUG(iRecognizerLib.Count()>0,User::Invariant());
       
   260 	// try to remove the recognizer from the list
       
   261 	TInt ret=RemoveDataRecognizerType(aDll->iRec);
       
   262 	if (ret!=KErrNone && ret!=KErrNotFound)
       
   263 		return ret;
       
   264 	//
       
   265 	// get a handle to the previous entry in the list
       
   266 	TInt count=iRecognizerLib.Count();
       
   267 	TInt i;
       
   268 	for (i=0; i<count; i++)
       
   269 		{
       
   270 		if (iRecognizerLib[i]==aDll)
       
   271 			break;
       
   272 		}
       
   273 	__ASSERT_ALWAYS(i<count,Panic(EPanicLibraryNotInList));
       
   274 	
       
   275 	// remove the dll
       
   276 	delete iRecognizerLib[i];
       
   277 	iRecognizerLib.Remove(i);
       
   278 	iRecognizerLib.Compress();
       
   279 	if(iExtra->iObserver)
       
   280 		iExtra->iObserver->SetUpdateCounter(iExtra->iObserver->UpdateCounter() +1);
       
   281 	return KErrNone;
       
   282 	}
       
   283 
       
   284 #ifdef USING_ECOM_RECOGS
       
   285 
       
   286 //load the ecom style data recognizer
       
   287 void CApaScanningDataRecognizer::LoadEcomRecognizerL(TDriveUnit& aDrive,TUid aUid)
       
   288 	{
       
   289 	//Get implementation information of the recognizer with UID aUid
       
   290 	RImplInfoPtrArray implementationInfoArray;
       
   291 	TCleanupItem cleanup(CleanUpEComInfoArray, &implementationInfoArray);
       
   292 	CleanupStack::PushL(cleanup);
       
   293 	REComSession::ListImplementationsL(KUidDataRecogInterFaceUid,implementationInfoArray);
       
   294 	const TInt availCount = implementationInfoArray.Count();
       
   295 	CImplementationInformation* info=NULL;
       
   296 	TInt ii;
       
   297 	for (ii=0;ii<availCount;++ii)
       
   298 		{
       
   299 		info = implementationInfoArray[ii];
       
   300 		TUid uid = info->ImplementationUid();
       
   301 		
       
   302 		if(uid.iUid == aUid.iUid )
       
   303 			{
       
   304 			break;
       
   305 			}
       
   306 		}
       
   307 
       
   308 	//If the implementation not found just return
       
   309 	if(ii==availCount)
       
   310 		{
       
   311 		CleanupStack::PopAndDestroy(&implementationInfoArray);		
       
   312 		return;
       
   313 		}
       
   314 	
       
   315 	//Get the version of implementation
       
   316 	TInt version=info->Version();
       
   317 	
       
   318 	// check we haven't loaded this one already
       
   319 	TInt count=iRecognizerLib.Count();
       
   320 	TInt i;
       
   321 	CApaRecognizerDll* lib=NULL; // the previous one in the list
       
   322 	for (i=0; i<count; i++)
       
   323 		{
       
   324 		lib=iRecognizerLib[i];
       
   325 		if (lib->iId.iUid==aUid)
       
   326 			{// we may have to override one
       
   327 			if (lib->iPresent!=CApaRecognizerDll::ENotPresent)
       
   328 				{
       
   329 				CleanupStack::PopAndDestroy(&implementationInfoArray);				
       
   330 				return; // already found
       
   331 				}
       
   332 			else
       
   333 				{
       
   334 				if (lib->iVersion < version)
       
   335 					{
       
   336 					//Upgrade a plugin if a plugin with higher version than loaded plugin is installed.  
       
   337 					lib->iPresent = CApaRecognizerDll::ESuperseded;
       
   338 					break;
       
   339 					}
       
   340 				else
       
   341 					{
       
   342 					CleanupStack::PopAndDestroy(&implementationInfoArray);					
       
   343 					lib->iPresent = CApaRecognizerDll::EIsPresent;
       
   344 					return;
       
   345 					}
       
   346 				}
       
   347 			}			
       
   348 		}
       
   349 	//Remove the old version and let it continue to load new version
       
   350 	if ((i<count)&&(lib->iPresent==CApaRecognizerDll::ESuperseded))
       
   351 		{
       
   352 		RemoveRecognizer(lib);
       
   353 		}
       
   354 
       
   355 	// load the library
       
   356 	HBufC* recogName=info->DisplayName().AllocL();
       
   357 	CleanupStack::PushL(recogName);
       
   358 	lib=NULL;
       
   359 	lib = new(ELeave) CApaRecognizerDll(recogName);
       
   360 	CleanupStack::Pop(recogName);
       
   361 	CleanupStack::PushL(lib);
       
   362 	TRAPD(error,lib->iRec = CApaDataRecognizerType::CreateDataRecognizerL(aUid));
       
   363 	if(!error && lib->iRec)
       
   364 		{
       
   365 		lib->iPresent = CApaRecognizerDll::EIsPresent;
       
   366 		AddDataRecognizerTypeL(lib->iRec);
       
   367 		lib->iId.iUid = aUid;
       
   368 		lib->iId.iDrive = aDrive;
       
   369 		lib->iVersion=version;
       
   370 		iExtra->iObserver->SetUpdateCounter(iExtra->iObserver->UpdateCounter() + 1);
       
   371 		//
       
   372 		// add lib to the library list
       
   373 		iRecognizerLib.AppendL(lib);
       
   374 		CleanupStack::Pop(lib); // lib
       
   375 		}
       
   376 	else
       
   377 		{
       
   378 		CleanupStack::PopAndDestroy(lib); // lib
       
   379 		}
       
   380 	CleanupStack::PopAndDestroy(&implementationInfoArray);	
       
   381 	}
       
   382 #else
       
   383 void CApaScanningDataRecognizer::LoadEcomRecognizerL(TDriveUnit&,TUid)
       
   384 	{
       
   385 	}
       
   386 #endif
       
   387 
       
   388 void CApaScanningDataRecognizer::ScanForEcomRecognizersL()
       
   389 	{
       
   390 #ifdef USING_ECOM_RECOGS
       
   391 	//scan ecom plugin
       
   392 	RImplInfoPtrArray implementationArray;
       
   393 	TCleanupItem cleanup(CleanUpEComInfoArray, &implementationArray);
       
   394 	CleanupStack::PushL(cleanup);
       
   395 	REComSession::ListImplementationsL(KUidDataRecogInterFaceUid,implementationArray);
       
   396 	const TInt availCount = implementationArray.Count();
       
   397 	for (TInt count=0;count<availCount;++count)
       
   398 		{
       
   399 		const CImplementationInformation* info = implementationArray[count];
       
   400 		TUid uid = info->ImplementationUid();
       
   401 		TDriveUnit drive=info-> Drive();
       
   402 		TRAP_IGNORE(LoadEcomRecognizerL(drive,uid));
       
   403 		}
       
   404 	CleanupStack::PopAndDestroy(&implementationArray);
       
   405 #endif
       
   406 	}
       
   407 
       
   408 /**
       
   409 @internalTechnology
       
   410 */
       
   411 EXPORT_C void CApaScanningDataRecognizer::LoadRecognizersL()
       
   412 	{
       
   413 	iExtra->iRecognizersLoaded = ETrue;
       
   414 	ScanForRecognizersL();
       
   415 	UpdateDataTypesL();
       
   416 	}
       
   417 
       
   418 /**
       
   419 @internalTechnology
       
   420 */
       
   421 EXPORT_C void CApaScanningDataRecognizer::UnloadRecognizers()
       
   422 	{	
       
   423 	iExtra->iRecognizersLoaded = EFalse;
       
   424 	DestroyRecognizerList();
       
   425 	iRecognizerLib.ResetAndDestroy();
       
   426 	}
       
   427