omaprovisioning/provisioning/ProvisioningSC/Src/CWPImsiDbHandler.cpp
changeset 0 b497e44ab2fc
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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:  Manages IMSI (DBMS) database 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "CWPImsiDbHandler.h"
       
    22 #include    <sysutil.h>
       
    23 #include    "ProvisioningDebug.h"
       
    24 
       
    25 // CONSTANTS
       
    26 
       
    27 _LIT( KSimCardDb,   "wpsimcard.db" );
       
    28 _LIT( KDbColImsi,   "imsi" ); 
       
    29 _LIT( KDbSimTable,  "sim" );
       
    30 _LIT( KDbImsiIndex, "I" );
       
    31 _LIT( KDbNameDrive, "C:" );
       
    32 const TInt KEmptyDbSizeEstimate     = 2048; // bytes to write
       
    33 const TInt KImsiSize                = 16;
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CWPImsiDbHandler::CWPImsiDbHandler
       
    39 // C++ default constructor can NOT contain any code, that
       
    40 // might leave.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CWPImsiDbHandler::CWPImsiDbHandler()
       
    44     {
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CWPImsiDbHandler::ConstructL
       
    49 // Symbian 2nd phase constructor can leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 void CWPImsiDbHandler::ConstructL()
       
    53     {
       
    54     FLOG( _L( "[ProvisioningSC] CWPImsiDbHandler::ConstructL" ) );
       
    55 
       
    56 	User::LeaveIfError( iFs.Connect() );
       
    57     // Get process privatepath
       
    58     TInt err = iFs.CreatePrivatePath( EDriveC );
       
    59     if( err != KErrAlreadyExists )
       
    60     	{
       
    61     	User::LeaveIfError( err );
       
    62     	}
       
    63     User::LeaveIfError( iFs.PrivatePath( iDataBasePath ) );
       
    64     iDataBasePath.Insert(0, KDbNameDrive );
       
    65     iDataBasePath.Append( KSimCardDb );
       
    66  
       
    67 	// open imsi database
       
    68     TInt result( iDb.Open( iFs, iDataBasePath ) );
       
    69     FTRACE(RDebug::Print(_L("[ProvisioningSC] CWPImsiDbHandler db open 1 result (%d)"), result));
       
    70 	if( result == KErrNotFound )
       
    71         {
       
    72         CreateDatabaseL();
       
    73         result = iDb.Open( iFs, iDataBasePath );
       
    74         FTRACE(RDebug::Print(_L("[ProvisioningSC] CWPImsiDbHandler db open 2 result (%d)"), result));
       
    75         }
       
    76     User::LeaveIfError( result );
       
    77     if( iDb.IsDamaged() )
       
    78         {
       
    79         User::LeaveIfError( iDb.Recover() );
       
    80         }
       
    81 	User::LeaveIfError( iImsiTable.Open( iDb, KDbSimTable,
       
    82 		RDbRowSet::TAccess( RDbRowSet::EUpdatable ) ) );
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CWPImsiDbHandler::NewL
       
    87 // Two-phased constructor.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 CWPImsiDbHandler* CWPImsiDbHandler::NewL()
       
    91     {
       
    92 	CWPImsiDbHandler* handler  = CWPImsiDbHandler::NewLC();
       
    93     CleanupStack::Pop();
       
    94     return handler;
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CWPImsiDbHandler::NewLC
       
    99 // Two-phased constructor.
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 CWPImsiDbHandler* CWPImsiDbHandler::NewLC()
       
   103     {
       
   104     CWPImsiDbHandler* self = new( ELeave ) CWPImsiDbHandler;    
       
   105     CleanupStack::PushL( self );
       
   106     self->ConstructL();
       
   107     return self;
       
   108     }
       
   109     
       
   110 // -----------------------------------------------------------------------------
       
   111 // CWPImsiDbHandler::~CWPImsiDbHandler
       
   112 // Destructor
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 CWPImsiDbHandler::~CWPImsiDbHandler()
       
   116     {
       
   117 	iImsiTable.Close();
       
   118 	iDb.Close();
       
   119     iFs.Close();	
       
   120     }
       
   121 // -----------------------------------------------------------------------------
       
   122 // CWPImsiDbHandler::ImsiExistsL
       
   123 // Check if imsi exists in the database
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 TBool CWPImsiDbHandler::ImsiExistsL( const TDesC& aPhoneImsi )
       
   127 	{
       
   128     User::LeaveIfError( iImsiTable.SetIndex( KDbImsiIndex ) );
       
   129 	TBool result( ETrue );
       
   130     if( !iImsiTable.SeekL( TDbSeekKey( aPhoneImsi ) ) )
       
   131         {
       
   132         result = EFalse;
       
   133         }
       
   134 	return result;
       
   135 	}
       
   136 // -----------------------------------------------------------------------------
       
   137 // CWPImsiDbHandler::StoreImsiL
       
   138 // Store new imsi into database
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 void CWPImsiDbHandler::StoreImsiL( const TDesC& aPhoneImsi )
       
   142 	{
       
   143 	if( aPhoneImsi == KNullDesC )
       
   144 		{
       
   145 		User::Leave( KErrBadDescriptor );
       
   146 		}
       
   147 	if( SysUtil::FFSSpaceBelowCriticalLevelL( NULL, KImsiSize ) )
       
   148 		{
       
   149 		User::Leave( KErrDiskFull );
       
   150 		}
       
   151 	CDbColSet* colSet = iImsiTable.ColSetL();
       
   152     TInt imsisCol( colSet->ColNo( KDbColImsi ) );
       
   153 	delete colSet;
       
   154 	colSet = NULL;
       
   155 	// create a view on the database
       
   156 	RDbView view;
       
   157 	CleanupClosePushL( view ); // 1
       
   158 	_LIT( KSQLStatement,"select imsi from sim order by imsi" );
       
   159 	User::LeaveIfError( view.Prepare( iDb,TDbQuery( KSQLStatement,
       
   160 						                            EDbCompareNormal )));
       
   161 	User::LeaveIfError( view.EvaluateAll() );
       
   162 	
       
   163 	// begin transaction
       
   164 	User::LeaveIfError( iDb.Begin() ) ;
       
   165 	CleanupStack::PushL( TCleanupItem( CleanupRollback, &iDb ) ); // 2
       
   166 	// insert a row
       
   167 	view.InsertL();	
       
   168 	CleanupStack::PushL ( TCleanupItem( CleanupCancel, &view ) ); // 3
       
   169 	view.SetColL( imsisCol, aPhoneImsi );
       
   170 	view.PutL();
       
   171 	CleanupStack::Pop(); // cleanupcancel, 3
       
   172 	User::LeaveIfError( iDb.Commit() );
       
   173 	CleanupStack::Pop(); // cleanuprollback, 2
       
   174 	CleanupStack::PopAndDestroy(); // view, 1
       
   175 	return;
       
   176 	}	
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CWPImsiDbHandler::CreateDatabaseL
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void CWPImsiDbHandler::CreateDatabaseL()
       
   183 	{
       
   184 	if( SysUtil::FFSSpaceBelowCriticalLevelL( &iFs,	KEmptyDbSizeEstimate ) )
       
   185         {
       
   186         User::Leave( KErrDiskFull );
       
   187         }
       
   188 
       
   189     // Get process privatepath
       
   190     iDataBasePath.Zero();
       
   191     User::LeaveIfError( iFs.PrivatePath( iDataBasePath ) );
       
   192     iDataBasePath.Insert(0, KDbNameDrive );
       
   193     iDataBasePath.Append( KSimCardDb );
       
   194     FLOG( _L( "[ProvisioningSC] CWPImsiDbHandler::CreateDatabaseL priv path" ) );
       
   195 
       
   196     FLOG( _L( "[ProvisioningSC] CWPImsiDbHandler::CreateDatabaseL" ) );
       
   197     User::LeaveIfError( iDb.Create( iFs, iDataBasePath ) );
       
   198     FLOG( _L( "[ProvisioningSC] CWPImsiDbHandler::CreateDatabaseL ok" ) );
       
   199 	CreateTableL();
       
   200 	CreateIndexL();
       
   201 	iDb.Close();
       
   202     FLOG( _L( "[ProvisioningSC] CWPImsiDbHandler::CreateDatabaseL done" ) );
       
   203 	}
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CWPImsiDbHandler::CreateTableL
       
   207 // Create database table
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CWPImsiDbHandler::CreateTableL()
       
   211 	{
       
   212     CDbColSet* colset = CDbColSet::NewLC(); // 1
       
   213     TDbCol idCol( KDbColImsi, EDbColText16 );
       
   214     idCol.iAttributes = TDbCol::ENotNull;
       
   215 	colset->AddL( idCol );
       
   216 	User::LeaveIfError( iDb.CreateTable( KDbSimTable, *colset ) );  
       
   217 	CleanupStack::PopAndDestroy(); // colset
       
   218     } 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CWPImsiDbHandler::CreateIndexL
       
   221 // Create database index
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CWPImsiDbHandler::CreateIndexL()
       
   225 	{
       
   226 	CDbKey* key = CDbKey::NewLC(); // 1
       
   227 	TDbKeyCol imsi( KDbColImsi );
       
   228 	key->AddL( imsi );
       
   229 	User::LeaveIfError(  iDb.CreateIndex( KDbImsiIndex, 
       
   230 							KDbSimTable, *key ) ) ;
       
   231     CleanupStack::PopAndDestroy( ); // key
       
   232 	}
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CWPImsiDbHandler::CleanupRollback
       
   236 // Create database index
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 void CWPImsiDbHandler::CleanupRollback( TAny* aItem )
       
   240 	{
       
   241 	RDbDatabase* db = static_cast<RDbDatabase*>(aItem);
       
   242 	db->Rollback();
       
   243 	}
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // CWPImsiDbHandler::CleanupCancel
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 void CWPImsiDbHandler::CleanupCancel( TAny* aItem )
       
   250     {
       
   251     RDbTable* table = static_cast<RDbTable*>(aItem);
       
   252     table->Cancel();
       
   253     }
       
   254 
       
   255 //  End of File