locationcentre/lcserver/src/lcregappstore.cpp
branchRCL_3
changeset 16 4721bd00d3da
parent 14 3a25f69541ff
child 21 e15b7f06eba6
equal deleted inserted replaced
14:3a25f69541ff 16:4721bd00d3da
     1 /*
       
     2 * Copyright (c) 2007 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:  Location Centre Server object.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include <badesca.h>    // CDesCArrayFlat
       
    21 #include <s32file.h>    // CFileStore & CPermanentFileStore
       
    22 #include <bautils.h>    // file helpers
       
    23 
       
    24 // USER INCLUDES
       
    25 #include "lcregappstore.h"
       
    26 
       
    27 // CONSTANT DEFINTION
       
    28 _LIT(KLcPrivateDbName,"lcregappinfo.db"); // Name of the Database file for storing registered app info.
       
    29 _LIT(KAppInfoTable, "AppInfo");            // Name of the one table
       
    30 _LIT(KAppUUIdCol, "UUID");           	   // First column
       
    31 
       
    32 const TInt 	KMaxColumnLength = 32*1024;
       
    33 
       
    34 // ----- Member funtions for CLcRegAppStore ---------------------------------
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // CLcRegAppStore::CLcRegAppStore
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CLcRegAppStore::CLcRegAppStore( RFs&  aFs )
       
    41 	:iFsSession( aFs )
       
    42     {
       
    43     // C++ Default constructor. No allocations or functions which can Leave
       
    44     // should be called from here.Initiallize all the variable here
       
    45     }
       
    46          
       
    47 // ---------------------------------------------------------------------------
       
    48 // CLcRegAppStore::~CLcRegAppStore
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CLcRegAppStore::~CLcRegAppStore()
       
    52     {
       
    53     // C++ Destructor. Free all resources associated with this class.
       
    54     Close();
       
    55     }
       
    56         
       
    57 // ---------------------------------------------------------------------------
       
    58 // CLcRegAppStore* CLcRegAppStore::NewL
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CLcRegAppStore* CLcRegAppStore::NewL( RFs&  aFs )
       
    62     {
       
    63     CLcRegAppStore* self = NewLC( aFs );
       
    64     CleanupStack::Pop( self );
       
    65     return self;         
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // CLcRegAppStore* CLcRegAppStore::NewLC
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CLcRegAppStore* CLcRegAppStore::NewLC( RFs&  aFs )
       
    73     {
       
    74     // Symbian Two phased constructor. Leaves the object on the Clean-up
       
    75     // stack.
       
    76     CLcRegAppStore* self = new ( ELeave )CLcRegAppStore( aFs );
       
    77     CleanupStack::PushL( self );
       
    78     self->ConstructL();
       
    79     return self;         
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // void CLcRegAppStore::ConstructL
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 void CLcRegAppStore::ConstructL()
       
    87     {
       
    88     // Obtain the Db file path
       
    89     User::LeaveIfError( iFsSession.SessionPath( iLcRegdbFile ) );
       
    90 
       
    91     // Create the Database Directory ie the private directory of the process
       
    92     if( !BaflUtils::PathExists( iFsSession, iLcRegdbFile ) )
       
    93 	    {
       
    94     	User::LeaveIfError( iFsSession.MkDirAll( iLcRegdbFile ) );	    	
       
    95 	    }
       
    96     
       
    97     // Generate the Db file
       
    98     iLcRegdbFile.Append( KLcPrivateDbName );
       
    99 
       
   100 	// Check if the db file is exists or not.
       
   101     if( BaflUtils::FileExists( iFsSession, iLcRegdbFile ) )
       
   102         {
       
   103         OpenDbL( iLcRegdbFile );
       
   104         }
       
   105 	else
       
   106 		{
       
   107 		CreateDbL( iLcRegdbFile );	
       
   108 		}        
       
   109     }  
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // void CLcRegAppStore::OpenDbL
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CLcRegAppStore::OpenDbL( const TFileName& aRegAppInfoFile )
       
   116     {  	
       
   117     // if it is already open then close it.
       
   118     Close();
       
   119 	// Check if the db file is exists or not.
       
   120     if( BaflUtils::FileExists( iFsSession, aRegAppInfoFile ) )
       
   121         {
       
   122 		TRAPD(error, 
       
   123 			iFileStore = CPermanentFileStore::OpenL( iFsSession, aRegAppInfoFile, 
       
   124 				EFileRead|EFileWrite );
       
   125 			//Set file store type	
       
   126 			iFileStore->SetTypeL( iFileStore->Layout() );
       
   127 			iRegAppInfoDb.OpenL( iFileStore,iFileStore->Root() )
       
   128 			);
       
   129 
       
   130 	    if( !error )
       
   131 	    	{
       
   132 	    	iDbStatus = ELcDbOpen;
       
   133 	    	}
       
   134 	    else
       
   135 		    {
       
   136 		    User::Leave( error );	
       
   137 		    }
       
   138         }
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // void CLcRegAppStore::CreateDbL()
       
   143 //
       
   144 // Create a new database. The database will be in exclusive access mode.
       
   145 // ---------------------------------------------------------------------------
       
   146 void CLcRegAppStore::CreateDbL( const TFileName& aRegAppInfoFile )
       
   147     {
       
   148     // if it is already open then close it.
       
   149     Close();
       
   150 
       
   151     // Create empty database file.
       
   152     TRAPD(error,
       
   153     	iFileStore = CPermanentFileStore::ReplaceL(iFsSession, aRegAppInfoFile, 
       
   154     		EFileRead|EFileWrite);
       
   155     	iFileStore->SetTypeL( iFileStore->Layout() );// Set file store type
       
   156     	TStreamId id = iRegAppInfoDb.CreateL( iFileStore );// Create stream object
       
   157     	iFileStore->SetRootL( id );// Keep database id as root of store
       
   158     	iFileStore->CommitL();// Complete creation by commiting
       
   159     	// Create appinfo tables
       
   160     	CreateAppInfoTableL();
       
   161     	);
       
   162     
       
   163     if( !error )
       
   164     	{
       
   165     	iDbStatus = ELcDbCreate;
       
   166     	}
       
   167     else
       
   168 	    {
       
   169 	    User::Leave( error );	
       
   170 	    }
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------------------------
       
   174 // void CLcRegAppStore::Close()
       
   175 //
       
   176 // Close the database.
       
   177 // ---------------------------------------------------------------------------
       
   178 void CLcRegAppStore::Close()
       
   179     {
       
   180     iRegAppInfoDb.Close();
       
   181     delete iFileStore;
       
   182     iFileStore = NULL;
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // void CLcRegAppStore::CreateAppInfoTableL()
       
   187 //
       
   188 // Creates Appinfo table. Leaves, if the table cannot be created.
       
   189 // ---------------------------------------------------------------------------
       
   190 void CLcRegAppStore::CreateAppInfoTableL()
       
   191     {
       
   192     // Specify columns for appinfo table
       
   193     TDbCol uuidCol( KAppUUIdCol, EDbColLongText8,KMaxColumnLength );   // Using default length
       
   194 
       
   195     // Add the columns to column set
       
   196     CDbColSet* appInfoColSet = CDbColSet::NewLC();
       
   197     appInfoColSet->AddL( uuidCol );
       
   198 
       
   199     // Create the appinfo table
       
   200     User::LeaveIfError( iRegAppInfoDb.CreateTable(KAppInfoTable,
       
   201         *appInfoColSet) );
       
   202     CleanupStack::PopAndDestroy( appInfoColSet );
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // void CLcRegAppStore::AddDataToTable()
       
   207 //
       
   208 // Add item to database using RDbTable API
       
   209 // ---------------------------------------------------------------------------
       
   210 void CLcRegAppStore::AddDataToTableL( const TDesC8& aAppUid )
       
   211     {
       
   212     // Create an updateable database table object
       
   213     RDbTable table;
       
   214     CleanupClosePushL( table );
       
   215     
       
   216     User::LeaveIfError( table.Open( iRegAppInfoDb, KAppInfoTable, table.EUpdatable ));
       
   217     
       
   218 	CDbColSet* dbColSet = table.ColSetL();
       
   219     CleanupStack::PushL( dbColSet );
       
   220 	    
       
   221     //table.Reset();
       
   222     TBool lRowPos = table.FirstL();
       
   223     
       
   224     TDbColNo colNo = dbColSet->ColNo(KAppUUIdCol);
       
   225 	    
       
   226     // Do first time insert and then do Update
       
   227     if( iDbStatus == ELcDbOpen && lRowPos )
       
   228         {
       
   229     	table.UpdateL();
       
   230        	table.SetColL( colNo, aAppUid ); // col = 1
       
   231         }
       
   232     else
       
   233         {
       
   234     	table.InsertL();
       
   235        	table.SetColL( colNo, aAppUid ); // col = 1
       
   236         iDbStatus = ELcDbOpen;    	    	
       
   237         }	        
       
   238 
       
   239     table.PutL();    // Complete changes (the insertion)
       
   240 
       
   241     CleanupStack::PopAndDestroy( dbColSet );    	
       
   242     CleanupStack::PopAndDestroy();
       
   243     }
       
   244 
       
   245 // ---------------------------------------------------------------------------
       
   246 // void CLcRegAppStore::GetUuidValueL()
       
   247 //
       
   248 // Get packed Uuid from database according to column name 
       
   249 // ---------------------------------------------------------------------------
       
   250 void CLcRegAppStore::GetUuidValueL(TDes8& aUuid)
       
   251     {
       
   252     aUuid.Zero();
       
   253     if(!BaflUtils::FileExists( iFsSession, iLcRegdbFile ) )
       
   254         {
       
   255         return;
       
   256         }
       
   257 
       
   258 	_LIT(KSelectAll,"SELECT * FROM AppInfo" );
       
   259 
       
   260     // Create a view on the database
       
   261     RDbView view;
       
   262     CleanupClosePushL( view );
       
   263     TInt lResult = view.Prepare( iRegAppInfoDb, TDbQuery( KSelectAll ), view.EReadOnly );
       
   264     if( lResult == KErrNone )
       
   265 	    {
       
   266 	    lResult = view.EvaluateAll();
       
   267 
       
   268 	    CDbColSet* colSet = view.ColSetL();
       
   269 	    CleanupStack::PushL( colSet );
       
   270 
       
   271 	    // Append each result row
       
   272 	    if ( view.FirstL() )
       
   273 	        {
       
   274 	        view.GetL();
       
   275 	    	TDbColNo colNo = colSet->ColNo( KAppUUIdCol );	        
       
   276 	        RDbColReadStream readStream;       // A stream object for long columns
       
   277 	        readStream.OpenLC( view, colNo );
       
   278 	        readStream.ReadL( aUuid, view.ColLength( colNo ) );
       
   279 	        CleanupStack::Pop(); //readStream
       
   280 	        readStream.Close();
       
   281 	        }
       
   282 	    CleanupStack::PopAndDestroy( colSet );
       
   283 	    }
       
   284     CleanupStack::PopAndDestroy( &view );
       
   285     }
       
   286 // End of file
       
   287