omads/omadsextensions/datamod/src/nsmldatamodbase.cpp
changeset 0 dab8a81a92de
child 10 b922b9936679
equal deleted inserted replaced
-1:000000000000 0:dab8a81a92de
       
     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:  Versit data stripper and merger.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "NSmlDataModBase.h"
       
    21 #include "nsmldebug.h"
       
    22 #include "nsmlconstants.h"
       
    23 
       
    24 #include <versit.h>
       
    25 #include <vcal.h>
       
    26 #include <vcard.h>
       
    27 #include <s32mem.h>
       
    28 #include <stringpool.h>
       
    29 #include <vtoken.h>
       
    30 #include <e32property.h>
       
    31 #include <DataSyncInternalPSKeys.h>
       
    32 
       
    33 //Fix to Remove the Bad Compiler Warnings
       
    34 #ifndef __WINS__
       
    35 // This lowers the unnecessary compiler warning (armv5) to remark.
       
    36 // "Warning:  #174-D: expression has no effect..." is caused by 
       
    37 // DBG_ARGS8 macro in no-debug builds.
       
    38 #pragma diag_remark 174
       
    39 #endif
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ===============================
       
    42 
       
    43 // ------------------------------------------------------------------------------------------------
       
    44 // CNSmlDataModBase::~CNSmlDataModBase
       
    45 // Destructor.
       
    46 // ------------------------------------------------------------------------------------------------
       
    47 EXPORT_C CNSmlDataModBase::~CNSmlDataModBase()
       
    48 	{
       
    49 	}
       
    50 
       
    51 // ------------------------------------------------------------------------------------------------
       
    52 // CNSmlDataModBase::SetOwnStoreFormat
       
    53 // Sets own database format.
       
    54 // ------------------------------------------------------------------------------------------------
       
    55 EXPORT_C void CNSmlDataModBase::SetOwnStoreFormat( CSmlDataStoreFormat& aOwnStoreFormat )
       
    56 	{
       
    57 	_DBG_FILE("CNSmlDataModBase::SetOwnStoreFormat(): begin");
       
    58 	iOwnStoreFormat = &aOwnStoreFormat;
       
    59 	_DBG_FILE("CNSmlDataModBase::SetOwnStoreFormat(): end");
       
    60 	}
       
    61 
       
    62 // ------------------------------------------------------------------------------------------------
       
    63 // CNSmlDataModBase::SetPartnerStoreFormat
       
    64 // Sets remote database format.
       
    65 // ------------------------------------------------------------------------------------------------
       
    66 EXPORT_C void CNSmlDataModBase::SetPartnerStoreFormat( CSmlDataStoreFormat& aRemoteStoreFormat )
       
    67 	{
       
    68 	_DBG_FILE("CNSmlDataModBase::SetPartnerStoreFormat(): begin");
       
    69 	iRemoteStoreFormat = &aRemoteStoreFormat;
       
    70 	_DBG_FILE("CNSmlDataModBase::SetPartnerStoreFormat(): end");
       
    71 	}
       
    72 
       
    73 // ------------------------------------------------------------------------------------------------
       
    74 // CNSmlDataModBase::SetUsedMimeType
       
    75 // Sets mime type that is used for sending and receiving. 
       
    76 // This method can be called separately before every item.
       
    77 // ------------------------------------------------------------------------------------------------
       
    78 EXPORT_C TInt CNSmlDataModBase::SetUsedMimeType( const RStringF aMimeType, const RStringF aMimeVersion )
       
    79 	{
       
    80 	_DBG_FILE("CNSmlDataModBase::SetUsedMimeTypeL(): begin");
       
    81 	iUsedRemoteMimeType = -1;
       
    82 	if ( !iRemoteStoreFormat )
       
    83 		{
       
    84 		_DBG_FILE("CNSmlDataModBase::SetUsedMimeTypeL() - Not Found: end");
       
    85 		return KErrNotFound;
       
    86 		}
       
    87 	else
       
    88 		{
       
    89 		for ( TInt i = 0; i < iRemoteStoreFormat->MimeFormatCount(); i++ )
       
    90 			{
       
    91 			if ( iRemoteStoreFormat->MimeFormat( i ).MimeType().DesC().Compare( aMimeType.DesC() ) == 0 )
       
    92 				{
       
    93 				if ( iRemoteStoreFormat->MimeFormat( i ).MimeVersion().DesC().Compare( aMimeVersion.DesC() ) == 0 )
       
    94 					{
       
    95 					iUsedRemoteMimeType = i;
       
    96 					iMimeType = iRemoteStoreFormat->MimeFormat( i ).MimeType();
       
    97 					iMimeVersion = iRemoteStoreFormat->MimeFormat( i ).MimeVersion();
       
    98 					_DBG_FILE("CNSmlDataModBase::SetUsedMimeTypeL() - Found: end");
       
    99 					return KErrNone;
       
   100 					}
       
   101 				}
       
   102 			}
       
   103 		}
       
   104 	_DBG_FILE("CNSmlDataModBase::SetUsedMimeTypeL() - Not Found: end");
       
   105 	return KErrNotFound;
       
   106 	}
       
   107 	
       
   108 // ------------------------------------------------------------------------------------------------
       
   109 // CNSmlDataModBase::StripTxL
       
   110 // Strips data that is to be transmitted to the sync partner.
       
   111 // ------------------------------------------------------------------------------------------------
       
   112 EXPORT_C void CNSmlDataModBase::StripTxL( CBufBase& aItem )
       
   113 	{
       
   114 	_DBG_FILE("CNSmlDataModBase::StripTxL(): begin");
       
   115 	HBufC8* buf = HBufC8::NewLC(aItem.Size() * 2);
       
   116 	*buf = aItem.Ptr(0);
       
   117 	TPtr8 ptrBuf = buf->Des();
       
   118 
       
   119 	StripL(ptrBuf);
       
   120 
       
   121 	aItem.Reset();
       
   122 	aItem.InsertL(0, ptrBuf);
       
   123 	CleanupStack::PopAndDestroy(); // buf
       
   124 	_DBG_FILE("CNSmlDataModBase::StripTxL(): end");
       
   125 	}
       
   126 
       
   127 // ------------------------------------------------------------------------------------------------
       
   128 // CNSmlDataModBase::MergeRxL
       
   129 // Merges received item with item in exported from the local database.
       
   130 // ------------------------------------------------------------------------------------------------
       
   131 EXPORT_C void CNSmlDataModBase::MergeRxL( CBufBase& aNewItem, CBufBase& aOldItem, TBool aFieldLevel )
       
   132 	{
       
   133 	_DBG_FILE("CNSmlDataModBase::MergeRxL(): begin");
       
   134 	if( NeedsMerge() )
       
   135 		{
       
   136 		HBufC8* b1 = HBufC8::NewLC(aNewItem.Size() + aOldItem.Size());
       
   137 		*b1 = aNewItem.Ptr(0);
       
   138 		TPtr8 ptrB1 = b1->Des();
       
   139 		TPtr8 p2 = aOldItem.Ptr(0);
       
   140 
       
   141 		MergeL( ptrB1, p2, aFieldLevel );
       
   142 
       
   143 		aNewItem.Reset();
       
   144 		aNewItem.InsertL(0, ptrB1);
       
   145 		CleanupStack::PopAndDestroy(); // b1
       
   146 		}
       
   147 	else if ( iUsedRemoteMimeType == -1 )
       
   148 		{
       
   149 		User::Leave( KErrNotFound );
       
   150 		}
       
   151 	_DBG_FILE("CNSmlDataModBase::MergeRxL(): end");
       
   152 	}
       
   153 
       
   154 // ------------------------------------------------------------------------------------------------
       
   155 // CNSmlDataModBase::NeedsMerge
       
   156 // Checks whether merging is needed or not.
       
   157 // ------------------------------------------------------------------------------------------------
       
   158 EXPORT_C TBool CNSmlDataModBase::NeedsMerge()
       
   159 	{
       
   160 	_DBG_FILE("CNSmlDataModBase::NeedsMerge(): begin");
       
   161 	iUsedRemoteMimeType = -1;
       
   162 	if ( !iRemoteStoreFormat )
       
   163 		{
       
   164 		_DBG_FILE("CNSmlDataModBase::NeedsMerge() - EFalse: end");
       
   165 		return EFalse;
       
   166 		}
       
   167 	else
       
   168 		{
       
   169 		for ( TInt i = 0; i < iRemoteStoreFormat->MimeFormatCount(); i++ )
       
   170 			{
       
   171 			if ( iRemoteStoreFormat->MimeFormat( i ).MimeType().DesC().Compare( iMimeType.DesC() ) == 0 )
       
   172 				{
       
   173 				if ( iRemoteStoreFormat->MimeFormat( i ).MimeVersion().DesC().Compare( iMimeVersion.DesC() ) == 0 )
       
   174 					{
       
   175 					iUsedRemoteMimeType = i;
       
   176 					if ( iRemoteStoreFormat->MimeFormat( i ).PropertyCount() > 0 )
       
   177 						{
       
   178 						_DBG_FILE("CNSmlDataModBase::NeedsMerge() - ETrue: end");
       
   179 						return ETrue;
       
   180 						}
       
   181 					}
       
   182 				}
       
   183 			}
       
   184 		}
       
   185 	_DBG_FILE("CNSmlDataModBase::NeedsMerge() - EFalse: end");
       
   186 	return EFalse;
       
   187 	}
       
   188 	
       
   189 // ------------------------------------------------------------------------------------------------
       
   190 // CNSmlDataModBase::CNSmlDataModBase
       
   191 // Basic constructor of class.
       
   192 // ------------------------------------------------------------------------------------------------
       
   193 CNSmlDataModBase::CNSmlDataModBase()
       
   194 	{
       
   195 	iUsedOwnMimeType = -1;
       
   196 	iUsedRemoteMimeType = -1;
       
   197 	}
       
   198 	
       
   199 // ------------------------------------------------------------------------------------------------
       
   200 // CNSmlDataModBase::StripL
       
   201 // Strips data that is to be transmitted to the sync partner.
       
   202 // ------------------------------------------------------------------------------------------------
       
   203 void CNSmlDataModBase::StripL( TDes8& aItem )
       
   204 	{
       
   205 	_DBG_FILE("CNSmlDataModBase::StripL(): begin");
       
   206 	if ( !NeedsMerge() )
       
   207 		{
       
   208 		if ( iUsedRemoteMimeType == -1 )
       
   209 			{
       
   210 			User::Leave( KErrNotFound );
       
   211 			}
       
   212 		return;
       
   213 		}
       
   214 	TBool modified( EFalse );
       
   215 	CVersitParser* parser = ChildCreateParserLC();
       
   216 	RDesReadStream drs( aItem );
       
   217 	CleanupClosePushL( drs );
       
   218 	parser->InternalizeL( drs );
       
   219 
       
   220 	// Now we're ready to start analysis
       
   221 	CArrayPtr<CVersitParser>* entities = parser->ArrayOfEntities( EFalse );
       
   222 	if( entities )
       
   223 		{
       
   224 		for( TInt i = 0; i < entities->Count(); i++ )
       
   225 			{
       
   226 			StripEntityL( entities->At( i ), modified );
       
   227 			}
       
   228 		}
       
   229 	else
       
   230 		{
       
   231 		StripEntityL( parser, modified );
       
   232 		}
       
   233 
       
   234 	// Only update if anything was modified in process
       
   235 	if( modified )
       
   236 		{
       
   237 		aItem.Zero();
       
   238 		RDesWriteStream dws( aItem );
       
   239 		CleanupClosePushL( dws );
       
   240 		parser->ExternalizeL( dws );
       
   241 		dws.CommitL();
       
   242 		CleanupStack::PopAndDestroy(); // dws
       
   243 		}
       
   244 	CleanupStack::PopAndDestroy( 2 ); // drs, parser
       
   245 	_DBG_FILE("CNSmlDataModBase::StripL(): end");
       
   246 	}
       
   247 
       
   248 // ------------------------------------------------------------------------------------------------
       
   249 // CNSmlDataModBase::StripEntityL
       
   250 // Strips data that is to be transmitted to the sync partner from entity.
       
   251 // ------------------------------------------------------------------------------------------------
       
   252 void CNSmlDataModBase::StripEntityL( CVersitParser* aEntity, TBool& aModified ) const
       
   253 	{
       
   254 	_DBG_FILE("CNSmlDataModBase::StripEntityL(): begin");
       
   255 	StripAllNotOnPartnerListL( aEntity, aModified, ETrue );
       
   256 	_DBG_FILE("CNSmlDataModBase::StripEntityL(): end");
       
   257 	}
       
   258 
       
   259 // ------------------------------------------------------------------------------------------------
       
   260 // CNSmlDataModBase::StripAllOnPartnerListL
       
   261 // Strips all data from entity that is supported by remote server.
       
   262 // ------------------------------------------------------------------------------------------------
       
   263 void CNSmlDataModBase::StripAllOnPartnerListL( CVersitParser* aEntity, TBool& aModified, TBool aParamLevelCheck ) const
       
   264 	{
       
   265     _DBG_FILE("CNSmlDataModBase::StripAllOnPartnerListL(): begin");
       
   266 
       
   267     if( iRemoteStoreFormat->MimeFormat( iUsedRemoteMimeType ).PropertyCount() )
       
   268         {
       
   269         
       
   270         // Check correct Data Sync protocol
       
   271         TInt value( EDataSyncNotRunning );
       
   272         TSmlProtocolVersion usedSyncProtocol( ESmlVersion1_2 );
       
   273         TInt error = RProperty::Get( KPSUidDataSynchronizationInternalKeys,
       
   274                                      KDataSyncStatus,
       
   275                                      value );
       
   276         if ( error == KErrNone &&
       
   277              value == EDataSyncRunning )
       
   278             {
       
   279             usedSyncProtocol = ESmlVersion1_1_2;
       
   280             }
       
   281         
       
   282         TBool wasModified( EFalse );
       
   283 
       
   284         CArrayPtr<CParserProperty>* allProps = aEntity->ArrayOfProperties( EFalse );
       
   285         for( TInt i = 0; i < allProps->Count(); ) // Variable i is not increased here because size of count might be changes during loop
       
   286             {
       
   287             const CParserProperty& ownProperty = *allProps->At( i );
       
   288             CParserPropertyValue* ownValue = ownProperty.Value();
       
   289 
       
   290             TBool removeMe( EFalse );
       
   291             
       
   292             for( TInt i2 = 0; i2 < iRemoteStoreFormat->MimeFormat( iUsedRemoteMimeType ).PropertyCount(); i2++ )
       
   293                 {
       
   294                 const CSmlDataProperty& remoteProperty = iRemoteStoreFormat->MimeFormat( iUsedRemoteMimeType ).Property( i2 );
       
   295                 if( !ownProperty.Name().Compare( remoteProperty.Field().Name().DesC() ) )
       
   296                     {
       
   297                     if( remoteProperty.ParamCount() > 0 && aParamLevelCheck )
       
   298                         {
       
   299                         if ( usedSyncProtocol == ESmlVersion1_1_2 )
       
   300                             {
       
   301                             for( TInt i3 = 0; i3 < remoteProperty.ParamCount(); i3++ )
       
   302                                 {
       
   303                                 if( ownProperty.Param( remoteProperty.Param( i3 ).Field().Name().DesC() ) )
       
   304                                     {
       
   305                                     removeMe = ETrue;
       
   306                                     }
       
   307                                 }                           
       
   308                             }
       
   309                         else // ESmlVersion1_2
       
   310                             {
       
   311                             CArrayPtr<CParserParam>* ownerparamarray = ownProperty.ParamArray();
       
   312                             if(ownerparamarray != NULL)
       
   313                                 {
       
   314                                 for(TInt ownerparam = 0; ownerparam < ownerparamarray->Count(); ownerparam++)
       
   315                                     {
       
   316                                     removeMe = EFalse;
       
   317                                     const CParserParam& ownParam = *ownerparamarray->At( ownerparam );
       
   318                                      TPtrC8 ownparamname = ownParam.Name();
       
   319                                      TPtrC8 ownparamvalue = ownParam.Value();
       
   320                                      if(ownparamvalue == _L8(""))
       
   321                                         {
       
   322                                          for(TInt remoteparam = 0; remoteparam < remoteProperty.ParamCount(); remoteparam++)
       
   323                                             {
       
   324                                             TDesC8 remoteparamname = remoteProperty.Param( remoteparam ).Field().Name().DesC();
       
   325                                             const CSmlDataField& field = remoteProperty.Param( remoteparam ).Field();
       
   326 											if(field.EnumValueCount() > 0)
       
   327 												{
       
   328 	                                            for( TInt rmtenumcount = 0; rmtenumcount < field.EnumValueCount(); rmtenumcount++ )
       
   329 													{
       
   330 	                                                TPtrC8 rmtenumvalue = field.EnumValue( rmtenumcount ).DesC();
       
   331 													if( rmtenumvalue.Compare(ownparamname)== 0 )
       
   332 	                                                    {
       
   333 	                                                    removeMe = ETrue;
       
   334 														break;
       
   335 	                                                    }  
       
   336 	                                                } 
       
   337 												}
       
   338 											else
       
   339 												{
       
   340 												removeMe = ETrue;
       
   341 												break;
       
   342 												}
       
   343                                             }
       
   344                                         }
       
   345                                      else
       
   346                                         {
       
   347 										//Handling when the device supports VersitTokenType as "Encoding"    
       
   348 										if(ownparamname == KVersitTokenENCODING)
       
   349 											{
       
   350 											removeMe = ETrue;
       
   351 											}
       
   352 										else
       
   353 											{
       
   354 	                                         for(TInt remoteparam = 0; remoteparam < remoteProperty.ParamCount(); remoteparam++)
       
   355 	                                            {
       
   356 	                                            if(ownparamname.Compare(remoteProperty.Param( remoteparam ).Field().Name().DesC()) == 0)
       
   357 	                                                {
       
   358 	                                                const CSmlDataField& field = remoteProperty.Param( remoteparam ).Field();
       
   359 													if( field.EnumValueCount() > 0 )
       
   360 														{
       
   361 		                                                for( TInt rmtenumcount = 0; rmtenumcount < field.EnumValueCount(); rmtenumcount++ )
       
   362 		                                                    {
       
   363 		                                                     TPtrC8 rmtenumvalue = field.EnumValue( rmtenumcount ).DesC();
       
   364 		                                                     if( rmtenumvalue.Compare(ownparamvalue)== 0 )
       
   365 		                                                         {
       
   366 		                                                         removeMe = ETrue;
       
   367 																 break;
       
   368 		                                                         }  
       
   369 		                                                    } 
       
   370 														}
       
   371 													else
       
   372 														{
       
   373 														removeMe = ETrue;
       
   374 														break;
       
   375 														}
       
   376 	                                                }                                               
       
   377 	                                            }
       
   378 											}
       
   379                                         }
       
   380                                     	if( !removeMe )
       
   381                                         {
       
   382                                         	break;
       
   383                                         }	    
       
   384                                     } 
       
   385                                 }
       
   386                             else
       
   387                                 {
       
   388                                 removeMe = ETrue;
       
   389                                 }
       
   390                             }
       
   391                         }
       
   392                     else
       
   393                         {
       
   394                         removeMe = ETrue;
       
   395                         }
       
   396 
       
   397                     if( removeMe )
       
   398                         {
       
   399                         break;
       
   400                         }
       
   401                     }
       
   402                 }
       
   403             if( removeMe )
       
   404                 {
       
   405                 #ifdef __NSML_DEBUG__
       
   406                     TPtrC8 pn( ownProperty.Name() );
       
   407                     DBG_ARGS8(_S8("CNSmlDataModBase::StripAllOnPartnerListL(): Dropping %S"), &pn);
       
   408                 #endif // __NSML_DEBUG__
       
   409                 delete allProps->At( i );
       
   410                 allProps->Delete( i );
       
   411                 wasModified = ETrue;
       
   412                 aModified = ETrue;
       
   413                 }
       
   414             else
       
   415                 {
       
   416                 #ifdef __NSML_DEBUG__
       
   417                     TPtrC8 pn( ownProperty.Name() );
       
   418                     DBG_ARGS8(_S8("CNSmlDataModBase::StripAllOnPartnerListL(): NOT dropping %S"), &pn);
       
   419                 #endif // __NSML_DEBUG__
       
   420                 i++;
       
   421                 }
       
   422             }
       
   423         // can't use aModified as that may have been set earlier!
       
   424         if( wasModified )
       
   425             {
       
   426             allProps->Compress();
       
   427             }
       
   428         }
       
   429     _DBG_FILE("CNSmlDataModBase::StripAllOnPartnerListL(): end");
       
   430     }
       
   431 
       
   432 // ------------------------------------------------------------------------------------------------
       
   433 // CNSmlDataModBase::StripAllNotOnPartnerListL
       
   434 // Strips all data from entity that is not supported by remote server.
       
   435 // ------------------------------------------------------------------------------------------------
       
   436 void CNSmlDataModBase::StripAllNotOnPartnerListL( CVersitParser* aEntity, TBool& aModified, TBool aParamLevelCheck ) const
       
   437 	{
       
   438     _DBG_FILE("CNSmlDataModBase::StripAllNotOnPartnerListL(): begin");
       
   439     TInt remotepropertycount = iRemoteStoreFormat->MimeFormat( iUsedRemoteMimeType ).PropertyCount();
       
   440     if( iRemoteStoreFormat->MimeFormat( iUsedRemoteMimeType ).PropertyCount() )
       
   441         {
       
   442         
       
   443         // Check correct Data Sync protocol
       
   444         TInt value( EDataSyncNotRunning );
       
   445         TSmlProtocolVersion usedSyncProtocol( ESmlVersion1_2 );
       
   446         TInt error = RProperty::Get( KPSUidDataSynchronizationInternalKeys,
       
   447                                      KDataSyncStatus,
       
   448                                      value );
       
   449         if ( error == KErrNone &&
       
   450              value == EDataSyncRunning )
       
   451             {
       
   452             usedSyncProtocol = ESmlVersion1_1_2;
       
   453             }
       
   454 
       
   455         TBool wasModified( EFalse );
       
   456 
       
   457         CArrayPtr<CParserProperty>* allProps = aEntity->ArrayOfProperties( EFalse );
       
   458         for( TInt i = 0; i < allProps->Count(); ) // Variable i is not increased here because size of count might be changes during loop
       
   459             {
       
   460             const CParserProperty& ownProperty = *allProps->At( i );
       
   461             TBuf8<30> name = ownProperty.Name();
       
   462             CParserPropertyValue* ownValue = ownProperty.Value();
       
   463 
       
   464             TBool removeMe( ETrue );
       
   465             
       
   466             for( TInt i2 = 0; i2 < iRemoteStoreFormat->MimeFormat( iUsedRemoteMimeType ).PropertyCount(); i2++ )
       
   467                 {
       
   468                 const CSmlDataProperty& remoteProperty = iRemoteStoreFormat->MimeFormat( iUsedRemoteMimeType ).Property( i2 );
       
   469                 TPtrC8 remotename = remoteProperty.Field().Name().DesC();
       
   470                 if( !ownProperty.Name().Compare( remoteProperty.Field().Name().DesC() ) )
       
   471                     {
       
   472                     TInt remoteparamcount = remoteProperty.ParamCount();
       
   473                     if( remoteProperty.ParamCount() > 0 && aParamLevelCheck )
       
   474                         {
       
   475                         if ( usedSyncProtocol == ESmlVersion1_1_2 )
       
   476                             {
       
   477                             const CParserProperty* p = allProps->At( i );
       
   478                             TInt entityParamCount = (( CNSmlProperty* )p)->ParamCount();
       
   479                             if( entityParamCount > 0)
       
   480                             {
       
   481                             for( TInt i3 = 0; i3 < remoteProperty.ParamCount(); i3++ )
       
   482                                 {
       
   483                                 TPtrC8 remoteparamname = remoteProperty.Param( i3 ).Field().Name().DesC();
       
   484                                 if( ownProperty.Param( remoteProperty.Param( i3 ).Field().Name().DesC() ) )
       
   485                                     {
       
   486                                     removeMe = EFalse;
       
   487                                     }
       
   488                                 }                           
       
   489                             }
       
   490                             else
       
   491                             {
       
   492                                 removeMe = EFalse;
       
   493                             }
       
   494                             }
       
   495                         else // ESmlVersion1_2
       
   496                             {
       
   497                             CArrayPtr<CParserParam>* ownerparamarray = ownProperty.ParamArray();
       
   498                             if(ownerparamarray != NULL)
       
   499                                 {
       
   500                                 for(TInt ownerparam = 0; ownerparam < ownerparamarray->Count(); ownerparam++)
       
   501                                     {
       
   502 									removeMe = ETrue;
       
   503                                     const CParserParam& ownParam = *ownerparamarray->At( ownerparam );
       
   504                                      TPtrC8 ownparamname = ownParam.Name();
       
   505                                      TPtrC8 ownparamvalue = ownParam.Value();
       
   506                                      if(ownparamvalue == _L8(""))
       
   507                                         {
       
   508                                          for(TInt remoteparam = 0; remoteparam < remoteProperty.ParamCount(); remoteparam++)
       
   509                                             {
       
   510                                             TDesC8 remoteparamname = remoteProperty.Param( remoteparam ).Field().Name().DesC();
       
   511                                             const CSmlDataField& field = remoteProperty.Param( remoteparam ).Field();
       
   512 											if( field.EnumValueCount() > 0)
       
   513 												{
       
   514 												for( TInt rmtenumcount = 0; rmtenumcount < field.EnumValueCount(); rmtenumcount++ )
       
   515 													{
       
   516 													TPtrC8 rmtenumvalue = field.EnumValue( rmtenumcount ).DesC();
       
   517 													if( rmtenumvalue.Compare(ownparamname)== 0 )
       
   518 														{
       
   519 														removeMe = EFalse;
       
   520 														break;
       
   521 														}
       
   522 													} 
       
   523 												}
       
   524 											else
       
   525 												{
       
   526 												removeMe = EFalse;
       
   527 												break;
       
   528 												}											
       
   529                                             }
       
   530                                         }
       
   531                                      else
       
   532                                         {
       
   533 										//Handling when the device supports VersitTokenType as "Encoding"    
       
   534 										if(ownparamname == KVersitTokenENCODING)
       
   535 											{
       
   536 											removeMe = EFalse;
       
   537 											}
       
   538 										else
       
   539 											{
       
   540 	                                         for(TInt remoteparam = 0; remoteparam < remoteProperty.ParamCount(); remoteparam++)
       
   541 	                                            {
       
   542 	                                            TDesC8 remoteparamname = remoteProperty.Param( remoteparam ).Field().Name().DesC();
       
   543 	                                            if(ownparamname.Compare(remoteProperty.Param( remoteparam ).Field().Name().DesC()) == 0)
       
   544 	                                                {
       
   545 	                                                const CSmlDataField& field = remoteProperty.Param( remoteparam ).Field();
       
   546 													if( field.EnumValueCount() > 0)
       
   547 														{
       
   548 		                                                for( TInt rmtenumcount = 0; rmtenumcount < field.EnumValueCount(); rmtenumcount++ )
       
   549 		                                                    {
       
   550 		                                                     TPtrC8 rmtenumvalue = field.EnumValue( rmtenumcount ).DesC();
       
   551 		                                                     if( rmtenumvalue.Compare(ownparamvalue)== 0 )
       
   552 		                                                         {
       
   553 		                                                         removeMe = EFalse;
       
   554 																 break;
       
   555 		                                                         }  
       
   556 		                                                    }
       
   557 														}
       
   558 													else
       
   559 														{
       
   560 														removeMe = EFalse;
       
   561 														break;
       
   562 														}
       
   563 	                                                }
       
   564 												}
       
   565 											}                                 
       
   566                                         }
       
   567                                         if( removeMe )
       
   568                                         {
       
   569                                         	break;
       
   570                                         }	
       
   571                                     } 
       
   572                                 }
       
   573                             else
       
   574                                 {
       
   575                                 removeMe = EFalse;
       
   576                                 }
       
   577                             }
       
   578                         }
       
   579                     else
       
   580                         {
       
   581                         removeMe = EFalse;
       
   582                         }
       
   583 
       
   584                     if( !removeMe )
       
   585                         {
       
   586                         break;
       
   587                         }
       
   588                     }
       
   589                 }
       
   590             if( removeMe )
       
   591                 {
       
   592                 #ifdef __NSML_DEBUG__
       
   593                     TPtrC8 pn( ownProperty.Name() );
       
   594                     DBG_ARGS8(_S8("CNSmlDataModBase::StripAllNotOnPartnerListL(): Dropping %S"), &pn);
       
   595                 #endif // __NSML_DEBUG__
       
   596                 delete allProps->At( i );
       
   597                 allProps->Delete( i );
       
   598                 wasModified = ETrue;
       
   599                 aModified = ETrue;
       
   600                 }
       
   601             else
       
   602                 {
       
   603                 #ifdef __NSML_DEBUG__
       
   604                     TPtrC8 pn( ownProperty.Name() );
       
   605                     DBG_ARGS8(_S8("CNSmlDataModBase::StripAllNotOnPartnerListL(): NOT dropping %S"), &pn);
       
   606                 #endif // __NSML_DEBUG__
       
   607                 i++;
       
   608                 }
       
   609             }
       
   610         // can't use aModified as that may have been set earlier!
       
   611         if( wasModified )
       
   612             {
       
   613             allProps->Compress();
       
   614             }
       
   615         }
       
   616     _DBG_FILE("CNSmlDataModBase::StripAllNotOnPartnerListL(): end");
       
   617     }
       
   618 
       
   619 // ------------------------------------------------------------------------------------------------
       
   620 // CNSmlDataModBase::MergeL
       
   621 // Merges data from old item to new item.
       
   622 // ------------------------------------------------------------------------------------------------
       
   623 void CNSmlDataModBase::MergeL( TDes8& aNewItem, const TDesC8& aOldItem,TBool aFieldLevel )
       
   624 	{
       
   625 	TBool modified( EFalse );
       
   626 	CVersitParser* newItemParser = ChildCreateParserLC();
       
   627 	RDesReadStream newStream( aNewItem );
       
   628 	CleanupClosePushL( newStream );
       
   629 	newItemParser->InternalizeL( newStream );
       
   630 	CVersitParser* oldItemParser = ChildCreateParserLC();
       
   631 	RDesReadStream oldStream( aOldItem );
       
   632 	CleanupClosePushL( oldStream );
       
   633 	oldItemParser->InternalizeL( oldStream );
       
   634 
       
   635 	// Now we're ready to start analysis
       
   636 	CArrayPtr<CVersitParser>* newEntities = newItemParser->ArrayOfEntities( EFalse );
       
   637 	CArrayPtr<CVersitParser>* oldEntities = oldItemParser->ArrayOfEntities( ETrue );
       
   638 
       
   639 	if( newEntities && oldEntities )
       
   640 		{
       
   641 		CleanupPtrArrayPushL( oldEntities );
       
   642 		for( TInt i = 0; ( i < newEntities->Count() ) && ( i < oldEntities->Count() ); i++ )
       
   643 			{
       
   644 			MergeEntityL( newEntities->At( i ), oldEntities->At( i ), modified, aFieldLevel );
       
   645 			}
       
   646 		CleanupStack::PopAndDestroy(); // oldEntities
       
   647 		}
       
   648 	else
       
   649 		{
       
   650 		MergeEntityL( newItemParser, oldItemParser, modified, aFieldLevel );
       
   651 		}
       
   652 
       
   653 	// Only update if anything was modified in process
       
   654 	if ( modified )
       
   655 		{
       
   656 		aNewItem.Zero();
       
   657 		RDesWriteStream dws( aNewItem );
       
   658 		CleanupClosePushL( dws );
       
   659 		newItemParser->ExternalizeL( dws );
       
   660 		dws.CommitL();
       
   661 		CleanupStack::PopAndDestroy(); // dws
       
   662 		}
       
   663 
       
   664 	CleanupStack::PopAndDestroy( 4 ); // oldStream, oldItemParser, newStream, newItemParser
       
   665 	}
       
   666 
       
   667 // ------------------------------------------------------------------------------------------------
       
   668 // CNSmlDataModBase::MergeEntityL
       
   669 // Merges data from old entity to new entity.
       
   670 // ------------------------------------------------------------------------------------------------
       
   671 void CNSmlDataModBase::MergeEntityL( CVersitParser* aNewEntity, CVersitParser* aOldEntity, TBool& aModified, TBool aFieldLevel ) const
       
   672 	{
       
   673 	_DBG_FILE("CNSmlDataModBase::MergeEntityL(): begin");
       
   674 
       
   675 	// Remove all data that was not supposed to be supported by the partner but
       
   676 	// it was still sent to us.
       
   677 	StripAllNotOnPartnerListL( aNewEntity, aModified );
       
   678 	
       
   679 	// Remove all properties from old item that are supported by remote server.
       
   680 	// If it is field level then old this is not done.
       
   681 	if( !aFieldLevel )
       
   682 		{
       
   683 		StripAllOnPartnerListL( aOldEntity, aModified, ETrue );
       
   684 		CArrayPtr<CParserProperty>* mergeProps = aOldEntity->ArrayOfProperties( ETrue );
       
   685 		if( mergeProps )
       
   686 		    {
       
   687 			CleanupStack::PushL( mergeProps );
       
   688 			
       
   689 			for( TInt i = 0; i < mergeProps->Count(); i++ )
       
   690 				{
       
   691 				aNewEntity->AddPropertyL( mergeProps->At( i ), ETrue );
       
   692 				}
       
   693 			
       
   694 			CleanupStack::PopAndDestroy(); // mergeProps
       
   695 		    }
       
   696 		}
       
   697 	else
       
   698 		{
       
   699 		User::Leave( KErrNotSupported );
       
   700 		}
       
   701 	
       
   702 	#ifdef __NSML_DEBUG__
       
   703 		CArrayPtr<CParserProperty>* props = aNewEntity->ArrayOfProperties( EFalse );
       
   704 		for( TInt i = 0; i < props->Count(); i++ )
       
   705 			{
       
   706 			TBuf8<512> b;
       
   707 			const CParserProperty* p = props->At( i );
       
   708 			b = p->Name();
       
   709 			const CArrayPtr<CParserParam>* pa = ( ( CNSmlProperty* )p )->Parameters();
       
   710 			if( pa )
       
   711 				{
       
   712 				for( TInt i2 = 0; i2 < pa->Count(); i2++ )
       
   713 					{
       
   714 					b.Append( _L8(":") );
       
   715 					b.Append( pa->At( i2 )->Name() );
       
   716 					}
       
   717 				}
       
   718 			DBG_ARGS8(_S8("CNSmlDataModBase::MergeEntityL(): %S"), &b);
       
   719 			}
       
   720 	#endif // __NSML_DEBUG__
       
   721 	_DBG_FILE("CNSmlDataModBase::MergeEntityL(): end");
       
   722 	}
       
   723 
       
   724 // End of file