tzservices/tzserver/Server/Source/tzsystemdata.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2008-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 "tzsystemdata.h"
       
    17 #include <bautils.h>
       
    18 #include <tzlocalizedcityrecord.h>
       
    19 #include <tzlocalizedtimezonerecord.h>
       
    20 
       
    21 // Resource File locations
       
    22 _LIT(KTzLocalizationTimeZoneResourceFileName,"\\Resource\\TimeZoneLocalization\\timezones.rSC");
       
    23 _LIT(KTzLocalizationGroupResourceFileName,"\\Resource\\TimeZoneLocalization\\timezonegroups.rSC");
       
    24 _LIT(KFlashPath,"c:\\Resource\\TimeZoneLocalization\\");
       
    25 _LIT(KFlashDrive, "c:");
       
    26 _LIT(KRom, "z:");
       
    27 
       
    28 /**
       
    29 Allocates and constructs a new CTzLocalizationResourceReader object
       
    30 @return the newly constructed CTzLocalizationResourceReader
       
    31 @internalTechnology
       
    32 */
       
    33 CTzSystemDataDb* CTzSystemDataDb::NewLC()
       
    34 	{
       
    35 	CTzSystemDataDb* self = new(ELeave) CTzSystemDataDb();
       
    36 	CleanupStack::PushL(self);
       
    37 	self->ConstructL();
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 /**
       
    42 Destructor
       
    43 @internalTechnology
       
    44 */
       
    45 CTzSystemDataDb::~CTzSystemDataDb()
       
    46 	{
       
    47 	iTimeZoneResourceFile.Close();
       
    48 	if(iGroupResourceFileExists)
       
    49 		{
       
    50 		iGroupResourceFile.Close();
       
    51 		}
       
    52 	delete iResourceBuffer;
       
    53 	iFs.Close();
       
    54 	}
       
    55 
       
    56 /**
       
    57 Second phase contructor
       
    58 @internalTechnology
       
    59 */
       
    60 void CTzSystemDataDb::ConstructL()
       
    61 	{
       
    62 	User::LeaveIfError(iFs.Connect());
       
    63 	
       
    64 	// If resource files are found on flash drive,
       
    65   	// these will be used exclusively otherwise the
       
    66   	// default ones on ROM will be used.   
       
    67 	CDir* dirList;
       
    68 	// Get a list of files on flash drive
       
    69 	TInt ret = iFs.GetDir(KFlashPath, KEntryAttMaskSupported, ESortNone, dirList);
       
    70 	CleanupStack::PushL(dirList);
       
    71 	TFileName* timeZoneResourceFileName = new(ELeave) TFileName;
       
    72 	CleanupStack::PushL(timeZoneResourceFileName);
       
    73 	TFileName* groupResourceFileName = new(ELeave) TFileName;
       
    74 	CleanupStack::PushL(groupResourceFileName);
       
    75 
       
    76 	TInt count = 0;
       
    77 	if(dirList != NULL)
       
    78 		{
       
    79 		count = dirList->Count();		
       
    80 		}
       
    81 	// Use the files on ROM
       
    82 	if(ret == KErrPathNotFound || count == 0)
       
    83 		{
       
    84 		timeZoneResourceFileName->Format(KRom);
       
    85 		timeZoneResourceFileName->Append(KTzLocalizationTimeZoneResourceFileName);
       
    86 
       
    87 		groupResourceFileName->Format(KRom);
       
    88 		groupResourceFileName->Append(KTzLocalizationGroupResourceFileName);		
       
    89 		}
       
    90 	// Use the files on system drive
       
    91 	else if(ret == KErrNone)
       
    92 		{
       
    93 		if(count > 0)
       
    94 			{
       
    95 			timeZoneResourceFileName->Format(KFlashDrive);
       
    96 			timeZoneResourceFileName->Append(KTzLocalizationTimeZoneResourceFileName);
       
    97 			
       
    98 			groupResourceFileName->Format(KFlashDrive);
       
    99 			groupResourceFileName->Append(KTzLocalizationGroupResourceFileName);
       
   100 			}
       
   101 		}
       
   102 	else
       
   103 		{
       
   104 		User::Leave(ret);
       
   105 		}
       
   106 	// Form the time zone resource filename using BaflUtils::NearestLanguageFile
       
   107 	BaflUtils::NearestLanguageFile(iFs,*timeZoneResourceFileName);
       
   108 
       
   109 	// Form the time zone group resource filename using BaflUtils::NearestLanguageFile
       
   110 	BaflUtils::NearestLanguageFile(iFs,*groupResourceFileName);
       
   111 	
       
   112 	// Open resource files - this can be done now, as it is opened in EFileShareReadersOnly mode		
       
   113 	iTimeZoneResourceFile.OpenL(iFs,*timeZoneResourceFileName);
       
   114 	iTimeZoneResourceFile.ConfirmSignatureL();
       
   115 		
       
   116 	TRAPD(groupResErr,iGroupResourceFile.OpenL(iFs,*groupResourceFileName));
       
   117 	
       
   118 	// Set iGroupResourceFileExists to so that it can be checked before 
       
   119 	// iGroupResourceFile is used
       
   120 	iGroupResourceFileExists = (groupResErr == KErrNone);
       
   121 	if(iGroupResourceFileExists)
       
   122 		{
       
   123 		iGroupResourceFile.ConfirmSignatureL();
       
   124 		iGroupResourceFileExists = ETrue;
       
   125 		}
       
   126 	CleanupStack::PopAndDestroy(3, dirList); // groupResourceFileName, timeZoneResourceFileName, dirList
       
   127 	}
       
   128 
       
   129 /**
       
   130 Searches the time zone resource file for the given time zone ID. This function will
       
   131 leave if the specified ID is not found.
       
   132 @param aTimeZoneId time zone to search for.
       
   133 @return The resource ID or KErrNotFound.
       
   134 @internalTechnology
       
   135 */
       
   136 TInt CTzSystemDataDb::FindTimeZoneResourceIdL(TInt aTimeZoneId)
       
   137 	{
       
   138 	TInt initialOffset = FirstTimeZoneResourceIdL();
       
   139 
       
   140 	TInt idToCheck;
       
   141 
       
   142 	// Search through all resources
       
   143 	TInt i = 0;
       
   144 	while(iTimeZoneResourceFile.OwnsResourceIdL(initialOffset + i))
       
   145 		{
       
   146 		// Read current resource contents
       
   147 		BufferResourceL(iTimeZoneResourceFile,initialOffset + i);
       
   148 
       
   149 		// get the first int16 (WORD in resource STRUCT)
       
   150 		idToCheck = iResourceReader.ReadInt16();
       
   151 
       
   152 		// get rid of the buffer, it's no longer needed
       
   153 		ResetResourceBuffer();
       
   154 
       
   155 		// perform check
       
   156 		if(idToCheck == aTimeZoneId)
       
   157 			{
       
   158 			// return the current resource ID
       
   159 			return initialOffset + i;
       
   160 			}
       
   161 		++i;
       
   162 		}
       
   163 
       
   164 	// If it's got to here the aId hasn't been found, so leave
       
   165 	User::Leave(KErrNotFound);
       
   166 
       
   167 	return KErrNotFound;	// To satisfy compiler
       
   168 	}
       
   169 
       
   170 /**
       
   171 Returns the resource Id of the specified resource. This
       
   172 leaves if this resource is not found in the resource file.
       
   173 @param aResourceFile The resource file that the offset belongs to.
       
   174 @param aOffset The offset to return.
       
   175 @return The resource Id of the first resource containing localised data.
       
   176 @internalTechnology
       
   177 */
       
   178 TInt CTzSystemDataDb::LocalizedResourceIdL(const RResourceFile& aResourceFile, const TTzResourceOffset aOffset)
       
   179 	{
       
   180 	TInt resourceIdToReturn = aResourceFile.Offset();
       
   181 
       
   182 	// Skip on the relevant number of resources, using the Offset enum
       
   183 	resourceIdToReturn += aOffset;
       
   184 
       
   185 	if(!aResourceFile.OwnsResourceIdL(resourceIdToReturn))
       
   186 		{
       
   187 		User::Leave(KErrNotFound);
       
   188 		}
       
   189 
       
   190 	return resourceIdToReturn;
       
   191 	}
       
   192 
       
   193 /**
       
   194 Returns the resource Id of the first time zone resource. This
       
   195 leaves if this resource is not found in the resource file.
       
   196 @return The resource Id of the first time zone resource.
       
   197 @internalTechnology
       
   198 */
       
   199 TInt CTzSystemDataDb::FirstTimeZoneResourceIdL()
       
   200 	{
       
   201 	return LocalizedResourceIdL(iTimeZoneResourceFile,ETzFirstTimeZoneResource);
       
   202 	}
       
   203 
       
   204 /**
       
   205 Returns the resource Id of the resource containing the default cached zone
       
   206 information. This leaves if this resource is not found in the resource file.
       
   207 @return The resource Id of the resource containing the default cached zone
       
   208 information.
       
   209 @internalTechnology
       
   210 */
       
   211 TInt CTzSystemDataDb::CachedTimeZoneResourceIdL()
       
   212 	{
       
   213 	return LocalizedResourceIdL(iTimeZoneResourceFile,ETzFirstLocalizedResource);
       
   214 	}
       
   215 
       
   216 /**
       
   217 Fills iResourceBuffer with the resource specified by aResourceId and sets up
       
   218 iResourceReader to read from this resource. This should always be followed by a
       
   219 call to ResetResourceBuffer.  This function will leave if the requested resource
       
   220 is not found in the specified resource file.
       
   221 @param aResourceFile The resourcefile to read from.
       
   222 @param aResourceId The resource to read from.
       
   223 @internalTechnology
       
   224 */
       
   225 void CTzSystemDataDb::BufferResourceL(const RResourceFile& aResourceFile, TInt aResourceId)
       
   226 	{
       
   227 	if(!aResourceFile.OwnsResourceIdL(aResourceId))
       
   228 		{
       
   229 		User::Leave(KErrNotFound);
       
   230 		}
       
   231 
       
   232 	// Assign buffer for holding resource
       
   233 	iResourceBuffer = aResourceFile.AllocReadL(aResourceId);
       
   234 
       
   235 	// TResourceReader for getting data out of the buffer
       
   236 	iResourceReader.SetBuffer(iResourceBuffer);
       
   237 	}
       
   238 
       
   239 /**
       
   240 Resets the iResourceBuffer that contains the binary data of a resource.
       
   241 @internalTechnology
       
   242 */
       
   243 void CTzSystemDataDb::ResetResourceBuffer()
       
   244 	{
       
   245 	delete iResourceBuffer;
       
   246 	iResourceBuffer = NULL;
       
   247 	}
       
   248 
       
   249 CTzLocalizedTimeZoneRecord* CTzSystemDataDb::ReadTimeZoneL(TInt aTimeZoneId)
       
   250 	{
       
   251 	TInt resourceId = FindTimeZoneResourceIdL(aTimeZoneId);
       
   252 	return DoReadTimeZoneL(resourceId);
       
   253 	}
       
   254 
       
   255 /**
       
   256 Reads in and returns a localized time zone found at the specified resource id.
       
   257 @param aResourceId The resource ID of the time zone in the time zone resource
       
   258 file
       
   259 @return The localized time zone found at the specified resource ID.
       
   260 @internalTechnology
       
   261 */
       
   262 CTzLocalizedTimeZoneRecord* CTzSystemDataDb::DoReadTimeZoneL(TInt aResourceId)
       
   263 	{
       
   264 	BufferResourceL(iTimeZoneResourceFile,aResourceId);
       
   265 	CTzLocalizedTimeZoneRecord* timeZone = CreateTimeZoneRecordFromResourceL(aResourceId);
       
   266 	ResetResourceBuffer();
       
   267 	return timeZone;
       
   268 	}
       
   269 
       
   270 /**
       
   271 This does the actual reading of a localized time zone from the resource file.
       
   272 The iResourceReader MUST already have been set up with a call to
       
   273 BufferResourceL(iTimeZoneResourceFile,...), BEFORE this function is called.
       
   274 
       
   275 @return A CTzLocalizedTimeZone containing the localized time zone information
       
   276 that is currently found in iResourceReader.
       
   277 
       
   278 @internalTechnology
       
   279 */
       
   280 CTzLocalizedTimeZoneRecord* CTzSystemDataDb::CreateTimeZoneRecordFromResourceL(TInt aResourceId)
       
   281 	{
       
   282 	// Read localized time zone data form the resource file in the correct order
       
   283 	TInt16 timeZoneId = (TInt16)iResourceReader.ReadInt16();
       
   284 	TPtrC standardName(iResourceReader.ReadTPtrC());
       
   285 	TPtrC daylightName(iResourceReader.ReadTPtrC());
       
   286 	TPtrC shortStandardName(iResourceReader.ReadTPtrC());
       
   287 	TPtrC shortDaylightName(iResourceReader.ReadTPtrC());
       
   288 
       
   289 	CTzLocalizedTimeZoneRecord* timeZone = CTzLocalizedTimeZoneRecord::NewL(timeZoneId, standardName,
       
   290 		daylightName, shortStandardName, shortDaylightName, aResourceId);
       
   291 
       
   292 	return timeZone;
       
   293 	}
       
   294 
       
   295 /**
       
   296 From MTzLocalizationReader Read the cities that are in the specified time zone
       
   297 into the specified city array.
       
   298 @param aCities An array of cities that will contain the cities from the
       
   299 specified time zone.
       
   300 @param aTimeZoneId The ID of the time zone containing the cities to return,.
       
   301 @leave KErrNotFound Leaves if the specified time zone is not found or it
       
   302 contains no cities.
       
   303 @internalTechnology
       
   304 */
       
   305 void CTzSystemDataDb::ReadCitiesL(RPointerArray<CTzLocalizedCityRecord>& aCities, TInt aTimeZoneId)
       
   306 	{
       
   307 	TInt resourceId = FindTimeZoneResourceIdL(aTimeZoneId);
       
   308 	DoReadCitiesL(aCities, resourceId);
       
   309 	}
       
   310 
       
   311 /**
       
   312 Reads in and adds the array of cities contained in the specified resource ID to
       
   313 the supplied city array
       
   314 @param The array of cities to which the the cities found in the time zone at
       
   315 the specified resource ID will be appended to.
       
   316 @param aResourceId The resource ID of the time zone that contains the cities to
       
   317 be read.
       
   318 @internalTechnology
       
   319 */
       
   320 void CTzSystemDataDb::DoReadCitiesL(RPointerArray<CTzLocalizedCityRecord>& aCities, TInt aResourceId)
       
   321 	{
       
   322 	BufferResourceL(iTimeZoneResourceFile,aResourceId);
       
   323 	AddCityArrayFromResourceL(aCities,aResourceId);
       
   324 	ResetResourceBuffer();
       
   325 	}
       
   326 
       
   327 /**
       
   328 This does the actual reading of the localized cities from the resource file.
       
   329 The iResourceReader MUST already have been set up with a call to
       
   330 BufferResourceL(iTimeZoneResourceFile,...) BEFORE this function is called.
       
   331 @param aCities The array of cities to add the resource cities to.
       
   332 @param aResourceId The resource ID of the time zone containing the cities to
       
   333 read.
       
   334 @return An array of CTzLocalizedCity objects containing the cities that are
       
   335 currently found in iResourceReader.
       
   336 @internalTechnology
       
   337 */
       
   338 void CTzSystemDataDb::AddCityArrayFromResourceL(RPointerArray<CTzLocalizedCityRecord>& aCities, TInt aResourceId)
       
   339 	{
       
   340 	// Eat up the localised time zone info from the resource
       
   341 	CTzLocalizedTimeZoneRecord* timeZone = CreateTimeZoneRecordFromResourceL(aResourceId);
       
   342 	TUint tzid = timeZone->Id();
       
   343 	delete timeZone;
       
   344 	timeZone = NULL;
       
   345 
       
   346 	// Get the number of cities (which automatically precedes the array as a WORD in the STRUCT)
       
   347 	TInt numCities = iResourceReader.ReadInt16();
       
   348 
       
   349 	// Leave if no cities are found
       
   350 	if(numCities < 1)
       
   351 		{
       
   352 		User::Leave(KErrNotFound);
       
   353 		}
       
   354 
       
   355 	aCities.ReserveL(numCities + aCities.Count());
       
   356 	// Loop through all the cities
       
   357 	for(TInt i = 0; i < numCities; ++i)
       
   358 		{
       
   359 		TUint8 groupId = iResourceReader.ReadUint8();
       
   360 		TPtrC cityName;
       
   361 		cityName.Set(iResourceReader.ReadTPtrC());
       
   362 		CTzLocalizedCityRecord* city = CTzLocalizedCityRecord::NewLC(cityName,groupId,i,tzid,aResourceId);
       
   363         aCities.Append(city);
       
   364 		CleanupStack::Pop(city);
       
   365 		}
       
   366 	}
       
   367 
       
   368 CTzLocalizedCityRecord* CTzSystemDataDb::ReadDefaultCityL(TInt aTimeZoneId)
       
   369 	{
       
   370 	// Read the cities from the specified resource
       
   371 	RPointerArray<CTzLocalizedCityRecord> cities;
       
   372 	CleanupStack::PushL(TCleanupItem(CTzLocalizedCityRecord::CleanupArray, &cities));
       
   373 	ReadCitiesL(cities,aTimeZoneId);
       
   374 
       
   375 	// Move first element (which is the default city) out of cities
       
   376 	// to stop it being deleted when cities is
       
   377 	CTzLocalizedCityRecord* defaultCity = cities[0];
       
   378 	cities.Remove(0);
       
   379 
       
   380 	CleanupStack::PopAndDestroy();	// cities
       
   381 	
       
   382 	return defaultCity;
       
   383 	}
       
   384 
       
   385 TInt CTzSystemDataDb::ReadFrequentlyUsedZoneIdL(TInt aFrequentlyUsedZone)
       
   386 	{
       
   387 	// You cannot pass ECachedTimeZones in as the argument, because it is only
       
   388 	// used to keep count of how many cached zones there are.
       
   389 	__ASSERT_ALWAYS(aFrequentlyUsedZone != CTzLocalizedTimeZone::ECachedTimeZones, User::Leave(KErrArgument));
       
   390 
       
   391 	TInt offset = CachedTimeZoneResourceIdL();
       
   392 
       
   393 	BufferResourceL(iTimeZoneResourceFile,offset);
       
   394 	
       
   395 	TInt tzid = 0;
       
   396 	TInt defaultHomeZoneId = iResourceReader.ReadInt16();
       
   397 	TInt defaultInterestZoneId = iResourceReader.ReadInt16();
       
   398 	TInt defaultRecentZone1Id = iResourceReader.ReadInt16();
       
   399 	TInt defaultRecentZone2Id = iResourceReader.ReadInt16();
       
   400 	switch(aFrequentlyUsedZone)
       
   401 			{
       
   402 			case CTzLocalizedTimeZone::EHomeZone:
       
   403 				tzid = defaultHomeZoneId;
       
   404 				break;
       
   405 			case CTzLocalizedTimeZone::EInterestZone:
       
   406 				tzid = defaultInterestZoneId;
       
   407 				break;
       
   408 			case CTzLocalizedTimeZone::ERecentZone1:
       
   409 				tzid = defaultRecentZone1Id;
       
   410 				break;
       
   411 			case CTzLocalizedTimeZone::ERecentZone2:
       
   412 				tzid = defaultRecentZone2Id;
       
   413 				break;
       
   414 			case CTzLocalizedTimeZone::ECurrentZone:	// FALL THROUGH to default - current zone not supported
       
   415 			default:
       
   416 				User::Leave(KErrArgument);
       
   417 			}
       
   418 	ResetResourceBuffer();
       
   419 	return tzid;
       
   420 	}
       
   421 
       
   422 CTzLocalizedTimeZoneRecord* CTzSystemDataDb::ReadFrequentlyUsedZoneL(TInt aFrequentlyUsedZone)
       
   423 	{
       
   424 	TInt tzid = ReadFrequentlyUsedZoneIdL(aFrequentlyUsedZone);
       
   425 	return ReadTimeZoneL(tzid);
       
   426 	}