pushmtm/MtmUtilSrc/WhiteListImporterWL.cpp
changeset 51 48e827313edd
parent 37 481242ead638
child 53 f427d27b98d8
equal deleted inserted replaced
37:481242ead638 51:48e827313edd
     1 /*
       
     2 * Copyright (c) 2003, 2004 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:  Implementation of CWhiteListConverter.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "WhiteListImporter.h"
       
    22 #include "PushMtmSettings.h"
       
    23 #include "PushInitiatorList.h"
       
    24 #include "PushInitiator.h"
       
    25 #include "PushMtmLog.h"
       
    26 #include <e32std.h>
       
    27 
       
    28 // CONSTANTS
       
    29 
       
    30 /// Max length allowed.
       
    31 LOCAL_C const TInt KMaxLength = 2048;
       
    32 
       
    33 /*
       
    34 * Special characters.
       
    35 */
       
    36 /// ',' character.
       
    37 LOCAL_C const TUint KComma = ',';
       
    38 /// ';' character.
       
    39 LOCAL_C const TUint KSemicolon = ';';
       
    40 /// Escape '\' character.
       
    41 LOCAL_C const TUint KEscape = '\\';
       
    42 
       
    43 /*
       
    44 * Characters considered as EOS.
       
    45 */
       
    46 /// '\r' character.
       
    47 LOCAL_C const TUint KCr = '\r';
       
    48 /// '\n' character.
       
    49 LOCAL_C const TUint KLf = '\n';
       
    50 
       
    51 /*
       
    52 * Separator characters.
       
    53 */
       
    54 /// End-Of-String (0) character. It is a separator.
       
    55 LOCAL_C const TUint KEos = 0;
       
    56 /// Record separator (30) character.
       
    57 LOCAL_C const TUint KRecordSeparator = 30;
       
    58 /// Unit separator (31) character.
       
    59 LOCAL_C const TUint KUnitSeparator = 31;
       
    60 
       
    61 /*
       
    62 * Keywords.
       
    63 */
       
    64 /// "Ipv4" keyword.
       
    65 _LIT( KIpv4, "Ipv4" );
       
    66 /// "Ipv6" keyword.
       
    67 _LIT( KIpv6, "Ipv6" );
       
    68 /// "E164" keyword.
       
    69 _LIT( KE164, "E164" );
       
    70 /// "Alpha" keyword.
       
    71 _LIT( KAlpha, "Alpha" );
       
    72 
       
    73 
       
    74 // ================= MEMBER FUNCTIONS =======================
       
    75 
       
    76 // ---------------------------------------------------------
       
    77 // CWhiteListConverter::NewL
       
    78 // ---------------------------------------------------------
       
    79 //
       
    80 CWhiteListConverter* CWhiteListConverter::NewL( CPushInitiatorList& aPushInitiatorList )
       
    81     {
       
    82     CWhiteListConverter* converter = 
       
    83         new (ELeave) CWhiteListConverter( aPushInitiatorList );
       
    84     CleanupStack::PushL( converter );
       
    85     converter->ConstructL();
       
    86     CleanupStack::Pop( converter );    // converter
       
    87     return converter;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------
       
    91 // CWhiteListConverter::~CWhiteListConverter
       
    92 // ---------------------------------------------------------
       
    93 //
       
    94 CWhiteListConverter::~CWhiteListConverter()
       
    95     {
       
    96     delete iConverterBuf;
       
    97     delete iBuf;
       
    98     iNextCh = NULL; // Only used.
       
    99     iMaxCh = NULL; // Only used.
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------
       
   103 // CWhiteListConverter::Buffer2ListL
       
   104 // ---------------------------------------------------------
       
   105 //
       
   106 void CWhiteListConverter::Buffer2ListL( const TDesC& aSource )
       
   107     {
       
   108     PUSHLOG_ENTERFN("CWhiteListConverter::Buffer2ListL");
       
   109 
       
   110     iSource.Assign( aSource );
       
   111     iCurCh = KRecordSeparator; // Cannot be 0, because of GetChar()
       
   112     GetChar();
       
   113     while( NextLineL() )
       
   114         {
       
   115         };
       
   116 
       
   117     PUSHLOG_LEAVEFN("CWhiteListConverter::Buffer2ListL");
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 // CWhiteListConverter::List2BufferL
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 HBufC* CWhiteListConverter::List2BufferL()
       
   125     {
       
   126     PUSHLOG_ENTERFN("CWhiteListConverter::List2BufferL");
       
   127 
       
   128     HBufC* newConverterBuf = KNullDesC().AllocL(); // initial zero length buffer
       
   129     delete iConverterBuf;
       
   130     iConverterBuf = newConverterBuf;
       
   131     TPtr writableConvBuf = iConverterBuf->Des();
       
   132     writableConvBuf.SetLength(0);
       
   133     TInt itemsToExport = iPushInitiatorList.Count();
       
   134     // temp buffers
       
   135     TBuf<16> type;
       
   136     TBuf<16> entryId;
       
   137 
       
   138     for ( TInt i=0; i < itemsToExport; ++i )
       
   139         {
       
   140         CPushInitiator& curr = iPushInitiatorList.At(i);
       
   141         switch ( curr.Type() )
       
   142             {
       
   143             case CPushInitiator::ETypeIpv4: type=KIpv4; break;
       
   144             case CPushInitiator::ETypeIpv6: type=KIpv6; break;
       
   145             case CPushInitiator::ETypeE164: type=KE164; break;
       
   146             case CPushInitiator::ETypeAlpha: type=KAlpha; break;
       
   147             default: type=KE164; break;
       
   148             }
       
   149         entryId.Format( _L("%d"), curr.EntryID() );
       
   150         
       
   151         // check available space
       
   152         // 3 is required for 2 commas and 1 semicolon.
       
   153         TInt newItemLength = curr.Addr().Length()+type.Length()+entryId.Length()+3;
       
   154         if ( writableConvBuf.MaxLength() < writableConvBuf.Length() + newItemLength )
       
   155             {
       
   156             // reallocate a greater buffer
       
   157             TInt currentMaxLength = writableConvBuf.MaxLength();
       
   158             iConverterBuf = iConverterBuf->ReAllocL( currentMaxLength + newItemLength );
       
   159             writableConvBuf.Set( iConverterBuf->Des() ); // re-initialize
       
   160             }
       
   161             
       
   162         writableConvBuf.Append( curr.Addr() );
       
   163         writableConvBuf.Append( TChar(KComma) );
       
   164         writableConvBuf.Append( type );
       
   165         writableConvBuf.Append( TChar(KComma) );
       
   166         writableConvBuf.Append( entryId );
       
   167         if ( i+1 < itemsToExport )
       
   168             {
       
   169             // there are still items to add
       
   170             writableConvBuf.Append( TChar(KSemicolon) );
       
   171             }
       
   172         }
       
   173 
       
   174     HBufC* ret = iConverterBuf; // ownersip is transferred to the caller.
       
   175     iConverterBuf = 0;
       
   176 
       
   177     PUSHLOG_LEAVEFN("CWhiteListConverter::List2BufferL");
       
   178     return ret;
       
   179     }
       
   180 
       
   181 // ---------------------------------------------------------
       
   182 // CWhiteListConverter::CWhiteListConverter
       
   183 // ---------------------------------------------------------
       
   184 //
       
   185 CWhiteListConverter::CWhiteListConverter( CPushInitiatorList& aPushInitiatorList ) 
       
   186 :   iPushInitiatorList( aPushInitiatorList )
       
   187     {
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------
       
   191 // CWhiteListConverter::ConstructL
       
   192 // ---------------------------------------------------------
       
   193 //
       
   194 void CWhiteListConverter::ConstructL()
       
   195     {
       
   196     iBuf = new (ELeave) TText[KMaxLength];
       
   197     iMaxCh = iBuf + KMaxLength;
       
   198     }
       
   199 
       
   200 // ---------------------------------------------------------
       
   201 // CWhiteListConverter::GetChar
       
   202 // ---------------------------------------------------------
       
   203 //
       
   204 inline void CWhiteListConverter::GetChar()
       
   205     {
       
   206     // Do not continue if we already reached EOS:
       
   207     if ( iCurCh == KEos )
       
   208         {
       
   209         return;
       
   210         }
       
   211 
       
   212     iCurCh = iSource.Get();
       
   213 
       
   214     if ( iCurCh == KCr || iCurCh == KLf )
       
   215         {
       
   216         // They are considered as EOS.
       
   217         iCurCh = KEos;
       
   218         }
       
   219     else if ( iCurCh == KEscape )
       
   220         {
       
   221         // Drop the Escape character and get the next as is.
       
   222         iCurCh = iSource.Get();
       
   223         }
       
   224     else if ( iCurCh == KComma )
       
   225         {
       
   226         // It is a Unit Separator.
       
   227         iCurCh = KUnitSeparator;
       
   228         }
       
   229     else if ( iCurCh == KSemicolon )
       
   230         {
       
   231         // It is a Record Separator.
       
   232         iCurCh = KRecordSeparator;
       
   233         }
       
   234     }
       
   235 
       
   236 // ---------------------------------------------------------
       
   237 // CWhiteListConverter::NextLineL
       
   238 // ---------------------------------------------------------
       
   239 //
       
   240 TBool CWhiteListConverter::NextLineL()
       
   241     {
       
   242     switch( iCurCh )
       
   243         {
       
   244         case KEos:
       
   245             // EOS
       
   246             return EFalse;
       
   247 
       
   248         default:
       
   249             // Parse White List attributes and process them.
       
   250             AttrsL();
       
   251             break;
       
   252         }
       
   253 
       
   254     return ETrue;
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------
       
   258 // CWhiteListConverter::NextTokenL
       
   259 // ---------------------------------------------------------
       
   260 //
       
   261 TPtrC CWhiteListConverter::NextTokenL( TUint aStopChar )
       
   262     {
       
   263     iNextCh = iBuf; // Start storing token at start of buffer.
       
   264     while ( iNextCh < iMaxCh )
       
   265         {
       
   266         if ( iCurCh == aStopChar || iCurCh == KEos )
       
   267             {
       
   268             // Stop character found - return what we have stored so far. 
       
   269             // This may be an empty string as well.
       
   270             return TPtrC( iBuf, iNextCh - iBuf );
       
   271             }
       
   272         else
       
   273             {
       
   274             *iNextCh = STATIC_CAST( TText16, iCurCh );
       
   275             iNextCh++;
       
   276             GetChar();
       
   277             }
       
   278         }
       
   279     // No more space in buffer to store token.
       
   280     User::Leave( KErrOverflow );
       
   281     /*NOTREACHED*/
       
   282     return TPtrC();
       
   283     }
       
   284 
       
   285 // ---------------------------------------------------------
       
   286 // CWhiteListConverter::AttrsL
       
   287 // ---------------------------------------------------------
       
   288 //
       
   289 void CWhiteListConverter::AttrsL()
       
   290     {
       
   291     CPushInitiator* newInitiator = new (ELeave) CPushInitiator;
       
   292     CleanupStack::PushL( newInitiator );
       
   293 
       
   294     TPtrC token;
       
   295 
       
   296     // Address.
       
   297     token.Set( NextTokenL( KUnitSeparator ) );
       
   298     HBufC* address = token.AllocLC(); // Copy the token to a separate area.
       
   299     GetChar();
       
   300 
       
   301     // Address type and/or entry ID.
       
   302     token.Set( NextTokenL( KRecordSeparator ) );
       
   303     HBufC* addressTypeBuf = 0;
       
   304     HBufC* entryIdBuf = 0;
       
   305     // check if it contains an entry ID separated with a comma (KUnitSeparator)
       
   306     TInt offset = token.Locate( TChar(KUnitSeparator) );
       
   307     if ( offset == KErrNotFound )
       
   308         {
       
   309         // the whole token is address type
       
   310         addressTypeBuf = token.AllocLC(); // Copy the token to a separate area.
       
   311         entryIdBuf = 0;
       
   312         }
       
   313     else
       
   314         {
       
   315         addressTypeBuf = token.Mid(0,offset).AllocLC();
       
   316         TInt tokenLength = token.Length();
       
   317         entryIdBuf = token.Mid((offset+1),tokenLength-(offset+1)).AllocLC();
       
   318         }
       
   319     
       
   320     // Convert the address type string to enum.
       
   321     // addressTypeBuf must not be NULL.
       
   322     CPushInitiator::TAddrType addrType = CPushInitiator::ETypeE164;
       
   323     if      ( !(*addressTypeBuf).Compare( KIpv4 ) )
       
   324         {
       
   325         addrType = CPushInitiator::ETypeIpv4;
       
   326         }
       
   327     else if ( !(*addressTypeBuf).Compare( KIpv6 ) )
       
   328         {
       
   329         addrType = CPushInitiator::ETypeIpv6;
       
   330         }
       
   331     else if ( !(*addressTypeBuf).Compare( KE164 ) )
       
   332         {
       
   333         addrType = CPushInitiator::ETypeE164;
       
   334         }
       
   335     else if ( !(*addressTypeBuf).Compare( KAlpha ) )
       
   336         {
       
   337         addrType = CPushInitiator::ETypeAlpha;
       
   338         }
       
   339     else
       
   340         {
       
   341         User::Leave( KErrCorrupt );
       
   342         }
       
   343     PUSHLOG_WRITE_FORMAT2("WL Importer: <%S> <%S>",&(address->Des()),
       
   344                                                    &(addressTypeBuf->Des()));
       
   345         
       
   346     TUint32 entryId(0);
       
   347     if ( entryIdBuf )
       
   348         {
       
   349         PUSHLOG_WRITE_FORMAT("WL Importer entryIdBuf: <%S>",&(entryIdBuf->Des()));
       
   350         TLex lex( *entryIdBuf );
       
   351         lex.Val( entryId, EDecimal );
       
   352         CleanupStack::PopAndDestroy( entryIdBuf ); // entryIdBuf
       
   353         }
       
   354     
       
   355     GetChar();
       
   356 
       
   357     // No more attribute to parse.
       
   358 
       
   359     // Add the new initiator data to the Push settings.
       
   360     newInitiator->SetAddressL( *address, addrType );
       
   361     newInitiator->SetEntryID( entryId );
       
   362     CleanupStack::PopAndDestroy( 2, address ); // addressTypeBuf, address
       
   363 
       
   364     iPushInitiatorList.AddL( newInitiator );
       
   365     CleanupStack::Pop( newInitiator ); // newInitiator
       
   366     }
       
   367