voipplugins/sipconnectionprovider/src/scpservicestorage.cpp
changeset 0 a4daefaec16c
child 12 876a3df1f464
equal deleted inserted replaced
-1:000000000000 0:a4daefaec16c
       
     1 /*
       
     2 * Copyright (c) 2002-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:  Service storage class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "scpservicestorage.h"
       
    20 #include "scplogger.h"
       
    21 #include "scpservice.h"
       
    22 #include "scpsubservice.h"
       
    23 #include "scpsettinghandler.h"
       
    24 
       
    25 const TInt KServiceIdCounterIncrement = 100;
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CScpServiceStorage::CScpServiceStorage
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CScpServiceStorage::CScpServiceStorage( CScpSettingHandler& aSettingHandler ) : 
       
    32     iSettingHandler( aSettingHandler )
       
    33     {
       
    34     }
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CScpServiceStorage::NewL
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CScpServiceStorage* CScpServiceStorage::NewL( CScpSettingHandler& aSettingHandler )
       
    41     {
       
    42     SCPLOGSTRING( "CScpServiceStorage::NewL" );
       
    43 
       
    44     CScpServiceStorage* self = new ( ELeave ) CScpServiceStorage( aSettingHandler );
       
    45     return self;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CScpServiceStorage::~CScpServiceStorage
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CScpServiceStorage::~CScpServiceStorage()
       
    53     {
       
    54     SCPLOGSTRING( "CScpServiceStorage::~CScpServiceStorage" );
       
    55 
       
    56     iServiceItems.ResetAndDestroy();
       
    57     iServiceItems.Close();
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CScpServiceStorage::CreateServiceL
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CScpService& CScpServiceStorage::CreateServiceL( 
       
    65     TInt aServiceId, 
       
    66     CScpProfileHandler& aProfileHandler,
       
    67     MCchServiceObserver& aServiceObserver )
       
    68     {
       
    69     SCPLOGSTRING( "CScpServiceStorage::CreateServiceL" );
       
    70 
       
    71     CScpService* service( NULL );
       
    72     
       
    73     // Check that the service id does not already exist
       
    74     service = GetServiceByServiceId( aServiceId );
       
    75     if( service )
       
    76         {
       
    77         User::Leave( KErrAlreadyExists );
       
    78         }
       
    79 
       
    80     TInt newId = GenerateNewInternalIdForService();
       
    81 
       
    82     service = CScpService::NewL( newId, 
       
    83                                  aServiceId, 
       
    84                                  aProfileHandler,
       
    85                                  *this,
       
    86                                  aServiceObserver );
       
    87 
       
    88     CleanupStack::PushL( service );
       
    89     iServiceItems.AppendL( service );
       
    90     CleanupStack::Pop( service );
       
    91 
       
    92     CScpService* lastService = iServiceItems[ iServiceItems.Count() - 1 ];
       
    93     return *lastService;    
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CScpServiceStorage::GenerateNewServiceId
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 TInt CScpServiceStorage::GenerateNewInternalIdForService()
       
   101     {
       
   102     SCPLOGSTRING( "CScpServiceStorage::GenerateNewInternalIdForService" );
       
   103 
       
   104     iServiceIdCounter += KServiceIdCounterIncrement;
       
   105     
       
   106     return iServiceIdCounter;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CScpServiceStorage::RemoveService
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 TInt CScpServiceStorage::RemoveService( TInt aId )
       
   114     {    
       
   115     SCPLOGSTRING2( "CScpServiceStorage::RemoveService id: %d", aId );
       
   116 
       
   117     for( TInt i=0; i<iServiceItems.Count(); i++)
       
   118         {
       
   119         CScpService* service = iServiceItems[ i ];
       
   120         if( service && aId == service->Id() )
       
   121             {
       
   122             delete service;
       
   123             iServiceItems.Remove( i );
       
   124             return KErrNone;
       
   125             }
       
   126         }
       
   127 
       
   128     return KErrNotFound;      
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CScpServiceStorage::RemoveDisabledServices
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CScpServiceStorage::RemoveDisabledServices()
       
   136     {
       
   137     SCPLOGSTRING( "CScpServiceStorage::RemoveEmptyServices" );
       
   138 
       
   139     RArray< TInt > subServiceIds;
       
   140     RArray< TInt > removedServiceIds;
       
   141 
       
   142     for( TInt i=0; i<iServiceItems.Count(); i++)
       
   143         {
       
   144         CScpService* service = iServiceItems[ i ];
       
   145         if( service )
       
   146             {
       
   147             subServiceIds.Reset();
       
   148                 
       
   149             service->GetSubServiceIds( subServiceIds );
       
   150 
       
   151             TBool remove( ETrue );
       
   152 
       
   153             // Remove if there are no sub services
       
   154             if( subServiceIds.Count() > 0 )
       
   155                 {
       
   156                 for ( TInt j=0; j<subServiceIds.Count(); j++ )
       
   157                     {
       
   158                     CScpSubService* subService = 
       
   159                         service->GetSubService( subServiceIds[ j ] );
       
   160                     
       
   161                     // Don't remove if there is at least one enabled or disconnectin
       
   162                     // sub service
       
   163                     if( subService->EnableRequestedState() == CScpSubService::EScpEnabled ||
       
   164                         subService->State() == ECCHDisconnecting )
       
   165                         {
       
   166                         remove = EFalse;
       
   167                         }
       
   168                     else
       
   169                         {
       
   170                         service->RemoveSubService( subService->Id() );
       
   171                         }
       
   172                     }
       
   173                 }
       
   174 
       
   175             if( remove ) 
       
   176                 {
       
   177                 removedServiceIds.Append( service->Id() ); 
       
   178                 }
       
   179             }
       
   180         }
       
   181 
       
   182     for( TInt i=0; i<removedServiceIds.Count(); i++ )
       
   183         {
       
   184         RemoveService( removedServiceIds[ i ] );
       
   185         }
       
   186 
       
   187     removedServiceIds.Close();
       
   188     subServiceIds.Close();
       
   189     }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CScpServiceStorage::GetService
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 CScpService* CScpServiceStorage::GetService( TInt aId ) const
       
   196     {
       
   197     SCPLOGSTRING2( "CScpServiceStorage::GetService id: %d", aId );
       
   198 
       
   199     for( TInt i=0; i<iServiceItems.Count(); i++)
       
   200         {
       
   201         CScpService* service = iServiceItems[ i ];
       
   202         if( aId == service->Id() )
       
   203             {
       
   204             return service;
       
   205             }
       
   206         }
       
   207     
       
   208     return NULL;    
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CScpServiceStorage::GetServiceByServiceId
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 CScpService* CScpServiceStorage::GetServiceByServiceId( TUint aServiceId ) const
       
   216     {
       
   217     SCPLOGSTRING2( "CScpServiceStorage::GetServiceByServiceId id: %d", aServiceId );
       
   218 
       
   219     for( TInt i=0; i<iServiceItems.Count(); i++)
       
   220         {
       
   221         CScpService* service = iServiceItems[ i ];
       
   222         if( aServiceId == service->ServiceId() )
       
   223             {
       
   224             return service;
       
   225             }
       
   226         }
       
   227     
       
   228     return NULL;
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CScpServiceStorage::GetSubService
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 CScpSubService* CScpServiceStorage::GetSubService( TInt aId ) const
       
   236     {
       
   237     SCPLOGSTRING2( "CScpServiceStorage::GetSubService id: %d", aId );
       
   238 
       
   239     CScpSubService* returnSubService( NULL );
       
   240 
       
   241     RArray<TInt> subServiceIds;
       
   242     for( TInt i=0; i<iServiceItems.Count() && !returnSubService; i++)
       
   243         {
       
   244         CScpService* service = iServiceItems[ i ];
       
   245         
       
   246         subServiceIds.Reset();
       
   247         service->GetSubServiceIds( subServiceIds );
       
   248 
       
   249         for( TInt j=0; j<subServiceIds.Count(); j++)
       
   250             {
       
   251             returnSubService = service->GetSubService( subServiceIds[ j ] );
       
   252 
       
   253             if( returnSubService )
       
   254                 {
       
   255                 if( returnSubService->Id() == aId )
       
   256                     {
       
   257                     break;
       
   258                     }
       
   259                 else
       
   260                     {
       
   261                     returnSubService = NULL;
       
   262                     }
       
   263                 }
       
   264             }
       
   265         }
       
   266 
       
   267     subServiceIds.Close();
       
   268     
       
   269     return returnSubService;
       
   270     }
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CScpServiceStorage::GetServiceIds
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 void CScpServiceStorage::GetServiceIds( RArray<TInt>& aIds ) const
       
   277     {
       
   278     SCPLOGSTRING( "CScpServiceStorage::GetServiceIds" );
       
   279     __ASSERT_DEBUG( aIds.Count() == 0, User::Panic( KNullDesC, KErrGeneral ) );
       
   280 
       
   281     for( TInt i=0; i<iServiceItems.Count(); i++)
       
   282         {
       
   283         CScpService* service = iServiceItems[ i ];
       
   284         aIds.Append( service->Id() );
       
   285         }
       
   286     }
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CScpServiceStorage::GetSubServiceIds
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CScpServiceStorage::GetSubServiceIds( RArray<TInt>& aIds ) const
       
   293     {
       
   294     SCPLOGSTRING( "CScpServiceStorage::GetSubServiceIds" );
       
   295     __ASSERT_DEBUG( aIds.Count() == 0, User::Panic( KNullDesC, KErrGeneral ) );
       
   296 
       
   297     RArray<TInt> subServiceIds;
       
   298 
       
   299     for( TInt i=0; i<iServiceItems.Count(); i++ )
       
   300         {
       
   301         CScpService* service = iServiceItems[ i ];
       
   302         
       
   303         subServiceIds.Reset();
       
   304         service->GetSubServiceIds( subServiceIds );
       
   305 
       
   306         for( TInt j=0; j<subServiceIds.Count(); j++ )
       
   307             {
       
   308             aIds.Append( subServiceIds[ j ] );
       
   309             }
       
   310         }
       
   311 
       
   312     subServiceIds.Close();
       
   313     }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CScpServiceStorage::GetSubServiceIds
       
   317 // -----------------------------------------------------------------------------
       
   318 //
       
   319 void CScpServiceStorage::GetSubServiceIds( TCCHSubserviceType aSubServiceType,
       
   320                                            RArray<TInt>& aIds ) const
       
   321     {
       
   322     SCPLOGSTRING( "CScpServiceStorage::GetSubServiceIds" );
       
   323     __ASSERT_DEBUG( aIds.Count() == 0, User::Panic( KNullDesC, KErrGeneral ) );
       
   324 
       
   325     RArray< TInt > subServiceIds;
       
   326     GetSubServiceIds( subServiceIds );
       
   327     
       
   328     for( TInt i=0; i<subServiceIds.Count(); i++ )
       
   329         {
       
   330         CScpSubService* subService;
       
   331         subService = GetSubService( subServiceIds[ i ] );
       
   332 
       
   333         if( subService && subService->SubServiceType() == aSubServiceType )
       
   334             {
       
   335             aIds.Append( subService->Id() );
       
   336             }
       
   337         }
       
   338     
       
   339     subServiceIds.Close();
       
   340     }
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 // CScpServiceStorage::GetSubServiceIds
       
   344 // -----------------------------------------------------------------------------
       
   345 //
       
   346 void CScpServiceStorage::GetSubServiceIds( TUint32 aProfileId,
       
   347                                            TCCHSubserviceType aSubServiceType,  
       
   348                                            RArray<TInt>& aIds ) const
       
   349     {
       
   350     SCPLOGSTRING3( "CScpServiceStorage::GetSubServiceIds profileId: %d type: %d",
       
   351                    aProfileId, aSubServiceType );
       
   352     __ASSERT_DEBUG( aIds.Count() == 0, User::Panic( KNullDesC, KErrGeneral ) );
       
   353 
       
   354     RArray< TInt > subServiceIds;
       
   355     GetSubServiceIds( subServiceIds );
       
   356     
       
   357     for( TInt i=0; i<subServiceIds.Count(); i++ )
       
   358         {
       
   359         CScpSubService* subService;
       
   360         subService = GetSubService( subServiceIds[ i ] );
       
   361 
       
   362         if( subService &&
       
   363             subService->SipProfileId() == aProfileId &&
       
   364             ( subService->SubServiceType() == aSubServiceType ||
       
   365             aSubServiceType == ECCHUnknown ) )
       
   366             {
       
   367             aIds.Append( subService->Id() );
       
   368             }
       
   369         }
       
   370     
       
   371     subServiceIds.Close();
       
   372     }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CScpServiceStorage::IsSubServiceEnabled
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 TBool CScpServiceStorage::IsSubServiceEnabled( TUint32 aProfileId ) const
       
   379     {
       
   380     SCPLOGSTRING2( "CScpServiceStorage::IsSubServiceEnabled profile id: %d",
       
   381                    aProfileId );
       
   382 
       
   383     TBool result( EFalse );
       
   384     RArray< TInt > subServiceIds;
       
   385 
       
   386     GetSubServiceIds( aProfileId, ECCHUnknown, subServiceIds );
       
   387 
       
   388     for( TInt i=0; i<subServiceIds.Count(); i++ )
       
   389         {
       
   390         CScpSubService* subService = GetSubService( subServiceIds[ i ] );
       
   391         if( subService )
       
   392             {
       
   393             // Find at least one sub service that is still enabled
       
   394             if( subService->EnableRequestedState() == CScpSubService::EScpEnabled )
       
   395                 {
       
   396                 result = ETrue;
       
   397                 break;
       
   398                 }
       
   399             }
       
   400         }
       
   401 
       
   402     subServiceIds.Close();
       
   403     return result;
       
   404     }
       
   405 
       
   406 // -----------------------------------------------------------------------------
       
   407 // CScpServiceStorage::AreAllSubServicesDisconnected
       
   408 // -----------------------------------------------------------------------------
       
   409 //
       
   410 TBool CScpServiceStorage::AreAllSubServicesDisconnected( TUint32 aProfileId ) const
       
   411     {
       
   412     SCPLOGSTRING2( "CScpServiceStorage::AreAllSubServicesDisconnected profile id: %d",
       
   413                    aProfileId );
       
   414 
       
   415     TBool result( ETrue );
       
   416     RArray< TInt > subServiceIds;
       
   417 
       
   418     GetSubServiceIds( aProfileId, ECCHUnknown, subServiceIds );
       
   419 
       
   420     for( TInt i=0; i<subServiceIds.Count(); i++ )
       
   421         {
       
   422         CScpSubService* subService = GetSubService( subServiceIds[ i ] );
       
   423         if( subService )
       
   424             {
       
   425             // Find at least one sub service that has been disabled but
       
   426             // it has not yet disconnected the service
       
   427             if( subService->EnableRequestedState() == CScpSubService::EScpDisabled &&
       
   428                 subService->SubServiceDisconnected() == EFalse )
       
   429                 {
       
   430                 result = EFalse;
       
   431                 break;
       
   432                 }
       
   433             }
       
   434         }
       
   435 
       
   436     subServiceIds.Close();
       
   437     return result;
       
   438     }
       
   439 
       
   440 // -----------------------------------------------------------------------------
       
   441 // CScpServiceStorage::SettingsHandler
       
   442 // -----------------------------------------------------------------------------
       
   443 //
       
   444 CScpSettingHandler& CScpServiceStorage::SettingsHandler()
       
   445     {
       
   446     SCPLOGSTRING( "CScpServiceStorage::SettingsHandler" );
       
   447 
       
   448     return iSettingHandler;
       
   449     }
       
   450 
       
   451 
       
   452 #ifdef _DEBUG
       
   453 // -----------------------------------------------------------------------------
       
   454 // CScpServiceStorage::GetDebugInfo
       
   455 // -----------------------------------------------------------------------------
       
   456 //
       
   457 void CScpServiceStorage::GetDebugInfo( TDes& aInfo ) const
       
   458     {
       
   459     TInt serviceCount = iServiceItems.Count();
       
   460     TInt subServiceCount( 0 );
       
   461     TInt enabledSubServices( 0 );
       
   462 
       
   463     for( TInt i=0; i<serviceCount; i++ )
       
   464         {
       
   465         CScpService* service = iServiceItems[ i ];
       
   466         subServiceCount += service->SubServiceCount();
       
   467     
       
   468         RArray<TInt> subServiceIds;
       
   469         service->GetSubServiceIds( subServiceIds );
       
   470 
       
   471         for( TInt j=0; j<subServiceIds.Count(); j++ )
       
   472             {
       
   473             CScpSubService* subService = service->GetSubService( subServiceIds[ j ] );
       
   474             
       
   475             //if( subService->IsEnabled() )
       
   476             if( subService->EnableRequestedState() == CScpSubService::EScpEnabled )
       
   477                 {
       
   478                 enabledSubServices++;
       
   479                 }
       
   480             }
       
   481 
       
   482         subServiceIds.Close();
       
   483         }
       
   484 
       
   485     TBuf< 255 > buffer;
       
   486     buffer.Format( _L( "\nServices: %d\n SubServices: %d\n Enabled: %d\n" ),
       
   487                   serviceCount, subServiceCount, enabledSubServices ); 
       
   488 
       
   489     aInfo.Append( buffer );
       
   490     }
       
   491 #endif
       
   492 
       
   493 //  End of File  
       
   494