locationcentre/lcserver/src/lcregistry.cpp
changeset 15 13ae750350c9
parent 0 522cd55cc3d7
child 17 0f22fb80ebba
equal deleted inserted replaced
0:522cd55cc3d7 15:13ae750350c9
     1 /*
       
     2 * Copyright (c) 2007 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:  Location Centre Server object.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include <PathInfo.h>
       
    21 #include <apgcli.h>
       
    22 #include <documenthandler.h>            // CDocumentHandler
       
    23 #include <pathinfo.h>
       
    24 
       
    25 // USER INCLUDES
       
    26 #include "lcregistry.h"
       
    27 #include "lcregistrationparser.h"
       
    28 #include "lcregistryupdate.h"
       
    29 #include "lcregistrationupdatenotifier.h"
       
    30 #include "lcregapporder.h"
       
    31 #include "lcregistryobserver.h"
       
    32 #include "lcregappinfo.h"
       
    33 #include "lcregappstore.h"
       
    34 #include "lcdebug.h"
       
    35 
       
    36 // CONSTANT DEFINTIONS
       
    37 //Forward declaration. Function for sorted ordering of appinfo array
       
    38 static TInt PositionDescOrdering( const CLcRegAppInfo& aAppInfo1, 
       
    39 			  				   	  const CLcRegAppInfo& aAppInfo2 );
       
    40 
       
    41 		  					   	  
       
    42 		  					   	  
       
    43 // ----- Member funtions for CLcRegistry ---------------------------------
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // CLcRegistry::CLcRegistry
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CLcRegistry::CLcRegistry( MLcRegistryObserver& aRegistryObserver )
       
    50 	:CActive(EPriorityStandard),
       
    51 	iRegistryObserver( aRegistryObserver )
       
    52     {
       
    53     // C++ Default constructor. No allocations or functions which can Leave
       
    54     // should be called from here.Initiallize all the variable here
       
    55     }
       
    56          
       
    57 // ---------------------------------------------------------------------------
       
    58 // CLcRegistry::~CLcRegistry
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CLcRegistry::~CLcRegistry()
       
    62     {
       
    63 	Cancel();
       
    64 	    
       
    65     // C++ Destructor. Free all resources associated with this class.
       
    66 	delete iLcRegUpdate;
       
    67 	delete iRegAppStore;
       
    68 	delete iLcRegAppOrder;
       
    69 	
       
    70 	//close the file server session.
       
    71 	iFs.Close();
       
    72 	// free filename array memory
       
    73 	iFileNameArry.Reset();		
       
    74 	iFileNameArry.Close();
       
    75 	
       
    76 	//delete all items from the appinfo array.
       
    77 	iAppInfoArray.ResetAndDestroy();
       
    78 	iAppInfoArray.Close();
       
    79 	
       
    80 	iAppInfoNewArray.ResetAndDestroy();
       
    81 	iAppInfoNewArray.Close();
       
    82     }
       
    83         
       
    84 // ---------------------------------------------------------------------------
       
    85 // CLcRegistry* CLcRegistry::NewL
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CLcRegistry* CLcRegistry::NewL( MLcRegistryObserver& aRegistryObserver )
       
    89     {
       
    90     CLcRegistry* self = NewLC( aRegistryObserver );
       
    91     CleanupStack::Pop( self );
       
    92     return self;         
       
    93     }
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CLcRegistry* CLcRegistry::NewLC
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 CLcRegistry* CLcRegistry::NewLC( MLcRegistryObserver& aRegistryObserver )
       
   100     {
       
   101     // Symbian Two phased constructor. Leaves the object on the Clean-up
       
   102     // stack.
       
   103     CLcRegistry* self = new ( ELeave )CLcRegistry( aRegistryObserver );
       
   104     CleanupStack::PushL( self );
       
   105     self->ConstructL();
       
   106     return self;         
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // void CLcRegistry::ConstructL
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 void CLcRegistry::ConstructL()
       
   114     {
       
   115     // connect to file server
       
   116     // and pass this session to all the class required.
       
   117 	User::LeaveIfError(iFs.Connect());
       
   118     
       
   119     // creation of dynamic registration notifier object
       
   120     iLcRegUpdate	= CLcRegistryUpdate::NewL( *this, iFs);
       
   121 
       
   122     // creation of private info object
       
   123     iRegAppStore	= CLcRegAppStore::NewL(iFs); 
       
   124 
       
   125     // creation of application order settings object
       
   126     iLcRegAppOrder	= CLcRegAppOrder::NewL( *iRegAppStore );
       
   127 
       
   128 	// reset the new applist array count here
       
   129 	iAppInfoNewArray.Reset();    
       
   130 	/* Now Check if any update needed to that list.
       
   131 	 * To do that start all the system drive scan and collect
       
   132 	 * all the registered file names
       
   133 	 */     
       
   134 	iRegistryHasToUpdate = EFalse;
       
   135 
       
   136 	// registered for notification in private directory from file server
       
   137     iLcRegUpdate->StartFileNotifation();
       
   138 	// Add it to Active scheduler
       
   139     CActiveScheduler::Add( this );    
       
   140     // issue the request to active scheduker
       
   141     CheckForUpdationL();
       
   142     }  
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // void CLcRegistry::CheckForUpdationL()
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CLcRegistry::CheckForUpdationL()
       
   149     {
       
   150     // inform registry engine that registration process is under updation
       
   151     iRegistryObserver.RegistryUnderUpdation();
       
   152     // set the file count to zero
       
   153     iregFileCount = 0;
       
   154 
       
   155     // Scan all the drives and get all the registered file names
       
   156 	ScanDrivesAndGetFileNameL();
       
   157 
       
   158     DEBUG3("Updated File list : %d", iFileNameArry.Count(), 0, 0 );
       
   159     
       
   160 	// issue a request to active scheduler
       
   161 	IssueRequest();
       
   162     }  
       
   163 
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // void CLcRegistry::IssueRequest
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CLcRegistry::IssueRequest()
       
   170     {
       
   171 	if(!IsActive())
       
   172 		{
       
   173 		SetActive();
       
   174 		TRequestStatus *status = &iStatus;
       
   175 		User::RequestComplete(status,KErrNone);			
       
   176 		}
       
   177     }  
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // TInt CLcRegistry::GetAllRegisteredAppsList
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 TInt CLcRegistry::GetAllRegisteredAppsList( RPointerArray<CLcAppInfo>& appInfoArray )
       
   184     {
       
   185     //DEBUG( "+ CLcRegistry::GetAllRegisteredAppsList");
       
   186         
       
   187     TInt error = KErrNone;
       
   188     if( iAppInfoArray.Count() <= 0 )
       
   189 	    {
       
   190 	    error = KErrNotFound;	
       
   191 	    }
       
   192     else
       
   193 	    {
       
   194 	    for(TInt i = 0; i < iAppInfoArray.Count(); i++)
       
   195 		    {
       
   196 		    // check wheather application is present
       
   197 		    // append only present application
       
   198 		    if( iAppInfoArray[i]->IsAppPresent() )
       
   199 			    {
       
   200 				error = appInfoArray.Append( iAppInfoArray[i] );
       
   201 				if ( error )
       
   202     				{
       
   203     				break;
       
   204     				}
       
   205 			    }
       
   206 		    }
       
   207 	    }	    
       
   208 	DEBUG3("Get Registered Apps : %d", appInfoArray.Count(), 0, 0 );
       
   209     //DEBUG( "- CLcRegistry::GetAllRegisteredAppsList");    
       
   210 	return error;
       
   211     }  
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // TInt CLcRegistry::GetApplicationInfo
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 TInt CLcRegistry::GetApplicationInfo( const TDesC& 	aAppUid,
       
   218 									  CLcBasicAppInfo*&	aLcRegAppInfo )   
       
   219 	{
       
   220     //DEBUG( "+ CLcRegistry::GetApplicationInfo");
       
   221 
       
   222 	for(TInt i = 0; i< iAppInfoArray.Count(); i++)
       
   223 		{
       
   224 		if( !aAppUid.Compare( iAppInfoArray[i]->Id()) &&
       
   225 		    iAppInfoArray[i]->IsAppPresent())		
       
   226 			{
       
   227 			aLcRegAppInfo = iAppInfoArray[i];
       
   228 			return KErrNone;	
       
   229 			}
       
   230 		}
       
   231 		
       
   232     //DEBUG( "- CLcRegistry::GetApplicationInfo");
       
   233     		
       
   234 	return KErrNotFound;		
       
   235 	}
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // void CLcRegistry::HandleDynamicRegistrationL
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 void CLcRegistry::HandleDynamicRegistrationL()
       
   242     {
       
   243     DEBUG("+CLcRegistry::HandleDynamicRegistrationL");
       
   244     iRegistryHasToUpdate = ETrue;
       
   245 	CheckForUpdationL();
       
   246     }  
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // void CLcService::ScanDrivesAndGetFileNameL
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 void CLcRegistry::ScanDrivesAndGetFileNameL()
       
   253     {
       
   254     DEBUG( "+ CLcRegistry::ScanDrivesAndGetFileNameL");    
       
   255     
       
   256     iFileNameArry.Reset();
       
   257 	//declare the directory to be scan through
       
   258 	_LIT(KPriDirName, ":\\private\\10283138\\import\\" );
       
   259     TFileName srcDirName( KPriDirName );
       
   260 	TDriveList dirList;
       
   261 	
       
   262     if(iFs.DriveList( dirList ) != KErrNone)
       
   263 	    {
       
   264 	    return;	
       
   265 	    }
       
   266 
       
   267     TBuf<KMaxFileName> rootDir;
       
   268     //creating the CDirscan object
       
   269     CDirScan* scanner = CDirScan::NewLC(iFs); 
       
   270     TChar driveLetter = '?';
       
   271     TDriveInfo info;
       
   272     TInt error;
       
   273  
       
   274     //Scan for all drives in the system 
       
   275     TInt driveListSize = dirList.Length();
       
   276     for(TInt dir(0); dir < driveListSize; dir++)
       
   277         {
       
   278         error = iFs.Drive(info, dir);
       
   279         if  ((error == KErrNone) && 
       
   280             (dirList[dir] && info.iType != EMediaNotPresent))
       
   281             {
       
   282             RFs::DriveToChar(dir, driveLetter);
       
   283             rootDir.Zero();//Clear the previous contents                  
       
   284             rootDir.Append(driveLetter);
       
   285             rootDir.Append( srcDirName );
       
   286 
       
   287 			CDir *currentDir = NULL;
       
   288 			// Go through the files only
       
   289 			scanner->SetScanDataL( rootDir, KEntryAttNormal, ESortNone );
       
   290 			
       
   291 			TRAP( error, scanner->NextL( currentDir ));
       
   292 			if( error )
       
   293 				{
       
   294 				continue;
       
   295 				}
       
   296 			TInt entryCount = currentDir->Count();           
       
   297 		    while(entryCount--)
       
   298         		{
       
   299 			    TBuf<KMaxFileName> lookup;
       
   300 				TEntry entry=( *currentDir )[entryCount];
       
   301 				//Clear the previous values
       
   302 				lookup.Zero();
       
   303 				//Append next one
       
   304 				lookup.Append(scanner->FullPath());
       
   305 				lookup.Append(entry.iName);
       
   306 				//Store it in for future reference
       
   307 				iFileNameArry.Append(lookup);
       
   308         		}
       
   309 			delete currentDir;					
       
   310             }
       
   311         }
       
   312     CleanupStack::PopAndDestroy( scanner );
       
   313     
       
   314     DEBUG( "- CLcRegistry::ScanDrivesAndGetFileNameL");     
       
   315     }  
       
   316 
       
   317 // ----------------------------------------------------------------------------
       
   318 // CLcRegistry::RunL
       
   319 // ----------------------------------------------------------------------------
       
   320 //
       
   321 void CLcRegistry::RunL()
       
   322     {
       
   323 	TUint mmcId 	= 0;
       
   324 	// This check is for if some update events come
       
   325 	// during registration updation going on.
       
   326 	if( iRegistryHasToUpdate )
       
   327 		{
       
   328 		DEBUG( "Dynamic notification recived");
       
   329 		// Add all the appinfo to current list from new created list
       
   330 		for( TInt i = 0; i < iAppInfoNewArray.Count(); i++ )
       
   331 			{
       
   332 			User::LeaveIfError( iAppInfoArray.Append( iAppInfoNewArray[i] ));
       
   333 			}
       
   334 		iAppInfoNewArray.Reset();		
       
   335 		// end of loop set the flag as EFalse			
       
   336 		iRegistryHasToUpdate = EFalse;
       
   337 		DEBUG( "Notify execute");
       
   338 		IssueRequest();
       
   339 		}
       
   340 	else
       
   341 		{
       
   342 		CLcRegAppInfo* regAppInfo = NULL;				
       
   343 		if( iregFileCount < iFileNameArry.Count() )
       
   344 			{
       
   345 			// Check wheather registration file name is present in the
       
   346 			// old registered list.If not then add as a new registration service
       
   347 			TBool lResult = DoesApplicationExists( iFileNameArry[ iregFileCount ],
       
   348 												   regAppInfo );
       
   349 			if ( lResult )
       
   350 				{
       
   351 				// Check if the Application suffered an Upgrade. If its an upgrade
       
   352 				// then its equivalent to a new installation.
       
   353 				if ( IsAppUpgraded( regAppInfo->FileName()))
       
   354 					{
       
   355 					// Remove the element from the Old array
       
   356 					TInt index = iAppInfoArray.Find( regAppInfo );
       
   357 					CLcRegAppInfo* info = iAppInfoArray[index];
       
   358 					iAppInfoArray.Remove( index );
       
   359 					delete info;
       
   360 					 
       
   361 					// Parse new registration info validate and append it to list
       
   362 					TRAPD( err, regAppInfo = 
       
   363 									CreateAppRegInfoL(iFileNameArry[ iregFileCount ]));
       
   364 					if( !err )
       
   365 						{
       
   366 						CleanupStack::PushL( regAppInfo );
       
   367 						InsertAppRegInfoL( regAppInfo );
       
   368 						CleanupStack::Pop( regAppInfo );
       
   369 						}
       
   370 					iregFileCount++;
       
   371 					IssueRequest();				
       
   372 					}
       
   373 				else 
       
   374 					{
       
   375 					// Check and Set the Present status of the Application
       
   376 					if( !regAppInfo->IsAppPresent() && 
       
   377 						!GetMmcId( mmcId ) && 
       
   378 						!mmcId == regAppInfo->MmcId())
       
   379 						{
       
   380 						// This condition is only when the Application was registered
       
   381 						// prior to this and was on an MMC which was not mounted
       
   382 						regAppInfo->SetFilePresenceStatus(ETrue);
       
   383 						}
       
   384 						
       
   385 					// Remove the element from the Old array
       
   386 					TInt index = iAppInfoArray.Find( regAppInfo );
       
   387 					iAppInfoArray.Remove( index );
       
   388 					// push the object to cleanup stack before calling 
       
   389 					// InsertAppRegInfoL.
       
   390 					CleanupStack::PushL( regAppInfo );
       
   391 					InsertAppRegInfoL( regAppInfo );
       
   392 					CleanupStack::Pop( regAppInfo );
       
   393 					
       
   394 					iregFileCount++;
       
   395 					IssueRequest();					
       
   396 					}				
       
   397 				}
       
   398 			else
       
   399 				{
       
   400 				// Parse new registration info validate and append it to list
       
   401 				TRAPD( err, regAppInfo = 
       
   402 								CreateAppRegInfoL( iFileNameArry[ iregFileCount ] ));
       
   403 				if( !err )
       
   404 					{
       
   405 					CleanupStack::PushL( regAppInfo );
       
   406 					InsertAppRegInfoL( regAppInfo );
       
   407 					CleanupStack::Pop( regAppInfo );
       
   408 					}
       
   409 				iregFileCount++;
       
   410 				IssueRequest();			
       
   411 				}			
       
   412 			}
       
   413 		else
       
   414 			{
       
   415 			// After completing if part if there is any application present 
       
   416 			// in the old array then check those application for absent or removal case.
       
   417 	    	if( iAppInfoArray.Count() > 0 )
       
   418 		    	{
       
   419 				//Get current MMC id.
       
   420 				if( GetMmcId( mmcId ) )
       
   421 					{
       
   422 					// Check for all Absent applications
       
   423 					while( iAppInfoArray.Count())
       
   424 						{
       
   425 						regAppInfo = iAppInfoArray[0];
       
   426 						iAppInfoArray.Remove( 0 );
       
   427 						
       
   428 						if( regAppInfo->MmcId() != mmcId )
       
   429 							{
       
   430 							regAppInfo->SetFilePresenceStatus(EFalse);
       
   431 							CleanupStack::PushL( regAppInfo );
       
   432 							InsertAppRegInfoL( regAppInfo );
       
   433 							CleanupStack::Pop( regAppInfo );
       
   434 							}
       
   435 						else
       
   436 							{							
       
   437 							iLcRegAppOrder->RemoveArrayL( regAppInfo->Id() );
       
   438 							delete regAppInfo;
       
   439 							}							
       
   440 						}
       
   441 					}
       
   442 				
       
   443 				// Check for all de-registered applications
       
   444 				while( iAppInfoArray.Count())
       
   445 					{
       
   446 					regAppInfo = iAppInfoArray[0];
       
   447 					iAppInfoArray.Remove( 0 );							
       
   448 					iLcRegAppOrder->RemoveArrayL( regAppInfo->Id() );
       
   449 					delete regAppInfo;							
       
   450 					}		
       
   451 		    	}
       
   452 			iAppInfoArray.ResetAndDestroy();
       
   453 			FinalRegistrationUpdateL();	    	
       
   454 			}		
       
   455 		} 	
       
   456     }
       
   457 
       
   458 // ---------------------------------------------------------------------------
       
   459 // CLcServerShutDownTimer::RunError
       
   460 // ---------------------------------------------------------------------------
       
   461 //        
       
   462 TInt CLcRegistry::RunError( TInt aError )
       
   463     {
       
   464     // Dont leave. Continue with the next operation
       
   465     return aError;
       
   466     }
       
   467 
       
   468 
       
   469 // ---------------------------------------------------------------------------
       
   470 // void CLcRegistry::FinalRegistrationUpdateL
       
   471 // ---------------------------------------------------------------------------
       
   472 //
       
   473 void CLcRegistry::FinalRegistrationUpdateL()
       
   474     {
       
   475     // Resync all the existing applications. Their order could have been
       
   476     // changed because of the possible changes in the index ordering
       
   477     
       
   478     // Temporary array to assist copying of all content from one place
       
   479     // to another
       
   480     RPointerArray<CLcRegAppInfo> tempArray;
       
   481     while ( iAppInfoArray.Count())
       
   482         {
       
   483         CLcRegAppInfo* info = iAppInfoArray[0];
       
   484         iAppInfoArray.Remove( 0 );
       
   485         tempArray.Append( info );
       
   486         }
       
   487     
       
   488     // Insert back all temporary elements back into the Info Array
       
   489     TInt appcount = tempArray.Count();
       
   490 	for( TInt i = 0; i < appcount; i++ )
       
   491 		{
       
   492 		User::LeaveIfError( iAppInfoArray.Append( tempArray[i] ));
       
   493 		}
       
   494 		
       
   495     // Now destroy all the elements back in the temporary array.
       
   496     tempArray.ResetAndDestroy();
       
   497     tempArray.Close();
       
   498     
       
   499 	// Add all the appinfo to current list from new created list
       
   500 	appcount = iAppInfoNewArray.Count();
       
   501 	for( TInt i = 0; i < appcount; i++ )
       
   502 		{
       
   503 		User::LeaveIfError(iAppInfoArray.Append( iAppInfoNewArray[i] ));
       
   504 		}
       
   505 	iAppInfoNewArray.Reset();		
       
   506 	
       
   507 	DEBUG1("New List Count : %d", iAppInfoArray.Count())
       
   508 			
       
   509 	// Send a notification to registry engine that updation completed
       
   510 	iRegistryObserver.RegistryUpdated();
       
   511     }  
       
   512 
       
   513 // ----------------------------------------------------------------------------
       
   514 // CLcRegistry::IsInMMc
       
   515 // ----------------------------------------------------------------------------
       
   516 //
       
   517 TBool CLcRegistry::IsInMMc( TFileName aFileName )
       
   518 	{
       
   519 	TBool lResult = EFalse;
       
   520 	TParse parse;
       
   521 	parse.Set(aFileName,NULL,NULL);
       
   522 	TPtrC lDrive = parse.Drive();
       
   523     TInt err,mmcDrive,fileDrive;
       
   524     err = RFs::CharToDrive( PathInfo::MemoryCardRootPath()[0], mmcDrive );    
       
   525     if (err == KErrNone)
       
   526         {
       
   527         err = RFs::CharToDrive( lDrive[0], fileDrive );    
       
   528         if( err == KErrNone )
       
   529 	        {
       
   530 	        if( mmcDrive == fileDrive )
       
   531 		        {
       
   532 				lResult = ETrue;	        	
       
   533 		        }
       
   534 	        }
       
   535         }
       
   536 	return lResult;		
       
   537 	}
       
   538 
       
   539 // ----------------------------------------------------------------------------
       
   540 // CLcRegistry::DoesApplicationExists
       
   541 // ----------------------------------------------------------------------------
       
   542 //
       
   543 TBool CLcRegistry::DoesApplicationExists( TFileName 		aFileName,
       
   544 										  CLcRegAppInfo*& 	alcRegAppInfoPtr )
       
   545 	{
       
   546 	// This function checks wheather registration info with same file name
       
   547 	// is present in the old list. If present then return ETrue or else EFalse.
       
   548 	TInt count = iAppInfoArray.Count();
       
   549 	for( TInt i = 0; i < count; i++ )
       
   550 		{
       
   551 		alcRegAppInfoPtr = iAppInfoArray[i];
       
   552 		if( alcRegAppInfoPtr )
       
   553 			{
       
   554 			if( aFileName.Compare( alcRegAppInfoPtr->FileName()) == 0 )	
       
   555 				{
       
   556 				return ETrue;	
       
   557 				}			
       
   558 			}
       
   559 		}
       
   560 	return EFalse;		
       
   561 	}
       
   562 
       
   563 // ----------------------------------------------------------------------------
       
   564 // CLcRegistry::CreateAppRegInfoL
       
   565 // ----------------------------------------------------------------------------
       
   566 //
       
   567 CLcRegAppInfo* CLcRegistry::CreateAppRegInfoL(  TFileName	aFileName )
       
   568 	{
       
   569 	DEBUG( "+ CLcRegistry::CreateAppRegInfoL" );
       
   570 
       
   571     CLcRegistrationParser* regParser = CLcRegistrationParser::NewL( iFs );
       
   572  	CleanupStack::PushL( regParser );
       
   573     
       
   574 	//Parse the file content and get all the registration info.
       
   575 	CLcRegAppInfo* info = regParser->ParseFileL( aFileName );
       
   576 	
       
   577 	CleanupStack::PopAndDestroy( regParser );
       
   578 	
       
   579 	CleanupStack::PushL( info );	
       
   580 	
       
   581 	//Check that same UUID based appinfo is already registered or not.
       
   582 	if( DoesSameUuidPresent( info ))
       
   583 		{
       
   584 		User::Leave( KErrAlreadyExists );	
       
   585 		}
       
   586 		
       
   587 	//validate the application or document
       
   588 	if ( info->ApplicationType() == ELcNativeApplication ||
       
   589 		 info->ApplicationType() == ELcDocument )
       
   590 	 	{
       
   591 	 	//DEBUG("+ Validate")
       
   592 	 	ValidateRegisteredServiceL( *info );
       
   593 	 	//DEBUG("- Validate")
       
   594 	 	}
       
   595 
       
   596 	// Set the language specific name for the service
       
   597 	if( info->IsAppNamePresent())
       
   598 	    {
       
   599 	    info->SetAppLangSpecificNameL(iFs);    
       
   600 	    }
       
   601     else
       
   602         {
       
   603         SetAbsentAppNameL( info );
       
   604         }	    
       
   605 	
       
   606 	
       
   607 	// Set the file name for future reference.
       
   608 	info->SetFileNameL( aFileName );
       
   609 	
       
   610 	TUint mmcId;
       
   611 											
       
   612 	//Get current MMC id.
       
   613 	if( !GetMmcId( mmcId ) && IsInMMc( aFileName ))
       
   614 		{
       
   615 		info->SetMmcId(mmcId);
       
   616 		}		
       
   617 				
       
   618 	CleanupStack::Pop( info );		
       
   619 	
       
   620 	DEBUG( "- CLcRegistry::CreateAppRegInfoL" );		
       
   621 	return info;		
       
   622 	}
       
   623 
       
   624 // ----------------------------------------------------------------------------
       
   625 // CLcRegistry::InsertAppRegInfoL
       
   626 // ----------------------------------------------------------------------------
       
   627 //
       
   628 void CLcRegistry::InsertAppRegInfoL( CLcRegAppInfo*		aAppInfo )
       
   629 	{
       
   630 	TInt	index = 0;
       
   631 	
       
   632 	// Get the required order of the 
       
   633 	// service from the central repository
       
   634 	//Add the appinfo to array
       
   635 	if( iLcRegAppOrder->GetRegisteredAppOrder( aAppInfo->Id(),
       
   636 											   index ))
       
   637 		{
       
   638 		
       
   639 		// this is for new regapp info addition
       
   640 		// to db and get the final order
       
   641 		index = iLcRegAppOrder->AppendArrayL( aAppInfo->Id());
       
   642 		
       
   643 	    for ( TInt i = 0; i < iAppInfoArray.Count(); i++ )
       
   644 	        {
       
   645 	        // Get the new order for the respective elements and set the correct values.
       
   646 	        CLcRegAppInfo* info = iAppInfoArray[i];
       
   647 	        TInt currentIndex = 0;
       
   648 	        iLcRegAppOrder->GetRegisteredAppOrder( info->Id(),
       
   649 										           currentIndex );
       
   650             // Set the new Index
       
   651             info->SetArrayIndex( currentIndex );											           
       
   652 	        }
       
   653 	        
       
   654 		// Set the index of all elements in the new array
       
   655         for ( TInt i = 0; i < iAppInfoNewArray.Count(); i++ )
       
   656 	        {
       
   657 	        // Get the new order for the respective elements and set the correct values.
       
   658 	        CLcRegAppInfo* info = iAppInfoNewArray[i];
       
   659 	        TInt currentIndex = 0;
       
   660 	        iLcRegAppOrder->GetRegisteredAppOrder( info->Id(),
       
   661 										           currentIndex );
       
   662             // Set the new Index
       
   663             info->SetArrayIndex( currentIndex );											           
       
   664 	        }
       
   665 		}
       
   666     
       
   667     // Set the array index
       
   668     aAppInfo->SetArrayIndex( index );
       
   669 							
       
   670 	User::LeaveIfError( 
       
   671 				iAppInfoNewArray.InsertInOrder( aAppInfo,
       
   672 												PositionDescOrdering ));		
       
   673 	}
       
   674 
       
   675 // ----------------------------------------------------------------------------
       
   676 // CLcRegistry::DoesSameUuidPresent
       
   677 // ----------------------------------------------------------------------------
       
   678 //
       
   679 TBool CLcRegistry::DoesSameUuidPresent( CLcRegAppInfo* aLcRegAppInfo )
       
   680 	{
       
   681     // Check in the Old array	
       
   682 	for( TInt i = 0; i < iAppInfoArray.Count(); i++ )
       
   683 		{
       
   684 		CLcRegAppInfo* lcRegAppInfoPtr = iAppInfoArray[i];
       
   685 		if( lcRegAppInfoPtr )
       
   686 			{
       
   687 			if( !aLcRegAppInfo->Id().Compare( lcRegAppInfoPtr->Id()))	
       
   688 				{
       
   689 				return ETrue;
       
   690 				}			
       
   691 			}
       
   692 		}
       
   693 
       
   694     // Now check in the new array
       
   695 	for( TInt i = 0; i < iAppInfoNewArray.Count(); i++ )
       
   696 		{
       
   697 		CLcRegAppInfo* lcRegAppInfoPtr = iAppInfoNewArray[i];
       
   698 		if( lcRegAppInfoPtr )
       
   699 			{
       
   700 			if( !aLcRegAppInfo->Id().Compare( lcRegAppInfoPtr->Id()))	
       
   701 				{
       
   702 				return ETrue;
       
   703 				}			
       
   704 			}
       
   705 		}
       
   706 	
       
   707 	// No match found	
       
   708 	return EFalse;		
       
   709 	}
       
   710 
       
   711 // ----------------------------------------------------------------------------
       
   712 // CLcRegistry::DoCancel
       
   713 // ----------------------------------------------------------------------------
       
   714 //
       
   715 void CLcRegistry::DoCancel()
       
   716     {
       
   717     Cancel();
       
   718     }
       
   719 
       
   720 // ---------------------------------------------------------------------------
       
   721 // void CLcService::ValidateRegisteredAppL
       
   722 // ---------------------------------------------------------------------------
       
   723 //
       
   724 void CLcRegistry::ValidateRegisteredServiceL( CLcRegAppInfo& aApplicationInfo )
       
   725     {
       
   726     DEBUG( "+ CLcRegistry::ValidateRegisteredServiceL" )
       
   727 	
       
   728 	RApaLsSession lsSession;
       
   729 	User::LeaveIfError( lsSession.Connect() );
       
   730 	CleanupClosePushL( lsSession );
       
   731     
       
   732 	//Check the application type.If Application then validate it
       
   733 	if( aApplicationInfo.ApplicationType() == ELcNativeApplication )
       
   734 		{
       
   735 		DEBUG("+ App Validation")
       
   736 	    TLex lexer(aApplicationInfo.ApplicationData());
       
   737 	    TUint32 lAppSid = 0;
       
   738 	    TUint limit = 0xFFFFFFFF; 
       
   739 	    User::LeaveIfError( lexer.BoundedVal(lAppSid, EHex, limit) );
       
   740 	    TApaAppInfo appInfo;
       
   741 		User::LeaveIfError( lsSession.GetAppInfo( appInfo, TUid::Uid( lAppSid )));
       
   742 		DEBUG("- App Validation")
       
   743 		}
       
   744 	else if( aApplicationInfo.ApplicationType() == ELcDocument )
       
   745 		{		
       
   746 		DEBUG("+ Document Validation")
       
   747 		//Validate document
       
   748 		CDocumentHandler* documentHandler = CDocumentHandler::NewL();						
       
   749 		CleanupStack::PushL( documentHandler );
       
   750 		// empty datatype for automatic recognization
       
   751 		TUint8* contentTypeString = NULL;
       
   752 		FindContentTypeFromFileL( lsSession,
       
   753 								  contentTypeString,
       
   754 								  &aApplicationInfo );
       
   755 		User::LeaveIfNull( contentTypeString );
       
   756 										  
       
   757 		CleanupStack::PushL( contentTypeString );								 	
       
   758 		documentHandler->CanOpenL( TDataType( TPtrC8( contentTypeString )));
       
   759 		CleanupStack::PopAndDestroy( 2, documentHandler );			
       
   760 		DEBUG("- Document Validation")
       
   761 		}
       
   762 	
       
   763 	// Now set the system characteristics for application / document / weburl
       
   764 	SetAppSysCharacteristicsL( lsSession, aApplicationInfo );		
       
   765 	
       
   766 	CleanupStack::PopAndDestroy(); //lsSession
       
   767 	
       
   768 	DEBUG( "- CLcRegistry::ValidateRegisteredServiceL" )      
       
   769     }  
       
   770 
       
   771 
       
   772 // ---------------------------------------------------------------------------
       
   773 // void CLcRegistry::FindContentTypeFromFileL
       
   774 // ---------------------------------------------------------------------------
       
   775 //
       
   776 void CLcRegistry::FindContentTypeFromFileL( RApaLsSession apaSession,
       
   777 										    TUint8*& aContentTypeString,
       
   778 										    CLcRegAppInfo* aApplicationInfo )
       
   779 	{
       
   780     TDataRecognitionResult dataType;
       
   781     TInt ret;
       
   782 
       
   783 	// Create a buffer to hold data from the file
       
   784 	TInt bufferSize = 0;
       
   785     TInt seekPosition = 0;
       
   786 	apaSession.GetMaxDataBufSize(bufferSize);
       
   787 	HBufC8* buffer = HBufC8::NewLC(bufferSize);
       
   788 	TPtr8 buf = buffer->Des();
       
   789 
       
   790 	RFile file;
       
   791     HBufC* fileName = HBufC::NewLC( KMaxPath );
       
   792     fileName->Des().Copy(aApplicationInfo->ApplicationData());
       
   793 	
       
   794     ret = file.Open( iFs,
       
   795     				 *fileName, 
       
   796 		  			 EFileShareAny | EFileRead );
       
   797 	if( !ret )
       
   798 		{
       
   799 		// Find current file pointer position
       
   800 	    file.Seek(ESeekStart, seekPosition);
       
   801 		// Read from file
       
   802 		file.Read(buf);
       
   803 		// return file pointer to original position
       
   804 	    file.Seek(ESeekStart, seekPosition);
       
   805 	    // Ask the application architecture to find the file type
       
   806 	    ret = apaSession.RecognizeData(*fileName, buf, dataType);
       
   807 
       
   808 	    if (ret == KErrNone &&
       
   809 	        (dataType.iConfidence == CApaDataRecognizerType::ECertain) ||
       
   810 	        (dataType.iConfidence == CApaDataRecognizerType::EProbable))
       
   811 	        {
       
   812 	        // If the file type was found, try to match it to a known file type
       
   813 	        TPtrC8 mimeTypePtr = dataType.iDataType.Des8();
       
   814 	        TInt len = mimeTypePtr.Length() + 1;
       
   815 	        aContentTypeString = new(ELeave) TUint8 [len];
       
   816 
       
   817 	        TPtr8 contentTypeStringPtr(aContentTypeString, len);
       
   818 	        contentTypeStringPtr.Copy(mimeTypePtr);
       
   819 	        contentTypeStringPtr.ZeroTerminate();
       
   820 	        }
       
   821 		file.Close();	        
       
   822 		}
       
   823 	CleanupStack::PopAndDestroy(fileName); //fileName		
       
   824 	CleanupStack::PopAndDestroy(buffer); //buffer			
       
   825 	}
       
   826 
       
   827 // -----------------------------------------------------------------------------
       
   828 // CLcRegistry::MmcIdL
       
   829 // Get the id of the MMC card.
       
   830 // -----------------------------------------------------------------------------
       
   831 //
       
   832 TInt CLcRegistry::GetMmcId( TUint& aUid )
       
   833     {
       
   834     TDriveInfo info;
       
   835     info.iType = EMediaNotPresent;
       
   836     TInt mmcDrive;
       
   837     TInt err;
       
   838     TChar cha = '?';
       
   839     err = RFs::CharToDrive( PathInfo::MemoryCardRootPath()[0], mmcDrive );    
       
   840     if (err == KErrNone)
       
   841         {
       
   842         err = iFs.Drive( info, mmcDrive );
       
   843         }
       
   844     if( err != KErrNone ||
       
   845         info.iType == EMediaNotPresent )
       
   846         {
       
   847         // MMC locked or not installed        
       
   848         err = KErrNotFound;
       
   849         }
       
   850     else
       
   851         {
       
   852         TVolumeInfo volumeInfo;
       
   853         err = iFs.Volume( volumeInfo, mmcDrive );
       
   854         if( err == KErrNone )
       
   855             {
       
   856             aUid = volumeInfo.iUniqueID;
       
   857             }
       
   858         }       
       
   859     return err;
       
   860     }
       
   861 
       
   862 
       
   863 // ----------------------------------------------------------------------------
       
   864 // CLcRegistry::SetAppSysCharacteristicsL
       
   865 // ----------------------------------------------------------------------------
       
   866 //
       
   867 void CLcRegistry::SetAppSysCharacteristicsL( RApaLsSession&		alsSession,
       
   868 											 CLcRegAppInfo&		aRegAppInfo )
       
   869 	{
       
   870 	TFileName fileName;
       
   871 	if( aRegAppInfo.ApplicationType() == ELcNativeApplication )
       
   872 		{
       
   873 		// for application we should check
       
   874 		// where the binary is present
       
   875 	    TLex lexer(aRegAppInfo.ApplicationData());
       
   876 	    TUint32 lAppSid = 0;
       
   877 	    TUint limit = 0xFFFFFFFF; 
       
   878 	    User::LeaveIfError( lexer.BoundedVal(lAppSid, EHex, limit) );
       
   879 	    TApaAppInfo appInfo;
       
   880 		User::LeaveIfError( alsSession.GetAppInfo( appInfo, TUid::Uid( lAppSid )));
       
   881 		// check if the dll file is in ROM or not
       
   882 		fileName.Copy( appInfo.iFullName );
       
   883 		}
       
   884 	else if( aRegAppInfo.ApplicationType() == ELcDocument )
       
   885 		{
       
   886 		// for document we should check for the document file name
       
   887 		fileName.Copy( aRegAppInfo.ApplicationData() );
       
   888 		}
       
   889 	else if( aRegAppInfo.ApplicationType() == ELcWebUrl )
       
   890 		{
       
   891 		// for web url it will be registration filename
       
   892 		fileName.Copy( iFileNameArry[ iregFileCount ] );
       
   893 		}
       
   894 		
       
   895 	if( fileName.Length() > 0 )
       
   896 		{
       
   897 		// parse the file name and check the drive letter
       
   898 		TParse parse;
       
   899 		parse.Set( fileName, NULL, NULL );
       
   900 		TPtrC drive = parse.Drive();
       
   901 		if( drive[0] ==  PathInfo::RomRootPath()[0] )
       
   902 			{
       
   903 			aRegAppInfo.SetSystemCharacteristics( CLcLocationAppInfo::ESysCharRomBased);
       
   904 			}
       
   905 		else
       
   906 			{
       
   907 			aRegAppInfo.SetSystemCharacteristics( CLcLocationAppInfo::ESysCharNone);	
       
   908 			}
       
   909 		}
       
   910 	}
       
   911 
       
   912 // -----------------------------------------------------------------------------
       
   913 // CLcRegistry::IsAppUpgraded
       
   914 // -----------------------------------------------------------------------------
       
   915 //	
       
   916 TBool CLcRegistry::IsAppUpgraded( TFileName aFileName )
       
   917 	{
       
   918 	DEBUG( "+ CLcRegistry::CheckforAppUpgrade(");
       
   919 	
       
   920 	// If filename is already there in the old list and presence
       
   921 	// flag also true then check the time stamp between current file and old file
       
   922 	TEntry oldEntry,newEntry;
       
   923 	TFileName newFileName = iFileNameArry[ iregFileCount ];
       
   924 	iFs.Entry( aFileName, oldEntry );
       
   925 	iFs.Entry( newFileName, newEntry );
       
   926 	
       
   927 	if( oldEntry.iModified == newEntry.iModified ) 	
       
   928 		{
       
   929 		return EFalse;
       
   930 		}
       
   931 	else
       
   932 		{
       
   933 		return ETrue;	
       
   934 		}		
       
   935 	}
       
   936 
       
   937 // -----------------------------------------------------------------------------
       
   938 // CLcRegistry::SetAbsentAppNameL
       
   939 // -----------------------------------------------------------------------------
       
   940 //
       
   941 void CLcRegistry::SetAbsentAppNameL( CLcRegAppInfo*    aAppInfo )
       
   942     {
       
   943 	RApaLsSession lsSession;
       
   944 	User::LeaveIfError( lsSession.Connect() );
       
   945 	CleanupClosePushL( lsSession );
       
   946 	//Check the application type.If Application then validate it
       
   947 	if( aAppInfo->ApplicationType() == ELcNativeApplication )
       
   948 		{
       
   949 	    TLex lexer(aAppInfo->ApplicationData());
       
   950 	    TUint32 lAppSid = 0;
       
   951 	    TUint limit = 0xFFFFFFFF; 
       
   952 	    User::LeaveIfError( lexer.BoundedVal(lAppSid, EHex, limit) );
       
   953 	    TApaAppInfo appNameInfo;
       
   954 		User::LeaveIfError( lsSession.GetAppInfo( appNameInfo, TUid::Uid( lAppSid )));
       
   955 		aAppInfo->SetNameL(appNameInfo.iCaption);
       
   956 		}
       
   957 	else
       
   958 		{
       
   959 		aAppInfo->SetNameL( aAppInfo->ApplicationData() );	
       
   960 		}
       
   961     CleanupStack::PopAndDestroy(); //lsSession
       
   962     }  		
       
   963 
       
   964 
       
   965 // ---------------------------------------------------------------------------
       
   966 // static TInt PositionDescOrdering
       
   967 // Ordering function for inserting the elements into the applist array.
       
   968 // The ordering is done first based on the previous index. 
       
   969 //
       
   970 // @param CLcRegAppInfo& First appinfo Element
       
   971 // @param CLcRegAppInfo& Second appinfo Element
       
   972 // @return 1. zero, if the two objects are equal.
       
   973 //
       
   974 //		   2. a negative value, if the first object is less than the second.
       
   975 //
       
   976 // 		   3. a positive value, if the first object is greater than the second.
       
   977 // 
       
   978 // ---------------------------------------------------------------------------	
       
   979 TInt PositionDescOrdering( const CLcRegAppInfo& aAppInfo1, 
       
   980 		  				   const CLcRegAppInfo& aAppInfo2 )
       
   981     {
       
   982     CLcRegAppInfo* appInfo1 = const_cast< CLcRegAppInfo*>(&aAppInfo1);
       
   983     CLcRegAppInfo* appInfo2 = const_cast< CLcRegAppInfo*>(&aAppInfo2);
       
   984     return ( appInfo1->Index() - appInfo2->Index());
       
   985 	}
       
   986 //End of file
       
   987