mmshplugins/mmshsettingsuiplugin/src/mussipprofilemodel.cpp
changeset 0 f0cf47e981f9
child 2 b31261fd4e04
equal deleted inserted replaced
-1:000000000000 0:f0cf47e981f9
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Model for managing SIP profiles.
       
    15 *  Version     : %version: 14 % << Don't touch! Updated by Synergy at check-out.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include "mussipprofilemodel.h"
       
    22 #include "muslogger.h"
       
    23 #include "mussettings.inl"
       
    24 #include <aknnotewrappers.h>
       
    25 #include <StringLoader.h>
       
    26 #include <aknview.h>
       
    27 #include <sipmanagedprofileregistry.h>
       
    28 #include <sipprofileregistry.h>
       
    29 
       
    30 
       
    31 const TInt KSIPGranularity   = 5;
       
    32 const TInt KUnknownProfileId = 0;
       
    33 
       
    34 /** 
       
    35 * VSC Enable/Disable is of type TBool
       
    36 */
       
    37 const TUint32 KMuSVSCDisable = 200;
       
    38 
       
    39 
       
    40 // ======== MEMBER FUNCTIONS ========
       
    41 
       
    42 
       
    43 CMusSIPProfileModel::CMusSIPProfileModel()
       
    44     {
       
    45     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::CMusSIPProfileModel()" )
       
    46     }
       
    47 
       
    48 
       
    49 void CMusSIPProfileModel::ConstructL()
       
    50     {
       
    51     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::ConstructL()" )
       
    52     iEngine = CSIPManagedProfileRegistry::NewL( *this );
       
    53     ReadArrayFromEngineL();
       
    54     }
       
    55 
       
    56 
       
    57 CMusSIPProfileModel* CMusSIPProfileModel::NewL()
       
    58     {
       
    59     MUS_LOG( "[MUSSET] -> CMusSIPProfileModel::NewL()" )
       
    60     CMusSIPProfileModel* self = new( ELeave ) CMusSIPProfileModel();
       
    61 
       
    62     CleanupStack::PushL( self );
       
    63     self->ConstructL();
       
    64     CleanupStack::Pop( self );
       
    65 
       
    66     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::NewL()" )
       
    67     return self;
       
    68     }
       
    69 
       
    70 
       
    71 CMusSIPProfileModel::~CMusSIPProfileModel()
       
    72     {
       
    73     MUS_LOG( "[MUSSET] -> CMusSIPProfileModel::~CMusSIPProfileModel()" )
       
    74     DeleteProfiles();
       
    75     delete iEngine;
       
    76     iEngine = NULL;
       
    77     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::~CMusSIPProfileModel()" )
       
    78     }
       
    79 
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // Returns pointer to the locally cached SIP profile array.
       
    83 // ----------------------------------------------------------------------------
       
    84 //
       
    85 CArrayPtr<CSIPManagedProfile>* CMusSIPProfileModel::ProfileArrayL()
       
    86     {
       
    87     MUS_LOG( "[MUSSET] -> CMusSIPProfileModel::ProfileArrayL()" )
       
    88     ReadProfileListFromEngineSafeL();
       
    89     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::ProfileArrayL()" )
       
    90     return iProfiles;
       
    91     }
       
    92 
       
    93 
       
    94 // ----------------------------------------------------------------------------
       
    95 // Returns index to the default SIP profile in locally cached array.
       
    96 // ----------------------------------------------------------------------------
       
    97 //
       
    98 TInt CMusSIPProfileModel::DefaultProfileIndex()
       
    99     {
       
   100     MUS_LOG( "[MUSSET] -> CMusSIPProfileModel::DefaultProfileIndex()" )
       
   101     TInt index( KErrNotFound );
       
   102 
       
   103     for ( TInt i = 0; i < iProfiles->Count(); i++ )
       
   104         {
       
   105         TBool defProfile( EFalse );
       
   106         if (
       
   107     	    KErrNone ==
       
   108     	    iProfiles->At( i )->GetParameter( KSIPDefaultProfile, defProfile )
       
   109     	    && defProfile )
       
   110             {
       
   111             index = i;
       
   112             break;
       
   113             }
       
   114         }
       
   115 
       
   116     MUS_LOG1( "[MUSSET] <- CMusSIPProfileModel::DefaultProfileIndex()( %d )",
       
   117               index )
       
   118     return index;
       
   119     }
       
   120 
       
   121 
       
   122 // ----------------------------------------------------------------------------
       
   123 // Returns id of the default SIP profile. If default SIP profile is not found
       
   124 // KUnknownProfileId is returned.
       
   125 // ----------------------------------------------------------------------------
       
   126 //
       
   127 TUint32 CMusSIPProfileModel::DefaultProfileId()
       
   128     {
       
   129     MUS_LOG( "[MUSSET] -> CMusSIPProfileModel::DefaultProfileId()" )
       
   130     TUint32 id( KUnknownProfileId );
       
   131 
       
   132     for ( TInt i = 0; i < iProfiles->Count(); i++ )
       
   133         {
       
   134         TBool defProfile( EFalse );
       
   135         if (
       
   136     	    KErrNone ==
       
   137     	    iProfiles->At( i )->GetParameter( KSIPDefaultProfile, defProfile )
       
   138     	    && defProfile )
       
   139             {
       
   140         	iProfiles->At( i )->GetParameter( KSIPProfileId, id );
       
   141         	break;
       
   142             }
       
   143         }
       
   144 
       
   145     MUS_LOG1( "[MUSSET] <- CMusSIPProfileModel::DefaultProfileId()( %d )",
       
   146               id )
       
   147     return id;
       
   148     }
       
   149 // -----------------------------------------------------------------------------
       
   150 // Disable SIP Registration.
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 void CMusSIPProfileModel::DisableProfileL()
       
   154     {
       
   155     MUS_LOG( "[MUSSET]  -> CMusSIPProfileModel::DisableProfileL " )        
       
   156     CSIPManagedProfile* managedProfile = static_cast<CSIPManagedProfile*>(
       
   157             iEngine->ProfileL( DefaultProfileId()) );
       
   158     CleanupStack::PushL( managedProfile );
       
   159     //Disable registration from profile.dat file
       
   160     MUS_LOG( "Add registration parameter profile.dat file " )
       
   161     TInt err = managedProfile->SetParameter( KMuSVSCDisable, (TBool)ETrue );
       
   162     User::LeaveIfError(err);
       
   163     iEngine->SaveL( *managedProfile );
       
   164     CleanupStack::PopAndDestroy( managedProfile );
       
   165     MUS_LOG( "[MUSSET]  <- CMusAvaRegisterAvailability::DisableProfileL " )        
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // Enable SIP Registration.
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CMusSIPProfileModel::EnableProfileL()
       
   173     {
       
   174     MUS_LOG( "[MUSSET]  -> CMusSIPProfileModel::EnableProfileL " )        
       
   175     CSIPManagedProfile* managedProfile = static_cast<CSIPManagedProfile*>(
       
   176             iEngine->ProfileL( DefaultProfileId() ) );
       
   177     CleanupStack::PushL( managedProfile );
       
   178     //Disable registration from profile.dat file
       
   179     MUS_LOG( "Add registration parameter profile.dat file " )
       
   180     TInt err = managedProfile->SetParameter( KMuSVSCDisable, (TBool)EFalse );
       
   181     User::LeaveIfError(err);
       
   182     iEngine->SaveL( *managedProfile );
       
   183     CleanupStack::PopAndDestroy( managedProfile );
       
   184     MUS_LOG( "[MUSSET]  <- CMusAvaRegisterAvailability::EnableRegisterL " )        
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // if SIP Registration enabled.
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 TBool CMusSIPProfileModel::ProfileEnabledL()
       
   192     {
       
   193     MUS_LOG( "[MUSSET]  -> CMusSIPProfileModel::ProfileEnabledL " ) 
       
   194     CSIPManagedProfile* managedProfile = static_cast<CSIPManagedProfile*>(
       
   195             iEngine->ProfileL( DefaultProfileId() ) );
       
   196     CleanupStack::PushL( managedProfile );
       
   197     //Activation status from profile.dat file
       
   198     TBool enabled = EFalse;
       
   199     MUS_LOG( "Get registration parameter from profile.dat file " )
       
   200     TInt err = managedProfile->GetParameter( KMuSVSCDisable, enabled ); 
       
   201     MUS_LOG1( "KMuSVSCDisable value %d )",
       
   202             enabled )
       
   203     CleanupStack::PopAndDestroy( managedProfile );
       
   204     MUS_LOG( "[MUSSET]  <- CMusAvaRegisterAvailability::ProfileEnabledL " )
       
   205     return enabled;
       
   206     }
       
   207 // ----------------------------------------------------------------------------
       
   208 // Returns index of the default SIP profile on locally cached array. If default
       
   209 // SIP profile is not found, KErrNotFound is returned.
       
   210 // ----------------------------------------------------------------------------
       
   211 //
       
   212 TInt CMusSIPProfileModel::ProfileIndexByIdL( TUint32 aId )
       
   213     {
       
   214     MUS_LOG( "[MUSSET] -> CMusSIPProfileModel::ProfileIndexByIdL()" )
       
   215     TInt index( KErrNotFound );
       
   216 
       
   217 	ReadProfileListFromEngineSafeL();
       
   218     for ( TInt i = 0; i < iProfiles->Count(); i++ )
       
   219         {
       
   220         TUint32 id( KUnknownProfileId );
       
   221 
       
   222         if ( KErrNone == iProfiles->At( i )->GetParameter( KSIPProfileId, id )
       
   223             && id == aId )
       
   224             {
       
   225             index = i;
       
   226             break;
       
   227             }
       
   228         }
       
   229 
       
   230     MUS_LOG1( "[MUSSET] <- CMusSIPProfileModel::ProfileIndexByIdL()( %d )",
       
   231               index )
       
   232     return index;
       
   233     }
       
   234 
       
   235 
       
   236 // ----------------------------------------------------------------------------
       
   237 // Returns profile ID by index.
       
   238 // ----------------------------------------------------------------------------
       
   239 //
       
   240 TUint32 CMusSIPProfileModel::ProfileIdByIndex( TUint aIndex )
       
   241     {
       
   242     MUS_LOG1( "[MUSSET] -> CMusSIPProfileModel::ProfileIdByIndex()( %d )",
       
   243               aIndex )
       
   244 	TUint32 profileId( KUnknownProfileId );
       
   245     if ( iProfiles->Count() > aIndex )
       
   246         {
       
   247         if ( iProfiles->At( aIndex )->GetParameter(
       
   248             KSIPProfileId, profileId ) )
       
   249             {
       
   250             profileId = KUnknownProfileId;
       
   251             }
       
   252         }
       
   253 
       
   254     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::ProfileIdByIndex()" )
       
   255     return profileId;
       
   256     }
       
   257 
       
   258 
       
   259 // ----------------------------------------------------------------------------
       
   260 // Reads SIP profiles from SIP Profile Client array to locally cached array.
       
   261 // ----------------------------------------------------------------------------
       
   262 //
       
   263 void CMusSIPProfileModel::ReadArrayFromEngineL()
       
   264     {
       
   265     MUS_LOG( "[MUSSET] -> CMusSIPProfileModel::ReadArrayFromEngineL()" )
       
   266 
       
   267     DeleteProfiles();
       
   268 
       
   269     // Create the profile pointer array
       
   270     iProfiles =
       
   271         new ( ELeave ) CArrayPtrFlat<CSIPManagedProfile>( KSIPGranularity );
       
   272 
       
   273     RPointerArray<CSIPProfile> profilePointerArray;
       
   274     TCleanupItem clItem( ResetAndDestroy, &profilePointerArray );
       
   275     CleanupStack::PushL( clItem );
       
   276 
       
   277     iEngine->ProfilesL( profilePointerArray );
       
   278     for ( TInt i = 0; i < profilePointerArray.Count(); i++ )
       
   279         {
       
   280         iProfiles->AppendL(
       
   281             static_cast<CSIPManagedProfile*>( profilePointerArray[i] ) );
       
   282         }
       
   283 
       
   284     profilePointerArray.Reset();
       
   285     CleanupStack::PopAndDestroy(); // clItem (profilePointerArray)
       
   286 
       
   287     SortProfilesL();
       
   288 
       
   289     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::ReadArrayFromEngineL()" )
       
   290     }
       
   291 
       
   292 
       
   293 // ----------------------------------------------------------------------------
       
   294 // Sorts internal array of SIP profiles by id. Used algorithm is generally
       
   295 // slower than quicksort and selectionsort but very feasible for expected
       
   296 // amount of items to be sorted and complexity vise.
       
   297 // ----------------------------------------------------------------------------
       
   298 //
       
   299 void CMusSIPProfileModel::SortProfilesL()
       
   300     {
       
   301 	TUint32 profileIdFirst( 0 );
       
   302 	TUint32 profileIdSecond( 0 );
       
   303     for ( TInt a = 0; a < iProfiles->Count() - 1; a++ )
       
   304         {
       
   305         for ( TInt b = a + 1; b < iProfiles->Count(); b++ )
       
   306             {
       
   307             User::LeaveIfError( iProfiles->At( a )->GetParameter(
       
   308             	KSIPProfileId, profileIdFirst ) );
       
   309     		User::LeaveIfError( iProfiles->At( b )->GetParameter(
       
   310     			KSIPProfileId, profileIdSecond ) );
       
   311             if ( profileIdFirst > profileIdSecond )
       
   312                 {
       
   313                 CSIPManagedProfile* tmp = iProfiles->At( b );
       
   314                 iProfiles->At( b ) = iProfiles->At( a );
       
   315                 iProfiles->At( a )  = tmp;
       
   316                 }
       
   317             }
       
   318         }
       
   319     }
       
   320 
       
   321 
       
   322 // ----------------------------------------------------------------------------
       
   323 // Deletes internally cached SIP profiles.
       
   324 // ----------------------------------------------------------------------------
       
   325 //
       
   326 void CMusSIPProfileModel::DeleteProfiles()
       
   327     {
       
   328     if ( iProfiles )
       
   329         {
       
   330         iProfiles->ResetAndDestroy();
       
   331         delete iProfiles;
       
   332         iProfiles = NULL;
       
   333         }
       
   334     }
       
   335 
       
   336 
       
   337 // ----------------------------------------------------------------------------
       
   338 // From class MSIPProfileRegistryObserver.
       
   339 // Notifies of an event in SIP profile registry.
       
   340 // ----------------------------------------------------------------------------
       
   341 //
       
   342 void CMusSIPProfileModel::ProfileRegistryEventOccurred(
       
   343     TUint32 /*aSIPProfileId*/, TEvent /*aEvent*/ )
       
   344 	{
       
   345     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::ProfileRegistryEventOccurred()" )
       
   346 	}
       
   347 
       
   348 
       
   349 // ----------------------------------------------------------------------------
       
   350 // From class MSIPProfileRegistryObserver.
       
   351 // An asynchronous error has occurred related to SIP profile
       
   352 // ----------------------------------------------------------------------------
       
   353 //
       
   354 void CMusSIPProfileModel::ProfileRegistryErrorOccurred(
       
   355 	TUint32 /*aSIPProfileId*/,
       
   356 	TInt /*aError*/ )
       
   357     {
       
   358     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::ProfileRegistryErrorOccurred()" )
       
   359     }
       
   360 
       
   361 
       
   362 // ----------------------------------------------------------------------------
       
   363 // Reads SIP profile list from engine; if reading fails, old cached list is
       
   364 // kept and returned.
       
   365 // ----------------------------------------------------------------------------
       
   366 //
       
   367 void CMusSIPProfileModel::ReadProfileListFromEngineSafeL()
       
   368     {
       
   369     MUS_LOG(
       
   370     	"[MUSSET] -> CMusSIPProfileModel::ReadProfileListFromEngineSafeL()" )
       
   371     // To prevent complete disaster it we'll save the pointer of the old array
       
   372     CArrayPtrFlat<CSIPManagedProfile>* profiles = iProfiles;
       
   373     iProfiles = 0;
       
   374     TRAPD( error, ReadArrayFromEngineL() );
       
   375 
       
   376     if ( error )
       
   377         {
       
   378         // Problems with re-reading profiles; use existing array
       
   379         DeleteProfiles();
       
   380         iProfiles = profiles;
       
   381         User::Leave( error );
       
   382         }
       
   383     else
       
   384         {
       
   385         // No problems; delete backup array
       
   386         profiles->ResetAndDestroy();
       
   387         delete profiles;
       
   388         }
       
   389     MUS_LOG(
       
   390     	"[MUSSET] <- CMusSIPProfileModel::ReadProfileListFromEngineSafeL()" )
       
   391     }
       
   392 
       
   393 
       
   394 // ----------------------------------------------------------------------------
       
   395 // For deleting RPointerArray in case of leave (used in association with
       
   396 // TCleanupItem).
       
   397 // ----------------------------------------------------------------------------
       
   398 //
       
   399 void CMusSIPProfileModel::ResetAndDestroy( TAny* aPointerArray )
       
   400     {
       
   401     MUS_LOG( "[MUSSET] -> CMusSIPProfileModel::ResetAndDestroy()" )
       
   402     RPointerArray<CSIPProfile>* array =
       
   403         static_cast<RPointerArray<CSIPProfile>*>( aPointerArray );
       
   404     array->ResetAndDestroy();
       
   405     array->Close();
       
   406     MUS_LOG( "[MUSSET] <- CMusSIPProfileModel::ResetAndDestroy()" )
       
   407     }