connectivitymodules/SeCon/servers/syncserver/src/sconasynchandler.cpp
branchRCL_3
changeset 42 0aa8cc770c8a
parent 40 453dfc402455
child 43 4a793f564d72
equal deleted inserted replaced
40:453dfc402455 42:0aa8cc770c8a
     1 /*
       
     2 * Copyright (c) 2009 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:  CSconAsyncHandler implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "sconasynchandler.h"
       
    20 
       
    21 #include <ecom/ecom.h>
       
    22 #include <SmlDataProvider.h>
       
    23 #include <s32mem.h>
       
    24 #include <nsmlchangefinder.h>
       
    25 #include <e32cmn.h>
       
    26 #include <ecom/ImplementationInformation.h>
       
    27 #include <mmf/common/mmfcontrollerpluginresolver.h>
       
    28 
       
    29 #include "sconsyncclientserver.h"
       
    30 #include "sconsyncrelationship.h"
       
    31 #include "scondataproviderinfo.h"
       
    32 #include "debug.h"
       
    33 #include "logdatastoreformat.h"
       
    34 
       
    35 // Data Store interface implementation Uid.
       
    36 // Load all plugins with this uid.
       
    37 const TUid KDSEcomIFUid = {0x101F4D3A};
       
    38 
       
    39 const TInt KDefaultExpandSize = 1024;
       
    40 
       
    41 // one store per computer, max store count.
       
    42 const TInt KMaxStoresCount = 10;
       
    43 
       
    44 const TInt KDefaultTimeOutInMicroSeconds = 30 * 1000000; // 30 seconds
       
    45 const TInt KDeleteAllTimeOutInMicroSeconds = 300 * 1000000; // 5 minutes
       
    46 const TInt KOpenStoreTimeOutInMicroSeconds = 180 * 1000000; // 180 seconds
       
    47 
       
    48 CSconAsyncHandler::~CSconAsyncHandler()
       
    49     {
       
    50     TRACE_FUNC_ENTRY;
       
    51     Cancel();
       
    52     DoCloseStore();
       
    53     delete iItems;
       
    54     delete iWriteData;
       
    55     delete iDataStore;
       
    56     delete iDataProvider;
       
    57     delete iContext;
       
    58     delete iStoreFormat;
       
    59     iStringPool.Close();
       
    60     iFs.Close();
       
    61     REComSession::FinalClose();
       
    62     iChunk.Close();
       
    63     delete iTimeOut;
       
    64     TRACE_FUNC_EXIT;
       
    65     }
       
    66 
       
    67 CSconAsyncHandler::CSconAsyncHandler(): CActive( EPriorityStandard )
       
    68     {
       
    69     TRACE_FUNC;
       
    70     CActiveScheduler::Add( this );
       
    71     }
       
    72 
       
    73 CSconAsyncHandler* CSconAsyncHandler::NewL()
       
    74     {
       
    75     TRACE_FUNC;
       
    76     CSconAsyncHandler* self = new(ELeave) CSconAsyncHandler();
       
    77     CleanupStack::PushL( self );
       
    78     self->ConstructL();
       
    79     CleanupStack::Pop( self );
       
    80     return self;
       
    81     }
       
    82 
       
    83 void CSconAsyncHandler::ConstructL()
       
    84     {
       
    85     TRACE_FUNC;
       
    86     User::LeaveIfError( iFs.Connect() );
       
    87     TInt err = iFs.CreatePrivatePath( EDriveC );
       
    88     LOGGER_WRITE_1("CreatePrivatePath err: %d", err);
       
    89     iStringPool.OpenL();
       
    90     iTimeOut = CCSconTimeOut::NewL( *this );
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CSconAsyncHandler::HandleServiceL()
       
    95 // Handles the client request
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void CSconAsyncHandler::HandleServiceL( const RMessage2& aMessage )
       
    99     {
       
   100     TRACE_FUNC_ENTRY;
       
   101     if ( aMessage.Function() == ECancelRequest )
       
   102         {
       
   103         LOGGER_WRITE("ECancelRequest");
       
   104         if ( IsActive() )
       
   105             {
       
   106             Cancel();
       
   107             LOGGER_WRITE("iMessage.Complete( KErrCancel )");
       
   108             CompleteRequest( KErrCancel );
       
   109             }
       
   110         
       
   111         LOGGER_WRITE("aMessage.Complete( KErrNone )");
       
   112         aMessage.Complete( KErrNone );
       
   113         TRACE_FUNC_EXIT;
       
   114         return;
       
   115         }
       
   116     
       
   117     iTimeOut->Start( KDefaultTimeOutInMicroSeconds );
       
   118     
       
   119     iMessage = aMessage;
       
   120     switch ( aMessage.Function() )
       
   121         {
       
   122         case ESendChunkHandle:
       
   123             LOGGER_WRITE( "CSconSyncSession::ServiceL() : ESendChunkHandle" );
       
   124             HandleChunkMessage();
       
   125             break;
       
   126             
       
   127         case EListImplementations:
       
   128             LOGGER_WRITE( "CSconSyncSession::ServiceL() : EListImplementations" );
       
   129             ListAllImplementationsL();
       
   130             break;
       
   131             
       
   132         case EOpenStore:
       
   133             LOGGER_WRITE( "CSconSyncSession::ServiceL() : EOpenStore" );
       
   134             iTimeOut->Start( KOpenStoreTimeOutInMicroSeconds );
       
   135             OpenStoreL();
       
   136             break;
       
   137             
       
   138         case EOpenItem:
       
   139             OpenItemL();
       
   140             break;
       
   141         case ECreateItem:
       
   142             CreateItemL();
       
   143             break;
       
   144         case EReplaceItem:
       
   145             ReplaceItemL();
       
   146             break;
       
   147         case EMoveItem:
       
   148             MoveItemL();
       
   149             break;
       
   150         case EDeleteItem:
       
   151             DeleteItemL();
       
   152             break;
       
   153         case ESoftDeleteItem:
       
   154             SoftDeleteItemL();
       
   155             break;
       
   156         case EDeleteAllItems:
       
   157             iTimeOut->Start( KDeleteAllTimeOutInMicroSeconds );
       
   158             DeleteAllItemsL();
       
   159             break;
       
   160         case EReadParent:
       
   161             ReadParentL();
       
   162             break;
       
   163             
       
   164         case EHasHistory:
       
   165             HasHistoryL();
       
   166             break;
       
   167         case EAddedItems:
       
   168             AddedItemsL();
       
   169             break;
       
   170         case EDeletedItems:
       
   171             DeletedItemsL();
       
   172             break;
       
   173         case ESoftDeletedItems:
       
   174             SoftDeletedItemsL();
       
   175             break;
       
   176         case EModifiedItems:
       
   177             ModifiedItemsL();
       
   178             break;
       
   179         case EMovedItems:
       
   180             MovedItemsL();
       
   181             break;
       
   182         case EResetChangeInfo:
       
   183             ResetChangeInfoL();
       
   184             break;
       
   185         case ECommitChangeInfo:
       
   186             CommitChangeInfoL();
       
   187             break;
       
   188         case ECloseStore:
       
   189             CloseStore();
       
   190             break;
       
   191         case ESetSyncTimeStamp:
       
   192             SetSyncTimeStampL();
       
   193             break;
       
   194         case EGetSyncTimeStamp:
       
   195             GetSyncTimeStampL();
       
   196             break;
       
   197         case EExportStoreFormat:
       
   198             ExportStoreFormatL();
       
   199             break;
       
   200         case ESetRemoteStoreFormat:
       
   201             SetRemoteStoreFormatL();
       
   202             break;
       
   203             
       
   204         default:
       
   205             aMessage.Complete(KErrNotSupported);
       
   206             break;
       
   207         }
       
   208     TRACE_FUNC_EXIT;
       
   209     }
       
   210 
       
   211 
       
   212 void CSconAsyncHandler::RunL()
       
   213     {
       
   214     TRACE_FUNC_ENTRY;
       
   215     LOGGER_WRITE_1("iStatus.Int(): %d", iStatus.Int());
       
   216     switch ( iMessage.Function() )
       
   217         {
       
   218         case EOpenStore:
       
   219             LOGGER_WRITE( "CSconAsyncHandler::RunL() : EOpenStore" );
       
   220             CompleteRequest( iStatus.Int() );
       
   221             break;
       
   222             
       
   223         case EOpenItem:
       
   224             LOGGER_WRITE( "CSconAsyncHandler::RunL() : EOpenItem" );
       
   225             OpenItemCompletedL( iStatus.Int() );
       
   226             break;
       
   227             
       
   228         case ECreateItem:
       
   229             LOGGER_WRITE( "CSconAsyncHandler::RunL() : ECreateItem" );
       
   230             if (!iCommitInProgress)
       
   231                 {
       
   232                 // Write data to item
       
   233                 CreateItemCompletedL( iStatus.Int() );
       
   234                 }
       
   235             else
       
   236                 {
       
   237                 // commit is done, all ready
       
   238                 LOGGER_WRITE( "CSconAsyncHandler::RunL() : ECreateItem, commit done" );
       
   239                 TPckg<TSmlDbItemUid> pckg(iNewItem);
       
   240                 iMessage.WriteL( 0, pckg, 0);
       
   241                 CompleteRequest( iStatus.Int() );
       
   242                 }
       
   243             break;
       
   244             
       
   245         case EReplaceItem:
       
   246             LOGGER_WRITE( "CSconAsyncHandler::RunL() : EReplaceItem" );
       
   247             if (!iCommitInProgress)
       
   248                 {
       
   249                 // Write data to item
       
   250                 ReplaceItemCompletedL( iStatus.Int() );
       
   251                 }
       
   252             else
       
   253                 {
       
   254                 // commit is done, all ready
       
   255                 LOGGER_WRITE( "CSconAsyncHandler::RunL() : EReplaceItem, commit done" );
       
   256                 CompleteRequest( iStatus.Int() );
       
   257                 }
       
   258             break;
       
   259             
       
   260         case EMoveItem:
       
   261             LOGGER_WRITE( "CSconAsyncHandler::RunL() : EMoveItem" );
       
   262             CompleteRequest( iStatus.Int() );
       
   263             break;
       
   264             
       
   265         case EDeleteItem:
       
   266             LOGGER_WRITE( "CSconAsyncHandler::RunL() : EDeleteItem" );
       
   267             CompleteRequest( iStatus.Int() );
       
   268             break;
       
   269             
       
   270         case ESoftDeleteItem:
       
   271             LOGGER_WRITE( "CSconAsyncHandler::RunL() : ESoftDeleteItem" );
       
   272             CompleteRequest( iStatus.Int() );
       
   273             break;
       
   274             
       
   275         case EDeleteAllItems:
       
   276             LOGGER_WRITE( "CSconAsyncHandler::RunL() : EDeleteAllItems" );
       
   277             CompleteRequest( iStatus.Int() );
       
   278             break;
       
   279             
       
   280         case EReadParent:
       
   281             LOGGER_WRITE( "CSconAsyncHandler::RunL() : EReadParent" );
       
   282             ReadParentCompletedL( iStatus.Int() );
       
   283             break;
       
   284             
       
   285         case EResetChangeInfo:
       
   286             LOGGER_WRITE( "CSconAsyncHandler::RunL() : EResetChangeInfo" );
       
   287             CompleteRequest( iStatus.Int() );
       
   288             break;
       
   289             
       
   290         case ECommitChangeInfo:
       
   291             LOGGER_WRITE( "CSconAsyncHandler::RunL() : ECommitChangeInfo" );
       
   292             delete iItems;
       
   293             iItems = NULL;
       
   294             CompleteRequest( iStatus.Int() );
       
   295             break;
       
   296             
       
   297         default:
       
   298             LOGGER_WRITE("Not asynchronous function")
       
   299             CompleteRequest( KErrNotSupported );
       
   300             break;
       
   301         }
       
   302     TRACE_FUNC_EXIT;
       
   303     }
       
   304 
       
   305 TInt CSconAsyncHandler::RunError(TInt aError)
       
   306     {
       
   307     LOGGER_WRITE_1("CSconAsyncHandler::RunError() aError: %d", aError);
       
   308     delete iItems;
       
   309     iItems = NULL;
       
   310     delete iWriteData;
       
   311     iWriteData = NULL;
       
   312     
       
   313     if ( IsActive() )
       
   314         {
       
   315         LOGGER_WRITE("IsActive");
       
   316         //iDataStore->CancelRequest();
       
   317         Cancel();
       
   318         }
       
   319     
       
   320     if ( iDataStore && iMessage.Function() == EOpenItem )
       
   321         {
       
   322         LOGGER_WRITE( "CSconAsyncHandler::RunError() : EOpenItem" );
       
   323         iDataStore->CloseItem();
       
   324         }
       
   325     
       
   326     CompleteRequest( aError );
       
   327     aError = KErrNone;
       
   328     LOGGER_WRITE_1("CSconAsyncHandler::RunError() : Return %d", aError);
       
   329     return aError;
       
   330     //return KErrNone;//aError;
       
   331     }
       
   332 
       
   333 void CSconAsyncHandler::DoCancel()
       
   334     {
       
   335     TRACE_FUNC_ENTRY;
       
   336     iDataStore->CancelRequest();
       
   337     iTimeOut->Cancel();
       
   338     
       
   339     if ( iStatus == KRequestPending )
       
   340         {
       
   341         LOGGER_WRITE( "iStatus == KRequestPending" );
       
   342         }
       
   343     
       
   344     if ( iDataStore && iMessage.Function() == EOpenItem )
       
   345         {
       
   346         LOGGER_WRITE( "CSconAsyncHandler::RunError() : EOpenItem" );
       
   347         iDataStore->CloseItem();
       
   348         }
       
   349     
       
   350     TRACE_FUNC_EXIT;
       
   351     }
       
   352 
       
   353 void CSconAsyncHandler::TimeOut()
       
   354     {
       
   355     TRACE_FUNC_ENTRY;
       
   356     if ( IsActive() )
       
   357         {
       
   358         Cancel();
       
   359         CompleteRequest( KErrCancel );
       
   360         }
       
   361     TRACE_FUNC_EXIT;
       
   362     }
       
   363 
       
   364 
       
   365 void CSconAsyncHandler::HandleChunkMessage()
       
   366     {
       
   367     TRACE_FUNC_ENTRY;
       
   368     TInt ret ( KErrNone );
       
   369     
       
   370     ret = iChunk.Open( iMessage, 0, EFalse );
       
   371         
       
   372     LOGGER_WRITE_1( "CSconAsyncHandler::HandleChunkMessageL() : ret %d", ret );
       
   373     CompleteRequest( ret );
       
   374     }
       
   375 
       
   376 
       
   377 
       
   378 void CSconAsyncHandler::ListAllImplementationsL()
       
   379     {
       
   380     TRACE_FUNC_ENTRY;
       
   381     
       
   382     RImplInfoPtrArray implInfoArray;
       
   383     CleanupResetAndDestroyPushL( implInfoArray );
       
   384     REComSession::ListImplementationsL( KDSEcomIFUid, implInfoArray );
       
   385     
       
   386     const TInt KDataproviderInfoSize = 250; // 250 bytes should be enought
       
   387     TInt requiredSize = implInfoArray.Count() * KDataproviderInfoSize;
       
   388     if ( iChunk.Size() < requiredSize )
       
   389         {
       
   390         User::LeaveIfError( iChunk.Adjust( requiredSize ) );
       
   391         }
       
   392     
       
   393     RMemWriteStream stream ( iChunk.Base(), iChunk.Size() );
       
   394     CleanupClosePushL( stream );
       
   395     
       
   396     RSconDataProviderInfoArray dpInfoArray;
       
   397     CleanupResetAndDestroyPushL( dpInfoArray );
       
   398     
       
   399     // Collect information from dataproviders
       
   400     for ( TInt i=0; i < implInfoArray.Count(); i++ )
       
   401         {
       
   402         LOGGER_WRITE_1("implInfoArray: %d",i);
       
   403         CImplementationInformation& implInfo = *implInfoArray[i];
       
   404         
       
   405         CSmlDataProvider* dp(NULL);
       
   406         TRAPD( err, dp = CSmlDataProvider::NewL( implInfo.ImplementationUid().iUid ) );
       
   407         LOGGER_WRITE_2("CSmlDataProvider::NewL, uid(0x%08x) err: %d",implInfo.ImplementationUid().iUid, err );
       
   408         if ( !err )
       
   409             {
       
   410             CleanupStack::PushL( dp );
       
   411             
       
   412             CSconDataproviderInfo *dpInfo = CSconDataproviderInfo::NewL();
       
   413             CleanupStack::PushL( dpInfo );
       
   414             
       
   415             dpInfo->SetImplementationUid( implInfo.ImplementationUid() );
       
   416             LOGGER_WRITE_1("ImplementationUid: 0x%08x", implInfo.ImplementationUid().iUid );
       
   417             dpInfo->SetDisplayNameL( implInfo.DisplayName() );
       
   418             LOGGER_WRITE_1("DisplayName: %S", &implInfo.DisplayName() );
       
   419             
       
   420             dpInfo->SetDefaultStoreL( dp->DefaultStoreL() );
       
   421             LOGGER_WRITE_1("DefaultStoreL: %S", &dp->DefaultStoreL() );
       
   422             CDesCArray* stores = dp->ListStoresLC();
       
   423             dpInfo->SetStoresL( *stores );
       
   424             CleanupStack::PopAndDestroy( stores );
       
   425             
       
   426             dpInfoArray.AppendL( dpInfo );
       
   427             CleanupStack::Pop( dpInfo );
       
   428             
       
   429             CleanupStack::PopAndDestroy( dp );
       
   430             }
       
   431         }
       
   432     
       
   433     // Wrtie to stream
       
   434     stream.WriteUint16L( dpInfoArray.Count() );
       
   435     for ( TInt i = 0; i < dpInfoArray.Count(); i++ )
       
   436         {
       
   437         CSconDataproviderInfo& dpInfo = *dpInfoArray[i];
       
   438         dpInfo.ExternalizeL( stream );
       
   439         }
       
   440     
       
   441     stream.CommitL();
       
   442     
       
   443     CleanupStack::PopAndDestroy( &dpInfoArray );
       
   444     CleanupStack::PopAndDestroy( &stream );
       
   445     CleanupStack::PopAndDestroy( &implInfoArray );
       
   446     
       
   447     CompleteRequest( KErrNone );
       
   448     TRACE_FUNC_EXIT;
       
   449     }
       
   450 
       
   451 void CSconAsyncHandler::OpenStoreL()
       
   452     {
       
   453     TRACE_FUNC_ENTRY;
       
   454     if ( iDataStore )
       
   455         {
       
   456         LOGGER_WRITE("Warning: Previous DataStore was not closed properly.");
       
   457         DoCloseStore();
       
   458         }
       
   459     
       
   460     // copy data from the chunk
       
   461     RMemReadStream readStream( iChunk.Base(), iChunk.Size() );
       
   462     CleanupClosePushL( readStream );
       
   463     
       
   464     TSmlDataProviderId providerId = readStream.ReadUint32L();
       
   465     LOGGER_WRITE_1("providerId: 0x%08x", providerId);
       
   466     
       
   467     TInt len = readStream.ReadUint32L();
       
   468     HBufC* storeName = HBufC::NewLC(len);
       
   469     TPtr storeNamePtr = storeName->Des();
       
   470     readStream.ReadL(storeNamePtr, len);
       
   471     LOGGER_WRITE_1("storeName: %S", &storeNamePtr);
       
   472     
       
   473     TInt contextUid = readStream.ReadUint32L();
       
   474     
       
   475     LOGGER_WRITE_1("contextIdentifier: 0x%08x", contextUid);
       
   476     
       
   477     LOGGER_WRITE("Open provider");
       
   478     delete iDataProvider;
       
   479     iDataProvider = NULL;
       
   480     iDataProvider = CSmlDataProvider::NewL( providerId );
       
   481     
       
   482     delete iContext;
       
   483     iContext = NULL;
       
   484     
       
   485     delete iStoreFormat;
       
   486     iStoreFormat = NULL;
       
   487 
       
   488     LOGGER_WRITE("Create context");
       
   489     iContext = CSconSyncRelationship::NewL( iFs, TUid::Uid(contextUid), providerId );
       
   490     
       
   491     
       
   492     LOGGER_WRITE("Create NewStoreInstanceLC");
       
   493     iDataStore = iDataProvider->NewStoreInstanceLC();
       
   494     CleanupStack::Pop( iDataStore );
       
   495     
       
   496     SetActive();
       
   497     iStatus = KRequestPending;
       
   498     LOGGER_WRITE("OpenL");
       
   499     TRAPD(err, iDataStore->OpenL(storeNamePtr,*iContext, iStatus));
       
   500     if ( err )
       
   501         {
       
   502         // we are on active state, call request completed (RunL)
       
   503         LOGGER_WRITE_1("iDataStore->OpenL leaved with err: %d", err);
       
   504         TRequestStatus* status = &iStatus;
       
   505         User::RequestComplete( status, err );
       
   506         }
       
   507     
       
   508     CleanupStack::PopAndDestroy( storeName );
       
   509     CleanupStack::PopAndDestroy( &readStream );
       
   510     
       
   511     TRACE_FUNC_EXIT;
       
   512     }
       
   513 
       
   514 
       
   515 void CSconAsyncHandler::OpenItemL()
       
   516     {
       
   517     TRACE_FUNC_ENTRY;
       
   518     LeaveIfNoInstanceL();
       
   519     if ( IsActive() )
       
   520         {
       
   521         LOGGER_WRITE("Was still in Active");
       
   522         Cancel();
       
   523         }
       
   524     
       
   525     TSmlDbItemUid itemUid = iMessage.Int0();
       
   526     LOGGER_WRITE_1("Open item: %d", itemUid);
       
   527     
       
   528     // set default values
       
   529     iFieldChange = EFalse;
       
   530     iSize = 0;
       
   531     iParent = 0;
       
   532     iMimeType.Copy(KNullDesC);
       
   533     iMimeVer.Copy(KNullDesC);
       
   534     
       
   535     SetActive();
       
   536     LOGGER_WRITE("iDataStore->OpenItemL");
       
   537     iStatus = KRequestPending;
       
   538     TRAPD( err, iDataStore->OpenItemL(itemUid,iFieldChange,iSize,iParent,iMimeType,iMimeVer,iStatus));
       
   539     if ( err )
       
   540         {
       
   541         // we are on active state, call request completed (RunL)
       
   542         LOGGER_WRITE_1("iDataStore->OpenItemL leaved with err: %d", err);
       
   543         TRequestStatus* status = &iStatus;
       
   544         User::RequestComplete( status, err );
       
   545         }
       
   546     
       
   547     TRACE_FUNC_EXIT;
       
   548     }
       
   549 
       
   550 void CSconAsyncHandler::OpenItemCompletedL( TInt aError )
       
   551     {
       
   552     TRACE_FUNC_ENTRY;
       
   553     // item opened, now read it
       
   554     if ( aError )
       
   555         {
       
   556         CompleteRequest( aError );
       
   557         return;
       
   558         }
       
   559     LeaveIfNoInstanceL();
       
   560     
       
   561     LOGGER_WRITE_1("item size: %d", iSize);
       
   562     
       
   563     TInt totalObjectSize( 0 );
       
   564     totalObjectSize+= sizeof(TInt32);   // data length
       
   565     totalObjectSize+= iSize;            // data
       
   566     totalObjectSize+= sizeof(TInt8);    // iFieldChange
       
   567     totalObjectSize+= sizeof(TInt32);   // iParent
       
   568     totalObjectSize+= sizeof(TInt32);   // iMimeType
       
   569     totalObjectSize+= iMimeType.Length();  // iMimeType
       
   570     totalObjectSize+= sizeof(TInt32);   // iMimeVer
       
   571     totalObjectSize+= iMimeVer.Length();  // iMimeType
       
   572     
       
   573     LOGGER_WRITE_1("iChunk.Size(): %d", iChunk.Size());
       
   574     LOGGER_WRITE_1("iChunk.MaxSize(): %d", iChunk.MaxSize());
       
   575     LOGGER_WRITE_1("totalObjectSize: %d", totalObjectSize);
       
   576     
       
   577     if ( iChunk.Size() < totalObjectSize )
       
   578         {
       
   579         LOGGER_WRITE("adjust chunk");
       
   580         TInt err = iChunk.Adjust( totalObjectSize );
       
   581         LOGGER_WRITE_1("Chunk.Adjust err: %d", err);
       
   582         User::LeaveIfError( err );
       
   583         }
       
   584     LOGGER_WRITE_1("new Chunk size: %d", iChunk.Size());
       
   585     LOGGER_WRITE_1("new ChunkMaxSize(): %d", iChunk.MaxSize());
       
   586     RMemWriteStream stream ( iChunk.Base(), iChunk.Size() );
       
   587     CleanupClosePushL( stream );
       
   588     
       
   589     LOGGER_WRITE("ReadItemL");
       
   590     HBufC8* tempBuf = HBufC8::NewLC(iSize);
       
   591     TPtr8 tempPtr = tempBuf->Des();
       
   592     
       
   593     TUint8* ptr = (TUint8*) tempPtr.Ptr();
       
   594     
       
   595     TPtr8 tempPtr2( ptr, 0, iSize );
       
   596     LOGGER_WRITE_1( "tempPtr2.Size(): %d", tempPtr2.Size());
       
   597     iDataStore->ReadItemL( tempPtr2 );
       
   598     LOGGER_WRITE("ReadItemL -ok");
       
   599     
       
   600     LOGGER_WRITE_1("readed Length: %d", tempPtr2.Length() );
       
   601     LOGGER_WRITE_1("readed Size: %d", tempPtr2.Size() );
       
   602     
       
   603     LOGGER_WRITE8_1("iMimeType: %S", &iMimeType );
       
   604     LOGGER_WRITE_1("iMimeType.Length(): %d", iMimeType.Length() );
       
   605     LOGGER_WRITE_1("iMimeType Size: %d", iMimeType.Size() );
       
   606     
       
   607     LOGGER_WRITE8_1("iMimeVer: %S", &iMimeVer );
       
   608     LOGGER_WRITE_1("iMimeVer.Length(): %d", iMimeVer.Length() );
       
   609     LOGGER_WRITE_1("iMimeVer Size: %d", iMimeVer.Size() );
       
   610     
       
   611     iDataStore->CloseItem();
       
   612     LOGGER_WRITE("Write to chunk");
       
   613     stream.WriteInt32L( tempPtr2.Length() );
       
   614     stream.WriteL( tempPtr2 );
       
   615     CleanupStack::PopAndDestroy( tempBuf );
       
   616     
       
   617     stream.WriteInt8L( (TInt)iFieldChange );
       
   618     stream.WriteInt32L( iParent );
       
   619     stream.WriteInt32L( iMimeType.Length() );
       
   620     stream.WriteL( iMimeType );
       
   621     stream.WriteInt32L( iMimeVer.Length() );
       
   622     stream.WriteL( iMimeVer );
       
   623     
       
   624     stream.CommitL();
       
   625     LOGGER_WRITE("Writed ok");
       
   626     CleanupStack::PopAndDestroy( &stream );
       
   627     CompleteRequest( KErrNone );
       
   628     TRACE_FUNC_EXIT;
       
   629     }
       
   630 
       
   631 void CSconAsyncHandler::CreateItemL()
       
   632     {
       
   633     TRACE_FUNC_ENTRY;
       
   634     LeaveIfNoInstanceL();
       
   635     iCommitInProgress = EFalse;
       
   636     RMemReadStream readStream( iChunk.Base(), iChunk.Size() );
       
   637     CleanupClosePushL( readStream );
       
   638     
       
   639     TSmlDbItemUid parent = readStream.ReadUint32L();
       
   640     TInt len = readStream.ReadUint32L();
       
   641     readStream.ReadL(iMimeType, len);
       
   642     len = readStream.ReadUint32L();
       
   643     readStream.ReadL(iMimeVer, len);
       
   644     len = readStream.ReadUint32L();
       
   645     delete iWriteData;
       
   646     iWriteData = NULL;
       
   647     iWriteData = HBufC8::New(len);
       
   648     TPtr8 dataPtr = iWriteData->Des();
       
   649     readStream.ReadL(dataPtr, len);
       
   650     CleanupStack::PopAndDestroy( &readStream );
       
   651     SetActive();
       
   652     iStatus = KRequestPending;
       
   653     TRAPD(err, iDataStore->CreateItemL( iNewItem, dataPtr.Size(), parent, iMimeType, iMimeVer, iStatus ));
       
   654     if ( err )
       
   655         {
       
   656         // we are on active state, call request completed (RunL)
       
   657         LOGGER_WRITE_1("iDataStore->CreateItemL leaved with err: %d", err);
       
   658         TRequestStatus* status = &iStatus;
       
   659         User::RequestComplete( status, err );
       
   660         }
       
   661     TRACE_FUNC_EXIT;
       
   662     }
       
   663 
       
   664 void CSconAsyncHandler::CreateItemCompletedL( TInt aError )
       
   665     {
       
   666     TRACE_FUNC_ENTRY;
       
   667     User::LeaveIfError( aError );
       
   668     LeaveIfNoInstanceL();
       
   669     // CreateItem completed, now we must write the data to the created item and commit it.
       
   670     
       
   671     iDataStore->WriteItemL( iWriteData->Des() );
       
   672     delete iWriteData;
       
   673     iWriteData = NULL;
       
   674     SetActive();
       
   675     iStatus = KRequestPending;
       
   676     TRAPD(err, iDataStore->CommitItemL( iStatus ));
       
   677     if ( err )
       
   678         {
       
   679         // we are on active state, call request completed (RunL)
       
   680         LOGGER_WRITE_1("iDataStore->CommitItemL leaved with err: %d", err);
       
   681         TRequestStatus* status = &iStatus;
       
   682         User::RequestComplete( status, err );
       
   683         }
       
   684     iCommitInProgress = ETrue;
       
   685     
       
   686     TRACE_FUNC_EXIT;
       
   687     }
       
   688 
       
   689 void CSconAsyncHandler::ReplaceItemL()
       
   690     {
       
   691     TRACE_FUNC_ENTRY;
       
   692     LeaveIfNoInstanceL();
       
   693     
       
   694     iCommitInProgress = EFalse;
       
   695     RMemReadStream readStream( iChunk.Base(), iChunk.Size() );
       
   696     CleanupClosePushL( readStream );
       
   697     
       
   698     TSmlDbItemUid uid = readStream.ReadUint32L();
       
   699     TSmlDbItemUid parent = readStream.ReadUint32L();
       
   700     TBool fieldChange = readStream.ReadUint8L();
       
   701     TInt len = readStream.ReadUint32L();
       
   702     delete iWriteData;
       
   703     iWriteData = NULL;
       
   704     iWriteData = HBufC8::New(len);
       
   705     TPtr8 dataPtr = iWriteData->Des();
       
   706     readStream.ReadL(dataPtr, len);
       
   707     
       
   708     CleanupStack::PopAndDestroy( &readStream );
       
   709     SetActive();
       
   710     iStatus = KRequestPending;
       
   711     TRAPD(err,iDataStore->ReplaceItemL( uid,dataPtr.Size(),parent,fieldChange, iStatus ));
       
   712     if ( err )
       
   713         {
       
   714         // we are on active state, call request completed (RunL)
       
   715         LOGGER_WRITE_1("iDataStore->CommitItemL leaved with err: %d", err);
       
   716         TRequestStatus* status = &iStatus;
       
   717         User::RequestComplete( status, err );
       
   718         }
       
   719     TRACE_FUNC_EXIT;
       
   720     }
       
   721 
       
   722 void CSconAsyncHandler::ReplaceItemCompletedL( TInt aError )
       
   723     {
       
   724     TRACE_FUNC_ENTRY;
       
   725     User::LeaveIfError( aError );
       
   726     LeaveIfNoInstanceL();
       
   727     // CreateItem completed, now we must write the data to the created item and commit it.
       
   728     
       
   729     iDataStore->WriteItemL( iWriteData->Des() );
       
   730     delete iWriteData;
       
   731     iWriteData = NULL;
       
   732     
       
   733     SetActive();
       
   734     iStatus = KRequestPending;
       
   735     TRAPD(err, iDataStore->CommitItemL( iStatus ));
       
   736     if ( err )
       
   737         {
       
   738         // we are on active state, call request completed (RunL)
       
   739         LOGGER_WRITE_1("iDataStore->CommitItemL leaved with err: %d", err);
       
   740         TRequestStatus* status = &iStatus;
       
   741         User::RequestComplete( status, err );
       
   742         }
       
   743     iCommitInProgress = ETrue;
       
   744     TRACE_FUNC_EXIT;
       
   745     }
       
   746 
       
   747 void CSconAsyncHandler::MoveItemL()
       
   748     {
       
   749     TRACE_FUNC_ENTRY;
       
   750     LeaveIfNoInstanceL();
       
   751     TSmlDbItemUid uid = iMessage.Int0();
       
   752     TSmlDbItemUid newParent = iMessage.Int1();
       
   753     LOGGER_WRITE_1( "uid: %d", uid );
       
   754     LOGGER_WRITE_1( "newParent: %d", newParent );
       
   755     
       
   756     SetActive();
       
   757     iStatus = KRequestPending;
       
   758     TRAPD(err, iDataStore->MoveItemL( uid, newParent, iStatus )); 
       
   759     if ( err )
       
   760         {
       
   761         // we are on active state, call request completed (RunL)
       
   762         LOGGER_WRITE_1("iDataStore->MoveItemL leaved with err: %d", err);
       
   763         TRequestStatus* status = &iStatus;
       
   764         User::RequestComplete( status, err );
       
   765         }
       
   766     TRACE_FUNC_EXIT;
       
   767     }
       
   768 
       
   769 void CSconAsyncHandler::DeleteItemL()
       
   770     {
       
   771     TRACE_FUNC_ENTRY;
       
   772     LeaveIfNoInstanceL();
       
   773     
       
   774     TSmlDbItemUid uid = iMessage.Int0();
       
   775     LOGGER_WRITE_1( "uid: %d", uid );
       
   776     
       
   777     SetActive();
       
   778     iStatus = KRequestPending;
       
   779     TRAPD(err, iDataStore->DeleteItemL( uid, iStatus ));
       
   780     if ( err )
       
   781         {
       
   782         // we are on active state, call request completed (RunL)
       
   783         LOGGER_WRITE_1("iDataStore->DeleteItemL leaved with err: %d", err);
       
   784         TRequestStatus* status = &iStatus;
       
   785         User::RequestComplete( status, err );
       
   786         }
       
   787     TRACE_FUNC_EXIT;
       
   788     }
       
   789 
       
   790 void CSconAsyncHandler::SoftDeleteItemL()
       
   791     {
       
   792     TRACE_FUNC_ENTRY;
       
   793     LeaveIfNoInstanceL();
       
   794     
       
   795     TSmlDbItemUid uid = iMessage.Int0();
       
   796     LOGGER_WRITE_1( "uid: %d", uid );
       
   797     
       
   798     SetActive();
       
   799     iStatus = KRequestPending;
       
   800     TRAPD(err, iDataStore->SoftDeleteItemL( uid, iStatus ));
       
   801     if ( err )
       
   802         {
       
   803         // we are on active state, call request completed (RunL)
       
   804         LOGGER_WRITE_1("iDataStore->SoftDeleteItemL leaved with err: %d", err);
       
   805         TRequestStatus* status = &iStatus;
       
   806         User::RequestComplete( status, err );
       
   807         }
       
   808     TRACE_FUNC_EXIT;
       
   809     }
       
   810 
       
   811 void CSconAsyncHandler::DeleteAllItemsL()
       
   812     {
       
   813     TRACE_FUNC_ENTRY;
       
   814     LeaveIfNoInstanceL();
       
   815     
       
   816     SetActive();
       
   817     iStatus = KRequestPending;
       
   818     TRAPD(err, iDataStore->DeleteAllItemsL( iStatus ));
       
   819     if ( err )
       
   820         {
       
   821         // we are on active state, call request completed (RunL)
       
   822         LOGGER_WRITE_1("iDataStore->DeleteAllItemsL leaved with err: %d", err);
       
   823         TRequestStatus* status = &iStatus;
       
   824         User::RequestComplete( status, err );
       
   825         }
       
   826     TRACE_FUNC_EXIT;
       
   827     }
       
   828 
       
   829 void CSconAsyncHandler::ReadParentL()
       
   830     {
       
   831     TRACE_FUNC_ENTRY;
       
   832     OpenItemL();
       
   833     
       
   834     TRACE_FUNC_EXIT;
       
   835     }
       
   836 
       
   837 void CSconAsyncHandler::ReadParentCompletedL( TInt aError )
       
   838     {
       
   839     TRACE_FUNC_ENTRY;
       
   840     // item opened, now read it
       
   841     if ( aError )
       
   842         {
       
   843         CompleteRequest( aError );
       
   844         return;
       
   845         }
       
   846     LeaveIfNoInstanceL();
       
   847     
       
   848     iDataStore->CloseItem();
       
   849     
       
   850     TPckg<TSmlDbItemUid> pckg(iParent);
       
   851     iMessage.WriteL( 1, pckg, 0);
       
   852     CompleteRequest( KErrNone );
       
   853     TRACE_FUNC_EXIT;
       
   854     }
       
   855 
       
   856 void CSconAsyncHandler::HasHistoryL()
       
   857     {
       
   858     TRACE_FUNC_ENTRY;
       
   859     LeaveIfNoInstanceL();
       
   860     TBool hasHistory = iDataStore->HasSyncHistory();
       
   861     
       
   862     TPckgC<TBool> pckg(hasHistory);
       
   863     iMessage.WriteL( 0, pckg, 0);
       
   864     CompleteRequest( KErrNone );
       
   865     TRACE_FUNC_EXIT;
       
   866     }
       
   867 
       
   868 void CSconAsyncHandler::AddedItemsL()
       
   869     {
       
   870     TRACE_FUNC_ENTRY;
       
   871     LeaveIfNoInstanceL();
       
   872     const MSmlDataItemUidSet& items = iDataStore->AddedItems();
       
   873     
       
   874     RMemWriteStream stream ( iChunk.Base(), iChunk.Size() );
       
   875     CleanupClosePushL( stream );
       
   876     items.ExternalizeL( stream );
       
   877     CleanupStack::PopAndDestroy( &stream );
       
   878     
       
   879     CompleteRequest( KErrNone );
       
   880     TRACE_FUNC_EXIT;
       
   881     }
       
   882 
       
   883 void CSconAsyncHandler::DeletedItemsL()
       
   884     {
       
   885     TRACE_FUNC_ENTRY;
       
   886     LeaveIfNoInstanceL();
       
   887     const MSmlDataItemUidSet& items = iDataStore->DeletedItems();
       
   888     
       
   889     RMemWriteStream stream ( iChunk.Base(), iChunk.Size() );
       
   890     CleanupClosePushL( stream );
       
   891     items.ExternalizeL( stream );
       
   892     CleanupStack::PopAndDestroy( &stream );
       
   893     
       
   894     CompleteRequest( KErrNone );
       
   895     TRACE_FUNC_EXIT;
       
   896     }
       
   897 
       
   898 void CSconAsyncHandler::SoftDeletedItemsL()
       
   899     {
       
   900     TRACE_FUNC_ENTRY;
       
   901     LeaveIfNoInstanceL();
       
   902     const MSmlDataItemUidSet& items = iDataStore->SoftDeletedItems();
       
   903     
       
   904     RMemWriteStream stream ( iChunk.Base(), iChunk.Size() );
       
   905     CleanupClosePushL( stream );
       
   906     items.ExternalizeL( stream );
       
   907     CleanupStack::PopAndDestroy( &stream );
       
   908     
       
   909     CompleteRequest( KErrNone );
       
   910     TRACE_FUNC_EXIT;
       
   911     }
       
   912 
       
   913 void CSconAsyncHandler::ModifiedItemsL()
       
   914     {
       
   915     TRACE_FUNC_ENTRY;
       
   916     LeaveIfNoInstanceL();
       
   917     const MSmlDataItemUidSet& items = iDataStore->ModifiedItems();
       
   918     
       
   919     RMemWriteStream stream ( iChunk.Base(), iChunk.Size() );
       
   920     CleanupClosePushL( stream );
       
   921     items.ExternalizeL( stream );
       
   922     CleanupStack::PopAndDestroy( &stream );
       
   923     
       
   924     CompleteRequest( KErrNone );
       
   925     TRACE_FUNC_EXIT;
       
   926     }
       
   927 
       
   928 void CSconAsyncHandler::MovedItemsL()
       
   929     {
       
   930     TRACE_FUNC_ENTRY;
       
   931     LeaveIfNoInstanceL();
       
   932     const MSmlDataItemUidSet& items = iDataStore->MovedItems();
       
   933     
       
   934     RMemWriteStream stream ( iChunk.Base(), iChunk.Size() );
       
   935     CleanupClosePushL( stream );
       
   936     items.ExternalizeL( stream );
       
   937     CleanupStack::PopAndDestroy( &stream );
       
   938     
       
   939     CompleteRequest( KErrNone );
       
   940     TRACE_FUNC_EXIT;
       
   941     }
       
   942 
       
   943 void CSconAsyncHandler::ResetChangeInfoL()
       
   944     {
       
   945     TRACE_FUNC_ENTRY;
       
   946     LeaveIfNoInstanceL();
       
   947     SetActive();
       
   948     iStatus = KRequestPending;
       
   949     TRAPD(err, iDataStore->ResetChangeInfoL( iStatus ));
       
   950     if ( err )
       
   951         {
       
   952         // we are on active state, call request completed (RunL)
       
   953         LOGGER_WRITE_1("iDataStore->ResetChangeInfoL leaved with err: %d", err);
       
   954         TRequestStatus* status = &iStatus;
       
   955         User::RequestComplete( status, err );
       
   956         }
       
   957     TRACE_FUNC_EXIT;
       
   958     }
       
   959 
       
   960 void CSconAsyncHandler::CommitChangeInfoL()
       
   961     {
       
   962     TRACE_FUNC_ENTRY;
       
   963     LeaveIfNoInstanceL();
       
   964     RMemReadStream readStream( iChunk.Base(), iChunk.Size() );
       
   965     CleanupClosePushL( readStream );
       
   966     
       
   967     if ( iItems )
       
   968         {
       
   969         delete iItems;
       
   970         iItems = NULL;
       
   971         }
       
   972     iItems = new (ELeave) CNSmlDataItemUidSet();
       
   973     iItems->InternalizeL( readStream );
       
   974     
       
   975     CleanupStack::PopAndDestroy( &readStream );
       
   976     
       
   977     SetActive();
       
   978     iStatus = KRequestPending;
       
   979     TRAPD(err, iDataStore->CommitChangeInfoL( iStatus, *iItems ));
       
   980     if ( err )
       
   981         {
       
   982         // we are on active state, call request completed (RunL)
       
   983         LOGGER_WRITE_1("iDataStore->CommitChangeInfoL leaved with err: %d", err);
       
   984         TRequestStatus* status = &iStatus;
       
   985         User::RequestComplete( status, err );
       
   986         }
       
   987     TRACE_FUNC_EXIT;
       
   988     }
       
   989 
       
   990 void CSconAsyncHandler::CloseStore()
       
   991     {
       
   992     TRACE_FUNC_ENTRY;
       
   993     DoCloseStore();
       
   994     REComSession::FinalClose();
       
   995     CompleteRequest( KErrNone );
       
   996     TRACE_FUNC_EXIT;
       
   997     }
       
   998 
       
   999 void CSconAsyncHandler::SetSyncTimeStampL()
       
  1000     {
       
  1001     TRACE_FUNC_ENTRY;
       
  1002     RMemReadStream readStream( iChunk.Base(), iChunk.Size() );
       
  1003     CleanupClosePushL( readStream );
       
  1004     TInt relationId = readStream.ReadInt32L();
       
  1005     TInt providerId = readStream.ReadInt32L();
       
  1006     CleanupStack::PopAndDestroy( &readStream );
       
  1007     
       
  1008     CSconSyncRelationship::SetTimeStampL( iFs, TUid::Uid(relationId), providerId);
       
  1009     
       
  1010     CompleteRequest( KErrNone );
       
  1011     TRACE_FUNC_EXIT;
       
  1012     }
       
  1013 
       
  1014 void CSconAsyncHandler::GetSyncTimeStampL()
       
  1015     {
       
  1016     TRACE_FUNC_ENTRY;
       
  1017     TInt providerId = iMessage.Int0();
       
  1018     TInt relationId = iMessage.Int1();
       
  1019         
       
  1020     TDateTime time;
       
  1021     CSconSyncRelationship::GetTimeStampL( iFs, TUid::Uid(relationId), providerId, time);
       
  1022     
       
  1023     // write timestamp
       
  1024     TPckgC<TDateTime> timeBuf(time);
       
  1025     iMessage.WriteL( 2, timeBuf, 0);
       
  1026     
       
  1027     CompleteRequest( KErrNone );
       
  1028     TRACE_FUNC_EXIT;
       
  1029     }
       
  1030 
       
  1031 void CSconAsyncHandler::ExportStoreFormatL()
       
  1032     {
       
  1033     TRACE_FUNC_ENTRY;
       
  1034     LeaveIfNoInstanceL();
       
  1035     if ( !iDataProvider )
       
  1036         {
       
  1037         User::Leave( KErrNotReady );
       
  1038         }
       
  1039     CBufFlat* buffer = CBufFlat::NewL( KDefaultExpandSize );
       
  1040     CleanupStack::PushL( buffer );
       
  1041     RBufWriteStream stream( *buffer );
       
  1042     CleanupClosePushL( stream );
       
  1043     
       
  1044     const CSmlDataStoreFormat& storeFormat = iDataProvider->StoreFormatL();
       
  1045     TRAP_IGNORE( TLogDataStoreFormat::LogDataStoreFormatL( _L("iDataProvider->StoreFormatL()"), storeFormat ));
       
  1046     
       
  1047     storeFormat.ExternalizeL( stream );
       
  1048     
       
  1049     stream.CommitL();
       
  1050     CleanupStack::PopAndDestroy( &stream );
       
  1051     buffer->Compress();
       
  1052     
       
  1053     RMemWriteStream chunkStream( iChunk.Base(), iChunk.Size() );
       
  1054     CleanupClosePushL( chunkStream );
       
  1055     
       
  1056     chunkStream.WriteInt32L( buffer->Size() );
       
  1057     chunkStream.WriteL( buffer->Ptr(0), buffer->Size());
       
  1058     chunkStream.CommitL();
       
  1059     CleanupStack::PopAndDestroy( &chunkStream );
       
  1060     
       
  1061     CleanupStack::PopAndDestroy( buffer );
       
  1062     
       
  1063     CompleteRequest( KErrNone );
       
  1064     TRACE_FUNC_EXIT;
       
  1065     }
       
  1066 
       
  1067 void CSconAsyncHandler::SetRemoteStoreFormatL()
       
  1068     {
       
  1069     TRACE_FUNC_ENTRY;
       
  1070     LeaveIfNoInstanceL();
       
  1071     
       
  1072     RMemReadStream readStream( iChunk.Base(), iChunk.Size() );
       
  1073     CleanupClosePushL( readStream );
       
  1074     
       
  1075     if ( iStoreFormat )
       
  1076         {
       
  1077         delete iStoreFormat;
       
  1078         iStoreFormat = NULL;
       
  1079         }
       
  1080     iStoreFormat = CSmlDataStoreFormat::NewLC( iStringPool, readStream);
       
  1081     CleanupStack::Pop( iStoreFormat );
       
  1082     TRAP_IGNORE( TLogDataStoreFormat::LogDataStoreFormatL( _L("iDataProvider->SetRemoteStoreFormatL()"), *iStoreFormat ));
       
  1083     iDataStore->SetRemoteStoreFormatL( *iStoreFormat );
       
  1084     
       
  1085     CleanupStack::PopAndDestroy( &readStream );
       
  1086     
       
  1087     CompleteRequest( KErrNone );
       
  1088     TRACE_FUNC_EXIT;
       
  1089     }
       
  1090 
       
  1091 void CSconAsyncHandler::LeaveIfNoInstanceL()
       
  1092     {
       
  1093     if ( !iDataStore )
       
  1094         {
       
  1095         LOGGER_WRITE("DataStore instance was not ready, leaving KErrNotReady");
       
  1096         User::Leave( KErrNotReady );
       
  1097         }
       
  1098     }
       
  1099 
       
  1100 void CSconAsyncHandler::DoCloseStore()
       
  1101     {
       
  1102     TRACE_FUNC_ENTRY;
       
  1103     delete iItems;
       
  1104     iItems = NULL;
       
  1105     delete iWriteData;
       
  1106     iWriteData = NULL;
       
  1107     if ( iDataProvider && iContext )
       
  1108         {
       
  1109         TInt providerId = iDataProvider->Identifier();
       
  1110         TInt contextId = iContext->SyncTaskKey();
       
  1111         TRAP_IGNORE( CSconSyncRelationship::SetTimeStampL( iFs, TUid::Uid(contextId), providerId) );
       
  1112         }
       
  1113     delete iDataStore;
       
  1114     iDataStore = NULL;
       
  1115     delete iDataProvider;
       
  1116     iDataProvider = NULL;
       
  1117     delete iContext;
       
  1118     iContext = NULL;
       
  1119     delete iStoreFormat;
       
  1120     iStoreFormat = NULL;
       
  1121     
       
  1122     CleanOldStoresL();
       
  1123     
       
  1124     TRACE_FUNC_EXIT;
       
  1125     }
       
  1126 
       
  1127 void CSconAsyncHandler::CleanOldStoresL()
       
  1128     {
       
  1129     TRACE_FUNC_ENTRY;
       
  1130     TFileName path;
       
  1131     User::LeaveIfError( iFs.PrivatePath( path ) );
       
  1132     CDir* dir;
       
  1133     iFs.GetDir( path, KEntryAttNormal, ESortByDate, dir );
       
  1134     CleanupStack::PushL( dir );
       
  1135     TInt storesCount(0);
       
  1136     LOGGER_WRITE_1("count: %d", dir->Count() );
       
  1137     for ( TInt i=dir->Count()-1; i >= 0; i-- )
       
  1138         {
       
  1139         LOGGER_WRITE_1("dir[%d]", i);
       
  1140         TEntry entry = (*dir)[i];
       
  1141         LOGGER_WRITE_1("file: %S", &entry.iName);
       
  1142         _LIT(KContextStoreName, "contextstore");
       
  1143         _LIT(KTimeStoreName, "timestore");
       
  1144         if ( entry.iName.Find(KContextStoreName) == 0 )
       
  1145             {
       
  1146             storesCount++;
       
  1147             LOGGER_WRITE_1("storeNro: %d", storesCount);
       
  1148 #ifdef _DEBUG
       
  1149             TTime time = entry.iModified;
       
  1150             TDateTime dt = time.DateTime();
       
  1151             _LIT(KFormat, "%d.%d %02d:%02d");
       
  1152             TFileName mod;
       
  1153             mod.Format(KFormat, dt.Day()+1, dt.Month()+1, dt.Hour()+1,dt.Minute() );
       
  1154             LOGGER_WRITE_1("time: %S", &mod);
       
  1155 #endif
       
  1156             if ( storesCount > KMaxStoresCount )
       
  1157                 {
       
  1158                 LOGGER_WRITE_1("delete contextstore: '%S'", &entry.iName );
       
  1159                 iFs.Delete( entry.iName );
       
  1160                 TFileName timeStoreFile;
       
  1161                 timeStoreFile = entry.iName.Right(15);
       
  1162                 timeStoreFile.Insert(0, KTimeStoreName);
       
  1163                 LOGGER_WRITE_1("delete timeStoreFile: %S", &timeStoreFile);
       
  1164                 iFs.Delete( timeStoreFile );
       
  1165                 }
       
  1166             }
       
  1167         
       
  1168         }
       
  1169     CleanupStack::PopAndDestroy( dir );
       
  1170     TRACE_FUNC_EXIT;
       
  1171     }
       
  1172 
       
  1173 void CSconAsyncHandler::CompleteRequest( TInt aError )
       
  1174     {
       
  1175     TRACE_FUNC;
       
  1176     iTimeOut->Cancel();
       
  1177     LOGGER_WRITE_1("iMessage.Complete( %d )", aError);
       
  1178     iMessage.Complete( aError );
       
  1179     }