deviceupdatesui/deviceupdates/src/nsmldmsyncutil.cpp
changeset 33 7464c1e21dae
child 36 9eefb50e711a
equal deleted inserted replaced
2:a5fecba4b1e4 33:7464c1e21dae
       
     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:  Utility classes.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include <SyncMLTransportProperties.h>
       
    22 #include <eikenv.h>
       
    23 #include <bautils.h>
       
    24 #include <collate.h>
       
    25 #include <StringLoader.h>
       
    26 #include <avkon.rsg>
       
    27 
       
    28 #include <SettingEnforcementInfo.h> // VSettingEnforcementInfo
       
    29 
       
    30 #include "NSmlDMdef.h"
       
    31 #include "NSmlDMSyncUtil.h"
       
    32 #include "NSmlDMSyncDebug.h"
       
    33 #include <featmgr.h>
       
    34 
       
    35 /*****************************************************************************
       
    36  * class TUtil
       
    37  *****************************************************************************/
       
    38 
       
    39 
       
    40 // ----------------------------------------------------------------------------
       
    41 // Panic
       
    42 // ----------------------------------------------------------------------------
       
    43 //
       
    44 void TUtil::Panic(TInt aReason)
       
    45     {
       
    46 	_LIT(KPanicCategory,"NSmlDMSyncUtil");
       
    47 
       
    48 	User::Panic( KPanicCategory, aReason ); 
       
    49     }
       
    50 
       
    51 // ----------------------------------------------------------------------------
       
    52 // StrCopy
       
    53 // String copy with lenght check.
       
    54 // ----------------------------------------------------------------------------
       
    55 //
       
    56 void TUtil::StrCopy( TDes8& aTarget, const TDesC& aSource )
       
    57     {
       
    58     aTarget.Copy( aSource.Left(aTarget.MaxLength() ) );
       
    59     }
       
    60 
       
    61 // ----------------------------------------------------------------------------
       
    62 // StrCopy
       
    63 // String copy with lenght check.
       
    64 // ----------------------------------------------------------------------------
       
    65 //
       
    66 void TUtil::StrCopy( TDes& aTarget, const TDesC8& aSource )
       
    67     {
       
    68 	aTarget.Copy( aSource.Left(aTarget.MaxLength() ) );
       
    69     }
       
    70 
       
    71 // ----------------------------------------------------------------------------
       
    72 // StrCopy
       
    73 // String copy with lenght check.
       
    74 // ----------------------------------------------------------------------------
       
    75 //
       
    76 void TUtil::StrCopy( TDes& aTarget, const TDesC& aSource )
       
    77     {
       
    78 	aTarget.Copy( aSource.Left( aTarget.MaxLength() ) );
       
    79     }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // StrAppend
       
    83 // ----------------------------------------------------------------------------
       
    84 //
       
    85 void TUtil::StrAppend( TDes& aTarget, const TDesC& aSource )
       
    86     {
       
    87     if ( aSource.Length() == 0 )
       
    88     	{
       
    89     	return;
       
    90     	}
       
    91     TInt free = aTarget.MaxLength() - aTarget.Length();
       
    92 	if ( aSource.Length() <= free )
       
    93 		{
       
    94 		aTarget.Append( aSource );
       
    95 		}
       
    96     }
       
    97 
       
    98 // ----------------------------------------------------------------------------
       
    99 // StrInsert
       
   100 // ----------------------------------------------------------------------------
       
   101 //
       
   102 void TUtil::StrInsert( TDes& aTarget, const TDesC& aSource )
       
   103     {
       
   104 	TInt free = aTarget.MaxLength() - aTarget.Length();
       
   105 	if (aSource.Length() <= free)
       
   106 		{
       
   107 		aTarget.Insert(0, aSource);
       
   108 		}
       
   109     }
       
   110 
       
   111 // ----------------------------------------------------------------------------
       
   112 // StrToInt
       
   113 // ----------------------------------------------------------------------------
       
   114 //
       
   115 TInt TUtil::StrToInt( const TDesC& aText, TInt& aNum )
       
   116 	{
       
   117     TLex lex( aText );
       
   118     TInt err = lex.Val( aNum ); 
       
   119 	return err;
       
   120 	}
       
   121 
       
   122 //
       
   123 //-----------------------------------------------------------------------------
       
   124 // IsEmpty
       
   125 // Function returns ETrue if string only contains white space
       
   126 // or has no characters.
       
   127 //-----------------------------------------------------------------------------
       
   128 //
       
   129 TBool TUtil::IsEmpty( const TDesC& aText )
       
   130 	{
       
   131 	TInt len = aText.Length();
       
   132 	for ( TInt index=0; index < len; index++ )
       
   133 		{
       
   134 		TChar character = aText[index];
       
   135 		if ( !character.IsSpace() )
       
   136 			{
       
   137 			return EFalse;
       
   138 			}
       
   139 		}
       
   140 	return ETrue;
       
   141 	}
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // SyncTimeLC
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 HBufC* TUtil::SyncTimeLC( TTime aLastSync )
       
   148 	{
       
   149 	FLOG( "[OMADM] TUtil::SyncTimeLC" );
       
   150 	
       
   151 	TTime homeTime = ConvertUniversalToHomeTime( aLastSync );
       
   152 	
       
   153 	HBufC* hBuf = HBufC::NewLC( KBufSize255 );
       
   154 	TPtr ptr = hBuf->Des();
       
   155 
       
   156 	if ( IsToday( homeTime ) )
       
   157 		{
       
   158 		FLOG( "[OMADM] TUtil::SyncTimeLC time" );
       
   159 		
       
   160 		TBuf<KBufSize> timeFormat;
       
   161 		HBufC* buf = StringLoader::LoadLC( R_QTN_TIME_USUAL_WITH_ZERO );
       
   162 		TUtil::StrCopy( timeFormat, *buf );
       
   163 		CleanupStack::PopAndDestroy( buf );
       
   164 		homeTime.FormatL( ptr, timeFormat );
       
   165 		
       
   166 		FLOG( "[OMADM] TUtil::SyncTimeLC time done" );
       
   167 		}
       
   168 	else
       
   169 		{
       
   170 		FLOG( "[OMADM] TUtil::SyncTimeLC date" );
       
   171 		
       
   172 		TBuf<KBufSize> dateFormat;
       
   173         HBufC* buf = StringLoader::LoadLC( R_QTN_DATE_USUAL_WITH_ZERO );
       
   174         TUtil::StrCopy( dateFormat, *buf );
       
   175         CleanupStack::PopAndDestroy( buf );
       
   176 		homeTime.FormatL(ptr, dateFormat);
       
   177 		
       
   178 		FLOG( "[OMADM] TUtil::SyncTimeLC date done" );
       
   179 		}
       
   180     FLOG( "[OMADM] TUtil::SyncTimeLC Done" );
       
   181 	return hBuf;
       
   182 	}
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // ConvertUniversalToHomeTime
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 TTime TUtil::ConvertUniversalToHomeTime( const TTime& aUniversalTime )
       
   189     {
       
   190     TTime time( aUniversalTime );   // time stores UTC time.
       
   191 
       
   192     TLocale locale;
       
   193     TTimeIntervalSeconds universalTimeOffset( locale.UniversalTimeOffset() );
       
   194     
       
   195     // Add locale's universal time offset to universal time.
       
   196     time += universalTimeOffset;    // time stores Local Time.
       
   197 
       
   198     // If home daylight saving in effect, add one hour offset.
       
   199     if ( locale.QueryHomeHasDaylightSavingOn() )
       
   200         {
       
   201         TTimeIntervalHours daylightSaving(1);
       
   202         time += daylightSaving;
       
   203         }
       
   204 
       
   205     return time;
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // IsToDay
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 TBool TUtil::IsToday(TTime aTime)
       
   213 	{
       
   214 	FLOG( "[OMADM] TUtil::IsToday" );
       
   215 	
       
   216 	TTime now;
       
   217 	now.HomeTime();
       
   218 	TInt day1 = now.DayNoInYear();
       
   219 	TInt day2 = aTime.DayNoInYear();
       
   220 	TTimeIntervalDays daysBetween = now.DaysFrom( aTime );
       
   221 	
       
   222 	if ( day1 != day2 )
       
   223 		{
       
   224 		FLOG( "[OMADM] TUtil::IsToday False 1" );
       
   225 		return EFalse;
       
   226 		}
       
   227 		
       
   228 	if ( daysBetween.Int() > 0 )
       
   229 		{
       
   230 		FLOG( "[OMADM] TUtil::IsToday False 2" );
       
   231 		return EFalse;
       
   232 		}
       
   233 
       
   234     FLOG( "[OMADM] TUtil::IsToday True" );
       
   235 	return ETrue; 
       
   236 	}
       
   237 
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // TUtil::GetDateTextL
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 void TUtil::GetDateTextL(TDes& aText, TTime aDateTime)
       
   244 	{
       
   245 	TTime homeTime = ConvertUniversalToHomeTime( aDateTime );
       
   246 	HBufC* hBuf = StringLoader::LoadLC( R_QTN_DATE_USUAL_WITH_ZERO );
       
   247     homeTime.FormatL( aText, *hBuf );
       
   248     CleanupStack::PopAndDestroy( hBuf );
       
   249 	}
       
   250 
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // TUtil::GetTimeTextL
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void TUtil::GetTimeTextL( TDes& aText, TTime aDateTime )
       
   257 	{
       
   258 	TTime homeTime = ConvertUniversalToHomeTime( aDateTime );
       
   259 	HBufC* hBuf = StringLoader::LoadLC( R_QTN_TIME_USUAL_WITH_ZERO );
       
   260     homeTime.FormatL( aText, *hBuf );
       
   261     CleanupStack::PopAndDestroy( hBuf );
       
   262 	}
       
   263 
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // TUtil::GetDateTimeTextL
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 void TUtil::GetDateTimeTextL( TDes& aText, TTime aDateTime )
       
   270 	{
       
   271 	TDateTime dt = aDateTime.DateTime();
       
   272 	aText.Format(_L("%02d.%02d.%04d %02d:%02d:%02d"), dt.Day()+1,
       
   273 	                                                  dt.Month()+1,
       
   274 	                                                  dt.Year(),
       
   275 	                                                  dt.Hour(),
       
   276 	                                                  dt.Minute(),
       
   277 	                                                  dt.Second() );
       
   278 	}
       
   279 
       
   280 
       
   281 // ---------------------------------------------------------
       
   282 // TUtil::SettingEnforcementStateL
       
   283 // 
       
   284 // Checks if setting enforcement is activated.
       
   285 // ---------------------------------------------------------
       
   286 TBool TUtil::SettingEnforcementStateL()
       
   287     {
       
   288 FeatureManager::InitializeLibL();
       
   289 if(!FeatureManager::FeatureSupported(KFeatureIdSapPolicyManagement))
       
   290 {
       
   291 		FeatureManager::UnInitializeLib();
       
   292 		    return EFalse;
       
   293 }
       
   294 else
       
   295 {
       
   296 	FeatureManager::UnInitializeLib();
       
   297 	CSettingEnforcementInfo* info = CSettingEnforcementInfo::NewL();
       
   298 	CleanupStack::PushL(info);
       
   299 
       
   300 	TBool active = EFalse;
       
   301 	User::LeaveIfError(info->EnforcementActive(ESyncMLEnforcement, active));
       
   302 	CleanupStack::PopAndDestroy(info);
       
   303 
       
   304 	return active;
       
   305 }
       
   306 
       
   307     }
       
   308 
       
   309 
       
   310 // ---------------------------------------------------------
       
   311 // TUtil::SettingEnforcementState
       
   312 // 
       
   313 // Checks if setting enforcement is activated.
       
   314 // ---------------------------------------------------------
       
   315 TBool TUtil::SettingEnforcementState()
       
   316     {
       
   317     TBool ret = EFalse;
       
   318 
       
   319     TRAP_IGNORE(ret = SettingEnforcementStateL());
       
   320 
       
   321     return ret;
       
   322     }
       
   323 
       
   324 
       
   325 /*****************************************************************************
       
   326  * class TURIParser
       
   327  *****************************************************************************/
       
   328 
       
   329 // ----------------------------------------------------------------------------
       
   330 // Constructor
       
   331 // ----------------------------------------------------------------------------
       
   332 //
       
   333 TURIParser::TURIParser( const TDesC& aStr ) : iOriginalStr( aStr )
       
   334     {
       
   335     SkipHttp();
       
   336     }
       
   337 
       
   338 // ----------------------------------------------------------------------------
       
   339 // GetUriWithoutPort
       
   340 // URI format: "http://serveraddress/documentname/"
       
   341 // ----------------------------------------------------------------------------
       
   342 //
       
   343 void TURIParser::GetUriWithoutPort( TDes& aText )
       
   344 	{
       
   345 	aText = KNullDesC;
       
   346 	TPtrC http = ParseHttp();
       
   347 	TPtrC address = ParseAddress();
       
   348 	TPtrC document = ParseDocument();
       
   349 	TUtil::StrAppend( aText, http );
       
   350 	TUtil::StrAppend( aText, address );
       
   351 	
       
   352 	if (document.Length() > 0)
       
   353 		{
       
   354 		TUtil::StrAppend( aText, KSlash );
       
   355 		TUtil::StrAppend( aText, document );
       
   356 		}
       
   357 	else if ( iSlash )
       
   358 		{
       
   359 		TUtil::StrAppend( aText, KSlash );
       
   360 		}
       
   361 	}
       
   362 	
       
   363 // ----------------------------------------------------------------------------
       
   364 // GetURI
       
   365 // URI format: "http://serveraddress:port/documentname/"
       
   366 // ----------------------------------------------------------------------------
       
   367 //
       
   368 void TURIParser::GetUri( TDes& aText, const TInt aPort )
       
   369 	{
       
   370 	aText = KNullDesC;
       
   371 	TPtrC http = ParseHttp();
       
   372 	TPtrC address = ParseAddress();
       
   373 	TPtrC document = ParseDocument();
       
   374 	TUtil::StrAppend( aText, http );
       
   375 	TUtil::StrAppend( aText, address );
       
   376 	
       
   377 	TBuf<KBufSize32> buf;
       
   378 	buf.Num( aPort );
       
   379 	TUtil::StrAppend( aText, KColon );
       
   380 	TUtil::StrAppend( aText, buf );
       
   381 	
       
   382 	if ( document.Length() > 0 )
       
   383 		{
       
   384 		TUtil::StrAppend( aText, KSlash );
       
   385 		TUtil::StrAppend( aText, document );
       
   386 		}
       
   387 	else if ( iSlash )
       
   388 		{
       
   389 		TUtil::StrAppend( aText, KSlash );
       
   390 		}
       
   391 	}
       
   392 
       
   393 // ----------------------------------------------------------------------------
       
   394 // Port
       
   395 // ----------------------------------------------------------------------------
       
   396 //
       
   397 TInt TURIParser::Port()
       
   398 	{
       
   399 	TInt port = KErrNotFound;
       
   400 	
       
   401 	TInt err = ParsePort( port );
       
   402 	if ( err != KErrNone )
       
   403 		{
       
   404 		return KErrNotFound;
       
   405 		}
       
   406 	return port;
       
   407 	}
       
   408 
       
   409 // ----------------------------------------------------------------------------
       
   410 // DefaultPort
       
   411 // 
       
   412 // ----------------------------------------------------------------------------
       
   413 //
       
   414 TInt TURIParser::DefaultPort()
       
   415 	{
       
   416 	TInt pos = iOriginalStr.FindF( KHttpsHeader );
       
   417 	if ( pos != KErrNotFound )
       
   418 		{
       
   419 		return KDefaultHttpsPort;
       
   420 		}
       
   421 	return KDefaultHttpPort;
       
   422 	}
       
   423 
       
   424 // ----------------------------------------------------------------------------
       
   425 // SkipHttp
       
   426 // Set pointer to the end of "http://".
       
   427 // ----------------------------------------------------------------------------
       
   428 //
       
   429 void TURIParser::SkipHttp()
       
   430 	{
       
   431     TInt start = KErrNotFound;
       
   432     TInt end   = KErrNotFound;
       
   433     TStr str(iOriginalStr);
       
   434     
       
   435     GetHttpPos( start, end );
       
   436     
       
   437     if ( start != KErrNotFound )
       
   438     	{
       
   439     	iPtr.Set( str.Right( end + 1 ) );
       
   440     	}
       
   441     else
       
   442     	{
       
   443     	iPtr.Set( str.Right(0) );
       
   444     	}
       
   445 	}
       
   446 
       
   447 // ----------------------------------------------------------------------------
       
   448 // ParseHttp
       
   449 // 
       
   450 // ----------------------------------------------------------------------------
       
   451 //
       
   452 TPtrC TURIParser::ParseHttp()
       
   453 	{
       
   454     TInt start = KErrNotFound;
       
   455     TInt end   = KErrNotFound;
       
   456     
       
   457     GetHttpPos(start, end);
       
   458     
       
   459     if (start == KErrNotFound)
       
   460     	{
       
   461     	return TPtrC();
       
   462     	}
       
   463     
       
   464    	TStr str(iOriginalStr);
       
   465     return str.SubStr(start, end);
       
   466 	}
       
   467 
       
   468 // ----------------------------------------------------------------------------
       
   469 // GetHttpPos 
       
   470 // ----------------------------------------------------------------------------
       
   471 //
       
   472 void TURIParser::GetHttpPos( TInt& aStartPos, TInt& aEndPos )
       
   473 	{
       
   474 	aEndPos = KErrNotFound;
       
   475 	
       
   476 	aStartPos = iOriginalStr.FindF( KHttpHeader );
       
   477 	if ( aStartPos != KErrNotFound )
       
   478 		{
       
   479 		aEndPos = aStartPos + KHttpHeader().Length() - 1;
       
   480 		return;
       
   481 		}
       
   482 	
       
   483 	aStartPos = iOriginalStr.FindF( KHttpsHeader );
       
   484 	if ( aStartPos != KErrNotFound )
       
   485 		{
       
   486 		aEndPos = aStartPos + KHttpsHeader().Length() - 1;
       
   487 		return;
       
   488 		}
       
   489 	}
       
   490 
       
   491 // ----------------------------------------------------------------------------
       
   492 // ParseAddress
       
   493 // ----------------------------------------------------------------------------
       
   494 TPtrC TURIParser::ParseAddress()
       
   495 	{
       
   496 	const TChar KSlash('/');
       
   497 	const TChar KColon(':');
       
   498 	TStr str( iPtr );
       
   499     TInt firstSlash = str.Find( 0, KSlash );
       
   500 	TInt firstcolon = str.Find( 0, KColon );
       
   501 	TBool portFound = EFalse;
       
   502 	iSlash = EFalse;
       
   503 	if ( firstSlash != KErrNotFound ) 
       
   504 	    {
       
   505 		 iSlash = ETrue;
       
   506 	    }
       
   507 	if (firstcolon != KErrNotFound)
       
   508 		{
       
   509 		if ( firstSlash == KErrNotFound )
       
   510 			{
       
   511 			portFound = ETrue;
       
   512 			}
       
   513 		else if ( firstcolon < firstSlash )
       
   514 			{
       
   515 			portFound = ETrue;
       
   516 			}
       
   517 		}
       
   518     
       
   519 	if ( portFound )
       
   520 		{
       
   521 		// address is text before first colon
       
   522 		return str.Left( firstcolon - 1 );
       
   523 		}
       
   524 	else if (firstSlash != KErrNotFound)
       
   525 		{
       
   526 		// address is text before first slash
       
   527 		return str.Left( firstSlash - 1 );
       
   528 		}
       
   529 	else
       
   530 		{
       
   531 		// address is all text
       
   532 		return str.Right( 0 );
       
   533 		}
       
   534 	}
       
   535 
       
   536 // ----------------------------------------------------------------------------
       
   537 // ParseDocument
       
   538 // ----------------------------------------------------------------------------
       
   539 TPtrC TURIParser::ParseDocument()
       
   540 	{
       
   541 	const TChar KSlash('/');
       
   542 	
       
   543 	TStr str(iPtr);
       
   544     TInt firstSlash = str.Find( 0, KSlash );
       
   545     
       
   546 	if ( firstSlash != KErrNotFound )
       
   547 		{
       
   548 		// document is all text after first slash
       
   549 		return str.Right( firstSlash + 1 );
       
   550 		}
       
   551 	return TPtrC();
       
   552 	}
       
   553 	
       
   554 // ----------------------------------------------------------------------------
       
   555 // ParsePort
       
   556 // ----------------------------------------------------------------------------
       
   557 // 
       
   558 TInt TURIParser::ParsePort( TInt& aNumber )
       
   559 	{
       
   560 	TPtrC port = ParsePort();
       
   561 	if ( port.Length() == 0 )
       
   562 		{
       
   563 		return KErrNotFound;
       
   564 		}
       
   565 	return TUtil::StrToInt( port, aNumber );
       
   566 	}
       
   567 
       
   568 // ----------------------------------------------------------------------------
       
   569 // ParsePort
       
   570 // ----------------------------------------------------------------------------
       
   571 // 
       
   572 TPtrC TURIParser::ParsePort()
       
   573 	{
       
   574 	const TChar KSlash('/');
       
   575 	const TChar KColon(':');
       
   576 	
       
   577 	TStr str(iPtr);
       
   578     TInt firstSlash = str.Find( 0, KSlash );
       
   579 	TInt firstColon = str.Find( 0, KColon );
       
   580     
       
   581 	if ( firstSlash != KErrNotFound )
       
   582 		{
       
   583 		if ( firstColon != KErrNotFound && firstColon < firstSlash )
       
   584 			{
       
   585 		    // port number is text between colon and slash 
       
   586 		    return str.SubStrEx( firstColon, firstSlash );
       
   587 			}
       
   588 		}
       
   589 	else 
       
   590 		{
       
   591 		if ( firstColon != KErrNotFound )
       
   592 			{
       
   593 		    // port number is all text after colon 
       
   594 		    return str.Right(firstColon+1);
       
   595 			}
       
   596 		}
       
   597 	return TPtrC();
       
   598 	}
       
   599 
       
   600 /*****************************************************************************
       
   601  * class TStr
       
   602  *****************************************************************************/
       
   603 
       
   604 // ----------------------------------------------------------------------------
       
   605 // TStr::TStr
       
   606 // C++ default constructor can NOT contain any code, that
       
   607 // might leave.
       
   608 // ----------------------------------------------------------------------------
       
   609 //
       
   610 TStr::TStr( const TDesC& aStr ) : iStr( aStr )
       
   611     {
       
   612     }
       
   613 
       
   614 // ----------------------------------------------------------------------------
       
   615 // TStr::Mid
       
   616 // ----------------------------------------------------------------------------
       
   617 //
       
   618 TPtrC TStr::Mid( TInt aPos )
       
   619     {
       
   620 	if ( ( aPos < 0 ) || ( aPos >= iStr.Length() ) )
       
   621 	    {
       
   622 		return TPtrC();  // return empty descriptor
       
   623 	    }
       
   624 	return iStr.Mid( aPos );
       
   625     }
       
   626 
       
   627 // ----------------------------------------------------------------------------
       
   628 // TStr::Mid
       
   629 // ----------------------------------------------------------------------------
       
   630 //
       
   631 TPtrC TStr::Mid( TInt aPos, TInt aLen )
       
   632     {
       
   633 	TInt len = iStr.Length();
       
   634 	if ( ( aPos < 0 ) || ( aPos >= len ) )
       
   635 	    {
       
   636 		return TPtrC();
       
   637 	    }
       
   638 	if ( ( aLen <= 0 ) || ( aPos + aLen > len ) )
       
   639 	    {
       
   640 		return TPtrC();
       
   641 	    }
       
   642 
       
   643 	return iStr.Mid( aPos, aLen );
       
   644     }
       
   645 
       
   646 // ----------------------------------------------------------------------------
       
   647 // TStr::Right
       
   648 // ----------------------------------------------------------------------------
       
   649 //
       
   650 TPtrC TStr::Right( TInt aPos )
       
   651     {
       
   652 	return Mid( aPos );
       
   653     }
       
   654 
       
   655 // ----------------------------------------------------------------------------
       
   656 // TStr::Left
       
   657 // ----------------------------------------------------------------------------
       
   658 //
       
   659 TPtrC TStr::Left( TInt aPos )
       
   660     {
       
   661 	TInt len = iStr.Length();
       
   662 
       
   663 	if ( ( aPos < 0 ) || ( len == 0 ) )
       
   664 	    {
       
   665 		return TPtrC();
       
   666 	    }
       
   667 
       
   668 	TInt pos = len;
       
   669 	if ( ( aPos + 1 ) < len )
       
   670 	    {
       
   671 		pos = aPos + 1;
       
   672 	    }
       
   673 
       
   674 	return iStr.Left( pos );
       
   675     }
       
   676 
       
   677 // ----------------------------------------------------------------------------
       
   678 // TStr::SubStr
       
   679 // ----------------------------------------------------------------------------
       
   680 //
       
   681 TPtrC TStr::SubStr( TInt aStartPos, TInt aEndPos )
       
   682     {
       
   683 	return Mid( aStartPos, aEndPos - aStartPos + 1 );
       
   684     }
       
   685 
       
   686 // ----------------------------------------------------------------------------
       
   687 // TStr::SubStrEx
       
   688 // ----------------------------------------------------------------------------
       
   689 //
       
   690 TPtrC TStr::SubStrEx( TInt aStartPos, TInt aEndPos )
       
   691     {
       
   692 	return Mid( aStartPos + 1, aEndPos - aStartPos - 1 );
       
   693     }
       
   694 
       
   695 // ----------------------------------------------------------------------------
       
   696 // TStr::LastPos
       
   697 // ----------------------------------------------------------------------------
       
   698 //
       
   699 TInt TStr::LastPos()
       
   700     {
       
   701 	return iStr.Length() - 1;
       
   702     }
       
   703 
       
   704 // ----------------------------------------------------------------------------
       
   705 // TStr::Len
       
   706 // ----------------------------------------------------------------------------
       
   707 //
       
   708 TInt TStr::Len()
       
   709     {
       
   710     return iStr.Length();
       
   711     }
       
   712 
       
   713 // ----------------------------------------------------------------------------
       
   714 // TStr::Compare
       
   715 // ----------------------------------------------------------------------------
       
   716 //
       
   717 TBool TStr::Compare( TInt aPos, TChar aChar )
       
   718     {
       
   719 	TInt len = iStr.Length();
       
   720 	if ( ( aPos < 0) || ( aPos >= len ) )
       
   721 	    {
       
   722 		return EFalse;
       
   723 	    }
       
   724 	TChar ch = iStr[aPos];
       
   725 	if ( ch == aChar )
       
   726 	    {
       
   727 		return ETrue;
       
   728 	    }
       
   729 	return EFalse;
       
   730     }
       
   731 
       
   732 // ----------------------------------------------------------------------------
       
   733 // TStr::Find
       
   734 // ----------------------------------------------------------------------------
       
   735 //
       
   736 TInt TStr::Find(TInt aPos, TChar aChar)
       
   737     {
       
   738 	TInt len = iStr.Length();
       
   739 	if ( (aPos < 0) || (aPos >= len) )
       
   740 	    {
       
   741 		return KErrNotFound;
       
   742 	    }
       
   743 	    
       
   744 	TPtrC ptr(iStr.Mid(aPos)); // move to possition aPos
       
   745 	return ptr.Locate(aChar);
       
   746     }
       
   747 
       
   748 //*****************************************************************************
       
   749 //* class CNSmlDMActiveCaller
       
   750 //*****************************************************************************
       
   751 
       
   752 // ----------------------------------------------------------------------------
       
   753 // CNSmlDMActiveCaller::NewL
       
   754 // ----------------------------------------------------------------------------
       
   755 //
       
   756 CNSmlDMActiveCaller* CNSmlDMActiveCaller::NewL( 
       
   757                                        MNSmlDMActiveCallerObserver* aObserver )
       
   758     {
       
   759     CNSmlDMActiveCaller* self = new(ELeave) CNSmlDMActiveCaller( aObserver );
       
   760 	CleanupStack::PushL(self);
       
   761 	self->ConstructL();
       
   762 	CleanupStack::Pop(self);
       
   763 
       
   764 	return self;
       
   765     }
       
   766 
       
   767 // ----------------------------------------------------------------------------
       
   768 // Destructor
       
   769 // ----------------------------------------------------------------------------
       
   770 //
       
   771 CNSmlDMActiveCaller::~CNSmlDMActiveCaller()
       
   772     {
       
   773 	Cancel();
       
   774 	iTimer.Close();
       
   775     }
       
   776 
       
   777 // ----------------------------------------------------------------------------
       
   778 // CNSmlDMActiveCaller::CNSmlDMActiveCaller
       
   779 // ----------------------------------------------------------------------------
       
   780 //
       
   781 CNSmlDMActiveCaller::CNSmlDMActiveCaller( 
       
   782                                         MNSmlDMActiveCallerObserver* aObserver )
       
   783                                       : CActive(CActive::EPriorityStandard )
       
   784     {
       
   785 	iObserver = aObserver;
       
   786     }
       
   787 
       
   788 // ----------------------------------------------------------------------------
       
   789 // CNSmlDMActiveCaller::ConstructL
       
   790 // ----------------------------------------------------------------------------
       
   791 //
       
   792 void CNSmlDMActiveCaller::ConstructL()
       
   793     {
       
   794 	User::LeaveIfError(iTimer.CreateLocal());
       
   795 	CActiveScheduler::Add(this);
       
   796     }
       
   797 
       
   798 // ----------------------------------------------------------------------------
       
   799 // CNSmlDMActiveCaller::DoCancel
       
   800 // ----------------------------------------------------------------------------
       
   801 //
       
   802 void CNSmlDMActiveCaller::DoCancel()
       
   803     {
       
   804 	iTimer.Cancel();
       
   805     }
       
   806 
       
   807 // ----------------------------------------------------------------------------
       
   808 // CNSmlDMActiveCaller::RunL
       
   809 // ----------------------------------------------------------------------------
       
   810 //
       
   811 void CNSmlDMActiveCaller::RunL()
       
   812     {
       
   813     iObserver->HandleActiveCallL( );
       
   814     }
       
   815 
       
   816 // ----------------------------------------------------------------------------
       
   817 // CNSmlDMActiveCaller::RunError
       
   818 // ----------------------------------------------------------------------------
       
   819 //
       
   820 TInt CNSmlDMActiveCaller::RunError( TInt /*aError*/ )
       
   821     {
       
   822     return KErrNone;
       
   823     }
       
   824     
       
   825 // ----------------------------------------------------------------------------
       
   826 // CNSmlDMActiveCaller::CompleteSelf
       
   827 //
       
   828 // This function calls this class RunL.
       
   829 // ----------------------------------------------------------------------------
       
   830 //
       
   831 void CNSmlDMActiveCaller::CompleteSelf()
       
   832     {
       
   833 	SetActive();
       
   834 	TRequestStatus* status = &iStatus;
       
   835 	User::RequestComplete( status, KErrNone );
       
   836     }
       
   837 
       
   838 // ----------------------------------------------------------------------------
       
   839 // CNSmlDMActiveCaller::Start
       
   840 // ----------------------------------------------------------------------------
       
   841 //
       
   842 void CNSmlDMActiveCaller::Start( TInt aCallId, TInt aMilliseconds )
       
   843     {
       
   844 	if (IsActive())
       
   845 		{
       
   846 		return;
       
   847 		}
       
   848 	
       
   849 	iCallId = aCallId;
       
   850 
       
   851 	if (aMilliseconds <= 0)
       
   852 		{
       
   853 		CompleteSelf();  // no delay - complete right away
       
   854 		}
       
   855 	else
       
   856 		{
       
   857 	    iTimer.After( iStatus, aMilliseconds*1000 );
       
   858 	    SetActive();
       
   859 		}
       
   860     }
       
   861 
       
   862 // End of File