syncmlfw/dm/hostserver/dmhostserverbase/src/nsmldmhostsession.cpp
changeset 0 b497e44ab2fc
child 2 5594fba90824
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 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:  A common session class for all the specialized 
       
    15 *				 DM Host Servers.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // ------------------------------------------------------------------------------------------------
       
    21 // Includes
       
    22 // ------------------------------------------------------------------------------------------------
       
    23 //
       
    24 #include <s32mem.h>
       
    25 #include <s32strm.h>
       
    26 #include <implementationinformation.h>
       
    27 #include <ecom.h>
       
    28 #include <nsmlconstants.h>
       
    29 #include <nsmldebug.h>
       
    30 #include <smldmadapter.h>
       
    31 
       
    32 // FOTA
       
    33 #include <nsmldmconst.h>
       
    34 // FOTA end
       
    35 #include <nsmldmuri.h>
       
    36 #include <e32property.h>
       
    37 #include "nsmldminternalpskeys.h"
       
    38 #include "nsmldmclientandserver.h"
       
    39 #include "nsmldmhostserver.h"
       
    40 #include "nsmldmhostsession.h"
       
    41 #include "nsmldmcmdarghandler.h"
       
    42 #include "nsmldmimpluids.h"
       
    43 #include <featmgr.h>
       
    44 
       
    45 #ifndef __WINS__
       
    46 // This lowers the unnecessary compiler warning (armv5) to remark.
       
    47 // "Warning:  #174-D: expression has no effect..." is caused by 
       
    48 // DBG_ARGS8 macro in no-debug builds.
       
    49 #pragma diag_remark 174
       
    50 #endif
       
    51 
       
    52 
       
    53 // ------------------------------------------------------------------------------------------------
       
    54 // Constants
       
    55 // ------------------------------------------------------------------------------------------------
       
    56 //
       
    57 // Granularity of the lists; mapping info and prevUriSegList
       
    58 const TInt KNSmlDmGranularity = 8;
       
    59 
       
    60 // This means the dm adapter doesn't support streaming
       
    61 const TInt KNSmlDmNotSupported = -1;
       
    62 
       
    63 // During the fetch link serving the callbacks are handled differently
       
    64 const TInt KNSmlDmNoRef = -1; 
       
    65 
       
    66 // A safety margin (in bytes) which is left unreserved in the chunk
       
    67 const TInt KNSmlDmChunkMarginal = 128;
       
    68 
       
    69 // A cross-reference to the interner adapter is recognized using these strings
       
    70 _LIT8 ( KNSmlDmApAdapterURI1, "AP" );
       
    71 _LIT8 ( KNSmlDmApAdapterURI2, "./AP" );
       
    72 
       
    73 // Identifies the server which owns the internet DM plug-in adapter
       
    74 const TInt KNSmlDmApAdapterServerId = 1;
       
    75 
       
    76 // Tells to the session class that it is acting as a fetch link server
       
    77 const TInt KNSmlDmFetchLinkServerId = 99;
       
    78 
       
    79 // The constant part of the result item size in bytes: 	
       
    80 //  iResultRef => 2 bytes
       
    81 //  iResult->Size => 4 bytes
       
    82 //  iMimeType->Length => 2 bytes
       
    83 const TInt KNSmlDmConstItemSize = 8;
       
    84 
       
    85 // FOTA
       
    86 const TInt KNSmlDmLastPackage = 1;
       
    87 // FOTA end
       
    88 
       
    89 
       
    90 // ------------------------------------------------------------------------------------------------
       
    91 // CNSmlDMHostSession::CNSmlDMHostSession
       
    92 //	C++ constructor.
       
    93 //	@param aServer. The server instance, owner of session.
       
    94 // ------------------------------------------------------------------------------------------------
       
    95 //
       
    96 CNSmlDMHostSession::CNSmlDMHostSession ( CNSmlDMHostServer& aServer ) : 
       
    97     iServer(aServer)
       
    98 	{
       
    99 	_DBG_FILE("CNSmlDMHostSession::CNSmlDMHostSession(): begin");
       
   100 	iServer.IncSessionCount();
       
   101 	_DBG_FILE("CNSmlDMHostSession::CNSmlDMHostSession(): end");
       
   102 	}
       
   103 
       
   104 // ------------------------------------------------------------------------------------------------
       
   105 // CNSmlDMHostSession::~CNSmlDMHostSession
       
   106 //  C++ destructor.
       
   107 // ------------------------------------------------------------------------------------------------
       
   108 //
       
   109 CNSmlDMHostSession::~CNSmlDMHostSession()
       
   110 	{	
       
   111 	_DBG_FILE("CNSmlDMHostSession::~CNSmlDMHostSession(): begin");
       
   112 	if ( iCbSessConnected )
       
   113 		{
       
   114 		iCbSession.Close();		
       
   115 		}
       
   116 	iServer.DecSessionCount();
       
   117 	delete iMgmtTree;
       
   118 	if ( iOwnId != KNSmlDmFetchLinkServerId )
       
   119 		{
       
   120 		iChunk.Close(); 		
       
   121 		}
       
   122 	
       
   123 	for ( TInt i=0; i<iResultList.Count(); i++ )
       
   124 		{
       
   125 		delete iResultList[i];
       
   126 		}
       
   127 	iResultList.Close();
       
   128 	iStatusCodeList.Close();
       
   129 	iCallbackList.Close();
       
   130 	
       
   131 	if ( iLargeWriteStream.streamPtr )
       
   132 		{
       
   133 		iLargeWriteStream.streamPtr->Close();		
       
   134 		}
       
   135 	if ( iResultReadStream )
       
   136 		{
       
   137 		iResultReadStream->Close();		
       
   138 		}
       
   139 	
       
   140 	// delete all loaded adapters
       
   141 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
   142 		{
       
   143 		delete iAdapters[i].adapterPtr;
       
   144 		}
       
   145 	iAdapters.Close();
       
   146 	REComSession::FinalClose();	
       
   147 	if ( iMapUris )
       
   148 		{
       
   149 		iMapUris->ResetAndDestroy();
       
   150 		delete iMapUris;	
       
   151 		}
       
   152 	if ( iMapLuids )
       
   153 		{
       
   154 		iMapLuids->ResetAndDestroy();
       
   155 		delete iMapLuids;
       
   156 		}
       
   157 	delete iSemaphoreName;
       
   158 	delete iResultOtherAdapter;
       
   159 	delete iLargeObject;
       
   160 	DeletePrevParams();
       
   161 	FeatureManager::UnInitializeLib();
       
   162 	_DBG_FILE("CNSmlDMHostSession::~CNSmlDMHostSession(): end");
       
   163 	}
       
   164 
       
   165 // ------------------------------------------------------------------------------------------------
       
   166 // CNSmlDMHostSession::NewL
       
   167 //	Creates a new session object.
       
   168 //	@param aServer. Reference to server is needed in order to increase and decrease session counts.
       
   169 //	@return CNSmlDMHostSession*. New instance of this class.
       
   170 // ------------------------------------------------------------------------------------------------
       
   171 //
       
   172 CNSmlDMHostSession* CNSmlDMHostSession::NewL ( CNSmlDMHostServer& aServer )
       
   173 	{
       
   174 	_DBG_FILE("CNSmlDMHostSession::NewL(): begin");
       
   175 	CNSmlDMHostSession* self= new (ELeave) CNSmlDMHostSession ( aServer );
       
   176 	CleanupStack::PushL(self);
       
   177 	self->ConstructL();
       
   178 	CleanupStack::Pop(); // self
       
   179 	_DBG_FILE("CNSmlDMHostSession::NewL(): end");
       
   180 	return self;
       
   181 	}
       
   182 
       
   183 // ------------------------------------------------------------------------------------------------
       
   184 // CNSmlDMHostSession::ConstructL
       
   185 //	Symbian 2nd phase constructor
       
   186 // ------------------------------------------------------------------------------------------------
       
   187 //
       
   188 void CNSmlDMHostSession::ConstructL()
       
   189 	{
       
   190 	_DBG_FILE("CNSmlDMHostSession::ConstructL(): begin");
       
   191 	iMgmtTree = CNSmlDmMgmtTree::NewL();
       
   192 	iCommitStatus.iStreamingOngoing = EFalse;
       
   193 	iCommitStatus.iStreamCommitted = EFalse;
       
   194 	FeatureManager::InitializeLibL();
       
   195 	_DBG_FILE("CNSmlDMHostSession::ConstructL(): end");
       
   196 	}
       
   197 
       
   198 // ------------------------------------------------------------------------------------------------
       
   199 // CNSmlDMHostSession::ServiceL
       
   200 //	Handles the servicing of client requests.
       
   201 //	Entry point for arriving messages.
       
   202 //	@param aMessage. An object which encapsulates a client request.
       
   203 // ------------------------------------------------------------------------------------------------
       
   204 //
       
   205 void CNSmlDMHostSession::ServiceL ( const RMessage2& aMessage )
       
   206 	{
       
   207 	_DBG_FILE("CNSmlDMHostSession::ServiceL(): begin");
       
   208 	if(!aMessage.HasCapability(ECapabilityDiskAdmin))
       
   209 	    {
       
   210 	    _DBG_FILE("CNSmlDMHostSession::ServiceL(): capability fail");
       
   211 	    aMessage.Complete( KErrPermissionDenied );
       
   212 	    return;
       
   213 	    }
       
   214 	TRAPD ( err, DispatchMessageL ( aMessage ) );
       
   215 	aMessage.Complete( err );
       
   216 	_DBG_FILE("CNSmlDMHostSession::ServiceL(): end");
       
   217 	}
       
   218 
       
   219 // ------------------------------------------------------------------------------------------------
       
   220 // CNSmlDMHostSession::DispatchMessageL
       
   221 //	Extracts the operation code from the message. 
       
   222 //	Based on that calls the right private function.
       
   223 //	@param aMessage. An object which encapsulates a client request.
       
   224 // ------------------------------------------------------------------------------------------------
       
   225 //
       
   226 void CNSmlDMHostSession::DispatchMessageL ( const RMessage2& aMessage )
       
   227 	{
       
   228 	_DBG_FILE("CNSmlDMHostSession::DispatchMessageL(): begin");
       
   229 	switch( aMessage.Function() )
       
   230         {
       
   231 		case ENSmlDMCloseSession:
       
   232         	CloseSessionL();
       
   233         	break;
       
   234 		case ENSmlDMGetDDF:
       
   235         	GetDDFStructureL ( aMessage );
       
   236         	break;
       
   237         case ENSmlDMUpdateLeaf:
       
   238         	UpdateLeafOrExecuteL ( aMessage, ENSmlDMUpdateLeaf );
       
   239         	break;
       
   240         case ENSmlDMExecute:
       
   241         	UpdateLeafOrExecuteL ( aMessage, ENSmlDMExecute );
       
   242         	break;
       
   243         case ENSmlDMUpdateLeafLarge:
       
   244         	UpdateLeafOrExecuteLargeL ( aMessage, ENSmlDMUpdateLeafLarge );
       
   245         	break;
       
   246         case ENSmlDMExecuteLarge:
       
   247         	UpdateLeafOrExecuteLargeL ( aMessage, ENSmlDMExecuteLarge );
       
   248         	break;        	
       
   249         case ENSmlDMAddNode:
       
   250         	AddCopyOrDeleteL ( aMessage, ENSmlDMAddNode );
       
   251         	break;
       
   252         case ENSmlDMCopy:
       
   253         	AddCopyOrDeleteL ( aMessage, ENSmlDMCopy );
       
   254         	break;
       
   255         case ENSmlDMDelete:
       
   256         	AddCopyOrDeleteL ( aMessage, ENSmlDMDelete );
       
   257         	break;        
       
   258         case ENSmlDMStartAtomic:
       
   259         	StartAtomicL ( aMessage );
       
   260         	break;
       
   261         case ENSmlDMCommitAtomic:
       
   262         	CommitAtomicL ( aMessage );
       
   263         	break;
       
   264         case ENSmlDMRollbackAtomic:
       
   265         	RollbackAtomicL ( aMessage );
       
   266         	break;
       
   267         case ENSmlDMCompleteCommands:
       
   268         	CompleteCommandsL ( aMessage );
       
   269         	break;
       
   270         case ENSmlDMFetchLeaf:
       
   271         	FetchLeafChildUrisOrSizeL ( aMessage, ENSmlDMFetchLeaf );
       
   272         	break;
       
   273         case ENSmlDMFetchLink:
       
   274         	FetchLinkL ( aMessage );
       
   275         	break;
       
   276         case ENSmlDMFetchLeafSize:
       
   277         	FetchLeafChildUrisOrSizeL ( aMessage, ENSmlDMFetchLeafSize );
       
   278         	break;
       
   279         case ENSmlDMChildUriList:
       
   280         	FetchLeafChildUrisOrSizeL ( aMessage, ENSmlDMChildUriList );
       
   281         	break;
       
   282     	case ENSmlDMChunkHandle:
       
   283         	SetChunkHandleL ( aMessage ); 
       
   284         	break;
       
   285     	case ENSmlDMGetMappings:
       
   286         	GetMappingsL ( aMessage ); 
       
   287         	break;
       
   288     	case ENSmlDMGetStatuses:
       
   289         	GetStatusesL ( aMessage ); 
       
   290         	break;
       
   291     	case ENSmlDMGetResults:
       
   292         	GetResultsL ( aMessage ); 
       
   293         	break;
       
   294     	case ENSmlDMGetMore:
       
   295         	GetResultsL ( aMessage ); 
       
   296         	break;
       
   297         // FOTA
       
   298     	case ENSmlDMGenericAlertsSent:
       
   299     	if(!FeatureManager::FeatureSupported( KFeatureIdSyncMlDm112  ))
       
   300     	{
       
   301     	_DBG_FILE(" CNSmlDMHostSession::DispatchMessageL(): calling new MarkGenAlertsSentL(message)");
       
   302         	MarkGenAlertsSentL(aMessage); 	
       
   303     	}
       
   304     	else
       
   305     	{
       
   306     	_DBG_FILE(" CNSmlDMHostSession::DispatchMessageL(): calling old  MarkGenAlertsSentL ");
       
   307         MarkGenAlertsSentL();	
       
   308     	}
       
   309         	break;
       
   310         // FOTA end
       
   311 		default:
       
   312 			PanicClient ( aMessage, KErrNotSupported );
       
   313         }
       
   314 	_DBG_FILE("CNSmlDMHostSession::DispatchMessageL(): end");
       
   315 	}
       
   316 
       
   317 
       
   318 // ------------------------------------------------------------------------------------------------
       
   319 // CNSmlDMHostSession::PanicClient
       
   320 //	Panics the client
       
   321 //	@param aMessage. Message from client to panic.
       
   322 //	@param aReason. Reason code.
       
   323 // ------------------------------------------------------------------------------------------------
       
   324 //
       
   325 void CNSmlDMHostSession::PanicClient ( const RMessage2& aMessage, TInt aReason ) const
       
   326 	{
       
   327 	_DBG_FILE("CNSmlDMHostSession::PanicClient(): begin");
       
   328 	aMessage.Panic ( _L( "CNSmlDMHostSession" ), aReason );
       
   329 	_DBG_FILE("CNSmlDMHostSession::PanicClient(): end");
       
   330 	}
       
   331 	
       
   332 // ------------------------------------------------------------------------------------------------
       
   333 // CNSmlDMHostSession::CloseSessionL
       
   334 //	Empty function.
       
   335 //	Kept here for possible future needs. 
       
   336 //	Freeing of resources is done in the destructor.
       
   337 // ------------------------------------------------------------------------------------------------
       
   338 //
       
   339 void CNSmlDMHostSession::CloseSessionL() const
       
   340 	{
       
   341 	_DBG_FILE("CNSmlDMHostSession::CloseSessionL(): begin");
       
   342 	_DBG_FILE("CNSmlDMHostSession::CloseSessionL(): end");
       
   343 	}
       
   344 
       
   345 void CNSmlDMHostSession::MarkGenAlertsSentL(const RMessage2& aMessage)
       
   346 	{
       
   347 
       
   348 	_DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(RMessage2): begin");
       
   349 	
       
   350 	HBufC8* tempPtr = HBufC8::NewLC(aMessage.GetDesLength(0));
       
   351 	_DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(RMessage2): crating hBufC for length of URI ");
       
   352 	TPtr8 uRI = tempPtr->Des();
       
   353    _DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(RMessage2): TPrt got from HBufC ");
       
   354 	aMessage.ReadL(0, uRI, 0);
       
   355 	TUint32 adId(0);
       
   356 	TNSmlDmDDFFormat nodeType;
       
   357 	_DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(RMessage2): Before calling adapterIdFromUriL  ");
       
   358 	TBool adapterFound = AdapterIdFromUriL ( uRI, adId, nodeType );
       
   359 	
       
   360 	_LIT8(KSCOMONODE, "SCOMO");
       
   361 	
       
   362 	// For AM Adapter check for Node "SCOMO" if present in Uri then use Adapter Implementation UID
       
   363 	
       
   364 	if(uRI.Compare(KSCOMONODE) == KErrNone)
       
   365 	{
       
   366 		adId = KNSmlDMAMAdapterImplUid;
       
   367 		adapterFound = ETrue;
       
   368 	}
       
   369 	
       
   370 	
       
   371 	_DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(RMessage2): After calling adapterIdFromUriL ");
       
   372 	
       
   373 	if ( adapterFound )
       
   374 		{
       
   375 	_DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(RMessage2): adapter is found ! ");	  
       
   376 	
       
   377         CSmlDmAdapter* adapter = NULL;
       
   378         adapterFound = EFalse;
       
   379 	    for ( TInt i(0); i<iAdapters.Count(); i++ )
       
   380 		{
       
   381 		
       
   382 		if ( iAdapters[i].adapterId == adId )
       
   383 			{
       
   384 			adapterFound = ETrue;
       
   385 			if ( !iAdapters[i].adapterPtr )
       
   386 				{
       
   387 				adapter = LoadNewAdapterL ( i, adId );
       
   388 				if ( !adapter )
       
   389 					{
       
   390 					User::Leave ( KErrArgument );
       
   391 					}
       
   392 				}
       
   393 			// Call overloaded function of the  adapter to request
       
   394 			// the  adapter to mark Generic Alerts sent.
       
   395 		
       
   396 			TInt resetGenAlerts ( KNSmlDMResetGenAlerts );
       
   397 			iAdapters[i].adapterPtr->StreamingSupport ( resetGenAlerts );
       
   398 		
       
   399 			}
       
   400 		}
       
   401 	}
       
   402 	CleanupStack::PopAndDestroy(); //tempPtr
       
   403 	if ( !adapterFound )
       
   404 	    {
       
   405 	    _DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(RMessage2): adapter NOT :-( found ! ");	  
       
   406 		User::Leave ( KErrNotFound );	    
       
   407 	    }
       
   408 	_DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(): end");
       
   409 	}
       
   410 
       
   411 // FOTA
       
   412 // ------------------------------------------------------------------------------------------------
       
   413 // CNSmlDMHostSession::MarkGenAlertsSentL
       
   414 //	This function is called only for the FOTA adapter.
       
   415 //	Makes a request for the FOTA adapter to mark generic alerts sent.
       
   416 //	In other words, the written generic alerts are issued without
       
   417 //	errors to the remote server.
       
   418 // ------------------------------------------------------------------------------------------------
       
   419 //
       
   420 void CNSmlDMHostSession::MarkGenAlertsSentL()
       
   421 	{
       
   422 	_DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(): begin");
       
   423 	CSmlDmAdapter* adapter = NULL;
       
   424 	TBool adapterFound ( EFalse );
       
   425 	for ( TInt i(0); i<iAdapters.Count(); i++ )
       
   426 		{
       
   427 		if ( iAdapters[i].adapterId == KNSmlDMFotaAdapterImplUid )
       
   428 			{
       
   429 			adapterFound = ETrue;
       
   430 			if ( !iAdapters[i].adapterPtr )
       
   431 				{
       
   432 				adapter = LoadNewAdapterL ( i, KNSmlDMFotaAdapterImplUid );
       
   433 				if ( !adapter )
       
   434 					{
       
   435 					User::Leave ( KErrArgument );
       
   436 					}
       
   437 				}
       
   438 			// Call overloaded function of the FOTA adapter to request
       
   439 			// the FOTA adapter to mark Generic Alerts sent.
       
   440 			TInt resetGenAlerts ( KNSmlDMResetGenAlerts );
       
   441 			iAdapters[i].adapterPtr->StreamingSupport ( resetGenAlerts );
       
   442 			}
       
   443 		else if ( iAdapters[i].adapterId == KNSmlDMAMAdapterImplUid )
       
   444 			{
       
   445 			adapterFound = ETrue;
       
   446 			if ( !iAdapters[i].adapterPtr )
       
   447 				{
       
   448 				adapter = LoadNewAdapterL ( i, KNSmlDMAMAdapterImplUid );
       
   449 				if ( !adapter )
       
   450 					{
       
   451 					User::Leave ( KErrArgument );
       
   452 					}
       
   453 				}
       
   454 			// Call overloaded function of the AM adapter to request
       
   455 			// the AM adapter to mark Generic Alerts sent.
       
   456 			TInt resetGenAlerts ( KNSmlDMResetGenAlerts );
       
   457 			iAdapters[i].adapterPtr->StreamingSupport ( resetGenAlerts );
       
   458 			}
       
   459 		
       
   460 			
       
   461 		}
       
   462 	if ( !adapterFound )
       
   463 	    {
       
   464 		User::Leave ( KErrNotFound );	    
       
   465 	    }
       
   466 	_DBG_FILE("CNSmlDMHostSession::MarkGenAlertsSentL(): end");
       
   467 	}
       
   468 // FOTA end
       
   469 
       
   470 // ------------------------------------------------------------------------------------------------
       
   471 // CNSmlDMHostSession::GetDDFStructureL
       
   472 //	Gets DDF structure from the dm adapters.
       
   473 //	aMessage parameter contains a list of adapters which are already loaded.
       
   474 //	Writes the combined DDF structure to the chunk in the WBXML format.
       
   475 //	Returns also a checksum of the DDF structure to the client. 
       
   476 //	@param aMessage. A list of already loaded adapters, and place for checksum.
       
   477 // ------------------------------------------------------------------------------------------------
       
   478 //
       
   479 void CNSmlDMHostSession::GetDDFStructureL ( const RMessage2& aMessage )
       
   480 	{
       
   481 	_DBG_FILE("CNSmlDMHostSession::GetDDFStructureL(): begin");
       
   482 	RImplInfoPtrArray allDmAdapters;
       
   483 	CleanupStack::PushL ( PtrArrCleanupItemRArr ( CImplementationInformation, &allDmAdapters ) );
       
   484 
       
   485 	RArray<TUint32> dontLoadThese;
       
   486 	CleanupClosePushL ( dontLoadThese );
       
   487 	ReadUidsL ( aMessage, dontLoadThese );
       
   488 		
       
   489 	TUid ifUid = { KSmlDMInterfaceUid };
       
   490 	REComSession::ListImplementationsL ( ifUid, allDmAdapters );
       
   491 	RemoveAlreadyLoadedL ( allDmAdapters, dontLoadThese );
       
   492 	CleanupStack::PopAndDestroy();  // dontLoadThese
       
   493 
       
   494 	RPointerArray<HBufC8> versions;
       
   495 	CleanupStack::PushL ( PtrArrCleanupItemRArr ( HBufC8, &versions ) );
       
   496 	RArray<TInt32> uids;
       
   497 	CleanupClosePushL ( uids );
       
   498 	
       
   499 	CSmlDmAdapter* adapter = NULL;
       
   500 	for ( TInt i=0; i<allDmAdapters.Count(); i++ )
       
   501 		{
       
   502 		TRAPD ( err, ( adapter = CSmlDmAdapter::NewL ( allDmAdapters[i]->ImplementationUid(), *this ) ) );
       
   503 		if ( err == KErrNone && adapter )
       
   504 			{
       
   505 			CleanupStack::PushL(adapter);
       
   506 			AskInfoFromAdapterL ( adapter, versions, uids, allDmAdapters[i]->ImplementationUid().iUid );		
       
   507 			CleanupStack::PopAndDestroy(); //adapter
       
   508 			}
       
   509 		} 
       
   510 	TUint16 crc = CalcCheckSumL ( versions, uids );
       
   511 	CleanupStack::PopAndDestroy(3); // uids, versions, allDmAdapters
       
   512 	WriteTreeToChunkL();
       
   513 	aMessage.WriteL ( 2, TPckgBuf<TInt> (crc) );
       
   514 	TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
   515 	aMessage.WriteL ( 3, cb );	
       
   516 	_DBG_FILE("CNSmlDMHostSession::GetDDFStructureL(): end");
       
   517 	}
       
   518 
       
   519 
       
   520 // ------------------------------------------------------------------------------------------------
       
   521 // CNSmlDMHostSession::ReadUidsL
       
   522 //	Auxiliary function called by GetDDFStructureL.
       
   523 //	Reads the UIDs of the loaded adapters from the message, 
       
   524 //	and stores those to internal list. 
       
   525 //	@param aMessage. An object which encapsulates a client request.
       
   526 //	@param aUids. A reference parameter, a place for extracted list of UIDs.
       
   527 // ------------------------------------------------------------------------------------------------
       
   528 //
       
   529 void CNSmlDMHostSession::ReadUidsL ( const RMessage2& aMessage, RArray<TUint32>& aUids ) const
       
   530 	{
       
   531 	_DBG_FILE("CNSmlDMHostSession::ReadUidsL(): begin");
       
   532 	HBufC8* buffer = HBufC8::NewLC ( aMessage.GetDesLengthL(0) );
       
   533     TPtr8 bufPtr = buffer->Des();
       
   534     aMessage.ReadL ( 0, bufPtr, 0 );
       
   535     
       
   536     RDesReadStream readStream;
       
   537 	readStream.Open ( *buffer );
       
   538 	CleanupClosePushL ( readStream );
       
   539 	TUint8 nOfuids = readStream.ReadUint8L();
       
   540     	
       
   541 	for ( TInt i=0; i<nOfuids; i++ )
       
   542 		{
       
   543 		TUint32 uidTemp = readStream.ReadUint32L();
       
   544 		aUids.Append ( uidTemp );		
       
   545 		}
       
   546 	
       
   547 	CleanupStack::PopAndDestroy(2); // readStream, buffer
       
   548 	_DBG_FILE("CNSmlDMHostSession::ReadUidsL(): end");
       
   549 	}
       
   550 
       
   551 // ------------------------------------------------------------------------------------------------
       
   552 // CNSmlDMHostSession::RemoveAlreadyLoadedL
       
   553 //	Auxiliary function called by GetDDFStructureL.
       
   554 //	Gets a list of all the potential dm adapters.
       
   555 //	Removes from it those which are alreay loaded.  
       
   556 //	@param aAllAds. A reference parameter, after this function call 
       
   557 //	                contains the acceptable adapters.
       
   558 //	@param aUids. A list of adapters which are already loaded.
       
   559 // ------------------------------------------------------------------------------------------------
       
   560 //
       
   561 void CNSmlDMHostSession::RemoveAlreadyLoadedL ( RImplInfoPtrArray& aAllAds, RArray<TUint32>& aUids ) const
       
   562 	{
       
   563 	_DBG_FILE("CNSmlDMHostSession::RemoveAlreadyLoadedL(): begin");
       
   564 	for ( TInt i=0; i<aAllAds.Count(); i++ )
       
   565 		{
       
   566 		for ( TInt u=0; u<aUids.Count(); u++ )
       
   567 			{
       
   568 			if ( aUids[u] == (TUint32)aAllAds[i]->ImplementationUid().iUid )
       
   569 				{
       
   570 				delete aAllAds[i];
       
   571 				aAllAds[i] = NULL;
       
   572 				aAllAds.Remove(i);
       
   573 				i--;
       
   574 				break;								
       
   575 				}
       
   576 			}
       
   577 		}
       
   578 	_DBG_FILE("CNSmlDMHostSession::RemoveAlreadyLoadedL(): end");
       
   579 	}
       
   580 
       
   581 // ------------------------------------------------------------------------------------------------
       
   582 // CNSmlDMHostSession::AskInfoFromAdapterL
       
   583 //	Auxiliary function called by GetDDFStructureL.
       
   584 //	Asks initial information from the loaded dm adapter.
       
   585 //	E.g. whether the adapter supports streaming or not.
       
   586 //	@param aAdapter. A pointer to the just loaded dm plug-in adapter. 
       
   587 //	@param aVersions. A list of DDF versions collected from the adapters. 
       
   588 //	@param aUids. A list of adapter UIDs. Note that aVersions and aUids
       
   589 //	              are always ordered to ascending order in order to prevent
       
   590 //	              a checksum to change value if ECom framework gives a list
       
   591 //	    	      of potential plug-in adapters in different order.
       
   592 //	@param aAdapterId. Identifier of the dm plug-in adapter.
       
   593 // ------------------------------------------------------------------------------------------------
       
   594 //
       
   595 void CNSmlDMHostSession::AskInfoFromAdapterL ( CSmlDmAdapter* aAdapter, RPointerArray<HBufC8>& aVersions, 
       
   596 												RArray<TInt32>& aUids, TInt32 aAdapterId )
       
   597 	{
       
   598 	_DBG_FILE("CNSmlDMHostSession::AskInfoFromAdapterL(): begin");
       
   599 	TNSmlDmAdapter newElement;
       
   600 	newElement.adapterPtr = NULL;
       
   601 	newElement.adapterId = aAdapterId;
       
   602 	newElement.startAtomicCalled = EFalse;
       
   603 	newElement.commandsCalled = EFalse;
       
   604 	TInt limit ( KNSmlDmNotSupported );
       
   605 	newElement.streamingLimit = ( aAdapter->StreamingSupport ( limit ) ) ? limit : KNSmlDmNotSupported;	
       
   606 	iAdapters.Append ( newElement );
       
   607 
       
   608 	CBufBase* version = CBufFlat::NewL(16);
       
   609 	CleanupStack::PushL ( version );
       
   610 	aAdapter->DDFVersionL ( *version );
       
   611 	HBufC8* versionHBufC = HBufC8::NewLC ( version->Size() );
       
   612 	*versionHBufC = version->Ptr(0);
       
   613 	TInt j=0;
       
   614 	// Sort uids and versions to ascending order in arrays
       
   615 	while ( j<aUids.Count() &&  aAdapterId < aUids[j] )  
       
   616 		{
       
   617 		j++;
       
   618 		}
       
   619 	aUids.InsertL ( aAdapterId, j );
       
   620 	aVersions.InsertL ( versionHBufC, j );
       
   621 	CleanupStack::Pop(); //versionHBufC
       
   622 	CleanupStack::PopAndDestroy(); //version
       
   623 		
       
   624 	aAdapter->DDFStructureL ( *iMgmtTree );
       
   625 	iMgmtTree->SetAdapterUid ( aAdapterId );
       
   626 
       
   627 	_DBG_FILE("CNSmlDMHostSession::AskInfoFromAdapterL(): end");
       
   628 	}
       
   629 
       
   630 // ------------------------------------------------------------------------------------------------
       
   631 // CNSmlDMHostSession::WriteTreeToChunkL
       
   632 //	Auxiliary function called by GetDDFStructureL.
       
   633 //	Writes a combined DDF stucture to the chunk using DM Utils services.
       
   634 // ------------------------------------------------------------------------------------------------
       
   635 //
       
   636 void CNSmlDMHostSession::WriteTreeToChunkL()
       
   637 	{
       
   638 	_DBG_FILE("CNSmlDMHostSession::WriteTreeToChunkL(): begin");
       
   639 	CBufBase* buffer = CBufFlat::NewL ( 64 );
       
   640 	CleanupStack::PushL ( buffer );
       
   641 
       
   642 	RBufWriteStream writeStream ( *buffer );
       
   643 	CleanupClosePushL ( writeStream );
       
   644 	iMgmtTree->GenerateWBXMLL ( writeStream );
       
   645 	CleanupStack::PopAndDestroy();  // writeStream
       
   646 
       
   647 	RBufReadStream readStream ( *buffer );
       
   648 	CleanupClosePushL ( readStream );
       
   649 	iChunk.Adjust ( buffer->Size() );
       
   650 
       
   651 	RMemWriteStream chunkStream ( iChunk.Base(), iChunk.Size() );
       
   652 	CleanupClosePushL ( chunkStream );
       
   653 	chunkStream.WriteL ( readStream );
       
   654 	CleanupStack::PopAndDestroy(3);  // chunkStream, readStream, buffer
       
   655 	_DBG_FILE("CNSmlDMHostSession::WriteTreeToChunkL(): end");
       
   656 	}
       
   657 
       
   658 
       
   659 // ------------------------------------------------------------------------------------------------
       
   660 // CNSmlDMHostSession::CalcCheckSumL
       
   661 //	Auxiliary function called by GetDDFStructureL.
       
   662 //	Calculates a CCITT CRC checksum from the collected DDF versions and adapter UIDs 
       
   663 //	@param aVersions. A collected list from adapters containing versions of the DDF structure. 
       
   664 //	@param aUids. A list of adapter UIDs. 
       
   665 //	@return TUint16. Calculated checksum value.
       
   666 // ------------------------------------------------------------------------------------------------
       
   667 //
       
   668 TUint16 CNSmlDMHostSession::CalcCheckSumL ( RPointerArray<HBufC8>& aVersions, RArray<TInt32>& aUids ) const
       
   669 	{
       
   670 	_DBG_FILE("CNSmlDMHostSession::CalcCheckSumL(): begin");
       
   671 	TUint16 crc(0);	
       
   672 	CBufBase* checkData = CBufFlat::NewL(32);
       
   673 	CleanupStack::PushL(checkData);
       
   674 	for ( TInt i(0); i<aUids.Count(); i++ )
       
   675 		{
       
   676 		checkData->InsertL ( checkData->Size(), aVersions[i]->Des() );
       
   677 		checkData->InsertL ( checkData->Size(), &aUids[i], sizeof ( aUids[i] ) );
       
   678 		}
       
   679 	Mem::Crc ( crc, checkData->Ptr(0).Ptr(), checkData->Size() );
       
   680 	CleanupStack::PopAndDestroy(); //checkData
       
   681 	_DBG_FILE("CNSmlDMHostSession::CalcCheckSumL(): end");
       
   682 	return crc;
       
   683 	}
       
   684 
       
   685 // ------------------------------------------------------------------------------------------------
       
   686 // CNSmlDMHostSession::UpdateLeafOrExecuteL
       
   687 //	Common function for handling UpdateLeafObject and Execute DM commands.
       
   688 //	Reads parameters from the chunk.
       
   689 //	Loads the requested adapter if this is a first command to it in this session.
       
   690 //	Checks the need to inform adapter at atomic command situation.
       
   691 //	Decides whether or not use streaming for passing data to dm plug-in adapter.
       
   692 //	Informs the client (DM Tree Module) if there are unread statuses, results or mappings.
       
   693 //	@param aMessage. An object which encapsulates a client request. 
       
   694 //	@param aCommand. Needed to choose between Update and Execute commands. 
       
   695 // ------------------------------------------------------------------------------------------------
       
   696 //
       
   697 void CNSmlDMHostSession::UpdateLeafOrExecuteL ( const RMessage2& aMessage, TNSmlDmHostOperationCodes aCommand )
       
   698 	{
       
   699 	_DBG_FILE("CNSmlDMHostSession::UpdateLeafOrExecuteL(): begin");
       
   700 	ReadCmdParamsL ( aMessage );
       
   701 	CSmlDmAdapter* adapter = NULL;
       
   702 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
   703 		{
       
   704 		if ( iAdapters[i].adapterId == iAdapterId )
       
   705 			{
       
   706 			if ( !iAdapters[i].adapterPtr )
       
   707 				{
       
   708 				adapter = LoadNewAdapterL ( i, iAdapterId );
       
   709 				if ( !adapter )
       
   710 					{
       
   711 					User::Leave ( KErrArgument );					
       
   712 					}
       
   713 				}
       
   714 			if ( iStartAtomic && !iAdapters[i].startAtomicCalled )
       
   715 				{
       
   716 				iAdapters[i].startAtomicCalled = ETrue;
       
   717 				iAdapters[i].adapterPtr->StartAtomicL();
       
   718 				}				
       
   719 			if ( iAdapters[i].streamingLimit == KNSmlDmNotSupported || iData->Size() < iAdapters[i].streamingLimit )
       
   720 				{
       
   721 				CallAdapterCommandL ( aCommand, i );
       
   722 				}
       
   723 			else 
       
   724 				{
       
   725 				RWriteStream* writeStream = NULL;
       
   726 				// FOTA
       
   727 				if ( iAdapterId == KNSmlDMFotaAdapterImplUid && aCommand == ENSmlDMUpdateLeaf )
       
   728 					{
       
   729 					// Call overloaded function of the FOTA adapter to tell the 
       
   730 					// total size of incoming large object.
       
   731 					TInt sizeOfData ( iData->Size() );
       
   732 					iAdapters[i].adapterPtr->StreamingSupport ( sizeOfData );
       
   733 					if ( sizeOfData == KErrNoMemory )
       
   734 						{
       
   735 						User::Leave ( KErrTooBig );						
       
   736 						}
       
   737 					}
       
   738 				// FOTA end
       
   739 				CallAdapterCommandL ( aCommand, i, writeStream );
       
   740 				if ( !writeStream )
       
   741 					{
       
   742 					User::Leave ( KErrGeneral );						
       
   743 					}
       
   744 				writeStream->WriteL ( *iData );
       
   745 				writeStream->Close();
       
   746 				iAdapters[i].adapterPtr->StreamCommittedL();
       
   747 				}
       
   748 			TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
   749 			aMessage.WriteL ( 3, cb );				
       
   750 			return;
       
   751 			}
       
   752 		}
       
   753 	User::Leave ( KErrArgument );
       
   754 	_DBG_FILE("CNSmlDMHostSession::UpdateLeafOrExecuteL(): end");
       
   755 	}
       
   756 
       
   757 // ------------------------------------------------------------------------------------------------
       
   758 // CNSmlDMHostSession::UpdateLeafOrExecuteLargeL
       
   759 //	Common function for handling UpdateLeafObject and Execute DM commands for large objects.
       
   760 //	Reads parameters from the chunk.
       
   761 //	Loads the requested adapter if this is a first command to it in this session.
       
   762 //	Checks the need to inform adapter at atomic command situation.
       
   763 //	Decides whether or not use streaming for passing data to dm plug-in adapter.
       
   764 //	In case this is last package of the large object, closes and commits stream,
       
   765 //	provided streaming was used for passing a data.
       
   766 //	Informs the client (DM Tree Module) if there are unread statuses, results or mappings.
       
   767 //	@param aMessage. An object which encapsulates a client request. 
       
   768 //	@param aCommand. Needed to choose between Update and Execute commands. 
       
   769 // ------------------------------------------------------------------------------------------------
       
   770 //
       
   771 void CNSmlDMHostSession::UpdateLeafOrExecuteLargeL ( const RMessage2& aMessage, TNSmlDmHostOperationCodes aCommand )
       
   772 	{
       
   773 	_DBG_FILE("CNSmlDMHostSession::UpdateLeafOrExecuteLargeL(): begin");
       
   774 	ReadCmdParamsL ( aMessage );
       
   775 	CSmlDmAdapter* adapter = NULL;
       
   776 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
   777 		{
       
   778 		if ( iAdapters[i].adapterId == iAdapterId )
       
   779 			{
       
   780 			if ( !iAdapters[i].adapterPtr )
       
   781 				{
       
   782 				adapter = LoadNewAdapterL ( i, iAdapterId );
       
   783 				if ( !adapter )
       
   784 					{
       
   785 					User::Leave ( KErrArgument );								
       
   786 					}
       
   787 				}
       
   788 			if ( iStartAtomic && !iAdapters[i].startAtomicCalled )
       
   789 				{
       
   790 				iAdapters[i].startAtomicCalled = ETrue;
       
   791 				iAdapters[i].adapterPtr->StartAtomicL();
       
   792 				}				
       
   793 			if ( iAdapters[i].streamingLimit != KNSmlDmNotSupported )
       
   794 				{
       
   795 				CheckIfCorrectAdapterL();
       
   796 				if ( !iLargeWriteStream.streamPtr )
       
   797 					{
       
   798 					// FOTA
       
   799 					if ( iAdapterId == KNSmlDMFotaAdapterImplUid && aCommand == ENSmlDMUpdateLeafLarge )
       
   800 						{
       
   801 						// Call overloaded function of the FOTA adapter to tell the 
       
   802 						// total size of incoming large object.
       
   803 						// Note that in this context the third parameter holds the size
       
   804 						// information. Otherwise it contains the 'last package' information.
       
   805 						TInt totSizeOfLarge ( aMessage.Int2() );
       
   806 						iAdapters[i].adapterPtr->StreamingSupport ( totSizeOfLarge );
       
   807 						if ( totSizeOfLarge == KErrNoMemory )
       
   808 							{
       
   809 							User::Leave ( KErrTooBig );						
       
   810 							}
       
   811 						}					
       
   812 					// FOTA end
       
   813 					iLargeWriteStream.adapterId = iAdapterId;
       
   814 					CallAdapterCommandL ( aCommand, i, iLargeWriteStream.streamPtr );
       
   815 					if ( !iLargeWriteStream.streamPtr )
       
   816 						{
       
   817 						User::Leave ( KErrGeneral );						
       
   818 						}
       
   819 					iCommitStatus.iStreamingOngoing=ETrue;
       
   820 					iCommitStatus.iOldStatusRef = iStatusRef;
       
   821 					}
       
   822 				iLargeWriteStream.streamPtr->WriteL ( *iData );
       
   823 				iCommitStatus.iNewStatusRef = iStatusRef;
       
   824 				// FOTA
       
   825 				if ( aMessage.Int2() == KNSmlDmLastPackage )  //  Last package (TBool)
       
   826 				// FOTA end
       
   827 					{
       
   828 					iLargeWriteStream.streamPtr->Close();
       
   829 					iLargeWriteStream.streamPtr = NULL;									
       
   830 					iCommitStatus.iStreamCommitted = ETrue;
       
   831 					iAdapters[i].adapterPtr->StreamCommittedL();
       
   832 				    iCommitStatus.iStreamCommitted=EFalse;
       
   833         		    iCommitStatus.iStreamingOngoing=EFalse;
       
   834 					}
       
   835 				TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
   836 				aMessage.WriteL ( 3, cb );
       
   837 				}
       
   838 			else 
       
   839 				{
       
   840 				UpdateOrExecuteLargeNoStreamL ( aMessage, i, aCommand );
       
   841 				}
       
   842 			return;
       
   843 			}
       
   844 		}
       
   845 	User::Leave ( KErrArgument );
       
   846 	_DBG_FILE("CNSmlDMHostSession::UpdateLeafOrExecuteLargeL(): end");
       
   847 	}
       
   848 
       
   849 // ------------------------------------------------------------------------------------------------
       
   850 // CNSmlDMHostSession::UpdateOrExecuteLargeNoStreamL
       
   851 //	Auxiliary function of UpdateLeafOrExecuteLargeL.
       
   852 //	Provided the dm plug-in adapter doesn't support streaming, this function
       
   853 //	takes care of appending large object piece by piece.
       
   854 //	When the last package arrives, calls the adapter and gives the built-up 
       
   855 //	large object as a parameter.
       
   856 //	@param aMessage. An object which encapsulates a client request. 
       
   857 //	@param aAdIndex. Identifies the assigned adapter. 
       
   858 //	@param aCommand. Needed to choose between Update and Execute commands. 
       
   859 // ------------------------------------------------------------------------------------------------
       
   860 //
       
   861 void CNSmlDMHostSession::UpdateOrExecuteLargeNoStreamL ( const RMessage2& aMessage, TInt aAdIndex, TNSmlDmHostOperationCodes aCommand )
       
   862 	{
       
   863 	_DBG_FILE("CNSmlDMHostSession::UpdateOrExecuteLargeNoStreamL(): begin");
       
   864 
       
   865 	if ( !iLargeObject )
       
   866 		{
       
   867 		iLargeObject = HBufC8::NewL(0);
       
   868 		}
       
   869 	iLargeObject = iLargeObject->ReAllocL ( iLargeObject->Length() + iData->Length() );
       
   870 	TPtr8 tmpPtr = iLargeObject->Des();	
       
   871 	tmpPtr.Append ( *iData );
       
   872 	
       
   873 	if ( aMessage.Int2() )  //  Last package (TBool)
       
   874 		{					
       
   875 		CallAdapterCommandL ( aCommand, aAdIndex, ETrue );
       
   876 		delete iLargeObject;
       
   877 		iLargeObject = NULL;
       
   878 		}		
       
   879 	_DBG_FILE("CNSmlDMHostSession::UpdateOrExecuteLargeNoStreamL(): end");
       
   880 	}
       
   881 
       
   882 // ------------------------------------------------------------------------------------------------
       
   883 // CNSmlDMHostSession::AddCopyOrDeleteL
       
   884 //	Common function for handling Add Interior Node, Copy Node, or Delete Object DM commands.
       
   885 //	Reads parameters from the chunk.
       
   886 //	Loads the requested adapter if this is a first command to it in this session.
       
   887 //	Checks the need to inform adapter at atomic command situation.
       
   888 //	Informs the client (DM Tree Module) if there are unread statuses, results or mappings.
       
   889 //	@param aMessage. An object which encapsulates a client request. 
       
   890 //	@param aCommand. Needed to choose between Add, Copy, and Delete commands. 
       
   891 // ------------------------------------------------------------------------------------------------
       
   892 //
       
   893 void CNSmlDMHostSession::AddCopyOrDeleteL ( const RMessage2& aMessage, TNSmlDmHostOperationCodes aCommand )
       
   894 	{
       
   895 	_DBG_FILE("CNSmlDMHostSession::AddCopyOrDeleteL(): begin");
       
   896 	
       
   897 	if ( aCommand == ENSmlDMCopy )
       
   898 		{
       
   899 		ReadCopyCmdParamsL ( aMessage );			
       
   900 		}
       
   901 	else
       
   902 		{
       
   903 		ReadCmdParamsL ( aMessage );		
       
   904 		}
       
   905 	CSmlDmAdapter* adapter = NULL;
       
   906 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
   907 		{
       
   908 		if ( iAdapters[i].adapterId == iAdapterId )
       
   909 			{
       
   910 			if ( !iAdapters[i].adapterPtr )
       
   911 				{
       
   912 				adapter = LoadNewAdapterL ( i, iAdapterId );
       
   913 				if ( !adapter )
       
   914 					{
       
   915 					User::Leave ( KErrArgument );					
       
   916 					}
       
   917 				}
       
   918 			if ( iStartAtomic && !iAdapters[i].startAtomicCalled )
       
   919 				{
       
   920 				iAdapters[i].startAtomicCalled = ETrue;
       
   921 				iAdapters[i].adapterPtr->StartAtomicL();
       
   922 				}				
       
   923 			CallAdapterCommandL ( aCommand, i );
       
   924 			TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
   925 			aMessage.WriteL ( 3, cb );		
       
   926 			return;
       
   927 			}
       
   928 		}
       
   929 	User::Leave ( KErrArgument );
       
   930 	_DBG_FILE("CNSmlDMHostSession::AddCopyOrDeleteL(): end");
       
   931 	}
       
   932 
       
   933 // ------------------------------------------------------------------------------------------------
       
   934 // CNSmlDMHostSession::FetchLeafChildUrisOrSizeL
       
   935 //	Common function for handling Fetch Leaf Object, Get Child URI List, or Fetch Leaf Object Size 
       
   936 //	DM commands.
       
   937 //	Reads parameters from the chunk.
       
   938 //	Loads the requested adapter if this is a first command to it in this session.
       
   939 //	Checks the need to inform adapter at atomic command situation.
       
   940 //	Informs the client (DM Tree Module) if there are unread statuses, results or mappings.
       
   941 //	@param aMessage. An object which encapsulates a client request. 
       
   942 //	@param aCommand. Needed to choose between Fetch, Get Child URI List, and Fetch Size commands. 
       
   943 // ------------------------------------------------------------------------------------------------
       
   944 //
       
   945 void CNSmlDMHostSession::FetchLeafChildUrisOrSizeL ( const RMessage2& aMessage,  TNSmlDmHostOperationCodes aCommand )
       
   946 	{
       
   947 	_DBG_FILE("CNSmlDMHostSession::FetchLeafChildUrisOrSizeL(): begin");
       
   948 	RPointerArray<HBufC8> prevLuids;
       
   949 	CleanupStack::PushL ( PtrArrCleanupItemRArr ( HBufC8, &prevLuids ) );
       
   950 	ReadCmdFetchParamsL ( aMessage, prevLuids );
       
   951 	CSmlDmAdapter* adapter = NULL;
       
   952 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
   953 		{
       
   954 		if ( iAdapters[i].adapterId == iAdapterId )
       
   955 			{
       
   956 			if ( !iAdapters[i].adapterPtr )
       
   957 				{
       
   958 				adapter = LoadNewAdapterL ( i, iAdapterId );
       
   959 				if ( !adapter )
       
   960 					{
       
   961 					CleanupStack::PopAndDestroy();  // prevLuids
       
   962 					User::Leave ( KErrArgument );
       
   963 					}
       
   964 				}
       
   965 			if ( iStartAtomic && !iAdapters[i].startAtomicCalled )
       
   966 				{
       
   967 				iAdapters[i].startAtomicCalled = ETrue;
       
   968 				iAdapters[i].adapterPtr->StartAtomicL();
       
   969 				}
       
   970 			CallAdapterCommandL ( aCommand, i );
       
   971 			CleanupStack::PopAndDestroy();  // prevLuids					
       
   972 			TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
   973 			aMessage.WriteL ( 3, cb );
       
   974 			return;
       
   975 			}
       
   976 		}
       
   977 	CleanupStack::PopAndDestroy();  // prevLuids			
       
   978 	User::Leave ( KErrNotFound );
       
   979 	_DBG_FILE("CNSmlDMHostSession::FetchLeafChildUrisOrSizeL(): end");
       
   980 	}
       
   981 
       
   982 // ------------------------------------------------------------------------------------------------
       
   983 // CNSmlDMHostSession::FetchLinkL
       
   984 //	Called only from other DM Host Servers when they need to cross-reference AP -adapter. 
       
   985 //	Loads the AP dm plug in adapter (i.e. Internet adapter).
       
   986 //	Asks the DDF structure from the AP adapter.
       
   987 //	Checks that the given URI is found from the DDF structure.
       
   988 //	If the above actions are successful, calls the Fetch Leaf Object or Get Child URI List 
       
   989 //	commands of AP adapter.
       
   990 //	Writes the returned result to the chunk.
       
   991 //	@param aMessage. An object which encapsulates a client (other DM Host Server in this case) request. 
       
   992 // ------------------------------------------------------------------------------------------------
       
   993 //
       
   994 void CNSmlDMHostSession::FetchLinkL ( const RMessage2& aMessage )
       
   995 	{
       
   996 	_DBG_FILE("CNSmlDMHostSession::FetchLinkL(): begin");
       
   997 	
       
   998 	RPointerArray<HBufC8> versions;
       
   999 	CleanupStack::PushL ( PtrArrCleanupItemRArr ( HBufC8, &versions ) );
       
  1000 	RArray<TInt32> uids;
       
  1001 	CleanupClosePushL ( uids );
       
  1002 	
       
  1003 	CSmlDmAdapter* adapter = NULL;
       
  1004 	TUid uid = { GetAccessPointImplUid() };
       
  1005 	
       
  1006 	adapter = CSmlDmAdapter::NewL ( uid, *this );
       
  1007 	CleanupStack::PushL(adapter);
       
  1008 	AskInfoFromAdapterL ( adapter, versions, uids, uid.iUid );		
       
  1009 	CleanupStack::PopAndDestroy(3); // adapter, uids, versions 
       
  1010 	
       
  1011 	HBufC8* uri = HBufC8::NewLC ( aMessage.GetDesLengthL(0) );
       
  1012     TPtr8 uriPtr = uri->Des();
       
  1013     aMessage.ReadL ( 0, uriPtr, 0 );    
       
  1014 	
       
  1015 	CBufBase* buf = CBufFlat::NewL ( 16 );
       
  1016 	CleanupStack::PushL ( buf );
       
  1017 	MSmlDmAdapter::TError status = MSmlDmAdapter::EOk;
       
  1018 
       
  1019 	TUint32 adId(0);
       
  1020 	TNSmlDmDDFFormat nodeType;
       
  1021 	TBool adapterFound = AdapterIdFromUriL ( *uri, adId, nodeType );
       
  1022 	if ( adapterFound )
       
  1023 		{
       
  1024 		FetchLinkFromAdapterL ( *uri, adId, *buf, status, nodeType );
       
  1025 		}
       
  1026 	if ( !adapterFound || status != MSmlDmAdapter::EOk )
       
  1027 		{
       
  1028 		CleanupStack::PopAndDestroy(2); // buf, uri	
       
  1029 		User::Leave ( KErrNotFound );
       
  1030 		}
       
  1031 	
       
  1032 	// Write answer
       
  1033 	iChunk.Adjust ( 2 + buf->Size() );
       
  1034 	RMemWriteStream writeStream ( iChunk.Base(), iChunk.Size() );
       
  1035 	CleanupClosePushL ( writeStream );
       
  1036 	writeStream.WriteUint16L ( buf->Size() );
       
  1037 	writeStream.WriteL ( buf->Ptr(0) );
       
  1038 	CleanupStack::PopAndDestroy(3); // writestream, buf, uri
       
  1039 	_DBG_FILE("CNSmlDMHostSession::FetchLinkL(): end");
       
  1040 	}
       
  1041 
       
  1042 // ------------------------------------------------------------------------------------------------
       
  1043 // void CNSmlDMHostSession::StartAtomicL()
       
  1044 //	Sets a general atomic command state. 
       
  1045 //	When this state is set, StartAtomicL function of the adapters
       
  1046 //	is called when the first 'normal' command arrives.
       
  1047 //	In addition informs the client (DM Tree Module) if there are unread statuses, results or mappings.
       
  1048 //	@param aMessage. An object which encapsulates a client request. 
       
  1049 // ------------------------------------------------------------------------------------------------
       
  1050 //
       
  1051 void CNSmlDMHostSession::StartAtomicL ( const RMessage2& aMessage )
       
  1052 	{
       
  1053 	_DBG_FILE("CNSmlDMHostSession::StartAtomicL(): begin");	
       
  1054 	iStartAtomic = ETrue;
       
  1055 	TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
  1056 	aMessage.WriteL ( 3, cb );
       
  1057 	_DBG_FILE("CNSmlDMHostSession::StartAtomicL(): end");
       
  1058 	}
       
  1059 
       
  1060 // ------------------------------------------------------------------------------------------------
       
  1061 // void CNSmlDMHostSession::CommitAtomicL()
       
  1062 //	Resets the general atomic command state (When atomic commands were successful). 
       
  1063 //	This command is forwarded only to those adapters which have received StartAtomicL call.
       
  1064 //	In addition informs the client (DM Tree Module) if there are unread statuses, results or mappings.
       
  1065 //	@param aMessage. An object which encapsulates a client request. 
       
  1066 // ------------------------------------------------------------------------------------------------
       
  1067 //
       
  1068 void CNSmlDMHostSession::CommitAtomicL ( const RMessage2& aMessage )
       
  1069 	{
       
  1070 	_DBG_FILE("CNSmlDMHostSession::CommitAtomicL(): begin");
       
  1071 	iStartAtomic = EFalse;
       
  1072 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
  1073 		{
       
  1074 		if ( iAdapters[i].startAtomicCalled && iAdapters[i].adapterPtr )
       
  1075 			{
       
  1076 			iAdapters[i].startAtomicCalled = EFalse;	
       
  1077 			iAdapters[i].adapterPtr->CommitAtomicL();	
       
  1078 			}
       
  1079 		}
       
  1080 	TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
  1081 	aMessage.WriteL ( 3, cb );		
       
  1082 	_DBG_FILE("CNSmlDMHostSession::CommitAtomicL(): end");
       
  1083 	}
       
  1084 
       
  1085 // ------------------------------------------------------------------------------------------------
       
  1086 // void CNSmlDMHostSession::RollbackAtomicL()
       
  1087 //	Resets the general atomic command state (When one or more atomic commands failed). 
       
  1088 //	This command is forwarded only to those adapters which have received StartAtomicL call.
       
  1089 //	In addition informs the client (DM Tree Module) if there are unread statuses, results or mappings.
       
  1090 //	@param aMessage. An object which encapsulates a client request. 
       
  1091 // ------------------------------------------------------------------------------------------------
       
  1092 //
       
  1093 void CNSmlDMHostSession::RollbackAtomicL ( const RMessage2& aMessage )
       
  1094 	{
       
  1095 	_DBG_FILE("CNSmlDMHostSession::RollbackAtomicL(): begin");
       
  1096 	iStartAtomic = EFalse;	
       
  1097 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
  1098 		{
       
  1099 		if ( iAdapters[i].startAtomicCalled && iAdapters[i].adapterPtr )
       
  1100 			{
       
  1101 			iAdapters[i].startAtomicCalled = EFalse;	
       
  1102 			iAdapters[i].adapterPtr->RollbackAtomicL();	
       
  1103 			}
       
  1104 		}
       
  1105 	TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
  1106 	aMessage.WriteL ( 3, cb );		
       
  1107 	_DBG_FILE("CNSmlDMHostSession::RollbackAtomicL(): end");
       
  1108 	}
       
  1109 
       
  1110 // ------------------------------------------------------------------------------------------------
       
  1111 // void CNSmlDMHostSession::CompleteCommandsL()
       
  1112 //	Calls CompleteOutstandingCmdsL -function of the DM plug-in adapters.
       
  1113 //	This command is issued only to those adapters which have received some adapter 
       
  1114 //	commands during the session.
       
  1115 //	In addition informs the client (DM Tree Module) if there are unread statuses, results or mappings.
       
  1116 //	@param aMessage. An object which encapsulates a client request. 
       
  1117 // ------------------------------------------------------------------------------------------------
       
  1118 //
       
  1119 void CNSmlDMHostSession::CompleteCommandsL ( const RMessage2& aMessage )
       
  1120 	{	
       
  1121 	_DBG_FILE("CNSmlDMHostSession::CompleteCommandsL(): begin");
       
  1122 	for ( TInt i=0; i<iAdapters.Count(); i++ )
       
  1123 		{
       
  1124 		if ( iAdapters[i].commandsCalled && iAdapters[i].adapterPtr )
       
  1125 			{
       
  1126 			iAdapters[i].commandsCalled = EFalse;	
       
  1127 			iAdapters[i].adapterPtr->CompleteOutstandingCmdsL();
       
  1128 			}
       
  1129 		}
       
  1130 	TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
  1131 	aMessage.WriteL ( 3, cb );		
       
  1132 	_DBG_FILE("CNSmlDMHostSession::CompleteCommandsL(): end");
       
  1133 	}
       
  1134 
       
  1135 // ------------------------------------------------------------------------------------------------
       
  1136 // CNSmlDMHostSession::SetChunkHandleL
       
  1137 //	Sets a handle to the global memory chunk. 
       
  1138 //	The chunk is a common data area between the client (DM Tree Module) and the server
       
  1139 //	(DM Host Server).
       
  1140 //	Additionally, a server id is sent in the message.
       
  1141 //	This id tells to the (common) session class which server session is serving.  
       
  1142 //	@param aMessage. An object which encapsulates a client request. 
       
  1143 // ------------------------------------------------------------------------------------------------
       
  1144 //
       
  1145 void CNSmlDMHostSession::SetChunkHandleL ( const RMessage2& aMessage )
       
  1146 	{
       
  1147 	_DBG_FILE("CNSmlDMHostSession::SetChunkHandleL(): begin");
       
  1148 	iOwnId = aMessage.Int1();
       
  1149 	iChunk.Close();
       
  1150 	TInt err=iChunk.Open ( aMessage, 0, EFalse );	
       
  1151 	if(err != KErrNone)
       
  1152 	{
       
  1153 		User::Leave (err);		
       
  1154 	}
       
  1155 	if ( iChunk.MaxSize() <= KNSmlDmChunkMarginal )
       
  1156 		{
       
  1157 		User::Leave ( KErrGeneral );		
       
  1158 		}
       
  1159 	_DBG_FILE("CNSmlDMHostSession::SetChunkHandleL(): end");
       
  1160 	}
       
  1161 
       
  1162 // ------------------------------------------------------------------------------------------------
       
  1163 // CNSmlDMHostSession::GetMappingsL
       
  1164 //	Gets the URI / Luid mappings which have been received from the dm adapters 
       
  1165 //	via callback function calls. 
       
  1166 //	These mappings are buffered by the session class, and - during this call,
       
  1167 //	all written to the chunk at once.
       
  1168 //	Additionally informs the client (DM Tree Module) if there are also unread statuses or results.
       
  1169 //	@param aMessage. An object which encapsulates a client request. 
       
  1170 // ------------------------------------------------------------------------------------------------
       
  1171 //
       
  1172 void CNSmlDMHostSession::GetMappingsL ( const RMessage2& aMessage )
       
  1173 	{
       
  1174 	_DBG_FILE("CNSmlDMHostSession::GetMappingsL(): begin");
       
  1175 	if ( !iMapUris || !iMapLuids )
       
  1176 		{
       
  1177 		User::Leave ( KErrNotFound );		
       
  1178 		}
       
  1179 	
       
  1180 	iChunk.Adjust ( SizeOfMappings() );
       
  1181 	RMemWriteStream writeStream ( iChunk.Base(), iChunk.Size() );
       
  1182 	CleanupClosePushL ( writeStream );
       
  1183 	NSmlDmCmdArgHandler::PacketMappingsL ( writeStream, *iMapUris, *iMapLuids );			
       
  1184 	
       
  1185 	iMapUris->ResetAndDestroy();
       
  1186 	delete iMapUris;
       
  1187 	iMapUris = NULL;
       
  1188 	iMapLuids->ResetAndDestroy();
       
  1189 	delete iMapLuids;
       
  1190 	iMapLuids = NULL;
       
  1191 	for ( TInt i=0; i<iCallbackList.Count(); i++ )
       
  1192 		{
       
  1193 		if ( iCallbackList[i].callBack == ENSmlDmCallbackMappings )
       
  1194 			{
       
  1195 			iCallbackList.Remove(i);
       
  1196 			i--;				
       
  1197 			}
       
  1198 		}
       
  1199 	TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
  1200 	aMessage.WriteL ( 3, cb );	
       
  1201 	CleanupStack::PopAndDestroy();  // writeStream	
       
  1202 	_DBG_FILE("CNSmlDMHostSession::GetMappingsL(): end");
       
  1203 	}
       
  1204 
       
  1205 // ------------------------------------------------------------------------------------------------
       
  1206 // CNSmlDMHostSession::GetStatusesL
       
  1207 //	Gets the DM command statuses received from the dm adapters via callback function calls. 
       
  1208 //	These statuse are buffered by the session class, and - during this call,
       
  1209 //	all written to the chunk at once.
       
  1210 //	Additionally informs the client (DM Tree Module) if there are also unread mappings or results.
       
  1211 //	@param aMessage. An object which encapsulates a client request. 
       
  1212 // ------------------------------------------------------------------------------------------------
       
  1213 //
       
  1214 void CNSmlDMHostSession::GetStatusesL ( const RMessage2& aMessage )
       
  1215 	{
       
  1216 	_DBG_FILE("CNSmlDMHostSession::GetStatusesL(): begin");
       
  1217 	if ( !iStatusCodeList.Count() )
       
  1218 		{
       
  1219 		User::Leave ( KErrNotFound );		
       
  1220 		}
       
  1221 
       
  1222 	iChunk.Adjust ( SizeOfStatuses() );
       
  1223 	RMemWriteStream writeStream ( iChunk.Base(), iChunk.Size() );
       
  1224 	CleanupClosePushL ( writeStream );
       
  1225 	NSmlDmCmdArgHandler::PacketStatusesL ( writeStream, iStatusCodeList );			
       
  1226 	
       
  1227 	iStatusCodeList.Close();
       
  1228 	
       
  1229 	for ( TInt i=0; i<iCallbackList.Count(); i++ )
       
  1230 		{
       
  1231 		if ( iCallbackList[i].callBack == ENSmlDmCallbackStatuses )
       
  1232 			{
       
  1233 			iCallbackList.Remove(i);
       
  1234 			i--;			
       
  1235 			}
       
  1236 		}
       
  1237 	
       
  1238 	TPckgBuf<TInt> cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;
       
  1239 	aMessage.WriteL ( 3, cb );	
       
  1240 	CleanupStack::PopAndDestroy();  // writeStream
       
  1241 	_DBG_FILE("CNSmlDMHostSession::GetStatusesL(): end");
       
  1242 	}
       
  1243 
       
  1244 // ------------------------------------------------------------------------------------------------
       
  1245 // CNSmlDMHostSession::GetResultsL
       
  1246 //	Gets the DM command results received from the dm adapters via callback function calls.
       
  1247 //	In practice, these are the results from the 'FetchLeafObject', 'FecthLeafObjectSize', 
       
  1248 //	and 'GetChildURIList' commands.
       
  1249 //	The results are all written to the chunk at once if they just fit there.
       
  1250 //	In case of large objects the result item is cut into pieces by writing one 'chunkfull' at time.
       
  1251 //	Informs the client (DM Tree Module) if the result is cut up and needs thus additional 
       
  1252 //	GetResultsL -function calls. Additionally, informs the client if there are unread mappings 
       
  1253 //	or statuses.
       
  1254 //	@param aMessage. An object which encapsulates a client request. 
       
  1255 // ------------------------------------------------------------------------------------------------
       
  1256 //
       
  1257 void CNSmlDMHostSession::GetResultsL ( const RMessage2& aMessage )
       
  1258 	{
       
  1259 	_DBG_FILE("CNSmlDMHostSession::GetResultsL(): begin");
       
  1260 	TInt okToWrite(0);
       
  1261 	TInt adjustChunkSize(0);	
       
  1262 	TInt largeTotSize(0);
       
  1263 	TNSmlDmResultAnalysis conclusion = AnalyzeResultList ( okToWrite, adjustChunkSize );
       
  1264 	
       
  1265 	iChunk.Adjust ( adjustChunkSize );
       
  1266 	RMemWriteStream writeStream ( iChunk.Base(), iChunk.Size() );
       
  1267 	CleanupClosePushL ( writeStream );
       
  1268 	
       
  1269 	if ( conclusion == ENSmlDmAllWillFit )
       
  1270 		{
       
  1271 		NSmlDmCmdArgHandler::PacketResultsL ( writeStream, iResultList );			
       
  1272 		RemoveResultCallbacksL ( iResultList.Count() );
       
  1273 		}
       
  1274 	else if ( conclusion == ENSmlDmSomeWillFit )
       
  1275 		{
       
  1276 		RPointerArray<CNSmlDmResultElement> someResults;
       
  1277 		CleanupClosePushL ( someResults );
       
  1278 		for ( TInt i(0); i<okToWrite; i++ )
       
  1279 			{
       
  1280 			someResults.Append ( iResultList[i] );			
       
  1281 			}
       
  1282 		NSmlDmCmdArgHandler::PacketResultsL ( writeStream, someResults );
       
  1283 		CleanupStack::PopAndDestroy();  // someResults
       
  1284 		RemoveResultCallbacksL ( okToWrite );
       
  1285 		}
       
  1286 	else if ( conclusion == ENSmlDmStreamed )
       
  1287 		{
       
  1288 		if ( !iResultList[0]->iStreamed )
       
  1289 			{
       
  1290 			User::Leave ( KErrGeneral );			
       
  1291 			}
       
  1292 		
       
  1293 		RPointerArray<CNSmlDmResultElement> result;
       
  1294 		CleanupClosePushL ( result );
       
  1295 		result.Append ( iResultList[0] );			
       
  1296 		NSmlDmCmdArgHandler::PacketResultsL ( writeStream, result );
       
  1297 
       
  1298 		largeTotSize = iResultReadStream->Source()->SizeL();
       
  1299 		TInt leftToRead = largeTotSize - iResultOffset;
       
  1300 		TInt available = RoomForLargeData ( *iResultList[0]->iMimeType );
       
  1301 		delete iResultList[0]->iResult;
       
  1302 		iResultList[0]->iResult = NULL;
       
  1303 
       
  1304 		RBufWriteStream writeBufStream;
       
  1305 		CleanupClosePushL ( writeBufStream );
       
  1306 		if ( leftToRead > available )
       
  1307 			{
       
  1308 			iResultList[0]->iResult = CBufFlat::NewL ( available );
       
  1309 			writeBufStream.Open ( *iResultList[0]->iResult );
       
  1310 			iResultReadStream->ReadL ( writeBufStream, available );
       
  1311 			iResultOffset += available;
       
  1312 			}
       
  1313 		else
       
  1314 			{
       
  1315 			iResultList[0]->iResult = CBufFlat::NewL ( leftToRead );
       
  1316 			writeBufStream.Open ( *iResultList[0]->iResult );
       
  1317 			iResultReadStream->ReadL ( writeBufStream, leftToRead );
       
  1318 			iResultReadStream->Close();
       
  1319 			iResultList[0]->iStreamed = EFalse;
       
  1320 			iResultReadStream = NULL;
       
  1321 			iResultOffset = 0;
       
  1322 			}
       
  1323 		CleanupStack::PopAndDestroy ( 2 );  // writeBufStream, result
       
  1324 		}
       
  1325 	else if ( conclusion == ENSmlDmLargeDoesntFit )
       
  1326 		{
       
  1327 		largeTotSize = iResultList[0]->iResult->Size();
       
  1328 		TInt available = iChunk.Size() - KNSmlDmChunkMarginal;
       
  1329 		CBufBase* newBuffer = CBufFlat::NewL ( available );
       
  1330 		CleanupStack::PushL ( newBuffer );
       
  1331 		newBuffer->InsertL ( 0, iResultList[0]->iResult->Ptr(0).Left ( available ) );
       
  1332 		
       
  1333 		CBufBase* oldBuffer = iResultList[0]->iResult;
       
  1334 		iResultList[0]->iResult = newBuffer;
       
  1335 				
       
  1336 		RPointerArray<CNSmlDmResultElement> truncResult;
       
  1337 		CleanupClosePushL ( truncResult );
       
  1338 		truncResult.Append ( iResultList[0] );			
       
  1339 		NSmlDmCmdArgHandler::PacketResultsL ( writeStream, truncResult );
       
  1340 		
       
  1341 		CleanupStack::PopAndDestroy(2);  // truncResult, newBuffer
       
  1342 		oldBuffer->Delete ( 0, available );
       
  1343 		iResultList[0]->iResult = oldBuffer;
       
  1344 		}
       
  1345 	
       
  1346 	CleanupStack::PopAndDestroy();  // writeStream
       
  1347 	TPckgBuf<TInt> cb;
       
  1348 	if ( conclusion == ENSmlDmStreamed || conclusion == ENSmlDmLargeDoesntFit )
       
  1349 		{
       
  1350 		cb = ENSmlDmCallbackChunkFull;
       
  1351 		TPckgBuf<TInt> totalSize ( largeTotSize );
       
  1352 		aMessage.Write ( 2, totalSize );		
       
  1353 		}
       
  1354 	else 
       
  1355 		{
       
  1356 		cb = ( iCallbackList.Count() ) ? iCallbackList[0].callBack : ENSmlDmCallbackNone;		
       
  1357 		}
       
  1358 	aMessage.WriteL ( 3, cb );	
       
  1359 		
       
  1360 	_DBG_FILE("CNSmlDMHostSession::GetResultsL(): end");
       
  1361 	}
       
  1362 
       
  1363 // ------------------------------------------------------------------------------------------------
       
  1364 // CNSmlDMHostSession::AnalyzeResultList
       
  1365 //	Auxiliary function called by GetResultsL.
       
  1366 //	This function analyzes whether all the results will fit into the chunk
       
  1367 //	or not. 
       
  1368 //	@param aOkToWrite. A reference parameter which is used only in case of
       
  1369 //	                  'some will fit' for telling how many results can be
       
  1370 //	                   written at once.
       
  1371 //	@param aAdjustChunkSize. A reference parameter for adjusting the chunk to 
       
  1372 //	    				     adequate size before writing takes place.
       
  1373 //	@return TNSmlDmResultAnalysis. Possible return values are: 1) 'all will fit',
       
  1374 //	                               2) 'some will fit', 3) 'large object (streamed) -
       
  1375 //	     						   won't fit', and 4) 'large object (not streamed)
       
  1376 //	 							   - won't fit'.
       
  1377 // ------------------------------------------------------------------------------------------------
       
  1378 //
       
  1379 CNSmlDMHostSession::TNSmlDmResultAnalysis CNSmlDMHostSession::AnalyzeResultList ( TInt& aOkToWrite, TInt& aAdjustChunk  )
       
  1380 	{
       
  1381 	TInt occupied(2);  // 2 = count 16bits
       
  1382 	for ( TInt i(0); i<iResultList.Count(); i++ )
       
  1383 		{
       
  1384 		occupied += ResultItemSize ( i );
       
  1385 		TInt maximum ( KNSmlDmHostChunkMaxSize - KNSmlDmChunkMarginal );
       
  1386 		if (  occupied <= maximum )
       
  1387 			{
       
  1388 			aOkToWrite++;
       
  1389 			if ( iResultList[i]->iStreamed )
       
  1390 				{
       
  1391 				aAdjustChunk = KNSmlDmHostChunkMaxSize;				
       
  1392 				return ENSmlDmStreamed;
       
  1393 				}
       
  1394 			}
       
  1395 		else 
       
  1396 			{
       
  1397 			if ( aOkToWrite )
       
  1398 				{
       
  1399 				aAdjustChunk = occupied - ResultItemSize(i);				
       
  1400 				return ENSmlDmSomeWillFit;
       
  1401 				}
       
  1402 			else
       
  1403 				{
       
  1404 				aAdjustChunk = KNSmlDmHostChunkMaxSize;				
       
  1405 				return ENSmlDmLargeDoesntFit;			
       
  1406 				}
       
  1407 			}
       
  1408 		}
       
  1409 	aAdjustChunk = occupied;
       
  1410 	return ENSmlDmAllWillFit;
       
  1411 	}
       
  1412 
       
  1413 
       
  1414 // ------------------------------------------------------------------------------------------------
       
  1415 // CNSmlDMHostSession::RemoveResultCallbacksL
       
  1416 //	Auxiliary function for removing the 'read' results from the internal list.
       
  1417 //	This function is called from GetResultsL after some (or all) results 
       
  1418 //	are written to the chunk. 
       
  1419 //	@param aNumber. The number of the results which are needed to be 
       
  1420 //	  			    removed from the internal list.
       
  1421 // ------------------------------------------------------------------------------------------------
       
  1422 //
       
  1423 void CNSmlDMHostSession::RemoveResultCallbacksL ( TInt aNumber )
       
  1424 	{
       
  1425 	if ( aNumber > iCallbackList.Count() || aNumber > iResultList.Count() || aNumber < 0 )
       
  1426 		{
       
  1427 		User::Leave ( KErrGeneral );		
       
  1428 		}
       
  1429 	
       
  1430 	for ( TInt i(0); i<aNumber; i++ )
       
  1431 		{
       
  1432 		for ( TInt u(0); u<iCallbackList.Count(); u++ )
       
  1433 			{
       
  1434 			if ( iCallbackList[u].callBack == ENSmlDmCallbackResults )
       
  1435 				{
       
  1436 				iCallbackList.Remove(u);
       
  1437 				break;				
       
  1438 				}
       
  1439 			}
       
  1440 		delete iResultList[0];
       
  1441 		iResultList.Remove(0);
       
  1442 		}
       
  1443 	}
       
  1444 
       
  1445 // ------------------------------------------------------------------------------------------------
       
  1446 // CNSmlDMHostSession::LoadNewAdapterL
       
  1447 //	Auxiliary function called by the command handling functions (e.g. AddCopyOrDeleteL).
       
  1448 //	Loads the ECom plug-in Dm Adapter.
       
  1449 //	The adapter is identified with the given implementation UID.
       
  1450 //	@param aIndex. A place in the internal list where the adpater pointer is stored. 
       
  1451 //	@param aAdapterId. The implementation UID of the requested DM adapter. 
       
  1452 //	@return CSmlDmAdapter*. New instance of the ECom plug-in DM adapter.
       
  1453 // ------------------------------------------------------------------------------------------------
       
  1454 //
       
  1455 CSmlDmAdapter* CNSmlDMHostSession::LoadNewAdapterL ( TInt aIndex, TUint32 aAdapterId )
       
  1456 	{
       
  1457 	_DBG_FILE("CNSmlDMHostSession::LoadNewAdapterL(): begin");	
       
  1458 	CSmlDmAdapter* adapter = NULL;
       
  1459 	TUid uid = { aAdapterId };
       
  1460 	adapter = CSmlDmAdapter::NewL ( uid, *this );
       
  1461 	iAdapters[aIndex].adapterPtr = adapter;
       
  1462 	_DBG_FILE("CNSmlDMHostSession::LoadNewAdapterL(): end");	
       
  1463 	return adapter;
       
  1464 	}
       
  1465 
       
  1466 // ------------------------------------------------------------------------------------------------
       
  1467 // CNSmlDMHostSession::ReadCmdParamsL
       
  1468 //	Auxiliary function called by the command handling functions (e.g. UpdateLeafOrExecuteL).
       
  1469 //	Reads the DM command parameters from the message and from the chunk.
       
  1470 //	Utilizes the DM Utils services for parsing data in the chunk.
       
  1471 //	@param aMessage. An object which encapsulates a client request. 
       
  1472 // ------------------------------------------------------------------------------------------------
       
  1473 //
       
  1474 void CNSmlDMHostSession::ReadCmdParamsL ( const RMessage2& aMessage )
       
  1475 	{
       
  1476 	_DBG_FILE("CNSmlDMHostSession::ReadCmdParamsL(): begin");
       
  1477 	DeletePrevParams();
       
  1478 	RMemReadStream readStream ( iChunk.Base(), iChunk.Size() );
       
  1479 	CleanupClosePushL ( readStream );
       
  1480 	NSmlDmCmdArgHandler::ParseDataL ( readStream, iLuid, iData, iUri, iType );
       
  1481 	iAdapterId = aMessage.Int0();
       
  1482 	iStatusRef = aMessage.Int1();
       
  1483 	CleanupStack::PopAndDestroy();	// readStream	
       
  1484 	_DBG_FILE("CNSmlDMHostSession::ReadCmdParamsL(): end");
       
  1485 	}
       
  1486 
       
  1487 // ------------------------------------------------------------------------------------------------
       
  1488 // CNSmlDMHostSession::ReadCmdFetchParamsL
       
  1489 //	Auxiliary function called by the FetchLeafChildUrisOrSizeL.
       
  1490 //	Reads the DM command parameters from the message and from the chunk.
       
  1491 //	Utilizes the DM Utils services for parsing data in the chunk.
       
  1492 //	@param aMessage. An object which encapsulates a client request. 
       
  1493 //	@param aPreviousLuids. A reference parameter, needed for freeing the reserved 
       
  1494 //	                       heap memory when the luids can be destroyed. 
       
  1495 // ------------------------------------------------------------------------------------------------
       
  1496 //
       
  1497 void CNSmlDMHostSession::ReadCmdFetchParamsL ( const RMessage2& aMessage, RPointerArray<HBufC8>& aPreviousLuids )
       
  1498 	{
       
  1499 	_DBG_FILE("CNSmlDMHostSession::ReadCmdFetchParamsL(): begin");	
       
  1500 	DeletePrevParams();
       
  1501 	RMemReadStream readStream ( iChunk.Base(), iChunk.Size() );
       
  1502 	CleanupClosePushL ( readStream );
       
  1503 	iPrevSegURIList = new (ELeave) CArrayFixFlat<TSmlDmMappingInfo>(KNSmlDmGranularity);
       
  1504 	NSmlDmCmdArgHandler::ParseFetchArgumentsL ( readStream, iLuid, iUri, iType, *iPrevSegURIList, aPreviousLuids );
       
  1505 	iAdapterId = aMessage.Int0();
       
  1506 	iStatusRef = aMessage.Int1();
       
  1507 	iResultRef = aMessage.Int2();
       
  1508 	CleanupStack::PopAndDestroy();	// readStream	
       
  1509 	_DBG_FILE("CNSmlDMHostSession::ReadCmdFetchParamsL(): end");
       
  1510 	}
       
  1511 
       
  1512 // ------------------------------------------------------------------------------------------------
       
  1513 // CNSmlDMHostSession::ReadCopyCmdParamsL
       
  1514 //	Auxiliary function called by the AddCopyOrDeleteL.
       
  1515 //	Reads the DM command parameters from the message and from the chunk.
       
  1516 //	Utilizes the DM Utils services for parsing data in the chunk.
       
  1517 //	@param aMessage. An object which encapsulates a client request. 
       
  1518 // ------------------------------------------------------------------------------------------------
       
  1519 //
       
  1520 void CNSmlDMHostSession::ReadCopyCmdParamsL ( const RMessage2& aMessage )
       
  1521 	{
       
  1522 	_DBG_FILE("CNSmlDMHostSession::ReadCopyCmdParamsL(): begin");
       
  1523 	DeletePrevParams();
       
  1524 	RMemReadStream readStream ( iChunk.Base(), iChunk.Size() );
       
  1525 	CleanupClosePushL ( readStream );
       
  1526 	NSmlDmCmdArgHandler::ParseCopyArgumentsL ( readStream, iTargetLuid, iTargetURI, iLuid, iUri, iType );
       
  1527 	iAdapterId = aMessage.Int0();
       
  1528 	iStatusRef = aMessage.Int1();
       
  1529 	CleanupStack::PopAndDestroy();	// readStream	
       
  1530 	_DBG_FILE("CNSmlDMHostSession::ReadCopyCmdParamsL(): end");
       
  1531 	}
       
  1532 
       
  1533 // ------------------------------------------------------------------------------------------------
       
  1534 // CNSmlDMHostSession::DeletePrevParams
       
  1535 //	Auxiliary function for deleting previous set of DM command parameters.
       
  1536 // ------------------------------------------------------------------------------------------------
       
  1537 //
       
  1538 void CNSmlDMHostSession::DeletePrevParams()
       
  1539 	{
       
  1540 	_DBG_FILE("CNSmlDMHostSession::DeletePrevParams(): begin");	
       
  1541 	delete iLuid;
       
  1542 	iLuid = NULL;
       
  1543 	delete iUri;
       
  1544 	iUri = NULL;
       
  1545 	delete iData;
       
  1546 	iData = NULL;
       
  1547 	delete iType;
       
  1548 	iType = NULL;
       
  1549 	delete iTargetLuid;
       
  1550 	iTargetLuid = NULL;
       
  1551 	delete iTargetURI;
       
  1552 	iTargetURI = NULL;
       
  1553     if ( iPrevSegURIList )
       
  1554     	{
       
  1555     	iPrevSegURIList->Reset();    	
       
  1556     	}
       
  1557     delete iPrevSegURIList;
       
  1558     iPrevSegURIList = NULL;
       
  1559 	_DBG_FILE("CNSmlDMHostSession::DeletePrevParams(): end");	
       
  1560 	}
       
  1561 
       
  1562 // ------------------------------------------------------------------------------------------------
       
  1563 // CNSmlDMHostSession::CheckIfCorrectAdapterL
       
  1564 //	Auxiliary function called by the UpdateLeafOrExecuteLargeL.
       
  1565 //	Checks that the arrived command is not to different adapter,
       
  1566 //	during the large object update to the other. 
       
  1567 // ------------------------------------------------------------------------------------------------
       
  1568 //
       
  1569 void CNSmlDMHostSession::CheckIfCorrectAdapterL() const
       
  1570 	{
       
  1571 	_DBG_FILE("CNSmlDMHostSession::CheckIfCorrectAdapterL(): begin");	
       
  1572 	if ( iLargeWriteStream.streamPtr && iLargeWriteStream.adapterId != iAdapterId )
       
  1573 		{
       
  1574 		User::Leave ( KErrArgument );		
       
  1575 		}
       
  1576 	_DBG_FILE("CNSmlDMHostSession::CheckIfCorrectAdapterL(): end");	
       
  1577 	}
       
  1578 
       
  1579 // ------------------------------------------------------------------------------------------------
       
  1580 // CNSmlDMHostSession::CallAdapterCommandL
       
  1581 //	Auxiliary function called by the the command handling functions, 
       
  1582 //	when the streaming is not used.
       
  1583 //	Calls the chosen DM adapter function of the given adapter.
       
  1584 //	The function parameters are set from the internal member variables.
       
  1585 //	@param aCommand. Identifies the chosen adapter command. 
       
  1586 //	@param aAdIndex. Identifies the chosen DM plug-in adapter. 
       
  1587 //	@param aLargeObject. If the data is a large object, a different 
       
  1588 //	                     internal variable is chosen. 
       
  1589 // ------------------------------------------------------------------------------------------------
       
  1590 //
       
  1591 void CNSmlDMHostSession::CallAdapterCommandL ( TNSmlDmHostOperationCodes aCommand, TInt aAdIndex, TBool aLargeObject )
       
  1592 	{
       
  1593 	_DBG_FILE("CNSmlDMHostSession::CallAdapterCommandL(): begin");
       
  1594 	iAdapters[aAdIndex].commandsCalled = ETrue;
       
  1595 	switch ( aCommand ) 
       
  1596 		{
       
  1597 		case ENSmlDMAddNode:
       
  1598 			iAdapters[aAdIndex].adapterPtr->AddNodeObjectL ( *iUri, *iLuid, iStatusRef );				
       
  1599 			break;
       
  1600 		case ENSmlDMCopy:
       
  1601 			iAdapters[aAdIndex].adapterPtr->CopyCommandL ( *iTargetURI, *iTargetLuid, *iUri, *iLuid, *iType, iStatusRef );
       
  1602 			break;
       
  1603 		case ENSmlDMDelete:
       
  1604 			iAdapters[aAdIndex].adapterPtr->DeleteObjectL ( *iUri, *iLuid, iStatusRef );			
       
  1605 			break;
       
  1606 		case ENSmlDMFetchLeaf:
       
  1607 			iAdapters[aAdIndex].adapterPtr->FetchLeafObjectL ( *iUri, *iLuid, *iType, iResultRef, iStatusRef );
       
  1608 			break;	
       
  1609 		case ENSmlDMChildUriList:
       
  1610 			iAdapters[aAdIndex].adapterPtr->ChildURIListL ( *iUri, *iLuid, *iPrevSegURIList, iResultRef, iStatusRef );
       
  1611 			break;
       
  1612 		case ENSmlDMFetchLeafSize:
       
  1613 			iAdapters[aAdIndex].adapterPtr->FetchLeafObjectSizeL ( *iUri, *iLuid, *iType, iResultRef, iStatusRef );
       
  1614 			break;
       
  1615 		case ENSmlDMUpdateLeaf:
       
  1616 		case ENSmlDMUpdateLeafLarge:
       
  1617 			if ( !aLargeObject )
       
  1618 				{
       
  1619 				iAdapters[aAdIndex].adapterPtr->UpdateLeafObjectL ( *iUri, *iLuid, *iData, *iType, iStatusRef );				
       
  1620 				}
       
  1621 			else
       
  1622 				{
       
  1623 				iAdapters[aAdIndex].adapterPtr->UpdateLeafObjectL ( *iUri, *iLuid, *iLargeObject, *iType, iStatusRef );				
       
  1624 				}			
       
  1625 			break;
       
  1626 		case ENSmlDMExecute:
       
  1627 		case ENSmlDMExecuteLarge:
       
  1628 			if ( !aLargeObject )
       
  1629 				{
       
  1630 				iAdapters[aAdIndex].adapterPtr->ExecuteCommandL ( *iUri, *iLuid, *iData, *iType, iStatusRef );
       
  1631 				}
       
  1632 			else
       
  1633 				{
       
  1634 				iAdapters[aAdIndex].adapterPtr->ExecuteCommandL ( *iUri, *iLuid, *iLargeObject, *iType, iStatusRef );				
       
  1635 				}
       
  1636 			break;
       
  1637 		default:
       
  1638 			User::Leave ( KErrNotSupported );			
       
  1639 		}
       
  1640 	_DBG_FILE("CNSmlDMHostSession::CallAdapterCommandL(): end");
       
  1641 	}
       
  1642 
       
  1643 // ------------------------------------------------------------------------------------------------
       
  1644 // CNSmlDMHostSession::CallAdapterCommandL
       
  1645 //	Auxiliary function called by the the command handling functions, 
       
  1646 //	when the streaming is used.
       
  1647 //	Calls the chosen DM adapter function of the given adapter.
       
  1648 //	The function parameters are set from the internal member variables.
       
  1649 //	@param aCommand. Identifies the chosen adapter command. 
       
  1650 //	@param aAdIndex. Identifies the chosen DM plug-in adapter. 
       
  1651 //	@param aWriteStream. A reference parameter. The adapter sets this parameter to point
       
  1652 //	                     to the correct place. 
       
  1653 // ------------------------------------------------------------------------------------------------
       
  1654 //
       
  1655 void CNSmlDMHostSession::CallAdapterCommandL ( TNSmlDmHostOperationCodes aCommand, TInt aAdIndex, RWriteStream*& aWriteStream )
       
  1656 	{
       
  1657 	_DBG_FILE("CNSmlDMHostSession::CallAdapterCommandL( writeStream ): begin");
       
  1658 	iAdapters[aAdIndex].commandsCalled = ETrue;	
       
  1659 	switch ( aCommand )
       
  1660 		{
       
  1661 		case ENSmlDMUpdateLeaf:
       
  1662 		case ENSmlDMUpdateLeafLarge:
       
  1663 			iAdapters[aAdIndex].adapterPtr->UpdateLeafObjectL ( *iUri, *iLuid, aWriteStream, *iType, iStatusRef );
       
  1664 			break;
       
  1665 		case ENSmlDMExecute:
       
  1666 		case ENSmlDMExecuteLarge:
       
  1667 			iAdapters[aAdIndex].adapterPtr->ExecuteCommandL ( *iUri, *iLuid, aWriteStream, *iType, iStatusRef );
       
  1668 			break;		
       
  1669 		default:
       
  1670 			User::Leave ( KErrNotSupported );
       
  1671 		}
       
  1672 
       
  1673 	_DBG_FILE("CNSmlDMHostSession::CallAdapterCommandL( writeStream ): end");
       
  1674 	}
       
  1675 
       
  1676 // ------------------------------------------------------------------------------------------------
       
  1677 // CNSmlDMHostSession::ResultItemSize
       
  1678 //	Auxiliary function for calculating how much memory does one particular result item reserve.
       
  1679 //	This function is called from AnalyzeResultList. 
       
  1680 //	@param aIndex. The place of the item in the internal result list.
       
  1681 //	@return TInt. Needed memory area in bytes for the given result item.
       
  1682 // ------------------------------------------------------------------------------------------------
       
  1683 //
       
  1684 inline TInt CNSmlDMHostSession::ResultItemSize ( TInt aIndex )
       
  1685 	{
       
  1686 	_DBG_FILE("CNSmlDMHostSession::ResultItemSize: begin");
       
  1687 	// (iResultRef=2bytes) + (iResult->Size=4) + (iResult) + (iMimeType->Lenght=2) + (iMimeType)
       
  1688 	TInt bytes = iResultList[aIndex]->iResult->Size() + iResultList[aIndex]->iMimeType->Size() + KNSmlDmConstItemSize;
       
  1689 	_DBG_FILE("CNSmlDMHostSession::ResultItemSize: end");
       
  1690 	return bytes;
       
  1691 	}
       
  1692 
       
  1693 // ------------------------------------------------------------------------------------------------
       
  1694 // CNSmlDMHostSession::SizeOfMappings
       
  1695 //	Auxiliary function for calculating how much memory do the mappings need.
       
  1696 //	This function is called from GetMappingsL in order to adjust the chunk 
       
  1697 //	to adequate size before the mappings are written to the chunk. 
       
  1698 //	@return TInt. Needed memory area in bytes for all the unread mappings.
       
  1699 // ------------------------------------------------------------------------------------------------
       
  1700 //
       
  1701 TInt CNSmlDMHostSession::SizeOfMappings()
       
  1702 	{
       
  1703 	_DBG_FILE("CNSmlDMHostSession::SizeOfMappings: begin");
       
  1704 	TInt bytes(2);  // 2 = count 16bits
       
  1705 	for ( TInt i(0); i<iMapUris->Count(); i++ )
       
  1706 		{
       
  1707 		bytes += 2 + iMapUris->At(i)->Size() + 2 + iMapLuids->At(i)->Size();
       
  1708 		}
       
  1709 	_DBG_FILE("CNSmlDMHostSession::SizeOfMappings: end");
       
  1710 	return bytes;
       
  1711 	}
       
  1712 
       
  1713 // ------------------------------------------------------------------------------------------------
       
  1714 // CNSmlDMHostSession::SizeOfStatuses
       
  1715 //	Auxiliary function for calculating how much memory do the arrived statuse need.
       
  1716 //	This function is called from GetStatusesL in order to adjust the chunk 
       
  1717 //	to adequate size before the statuses are written to the chunk. 
       
  1718 //	@return TInt. Needed memory area in bytes for all the unread statuses.
       
  1719 // ------------------------------------------------------------------------------------------------
       
  1720 //
       
  1721 TInt CNSmlDMHostSession::SizeOfStatuses() const
       
  1722 	{
       
  1723 	_DBG_FILE("CNSmlDMHostSession::SizeOfStatuses: begin");
       
  1724 	TInt bytes(2);  // 2 = count 16 bits
       
  1725 	for ( TInt i(0); i<iStatusCodeList.Count(); i++ )
       
  1726 		{
       
  1727 		bytes += 2 + 1;		
       
  1728 		}
       
  1729 	_DBG_FILE("CNSmlDMHostSession::SizeOfStatuses: end");
       
  1730 	return bytes;
       
  1731 	}
       
  1732 
       
  1733 
       
  1734 
       
  1735 // ------------------------------------------------------------------------------------------------
       
  1736 // Callbacks from the dm adapters
       
  1737 // ------------------------------------------------------------------------------------------------
       
  1738 
       
  1739 // ------------------------------------------------------------------------------------------------
       
  1740 // CNSmlDMHostSession::SetResultsL
       
  1741 //	MSmlDmCallback - callback interface.
       
  1742 // 	For returning fetch results from dm plug-in adapters.
       
  1743 //	@param aResultsRef. Reference to correct command.
       
  1744 //	@param aObject. The data which should be returned.
       
  1745 //	@param aType. MIME type of the object.
       
  1746 // ------------------------------------------------------------------------------------------------
       
  1747 //
       
  1748 void CNSmlDMHostSession::SetResultsL ( TInt aResultsRef, CBufBase& aObject, const TDesC8& aType )
       
  1749 	{
       
  1750 	_DBG_FILE("CNSmlDMHostSession::SetResultsL ( CBufBase ): begin");
       
  1751 	
       
  1752 	if ( !iFetchLinkResult )
       
  1753 		{
       
  1754 		TNSmlDmCallbackRef newCallback;
       
  1755 		newCallback.callBack = ENSmlDmCallbackResults;
       
  1756 		newCallback.reference = aResultsRef;
       
  1757 
       
  1758 		CNSmlDmResultElement* newResult = new (ELeave) CNSmlDmResultElement;
       
  1759 		CleanupStack::PushL ( newResult );
       
  1760 		newResult->iResultRef = aResultsRef;
       
  1761 		newResult->iResult = CBufFlat::NewL ( aObject.Size() );
       
  1762 		newResult->iResult->InsertL ( 0, aObject.Ptr(0), aObject.Size() );
       
  1763 		newResult->iMimeType = aType.AllocL();
       
  1764 		if ( iResultList.Append ( newResult ) == KErrNone )
       
  1765 			{
       
  1766 			CleanupStack::Pop();  // newResult
       
  1767 			if ( iCallbackList.Append ( newCallback ) != KErrNone )
       
  1768 				{
       
  1769 				delete iResultList[iResultList.Count()-1];
       
  1770 				iResultList.Remove(iResultList.Count()-1);
       
  1771 				}
       
  1772 			}
       
  1773 		else
       
  1774 			{
       
  1775 			CleanupStack::PopAndDestroy();  // newResult			
       
  1776 			}
       
  1777 		}
       
  1778 	else
       
  1779 		{
       
  1780 		delete iResultOtherAdapter;
       
  1781 		iResultOtherAdapter = NULL;
       
  1782 		iResultOtherAdapter = aObject.Ptr(0).AllocL();
       
  1783 		}
       
  1784 	_DBG_FILE("CNSmlDMHostSession::SetResultsL ( CBufBase ): end");
       
  1785 	}
       
  1786 
       
  1787 // ------------------------------------------------------------------------------------------------
       
  1788 // CNSmlDMHostSession::SetResultsL
       
  1789 //	MSmlDmCallback - callback interface.
       
  1790 // 	For returning fetch results from dm plug-in adapters (using streaming).
       
  1791 //	@param aResultsRef. Reference to correct command.
       
  1792 //	@param aStream. Large data which should be returned.
       
  1793 //	@param aType. MIME type of the object.
       
  1794 // ------------------------------------------------------------------------------------------------
       
  1795 //
       
  1796 void CNSmlDMHostSession::SetResultsL ( TInt aResultsRef, RReadStream*& aStream, const TDesC8& aType )
       
  1797 	{
       
  1798 	_DBG_FILE("CNSmlDMHostSession::SetResultsL ( RReadStream ): begin");
       
  1799 	if ( !iFetchLinkResult )
       
  1800 		{	
       
  1801 		iResultReadStream = NULL;
       
  1802 		iResultOffset = 0;
       
  1803 	
       
  1804 		TNSmlDmCallbackRef newCallback;
       
  1805 		newCallback.callBack = ENSmlDmCallbackResults;
       
  1806 		newCallback.reference = aResultsRef;
       
  1807 
       
  1808 		CNSmlDmResultElement* newResult = new (ELeave) CNSmlDmResultElement;
       
  1809 		CleanupStack::PushL ( newResult );
       
  1810 		newResult->iResultRef = aResultsRef;
       
  1811 		newResult->iMimeType = aType.AllocL();
       
  1812 	
       
  1813 		TInt available = RoomForLargeData ( aType );
       
  1814 	
       
  1815 		RBufWriteStream writeStream;
       
  1816 		CleanupClosePushL ( writeStream );
       
  1817 		if ( aStream->Source()->SizeL() >  available )
       
  1818 			{
       
  1819 			newResult->iStreamed = ETrue;
       
  1820 			iResultReadStream = aStream;
       
  1821 			newResult->iResult = CBufFlat::NewL ( available );
       
  1822 			writeStream.Open ( *newResult->iResult );
       
  1823 			aStream->ReadL ( writeStream, available );
       
  1824 			iResultOffset = available;
       
  1825 			}
       
  1826 		else
       
  1827 			{
       
  1828 			newResult->iResult = CBufFlat::NewL ( aStream->Source()->SizeL() );
       
  1829 			writeStream.Open ( *newResult->iResult );
       
  1830 			aStream->ReadL ( writeStream, aStream->Source()->SizeL() );
       
  1831 			aStream->Close();
       
  1832 			}
       
  1833 		CleanupStack::PopAndDestroy();  // writeStream
       
  1834 		if ( iResultList.Append ( newResult ) == KErrNone )
       
  1835 			{
       
  1836 			CleanupStack::Pop();   // newResult
       
  1837 			if ( iCallbackList.Append ( newCallback ) != KErrNone )
       
  1838 				{
       
  1839 				delete iResultList[iResultList.Count()-1];
       
  1840 				iResultList.Remove(iResultList.Count()-1);				
       
  1841 				}
       
  1842 			}
       
  1843 		else
       
  1844 			{
       
  1845 			CleanupStack::PopAndDestroy();   // newResult			
       
  1846 			}
       
  1847 		}
       
  1848 	else
       
  1849 		{
       
  1850 		delete iResultOtherAdapter;
       
  1851 		iResultOtherAdapter = NULL;
       
  1852 		iResultOtherAdapter = HBufC8::NewL ( aStream->Source()->SizeL() );
       
  1853 		TPtr8 tmpPtr = iResultOtherAdapter->Des();
       
  1854 		aStream->ReadL ( tmpPtr );
       
  1855 		aStream->Close();
       
  1856 		}
       
  1857 		
       
  1858 	_DBG_FILE("CNSmlDMHostSession::SetResultsL ( RReadStream ): end");
       
  1859 	}
       
  1860 
       
  1861 // ------------------------------------------------------------------------------------------------
       
  1862 // CNSmlDMHostSession::SetStatusL
       
  1863 //	MSmlDmCallback - callback interface.
       
  1864 // 	For returning statuses for dm adapter commands.
       
  1865 //	@param aStatusRef. Reference to correct command.
       
  1866 //	@param aErrorCode. Information about the command success.
       
  1867 // ------------------------------------------------------------------------------------------------
       
  1868 //
       
  1869 void CNSmlDMHostSession::SetStatusL ( TInt aStatusRef, MSmlDmAdapter::TError aErrorCode )
       
  1870 	{
       
  1871 	_DBG_FILE("CNSmlDMHostSession::SetStatusL(): begin");
       
  1872 		
       
  1873 	if(aErrorCode  )
       
  1874 		{
       
  1875 		RProperty::Set(KPSUidNSmlDMSyncAgent, 
       
  1876 				KNSmlDMCmdAddNodeSuccess, EFailed );
       
  1877 		}
       
  1878 		else 
       
  1879 		{
       
  1880 		RProperty::Set(KPSUidNSmlDMSyncAgent, 
       
  1881 				KNSmlDMCmdAddNodeSuccess, EAdded );
       
  1882 		}
       
  1883 	if ( !iFetchLinkResult )
       
  1884 		{		
       
  1885 		TNSmlDmCallbackRef newCallback;
       
  1886 		newCallback.callBack = ENSmlDmCallbackStatuses;
       
  1887 		newCallback.reference = aStatusRef;
       
  1888 
       
  1889 		TNSmlDmStatusElement newStatus;
       
  1890 		newStatus.iStatusRef = aStatusRef;
       
  1891 		newStatus.iStatus = aErrorCode;	
       
  1892     	if(iCommitStatus.iStreamingOngoing&&iCommitStatus.iStreamCommitted)
       
  1893 		    {
       
  1894 		    if(aStatusRef==iCommitStatus.iOldStatusRef)
       
  1895 		        {
       
  1896 		        newStatus.iStatusRef = iCommitStatus.iNewStatusRef;
       
  1897 		        }
       
  1898 		    }
       
  1899 
       
  1900 		if ( iStatusCodeList.Append ( newStatus ) == KErrNone )
       
  1901 			{
       
  1902 			if ( iCallbackList.Append ( newCallback ) != KErrNone )
       
  1903 				{
       
  1904 				iStatusCodeList.Remove ( iStatusCodeList.Count()-1 );
       
  1905 				}
       
  1906 			}
       
  1907 		}
       
  1908 	else 
       
  1909 		{
       
  1910 		iStatusOtherAdapter = aErrorCode;
       
  1911 		}
       
  1912 	_DBG_FILE("CNSmlDMHostSession::SetStatusL(): end");
       
  1913 	}
       
  1914 
       
  1915 // ------------------------------------------------------------------------------------------------
       
  1916 // CNSmlDMHostSession::SetMappingL
       
  1917 //	MSmlDmCallback - callback interface.
       
  1918 // 	This function is called for a new management object, 
       
  1919 //	both for node objects and for leaf objects by dm adapter.
       
  1920 //	@param aURI. URI of the object. 
       
  1921 //	@param aLUID. Typically this is ID for the database table.
       
  1922 // ------------------------------------------------------------------------------------------------
       
  1923 //
       
  1924 void CNSmlDMHostSession::SetMappingL ( const TDesC8& aURI, const TDesC8& aLUID )
       
  1925 	{
       
  1926 	_DBG_FILE("CNSmlDMHostSession::SetMappingL(): begin");
       
  1927 	if ( !iFetchLinkResult )
       
  1928 		{
       
  1929 		if ( !iMapUris )
       
  1930 			{
       
  1931 			iMapUris = new (ELeave) CArrayPtrSeg<HBufC8>(KNSmlDmGranularity);			
       
  1932 			}
       
  1933 		if ( !iMapLuids )
       
  1934 			{
       
  1935 			iMapLuids = new (ELeave) CArrayPtrSeg<HBufC8>(KNSmlDmGranularity);			
       
  1936 			}
       
  1937 
       
  1938 		HBufC8* newUri = aURI.AllocLC();
       
  1939 		iMapUris->AppendL ( newUri );
       
  1940 		CleanupStack::Pop();  // newUri
       
  1941 		 
       
  1942 		HBufC8* newLuid = aLUID.AllocLC();
       
  1943 		iMapLuids->AppendL ( newLuid );
       
  1944 		CleanupStack::Pop();  // newLuid 
       
  1945 
       
  1946 		TNSmlDmCallbackRef newCallback;
       
  1947 		newCallback.callBack = ENSmlDmCallbackMappings;
       
  1948 		newCallback.reference = 0;
       
  1949 		iCallbackList.Append ( newCallback );	
       
  1950 		}
       
  1951 	else
       
  1952 		{
       
  1953 		if ( iCbSessConnected && ( aURI.Find ( KNSmlDmApAdapterURI1 ) == 0 || aURI.Find ( KNSmlDmApAdapterURI2 ) == 0 ) )
       
  1954 			{
       
  1955 			iCbSession.AddMappingInfoL ( KNSmlInternetAdapterImplUid, aURI, aLUID );			
       
  1956 			}
       
  1957 		}
       
  1958 	_DBG_FILE("CNSmlDMHostSession::SetMappingL(): end");
       
  1959 	}
       
  1960 
       
  1961 // ------------------------------------------------------------------------------------------------
       
  1962 // CNSmlDMHostSession::FetchLinkL
       
  1963 //	MSmlDmCallback - callback interface.
       
  1964 // 	The function is used to make a fetch to other adapters. 
       
  1965 //	@param aURI. URI of the object. 
       
  1966 //	@param aData. Reference to data, i.e. data is returned here
       
  1967 //	@param aStatus. The status of fetch command is returned here
       
  1968 // ------------------------------------------------------------------------------------------------
       
  1969 //
       
  1970 void CNSmlDMHostSession::FetchLinkL ( const TDesC8& aURI, CBufBase& aData, MSmlDmAdapter::TError& aStatus )
       
  1971 	{
       
  1972 	_DBG_FILE("CNSmlDMHostSession::FetchLinkL(): begin");
       
  1973 	// Remove possible ./ from the beginning
       
  1974 	TPtrC8 uri = NSmlDmURI::RemoveDotSlash ( aURI );
       
  1975 	
       
  1976 	// Check the ACL rights
       
  1977 	if ( !iCbSessConnected )
       
  1978 		{
       
  1979 		if ( iCbSession.Connect() != KErrNone )
       
  1980 			{
       
  1981 			aData.InsertL ( 0, KNullDesC8 );
       
  1982 			aStatus = MSmlDmAdapter::EError;
       
  1983 			return;			
       
  1984 			}
       
  1985 		else 
       
  1986 			{
       
  1987 			iCbSessConnected = ETrue;			
       
  1988 			}
       
  1989 		}
       
  1990 	
       
  1991 	if ( iCbSession.CheckDynamicAclL( uri, EAclGet, iMgmtTree ) ) //tarm
       
  1992 		{
       
  1993 		// Check if this server has access to the required adapter
       
  1994 		TUint32 adId(0);
       
  1995 		TNSmlDmDDFFormat nodeType;
       
  1996 		if ( AdapterIdFromUriL ( uri, adId, nodeType ) )
       
  1997 			{	
       
  1998 			FetchLinkFromAdapterL ( uri, adId, aData, aStatus, nodeType );
       
  1999 			}
       
  2000 		else
       
  2001 			{
       
  2002 			// Help from other servers needed
       
  2003 			FetchLinkViaIPCL ( uri, aData, aStatus );		
       
  2004 			}
       
  2005 		}
       
  2006 	else 
       
  2007 		{
       
  2008 		aData.InsertL ( 0, KNullDesC8 );
       
  2009 		aStatus = MSmlDmAdapter::EError;
       
  2010 		}
       
  2011 	
       
  2012 	_DBG_FILE("CNSmlDMHostSession::FetchLinkL(): end");
       
  2013 	}
       
  2014 
       
  2015 // ------------------------------------------------------------------------------------------------
       
  2016 // CNSmlDMHostSession::FetchLinkViaIPCL
       
  2017 //	Serves the FetchLink callback when the AP adapter is not found from the server.
       
  2018 //	Issues an IPC -FetchLink command to the other DM Host Server 
       
  2019 //	using RNSmlDMFetchLink -client API.
       
  2020 //	@param aURI. A path to the requested interior node object or leaf object. 
       
  2021 //	@param aData. A reference parameter, i.e. a place for fetch result. 
       
  2022 //	@param aStatus. A reference parameter telling the command success (e.g. EOk or ENotFound).
       
  2023 // ------------------------------------------------------------------------------------------------
       
  2024 //
       
  2025 void CNSmlDMHostSession::FetchLinkViaIPCL ( const TDesC8& aURI, CBufBase& aData, MSmlDmAdapter::TError& aStatus ) const
       
  2026 	{
       
  2027 	_DBG_FILE("CNSmlDMHostSession::FetchLinkViaIPCL(): begin");
       
  2028 	if ( ( aURI.Find ( KNSmlDmApAdapterURI1 ) == 0 || aURI.Find ( KNSmlDmApAdapterURI2 ) == 0 )
       
  2029 	      && iOwnId != KNSmlDmApAdapterServerId )
       
  2030 		{
       
  2031 		RNSmlDMFetchLink fetchLink;
       
  2032 		TInt openErr ( KErrNone );
       
  2033 		switch ( KNSmlDmApAdapterServerId )
       
  2034 			{
       
  2035 			case 1:
       
  2036 				TRAP ( openErr, fetchLink.OpenL ( KNSmlDmHostServer1Name ) );				
       
  2037 				break;
       
  2038 			case 2:
       
  2039 				TRAP ( openErr, fetchLink.OpenL ( KNSmlDmHostServer2Name ) );				
       
  2040 				break;
       
  2041 			case 3:
       
  2042 				TRAP ( openErr, fetchLink.OpenL ( KNSmlDmHostServer3Name ) );				
       
  2043 				break;
       
  2044 			case 4:
       
  2045 				TRAP ( openErr, fetchLink.OpenL ( KNSmlDmHostServer4Name ) );				
       
  2046 				break;
       
  2047 			default:
       
  2048 				break;
       
  2049 			}
       
  2050 		TRAPD ( chunkErr, fetchLink.SendChunkHandleL ( iChunk ) );
       
  2051 		if ( openErr == KErrNone && chunkErr == KErrNone )
       
  2052 			{
       
  2053 			HBufC8* answer = NULL;
       
  2054 			RMemReadStream readStream ( iChunk.Base(), iChunk.Size() );
       
  2055 			CleanupClosePushL ( readStream );
       
  2056 			TRAPD ( connErr, ( answer = fetchLink.FetchL ( aURI, readStream ) ) );
       
  2057 			CleanupStack::PopAndDestroy();  // readStream
       
  2058 			fetchLink.Close();
       
  2059 			if ( answer && connErr == KErrNone )
       
  2060 				{
       
  2061 				aStatus = MSmlDmAdapter::EOk;
       
  2062 				aData.InsertL ( 0, *answer );
       
  2063 				delete answer;
       
  2064 				answer = 0;
       
  2065 				return;				
       
  2066 				}
       
  2067 			}
       
  2068 		}
       
  2069 	aData.InsertL ( 0, KNullDesC8 );
       
  2070 	aStatus = MSmlDmAdapter::EError;		
       
  2071 	_DBG_FILE("CNSmlDMHostSession::FetchLinkViaIPCL(): end");
       
  2072 	}
       
  2073 
       
  2074 // ------------------------------------------------------------------------------------------------
       
  2075 // CNSmlDMHostSession::FetchLinkFromAdapterL
       
  2076 //	Serves the FetchLink callback when the AP adapter is owned this server.
       
  2077 //	Asks first a luid from the callback server.
       
  2078 //	Calls FetchLeafObjectL -function of the DM adapter if the requested object is a leaf. 
       
  2079 //	Calls ChildURIListL -function of the DM adapter if the requested object is an interior node. 
       
  2080 //	@param aURI. A path to the requested interior node object or leaf object. 
       
  2081 //	@param aAdapterId. Identifies the requested adapter. 
       
  2082 //	@param aData. A reference parameter, i.e. a place for fetch result. 
       
  2083 //	@param aStatus. A reference parameter telling the command success (e.g. EOk or ENotFound).
       
  2084 //	@param aNodeType. Defines whether the object is a leaf or a node.
       
  2085 // ------------------------------------------------------------------------------------------------
       
  2086 //
       
  2087 void CNSmlDMHostSession::FetchLinkFromAdapterL ( const TDesC8& aURI, TUint32 aAdapterId, CBufBase& aData, MSmlDmAdapter::TError& aStatus, TNSmlDmDDFFormat aNodeType )
       
  2088 	{
       
  2089 	_DBG_FILE("CNSmlDMHostSession::FetchLinkFromAdapterL(): begin");
       
  2090 	delete iResultOtherAdapter;
       
  2091 	iResultOtherAdapter = NULL;
       
  2092 	CSmlDmAdapter* adapter = NULL;
       
  2093 	for ( TInt i(0); i<iAdapters.Count(); i++ )
       
  2094 		{
       
  2095 		if ( iAdapters[i].adapterId == aAdapterId )
       
  2096 			{
       
  2097 			if ( !iAdapters[i].adapterPtr )
       
  2098 				{
       
  2099 				adapter = LoadNewAdapterL ( i, aAdapterId );
       
  2100 				if ( !adapter )
       
  2101 					{
       
  2102 					break;					
       
  2103 					}
       
  2104 				}
       
  2105 			HBufC8* luid = GetLuidAllocL ( aURI );
       
  2106 			CleanupStack::PushL ( luid );
       
  2107 			if ( aNodeType == ENSmlDmDDFLeaf )
       
  2108 				{
       
  2109 				iFetchLinkResult = ETrue;
       
  2110 				iAdapters[i].adapterPtr->FetchLeafObjectL ( aURI, *luid, KNullDesC8, KNSmlDmNoRef, KNSmlDmNoRef );
       
  2111 				iFetchLinkResult = EFalse;
       
  2112 				}
       
  2113 			else if ( aNodeType == ENSmlDmDDFNode )
       
  2114 				{
       
  2115 				CArrayFixFlat<TSmlDmMappingInfo>* prevURISegList = new ( ELeave ) CArrayFixFlat<TSmlDmMappingInfo> ( KNSmlDmGranularity );
       
  2116 				CleanupStack::PushL ( prevURISegList ); 
       
  2117 				PrevURISegListL ( aAdapterId, aURI, *prevURISegList );
       
  2118 				iFetchLinkResult = ETrue;
       
  2119 				iAdapters[i].adapterPtr->ChildURIListL ( aURI, *luid, *prevURISegList, KNSmlDmNoRef, KNSmlDmNoRef );			
       
  2120 				iFetchLinkResult = EFalse;
       
  2121 				CleanupStack::PopAndDestroy();  // prevURISegList
       
  2122 				}
       
  2123 			CleanupStack::PopAndDestroy();  // luid
       
  2124 			if ( !iResultOtherAdapter )
       
  2125 				{
       
  2126 				break;				
       
  2127 				}
       
  2128 			aData.InsertL ( 0, *iResultOtherAdapter );
       
  2129 			aStatus = iStatusOtherAdapter;
       
  2130 			return;
       
  2131 			}
       
  2132 		}
       
  2133 	aData.InsertL ( 0, KNullDesC8 );
       
  2134 	aStatus = MSmlDmAdapter::EError;
       
  2135 	_DBG_FILE("CNSmlDMHostSession::FetchLinkFromAdapterL(): end");
       
  2136 	}
       
  2137 
       
  2138 // ------------------------------------------------------------------------------------------------
       
  2139 // CNSmlDMHostSession::GetLuidAllocL
       
  2140 //	MSmlDmCallback - callback interface.
       
  2141 // 	The function returns the LUID which is mapped to aURI. 
       
  2142 //	@param aURI. URI of the object. 
       
  2143 //	@return HBufC8*. If LUID is not found, the function allocates a null length string.
       
  2144 // ------------------------------------------------------------------------------------------------
       
  2145 //
       
  2146 HBufC8* CNSmlDMHostSession::GetLuidAllocL ( const TDesC8& aURI )
       
  2147 	{
       
  2148 	_DBG_FILE("CNSmlDMHostSession::GetLuidAllocL()");
       
  2149 	DBG_ARGS8( _S8("GetLuidAllocL(): aURI = %S "), &aURI );	
       
  2150 	HBufC8* luid = NULL;
       
  2151 	
       
  2152 	// Search matching URI from the internal buffer,
       
  2153 	// in case a mapping information is not yet written to db.
       
  2154 	TBool foundInternally(EFalse);
       
  2155 	if ( iMapUris && iMapLuids )
       
  2156 	    {
       
  2157 	    for ( TInt i(0); i<iMapUris->Count(); i++ )
       
  2158 	        {
       
  2159 	        if ( *iMapUris->At(i) == aURI )
       
  2160 	            {
       
  2161             	_DBG_FILE("CNSmlDMHostSession::GetLuidAllocL() LUID found from internal buffer");	            
       
  2162 	            foundInternally = ETrue;
       
  2163 	            luid = iMapLuids->At(i)->AllocL();
       
  2164 	            }
       
  2165 	        }
       
  2166 	    }
       
  2167 	if ( !foundInternally )
       
  2168 	    {
       
  2169     	if ( !iCbSessConnected )
       
  2170 	    	{
       
  2171 		    if ( iCbSession.Connect() != KErrNone )
       
  2172 			    {
       
  2173             	_DBG_FILE("ERROR!!! Connect to Callback server FAILED!!!!");			    
       
  2174 			    luid = HBufC8::NewL(0);
       
  2175 			    return luid;
       
  2176 			    }
       
  2177 		    else 
       
  2178 			    {
       
  2179 			    iCbSessConnected = ETrue;
       
  2180 			    }
       
  2181 		    }
       
  2182         TUint32 adId(0);
       
  2183         TNSmlDmDDFFormat nodeType;
       
  2184 	    if ( AdapterIdFromUriL ( aURI, adId, nodeType ) )
       
  2185 	        {
       
  2186 		    luid = iCbSession.GetLuidAllocL ( adId, aURI );
       
  2187 	        }
       
  2188 	    else
       
  2189 		    {
       
  2190             _DBG_FILE("ERROR!!! AdapterId could not be found using URI");		    
       
  2191 		    luid = HBufC8::NewL(0);	
       
  2192 		    }							
       
  2193 	    }
       
  2194 	DBG_ARGS8( _S8("GetLuidAllocL(): returned luid = %S "), luid );	    
       
  2195     return luid;			
       
  2196 	}
       
  2197 
       
  2198 // ------------------------------------------------------------------------------------------------
       
  2199 // CNSmlDMHostSession::PrevURISegListL
       
  2200 //	Auxiliary function which is called from FetchLinkFromAdapterL.
       
  2201 //	Gets a previous URI segment list from the callback server. 
       
  2202 //	This function is called only during the fetchlink callback and when
       
  2203 //	the requested object is an interior node object.
       
  2204 //	@param aAdapterId. Identifies the requested adapter. 
       
  2205 //	@param aURI. A path to the requested interior node object. 
       
  2206 //	@param aURISegList. A reference parameter where callback server writes a list of the child nodes.
       
  2207 // ------------------------------------------------------------------------------------------------
       
  2208 //
       
  2209 void CNSmlDMHostSession::PrevURISegListL ( TUint32 aAdapterId, const TDesC8& aURI, CArrayFixFlat<TSmlDmMappingInfo>& aURISegList )
       
  2210 	{
       
  2211 	_DBG_FILE("CNSmlDMHostSession::PrevURISegListL(): begin");
       
  2212 	if ( !iCbSessConnected )
       
  2213 		{
       
  2214 		if ( iCbSession.Connect() != KErrNone )
       
  2215 			{
       
  2216 			return;			
       
  2217 			}
       
  2218 		else 
       
  2219 			{
       
  2220 			iCbSessConnected = ETrue;			
       
  2221 			}
       
  2222 		}
       
  2223 	iCbSession.GetUriSegListL ( aAdapterId, aURI, aURISegList );
       
  2224 	_DBG_FILE("CNSmlDMHostSession::PrevURISegListL(): end");
       
  2225 	}
       
  2226 
       
  2227 // ------------------------------------------------------------------------------------------------
       
  2228 // CNSmlDMHostSession::AdapterIdFromUriL
       
  2229 //	Checks if the given URI can be found from the combined DDF structure.
       
  2230 //	If not, most probably the URI belongs to some other DM adapter which this server
       
  2231 //	is not capable of loading.  
       
  2232 //	@param aURI. A path to the requested interior node object or leaf object. 
       
  2233 //	@param aAdapterId. A reference parameter, to which DM Utils service writes the implementation
       
  2234 //	                   UID of the adapter - if the URI was found. 
       
  2235 //	@param aNodeType. A reference parameter defining whether the object is a leaf or a node.
       
  2236 //	@return TBool. ETrue if both the URI and the adapter id were found, EFalse otherwise.
       
  2237 // ------------------------------------------------------------------------------------------------
       
  2238 //
       
  2239 TBool CNSmlDMHostSession::AdapterIdFromUriL ( const TDesC8& aURI, TUint32& aAdapterId, TNSmlDmDDFFormat& aNodeType )
       
  2240 	{
       
  2241 	_DBG_FILE("CNSmlDMHostSession::GetAdapterIdL(): begin");
       
  2242 	CNSmlDmNodeList* finalPtr = NULL;
       
  2243 	aNodeType = iMgmtTree->FindNameFromNodeListL ( aURI, finalPtr );
       
  2244 	if ( finalPtr )
       
  2245 		{
       
  2246 		TUint8 sessId(0);
       
  2247 		finalPtr->GetImplUid ( aAdapterId, sessId );
       
  2248 		if ( aAdapterId || sessId )
       
  2249 			{
       
  2250 			return ETrue;			
       
  2251 			}
       
  2252 		}
       
  2253 	_DBG_FILE("CNSmlDMHostSession::GetAdapterIdL(): end");
       
  2254 	return EFalse;
       
  2255 	}
       
  2256 
       
  2257 // ------------------------------------------------------------------------------------------------
       
  2258 // CNSmlDMHostSession::RoomForLargeData
       
  2259 //	Auxiliary function for defining how much there is room in the chunk for actual
       
  2260 //	data of the large object.
       
  2261 //	@param aType. MIME type of the result item.
       
  2262 //	@return TInt. The available size in bytes.
       
  2263 // ------------------------------------------------------------------------------------------------
       
  2264 //
       
  2265 inline TInt CNSmlDMHostSession::RoomForLargeData ( const TDesC8& aType ) const
       
  2266 	{
       
  2267 	return ( ( ( ( KNSmlDmHostChunkMaxSize - KNSmlDmChunkMarginal ) - KNSmlDmConstItemSize ) - aType.Size() ) - 2 );
       
  2268 	}
       
  2269 
       
  2270 // ------------------------------------------------------------------------------------------------
       
  2271 // void CNSmlDMHostSession::GetAccessPointImplUid()
       
  2272 //	This function is called for getting the accesspoint implementation uid.  
       
  2273 //	@return TUint. The Implementationuid.
       
  2274 // ------------------------------------------------------------------------------------------------
       
  2275 //
       
  2276 	TUint CNSmlDMHostSession::GetAccessPointImplUid()
       
  2277 	{
       
  2278 	if(FeatureManager::FeatureSupported(KFeatureIdFfDmConnmoAdapter))
       
  2279 		{
       
  2280 			return KNSmlDMConnMOAdapterImplUid;
       
  2281 		}
       
  2282 	else
       
  2283 		{
       
  2284 			return KNSmlInternetAdapterImplUid;
       
  2285 		}	
       
  2286 	}
       
  2287 
       
  2288 // ------------------------------------------------------------------------------------------------
       
  2289 // Client code to access other dm host servers
       
  2290 // ------------------------------------------------------------------------------------------------
       
  2291 
       
  2292 
       
  2293 // ------------------------------------------------------------------------------------------------
       
  2294 // RNSmlDMFetchLink::RNSmlDMFetchLink()
       
  2295 //	C++ constructor.
       
  2296 // ------------------------------------------------------------------------------------------------
       
  2297 //
       
  2298 RNSmlDMFetchLink::RNSmlDMFetchLink()
       
  2299 	: RSessionBase()
       
  2300 	{
       
  2301 	}
       
  2302 
       
  2303 // ------------------------------------------------------------------------------------------------
       
  2304 // RNSmlDMFetchLink::OpenL()
       
  2305 //	Creates a session (i.e. a connection) to the other DM Host Server.
       
  2306 //	@param aServer. The server name (e.g. "nsmldmhostserver2").	
       
  2307 // ------------------------------------------------------------------------------------------------
       
  2308 //
       
  2309 void RNSmlDMFetchLink::OpenL ( const TDesC& aServer )
       
  2310 	{
       
  2311 	_DBG_FILE("RNSmlDMFetchLink::OpenL(): begin");
       
  2312 	TVersion version ( KNSmlDmHostServerVerMajor, KNSmlDmHostServerVerMinor, KNSmlDmHostServerVerBuild );
       
  2313 	TInt res = CreateSession ( aServer, version );
       
  2314 	User::LeaveIfError( res );	
       
  2315 	_DBG_FILE("RNSmlDMFetchLink::OpenL(): end");
       
  2316 	}
       
  2317 
       
  2318 // ------------------------------------------------------------------------------------------------
       
  2319 // RNSmlDMFetchLink::SendChunkHandleL
       
  2320 //	Sets a handle to the global memory chunk. 
       
  2321 //	The client DM Host Server will send a handle of the already existing chunk.
       
  2322 //	In other words, no new chunks are created for the FetchLink IPC command. 
       
  2323 //	Additionally, a server id is sent in the message.
       
  2324 //	This id tells to the common session class that it is acting as a 'FetchLink server'.
       
  2325 //	This information is needed in order to avoid destroying the chunk still in use.
       
  2326 //	@param aHandle. A handle to the existing global memory chunk. 
       
  2327 // ------------------------------------------------------------------------------------------------
       
  2328 //
       
  2329 void RNSmlDMFetchLink::SendChunkHandleL ( const RChunk& aChunk ) const
       
  2330 	{
       
  2331 	_DBG_FILE("RNSmlDMFetchLink::SendChunkHandleL(): begin");
       
  2332 	TIpcArgs args;
       
  2333  	args.Set ( 0, aChunk );
       
  2334  	args.Set ( 1, KNSmlDmFetchLinkServerId );
       
  2335  	TInt error = SendReceive ( ENSmlDMChunkHandle, args );
       
  2336 	User::LeaveIfError ( error );		
       
  2337 	_DBG_FILE("RNSmlDMFetchLink::SendChunkHandleL(): end");
       
  2338 	}
       
  2339 
       
  2340 
       
  2341 // ------------------------------------------------------------------------------------------------
       
  2342 // void RNSmlDMFetchLink::FetchL()
       
  2343 //	Issues a fetch IPC command to the other DM Host Server.  
       
  2344 //	@param aURI. A path to the requested interior node object or the leaf object. 
       
  2345 //	@param aReadStream. A reference to the stream in the chunk from where the result
       
  2346 //	  				    can be read. 
       
  2347 //	@return HBufC8*. The result of the fetch link command.
       
  2348 // ------------------------------------------------------------------------------------------------
       
  2349 //
       
  2350 HBufC8* RNSmlDMFetchLink::FetchL ( const TDesC8& aURI, RReadStream& aReadStream ) const
       
  2351 	{
       
  2352 	_DBG_FILE("RNSmlDMFetchLink::FetchL(): begin");	
       
  2353     User::LeaveIfError ( SendReceive ( ENSmlDMFetchLink, TIpcArgs( &aURI ) ) );
       
  2354 
       
  2355 	TInt ansLength = aReadStream.ReadUint16L();
       
  2356     HBufC8* answer = HBufC8::NewLC ( ansLength );
       
  2357     TPtr8 answerPtr = answer->Des();
       
  2358     if ( ansLength )
       
  2359     	{
       
  2360     	aReadStream.ReadL ( answerPtr );    	
       
  2361     	}
       
  2362     CleanupStack::Pop();  // answer
       
  2363     _DBG_FILE("RNSmlDMFetchLink::FetchL(): end");
       
  2364     return answer;
       
  2365 	}
       
  2366 	
       
  2367 
       
  2368 
       
  2369 // ------------------------------------------------------------------------------------------------
       
  2370 // void RNSmlDMFetchLink::Close()
       
  2371 //	Closes a session to the other DM Host Server.  
       
  2372 // ------------------------------------------------------------------------------------------------
       
  2373 //
       
  2374 void RNSmlDMFetchLink::Close()
       
  2375 	{
       
  2376 	_DBG_FILE("RNSmlDMFetchLink::Close(): begin");
       
  2377 	// close session	
       
  2378 	RSessionBase::Close();
       
  2379 	_DBG_FILE("RNSmlDMFetchLink::Close(): end");
       
  2380 	}