omaprovisioning/provisioning/IMAdapter/Src/CWPIMSAPItem.cpp
changeset 73 ae69c2e8bc34
parent 71 d2517372cc44
child 77 9f85c58c0592
equal deleted inserted replaced
71:d2517372cc44 73:ae69c2e8bc34
     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 "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 *     Settings item for wireless village settings, handles also the saving of
       
    16 *     Settings item. 
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "CWPIMSAPItem.h"
       
    23 
       
    24 
       
    25 #include <cimpssapsettings.h>
       
    26 #include <cimpssapsettingsstore.h>
       
    27 #include <cimpssapsettingslist.h>
       
    28 #include <CWPParameter.h>
       
    29 
       
    30 // CONSTANTS
       
    31 const TUint KLengthOfIncrementChars = 4;
       
    32 
       
    33 // ================= MEMBER FUNCTIONS =======================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CWPIMSAPItem::NewL
       
    37 // Two-phased constructor.
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CWPIMSAPItem* CWPIMSAPItem::NewLC( TIMPSAccessGroup aAccessGroup )
       
    41     {
       
    42 	CWPIMSAPItem* self = new ( ELeave ) CWPIMSAPItem( aAccessGroup );
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL();    
       
    45     return self;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CWPIMSAPItem::CWPIMSAPItem
       
    50 // C++ default constructor can NOT contain any code, that
       
    51 // might leave.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CWPIMSAPItem::CWPIMSAPItem( TIMPSAccessGroup aAccessGroup )
       
    55                             : iAccessGroup( aAccessGroup )
       
    56     {
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // Destructor
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CWPIMSAPItem::~CWPIMSAPItem()
       
    64     {
       
    65     delete iWVSAPSettings;    
       
    66     delete iSAPStore;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CWPIMSAPItem::ConstructL
       
    71 // Symbian 2nd phase constructor can leave.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 void CWPIMSAPItem::ConstructL()
       
    75     {
       
    76     iWVSAPSettings = CIMPSSAPSettings::NewL();
       
    77 
       
    78     // create the SAPSettingsStore    
       
    79     TRAPD( err, iSAPStore = CIMPSSAPSettingsStore::NewL() );    
       
    80     
       
    81     if ( err == KErrCorrupt )
       
    82         {        
       
    83         // if corrupted, recreation creates also new db.
       
    84         iSAPStore = CIMPSSAPSettingsStore::NewL();
       
    85         }
       
    86     else
       
    87         {
       
    88         User::LeaveIfError(err);
       
    89         }
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CWPIMSAPItem::SetSettingsNameL
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CWPIMSAPItem::SetSettingsNameL(const TDesC& aName)
       
    97     {
       
    98     iWVSAPSettings->SetSAPNameL( aName );
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CWPIMSAPItem::SetUserIDL
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CWPIMSAPItem::SetUserIDL(const TDesC& aUserId)
       
   106     {
       
   107     iWVSAPSettings->SetSAPUserIdL( aUserId );
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CWPIMSAPItem::SetPasswordL
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CWPIMSAPItem::SetPasswordL(const TDesC& aPassword)
       
   115     {
       
   116     iWVSAPSettings->SetSAPUserPasswordL(aPassword);
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CWPIMSAPItem::SetSAPURIL
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 void CWPIMSAPItem::SetSAPURIL(const TDesC& aURI)
       
   124     {
       
   125     iWVSAPSettings->SetSAPAddressL( aURI);
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CWPIMSAPItem::SetIAPIdL
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 void CWPIMSAPItem::SetIAPIdL(TUint32 aUid)
       
   133     {
       
   134     iWVSAPSettings->SetAccessPoint( aUid );
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CWPIMSAPItem::StoreL
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 TUint32 CWPIMSAPItem::StoreL()
       
   142     {
       
   143     FLOG( _L( "CWPIMSAPItem::StoreL: Enter" ) );
       
   144     TUint32 uid(0);
       
   145     // check is there already a server with a same name; if there is, 
       
   146     // increment the name. 
       
   147     if ( !IsServerNameUniqueL( iWVSAPSettings->SAPName() ) )
       
   148         {
       
   149         TInt length = iWVSAPSettings->SAPName().Length() + KLengthOfIncrementChars;
       
   150         
       
   151         HBufC* newName = HBufC::NewLC(length);
       
   152         TPtr namePtr = newName->Des();
       
   153         namePtr = iWVSAPSettings->SAPName();
       
   154 
       
   155         do 
       
   156             {
       
   157          IncrementNameL(namePtr);
       
   158             }
       
   159         while ( !IsServerNameUniqueL(namePtr) );
       
   160         
       
   161         iWVSAPSettings->SetSAPNameL(namePtr);
       
   162         
       
   163         CleanupStack::PopAndDestroy(newName);
       
   164         }
       
   165 
       
   166     // store the SAP item
       
   167     uid = iSAPStore->StoreNewSAPL( iWVSAPSettings, iAccessGroup );
       
   168     FLOG( _L( "CWPIMSAPItem::StoreL: Exit" ) );
       
   169     return uid;    
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CWPIMSAPItem::DeleteL
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 void CWPIMSAPItem::DeleteL( TUint32 aUid )
       
   177 	{
       
   178 	TUint32 defaultUid = 0;
       
   179 	TRAPD( error, iSAPStore->GetDefaultL( defaultUid, iAccessGroup ));
       
   180 	if ( error != KErrNone )
       
   181 		{
       
   182 		// No default found, which is fine.
       
   183 		// set default to 0 just in case
       
   184 		defaultUid = 0;
       
   185 		}
       
   186 	TUint32 sapCount = iSAPStore->SAPCountL( iAccessGroup );
       
   187 
       
   188 	if ( aUid == defaultUid &&  sapCount != 1 )
       
   189 		{
       
   190 		// SAP to be deleted is default SAP, need to set another to default
       
   191 		CIMPSSAPSettingsList* list = CIMPSSAPSettingsList::NewLC();
       
   192 		iSAPStore->PopulateSAPSettingsListL( *list, iAccessGroup );
       
   193 		TUint32 tempUid = 0;
       
   194 		for ( TUint32 i = 0; i < sapCount; i++ )
       
   195 			{
       
   196 			tempUid = list->UidForIndex( i );
       
   197 			if ( tempUid != aUid )
       
   198 				{
       
   199 				iSAPStore->SetToDefaultL( tempUid, iAccessGroup );
       
   200 				break;
       
   201 				}
       
   202 			}
       
   203 		CleanupStack::PopAndDestroy( list );
       
   204 		}
       
   205 
       
   206 	// currently do not react to error situations
       
   207 	TRAP( error, iSAPStore->DeleteSAPL( aUid ));
       
   208 	}
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CWPIMSAPItem::IsServerNameUniqueL
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 TBool CWPIMSAPItem::IsServerNameUniqueL( const TDesC& aServerName )
       
   215     {
       
   216     TBool ret(ETrue);
       
   217     // create list for SAP items. 
       
   218     CIMPSSAPSettingsList* SAPList = CIMPSSAPSettingsList::NewLC();
       
   219     // populate the list with SAP settings
       
   220     iSAPStore->PopulateSAPSettingsListL( *SAPList, iAccessGroup );
       
   221     
       
   222     TInt storedCount = SAPList->MdcaCount();
       
   223     
       
   224     for( TInt index(0); index < storedCount; index++  ) 
       
   225         {
       
   226         SAPList->MdcaPoint( index );
       
   227         TInt SAPListUid = SAPList->UidForIndex( index );        
       
   228         // get list item
       
   229         const CIMPSSAPSettingsListItem* item = 
       
   230                                         SAPList->ListItemForUid( SAPListUid  );
       
   231         // compare is there already a server with same name. 
       
   232         if ( item && aServerName == item->Name() )
       
   233             {            
       
   234             // SAP setting name exists in list.
       
   235             ret = EFalse;
       
   236             break;
       
   237             }
       
   238         }
       
   239     CleanupStack::PopAndDestroy( SAPList );
       
   240     return ret;
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CWPIMSAPItem::SetAsDefaultL
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 void CWPIMSAPItem::SetAsDefaultL( TUint32 aUid, TIMPSAccessGroup aAccessGroup )
       
   248     {
       
   249     // create the store
       
   250     CIMPSSAPSettingsStore* sapStore = CIMPSSAPSettingsStore::NewLC();
       
   251     // set SAPItem with aUid as default
       
   252     sapStore->SetToDefaultL( aUid, aAccessGroup );
       
   253     CleanupStack::PopAndDestroy( sapStore );
       
   254     }
       
   255  
       
   256 void CWPIMSAPItem::IncrementNameL(TDes& aName)
       
   257     {
       
   258 
       
   259 	FLOG( _L( "CWPIMSAPItem::IncrementNameL: Enter" ) );
       
   260     
       
   261     TInt length = aName.Length();
       
   262     
       
   263     
       
   264     // If this fails it means that at least default name should have been assigned
       
   265     // to item.
       
   266     //__ASSERT_ALWAYS(length > 0, Panic(EMEUNameEmpty));
       
   267     TInt newOrdinal = 0;
       
   268     TInt index = length-1;
       
   269     TInt charValue = aName[index];
       
   270 
       
   271     // Check if name ends with ')'
       
   272     if (charValue == ')')
       
   273         {
       
   274         TBool cutBetweenIndexes = EFalse;
       
   275         index--;
       
   276         TInt multi = 1;
       
   277         while ( index >= 0)
       
   278             {
       
   279             charValue = aName[index];
       
   280             TChar ch(charValue);
       
   281 
       
   282             if (!ch.IsDigit())
       
   283                 {
       
   284                 // There was non-digits inside the brackets.
       
   285                 if ( charValue == '(')
       
   286                     {
       
   287                     // We found the start
       
   288                     cutBetweenIndexes = ETrue;
       
   289                     index--;
       
   290                     break;
       
   291                     }
       
   292                 else
       
   293                     {
       
   294                     // There was a non-digit inside the '(' and ')'
       
   295                     // We just append after the original name.
       
   296                     break;
       
   297                     }
       
   298                 } // if
       
   299             else
       
   300                 {
       
   301                 TInt newNum = ch.GetNumericValue();
       
   302                 if (multi <= 100000000)
       
   303                     {
       
   304                     // If there's a pattern (007) then this makes it to be (007)(01)
       
   305                     if (newNum == 0 && newOrdinal != 0 && multi > 10)
       
   306                         {
       
   307                         break;
       
   308                         }
       
   309                     newOrdinal += newNum * multi;
       
   310                     multi*=10;
       
   311                     }//0010
       
   312                 else
       
   313                     {
       
   314                     newOrdinal = 0;
       
   315                     break;
       
   316                     }
       
   317                 }
       
   318             index--;
       
   319             } // while
       
   320 
       
   321         // There was correct pattern of (0000) so now we delete it.
       
   322         if (cutBetweenIndexes)
       
   323             {
       
   324             aName.Delete(index+1, length-index);
       
   325             }
       
   326         else
       
   327             {
       
   328             // This case is for example 12345) so we get 12345)(01)
       
   329             newOrdinal = 0;
       
   330             }
       
   331         } // if
       
   332 
       
   333     // Add one to the ordinal read from the old name
       
   334     newOrdinal++;
       
   335 
       
   336     // Check that there's enough space to add the ordinal
       
   337     TInt maxLength = aName.MaxLength();
       
   338     TInt ordinalSpace(4); // Ordinal space comes from (00) so it's 4
       
   339     length = aName.Length();
       
   340     if (length + ordinalSpace > maxLength)
       
   341         {
       
   342         aName.Delete(maxLength-ordinalSpace, ordinalSpace);
       
   343         }
       
   344 
       
   345     // Append the ordinal at the end of the name
       
   346     HBufC* old = aName.AllocLC();
       
   347 
       
   348     _LIT(KFormatSpecLessTen, "%S(0%d)");
       
   349     _LIT(KFormatSpecOverTen, "%S(%d)");
       
   350     if (newOrdinal < 10)
       
   351         {
       
   352         aName.Format(KFormatSpecLessTen, old, newOrdinal);
       
   353         }
       
   354     else
       
   355         {
       
   356         aName.Format(KFormatSpecOverTen, old, newOrdinal);
       
   357         }
       
   358     CleanupStack::PopAndDestroy(old); // old
       
   359     
       
   360     FLOG( _L( "CWPIMSAPItem::IncrementNameL: Exit" ) );
       
   361     }
       
   362 
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CWPIMSAPItem::IncrementNameL
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 void CWPIMSAPItem::IncrementNameL(TDes& aName, TInt aMaxLength)
       
   369     {
       
   370     
       
   371     FLOG( _L( "CWPIMSAPItem::IncrementNameL: Enter" ) );
       
   372     
       
   373     TInt length = aName.Length();
       
   374 
       
   375     	
       
   376     // If this fails it means that at least default name should have been assigned
       
   377     // to item.
       
   378     //__ASSERT_ALWAYS(length > 0, Panic(EMEUNameEmpty));
       
   379     TInt newOrdinal = 0;
       
   380     TInt index = length-1;
       
   381     TInt charValue = aName[index];
       
   382 
       
   383     // Check if name ends with ')'
       
   384     if (charValue == ')')
       
   385         {
       
   386         TBool cutBetweenIndexes = EFalse;
       
   387         index--;
       
   388         TInt multi = 1;
       
   389         while ( index >= 0)
       
   390             {
       
   391             charValue = aName[index];
       
   392             TChar ch(charValue);
       
   393 
       
   394             if (!ch.IsDigit())
       
   395                 {
       
   396                 // There was non-digits inside the brackets.
       
   397                 if ( charValue == '(')
       
   398                     {
       
   399                     // We found the start
       
   400                     cutBetweenIndexes = ETrue;
       
   401                     index--;
       
   402                     break;
       
   403                     }
       
   404                 else
       
   405                     {
       
   406                     // There was a non-digit inside the '(' and ')'
       
   407                     // We just append after the original name.
       
   408                     break;
       
   409                     }
       
   410                 } // if
       
   411             else
       
   412                 {
       
   413                 TInt newNum = ch.GetNumericValue();
       
   414                 if (multi <= 100000000)
       
   415                     {
       
   416                     // If there's a pattern (007) then this makes it to be (007)(01)
       
   417                     if (newNum == 0 && newOrdinal != 0 && multi > 10)
       
   418                         {
       
   419                         break;
       
   420                         }
       
   421                     newOrdinal += newNum * multi;
       
   422                     multi*=10;
       
   423                     }//0010
       
   424                 else
       
   425                     {
       
   426                     newOrdinal = 0;
       
   427                     break;
       
   428                     }
       
   429                 }
       
   430             index--;
       
   431             } // while
       
   432 
       
   433         // There was correct pattern of (0000) so now we delete it.
       
   434         if (cutBetweenIndexes)
       
   435             {
       
   436             aName.Delete(index+1, length-index);
       
   437             }
       
   438         else
       
   439             {
       
   440             // This case is for example 12345) so we get 12345)(01)
       
   441             newOrdinal = 0;
       
   442             }
       
   443         } // if
       
   444 
       
   445     // Add one to the ordinal read from the old name
       
   446     newOrdinal++;
       
   447 
       
   448     // Check that there's enough space to add the ordinal
       
   449     TInt maxLength = aMaxLength;
       
   450     TInt ordinalSpace(4); // Ordinal space comes from (00) so it's 4
       
   451     length = aName.Length();
       
   452     if (length + ordinalSpace > maxLength)
       
   453         {
       
   454         aName.Delete(maxLength-ordinalSpace, ordinalSpace);
       
   455         }
       
   456 
       
   457     // Append the ordinal at the end of the name
       
   458     HBufC* old = aName.AllocLC();
       
   459 
       
   460     _LIT(KFormatSpecLessTen, "%S(0%d)");
       
   461     _LIT(KFormatSpecOverTen, "%S(%d)");
       
   462     if (newOrdinal < 10)
       
   463         {
       
   464         aName.Format(KFormatSpecLessTen, old, newOrdinal);
       
   465         }
       
   466     else
       
   467         {
       
   468         aName.Format(KFormatSpecOverTen, old, newOrdinal);
       
   469         }
       
   470     CleanupStack::PopAndDestroy(old); // old
       
   471     
       
   472     FLOG( _L( "CWPIMSAPItem::IncrementNameL: Enter" ) );
       
   473     }
       
   474 
       
   475 //  End of File  
       
   476