uiservicetab/vimpststorage/src/cvimpststoragevpbklocalstore.cpp
changeset 0 5e5d6b214f4f
child 9 9fdee5e1da30
equal deleted inserted replaced
-1:000000000000 0:5e5d6b214f4f
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Class that provides handling of vpbk local store
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <AknUtils.h> 
       
    20 #include "cvimpststoragevpbklocalstore.h"
       
    21 
       
    22 #include "cvimpststoragevpbkstorehandler.h"
       
    23 
       
    24 #include <MVPbkContactView.h>
       
    25 #include <CVPbkContactIdConverter.h>
       
    26 #include "mvimpststorageserviceview.h"
       
    27 #include "vimpstdebugtrace.h" 
       
    28 #include "tvimpstconsts.h"
       
    29 #include <CVPbkContactLinkArray.h>
       
    30 #include <MVPbkContactLink.h>
       
    31 #include "cvimpststoragecontact.h"
       
    32 #include "vimpststorageutils.h"
       
    33 
       
    34 #include <f32file.h>
       
    35 #include <sysutil.h>
       
    36 
       
    37 
       
    38 // ================= MEMBER FUNCTIONS =======================
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // CVIMPSTStorageVPbkLocalStore::
       
    42 //      CVIMPSTStorageVPbkLocalStore
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CVIMPSTStorageVPbkLocalStore:: CVIMPSTStorageVPbkLocalStore( MVIMPSTStorageServiceCacheWriter& aServiceCacheWriter ):
       
    46 	CActive( CActive::EPriorityIdle ),
       
    47     iServiceCacheWriter( aServiceCacheWriter ),
       
    48     iFetchStep( EFetchingUnknown )
       
    49     { 
       
    50     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CVIMPSTStorageVPbkLocalStore()") ); 
       
    51     CActiveScheduler::Add( this );
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CVIMPSTStorageVPbkLocalStore::NewL
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CVIMPSTStorageVPbkLocalStore* CVIMPSTStorageVPbkLocalStore::NewL( const TDesC& aContactDb,
       
    59     															 const TDesC& aServiceName,
       
    60     															 MVIMPSTStorageServiceCacheWriter& aServiceCacheWriter )
       
    61     {
       
    62     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::NewL() begin") ); 
       
    63     CVIMPSTStorageVPbkLocalStore* self = NewLC( aContactDb,  
       
    64     										   aServiceName,
       
    65 											  aServiceCacheWriter);
       
    66     CleanupStack::Pop( self );
       
    67     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::NewL() end") ); 
       
    68     return self;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CVIMPSTStorageVPbkLocalStore::NewLC
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CVIMPSTStorageVPbkLocalStore* 
       
    76     CVIMPSTStorageVPbkLocalStore::NewLC( const TDesC& aContactDb, 
       
    77     									const TDesC& aServiceName,
       
    78     									MVIMPSTStorageServiceCacheWriter& aServiceCacheWriter  )
       
    79     {
       
    80     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::NewLC() begin") );
       
    81     CVIMPSTStorageVPbkLocalStore* self =
       
    82         new (ELeave) CVIMPSTStorageVPbkLocalStore(aServiceCacheWriter);
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL( aContactDb, aServiceName );
       
    85     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::NewLC() end") );
       
    86     return self;
       
    87     }
       
    88 // ---------------------------------------------------------------------------
       
    89 // CVIMPSTStorageVPbkLocalStore::ConstructL
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CVIMPSTStorageVPbkLocalStore::ConstructL( const TDesC& aContactDb, const TDesC& aServiceName )
       
    93     {
       
    94     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() begin") );
       
    95     iVPbkStoreHandler = CVIMPSTStorageVPbkStoreHandler::NewL( aContactDb ,aServiceName, *this,ETrue );
       
    96     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() iVPbkStoreHandler created") );
       
    97      //construct the persistent store name
       
    98     iLocalDBName = HBufC::NewL( aServiceName.Length()+ KStorageExtn().Length());
       
    99     TPtr iLocalDBNamePtr = iLocalDBName->Des();
       
   100     iLocalDBNamePtr.Append(aServiceName);
       
   101     _LIT(KSpecialChar ,"/\\:*?<>\"");
       
   102     AknTextUtils :: StripCharacters(iLocalDBNamePtr,KSpecialChar); 
       
   103     iLocalDBNamePtr.Append(KStorageExtn());    
       
   104     TRACE( T_LIT("iLocalDBNamePtr: %S"), &iLocalDBNamePtr );
       
   105     MVPbkContactStore* defaultStore = iVPbkStoreHandler->GetDefaultStoreL( aContactDb );
       
   106     User::LeaveIfNull(defaultStore);  
       
   107     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() defaultStore retrived") );
       
   108     iIdConverter = CVPbkContactIdConverter::NewL(*defaultStore);
       
   109     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() iIdConverter created") ); 
       
   110     User::LeaveIfError( iFs.Connect() );
       
   111     // Open existing or create new database.
       
   112     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() connected to file server") ); 
       
   113 	if ( DbExists() )
       
   114 		{
       
   115 		TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() local db exist") ); 
       
   116         OpenDbL();
       
   117         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() open success") );
       
   118 		}
       
   119 	else
       
   120 		{
       
   121 		TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() local db not exist") );
       
   122         DoFreespaceLevelCheckL( KEmptyDbSize );
       
   123         CreateDbL();
       
   124         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() local db created") );
       
   125 		}
       
   126     OpenTableL();
       
   127     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() OpenTableL success ") );
       
   128     iColset = iDb.ColSetL( KContactTable );    
       
   129    	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() iColset is set") );
       
   130     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ConstructL() end") );	
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // CVIMPSTStorageVPbkLocalStore::
       
   135 //      ~CVIMPSTStorageVPbkLocalStore
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 CVIMPSTStorageVPbkLocalStore::~CVIMPSTStorageVPbkLocalStore()
       
   139     {
       
   140     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::~CVIMPSTStorageVPbkLocalStore() begin") );
       
   141     iRetrivedContactArray.ResetAndDestroy();
       
   142     TRAP_IGNORE( ResetAndDestroyLocalArrayL() );
       
   143         
       
   144     CloseTable();
       
   145     delete iColset;
       
   146     CloseDb();
       
   147     iFs.Close();    
       
   148     
       
   149     delete iLocalDBName;
       
   150     delete iIdConverter;	        
       
   151     delete iInviteId;
       
   152     delete iVPbkStoreHandler;
       
   153     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::~CVIMPSTStorageVPbkLocalStore() end") );
       
   154     }
       
   155  
       
   156  // ---------------------------------------------------------------------------
       
   157 // CVIMPSTStorageVPbkLocalStore::
       
   158 //      ResetAndDestroyLocalArrayL
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CVIMPSTStorageVPbkLocalStore::ResetAndDestroyLocalArrayL()
       
   162     {
       
   163     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ResetAndDestroyLocalArrayL() begin") );
       
   164     TInt count = iFetchContactsToBeAdded.Count();
       
   165    	while( count )
       
   166 	   	{
       
   167 	   	TVIMPSTContactInfo contactInf = iFetchContactsToBeAdded[ count-1 ];
       
   168 	   	iFetchContactsToBeAdded.Remove( count-1 );
       
   169 		delete contactInf.iUserId;
       
   170 		contactInf.iUserId = NULL;	
       
   171 		delete contactInf.iDisplayName;
       
   172 		contactInf.iDisplayName = NULL;				         
       
   173          // not exist need to add 
       
   174         iFetchContactsToBeAdded.Compress();	
       
   175         count = iFetchContactsToBeAdded.Count();
       
   176 	   	}
       
   177 	iFetchContactsToBeAdded.Reset();
       
   178     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ResetAndDestroyLocalArrayL() end") );
       
   179     }
       
   180  
       
   181 // ----------------------------------------------------------
       
   182 // CVIMPSTStorageVPbkLocalStore::LocalStore
       
   183 // ----------------------------------------------------------
       
   184 //
       
   185 TBool CVIMPSTStorageVPbkLocalStore::LocalStore() const 
       
   186 	{
       
   187 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::LocalStore() local store") );
       
   188 	return ETrue;
       
   189 	}
       
   190 // ---------------------------------------------------------------------------
       
   191 // CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 void CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL(TVIMPSTVPbkStoreEvent aVPbkStoreEvent) 
       
   195 	{
       
   196 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() begin") );
       
   197 	switch( aVPbkStoreEvent.iEventType )
       
   198 	    {
       
   199 	    case EVPbkContactReadCompleted:
       
   200 	    	{
       
   201 	    	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactReadCompleted started") );
       
   202 	    	iServiceCacheWriter.NotifyServiceViewL(TVIMPSTEnums::EStorageContactReadComplete);
       
   203 	    	if( iFetchStep == EFetchingCalled )
       
   204 		    	{
       
   205 		    	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactReadCompleted EFetchingCalled ") );
       
   206 		    	IssueRequest();		
       
   207 		    	}
       
   208 	    	iFetchStep = EContactReadComplete;
       
   209 	    	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EContactReadComplete completed ") );
       
   210 		  	break;
       
   211 	    	}
       
   212 	    case EVPbkContactReading:
       
   213 		    {
       
   214 		    // a contact entry has been created in virtual database store
       
   215 			// create the cache contact
       
   216 			TInt32 linkId = iIdConverter->LinkToIdentifier(*aVPbkStoreEvent.iContactLink );
       
   217 			TDbColNo colNo = iColset->ColNo( KContactId );
       
   218 		    if ( SeekRowL( colNo, linkId ) )
       
   219 				{
       
   220 				TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactReading contact") );
       
   221 				/*
       
   222 					*	This is local store, that is phone book and service tab shares only one cdb file.
       
   223 					* 	in that case, if somebody edits the cdb file from contacts tab, then also we get the 
       
   224 					* 	callback, then checking for entry in db table makes sens.. 
       
   225 				*/
       
   226 				/*
       
   227 				 * 	in case when the user adds a contact from the service tab, then it first tries to add it in the
       
   228 				 *	cdb file, after that we get this callback, then , there is no way that it will be present in the
       
   229 				 *	db table, in that case, we will not add it in the cache - which is wrong.. 
       
   230 				 *	so we need to make a differentiation, on how the contact was added.. whether from cotnacts tab
       
   231 				 *	or from the service tab. --- to solve this issue.. we can store the userid in memberVariable.. 
       
   232 				 *	before making a add request to the VPBK (cdb file).. then in this function, we check if
       
   233 				 *	the contactid is same as the member variable.  aVPbkStoreEvent contains the userid that was added.
       
   234 				 *	then if it matches, then we know that it was added from our service tab.. and then we can add it in our DB.
       
   235 				 */
       
   236 				// contatc found in local database ,add to storage
       
   237 				iServiceCacheWriter.AddContactToCacheL (*aVPbkStoreEvent.iContactLink , 
       
   238 										 aVPbkStoreEvent.iUserId ,
       
   239 										 aVPbkStoreEvent.iDisplayName,
       
   240 										 aVPbkStoreEvent.iAvatarContent,
       
   241 										 TVIMPSTEnums::EStorageContactReading );
       
   242 				}
       
   243 			
       
   244 		    break;	
       
   245 		    }
       
   246 		case EVPbkContactFetching:
       
   247 	   	case EVPbkContactAdded:
       
   248 			{
       
   249 			// a contact entry has been created in virtual database store
       
   250 			// create the cache contact
       
   251 			TInt32 linkId = iIdConverter->LinkToIdentifier(*aVPbkStoreEvent.iContactLink );
       
   252 			TDbColNo colNo = iColset->ColNo( KContactId );
       
   253 			if( iFetchStep  == EFetchingOn )
       
   254 		         {
       
   255 		         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactFetching contact") );
       
   256 		         WriteToStoreDbL( linkId );
       
   257 		         iServiceCacheWriter.AddContactToCacheL (*aVPbkStoreEvent.iContactLink , 
       
   258 		                 aVPbkStoreEvent.iUserId ,
       
   259 		                 aVPbkStoreEvent.iDisplayName ,
       
   260 		                 aVPbkStoreEvent.iAvatarContent,
       
   261 		                 TVIMPSTEnums::EStorageContactFetching );
       
   262 		      	IssueRequest();		
       
   263 				}
       
   264 			else if ( SeekRowL( colNo, linkId ) )
       
   265 				{
       
   266 				TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactAdded start") );
       
   267 				/*
       
   268 					*	This is local store, that is phone book and service tab shares only one cdb file.
       
   269 					* 	in that case, if somebody edits the cdb file from contacts tab, then also we get the 
       
   270 					* 	callback, then checking for entry in db table makes sens.. 
       
   271 				*/
       
   272 				/*
       
   273 				 * 	in case when the user adds a contact from the service tab, then it first tries to add it in the
       
   274 				 *	cdb file, after that we get this callback, then , there is no way that it will be present in the
       
   275 				 *	db table, in that case, we will not add it in the cache - which is wrong.. 
       
   276 				 *	so we need to make a differentiation, on how the contact was added.. whether from cotnacts tab
       
   277 				 *	or from the service tab. --- to solve this issue.. we can store the userid in memberVariable.. 
       
   278 				 *	before making a add request to the VPBK (cdb file).. then in this function, we check if
       
   279 				 *	the contactid is same as the member variable.  aVPbkStoreEvent contains the userid that was added.
       
   280 				 *	then if it matches, then we know that it was added from our service tab.. and then we can add it in our DB.
       
   281 				 */
       
   282 				// contatc found in local database ,add to storage
       
   283 				iServiceCacheWriter.AddContactToCacheL (*aVPbkStoreEvent.iContactLink , 
       
   284 										 aVPbkStoreEvent.iUserId ,
       
   285 										 aVPbkStoreEvent.iDisplayName,
       
   286 										 aVPbkStoreEvent.iAvatarContent,
       
   287 										 TVIMPSTEnums::EStorageEventContactAddition  );
       
   288 				TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactAdded completed") );
       
   289 				}
       
   290 			else if(  iInviteId )
       
   291 			    {
       
   292 			    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() iInviteId accepted") );
       
   293 			    if( IsSameContactIdL( *iInviteId, aVPbkStoreEvent.iUserId ) )
       
   294 			        {
       
   295 			        WriteToStoreDbL( linkId );
       
   296 			        iServiceCacheWriter.AddContactToCacheL (*aVPbkStoreEvent.iContactLink , 
       
   297 			                aVPbkStoreEvent.iUserId ,
       
   298 			                aVPbkStoreEvent.iDisplayName,
       
   299 			                aVPbkStoreEvent.iAvatarContent,
       
   300 			                TVIMPSTEnums::EStorageEventContactAddition );
       
   301 			        delete iInviteId;
       
   302 			        iInviteId = NULL;
       
   303 			        }
       
   304                  TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() iInviteId accepted sucess") );
       
   305 			    }
       
   306 			break;	
       
   307 			}
       
   308 	   	case EVPbkContactSynchronizing:
       
   309 	   	    {
       
   310 	   	    TRACE( T_LIT("CVIMPSTStorageVPbkServerStore::HandleVPbkStoreEventL EVPbkContactSynchronizing " ) );
       
   311 	   	    // a contact entry has been created in virtual database store
       
   312 	   	    // create the cache contact
       
   313 	   	    TInt32 linkId = iIdConverter->LinkToIdentifier(*aVPbkStoreEvent.iContactLink );
       
   314 	   	    TDbColNo colNo = iColset->ColNo( KContactId );
       
   315 	   	    if ( SeekRowL( colNo, linkId ) )
       
   316 	   	        {
       
   317 	   	        /*
       
   318 	   	         *   This is local store, that is phone book and service tab shares only one cdb file.
       
   319 	   	         *   in that case, if somebody edits the cdb file from contacts tab, then also we get the 
       
   320 	   	         *   callback, then checking for entry in db table makes sens.. 
       
   321 	   	         */
       
   322 	   	        /*
       
   323 	   	         *  in case when the user adds a contact from the service tab, then it first tries to add it in the
       
   324 	   	         *  cdb file, after that we get this callback, then , there is no way that it will be present in the
       
   325 	   	         *  db table, in that case, we will not add it in the cache - which is wrong.. 
       
   326 	   	         *  so we need to make a differentiation, on how the contact was added.. whether from cotnacts tab
       
   327 	   	         *  or from the service tab. --- to solve this issue.. we can store the userid in memberVariable.. 
       
   328 	   	         *  before making a add request to the VPBK (cdb file).. then in this function, we check if
       
   329 	   	         *  the contactid is same as the member variable.  aVPbkStoreEvent contains the userid that was added.
       
   330 	   	         *  then if it matches, then we know that it was added from our service tab.. and then we can add it in our DB.
       
   331 	   	         */
       
   332 	   	        // contatc found in local database ,add to storage
       
   333 	   	        iServiceCacheWriter.AddContactToCacheL (*aVPbkStoreEvent.iContactLink , 
       
   334 	   	                aVPbkStoreEvent.iUserId ,
       
   335 	   	                aVPbkStoreEvent.iDisplayName,
       
   336 	   	                aVPbkStoreEvent.iAvatarContent,
       
   337 	   	                TVIMPSTEnums::EStorageContactFetching  );
       
   338 	   	        }
       
   339 	   	   if(iFetchStep == EFetchingOn)          
       
   340                {
       
   341                TRACE( T_LIT("CVIMPSTStorageVPbkServerStore::HandleVPbkStoreEventL  EVPbkContactSynchronizing fetchin on" ) );
       
   342                IssueRequest();
       
   343                }
       
   344 	   	    TRACE( T_LIT("CVIMPSTStorageVPbkServerStore::HandleVPbkStoreEventL EVPbkContactSynchronizing completed" ) );
       
   345 	   	    break;
       
   346 	   	    }
       
   347 		case EVPbkContactDeleted:
       
   348 		    {
       
   349 		    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactDeleted") );
       
   350 		    TInt32 linkId = iIdConverter->LinkToIdentifier(*aVPbkStoreEvent.iContactLink );
       
   351 		    // a contact entry has been deleted from virtual database store
       
   352 		    TDbColNo colNo = iColset->ColNo( KContactId );
       
   353 		    if ( SeekRowL( colNo, linkId ) )
       
   354 		        { 
       
   355 		        TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactDeleted found in db") );
       
   356 		        // contatc found in local database ,delete from local database 
       
   357 		        DeleteContactFromInternalStoreL( linkId );
       
   358 		        TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactDeleted deleted from db") );
       
   359 		         // delete from cache contact
       
   360 		    	iServiceCacheWriter.RemoveContactFromCacheL(*aVPbkStoreEvent.iContactLink, 
       
   361 		    												TVIMPSTEnums::EStorageEventDeleteFromPbk );
       
   362 		    	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactDeleted deleted from cache") );
       
   363 		        }
       
   364 		    break;	 
       
   365 		    }
       
   366 		case EVPbkContactChanged:
       
   367 		    {
       
   368 		    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactChanged ") );
       
   369 		    TInt32 linkId = iIdConverter->LinkToIdentifier(*aVPbkStoreEvent.iContactLink );
       
   370 		    TDbColNo colNo = iColset->ColNo( KContactId );
       
   371 		    if ( SeekRowL( colNo, linkId ) )
       
   372 		       {
       
   373 		       TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactChanged found in local db") );
       
   374 		       // found in the list update it
       
   375 		       iServiceCacheWriter.UpdateCacheContactL(*aVPbkStoreEvent.iContactLink , 
       
   376 					    						 	 aVPbkStoreEvent.iUserId ,
       
   377 					    							 aVPbkStoreEvent.iDisplayName,
       
   378 					    							 aVPbkStoreEvent.iAvatarContent );
       
   379 			   TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactChanged changed in cache") );
       
   380 		       }
       
   381 		    break;	
       
   382 		    }
       
   383 	    case EVPbkContactRetriving:
       
   384 		    {
       
   385 		    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactRetriving") );
       
   386 		    // in case of local we allow all 
       
   387 		    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactRetriving valid Id") );
       
   388 			CVIMPSTStorageContact* retrivedContact = CVIMPSTStorageContact::NewL( aVPbkStoreEvent.iUserId ,
       
   389 											    								  aVPbkStoreEvent.iDisplayName,
       
   390 											    								  *aVPbkStoreEvent.iContactLink,
       
   391 											    								  aVPbkStoreEvent.iAvatarContent);
       
   392 			iRetrivedContactArray.Append( retrivedContact ); // takes the ownership of retrivedContact
       
   393 			TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkContactRetriving valid Id added") );
       
   394 			// keep the backup of retrieved contacts
       
   395 			// user can add later once it sent to server
       
   396 			break;	
       
   397 		    }
       
   398 		case EVPbkUnknownChanges:
       
   399 		    {
       
   400 		    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkUnknownChanges start") );
       
   401 		    DeleteAllContactFromInternalStoreL();
       
   402 		    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkUnknownChanges all delete from db") );
       
   403 		     // unknow mean all contact deleted from store
       
   404 		    iServiceCacheWriter.RemoveAllCacheContactsL();
       
   405 		    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() EVPbkUnknownChanges all delete from cache") );
       
   406 		    break;	
       
   407 		    }
       
   408 	    default:
       
   409 		 	break;
       
   410 	    }
       
   411 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::HandleVPbkStoreEventL() end") );
       
   412 	}
       
   413 
       
   414 
       
   415 // --------------------------------------------------------------------------
       
   416 // CVIMPSTStorageVPbkLocalStore::IssueRequest
       
   417 // --------------------------------------------------------------------------
       
   418 //
       
   419 void CVIMPSTStorageVPbkLocalStore::IssueRequest()
       
   420     {
       
   421     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::IssueRequest() begin") );
       
   422     if( !IsActive() )
       
   423 	    {
       
   424 	    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::IssueRequest() SetActive") );
       
   425 	    TRequestStatus* status = &iStatus;
       
   426 	    User::RequestComplete( status, KErrNone );
       
   427 	    SetActive();
       
   428 	    }
       
   429 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::IssueRequest() begin") );
       
   430     }
       
   431     
       
   432 // --------------------------------------------------------------------------
       
   433 // CVIMPSTStorageVPbkLocalStore::RunL
       
   434 // --------------------------------------------------------------------------
       
   435 //
       
   436 void CVIMPSTStorageVPbkLocalStore::RunL()
       
   437     {
       
   438     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() begin") );
       
   439     TInt count = iFetchContactsToBeAdded.Count();
       
   440     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() count to be added = %d "),count );
       
   441     if( !count )
       
   442         {
       
   443         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() fetch completed " ) );
       
   444         iFetchStep = EContactReadComplete;
       
   445         iServiceCacheWriter.NotifyServiceViewL( TVIMPSTEnums::EStorageContactFetchComplete );
       
   446 		TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() fetch completed notification sent" ) );     
       
   447 	    }
       
   448     else
       
   449         {
       
   450         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() fetch in progress" ) );     
       
   451         iFetchStep  = EFetchingOn;
       
   452         TVIMPSTContactInfo contactInfoToAdd = iFetchContactsToBeAdded[0];
       
   453         MVIMPSTStorageContact* exist = iServiceCacheWriter.FindCacheContactByUserId( *contactInfoToAdd.iUserId );
       
   454         if( exist )
       
   455 	        {
       
   456 	        // contact is already exist ,send the notification about this
       
   457 	        iServiceCacheWriter.NotifyServiceViewL(TVIMPSTEnums::EStorageContactFetchExistInStore, exist );
       
   458 	        TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() fetch in progress id exist" ) ); 
       
   459 	        // process next
       
   460 	        IssueRequest();
       
   461 	        }
       
   462 	    else
       
   463 		    {
       
   464 		    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() fetch in progress create id in store called" ) ); 
       
   465 		     // pass the display NAme in place of NULL
       
   466         	iVPbkStoreHandler->CreateVPbkContactL( *contactInfoToAdd.iUserId, *contactInfoToAdd.iDisplayName ); // process always 0th item
       
   467             }
       
   468         iFetchContactsToBeAdded.Remove( 0 );
       
   469         delete contactInfoToAdd.iUserId;
       
   470         contactInfoToAdd.iUserId = NULL;
       
   471         delete contactInfoToAdd.iDisplayName;
       
   472         contactInfoToAdd.iDisplayName = NULL ;
       
   473         iFetchContactsToBeAdded.Compress();
       
   474         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() fetch in progress iFetchContactsToBeAdded ,one entry removed" ) ); 
       
   475         }
       
   476     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunL() end") );
       
   477     }
       
   478 
       
   479 // --------------------------------------------------------------------------
       
   480 // CVIMPSTStorageVPbkLocalStore::DoCancel
       
   481 // --------------------------------------------------------------------------
       
   482 //
       
   483 void CVIMPSTStorageVPbkLocalStore::DoCancel()
       
   484     {
       
   485     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DoCancel()") );
       
   486     }
       
   487 
       
   488 // --------------------------------------------------------------------------
       
   489 // CVIMPSTStorageVPbkLocalStore::RunError
       
   490 // --------------------------------------------------------------------------
       
   491 //
       
   492 TInt CVIMPSTStorageVPbkLocalStore::RunError( TInt aError )
       
   493     { 
       
   494     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RunError()") );   
       
   495     return aError;
       
   496     }
       
   497 
       
   498 //////////////////////////////////////////////////////////
       
   499 // ---------------------------------------------------------------------------
       
   500 // CVIMPSTStorageVPbkLocalStore::CreateVPbkContactL
       
   501 // ---------------------------------------------------------------------------
       
   502 // 
       
   503 TInt CVIMPSTStorageVPbkLocalStore::CreateVPbkContactL(const TDesC& aUserId, 
       
   504         										   	  const TDesC& aDisplayName ,
       
   505         										   	  TBool aInvitationAutoAccept /* = EFalse */ )   
       
   506 	{
       
   507 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateVPbkContactL() begin") ); 
       
   508 	TInt error = KErrGeneral;
       
   509 	if( aInvitationAutoAccept)
       
   510 	    {
       
   511 	    TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateVPbkContactL() autoaccept add contact") ); 
       
   512 	    delete iInviteId;
       
   513 	    iInviteId = NULL;
       
   514 	    iInviteId = aUserId.AllocL();
       
   515 	    error = iVPbkStoreHandler->CreateVPbkContactL( *iInviteId,aDisplayName ); 
       
   516 	    }
       
   517 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateVPbkContactL() error = %d"),error ); 
       
   518 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateVPbkContactL() begin") ); 
       
   519 	return error;
       
   520 	}
       
   521 
       
   522 // ---------------------------------------------------------------------------
       
   523 // CVIMPSTStorageVPbkLocalStore::RemoveVPbkContactL
       
   524 // ---------------------------------------------------------------------------
       
   525 //  
       
   526 TInt CVIMPSTStorageVPbkLocalStore::RemoveVPbkContactL(const MVPbkContactLink& aContactLink) 
       
   527 	{
       
   528 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveVPbkContactL() begin") ); 
       
   529 	TInt error = KErrNotFound;
       
   530 	// text now holds the name (or first field of the contact) 
       
   531 	TInt32 linkId = iIdConverter->LinkToIdentifier(aContactLink);
       
   532 	// a contact entry has been deleted from virtual database store
       
   533 	TDbColNo colNo = iColset->ColNo( KContactId );
       
   534 	if ( SeekRowL( colNo, linkId ) )
       
   535 		{
       
   536 		TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveVPbkContactL() link exist in db") ); 
       
   537 		// contatc found in local database ,delete from local database 
       
   538 		DeleteContactFromInternalStoreL( linkId );
       
   539 		TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveVPbkContactL() link deleted from db") ); 
       
   540 		// delete from cache contact
       
   541 		error = iServiceCacheWriter.RemoveContactFromCacheL(aContactLink, 
       
   542 															TVIMPSTEnums::EStorageEventContactDelete );
       
   543 		TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveVPbkContactL() link deleted from cache") ); 
       
   544 		}
       
   545 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveVPbkContactL() error %d"),error );
       
   546 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveVPbkContactL() end") ); 
       
   547     return error;
       
   548   	}
       
   549  
       
   550 // ---------------------------------------------------------------------------
       
   551 // CVIMPSTStorageVPbkLocalStore::RemoveAllVPbkContactsL
       
   552 // ---------------------------------------------------------------------------
       
   553 // 
       
   554 TInt CVIMPSTStorageVPbkLocalStore::RemoveAllVPbkContactsL() 
       
   555 	{
       
   556 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveAllVPbkContactsL() begin" ) );
       
   557 	iRetrivedContactArray.ResetAndDestroy(); 
       
   558 	DeleteAllContactFromInternalStoreL();
       
   559 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveAllVPbkContactsL() all contacts deleted from db") );
       
   560 	iServiceCacheWriter.RemoveAllCacheContactsL();
       
   561 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveAllVPbkContactsL() all contacts deleted from cache") );
       
   562 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveAllVPbkContactsL() end" ) );
       
   563 	return KErrNone;// local store : delete from local store ,own user has changed
       
   564 	}
       
   565 // ---------------------------------------------------------------------------
       
   566 // CVIMPSTStorageVPbkLocalStore::RetrieveVPbkXSPIdL
       
   567 // ---------------------------------------------------------------------------
       
   568 //  
       
   569 TInt CVIMPSTStorageVPbkLocalStore::RetrieveVPbkXSPIdL(const TDesC8& aPackedLinks ) 
       
   570 	{
       
   571 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RetrieveVPbkXSPIdL() begin" ) );
       
   572 	iRetrivedContactArray.ResetAndDestroy();
       
   573 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RetrieveVPbkXSPIdL() iRetrivedContactArray Reset" ) );
       
   574     iVPbkStoreHandler->RetrieveVPbkContactL( aPackedLinks );
       
   575     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RetrieveVPbkXSPIdL() iRetrivedContactArray count= %d " ),iRetrivedContactArray.Count() );
       
   576     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RetrieveVPbkXSPIdL() end " ) );
       
   577     return iRetrivedContactArray.Count();
       
   578     }
       
   579 
       
   580  // ---------------------------------------------------------------------------
       
   581 // CVIMPSTStorageVPbkLocalStore::GetRetrieveVPbkXSPIdL
       
   582 // ---------------------------------------------------------------------------
       
   583 //  
       
   584 const TDesC& CVIMPSTStorageVPbkLocalStore::GetRetrieveVPbkXSPIdL(TInt aIndex ) 
       
   585 	{
       
   586 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::GetRetrieveVPbkXSPIdL() begin" ) );
       
   587 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::GetRetrieveVPbkXSPIdL() aIndex = %d" ),aIndex );
       
   588 	MVIMPSTStorageContact* contact = NULL;
       
   589 	if( aIndex >= 0 && aIndex < iRetrivedContactArray.Count() )
       
   590         {
       
   591         contact = iRetrivedContactArray[ aIndex ];
       
   592         }
       
   593     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::GetRetrieveVPbkXSPIdL() end" ) );   
       
   594     return contact ? contact->UserId() : KNullDesC ; 	
       
   595 	}
       
   596 // ---------------------------------------------------------------------------
       
   597 // CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL
       
   598 // ---------------------------------------------------------------------------
       
   599 // 
       
   600 TInt CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL( TInt aIndexToUse  )   
       
   601     {
       
   602     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL() begin" ) );
       
   603     if( aIndexToUse < 0 && aIndexToUse >= iRetrivedContactArray.Count() )
       
   604         {
       
   605         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL() argument error" ) );
       
   606         return KErrArgument;
       
   607         }
       
   608     TInt error = KErrAlreadyExists;
       
   609     MVIMPSTStorageContact* retrivedContact = iRetrivedContactArray[ aIndexToUse ];
       
   610     
       
   611     MVPbkContactLink* contactLink = retrivedContact->ContactLink();
       
   612     MVIMPSTStorageContact* exist = NULL;
       
   613     if( contactLink )
       
   614 	    {
       
   615 	    exist = iServiceCacheWriter.FindCacheContactByLink( *contactLink  );		
       
   616 	    }
       
   617     if( !exist )
       
   618         {
       
   619         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL() adding to  cache" ) );
       
   620         MVPbkContactLink* link = retrivedContact->ContactLink();
       
   621         if( link )
       
   622 			{
       
   623 			CVIMPSTStorageContact* contactToAdd = CVIMPSTStorageContact::NewL( retrivedContact->UserId() ,
       
   624 												    					 	  retrivedContact->Name(),
       
   625 																	    	 *link,
       
   626 																	    	 retrivedContact->AvatarContent()
       
   627 																	    	 );
       
   628 			CleanupStack::PushL( contactToAdd );
       
   629 			TInt32 linkId = iIdConverter->LinkToIdentifier( *link );
       
   630 			error = iServiceCacheWriter.AddStorageContactToCacheL( contactToAdd ); // contactToAdd ownership is transfered
       
   631 			TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL() adding to  cache done" ) );
       
   632 			if( error == KErrNone )
       
   633 				{
       
   634 				CleanupStack::Pop(); // contactToAdd , Ownership is transfered to AddStorageContactToCacheL
       
   635 				WriteToStoreDbL( linkId );	
       
   636 				}
       
   637 			else
       
   638 				{
       
   639 				CleanupStack::PopAndDestroy(); // contactToAdd	
       
   640 				}
       
   641 			TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL() adding to  db done" ) );
       
   642 			}
       
   643         }
       
   644     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL() error =%d" ),error );
       
   645     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateRetriveVPbkContactL() end" ) );
       
   646     return error;
       
   647     }
       
   648 
       
   649 // ---------------------------------------------------------------------------
       
   650 // CVIMPSTStorageVPbkLocalStore::deleteRetriveVPbkContactL
       
   651 // ---------------------------------------------------------------------------
       
   652 // 
       
   653 TInt CVIMPSTStorageVPbkLocalStore::deleteRetriveVPbkContactL( TInt aIndexToUse  )   
       
   654     {
       
   655     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::deleteRetriveVPbkContactL() begin" ) );
       
   656     if( aIndexToUse < 0 && aIndexToUse >= iRetrivedContactArray.Count() )
       
   657         {
       
   658         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::deleteRetriveVPbkContactL() argument error" ) );
       
   659         return KErrArgument;
       
   660         }
       
   661     MVIMPSTStorageContact* retrivedContact = iRetrivedContactArray[ aIndexToUse ];
       
   662     MVPbkContactLink* contactLink = retrivedContact->ContactLink();
       
   663     // delete from cache contact
       
   664     CVPbkContactLinkArray* contactsToDelete = CVPbkContactLinkArray::NewLC();
       
   665     MVPbkContactLink* link = contactLink->CloneLC();    
       
   666     contactsToDelete->AppendL( link ); // takes ownership
       
   667     CleanupStack::Pop(); // link
       
   668     TInt error = iVPbkStoreHandler->RemoveVPbkContactL( *contactsToDelete );
       
   669     CleanupStack::PopAndDestroy(); // contactsToDelete
       
   670     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::deleteRetriveVPbkContactL() begin" ) );
       
   671     return error;
       
   672     }
       
   673 // CVIMPSTStorageVPbkLocalStore::UpdateAvatarFieldDataL()
       
   674 // ---------------------------------------------------------------------------
       
   675 //
       
   676 TInt CVIMPSTStorageVPbkLocalStore::UpdateAvatarFieldDataL(const MVPbkContactLink& aContactLink,
       
   677 														  const TDesC8& aAvatartData )
       
   678     {
       
   679     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::UpdateAvatarFieldDataL()" ) );
       
   680     return iVPbkStoreHandler->UpdateAvatarFieldDataL( aContactLink, aAvatartData );
       
   681     }
       
   682 
       
   683 // ---------------------------------------------------------------------------
       
   684 // CVIMPSTStorageVPbkLocalStore::RemoveAvatarFieldL()
       
   685 // ---------------------------------------------------------------------------
       
   686 //
       
   687 TInt CVIMPSTStorageVPbkLocalStore::RemoveAvatarFieldL(MVPbkStoreContact& aStoreContact)
       
   688 	{
       
   689 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::RemoveAvatarFieldL()" ) );
       
   690 	return iVPbkStoreHandler->RemoveAvatarFieldL( aStoreContact );	
       
   691 	}
       
   692 
       
   693 // ---------------------------------------------------------------------------
       
   694 // CVIMPSTStorageVPbkLocalStore::AddVPbkFetchContactsL()
       
   695 // ---------------------------------------------------------------------------
       
   696 //
       
   697 void CVIMPSTStorageVPbkLocalStore::AddVPbkFetchContactsL( RArray <TPtrC>& aFirstNameList, 
       
   698                                                            RArray <TPtrC> &aServiceField ) 
       
   699      {
       
   700      TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::AddVPbkFetchContactsL() begin" ) );
       
   701      // in case of server contacts delete all contacts
       
   702      ResetAndDestroyLocalArrayL();
       
   703      TInt count = aServiceField.Count();
       
   704      TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::AddVPbkFetchContactsL() count=%d" ),count );
       
   705      for( TInt i = 0; i<count; i++ )
       
   706          {
       
   707          TVIMPSTContactInfo contactInf =
       
   708 							         {
       
   709 							         aServiceField[i].AllocL() ,
       
   710 							         aFirstNameList[i].AllocL()	
       
   711 							         };
       
   712          // not exist need to add 
       
   713          iFetchContactsToBeAdded.Append( contactInf );
       
   714          }
       
   715      if( iFetchStep == EContactReadComplete )
       
   716 	     {
       
   717 	     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::AddVPbkFetchContactsL() start fetcing" ) );
       
   718 	     IssueRequest();	
       
   719 	     }
       
   720 	 else
       
   721 		 {
       
   722 		 TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::AddVPbkFetchContactsL() EFetchingCalled" ) );
       
   723 		 iFetchStep = EFetchingCalled;	
       
   724 		 }
       
   725 	 TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::AddVPbkFetchContactsL() end" ) );
       
   726      }
       
   727 
       
   728 //******************* Database Operations *********************************//
       
   729 // -----------------------------------------------------------
       
   730 // CVIMPSTStorageVPbkLocalStore::DbExists
       
   731 // -----------------------------------------------------------
       
   732 //
       
   733 TBool CVIMPSTStorageVPbkLocalStore::DbExists()
       
   734 	{
       
   735 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DbExists() begin" ) );
       
   736 	RFile temp;
       
   737 
       
   738 	TBuf< KMaxPath > storagePath;
       
   739 
       
   740 	storagePath.Append( KDbPath );    
       
   741 	storagePath.Append( *iLocalDBName );
       
   742 	TBool ret = ETrue;
       
   743 	TInt err( temp.Open( iFs, *(&storagePath), EFileShareReadersOrWriters|EFileRead ) );
       
   744 	TInt size( 0 );
       
   745 	if ( err == KErrNone )
       
   746 		{
       
   747 		temp.Size( size );
       
   748 		}
       
   749 	temp.Close();
       
   750 	if ( (size == 0) || ( err != KErrNone ) )
       
   751 		{
       
   752 		ret = EFalse;
       
   753 		}
       
   754 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DbExists() end" ) );
       
   755 	return ret;
       
   756 	}
       
   757 
       
   758 
       
   759 // -----------------------------------------------------------
       
   760 // CVIMPSTStorageVPbkLocalStore::CreateDbL
       
   761 // -----------------------------------------------------------
       
   762 //
       
   763 void CVIMPSTStorageVPbkLocalStore::CreateDbL()
       
   764 	{
       
   765 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateDbL() begin" ) );
       
   766 	TBuf< KMaxPath > storagePath;
       
   767 	storagePath.Append( KDbPath );
       
   768 	iFs.MkDirAll( storagePath );    // make sure the directory exists	
       
   769     storagePath.Append( *iLocalDBName );
       
   770     iFs.SetAtt( *iLocalDBName ,KEntryAttHidden|KEntryAttSystem , KEntryAttNormal);
       
   771     iFileStore = CPermanentFileStore::ReplaceL( iFs, storagePath, EFileShareReadersOrWriters|EFileWrite );
       
   772     
       
   773 	iFileStore->SetTypeL( iFileStore->Layout() );
       
   774     
       
   775     TStreamId streamId( 0 );
       
   776 	TRAPD( err, streamId = iDb.CreateL( iFileStore ) );
       
   777 
       
   778     if( err ) 
       
   779         {
       
   780         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateDbL() err = %d" ),err );
       
   781         delete iFileStore;
       
   782         iFileStore = NULL;
       
   783 
       
   784         // remember to keep this updated
       
   785         iFs.Delete( storagePath );
       
   786         User::Leave( err );
       
   787         }
       
   788 
       
   789     iFileStore->SetRootL( streamId );
       
   790     TInt err2( iFileStore->Commit() );
       
   791     User::LeaveIfError( err2 );
       
   792     
       
   793 	TRAPD( err3, CreateTablesL() );
       
   794     if( err3 )
       
   795         {
       
   796         delete iFileStore;
       
   797         iFileStore = NULL;
       
   798         iDb.Close();
       
   799         iFs.Delete( storagePath );
       
   800         User::Leave( err3 );
       
   801         }
       
   802     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateDbL() end" ) );
       
   803     }    
       
   804 
       
   805 // -----------------------------------------------------------
       
   806 // CVIMPSTStorageVPbkLocalStore::OpenDbL
       
   807 // -----------------------------------------------------------
       
   808 //
       
   809 void CVIMPSTStorageVPbkLocalStore::OpenDbL()
       
   810     {
       
   811     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::OpenDbL() begin" ) );
       
   812     TBuf< KMaxPath > storagePath;
       
   813     storagePath.Append( KDbPath );
       
   814     storagePath.Append( *iLocalDBName );
       
   815     iFileStore = CPermanentFileStore::OpenL( iFs, storagePath, EFileShareReadersOrWriters|EFileWrite );   
       
   816     iFileStore->SetTypeL( iFileStore->Layout() );
       
   817     iDb.OpenL( iFileStore, iFileStore->Root() );
       
   818     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::OpenDbL() begin" ) );
       
   819     }
       
   820 
       
   821 // -----------------------------------------------------------
       
   822 // CVIMPSTStorageVPbkLocalStore::CloseDb
       
   823 // -----------------------------------------------------------
       
   824 //
       
   825 void CVIMPSTStorageVPbkLocalStore::CloseDb()
       
   826 	{ 
       
   827 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CloseDb() begin" ) );
       
   828     delete iFileStore;
       
   829     iFileStore = NULL;
       
   830     iDb.Close();
       
   831     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CloseDb() end" ) );
       
   832     }
       
   833 
       
   834 // -----------------------------------------------------------
       
   835 // CVIMPSTStorageVPbkLocalStore::CreateTablesL
       
   836 // -----------------------------------------------------------
       
   837 //
       
   838 void CVIMPSTStorageVPbkLocalStore::CreateTablesL()
       
   839 	{
       
   840 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateTablesL() begin" ) );
       
   841 	CDbColSet* contactId = CDbColSet::NewLC();
       
   842 
       
   843 	AddColumnL( KContactId, EDbColInt32, contactId );
       
   844 
       
   845 	TInt err = iDb.CreateTable( KContactTable, *contactId );
       
   846 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateTablesL() err = %d" ),err );
       
   847 	User::LeaveIfError( err );
       
   848 
       
   849 	CleanupStack::PopAndDestroy( contactId );
       
   850 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CreateTablesL() end" ) );	
       
   851 	}
       
   852 
       
   853 // -----------------------------------------------------------
       
   854 // CVIMPSTStorageVPbkLocalStore::AddColumnL
       
   855 // -----------------------------------------------------------
       
   856 //
       
   857 void CVIMPSTStorageVPbkLocalStore::AddColumnL( const TDesC& aName, TDbColType aType, CDbColSet* aColset  )
       
   858 	{
       
   859 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::AddColumnL() begin" ) );
       
   860 	TDbCol column( aName, aType );
       
   861 	aColset->AddL( column );
       
   862 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::AddColumnL() end" ) );
       
   863 	}
       
   864 
       
   865 // -----------------------------------------------------------
       
   866 // CVIMPSTStorageVPbkLocalStore::DoFreespaceLevelCheckL
       
   867 // -----------------------------------------------------------
       
   868 //
       
   869 void CVIMPSTStorageVPbkLocalStore::DoFreespaceLevelCheckL( TInt aSize )
       
   870 	{
       
   871 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DoFreespaceLevelCheckL() begin" ) );
       
   872     if ( SysUtil::FFSSpaceBelowCriticalLevelL( &iFs, aSize ) )
       
   873         {
       
   874 		User::Leave( KErrDiskFull );
       
   875         }
       
   876     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DoFreespaceLevelCheckL() end" ) );
       
   877 	}
       
   878 
       
   879 // -----------------------------------------------------------------------------
       
   880 // CVIMPSTStorageVPbkLocalStore::DeleteAllContactFromInternalStoreL
       
   881 // -----------------------------------------------------------------------------
       
   882 //
       
   883 void CVIMPSTStorageVPbkLocalStore::DeleteAllContactFromInternalStoreL()
       
   884 	{
       
   885 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DeleteAllContactFromInternalStoreL() begin" ) );
       
   886 	iColset->ColNo( KContactId );
       
   887 	iTable.LastL();	
       
   888 	while( !iTable.IsEmptyL() )
       
   889 		{
       
   890 		iTable.GetL();
       
   891 		iTable.DeleteL();
       
   892 		iDb.Compact();
       
   893 		// delete alwasy first item untill table is empty
       
   894 		iTable.LastL();
       
   895 		}
       
   896 	TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DeleteAllContactFromInternalStoreL() end" ) );
       
   897 	}
       
   898 // ----------------------------------------------------------
       
   899 // CVIMPSTStorageVPbkLocalStore::DeleteContactFromInternalStoreL
       
   900 // ----------------------------------------------------------
       
   901 //
       
   902 void CVIMPSTStorageVPbkLocalStore::DeleteContactFromInternalStoreL( TInt32& aIdentifier )
       
   903     {
       
   904     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DeleteContactFromInternalStoreL() begin" ) );
       
   905     TDbColNo colNo = iColset->ColNo( KContactId );
       
   906     if ( SeekRowL( colNo, aIdentifier ) )
       
   907         {
       
   908         iTable.DeleteL();
       
   909         iDb.Compact();
       
   910         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DeleteContactFromInternalStoreL() found deleted" ) );
       
   911         }
       
   912     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DeleteContactFromInternalStoreL() end" ) );
       
   913     }
       
   914  
       
   915 // ----------------------------------------------------------
       
   916 // CVIMPSTStorageVPbkLocalStore::WriteToStoreDbL
       
   917 // ----------------------------------------------------------
       
   918 //
       
   919 void CVIMPSTStorageVPbkLocalStore::WriteToStoreDbL( TInt32& aIdentifier )
       
   920     {
       
   921     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::WriteToStoreDbL() begin" ) );
       
   922     TDbColNo colNo = iColset->ColNo( KContactId );
       
   923     if (!SeekRowL( colNo, aIdentifier ) )        
       
   924         {
       
   925         iTable.InsertL();
       
   926      
       
   927         colNo = iColset->ColNo( KContactId );
       
   928         iTable.SetColL( colNo, aIdentifier );
       
   929             
       
   930         // Write row to table
       
   931         TRAPD( err, iTable.PutL() );
       
   932         if( err != KErrNone )
       
   933             {
       
   934             // recover
       
   935             iTable.Cancel();
       
   936             iTable.Reset();
       
   937             } 
       
   938         }
       
   939     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::WriteToStoreDbL() end" ) );    
       
   940     }
       
   941 // ----------------------------------------------------------
       
   942 // CVIMPSTStorageVPbkLocalStore::OpenTableL
       
   943 // ----------------------------------------------------------
       
   944 //
       
   945 void CVIMPSTStorageVPbkLocalStore::OpenTableL()
       
   946     {
       
   947     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::OpenTableL() begin" ) );
       
   948     TInt err( iTable.Open( iDb, KContactTable ) );
       
   949     if ( err != KErrNone )
       
   950         {
       
   951         TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::OpenTableL() err =%d" ),err );
       
   952         iTable.Close();
       
   953         User::Leave( err );
       
   954         }
       
   955     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::OpenTableL() end" ) );
       
   956     }
       
   957 
       
   958 // ----------------------------------------------------------
       
   959 // CVIMPSTStorageVPbkLocalStore::CloseTable
       
   960 // ----------------------------------------------------------
       
   961 //
       
   962 void CVIMPSTStorageVPbkLocalStore::CloseTable()
       
   963     {
       
   964     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CloseTable() begin" ) );
       
   965     iTable.Close();
       
   966     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::CloseTable() end" ) );
       
   967     }
       
   968 
       
   969 // ----------------------------------------------------------
       
   970 // CVIMPSTStorageVPbkLocalStore::ReadFirstL
       
   971 // ----------------------------------------------------------
       
   972 //
       
   973 TInt CVIMPSTStorageVPbkLocalStore::ReadFirstL(  TInt32& aIdentifier )
       
   974     { 
       
   975     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ReadFirstL() begin" ) );   
       
   976     iTable.BeginningL();
       
   977     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ReadFirstL() end" ) );
       
   978     return ReadNextL( aIdentifier );
       
   979     }
       
   980 
       
   981 // ----------------------------------------------------------
       
   982 // CVIMPSTStorageVPbkLocalStore::ReadNextL
       
   983 // ----------------------------------------------------------
       
   984 //
       
   985 TInt CVIMPSTStorageVPbkLocalStore::ReadNextL( TInt32& aIdentifier )
       
   986     { 
       
   987     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ReadNextL() begin" ) );
       
   988     TInt err = KErrNotFound;   
       
   989     if ( iTable.NextL() )
       
   990         {
       
   991         TRAP( err, ReadCurrentL( aIdentifier ) );
       
   992 		// For some reason ReadCurrentL leaves with KErrEof,
       
   993         // even if the contact was read succesfully.
       
   994         }
       
   995     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ReadNextL() err = %d" ),err );
       
   996     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ReadNextL() end" ) );
       
   997     return err;
       
   998     }
       
   999 
       
  1000 
       
  1001 // ----------------------------------------------------------
       
  1002 // CVIMPSTStorageVPbkLocalStore::ReadCurrentL
       
  1003 // ----------------------------------------------------------
       
  1004 //
       
  1005 void CVIMPSTStorageVPbkLocalStore::ReadCurrentL( TInt32& aIdentifier )
       
  1006     {
       
  1007     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ReadCurrentL() begin" ) );
       
  1008     iTable.GetL();
       
  1009 	 // Get Column number for contact data size
       
  1010     TDbColNo colNo = iColset->ColNo( KContactId );
       
  1011     aIdentifier = iTable.ColInt32(colNo);
       
  1012     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::ReadCurrentL() end" ) );
       
  1013     }
       
  1014     
       
  1015 // ----------------------------------------------------------
       
  1016 // CVIMPSTStorageVPbkLocalStore::SeekRowL
       
  1017 // ----------------------------------------------------------
       
  1018 //
       
  1019 TBool CVIMPSTStorageVPbkLocalStore::SeekRowL( TDbColNo aColNo, TInt32& aIdentifier )
       
  1020     {
       
  1021     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::SeekRowL() begin" ) );
       
  1022     TBool ret = EFalse;
       
  1023     iTable.BeginningL();
       
  1024     while ( iTable.NextL() )
       
  1025         {
       
  1026         iTable.GetL();
       
  1027         if ( iTable.ColInt32( aColNo ) == aIdentifier )
       
  1028             {
       
  1029             ret = ETrue;
       
  1030             break; 
       
  1031             }
       
  1032         }
       
  1033     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::SeekRowL() end" ) );
       
  1034     return ret;
       
  1035     }
       
  1036 // ----------------------------------------------------------
       
  1037 // CVIMPSTStorageVPbkLocalStore::DeleteDatabaseL
       
  1038 // ----------------------------------------------------------
       
  1039 void CVIMPSTStorageVPbkLocalStore::DeleteDatabaseL()
       
  1040     {
       
  1041     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DeleteDatabaseL() begin" ) );
       
  1042     TBuf< KMaxPath > storagePath;
       
  1043     storagePath.Append( KDbPath ); 
       
  1044     iFs.MkDirAll( storagePath );   
       
  1045     storagePath.Append( *iLocalDBName );
       
  1046     //close db before deleting it.
       
  1047     CloseDb();
       
  1048     iFs.Delete( storagePath );
       
  1049     TRACE( T_LIT("CVIMPSTStorageVPbkLocalStore::DeleteDatabaseL() end" ) );
       
  1050     }
       
  1051 // ----------------------------------------------------------
       
  1052 // CVIMPSTStorageVPbkLocalStore::IsSameContactIdL
       
  1053 // ----------------------------------------------------------
       
  1054 TBool CVIMPSTStorageVPbkLocalStore::IsSameContactIdL(const TDesC& aFirstId, const TDesC& aSecondId )
       
  1055     {
       
  1056     TInt colIndex = aFirstId.Find(_L(":"));
       
  1057     TPtrC firstId = aFirstId.Right( aFirstId.Length() - colIndex -1);
       
  1058     colIndex = aSecondId.Find(_L(":"));
       
  1059     TPtrC secondId = aSecondId.Right( aSecondId.Length() - colIndex -1);
       
  1060     //if( Compare( firstId,secondId  ) == 0 )
       
  1061     if(firstId.Compare(aSecondId) ==0 )
       
  1062         {
       
  1063         return ETrue;
       
  1064         }
       
  1065     return EFalse;
       
  1066     }
       
  1067 // End of file
       
  1068