clock2/clockengines/clocktimezoneresolver/src/clockmcctzmapper.cpp
branchRCL_3
changeset 30 bd7edf625bdd
equal deleted inserted replaced
29:12af337248b1 30:bd7edf625bdd
       
     1 /*
       
     2 * Copyright (c) 2008 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:   The source file for the CClockMCCTzIdMapper class.
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <tz.h>
       
    20 #include <tzdefines.h>
       
    21 #include <vtzrules.h>
       
    22 #include <bautils.h>
       
    23 
       
    24 // User includes
       
    25 #include "clockmcctzmapper.h"
       
    26 #include "clock_debug.h"
       
    27 
       
    28 // Constants
       
    29 
       
    30 // Literals
       
    31 _LIT( KMCCResourceFileDir, "\\resource\\mcc\\" );
       
    32 _LIT( KMCCResourceFileName, "mcc.rsc" );
       
    33 
       
    34 // ---------------------------------------------------------
       
    35 // CClockMCCTzIdMapper::NewL
       
    36 // rest of the details are commented in the header
       
    37 // ---------------------------------------------------------
       
    38 //
       
    39 CClockMCCTzIdMapper* CClockMCCTzIdMapper::NewL()
       
    40 	{
       
    41 	__PRINTS( "CClockMCCTzIdMapper::NewL - Entry" );
       
    42     
       
    43     CClockMCCTzIdMapper* self = CClockMCCTzIdMapper::NewLC();
       
    44 	CleanupStack::Pop( self );
       
    45 	
       
    46 	__PRINTS( "CClockMCCTzIdMapper::NewL - Exit" );
       
    47 	
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 // ---------------------------------------------------------
       
    52 // CClockMCCTzIdMapper::NewLC
       
    53 // rest of the details are commented in the header
       
    54 // ---------------------------------------------------------
       
    55 //
       
    56 CClockMCCTzIdMapper* CClockMCCTzIdMapper::NewLC()
       
    57 	{
       
    58 	__PRINTS( "CClockMCCTzIdMapper::NewLC - Entry" );
       
    59 	
       
    60 	CClockMCCTzIdMapper* self = new ( ELeave ) CClockMCCTzIdMapper();
       
    61 	CleanupStack::PushL( self );
       
    62 
       
    63 	self->ConstructL();
       
    64 	
       
    65 	__PRINTS( "CClockMCCTzIdMapper::NewLC - Exit" );
       
    66 	
       
    67 	return self;
       
    68 	}
       
    69 
       
    70 // ---------------------------------------------------------
       
    71 // CClockMCCTzIdMapper::ConstructL
       
    72 // rest of the details are commented in the header
       
    73 // ---------------------------------------------------------
       
    74 //
       
    75 void CClockMCCTzIdMapper::ConstructL()
       
    76 	{
       
    77 	__PRINTS( "CClockMCCTzIdMapper::ConstructL - Entry" );
       
    78 	
       
    79 	// Connect to the file server
       
    80 	User::LeaveIfError( iFs.Connect() );	
       
    81     
       
    82     // Get the full filename with drive letter
       
    83     TFileName rscFileName; 
       
    84     TFindFile fileFinder( iFs );
       
    85 
       
    86     // TFindFile applies search order that is from drive Y to A, then Z
       
    87     TInt err = fileFinder.FindByDir( KMCCResourceFileName, KMCCResourceFileDir );
       
    88 
       
    89     if ( err == KErrNone )
       
    90         {
       
    91         // Found file.
       
    92         rscFileName = fileFinder.File();
       
    93         }
       
    94     else
       
    95         {
       
    96         __PRINTS( "Unable to open mcc.rsc. Leaving now!!" );
       
    97         
       
    98         // Leave with reason, file not found.
       
    99         User::Leave( KErrPathNotFound );
       
   100         }
       
   101     
       
   102 	// Open the resource file.  The resource file will not be localized as it
       
   103 	// only contains integers, so there is no need to find a language specific
       
   104 	// resource file name.
       
   105 	iMCCResourceFile.OpenL( iFs, rscFileName );
       
   106 	iMCCResourceFile.ConfirmSignatureL();
       
   107 
       
   108 	// Assign buffer for holding resource
       
   109 	TInt resourceId = iMCCResourceFile.Offset() + EMCCFirstResource;
       
   110 	iResourceBuffer = iMCCResourceFile.AllocReadL( resourceId );
       
   111 	
       
   112 	__PRINTS( "CClockMCCTzIdMapper::ConstructL - Exit" );
       
   113 	}
       
   114 
       
   115 // ---------------------------------------------------------
       
   116 // CClockMCCTzIdMapper::~CClockMCCTzIdMapper
       
   117 // rest of the details are commented in the header
       
   118 // ---------------------------------------------------------
       
   119 //
       
   120 CClockMCCTzIdMapper::~CClockMCCTzIdMapper()
       
   121 	{
       
   122 	__PRINTS( "CClockMCCTzIdMapper::~CClockMCCTzIdMapper - Entry" );
       
   123 	
       
   124 	// Close the resource file.
       
   125 	iMCCResourceFile.Close();
       
   126 	// Free up the resource buffer.
       
   127 	if( iResourceBuffer )
       
   128 	    {
       
   129 	    delete iResourceBuffer;
       
   130 	    iResourceBuffer = NULL;
       
   131 	    }
       
   132 
       
   133 	// Close the file server session.
       
   134 	iFs.Close();
       
   135 	
       
   136 	__PRINTS( "CClockMCCTzIdMapper::~CClockMCCTzIdMapper - Exit" );
       
   137 	}
       
   138 
       
   139 // ---------------------------------------------------------
       
   140 // CClockMCCTzIdMapper::UTCOffSetWithoutDSTChangesL
       
   141 // rest of the details are commented in the header
       
   142 // ---------------------------------------------------------
       
   143 //
       
   144 TInt CClockMCCTzIdMapper::UTCOffSetWithoutDSTChangesL( RTz& aTz, const CTzId& aTzId ) const
       
   145 	{
       
   146 	TInt timeOffset( KErrNotFound );
       
   147 
       
   148 	TTime universalTime;
       
   149 	universalTime.UniversalTime();
       
   150 
       
   151 	// Get the current rules for the timezone rules
       
   152 	CTzRules* currentRules = aTz.GetTimeZoneRulesL( aTzId, universalTime, universalTime, ETzUtcTimeReference );
       
   153 	if( currentRules )
       
   154 	    {
       
   155 	    timeOffset = currentRules->InitialStdTimeOffset();
       
   156 	    delete currentRules;
       
   157 	    }
       
   158 	
       
   159 	return timeOffset;
       
   160 	}
       
   161 
       
   162 // ---------------------------------------------------------
       
   163 // CClockMCCTzIdMapper::TzIdFromMccL
       
   164 // rest of the details are commented in the header
       
   165 // ---------------------------------------------------------
       
   166 //
       
   167 void CClockMCCTzIdMapper::TzIdFromMccL( const RMobilePhone::TMobilePhoneNetworkCountryCode& aMCC,
       
   168 								        RArray< CTzId >& aTzIdArray,
       
   169 								        const TTimeIntervalMinutes& aStdTimeOffset )
       
   170 	{
       
   171 	__PRINTS( "CClockMCCTzIdMapper::TzIdFromMccL - Entry" );
       
   172 	
       
   173 	// Convert the TMobilePhoneNetworkCountryCode to a TInt.
       
   174 	TInt currentMcc;
       
   175 	TLex lexUtil( aMCC );
       
   176 	User::LeaveIfError( lexUtil.Val( currentMcc ) );
       
   177 	
       
   178 	__PRINT( "MCC: %d", currentMcc );
       
   179 
       
   180 	// Get all the DST zones that match mcc
       
   181 	DoGetTzIdFromMCCL( currentMcc, aTzIdArray );
       
   182 
       
   183 	// Limit the returned entries to those with a specific StdTimeOffset
       
   184 	TInt offset = aStdTimeOffset.Int();
       
   185 	if( offset != -1 )
       
   186 		{
       
   187 		// Connect to Tz
       
   188 		RTz tzHandle;
       
   189 		CleanupClosePushL( tzHandle );
       
   190 		User::LeaveIfError( tzHandle.Connect() );
       
   191 
       
   192 		TInt currentZoneOffset;
       
   193 		TInt count = aTzIdArray.Count();
       
   194 		// Loop through the array in reverse order, removing all entries
       
   195 		// with non matching standard time offsets.
       
   196 		for( TInt index( count - 1 ); index >= KErrNone; --index )
       
   197 			{
       
   198 			currentZoneOffset = UTCOffSetWithoutDSTChangesL( tzHandle, aTzIdArray[ index ] );
       
   199 			if( currentZoneOffset != offset )
       
   200 				{
       
   201 				aTzIdArray.Remove( index );
       
   202 				}
       
   203 			}
       
   204 		// Cleanup.
       
   205 		CleanupStack::PopAndDestroy( &tzHandle );
       
   206 		}
       
   207 	
       
   208 	__PRINTS( "CClockMCCTzIdMapper::TzIdFromMccL - Exit" );
       
   209 	}
       
   210 
       
   211 // ---------------------------------------------------------
       
   212 // CClockMCCTzIdMapper::TzIdFromOffsetL
       
   213 // rest of the details are commented in the header
       
   214 // ---------------------------------------------------------
       
   215 //
       
   216 void CClockMCCTzIdMapper::TzIdFromOffsetL( const TTimeIntervalMinutes& aStdTimeOffset, RArray< CTzId >& aTzIdArray )
       
   217 	{
       
   218 	__PRINTS( "CClockMCCTzIdMapper::TzIdFromOffsetL - Entry" );
       
   219 	
       
   220 	// Reset the position in the resource buffer
       
   221 	iResourceReader.SetBuffer( iResourceBuffer );
       
   222 	// First TInt is the number of array items
       
   223 	TInt numberOfItems( iResourceReader.ReadInt16() );
       
   224 
       
   225 	// Loop through the resource array reading the TzId
       
   226 	for( TInt index( KErrNone ); index < numberOfItems; ++index )
       
   227 		{
       
   228 		TInt timeZoneId( iResourceReader.ReadInt16() );
       
   229 		TInt mobileCC( iResourceReader.ReadInt16() );
       
   230 		
       
   231 		CTzId* timeZone = CTzId::NewL( timeZoneId );
       
   232 		CleanupStack::PushL( timeZone );
       
   233 		
       
   234 		aTzIdArray.AppendL( *timeZone );
       
   235 		
       
   236 		CleanupStack::PopAndDestroy( timeZone );
       
   237 		timeZone = NULL;
       
   238 		}
       
   239 	
       
   240 	// Limit the returned entries to those with a specific StdTimeOffset
       
   241 	TInt timeOffset( aStdTimeOffset.Int() );
       
   242 
       
   243 	// Connect to Tz
       
   244 	RTz tzHandle;
       
   245 	CleanupClosePushL( tzHandle );
       
   246 	User::LeaveIfError( tzHandle.Connect() );
       
   247 
       
   248 	TInt currentZoneOffset;
       
   249 	TInt idCount( aTzIdArray.Count() );
       
   250 
       
   251 	// Loop through the array in reverse order, removing all entries
       
   252 	// with non matching standard time offsets.
       
   253 	for( TInt index( idCount - 1 ); index >= KErrNone; --index )
       
   254 		{
       
   255 		currentZoneOffset = UTCOffSetWithoutDSTChangesL( tzHandle, aTzIdArray[ index ] );
       
   256 		if( currentZoneOffset != timeOffset )
       
   257 			{
       
   258 			aTzIdArray.Remove( index );
       
   259 			}
       
   260 		}
       
   261 	CleanupStack::PopAndDestroy( &tzHandle );
       
   262 	
       
   263 	// This is a check to see if the timezone array has multiple entries with same timezone ID.
       
   264 	// This happens because many countries have multiple MCCs for a single timezone ID.
       
   265 	// For ex: India has MCCs 404 and 405 for the same timezone ID 1624 and offset GMT+5:30.
       
   266 	TInt zoneCount( aTzIdArray.Count() );
       
   267 	if( 1 < zoneCount )
       
   268 		{
       
   269 		CTzId& prevTzId = aTzIdArray[ KErrNone ];
       
   270 		for( TInt zoneIndex( zoneCount - 1 ); zoneIndex > KErrNone; --zoneIndex )
       
   271 			{
       
   272 			CTzId& curTzId = aTzIdArray[ zoneIndex ];
       
   273 			if( prevTzId != curTzId )
       
   274 				{
       
   275 				// There are multiple timezones. So skip.
       
   276 				break;
       
   277 				}
       
   278 			else
       
   279 				{
       
   280 				// Keep removing duplicate zone IDs.
       
   281 				aTzIdArray.Remove( zoneIndex );
       
   282 				}
       
   283 			}
       
   284 		}
       
   285 
       
   286 	__PRINTS( "CClockMCCTzIdMapper::TzIdFromOffsetL - Exit" );
       
   287 	}
       
   288 
       
   289 // ---------------------------------------------------------
       
   290 // CClockMCCTzIdMapper::DoGetTzIdFromMCCL
       
   291 // rest of the details are commented in the header
       
   292 // ---------------------------------------------------------
       
   293 //
       
   294 void CClockMCCTzIdMapper::DoGetTzIdFromMCCL( TInt aMcc, RArray< CTzId >& aTzIdArray )
       
   295 	{
       
   296 	__PRINTS( "CClockMCCTzIdMapper::DoGetTzIdFromMCCL - Entry" );
       
   297 	
       
   298 	__PRINT( "MCC:%d", aMcc );
       
   299 	
       
   300 	// Reset the position in the resource buffer.
       
   301 	iResourceReader.SetBuffer( iResourceBuffer );
       
   302 	// First TInt is the number of array items.
       
   303 	TInt numberOfItems( iResourceReader.ReadInt16() );
       
   304 
       
   305 	TInt timeZoneId;
       
   306 	TInt mobileCC;
       
   307 	CTzId* matchingDSTZone( NULL );
       
   308 
       
   309 	// Loop through the resource array reading the TzId / MCC pairs.
       
   310 	for( TInt index( KErrNone ); index < numberOfItems; ++index )
       
   311 		{
       
   312 		timeZoneId = iResourceReader.ReadInt16();
       
   313 		mobileCC = iResourceReader.ReadInt16();
       
   314 
       
   315 		// Found a match - create a new CTzId and append to the TzId array.
       
   316 		if( mobileCC == aMcc )
       
   317 			{
       
   318 			matchingDSTZone = CTzId::NewL( timeZoneId );
       
   319 			CleanupStack::PushL( matchingDSTZone );
       
   320 			
       
   321 			aTzIdArray.AppendL( *matchingDSTZone );
       
   322 			
       
   323 			CleanupStack::PopAndDestroy( matchingDSTZone );
       
   324 			
       
   325 			matchingDSTZone = NULL;
       
   326 			}
       
   327 		}
       
   328 	
       
   329 	__PRINTS( "CClockMCCTzIdMapper::DoGetTzIdFromMCCL - Exit" );
       
   330 	}
       
   331 
       
   332 // ---------------------------------------------------------
       
   333 // CClockMCCTzIdMapper::MCCFromTzIdL
       
   334 // rest of the details are commented in the header
       
   335 // ---------------------------------------------------------
       
   336 //
       
   337 TInt CClockMCCTzIdMapper::MCCFromTzIdL( const CTzId& aTzId )
       
   338 	{
       
   339 	__PRINTS( "CClockMCCTzIdMapper::MCCFromTzIdL - Entry" );
       
   340 	
       
   341 	// Reset the position in the resource buffer
       
   342 	iResourceReader.SetBuffer( iResourceBuffer );
       
   343 	// First TInt is the number of array items
       
   344 	TInt numberOfItems( iResourceReader.ReadInt16() );
       
   345 	TInt timeZoneId;
       
   346 	TInt mobileCC;
       
   347 
       
   348 	// Loop through the resource array reading the TzId / MCC pairs
       
   349 	for( TInt index( KErrNone ); index < numberOfItems; ++index )
       
   350 		{
       
   351 		timeZoneId = iResourceReader.ReadInt16();
       
   352 		mobileCC = iResourceReader.ReadInt16();
       
   353 
       
   354 		// Found a match return the mcc
       
   355 		if( timeZoneId == aTzId.TimeZoneNumericID() )
       
   356 			{
       
   357 			__PRINT( "Found a match, MCC - %d", mobileCC );
       
   358 			
       
   359 			return mobileCC;
       
   360 			}
       
   361 		}
       
   362 	
       
   363 	__PRINTS( "CClockMCCTzIdMapper::MCCFromTzIdL - Exit" );
       
   364 	
       
   365 	return KErrNotFound;
       
   366 	}
       
   367 
       
   368 // End of file