phonebookengines/cntmaptileservice/src/cntmaptiledblookuptable.cpp
changeset 61 d30183af6ca6
parent 56 d508aa856878
child 66 554fe4dbbb59
equal deleted inserted replaced
56:d508aa856878 61:d30183af6ca6
     1 /*
       
     2 * Copyright (c) 2010 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: 
       
    15 *     Retrieving maptile path from lookup db implementation
       
    16 *
       
    17 */
       
    18 
       
    19 #include <bautils.h>
       
    20 
       
    21 #include "cntmaptiledblookuptable.h"
       
    22 
       
    23 // select all from
       
    24 _LIT( KSelectAllFrom, "SELECT * FROM " );
       
    25 
       
    26 // string 'where'
       
    27 _LIT( KStringWhere, " WHERE " );
       
    28 
       
    29 // string ' = '
       
    30 _LIT( KStringEqual, " = " );
       
    31 
       
    32 // string 'And'
       
    33 _LIT( KStringAnd, " AND " );
       
    34 
       
    35 
       
    36 _LIT(KQueryByMaptileState,"SELECT * FROM cntmaptilelookuptable WHERE cntuid = %d AND ( fetchingstatus = %d OR fetchingstatus = %d )");
       
    37 // -----------------------------------------------------------------------------
       
    38 // CLookupMapTileDatabase::CLookupMapTileDatabase()
       
    39 // Default constructor.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CLookupMapTileDatabase::CLookupMapTileDatabase()
       
    43 {
       
    44 }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CLookupMapTileDatabase::~CLookupMapTileDatabase()
       
    48 // Destructor.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CLookupMapTileDatabase::~CLookupMapTileDatabase()
       
    52 {
       
    53     // close the database
       
    54 	iItemsDatabase.Close();
       
    55 	
       
    56 	// close the file session
       
    57 	iFsSession.Close();
       
    58 }
       
    59  
       
    60 // -----------------------------------------------------------------------------
       
    61 // CLookupMapTileDatabase::~CLookupMapTileDatabase()
       
    62 // Creates an object of this class and pushes to cleanup stack.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CLookupMapTileDatabase* CLookupMapTileDatabase::NewLC( const TDesC& aLookupTableName )
       
    66 {
       
    67     CLookupMapTileDatabase* self = new (ELeave) CLookupMapTileDatabase;
       
    68     CleanupStack::PushL(self);
       
    69     self->ConstructL( aLookupTableName );
       
    70     return self;
       
    71 }
       
    72 
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CLookupMapTileDatabase::NewL()
       
    76 // Creates an object of this class.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CLookupMapTileDatabase* CLookupMapTileDatabase::NewL( const TDesC& aLookupTableName )
       
    80 {
       
    81     CLookupMapTileDatabase* self = CLookupMapTileDatabase::NewLC( aLookupTableName );
       
    82     CleanupStack::Pop( self );
       
    83     return self;
       
    84 }
       
    85  
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CLookupMapTileDatabase::ConstructL()
       
    89 // 2nd phase contructor.
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void CLookupMapTileDatabase::ConstructL( const TDesC& aLookupTableName )
       
    93 {
       
    94     User::LeaveIfError( iFsSession.Connect() );
       
    95     
       
    96     iDbFileName.Copy( KLookupDbPath );
       
    97     iDbFileName.Append( aLookupTableName );
       
    98     
       
    99     iDatabaseExists = EFalse; 
       
   100 
       
   101     if( BaflUtils::FileExists( iFsSession, iDbFileName ) )
       
   102     {	
       
   103         // database exists 
       
   104         iDatabaseExists = ETrue; 
       
   105     }
       
   106 }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CLookupMapTileDatabase::FindNumberOfAddressL()
       
   110 // Finds the number of address associated with an contact.
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 int CLookupMapTileDatabase::FindNumberOfAddressL( int& aId )
       
   114 {
       
   115     int count = 0;
       
   116     
       
   117     // Create a query to find the item.
       
   118     TFileName queryBuffer;
       
   119     queryBuffer.Format( KQueryByMaptileState,aId,
       
   120              CntMapTileService::MapTileFetchingInProgress,
       
   121              CntMapTileService::MapTileFetchingNetworkError );
       
   122     
       
   123     TInt ret = iItemsDatabase.Open( iFsSession, iDbFileName );
       
   124     
       
   125     if( ret != KErrNone )
       
   126     {          
       
   127         //if already opened , close and open again
       
   128         iItemsDatabase.Close();          
       
   129         User::LeaveIfError( iItemsDatabase.Open( iFsSession, iDbFileName ) );
       
   130     }
       
   131     
       
   132     User::LeaveIfError( iItemsDatabase.Begin() );       
       
   133     // Create a view of the table with the above query.
       
   134     RDbView myView;
       
   135     myView.Prepare( iItemsDatabase, TDbQuery( queryBuffer ) );
       
   136     CleanupClosePushL( myView );
       
   137     myView.EvaluateAll();
       
   138     myView.FirstL();
       
   139     
       
   140     
       
   141     while (myView.AtRow())
       
   142     {
       
   143         count++;
       
   144         myView.NextL();
       
   145     }
       
   146     
       
   147     CleanupStack::PopAndDestroy( &myView ); // myView
       
   148          
       
   149     //Close the database
       
   150     iItemsDatabase.Close();
       
   151 
       
   152     return count;
       
   153 }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CLookupMapTileDatabase::FindEntryL()
       
   157 // Finds an entry in the lookup table.
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CLookupMapTileDatabase::FindEntryL( TLookupItem& aLookupItem )
       
   161 { 
       
   162  
       
   163     // used to check whether entry available or not
       
   164     TBool entryAvailable = EFalse;
       
   165   
       
   166     if ( iDatabaseExists )
       
   167     {
       
   168         // Create a query to find the item.
       
   169         TFileName queryBuffer;
       
   170         queryBuffer.Copy( KSelectAllFrom );
       
   171         queryBuffer.Append( KMapTileLookupTable );
       
   172         queryBuffer.Append( KStringWhere );
       
   173         queryBuffer.Append( NCntColUid );
       
   174         queryBuffer.Append( KStringEqual );
       
   175         queryBuffer.AppendNum( aLookupItem.iUid );
       
   176         queryBuffer.Append( KStringAnd );
       
   177         queryBuffer.Append( NColSource );
       
   178         queryBuffer.Append( KStringEqual );
       
   179         queryBuffer.AppendNum( aLookupItem.iSource );
       
   180         
       
   181         TInt ret = iItemsDatabase.Open( iFsSession, iDbFileName );
       
   182         
       
   183         if( ret != KErrNone )
       
   184         {
       
   185             //if already opened , close and open again
       
   186             iItemsDatabase.Close();          
       
   187             User::LeaveIfError( iItemsDatabase.Open( iFsSession, iDbFileName ) );
       
   188         }
       
   189         User::LeaveIfError( iItemsDatabase.Begin() );
       
   190         
       
   191         // Create a view of the table with the above query.
       
   192         RDbView myView;
       
   193         myView.Prepare( iItemsDatabase, TDbQuery( queryBuffer ) );
       
   194         CleanupClosePushL( myView );
       
   195         myView.EvaluateAll();
       
   196         myView.FirstL();
       
   197     
       
   198         if( myView.AtRow() ) 
       
   199         {   
       
   200             // Item found. get the details.
       
   201             myView.GetL();      
       
   202             if( aLookupItem.iUid == myView.ColUint( KColumncntUid ) )
       
   203             {
       
   204                 aLookupItem.iFilePath.Copy( myView.ColDes16( KColumnFilePath ) );
       
   205                 aLookupItem.iFetchingStatus = myView.ColUint( KColumnMapTileFetchingStatus );
       
   206                 entryAvailable = ETrue;
       
   207             }      
       
   208         } 
       
   209     
       
   210         CleanupStack::PopAndDestroy( &myView ); // myView
       
   211         
       
   212         //Close the database
       
   213         iItemsDatabase.Close();
       
   214     }
       
   215     
       
   216     //No entry found 
       
   217     if( !entryAvailable )
       
   218     {
       
   219         User::Leave( KErrNotFound );
       
   220     }
       
   221 }
       
   222 
       
   223 
       
   224 // End of file
       
   225