simpleengine/siputils/src/simplesipprofileobserver.cpp
changeset 0 c8caa15ef882
child 12 e6a66db4e9d0
equal deleted inserted replaced
-1:000000000000 0:c8caa15ef882
       
     1 /*
       
     2 * Copyright (c) 2006 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:    sip connection
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <sip.h>
       
    23 #include <sipconnection.h>
       
    24 #include <sipconnectionobserver.h>
       
    25 #include <sipresponseelements.h>
       
    26 #include <sipclienttransaction.h>
       
    27 #include <sipprofile.h>
       
    28 #include <sipprofileregistry.h>
       
    29 #include "simplesipprofileobserver.h"
       
    30 
       
    31 #ifdef _DEBUG
       
    32 #include "simpledebugutils.h"
       
    33 #endif
       
    34 
       
    35 
       
    36 // ================= MEMBER FUNCTIONS =======================
       
    37 //
       
    38 
       
    39 // ----------------------------------------------------------
       
    40 // CSimpleSipProfileObserver::CSimpleSipProfileObserver
       
    41 // ----------------------------------------------------------
       
    42 //
       
    43 CSimpleSipProfileObserver::CSimpleSipProfileObserver( CSimpleSipConnectionObserver& aObs )
       
    44 : iObs(aObs)
       
    45     {
       
    46     }
       
    47 
       
    48 // ----------------------------------------------------------
       
    49 // CSimpleSipProfileObserver::~CSimpleSipProfileObserver
       
    50 // ----------------------------------------------------------
       
    51 //
       
    52 CSimpleSipProfileObserver::~CSimpleSipProfileObserver()
       
    53     {
       
    54 #ifdef _DEBUG
       
    55     TSimpleLogger::Log(_L("SipProfileObserver: DESTRUCTOR start" ));
       
    56 #endif
       
    57     if ( iProfile && iRegistry )
       
    58         {
       
    59         iRegistry->Disable( *iProfile );
       
    60         }
       
    61     delete iProfile;
       
    62     delete iRegistry;
       
    63 #ifdef _DEBUG
       
    64     TSimpleLogger::Log(_L("SipProfileObserver: DESTRUCTOR end" ));
       
    65 #endif
       
    66     }
       
    67 
       
    68 // ----------------------------------------------------------
       
    69 // CSimpleSipProfileObserver::NewL
       
    70 // ----------------------------------------------------------
       
    71 //
       
    72 CSimpleSipProfileObserver* CSimpleSipProfileObserver::NewL(
       
    73     CSIP* aSIP,
       
    74     CSimpleSipConnectionObserver& aObs )
       
    75     {
       
    76 #ifdef _DEBUG
       
    77     TSimpleLogger::Log(_L("SipProfileObserver: NewL" ));
       
    78 #endif
       
    79     CSimpleSipProfileObserver* self = new (ELeave) CSimpleSipProfileObserver( aObs );
       
    80     CleanupStack::PushL( self );
       
    81     self->ConstructL( aSIP );
       
    82     CleanupStack::Pop( self );
       
    83     return self;
       
    84     }
       
    85 
       
    86 // ----------------------------------------------------------
       
    87 // CSimpleSipProfileObserver::ConstructL
       
    88 // ----------------------------------------------------------
       
    89 //
       
    90 void CSimpleSipProfileObserver::ConstructL( CSIP* aSIP )
       
    91     {
       
    92     iRegistry = CSIPProfileRegistry::NewL( *aSIP, *this );
       
    93     }
       
    94 
       
    95 // ----------------------------------------------------------
       
    96 // CSimpleSipProfileObserver::RegisterDefaultProfileL
       
    97 // ----------------------------------------------------------
       
    98 //
       
    99 void CSimpleSipProfileObserver::RegisterDefaultProfileL( )
       
   100     {
       
   101 #ifdef _DEBUG
       
   102     TSimpleLogger::Log(_L("SipProfileObserver: RegisterDefaultProfileL" ));
       
   103 #endif
       
   104     if ( iProfile )
       
   105         {
       
   106         User::Leave( KErrAlreadyExists );
       
   107         }
       
   108 
       
   109     // Get the default profile.
       
   110     iProfile = iRegistry->DefaultProfileL();
       
   111 
       
   112     // Safety check that DefaultProfileL() didn't return NULL pointer.
       
   113     if ( !iProfile )
       
   114         {
       
   115         User::Leave( KErrNotFound );
       
   116         }
       
   117     if ( !iRegistry->IsEnabled( *iProfile ))
       
   118         {
       
   119         // Ask Profile API to enable the retrieved profile for our use.
       
   120         iRegistry->EnableL( *iProfile, iObs );
       
   121         }
       
   122     }
       
   123 
       
   124 // ----------------------------------------------------------
       
   125 // CSimpleSipProfileObserver::RegisterGivenProfileL
       
   126 // ----------------------------------------------------------
       
   127 //
       
   128 void CSimpleSipProfileObserver::RegisterGivenProfileL( TUint32 aID )
       
   129     {
       
   130 #ifdef _DEBUG
       
   131     TSimpleLogger::Log(_L("SipProfileObserver: RegisterGivenProfileL id=%d" ), aID);
       
   132 #endif
       
   133 
       
   134     RPointerArray<CSIPProfile> profiles;
       
   135 
       
   136     if ( iProfile )
       
   137         {
       
   138         User::Leave( KErrAlreadyExists );
       
   139         }
       
   140 
       
   141     // Search the profiles until the proper one is found.
       
   142     iRegistry->ProfilesL( profiles );
       
   143 
       
   144     TInt count = profiles.Count();
       
   145     TUint32 val = 0;
       
   146     CSIPProfile* sippro = NULL;
       
   147     for ( TInt i = 0; i < count; i++ )
       
   148         {
       
   149         sippro = profiles[i];
       
   150         TInt err = sippro->GetParameter( KSIPProfileId, val );
       
   151 #ifdef _DEBUG
       
   152         TSimpleLogger::Log(_L("SipProfileObserver: Profile id=%d" ), val);
       
   153 #endif
       
   154         if ( !err && val == aID)
       
   155             {
       
   156             // The profile matches for the given search criteria
       
   157             iProfile = sippro;
       
   158             }
       
   159         else
       
   160             {
       
   161             // delete unnecessary profile entity,
       
   162             // the ownership was transferred to us.
       
   163             delete sippro;
       
   164             }
       
   165         }
       
   166 
       
   167     // reset array, unnecessary profiles are already deleted
       
   168     profiles.Reset();
       
   169 
       
   170     // Safety check that DefaultProfileL() didn't return NULL pointer.
       
   171     if ( !iProfile )
       
   172         {
       
   173         User::Leave( KErrNotFound );
       
   174         }
       
   175     if ( !iRegistry->IsEnabled( *iProfile ))
       
   176         {
       
   177         // Ask Profile API to enable the retrieved profile for our use.
       
   178         iRegistry->EnableL( *iProfile, iObs );
       
   179         }
       
   180 
       
   181     }
       
   182 
       
   183 // ----------------------------------------------------------
       
   184 // CSimpleSipProfileObserver::GiveConnectionL
       
   185 // ----------------------------------------------------------
       
   186 //
       
   187 CSIPConnection* CSimpleSipProfileObserver::GiveConnectionL()
       
   188     {
       
   189 #ifdef _DEBUG
       
   190     TSimpleLogger::Log(_L("SipProfileObserver: GiveConnectionL" ));
       
   191 #endif
       
   192     // Check if the SIP connection is already enabled or should we
       
   193     // wait it for.
       
   194     TBool val( EFalse );
       
   195     TInt err = iProfile->GetParameter( KSIPProfileRegistered, val );
       
   196     User::LeaveIfError( err ); 
       
   197     // The pameter val indicates if the profile can be immediately used
       
   198     if ( val )
       
   199         {
       
   200         // get the SIP connection used by the profile
       
   201         return iRegistry->ConnectionL( *iProfile );
       
   202         }
       
   203     else
       
   204         {
       
   205         User::Leave( KErrNotReady );
       
   206         }
       
   207     return (CSIPConnection*) NULL;
       
   208     }
       
   209     
       
   210 // ----------------------------------------------------------
       
   211 // CSimpleSipProfileObserver::IsProfileActive
       
   212 // ----------------------------------------------------------
       
   213 //
       
   214 TBool CSimpleSipProfileObserver::IsProfileActive()
       
   215     {
       
   216     // Check if the SIP profile is active
       
   217     // wait it for.
       
   218     TBool val( EFalse );
       
   219     // Profile is created in ConstructL, so it exists.
       
   220     if ( iProfile )
       
   221         {                
       
   222         iProfile->GetParameter( KSIPProfileRegistered, val );
       
   223         }
       
   224     return val;
       
   225     }    
       
   226 
       
   227 // ----------------------------------------------------------
       
   228 // CSimpleSipProfileObserver::GiveUserAorL
       
   229 // ----------------------------------------------------------
       
   230 //
       
   231 TPtrC8 CSimpleSipProfileObserver::GiveUserAorL()
       
   232     {
       
   233 #ifdef _DEBUG
       
   234     TSimpleLogger::Log(_L("SipProfileObserver: GiveUserAorL" ));
       
   235 #endif   
       
   236     // Check first if registered
       
   237     TBool val( EFalse );
       
   238     TInt err = iProfile->GetParameter( KSIPProfileRegistered, val );
       
   239     User::LeaveIfError( err );    
       
   240     if ( !val)
       
   241         {
       
   242         User::Leave( KErrNotReady );
       
   243         }
       
   244     // Get the first element in the array         
       
   245     const MDesC8Array* aors = 0;
       
   246     err=  iProfile->GetParameter( KSIPRegisteredAors, aors );    
       
   247     User::LeaveIfError(err);    
       
   248     if ( !aors || aors->MdcaCount() == 0 )
       
   249         {
       
   250         User::Leave( KErrNotReady );
       
   251         }
       
   252     return aors->MdcaPoint(0);
       
   253     }
       
   254 
       
   255 // ----------------------------------------------------------
       
   256 // CSimpleSipProfileObserver::ProfileRegistryEventOccurred
       
   257 // ----------------------------------------------------------
       
   258 //
       
   259 void CSimpleSipProfileObserver::ProfileRegistryEventOccurred(
       
   260     TUint32 /*aProfileId*/, TEvent aEvent)
       
   261     {
       
   262 #ifdef _DEBUG
       
   263     TSimpleLogger::Log(_L("SipProfileObserver: ProfileRegistryEventOccurred event=%d" ), aEvent );
       
   264 #endif
       
   265     if ( aEvent == EProfileRegistered )
       
   266         {
       
   267         iObs.ProfileStateChanged( CSIPConnection::EActive, KErrNone );
       
   268         }
       
   269     else if ( aEvent == EProfileDeregistered )
       
   270         {
       
   271         iObs.ProfileStateChanged( CSIPConnection::EInactive, KErrNone );
       
   272         }     
       
   273     else if ( aEvent == EProfileUpdated )
       
   274         {
       
   275 #ifdef _DEBUG
       
   276         TSimpleLogger::Log(_L("SipProfileObserver: EProfileUpdated - ProfileEnabled : %d" ), iRegistry->IsEnabled( *iProfile ) );
       
   277         TSimpleLogger::Log(_L("SipProfileObserver: EProfileUpdated - Profile reg er : %d" ), iRegistry->LastRegistrationError( *iProfile ) );
       
   278         TSimpleLogger::Log(_L("SipProfileObserver: EProfileUpdated - IsContextActive : %d" ), iProfile->IsContextActive() );
       
   279 #endif
       
   280 
       
   281         // Notify observer to refresh SIP connection.
       
   282         iObs.ProfileUpdated();
       
   283         }           
       
   284     else if ( aEvent == EProfileDestroyed )
       
   285         {      
       
   286         iObs.ProfileStateChanged( CSIPConnection::EUnavailable, KErrNone );
       
   287         }
       
   288   }
       
   289 
       
   290 // ----------------------------------------------------------
       
   291 // CSimpleSipProfileObserver::ProfileRegistryErrorOccurred
       
   292 // ----------------------------------------------------------
       
   293 //
       
   294 void CSimpleSipProfileObserver::ProfileRegistryErrorOccurred(
       
   295     TUint32 /*aProfileId*/, TInt aError)
       
   296     {
       
   297 #ifdef _DEBUG
       
   298     TSimpleLogger::Log(_L("SipProfileObserver: ProfileRegistryErrorOccurred" ));
       
   299 #endif
       
   300     iObs.ProfileStateChanged( CSIPConnection::EUnavailable, aError );
       
   301     }
       
   302 
       
   303 // ----------------------------------------------------------
       
   304 // CSimpleSipProfileObserver::ProfileContext
       
   305 // ----------------------------------------------------------
       
   306 //
       
   307 MSIPRegistrationContext* CSimpleSipProfileObserver::ProfileContext()
       
   308     {
       
   309     return iProfile;
       
   310     }