appfw/apparchitecture/apserv/APSRECUTIL.CPP
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     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 // Classes for synchronous and asynchronous file recognitions (of a directory)
       
    15 // 
       
    16 // apsrecutil.cpp
       
    17 //
       
    18 
       
    19 
       
    20 #include "APSRECUTIL.H"
       
    21 #include "APSRECCACHE.h"
       
    22 
       
    23 //
       
    24 // CRecognitionResult
       
    25 //
       
    26 
       
    27 CRecognitionResult* CRecognitionResult::NewL(const TDesC& aFileName, const TDataRecognitionResult& aRecognitionResult)
       
    28 	{
       
    29 	ASSERT(aFileName.Find(_L("\\")) == KErrNotFound); // the filename should not contain any path
       
    30 
       
    31 	HBufC* fileName = aFileName.AllocLC();
       
    32 	HBufC8* dataType = aRecognitionResult.iDataType.Des8().AllocLC();	
       
    33 	CRecognitionResult* self = new(ELeave) CRecognitionResult(fileName, dataType, aRecognitionResult.iDataType.Uid(), aRecognitionResult.iConfidence);
       
    34 	CleanupStack::Pop(2, fileName);
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 CRecognitionResult::CRecognitionResult(HBufC* aFileName, HBufC8* aDataType, TUid aUid, TInt aConfidence)
       
    39 	: iFileName(aFileName),
       
    40 	  iDataType(aDataType),
       
    41  	  iUid(aUid), 
       
    42 	  iConfidence(aConfidence)
       
    43 	{
       
    44 	}
       
    45 
       
    46 CRecognitionResult::~CRecognitionResult()
       
    47 	{
       
    48 	delete iFileName;
       
    49 	delete iDataType;
       
    50 	}
       
    51 
       
    52 void CRecognitionResult::Get(TDataRecognitionResult& aRecognitionResult)
       
    53 	{
       
    54 	if(iUid == KNullUid)
       
    55 		aRecognitionResult.iDataType = TDataType(*iDataType);
       
    56 	else
       
    57 		aRecognitionResult.iDataType = TDataType(iUid);
       
    58 	
       
    59 	aRecognitionResult.iConfidence = iConfidence;
       
    60 	}
       
    61 	
       
    62 
       
    63 
       
    64 TUint CRecognitionResult::GetRequiredBufferSize() const
       
    65 	{
       
    66 	return iFileName->Size() + iDataType->Size() + sizeof(TUid) + sizeof(TInt) + 2;
       
    67 	}
       
    68 
       
    69 void CRecognitionResult::WriteToStreamL(RWriteStream& aWriteStream)
       
    70 	{
       
    71 	aWriteStream.WriteUint8L(iFileName->Length());
       
    72 	aWriteStream.WriteL(*iFileName);
       
    73 	aWriteStream.WriteUint8L(iDataType->Length());
       
    74 	aWriteStream.WriteL(*iDataType);
       
    75 	aWriteStream.WriteInt32L(iUid.iUid);
       
    76 	aWriteStream.WriteInt32L(iConfidence);
       
    77 	}
       
    78 	
       
    79 //
       
    80 // CDirectoryRecognitionResult
       
    81 //
       
    82 
       
    83 CDirectoryRecognitionResult::CDirectoryRecognitionResult(HBufC* aPath, HBufC8* aDataTypeFilter)
       
    84 	: iPath(aPath), iDataTypeFilter(aDataTypeFilter)
       
    85 	{
       
    86 	iRequiredBufferSize = sizeof(TUint);
       
    87 	}
       
    88 
       
    89 CDirectoryRecognitionResult::~CDirectoryRecognitionResult()
       
    90 	{
       
    91 	delete iPath;
       
    92 	delete iDataTypeFilter;
       
    93 	
       
    94 	const TInt count = iEntries.Count();
       
    95 	for(TInt i = 0; i < count; i++)
       
    96 		iEntries[i]->Close();
       
    97 
       
    98 	iEntries.Reset();
       
    99 	}
       
   100 
       
   101 void CDirectoryRecognitionResult::AppendL(CRecognitionResult* aEntry)
       
   102 	{
       
   103 	if(iDataTypeFilter)
       
   104 		{
       
   105 		if(aEntry->DataType().Match(*iDataTypeFilter) == KErrNotFound)
       
   106 			{
       
   107 			aEntry->Close(); //if we don't take ownership we must decrease reference count
       
   108 			return;
       
   109 			}
       
   110 		}
       
   111 
       
   112 	iEntries.AppendL(aEntry);
       
   113 	iRequiredBufferSize += aEntry->GetRequiredBufferSize();
       
   114 	} //lint !e818: Pointer parameter could be declared as pointing to const
       
   115 
       
   116 void CDirectoryRecognitionResult::WriteToStreamL(RWriteStream& aWriteStream)
       
   117 	{
       
   118 	aWriteStream.WriteUint32L(iEntries.Count());
       
   119 	for (TInt i=0; i < iEntries.Count(); i++)
       
   120 		iEntries[i]->WriteToStreamL(aWriteStream);
       
   121 		
       
   122 	aWriteStream.CommitL();
       
   123 	}
       
   124 
       
   125 //
       
   126 // CFileRecognitionUtility
       
   127 //
       
   128 
       
   129 CFileRecognitionUtility::CFileRecognitionUtility(CApaAppArcServer& aServer, TInt aMaxBufSize, RFs& aFs)
       
   130 	: CActive(0), 
       
   131 	  iServer(aServer), 
       
   132 	  iMaxBufSize(aMaxBufSize), 
       
   133 	  iFs(aFs),
       
   134 	  iIndex(0),
       
   135 	  iStep(0)
       
   136 	{
       
   137 	CActiveScheduler::Add(this);
       
   138 	}
       
   139 
       
   140 CFileRecognitionUtility::~CFileRecognitionUtility()
       
   141 	{
       
   142 	Cancel();
       
   143 	delete iEntryList;
       
   144 	
       
   145 	iPath = NULL;
       
   146 	iResult = NULL;
       
   147 	}
       
   148 
       
   149 void CFileRecognitionUtility::RecognizeSynchronouslyL(CDirectoryRecognitionResult& aResult)
       
   150 	{
       
   151 	iPath = &(aResult.Path());
       
   152 	iResult = &aResult;
       
   153 
       
   154 	User::LeaveIfError(iFs.IsValidName(iPath->Des()));
       
   155 
       
   156 	ReadDirectoryL();
       
   157 
       
   158 	// recognize files and calculate size of buffer
       
   159 	for(iIndex=0;iIndex<iEntryList->Count();iIndex++)
       
   160 		RecognizeFileL();
       
   161 		
       
   162 	delete iEntryList;
       
   163 	iEntryList = NULL;
       
   164 	}
       
   165 
       
   166 void CFileRecognitionUtility::RecognizeAsynchronously(CDirectoryRecognitionResult& aResult, const RMessage2& aMessage)
       
   167 	{
       
   168 	iIndex = 0;
       
   169 	iStep = 0;
       
   170 	iPath = &(aResult.Path());
       
   171 	iResult = &aResult;
       
   172 	iMessage = aMessage;
       
   173 
       
   174 	NextStep();
       
   175 	}
       
   176 
       
   177 void CFileRecognitionUtility::ReadDirectoryL()
       
   178 	{
       
   179 	delete iEntryList;
       
   180 	iEntryList = NULL;
       
   181 
       
   182 	// read directory content
       
   183 	User::LeaveIfError(iFs.GetDir(iPath->Des(),KEntryAttNormal,EDirsAnyOrder,iEntryList));
       
   184 	}
       
   185 	
       
   186 TInt CFileRecognitionUtility::BufferSizeL() const
       
   187 	{
       
   188 	const TInt preferredBufferSize = iServer.DataRecognizerPreferredBufSizeL();
       
   189 	TInt bufSize = Min(preferredBufferSize,iMaxBufSize);
       
   190 	if (bufSize < 1)
       
   191 		bufSize = 8; // sensible minimum.
       
   192 
       
   193 	return bufSize;
       
   194 	}
       
   195 
       
   196 void CFileRecognitionUtility::RecognizeFileL()
       
   197 	{
       
   198 	TFileName fullFileName(*iPath);
       
   199 	fullFileName.Append((*iEntryList)[iIndex].iName);
       
   200 	RFile file;
       
   201 	User::LeaveIfError(file.Open(iFs, fullFileName, EFileRead | EFileShareReadersOnly));
       
   202 	CleanupClosePushL(file);
       
   203 	
       
   204 	CRecognitionResult* result = NULL;
       
   205 	TRAP_IGNORE(result = iServer.RecognizeDataAsCRecognitionResultL(file, BufferSizeL()));
       
   206 	if(result)
       
   207 		{
       
   208 		CleanupClosePushL(*result);
       
   209 		iResult->AppendL(result);
       
   210 		CleanupStack::Pop(result);
       
   211 		}
       
   212 	
       
   213 	CleanupStack::PopAndDestroy(&file);
       
   214 	}
       
   215 
       
   216 void CFileRecognitionUtility::CancelRecognitionRequest()
       
   217 	{
       
   218 	if(iStep <= 1)
       
   219 		{
       
   220 		Cancel();
       
   221 		iMessage.Complete(KErrCancel);	
       
   222 		}
       
   223 	}
       
   224 
       
   225 void CFileRecognitionUtility::NextStep()
       
   226 	{
       
   227 	TRequestStatus* pStatus = &iStatus;
       
   228 	User::RequestComplete(pStatus, KErrNone);
       
   229 	SetActive();
       
   230 	}
       
   231 
       
   232 void CFileRecognitionUtility::RunL()
       
   233 	{
       
   234 	switch(iStep)
       
   235 		{
       
   236 		case 0:		// first step: read the directory content
       
   237 			ReadDirectoryL();
       
   238 			iIndex=0;
       
   239 			iStep++;
       
   240 			NextStep();
       
   241 			break;
       
   242 		case 1:		// read current file and recognize it
       
   243 			if(iIndex < iEntryList->Count())
       
   244 				{
       
   245 				RecognizeFileL();
       
   246 				iIndex++;
       
   247 				NextStep();
       
   248 				}
       
   249 			else	// finished
       
   250 				{
       
   251 				iMessage.WriteL(1,TPckgBuf<TUint>(iResult->RequiredBufferSize()));
       
   252 				iMessage.Complete(KErrNone);
       
   253 				iStep++;	// to show that we have completed the request
       
   254 				delete iEntryList;
       
   255 				iEntryList = NULL;
       
   256 				}
       
   257 			break;
       
   258 		default:
       
   259 			break;
       
   260 		}
       
   261 	}
       
   262 
       
   263 void CFileRecognitionUtility::DoCancel()
       
   264 	{
       
   265 	iIndex = 0;
       
   266 	iStep = 0;
       
   267 	}
       
   268