pimappsupport/vcardandvcal/tsrc/thelpers.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "thelpers.h"
       
    17 
       
    18 // System includes
       
    19 #include <e32math.h>
       
    20 #include <charconv.h>
       
    21 #include <concnf.h>
       
    22 #include <confndr.h>
       
    23 #include <conlist.h>
       
    24 #include <utf.h>
       
    25 #include <vprop.h>
       
    26 
       
    27 // Constants
       
    28 //_LIT8(KReplacementChars, " ?");
       
    29 
       
    30 
       
    31 //
       
    32 // VTestHelpers (source)
       
    33 //
       
    34 
       
    35 
       
    36 //****************************************************************************************
       
    37 TInt VTestHelpers::MakeRandomNumber(const TInt aLow, const TInt aHigh, TInt64& aSeed)
       
    38 	{
       
    39 	TReal initialRand = (Math::FRand(aSeed) * (aHigh - aLow));
       
    40 	TInt32 rand;
       
    41 
       
    42 	// Round to 0 decimal places, ie. the nearest whole numer
       
    43 	Math::Round(initialRand, initialRand, 0);
       
    44 	Math::Int(rand, initialRand);
       
    45 
       
    46 	return (aLow + rand);
       
    47 	}
       
    48 
       
    49 
       
    50 //****************************************************************************************
       
    51 /*void VTestHelpers::ConvertUnicodeToNarrowL(const TDesC16& aSource, TDes8& aDest, TUint aCharConvCharSet)
       
    52 	{
       
    53 	TInt stateValueDummyParameter = 0;
       
    54 
       
    55 	switch(aCharConvCharSet)
       
    56 		{
       
    57 		// UTF-X
       
    58 		case KVersitTest2InternalUidForUTF8:
       
    59 		{
       
    60 			User::LeaveIfError(CnvUtfConverter::ConvertFromUnicodeToUtf8(aDest, aSource));
       
    61 			break;
       
    62 		}
       
    63 		case KVersitTest2InternalUidForUTF7:
       
    64 		{
       
    65 			User::LeaveIfError(CnvUtfConverter::ConvertFromUnicodeToUtf7(aDest, aSource, stateValueDummyParameter));
       
    66 			break;
       
    67 		}
       
    68 		
       
    69 		// ISO-X
       
    70 		case KCharacterSetIdentifierIso88591:
       
    71 		case KCharacterSetIdentifierIso88592:
       
    72 		case KCharacterSetIdentifierIso88594:
       
    73 		case KCharacterSetIdentifierIso88595:
       
    74 		case KCharacterSetIdentifierIso88597:
       
    75 		case KCharacterSetIdentifierIso88599:
       
    76 		{
       
    77 			RFs fileServerSession;
       
    78 			CleanupClosePushL(fileServerSession);
       
    79 			User::LeaveIfError(fileServerSession.Connect());
       
    80 			CCnvCharacterSetConverter* characterSetConverter=CCnvCharacterSetConverter::NewLC();
       
    81 
       
    82 			CCnvCharacterSetConverter::TAvailability avail=CCnvCharacterSetConverter::ENotAvailable;
       
    83 			avail = characterSetConverter->PrepareToConvertToOrFromL(aCharConvCharSet, fileServerSession);
       
    84 			if (avail==CCnvCharacterSetConverter::ENotAvailable)
       
    85 				{
       
    86 				CleanupStack::PopAndDestroy(2);
       
    87 				return;
       
    88 				}
       
    89 
       
    90 			characterSetConverter->SetReplacementForUnconvertibleUnicodeCharactersL(KReplacementChars);
       
    91 			characterSetConverter->ConvertFromUnicode(aDest, aSource);	
       
    92 
       
    93 			CleanupStack::PopAndDestroy(2); // fileserver, charactersetconverter
       
    94 
       
    95 			TBuf8<1000> buffer1 = aDest;
       
    96 			TInt pos = 0;
       
    97 			const TInt length = aDest.Length();
       
    98 			for(TInt i=1; i<length; i+=2)
       
    99 				{
       
   100 				buffer1[pos] = buffer1[i];
       
   101 				pos++;
       
   102 				}
       
   103 			aDest = buffer1;
       
   104 			aDest.SetLength(length/2);
       
   105 			break;
       
   106 		}
       
   107 
       
   108 		// Not handled by CharConv
       
   109 		default:
       
   110 		{
       
   111 			const TInt length = aSource.Length();
       
   112 			aDest.SetLength(length);
       
   113 			for(TInt i = 0; i < length; i++)
       
   114 				aDest[i] = (TUint8) aSource[i];
       
   115 			break;
       
   116 		}
       
   117 		}
       
   118 
       
   119 	}*/
       
   120 
       
   121 
       
   122 //****************************************************************************************
       
   123 TBool VTestHelpers::DoesParameterAlreadyExist(CArrayPtr<CParserParam>* aArrayOfParams, const TDesC8& aName)
       
   124 	{
       
   125 	for(TInt i=0; i<aArrayOfParams->Count(); i++)
       
   126 		{
       
   127 		if	(aArrayOfParams->At(i)->Name() == aName)
       
   128 			return ETrue;
       
   129 		}
       
   130 
       
   131 	return EFalse;
       
   132 	}
       
   133 
       
   134 
       
   135 //
       
   136 //
       
   137 //
       
   138 
       
   139 
       
   140 //****************************************************************************************
       
   141 CParserPropertyValueHBufC* VTestHelpers::CreateTextPropertyValueL(const TDesC& aValue)
       
   142 	{
       
   143 	CParserPropertyValueHBufC* value = CParserPropertyValueHBufC::NewL(aValue);
       
   144 	return value;
       
   145 	}
       
   146 
       
   147 
       
   148 //****************************************************************************************
       
   149 CParserPropertyValueDateTime* VTestHelpers::CreateDateTimePropertyValueL(const TDateTime& aDateTime)
       
   150 	{
       
   151 	TVersitDateTime* versitDateTime = new(ELeave) TVersitDateTime(aDateTime, TVersitDateTime::EIsMachineLocal);
       
   152 	CleanupStack::PushL(versitDateTime);
       
   153 	CParserPropertyValueDateTime* value = new(ELeave) CParserPropertyValueDateTime(versitDateTime); // takes ownership
       
   154 	CleanupStack::Pop(versitDateTime);
       
   155 	return value;
       
   156 	}
       
   157 
       
   158 
       
   159 //****************************************************************************************
       
   160 CParserPropertyValueDate* VTestHelpers::CreateDatePropertyValueL(const TDateTime& aDateTime)
       
   161 	{
       
   162 	TVersitDateTime* versitDateTime = new(ELeave) TVersitDateTime(aDateTime, TVersitDateTime::EIsMachineLocal);
       
   163 	CleanupStack::PushL(versitDateTime);
       
   164 	CParserPropertyValueDate* value = new(ELeave) CParserPropertyValueDate(versitDateTime); // takes ownership
       
   165 	CleanupStack::Pop(versitDateTime);
       
   166 	return value;
       
   167 	}
       
   168 
       
   169 
       
   170 //
       
   171 //
       
   172 //
       
   173 
       
   174 
       
   175 //****************************************************************************************
       
   176 void VTestHelpers::CreateDescriptorArrayL(const TDesC& aItem, CDesCArray*& aArray, TBool aAddSpace)
       
   177 	{
       
   178 	CleanupStack::PushL(aArray);
       
   179 	if (!aAddSpace)
       
   180 		aArray->AppendL(aItem);
       
   181 	else
       
   182 		{
       
   183 		TBuf<64> string;
       
   184 		string.Append(' ');
       
   185 		string.Append(aItem);
       
   186 		aArray->AppendL(string);
       
   187 		}
       
   188 	CleanupStack::Pop();
       
   189 	}
       
   190 
       
   191 
       
   192 //****************************************************************************************
       
   193 CParserPropertyValueCDesCArray* VTestHelpers::CreateArrayPropertyValueL(CDesCArray* aArray)
       
   194 	{
       
   195 	CleanupStack::PushL(aArray);
       
   196 	CParserPropertyValueCDesCArray* value = new(ELeave) CParserPropertyValueCDesCArray(aArray);
       
   197 	CleanupStack::Pop();
       
   198 	return value;
       
   199 	}
       
   200 
       
   201 
       
   202 
       
   203 
       
   204 
       
   205 
       
   206 
       
   207 
       
   208 //
       
   209 // CComparitorParserProperty (source)
       
   210 //
       
   211 
       
   212 
       
   213 //****************************************************************************************
       
   214 TBool CComparitorParserProperty::IsEqualL(const CComparitorParserProperty* aProp) const
       
   215 	{
       
   216 	if	(aProp->Name() != Name())
       
   217 		return EFalse;
       
   218 	if	(aProp->Value()->Uid() != Value()->Uid())
       
   219 		return EFalse;
       
   220 	if	(aProp->ParameterCount() != ParameterCount())
       
   221 		return EFalse;
       
   222 
       
   223 
       
   224 	switch(aProp->Value()->Uid().iUid)
       
   225 		{
       
   226 	case KVersitPropertyCDesCArrayUid:
       
   227 		{
       
   228 		return PropCDesCArrayIsEqualL(STATIC_CAST(const CParserPropertyValueCDesCArray*, aProp->Value())->Value(), STATIC_CAST(CParserPropertyValueCDesCArray*, Value())->Value());
       
   229 		}
       
   230 	case KVersitPropertyTimeZoneUid:
       
   231 		{
       
   232 		return PropTimeZoneIsEqualL(STATIC_CAST(const CParserPropertyValueTimeZone*, aProp->Value())->Value(), STATIC_CAST(CParserPropertyValueTimeZone*, Value())->Value());
       
   233 		}
       
   234 	case KVersitPropertyDaylightUid:
       
   235 		{
       
   236 		return PropDaylightIsEqualL(STATIC_CAST(const CParserPropertyValueDaylight*, aProp->Value())->Value(), STATIC_CAST(CParserPropertyValueDaylight*, Value())->Value());
       
   237 		}
       
   238 	case KVersitPropertyDateTimeUid:
       
   239 		{
       
   240 		return PropDateTimeIsEqualL(STATIC_CAST(const CParserPropertyValueDateTime*, aProp->Value())->Value(), STATIC_CAST(CParserPropertyValueDateTime*, Value())->Value());
       
   241 		}
       
   242 	case KVersitPropertyDateUid:
       
   243 		{
       
   244 		return PropDateIsEqualL(STATIC_CAST(const CParserPropertyValueDate*, aProp->Value())->Value(), STATIC_CAST(CParserPropertyValueDate*, Value())->Value());
       
   245 		}
       
   246 	case KVersitPropertyMultiDateTimeUid:
       
   247 		{
       
   248 		return PropMultiDateTimeIsEqualL(STATIC_CAST(const CParserPropertyValueMultiDateTime*, aProp->Value())->Value(), STATIC_CAST(CParserPropertyValueMultiDateTime*, Value())->Value());
       
   249 		}
       
   250 	case KVersitPropertyIntUid:
       
   251 		{
       
   252 		return PropIntIsEqualL(STATIC_CAST(const CParserPropertyValueInt*, aProp->Value())->Value(), STATIC_CAST(CParserPropertyValueInt*, Value())->Value());
       
   253 		}
       
   254 	default:
       
   255 		{
       
   256 		return PropHBufCIsEqualL(STATIC_CAST(const CParserPropertyValueHBufC*, aProp->Value())->Value(), STATIC_CAST(CParserPropertyValueHBufC*, Value())->Value());
       
   257 		}
       
   258 		}
       
   259 	}
       
   260 
       
   261 
       
   262 //
       
   263 //
       
   264 //
       
   265 
       
   266 
       
   267 //****************************************************************************************
       
   268 TBool CComparitorParserProperty::PropCDesCArrayIsEqualL(CDesCArray* a1, CDesCArray* a2)
       
   269 	{
       
   270 	TInt count = a1->MdcaCount();
       
   271 	if	(count != a2->MdcaCount())
       
   272 		return EFalse;
       
   273 
       
   274 	for(TInt i=0; i<count; i++)
       
   275 		{
       
   276 		const TDesC& des = a1->MdcaPoint(i);
       
   277 		if	(des.CompareC(a2->MdcaPoint(i)))
       
   278 			return EFalse;
       
   279 		}
       
   280 	return ETrue;
       
   281 	}
       
   282 
       
   283 
       
   284 //****************************************************************************************
       
   285 TBool CComparitorParserProperty::PropTimeZoneIsEqualL(TTimeIntervalSeconds a1, TTimeIntervalSeconds a2)
       
   286 	{
       
   287 	return (a1.Int() == a2.Int());
       
   288 	}
       
   289 
       
   290 
       
   291 //****************************************************************************************
       
   292 TBool CComparitorParserProperty::PropDaylightIsEqualL(CVersitDaylight* a1, CVersitDaylight* a2)
       
   293 	{
       
   294 	if	(a1->iSavings != a2->iSavings)
       
   295 		return EFalse;
       
   296 	if	(a1->iOffset.Int() != a2->iOffset.Int())
       
   297 		return EFalse;
       
   298 
       
   299 	// Just compare the dateTime not the relative time
       
   300 	if	(!DateTimeIsEqual(a1->iStartTime->iDateTime, a2->iStartTime->iDateTime))
       
   301 		return EFalse;
       
   302 	if	(!DateTimeIsEqual(a1->iEndTime->iDateTime, a2->iEndTime->iDateTime))
       
   303 		return EFalse;
       
   304 
       
   305 	if	(a1->iStandardDesignation->CompareC(*a2->iStandardDesignation))
       
   306 		return EFalse;
       
   307 	if	(a1->iDaylightDesignation->CompareC(*a2->iDaylightDesignation))
       
   308 		return EFalse;
       
   309 	return ETrue;
       
   310 	}
       
   311 
       
   312 
       
   313 //****************************************************************************************
       
   314 TBool CComparitorParserProperty::PropDateTimeIsEqualL(TVersitDateTime* a1, TVersitDateTime* a2)
       
   315 	{
       
   316 	return DateTimeIsEqual(a1->iDateTime, a2->iDateTime);
       
   317 	}
       
   318 
       
   319 
       
   320 //****************************************************************************************
       
   321 TBool CComparitorParserProperty::PropDateIsEqualL(TVersitDateTime* a1, TVersitDateTime* a2)
       
   322 	{
       
   323 	return DateEqual(a1->iDateTime, a2->iDateTime);
       
   324 	}
       
   325 
       
   326 
       
   327 //****************************************************************************************
       
   328 TBool CComparitorParserProperty::PropMultiDateTimeIsEqualL(CArrayPtr<TVersitDateTime>* a1, CArrayPtr<TVersitDateTime>* a2)
       
   329 	{
       
   330 	if	((!a1 && a2) || (a1 && !a2))
       
   331 		return EFalse;
       
   332 
       
   333 	TInt count = a1->Count();
       
   334 	if	(count != a2->Count())
       
   335 		return EFalse;
       
   336 
       
   337 	for(TInt i=0; i<count; i++)
       
   338 		{
       
   339 		if	(!PropDateTimeIsEqualL(a1->At(i), a2->At(i)))
       
   340 			return EFalse;
       
   341 		}
       
   342 	return ETrue;
       
   343 	}
       
   344 
       
   345 
       
   346 //****************************************************************************************
       
   347 TBool CComparitorParserProperty::PropIntIsEqualL(TInt a1, TInt a2)
       
   348 	{
       
   349 	return (a1 == a2);
       
   350 	}
       
   351 
       
   352 
       
   353 //****************************************************************************************
       
   354 TBool CComparitorParserProperty::PropHBufCIsEqualL(const TDesC& a1, const TDesC& a2)
       
   355 	{
       
   356 	return !(a1.CompareC(a2));
       
   357 	}
       
   358 
       
   359 
       
   360 //****************************************************************************************
       
   361 TBool CComparitorParserProperty::DateTimeIsEqual(const TDateTime& a1, const TDateTime& a2)
       
   362 	{
       
   363 	TTime time1(a1);
       
   364 	TTime time2(a2);
       
   365 	return (time1 == time2);
       
   366 	}
       
   367 
       
   368 
       
   369 //****************************************************************************************
       
   370 TBool CComparitorParserProperty::DateEqual(const TDateTime& a1, const TDateTime& a2)
       
   371 	{
       
   372 	if	(a1.Year() == a2.Year() && a1.Month() == a2.Month() && a1.Day() == a2.Day())
       
   373 		return ETrue;
       
   374 	return EFalse;
       
   375 	}
       
   376 
       
   377 
       
   378 
       
   379 //
       
   380 // CParserPropertyWithoutCharsetTags (source)
       
   381 //
       
   382 
       
   383 //****************************************************************************************
       
   384 void CParserPropertyWithoutCharsetTags::RemoveAllCharsetTagsFromParametersL()
       
   385 	{
       
   386 	if	(!iArrayOfParams)
       
   387 		return;
       
   388 
       
   389 	TInt count = iArrayOfParams->Count();
       
   390 	for(TInt i=count-1; i>=0; i--)
       
   391 		{
       
   392 		CParserParam* param = iArrayOfParams->At(i);
       
   393 		HBufC8* name = param->Name().AllocLC();
       
   394 		TPtr8 pName = name->Des();
       
   395 		pName.UpperCase();
       
   396 		pName.Trim();
       
   397 
       
   398 		if	(pName == KVersitTokenCHARSET)
       
   399 			{
       
   400 			iArrayOfParams->Delete(i);
       
   401 			delete param;
       
   402 			}
       
   403 		CleanupStack::PopAndDestroy(); // name
       
   404 		}
       
   405 	}
       
   406