sipproviderplugins/sipprovider/sipstatemachine/src/TransitionEngineMgr.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // CTransitionEngineMgr implementation file.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalComponent
       
    21 */
       
    22 
       
    23 #include "TransitionEngineMgr.h"
       
    24 
       
    25 
       
    26 EXPORT_C CSIPTransitionEngine* CTransitionEngineMgr::FindOrCreateL(TUid aAppUid, TUint32 &aProfileId)
       
    27 /**	Finds an instance of CSIPTransitionEngine that pertains to the given application UID
       
    28 and IAP (Extracted from Profile) or creates a new one if none found. IMPORTANT: When the returned instance
       
    29 is no longer needed, the caller must call Detach(<the_instance_pointer>) on 'this'.
       
    30 
       
    31 @param aAppUid the application UID
       
    32 @param aProfileId 
       
    33 @return a pointer to the CSIPTransitionEngine instance or NULL if allocation problems
       
    34 @exception leaves with KErrNoMemory if memory allocation fails
       
    35 */	
       
    36 	{
       
    37 	CSIPTransitionEngineBundle* theBundle = NULL;
       
    38 	for (int i = 0; i < iTEBundles.Count(); i++ )
       
    39 		{
       
    40 		if (iTEBundles[i]->GetUID() == aAppUid)
       
    41 			{
       
    42 			theBundle = iTEBundles[i];
       
    43 			}
       
    44 		}
       
    45 			
       
    46 	if (NULL == theBundle)
       
    47 		{
       
    48 		theBundle = CSIPTransitionEngineBundle::NewL(aAppUid);
       
    49 		CleanupStack::PushL(theBundle);
       
    50 		iTEBundles.AppendL(theBundle);
       
    51 		CleanupStack::Pop();
       
    52 		}
       
    53 	return theBundle->FindOrCreateL(aProfileId);
       
    54 	}
       
    55 	
       
    56 
       
    57 EXPORT_C void CTransitionEngineMgr::Detach(CSIPTransitionEngine* aTE)
       
    58 //EXPORT_C void CTransitionEngineMgr::Detach(CSIPTransitionEngine* aTE, MSIPRegistrationClient* aRegClient)
       
    59 /**	This method must be called when the instance of CSIPTransitionEngine previously
       
    60 obtained with CTransitionEngineMgr::FindOrCreateL is no longer necessery. The
       
    61 necessary cleanup will be performed here if there are no other clients using
       
    62 the instance.
       
    63 
       
    64 @param aTE the pointer the caller whishes to detach from.
       
    65 */	
       
    66 	{
       
    67 	TInt detached = -1;
       
    68 	for (int i = 0; i < iTEBundles.Count(); i++ )
       
    69 		{
       
    70 		detached = iTEBundles[i]->Detach(aTE);
       
    71 		if (detached == 0)
       
    72 			{
       
    73 			//found and no more users to the found TE.
       
    74 			CSIPTransitionEngineBundle * bundle = iTEBundles[i];
       
    75 			iTEBundles.Remove(i);
       
    76 			delete bundle;
       
    77 			break;			
       
    78 			}
       
    79 		else if (detached > 0)
       
    80 	    	{
       
    81 			//found but still users to the found TE.    		
       
    82 	    	break;
       
    83 	    	}
       
    84 		}
       
    85 		
       
    86     //make sure we actually found and detached from a TE.
       
    87 	ASSERT(detached>=0);
       
    88 	}
       
    89 	
       
    90 EXPORT_C TUint32 CTransitionEngineMgr::DefaultProfileId()
       
    91 	{
       
    92 	//any bundle can find the default profile Id
       
    93 	//so send requiest to the first one
       
    94 	return iTEBundles[0]->DefaultProfileId();
       
    95 	}
       
    96 
       
    97 CSIPTransitionEngineBundle* CSIPTransitionEngineBundle::NewL(TUid aAppUid)
       
    98 	{
       
    99 	CSIPTransitionEngineBundle* theBundle = new (ELeave) CSIPTransitionEngineBundle(aAppUid);
       
   100 	CleanupStack::PushL(theBundle);
       
   101 	theBundle->ConstructL();
       
   102 	CleanupStack::Pop();
       
   103 	return theBundle;
       
   104 	}
       
   105 
       
   106 
       
   107 CSIPTransitionEngine* CSIPTransitionEngineBundle::FindOrCreateL(TUint32 &aProfileId)
       
   108 	{
       
   109 	TUint32 iapId;
       
   110 	CSIPTransitionEngine* theTE = NULL;
       
   111 	CSIPProfile* profile = NULL;
       
   112 	//Retrive Profile using ProfileRegistry
       
   113 	//and extract Iap from Profile
       
   114 	
       
   115 	if (aProfileId == KSIPDefaultProfileId)
       
   116 		{
       
   117 		profile = iProfileRegistry->DefaultProfileL();
       
   118 		}
       
   119 	else
       
   120 		{
       
   121 		profile = iProfileRegistry->ProfileL(aProfileId);	
       
   122 		}
       
   123 	
       
   124 	//the following check added because ProfileL not leaving on error
       
   125 	if (profile != NULL)
       
   126 		{
       
   127 		profile->GetParameter(KSIPAccessPointId, iapId);
       
   128 		delete profile;
       
   129 		}
       
   130 	else
       
   131 		{
       
   132 		User::Leave(KErrNotFound);
       
   133 		}
       
   134 	
       
   135 	for (int i = 0; i < iTEs.Count(); i++ )
       
   136 		{
       
   137 		if (iTEs[i]->IapId() == iapId)
       
   138 			{
       
   139 			theTE = iTEs[i];
       
   140 			}
       
   141 		}
       
   142 		
       
   143 	if (NULL == theTE)
       
   144 		{
       
   145 		theTE = CSIPTransitionEngine::NewL(*iSip, iapId);
       
   146 		CleanupStack::PushL(theTE);
       
   147 		iTEs.AppendL(theTE);
       
   148 		CleanupStack::Pop();
       
   149 		}
       
   150 		
       
   151 	theTE->Attach();
       
   152 	return theTE;
       
   153 
       
   154 	}
       
   155 	
       
   156 void CSIPTransitionEngineBundle::ConstructL()
       
   157 	{
       
   158 	__FLOG_1(_L("CSIPTransitionEngineBundle %08x:\tConstructL, expect 'ConstructL successful', otherwise the method left"), this);	
       
   159 	__FLOG_1(_L("CSIPTransitionEngineBundle %08x:\tinstantiating CSIP, may leave..."), this);	
       
   160 	iSip = CSIP::NewL( iAppUid, *this );
       
   161 	__FLOG_1(_L("CSIPTransitionEngineBundle %08x:\tinstantiating CSIPProfileRegistry, may leave..."), this);
       
   162 	iProfileRegistry = CSIPProfileRegistry::NewL(*iSip, *this);
       
   163 	__FLOG_1(_L("CSIPTransitionEngineBundle %08x:\tConstructL successful"), this);
       
   164 	}
       
   165 
       
   166 //todo
       
   167 TInt CSIPTransitionEngineBundle::Detach(CSIPTransitionEngine* aTE)
       
   168 //TInt CSIPTransitionEngineBundle::Detach(CSIPTransitionEngine* aTE, MSIPRegistrationClient* aRegClient)
       
   169 /**Called when the user of the previously obtained CSIPTransitionEngine
       
   170    no longer needs it.
       
   171 
       
   172 @param aTE the pointer the caller whishes to detach from.
       
   173 @return the number of TEs still held here or -1 if the requested TE not found.
       
   174 If the number of TEs still held here is zero, the caller should destroy 'this'.
       
   175 */
       
   176 	{
       
   177 	TInt detached = -1;
       
   178 	for (int i = 0; i < iTEs.Count(); i++ )
       
   179 		{
       
   180 		if (iTEs[i] == aTE)
       
   181 			{
       
   182 			detached = iTEs[i]->Detach();
       
   183 			if (detached == 0)
       
   184 				{
       
   185 				CSIPTransitionEngine * tE = iTEs[i];
       
   186 				iTEs.Remove(i);
       
   187 				delete tE;
       
   188 				}
       
   189 			break;
       
   190 			}
       
   191 		}
       
   192 	return detached >=0 ? iTEs.Count() : detached;
       
   193 	}
       
   194 
       
   195 CSIPTransitionEngineBundle::~CSIPTransitionEngineBundle()
       
   196 	{
       
   197 		// for safety try deleting TEs
       
   198 	for(TInt count = 0; count < iTEs.Count(); count ++)
       
   199 		{
       
   200 		CSIPTransitionEngine * tE = iTEs[count];
       
   201 		iTEs.Remove(count);
       
   202 		delete tE;
       
   203 		}
       
   204 	iTEs.Close();
       
   205 	
       
   206 	if(iProfileRegistry != NULL)
       
   207 		{
       
   208 		delete iProfileRegistry;
       
   209 		iProfileRegistry = NULL;
       
   210 		}
       
   211 		
       
   212 	delete iSip;
       
   213 	iSip = NULL;
       
   214 	__FLOG_CLOSE;
       
   215 	}
       
   216 	 
       
   217 	 
       
   218 //From MSIPObserver
       
   219 void CSIPTransitionEngineBundle::IncomingRequest( TUint32 /*aIapId*/,
       
   220 							  CSIPServerTransaction* /*aTransaction*/ )
       
   221 	{
       
   222 	}
       
   223 
       
   224 //From MSIPObserver
       
   225 void CSIPTransitionEngineBundle::TimedOut( CSIPServerTransaction& /*aSIPServerTransaction*/ )
       
   226 	{
       
   227 	}
       
   228 
       
   229 //From MSIPProfileRegistryObserver
       
   230 void CSIPTransitionEngineBundle::ProfileRegistryEventOccurred(TUint32 /*aProfileId*/, TEvent /*aEvent*/)
       
   231 	{
       
   232 	}
       
   233 void CSIPTransitionEngineBundle::ProfileRegistryErrorOccurred(TUint32 /*aProfileId*/, TInt /*aError*/)
       
   234 	{
       
   235 	}
       
   236 
       
   237 TUint32 CSIPTransitionEngineBundle::DefaultProfileId()
       
   238 	{
       
   239 	TUint32 profileId = KSIPInvalidProfileId;
       
   240 	CSIPProfile* profile = NULL;
       
   241 	TRAPD(err, profile = iProfileRegistry->DefaultProfileL());
       
   242 	if (err == KErrNone)
       
   243 		{
       
   244 		profile->GetParameter(KSIPProfileId, profileId);
       
   245 		delete profile;
       
   246 		}
       
   247 	return profileId;	
       
   248 	}