landmarksui/engine/src/CLmkParser.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2002-2005 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:   This file contains methods which interact with Landmarks Framework
       
    15 *                parser which does the job of parsing landmarks in the received
       
    16 *                landmark package
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 // INCLUDE FILES
       
    27 
       
    28 #include "EPos_CPosLandmarkParser.h"
       
    29 #include "EPos_CPosLandmarkDatabase.h"
       
    30 #include "EPos_CPosLandmark.h"
       
    31 #include "landmarks.hrh"
       
    32 #include "LmkFileUtils.h"
       
    33 #include "CLmkAOOperation.h"
       
    34 #include "CLmkDbUtils.h"
       
    35 #include "CLmkParser.h"
       
    36 #include "CLmkParseAllWrapper.h"
       
    37 #include <apgcli.h>
       
    38 #include <lmkerrors.h>
       
    39 
       
    40 // CONSTANTS
       
    41 /// Unnamed namespace for local definitions
       
    42 namespace {
       
    43 
       
    44 #if defined(_DEBUG)
       
    45 _LIT( KPanicMsg, "CLmkParser" );
       
    46 
       
    47 void Panic( TPanicCode aReason )
       
    48     {
       
    49     User::Panic( KPanicMsg, aReason );
       
    50     }
       
    51 #endif
       
    52 
       
    53 _LIT( KLmkTempDataFile, "c:\\system\\Temp\\LmkTemp.lmx" );
       
    54 
       
    55 }  // namespace
       
    56 
       
    57 // ============================ MEMBER FUNCTIONS ===============================
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CLmkParser::CLmkParser
       
    61 // C++ default constructor can NOT contain any code, that
       
    62 // might leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CLmkParser::CLmkParser(
       
    66 	CPosLandmarkDatabase& aDb,
       
    67 	MLmkAOOperationObserver& aObserver,
       
    68 	TBool aProgressNote )
       
    69 	: iDb( aDb ),
       
    70 	  iObserver( &aObserver ),
       
    71 	  iProgressNote( aProgressNote )
       
    72 	{
       
    73 	}
       
    74 
       
    75 // ---------------------------------------------------------
       
    76 // CLmkParser::NewL
       
    77 // ---------------------------------------------------------
       
    78 //
       
    79 EXPORT_C CLmkParser* CLmkParser::NewL(
       
    80 	const TDesC& aFile,
       
    81 	CPosLandmarkDatabase& aDb,
       
    82 	MLmkAOOperationObserver& aObserver,
       
    83 	TBool aProgressNote )
       
    84 	{
       
    85 	CLmkParser* self =
       
    86         new ( ELeave ) CLmkParser( aDb, aObserver, aProgressNote );
       
    87 	CleanupStack::PushL( self );
       
    88 	self->ConstructL( aFile );
       
    89 	CleanupStack::Pop(); //self
       
    90     return self;
       
    91 	}
       
    92 
       
    93 // ---------------------------------------------------------
       
    94 // CLmkParser::NewL
       
    95 // ---------------------------------------------------------
       
    96 //
       
    97 EXPORT_C CLmkParser* CLmkParser::NewL(
       
    98 	RFile& aFile,
       
    99 	CPosLandmarkDatabase& aDb,
       
   100 	MLmkAOOperationObserver& aObserver,
       
   101 	TBool aProgressNote )
       
   102 	{
       
   103 	CLmkParser* self =
       
   104         new ( ELeave ) CLmkParser( aDb, aObserver, aProgressNote );
       
   105 	CleanupStack::PushL( self );
       
   106 	self->ConstructL( aFile );
       
   107 	CleanupStack::Pop(); //self
       
   108 	return self;
       
   109 	}
       
   110 
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // CLmkParser::ConstructL()
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 void CLmkParser::ConstructL( const TDesC& aFile )
       
   117 	{
       
   118 
       
   119 	iDbUtils = CLmkDbUtils::NewL( iDb, *this, iProgressNote );
       
   120 	RFs fs;
       
   121 	User::LeaveIfError(fs.Connect());
       
   122 	CleanupClosePushL(fs);
       
   123 
       
   124 	RFile file;
       
   125 	LmkFileUtils::DeleteFileL(KLmkTempDataFile);
       
   126 	User::LeaveIfError(file.Create(fs, KLmkTempDataFile, EFileShareAny));
       
   127 	CleanupClosePushL(file);
       
   128 
       
   129 	RFile fileHandle;
       
   130 	User::LeaveIfError(fileHandle.Open(fs, aFile, EFileShareAny));
       
   131 	CleanupClosePushL(fileHandle);
       
   132 
       
   133 	TDataType mimeType;
       
   134 	User::LeaveIfError(ResolveMimeTypeL(fileHandle,mimeType));
       
   135 	iParser = CPosLandmarkParser::NewL(  mimeType.Des8());
       
   136 
       
   137 	HBufC8* tempBuf = HBufC8::NewLC(5000);  // to manage for 5 Landmarks at the most
       
   138 	TPtr8 ptr = tempBuf->Des();
       
   139 	User::LeaveIfError(fileHandle.Read( ptr ));
       
   140 	User::LeaveIfError(file.Write( ptr ));
       
   141 
       
   142 	CleanupStack::PopAndDestroy(); // File
       
   143 	CleanupStack::PopAndDestroy(1); //tempBuf
       
   144 
       
   145 	iParser->SetInputFileL( KLmkTempDataFile);
       
   146 	iParseWrapper = CLmkParseAllWrapper::NewL( *iParser, *this, iDb );
       
   147 	iFileName = HBufC::NewL(256);
       
   148 	*iFileName = KLmkTempDataFile;
       
   149 	iFileHandleFlag = EFalse;
       
   150 
       
   151     CleanupStack::PopAndDestroy(); // File Handle
       
   152 	CleanupStack::PopAndDestroy(); // fs
       
   153 	}
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CLmkParser::ConstructL()
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void CLmkParser::ConstructL(RFile& aFile )
       
   160 	{
       
   161 	iDbUtils = CLmkDbUtils::NewL( iDb, *this, iProgressNote );
       
   162 
       
   163 	TDataType mimeType;
       
   164 	User::LeaveIfError(ResolveMimeTypeL(aFile,mimeType));
       
   165 	iParser = CPosLandmarkParser::NewL(  mimeType.Des8());
       
   166 	iParser->SetInputFileHandleL(aFile);
       
   167 	iParseWrapper = CLmkParseAllWrapper::NewL( *iParser, *this, iDb );
       
   168 
       
   169 	iFileHandle = &aFile;
       
   170 	iFileHandleFlag = ETrue;
       
   171 	}
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CLmkParser::~CLmkParser()
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 CLmkParser::~CLmkParser()
       
   178 	{
       
   179 	delete iAOOperation;
       
   180 	delete iParser;
       
   181 	delete iParseWrapper;
       
   182 	delete iDbUtils;
       
   183 	delete iFileName;
       
   184 	iFileHandle = NULL;
       
   185 	}
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CLmkParser::HandleOperationL
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CLmkParser::HandleOperationL(
       
   192 TOperationTypes aType,
       
   193 TReal32 aProgress,
       
   194 TInt aStatus )
       
   195 	{
       
   196 	// Initialize operation is not done with iAOOperation so in that case
       
   197 	// we don't delete iAOOperation.
       
   198 	if ( aType != EInitialize && aStatus != KPosLmOperationNotComplete )
       
   199 		{
       
   200 		/* Parsing is done with iAOOperation when wrapper isn't used anymore.
       
   201 		 * Remove the following if-statement when removing wrapper
       
   202 		 */
       
   203 		if( aType != EParse )
       
   204 			{
       
   205 			__ASSERT_DEBUG( iAOOperation, Panic( KLmkPanicNullMember ) );
       
   206 
       
   207 			delete iAOOperation;
       
   208 			iAOOperation = NULL;
       
   209 			}
       
   210 		}
       
   211 	__ASSERT_DEBUG( iObserver, Panic( KLmkPanicNullMember ) );
       
   212 	iObserver->HandleOperationL( aType, aProgress, aStatus );
       
   213 	}
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CLmkParser::FileName
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 EXPORT_C TPtrC CLmkParser::FileName()
       
   220 	{
       
   221 	TInt error = KErrNone;
       
   222 	if( !iFileName )
       
   223 		{
       
   224 		TRAP( error,iFileName = HBufC::NewL(256));
       
   225 		}
       
   226 	if( !error )
       
   227 		{
       
   228 		TPtr ptr = iFileName->Des();
       
   229 		iFileHandle->Name( ptr );
       
   230 		return *iFileName;
       
   231 		}
       
   232 	return TPtrC();
       
   233 	}
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // CLmkParser::FileHandle
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 EXPORT_C RFile& CLmkParser::FileHandle()
       
   240 	{
       
   241 	return *iFileHandle;
       
   242 	}
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CLmkParser::IsFileHandle
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 EXPORT_C TBool CLmkParser::IsFileHandle()
       
   249     {
       
   250     return iFileHandleFlag;
       
   251     }
       
   252 
       
   253 // -----------------------------------------------------------------------------
       
   254 // CLmkParser::InitializeDbL
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 EXPORT_C void CLmkParser::InitializeDbL()
       
   258 	{
       
   259 	iDbUtils->InitializeDbL();
       
   260 	}
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // CLmkParser::StartParsingL
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 EXPORT_C void CLmkParser::StartParsingL()
       
   267 	{
       
   268 	LeaveIfInUseL();
       
   269 	iParseWrapper->ParseAllL();
       
   270 	}
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CLmkParser::NumOfParsedLandmarks
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 EXPORT_C TInt CLmkParser::NumOfParsedLandmarks()
       
   277 	{
       
   278 	return iParseWrapper->NumOfLandmarks();
       
   279 	}
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CLmkParser::PackageName
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 EXPORT_C TPtrC CLmkParser::PackageName()
       
   286 	{
       
   287 	_LIT( KDot, "." );
       
   288 	TPtrC ptr = iParser->CollectionData( EPosLmCollDataCollectionName );
       
   289 	if( ptr.Length() == 0)
       
   290 		{
       
   291 		//get the position of '.' character
       
   292 		TInt pos = FileName().Find(KDot);
       
   293 		if( pos != KErrNotFound)
       
   294 			{
       
   295 			//get the left most string starting from '.' char
       
   296 			return FileName().Left( pos );
       
   297 			}
       
   298 		else
       
   299 			{
       
   300 			return FileName();
       
   301 			}
       
   302 		}
       
   303 	return ptr;
       
   304 	}
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // CLmkParser::LandmarkLC
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 EXPORT_C CPosLandmark* CLmkParser::LandmarkLC( TInt aIndex )
       
   311 	{
       
   312 	return iParseWrapper->LandmarkLC( aIndex );
       
   313 	}
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CLmkParser::LmkLandmarkLC
       
   317 // -----------------------------------------------------------------------------
       
   318 //
       
   319 EXPORT_C CLmkLandmark* CLmkParser::LmkLandmarkLC( TInt aIndex )
       
   320 	{
       
   321 	return iParseWrapper->LmkLandmarkLC(aIndex);
       
   322 	}
       
   323 // -----------------------------------------------------------------------------
       
   324 // CLmkParser::CategoryNameLC
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 EXPORT_C CLmkLandMarkCategoriesName* CLmkParser::CategoryNameLC( TInt aIndex )
       
   328     {
       
   329     /* THIS FUNCTIONALITY WILL BE USED WHEN WRAPPER IS REMOVED:
       
   330     return iParser->LandmarkLC( aIndex );
       
   331     */
       
   332     return iParseWrapper->CategoryNameLC( aIndex );
       
   333     }
       
   334 
       
   335 //
       
   336 // -----------------------------------------------------------------------------
       
   337 // CLmkParser::ChangeObserver
       
   338 // -----------------------------------------------------------------------------
       
   339 //
       
   340 EXPORT_C void CLmkParser::ChangeObserver(
       
   341 	MLmkAOOperationObserver& aNewObserver )
       
   342 	{
       
   343 	iObserver = &aNewObserver;
       
   344 	}
       
   345 
       
   346 // -----------------------------------------------------------------------------
       
   347 // CLmkParser::StartImportingL
       
   348 // -----------------------------------------------------------------------------
       
   349 //
       
   350 EXPORT_C void CLmkParser::StartImportingL(TBool aIncludeNewCategories)
       
   351 	{
       
   352 	LeaveIfInUseL();
       
   353 
       
   354 	CPosLandmarkDatabase::TTransferOptions options;
       
   355 
       
   356     if ( aIncludeNewCategories )
       
   357 	    {
       
   358 	    options = CPosLandmarkDatabase::EIncludeCategories;
       
   359 	    }
       
   360 	else
       
   361 		{
       
   362 		options =  (CPosLandmarkDatabase::EIncludeCategories) |
       
   363 		           (CPosLandmarkDatabase::ESupressCategoryCreation);
       
   364 		}
       
   365 
       
   366 	CPosLmOperation* operation =
       
   367 	    iDb.ImportLandmarksL( *iParser, options );
       
   368 	CleanupStack::PushL( operation );
       
   369 	iAOOperation = CLmkAOOperation::NewL( operation,
       
   370 	                                      *this,
       
   371 	                                      EImport,
       
   372 	                                      iProgressNote );
       
   373 	CleanupStack::Pop( operation ); // ownership transferred
       
   374 
       
   375 	iAOOperation->StartOperation();
       
   376 	}
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // CLmkParser::StartImportingL
       
   380 // -----------------------------------------------------------------------------
       
   381 //
       
   382 EXPORT_C void CLmkParser::StartImportingL(
       
   383 	const RArray<TUint>& aSelected, TBool aIncludeNewCategories)
       
   384 	{
       
   385 	LeaveIfInUseL();
       
   386 
       
   387     CPosLandmarkDatabase::TTransferOptions options;
       
   388 
       
   389     if ( aIncludeNewCategories )
       
   390 	    {
       
   391 	    options = CPosLandmarkDatabase::EIncludeCategories;
       
   392 	    }
       
   393 	else
       
   394 		{
       
   395 		options = (CPosLandmarkDatabase::EIncludeCategories) | (CPosLandmarkDatabase::ESupressCategoryCreation);
       
   396 		}
       
   397 
       
   398 	CPosLmOperation* operation =
       
   399 	    iDb.ImportLandmarksL( *iParser, aSelected, options );
       
   400 	CleanupStack::PushL( operation );
       
   401 	iAOOperation = CLmkAOOperation::NewL( operation,
       
   402 	                                      *this,
       
   403 	                                      EImport,
       
   404 	                                      iProgressNote );
       
   405 	CleanupStack::Pop( operation ); // ownership transferred
       
   406 
       
   407 	iAOOperation->StartOperation();
       
   408 	}
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // CLmkParser::CancelImporting
       
   412 // -----------------------------------------------------------------------------
       
   413 //
       
   414 EXPORT_C void CLmkParser::CancelImporting()
       
   415 	{
       
   416 	if ( iAOOperation )
       
   417 		{
       
   418 		iAOOperation->Cancel();
       
   419 		delete iAOOperation;
       
   420 		iAOOperation = NULL;
       
   421 		}
       
   422 	}
       
   423 
       
   424 // -----------------------------------------------------------------------------
       
   425 // CLmkParser::LeaveIfInUseL
       
   426 // -----------------------------------------------------------------------------
       
   427 //
       
   428 void CLmkParser::LeaveIfInUseL()
       
   429 	{
       
   430 	if ( iAOOperation )
       
   431 		{
       
   432 		User::Leave( KErrInUse );
       
   433 		}
       
   434 	}
       
   435 
       
   436 // -----------------------------------------------------------------------------
       
   437 // CLmkParser::ResolveMimeTypeL
       
   438 // -----------------------------------------------------------------------------
       
   439 //
       
   440 TInt CLmkParser::ResolveMimeTypeL( RFile& aFile, TDataType& aDataType ) const
       
   441 	{
       
   442 	TInt result(KErrNotFound);
       
   443 	RApaLsSession ls ;
       
   444 	TDataRecognitionResult dataType;
       
   445 	TInt err( ls.Connect() );
       
   446 	if ( err == KErrNone )
       
   447 		{
       
   448 		CleanupClosePushL( ls );
       
   449 		err = ls.RecognizeData(aFile, dataType);
       
   450 		if(err == KErrNone &&
       
   451 		   (dataType.iConfidence == CApaDataRecognizerType::EProbable
       
   452 		    || dataType.iConfidence == CApaDataRecognizerType::ECertain
       
   453 		    || dataType.iConfidence == CApaDataRecognizerType::EPossible))
       
   454 			{
       
   455 			aDataType = dataType.iDataType;
       
   456 			result = KErrNone;
       
   457 			}
       
   458 		CleanupStack::PopAndDestroy(&ls);
       
   459 		}
       
   460 	return result;
       
   461 	}
       
   462 //  End of File