clock2/clockengines/clocktimezoneresolver/src/clockmcctzmapper.cpp
changeset 0 f979ecb2b13e
child 14 21239b3bcd78
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     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 	__PRINTS( "CClockMCCTzIdMapper::UTCOffSetWithoutDSTChangesL - Entry" );
       
   147 	
       
   148 	TInt timeOffset( KErrNotFound );
       
   149 
       
   150 	TTime universalTime;
       
   151 	universalTime.UniversalTime();
       
   152 
       
   153 	// Get the current rules for the timezone rules
       
   154 	CTzRules* currentRules = aTz.GetTimeZoneRulesL( aTzId, universalTime, universalTime, ETzUtcTimeReference );
       
   155 	if( currentRules )
       
   156 	    {
       
   157 	    timeOffset = currentRules->InitialStdTimeOffset();
       
   158 	    delete currentRules;
       
   159 	    }
       
   160 	
       
   161 	__PRINTS( "CClockMCCTzIdMapper::UTCOffSetWithoutDSTChangesL - Exit" );
       
   162 
       
   163 	return timeOffset;
       
   164 	}
       
   165 
       
   166 // ---------------------------------------------------------
       
   167 // CClockMCCTzIdMapper::TzIdFromMccL
       
   168 // rest of the details are commented in the header
       
   169 // ---------------------------------------------------------
       
   170 //
       
   171 void CClockMCCTzIdMapper::TzIdFromMccL( const RMobilePhone::TMobilePhoneNetworkCountryCode& aMCC,
       
   172 								        RArray< CTzId >& aTzIdArray,
       
   173 								        const TTimeIntervalMinutes& aStdTimeOffset )
       
   174 	{
       
   175 	__PRINTS( "CClockMCCTzIdMapper::TzIdFromMccL - Entry" );
       
   176 	
       
   177 	// Convert the TMobilePhoneNetworkCountryCode to a TInt.
       
   178 	TInt currentMcc;
       
   179 	TLex lexUtil( aMCC );
       
   180 	User::LeaveIfError( lexUtil.Val( currentMcc ) );
       
   181 	
       
   182 	__PRINT( "MCC: %d", currentMcc );
       
   183 
       
   184 	// Get all the DST zones that match mcc
       
   185 	DoGetTzIdFromMCCL( currentMcc, aTzIdArray );
       
   186 
       
   187 	// Limit the returned entries to those with a specific StdTimeOffset
       
   188 	TInt offset = aStdTimeOffset.Int();
       
   189 	if( offset != -1 )
       
   190 		{
       
   191 		// Connect to Tz
       
   192 		RTz tzHandle;
       
   193 		CleanupClosePushL( tzHandle );
       
   194 		User::LeaveIfError( tzHandle.Connect() );
       
   195 
       
   196 		TInt currentZoneOffset;
       
   197 		TInt count = aTzIdArray.Count();
       
   198 		// Loop through the array in reverse order, removing all entries
       
   199 		// with non matching standard time offsets.
       
   200 		for( TInt index( count - 1 ); index >= KErrNone; --index )
       
   201 			{
       
   202 			currentZoneOffset = UTCOffSetWithoutDSTChangesL( tzHandle, aTzIdArray[ index ] );
       
   203 			if( currentZoneOffset != offset )
       
   204 				{
       
   205 				aTzIdArray.Remove( index );
       
   206 				}
       
   207 			}
       
   208 		// Cleanup.
       
   209 		CleanupStack::PopAndDestroy( &tzHandle );
       
   210 		}
       
   211 	
       
   212 	__PRINTS( "CClockMCCTzIdMapper::TzIdFromMccL - Exit" );
       
   213 	}
       
   214 
       
   215 // ---------------------------------------------------------
       
   216 // CClockMCCTzIdMapper::TzIdFromOffsetL
       
   217 // rest of the details are commented in the header
       
   218 // ---------------------------------------------------------
       
   219 //
       
   220 void CClockMCCTzIdMapper::TzIdFromOffsetL( const TTimeIntervalMinutes& aStdTimeOffset, RArray< CTzId >& aTzIdArray )
       
   221 	{
       
   222 	__PRINTS( "CClockMCCTzIdMapper::TzIdFromOffsetL - Entry" );
       
   223 	
       
   224 	// Reset the position in the resource buffer
       
   225 	iResourceReader.SetBuffer( iResourceBuffer );
       
   226 	// First TInt is the number of array items
       
   227 	TInt numberOfItems( iResourceReader.ReadInt16() );
       
   228 
       
   229 	// Loop through the resource array reading the TzId
       
   230 	for( TInt index( KErrNone ); index < numberOfItems; ++index )
       
   231 		{
       
   232 		TInt timeZoneId( iResourceReader.ReadInt16() );
       
   233 		TInt mobileCC( iResourceReader.ReadInt16() );
       
   234 		
       
   235 		CTzId* timeZone = CTzId::NewL( timeZoneId );
       
   236 		CleanupStack::PushL( timeZone );
       
   237 		
       
   238 		aTzIdArray.AppendL( *timeZone );
       
   239 		
       
   240 		CleanupStack::PopAndDestroy( timeZone );
       
   241 		timeZone = NULL;
       
   242 		}
       
   243 	
       
   244 	// Limit the returned entries to those with a specific StdTimeOffset
       
   245 	TInt timeOffset( aStdTimeOffset.Int() );
       
   246 
       
   247 	// Connect to Tz
       
   248 	RTz tzHandle;
       
   249 	CleanupClosePushL( tzHandle );
       
   250 	User::LeaveIfError( tzHandle.Connect() );
       
   251 
       
   252 	TInt currentZoneOffset;
       
   253 	TInt idCount( aTzIdArray.Count() );
       
   254 
       
   255 	// Loop through the array in reverse order, removing all entries
       
   256 	// with non matching standard time offsets.
       
   257 	for( TInt index( idCount - 1 ); index >= KErrNone; --index )
       
   258 		{
       
   259 		currentZoneOffset = UTCOffSetWithoutDSTChangesL( tzHandle, aTzIdArray[ index ] );
       
   260 		if( currentZoneOffset != timeOffset )
       
   261 			{
       
   262 			aTzIdArray.Remove( index );
       
   263 			}
       
   264 		}
       
   265 	CleanupStack::PopAndDestroy( &tzHandle );
       
   266 	
       
   267 	// This is a check to see if the timezone array has multiple entries with same timezone ID.
       
   268 	// This happens because many countries have multiple MCCs for a single timezone ID.
       
   269 	// For ex: India has MCCs 404 and 405 for the same timezone ID 1624 and offset GMT+5:30.
       
   270 	TInt zoneCount( aTzIdArray.Count() );
       
   271 	if( 1 < zoneCount )
       
   272 		{
       
   273 		CTzId& prevTzId = aTzIdArray[ KErrNone ];
       
   274 		for( TInt zoneIndex( zoneCount - 1 ); zoneIndex > KErrNone; --zoneIndex )
       
   275 			{
       
   276 			CTzId& curTzId = aTzIdArray[ zoneIndex ];
       
   277 			if( prevTzId != curTzId )
       
   278 				{
       
   279 				// There are multiple timezones. So skip.
       
   280 				break;
       
   281 				}
       
   282 			else
       
   283 				{
       
   284 				// Keep removing duplicate zone IDs.
       
   285 				aTzIdArray.Remove( zoneIndex );
       
   286 				}
       
   287 			}
       
   288 		}
       
   289 
       
   290 	__PRINTS( "CClockMCCTzIdMapper::TzIdFromOffsetL - Exit" );
       
   291 	}
       
   292 
       
   293 // ---------------------------------------------------------
       
   294 // CClockMCCTzIdMapper::DoGetTzIdFromMCCL
       
   295 // rest of the details are commented in the header
       
   296 // ---------------------------------------------------------
       
   297 //
       
   298 void CClockMCCTzIdMapper::DoGetTzIdFromMCCL( TInt aMcc, RArray< CTzId >& aTzIdArray )
       
   299 	{
       
   300 	__PRINTS( "CClockMCCTzIdMapper::DoGetTzIdFromMCCL - Entry" );
       
   301 	
       
   302 	__PRINT( "MCC:%d", aMcc );
       
   303 	
       
   304 	// Reset the position in the resource buffer.
       
   305 	iResourceReader.SetBuffer( iResourceBuffer );
       
   306 	// First TInt is the number of array items.
       
   307 	TInt numberOfItems( iResourceReader.ReadInt16() );
       
   308 
       
   309 	TInt timeZoneId;
       
   310 	TInt mobileCC;
       
   311 	CTzId* matchingDSTZone( NULL );
       
   312 
       
   313 	// Loop through the resource array reading the TzId / MCC pairs.
       
   314 	for( TInt index( KErrNone ); index < numberOfItems; ++index )
       
   315 		{
       
   316 		timeZoneId = iResourceReader.ReadInt16();
       
   317 		mobileCC = iResourceReader.ReadInt16();
       
   318 
       
   319 		// Found a match - create a new CTzId and append to the TzId array.
       
   320 		if( mobileCC == aMcc )
       
   321 			{
       
   322 			matchingDSTZone = CTzId::NewL( timeZoneId );
       
   323 			CleanupStack::PushL( matchingDSTZone );
       
   324 			
       
   325 			aTzIdArray.AppendL( *matchingDSTZone );
       
   326 			
       
   327 			CleanupStack::PopAndDestroy( matchingDSTZone );
       
   328 			
       
   329 			matchingDSTZone = NULL;
       
   330 			}
       
   331 		}
       
   332 	
       
   333 	__PRINTS( "CClockMCCTzIdMapper::DoGetTzIdFromMCCL - Exit" );
       
   334 	}
       
   335 
       
   336 // ---------------------------------------------------------
       
   337 // CClockMCCTzIdMapper::MCCFromTzIdL
       
   338 // rest of the details are commented in the header
       
   339 // ---------------------------------------------------------
       
   340 //
       
   341 TInt CClockMCCTzIdMapper::MCCFromTzIdL( const CTzId& aTzId )
       
   342 	{
       
   343 	__PRINTS( "CClockMCCTzIdMapper::MCCFromTzIdL - Entry" );
       
   344 	
       
   345 	// Reset the position in the resource buffer
       
   346 	iResourceReader.SetBuffer( iResourceBuffer );
       
   347 	// First TInt is the number of array items
       
   348 	TInt numberOfItems( iResourceReader.ReadInt16() );
       
   349 	TInt timeZoneId;
       
   350 	TInt mobileCC;
       
   351 
       
   352 	// Loop through the resource array reading the TzId / MCC pairs
       
   353 	for( TInt index( KErrNone ); index < numberOfItems; ++index )
       
   354 		{
       
   355 		timeZoneId = iResourceReader.ReadInt16();
       
   356 		mobileCC = iResourceReader.ReadInt16();
       
   357 
       
   358 		// Found a match return the mcc
       
   359 		if( timeZoneId == aTzId.TimeZoneNumericID() )
       
   360 			{
       
   361 			__PRINT( "Found a match, MCC - %d", mobileCC );
       
   362 			
       
   363 			return mobileCC;
       
   364 			}
       
   365 		}
       
   366 	
       
   367 	__PRINTS( "CClockMCCTzIdMapper::MCCFromTzIdL - Exit" );
       
   368 	
       
   369 	return KErrNotFound;
       
   370 	}
       
   371 
       
   372 // End of file