tzservices/tzloc/src/TzLocalizationDbAccessor.cpp
changeset 0 2e3d3ce01487
child 33 332e7bf3b42f
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2004-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 "TzLocalizationDbAccessor.h"
       
    17 #include "TzLocalizationDataTypes.h"
       
    18 
       
    19 //==================================================================
       
    20 // Database location and access policy
       
    21 //==================================================================
       
    22 _LIT(KTzLocalizationDbName,"c:TzLocalization.db");
       
    23 _LIT(KTzLocalizationDbSecurityPolicy,"secure[10206A8B]");
       
    24 //==================================================================
       
    25 
       
    26 //==================================================================
       
    27 //Table and Column names for Cached Time Zone Table
       
    28 //These text strings are never visible and do not need localizing
       
    29 //==================================================================
       
    30 _LIT(KCZTableName, 			"FrequentlyUsedZones");
       
    31 _LIT(KCZTableTzIdCol, 		"TzId");
       
    32 _LIT(KCZTableStdCol, 		"StandardName");
       
    33 _LIT(KCZTableDSTCol, 		"DSTName");
       
    34 _LIT(KCZTableShortStdCol,	"ShortStandardName");
       
    35 _LIT(KCZTableShortDSTCol, 	"ShortDSTName");
       
    36 _LIT(KCZTableCityCol, 		"City");
       
    37 _LIT(KCZTableCityGroupCol, 	"GroupId");
       
    38 _LIT(KCZTableResourceIdCol,	"ResourceId");
       
    39 _LIT(KCZTableCityIndexCol,	"CityIndex");
       
    40 
       
    41 //==================================================================
       
    42 //Table and Column names for User Added Cities Table
       
    43 //These text strings are never visible and do not need localizing
       
    44 //==================================================================
       
    45 _LIT(KUCTableName, 			"UserCities");
       
    46 _LIT(KUCTableTzId, 			"TzId");
       
    47 _LIT(KUCTableCity, 			"City");
       
    48 _LIT(KUCTableGroup, 		"GroupId");
       
    49 _LIT(KUCTableResourceId,	"ResourceId");
       
    50 
       
    51 /**
       
    52 Allocates and constructs a new CTzLocalizationDbAccessor
       
    53 @return the newly constructed CTzLocalizationDbAccessor
       
    54 @internalTechnology
       
    55 */
       
    56 CTzLocalizationDbAccessor* CTzLocalizationDbAccessor::NewL()
       
    57 	{
       
    58 	CTzLocalizationDbAccessor* self = CTzLocalizationDbAccessor::NewLC();
       
    59 	CleanupStack::Pop(self);
       
    60 	return self;
       
    61 	}
       
    62 
       
    63 /**
       
    64 Allocates and constructs a new CTzLocalizationDbAccessor
       
    65 The pointer to the new object is left on the cleanup stack
       
    66 @return the newly constructed CTzLocalizationDbAccessor
       
    67 @internalTechnology
       
    68 */
       
    69 CTzLocalizationDbAccessor* CTzLocalizationDbAccessor::NewLC()
       
    70 	{
       
    71 	CTzLocalizationDbAccessor* self = new (ELeave) CTzLocalizationDbAccessor();
       
    72 	CleanupStack::PushL(self);
       
    73 	self->ConstructL();
       
    74 	return self;
       
    75 	}
       
    76 
       
    77 CTzLocalizationDbAccessor::CTzLocalizationDbAccessor()
       
    78 	{
       
    79 	}
       
    80 
       
    81 /**
       
    82 Second phase construction
       
    83 Connects to a database session and opens the database.  If the database doesn't exist it is created
       
    84 and populated with blank time zone data
       
    85 @internalTechnology
       
    86 */
       
    87 void CTzLocalizationDbAccessor::ConstructL()
       
    88 	{
       
    89     OpenDbL();
       
    90     }
       
    91 
       
    92 void CTzLocalizationDbAccessor::OpenDbL()
       
    93 	{
       
    94 	if (iZoneMutex.OpenGlobal(KTzMutexName) != KErrNone)
       
    95 		{
       
    96 		User::LeaveIfError(iZoneMutex.CreateGlobal(KTzMutexName)) ;
       
    97 		}        
       
    98 	//Connect to database server session
       
    99 	User::LeaveIfError(iDbsSession.Connect());
       
   100 	//Attempt to open the database
       
   101 	TInt error = iLocalizedTimeZoneDb.Open(iDbsSession,KTzLocalizationDbName,KTzLocalizationDbSecurityPolicy);
       
   102 	User::LeaveIfError(error);
       
   103 	}
       
   104 
       
   105 /**
       
   106 Destructor
       
   107 Closes the database tables, the database and the database session
       
   108 @internalTechnology
       
   109 */
       
   110 CTzLocalizationDbAccessor::~CTzLocalizationDbAccessor()
       
   111 	{
       
   112     CloseDb();
       
   113     iDbsSession.Close();
       
   114     }
       
   115 
       
   116 void CTzLocalizationDbAccessor::CloseDb()
       
   117     {
       
   118 	iCityView.Close();
       
   119 	iZoneView.Close();
       
   120 	iLocalizedTimeZoneDb.Close();
       
   121 	if (iZoneMutex.IsHeld())    // it is a leave, so release mutex
       
   122 		{
       
   123 		iZoneMutex.Signal();
       
   124 		}       
       
   125 	iZoneMutex.Close();
       
   126 	}
       
   127 
       
   128 // Close and delete the DB and create a new one in the latest format
       
   129 void CTzLocalizationDbAccessor::RecreateDbL(RTz& aTzSession)
       
   130     {
       
   131     CloseDb();
       
   132     aTzSession.LocalizationCloseDbL();
       
   133     const TUid KTzLocalizationDbSecurityPolicyUid = {0x10206A8B};
       
   134     User::LeaveIfError(iDbsSession.DeleteDatabase(KTzLocalizationDbName(),KTzLocalizationDbSecurityPolicyUid));
       
   135     aTzSession.LocalizationOpenDbL();
       
   136     OpenDbL();
       
   137     }
       
   138 
       
   139 TBool CTzLocalizationDbAccessor::DbNeedsUpdatingL()
       
   140     {
       
   141     // Check the version of the database
       
   142     // There are 9 columns as of Oct 2006
       
   143     PrepareZoneViewL();
       
   144     if (iZoneView.ColCount() == ETzZoneCityIndex)
       
   145         {
       
   146         return EFalse;
       
   147         }
       
   148     return ETrue;
       
   149      }
       
   150 /**
       
   151 Releases the mutex if leave occurs between wait and signal.
       
   152 @internalTechnology
       
   153 */
       
   154 void CTzLocalizationDbAccessor::ReleaseMutex(TAny *target)
       
   155 {
       
   156 	RMutex *mutex = static_cast<RMutex*> (target) ;	
       
   157 	mutex->Signal() ;	
       
   158 }
       
   159 
       
   160 /**
       
   161 Creates the Cached Zone Table if it doesn't already exist.
       
   162 @return KErrNone if succesful, or one of the DBMS Leave codes
       
   163 @internalTechnology
       
   164 */
       
   165 TInt CTzLocalizationDbAccessor::CreateFrequentlyUsedZoneTableL()
       
   166 	{
       
   167 	// Create the columns for the cached zones table
       
   168 	RArray<TDbCol> cachedTableCols;
       
   169 	CleanupClosePushL(cachedTableCols);
       
   170 	cachedTableCols.AppendL(TDbCol(KCZTableTzIdCol,			EDbColUint16));
       
   171 	cachedTableCols.AppendL(TDbCol(KCZTableStdCol, 			EDbColText));
       
   172 	cachedTableCols.AppendL(TDbCol(KCZTableDSTCol, 			EDbColText));
       
   173 	cachedTableCols.AppendL(TDbCol(KCZTableShortStdCol, 	EDbColText));
       
   174 	cachedTableCols.AppendL(TDbCol(KCZTableShortDSTCol, 	EDbColText));
       
   175 	cachedTableCols.AppendL(TDbCol(KCZTableCityCol, 		EDbColText));
       
   176 	cachedTableCols.AppendL(TDbCol(KCZTableCityGroupCol, 	EDbColUint8));
       
   177 	cachedTableCols.AppendL(TDbCol(KCZTableResourceIdCol, 	EDbColUint32));
       
   178     cachedTableCols.AppendL(TDbCol(KCZTableCityIndexCol,	EDbColInt32));
       
   179 
       
   180 	// Create the columnset - add the columns
       
   181 	// Columns MUST be added in the same order they appear in TTzZoneColumn
       
   182 	CDbColSet* FrequentlyUsedZoneColSet = CDbColSet::NewLC();
       
   183 	TInt numCols = cachedTableCols.Count();
       
   184 	for(TInt i = 0; i < numCols; ++i)
       
   185 		{
       
   186 		FrequentlyUsedZoneColSet->AddL(cachedTableCols[i]);
       
   187 		}
       
   188 
       
   189 	// Create the Cached Time Zone table
       
   190 	TInt error = iLocalizedTimeZoneDb.CreateTable(KCZTableName,*FrequentlyUsedZoneColSet);
       
   191 	User::LeaveIfError(error);
       
   192 
       
   193 	//Open the newly created table
       
   194 	PrepareZoneViewL();
       
   195 	iZoneView.Reset();
       
   196 	//Populate with initial (blank) data.
       
   197 
       
   198 	_LIT(KEmptyString," ");
       
   199 
       
   200 	for (TInt x = 0; x < CTzLocalizedTimeZone::ECachedTimeZones; ++x)
       
   201 		{
       
   202 		iZoneMutex.Wait() ;
       
   203 		CleanupStack::PushL(TCleanupItem(ReleaseMutex,&iZoneMutex));
       
   204 		// Insert empty row
       
   205 		iZoneView.InsertL();
       
   206 		// Fill the table with blank data
       
   207 		iZoneView.SetColL(ETzZoneId,			0);
       
   208 		iZoneView.SetColL(ETzZoneStdName,		KEmptyString);
       
   209 		iZoneView.SetColL(ETzZoneDSTName,		KEmptyString);
       
   210 		iZoneView.SetColL(ETzZoneShortStdName,	KEmptyString);
       
   211 		iZoneView.SetColL(ETzZoneShortDSTName,	KEmptyString);
       
   212 		iZoneView.SetColL(ETzZoneCity,			KEmptyString);
       
   213 		iZoneView.SetColL(ETzZoneGroupId,		0);
       
   214 		iZoneView.SetColL(ETzZoneResourceId,	0);
       
   215         iZoneView.SetColL(ETzZoneCityIndex,		0);
       
   216 		iZoneView.PutL(); // Complete insertion
       
   217 		CleanupStack::Pop() ;
       
   218 		iZoneMutex.Signal() ;
       
   219 		}
       
   220 	
       
   221 	iZoneView.Close();
       
   222 
       
   223 	CleanupStack::PopAndDestroy(FrequentlyUsedZoneColSet);
       
   224 	CleanupStack::PopAndDestroy(&cachedTableCols); //cachedTableCols
       
   225 	return error;
       
   226 	}
       
   227 
       
   228 /**
       
   229 Creates the user added city database table if it doesn't exist
       
   230 @return KErrNone if succesful, or one of the DBMS Leave codes
       
   231 @internalTechnology
       
   232 */
       
   233 TInt CTzLocalizationDbAccessor::CreateUserCityTableL()
       
   234 	{
       
   235 	//Create the columns for the user aded cities table
       
   236 	RArray<TDbCol> cityTableCols;
       
   237 	CleanupClosePushL(cityTableCols);
       
   238 	cityTableCols.AppendL(TDbCol(KUCTableTzId,			EDbColUint16));
       
   239 	cityTableCols.AppendL(TDbCol(KUCTableCity,			EDbColText));
       
   240 	cityTableCols.AppendL(TDbCol(KUCTableGroup,			EDbColUint8));
       
   241 	cityTableCols.AppendL(TDbCol(KUCTableResourceId,	EDbColUint32));
       
   242 
       
   243 	// Create the columnset - add the columns
       
   244 	// Columns MUST be added in the same order they appear in TTzCityColumn
       
   245 	CDbColSet* userCityColSet = CDbColSet::NewLC();
       
   246 	TInt numCols = cityTableCols.Count();
       
   247 	for(TInt i = 0; i < numCols; ++i)
       
   248 		{
       
   249 		userCityColSet->AddL(cityTableCols[i]);
       
   250 		}
       
   251 
       
   252 	// Create the User City table
       
   253 	TInt error = iLocalizedTimeZoneDb.CreateTable(KUCTableName,*userCityColSet);
       
   254 	
       
   255 	CleanupStack::PopAndDestroy(userCityColSet);
       
   256 	CleanupStack::PopAndDestroy(); //cityTableCols
       
   257 
       
   258 	return error;
       
   259 	}
       
   260 
       
   261 
       
   262 /**
       
   263 Opens the a view on the city table and returns a reference to it.
       
   264 The view should be released after use with a call to CloseCityView()
       
   265 @param aSql A sql string to prepare the view with
       
   266 @return reference to opened iCityTable
       
   267 @internalTechnology
       
   268 */
       
   269 void CTzLocalizationDbAccessor::PrepareCityViewL(const TDesC& aSqlQuery)
       
   270 	{
       
   271 	User::LeaveIfError(
       
   272 		iCityView.Prepare(iLocalizedTimeZoneDb,
       
   273 		TDbQuery(aSqlQuery),
       
   274 		iCityView.EUpdatable)
       
   275 		);
       
   276 	User::LeaveIfError(iCityView.EvaluateAll());
       
   277 	}
       
   278 
       
   279 
       
   280 /**
       
   281 Opens the zone table and returns a reference
       
   282 @return reference to the opened iZoneTable
       
   283 @internalTechnology
       
   284 */
       
   285 void CTzLocalizationDbAccessor::PrepareZoneViewL()
       
   286 	{
       
   287 	_LIT(KReadZoneView,"SELECT * FROM FrequentlyUsedZones");
       
   288 	User::LeaveIfError(
       
   289 		iZoneView.Prepare(iLocalizedTimeZoneDb,
       
   290 		TDbQuery(KReadZoneView),
       
   291 		iZoneView.EUpdatable)
       
   292 		);
       
   293 
       
   294 	User::LeaveIfError(iZoneView.EvaluateAll());
       
   295 	}