syncmlfw/common/sosserver/src/nsmlsoshandler.cpp
changeset 0 b497e44ab2fc
child 60 eb6690d0d439
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Symbian OS Server source.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <s32mem.h>
       
    19 #include <ecom.h>
       
    20 #include <implementationinformation.h>
       
    21 //#include <syncmlnotifier.h>
       
    22 #include <e32property.h>
       
    23 #include <DevManInternalCRKeys.h>
       
    24 #include <centralrepository.h>
       
    25 #include <nsmlconstants.h>
       
    26 #include <nsmldebug.h>
       
    27 #include <nsmldsconstants.h>
       
    28 
       
    29 #include "nsmlsosserver.h"
       
    30 #include "nsmltransport.h"
       
    31 #include "nsmlsosthread.h"
       
    32 #include "nsmlprivatepskeys.h"
       
    33 
       
    34 // --------------------------------------------------------------------------
       
    35 // CNSmlSOSHandler* CNSmlSOSHandler::NewL()
       
    36 // --------------------------------------------------------------------------
       
    37 //
       
    38 CNSmlSOSHandler* CNSmlSOSHandler::NewL()
       
    39     {
       
    40 	CNSmlSOSHandler* self= new (ELeave) CNSmlSOSHandler();
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	CleanupStack::Pop(); // self
       
    44 	return self;    
       
    45     }
       
    46 
       
    47 // --------------------------------------------------------------------------
       
    48 // void CNSmlSOSHandler::ConstructL()
       
    49 // --------------------------------------------------------------------------
       
    50 //
       
    51 void CNSmlSOSHandler::ConstructL()
       
    52     {
       
    53     iMsgQueue = CNSmlMessageQueue::NewL(this);
       
    54     iContactSuiteObserverProfileId = 0;
       
    55     }
       
    56 
       
    57 // --------------------------------------------------------------------------
       
    58 // CNSmlSOSHandler::CNSmlSOSHandler()
       
    59 // --------------------------------------------------------------------------
       
    60 //
       
    61 CNSmlSOSHandler::CNSmlSOSHandler() : CActive(EPriorityStandard), iSessionCount(0)
       
    62     {     
       
    63     CActiveScheduler::Add(this);
       
    64     }
       
    65 
       
    66 // --------------------------------------------------------------------------
       
    67 // CNSmlSOSHandler::~CNSmlSOSHandler()
       
    68 // --------------------------------------------------------------------------
       
    69 //
       
    70 CNSmlSOSHandler::~CNSmlSOSHandler()
       
    71     {
       
    72     Cancel();
       
    73     delete iMsgQueue;
       
    74     
       
    75     delete iParams;
       
    76     delete iThreadObserver;
       
    77   	
       
    78     iProfileLocks.Close();
       
    79     iSessionLib.Close();
       
    80     iEventMsgs.Close();
       
    81     iProgressMsgs.Close();
       
    82 	iContactSuiteProgressMsgs.Close();
       
    83     
       
    84     iWrtStream.Close();
       
    85     }
       
    86 
       
    87 // --------------------------------------------------------------------------
       
    88 // void CNSmlSOSHandler::LockProfile( const TInt aProfId )
       
    89 // Adds profile to the array of locked profiles.
       
    90 // --------------------------------------------------------------------------
       
    91 //
       
    92 void CNSmlSOSHandler::LockProfile( const TInt aProfId )
       
    93     {
       
    94     iProfileLocks.Append( aProfId );
       
    95     }
       
    96 
       
    97 // --------------------------------------------------------------------------
       
    98 // void CNSmlSOSHandler::RemoveLock( const TInt aProfId )
       
    99 // Removes lock from profile.
       
   100 // --------------------------------------------------------------------------
       
   101 //
       
   102 void CNSmlSOSHandler::RemoveLock( const TInt aProfId )
       
   103     {
       
   104     TInt index = iProfileLocks.Find( aProfId );
       
   105 
       
   106     if ( index != KErrNotFound )
       
   107         {
       
   108         iProfileLocks.Remove(index);
       
   109         }
       
   110     }
       
   111 
       
   112 // --------------------------------------------------------------------------
       
   113 // TBool CNSmlSOSHandler::IsLocked( const TInt aProfId )
       
   114 // Checks whether profile is locked or not.
       
   115 // --------------------------------------------------------------------------
       
   116 //
       
   117 TBool CNSmlSOSHandler::IsLocked( const TInt aProfId )
       
   118     {
       
   119     if ( iProfileLocks.Find( aProfId ) != KErrNotFound )
       
   120         {
       
   121         return ETrue;
       
   122         }
       
   123     return EFalse;
       
   124     }
       
   125 
       
   126 // --------------------------------------------------------------------------
       
   127 // TInt CNSmlSOSHandler::AddJobL( CNSmlJob* aJob, TInt& aJobId )
       
   128 // Adds a created job to the job queue.
       
   129 // --------------------------------------------------------------------------
       
   130 //
       
   131 TInt CNSmlSOSHandler::AddJobL( CNSmlJob* aJob, TInt& aJobId )
       
   132     {
       
   133     aJobId = CreateJobId();
       
   134     aJob->SetJobId( aJobId );
       
   135     if ( !iCurrentJob )
       
   136         {
       
   137         iCurrentJob = aJob;
       
   138         
       
   139         if ( iCurrentJob->UsageType() == ESmlDataSync )
       
   140             {
       
   141             StartDSSyncL();
       
   142             }
       
   143         else
       
   144             {
       
   145             StartDMSyncL();
       
   146             }
       
   147         }
       
   148     else
       
   149         {
       
   150         if ( SearchSimilarJobs( aJob ) )
       
   151             {
       
   152             delete aJob;
       
   153             aJob = NULL;
       
   154             return KErrAlreadyExists;
       
   155             }
       
   156         CNSmlJob* tempJob = iCurrentJob;
       
   157         
       
   158         while ( tempJob->iNext )
       
   159             {            
       
   160             tempJob = tempJob->iNext;
       
   161             }
       
   162 
       
   163         tempJob->iNext = aJob;    
       
   164         }
       
   165     return KErrNone;
       
   166     }
       
   167 
       
   168 // --------------------------------------------------------------------------
       
   169 // CNSmlJob* CNSmlSOSHandler::FindJob( TInt aJobId )
       
   170 // Finds job from queue.
       
   171 // --------------------------------------------------------------------------
       
   172 //
       
   173 CNSmlJob* CNSmlSOSHandler::FindJob( TInt aJobId )
       
   174     {
       
   175     if ( iCurrentJob )
       
   176         {
       
   177         CNSmlJob* job = iCurrentJob;
       
   178         if ( job->JobId() == aJobId )
       
   179             {
       
   180             return iCurrentJob; 
       
   181             }
       
   182         while ( job->iNext )
       
   183             {
       
   184             job = job->iNext;
       
   185             if ( job->JobId() == aJobId )
       
   186                 {
       
   187                 return job;
       
   188                 }
       
   189             }
       
   190         }
       
   191     return NULL;   
       
   192     }
       
   193 
       
   194 // --------------------------------------------------------------------------
       
   195 // TInt CNSmlSOSHandler::CreateJobId()
       
   196 // Creates an id to the new job.
       
   197 // --------------------------------------------------------------------------
       
   198 //
       
   199 TInt CNSmlSOSHandler::CreateJobId()
       
   200     {
       
   201     return iJobIndex++;
       
   202     }
       
   203 
       
   204 // --------------------------------------------------------------------------
       
   205 // TInt CNSmlSOSHandler::StopJob( const TInt aJobId, const TInt aCreatorId )
       
   206 // Removes job from queue or cancels the job if currently running.
       
   207 // --------------------------------------------------------------------------
       
   208 //
       
   209 TInt CNSmlSOSHandler::StopJob( const TInt aJobId, const TInt aCreatorId )
       
   210     {
       
   211     if ( !iCurrentJob )
       
   212         {
       
   213         return KErrNotFound;
       
   214         }
       
   215     if ( iCurrentJob->JobId() == aJobId )
       
   216         {
       
   217         if ( iCurrentJob->CreatorId() != aCreatorId )
       
   218             {
       
   219             return KErrPermissionDenied;
       
   220             }
       
   221         else
       
   222             {
       
   223             if ( iCurrentJob->UsageType() == ESmlDataSync )
       
   224                 {
       
   225                 CancelDSSync();
       
   226                 }
       
   227             else
       
   228                 {
       
   229                 CancelDMSync();
       
   230                 }           
       
   231             return KErrNone;
       
   232             }
       
   233         }
       
   234     else
       
   235         {
       
   236         CNSmlJob* tempJob = iCurrentJob;
       
   237         CNSmlJob* prevJob = iCurrentJob;
       
   238         while ( tempJob->iNext )
       
   239             {
       
   240             prevJob = tempJob;
       
   241             tempJob = tempJob->iNext;
       
   242             if ( tempJob->JobId() == aJobId )
       
   243                 {
       
   244  				if ( tempJob->CreatorId() != aCreatorId )
       
   245                     {
       
   246                     return KErrPermissionDenied;
       
   247                     }
       
   248                 else
       
   249                     {
       
   250                     CNSmlJob* nextJob = tempJob->iNext;
       
   251                     TRAP_IGNORE( CompleteEventMsgL( MSyncMLEventObserver::EJobStop, tempJob->JobId(), KErrNone ));  
       
   252                     delete tempJob;
       
   253                     prevJob->iNext = nextJob;
       
   254                     tempJob = nextJob;
       
   255                     return KErrNone;
       
   256                     }
       
   257                 }            
       
   258             }
       
   259         }
       
   260     return KErrNotFound;
       
   261     }
       
   262 
       
   263 // --------------------------------------------------------------------------
       
   264 // void CNSmlSOSHandler::CurrentJob( TInt& aId, TSmlUsageType& aType )
       
   265 // Return the id and type of a job currently running.
       
   266 // --------------------------------------------------------------------------
       
   267 //
       
   268 void CNSmlSOSHandler::CurrentJob( TInt& aId, TSmlUsageType& aType )
       
   269     {
       
   270     if ( iCurrentJob )
       
   271         {
       
   272         aId = iCurrentJob->JobId();
       
   273         aType = iCurrentJob->UsageType();       
       
   274         }
       
   275     else    // No job running.
       
   276         {
       
   277         aId = KNSmlNullId;
       
   278         }
       
   279     }
       
   280 
       
   281 // --------------------------------------------------------------------------
       
   282 // void CNSmlSOSHandler::QueuedJobsL( RArray<TInt>& aArray, const TSmlUsageType aType )
       
   283 // Returns an array of ids of queued jobs of seleted type.
       
   284 // --------------------------------------------------------------------------
       
   285 //
       
   286 void CNSmlSOSHandler::QueuedJobsL( RArray<TInt>& aArray, const TSmlUsageType aType )
       
   287     {
       
   288     // Start from current job. If the usage type matches, append id to the array.
       
   289     if ( iCurrentJob )
       
   290         {
       
   291         CNSmlJob* job = iCurrentJob;
       
   292 
       
   293         if ( job->UsageType() == aType )
       
   294             {
       
   295             aArray.AppendL( job->JobId() );    
       
   296             }
       
   297         while ( job->iNext )
       
   298             {
       
   299             job = job->iNext;
       
   300             if ( job->UsageType() == aType )
       
   301                 {
       
   302                 aArray.AppendL( job->JobId() );    
       
   303                 }
       
   304             }
       
   305         }
       
   306     }
       
   307 
       
   308 // --------------------------------------------------------------------------
       
   309 // void CNSmlSOSHandler::AddProgressMsgL( const RMessage2& aMessage )
       
   310 // Adds progress message to the message array.
       
   311 // --------------------------------------------------------------------------
       
   312 //
       
   313 void CNSmlSOSHandler::AddProgressMsgL( const RMessage2& aMessage )
       
   314     {
       
   315     iProgressMsgs.AppendL( aMessage );
       
   316     }
       
   317 
       
   318 // --------------------------------------------------------------------------
       
   319 // void CNSmlSOSHandler::AddProgressMsgL( const RMessage2& aMessage )
       
   320 // Adds progress message to the message array.
       
   321 // --------------------------------------------------------------------------
       
   322 //
       
   323 void CNSmlSOSHandler::AddContactSuiteProgressMsgL( const RMessage2& aMessage )
       
   324     {
       
   325         iContactSuiteProgressMsgs.AppendL( aMessage );
       
   326     }
       
   327 
       
   328 
       
   329 // --------------------------------------------------------------------------
       
   330 // TBool CNSmlSOSHandler::CompleteBufProgressMsg( const TDesC8& aBuf, 
       
   331 // const CNSmlSOSSession* aSession )
       
   332 // Write data to the progress message and completes it.
       
   333 // --------------------------------------------------------------------------
       
   334 //
       
   335 TBool CNSmlSOSHandler::CompleteBufProgressMsg( 
       
   336 const TDesC8& aBuf, 
       
   337 const CNSmlSOSSession* aSession  )
       
   338     {
       
   339     _DBG_FILE("CNSmlSOSHandler::CompleteBufProgressMsg : Begin");
       
   340     TBool request( EFalse );    
       
   341         
       
   342     // Session info is stored to job and compared to session of the message.
       
   343     for( TInt i(0); i < iProgressMsgs.Count(); i++ )
       
   344         {
       
   345         if ( aSession == iProgressMsgs[i].Session() )
       
   346             {     
       
   347             _DBG_FILE("Owner session of job found. Progress completed.");    
       
   348             request = ETrue;
       
   349                         
       
   350             iProgressMsgs[i].Write( 0, aBuf, 0 );
       
   351             iProgressMsgs[i].Complete( KErrNone );
       
   352             iProgressMsgs.Remove(i);        
       
   353             }
       
   354         }    
       
   355     _DBG_FILE("CNSmlSOSHandler::CompleteBufProgressMsg : End");    
       
   356     return request;     
       
   357     }
       
   358 
       
   359 // --------------------------------------------------------------------------
       
   360 // TBool CNSmlSOSHandler::CompleteBufProgressMsg( const TDesC8& aBuf, 
       
   361 // const CNSmlSOSSession* aSession )
       
   362 // Write data to the progress message and completes it.
       
   363 // --------------------------------------------------------------------------
       
   364 //
       
   365 TBool CNSmlSOSHandler::CompleteBufContactSuiteProgressMsg( 
       
   366 const TDesC8& aBuf, 
       
   367 const CNSmlSOSSession* aSession  )
       
   368     {
       
   369     _DBG_FILE("CNSmlSOSHandler::CompleteBufProgressMsg : Begin");
       
   370     TBool request( EFalse );    
       
   371         
       
   372 	    // Session info is stored to job and compared to session of the message.
       
   373     for( TInt i(0); i < iContactSuiteProgressMsgs.Count(); i++ )
       
   374         {
       
   375             request = ETrue;
       
   376 
       
   377 		    if(!iContactSuiteProgressMsgs[i].IsNull())
       
   378                 {
       
   379                 iContactSuiteProgressMsgs[i].Write( 0, aBuf, 0 );
       
   380                 iContactSuiteProgressMsgs[i].Complete( KErrNone );
       
   381                 }
       
   382             iContactSuiteProgressMsgs.Remove(i);
       
   383         }    
       
   384 
       
   385     _DBG_FILE("CNSmlSOSHandler::CompleteBufProgressMsg : End");    
       
   386     return request;     
       
   387     }
       
   388 
       
   389 // --------------------------------------------------------------------------
       
   390 // void CNSmlSOSHandler::CancelContactSuiteProgressMsg( const CNSmlSOSSession* aSession )
       
   391 // Completes cancelled progress message and removes it from array.
       
   392 // --------------------------------------------------------------------------
       
   393 //
       
   394 void CNSmlSOSHandler::CancelContactSuiteProgressMsg( const CNSmlSOSSession* aSession )
       
   395     {
       
   396     for ( TInt i(0) ; i < iContactSuiteProgressMsgs.Count() ; i++ )
       
   397         {
       
   398         //if ( iContactSuiteProgressMsgs[i].Session() == aSession )
       
   399             {
       
   400             iContactSuiteProgressMsgs[i].Complete( KErrCancel );
       
   401             iContactSuiteProgressMsgs.Remove(i);
       
   402             }
       
   403         }
       
   404     }
       
   405 
       
   406 // --------------------------------------------------------------------------
       
   407 // void CNSmlSOSHandler::CancelProgressMsg( const CNSmlSOSSession* aSession )
       
   408 // Completes cancelled progress message and removes it from array.
       
   409 // --------------------------------------------------------------------------
       
   410 //
       
   411 void CNSmlSOSHandler::CancelProgressMsg( const CNSmlSOSSession* aSession )
       
   412     {
       
   413     for ( TInt i(0) ; i < iProgressMsgs.Count() ; i++ )
       
   414         {
       
   415         if ( iProgressMsgs[i].Session() == aSession )
       
   416             {
       
   417             iProgressMsgs[i].Complete( KErrCancel );
       
   418             iProgressMsgs.Remove(i);
       
   419             }
       
   420         }
       
   421     }
       
   422 
       
   423 // --------------------------------------------------------------------------
       
   424 // void CNSmlSOSHandler::AddEventMsgL( const RMessage2& aMessagePtr )
       
   425 // Adds event message to the message array.
       
   426 // --------------------------------------------------------------------------
       
   427 //
       
   428 void CNSmlSOSHandler::AddEventMsgL( const RMessage2& aMessagePtr )
       
   429     {
       
   430     iEventMsgs.AppendL( aMessagePtr );
       
   431     }
       
   432 
       
   433 // --------------------------------------------------------------------------
       
   434 // void CNSmlSOSHandler::CompleteEventMsgL( MSyncMLEventObserver::TEvent aEvent, TInt aId, TInt aError, TInt aAdditional )
       
   435 // Writes data to all event messages and completes them.
       
   436 // --------------------------------------------------------------------------
       
   437 //
       
   438 void CNSmlSOSHandler::CompleteEventMsgL( MSyncMLEventObserver::TEvent aEvent, TInt aId, TInt aError, TInt aAdditional )
       
   439     {
       
   440     if ( iEventMsgs.Count() )
       
   441         {
       
   442         HBufC8* buf = HBufC8::NewLC(( (2*KSizeofTInt8) + (3*KSizeofTInt32) ));
       
   443         TPtr8 bufPtr = buf->Des();
       
   444 
       
   445         RDesWriteStream stream;
       
   446         stream.Open(bufPtr);
       
   447         CleanupClosePushL(stream);
       
   448 
       
   449         stream.WriteInt8L( (TInt8) ENSmlTypeEvent );
       
   450         stream.WriteInt8L( (TInt8) aEvent );
       
   451         stream.WriteInt32L( aId );
       
   452         stream.WriteInt32L( aError );
       
   453         stream.WriteInt32L( aAdditional );
       
   454         stream.CommitL();
       
   455 
       
   456         while ( iEventMsgs.Count() > 0 )
       
   457             {
       
   458             if(!iEventMsgs[0].IsNull()&& iEventMsgs[0].Handle())
       
   459                 {
       
   460                 iEventMsgs[0].Write( 0, bufPtr, 0 );
       
   461                 iEventMsgs[0].Complete( KErrNone );
       
   462                 }
       
   463             iEventMsgs.Remove(0);
       
   464             }
       
   465         CleanupStack::PopAndDestroy(&stream); 
       
   466         CleanupStack::PopAndDestroy(buf);
       
   467         }
       
   468     }
       
   469 
       
   470 // --------------------------------------------------------------------------
       
   471 // void CNSmlSOSHandler::CancelEventMsg( TInt aHandle )
       
   472 // Completes cancelled event message and removes it from array.
       
   473 // --------------------------------------------------------------------------
       
   474 //
       
   475 void CNSmlSOSHandler::CancelEventMsg( TInt aHandle )
       
   476     {
       
   477     for ( TInt i(0) ; i < iEventMsgs.Count() ; i++ )
       
   478         {
       
   479         if ( iEventMsgs[i].Handle() == aHandle )
       
   480             {
       
   481             iEventMsgs[i].Complete( KErrCancel );
       
   482             iEventMsgs.Remove(i);
       
   483             }
       
   484         }
       
   485     }
       
   486 
       
   487 // --------------------------------------------------------------------------
       
   488 // void CNSmlSOSHandler::ServerAlertL( TDesC8& aData, TSmlUsageType aType, TSmlProtocolVersion aVersion )
       
   489 // Add server alert message to message queue.
       
   490 // --------------------------------------------------------------------------
       
   491 //
       
   492 void CNSmlSOSHandler::ServerAlertL( TDesC8& aData, TSmlUsageType aType, TSmlProtocolVersion aVersion, TInt aBearerType )
       
   493     {
       
   494     iMsgQueue->AddMessageL( aData, aType, aVersion, aBearerType );
       
   495     }
       
   496 
       
   497 
       
   498 // FOTA
       
   499 
       
   500 // --------------------------------------------------------------------------
       
   501 // void CNSmlSOSHandler::GenericAlertL( HBufC8* aMgmtUri, HBufC8* aMetaType, HBufC8* aMetaFormat, TInt aFinalResult, HBufC8* aCorrelator )
       
   502 // Adds Generic Alert to DM Agent if it is currently running.
       
   503 // --------------------------------------------------------------------------
       
   504 //
       
   505 void CNSmlSOSHandler::GenericAlertL( HBufC8* aMgmtUri, HBufC8* aMetaType, HBufC8* aMetaFormat, TInt aFinalResult, HBufC8* aCorrelator )
       
   506 	{
       
   507 	if ( !iParams || !iParams->iThreadEngine )
       
   508 		{
       
   509 		User::Leave( KErrNotFound );
       
   510 		}
       
   511 	
       
   512 	iParams->iThreadEngine->DMAgentL()->SetGenericAlertL( *aMgmtUri, *aMetaType, *aMetaFormat, aFinalResult, *aCorrelator );
       
   513 	}
       
   514 
       
   515 // FOTA end
       
   516 
       
   517 // --------------------------------------------------------------------------
       
   518 // void CNSmlSOSHandler::GenericAlertL( HBufC8* aMgmtUri, HBufC8* aMetaType, HBufC8* aMetaFormat, HBufC8* aCorrelator, HBufC8* aTargetUri, HBufC8* aMark, HBufC8* aData )
       
   519 // Adds Generic Alert to DM Agent if it is currently running.
       
   520 // --------------------------------------------------------------------------
       
   521 //
       
   522 void CNSmlSOSHandler::GenericAlertL( HBufC8* aCorrelator, RArray<CNSmlDMAlertItem>* aItem )
       
   523 	{
       
   524 	if ( !iParams || !iParams->iThreadEngine )
       
   525 		{
       
   526 		User::Leave( KErrNotFound );
       
   527 		}
       
   528 	
       
   529 	iParams->iThreadEngine->DMAgentL()->SetGenericAlertL( *aCorrelator, *aItem );
       
   530 	}
       
   531 
       
   532 // --------------------------------------------------------------------------
       
   533 // void CNSmlSOSHandler::IncreaseSessionCount()
       
   534 // --------------------------------------------------------------------------
       
   535 //
       
   536 void CNSmlSOSHandler::IncreaseSessionCount()
       
   537     {
       
   538     iSessionCount++;
       
   539     }
       
   540 
       
   541 // --------------------------------------------------------------------------
       
   542 // void CNSmlSOSHandler::DecreaseSessionCount()
       
   543 // --------------------------------------------------------------------------
       
   544 //
       
   545 void CNSmlSOSHandler::DecreaseSessionCount()
       
   546     {
       
   547     iSessionCount--;
       
   548     if ( !iSessionCount && !iCurrentJob && iMsgQueue->IsEmpty() ) 
       
   549         {
       
   550         CActiveScheduler::Stop();
       
   551         }
       
   552     }
       
   553 
       
   554 // --------------------------------------------------------------------------
       
   555 // void CNSmlSOSHandler::ServerSuspendedL( const TBool aSuspended )
       
   556 // Sets suspended state. 
       
   557 // --------------------------------------------------------------------------
       
   558 //
       
   559 void CNSmlSOSHandler::ServerSuspendedL( const TBool aSuspended )
       
   560     {
       
   561     
       
   562     if ( iSuspended != aSuspended )
       
   563         {
       
   564         CompleteEventMsgL( MSyncMLEventObserver::EServerSuspended, 0, 0, aSuspended );         
       
   565         }
       
   566         
       
   567     iSuspended = aSuspended; 
       
   568 
       
   569     iMsgQueue->ServerSuspended( iSuspended ); // Send suspend state to message queue.
       
   570 
       
   571     if ( iSuspended )
       
   572         {
       
   573         // Cancel job if running.
       
   574         if ( iCurrentJob )
       
   575             {
       
   576             if ( iCurrentJob->UsageType() == ESmlDataSync )
       
   577                 {
       
   578                 CancelDSSync();    
       
   579                 }
       
   580             else // ESmlDevMan
       
   581                 {
       
   582                 CancelDMSync();
       
   583                 }
       
   584             }
       
   585         }
       
   586     else
       
   587         {
       
   588         // Start job if queued.
       
   589         if ( iCurrentJob && iCurrentJob->JobRunning() == EFalse )
       
   590             {
       
   591             if ( iCurrentJob->UsageType() == ESmlDataSync )
       
   592                 {
       
   593                 StartDSSyncL();    
       
   594                 }
       
   595             else // ESmlDevMan
       
   596                 {
       
   597                 StartDMSyncL();
       
   598                 }
       
   599             }
       
   600         // If there are no sessions or jobs and message queue is empty
       
   601         // then server can be stopped.
       
   602         if ( !iSessionCount && !iCurrentJob && iMsgQueue->IsEmpty() )
       
   603             {
       
   604             CActiveScheduler::Stop();
       
   605             }
       
   606         }
       
   607     }
       
   608 
       
   609 // --------------------------------------------------------------------------
       
   610 // TBool CNSmlSOSHandler::IsSuspended()
       
   611 // --------------------------------------------------------------------------
       
   612 //
       
   613 TBool CNSmlSOSHandler::IsSuspended()
       
   614     {
       
   615     return iSuspended;
       
   616     }
       
   617 
       
   618 // --------------------------------------------------------------------------
       
   619 // void CNSmlSOSHandler::StartDSSyncL()
       
   620 // Starts DS sync job.
       
   621 // --------------------------------------------------------------------------
       
   622 //
       
   623 void CNSmlSOSHandler::StartDSSyncL()
       
   624     {
       
   625     _DBG_FILE("CNSmlSOSHandler::StartDSSyncL(): begin");
       
   626 	static _LIT_SECURITY_POLICY_PASS(KAllowAllPolicy);
       
   627    	static _LIT_SECURITY_POLICY_C1(KAllowWriteDeviceDataPolicy, ECapabilityWriteDeviceData);
       
   628     TInt sessiontype=1;
       
   629     TInt r=RProperty::Define(KPSUidNSmlSOSServerKey,KNSmlSyncJobOngoing,RProperty::EInt,KAllowAllPolicy,KAllowWriteDeviceDataPolicy);
       
   630 	if ( r != KErrNone && r != KErrAlreadyExists )
       
   631     	{
       
   632     	User::LeaveIfError(r);
       
   633     	}
       
   634     //sessiontype=1 for DS session 	              
       
   635     TInt r2=RProperty::Set(KPSUidNSmlSOSServerKey,KNSmlSyncJobOngoing,sessiontype);
       
   636     DBG_FILE_CODE( sessiontype, _S8("P&S key set to 1 for ds session") );    
       
   637     //Set job as running from now on
       
   638     iCurrentJob->SetJobRunning( ETrue );
       
   639     
       
   640     // client or server iniated sync
       
   641 	TNSmlSyncInitiation syncInit = EClientInitiated;
       
   642 	
       
   643     // Get non-overrided data from settings.
       
   644     CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
   645     CNSmlDSProfile* prof = settings->ProfileL( iCurrentJob->ProfileId() );
       
   646     if ( !prof )    // profile not found, delete job.
       
   647         {
       
   648         CleanupStack::PopAndDestroy(settings);
       
   649         FinishCurrentJobL();      
       
   650 	    _DBG_FILE("CNSmlSOSHandler::StartDSSyncL(): profile not found!");
       
   651         return;
       
   652         }
       
   653 
       
   654     CleanupStack::PushL(prof);    
       
   655 	
       
   656 	if ( iCurrentJob->iPending || prof->IntValue( EDSProfileHidden ) )
       
   657 		{
       
   658 		syncInit = EServerAlerted;	
       
   659 		}
       
   660 		
       
   661     iParams = new (ELeave) CNSmlThreadParams( *iCurrentJob, this );
       
   662 	iParams->iCSArray = new(ELeave) CArrayFixFlat<TNSmlContentSpecificSyncType>(1);
       
   663 	iParams->iSyncInit = syncInit;
       
   664 		
       
   665 	TNSmlContentSpecificSyncType css;
       
   666     // Tasks and sync type are overrided in a same struct -> must be handled together.
       
   667    
       
   668     RArray<TInt> tasks;
       
   669     iCurrentJob->TaskIds( tasks );
       
   670     CleanupClosePushL(tasks);
       
   671 	if (iCurrentJob->iCtInfo)
       
   672 		{
       
   673 		for (TInt i = 0; i < iCurrentJob->iCtInfo->Count(); i++)
       
   674         	{
       
   675             css.iType = ConvertSyncTypes( iCurrentJob->iCtInfo->At(i).iSyncType );   
       
   676         	css.iTaskId = iCurrentJob->iCtInfo->At(i).iTaskId;
       
   677         	iParams->iCSArray->AppendL(css);
       
   678         	}
       
   679         	
       
   680 		}
       
   681 	else
       
   682     if ( tasks.Count() || ( iCurrentJob->SyncType() >= ESmlTwoWay &&
       
   683                            iCurrentJob->SyncType() <= ESmlRefreshFromClient ) )
       
   684         {
       
   685         
       
   686         TSmlSyncType sType;
       
   687     
       
   688         // If tasks are not overrided, get tasks from db.
       
   689         if ( !tasks.Count() )
       
   690             {
       
   691             ReadTasksFromDBL( tasks );
       
   692             }
       
   693 
       
   694         for ( TInt i(0) ; i < tasks.Count() ; i++ )
       
   695             {            
       
   696             if ( iCurrentJob->SyncType() >= ESmlTwoWay &&
       
   697                  iCurrentJob->SyncType() <= ESmlRefreshFromClient )
       
   698                 {
       
   699                 sType = iCurrentJob->SyncType();
       
   700                 }
       
   701             else    // If sync type is not overridden, get sync type from db.
       
   702                 {
       
   703                 sType = ReadSyncTypeForTaskL( tasks[i] );
       
   704                 }
       
   705                 
       
   706             // Convert sync types to match CNSmlDbCaps sync types.
       
   707             css.iType = ConvertSyncTypes( sType );
       
   708             css.iTaskId = tasks[i];
       
   709             iParams->iCSArray->AppendL(css);
       
   710             }
       
   711         }
       
   712        
       
   713 	iStatus = KRequestPending;
       
   714 	        		
       
   715 	iThreadObserver = new (ELeave) CNSmlThreadObserver( iStatus );
       
   716 	
       
   717 	//Start observing DM session thread.
       
   718 	TInt error = TNSmlThreadLauncher::RunJobSession( *iParams, *iThreadObserver );
       
   719 	if (  error != KErrNone )
       
   720 		{
       
   721 		HandleEventMsg( MSyncMLEventObserver::EJobStartFailed, error );	
       
   722 		return;
       
   723 		}
       
   724 	
       
   725 	HandleEventMsg( MSyncMLEventObserver::EJobStart );    
       
   726 
       
   727     CleanupStack::PopAndDestroy(&tasks);
       
   728     
       
   729     CleanupStack::PopAndDestroy(prof);
       
   730     CleanupStack::PopAndDestroy(settings);
       
   731 
       
   732     if ( !IsActive() )
       
   733         {
       
   734         SetActive(); // Set object active to start listening RequestComplete() in RunL.
       
   735         }
       
   736     
       
   737     _DBG_FILE("CNSmlSOSHandler::StartDSSyncL(): end");
       
   738     }
       
   739 
       
   740 // --------------------------------------------------------------------------
       
   741 // CNSmlDbCaps::ENSmlSyncTypes CNSmlSOSHandler::ConvertSyncTypes(TSmlSyncType aSyncType)
       
   742 // --------------------------------------------------------------------------
       
   743 //
       
   744 CNSmlDbCaps::ENSmlSyncTypes CNSmlSOSHandler::ConvertSyncTypes(TSmlSyncType aSyncType)
       
   745 	{
       
   746 	CNSmlDbCaps::ENSmlSyncTypes syncType;
       
   747 	
       
   748 	switch ( aSyncType )
       
   749         {
       
   750         case ESmlTwoWay:
       
   751             syncType = CNSmlDbCaps::ETwoWaySync;
       
   752             break;
       
   753         case ESmlOneWayFromServer:
       
   754             syncType = CNSmlDbCaps::EOneWaySyncFromServer;
       
   755             break;
       
   756         case ESmlOneWayFromClient:
       
   757             syncType = CNSmlDbCaps::EOneWaySyncFromClient;
       
   758             break;
       
   759         case ESmlSlowSync:
       
   760             syncType = CNSmlDbCaps::ESlowTwoWaySync;
       
   761             break;
       
   762         case ESmlRefreshFromServer:
       
   763             syncType = CNSmlDbCaps::ERefreshSyncFromServer;
       
   764             break;
       
   765         case ESmlRefreshFromClient: 
       
   766             syncType = CNSmlDbCaps::ERefreshSyncFromClient;
       
   767             break;
       
   768         default:
       
   769             syncType = CNSmlDbCaps::ETwoWaySync;
       
   770             break;
       
   771  		}
       
   772  	return syncType;
       
   773 	}
       
   774 // --------------------------------------------------------------------------
       
   775 // void CNSmlSOSHandler::CancelDSSync()
       
   776 // --------------------------------------------------------------------------
       
   777 //
       
   778 void CNSmlSOSHandler::CancelDSSync()
       
   779     {
       
   780     _DBG_FILE("CNSmlSOSHandler::CancelDSSync(): begin");
       
   781    
       
   782     if (( iParams) && (iParams->iThreadEngine ))
       
   783     	{
       
   784     	iParams->iThreadEngine->CancelJob();	
       
   785     	}
       
   786        
       
   787     _DBG_FILE("CNSmlSOSHandler::CancelDSSync(): end");
       
   788     }
       
   789 
       
   790 // --------------------------------------------------------------------------
       
   791 // void CNSmlSOSHandler::StartDMSyncL()
       
   792 // Starts DM configuration job.
       
   793 // --------------------------------------------------------------------------
       
   794 //
       
   795 void CNSmlSOSHandler::StartDMSyncL()
       
   796     {
       
   797     _DBG_FILE("CNSmlSOSHandler::StartDMSyncL(): begin");
       
   798 	static _LIT_SECURITY_POLICY_PASS(KAllowAllPolicy);
       
   799    	static _LIT_SECURITY_POLICY_C1(KAllowWriteDeviceDataPolicy, ECapabilityWriteDeviceData);
       
   800     TInt sessiontype = 2;
       
   801     TInt r=RProperty::Define(KPSUidNSmlSOSServerKey,KNSmlSyncJobOngoing,RProperty::EInt,KAllowAllPolicy,KAllowWriteDeviceDataPolicy);
       
   802 	if ( r != KErrNone && r != KErrAlreadyExists )
       
   803     	{
       
   804     	User::LeaveIfError(r);
       
   805         }
       
   806     //sessiontype=2 for dm session                  
       
   807     TInt r2=RProperty::Set(KPSUidNSmlSOSServerKey,KNSmlSyncJobOngoing,sessiontype);
       
   808     DBG_FILE_CODE( sessiontype, _S8("P&S key set to 2 for dm session") );    
       
   809     //Set job as running from now on
       
   810     iCurrentJob->SetJobRunning( ETrue );
       
   811 
       
   812 	TNSmlSyncInitiation syncInit = EClientInitiated;
       
   813     CNSmlDMSettings* settings = CNSmlDMSettings::NewLC();
       
   814     CNSmlDMProfile* prof = settings->ProfileL( iCurrentJob->ProfileId() );
       
   815     if(prof)
       
   816         {
       
   817         CleanupStack::PushL( prof );
       
   818 	    }
       
   819     
       
   820     if ( iCurrentJob->iPending || prof->IntValue( EDMProfileHidden ))
       
   821         {
       
   822         syncInit = EServerAlerted;
       
   823         }
       
   824 
       
   825     if ( !iCurrentJob->TransportId() )
       
   826         {
       
   827         if ( prof )
       
   828             {
       
   829             iCurrentJob->SetTransportId( prof->IntValue( EDMProfileTransportId ) );
       
   830             }
       
   831         }
       
   832     if(prof)
       
   833         {
       
   834         CleanupStack::PopAndDestroy( prof );
       
   835         }
       
   836     CleanupStack::PopAndDestroy( settings );
       
   837  
       
   838 
       
   839     switch ( iCurrentJob->JobType() )
       
   840         {
       
   841         case EDMJobProfile:
       
   842         case EDMJobTransport:
       
   843         	    {        		                		
       
   844         		iStatus = KRequestPending;
       
   845         		iParams = new (ELeave) CNSmlThreadParams( *iCurrentJob, this );
       
   846         		iParams->iSyncInit = syncInit; 
       
   847         		        		
       
   848         		iThreadObserver = new (ELeave) CNSmlThreadObserver( iStatus );
       
   849         		
       
   850         		//Start observing DM session thread.
       
   851         		TInt error = TNSmlThreadLauncher::RunJobSession( *iParams, *iThreadObserver );
       
   852    		    	if (  error != KErrNone )
       
   853    		    		{
       
   854    		    		HandleEventMsg( MSyncMLEventObserver::EJobStartFailed, error );   		    		
       
   855    		    		return;
       
   856    		    		}
       
   857         	    }
       
   858             break;
       
   859         default:
       
   860             HandleEventMsg( MSyncMLEventObserver::EJobStartFailed );
       
   861             break;
       
   862         }
       
   863     HandleEventMsg( MSyncMLEventObserver::EJobStart, iCurrentJob->JobId() );
       
   864 
       
   865  	if ( !IsActive() )
       
   866         {
       
   867         SetActive(); // Set object active to start listening RequestComplete() in RunL.
       
   868         }   
       
   869 
       
   870     _DBG_FILE("CNSmlSOSHandler::StartDMSyncL(): end");
       
   871     }
       
   872 
       
   873 // --------------------------------------------------------------------------
       
   874 // void CNSmlSOSHandler::CancelDMSync()
       
   875 // --------------------------------------------------------------------------
       
   876 //
       
   877 void CNSmlSOSHandler::CancelDMSync()
       
   878     {
       
   879     _DBG_FILE("CNSmlSOSHandler::CancelDMSync(): begin");
       
   880     
       
   881     if (( iParams) && (iParams->iThreadEngine ))
       
   882     	{
       
   883     	iParams->iThreadEngine->CancelJob();	
       
   884     	}
       
   885         
       
   886     _DBG_FILE("CNSmlSOSHandler::CancelDMSync(): end");
       
   887     }
       
   888 
       
   889 // --------------------------------------------------------------------------
       
   890 // TBool CNSmlSOSHandler::SearchSimilarJobs( const CNSmlJob* aJob )
       
   891 // Searches queued job if created job already exists.
       
   892 // --------------------------------------------------------------------------
       
   893 //
       
   894 TBool CNSmlSOSHandler::SearchSimilarJobs( const CNSmlJob* aJob )
       
   895     {
       
   896     // Goes through all the jobs and returns ETrue if similar exists.
       
   897     CNSmlJob* tempJob = iCurrentJob;
       
   898         
       
   899     while ( tempJob->iNext )
       
   900        {
       
   901        RArray<TInt> tempTasks;
       
   902        tempJob->TaskIds( tempTasks );
       
   903        
       
   904        RArray<TInt> tasks;
       
   905        aJob->TaskIds( tasks );
       
   906        
       
   907        TInt match(0);
       
   908        if ( tempJob->ProfileId()       == aJob->ProfileId()       &&
       
   909             tempTasks.Count()          == tasks.Count()           &&
       
   910             tempJob->SyncType()        == aJob->SyncType()        &&
       
   911             tempJob->TransportId()     == aJob->TransportId() )
       
   912             {
       
   913             // Also check if all the task ids match if there are any.
       
   914             if( !tasks.Count() )
       
   915                 {
       
   916                 tempTasks.Close();
       
   917                 tasks.Close();
       
   918                 return ETrue;
       
   919                 }
       
   920             for( TInt i(0); i < tasks.Count(); i++ )
       
   921                 {    
       
   922                 if( tasks.operator[](i) == tempTasks.operator[](i) )
       
   923                     {
       
   924                     match++;   
       
   925                     }
       
   926                 if ( match == tasks.Count() )
       
   927                     {
       
   928                     tempTasks.Close();
       
   929                     tasks.Close();
       
   930                     return ETrue;
       
   931                     }
       
   932                 }
       
   933             }
       
   934             
       
   935 	   tempTasks.Close();
       
   936        tasks.Close();
       
   937        
       
   938        tempJob = tempJob->iNext;
       
   939        }
       
   940     return EFalse;
       
   941     }
       
   942 
       
   943 // --------------------------------------------------------------------------
       
   944 // void CNSmlSOSHandler::ReadTasksFromDBL( RArray<TInt>& aArray )
       
   945 // --------------------------------------------------------------------------
       
   946 //
       
   947 void CNSmlSOSHandler::ReadTasksFromDBL( RArray<TInt>& aArray )
       
   948     {
       
   949     // Get non-overrided data from settings.
       
   950     CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
   951     CNSmlDSProfile* prof = settings->ProfileL( iCurrentJob->ProfileId() );
       
   952 
       
   953     if ( prof )
       
   954         {        
       
   955         CleanupStack::PushL(prof);
       
   956         RImplInfoPtrArray implArray;
       
   957         CleanupStack::PushL(PtrArrCleanupItemRArr( CImplementationInformation, &implArray ) );    
       
   958         TUid ifUid = { KNSmlDSInterfaceUid };
       
   959         REComSession::ListImplementationsL( ifUid, implArray );
       
   960 
       
   961         for ( TInt i = 0 ; i < implArray.Count() ; i++ )
       
   962             {    
       
   963             CImplementationInformation* implInfo = implArray[i];
       
   964             CNSmlDSContentType* ctype = prof->ContentType( implInfo->ImplementationUid().iUid );
       
   965             if ( ctype )
       
   966                 {            
       
   967                 aArray.AppendL( ctype->IntValue( EDSAdapterTableId ) );
       
   968                 }
       
   969             }        
       
   970         CleanupStack::PopAndDestroy(&implArray); 
       
   971         CleanupStack::PopAndDestroy(prof);
       
   972         REComSession::FinalClose();        
       
   973         }    
       
   974     CleanupStack::PopAndDestroy(settings);
       
   975     }
       
   976 
       
   977 // --------------------------------------------------------------------------
       
   978 // TSmlSyncType CNSmlSOSHandler::ReadSyncTypeForTaskL( const TInt aTaskId )
       
   979 // --------------------------------------------------------------------------
       
   980 //
       
   981 TSmlSyncType CNSmlSOSHandler::ReadSyncTypeForTaskL( const TInt aTaskId )
       
   982     {
       
   983     CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
   984     TSmlSyncType syncType( ESmlTwoWay );
       
   985     CNSmlDSProfile* prof = settings->ProfileL( iCurrentJob->ProfileId() );
       
   986     if ( prof )
       
   987         {
       
   988         CNSmlDSContentType* ctype = prof->ContentTypeId( aTaskId ); 
       
   989         // content type is deleted in CNSmlDSProfile destructor.
       
   990         syncType = (TSmlSyncType) ctype->IntValue( EDSAdapterSyncType );
       
   991             
       
   992         delete prof;
       
   993         prof = NULL;
       
   994         }
       
   995     CleanupStack::PopAndDestroy(settings);
       
   996     return syncType;
       
   997     }
       
   998 
       
   999 // --------------------------------------------------------------------------
       
  1000 // void CNSmlSOSHandler::FinishCurrentJobL()
       
  1001 // Removes finished job from queue and starts next from queue if one exists.
       
  1002 // --------------------------------------------------------------------------
       
  1003 //
       
  1004 void CNSmlSOSHandler::FinishCurrentJobL()
       
  1005     {
       
  1006     _DBG_FILE("CNSmlSOSHandler::FinishCurrentJobL(): begin");
       
  1007         
       
  1008 	if ( iParams )
       
  1009     	{
       
  1010        	delete iParams;
       
  1011        	iParams = NULL;	
       
  1012        	}
       
  1013        	
       
  1014      if (iThreadObserver)
       
  1015     	{
       
  1016     	delete iThreadObserver;
       
  1017     	iThreadObserver = NULL;        	
       
  1018     	}
       
  1019      
       
  1020     if ( iCurrentJob->iNext ) // If there is a job waiting, remove current and start next.  
       
  1021         {
       
  1022         CNSmlJob* nextJob = iCurrentJob->iNext;
       
  1023         delete iCurrentJob;
       
  1024         iCurrentJob = nextJob;
       
  1025 
       
  1026         if ( iCurrentJob->UsageType() == ESmlDataSync )
       
  1027             {
       
  1028             StartDSSyncL();
       
  1029             }
       
  1030         else
       
  1031             {
       
  1032             StartDMSyncL();
       
  1033             }
       
  1034         }
       
  1035     else // No jobs at queue.
       
  1036         {
       
  1037         delete iCurrentJob;
       
  1038         iCurrentJob = NULL;
       
  1039         // if no clients connected and no jobs queued -> stop the server.
       
  1040         if ( !iSessionCount && iMsgQueue->IsEmpty() )
       
  1041             {
       
  1042             _DBG_FILE("CNSmlSOSHandler::FinishCurrentJobL(): CActiveScheduler::Stop()");
       
  1043             CActiveScheduler::Stop();   
       
  1044             }
       
  1045         }    
       
  1046      _DBG_FILE("CNSmlSOSHandler::FinishCurrentJobL(): end");
       
  1047     }
       
  1048 
       
  1049 // --------------------------------------------------------------------------
       
  1050 // void CNSmlSOSHandler::DoCancel()
       
  1051 // --------------------------------------------------------------------------
       
  1052 //
       
  1053 void CNSmlSOSHandler::DoCancel()
       
  1054     {
       
  1055     }
       
  1056 
       
  1057 // --------------------------------------------------------------------------
       
  1058 // void CNSmlSOSHandler::RunL()
       
  1059 // --------------------------------------------------------------------------
       
  1060 //
       
  1061 void CNSmlSOSHandler::RunL()
       
  1062     {
       
  1063     _DBG_FILE("CNSmlSOSHandler::RunL() : Begin");
       
  1064     
       
  1065     HandleEventMsg( MSyncMLEventObserver::EJobStop, iStatus.Int() );        
       
  1066     // Remove current job and start next if one exists.  
       
  1067     TRAP_IGNORE( FinishCurrentJobL() );       
       
  1068     
       
  1069     _DBG_FILE("CNSmlSOSHandler::RunL() : End");
       
  1070     }
       
  1071 
       
  1072 // --------------------------------------------------------------------------
       
  1073 // void CNSmlSOSHandler::CreateJobL( CNSmlAlertJobInfo& aJobInfo )
       
  1074 // Creates server alerted job got from message queue.
       
  1075 // --------------------------------------------------------------------------
       
  1076 //
       
  1077 void CNSmlSOSHandler::CreateJobL( CNSmlAlertJobInfo& aJobInfo )
       
  1078     {
       
  1079     CNSmlJob* job = new (ELeave) CNSmlJob;
       
  1080     CleanupStack::PushL(job);
       
  1081     job->SetProfId( aJobInfo.iProfileId );
       
  1082     job->SetTransportId( aJobInfo.iTransportId );
       
  1083     job->SetUsageType( aJobInfo.iType );
       
  1084     job->iUimode = aJobInfo.iUimode;   
       
  1085     if ( aJobInfo.iType == ESmlDataSync )
       
  1086         {        
       
  1087         if ( aJobInfo.iPackage.Length() )
       
  1088             {
       
  1089             job->iPackage = aJobInfo.iPackage.AllocL();
       
  1090             job->iSessionId = aJobInfo.iSessionId;
       
  1091             }
       
  1092             
       
  1093         job->iCtInfo = new (ELeave) CArrayFixFlat<TNSmlContentTypeInfo>(1);
       
  1094 
       
  1095         for ( TInt i=0 ; i < aJobInfo.iContentType->Count() ; i++ )
       
  1096             {
       
  1097             job->AddTaskIdL( aJobInfo.iContentType->At(i).iTaskId);
       
  1098             job->iCtInfo->AppendL( aJobInfo.iContentType->At(i) );
       
  1099             }
       
  1100 
       
  1101         job->SetJobType( EDSJobTaskTransST );
       
  1102         
       
  1103         CNSmlDSSettings* settings = CNSmlDSSettings::NewLC();
       
  1104         CNSmlDSProfile* prof = settings->ProfileL( aJobInfo.iProfileId );
       
  1105         if ( prof )
       
  1106             {
       
  1107             if ( !prof->IntValue( EDSProfileHidden ) )
       
  1108                 {
       
  1109                 job->iPending = ETrue; // Notifier is not called if profile is hidden.
       
  1110                 }
       
  1111             delete prof;
       
  1112             prof = NULL;
       
  1113             }
       
  1114         CleanupStack::PopAndDestroy(settings);
       
  1115         }
       
  1116     else  // ESmlDevMan
       
  1117         {
       
  1118         job->SetJobType( EDMJobTransport );
       
  1119         CNSmlDMSettings* settings = CNSmlDMSettings::NewLC();
       
  1120         CNSmlDMProfile* prof = settings->ProfileL( aJobInfo.iProfileId );
       
  1121         if ( prof )
       
  1122             {
       
  1123             if ( !prof->IntValue( EDMProfileHidden ) )
       
  1124                 {
       
  1125                 job->iPending = ETrue; // Notifier is not called if profile is hidden.
       
  1126                 }
       
  1127             delete prof;
       
  1128             prof = NULL;
       
  1129             }
       
  1130         CleanupStack::PopAndDestroy(settings);
       
  1131         }        
       
  1132 
       
  1133     TInt jobId(0);
       
  1134     // Add created job to the queue.
       
  1135     AddJobL( job, jobId );
       
  1136     CleanupStack::Pop(job);
       
  1137     }
       
  1138 
       
  1139 // --------------------------------------------------------------------------
       
  1140 // CNSmlSOSHandler::DoDisconnectL()
       
  1141 // Disconnects local connection.
       
  1142 // --------------------------------------------------------------------------
       
  1143 //
       
  1144 void CNSmlSOSHandler::DoDisconnectL()
       
  1145 	{
       
  1146 	CNSmlTransport* transport = CNSmlTransport::NewLC();
       
  1147 		
       
  1148 	TRequestStatus status;
       
  1149 	TDesC8 temp = KNullDesC8();
       
  1150 	
       
  1151 	HBufC8* mimeType = KNSmlDSAgentMIMEType().AllocLC();	
       
  1152 	transport->ConnectL( KUidNSmlMediumTypeUSB, ETrue, NULL, temp, *mimeType , status, temp, temp, 0 ); 
       
  1153 	User::WaitForRequest(status);
       
  1154 		
       
  1155 	if (status.Int() == KErrNone)
       
  1156 		{
       
  1157 		transport->Disconnect();
       
  1158 		}
       
  1159 	CleanupStack::PopAndDestroy( mimeType );
       
  1160 	CleanupStack::PopAndDestroy( transport );
       
  1161 	}
       
  1162 	
       
  1163 // --------------------------------------------------------------------------
       
  1164 // Progress observer
       
  1165 // void CNSmlSOSHandler::OnSyncMLSyncError( TErrorLevel aErrorLevel, TInt aError, TInt aTaskId, TInt aInfo1, TInt aInfo2 )
       
  1166 // --------------------------------------------------------------------------
       
  1167 //
       
  1168 void CNSmlSOSHandler::OnSyncMLSyncError( TErrorLevel aErrorLevel, TInt aError, TInt aTaskId, TInt aInfo1, TInt aInfo2 )
       
  1169     {
       
  1170     // do not try to buffer, if there is not active session
       
  1171     if ( iCurrentJob->OwnerSession() != NULL )
       
  1172         {
       
  1173         TNSmlProgressEvent event;
       
  1174         event.iEventType = ENSmlSyncError;        
       
  1175         event.iErrorLevel = aErrorLevel;   
       
  1176         event.iInfo1 = aError;
       
  1177         event.iInfo2 = aTaskId;   
       
  1178         event.iInfo3 = aInfo1;
       
  1179         event.iInfo4 = aInfo2;
       
  1180 
       
  1181         iCurrentJob->OwnerSessionNonConst()->StoreToProgessEventBuffer( event );            
       
  1182         }      
       
  1183     }
       
  1184 
       
  1185 // --------------------------------------------------------------------------
       
  1186 // HBufC8* CNSmlSOSHandler::WriteSyncErrorL( TErrorLevel aErrorLevel, TInt aError, TInt aTaskId, TInt aInfo1, TInt aInfo2 )
       
  1187 // --------------------------------------------------------------------------
       
  1188 //
       
  1189 HBufC8* CNSmlSOSHandler::WriteSyncErrorL( TErrorLevel aErrorLevel, TInt aError, TInt aTaskId, TInt aInfo1, TInt aInfo2 )
       
  1190 	{
       
  1191     HBufC8* buf = HBufC8::NewLC( ( 3*KSizeofTInt8 + ( 4*KSizeofTInt32 ) ) );
       
  1192     TPtr8 bufPtr = buf->Des();
       
  1193 
       
  1194     RDesWriteStream stream;
       
  1195     stream.Open( bufPtr );
       
  1196     CleanupClosePushL( stream );
       
  1197 
       
  1198     stream.WriteInt8L( (TInt8) ENSmlTypeProgressEvent );
       
  1199     stream.WriteInt8L( (TInt8) ENSmlSyncError );
       
  1200     stream.WriteInt8L( (TInt8) aErrorLevel );
       
  1201 
       
  1202     stream.WriteInt32L( aError );
       
  1203     stream.WriteInt32L( aTaskId );
       
  1204     stream.WriteInt32L( aInfo1 );
       
  1205     stream.WriteInt32L( aInfo2 );
       
  1206     stream.CommitL();
       
  1207     
       
  1208     CleanupStack::PopAndDestroy( &stream ); 
       
  1209     CleanupStack::Pop( buf );
       
  1210    	
       
  1211    	return buf;
       
  1212 	}
       
  1213 
       
  1214 // --------------------------------------------------------------------------
       
  1215 // void CNSmlSOSHandler::OnSyncMLSyncProgress( MSyncMLProgressObserver::TStatus aStatus, TInt aInfo1, TInt aInfo2 )
       
  1216 // --------------------------------------------------------------------------
       
  1217 //
       
  1218 void CNSmlSOSHandler::OnSyncMLSyncProgress( MSyncMLProgressObserver::TStatus aStatus, TInt aInfo1, TInt aInfo2 )
       
  1219     {
       
  1220     // do not try to buffer, if there is not active session
       
  1221     if ( iCurrentJob->OwnerSession() != NULL )
       
  1222         {
       
  1223         TNSmlProgressEvent event;
       
  1224         event.iEventType = ENSmlSyncProgress;
       
  1225         event.iProgressStatus = aStatus;    
       
  1226         event.iInfo1 = aInfo1;
       
  1227         event.iInfo2 = aInfo2;   
       
  1228         event.iInfo3 = 0;
       
  1229         event.iInfo4 = 0;
       
  1230     
       
  1231         iCurrentJob->OwnerSessionNonConst()->StoreToProgessEventBuffer( event );
       
  1232         }                	
       
  1233     }
       
  1234 
       
  1235 // --------------------------------------------------------------------------
       
  1236 // HBufC8* CNSmlSOSHandler::WriteSyncProgressL( TStatus aStatus, TInt aInfo1, TInt aInfo2 )
       
  1237 // --------------------------------------------------------------------------
       
  1238 //
       
  1239 HBufC8* CNSmlSOSHandler::WriteSyncProgressL( TStatus aStatus, TInt aInfo1, TInt aInfo2 )
       
  1240 	{
       
  1241 	HBufC8* buf = HBufC8::NewLC( (3*KSizeofTInt8 + (2*KSizeofTInt32) ));
       
  1242     TPtr8 bufPtr = buf->Des();
       
  1243 
       
  1244     RDesWriteStream stream;
       
  1245     stream.Open( bufPtr );
       
  1246     CleanupClosePushL( stream );
       
  1247 
       
  1248     stream.WriteInt8L( (TInt8) ENSmlTypeProgressEvent );
       
  1249     stream.WriteInt8L( (TInt8) ENSmlSyncProgress );
       
  1250     stream.WriteInt8L( (TInt8) aStatus );
       
  1251 
       
  1252     stream.WriteInt32L( aInfo1 );
       
  1253     stream.WriteInt32L( aInfo2 );
       
  1254     stream.CommitL(); 
       
  1255     
       
  1256     CleanupStack::PopAndDestroy( &stream );
       
  1257     CleanupStack::Pop( buf );
       
  1258     
       
  1259     return buf;
       
  1260 	}
       
  1261 
       
  1262 // --------------------------------------------------------------------------
       
  1263 // void CNSmlSOSHandler::OnSyncMLDataSyncModifications( TInt aTaskId, const TSyncMLDataSyncModifications& aClientMods, const TSyncMLDataSyncModifications& aServerMods )
       
  1264 // --------------------------------------------------------------------------
       
  1265 //
       
  1266 void CNSmlSOSHandler::OnSyncMLDataSyncModifications( TInt aTaskId, const TSyncMLDataSyncModifications& aClientMods,
       
  1267                                                                    const TSyncMLDataSyncModifications& aServerMods )
       
  1268     {
       
  1269     
       
  1270     // do not try to buffer, if there is not active session
       
  1271     if ( iCurrentJob->OwnerSession() != NULL )
       
  1272         {
       
  1273         TNSmlProgressEvent event;
       
  1274         event.iEventType = ENSmlModifications;
       
  1275         event.iClientMods = aClientMods;
       
  1276         event.iServerMods = aServerMods;
       
  1277         event.iInfo1 = aTaskId;
       
  1278         event.iInfo2 = 0;   
       
  1279         event.iInfo3 = 0;
       
  1280         event.iInfo4 = 0;
       
  1281     
       
  1282         iCurrentJob->OwnerSessionNonConst()->StoreToProgessEventBuffer( event );            
       
  1283         }
       
  1284     }
       
  1285 
       
  1286 // --------------------------------------------------------------------------
       
  1287 // HBufC8* CNSmlSOSHandler::WriteSyncModificationsL( TInt aTaskId, const TSyncMLDataSyncModifications& aClientMods, const TSyncMLDataSyncModifications& aServerMods )
       
  1288 // --------------------------------------------------------------------------
       
  1289 //
       
  1290 HBufC8* CNSmlSOSHandler::WriteSyncModificationsL( TInt aTaskId, const TSyncMLDataSyncModifications& aClientMods,
       
  1291                                                                 const TSyncMLDataSyncModifications& aServerMods )
       
  1292 	{
       
  1293 	HBufC8* buf = HBufC8::NewLC( (2*KSizeofTInt8 + (11*KSizeofTInt32) ));
       
  1294     TPtr8 bufPtr = buf->Des();
       
  1295 
       
  1296     RDesWriteStream stream;
       
  1297     stream.Open(bufPtr);
       
  1298     CleanupClosePushL(stream);
       
  1299 
       
  1300     stream.WriteInt8L( (TInt8) ENSmlTypeProgressEvent );
       
  1301     stream.WriteInt8L( (TInt8) ENSmlModifications );
       
  1302 
       
  1303     stream.WriteInt32L( aTaskId );
       
  1304 
       
  1305     stream.WriteInt32L( aClientMods.iNumAdded );
       
  1306     stream.WriteInt32L( aClientMods.iNumReplaced );
       
  1307     stream.WriteInt32L( aClientMods.iNumMoved );
       
  1308     stream.WriteInt32L( aClientMods.iNumDeleted );
       
  1309     stream.WriteInt32L( aClientMods.iNumFailed );
       
  1310 
       
  1311     stream.WriteInt32L( aServerMods.iNumAdded );
       
  1312     stream.WriteInt32L( aServerMods.iNumReplaced );
       
  1313     stream.WriteInt32L( aServerMods.iNumMoved );
       
  1314     stream.WriteInt32L( aServerMods.iNumDeleted );
       
  1315     stream.WriteInt32L( aServerMods.iNumFailed );
       
  1316     stream.CommitL();
       
  1317     
       
  1318     CleanupStack::PopAndDestroy( &stream ); 
       
  1319     CleanupStack::Pop( buf );
       
  1320     
       
  1321     return buf;
       
  1322 	}
       
  1323 
       
  1324 // --------------------------------------------------------------------------
       
  1325 // CNSmlSOSHandler::HandleEventMsg( MSyncMLEventObserver::TEvent aEvent, 
       
  1326 // TInt aStatus )
       
  1327 // --------------------------------------------------------------------------
       
  1328 //
       
  1329 void CNSmlSOSHandler::HandleEventMsg( 
       
  1330     MSyncMLEventObserver::TEvent aEvent,
       
  1331     TInt aStatus )
       
  1332     {
       
  1333     _DBG_FILE("CNSmlSOSHandler::HandleEventMsg : Begin");
       
  1334     TBool found( EFalse );
       
  1335     
       
  1336     for (TInt i(0); i < iEventMsgs.Count(); i++ )    
       
  1337         {
       
  1338         if ( iEventMsgs[i].Session() == iCurrentJob->OwnerSession() )
       
  1339             {
       
  1340             found = ETrue;
       
  1341             }             
       
  1342         } 
       
  1343     
       
  1344     if ( !found && !iCurrentJob->iSilentJob)
       
  1345         {                              
       
  1346         if ( iCurrentJob->OwnerSession() )
       
  1347             {
       
  1348             _DBG_FILE("HandleEventMsg : Event buffered");
       
  1349             iCurrentJob->OwnerSessionNonConst()->StoreToEventBuffer( aEvent, iCurrentJob->JobId(), aStatus );            
       
  1350             }        
       
  1351         }
       
  1352     else
       
  1353         {        
       
  1354         _DBG_FILE("HandleEventMsg : Event completed");                      
       
  1355         TRAP_IGNORE( CompleteEventMsgL( aEvent, iCurrentJob->JobId(), aStatus ));        
       
  1356         }
       
  1357     _DBG_FILE("CNSmlSOSHandler::HandleEventMsg : End");                      
       
  1358     }
       
  1359 
       
  1360 // --------------------------------------------------------------------------
       
  1361 // CNSmlSOSHandler::ClearSessionFromJob( const CNSmlSOSSession* aSession )
       
  1362 // Resets session from job.
       
  1363 // --------------------------------------------------------------------------
       
  1364 //
       
  1365 void CNSmlSOSHandler::ClearSessionFromJob( const CNSmlSOSSession* aSession )
       
  1366     {
       
  1367         
       
  1368     CNSmlJob* tempJob = iCurrentJob;
       
  1369         
       
  1370     while ( tempJob )
       
  1371         {            
       
  1372         if (tempJob->OwnerSession() == aSession )        
       
  1373             {
       
  1374             tempJob->SetOwnerSession( NULL );    
       
  1375             }
       
  1376         tempJob = tempJob->iNext;
       
  1377         }    
       
  1378     }
       
  1379 
       
  1380 void CNSmlSOSHandler::RegisterContactSuiteObserversWithProfileId( TInt aProfileId )
       
  1381     {
       
  1382     iContactSuiteObserverProfileId = aProfileId;
       
  1383     }
       
  1384 
       
  1385 // --------------------------------------------------------------------------
       
  1386 // CNSmlSOSHandler::ClosePendingMessages( const CNSmlSOSSession* aSession )
       
  1387 // Resets session from job.
       
  1388 // --------------------------------------------------------------------------
       
  1389 //
       
  1390 void CNSmlSOSHandler::ClosePendingMessages( const CNSmlSOSSession* aSession )
       
  1391     {
       
  1392     TInt count = iProgressMsgs.Count();
       
  1393     TInt i (0);
       
  1394     while ( i < count )
       
  1395         {
       
  1396         if ( aSession == iProgressMsgs[i].Session() )
       
  1397             {     
       
  1398             _DBG_FILE("Close pending progress message");                                        
       
  1399             iProgressMsgs.Remove(i); 
       
  1400             count = iProgressMsgs.Count();       
       
  1401             }   
       
  1402         else
       
  1403             {
       
  1404             i++;
       
  1405             }             
       
  1406         }
       
  1407     
       
  1408 	
       
  1409 	count = iContactSuiteProgressMsgs.Count(); 
       
  1410     i = 0;
       
  1411     while ( i < count )
       
  1412         {
       
  1413                 
       
  1414             _DBG_FILE("Close pending event message");                                        
       
  1415             iContactSuiteProgressMsgs.Remove(i); 
       
  1416             count = iContactSuiteProgressMsgs.Count();                                
       
  1417             i++;       
       
  1418         }
       
  1419 
       
  1420 
       
  1421     count = iEventMsgs.Count(); 
       
  1422     i = 0;
       
  1423     while ( i < count )
       
  1424         {
       
  1425         if ( aSession == iEventMsgs[i].Session() )
       
  1426             {     
       
  1427             _DBG_FILE("Close pending event message");                                        
       
  1428             iEventMsgs.Remove(i); 
       
  1429             count = iEventMsgs.Count();                       
       
  1430             } 
       
  1431          else
       
  1432             {
       
  1433             i++;
       
  1434             }    
       
  1435         }
       
  1436     }
       
  1437 
       
  1438 // End of file