tzservices/tzloc/src/tzlocalizationuserdatareader.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 "tzlocalizationuserdatareader.h"
       
    17 #include "TzLocalizationDataTypes.h"
       
    18 #include "TzLocalizer.h"
       
    19 #include <tz.h>
       
    20 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    21 #include <tzusernames.h>
       
    22 #include <tzuserdefineddata.h>
       
    23 #endif
       
    24 
       
    25 LOCAL_D void ResetAndDestroyTzIdArray(TAny* aPtr)
       
    26 	{
       
    27 	RPointerArray<CTzId>* ids = static_cast<RPointerArray<CTzId>*>(aPtr);
       
    28 	if (ids)
       
    29 		{
       
    30 		ids->ResetAndDestroy();
       
    31 		ids->Close();
       
    32 		}
       
    33 	}
       
    34 
       
    35 CTzLocalizationUserDataReader::CTzLocalizationUserDataReader(RTz& aTzSession)
       
    36 	:iTzSession(aTzSession)
       
    37 	{
       
    38 	}
       
    39 
       
    40 void CTzLocalizationUserDataReader::ConstructL()
       
    41 	{
       
    42 	iUserData = CTzUserData::NewL(iTzSession);
       
    43 	}
       
    44 /**
       
    45 Allocates and constructs a new CTzLocalizationUserDataReader
       
    46 @return the newly constructed CTzLocalizationUserDataReader
       
    47 @internalTechnology
       
    48 */
       
    49 CTzLocalizationUserDataReader* CTzLocalizationUserDataReader::NewL(RTz& aTzSession)
       
    50 	{
       
    51 	CTzLocalizationUserDataReader* self = CTzLocalizationUserDataReader::NewLC(aTzSession);
       
    52 	CleanupStack::Pop(self);
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 /**
       
    57 Allocates and constructs a new CTzLocalizationUserDataReader
       
    58 The pointer to the new object is left on the cleanup stack
       
    59 @return the newly constructed CTzLocalizationUserDataReader
       
    60 @internalTechnology
       
    61 */
       
    62 CTzLocalizationUserDataReader* CTzLocalizationUserDataReader::NewLC(RTz& aTzSession)
       
    63 	{
       
    64 	CTzLocalizationUserDataReader* self = new (ELeave) CTzLocalizationUserDataReader(aTzSession);
       
    65 	CleanupStack::PushL(self);
       
    66 	self->ConstructL();
       
    67 	return self;
       
    68 	}
       
    69 
       
    70 /**
       
    71 Destructor - frees resources
       
    72 @internalTechnology
       
    73 */
       
    74 CTzLocalizationUserDataReader::~CTzLocalizationUserDataReader()
       
    75 	{
       
    76 	delete iUserData;
       
    77 	}
       
    78 
       
    79 /** Read the time zone with the matching time zone identifier
       
    80 @param aTimeZoneId - time zone id
       
    81 @return CTzLocalizedTimeZone* the localized time zone with matching id
       
    82 @internalTechnology
       
    83 */
       
    84 CTzLocalizedTimeZone* CTzLocalizationUserDataReader::ReadTimeZoneL(const TInt aTimeZoneId)
       
    85 	{
       
    86 	return GetLocalizedTimeZoneL(aTimeZoneId);
       
    87 	}
       
    88 
       
    89 /** Read the time zone with the matching TTzLocalizedId
       
    90 @param aTzLocalizedId - time zone id
       
    91 @return CTzLocalizedTimeZone* the localized time zone with matching id
       
    92 @internalTechnology
       
    93 */
       
    94 CTzLocalizedTimeZone* CTzLocalizationUserDataReader::ReadTimeZoneL(const TTzLocalizedId& aTzLocalizedId)
       
    95 	{
       
    96 	return ReadTimeZoneL(aTzLocalizedId.TimeZoneId());
       
    97 	}
       
    98 
       
    99 /** Read all time zones stored in user database
       
   100 @internalTechnology
       
   101 @param aTimeZones array of localized time zones.
       
   102 */
       
   103 void CTzLocalizationUserDataReader::ReadAllTimeZonesL(CTzLocalizedTimeZoneArray& aTimeZones)
       
   104 	{
       
   105 	RPointerArray<CTzId> tzids;
       
   106 	CleanupStack::PushL(TCleanupItem(ResetAndDestroyTzIdArray, &tzids));
       
   107 	iUserData->GetTzIdsL(tzids);
       
   108 	const TInt count = tzids.Count();
       
   109 	aTimeZones.ReserveL(count + aTimeZones.Count());
       
   110 	for(TInt ii=0; ii<count; ++ii)
       
   111 		{
       
   112 		CTzLocalizedTimeZone* localizedTz = GetLocalizedTimeZoneL(tzids[ii]->TimeZoneNumericID());
       
   113 		aTimeZones.AppendL(localizedTz);
       
   114 		}
       
   115 	CleanupStack::PopAndDestroy(&tzids);
       
   116 	}
       
   117 
       
   118 /** Reads all the user defined cities stored in the
       
   119 database into the specified CTzLocalizedCityArray. If the database is empty the
       
   120 returned array will contain no elements.
       
   121 @param aCities Array of cities to add the user defined to.
       
   122 @internalTechnology
       
   123 */
       
   124 void CTzLocalizationUserDataReader::ReadCitiesL(CTzLocalizedCityArray& aCities)
       
   125 	{
       
   126 	RPointerArray<CTzId> tzids;
       
   127 	CleanupStack::PushL(TCleanupItem(ResetAndDestroyTzIdArray, &tzids));
       
   128 	iUserData->GetTzIdsL(tzids);
       
   129 	const TInt count = tzids.Count();
       
   130 	aCities.ReserveL(count + aCities.Count());
       
   131 	for(TInt ii=0; ii<count; ++ii)
       
   132 		{
       
   133 		CTzLocalizedCity* loccity = GetLocalizedCityL(tzids[ii]->TimeZoneNumericID());
       
   134 		aCities.AppendL(loccity);
       
   135 		}
       
   136 	CleanupStack::PopAndDestroy(&tzids);
       
   137 	}
       
   138 
       
   139 /** Reads the city with the matching aTimeZoneid into the specifed CTzLocalizedCityArray.
       
   140 Note that there only one city in a given user defined time zone.
       
   141 If the database is empty the returned array will contain no elements.
       
   142 @param aCities Array of citeis to add the user defined cities to.
       
   143 @param aTimeZoneId A time zone id identifying a time zone
       
   144 @internalTechnology
       
   145 */
       
   146 void CTzLocalizationUserDataReader::ReadCitiesL(CTzLocalizedCityArray& aCities, const TInt aTimeZoneId)
       
   147 	{
       
   148 	CTzLocalizedCity* loccity = GetLocalizedCityL(aTimeZoneId);
       
   149 	CleanupStack::PushL(loccity);
       
   150 	aCities.AppendL(loccity);
       
   151 	CleanupStack::Pop(loccity);
       
   152 	}
       
   153 
       
   154 /** Reads all cities in user defined time zone database into the specified CTzLocalizedCityArray. 
       
   155 Note that this method should be called only if aGroupId is KTzCityNotInGroupId since all of cities are groupless.
       
   156 If the database is empty the returned array will contain no elements.
       
   157 @param aCities Array of cities to add the user defined cities to.
       
   158 @param aGroupId A city group id
       
   159 @internalTechnology
       
   160 */
       
   161 void CTzLocalizationUserDataReader::ReadCitiesInGroupL(CTzLocalizedCityArray& aCities, const TUint8 aGroupId)
       
   162 	{
       
   163 	__ASSERT_ALWAYS(aGroupId == KTzCityNotInGroupId, User::Leave(KErrNotFound));
       
   164 	ReadCitiesL(aCities); 
       
   165 	}
       
   166 
       
   167 /** Reads all groups in user defined time zone database into the specified CTzLocalizedCityGroupArray. 
       
   168 Note that each time zone is formed as an individule CTzLocalizedCityGroup object since they do not belong to the same group.
       
   169 If the database is empty the returned array will contain no elements.
       
   170 @param aCities Array of groups.
       
   171 @internalTechnology
       
   172 */
       
   173 void CTzLocalizationUserDataReader::ReadAllGroupsL(CTzLocalizedCityGroupArray& aCities)
       
   174 	{
       
   175 	RPointerArray<CTzId> tzids;
       
   176 	CleanupStack::PushL(TCleanupItem(ResetAndDestroyTzIdArray, &tzids));
       
   177 	iUserData->GetTzIdsL(tzids);
       
   178 	TInt count = tzids.Count();
       
   179 	aCities.ReserveL(count + aCities.Count());
       
   180 	for(TInt ii=0; ii<count; ++ii)
       
   181 		{
       
   182 		CTzLocalizedCityGroup* group = GetLocalizedCityGroupL(tzids[ii]->TimeZoneNumericID());
       
   183 		aCities.AppendL(group);
       
   184 		}
       
   185 	CleanupStack::PopAndDestroy(&tzids);
       
   186 	}
       
   187 
       
   188 /** Reads the default city with the matching time zone ID.
       
   189 Note that there is only one city in a given user defined time zone.
       
   190 @param aTimeZoneId
       
   191 @internalTechnology
       
   192 */
       
   193 CTzLocalizedCity* CTzLocalizationUserDataReader::ReadDefaultCityL(const TInt aTimeZoneId)
       
   194 	{
       
   195 	return GetLocalizedCityL(aTimeZoneId);
       
   196 	}
       
   197 
       
   198 /*************************************** Private Methods*****************************/
       
   199 
       
   200 /* Get user names for a given time zone identifier
       
   201 @param aTimeZoneId A time zone identifier
       
   202 @param aUserData A reference of CTzUserData
       
   203 @return A pointer to CTzUserNames
       
   204 @internalComponent
       
   205 */
       
   206 CTzUserNames* CTzLocalizationUserDataReader::GetTzUserNamesLC(TInt aTimeZoneId)
       
   207 	{
       
   208 	CTzId* tzid = CTzId::NewL(aTimeZoneId);
       
   209 	CleanupStack::PushL(tzid);
       
   210 	CTzUserNames* names = iUserData->ReadNamesL(*tzid);
       
   211 	CleanupStack::PopAndDestroy(tzid);
       
   212 	CleanupStack::PushL(names);
       
   213 	return names;
       
   214 	}
       
   215 
       
   216 CTzLocalizedTimeZone* CTzLocalizationUserDataReader::GetLocalizedTimeZoneL(TInt aTimeZoneId)
       
   217 	{
       
   218 	CTzUserNames* names = GetTzUserNamesLC(aTimeZoneId);
       
   219 	TTzLocalizedId id(aTimeZoneId, 0);
       
   220 	CTzLocalizedTimeZone* locTimeZone = CTzLocalizedTimeZone::NewL(id, names->StandardName(), names->DaylightSaveName(), names->ShortStandardName(), names->ShortDaylightSaveName());	
       
   221 	CleanupStack::PopAndDestroy(names);
       
   222 	return locTimeZone;
       
   223 	}
       
   224 
       
   225 CTzLocalizedCity* CTzLocalizationUserDataReader::GetLocalizedCityL(TInt aTimeZoneId)
       
   226 	{
       
   227 	CTzLocalizedCity* localisedCity = NULL;
       
   228 	CTzUserNames* names = GetTzUserNamesLC(aTimeZoneId);
       
   229 	TTzLocalizedId id(aTimeZoneId, 0);
       
   230 	localisedCity = CTzLocalizedCity::NewL(names->CityName(), id);	
       
   231 	localisedCity->SetCityIndex(0);		
       
   232 	CleanupStack::PopAndDestroy(names);
       
   233 	return localisedCity;
       
   234 	}
       
   235 
       
   236 CTzLocalizedCityGroup* CTzLocalizationUserDataReader::GetLocalizedCityGroupL(TInt aTimeZoneId)
       
   237 	{
       
   238 	CTzLocalizedCityGroup* localisedGroup = NULL;
       
   239 	CTzUserNames* names = GetTzUserNamesLC(aTimeZoneId);
       
   240 	localisedGroup = CTzLocalizedCityGroup::NewL(names->RegionName(), KTzCityNotInGroupId);	
       
   241 	CleanupStack::PopAndDestroy(names);
       
   242 	return localisedGroup;
       
   243 	}