codhandler/codeng/src/CodUtil.cpp
changeset 0 dd21522fd290
child 36 0ed94ceaa377
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2002 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 the License "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 *      Implementation of class CodUtil.   
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "CodUtil.h"
       
    24 #include "CodLogger.h"
       
    25 #include "CodPanic.h"
       
    26 #include <uri8.h>
       
    27 #include <utf.h>
       
    28 #include <CharConv.h>
       
    29 #include <f32file.h>
       
    30 #include <commdb.h>
       
    31 #include <ApSelect.h>
       
    32 #include <BodyPart.h>
       
    33 #include <AiwGenericParam.h>
       
    34 #include <caf/caf.h>
       
    35 
       
    36 // ================= CONSTANTS =======================
       
    37 
       
    38 /// Conversion buffer size.
       
    39 LOCAL_D const TInt KCodConversionBufferSize = 256;
       
    40 /// Blank IP address.
       
    41 _LIT( KCodBlankIpAdress, "0.0.0.0" );
       
    42 /// Zero width non-breaking space character.
       
    43 LOCAL_D const TUint16 KZeroWidthNbsp = 0xfeff;
       
    44 /// cid: scheme.
       
    45 _LIT8( KCodCidScheme, "cid" );
       
    46 
       
    47 // ================= MEMBER FUNCTIONS =======================
       
    48 
       
    49 // ----------------------------------------------------------
       
    50 // CodUtil::CanOpenL()
       
    51 // ----------------------------------------------------------
       
    52 //
       
    53 EXPORT_C TBool CodUtil::CanOpenL( const TDesC& aFname )
       
    54     {
       
    55     CLOG(( ECodEng, 4, _L("-> CodUtil::CanOpenL <%S>"), &aFname ));
       
    56     __ASSERT_DEBUG( aFname.Length(), CodPanic( ECodInternal ) );
       
    57     TBool canOpen( EFalse );
       
    58     using namespace ContentAccess;
       
    59     CVirtualPath* vPath = CVirtualPath::NewL( aFname, KDefaultContentObject );
       
    60     CleanupStack::PushL( vPath );
       
    61     TInt err;
       
    62     CData* data = NULL;
       
    63     // 'No right' style errors leave, need to TRAP.
       
    64     TRAP( err, data = CData::NewL( *vPath, EPeek, EContentShareReadOnly ) );
       
    65     // Note, 'data' not pushed. No leaving calls below.
       
    66     if ( !err &&
       
    67                  (
       
    68                  !data->EvaluateIntent( EPlay ) ||
       
    69                  !data->EvaluateIntent( EView ) ||
       
    70                  !data->EvaluateIntent( EExecute )
       
    71                  )
       
    72        )
       
    73         {
       
    74         canOpen = ETrue;
       
    75         }
       
    76     delete data;
       
    77     CleanupStack::PopAndDestroy( vPath );
       
    78     CLOG(( ECodEng, 4, _L("<- CodUtil::CanOpenL (%d)"), canOpen ));
       
    79     return canOpen;
       
    80     }
       
    81 
       
    82 // ----------------------------------------------------------
       
    83 // CodUtil::CanOpenL()
       
    84 // ----------------------------------------------------------
       
    85 //
       
    86 EXPORT_C TBool CodUtil::CanOpenL( RFile& aFile )
       
    87     {
       
    88     CLOG(( ECodEng, 4, _L("-> CodUtil::CanOpenL (RFile)") ));
       
    89     TBool canOpen( EFalse );
       
    90     using namespace ContentAccess;
       
    91     TInt err;
       
    92     CData* data = NULL;
       
    93     // 'No right' style errors leave, need to TRAP.
       
    94     TRAP( err, data = CData::NewL( aFile, KDefaultContentObject, EPeek ) );
       
    95     // Note, 'data' not pushed. No leaving calls below.
       
    96     if ( !err &&
       
    97                  (
       
    98                  !data->EvaluateIntent( EPlay ) ||
       
    99                  !data->EvaluateIntent( EView ) ||
       
   100                  !data->EvaluateIntent( EExecute )
       
   101                  )
       
   102        )
       
   103         {
       
   104         canOpen = ETrue;
       
   105         }
       
   106     delete data;
       
   107     CLOG(( ECodEng, 4, _L("<- CodUtil::CanOpenL (%d)"), canOpen ));
       
   108     return canOpen;
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------
       
   112 // CodUtil::GetIntParam()
       
   113 // ---------------------------------------------------------
       
   114 //
       
   115 EXPORT_C void CodUtil::GetIntParam
       
   116         (
       
   117         TInt& aParam,
       
   118         TGenericParamId aSemanticId,
       
   119         const CAiwGenericParamList& aParams
       
   120         )
       
   121     {
       
   122     TInt index = 0;
       
   123     const TAiwGenericParam* param =
       
   124         aParams.FindFirst( index, aSemanticId, EVariantTypeTInt32 );
       
   125     if ( param )
       
   126         {
       
   127         aParam = param->Value().AsTInt32();
       
   128         CLOG(( ECodEng, 4, _L("CodUtil::GetIntParam id(%d) value(%d)"), \
       
   129             aSemanticId, aParam ));
       
   130         }
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------
       
   134 // CodUtil::GetUint32Param()
       
   135 // ---------------------------------------------------------
       
   136 //
       
   137 EXPORT_C void CodUtil::GetUint32Param
       
   138         (
       
   139         TUint32& aParam,
       
   140         TGenericParamId aSemanticId,
       
   141         const CAiwGenericParamList& aParams
       
   142         )
       
   143     {
       
   144     TInt index = 0;
       
   145     const TAiwGenericParam* param =
       
   146         aParams.FindFirst( index, aSemanticId, EVariantTypeTInt32 );
       
   147     if ( param )
       
   148         {
       
   149         aParam = STATIC_CAST( TUint32, param->Value().AsTInt32() );
       
   150         CLOG(( ECodEng, 4, _L("CodUtil::GetUint32Param id(%d) value(%d)"), \
       
   151             aSemanticId, aParam ));
       
   152         }
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------
       
   156 // CodUtil::GetBoolParam()
       
   157 // ---------------------------------------------------------
       
   158 //
       
   159 EXPORT_C void CodUtil::GetBoolParam
       
   160         (
       
   161         TBool& aParam,
       
   162         TGenericParamId aSemanticId,
       
   163         const CAiwGenericParamList& aParams
       
   164         )
       
   165     {
       
   166     TInt index = 0;
       
   167     const TAiwGenericParam* param =
       
   168         aParams.FindFirst( index, aSemanticId, EVariantTypeTInt32 );
       
   169     if ( param )
       
   170         {
       
   171         aParam = STATIC_CAST( TBool, param->Value().AsTInt32() );
       
   172         CLOG(( ECodEng, 4, _L("CodUtil::GetBoolParam id(%d) value(%d)"), \
       
   173             aSemanticId, aParam ));
       
   174         }
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------
       
   178 // CodUtil::GetDesParamLC()
       
   179 // ---------------------------------------------------------
       
   180 //
       
   181 EXPORT_C void CodUtil::GetDesParamLC
       
   182         (
       
   183         TPtrC8& aParam,
       
   184         TGenericParamId aSemanticId,
       
   185         const CAiwGenericParamList& aParams
       
   186         )
       
   187     {
       
   188     TInt index = 0;
       
   189     HBufC8* buf = NULL;
       
   190     // First check 8 bit variant.
       
   191     const TAiwGenericParam* param =
       
   192         aParams.FindFirst( index, aSemanticId, EVariantTypeDesC8 );
       
   193     if ( param )
       
   194         {
       
   195         aParam.Set( param->Value().AsData() );
       
   196         CLOG(( ECodEng, 4, \
       
   197             _L8("CodUtil::GetDesParamLC 8-bit id(%d) value<%S>)"), \
       
   198             aSemanticId, &aParam ));
       
   199         }
       
   200     else
       
   201         {
       
   202         // No 8-bit, check 16-bit.
       
   203         param = aParams.FindFirst( index, aSemanticId, EVariantTypeDesC );
       
   204         if ( param )
       
   205             {
       
   206             buf = ConvertL( param->Value().AsDes() );   // No leave until push.
       
   207             aParam.Set( *buf );
       
   208             CLOG(( ECodEng, 4, \
       
   209                 _L8("CodUtil::GetDesParamLC 16-bit id(%d) value<%S>)"), \
       
   210                 aSemanticId, &aParam ));
       
   211             }
       
   212         }
       
   213     CleanupStack::PushL( buf ); // May be NULL.
       
   214     }
       
   215 
       
   216 // ---------------------------------------------------------
       
   217 // CodUtil::GetDesParam()
       
   218 // ---------------------------------------------------------
       
   219 //
       
   220 EXPORT_C void CodUtil::GetDesParam
       
   221         (
       
   222         TPtrC& aParam,
       
   223         TGenericParamId aSemanticId,
       
   224         const CAiwGenericParamList& aParams
       
   225         )
       
   226     {
       
   227     TInt index = 0;
       
   228     const TAiwGenericParam* param =
       
   229         aParams.FindFirst( index, aSemanticId, EVariantTypeDesC );
       
   230     if ( param )
       
   231         {
       
   232         aParam.Set( param->Value().AsDes() );
       
   233         CLOG(( ECodEng, 4, \
       
   234             _L8("CodUtil::GetDesParam 16-bit id(%d) value<%S>)"), \
       
   235             aSemanticId, &aParam ));
       
   236         }
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------
       
   240 // CodUtil::IsCidSchemeL()
       
   241 // ---------------------------------------------------------
       
   242 //
       
   243 TBool CodUtil::IsCidSchemeL( const TDesC8& aUrl )
       
   244     {
       
   245     TUriParser8 uri;
       
   246     User::LeaveIfError( uri.Parse( aUrl ) );
       
   247     return uri.Extract( EUriScheme ).Compare( KCodCidScheme ) == 0;
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------
       
   251 // CodUtil::PartByCidL()
       
   252 // ---------------------------------------------------------
       
   253 //
       
   254 CBodyPart* CodUtil::PartByCidL
       
   255 ( const RPointerArray<CBodyPart>& aParts, const TDesC8& aUrl )
       
   256     {
       
   257     CBodyPart* part = NULL;
       
   258     TUriParser8 uri;
       
   259     User::LeaveIfError( uri.Parse( aUrl ) );
       
   260     __ASSERT_DEBUG( uri.Extract( EUriScheme ).Compare( KCodCidScheme ) == 0,
       
   261         CodPanic( ECodInternal ) ); // Expecting CID scheme!
       
   262     CUri8* cUri = CUri8::NewLC( uri );
       
   263     cUri->RemoveComponentL( EUriScheme );
       
   264     TPtrC8 cid( cUri->Uri().UriDes() );
       
   265     for ( TInt i = 0; i < aParts.Count(); i++ )
       
   266         {
       
   267         if ( !aParts[i]->ContentID().Compare( cid ) )
       
   268             {
       
   269             part = aParts[i];
       
   270             break;
       
   271             }
       
   272         }
       
   273     CleanupStack::PopAndDestroy( cUri );
       
   274     return part;
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------
       
   278 // CodUtil::FileNameFromUrl()
       
   279 // ---------------------------------------------------------
       
   280 //
       
   281 TPtrC8 CodUtil::FileNameFromUrl( const TDesC8& aUrl )
       
   282     {
       
   283     TPtrC8 ptr;
       
   284     TUriParser8 uri; 
       
   285     if ( uri.Parse( aUrl ) == KErrNone )
       
   286         {
       
   287         ptr.Set( uri.Extract( EUriPath ) );
       
   288         TInt slash = ptr.LocateReverse( TChar('/') );
       
   289         if( slash >= 0 )
       
   290             {
       
   291             // We have a slash - keep only what is after it.
       
   292             // Position 'slash + 1' can always be passed to Mid(); length is
       
   293             // one greater than any resulting index. (If last character is
       
   294             // a slash, we get an empty string as expected.)
       
   295             ptr.Set( ptr.Mid( slash + 1 ) );
       
   296             }
       
   297         }
       
   298     return ptr;
       
   299     }
       
   300 
       
   301 // ---------------------------------------------------------
       
   302 // CodUtil::AbsoluteUrlLC()
       
   303 // ---------------------------------------------------------
       
   304 //
       
   305 EXPORT_C HBufC8* CodUtil::AbsoluteUrlLC
       
   306 ( const TDesC8& aBase, const TDesC8& aUrl )
       
   307     {
       
   308     HBufC8* absUrl;
       
   309     TUriParser8 url;
       
   310     User::LeaveIfError( url.Parse( aUrl ) );
       
   311     if ( url.IsPresent( EUriScheme ) )
       
   312         {
       
   313         // aUrl is already absolute, use it "as-is".
       
   314         absUrl = aUrl.AllocLC();
       
   315         }
       
   316     else
       
   317         {
       
   318         // aUrl is relative, resolve it against the aBase.
       
   319         TUriParser8 base;
       
   320         User::LeaveIfError( base.Parse( aBase ) );
       
   321         CUri8* resolved = CUri8::ResolveL( base, url );
       
   322         CleanupStack::PushL( resolved );
       
   323         absUrl = resolved->Uri().UriDes().AllocL();     // Not pushed here...
       
   324         CleanupStack::PopAndDestroy( resolved );        // (no leaving here)
       
   325         CleanupStack::PushL( absUrl );                  // ...only here.
       
   326         }
       
   327     return absUrl;
       
   328     }
       
   329 
       
   330 // ---------------------------------------------------------
       
   331 // CodUtil::ConvertToUcs2L()
       
   332 // ---------------------------------------------------------
       
   333 //
       
   334 HBufC16* CodUtil::ConvertToUcs2L
       
   335 ( const TDesC8& aSrc, const TDesC8& aEncoding, RFs& aFs )
       
   336     {
       
   337     CLOG(( ECodEng | ECharset, 2, _L("-> CodUtil::ConvertToUcs2L") ));
       
   338     CCnvCharacterSetConverter* conv = CCnvCharacterSetConverter::NewLC();
       
   339     CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* charsets =
       
   340         CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC( aFs );
       
   341     TUint id = 0;
       
   342     if ( aEncoding.Length() )
       
   343         {
       
   344         // Encoding is specified, use it.
       
   345         CLOG(( ECodEng | ECharset, 4, _L8("charset name given <%S>"), \
       
   346             &aEncoding ));
       
   347         id = conv->ConvertStandardNameOfCharacterSetToIdentifierL
       
   348             ( aEncoding, aFs );
       
   349         CLOG(( ECodEng | ECharset, 4, _L("charset id 0x%x"), id ));
       
   350         }
       
   351     else 
       
   352         {
       
   353         // Autodetect.
       
   354         TInt confidence = 0;
       
   355         conv->AutoDetectCharacterSetL( confidence, id, *charsets, aSrc );
       
   356         CLOG(( ECodEng | ECharset, 4, \
       
   357             _L("detected charset id 0x%x confidence 0x%x"), id, confidence ));
       
   358         if ( confidence == 0 || id == 0 )
       
   359             {
       
   360             // Failed to detect.
       
   361             User::Leave( KErrNotSupported );
       
   362             }
       
   363         }
       
   364 
       
   365     HBufC16* ucs2buffer = NULL;
       
   366     TBool resultOnStack = EFalse;
       
   367 
       
   368     // If id is 0 then we don't know what encoding the file is or it is
       
   369     // already UCS2.
       
   370     if ( id == 0 )
       
   371         {
       
   372         // If the source is too small we don't even have a unicode header or
       
   373         // have a header with no data so just leave.
       
   374         if ( aSrc.Length() < 3 )
       
   375             {
       
   376             User::Leave( KErrCorrupt );
       
   377             }
       
   378         
       
   379         const TUint8* srcData = aSrc.Ptr();
       
   380         if ( srcData[0] == 0xFF && srcData[1] == 0xFE )
       
   381             {
       
   382             // Little endian, good, just drop the unicode header and return
       
   383             // the rest.
       
   384             TPtrC result( (TUint16*)(srcData + 2) );
       
   385             ucs2buffer = result.AllocL();
       
   386             }
       
   387         else if ( srcData[0] == 0xFE && srcData[1] == 0xFF )
       
   388             {
       
   389             // Big endian, we don't support it.
       
   390             User::Leave( KErrNotSupported );
       
   391             }
       
   392         else
       
   393             {
       
   394             // No Unicode header, assume little endian-ness.
       
   395             TPtrC result( (TUint16*)srcData );
       
   396             ucs2buffer = result.AllocL();
       
   397             }
       
   398         }
       
   399     else 
       
   400         {
       
   401         HBufC16* buffer = HBufC::NewLC( KCodConversionBufferSize );
       
   402         TPtr16 ptr( buffer->Des() );
       
   403         // Prepare conversion for the given charset ID.
       
   404         conv->PrepareToConvertToOrFromL( id, *charsets, aFs );
       
   405 
       
   406         TInt state = 0;
       
   407         TInt remaining = conv->ConvertToUnicode( ptr, aSrc, state );
       
   408         while ( remaining >= 0 )
       
   409             {
       
   410             if ( ucs2buffer == NULL )
       
   411                 {
       
   412                 ucs2buffer = HBufC::NewLC( ptr.Length() );
       
   413                 resultOnStack = ETrue;
       
   414                 }
       
   415             else
       
   416                 {
       
   417                 __ASSERT_DEBUG( resultOnStack, CodPanic( ECodInternal ));
       
   418                 // This may change the address of ucs2buffer so we need to put
       
   419                 // it on the cleanup stack again!!
       
   420                 ucs2buffer = ucs2buffer->ReAllocL
       
   421                     ( ucs2buffer->Length() + ptr.Length() );
       
   422                 CleanupStack::Pop();    // old ucs2buffer
       
   423                 CleanupStack::PushL( ucs2buffer );  // possibly new copy
       
   424                 }
       
   425             TPtr16 ucs2ptr( ucs2buffer->Des() );
       
   426             ucs2ptr.Append( ptr );
       
   427             if ( remaining > 0 )
       
   428                 {
       
   429                 // Try to convert all remaining characters.
       
   430                 ptr.Zero();
       
   431                 remaining = conv->ConvertToUnicode
       
   432                     ( ptr, aSrc.Right( remaining ), state );
       
   433                 }
       
   434             else
       
   435                 {
       
   436                 break;
       
   437                 }
       
   438             }
       
   439 
       
   440         if ( resultOnStack )
       
   441             {
       
   442             CleanupStack::Pop();    // ucs2buffer
       
   443             resultOnStack = EFalse;
       
   444             }
       
   445 
       
   446         CleanupStack::PopAndDestroy( buffer );
       
   447         }
       
   448 
       
   449     if ( resultOnStack )
       
   450         {
       
   451         CleanupStack::Pop();        // ucs2buffer
       
   452         }
       
   453 
       
   454     CleanupStack::PopAndDestroy( 2 );   // charsets, conv
       
   455 
       
   456     // Check if first character is a Zero-width nbsp.
       
   457     TPtrC16 ptr( ucs2buffer->Des() );
       
   458     if ( ptr.Length() >= 1 && ptr[0] == KZeroWidthNbsp )
       
   459         {
       
   460         // First character is a Zero-width NBSP. This character is used as
       
   461         // BOM in some encodings and should not be present at this point.
       
   462         // But we are tolerant and remove it.
       
   463         // (Not expecting big-endianness here.)
       
   464         CLOG(( ECodEng | ECharset, 4, _L("dropping BOM"), id ));
       
   465         HBufC16* temp = ucs2buffer;
       
   466         CleanupStack::PushL( temp );
       
   467         ucs2buffer = ptr.Mid( 1 ).AllocL();
       
   468         CleanupStack::PopAndDestroy();  // temp
       
   469         }
       
   470 
       
   471     CLOG(( ECodEng | ECharset, 2, _L("<- CodUtil::ConvertToUcs2L") ));
       
   472     return ucs2buffer;
       
   473     }
       
   474 
       
   475 // ---------------------------------------------------------
       
   476 // CodUtil::ConvertUcs2ToUtf8L()
       
   477 // ---------------------------------------------------------
       
   478 //
       
   479 HBufC8* CodUtil::ConvertUcs2ToUtf8L( const TDesC& aUcs2Src )
       
   480     {
       
   481     HBufC8* utf8buffer = NULL;
       
   482     TBool resultOnStack = EFalse;
       
   483     HBufC8* buffer = HBufC8::NewLC( KCodConversionBufferSize );
       
   484     TPtr8 ptr( buffer->Des() );
       
   485     TInt remaining =
       
   486         CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr, aUcs2Src );
       
   487     while ( remaining >= 0 )
       
   488         {
       
   489         if ( utf8buffer == NULL )
       
   490             {
       
   491             utf8buffer = HBufC8::NewLC( ptr.Length() );
       
   492             resultOnStack = ETrue;
       
   493             }
       
   494         else
       
   495             {
       
   496             __ASSERT_DEBUG( resultOnStack, CodPanic( ECodInternal ) );
       
   497             // This may change the address of utf8buffer so we need to put it
       
   498             // on the cleanup stack again!!
       
   499             utf8buffer = utf8buffer->ReAllocL
       
   500                 ( utf8buffer->Length() + ptr.Length() );
       
   501             CleanupStack::Pop();    // old utf8buffer
       
   502             CleanupStack::PushL( utf8buffer );  // possibly new copy
       
   503             }
       
   504         TPtr8 utf8ptr( utf8buffer->Des() );
       
   505         utf8ptr.Append( ptr );
       
   506         if ( remaining > 0 )
       
   507             {
       
   508             // Try to convert all remaining characters.
       
   509             ptr.Zero();
       
   510             remaining = CnvUtfConverter::ConvertFromUnicodeToUtf8
       
   511                 ( ptr, aUcs2Src.Right( remaining ) );
       
   512             }
       
   513         else
       
   514             {
       
   515             break;
       
   516             }
       
   517         }
       
   518     
       
   519     if ( resultOnStack )
       
   520         {
       
   521         CleanupStack::Pop();    // utf8buffer
       
   522         }
       
   523     CleanupStack::PopAndDestroy( buffer );
       
   524     return utf8buffer;
       
   525     }
       
   526 
       
   527 // ---------------------------------------------------------
       
   528 // CodUtil::ConvertLC()
       
   529 // ---------------------------------------------------------
       
   530 //
       
   531 EXPORT_C HBufC8* CodUtil::ConvertLC( const TDesC& aDes )
       
   532     {
       
   533     HBufC8* buf = HBufC8::NewLC( aDes.Length() );
       
   534     buf->Des().Copy( aDes );
       
   535     return buf;
       
   536     }
       
   537 
       
   538 // ---------------------------------------------------------
       
   539 // CodUtil::ConvertLC()
       
   540 // ---------------------------------------------------------
       
   541 //
       
   542 EXPORT_C HBufC* CodUtil::ConvertLC( const TDesC8& aDes )
       
   543     {
       
   544     HBufC* buf = HBufC::NewLC( aDes.Length() );
       
   545     buf->Des().Copy( aDes );
       
   546     return buf;
       
   547     }
       
   548 
       
   549 // ---------------------------------------------------------
       
   550 // CodUtil::ConvertL()
       
   551 // ---------------------------------------------------------
       
   552 //
       
   553 EXPORT_C HBufC8* CodUtil::ConvertL( const TDesC& aDes )
       
   554     {
       
   555     HBufC8* buf = HBufC8::NewL( aDes.Length() );
       
   556     buf->Des().Copy( aDes );
       
   557     return buf;
       
   558     }
       
   559 
       
   560 // ---------------------------------------------------------
       
   561 // CodUtil::ConvertL()
       
   562 // ---------------------------------------------------------
       
   563 //
       
   564 EXPORT_C HBufC* CodUtil::ConvertL( const TDesC8& aDes )
       
   565     {
       
   566     HBufC* buf = HBufC::NewL( aDes.Length() );
       
   567     buf->Des().Copy( aDes );
       
   568     return buf;
       
   569     }
       
   570 
       
   571 // ---------------------------------------------------------
       
   572 // CodUtil::ApCountL()
       
   573 // ---------------------------------------------------------
       
   574 //
       
   575 TUint32 CodUtil::ApCountL()
       
   576     {
       
   577     TUint32 count( 0 );
       
   578     CCommsDatabase* cdb = CCommsDatabase::NewL( EDatabaseTypeIAP );
       
   579     CleanupStack::PushL( cdb );
       
   580     CApSelect* apSel = CApSelect::NewLC
       
   581         (
       
   582         *cdb,
       
   583         KEApIspTypeAll,
       
   584         EApBearerTypeAll,
       
   585         KEApSortUidAscending
       
   586         );
       
   587     count = apSel->Count();
       
   588     CleanupStack::PopAndDestroy( 2 );   // apSel, cdb
       
   589     return count;
       
   590     }
       
   591 
       
   592 // ---------------------------------------------------------
       
   593 // CodUtil::WapGatewayL()
       
   594 // ---------------------------------------------------------
       
   595 //
       
   596 HBufC8* CodUtil::WapGatewayL( TUint32 aIap )
       
   597     {
       
   598     HBufC8* gateway8 = NULL;
       
   599 
       
   600     // Open a CommDB database server session.
       
   601     // This has to be local and initialised every time or we get
       
   602     // strange access denied leaves happening.
       
   603     CCommsDatabase* cdb = CCommsDatabase::NewL( EDatabaseTypeIAP );
       
   604     CleanupStack::PushL( cdb );
       
   605 
       
   606     // Now access the commdb to see whether we have an associated
       
   607     // WAP AP and if so determine the gateway that we are using.
       
   608     TPtrC wapApTable( WAP_IP_BEARER );
       
   609     TPtrC wapApIapCol( WAP_IAP );
       
   610     CCommsDbTableView* view =
       
   611         cdb->OpenViewMatchingUintLC( wapApTable, wapApIapCol, aIap );
       
   612     TInt err = view->GotoFirstRecord();
       
   613     if ( !err )
       
   614         {
       
   615         // There is a matching WAP AP table entry, does it have a
       
   616         // valid gateway?
       
   617         TPtrC wapApGwCol( WAP_GATEWAY_ADDRESS );
       
   618         HBufC* gateway16 = view->ReadLongTextLC( wapApGwCol );
       
   619         if ( gateway16->Compare( KCodBlankIpAdress ) )
       
   620             {
       
   621             // There is a valid WAP gateway.
       
   622             gateway8 = ConvertL( *gateway16 );  // Not pushed, no leave below.
       
   623             }
       
   624         CleanupStack::PopAndDestroy( gateway16 );
       
   625         }
       
   626     CleanupStack::PopAndDestroy( 2 );   // view, cdb
       
   627     return gateway8;
       
   628     }