metadataengine/server/src/mdsserversession.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This is Metadata engine server session file
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "mdsserversession.h"
       
    21 
       
    22 #include "mdcresult.h"
       
    23 #include "mdslogger.h"
       
    24 #include "mdcserializationbuffer.h"
       
    25 #include "mdsserver.h"
       
    26 #include "mdsmanipulationengine.h"
       
    27 #include "mdsmaintenanceengine.h"
       
    28 #include "mdsfindengine.h"
       
    29 #include "mdsobjectlocklist.h"
       
    30 #include "mdsnotifier.h"
       
    31 #include "mdsschema.h"
       
    32 #include "mdcresult.h"
       
    33 #include "mdcitem.h"
       
    34 
       
    35 #include "mdsutils.h"
       
    36 
       
    37 __USES_LOGGER
       
    38 
       
    39 
       
    40 // ========================= MEMBER FUNCTIONS ==================================
       
    41 
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // NewL
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CMdSServerSession* CMdSServerSession::NewL( CMdSServer& aServer )
       
    48     {
       
    49     CMdSServerSession* self = CMdSServerSession::NewLC( aServer );
       
    50     CleanupStack::Pop( self );
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // NewLC
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CMdSServerSession* CMdSServerSession::NewLC( CMdSServer& aServer )
       
    59     {
       
    60     CMdSServerSession* self = new ( ELeave ) CMdSServerSession( aServer );
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL();
       
    63     return self;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // ConstructL
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 void CMdSServerSession::ConstructL()
       
    71     {
       
    72     iServer.IncrementSessions();
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Default constructor
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 CMdSServerSession::CMdSServerSession( CMdSServer& aServer )
       
    80 	: iServer( aServer ) 
       
    81     {
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // Destructor
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CMdSServerSession::~CMdSServerSession()
       
    89     {
       
    90     iFindEngines.ResetAndDestroy();
       
    91     iFindEngines.Close();
       
    92 
       
    93 	iServer.LockList().UnlockBySession( *this );
       
    94 	iServer.Notifier().RemoveEntriesBySession( *this );
       
    95     iServer.DecrementSessions();
       
    96     
       
    97     // purge any pending notifications
       
    98 	iNotificationCache.ResetAndDestroy();
       
    99     iNotificationCache.Close();
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Service the server message 
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 void CMdSServerSession::ServiceL( const RMessage2& aMessage )
       
   107     {
       
   108     __LOG2( ELogServer, "ServiceL message: %d uid: %.8x", 
       
   109     		aMessage.Function(),
       
   110     		aMessage.Identity());
       
   111 
       
   112 	if( iServer.BackupOrRestoreRunning() )
       
   113 		{
       
   114 		aMessage.Complete( KErrServerBusy );
       
   115 		return;
       
   116 		}
       
   117 
       
   118     TRAPD( err, ServiceFunctionL( aMessage ) );
       
   119     if( err != KErrNone )
       
   120         {
       
   121         aMessage.Complete( err );
       
   122         }
       
   123     }
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // ServiceFunctionL
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 void CMdSServerSession::ServiceFunctionL( const RMessage2& aMessage )
       
   130     {
       
   131     TInt complete(KErrNone);
       
   132 
       
   133     switch ( aMessage.Function() )
       
   134         {
       
   135         // Add items 
       
   136         case EAdd:
       
   137             {
       
   138             AddL( aMessage );
       
   139             break;
       
   140             }
       
   141 
       
   142         // Add relation def
       
   143         case EAddRelationDef:
       
   144         	{
       
   145         	AddRelationDefL(aMessage);
       
   146         	iServer.Notifier().NotifySchemaAddedL();
       
   147         	break;
       
   148         	}
       
   149 
       
   150         // Add event def
       
   151         case EAddEventDef:
       
   152         	{
       
   153         	AddEventDefL(aMessage);
       
   154         	iServer.Notifier().NotifySchemaAddedL();
       
   155         	break;
       
   156         	}        	
       
   157 
       
   158         // Remove items
       
   159         case ERemove:
       
   160             {
       
   161             RemoveL( aMessage );
       
   162             break;
       
   163             }
       
   164 
       
   165         // Find items
       
   166         case EFind:
       
   167             {
       
   168             TRAPD( err, FindL( aMessage ) );
       
   169             if ( err != KErrNone )
       
   170                 {
       
   171                 if ( err != KErrNotReady )
       
   172                     {
       
   173                     FindCancel( aMessage, err );
       
   174                     return;
       
   175                     }
       
   176 	            complete = err;
       
   177                 }
       
   178             break;
       
   179             }
       
   180 
       
   181         case EAsyncFind:
       
   182             {
       
   183             TRAPD( err, FindAsyncL( aMessage ) );
       
   184             if ( err != KErrNone)
       
   185             	{
       
   186             	if(err != KErrNotReady)
       
   187 	                {
       
   188 	                FindCancel( aMessage, err );
       
   189 	                return;
       
   190 	                }
       
   191 	    		complete = err;
       
   192             	}
       
   193             else 
       
   194             	{
       
   195             	// if no error don't complete message
       
   196             	return;
       
   197             	}
       
   198             break;
       
   199             }
       
   200 
       
   201         case EContinueAsyncFind:
       
   202             {
       
   203             TRAPD( err, FindContinueAsyncL( aMessage ) );
       
   204             if ( err != KErrNone )
       
   205                 {
       
   206                 FindCancel( aMessage, err );
       
   207                 }
       
   208             
       
   209             // if no error don't complete message
       
   210             return;
       
   211             }
       
   212 
       
   213         case ECancelFind:
       
   214             {
       
   215             FindCancel( aMessage, KErrNone );
       
   216             return;
       
   217             }
       
   218 
       
   219         case ECheckObject:
       
   220             {
       
   221             CheckObjectL( aMessage );
       
   222             break;
       
   223             }
       
   224             
       
   225         case EGetData:
       
   226             {
       
   227             GetDataL( aMessage );
       
   228             break;
       
   229             }
       
   230 
       
   231         case ECancelObject:
       
   232             {
       
   233             CancelObjectL( aMessage );
       
   234             break;
       
   235             }
       
   236 
       
   237         case EUpdate:
       
   238             {
       
   239             UpdateL( aMessage );
       
   240             break;
       
   241             }
       
   242 
       
   243         case ERegister:
       
   244             {
       
   245             RegisterL( aMessage );
       
   246             break;
       
   247             }
       
   248 
       
   249         case EListen:
       
   250             {
       
   251             ListenL( aMessage );
       
   252             
       
   253             // if no error don't complete message
       
   254             return;
       
   255             }
       
   256 
       
   257         case EUnregister:
       
   258             {
       
   259             UnregisterL( aMessage );
       
   260             break;
       
   261             }
       
   262             
       
   263         case EShutdown:
       
   264             {
       
   265             ShutdownL( aMessage );
       
   266             break;
       
   267             }
       
   268             
       
   269         case EImportMetadata:
       
   270         case EAsyncImportMetadata:
       
   271             {
       
   272             ImportMetadataL( aMessage );
       
   273             break;
       
   274             }
       
   275 
       
   276         case EExportMetadata:
       
   277         case EAsyncExportMetadata:
       
   278             {
       
   279             ExportMetadataL( aMessage );
       
   280             break;
       
   281             }
       
   282 
       
   283         case EImportSchema:
       
   284             {
       
   285             TRAPD( err, ImportSchemaL( aMessage ) );
       
   286             if(err != KErrNone && err != KErrAccessDenied)
       
   287             	{
       
   288             	// Map all other errors to KErrCorrupt
       
   289             	err = KErrCorrupt;
       
   290             	}
       
   291 
       
   292             if(!err)
       
   293             	{
       
   294             	iServer.Notifier().NotifySchemaAddedL();
       
   295             	}
       
   296 
       
   297 			complete = err;
       
   298             break;
       
   299             }
       
   300 
       
   301         case EAddMemoryCard:
       
   302         	{
       
   303         	AddMemoryCardL( aMessage );
       
   304         	break;
       
   305         	}
       
   306 
       
   307     	case EGetMemoryCard:
       
   308         	{
       
   309         	GetMemoryCardL( aMessage );
       
   310         	break;
       
   311         	}
       
   312 
       
   313     	case ECheckMemoryCard:
       
   314         	{
       
   315         	CheckMemoryCardL( aMessage );
       
   316         	break;
       
   317         	}
       
   318 
       
   319     	case ESetMedia:
       
   320     		{
       
   321     		SetMediaL( aMessage );
       
   322     		break;
       
   323     		}
       
   324         
       
   325     	case EGetMedia:
       
   326     		{
       
   327     		GetMediaL( aMessage );
       
   328     		break;
       
   329     		}
       
   330         	
       
   331     	case EGetPresentMedias:
       
   332     		{
       
   333     		GetPresentMediasL( aMessage );
       
   334     		break;
       
   335     		}
       
   336         	
       
   337     	case ESetFileToPresent:
       
   338         	{
       
   339         	complete = KErrNotSupported;
       
   340         	break;
       
   341         	}
       
   342 
       
   343     	case ESetFilesToPresent:
       
   344         	{
       
   345         	SetFilesToPresentL(aMessage);
       
   346         	break;
       
   347         	}        	
       
   348         	
       
   349     	case ESetFilesToNotPresent:
       
   350         	{
       
   351         	SetFilesToNotPresentL(aMessage);
       
   352         	break;
       
   353         	}
       
   354 
       
   355     	case ERemoveFilesNotPresent:
       
   356         	{
       
   357         	RemoveFilesNotPresentL(aMessage);
       
   358         	break;
       
   359         	}
       
   360 
       
   361     	case EGetSchemaVersion:
       
   362         	{
       
   363         	GetSchemaVersionL(aMessage);
       
   364         	break;
       
   365         	}
       
   366 
       
   367     	case ESetObjectToPresentByGuid:
       
   368     		{
       
   369     		SetObjectToPresentByGuidL(aMessage);
       
   370     		break;
       
   371     		}
       
   372         	
       
   373         case EResetDB:
       
   374         	{
       
   375 			#ifdef _DEBUG
       
   376         	iServer.ResetDBL();
       
   377 			#else
       
   378         	User::Leave( KErrNotSupported );
       
   379 			#endif
       
   380         	break;
       
   381         	}
       
   382         	
       
   383         case ESetHarvestingPrioritizationChunk:
       
   384         	{
       
   385         	iServer.SetHarvestingPrioritizationChunkL( aMessage, 0 );
       
   386         	break;
       
   387         	}
       
   388 
       
   389         case EAddHarvestingPrioritizationObserver:
       
   390         	{
       
   391             User::LeaveIfError( iServer.AddHarvestingPrioritizationObserver( aMessage ) );
       
   392             // if no error don't complete message
       
   393         	return;
       
   394         	}
       
   395 
       
   396         case ECancelHarvestingPrioritizationObserver:
       
   397         	{
       
   398         	User::LeaveIfError( iServer.CancelHarvestingPrioritizationObserver() );
       
   399         	break;
       
   400         	}
       
   401 
       
   402         case EChangePath:
       
   403         	{
       
   404         	ChangePathL( aMessage );
       
   405         	break;
       
   406         	}
       
   407         	
       
   408         case EChangeMediaId:
       
   409         	{
       
   410         	ChangeMediaIdL( aMessage );
       
   411         	break;
       
   412         	}	
       
   413         	
       
   414         case ESetPending:
       
   415         	{
       
   416         	SetPendingL( aMessage );
       
   417         	}
       
   418         	break;
       
   419 
       
   420         case EResetPending:
       
   421         	{
       
   422         	ResetPendingL( aMessage );
       
   423         	}
       
   424         	break;
       
   425 
       
   426         case EGetPendingCount:
       
   427         	{
       
   428         	complete = GetPendingCountL( aMessage );
       
   429         	}
       
   430         	break;
       
   431 
       
   432         case EGetPending:
       
   433         	{
       
   434         	complete = GetPendingL( aMessage );
       
   435         	}
       
   436         	break;
       
   437         	
       
   438         default:
       
   439             iServer.PanicClient( aMessage, EBadRequest );
       
   440         }
       
   441 
       
   442     aMessage.Complete(complete);
       
   443     }
       
   444 
       
   445 // ---------------------------------------------------------------------------
       
   446 // QueriesCompleteL
       
   447 // ---------------------------------------------------------------------------
       
   448 //
       
   449 void CMdSServerSession::QueriesCompleteL()
       
   450 	{
       
   451 	// check if all queries are complete
       
   452     for( TInt i = iFindEngines.Count() - 1; i >= 0; i--)
       
   453     	{
       
   454     	CMdSFindEngine* fe = iFindEngines[i];
       
   455 
       
   456     	if( !fe->IsComplete() )
       
   457         	{
       
   458     	    User::Leave( KErrNotReady );
       
   459 			}
       
   460     	}		
       
   461 	}
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // AddL
       
   465 // ---------------------------------------------------------------------------
       
   466 //
       
   467 void CMdSServerSession::AddL( const RMessage2& aMsg )
       
   468     {
       
   469 	if ( iServer.DiskFull() )
       
   470 		{
       
   471 		User::Leave( KErrDiskFull );
       
   472 		}
       
   473 
       
   474 	TInt resultLength = aMsg.GetDesLengthL( 1 );
       
   475 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( aMsg, 0 );	
       
   476 	CMdCSerializationBuffer* resultBuf = CMdCSerializationBuffer::NewLC( resultLength );
       
   477 
       
   478     iServer.Manipulate().AddL( *buffer, *resultBuf, this );
       
   479 
       
   480 	aMsg.WriteL(1, resultBuf->Buffer() );
       
   481 	CleanupStack::PopAndDestroy( resultBuf );
       
   482 	CleanupStack::PopAndDestroy( buffer );
       
   483     }
       
   484 
       
   485 // ---------------------------------------------------------------------------
       
   486 // AddRelationDefL
       
   487 // ---------------------------------------------------------------------------
       
   488 //	
       
   489 void CMdSServerSession::AddRelationDefL(const RMessage2 &aMsg)	
       
   490 	{
       
   491 	TDefId namespaceId = aMsg.Int0();
       
   492 
       
   493 	TInt nameLength = aMsg.GetDesLength( EAddDefArgDefName );
       
   494 	if ( nameLength < 0 )
       
   495 		{
       
   496 		User::Leave( KErrBadDescriptor );
       
   497 		}
       
   498 	HBufC* namebuf = HBufC::NewLC( nameLength );
       
   499 	TPtr bufdes = namebuf->Des();
       
   500 	aMsg.ReadL( EAddDefArgDefName, bufdes );
       
   501 
       
   502 	iServer.Manipulate().AddRelationDefL( namespaceId, *namebuf );
       
   503 
       
   504 	iServer.Schema().SerializeToSharedMemoryL();
       
   505 	
       
   506 	CleanupStack::PopAndDestroy( namebuf );
       
   507 	}
       
   508 	
       
   509 void CMdSServerSession::AddEventDefL(const RMessage2 &aMsg)	
       
   510 	{
       
   511 	TDefId namespaceId = aMsg.Int0();
       
   512 
       
   513 	TInt nameLength = aMsg.GetDesLength( EAddDefArgDefName );
       
   514 	if ( nameLength < 0 )
       
   515 		{
       
   516 		User::Leave( KErrBadDescriptor );
       
   517 		}
       
   518 	HBufC* namebuf = HBufC::NewLC( nameLength );
       
   519 	TPtr bufdes = namebuf->Des();
       
   520 	aMsg.ReadL( EAddDefArgDefName, bufdes );
       
   521 
       
   522 	iServer.Manipulate().AddEventDefL( namespaceId, *namebuf );
       
   523 	
       
   524 	iServer.Schema().SerializeToSharedMemoryL();
       
   525 	
       
   526 	CleanupStack::PopAndDestroy( namebuf );
       
   527 	}	
       
   528 
       
   529 // RemoveL
       
   530 // ---------------------------------------------------------------------------
       
   531 //
       
   532 void CMdSServerSession::RemoveL( const RMessage2& aMsg )
       
   533     {
       
   534 	TInt successfulLength = aMsg.GetDesLengthL( 1 );
       
   535 
       
   536     CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( aMsg, 0 );
       
   537     CMdCSerializationBuffer* successfullBuffer = CMdCSerializationBuffer::NewLC( successfulLength );
       
   538 
       
   539     iServer.Manipulate().RemoveL( *buffer, *successfullBuffer );
       
   540     aMsg.WriteL( 1, successfullBuffer->Buffer() );
       
   541     CleanupStack::PopAndDestroy( 2, buffer ); // successfullBuffer, buffer
       
   542     }
       
   543 
       
   544 // ---------------------------------------------------------------------------
       
   545 // FindL
       
   546 // ---------------------------------------------------------------------------
       
   547 //
       
   548 void CMdSServerSession::FindL( const RMessage2& aMsg )
       
   549     {
       
   550     __LOGLB( ELogServer, "-> Find Sync" );
       
   551 
       
   552 	// check that all queries are complete
       
   553 	QueriesCompleteL();
       
   554 
       
   555     TInt serializedCriteriaLength = aMsg.GetDesLength( EFindArgConditions );
       
   556 	CMdCSerializationBuffer* serializedCriteriaBuffer = CMdCSerializationBuffer::NewLC( serializedCriteriaLength );
       
   557 	TPtr8 serializedCriteriaBufferPtr(serializedCriteriaBuffer->Buffer());
       
   558 	aMsg.ReadL( EFindArgConditions, serializedCriteriaBufferPtr );
       
   559 
       
   560     const TUint32 queryId = (TUint32)aMsg.Int0();
       
   561 
       
   562     CMdSFindEngine* find = CMdSFindEngine::NewLC( queryId, *this,
       
   563         iServer.LockList(), iServer.Schema() );
       
   564     find->SetFindCriteria( serializedCriteriaBuffer );
       
   565     find->CreateResultSet( aMsg );
       
   566     find->SetFindParams( (TUint32)aMsg.Int3() );
       
   567 
       
   568     CleanupStack::Pop( find );
       
   569     CleanupStack::Pop( serializedCriteriaBuffer );
       
   570     CleanupStack::PushL( find );
       
   571 
       
   572     // check clients user level (whether access to confidential data or not)
       
   573     TUserLevel userLevel = EUserLevelNone;
       
   574     userLevel = ( aMsg.HasCapability( ECapabilityReadDeviceData ) ?
       
   575                   EUserLevelDeviceAccess : EUserLevelNormal );
       
   576 
       
   577     find->FindL( userLevel );
       
   578 
       
   579     CleanupStack::Pop( find );
       
   580     
       
   581     iFindEngines.Append( find );
       
   582     }
       
   583 
       
   584 // ---------------------------------------------------------------------------
       
   585 // FindAsyncL
       
   586 // ---------------------------------------------------------------------------
       
   587 //
       
   588 void CMdSServerSession::FindAsyncL( const RMessage2& aMsg )
       
   589     {
       
   590     __LOGLB( ELogServer, "-> Find Async" );
       
   591 
       
   592     // check that all queries are complete
       
   593     QueriesCompleteL();
       
   594 
       
   595     TInt serializedCriteriaLength = aMsg.GetDesLength( EFindArgConditions );
       
   596 	CMdCSerializationBuffer* serializedCriteriaBuffer = CMdCSerializationBuffer::NewLC( serializedCriteriaLength );
       
   597 	TPtr8 serializedCriteriaBufferPtr(serializedCriteriaBuffer->Buffer());
       
   598 	aMsg.ReadL( EFindArgConditions, serializedCriteriaBufferPtr );
       
   599 
       
   600     const TUint32 queryId = (TUint32)aMsg.Int0();
       
   601 
       
   602     CMdSFindEngine* find = CMdSFindEngine::NewLC( queryId, *this,
       
   603         iServer.LockList(), iServer.Schema() );
       
   604     find->SetFindCriteria( serializedCriteriaBuffer );
       
   605     find->CreateResultSet( aMsg );
       
   606     find->SetFindParams( (TUint32)aMsg.Int3() );
       
   607 
       
   608     CleanupStack::Pop( find );
       
   609     CleanupStack::Pop( serializedCriteriaBuffer );    
       
   610     CleanupStack::PushL( find );
       
   611 
       
   612     // check clients user level (whether access to confidential data or not)
       
   613     TUserLevel userLevel = EUserLevelNone;
       
   614     userLevel = ( aMsg.HasCapability( ECapabilityReadDeviceData ) ?
       
   615                   EUserLevelDeviceAccess : EUserLevelNormal );
       
   616 
       
   617     find->FindAsyncL( userLevel );
       
   618 
       
   619     CleanupStack::Pop( find );
       
   620     
       
   621     iFindEngines.Append( find );
       
   622     }
       
   623 
       
   624 // ---------------------------------------------------------------------------
       
   625 // FindContinueAsyncL
       
   626 // ---------------------------------------------------------------------------
       
   627 //
       
   628 void CMdSServerSession::FindContinueAsyncL( const RMessage2& aMsg )
       
   629     {
       
   630     __LOGLB( ELogServer, "-> Find Continue" );
       
   631 
       
   632     CMdSFindEngine* findEngine = NULL;
       
   633 
       
   634     const TUint32 queryId = (TUint32)aMsg.Int0();
       
   635 
       
   636     const TInt count = iFindEngines.Count();
       
   637 
       
   638     TInt feIndex = 0;
       
   639 
       
   640     for( TInt i = 0; i < count; i++ )
       
   641     	{
       
   642     	if( queryId == iFindEngines[i]->QueryId() )
       
   643     		{
       
   644         	findEngine = iFindEngines[i];
       
   645 
       
   646     		feIndex = i;
       
   647     		break;
       
   648     		}
       
   649     	}
       
   650 
       
   651     // no correct find engine found
       
   652     if( !findEngine )
       
   653     	{
       
   654     	User::Leave( KErrNotFound );
       
   655     	}
       
   656     // find engine is already complete
       
   657     else if( findEngine->IsComplete() )
       
   658     	{
       
   659     	delete findEngine;
       
   660 
       
   661     	iFindEngines.Remove( feIndex );
       
   662     	
       
   663     	User::Leave( KErrCompletion );
       
   664     	}
       
   665 
       
   666     findEngine->CreateResultSet( aMsg );
       
   667 
       
   668     findEngine->ContinueAsync();
       
   669     }
       
   670 
       
   671 // ---------------------------------------------------------------------------
       
   672 // FindCancel
       
   673 // ---------------------------------------------------------------------------
       
   674 //
       
   675 void CMdSServerSession::FindCancel( const RMessage2& aMsg, TInt aError )
       
   676     {
       
   677     __LOGLB( ELogServer, "-> Find Cancel" );
       
   678     
       
   679     const TUint32 queryId = (TUint32)aMsg.Int0();
       
   680 
       
   681     const TInt count = iFindEngines.Count();
       
   682     
       
   683     for( TInt i = 0; i < count; i++ )
       
   684     	{
       
   685     	CMdSFindEngine* findEngine = iFindEngines[i];
       
   686 
       
   687     	if( queryId == findEngine->QueryId() )
       
   688     		{
       
   689     		findEngine->Cancel( aError );
       
   690 
       
   691     		delete findEngine;
       
   692 
       
   693     		iFindEngines.Remove( i );
       
   694 
       
   695     		aMsg.Complete( aError );
       
   696     		return;
       
   697     		}
       
   698     	}
       
   699 
       
   700     // complete message even if correct find engine wasn't found
       
   701     aMsg.Complete( KErrNotFound );
       
   702     }
       
   703 
       
   704 // ---------------------------------------------------------------------------
       
   705 // CheckObjectL
       
   706 // ---------------------------------------------------------------------------
       
   707 //
       
   708 void CMdSServerSession::CheckObjectL( const RMessage2& aMsg )
       
   709 	{	
       
   710 	TPckgBuf<TDefId> namespaceDefIdPckg;
       
   711 	TInt nsDefIdLength = aMsg.GetDesLengthL( ECheckObjectArgNamespaceDefId );
       
   712 	if( nsDefIdLength != sizeof( TDefId ) )
       
   713 		{
       
   714 		User::Leave( KErrArgument );
       
   715 		}	
       
   716 	aMsg.Read( ECheckObjectArgNamespaceDefId, namespaceDefIdPckg );
       
   717 	const TDefId namespaceDefId = namespaceDefIdPckg();
       
   718 
       
   719 	const TInt resultBufferLength = aMsg.GetDesLengthL( ECheckObjectArgObject );
       
   720 	if( resultBufferLength <= 0 )
       
   721 		{
       
   722 		User::Leave( KErrBadDescriptor );
       
   723 		}
       
   724 
       
   725 	CMdCSerializationBuffer* buffer = NULL;
       
   726 
       
   727 	const TInt type = aMsg.Int0();
       
   728 	switch( type )
       
   729 		{
       
   730 		case ECheckObjectByUri:
       
   731 			{
       
   732 			TInt uriLength = aMsg.GetDesLengthL( ECheckObjectArgTypeValue );
       
   733 			if( uriLength <= 0 )
       
   734 				{
       
   735 				User::Leave( KErrArgument );
       
   736 				}
       
   737 		    RBuf uri;
       
   738 		    uri.Create( uriLength );
       
   739 		    CleanupClosePushL( uri );
       
   740 		    aMsg.ReadL( ECheckObjectArgTypeValue, uri );
       
   741 		    buffer = iServer.Manipulate().CheckObjectL( resultBufferLength, uri, namespaceDefId );
       
   742 			CleanupStack::PopAndDestroy( &uri );
       
   743 			CleanupStack::PushL( buffer );
       
   744 			}
       
   745 			break;
       
   746 
       
   747 		case ECheckObjectById:
       
   748 			{
       
   749 			TPckgBuf<TItemId> idPckg;
       
   750 			TInt idLength = aMsg.GetDesLengthL( ECheckObjectArgTypeValue );
       
   751 			if( idLength != sizeof( TItemId ) )
       
   752 				{
       
   753 				User::Leave( KErrArgument );
       
   754 				}
       
   755 			aMsg.Read( ECheckObjectArgTypeValue, idPckg );
       
   756 			const TItemId id = idPckg();
       
   757 			buffer = iServer.Manipulate().CheckObjectL( resultBufferLength, id, namespaceDefId );
       
   758 			CleanupStack::PushL( buffer );
       
   759 			}
       
   760 			break;
       
   761 
       
   762 		case ECheckObjectByIds:
       
   763 			{
       
   764 			TInt idLength = aMsg.GetDesLengthL( ECheckObjectArgTypeValue );
       
   765 			if( idLength < CMdCSerializationBuffer::KRequiredSizeForTUint32 )
       
   766 				{
       
   767 				User::Leave( KErrArgument );
       
   768 				}
       
   769 
       
   770 			CMdCSerializationBuffer* ids = CMdCSerializationBuffer::NewLC( idLength );
       
   771 			TPtr8 idsPtr( ids->Buffer() );
       
   772 			aMsg.ReadL( ECheckObjectArgTypeValue, idsPtr );
       
   773 			
       
   774 			buffer = iServer.Manipulate().CheckObjectL( resultBufferLength, *ids, namespaceDefId );
       
   775 			
       
   776 			CleanupStack::PopAndDestroy( ids );
       
   777 			
       
   778 			CleanupStack::PushL( buffer );
       
   779 			}
       
   780 			break;
       
   781 
       
   782 		default:
       
   783 			{
       
   784 			User::Leave( KErrNotSupported );
       
   785 			}
       
   786 		}
       
   787 
       
   788 	aMsg.WriteL( ECheckObjectArgObject, buffer->Buffer() );
       
   789 	CleanupStack::PopAndDestroy( buffer );
       
   790 	}
       
   791 
       
   792 // ---------------------------------------------------------------------------
       
   793 // GetDataL writes to the client the data from the required operation
       
   794 // ---------------------------------------------------------------------------
       
   795 //
       
   796 void CMdSServerSession::GetDataL( const RMessage2& aMsg )
       
   797     {
       
   798     TMdEServRqst serverRequest = (TMdEServRqst)aMsg.Int2();
       
   799 
       
   800     switch( serverRequest )
       
   801         {
       
   802         case ELoadSchema:
       
   803             {
       
   804             __LOGLB( ELogServer, "-> Get data (Schema)" );
       
   805 		    TPckgBuf<TInt> handleBuf( iServer.Schema().SharedMemoryHandleL() );
       
   806     		aMsg.WriteL( 1, handleBuf );
       
   807             }
       
   808             break;
       
   809         case EFind:
       
   810         case EAsyncFindSetReady:
       
   811         case EAsyncFindComplete:
       
   812             {
       
   813             __LOGLB( ELogServer, "-> Get data (Find)" );
       
   814 
       
   815             const TUint32 queryId = (TUint32)aMsg.Int0();
       
   816             
       
   817             CMdSFindEngine* findEngine = NULL;
       
   818 
       
   819             const TInt count = iFindEngines.Count();
       
   820 
       
   821             TInt findEngineIndex;
       
   822             for( findEngineIndex = 0; findEngineIndex < count; findEngineIndex++ )
       
   823             	{
       
   824             	if( queryId == iFindEngines[findEngineIndex]->QueryId() )
       
   825             		{
       
   826             		findEngine = iFindEngines[findEngineIndex];
       
   827             		break;
       
   828             		}
       
   829             	}
       
   830 
       
   831             if( !findEngine )
       
   832             	{
       
   833             	User::Leave( KErrNotFound );
       
   834             	}
       
   835 
       
   836             TMdCQueryLockType extraData = (TMdCQueryLockType)aMsg.Int3();
       
   837             if (extraData == ELock && aMsg.HasCapability( ECapabilityWriteDeviceData ) )
       
   838             	{
       
   839             	findEngine->LockFindResultObjectsL( iServer.LockList() );
       
   840             	}
       
   841 
       
   842             // write result to client's result buffer
       
   843             aMsg.WriteL( 1, findEngine->ResultsL().Buffer() );
       
   844 
       
   845             if ( serverRequest != EAsyncFindSetReady )
       
   846                 {
       
   847                 delete findEngine;
       
   848                 
       
   849                 iFindEngines.Remove( findEngineIndex );
       
   850                 }
       
   851             }
       
   852             break;
       
   853         case EListen:
       
   854             {
       
   855             __LOGLB( ELogServer, "-> Get data (Listen)" );
       
   856             CMdSNotifier::TEntry& entry = iServer.Notifier().FindEntryL( aMsg.Int3() );
       
   857             CMdCSerializationBuffer* buffer = entry.GetDataBuffer();
       
   858         	CleanupStack::PushL( buffer );
       
   859         	aMsg.WriteL( 1, buffer->Buffer() );
       
   860         	CleanupStack::PopAndDestroy( buffer );
       
   861             }
       
   862             break;
       
   863         default:
       
   864             User::Leave( KErrNotSupported );
       
   865         }
       
   866     }
       
   867 
       
   868 // ---------------------------------------------------------------------------
       
   869 // CancelObjectL
       
   870 // ---------------------------------------------------------------------------
       
   871 //
       
   872 void CMdSServerSession::CancelObjectL( const RMessage2& aMsg )
       
   873     {
       
   874     CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( aMsg, 0 );
       
   875 
       
   876     const TMdCItemIds& itemIds = TMdCItemIds::GetFromBufferL( *buffer );
       
   877     if ( itemIds.iObjectIds.iPtr.iCount != 1 )
       
   878     	{
       
   879     	User::Leave( KErrArgument );
       
   880     	}
       
   881 	TItemId idValue;
       
   882 	buffer->PositionL( itemIds.iObjectIds.iPtr.iOffset );
       
   883 	buffer->ReceiveL( idValue );
       
   884 
       
   885 	CMdsSchema& schema = iServer.Schema();
       
   886 	const CMdsNamespaceDef* namespaceDef = schema.GetNamespaceByIdL( itemIds.iNamespaceDefId );
       
   887 
       
   888 	if (iServer.LockList().IsLocked( *namespaceDef, idValue ) )
       
   889 		{
       
   890 		iServer.LockList().UnlockById( *namespaceDef, idValue );
       
   891 		}
       
   892 	else
       
   893 		{
       
   894 		idValue = KNoId;
       
   895 		}
       
   896 
       
   897 	buffer->PositionL( itemIds.iObjectIds.iPtr.iOffset );
       
   898 	buffer->InsertL( idValue );
       
   899 
       
   900     aMsg.WriteL( 0, buffer->Buffer() );
       
   901     CleanupStack::PopAndDestroy( buffer ); // buffer
       
   902     }
       
   903 
       
   904 // ---------------------------------------------------------------------------
       
   905 // UpdateL
       
   906 // ---------------------------------------------------------------------------
       
   907 //
       
   908 void CMdSServerSession::UpdateL( const RMessage2& aMsg )
       
   909     {
       
   910     TInt successfulLength = aMsg.GetDesLengthL( 1 );
       
   911     
       
   912     CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( aMsg, 0 );
       
   913     CMdCSerializationBuffer* successfullBuffer = CMdCSerializationBuffer::NewLC( successfulLength );
       
   914 
       
   915     iServer.Manipulate().UpdateL( *buffer, *successfullBuffer );
       
   916     aMsg.WriteL( 1, successfullBuffer->Buffer() );
       
   917     CleanupStack::PopAndDestroy( 2, buffer ); // successfullBuffer, buffer
       
   918     }
       
   919     
       
   920 // ---------------------------------------------------------------------------
       
   921 // RegisterL
       
   922 // ---------------------------------------------------------------------------
       
   923 //
       
   924 void CMdSServerSession::RegisterL( const RMessage2& aMsg )
       
   925     {
       
   926     __LOG3( ELogServer, "-> Register %u for NS: %u Type: %d",
       
   927         (TUint32)aMsg.Int0(), (TDefId)aMsg.Int3(), aMsg.Int1() );
       
   928 
       
   929     TInt length = aMsg.GetDesLength( 2 );
       
   930 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( length );
       
   931 	TPtr8 bufferPtr( buffer->Buffer() );
       
   932 	aMsg.ReadL( 2, bufferPtr );
       
   933     
       
   934     iServer.Notifier().CreateEntryL( aMsg.Int0(),
       
   935         (TConditionType)aMsg.Int1(), buffer, (TDefId)aMsg.Int3(), *this, 
       
   936         aMsg.HasCapability(ECapabilityReadDeviceData) );
       
   937     
       
   938     CleanupStack::Pop( buffer );                
       
   939     }
       
   940 
       
   941 // ---------------------------------------------------------------------------
       
   942 // ListenL
       
   943 // ---------------------------------------------------------------------------
       
   944 //
       
   945 void CMdSServerSession::ListenL( const RMessage2& aMsg )
       
   946     {
       
   947     const TInt notifierId = aMsg.Int0();
       
   948     __LOG1( ELogServer, "-> Listen %d", notifierId );
       
   949 
       
   950     CMdSNotifier::TEntry& entry = iServer.Notifier().FindEntryL( notifierId );
       
   951     entry.SetupForCallback( aMsg, 1 );
       
   952     
       
   953 	const TInt count = iNotificationCache.Count();
       
   954 	for( TInt i = 0; i < count; ++i )
       
   955 		{
       
   956 		const TInt notificationCacheId = iNotificationCache[i]->iId;
       
   957 		const TInt entryId = entry.Id();
       
   958 
       
   959 		if( notificationCacheId == entryId )
       
   960 			{
       
   961 			// The cache holds a new notification for this notifier, trigger it
       
   962 			CNotificationCacheItem* item = iNotificationCache[i];
       
   963 			iNotificationCache.Remove(i);
       
   964 
       
   965 			CleanupStack::PushL( item );
       
   966 
       
   967 			entry.TriggerCachedL( item->iCode, item->iData );
       
   968 			
       
   969 			// take ownership of iData from item and delete it
       
   970 			item->iData = NULL;
       
   971 			CleanupStack::PopAndDestroy( item );
       
   972 			
       
   973 			return;
       
   974 			}
       
   975 		}
       
   976     }
       
   977 
       
   978 // ---------------------------------------------------------------------------
       
   979 // UnregisterL 
       
   980 // ---------------------------------------------------------------------------
       
   981 //
       
   982 void CMdSServerSession::UnregisterL( const RMessage2& aMsg )
       
   983     {
       
   984     __LOG1( ELogServer, "-> Unregister %d", aMsg.Int0() );
       
   985     TInt id = aMsg.Int0();
       
   986     iServer.Notifier().RemoveEntryL(id);
       
   987 	TInt count = iNotificationCache.Count();
       
   988 	for(TInt i(count - 1); i >= 0; --i)
       
   989 		{
       
   990 		if(iNotificationCache[i]->iId == id)
       
   991 			{
       
   992 			delete iNotificationCache[i]->iData;
       
   993 			iNotificationCache[i]->iData = NULL;
       
   994 			iNotificationCache.Remove(i);
       
   995 			}
       
   996 		}
       
   997     }
       
   998 
       
   999 // ---------------------------------------------------------------------------
       
  1000 // CacheNotificationL caches a notifier event
       
  1001 // ---------------------------------------------------------------------------
       
  1002 //
       
  1003 
       
  1004 CMdCSerializationBuffer* CMdSServerSession::CombineBuffersL(CMdCSerializationBuffer& aLeftBuffer,
       
  1005 		CMdCSerializationBuffer& aRightBuffer )
       
  1006 	{
       
  1007 	// IDs are always stored in object IDs, 
       
  1008 	// even if those are actually relation or event IDs	
       
  1009 	
       
  1010 	aLeftBuffer.PositionL( KNoOffset );
       
  1011 	aRightBuffer.PositionL( KNoOffset );
       
  1012 
       
  1013 	const TMdCItemIds& leftItemIds = TMdCItemIds::GetFromBufferL( aLeftBuffer );
       
  1014 	const TMdCItemIds& rightItemIds = TMdCItemIds::GetFromBufferL( aRightBuffer );
       
  1015 
       
  1016 	// check that namespaces match
       
  1017 	if ( leftItemIds.iNamespaceDefId != rightItemIds.iNamespaceDefId )
       
  1018 		{
       
  1019 		return NULL;
       
  1020 		}
       
  1021 
       
  1022 	// create new buffer, which will contain combined results
       
  1023 	const TInt leftBufferSize = aLeftBuffer.Size();
       
  1024 	const TInt rightBufferSize = aRightBuffer.Size();
       
  1025 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC( 
       
  1026 			leftBufferSize + rightBufferSize );
       
  1027 
       
  1028 	TMdCItemIds combinedItemIds;
       
  1029 	
       
  1030 	// use left buffer's data as base line
       
  1031 	Mem::Copy( &combinedItemIds, &leftItemIds, sizeof( TMdCItemIds ) );
       
  1032 	
       
  1033 	// and add right buffer's relation count
       
  1034 	combinedItemIds.iObjectIds.iPtr.iCount += rightItemIds.iObjectIds.iPtr.iCount;
       
  1035 
       
  1036 	combinedItemIds.SerializeL( *buffer );
       
  1037 
       
  1038 	// move left and right buffer to begin of relations
       
  1039 	aLeftBuffer.PositionL( leftItemIds.iObjectIds.iPtr.iOffset );
       
  1040 	aRightBuffer.PositionL( rightItemIds.iObjectIds.iPtr.iOffset );
       
  1041 
       
  1042 	// copy IDs from left and right buffers to combined buffer
       
  1043 	for (TInt i = 0; i < leftItemIds.iObjectIds.iPtr.iCount; ++i)
       
  1044 		{
       
  1045 		TItemId id;
       
  1046 		aLeftBuffer.ReceiveL( id );
       
  1047 		buffer->InsertL( id );		
       
  1048 		}
       
  1049 
       
  1050 	for (TInt i = 0; i < rightItemIds.iObjectIds.iPtr.iCount; ++i)
       
  1051 		{
       
  1052 		TItemId id;
       
  1053 		aRightBuffer.ReceiveL( id );
       
  1054 		buffer->InsertL( id );		
       
  1055 		}
       
  1056 	
       
  1057 	CleanupStack::Pop( buffer );
       
  1058 	return buffer;
       
  1059 	}
       
  1060 
       
  1061 CMdCSerializationBuffer* CMdSServerSession::CombineItemBuffersL( CMdCSerializationBuffer& aLeftBuffer,
       
  1062 		CMdCSerializationBuffer& aRightBuffer )
       
  1063 	{
       
  1064 	// Current implementation supports only combining relations 
       
  1065 	// (used in remove relation item observer case)
       
  1066 
       
  1067 	aLeftBuffer.PositionL( KNoOffset );
       
  1068 	aRightBuffer.PositionL( KNoOffset );
       
  1069 
       
  1070 	const TMdCItems& leftItems = TMdCItems::GetFromBufferL( aLeftBuffer );
       
  1071 	const TMdCItems& rightItems = TMdCItems::GetFromBufferL( aRightBuffer );
       
  1072 
       
  1073 	// check that namespaces match
       
  1074 	if ( leftItems.iNamespaceDefId != rightItems.iNamespaceDefId )
       
  1075 		{
       
  1076 		return NULL;
       
  1077 		}
       
  1078 
       
  1079 	// create new buffer, which will contain combined results
       
  1080 	const TInt leftBufferSize = aLeftBuffer.Size();
       
  1081 	const TInt rightBufferSize = aRightBuffer.Size();
       
  1082 	CMdCSerializationBuffer* buffer = CMdCSerializationBuffer::NewLC(
       
  1083 			leftBufferSize + rightBufferSize );
       
  1084 
       
  1085 	TMdCItems combinedItems;
       
  1086 
       
  1087 	// use left buffer's data as base line
       
  1088 	Mem::Copy( &combinedItems, &leftItems, sizeof( TMdCItems ) );
       
  1089 
       
  1090 	// and add right buffer's relation count
       
  1091 	combinedItems.iRelations.iPtr.iCount += rightItems.iRelations.iPtr.iCount;
       
  1092 
       
  1093 	combinedItems.SerializeL( *buffer );
       
  1094 
       
  1095 	// move left and right buffer to begin of relations
       
  1096 	aLeftBuffer.PositionL( leftItems.iRelations.iPtr.iOffset );
       
  1097 	aRightBuffer.PositionL( rightItems.iRelations.iPtr.iOffset );
       
  1098 
       
  1099 	// copy relations from left and right buffers to combined buffer
       
  1100 	for ( TUint32 i = 0; i < leftItems.iRelations.iPtr.iCount; ++i )
       
  1101 		{
       
  1102 		TMdCRelation& leftRelation = CONST_CAST( TMdCRelation&, 
       
  1103 				TMdCRelation::GetFromBufferL( aLeftBuffer ) );
       
  1104 		
       
  1105 		leftRelation.SerializeL( *buffer );
       
  1106 		}
       
  1107 
       
  1108 	for ( TUint32 i = 0; i < rightItems.iRelations.iPtr.iCount; ++i )
       
  1109 		{
       
  1110 		TMdCRelation& rightRelation = CONST_CAST( TMdCRelation&, 
       
  1111 				TMdCRelation::GetFromBufferL( aRightBuffer ) );
       
  1112 		
       
  1113 		rightRelation.SerializeL( *buffer );
       
  1114 		}
       
  1115 
       
  1116 	CleanupStack::Pop( buffer );
       
  1117 	return buffer;
       
  1118 	}
       
  1119 
       
  1120 
       
  1121 // ---------------------------------------------------------------------------
       
  1122 // CacheNotificationL caches a notifier event
       
  1123 // ---------------------------------------------------------------------------
       
  1124 //
       
  1125 void CMdSServerSession::CacheNotificationL(TInt aId, TUint32 aCompleteCode, CMdCSerializationBuffer* aData)
       
  1126 	{
       
  1127 	CleanupStack::PushL( aData );
       
  1128 
       
  1129 	const TInt notificationCount = iNotificationCache.Count();
       
  1130 
       
  1131 	const TInt KMaxCachedItems = 256;
       
  1132 
       
  1133 	if(notificationCount > KMaxCachedItems)
       
  1134 		{
       
  1135 		User::Leave( KErrOverflow );
       
  1136 		}
       
  1137 
       
  1138 	if( aData )
       
  1139 		{
       
  1140 		// search for matching notification and combine new results to it
       
  1141 		for (TInt i = 0; i < notificationCount; ++i)
       
  1142 			{
       
  1143 			CNotificationCacheItem& notificationItem = *iNotificationCache[i];
       
  1144 			if ( notificationItem.iId == aId && notificationItem.iCode == aCompleteCode )
       
  1145 				{
       
  1146 				CMdCSerializationBuffer* data = NULL;
       
  1147 				// combine buffers
       
  1148 				if ( notificationItem.iCode != ERelationItemNotifyRemove )
       
  1149 					{
       
  1150 					data = CombineBuffersL( *notificationItem.iData, *aData );
       
  1151 					}
       
  1152 				else
       
  1153 					{
       
  1154 					data = CombineItemBuffersL( *notificationItem.iData, *aData );
       
  1155 					}
       
  1156 				// delete unecessary ones and change iData to new data
       
  1157 				if (data)
       
  1158 					{
       
  1159 					CleanupStack::PopAndDestroy( aData );
       
  1160 					
       
  1161 					delete notificationItem.iData;
       
  1162 					notificationItem.iData = data;
       
  1163 					return;
       
  1164 					}
       
  1165 				}
       
  1166 			}
       
  1167 		}
       
  1168 
       
  1169 	// change ownership of aData to item
       
  1170 	CNotificationCacheItem* item = 
       
  1171 		new (ELeave) CNotificationCacheItem( aId, aCompleteCode, aData );
       
  1172 	CleanupStack::Pop( aData );
       
  1173 	CleanupStack::PushL( item );
       
  1174 
       
  1175 	iNotificationCache.AppendL( item );
       
  1176 
       
  1177 	CleanupStack::Pop( item );
       
  1178 	}
       
  1179 	
       
  1180 CMdSServer& CMdSServerSession::GetServer() const
       
  1181 	{
       
  1182 	return iServer;
       
  1183 	}
       
  1184 
       
  1185 // ---------------------------------------------------------------------------
       
  1186 // ShutdownL
       
  1187 // ---------------------------------------------------------------------------
       
  1188 //
       
  1189 void CMdSServerSession::ShutdownL( const RMessage2& /*aMsg*/ )
       
  1190     {
       
  1191     __LOGLB( ELogServer, "-> Shutdown session" );
       
  1192     
       
  1193     const TInt count = iFindEngines.Count();
       
  1194     
       
  1195     for( TInt i = 0; i < count; i++ )
       
  1196         {
       
  1197         CMdSFindEngine* fe = iFindEngines[i];
       
  1198         
       
  1199         fe->Cancel( KErrNone );
       
  1200         }
       
  1201     iFindEngines.ResetAndDestroy();
       
  1202     iFindEngines.Close();
       
  1203     }
       
  1204 
       
  1205 // ---------------------------------------------------------------------------
       
  1206 // SizeToRemoteL
       
  1207 // ---------------------------------------------------------------------------
       
  1208 //
       
  1209 void CMdSServerSession::SizeToRemoteL( const RMessage2& aMsg,
       
  1210     TInt aMessageSlot, TInt aSize)
       
  1211     {
       
  1212     __LOGLB( ELogServer, "-> Size to remote" );
       
  1213     TPckgBuf<TInt> sizeBuf( aSize );
       
  1214     aMsg.WriteL( aMessageSlot, sizeBuf );
       
  1215     }
       
  1216 
       
  1217 // ---------------------------------------------------------------------------
       
  1218 // ImportSchemaL
       
  1219 // ---------------------------------------------------------------------------
       
  1220 //
       
  1221 void CMdSServerSession::ImportSchemaL( const RMessage2& aMsg )
       
  1222     {
       
  1223     __LOGLB( ELogServer, "-> Import schema" );
       
  1224 
       
  1225     const TInt fileNameLength = aMsg.GetDesLength( 0 );
       
  1226     if ( fileNameLength <= 0 )
       
  1227     	{
       
  1228     	User::Leave( KErrBadDescriptor );
       
  1229     	}
       
  1230 
       
  1231     RBuf fileName;
       
  1232     fileName.Create( fileNameLength );
       
  1233     CleanupClosePushL( fileName );
       
  1234     aMsg.ReadL( 0, fileName );
       
  1235 
       
  1236     TVendorId id = aMsg.VendorId();
       
  1237     iServer.Maintenance().ImportSchemaL( iServer.Schema(), fileName, id.iId );
       
  1238     CleanupStack::PopAndDestroy( &fileName );
       
  1239     }
       
  1240 
       
  1241 // ---------------------------------------------------------------------------
       
  1242 // ImportMetadataL
       
  1243 // ---------------------------------------------------------------------------
       
  1244 //
       
  1245 void CMdSServerSession::ImportMetadataL( const RMessage2& aMsg )
       
  1246     {
       
  1247     __LOGLB( ELogServer, "-> Import (async?) metadata" );
       
  1248     
       
  1249     const TInt fileNameLength = aMsg.GetDesLength( 0 );
       
  1250     if ( fileNameLength <= 0 )
       
  1251     	{
       
  1252     	User::Leave( KErrBadDescriptor );
       
  1253     	}
       
  1254     
       
  1255     RBuf fileName;
       
  1256     fileName.Create( fileNameLength );
       
  1257     CleanupClosePushL( fileName );
       
  1258     aMsg.ReadL( 0, fileName );
       
  1259 
       
  1260     // if result is negative then result is error code
       
  1261     // else result is item import fail count
       
  1262     TInt result = iServer.Maintenance().ImportMetadataL( 
       
  1263     		iServer.Manipulate(), iServer.Schema(), fileName );
       
  1264 
       
  1265     CleanupStack::PopAndDestroy( &fileName );
       
  1266 
       
  1267     TPckgBuf<TInt> failBuf( result );
       
  1268     aMsg.WriteL( 1, failBuf );
       
  1269     }
       
  1270     
       
  1271 // ---------------------------------------------------------------------------
       
  1272 // ExportMetadataL
       
  1273 // ---------------------------------------------------------------------------
       
  1274 //
       
  1275 void CMdSServerSession::ExportMetadataL( const RMessage2& aMsg )
       
  1276     {
       
  1277     const TInt fileNameLength = aMsg.GetDesLength( 0 );
       
  1278     if ( fileNameLength <= 0 )
       
  1279     	{
       
  1280     	User::Leave( KErrBadDescriptor );
       
  1281     	}
       
  1282 
       
  1283     RBuf fileName;
       
  1284     fileName.Create( fileNameLength );
       
  1285     CleanupClosePushL( fileName );
       
  1286     aMsg.ReadL( 0, fileName );
       
  1287 
       
  1288     CMdCSerializationBuffer* items = CMdCSerializationBuffer::NewLC( aMsg, 1 );
       
  1289 
       
  1290     iServer.Maintenance().ExportMetadataL( iServer.Schema(), fileName, *items );
       
  1291 
       
  1292     CleanupStack::PopAndDestroy( 2, &fileName ); // items, fileName
       
  1293     }
       
  1294 
       
  1295 void CMdSServerSession::AddMemoryCardL(const RMessage2& aMessage)
       
  1296 	{
       
  1297 	TUint32 mediaId;
       
  1298     TPckg<TUint32> mediaIdPckg( mediaId );
       
  1299 	aMessage.ReadL( 0, mediaIdPckg );
       
  1300 
       
  1301 	iServer.Manipulate().AddMemoryCardL( mediaId );
       
  1302 	}
       
  1303 
       
  1304 void CMdSServerSession::GetMemoryCardL(const RMessage2& aMessage)
       
  1305 	{
       
  1306 	TUint32 mediaId;
       
  1307 
       
  1308 	iServer.Manipulate().GetMemoryCardL( mediaId );
       
  1309 
       
  1310     TPckg<TUint32> mediaIdPckg( mediaId );
       
  1311     aMessage.WriteL( 0, mediaIdPckg );
       
  1312 	}
       
  1313 
       
  1314 void CMdSServerSession::CheckMemoryCardL(const RMessage2& aMessage)
       
  1315 	{
       
  1316 	TUint32 mediaId;
       
  1317     TPckg<TUint32> mediaIdPckg( mediaId );
       
  1318 	aMessage.ReadL( 0, mediaIdPckg );
       
  1319 
       
  1320 	TBool result = iServer.Manipulate().CheckMemoryCardL( mediaId );
       
  1321 
       
  1322     TPckg<TBool> resultPckg( result );
       
  1323     aMessage.WriteL( 1, resultPckg );
       
  1324 	}
       
  1325 
       
  1326 void CMdSServerSession::SetMediaL(const RMessage2& aMessage)
       
  1327 	{
       
  1328 	TUint32 mediaId;
       
  1329     TPckg<TUint32> mediaIdPckg( mediaId );
       
  1330     aMessage.ReadL( 0, mediaIdPckg );
       
  1331     TChar drive;
       
  1332     TPckg<TChar> drivePckg( drive );
       
  1333     aMessage.ReadL( 1, drivePckg );
       
  1334     TBool presentState;
       
  1335     TPckg<TBool> presentStatePckg( presentState );
       
  1336     aMessage.ReadL( 2, presentStatePckg );
       
  1337 
       
  1338 	iServer.Manipulate().SetMediaL( mediaId, drive, presentState );
       
  1339 	}
       
  1340 
       
  1341 void CMdSServerSession::GetMediaL(const RMessage2& aMessage)
       
  1342 	{
       
  1343 	TUint32 mediaId;
       
  1344     TPckg<TUint32> mediaIdPckg( mediaId );
       
  1345     aMessage.ReadL( 0, mediaIdPckg );
       
  1346 
       
  1347     TChar drive;
       
  1348     TBool presentState;
       
  1349 
       
  1350     const TBool exists = iServer.Manipulate().GetMediaL( mediaId, drive, 
       
  1351     		presentState );
       
  1352 
       
  1353     if( exists )
       
  1354     	{
       
  1355     	TPckg<TChar> drivePckg( drive );
       
  1356     	aMessage.WriteL( 1, drivePckg );
       
  1357     	TPckg<TBool> presentStatePckg( presentState );
       
  1358     	aMessage.WriteL( 2, presentStatePckg );
       
  1359     	}
       
  1360 
       
  1361     TPckg<TBool> existsPckg( exists );
       
  1362     aMessage.WriteL( 3, existsPckg );
       
  1363     }
       
  1364 
       
  1365 void CMdSServerSession::GetPresentMediasL(const RMessage2& aMessage)
       
  1366 	{
       
  1367 	// buffer size for media info for every possible drive
       
  1368 	const TInt32 KMediaInfoSize = sizeof( TMdEMediaInfo ) * KMaxDrives;
       
  1369 
       
  1370 	RBuf8 mediaInfoBuffer;
       
  1371 	mediaInfoBuffer.Create( KMediaInfoSize );
       
  1372 	CleanupClosePushL( mediaInfoBuffer );
       
  1373 
       
  1374 	const TInt32 mediaCount = iServer.Manipulate().GetPresentMediasL( 
       
  1375 			mediaInfoBuffer );
       
  1376 
       
  1377 	TPckgC<TInt32> mediaCountPckg( mediaCount );
       
  1378 	aMessage.WriteL( 0, mediaCountPckg );
       
  1379 
       
  1380 	if( mediaCount > 0)
       
  1381 		{
       
  1382 		const TInt32 mediaInfosLength = aMessage.GetDesMaxLength( 1 );
       
  1383 		if ( mediaInfosLength < KMediaInfoSize )
       
  1384 			{
       
  1385 			User::Leave( KErrBadDescriptor );
       
  1386 			}
       
  1387 		
       
  1388 		aMessage.WriteL( 1, mediaInfoBuffer );
       
  1389 		}
       
  1390 
       
  1391 	CleanupStack::PopAndDestroy(); //mediaInfoBuffer
       
  1392 	}
       
  1393 
       
  1394 void CMdSServerSession::SetFilesToPresentL(const RMessage2& aMessage)
       
  1395 	{
       
  1396 	TMdSMediaIdAndCount mediaIdAndCount;
       
  1397 	TPckg<TMdSMediaIdAndCount> mediaIdAndCountPckg( mediaIdAndCount );
       
  1398 	aMessage.Read( 0, mediaIdAndCountPckg );
       
  1399 	
       
  1400 	CMdCSerializationBuffer* uris = CMdCSerializationBuffer::NewLC( aMessage, 1 );
       
  1401 	CMdCSerializationBuffer* fileInfos = CMdCSerializationBuffer::NewLC( aMessage, 2 );
       
  1402 
       
  1403 	TInt resultsSize = aMessage.GetDesMaxLengthL( 3 );
       
  1404 	if( resultsSize < ( mediaIdAndCount.iCount * CMdCSerializationBuffer::KRequiredSizeForTUint8 ) )
       
  1405 		{
       
  1406 		User::Leave( KErrBadDescriptor );
       
  1407 		}
       
  1408 	CMdCSerializationBuffer* results = CMdCSerializationBuffer::NewLC( resultsSize );
       
  1409 
       
  1410 	iServer.Manipulate().SetFilesToPresentL( 
       
  1411 			mediaIdAndCount.iMediaId, mediaIdAndCount.iCount, *uris, *fileInfos, 
       
  1412 			*results );
       
  1413 
       
  1414 	aMessage.WriteL( 3, results->Buffer() );
       
  1415 
       
  1416 	CleanupStack::PopAndDestroy( results );
       
  1417 	CleanupStack::PopAndDestroy( fileInfos );
       
  1418 	CleanupStack::PopAndDestroy( uris );
       
  1419 	}
       
  1420 
       
  1421 
       
  1422 void CMdSServerSession::SetFilesToNotPresentL(const RMessage2& aMessage)
       
  1423 	{
       
  1424 	TUint32 mediaId;
       
  1425     TPckg<TUint32> mediaIdPckg( mediaId );
       
  1426 	aMessage.Read( 0, mediaIdPckg );
       
  1427 	
       
  1428 	TBool startUp;
       
  1429     TPckg<TBool> startUpPckg( startUp );
       
  1430 	aMessage.Read( 1, startUpPckg );
       
  1431 	
       
  1432 	// only accept correct media IDs, other are silently ignored
       
  1433 	if( mediaId != 0 )
       
  1434 		{
       
  1435 		iServer.Manipulate().SetFilesToNotPresentL( mediaId, startUp );		
       
  1436 		}
       
  1437 	}
       
  1438 
       
  1439 void CMdSServerSession::RemoveFilesNotPresentL(const RMessage2& aMessage)
       
  1440 	{
       
  1441 	TUint32 mediaId;
       
  1442     TPckg<TUint32> mediaIdPckg( mediaId );
       
  1443 	aMessage.Read( 0, mediaIdPckg );
       
  1444 	
       
  1445 	TBool startUp;
       
  1446     TPckg<TBool> startUpPckg( startUp );
       
  1447 	aMessage.Read( 1, startUpPckg );
       
  1448 
       
  1449 	iServer.Manipulate().RemoveFilesNotPresentL( mediaId, startUp );
       
  1450 	}
       
  1451 
       
  1452 void CMdSServerSession::GetSchemaVersionL(const RMessage2& aMessage)
       
  1453 	{
       
  1454 	TInt majorVersion, minorVersion;
       
  1455 
       
  1456 	iServer.Manipulate().GetSchemaVersionL( majorVersion, minorVersion );
       
  1457 
       
  1458 	TPckg<TInt> pckgMajorVersion( majorVersion );
       
  1459 	TPckg<TInt> pckgMinorVersion( minorVersion );
       
  1460 	aMessage.WriteL( 0, pckgMajorVersion );
       
  1461 	aMessage.WriteL( 1, pckgMinorVersion );
       
  1462 	}
       
  1463 
       
  1464 void CMdSServerSession::SetObjectToPresentByGuidL(const RMessage2& aMessage)
       
  1465 	{
       
  1466 	TInt64 guidHigh;
       
  1467     TPckg<TInt64> guidHighPckg( guidHigh );
       
  1468 	aMessage.ReadL( ESetObjectToPresentByGuidArgGuidHigh, guidHighPckg );
       
  1469 	
       
  1470 	TInt64 guidLow;
       
  1471     TPckg<TInt64> guidLowPckg( guidLow );
       
  1472 	aMessage.ReadL( ESetObjectToPresentByGuidArgGuidLow, guidLowPckg );
       
  1473 	
       
  1474 	iServer.Manipulate().SetObjectToPresentByGuidL( guidHigh, guidLow );
       
  1475 	}
       
  1476 
       
  1477 void CMdSServerSession::ChangePathL(const RMessage2& aMessage)
       
  1478 	{
       
  1479 	const TInt oldPathLength = aMessage.GetDesLength( 0 );
       
  1480 	if ( oldPathLength <= 0 )
       
  1481 		{
       
  1482 		User::Leave( KErrBadDescriptor );
       
  1483 		}
       
  1484 
       
  1485 	const TInt newPathLength = aMessage.GetDesLength( 1 );
       
  1486 	if ( newPathLength <= 0 )
       
  1487 		{
       
  1488 		User::Leave( KErrBadDescriptor );
       
  1489 		}
       
  1490 
       
  1491 	RBuf oldPath;
       
  1492 	oldPath.Create( oldPathLength );
       
  1493     CleanupClosePushL( oldPath );
       
  1494     aMessage.ReadL( 0, oldPath );
       
  1495 
       
  1496     RBuf newPath;
       
  1497     newPath.Create( newPathLength );
       
  1498     CleanupClosePushL( newPath );
       
  1499     aMessage.ReadL( 1, newPath );
       
  1500     
       
  1501     iServer.Manipulate().ChangePathL( oldPath, newPath );
       
  1502     
       
  1503     CleanupStack::PopAndDestroy(&newPath);
       
  1504     CleanupStack::PopAndDestroy(&oldPath);
       
  1505 	}
       
  1506 
       
  1507 void CMdSServerSession::ChangeMediaIdL( const RMessage2& /*aMessage*/ )
       
  1508 	{
       
  1509 	iServer.Manipulate().ChangeMediaIdL();
       
  1510 	}
       
  1511 
       
  1512 void CMdSServerSession::SetPendingL(const RMessage2& aMessage)
       
  1513 	{
       
  1514 	const TInt serializedObjectIdsLength = aMessage.GetDesLength( 0 );
       
  1515 	
       
  1516 	if ( serializedObjectIdsLength < 0 || serializedObjectIdsLength % sizeof( TItemId ) )
       
  1517 		{
       
  1518 		User::Leave( KErrBadDescriptor );
       
  1519 		}
       
  1520 
       
  1521 	if( serializedObjectIdsLength > 0 )
       
  1522 		{
       
  1523 		RArray<TItemId> objectIds;
       
  1524 		CleanupClosePushL( objectIds );
       
  1525 	
       
  1526 		HBufC8* serializedObjectIds = HBufC8::NewLC( serializedObjectIdsLength );
       
  1527 	
       
  1528 		TPtr8 ptr = serializedObjectIds->Des();
       
  1529 		aMessage.ReadL( 0, ptr );
       
  1530 	
       
  1531 		DeserializeArrayL( *serializedObjectIds, objectIds );
       
  1532 	
       
  1533 		iServer.Manipulate().SetPendingL( objectIds );
       
  1534 		
       
  1535 		CleanupStack::PopAndDestroy( serializedObjectIds );
       
  1536 	
       
  1537 		CleanupStack::PopAndDestroy( &objectIds );
       
  1538 		}
       
  1539 	}
       
  1540 
       
  1541 void CMdSServerSession::ResetPendingL(const RMessage2& aMessage)
       
  1542 	{
       
  1543 	const TInt serializedObjectIdsLength = aMessage.GetDesLength( 0 );
       
  1544 	
       
  1545 	if ( serializedObjectIdsLength < 0 || serializedObjectIdsLength % sizeof( TItemId ) )
       
  1546 		{
       
  1547 		User::Leave( KErrBadDescriptor );
       
  1548 		}
       
  1549 	
       
  1550 	if( serializedObjectIdsLength > 0 )
       
  1551 		{
       
  1552 		RArray<TItemId> objectIds;
       
  1553 		CleanupClosePushL( objectIds );
       
  1554 		
       
  1555 		HBufC8* serializedObjectIds = HBufC8::NewLC( serializedObjectIdsLength );
       
  1556 		
       
  1557 		TPtr8 ptr = serializedObjectIds->Des();
       
  1558 		aMessage.ReadL( 0, ptr );
       
  1559 
       
  1560 		DeserializeArrayL( *serializedObjectIds, objectIds );
       
  1561 		
       
  1562 		iServer.Manipulate().ResetPendingL( objectIds );
       
  1563 		
       
  1564 		CleanupStack::PopAndDestroy( serializedObjectIds );
       
  1565 		
       
  1566 		CleanupStack::PopAndDestroy( &objectIds );
       
  1567 		}
       
  1568 	}
       
  1569 
       
  1570 TInt CMdSServerSession::GetPendingCountL(const RMessage2& aMessage)
       
  1571 	{
       
  1572 	TDefId objectDefId;
       
  1573 	TPckg<TDefId> objectDefIdPckg( objectDefId );
       
  1574 	aMessage.ReadL( 0, objectDefIdPckg );
       
  1575 	
       
  1576 	TInt result = iServer.Manipulate().GetPendingCountL( objectDefId );
       
  1577 
       
  1578 	TPckg<TInt> objectIdCountPckg( result );
       
  1579 	aMessage.WriteL( 1, objectIdCountPckg );
       
  1580 	
       
  1581 	return KErrNone;
       
  1582 	}
       
  1583 
       
  1584 TInt CMdSServerSession::GetPendingL(const RMessage2& aMessage)
       
  1585 	{
       
  1586 	TDefId objectDefId;
       
  1587 	TPckg<TDefId> objectDefIdPckg( objectDefId );
       
  1588 	aMessage.ReadL( 0, objectDefIdPckg );
       
  1589 
       
  1590 	const TInt serializedObjectIdsLength = aMessage.GetDesLength( 2 );
       
  1591 
       
  1592 	if ( serializedObjectIdsLength < sizeof(TItemId) || 
       
  1593 		 serializedObjectIdsLength % sizeof(TItemId) )
       
  1594 		{
       
  1595 		User::Leave( KErrBadDescriptor );
       
  1596 		}
       
  1597 
       
  1598 	RArray<TItemId> objectIds;
       
  1599 	CleanupClosePushL( objectIds );
       
  1600 
       
  1601 	TInt bufferSize = serializedObjectIdsLength / sizeof(TItemId);
       
  1602 
       
  1603 	TInt result = iServer.Manipulate().GetPendingL( objectDefId, bufferSize, objectIds );
       
  1604 
       
  1605 	const TInt objectIdCount = objectIds.Count();
       
  1606 
       
  1607 	TPckg<TInt> resultPckg( result );
       
  1608 	aMessage.WriteL( 3, resultPckg );
       
  1609 	
       
  1610 	TPckg<TInt> objectIdCountPckg( objectIdCount );
       
  1611 	aMessage.WriteL( 1, objectIdCountPckg );
       
  1612 
       
  1613 	if( objectIdCount > 0 )
       
  1614 		{
       
  1615 		HBufC8* serializedObjectIds = SerializeArrayL( objectIds );
       
  1616 		CleanupStack::PushL( serializedObjectIds );
       
  1617 
       
  1618 		aMessage.WriteL( 2, serializedObjectIds->Des() );
       
  1619 
       
  1620 		CleanupStack::PopAndDestroy( serializedObjectIds );
       
  1621 		}
       
  1622 
       
  1623 	CleanupStack::PopAndDestroy( &objectIds );
       
  1624 
       
  1625 	return KErrNone;
       
  1626 	}
       
  1627