sipplugins/sippsipsettingsui/src/gssipmodel.cpp
changeset 0 307788aac0a8
child 1 dd3853b8dc3f
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2003 - 2005 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:  Application's document class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include    <aknnotewrappers.h>
       
    22 #include    <StringLoader.h>
       
    23 #include    <aknview.h>
       
    24 #include 	<gsplugininterface.h>
       
    25 #include    <gssipsettingspluginrsc.rsg> //GUI Resource
       
    26 #include    <escapeutils.h>
       
    27 #include    "gssipmodel.h"
       
    28 #include    "gssipappshutter.h"
       
    29 #include    "gssipprofileutil.h"
       
    30 #include    "gssippluginlogger.h"
       
    31 
       
    32 // CONSTANTS
       
    33 _LIT( KOpenParenthesis, "(" );
       
    34 _LIT( KClosedParenthesis, ")" );
       
    35 const TInt KMaxDuplicatePostfixLength = 10;
       
    36 const TInt KSIPGranularity = 5;
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CGSSIPModel::CGSSIPModel
       
    42 // C++ default constructor can NOT contain any code, that
       
    43 // might leave.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CGSSIPModel::CGSSIPModel() 
       
    47     {
       
    48     __GSLOGSTRING("CGSSIPModel::CGSSIPModel" )
       
    49     iQuitAfterSave = EFalse;
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CGSSIPModel::ConstructL
       
    54 // Symbian 2nd phase constructor can leave.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CGSSIPModel::ConstructL()
       
    58     {
       
    59     __GSLOGSTRING("CGSSIPModel::ConstructL" )
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CGSSIPModel::NewL
       
    64 // Two-phased constructor.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 CGSSIPModel* CGSSIPModel::NewL()
       
    68     {
       
    69     __GSLOGSTRING("CGSSIPModel::NewL" )
       
    70     CGSSIPModel* self = new( ELeave ) CGSSIPModel();
       
    71     
       
    72     CleanupStack::PushL( self );
       
    73     self->ConstructL();
       
    74     CleanupStack::Pop( self );
       
    75 
       
    76     return self;
       
    77     }
       
    78 
       
    79   
       
    80 // Destructor
       
    81 CGSSIPModel::~CGSSIPModel()
       
    82     {
       
    83     __GSLOGSTRING("CGSSIPModel::~CGSSIPModel" )
       
    84     if ( iProfiles )
       
    85         {
       
    86         iProfiles->ResetAndDestroy();
       
    87         delete iProfiles;
       
    88         }
       
    89 
       
    90     delete iAppShutter;
       
    91     delete iEngine;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CGSSIPModel::CreateEngineL
       
    96 // Creates the application engine
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 void CGSSIPModel::CreateEngineL( CGSPluginInterface* aAppUi )
       
   100     {
       
   101     __GSLOGSTRING("CGSSIPModel::CreateEngineL Start" )
       
   102     iAppUi = aAppUi;
       
   103     
       
   104     // Create AppShutter instance.
       
   105     iAppShutter = CGSSIPAppShutter::NewL( iAppUi );   
       
   106         
       
   107     iEngine = CSIPManagedProfileRegistry::NewL( *this );    
       
   108     ReadArrayFromEngineL();
       
   109     __GSLOGSTRING("CGSSIPModel::CreateEngineL End" )
       
   110     }
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // CGSSIPModel::EditProfileL
       
   114 // Copies profile's data to iProfileData
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 TBool CGSSIPModel::EditProfileL( TInt aIndex )
       
   118     {    
       
   119     __GSLOGSTRING("CGSSIPModel::EditProfileL " )
       
   120     __GSLOGSTRING1("CGSSIPModel::EditProfileL aIndex: %d", aIndex)
       
   121     TBool moveToNextView = ETrue;
       
   122 
       
   123     CSIPManagedProfile* profile = 
       
   124         static_cast<CSIPManagedProfile*>( iProfiles->At( aIndex ) );
       
   125 
       
   126     if ( iEngine->IsInUseL( *profile ) )
       
   127         {
       
   128         HBufC* errorTxt = StringLoader::LoadLC( R_QTN_SIP_ERROR_PROFILE_USED );
       
   129         CAknErrorNote* note = new ( ELeave ) CAknErrorNote( ETrue );
       
   130         note->ExecuteLD( errorTxt->Des() );
       
   131         CleanupStack::PopAndDestroy( errorTxt );        
       
   132 
       
   133         moveToNextView = EFalse;
       
   134         }
       
   135     else
       
   136         {
       
   137         CopyToProfileDataStructL( aIndex );
       
   138         }
       
   139 
       
   140     return moveToNextView;
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CGSSIPModel::CreateAndEditProfileL
       
   145 // Starts editing a new profile that is based on an existing one
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 void CGSSIPModel::CreateAndEditProfileL( TInt aCopiedIndex )
       
   149     {
       
   150     __GSLOGSTRING("CGSSIPModel::CreateAndEditProfileL Start" )
       
   151     __GSLOGSTRING1("CGSSIPModel::CreateAndEditProfileL aCopiedIndex: %d", aCopiedIndex)
       
   152     if ( aCopiedIndex == KUnknownProfileIndex )
       
   153         {
       
   154         // Create new profile with default values
       
   155         FillDataStructWithDefaultValsL();        
       
   156         }
       
   157     else
       
   158         {
       
   159         CopyToProfileDataStructL( aCopiedIndex );
       
   160         iProfileData.iID = KUnknownProfileIndex;
       
   161         }
       
   162         
       
   163     CheckDuplicateNameL( iProfileData.iID, 
       
   164         iProfileData.iProviderName , ETrue);   
       
   165     __GSLOGSTRING("CGSSIPModel::CreateAndEditProfileL End" )
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CGSSIPModel::ProfileData
       
   170 // Returns pointer to profile data structure for modifying it directly
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 TSIPSettingsData* CGSSIPModel::ProfileData()
       
   174     {
       
   175     __GSLOGSTRING("CGSSIPModel::ProfileData" )
       
   176     return &iProfileData;
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // CGSSIPModel::ProfileData
       
   181 // Returns pointer to the profile array
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 CArrayPtr<CSIPManagedProfile>* CGSSIPModel::ProfileArray()
       
   185     {
       
   186     __GSLOGSTRING("CGSSIPModel::ProfileArray" )
       
   187     return iProfiles;
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CGSSIPModel::SaveProfileL
       
   192 // Saves profile to persistent storage
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 TBool CGSSIPModel::SaveProfileL()
       
   196     {
       
   197     __GSLOGSTRING("CGSSIPModel::SaveProfileL" )
       
   198     TBool modifications = ETrue;
       
   199     TInt err( KErrNone );
       
   200 
       
   201     CheckDuplicateNameL( iProfileData.iID, iProfileData.iProviderName );
       
   202 
       
   203     if ( iProfileData.iID == KUnknownProfileIndex )
       
   204         {
       
   205         // Need to create a new profile
       
   206         CSIPManagedProfile* profile = 
       
   207         	iEngine->CreateL( iProfileData.iServiceProfile );
       
   208         
       
   209         CleanupStack::PushL( profile );
       
   210 
       
   211         // Copy attributes to the new profile        
       
   212         iNewProfile = profile;
       
   213         profile = CopyDataToProfileL();        
       
   214         
       
   215         // Save the profile to persistent storage                
       
   216         TRAP( err, iEngine->SaveL( *profile ) );
       
   217         
       
   218         if ( err )
       
   219             {
       
   220             // When exit is called and we get error code because of same realm
       
   221             // with different username, we sohuld exit without saving.
       
   222             // Otherwise this method leaves.
       
   223             if ( iQuitAfterSave )
       
   224                 {
       
   225                 CleanupStack::PopAndDestroy( profile );
       
   226                 modifications = EFalse;
       
   227                 }
       
   228             else
       
   229                 {
       
   230                 User::Leave( err );
       
   231                 }
       
   232             }
       
   233         else
       
   234             {
       
   235             CleanupStack::Pop( profile );
       
   236             }
       
   237         }
       
   238     else
       
   239         {
       
   240 		// check at first if a profile is locked
       
   241     	TBool locked( EFalse );
       
   242 		err = Profile( iProfileData.iID )->GetParameter( KSIPProfileLocked,
       
   243 	    		locked );
       
   244     	if ( locked )
       
   245     		{
       
   246     		return EFalse;
       
   247     		}
       
   248 
       
   249         // We can save the modified changes if any
       
   250         if ( CheckForModificationsL() )  
       
   251             {
       
   252             // Check that the profile is not in use
       
   253             CSIPManagedProfile* profile = Profile( iProfileData.iID );
       
   254             CSIPManagedProfile* clonedProfile = profile->CloneL();
       
   255             CleanupStack::PushL( clonedProfile );
       
   256 
       
   257             profile = CopyDataToProfileL();
       
   258             
       
   259             TRAP( err, iEngine->SaveL( *profile ) );                        
       
   260 
       
   261             if ( err )
       
   262                 {
       
   263                 // Copy old profile data back to temporary variable
       
   264                 // If this leaves, then nope can do; there hasn't been enough
       
   265                 // memory on the first place to do the operations, so there isn't
       
   266                 // much we can do execpt prevent further memory loss               
       
   267                 ReplaceProfileL( iProfileData.iID, clonedProfile );
       
   268                 
       
   269                 // When exit is called and we get error code because of same realm
       
   270                 // with different username, we sohuld exit without saving.
       
   271                 // Otherwise this method leaves.
       
   272                 if ( iQuitAfterSave )
       
   273                     {
       
   274                     modifications = EFalse;
       
   275                     }
       
   276                 else
       
   277                     {
       
   278                     User::Leave( err );
       
   279                     }    
       
   280                 }
       
   281 
       
   282             CleanupStack::PopAndDestroy( clonedProfile );
       
   283             }
       
   284         else
       
   285             {
       
   286             // No modifications, not saving for nothing
       
   287             modifications = EFalse;
       
   288             }
       
   289         }
       
   290 
       
   291     return modifications;
       
   292     }
       
   293 
       
   294 // -----------------------------------------------------------------------------
       
   295 // CGSSIPModel::SetDefaultProfileL
       
   296 // Sets new default profile
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 void CGSSIPModel::SetDefaultProfileL( TInt aIndex )
       
   300     {
       
   301     __GSLOGSTRING("CGSSIPModel::SetDefaultProfileL Start" )
       
   302     __GSLOGSTRING1("CGSSIPModel::SetDefaultProfileL aIndex: %d", aIndex)
       
   303     TBool bDefault = EFalse ;
       
   304     TInt err = 
       
   305     	iProfiles->At( aIndex )->GetParameter( KSIPDefaultProfile, bDefault );
       
   306     User::LeaveIfError( err );
       
   307     if ( !bDefault )
       
   308         {
       
   309         CSIPManagedProfile* profile = 
       
   310             static_cast<CSIPManagedProfile*>( iProfiles->At( aIndex ) );
       
   311 
       
   312         if ( iEngine->IsInUseL( *profile ) )
       
   313             {
       
   314             // Can't change the profile, it is in use
       
   315             User::Leave( KErrInUse );
       
   316             }        
       
   317 
       
   318         // Set the profile to be default and save sum' settings
       
   319         err= profile->SetParameter( KSIPDefaultProfile, ETrue );
       
   320         User::LeaveIfError( err );
       
   321         
       
   322         TRAP( err, iEngine->SaveL( *profile ) );
       
   323 
       
   324         // If SaveL() leaves profile and the old default profile might be
       
   325         // set as defaults; need to read the whole array again..
       
   326         if ( err )
       
   327             {
       
   328             ReadProfileListFromEngineSafeL();                            
       
   329             User::Leave( err );
       
   330             }        
       
   331         }
       
   332     __GSLOGSTRING("CGSSIPModel::SetDefaultProfileL End" )
       
   333     }
       
   334 
       
   335 // -----------------------------------------------------------------------------
       
   336 // CGSSIPModel::DefaultProfileIndex
       
   337 // Returns index to the default profile
       
   338 // -----------------------------------------------------------------------------
       
   339 //
       
   340 TInt CGSSIPModel::DefaultProfileIndex()
       
   341     {     
       
   342     __GSLOGSTRING("CGSSIPModel::DefaultProfileIndex " )
       
   343     TInt i( 0 );
       
   344     TInt index( KUnknownProfileIndex );
       
   345     // "Default values" are not values from default profile; instead they
       
   346     // are the values that are defined on UI specification
       
   347     
       
   348     // Scans through the array and returns the index of the profile
       
   349     for ( i = 0; i < iProfiles->Count() && index == KUnknownProfileIndex; i++ )
       
   350         {
       
   351         TBool defProfile = EFalse;		
       
   352     	TInt err = 
       
   353     		iProfiles->At( i )->GetParameter( KSIPDefaultProfile, defProfile );        
       
   354         if ( err )
       
   355             {
       
   356             // No luck if leaves
       
   357             defProfile = EFalse;
       
   358             }
       
   359 
       
   360         if ( defProfile )
       
   361             {
       
   362             index = i;
       
   363             }
       
   364         }
       
   365     __GSLOGSTRING1("CGSSIPModel::DefaultProfileIndex: %d", index) 
       
   366     return index;
       
   367     }
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CGSSIPModel::TypeListL
       
   371 // Returns an array consisting of the typenames
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 RArray<TSIPProfileTypeInfo>* CGSSIPModel::TypeListL()
       
   375     {  
       
   376     __GSLOGSTRING("CGSSIPModel::TypeListL" )
       
   377     RArray<TSIPProfileTypeInfo>* typeArray = 
       
   378         new (ELeave) RArray<TSIPProfileTypeInfo>;
       
   379     
       
   380     TCleanupItem clItem( CloseAndDestroy, &typeArray );
       
   381     CleanupStack::PushL( clItem );
       
   382     
       
   383     // Get the supported profile types
       
   384     iEngine->SupportedProfileTypesL( *typeArray );
       
   385 
       
   386     CleanupStack::Pop(); //clItem
       
   387     
       
   388     return typeArray;
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CGSSIPModel::DeleteProfileL
       
   393 // Deletes a profile from permanent storage
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 void CGSSIPModel::DeleteProfileL( TInt aIndex )
       
   397     {
       
   398     __GSLOGSTRING("CGSSIPModel::DeleteProfileL Start" )
       
   399     __GSLOGSTRING1("CGSSIPModel::DeleteProfileL aIndex: %d", aIndex) 
       
   400     CSIPManagedProfile* profile = iProfiles->At( aIndex );    
       
   401     
       
   402     if ( iEngine->IsInUseL( *profile ) )
       
   403         {
       
   404         User::Leave( KErrInUse );
       
   405         }
       
   406 
       
   407     iEngine->DestroyL( *profile );
       
   408     __GSLOGSTRING("CGSSIPModel::DeleteProfileL End" )
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CGSSIPModel::QuitAfterSave
       
   413 // Set "Quit after save" flag
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 void CGSSIPModel::QuitAfterSave()
       
   417     {
       
   418     __GSLOGSTRING("CGSSIPModel::QuitAfterSave" )
       
   419     iQuitAfterSave = ETrue;
       
   420     }
       
   421 
       
   422 // -----------------------------------------------------------------------------
       
   423 // CGSSIPModel::ErrorInRegistration
       
   424 // Checks if there has been error in registration
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 TBool CGSSIPModel::ErrorInRegistration( TInt aIndex )
       
   428     {    
       
   429     __GSLOGSTRING("CGSSIPModel::ErrorInRegistration" )
       
   430     __GSLOGSTRING1("CGSSIPModel::ErrorInRegistration aIndex: %d", aIndex)
       
   431     return ( iEngine->LastRegistrationError( 
       
   432         *( iProfiles->At( aIndex ) ) ) != KErrNone );
       
   433     }
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // CGSSIPModel::ProfileRegistryEventOccurred
       
   437 // SIP profile event 
       
   438 // -----------------------------------------------------------------------------
       
   439 //
       
   440 void CGSSIPModel::ProfileRegistryEventOccurred( 
       
   441     TUint32 aSIPProfileId, TEvent aEvent )
       
   442 	{
       
   443 	__GSLOGSTRING("CGSSIPModel::ProfileRegistryEventOccurred Start" )
       
   444     __GSLOGSTRING1("CGSSIPModel::ProfileRegistryEventOccurred aSIPProfileId: %d", aSIPProfileId)  
       
   445     __GSLOGSTRING1("CGSSIPModel::ProfileRegistryEventOccurred aEvent: %d", aEvent) 
       
   446     switch( aEvent )	
       
   447         {
       
   448         case EProfileCreated:
       
   449 			TRAP_IGNORE( ProfileCreatedL( aSIPProfileId ) )
       
   450 			break;
       
   451         case EProfileUpdated:	
       
   452             TRAP_IGNORE( ProfileUpdatedL( aSIPProfileId ) )
       
   453             break;
       
   454         case EProfileRegistered:						
       
   455         case EProfileDeregistered:
       
   456             ProfileRegistrationStatusChanged( aSIPProfileId );
       
   457             break;
       
   458         case EProfileDestroyed:
       
   459             ProfileDestroyed( aSIPProfileId );
       
   460             break;
       
   461         default:
       
   462             // Do nothing.
       
   463             break;
       
   464 		}
       
   465 	__GSLOGSTRING("CGSSIPModel::ProfileRegistryEventOccurred End" )
       
   466 	}
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // CGSSIPModel::ProfileCreatedL
       
   470 // SIP profile creation event 
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 void CGSSIPModel::ProfileCreatedL( TUint32 aSIPProfileId )
       
   474     {   
       
   475     __GSLOGSTRING("CGSSIPModel::ProfileCreatedL Start" )
       
   476     __GSLOGSTRING1("CGSSIPModel::ProfileCreatedL aSIPProfileId: %d", aSIPProfileId)
       
   477     CSIPManagedProfile* profile = NULL;
       
   478     TBool byThisApp = ETrue;
       
   479     TUint32 profileId = 0;
       
   480     if( iNewProfile )
       
   481     	{
       
   482     	TInt err = iNewProfile->GetParameter( KSIPProfileId, profileId );
       
   483     	if( err )	
       
   484     		{
       
   485     	    return;
       
   486     		}
       
   487     	}
       
   488     if ( iNewProfile && aSIPProfileId == profileId )
       
   489         {
       
   490         profile = iNewProfile;
       
   491         iNewProfile = NULL;
       
   492         }
       
   493     else
       
   494         {
       
   495         // Some other application has created a new profile
       
   496         byThisApp = EFalse;
       
   497         TRAPD( err, profile = static_cast<CSIPManagedProfile*>( 
       
   498                         iEngine->ProfileL( aSIPProfileId ) ) );
       
   499         if ( err )
       
   500             {
       
   501             // Can't fetch the new profile - nothing to do
       
   502             return;
       
   503             }
       
   504         }
       
   505 
       
   506     // Inserts the profile to the correct place
       
   507     TInt count = iProfiles->Count();
       
   508     TInt i( 0 );    
       
   509     TBool profileAdded( EFalse );
       
   510     TInt err( KErrNone );
       
   511     const TDesC8* providerName = NULL;
       
   512     
       
   513     err = profile->GetParameter( KSIPProviderName, providerName );
       
   514     if( !err )	
       
   515     	{
       
   516     	const TDesC8* oldProvider = NULL;
       
   517     	for ( i = 0; i < count; i++ )
       
   518         	{
       
   519         	err = iProfiles->At( i )->GetParameter( 
       
   520         		KSIPProviderName, oldProvider );
       
   521         	//if there is an error it should continue for next . 
       
   522         	if ( !err && !profileAdded
       
   523         		&& providerName->CompareC( *oldProvider ) < 0 )
       
   524             	{
       
   525             	TRAP( err, iProfiles->InsertL( i, profile ) );            
       
   526             	profileAdded = ETrue;
       
   527             	iAddedProfileIndex = i;
       
   528 	            i = count;
       
   529             	}
       
   530         	}                          	
       
   531     	}
       
   532                 
       
   533     // Profile's name was the 'biggest' - append it
       
   534     if ( !profileAdded )
       
   535         {
       
   536         TRAP( err, iProfiles->AppendL( profile ) );
       
   537         iAddedProfileIndex = iProfiles->Count() - 1;
       
   538         }
       
   539 
       
   540     if ( iQuitAfterSave && byThisApp )
       
   541         {
       
   542         // This application has created a new profile, now
       
   543         // quit the app
       
   544         iAppShutter->RequestAppShutdown();        
       
   545         }
       
   546     else
       
   547         {
       
   548         // Update the view if not exiting the application
       
   549         UpdateListCmd();
       
   550         }
       
   551     __GSLOGSTRING("CGSSIPModel::ProfileCreatedL End" )
       
   552     }        
       
   553     
       
   554 // -----------------------------------------------------------------------------
       
   555 // CGSSIPModel::ProfileUpdatedL
       
   556 // SIP profile information update event
       
   557 // -----------------------------------------------------------------------------
       
   558 //
       
   559 void CGSSIPModel::ProfileUpdatedL( 
       
   560     TUint32 aSIPProfileId )
       
   561     {
       
   562     __GSLOGSTRING("CGSSIPModel::ProfileUpdatedL Start" )
       
   563     __GSLOGSTRING1("CGSSIPModel::ProfileUpdatedL aSIPProfileId: %d", aSIPProfileId)
       
   564     if ( iQuitAfterSave && aSIPProfileId == TUint( iProfileData.iID ) )
       
   565         {
       
   566         // Updated the profile that should be updated - now quit
       
   567         iAppShutter->RequestAppShutdown();        
       
   568         }
       
   569     else
       
   570         {
       
   571         // Updated profile name is not probably the same.
       
   572         // Remove from list and add it again, so it will be in right place 
       
   573         // in the list in alphabetical order
       
   574         TInt err( KErrNone );
       
   575         TInt i( 0 );
       
   576     	TUint32 profileId = 0;
       
   577     	TInt count = iProfiles->Count();    
       
   578     	TInt index( -1 );
       
   579         CSIPManagedProfile* profile = NULL;
       
   580         
       
   581         TRAP( err, profile = static_cast<CSIPManagedProfile*>( 
       
   582         	iEngine->ProfileL( aSIPProfileId ) ) );
       
   583         
       
   584         if ( err )
       
   585             {
       
   586             // Can't fetch the new profile - nothing to do
       
   587             return;
       
   588             }
       
   589         
       
   590     	for ( i = 0; i < count; i++ )
       
   591 			{
       
   592 			err =  iProfiles->At( i )->GetParameter(KSIPProfileId, profileId);
       
   593 			if (profileId == aSIPProfileId && !err )
       
   594 				{
       
   595 				index = i;
       
   596 				i = count;
       
   597 	            }
       
   598 			}
       
   599 			
       
   600 		if ( index >= 0 )
       
   601 			{
       
   602 			// first delete profile from list then add it in right place
       
   603 			// CSIPProfile* profile = iProfiles->At( index );
       
   604 			CSIPProfile* profileRemoved = iProfiles->At( index );
       
   605 			iProfiles->Delete( index );
       
   606 			delete profileRemoved;
       
   607 			count--;
       
   608 		
       
   609 			// now add it again
       
   610 		    const TDesC8* providerName = NULL;
       
   611 	        TBool profileAdded( EFalse );
       
   612 			err = profile->GetParameter( KSIPProviderName, providerName );
       
   613     		if( !err )	
       
   614     			{
       
   615     			const TDesC8* oldProvider = NULL;
       
   616     			for ( i = 0; i < count; i++ )
       
   617         			{
       
   618         			err = iProfiles->At( i )->GetParameter( 
       
   619         				KSIPProviderName, oldProvider );
       
   620         			
       
   621         			if ( !err && !profileAdded
       
   622         				&& providerName->CompareC( *oldProvider ) < 0 )
       
   623             			{
       
   624             			TRAP( err, iProfiles->InsertL( i, profile ) );            
       
   625             			profileAdded = ETrue;
       
   626 						iAddedProfileIndex = i;
       
   627 	            		i = count;
       
   628             			}
       
   629         			}
       
   630        	    	// Profile's name was the 'biggest' - append it
       
   631        	    	if ( !profileAdded )
       
   632 					{
       
   633 		        	TRAP( err, iProfiles->AppendL( profile ) );
       
   634 			        iAddedProfileIndex = iProfiles->Count() - 1;
       
   635         			}
       
   636     			}
       
   637     		}
       
   638     		
       
   639         UpdateListCmd();
       
   640         }
       
   641     __GSLOGSTRING("CGSSIPModel::ProfileUpdatedL End" )
       
   642     }
       
   643 
       
   644 // -----------------------------------------------------------------------------
       
   645 // CGSSIPModel::ProfileRegistrationStatusChanged
       
   646 // SIP profile registration status changed event
       
   647 // -----------------------------------------------------------------------------
       
   648 //
       
   649 void CGSSIPModel::ProfileRegistrationStatusChanged( 
       
   650     TUint32 /*aSIPProfileId*/ )
       
   651     {
       
   652     __GSLOGSTRING("CGSSIPModel::ProfileRegistrationStatusChanged" )
       
   653     UpdateListCmd();
       
   654     }
       
   655 
       
   656 // -----------------------------------------------------------------------------
       
   657 // CGSSIPModel::ProfileDestroyed
       
   658 // SIP profile destruction event.
       
   659 // -----------------------------------------------------------------------------
       
   660 //
       
   661 void CGSSIPModel::ProfileDestroyed(
       
   662     TUint32 aSIPProfileId )
       
   663     {	
       
   664     __GSLOGSTRING("CGSSIPModel::ProfileDestroyed Start" )
       
   665     __GSLOGSTRING1("CGSSIPModel::ProfileDestroyed aSIPProfileId: %d", aSIPProfileId)
       
   666 	TInt i( 0 );
       
   667     TUint32 profileId = 0;
       
   668     TInt count = iProfiles->Count();    	
       
   669 	for ( i = 0; i < count; i++ )
       
   670 		{
       
   671 		TInt err=  iProfiles->At( i )->GetParameter(KSIPProfileId,profileId);
       
   672 		if (profileId == aSIPProfileId && !err )
       
   673 			{
       
   674 			CSIPProfile* profile = iProfiles->At( i );
       
   675 			iProfiles->Delete( i );
       
   676 			delete profile;
       
   677             i = count;
       
   678             }
       
   679 		}
       
   680 
       
   681     UpdateListCmd( EGSCmdProfileDeleted );
       
   682     __GSLOGSTRING("CGSSIPModel::ProfileDestroyed End" )
       
   683     }     	    
       
   684 
       
   685 // -----------------------------------------------------------------------------
       
   686 // CGSSIPModel::AddedProfileIndex
       
   687 // Added profile index in the profiles list.
       
   688 // -----------------------------------------------------------------------------
       
   689 //
       
   690 TInt CGSSIPModel::AddedProfileIndex()
       
   691 	{
       
   692 	__GSLOGSTRING("CGSSIPModel::AddedProfileIndex" )
       
   693 	return iAddedProfileIndex;
       
   694 	}
       
   695 
       
   696 // -----------------------------------------------------------------------------
       
   697 // CGSSIPModel::ProfileRegistryErrorOccured
       
   698 // An asynchronous error has occurred related to SIP profile
       
   699 // -----------------------------------------------------------------------------
       
   700 //
       
   701 void CGSSIPModel::ProfileRegistryErrorOccurred(				    
       
   702 	TUint32 /*aSIPProfileId*/,
       
   703 	TInt /*aError*/ )
       
   704     {       
       
   705     __GSLOGSTRING("CGSSIPModel::ProfileRegistryErrorOccurred" )
       
   706     // Updates list to show that error has happened in registration
       
   707     UpdateListCmd();
       
   708     }
       
   709 
       
   710 // -----------------------------------------------------------------------------
       
   711 // CGSSIPModel::ReadArrayFromEngineL
       
   712 // Reads the profile array from SIP Profile Client
       
   713 // -----------------------------------------------------------------------------
       
   714 //
       
   715 void CGSSIPModel::ReadArrayFromEngineL()
       
   716     {
       
   717     __GSLOGSTRING("CGSSIPModel::ReadArrayFromEngineL Start" )
       
   718     RArray<TSIPProfileTypeInfo> array;
       
   719     CleanupClosePushL( array );
       
   720     
       
   721     // Get the supported profile types
       
   722     iEngine->SupportedProfileTypesL( array );
       
   723     
       
   724     // check iProfiles, if exists delete
       
   725     if ( iProfiles )
       
   726         {
       
   727         iProfiles->ResetAndDestroy();
       
   728         delete iProfiles;
       
   729         }
       
   730 
       
   731     // Create the profile pointer array
       
   732     iProfiles = 
       
   733         new ( ELeave ) CArrayPtrFlat<CSIPManagedProfile>( KSIPGranularity );    
       
   734 
       
   735     TInt i( 0 );
       
   736     RPointerArray<CSIPProfile> profilePointerArray;
       
   737     TCleanupItem clItem( ResetAndDestroy, &profilePointerArray );
       
   738     CleanupStack::PushL( clItem );
       
   739     
       
   740     // Get all profiles based on profile types
       
   741     for ( i = 0; i < array.Count(); i++ )
       
   742         {        
       
   743         iEngine->ProfilesL( array[i], profilePointerArray );
       
   744         TInt j( 0 );
       
   745         while ( profilePointerArray.Count() > 0 )
       
   746             {
       
   747             CSIPManagedProfile* profile = 
       
   748                 static_cast<CSIPManagedProfile*>( profilePointerArray[j] );
       
   749             iProfiles->AppendL( profile );
       
   750             profilePointerArray.Remove( j );
       
   751             }                    
       
   752         profilePointerArray.Reset();    
       
   753         }
       
   754 
       
   755     // Sort the profiles according to their ID
       
   756     // Bubble sort is slow, but because we have only few items it doesn't 
       
   757     // matter
       
   758     TInt j( 0 );
       
   759 	TInt err(KErrNone);
       
   760     const TDesC8* providerName1 = NULL;
       
   761     const TDesC8* providerName2 = NULL;
       
   762     for ( i = 0; i < iProfiles->Count() - 1; i++ ) 
       
   763         {
       
   764         for ( j = 0; j < iProfiles->Count() - 1 - i; j++ )
       
   765             {
       
   766             err=  iProfiles->At( j + 1 )->GetParameter(
       
   767             	KSIPProviderName, providerName1 );
       
   768             User::LeaveIfError( err );
       
   769     		err=  iProfiles->At( j )->GetParameter( 
       
   770     			KSIPProviderName, providerName2 );
       
   771 			User::LeaveIfError(	err	);  
       
   772 			if ( providerName1->CompareC( *providerName2 ) < 0 )  		       			
       
   773                 {  
       
   774                 CSIPManagedProfile* tmp = iProfiles->At( j );
       
   775                 CSIPManagedProfile* tmp2 = iProfiles->At( j + 1 );
       
   776                 iProfiles->Delete( j );
       
   777                 iProfiles->Delete( j );
       
   778                 iProfiles->InsertL( j, tmp );
       
   779                 iProfiles->InsertL( j, tmp2 );                
       
   780                 }
       
   781             }
       
   782         }
       
   783 
       
   784     CleanupStack::PopAndDestroy();  // clItem
       
   785     CleanupStack::PopAndDestroy( &array );
       
   786     __GSLOGSTRING("CGSSIPModel::ReadArrayFromEngineL End" )
       
   787     }
       
   788 
       
   789 // -----------------------------------------------------------------------------
       
   790 // CGSSIPModel::CopyToProfileDataStructL
       
   791 // Copies profile's attributes to the internal data structure
       
   792 // -----------------------------------------------------------------------------
       
   793 //
       
   794 void CGSSIPModel::CopyToProfileDataStructL( TInt aIndex )
       
   795     {
       
   796     __GSLOGSTRING("CGSSIPModel::CopyToProfileDataStructL Start" )
       
   797     __GSLOGSTRING1("CGSSIPModel::CopyToProfileDataStructL  aIndex: %d", aIndex)
       
   798     FillDataStructWithDefaultValsL(); 
       
   799      
       
   800     CSIPManagedProfile* profile = iProfiles->At( aIndex );
       
   801     
       
   802     GSSIPProfileUtil::CopyToTempStructL( profile, iProfileData );
       
   803     GSSIPProfileUtil::CopyServerSettingsToTempStructL(
       
   804         profile, KSIPOutboundProxy, iProfileData.iProxy );
       
   805     GSSIPProfileUtil::CopyServerSettingsToTempStructL(
       
   806         profile, KSIPRegistrar, iProfileData.iRegistrar );  
       
   807     __GSLOGSTRING("CGSSIPModel::CopyToProfileDataStructL End" )
       
   808     }
       
   809 
       
   810 // -----------------------------------------------------------------------------
       
   811 // CGSSIPModel::FillDataStructWithDefaultValsL
       
   812 // Fills the data structures fields with default values
       
   813 // -----------------------------------------------------------------------------
       
   814 //
       
   815 void CGSSIPModel::FillDataStructWithDefaultValsL()
       
   816     {
       
   817     __GSLOGSTRING("CGSSIPModel::FillDataStructWithDefaultValsL Start" )
       
   818     HBufC* providerName = StringLoader::LoadLC( R_QTN_DEF_PROFILE_NAME );
       
   819     iProfileData.iProviderName = providerName->Des();
       
   820     CleanupStack::PopAndDestroy( providerName );
       
   821     
       
   822     iProfileData.iID = KUnknownProfileIndex;
       
   823     iProfileData.iAPIndex = KDefaultIAP;
       
   824     iProfileData.iSNAPIndex = KDefaultSNAP;
       
   825     iProfileData.iUsername = KNullDesC;
       
   826     iProfileData.iCompression = KDefaultCompression;
       
   827     iProfileData.iRegistrationMode = KDefaultRegistration;
       
   828     iProfileData.iSecurity = KDefaultSecurity;
       
   829 
       
   830     // Get the supported profile types
       
   831     RArray<TSIPProfileTypeInfo> typeArray;
       
   832     CleanupClosePushL( typeArray );    
       
   833     iEngine->SupportedProfileTypesL( typeArray );
       
   834     TInt count = typeArray.Count();
       
   835     // Leave if no supported profiles found.
       
   836     if ( count == KErrNone )
       
   837         {
       
   838         User::Leave( KErrNotFound );
       
   839         }
       
   840     // Find default profile type.
       
   841     iProfileData.iServiceProfile = typeArray[0];
       
   842     for ( TInt i = 0; i < count; i++ )
       
   843         {
       
   844         if ( typeArray[i].iSIPProfileClass == TSIPProfileTypeInfo::EInternet )
       
   845             {
       
   846             iProfileData.iServiceProfile = typeArray[i];
       
   847             i = count;
       
   848             }
       
   849         }
       
   850        
       
   851     CleanupStack::PopAndDestroy( &typeArray );
       
   852     
       
   853     iProfileData.iProxy.iAddress = KNullDesC;
       
   854     iProfileData.iProxy.iRealm = KNullDesC;
       
   855     iProfileData.iProxy.iUsername = KNullDesC;
       
   856 
       
   857     iProfileData.iProxy.iLooseRouting = KDefaultLooseRouting;
       
   858     iProfileData.iProxy.iPassword = KNullDesC;
       
   859     iProfileData.iProxy.iPortNumber = KDefaultPortNumber;
       
   860     iProfileData.iProxy.iTransportProtocol = KDefaultTranspProtocol;
       
   861 
       
   862     // Initial registrar server settings are the same than with proxy
       
   863     iProfileData.iRegistrar = iProfileData.iProxy;      
       
   864     iProfileData.iRegistrar.iLooseRouting = EGSLooseRoutingOff;  
       
   865     
       
   866     // Private user identity
       
   867     iProfileData.iProxy.iPrivateIdentity = KNullDesC;
       
   868     iProfileData.iRegistrar.iPrivateIdentity = KNullDesC;
       
   869     __GSLOGSTRING("CGSSIPModel::FillDataStructWithDefaultValsL End" )
       
   870     }
       
   871 
       
   872 // -----------------------------------------------------------------------------
       
   873 // CGSSIPModel::Profile
       
   874 // Returns profile based on the ID
       
   875 // -----------------------------------------------------------------------------
       
   876 //
       
   877 CSIPManagedProfile* CGSSIPModel::Profile( TUint aId )
       
   878     {
       
   879     __GSLOGSTRING("CGSSIPModel::Profile" )
       
   880     __GSLOGSTRING1("CGSSIPModel::Profile aId: %d", aId)
       
   881     TInt i( 0 );
       
   882     CSIPManagedProfile* profile = NULL;
       
   883     
       
   884     TInt count = iProfiles->Count();
       
   885     TUint32 profileId( 0 );
       
   886     TInt err( KErrNone );
       
   887     for ( i = 0; i < count; i++ )
       
   888         {
       
   889         err = iProfiles->At( i )->GetParameter( KSIPProfileId,profileId );    	
       
   890         if ( profileId == aId && !err )
       
   891             {
       
   892             profile = iProfiles->At( i );
       
   893             i = count;
       
   894             }
       
   895         }
       
   896 
       
   897     return profile;
       
   898     }
       
   899 
       
   900 // -----------------------------------------------------------------------------
       
   901 // CGSSIPModel::CheckForModificationsL
       
   902 // Checks for the modifications on the active profile
       
   903 // -----------------------------------------------------------------------------
       
   904 //
       
   905 TBool CGSSIPModel::CheckForModificationsL()
       
   906     {
       
   907     __GSLOGSTRING("CGSSIPModel::CheckForModificationsL " )
       
   908     CSIPManagedProfile* profile = Profile( iProfileData.iID );
       
   909 
       
   910     TBool modifs = 
       
   911         GSSIPProfileUtil::ProfileModificationsL( profile, iProfileData );       
       
   912     modifs |= GSSIPProfileUtil::ServerModificationsL( 
       
   913         profile, KSIPOutboundProxy, iProfileData.iProxy );
       
   914     modifs |= GSSIPProfileUtil::ServerModificationsL(
       
   915         profile, KSIPRegistrar, iProfileData.iRegistrar );
       
   916 
       
   917     return modifs;
       
   918     }
       
   919 
       
   920 // -----------------------------------------------------------------------------
       
   921 // CGSSIPModel::CopyDataToProfileL
       
   922 // Copies data from temporary settings structure to profile
       
   923 // -----------------------------------------------------------------------------
       
   924 //
       
   925 CSIPManagedProfile* CGSSIPModel::CopyDataToProfileL()
       
   926     {
       
   927     __GSLOGSTRING("CGSSIPModel::CopyDataToProfileL " )
       
   928     CSIPManagedProfile* profile = NULL;
       
   929 
       
   930     // Get profile based on ID
       
   931     if ( iProfileData.iID == KUnknownProfileIndex )
       
   932         {
       
   933         profile = iNewProfile;
       
   934         }
       
   935     else
       
   936         {
       
   937         profile = Profile( iProfileData.iID );
       
   938         }
       
   939     
       
   940     GSSIPProfileUtil::CopyProfileAttributesL( profile, iProfileData );
       
   941     GSSIPProfileUtil::CopyServerAttributesL( 
       
   942         profile, KSIPOutboundProxy, iProfileData.iProxy );
       
   943     GSSIPProfileUtil::CopyServerAttributesL( 
       
   944         profile, KSIPRegistrar, iProfileData.iRegistrar );
       
   945     
       
   946     return profile;
       
   947     }
       
   948 
       
   949 // -----------------------------------------------------------------------------
       
   950 // CGSSIPModel::ReplaceProfileL
       
   951 // Replaces one profile with another on the array; only cloned profiles
       
   952 // should be replaced
       
   953 // -----------------------------------------------------------------------------
       
   954 //
       
   955 void CGSSIPModel::ReplaceProfileL( 
       
   956     TInt aId, 
       
   957     CSIPManagedProfile* aProfile )
       
   958     {
       
   959     __GSLOGSTRING("CGSSIPModel::ReplaceProfileL Start " )
       
   960     __GSLOGSTRING1("CGSSIPModel::ReplaceProfileL aId: %d", aId)
       
   961     TInt i( 0 );
       
   962     CSIPManagedProfile* origProfile = NULL;
       
   963     TUint32 profileId( 0 );
       
   964     TInt err( KErrNone );
       
   965     for ( i = 0; i < iProfiles->Count(); i++ )
       
   966         {
       
   967         err = iProfiles->At( i )->GetParameter( KSIPProfileId, profileId );
       
   968         if ( TUint( aId ) == profileId && !err )
       
   969             {
       
   970             origProfile = iProfiles->At( i );
       
   971             }
       
   972         }
       
   973 
       
   974     User::LeaveIfNull( origProfile );
       
   975     GSSIPProfileUtil::CopyProfileL(  aProfile, origProfile );
       
   976     __GSLOGSTRING("CGSSIPModel::ReplaceProfileL End" )
       
   977     }
       
   978 
       
   979 // -----------------------------------------------------------------------------
       
   980 // CGSSIPModel::ReadProfileListFromEngineSafeL
       
   981 // Reads profile list from the engine; if reading fails, keeps the old list 
       
   982 // safe and returns it
       
   983 // -----------------------------------------------------------------------------
       
   984 //
       
   985 void CGSSIPModel::ReadProfileListFromEngineSafeL()
       
   986     {    
       
   987     __GSLOGSTRING("CGSSIPModel::ReadProfileListFromEngineSafeL Start" )
       
   988     // To prevent complete disaster it we'll save the pointer of the old array
       
   989     CArrayPtrFlat<CSIPManagedProfile>* profiles = iProfiles;
       
   990     iProfiles = 0;
       
   991     TRAPD( err, ReadArrayFromEngineL() );
       
   992     
       
   993     if ( err )
       
   994         {
       
   995         // Yes, problems with re-reading profiles; use existing
       
   996         // array
       
   997         if ( iProfiles )
       
   998             {
       
   999             iProfiles->ResetAndDestroy();
       
  1000             delete iProfiles;
       
  1001             }
       
  1002         iProfiles = profiles;
       
  1003         User::Leave( err );
       
  1004         }
       
  1005     else
       
  1006         {
       
  1007         // No problems; delete backup array
       
  1008         profiles->ResetAndDestroy();
       
  1009         delete profiles;
       
  1010         }
       
  1011     __GSLOGSTRING("CGSSIPModel::ReadProfileListFromEngineSafeL End" )
       
  1012     }
       
  1013 
       
  1014 // -----------------------------------------------------------------------------
       
  1015 // CGSSIPModel::UpdateListCmd
       
  1016 // Calls app ui to udpate the lists; might also trigger exit
       
  1017 // -----------------------------------------------------------------------------
       
  1018 //
       
  1019 void CGSSIPModel::UpdateListCmd( 
       
  1020     TInt aCmd )
       
  1021     {
       
  1022     __GSLOGSTRING("CGSSIPModel::UpdateListCmd" )
       
  1023     TRAP_IGNORE( iAppUi->HandleCommandL( aCmd ) )
       
  1024     }
       
  1025 
       
  1026 // -----------------------------------------------------------------------------
       
  1027 // CGSSIPModel::ResetAndDestroy
       
  1028 // For deleting RPointerArray in case of leave
       
  1029 // -----------------------------------------------------------------------------
       
  1030 //
       
  1031 void CGSSIPModel::ResetAndDestroy( 
       
  1032     TAny* aPointerArray )
       
  1033     {
       
  1034     __GSLOGSTRING("CGSSIPModel::ResetAndDestroy" )
       
  1035     RPointerArray<CSIPProfile>* array =
       
  1036         static_cast<RPointerArray<CSIPProfile>*>( aPointerArray );
       
  1037     array->ResetAndDestroy();
       
  1038     array->Close();
       
  1039     }
       
  1040 
       
  1041 // -----------------------------------------------------------------------------
       
  1042 // CGSSIPModel::CloseAndDestroy
       
  1043 // For deleting RArray in case of leave
       
  1044 // -----------------------------------------------------------------------------
       
  1045 //
       
  1046 void CGSSIPModel::CloseAndDestroy( 
       
  1047     TAny* aArray )
       
  1048     {
       
  1049     __GSLOGSTRING("CGSSIPModel::CloseAndDestroy" )
       
  1050     RArray<TSIPProfileTypeInfo>* array =
       
  1051         static_cast<RArray<TSIPProfileTypeInfo>*>( aArray );
       
  1052     array->Close();
       
  1053     delete array;
       
  1054     }
       
  1055     
       
  1056 // -----------------------------------------------------------------------------
       
  1057 // CGSSIPModel::CheckDuplicateNameL
       
  1058 // Checks if duplicate named sip profiles. Renames if same.
       
  1059 // -----------------------------------------------------------------------------
       
  1060 //    
       
  1061 void CGSSIPModel::CheckDuplicateNameL( TInt32 aProfileId, TDes& aName, 
       
  1062                                     TBool aCreatedFromExistingProfile)
       
  1063     {
       
  1064     __GSLOGSTRING("CGSSIPModel::CheckDuplicateNameL Start" )
       
  1065     HBufC8* oldName = EscapeUtils::ConvertFromUnicodeToUtf8L( aName );
       
  1066     CleanupStack::PushL( oldName );
       
  1067     
       
  1068     HBufC8* newName = 
       
  1069         HBufC8::NewLC( oldName->Length() + KMaxDuplicatePostfixLength );
       
  1070     newName->Des().Copy( oldName->Des() );
       
  1071 
       
  1072     // Create 16-bit TBuf for comparison and convert its digit type   
       
  1073     TBuf<KMaxSIPProviderNameLength + KMaxDuplicatePostfixLength>
       
  1074         oldNameBuf;
       
  1075     oldNameBuf.Copy(aName);
       
  1076     AknTextUtils::ConvertDigitsTo(oldNameBuf, EDigitTypeWestern);
       
  1077      
       
  1078     TInt count( iProfiles->Count() );
       
  1079     for( TInt n = 0; n < count; n++ )
       
  1080         {
       
  1081         TUint32 id( KErrNone );
       
  1082         iProfiles->At( n )->GetParameter( KSIPProfileId, id );
       
  1083         
       
  1084         const TDesC8* existingName; 
       
  1085         iProfiles->At( n )->GetParameter( KSIPProviderName, existingName );   
       
  1086                 
       
  1087         // Create 16-bit TBuf for comparison and convert its digit type      
       
  1088         HBufC* tempExistingName16 = 
       
  1089             EscapeUtils::ConvertToUnicodeFromUtf8L( *existingName );
       
  1090         CleanupStack::PushL( tempExistingName16 );        
       
  1091         TBuf<KMaxSIPProviderNameLength *3 + KMaxDuplicatePostfixLength> 
       
  1092             existingNameBuf;
       
  1093         existingNameBuf.Copy(tempExistingName16->Des()); 
       
  1094         CleanupStack::PopAndDestroy( tempExistingName16 );           
       
  1095         AknTextUtils::ConvertDigitsTo(existingNameBuf, EDigitTypeWestern);
       
  1096         
       
  1097         if ( id != aProfileId && !existingNameBuf.Compare( oldNameBuf ) )
       
  1098             {
       
  1099             TBool isUnique( EFalse );
       
  1100 
       
  1101             for( TInt i = 1; !isUnique; i++ )
       
  1102                 {
       
  1103                 TBool found( EFalse );
       
  1104                     
       
  1105                 TBuf<KMaxDuplicatePostfixLength> postFix;
       
  1106                 postFix.Append( KOpenParenthesis() );
       
  1107                 postFix.AppendNum( ( TInt64 )i );
       
  1108                 postFix.Append( KClosedParenthesis() );
       
  1109                 
       
  1110                 newName->Des().Copy( *oldName );
       
  1111                 newName->Des().Append( postFix );
       
  1112                                 
       
  1113                 // Convert back to 16-bit descriptor 
       
  1114                 // because of possible Chinese/Japanese variant. 
       
  1115                 HBufC* newName16 = 
       
  1116                     EscapeUtils::ConvertToUnicodeFromUtf8L( newName->Des() );
       
  1117                 CleanupStack::PushL( newName16 );                               
       
  1118                 
       
  1119                 TInt length = newName16->Length();
       
  1120                 if ( length > KMaxSIPProviderNameLength )
       
  1121                     {
       
  1122                     TInt deleteLength = length - KMaxSIPProviderNameLength;
       
  1123                     TInt pos = length - postFix.Length() - deleteLength;
       
  1124                     newName16->Des().Delete( pos, deleteLength );
       
  1125     
       
  1126                     HBufC8* newName8 = EscapeUtils::ConvertFromUnicodeToUtf8L( 
       
  1127                         newName16->Des() );
       
  1128                     newName->Des().Copy( newName8->Des() );
       
  1129                     delete newName8;
       
  1130                     }
       
  1131                                         
       
  1132                 // Create 16-bit TBuf for comparison and convert its digit type
       
  1133                 TBuf<KMaxSIPProviderNameLength + KMaxDuplicatePostfixLength> 
       
  1134                     newName16Buf;
       
  1135                 newName16Buf.Copy(newName16->Des()); 
       
  1136                 AknTextUtils::ConvertDigitsTo(newName16Buf, EDigitTypeWestern);                
       
  1137                 CleanupStack::PopAndDestroy( newName16 );
       
  1138                 
       
  1139                 for( TInt m = 0; m < count && !found; m++ )
       
  1140                     {
       
  1141                     TUint32 mid( KErrNone );
       
  1142                     iProfiles->At( m )->GetParameter( KSIPProfileId, mid );
       
  1143         
       
  1144                     iProfiles->At( m )->GetParameter( 
       
  1145                         KSIPProviderName, existingName );
       
  1146                                     
       
  1147                     // Convert the digit type again in existingNameBuf
       
  1148                     HBufC* temporaryExistingName16 = 
       
  1149                         EscapeUtils::ConvertToUnicodeFromUtf8L( *existingName );
       
  1150                     CleanupStack::PushL( temporaryExistingName16 );
       
  1151                     existingNameBuf.Copy(temporaryExistingName16->Des()); 
       
  1152                     CleanupStack::PopAndDestroy( temporaryExistingName16 );   
       
  1153                     AknTextUtils::ConvertDigitsTo(existingNameBuf, EDigitTypeWestern);
       
  1154  
       
  1155                     if( mid != aProfileId && 
       
  1156                         !existingNameBuf.Compare( newName16Buf ) )
       
  1157                         {
       
  1158                         found = ETrue;
       
  1159                         }                      
       
  1160                     }
       
  1161                     
       
  1162                 if( !found )
       
  1163                     {
       
  1164                     isUnique = ETrue;
       
  1165                     }
       
  1166                 }
       
  1167             }
       
  1168         }    
       
  1169 
       
  1170     HBufC* tempName = 
       
  1171         EscapeUtils::ConvertToUnicodeFromUtf8L( newName->Des() );
       
  1172     CleanupStack::PushL( tempName );    
       
  1173     
       
  1174     if (aCreatedFromExistingProfile)
       
  1175         {
       
  1176         // Convert numbers to language specific numbers.
       
  1177         TPtr ptr = tempName->Des();
       
  1178         AknTextUtils::LanguageSpecificNumberConversion( ptr ); 
       
  1179         }
       
  1180   
       
  1181     aName.Copy( tempName->Des() );
       
  1182     CleanupStack::PopAndDestroy( tempName );  
       
  1183        
       
  1184     CleanupStack::PopAndDestroy( newName );
       
  1185     CleanupStack::PopAndDestroy( oldName ); 
       
  1186     __GSLOGSTRING("CGSSIPModel::CheckDuplicateNameL End" )
       
  1187     }
       
  1188 
       
  1189 // -----------------------------------------------------------------------------
       
  1190 // CGSSIPModel::CheckProfileForDeleteL
       
  1191 // Checks if profile can be deleted.
       
  1192 // -----------------------------------------------------------------------------
       
  1193 //    
       
  1194 void CGSSIPModel::CheckProfileForDeleteL( 
       
  1195             TInt aIndex, TBool& aIsUse, TBool& aDefault, TBool& aLocked )
       
  1196     {
       
  1197     __GSLOGSTRING("CGSSIPModel::CheckProfileForDeleteL Start" )
       
  1198     CSIPManagedProfile* profile = iProfiles->At( aIndex );    
       
  1199     
       
  1200     if ( profile )
       
  1201         {
       
  1202         profile->GetParameter( KSIPProfileRegistered, aIsUse );
       
  1203         profile->GetParameter( KSIPDefaultProfile, aDefault );
       
  1204         profile->GetParameter( KSIPProfileLocked, aLocked );
       
  1205         } 
       
  1206     __GSLOGSTRING("CGSSIPModel::CheckProfileForDeleteL End" )
       
  1207     }
       
  1208 
       
  1209 // -----------------------------------------------------------------------------
       
  1210 // CGSSIPModel::CheckIsProfileInUseL
       
  1211 // Checks if profile is used.
       
  1212 // -----------------------------------------------------------------------------
       
  1213 //    
       
  1214 TBool CGSSIPModel::CheckIsProfileInUseL( TUint32 aProfileId )
       
  1215     {
       
  1216     __GSLOGSTRING("CGSSIPModel::CheckIsProfileInUseL" )
       
  1217     TBool inUse( EFalse );
       
  1218     CSIPManagedProfile* profile = Profile( aProfileId );
       
  1219     
       
  1220     if ( profile )
       
  1221         {
       
  1222         profile->GetParameter( KSIPProfileRegistered, inUse );
       
  1223         }
       
  1224     
       
  1225     return inUse;    
       
  1226     }
       
  1227 
       
  1228 //  End of File  
       
  1229