landmarks/locationlandmarks/internalservices/src/EPos_PosLmDatabaseUtility.cpp
changeset 0 667063e416a2
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2002-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: Utility function for landmark database.
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <f32file.h>
       
    21 #include "EPos_CPosLmLocalDbAccess.h"
       
    22 #include "EPos_LandmarkDatabaseStructure.h"
       
    23 #include "EPos_PosLmDatabaseUtility.h"
       
    24 
       
    25 const TUint KPosSingleQuoteCharacter = '\'';
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 EXPORT_C HBufC* PosLmDatabaseUtility::CreateDatabaseUriL(
       
    33     const TDesC& aDbUri)
       
    34     {
       
    35     TPtrC dbUri = aDbUri;
       
    36 
       
    37     if (ProtocolPresent(aDbUri))
       
    38         {
       
    39         // Remove protocol if present first in the URI.
       
    40         dbUri.Set(UriWithoutProtocol(aDbUri));
       
    41         }
       
    42 
       
    43     TFileName* completePath = new (ELeave) TFileName;
       
    44     CleanupStack::PushL(completePath);
       
    45 
       
    46     TChar sysDriveChar = RFs::GetSystemDriveChar();
       
    47     TBuf<2> defaultDrive;
       
    48     defaultDrive.Append( sysDriveChar );
       
    49     defaultDrive.Append( KDriveDelimiter );
       
    50     User::LeaveIfError(
       
    51         VerifyUriIsValid( dbUri, &defaultDrive, completePath ) );
       
    52 
       
    53     // Create correct URI
       
    54     HBufC* dbUriToReturn = HBufC::NewL(
       
    55         KPosLocalDbFileProtocol().Length() + completePath->Length());
       
    56     dbUriToReturn->Des().Append(KPosLocalDbFileProtocol);
       
    57     dbUriToReturn->Des().Append(*completePath);
       
    58 
       
    59     CleanupStack::PopAndDestroy(completePath);
       
    60 
       
    61     return dbUriToReturn;
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 EXPORT_C TInt PosLmDatabaseUtility::ValidateDatabaseUri(
       
    68     const TDesC& aDbUri)
       
    69     {
       
    70     if (!ProtocolPresent(aDbUri))
       
    71         {
       
    72         return KErrArgument;
       
    73         }
       
    74 
       
    75     return VerifyUriIsValid(UriWithoutProtocol(aDbUri), NULL);
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 EXPORT_C void PosLmDatabaseUtility::RemoveProtocolFromUriL(
       
    82     TPtrC& aDbUri)
       
    83     {
       
    84     // Verify the file name is not too long to be placed into a TFileName
       
    85     __ASSERT_ALWAYS(aDbUri.Length() <= KMaxFileName +
       
    86         KPosLocalDbFileProtocol().Length(), User::Leave(KErrArgument));
       
    87 
       
    88     // Verify there is a protocol at the beginning
       
    89     __ASSERT_ALWAYS(ProtocolPresent(aDbUri), User::Leave(KErrArgument));
       
    90 
       
    91     // Assume the protocol is specified.
       
    92     aDbUri.Set(UriWithoutProtocol(aDbUri));
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 TInt PosLmDatabaseUtility::VerifyUriIsValid(
       
    99     const TDesC& aUriWoProtocol,
       
   100     const TDesC* aDefaultDrive,
       
   101     TFileName* aPath)
       
   102     {
       
   103     TParse parse;
       
   104 
       
   105     TInt err = parse.Set(aUriWoProtocol, NULL, aDefaultDrive);
       
   106 
       
   107     if (err == KErrBadName)
       
   108         {
       
   109         // File name too long (longer than KMaxFileName)
       
   110         err = KErrArgument;
       
   111         }
       
   112     else if (err == KErrNone)
       
   113         {
       
   114         if ((!aDefaultDrive && !parse.DrivePresent()) ||
       
   115             parse.PathPresent() || !parse.NamePresent() ||
       
   116             parse.Ext().CompareF(KPosDbFileExtension) != 0)
       
   117             {
       
   118             err = KErrArgument;
       
   119             }
       
   120         else if (aPath)
       
   121             {
       
   122             aPath->Copy(parse.FullName());
       
   123             }
       
   124         }
       
   125 
       
   126     return err;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 TPtrC PosLmDatabaseUtility::UriWithoutProtocol( const TDesC& aDbUri )
       
   133     {
       
   134     return aDbUri.Right(aDbUri.Length() - KPosLocalDbFileProtocol().Length());
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 TBool PosLmDatabaseUtility::ProtocolPresent( const TDesC& aDbUri )
       
   141     {
       
   142     return aDbUri.Left(KPosLocalDbFileProtocol().Length()).CompareF(
       
   143         KPosLocalDbFileProtocol) == 0;
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 EXPORT_C HBufC* PosLmDatabaseUtility::EmbedSingleQuoteCharactersLC(
       
   150     const TDesC& aSearchText)
       
   151     {
       
   152     // Count the number of single quote characters.
       
   153     TInt noOfCharMatches = 0;
       
   154     TPtrC originalText(aSearchText);
       
   155     TInt pos = originalText.Locate(KPosSingleQuoteCharacter);
       
   156     while (pos != KErrNotFound)
       
   157         {
       
   158         noOfCharMatches++;
       
   159 
       
   160         originalText.Set(originalText.Right(originalText.Length() - pos - 1));
       
   161         pos = originalText.Locate(KPosSingleQuoteCharacter);
       
   162         }
       
   163 
       
   164     HBufC* embeddedText = HBufC::NewLC(aSearchText.Length() + noOfCharMatches);
       
   165 
       
   166     // Embed all single quote characters.
       
   167     originalText.Set(aSearchText);
       
   168     pos = originalText.Locate(KPosSingleQuoteCharacter);
       
   169     while (pos != KErrNotFound)
       
   170         {
       
   171         embeddedText->Des().Append(originalText.Left(pos + 1));
       
   172         embeddedText->Des().Append(KPosSingleQuoteCharacter);
       
   173 
       
   174         originalText.Set(originalText.Right(originalText.Length() - pos - 1));
       
   175         pos = originalText.Locate(KPosSingleQuoteCharacter);
       
   176         }
       
   177 
       
   178     embeddedText->Des().Append(originalText);
       
   179 
       
   180     return embeddedText;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 EXPORT_C TUint PosLmDatabaseUtility::GetTableRowCountL( 
       
   187     CPosLmLocalDbAccess& aDbAccess,
       
   188     const TDesC& aTableName )
       
   189     {
       
   190     RDbNamedDatabase db;
       
   191     aDbAccess.GetDatabase( db );
       
   192     
       
   193     RDbTable table;
       
   194     User::LeaveIfError( table.Open( db, aTableName, RDbRowSet::EReadOnly ) );
       
   195     CleanupClosePushL( table );
       
   196     TInt numRows = table.CountL();
       
   197     CleanupStack::PopAndDestroy( &table );
       
   198 
       
   199     if ( numRows < 0 )
       
   200         {
       
   201         User::Leave( KErrNotFound );
       
   202         }
       
   203     
       
   204     return numRows;
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 EXPORT_C TBool PosLmDatabaseUtility::IndexExistsL(
       
   211     const RDbNamedDatabase& aDatabase,
       
   212     const TDesC& aTable,
       
   213     const TDesC& aIndex )
       
   214     {
       
   215     TBool found( EFalse );
       
   216     CDbIndexNames* indexes = aDatabase.IndexNamesL( aTable );
       
   217     for ( TInt i = 0; i < indexes->Count(); ++i ) 
       
   218         {
       
   219         if ( (*indexes)[i].Compare( aIndex ) == 0 )
       
   220             {
       
   221             found = ETrue;
       
   222             break;
       
   223             }
       
   224         }
       
   225     delete indexes;
       
   226     return found;
       
   227     }