connectivitymodules/SeCon/services/pcd/src/sconsynchandler.cpp
changeset 36 9ba7f05d28a5
equal deleted inserted replaced
35:0ca79e3612d9 36:9ba7f05d28a5
       
     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:  CSconSyncHandler implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "sconsynchandler.h"
       
    20 #include <f32file.h>
       
    21 #include <S32MEM.H>
       
    22 #include <UTF.H>
       
    23 #include <mmf/common/mmfcontrollerpluginresolver.h>
       
    24 #include <bautils.h>
       
    25 #include <nsmlconstants.h>
       
    26 #include <centralrepository.h>
       
    27 
       
    28 #include "debug.h"
       
    29 
       
    30 // versions up to 5 must be backward compatible
       
    31 const TUint16 KFormatVersionNumber ( 1 );
       
    32 
       
    33 _LIT8(KCommandListStores, "SYNC: ListStores");
       
    34 _LIT8(KCommandOpenStore, "SYNC: OpenStore: ");
       
    35 _LIT8(KCommandCloseStore, "SYNC: CloseStore");
       
    36 _LIT8(KCommandListChanges, "SYNC: ListChanges");
       
    37 _LIT8(KCommandResetChangeInfo, "SYNC: ResetChangeInfo");
       
    38 _LIT8(KCommandCommitChanges, "SYNC: CommitChanges: ");
       
    39 _LIT8(KCommandReadItems, "SYNC: ReadItems: ");
       
    40 _LIT8(KCommandGetParents, "SYNC: GetParents: ");
       
    41 _LIT8(KCommandCreateItems, "SYNC: CreateItems");
       
    42 _LIT8(KCommandGetCreateItemsResponse, "SYNC: GetCreateItemsResponse");
       
    43 _LIT8(KCommandGetCreateItems, "SYNC: GetCreateItems: ");
       
    44 _LIT8(KCommandReplaceItems, "SYNC: ReplaceItems");
       
    45 _LIT8(KCommandGetReplaceItemsResponse, "SYNC: GetReplaceItemsResponse");
       
    46 _LIT8(KCommandGetReplaceItems, "SYNC: GetReplaceItems: ");
       
    47 _LIT8(KCommandMoveItems, "SYNC: MoveItems");
       
    48 _LIT8(KCommandDeleteItems, "SYNC: DeleteItems: ");
       
    49 _LIT8(KCommandSoftDeleteItems, "SYNC: SoftDeleteItems: ");
       
    50 _LIT8(KCommandDeleteAllItems, "SYNC: DeleteAllItems");
       
    51 _LIT8(KCommandDeleteAllItemsStatus, "SYNC: GetDeleteAllItemsStatus");
       
    52 _LIT8(KCommandGetStoreFormat, "SYNC: GetStoreFormat");
       
    53 _LIT8(KCommandSetRemoteStoreFormat, "SYNC: SetRemoteStoreFormat");
       
    54 _LIT8(KCommandGetRestOfData, "SYNC: GetRestOfData");
       
    55 _LIT8(KCommandCancel, "SYNC: Cancel");
       
    56 
       
    57 
       
    58 _LIT(KRemoteFormatStore, "remoteformatstore_0x%08x.dat");
       
    59 _LIT(KDataFileExtension, ".tmp");
       
    60 
       
    61 const TInt KTimeStampLength = 16;
       
    62 _LIT8(KTimeStampFormat, "%02d%02d%04dT%02d%02d%02dZ");
       
    63 
       
    64 const TInt KDefaultExpandSize = 1024;
       
    65 
       
    66 // Calendar sync specific constants
       
    67 const TInt KCalendarDsUid = 0x101F6DDE;
       
    68 
       
    69 CSconSyncHandler::~CSconSyncHandler()
       
    70     {
       
    71     TRACE_FUNC_ENTRY;
       
    72     Cancel();
       
    73     iSyncSession.Close();
       
    74     delete iResponseData;
       
    75     iFileStream.Close();
       
    76     iCreatedItems.Close();
       
    77     iReplacedItems.Close();
       
    78     iItemsToRead.Close();
       
    79     iFs.Delete( iFileInProgress );
       
    80     ClearCalendarCenrepL();
       
    81     TRACE_FUNC_EXIT;
       
    82     }
       
    83 
       
    84 CSconSyncHandler::CSconSyncHandler( RFs& aFs ) 
       
    85     : CActive( EPriorityStandard ), iFs(aFs), iCreatedItemUidPckg(iCreatedItemUid)
       
    86     {
       
    87     CActiveScheduler::Add( this );
       
    88     }
       
    89 
       
    90 CSconSyncHandler* CSconSyncHandler::NewL( RFs& aFs)
       
    91     {
       
    92     TRACE_FUNC_ENTRY;
       
    93     CSconSyncHandler* self = new (ELeave) CSconSyncHandler(aFs);
       
    94     CleanupStack::PushL( self );
       
    95     self->ConstructL();
       
    96     CleanupStack::Pop( self );
       
    97     TRACE_FUNC_EXIT;
       
    98     return self;
       
    99     }
       
   100 
       
   101 void CSconSyncHandler::ConstructL()
       
   102     {
       
   103     TInt err = iFs.CreatePrivatePath( EDriveC );
       
   104     LOGGER_WRITE_1("CreatePrivatePath err: %d", err);
       
   105     iFs.SetSessionToPrivate( EDriveC );
       
   106     }
       
   107 
       
   108 void CSconSyncHandler::HandleGetSyncRequestL( const TDesC8& aRequest, RWriteStream& aResult, TInt aMaxObjectSize )
       
   109     {
       
   110     TRACE_FUNC_ENTRY;
       
   111     iMaxObjectSize = aMaxObjectSize;
       
   112     iBytesWrited = 0;
       
   113     LOGGER_WRITE_1("iMaxObjectSize: %d", iMaxObjectSize);
       
   114     if ( !iConnected )
       
   115         {
       
   116         LOGGER_WRITE("Try to connect")
       
   117         User::LeaveIfError( iSyncSession.Connect() );
       
   118         iConnected = ETrue;
       
   119         }
       
   120     
       
   121     if ( aRequest.Compare( KCommandListStores ) == KErrNone )
       
   122         {
       
   123         ListStoresL( aResult );
       
   124         }
       
   125     else if ( aRequest.Find( KCommandOpenStore ) == 0 )
       
   126         {
       
   127         TPtrC8 params = aRequest.Mid( KCommandOpenStore().Length() );
       
   128         OpenStoreL( params, aResult );
       
   129         }
       
   130     else if ( aRequest.Find( KCommandCloseStore ) == 0 )
       
   131         {
       
   132         CloseStoreL( aResult );
       
   133         }
       
   134     else if ( aRequest.Find( KCommandListChanges ) == 0 )
       
   135         {
       
   136         ListChangesL( aResult );
       
   137         }
       
   138     else if ( aRequest.Find( KCommandResetChangeInfo ) == 0 )
       
   139         {
       
   140         ResetChangeInfoL( aResult );
       
   141         }
       
   142     else if ( aRequest.Find( KCommandCommitChanges ) == 0 )
       
   143         {
       
   144         TPtrC8 params = aRequest.Mid( KCommandCommitChanges().Length() );
       
   145         CommitChangesL( params, aResult );
       
   146         }
       
   147     else if ( aRequest.Find( KCommandReadItems ) == 0 )
       
   148         {
       
   149         TPtrC8 params = aRequest.Mid( KCommandReadItems().Length() );
       
   150         ReadItemsL( params, aResult );
       
   151         }
       
   152     else if ( aRequest.Find( KCommandGetParents ) == 0 )
       
   153         {
       
   154         TPtrC8 params = aRequest.Mid( KCommandGetParents().Length() );
       
   155         GetParentsL( params, aResult );
       
   156         }
       
   157     else if ( aRequest.Find( KCommandGetRestOfData ) == 0 )
       
   158         {
       
   159         // Get rest of data
       
   160         ReadNextDataBlockL( aResult );
       
   161         }
       
   162     else if ( aRequest.Find( KCommandGetCreateItemsResponse ) == 0 )
       
   163         {
       
   164         GetCreateItemsResponseL( KNullDesC8(), aResult );
       
   165         }
       
   166     else if ( aRequest.Find( KCommandGetCreateItems ) == 0 )
       
   167         {
       
   168         TPtrC8 params = aRequest.Mid( KCommandGetCreateItems().Length() );
       
   169         GetCreateItemsResponseL( params, aResult );
       
   170         }
       
   171     else if ( aRequest.Find( KCommandGetReplaceItemsResponse ) == 0 )
       
   172         {
       
   173         TPtrC8 params = aRequest.Mid( KCommandGetReplaceItemsResponse().Length() );
       
   174         GetReplaceItemsResponseL( KNullDesC8(), aResult );
       
   175         }
       
   176     else if ( aRequest.Find( KCommandGetReplaceItems ) == 0 )
       
   177         {
       
   178         TPtrC8 params = aRequest.Mid( KCommandGetReplaceItems().Length() );
       
   179         GetReplaceItemsResponseL( params, aResult );
       
   180         }
       
   181     else if ( aRequest.Find( KCommandMoveItems ) == 0 )
       
   182         {
       
   183         TPtrC8 params = aRequest.Mid( KCommandMoveItems().Length() );
       
   184         MoveItemsL( params, aResult );
       
   185         }
       
   186     else if ( aRequest.Find( KCommandDeleteItems ) == 0 )
       
   187         {
       
   188         TPtrC8 params = aRequest.Mid( KCommandDeleteItems().Length() );
       
   189         DeleteItemsL( params, aResult );
       
   190         }
       
   191     else if ( aRequest.Find( KCommandSoftDeleteItems ) == 0 )
       
   192         {
       
   193         TPtrC8 params = aRequest.Mid( KCommandSoftDeleteItems().Length() );
       
   194         SoftDeleteItemsL( params, aResult );
       
   195         }
       
   196     else if ( aRequest.Find( KCommandDeleteAllItems ) == 0 )
       
   197         {
       
   198         DeleteAllItemsL( aResult );
       
   199         }
       
   200     else if ( aRequest.Find( KCommandDeleteAllItemsStatus ) == 0 )
       
   201         {
       
   202         GetDeleteAllItemsStatusL( aResult );
       
   203         }
       
   204     else if ( aRequest.Find( KCommandGetStoreFormat ) == 0 )
       
   205         {
       
   206         GetStoreFormatL( aResult );
       
   207         }
       
   208     else if ( aRequest.Find( KCommandCancel ) == 0 )
       
   209         {
       
   210         CancelOperationsL( aResult );
       
   211         }
       
   212     else    
       
   213         {
       
   214         LOGGER_WRITE("Unknown command");
       
   215         User::Leave( KErrNotSupported );
       
   216         }
       
   217     TRACE_FUNC_EXIT;
       
   218     }
       
   219 
       
   220 
       
   221 void CSconSyncHandler::HandlePutSyncRequestL( const TDesC8& aRequest, RReadStream& aData )
       
   222     {
       
   223     TRACE_FUNC_ENTRY;
       
   224     if ( !iConnected )
       
   225         {
       
   226         LOGGER_WRITE("Try to connect")
       
   227         User::LeaveIfError( iSyncSession.Connect() );
       
   228         iConnected = ETrue;
       
   229         }
       
   230     if ( aRequest.Find( KCommandCreateItems ) == KErrNone )
       
   231         {
       
   232         CreateItemsL( aData );
       
   233         }
       
   234     else if ( aRequest.Find( KCommandReplaceItems ) == 0 )
       
   235         {
       
   236         ReplaceItemsL( aData );
       
   237         }
       
   238     else if ( aRequest.Find( KCommandSetRemoteStoreFormat ) == 0 )
       
   239         {
       
   240         SetRemoteStoreFormatL( aData );
       
   241         }
       
   242     else
       
   243         {
       
   244         LOGGER_WRITE("Unknown command");
       
   245         User::Leave( KErrNotSupported );
       
   246         }
       
   247     TRACE_FUNC_EXIT;
       
   248     }
       
   249 
       
   250 void CSconSyncHandler::ListStoresL( RWriteStream& aResult )
       
   251     {
       
   252     TRACE_FUNC_ENTRY;
       
   253     RSconDataProviderInfoArray infoArray(5);
       
   254     CleanupResetAndDestroyPushL( infoArray );
       
   255     iSyncSession.ListAllImplementationsL( infoArray );
       
   256     LOGGER_WRITE_1("infoArray.Count(): %d", infoArray.Count());
       
   257     aResult.WriteInt32L( infoArray.Count() );
       
   258     for (TInt i=0; i<infoArray.Count(); i++ )
       
   259         {
       
   260         CSconDataproviderInfo& info = (*infoArray[i]);
       
   261         info.ExternalizeL( aResult );
       
   262         }
       
   263     CleanupStack::PopAndDestroy( &infoArray );
       
   264     TRACE_FUNC_EXIT;
       
   265     }
       
   266 
       
   267 void CSconSyncHandler::OpenStoreL( const TDesC8& aParams, RWriteStream& aResult )
       
   268     {
       
   269     TRACE_FUNC_ENTRY;
       
   270     LOGGER_WRITE8_1("Params:'%S'", &aParams);
       
   271     
       
   272     const TUint8* ptr = aParams.Ptr();
       
   273     LOGGER_WRITE_1("Params size: %d", aParams.Size())
       
   274     RMemReadStream stream( ptr , aParams.Size() );
       
   275     CleanupClosePushL( stream );
       
   276 
       
   277     TInt providerId = stream.ReadInt32L();
       
   278     LOGGER_WRITE_1("Provider Id: 0x%08x", providerId);
       
   279     TInt contextId = stream.ReadInt32L();
       
   280     LOGGER_WRITE_1("Context Id: 0x%08x", contextId);
       
   281     
       
   282     // read utf8 formatted text
       
   283     TInt len = stream.ReadUint16L();
       
   284     
       
   285     LOGGER_WRITE_1("storeName length: %d", len);
       
   286     HBufC8* buf8 = HBufC8::NewLC( len );
       
   287     TPtr8 bufPtr8 = buf8->Des();
       
   288     stream.ReadL( bufPtr8, len );
       
   289     
       
   290     LOGGER_WRITE8_1("storeName8: %S", &bufPtr8);
       
   291     
       
   292     // and convert it to unicode
       
   293     HBufC* storeName = CnvUtfConverter::ConvertToUnicodeFromUtf8L( bufPtr8 ); 
       
   294     CleanupStack::PopAndDestroy( buf8 );
       
   295     CleanupStack::PushL( storeName );
       
   296     
       
   297     TPtr storeNamePtr = storeName->Des();
       
   298     LOGGER_WRITE_1("storeName: %S", &storeNamePtr);
       
   299     
       
   300     if ( providerId == KCalendarDsUid && aParams.Size() >
       
   301             sizeof(TInt32) // providerId
       
   302             + sizeof(TInt32) // contextId
       
   303             + sizeof(TUint16)// storeName len
       
   304             + len )          // storeName
       
   305         {
       
   306         TInt profileId = stream.ReadInt32L();
       
   307         len = stream.ReadUint16L();
       
   308         HBufC8* buffer = HBufC8::NewLC( len );
       
   309         TPtr8 bufferPtr8 = buffer->Des();
       
   310         stream.ReadL( bufferPtr8, len );
       
   311         HBufC* serverId = CnvUtfConverter::ConvertToUnicodeFromUtf8L( bufferPtr8 ); 
       
   312         CleanupStack::PopAndDestroy( buffer );
       
   313         CleanupStack::PushL( serverId );
       
   314         SetCalendarCenrepL( profileId, serverId->Des() );
       
   315         CleanupStack::PopAndDestroy(serverId);
       
   316         }
       
   317     
       
   318     aResult.WriteUint16L( KFormatVersionNumber );
       
   319     TRAPD( err, iSyncSession.OpenDataStoreL( providerId, storeName->Des(), contextId));
       
   320     if ( err == KErrServerTerminated )
       
   321         {
       
   322         // server terminated, try to reconnect
       
   323         iConnected = EFalse;
       
   324         LOGGER_WRITE("Try to re-connect");
       
   325         err = iSyncSession.Connect();
       
   326         LOGGER_WRITE_1("iSyncSession.Connect() err: %d", err);
       
   327         if ( !err )
       
   328             {
       
   329             iConnected = ETrue;
       
   330             TRAP( err, iSyncSession.OpenDataStoreL( providerId, storeName->Des(), contextId));
       
   331             }
       
   332         }
       
   333     
       
   334     CleanupStack::PopAndDestroy( storeName );
       
   335     CleanupStack::PopAndDestroy( &stream );
       
   336     
       
   337     aResult.WriteInt32L( err );
       
   338     
       
   339     if ( err )
       
   340         {
       
   341         LOGGER_WRITE_1("Error: %d", err);
       
   342         LOGGER_WRITE("iSyncSession.CloseDataStore()");
       
   343         iSyncSession.CloseDataStore();
       
   344         ClearCalendarCenrepL();
       
   345         return;
       
   346         }
       
   347     iCurrentDataProviderUid = providerId;
       
   348     iCurrentContextUid = contextId;
       
   349     
       
   350     
       
   351     // Now the store is opened
       
   352     TRAP(err, LoadRemoteStoreFormatL( contextId, providerId ));
       
   353     if ( err )
       
   354         {
       
   355         aResult.WriteUint8L( 1 ); //  Remote store format needed
       
   356         }
       
   357     else
       
   358         {
       
   359         aResult.WriteUint8L( 0 ); //  Remote store format not needed
       
   360         }
       
   361     
       
   362     // return last sync timestamp, if has synced before.
       
   363     TBool hasHistory = iSyncSession.HasSyncHistoryL();
       
   364     TUint8 historyFlag(0);
       
   365     if ( hasHistory )
       
   366         {
       
   367         historyFlag = 1;
       
   368         }
       
   369     aResult.WriteUint8L( historyFlag );
       
   370     
       
   371     if ( hasHistory )
       
   372         {
       
   373         TDateTime timeStamp;
       
   374         TInt err(KErrNone);
       
   375         TRAP(err, iSyncSession.GetSyncTimeStampL( providerId, contextId, timeStamp));
       
   376         LOGGER_WRITE_1("GetSyncTimeStampL err: %d", err);
       
   377         
       
   378         TBuf8<KTimeStampLength> timeStampBuf;
       
   379         
       
   380         timeStampBuf.Format( KTimeStampFormat, 
       
   381                 timeStamp.Day()+1,timeStamp.Month()+1, timeStamp.Year(),
       
   382                 timeStamp.Hour(), timeStamp.Minute(), timeStamp.Second() );
       
   383         LOGGER_WRITE8_1("timeStamp: %S", &timeStampBuf);
       
   384         aResult.WriteL( timeStampBuf );
       
   385         }
       
   386     
       
   387     
       
   388     //  for testin purposes only
       
   389     /*
       
   390     if (!remoteStoreSetted)
       
   391         {
       
   392         RStringPool pool;
       
   393         pool.OpenL();
       
   394         CleanupClosePushL( pool );
       
   395         CSmlDataStoreFormat* storeFormat(NULL);
       
   396         TRAPD( err, storeFormat = iSyncSession.StoreFormatL( pool ) );
       
   397         CleanupStack::PushL( storeFormat );
       
   398         
       
   399         iSyncSession.SetRemoteStoreFormatL( *storeFormat );
       
   400         SaveRemoteStoreFormatL( *storeFormat, contextId, providerId );
       
   401         
       
   402         CleanupStack::PopAndDestroy( storeFormat );
       
   403         CleanupStack::PopAndDestroy( &pool );
       
   404         }
       
   405     */
       
   406     
       
   407     TRACE_FUNC_EXIT;
       
   408     }
       
   409 
       
   410 void CSconSyncHandler::LoadRemoteStoreFormatL( TInt aContextId, TInt aProviderId )
       
   411     {
       
   412     TRACE_FUNC_ENTRY;
       
   413     TFileName remoteFormatStore;
       
   414     iFs.SetSessionToPrivate( EDriveC );
       
   415     remoteFormatStore.Format( KRemoteFormatStore, aContextId );
       
   416        
       
   417     CDictionaryFileStore* dictionaryStore =
       
   418         CDictionaryFileStore::OpenLC(iFs, remoteFormatStore, TUid::Uid(0x0001));
       
   419     TBool present = dictionaryStore->IsPresentL( TUid::Uid(aProviderId) );
       
   420     if ( !present )
       
   421         {
       
   422         LOGGER_WRITE("Remote store was not saved");
       
   423         User::Leave( KErrNotFound );
       
   424         }
       
   425     else
       
   426         {
       
   427         RDictionaryReadStream stream;
       
   428         stream.OpenLC( *dictionaryStore, TUid::Uid(aProviderId) );
       
   429         RStringPool stringPool;
       
   430         stringPool.OpenL();
       
   431         CleanupClosePushL( stringPool );
       
   432         CSmlDataStoreFormat* storeFormat = CSmlDataStoreFormat::NewLC(stringPool, stream);
       
   433         iSyncSession.SetRemoteStoreFormatL( *storeFormat );
       
   434         CleanupStack::PopAndDestroy( storeFormat );
       
   435         CleanupStack::PopAndDestroy( &stringPool );
       
   436         CleanupStack::PopAndDestroy(); //OpenLC
       
   437         }
       
   438     CleanupStack::PopAndDestroy( dictionaryStore );
       
   439     TRACE_FUNC_EXIT;
       
   440     }
       
   441 
       
   442 void CSconSyncHandler::SaveRemoteStoreFormatL( CSmlDataStoreFormat& aStoreFormat, TInt aContextId, TInt aProviderId)
       
   443     {
       
   444     TRACE_FUNC_ENTRY;
       
   445     iFs.SetSessionToPrivate( EDriveC );
       
   446     TFileName remoteFormatStore;
       
   447     remoteFormatStore.Format( KRemoteFormatStore, aContextId );
       
   448     CDictionaryFileStore* dictionaryStore =
       
   449         CDictionaryFileStore::OpenLC(iFs, remoteFormatStore, TUid::Uid(0x0001));
       
   450     
       
   451     RDictionaryWriteStream stream;
       
   452     stream.AssignLC( *dictionaryStore, TUid::Uid(aProviderId) );
       
   453     
       
   454     aStoreFormat.ExternalizeL( stream );
       
   455     stream.CommitL();
       
   456     CleanupStack::PopAndDestroy(); //AssignLC
       
   457     dictionaryStore->CommitL();
       
   458         
       
   459     CleanupStack::PopAndDestroy( dictionaryStore );
       
   460     TRACE_FUNC_EXIT;
       
   461     }
       
   462 
       
   463 void CSconSyncHandler::CloseStoreL( RWriteStream& aResult )
       
   464     {
       
   465     TRACE_FUNC_ENTRY;
       
   466     iSyncSession.CloseDataStore();
       
   467     ClearCalendarCenrepL();
       
   468     
       
   469     // Get last used timestamp if provider was open
       
   470     if ( iCurrentDataProviderUid != 0 )
       
   471         {
       
   472         TDateTime timeStamp;
       
   473         iSyncSession.GetSyncTimeStampL(iCurrentDataProviderUid, iCurrentContextUid, timeStamp );
       
   474         
       
   475         iCurrentDataProviderUid = 0;
       
   476         iCurrentContextUid = 0;
       
   477         
       
   478         const TInt KTimeStampLength = 16;
       
   479         TBuf8<KTimeStampLength> timeStampBuf;
       
   480         
       
   481         
       
   482         timeStampBuf.Format( KTimeStampFormat, 
       
   483                 timeStamp.Day()+1,timeStamp.Month()+1, timeStamp.Year(),
       
   484                 timeStamp.Hour(), timeStamp.Minute(), timeStamp.Second() );
       
   485         aResult.WriteL( timeStampBuf );
       
   486         }
       
   487     
       
   488     TRACE_FUNC_EXIT;
       
   489     }
       
   490 
       
   491 void CSconSyncHandler::ListChangesL( RWriteStream& aResult )
       
   492     {
       
   493     TRACE_FUNC_ENTRY;
       
   494     RArray<TSmlDbItemUid> items;
       
   495     CleanupClosePushL( items );
       
   496     
       
   497     iSyncSession.AddedItemsL( items );
       
   498     LOGGER_WRITE_1("Added items count: %d", items.Count());
       
   499     aResult.WriteInt32L( items.Count() );
       
   500     for (TInt i=0; i<items.Count(); i++ )
       
   501         {
       
   502         aResult.WriteInt32L( items[i] );
       
   503         }
       
   504     
       
   505     items.Reset();
       
   506     iSyncSession.ModifiedItemsL( items );
       
   507     LOGGER_WRITE_1("Modified items count: %d", items.Count());
       
   508     aResult.WriteInt32L( items.Count() );
       
   509     for (TInt i=0; i<items.Count(); i++ )
       
   510         {
       
   511         aResult.WriteInt32L( items[i] );
       
   512         }
       
   513     
       
   514     items.Reset();
       
   515     iSyncSession.MovedItemsL( items );
       
   516     LOGGER_WRITE_1("Moved items count: %d", items.Count());
       
   517     aResult.WriteInt32L( items.Count() );
       
   518     for (TInt i=0; i<items.Count(); i++ )
       
   519         {
       
   520         aResult.WriteInt32L( items[i] );
       
   521         }
       
   522     
       
   523     items.Reset();
       
   524     iSyncSession.DeletedItemsL( items );
       
   525     LOGGER_WRITE_1("Deleted items count: %d", items.Count());
       
   526     aResult.WriteInt32L( items.Count() );
       
   527     for (TInt i=0; i<items.Count(); i++ )
       
   528         {
       
   529         aResult.WriteInt32L( items[i] );
       
   530         }
       
   531     
       
   532     items.Reset();
       
   533     iSyncSession.SoftDeletedItemsL( items );
       
   534     LOGGER_WRITE_1("SoftDeleted items count: %d", items.Count());
       
   535     aResult.WriteInt32L( items.Count() );
       
   536     for (TInt i=0; i<items.Count(); i++ )
       
   537         {
       
   538         aResult.WriteInt32L( items[i] );
       
   539         }
       
   540     CleanupStack::PopAndDestroy( &items );
       
   541     
       
   542     TRACE_FUNC_EXIT;
       
   543     }
       
   544 
       
   545 void CSconSyncHandler::ResetChangeInfoL( RWriteStream& /*aResult*/ )
       
   546     {
       
   547     TRACE_FUNC_ENTRY;
       
   548     iSyncSession.ResetChangeInfoL();
       
   549     TRACE_FUNC_EXIT;
       
   550     }
       
   551 
       
   552 void CSconSyncHandler::CommitChangesL( const TDesC8& aParams, RWriteStream& /*aResult*/ )
       
   553     {
       
   554     TRACE_FUNC_ENTRY;
       
   555     const TUint8* ptr = aParams.Ptr();
       
   556     RMemReadStream stream( ptr , aParams.Size() );
       
   557     CleanupClosePushL( stream );
       
   558     
       
   559     TInt itemCount = stream.ReadInt32L();
       
   560     RArray<TSmlDbItemUid> items;
       
   561     CleanupClosePushL( items );
       
   562     for ( TInt i=0; i<itemCount; i++ )
       
   563         {
       
   564         items.AppendL( stream.ReadInt32L() );
       
   565         }
       
   566     
       
   567     if ( items.Count() > 0 )
       
   568         {
       
   569         iSyncSession.CommitChangeInfoL( items );
       
   570         }
       
   571     
       
   572     CleanupStack::PopAndDestroy( &items );
       
   573     CleanupStack::PopAndDestroy( &stream );
       
   574     
       
   575     TRACE_FUNC_EXIT;
       
   576     }
       
   577 
       
   578 void CSconSyncHandler::ReadItemsL( const TDesC8& aParams, RWriteStream& aResult )
       
   579     {
       
   580     TRACE_FUNC_ENTRY;
       
   581     iItemsToRead.Reset();
       
   582     LOGGER_WRITE_1("aParams length: %d", aParams.Length());
       
   583     
       
   584     const TUint8* ptr = aParams.Ptr();
       
   585     RMemReadStream stream( ptr , aParams.Size() );
       
   586     CleanupClosePushL( stream );
       
   587     
       
   588     // Read item uids from parameter
       
   589     TInt itemCount = stream.ReadInt32L();
       
   590     LOGGER_WRITE_1("Item count: %d", itemCount);
       
   591     for ( TInt i=0; i<itemCount; i++ )
       
   592         {
       
   593         iItemsToRead.AppendL( stream.ReadInt32L() );
       
   594         LOGGER_WRITE_2("Item[%d] = %d", i, iItemsToRead[i] );
       
   595         }
       
   596     CleanupStack::PopAndDestroy( &stream );
       
   597     LOGGER_WRITE( "Items readed ok" );
       
   598     
       
   599     aResult.WriteInt32L( iItemsToRead.Count() );
       
   600     iBytesWrited += sizeof(TInt32);
       
   601     
       
   602     ReadNextDataBlockL( aResult );
       
   603     
       
   604     TRACE_FUNC_EXIT;
       
   605     }
       
   606 
       
   607 void CSconSyncHandler::GetParentsL( const TDesC8& aParams, RWriteStream& aResult )
       
   608     {
       
   609     TRACE_FUNC_ENTRY;
       
   610     const TUint8* ptr = aParams.Ptr();
       
   611     RMemReadStream stream( ptr , aParams.Size() );
       
   612     CleanupClosePushL( stream );
       
   613     
       
   614     // Read item uids from parameter
       
   615     TInt itemCount = stream.ReadInt32L();
       
   616     aResult.WriteInt32L( itemCount );
       
   617     LOGGER_WRITE_1("Item count: %d", itemCount);
       
   618     for ( TInt i=0; i<itemCount; i++ )
       
   619         {
       
   620         TSmlDbItemUid itemUid = stream.ReadInt32L();
       
   621         TSmlDbItemUid parent(KErrNotFound);
       
   622         aResult.WriteInt32L( itemUid );
       
   623         TInt err = iSyncSession.GetParent( itemUid, parent);
       
   624         LOGGER_WRITE_1("itemUid: %d", itemUid);
       
   625         LOGGER_WRITE_1("err: %d", err);
       
   626         aResult.WriteInt32L( err );
       
   627         if ( err == KErrNone )
       
   628             {
       
   629             LOGGER_WRITE_1("parent: %d", parent);
       
   630             aResult.WriteInt32L( parent );
       
   631             }
       
   632         }
       
   633     CleanupStack::PopAndDestroy( &stream );
       
   634     aResult.CommitL();
       
   635     TRACE_FUNC_EXIT;
       
   636     }
       
   637 
       
   638 void CSconSyncHandler::ReadNextDataBlockL( RWriteStream& aResult )
       
   639     {
       
   640     TRACE_FUNC_ENTRY;
       
   641     if ( (!iResponseData || iResponseData->Size() == 0 ) && iItemsToRead.Count() == 0 )
       
   642         {
       
   643         // no data
       
   644         LOGGER_WRITE("no more data or items");
       
   645         User::Leave(KErrEof);
       
   646         }
       
   647     
       
   648     if ( iResponseData )
       
   649         {
       
   650         // write data from tempbuffer to response buffer
       
   651         TInt writeLength = iResponseData->Size();
       
   652         if ( writeLength > iMaxObjectSize-iBytesWrited )
       
   653             {
       
   654             writeLength = iMaxObjectSize-iBytesWrited;
       
   655             }
       
   656         aResult.WriteL( iResponseData->Ptr(0), writeLength );
       
   657         iBytesWrited += writeLength;
       
   658         
       
   659         if ( iResponseData->Size()  > writeLength )
       
   660             {
       
   661             iResponseData->Delete(0, writeLength);
       
   662             }
       
   663         else
       
   664             {
       
   665             iResponseData->Reset();
       
   666             }
       
   667         }
       
   668     if ( iBytesWrited == iMaxObjectSize )
       
   669         {
       
   670         // responce buffer is full, return it.
       
   671         LOGGER_WRITE("Stream is full, return it");
       
   672         return;
       
   673         }
       
   674     
       
   675     TBool streamIsFull( EFalse );
       
   676     // Read items from server
       
   677     CBufFlat* dataBuffer = CBufFlat::NewL(KDefaultExpandSize);
       
   678     CleanupStack::PushL( dataBuffer );
       
   679     while ( iItemsToRead.Count() > 0 && !streamIsFull )
       
   680         {
       
   681         if ( iMaxObjectSize-iBytesWrited < sizeof(TUint32) )
       
   682             {
       
   683             streamIsFull = ETrue;
       
   684             LOGGER_WRITE("Stream is full, don't read next item");
       
   685             continue;
       
   686             }
       
   687         ReadItemL( iItemsToRead[0], *dataBuffer );
       
   688         aResult.WriteUint32L( dataBuffer->Size() );
       
   689         iBytesWrited += sizeof( TUint32 );
       
   690         
       
   691         TInt writeLength = dataBuffer->Size();
       
   692         if ( writeLength > iMaxObjectSize-iBytesWrited )
       
   693             {
       
   694             writeLength = iMaxObjectSize-iBytesWrited;
       
   695             LOGGER_WRITE_1("Write only %d bytes", writeLength);
       
   696             }
       
   697         aResult.WriteL( dataBuffer->Ptr(0), writeLength );
       
   698         iBytesWrited += writeLength;
       
   699         
       
   700         if ( dataBuffer->Size()  > writeLength )
       
   701             {
       
   702             // write rest to next data block
       
   703             if ( !iResponseData )
       
   704                 {
       
   705                 iResponseData = CBufFlat::NewL(KDefaultExpandSize);
       
   706                 }
       
   707             iResponseData->Reset();
       
   708             LOGGER_WRITE_1("Save %d bytes for next request", dataBuffer->Size() - writeLength);
       
   709             iResponseData->ResizeL( dataBuffer->Size() - writeLength );
       
   710             iResponseData->Write(0, dataBuffer->Ptr( writeLength ));
       
   711             streamIsFull = ETrue;
       
   712             }
       
   713         
       
   714         iItemsToRead.Remove(0);
       
   715         
       
   716         if ( iBytesWrited == iMaxObjectSize )
       
   717             {
       
   718             // writestream is full
       
   719             LOGGER_WRITE("Stream is full");
       
   720             streamIsFull = ETrue;
       
   721             }
       
   722         
       
   723         }
       
   724     aResult.CommitL();
       
   725     
       
   726     CleanupStack::PopAndDestroy( dataBuffer );
       
   727     TRACE_FUNC_EXIT;
       
   728     }
       
   729 
       
   730 void CSconSyncHandler::ReadItemL( TSmlDbItemUid aUid, CBufFlat& aItemData )
       
   731     {
       
   732     TRACE_FUNC_ENTRY;
       
   733     aItemData.Reset();
       
   734     CBufFlat* dataBuffer = CBufFlat::NewL(KDefaultExpandSize);
       
   735     CleanupStack::PushL( dataBuffer );
       
   736     TSmlDbItemUid parent(-1);
       
   737     TBool fieldChange(EFalse);
       
   738     
       
   739     TBuf8<256> mimeType;
       
   740     TBuf8<100> mimeVer;
       
   741     TRAPD( err, iSyncSession.OpenItemL( aUid, fieldChange,
       
   742                     parent, mimeType, mimeVer, *dataBuffer ));
       
   743     if ( err )
       
   744         {
       
   745         LOGGER_WRITE_1("Could not read item %d", aUid);
       
   746         // only item uid and errorcode will be writed
       
   747         aItemData.ResizeL( 
       
   748             sizeof(TInt32) +
       
   749             sizeof(TInt32));
       
   750         
       
   751         }
       
   752     else
       
   753         {
       
   754         // reserve memory for all fields
       
   755         aItemData.ResizeL( 
       
   756             sizeof(TInt32) +
       
   757             sizeof(TInt32) +
       
   758             sizeof(TUint8) +
       
   759             sizeof(TInt32) +
       
   760             sizeof(TUint16) +
       
   761             mimeType.Length() +
       
   762             sizeof(TUint16) +
       
   763             mimeVer.Length() +
       
   764             sizeof(TInt32) +
       
   765             dataBuffer->Size()
       
   766             );
       
   767         }
       
   768     
       
   769     RBufWriteStream tempStream( aItemData );
       
   770     CleanupClosePushL( tempStream );
       
   771     tempStream.WriteInt32L( aUid );
       
   772     tempStream.WriteInt32L( err );
       
   773     if ( !err )
       
   774         {
       
   775         tempStream.WriteUint8L( fieldChange );
       
   776         tempStream.WriteInt32L( parent );
       
   777         tempStream.WriteUint16L( mimeType.Length() );
       
   778         tempStream.WriteL( mimeType );
       
   779         tempStream.WriteUint16L( mimeVer.Length() );
       
   780         tempStream.WriteL( mimeVer );
       
   781         tempStream.WriteInt32L( dataBuffer->Size() );
       
   782         tempStream.WriteL( dataBuffer->Ptr(0), dataBuffer->Size() );
       
   783         }
       
   784     tempStream.CommitL();
       
   785     CleanupStack::PopAndDestroy( &tempStream );
       
   786     CleanupStack::PopAndDestroy( dataBuffer );
       
   787     TRACE_FUNC_EXIT;
       
   788     }
       
   789 
       
   790 void CSconSyncHandler::CreateItemsL( RReadStream& aData )
       
   791     {
       
   792     TRACE_FUNC_ENTRY;
       
   793     delete iResponseData;
       
   794     iResponseData = NULL;
       
   795     iResponseData = CBufFlat::NewL(KDefaultExpandSize);
       
   796     RBufWriteStream responseStream( *iResponseData );
       
   797     CleanupClosePushL( responseStream );
       
   798     // Read item uids from parameter
       
   799     TInt itemCount = aData.ReadInt32L();
       
   800     LOGGER_WRITE_1("itemCount: %d", itemCount);
       
   801     responseStream.WriteInt32L( itemCount );
       
   802     for ( TInt i=0; i<itemCount; i++ )
       
   803         {
       
   804         LOGGER_WRITE("read from aData stream");
       
   805         TInt tempUid = aData.ReadInt32L();
       
   806         
       
   807         TSmlDbItemUid parent = aData.ReadInt32L();
       
   808         
       
   809         TInt len = aData.ReadUint16L();
       
   810         HBufC8* mimeTypeBuf = HBufC8::NewLC( len );
       
   811         TPtr8 mimeTypePtr = mimeTypeBuf->Des();
       
   812         aData.ReadL(mimeTypePtr, len);
       
   813         
       
   814         len = aData.ReadUint16L();
       
   815         HBufC8* mimeVerBuf = HBufC8::NewLC( len );
       
   816         TPtr8 mimeVerPtr = mimeVerBuf->Des();
       
   817         aData.ReadL(mimeVerPtr, len);
       
   818         
       
   819         len = aData.ReadInt32L();
       
   820         HBufC8* dataBuf = HBufC8::NewLC( len );
       
   821         TPtr8 dataPtr = dataBuf->Des();
       
   822         aData.ReadL( dataPtr, len );
       
   823         
       
   824         TSmlDbItemUid newUid(-1);
       
   825         LOGGER_WRITE("read from aData stream -readed ok");
       
   826         TRAPD( err, iSyncSession.CreateItemL(newUid,
       
   827                 parent, mimeTypePtr,
       
   828                 mimeVerPtr, dataPtr));
       
   829         
       
   830         CleanupStack::PopAndDestroy( dataBuf );
       
   831         CleanupStack::PopAndDestroy( mimeVerBuf );
       
   832         CleanupStack::PopAndDestroy( mimeTypeBuf );
       
   833         LOGGER_WRITE("Write to responseStream");
       
   834         responseStream.WriteInt32L( tempUid );
       
   835         responseStream.WriteInt32L( err );
       
   836         if ( !err )
       
   837             {
       
   838             responseStream.WriteInt32L( newUid );
       
   839             }
       
   840         LOGGER_WRITE("Write to responseStream -writed ok");
       
   841         }
       
   842     
       
   843     responseStream.CommitL();
       
   844     CleanupStack::PopAndDestroy( &responseStream );
       
   845     TRACE_FUNC_EXIT;
       
   846     }
       
   847 
       
   848 void CSconSyncHandler::GetCreateItemsResponseL( const TDesC8& aParams, RWriteStream& aResult )
       
   849     {
       
   850     TRACE_FUNC_ENTRY;
       
   851     if ( aParams.Length() > 0 )
       
   852         {
       
   853         LOGGER_WRITE_1("params length: %d", aParams.Length());
       
   854         
       
   855         //LOGGER_WRITE8_1("aParams: %S", &aParams);
       
   856         if ( iSyncStatus == EReady )
       
   857             {
       
   858             LOGGER_WRITE("Start creating items from file");
       
   859             iCreatedItems.Reset();
       
   860             const TUint8* ptr = aParams.Ptr();
       
   861             RMemReadStream stream( ptr , aParams.Size() );
       
   862             CleanupClosePushL( stream );
       
   863             TInt filenameLength = stream.ReadInt32L();
       
   864             LOGGER_WRITE_1("filename length: %d", filenameLength );
       
   865             HBufC8* filenameBuf8 = HBufC8::NewLC( /*stream,*/ filenameLength ); // filename
       
   866             TPtr8 filenamePtr = filenameBuf8->Des();
       
   867             stream.ReadL( filenamePtr, filenameLength);
       
   868             LOGGER_WRITE("filename ok");
       
   869             TInt err = CnvUtfConverter::ConvertToUnicodeFromUtf8(iFileInProgress, filenamePtr );
       
   870             LOGGER_WRITE_1("ConvertToUnicodeFromUtf8 err: %d", err);
       
   871             User::LeaveIfError( err );
       
   872             LOGGER_WRITE_1("iFileInProgress: %S", &iFileInProgress);
       
   873             
       
   874             if ( iFileInProgress.RightTPtr(KDataFileExtension().Length()).CompareF(KDataFileExtension) != 0 )
       
   875                 {
       
   876                 iFileInProgress = KNullDesC();
       
   877                 LOGGER_WRITE("File extendion was not correct");
       
   878                 User::Leave( KErrAccessDenied );
       
   879                 }
       
   880             
       
   881             CleanupStack::PopAndDestroy( filenameBuf8 );
       
   882 			
       
   883             LOGGER_WRITE("open file");
       
   884             err = iFileStream.Open(iFs,iFileInProgress, EFileShareReadersOnly);
       
   885             LOGGER_WRITE_1("iFileStream.Open err: %d", err);
       
   886             User::LeaveIfError( err );
       
   887             
       
   888             CleanupStack::PopAndDestroy( &stream );
       
   889             
       
   890             iSyncStatus = ECreatingItemsFromFile;
       
   891             iItemsLeftInStream = iFileStream.ReadInt32L();
       
   892             LOGGER_WRITE_1("iItemsLeftInStream: %d", iItemsLeftInStream);
       
   893             aResult.WriteInt32L( iItemsLeftInStream );
       
   894             aResult.WriteInt32L( 0 ); // completed items since last sync
       
   895             CreateNextItemOnStreamL();
       
   896             iItemsLeftInStream--;
       
   897             }
       
   898         else if ( iSyncStatus == ECreatingItemsFromFile || iSyncStatus == EItemsCreated )
       
   899             {
       
   900             LOGGER_WRITE("Read status");
       
   901             TInt temp = 0;
       
   902             if ( iSyncStatus == ECreatingItemsFromFile )
       
   903                 {
       
   904                 // one item is on progress, add it to to "left" items.
       
   905                 temp = 1;
       
   906                 }
       
   907             LOGGER_WRITE_1("left items: %d", iItemsLeftInStream + temp);
       
   908             LOGGER_WRITE_1("completed items: %d", iCreatedItems.Count());
       
   909             aResult.WriteInt32L( iItemsLeftInStream + temp ); // items in progress
       
   910             aResult.WriteInt32L( iCreatedItems.Count() ); // completed items since last get
       
   911             
       
   912             while ( iCreatedItems.Count() > 0 )
       
   913                 {
       
   914                 aResult.WriteInt32L( iCreatedItems[0].iTemporaryId );
       
   915                 aResult.WriteInt32L( iCreatedItems[0].iErrorCode );
       
   916                 if ( !iCreatedItems[0].iErrorCode )
       
   917                     {
       
   918                     aResult.WriteInt32L( iCreatedItems[0].iNewUid );
       
   919                     }
       
   920                 iCreatedItems.Remove(0);
       
   921                 }
       
   922             if ( iSyncStatus == EItemsCreated )
       
   923                 {
       
   924                 // all done
       
   925                 iSyncStatus = EReady;
       
   926                 LOGGER_WRITE("iSyncStatus = EReady");
       
   927                 }
       
   928             }
       
   929         else
       
   930             {
       
   931             LOGGER_WRITE_1("Wrong sync status: %d", iSyncStatus);
       
   932             User::Leave( KErrInUse );
       
   933             }
       
   934         }
       
   935     else
       
   936         {
       
   937         LOGGER_WRITE("Read response");
       
   938         if ( !iResponseData )
       
   939             {
       
   940             User::Leave( KErrNotReady );
       
   941             }
       
   942         aResult.WriteL( iResponseData->Ptr(0), iResponseData->Size() );
       
   943         delete iResponseData;
       
   944         iResponseData = NULL;
       
   945         }
       
   946     aResult.CommitL();
       
   947     TRACE_FUNC_EXIT;
       
   948     }
       
   949 
       
   950 void CSconSyncHandler::ReplaceItemsL( RReadStream& aData )
       
   951     {
       
   952     TRACE_FUNC_ENTRY;
       
   953     delete iResponseData;
       
   954     iResponseData = NULL;
       
   955     iResponseData = CBufFlat::NewL(KDefaultExpandSize);
       
   956     RBufWriteStream responseStream( *iResponseData );
       
   957     CleanupClosePushL( responseStream );
       
   958     // Read item uids from parameter
       
   959     TInt itemCount = aData.ReadInt32L();
       
   960     
       
   961     responseStream.WriteInt32L( itemCount );
       
   962     for ( TInt i=0; i<itemCount; i++ )
       
   963         {
       
   964         TSmlDbItemUid uid = aData.ReadInt32L();
       
   965         TSmlDbItemUid parent = aData.ReadInt32L();
       
   966         TBool fieldChange = aData.ReadUint8L();
       
   967         TInt len = aData.ReadInt32L();
       
   968         HBufC8* dataBuf = HBufC8::NewLC( len );
       
   969         TPtr8 dataPtr = dataBuf->Des();
       
   970         aData.ReadL( dataPtr, len );
       
   971         
       
   972         TRAPD( err, iSyncSession.ReplaceItemL(uid, 
       
   973                 parent, 
       
   974                 fieldChange, dataPtr));
       
   975         CleanupStack::PopAndDestroy( dataBuf );
       
   976         
       
   977         responseStream.WriteInt32L( uid );
       
   978         responseStream.WriteInt32L( err );
       
   979         }
       
   980     responseStream.CommitL();
       
   981     CleanupStack::PopAndDestroy( &responseStream );
       
   982     TRACE_FUNC_EXIT;
       
   983     }
       
   984 
       
   985 void CSconSyncHandler::GetReplaceItemsResponseL( const TDesC8& aParams, RWriteStream& aResult )
       
   986     {
       
   987     TRACE_FUNC_ENTRY;
       
   988     if ( aParams.Length() > 0 )
       
   989         {
       
   990         LOGGER_WRITE8_1("aParams: %S", &aParams);
       
   991         if ( iSyncStatus == EReady )
       
   992             {
       
   993             LOGGER_WRITE("Start replacing items from file");
       
   994             iReplacedItems.Reset();
       
   995             
       
   996             // create parameter stream reader
       
   997             const TUint8* ptr = aParams.Ptr();
       
   998             RMemReadStream stream( ptr , aParams.Size() );
       
   999             CleanupClosePushL( stream );
       
  1000             
       
  1001             // read filename
       
  1002             TInt filenameLength = stream.ReadInt32L();
       
  1003             LOGGER_WRITE_1("filename length: %d", filenameLength );
       
  1004             HBufC8* filenameBuf8 = HBufC8::NewLC( filenameLength ); // filename
       
  1005             TPtr8 filenamePtr = filenameBuf8->Des();
       
  1006             stream.ReadL( filenamePtr, filenameLength);
       
  1007             LOGGER_WRITE("filename ok");
       
  1008             
       
  1009             TInt err = CnvUtfConverter::ConvertToUnicodeFromUtf8(iFileInProgress, filenameBuf8->Des() );
       
  1010             LOGGER_WRITE_1("ConvertToUnicodeFromUtf8 err: %d", err);
       
  1011             User::LeaveIfError( err );
       
  1012             if ( iFileInProgress.RightTPtr(KDataFileExtension().Length()).CompareF(KDataFileExtension) != 0 )
       
  1013                 {
       
  1014                 iFileInProgress = KNullDesC();
       
  1015                 LOGGER_WRITE("File extendion was not correct");
       
  1016                 User::Leave( KErrAccessDenied );
       
  1017                 }
       
  1018             
       
  1019             // open file
       
  1020             err = iFileStream.Open(iFs, iFileInProgress, EFileShareReadersOnly);
       
  1021             LOGGER_WRITE_1("iFileStream.Open err: %d", err);
       
  1022             User::LeaveIfError( err );
       
  1023             
       
  1024             CleanupStack::PopAndDestroy( filenameBuf8 );
       
  1025             CleanupStack::PopAndDestroy( &stream );
       
  1026             
       
  1027             iSyncStatus = EReplacingItemsFromFile;
       
  1028             iItemsLeftInStream = iFileStream.ReadInt32L();
       
  1029             LOGGER_WRITE_1("iItemsLeftInStream: %d", iItemsLeftInStream);
       
  1030             // write results to return stream
       
  1031             aResult.WriteInt32L( iItemsLeftInStream );
       
  1032             aResult.WriteInt32L( 0 ); // completed items since last sync
       
  1033             ReplaceNextItemOnStreamL();
       
  1034             iItemsLeftInStream--;
       
  1035             }
       
  1036         else if ( iSyncStatus == EReplacingItemsFromFile || iSyncStatus == EItemsReplaced )
       
  1037             {
       
  1038             LOGGER_WRITE("Read status");
       
  1039             TInt temp = 0;
       
  1040             if ( iSyncStatus == EReplacingItemsFromFile )
       
  1041                 {
       
  1042                 // one item is on progress, add it to to "left" items.
       
  1043                 temp = 1;
       
  1044                 }
       
  1045             LOGGER_WRITE_1("left items: %d", iItemsLeftInStream + temp);
       
  1046             LOGGER_WRITE_1("completed items: %d", iReplacedItems.Count());
       
  1047             aResult.WriteInt32L( iItemsLeftInStream + temp ); // items in progress
       
  1048             aResult.WriteInt32L( iReplacedItems.Count() ); // completed items since last get
       
  1049             //for (TInt i=0; i<iCreatedItems.Count(); i++)
       
  1050             while ( iReplacedItems.Count() > 0 )
       
  1051                 {
       
  1052                 aResult.WriteInt32L( iReplacedItems[0].iItemUid );
       
  1053                 aResult.WriteInt32L( iReplacedItems[0].iErrorCode );
       
  1054                 iReplacedItems.Remove(0);
       
  1055                 }
       
  1056             if ( iSyncStatus == EItemsReplaced )
       
  1057                 {
       
  1058                 // all done
       
  1059                 iSyncStatus = EReady;
       
  1060                 LOGGER_WRITE("iSyncStatus = EReady");
       
  1061                 }
       
  1062             }
       
  1063         }
       
  1064     else
       
  1065         {
       
  1066         if ( !iResponseData )
       
  1067             {
       
  1068             LOGGER_WRITE("No response data, leave KErrNotReady");
       
  1069             User::Leave( KErrNotReady );
       
  1070             }
       
  1071         aResult.WriteL( iResponseData->Ptr(0), iResponseData->Size() );
       
  1072         aResult.CommitL();
       
  1073         delete iResponseData;
       
  1074         iResponseData = NULL;
       
  1075         }
       
  1076     aResult.CommitL();
       
  1077     
       
  1078     
       
  1079     
       
  1080     TRACE_FUNC_EXIT;
       
  1081     }
       
  1082 
       
  1083 void CSconSyncHandler::MoveItemsL( const TDesC8& aParams, RWriteStream& aResult )
       
  1084     {
       
  1085     TRACE_FUNC_ENTRY;  
       
  1086     const TUint8* ptr = aParams.Ptr();
       
  1087     RMemReadStream stream( ptr , aParams.Size() );
       
  1088     CleanupClosePushL( stream );
       
  1089     
       
  1090     // Read item uids from parameter
       
  1091     TInt itemCount = stream.ReadInt32L();
       
  1092     aResult.WriteInt32L( itemCount );
       
  1093     for ( TInt i=0; i<itemCount; i++ )
       
  1094         {
       
  1095         TSmlDbItemUid uid = stream.ReadInt32L();
       
  1096         TSmlDbItemUid newParent = stream.ReadInt32L();
       
  1097         TRAPD( err, iSyncSession.MoveItemL( uid,newParent ));
       
  1098         
       
  1099         aResult.WriteInt32L( uid );
       
  1100         aResult.WriteInt32L( err );
       
  1101         }
       
  1102     aResult.CommitL();
       
  1103     CleanupStack::PopAndDestroy( &stream );
       
  1104     TRACE_FUNC_EXIT;
       
  1105     }
       
  1106 
       
  1107 void CSconSyncHandler::DeleteItemsL( const TDesC8& aParams, RWriteStream& aResult )
       
  1108     {
       
  1109     TRACE_FUNC_ENTRY;
       
  1110     const TUint8* ptr = aParams.Ptr();
       
  1111     RMemReadStream stream( ptr , aParams.Size() );
       
  1112     CleanupClosePushL( stream );
       
  1113     
       
  1114     // Read item uids from parameter
       
  1115     TInt itemCount = stream.ReadInt32L();
       
  1116     aResult.WriteInt32L( itemCount );
       
  1117     for ( TInt i=0; i<itemCount; i++ )
       
  1118         {
       
  1119         TSmlDbItemUid uid = stream.ReadInt32L();
       
  1120         
       
  1121         TRAPD( err, iSyncSession.DeleteItemL( uid ));
       
  1122         
       
  1123         aResult.WriteInt32L( uid );
       
  1124         aResult.WriteInt32L( err );
       
  1125         }
       
  1126     aResult.CommitL();
       
  1127     CleanupStack::PopAndDestroy( &stream );
       
  1128     TRACE_FUNC_EXIT;
       
  1129     }
       
  1130 
       
  1131 void CSconSyncHandler::SoftDeleteItemsL( const TDesC8& aParams, RWriteStream& aResult )
       
  1132     {
       
  1133     TRACE_FUNC_ENTRY;
       
  1134     const TUint8* ptr = aParams.Ptr();
       
  1135     RMemReadStream stream( ptr , aParams.Size() );
       
  1136     CleanupClosePushL( stream );
       
  1137     
       
  1138     // Read item uids from parameter
       
  1139     TInt itemCount = stream.ReadInt32L();
       
  1140     aResult.WriteInt32L( itemCount );
       
  1141     for ( TInt i=0; i<itemCount; i++ )
       
  1142         {
       
  1143         TSmlDbItemUid uid = stream.ReadInt32L();
       
  1144         
       
  1145         TRAPD( err, iSyncSession.SoftDeleteItemL( uid ));
       
  1146         
       
  1147         aResult.WriteInt32L( uid );
       
  1148         aResult.WriteInt32L( err );
       
  1149         }
       
  1150     aResult.CommitL();
       
  1151     CleanupStack::PopAndDestroy( &stream );
       
  1152     TRACE_FUNC_EXIT;
       
  1153     }
       
  1154 
       
  1155 void CSconSyncHandler::DeleteAllItemsL( RWriteStream& aResult )
       
  1156     {
       
  1157     TRACE_FUNC_ENTRY;
       
  1158     if ( IsActive() )
       
  1159         {
       
  1160         LOGGER_WRITE("Warning: Was on active state!");
       
  1161         Cancel();
       
  1162         }
       
  1163     SetActive();
       
  1164     iStatus = KRequestPending;
       
  1165     iSyncStatus = EDeletingAllItems;
       
  1166     iSyncSession.DeleteAllItems( iStatus );
       
  1167     aResult.WriteInt32L( KErrNone );
       
  1168     TRACE_FUNC_EXIT;
       
  1169     }
       
  1170 
       
  1171 void CSconSyncHandler::GetDeleteAllItemsStatusL( RWriteStream& aResult )
       
  1172     {
       
  1173     if ( iSyncStatus == EDeletingAllItems )
       
  1174         {
       
  1175         LOGGER_WRITE("CSconSyncHandler::GetDeleteAllItemsStatusL - In progress");
       
  1176         aResult.WriteInt8L( 1 ); // 1 = in progress
       
  1177         }
       
  1178     else
       
  1179         {
       
  1180         LOGGER_WRITE("CSconSyncHandler::GetDeleteAllItemsStatusL - Ready");
       
  1181         aResult.WriteInt8L( 0 ); // 0 = ready
       
  1182         aResult.WriteInt32L( iStatus.Int() );
       
  1183         }
       
  1184     }
       
  1185 
       
  1186 void CSconSyncHandler::GetStoreFormatL( RWriteStream& aResult )
       
  1187     {
       
  1188     TRACE_FUNC_ENTRY;
       
  1189     RStringPool pool;
       
  1190     pool.OpenL();
       
  1191     CleanupClosePushL( pool );
       
  1192     CSmlDataStoreFormat* storeFormat(NULL);
       
  1193     TRAPD( err, storeFormat = iSyncSession.StoreFormatL( pool ) );
       
  1194     CleanupStack::PushL( storeFormat );
       
  1195     aResult.WriteInt32L( err );
       
  1196     if ( !err )
       
  1197         {
       
  1198         storeFormat->ExternalizeL( aResult );
       
  1199         }
       
  1200     CleanupStack::PopAndDestroy( storeFormat );
       
  1201     CleanupStack::PopAndDestroy( &pool );
       
  1202     TRACE_FUNC_EXIT;
       
  1203     }
       
  1204 
       
  1205 
       
  1206 void CSconSyncHandler::SetRemoteStoreFormatL( RReadStream& aData )
       
  1207     {
       
  1208     TRACE_FUNC_ENTRY;
       
  1209     RStringPool pool;
       
  1210     pool.OpenL();
       
  1211     CleanupClosePushL( pool );
       
  1212     CSmlDataStoreFormat* serverDataStoreFormat = CSmlDataStoreFormat::NewLC( pool, aData );
       
  1213     iSyncSession.SetRemoteStoreFormatL( *serverDataStoreFormat );
       
  1214     // Save format for later use
       
  1215     TRAPD(err, SaveRemoteStoreFormatL( *serverDataStoreFormat, iCurrentContextUid, iCurrentDataProviderUid ));
       
  1216     if ( err )
       
  1217         {
       
  1218         LOGGER_WRITE_1("SaveRemoteStoreFormatL err: %d", err);
       
  1219         }
       
  1220     CleanupStack::PopAndDestroy( serverDataStoreFormat );
       
  1221     CleanupStack::PopAndDestroy( &pool );
       
  1222     TRACE_FUNC_EXIT;
       
  1223     }
       
  1224 
       
  1225 void CSconSyncHandler::CreateNextItemOnStreamL()
       
  1226     {
       
  1227     TRACE_FUNC_ENTRY;
       
  1228     if ( IsActive() )
       
  1229         {
       
  1230         LOGGER_WRITE("Warning: Was on active state!");
       
  1231         Cancel();
       
  1232         }
       
  1233     iTemporaryItemUid = iFileStream.ReadInt32L();
       
  1234     TSmlDbItemUid parent = iFileStream.ReadInt32L();
       
  1235     
       
  1236     TInt len = iFileStream.ReadUint16L();
       
  1237     HBufC8* mimeTypeBuf = HBufC8::NewLC( len );
       
  1238     TPtr8 mimeTypePtr = mimeTypeBuf->Des();
       
  1239     iFileStream.ReadL(mimeTypePtr, len);
       
  1240     
       
  1241     len = iFileStream.ReadUint16L();
       
  1242     HBufC8* mimeVerBuf = HBufC8::NewLC( len );
       
  1243     TPtr8 mimeVerPtr = mimeVerBuf->Des();
       
  1244     iFileStream.ReadL(mimeVerPtr, len);
       
  1245     
       
  1246     len = iFileStream.ReadInt32L();
       
  1247     HBufC8* dataBuf = HBufC8::NewLC( len );
       
  1248     TPtr8 dataPtr = dataBuf->Des();
       
  1249     iFileStream.ReadL( dataPtr, len );    
       
  1250     
       
  1251     SetActive();
       
  1252     iStatus = KRequestPending;
       
  1253     iSyncSession.CreateItemL(iCreatedItemUidPckg,
       
  1254             parent, mimeTypePtr,
       
  1255             mimeVerPtr, dataPtr, iStatus);
       
  1256     
       
  1257     CleanupStack::PopAndDestroy( dataBuf );
       
  1258     CleanupStack::PopAndDestroy( mimeVerBuf );
       
  1259     CleanupStack::PopAndDestroy( mimeTypeBuf );
       
  1260     
       
  1261    
       
  1262     TRACE_FUNC_EXIT;
       
  1263     }
       
  1264 
       
  1265 void CSconSyncHandler::ReplaceNextItemOnStreamL()
       
  1266     {
       
  1267     TRACE_FUNC_ENTRY;
       
  1268     if ( IsActive() )
       
  1269         {
       
  1270         LOGGER_WRITE("Warning: Was on active state!");
       
  1271         Cancel();
       
  1272         }
       
  1273     TSmlDbItemUid uid = iFileStream.ReadInt32L();
       
  1274     TSmlDbItemUid parent = iFileStream.ReadInt32L();
       
  1275     TBool fieldChange = iFileStream.ReadUint8L();
       
  1276     TInt len = iFileStream.ReadInt32L();
       
  1277     HBufC8* dataBuf = HBufC8::NewLC( len );
       
  1278     TPtr8 dataPtr = dataBuf->Des();
       
  1279     iFileStream.ReadL( dataPtr, len );
       
  1280     
       
  1281     iReplacedItem.iItemUid = uid;
       
  1282     
       
  1283     SetActive();
       
  1284     iStatus = KRequestPending;
       
  1285     
       
  1286     iSyncSession.ReplaceItemL(uid, 
       
  1287                 parent, 
       
  1288                 fieldChange, dataPtr, iStatus);
       
  1289     
       
  1290     CleanupStack::PopAndDestroy( dataBuf );
       
  1291    
       
  1292     TRACE_FUNC_EXIT;
       
  1293     }
       
  1294 
       
  1295 void CSconSyncHandler::CancelOperationsL( RWriteStream& aResult )
       
  1296     {
       
  1297     TRACE_FUNC_ENTRY;
       
  1298     Cancel();
       
  1299     // all done
       
  1300     iFileStream.Close();
       
  1301     iTemporaryItemUid = -1;
       
  1302     iCreatedItemUid = -1;
       
  1303     iSyncStatus = EItemsCreated;
       
  1304     iFs.Delete( iFileInProgress );
       
  1305     iFileInProgress = KNullDesC();
       
  1306     iItemsLeftInStream = 0;
       
  1307     _LIT8(KDummyParam, "Cancel");
       
  1308     if ( iSyncStatus == EItemsCreated )
       
  1309         {
       
  1310         GetCreateItemsResponseL( KDummyParam, aResult );
       
  1311         }
       
  1312     else if ( iSyncStatus == EItemsReplaced )
       
  1313         {
       
  1314         GetReplaceItemsResponseL( KDummyParam, aResult );
       
  1315         }
       
  1316     
       
  1317     TRACE_FUNC_EXIT;
       
  1318     }
       
  1319 
       
  1320 void CSconSyncHandler::DoCancel()
       
  1321     {
       
  1322     TRACE_FUNC_ENTRY;
       
  1323     iSyncSession.CancelRequest();
       
  1324     Reset();
       
  1325     TRACE_FUNC_EXIT;
       
  1326     }
       
  1327 
       
  1328 void CSconSyncHandler::RunL()
       
  1329     {
       
  1330     TRACE_FUNC_ENTRY;
       
  1331     LOGGER_WRITE_1("iSyncStatus: %d", iSyncStatus);
       
  1332     LOGGER_WRITE_1("iStatus: %d", iStatus.Int());
       
  1333     if ( iSyncStatus == ECreatingItemsFromFile )
       
  1334         {
       
  1335         LOGGER_WRITE_1("iTemporaryItemUid: %d", iTemporaryItemUid);
       
  1336         LOGGER_WRITE_1("iCreatedItemUid: %d", iCreatedItemUid);
       
  1337         TCreatedItem item;
       
  1338         item.iTemporaryId = iTemporaryItemUid;
       
  1339         item.iErrorCode = iStatus.Int();
       
  1340         item.iNewUid = iCreatedItemUid;
       
  1341         iCreatedItems.Append( item );
       
  1342         if ( iItemsLeftInStream > 0 )
       
  1343             {
       
  1344             CreateNextItemOnStreamL();
       
  1345             iItemsLeftInStream--;
       
  1346             LOGGER_WRITE_1("iItemsLeftInStream: %d", iItemsLeftInStream);
       
  1347             }
       
  1348         else
       
  1349             {
       
  1350             // all done
       
  1351             LOGGER_WRITE("All items added.");
       
  1352             iFileStream.Close();
       
  1353             iTemporaryItemUid = -1;
       
  1354             iCreatedItemUid = -1;
       
  1355             iSyncStatus = EItemsCreated;
       
  1356             TInt err = iFs.Delete( iFileInProgress );
       
  1357             LOGGER_WRITE_2("iFs.Delete( '%S' ) ret: %d", &iFileInProgress, err);
       
  1358             iFileInProgress = KNullDesC();
       
  1359             }
       
  1360         }
       
  1361     else if ( iSyncStatus == EReplacingItemsFromFile )
       
  1362         {
       
  1363         iReplacedItem.iErrorCode = iStatus.Int();
       
  1364         iReplacedItems.Append( iReplacedItem );
       
  1365         if ( iItemsLeftInStream > 0 )
       
  1366             {
       
  1367             ReplaceNextItemOnStreamL();
       
  1368             iItemsLeftInStream--;
       
  1369             LOGGER_WRITE_1("iItemsLeftInStream: %d", iItemsLeftInStream);
       
  1370             }
       
  1371         else
       
  1372             {
       
  1373             // all done
       
  1374             LOGGER_WRITE("All items replaced.");
       
  1375             iFileStream.Close();
       
  1376             iSyncStatus = EItemsReplaced;
       
  1377             TInt err = iFs.Delete( iFileInProgress );
       
  1378             LOGGER_WRITE_2("iFs.Delete( '%S' ) ret: %d", &iFileInProgress, err);
       
  1379             iFileInProgress = KNullDesC();
       
  1380             }
       
  1381         }
       
  1382     else if ( iSyncStatus == EDeletingAllItems )
       
  1383         {
       
  1384         iSyncStatus = EReady;
       
  1385         }
       
  1386     TRACE_FUNC_EXIT;
       
  1387     }
       
  1388 
       
  1389 TInt CSconSyncHandler::RunError( TInt aError )
       
  1390     {
       
  1391     TRACE_FUNC_ENTRY;
       
  1392     LOGGER_WRITE_1("aError: %d", aError);
       
  1393     LOGGER_WRITE_1("iSyncStatus: %d", iSyncStatus);
       
  1394     TInt err( aError );
       
  1395     if ( iSyncStatus == ECreatingItemsFromFile )
       
  1396         {
       
  1397         iSyncStatus = EItemsCreated;
       
  1398         iItemsLeftInStream = 0;
       
  1399         iFileStream.Close();
       
  1400         err = iFs.Delete( iFileInProgress );
       
  1401         LOGGER_WRITE_2("iFs.Delete( '%S' ) ret: %d", &iFileInProgress, err);
       
  1402         iFileInProgress = KNullDesC();
       
  1403         err = KErrNone;
       
  1404         }
       
  1405     else if ( iSyncStatus == EReplacingItemsFromFile )
       
  1406         {
       
  1407         iSyncStatus = EItemsReplaced;
       
  1408         iItemsLeftInStream = 0;
       
  1409         iFileStream.Close();
       
  1410         err = iFs.Delete( iFileInProgress );
       
  1411         LOGGER_WRITE_2("iFs.Delete( '%S' ) ret: %d", &iFileInProgress, err);
       
  1412         iFileInProgress = KNullDesC();
       
  1413         err = KErrNone;
       
  1414         }
       
  1415     else if ( iSyncStatus == EDeletingAllItems )
       
  1416         {
       
  1417         iSyncStatus = EReady;
       
  1418         }
       
  1419     
       
  1420     TRACE_FUNC_EXIT;
       
  1421     return err;
       
  1422     }
       
  1423 
       
  1424 void CSconSyncHandler::Reset()
       
  1425     {
       
  1426     TRACE_FUNC_ENTRY;
       
  1427     iFileStream.Close();
       
  1428     TInt err = iFs.Delete( iFileInProgress );
       
  1429     LOGGER_WRITE_2("iFs.Delete( '%S' ) ret: %d", &iFileInProgress, err);
       
  1430     iFileInProgress = KNullDesC();
       
  1431     iSyncStatus = EReady;
       
  1432     TRACE_FUNC_EXIT;
       
  1433     }
       
  1434 
       
  1435 void CSconSyncHandler::SetCalendarCenrepL( TInt aProfileId, const TDesC& aServerId )
       
  1436     {
       
  1437     LOGGER_WRITE(" Write cenrep values");
       
  1438     CRepository* rep = CRepository::NewLC(KNsmlDsSessionInfoKey);
       
  1439     User::LeaveIfError( rep->Set(EDSSessionProfileId, aProfileId) );
       
  1440     User::LeaveIfError( rep->Set(EDSSessionProfileName, KNullDesC) );
       
  1441     User::LeaveIfError( rep->Set(EDSSessionServerId, aServerId) );
       
  1442     CleanupStack::PopAndDestroy(rep);
       
  1443     iCalendarCenrepUsed = ETrue;
       
  1444     }
       
  1445 
       
  1446 void CSconSyncHandler::ClearCalendarCenrepL()
       
  1447     {
       
  1448     if ( iCalendarCenrepUsed )
       
  1449         {
       
  1450         LOGGER_WRITE("Calendar sync, clear cenrep values");
       
  1451                 
       
  1452         CRepository* rep = CRepository::NewLC(KNsmlDsSessionInfoKey);
       
  1453         rep->Set(EDSSessionProfileId, KErrNotFound);
       
  1454         rep->Set(EDSSessionProfileName, KNullDesC);
       
  1455         rep->Set(EDSSessionServerId, KNullDesC);
       
  1456         CleanupStack::PopAndDestroy(rep);
       
  1457         iCalendarCenrepUsed = EFalse;
       
  1458         }
       
  1459     }
       
  1460