simpledatamodeladapter/src/presencepluginsession.cpp
branchRCL_3
changeset 17 2669f8761a99
parent 16 2580314736af
child 18 fbd2e7cec7ef
equal deleted inserted replaced
16:2580314736af 17:2669f8761a99
     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:  IETF SIMPLE Protocol implementation for XIMP Framework
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <ximpserviceinfo.h> //For MXIMPServiceInfo
       
    21 #include <ximpprotocolconnectionhost.h> //Handle command
       
    22 #include <msimpleconnectionobserver.h>//Connection callback methods
       
    23 #include <simplefactory.h> //create instances to abstract class
       
    24 #include <msimpleconnection.h> //Part of Registration API from SIMPLE engine
       
    25 #include <msimpleconnectionobserver.h>
       
    26 #include <pressettingsapi.h> //for TPresSettingsSet
       
    27 #include <XdmSettingsApi.h>
       
    28 #include <spsettings.h>
       
    29 #include <spentry.h>
       
    30 #include <spproperty.h>
       
    31 #include <spdefinitions.h>
       
    32 
       
    33 #include "presencepluginxdmutils.h"
       
    34 #include "presencepluginsession.h"
       
    35 #include "mpresencepluginconnectionobs.h" 
       
    36 #include "presencepluginauthorization.h"
       
    37 #include "presenceplugindata.h"
       
    38 
       
    39 // ======== MEMBER FUNCTIONS ========
       
    40      
       
    41 // ---------------------------------------------------------------------------
       
    42 // CPresencePluginSession::CPresencePluginSession()
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CPresencePluginSession::CPresencePluginSession( 
       
    46     MPresencePluginConnectionObs& aObs )
       
    47     : CActive( CActive::EPriorityStandard ),iObs(aObs),
       
    48     iType( MPresencePluginConnectionObs::ENoReq ),iSipConnected( EFalse ),
       
    49     iXdmConnected( EFalse ), iXdmLocalMode( EFalse ), iOperation( ENoOperation ),
       
    50     iServiceId( KErrNotFound )
       
    51     {
       
    52     CActiveScheduler::Add(this);
       
    53     }
       
    54   
       
    55 // ---------------------------------------------------------------------------
       
    56 // CPresencePluginSession::ConstructL()
       
    57 // ---------------------------------------------------------------------------
       
    58 //  
       
    59 void CPresencePluginSession::ConstructL( const MXIMPServiceInfo& aService )
       
    60     {
       
    61     DP_SDA2("CPresencePluginSession::ConstructL: service id: %d", 
       
    62         aService.IapId() );
       
    63 
       
    64     CSPSettings* spSettings = CSPSettings::NewL();
       
    65     CleanupStack::PushL( spSettings );
       
    66     
       
    67     CSPProperty* property = CSPProperty::NewLC();
       
    68     spSettings->FindPropertyL( aService.IapId(),
       
    69                                 ESubPropertyPresenceSettingsId,
       
    70                                 *property );    
       
    71     if ( property )
       
    72         {
       
    73         property->GetValue( iPresSettingId );
       
    74         }
       
    75         
       
    76     CleanupStack::PopAndDestroy( property );        
       
    77     CleanupStack::PopAndDestroy( spSettings );
       
    78 
       
    79     DP_SDA2("PluginSession::ConstructL iPresSettingId %d",iPresSettingId );
       
    80     __ASSERT_ALWAYS( iPresSettingId > 0, User::Leave( KErrArgument ) );
       
    81     iServiceId = aService.IapId();
       
    82     
       
    83     //Create connection
       
    84     iConnection = TSimpleFactory::NewConnectionL( *this, aService.IapId() );
       
    85     
       
    86     //Check here is xdm settings ok
       
    87     CheckXDMSettingsL( aService.IapId() );
       
    88     
       
    89     DP_SDA("CPresencePluginSession::ConstructL end");
       
    90     }
       
    91     
       
    92 // ---------------------------------------------------------------------------
       
    93 // CPresencePluginSession::NewL()
       
    94 // ---------------------------------------------------------------------------
       
    95 //  
       
    96 CPresencePluginSession* CPresencePluginSession::NewL( 
       
    97     const MXIMPServiceInfo& aService,
       
    98     MPresencePluginConnectionObs& aObs )
       
    99     {
       
   100     DP_SDA("CPresencePluginSession::NewL");
       
   101     CPresencePluginSession* self =
       
   102         new( ELeave ) CPresencePluginSession( aObs );
       
   103     CleanupStack::PushL( self );
       
   104     self->ConstructL( aService );
       
   105     CleanupStack::Pop( self );
       
   106     return self;
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // CPresencePluginSession::~CPresencePluginSession()
       
   111 // ---------------------------------------------------------------------------
       
   112 //  
       
   113 CPresencePluginSession::~CPresencePluginSession()
       
   114     {
       
   115     DP_SDA("CPresencePluginSession::~CPresencePluginSession");
       
   116 
       
   117     delete iUserId8;
       
   118     delete iDomain;
       
   119     if ( iConnection )
       
   120         {
       
   121         iConnection->Close();        
       
   122         }    
       
   123     
       
   124     DP_SDA("~CPresencePluginSession iXdmUtils");
       
   125     delete iXdmUtils;
       
   126     iXdmUtils = NULL;
       
   127     DP_SDA("CPresencePluginSession::~CPresencePluginSession end");
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // CPresencePluginSession::SimpleConnection()
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 TInt32 CPresencePluginSession::GetPresenceSetId( ) const
       
   135     {
       
   136     return iPresSettingId;            
       
   137     }
       
   138         
       
   139 // ---------------------------------------------------------------------------
       
   140 // CPresencePluginSession::SimpleConnection()
       
   141 // ---------------------------------------------------------------------------
       
   142 //
       
   143 MSimpleConnection* CPresencePluginSession::SimpleConnection( )
       
   144     {
       
   145     return iConnection;            
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // CPresencePluginSession::XdmSettingsId()
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 TInt CPresencePluginSession::XdmSettingsId( )
       
   153     {
       
   154     DP_SDA("CPresencePluginSession::XdmSettingsId");
       
   155     return iXdmSetting;            
       
   156     } 
       
   157 
       
   158 // ---------------------------------------------------------------------------
       
   159 // CPresencePluginSession::CurrentDomain()
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 TPtrC16 CPresencePluginSession::CurrentDomain( )
       
   163     {
       
   164     return iDomain ? iDomain->Des() : TPtrC16();
       
   165     }    
       
   166 
       
   167 // ---------------------------------------------------------------------------
       
   168 // CPresencePluginSession::IsXdmLocalmode()
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 TBool CPresencePluginSession::IsXdmLocalmode()
       
   172 	{
       
   173 	DP_SDA("CPresencePluginSession::IsXdmLocalmode");
       
   174 	return iXdmLocalMode;  
       
   175 	}
       
   176 // ---------------------------------------------------------------------------
       
   177 // CPresencePluginSession::OpenSessionL()
       
   178 // ---------------------------------------------------------------------------
       
   179 //
       
   180 void CPresencePluginSession::OpenSessionL()
       
   181     {
       
   182     DP_SDA("CPresencePluginSession::OpenSessionL");
       
   183     //Check connection status
       
   184     MSimpleConnection::TSimpleState connectionStatus =
       
   185     	iConnection->ConnectionStatus();
       
   186     DP_SDA2("OpenSessionL Connection Status %d", connectionStatus);
       
   187     
       
   188     if(  connectionStatus == MSimpleConnection::EInactive )
       
   189         {
       
   190         DP_SDA("CPresencePluginSession::OpenSessionL Not connected yet"); 
       
   191         iSipConnected = EFalse;
       
   192         // SIP register using by give presence setting id
       
   193         iOpId = iConnection->LoginL( iPresSettingId );
       
   194         iType = MPresencePluginConnectionObs::EOpenSess;
       
   195         }
       
   196     else if ( connectionStatus == MSimpleConnection::EActive )
       
   197         {
       
   198         DP_SDA(":OpenSessionL already connected complete");
       
   199         iSipConnected = ETrue;
       
   200         iType = MPresencePluginConnectionObs::EOpenSess;
       
   201         RequestCompleteL( iOpId, KErrNone );
       
   202         }
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // CPresencePluginSession::ConnectionStatusL()
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 void CPresencePluginSession::ConnectionStatusL( 
       
   210     MSimpleConnection::TSimpleState aState )
       
   211     {
       
   212     DP_SDA2("CPresencePluginSession::ConnectionStatusL aState %d", aState );
       
   213         
       
   214     if ( (aState == MSimpleConnection::EInactive ) 
       
   215         && iSipConnected )
       
   216         {
       
   217         DP_SDA("CPresencePluginSession::ConnectionStatusL EInactive");
       
   218         
       
   219         DP_SDA("CPresencePluginSession::ConnectionStatusL clear pres cache");
       
   220         iObs.InternalPresenceAuthorization().PluginData().RemoveCacheL();
       
   221         DP_SDA("CPresencePluginSession::ConnectionStatusL clear cache ok");
       
   222         
       
   223         // cancel all XDM request
       
   224   		iObs.XdmUtilsL()->Cancel();
       
   225   		//Set connection Terminated, terminate will be remove all client binds
       
   226   		iObs.TerminateConnectionL();
       
   227         iSipConnected = EFalse;
       
   228         iXdmConnected = EFalse;
       
   229         }
       
   230     else if( ( aState == MSimpleConnection::EUnavailable && iSipConnected ) )
       
   231 		{
       
   232 		//Its seems that SIP connection is unavaible 
       
   233 		DP_SDA("CPresencePluginSession::ConnectionStatusL EUnavailable");
       
   234 		}
       
   235 
       
   236     }
       
   237     
       
   238 // ---------------------------------------------------------------------------
       
   239 // CPresencePluginSession::CloseConnection()
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void CPresencePluginSession::CloseConnection()
       
   243     {
       
   244     DP_SDA("CPresencePluginSession::CloseConnection");
       
   245     if( iConnection )
       
   246         {
       
   247         DP_SDA("CPresencePluginSession::CloseConnection close called");
       
   248         iConnection->Close();
       
   249         iConnection = NULL;
       
   250         }
       
   251     DP_SDA("CPresencePluginSession::CloseConnection end");
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // CPresencePluginSession::ConnectionStatus()
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 TBool CPresencePluginSession::ConnectionStatus()
       
   259     {
       
   260     DP_SDA("CPresencePluginSession::ConnectionStatus");
       
   261     TBool connection = EFalse;
       
   262     if( iXdmConnected && iSipConnected )
       
   263     	{
       
   264     	connection = ETrue;
       
   265     	}
       
   266     return connection;
       
   267     }
       
   268         
       
   269 // ---------------------------------------------------------------------------
       
   270 // CPresencePluginSession::RequestCompleteL()
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 void CPresencePluginSession::RequestCompleteL( TInt /*aOpId*/, TInt aStatus )
       
   274     {
       
   275     DP_SDA("CPresencePluginSession::RequestCompleteL");
       
   276     MPresencePluginConnectionObs::TReqType current = iType;
       
   277      
       
   278     if ( !aStatus  )
       
   279        {
       
   280        DP_SDA("CPresencePluginSession::RequestCompleteL SIP session is open");
       
   281        iSipConnected = ETrue;
       
   282        if( iXdmConnected )
       
   283     	   {
       
   284     	   DP_SDA("CPresencePluginSession::RequestCompleteL xdm IS OK");
       
   285     	   iType = MPresencePluginConnectionObs::ENoReq;
       
   286     	   iObs.CompleteReq( current, aStatus );
       
   287     	   }
       
   288        else
       
   289     	   {
       
   290     	   DP_SDA("CPresencePluginSession::RequestCompleteL Initialize XDM");
       
   291     	   __ASSERT_DEBUG( !IsActive(), User::Leave( KErrNotReady ) );
       
   292 	       //Lets test XDM connection before complete
       
   293 		   iXdmUtils = CPresencePluginXdmUtils::NewL( 
       
   294 					iObs, iXdmSetting, iXdmLocalMode );
       
   295 		   
       
   296 		   iOperation =  EInitializeXdm;
       
   297 		   iXdmUtils->InitializeXdmL( iStatus );
       
   298 		   SetActive();
       
   299     	   }
       
   300 	   
       
   301        }
       
   302     DP_SDA("CPresencePluginSession::RequestCompleteL END");
       
   303     } 
       
   304         
       
   305 // ---------------------------------------------------------------------------
       
   306 // CPresencePluginSession::CurrentSipPresentity()
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 TPtrC8 CPresencePluginSession::CurrentSipPresentity()
       
   310     {
       
   311     DP_SDA("CPresencePluginSession::CurrentSipPresentity");
       
   312     // Get from Simple engine
       
   313     delete iUserId8;
       
   314     iUserId8 = NULL;
       
   315     TRAPD(err, iUserId8 = iConnection->CurrentSIPIdentityL().AllocL() )
       
   316     if ( err == KErrNone )
       
   317         {
       
   318         return iUserId8 ? iUserId8->Des() : TPtrC8();
       
   319         }
       
   320     else
       
   321         {
       
   322         return iUserId8 ? KNullDesC8() : TPtrC8();
       
   323         }
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------------------------
       
   327 // CPresencePluginSession::CheckXDMSettings()
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 void CPresencePluginSession::CheckXDMSettingsL( TUint aServiceId )
       
   331 	{
       
   332 	//Check if xdm settings not valid or there is text http://localhost
       
   333 	//in settings. If not valid or text found client will be save resourcelist
       
   334 	//to in phonememory
       
   335     
       
   336 	TPresSettingsSet mySet;    
       
   337     User::LeaveIfError( 
       
   338             PresSettingsApi::SettingsSetL( iPresSettingId, mySet ));
       
   339     iXdmSetting = mySet.iXDMSetting;
       
   340     if( iDomain )
       
   341         {
       
   342         delete iDomain;
       
   343         iDomain = NULL;
       
   344         }
       
   345     iDomain = mySet.iDomainSyntax.AllocL(); 
       
   346     
       
   347     DP_SDA(" -> CheckXDMSettings - store domain to service table");
       
   348     // Service table 
       
   349     CSPSettings* spsettings = CSPSettings::NewLC();
       
   350     CSPProperty* property = CSPProperty::NewLC();
       
   351     
       
   352     DP_SDA(" -> CheckXDMSettings - spsettings and property created");
       
   353     User::LeaveIfError( property->SetName( ESubPropertyPresenceAddrScheme ) );
       
   354     DP_SDA(" -> CheckXDMSettings - property name set");
       
   355     
       
   356     TInt atPosInDomain = iDomain->Locate( '@' );
       
   357     DP_SDA2(" -> CheckXDMSettings - @ position in domain: %d", atPosInDomain );
       
   358     if ( KErrNotFound == atPosInDomain )
       
   359         {
       
   360         DP_SDA(" -> CheckXDMSettings - no @ in domain, set");
       
   361         property->SetValue( iDomain->Des() );
       
   362         }
       
   363     else
       
   364         {
       
   365         DP_SDA(" -> CheckXDMSettings - @ found in domain, remove");
       
   366         HBufC* formattedDomain = HBufC::NewLC( iDomain->Length() );    
       
   367         TPtr formattedDomainPtr( formattedDomain->Des() );
       
   368         formattedDomainPtr.Copy( iDomain->Mid( ( atPosInDomain + 1 ) ) );
       
   369         property->SetValue( formattedDomainPtr );
       
   370         CleanupStack::PopAndDestroy( formattedDomain );
       
   371         }
       
   372     DP_SDA(" -> CheckXDMSettings - property value set");
       
   373     User::LeaveIfError( 
       
   374         spsettings->AddOrUpdatePropertyL( aServiceId, *property ) );
       
   375     DP_SDA(" -> CheckXDMSettings - property added or updated");
       
   376     
       
   377     CleanupStack::PopAndDestroy( property );
       
   378     CleanupStack::PopAndDestroy( spsettings );    
       
   379 
       
   380 #ifdef _DEBUG    
       
   381     DP_SDA2("XdmSettingsId xdmSetting id %d",iXdmSetting );    
       
   382     DP_SDA2("CheckXDMSettings::XdmSettingsId domain %S",iDomain );   
       
   383 #endif
       
   384     
       
   385     //Continue check, add localhost to buf2
       
   386     HBufC* buf2 = KLocalhost().AllocLC();
       
   387     
       
   388 	//Get Uri from XDM settings 
       
   389 	HBufC* xcaproot = NULL;
       
   390 	
       
   391 	TRAPD( error, xcaproot =
       
   392 		TXdmSettingsApi::PropertyL( iXdmSetting, EXdmPropUri ) );
       
   393 	DP_SDA2("CheckXDMSettings Set localmode, err %d", error);
       
   394 
       
   395 	if( KErrNotFound != error && xcaproot )
       
   396 		{
       
   397 		//Compare buf2 to xcaproot
       
   398 		if ( !xcaproot->Compare( buf2->Des() ) )
       
   399         	{
       
   400         	DP_SDA("CheckXDMSettings Set localmode");
       
   401         	iXdmLocalMode = ETrue;
       
   402         	}
       
   403         else
       
   404         	{
       
   405         	DP_SDA("CheckXDMSettings Set Normal Mode");
       
   406         	iXdmLocalMode = EFalse;
       
   407         	}
       
   408 		
       
   409 		DP_SDA("CheckXDMSettings PopAndDestroy buf2");
       
   410 		}
       
   411 	else
       
   412 		{
       
   413 		//It seems there is no valid XCAP settings, so we try localmode
       
   414 		//Shuld we create automaticaly XCAP localhost settings?
       
   415 		iXdmLocalMode = ETrue;
       
   416 		}
       
   417 	
       
   418 	DP_SDA("CheckXDMSettings PopAndDestroy xcaproot");
       
   419 	CleanupStack::PopAndDestroy( buf2 );
       
   420 	delete xcaproot;
       
   421 	}
       
   422 
       
   423 // ---------------------------------------------------------------------------
       
   424 // CPresencePluginSession::XdmUtilsL()
       
   425 // ---------------------------------------------------------------------------
       
   426 //
       
   427 CPresencePluginXdmUtils* CPresencePluginSession::XdmUtilsL()
       
   428 	{
       
   429 	if ( !iXdmUtils )
       
   430 		{
       
   431 		DP_SDA("CPresencePluginSession::XdmUtilsL !iXdmUtils");                
       
   432 		iXdmUtils = CPresencePluginXdmUtils::NewL( 
       
   433 				iObs, iXdmSetting, iXdmLocalMode );
       
   434 		}
       
   435 	
       
   436 	return iXdmUtils;
       
   437 	}
       
   438 
       
   439 // ---------------------------------------------------------------------------
       
   440 // CPresencePluginSession::ServiceId()
       
   441 // ---------------------------------------------------------------------------
       
   442 //
       
   443 TInt& CPresencePluginSession::ServiceId()
       
   444     {
       
   445     return iServiceId;
       
   446     }
       
   447 
       
   448 // ---------------------------------------------------------------------------
       
   449 // CPresencePluginSession::DoCancel()
       
   450 // ---------------------------------------------------------------------------
       
   451 //
       
   452 void CPresencePluginSession::DoCancel(  )
       
   453     {
       
   454     DP_SDA("CPresencePluginSession::DoCancel CANCEL");
       
   455     iXdmUtils->Cancel();
       
   456     }
       
   457 
       
   458 // ---------------------------------------------------------------------------
       
   459 // CPresencePluginSession::RunL()
       
   460 // ---------------------------------------------------------------------------
       
   461 //
       
   462 void CPresencePluginSession::RunL(  )
       
   463     {
       
   464     DP_SDA("CPresencePluginSession::RunL");
       
   465     TInt status = iStatus.Int();
       
   466     DP_SDA2("CPresencePluginSession::RunL status %d", status );
       
   467     
       
   468     if(  ( iOperation == EInitializeXdm ) && !status )
       
   469     	{
       
   470     	DP_SDA("CPresencePluginSession::RequestCompleteL connection ready");
       
   471     	iXdmConnected = ETrue;
       
   472         MPresencePluginConnectionObs::TReqType current = iType;
       
   473         iType = MPresencePluginConnectionObs::ENoReq;
       
   474         iObs.CompleteReq( current, status );
       
   475     	}
       
   476     else
       
   477     	{
       
   478     	DP_SDA("Connection FAIL terminated");
       
   479     	iXdmConnected = EFalse;
       
   480     	iSipConnected = EFalse;
       
   481     	iType = MPresencePluginConnectionObs::ECloseSess;
       
   482     	iObs.CompleteReq( iType, status );
       
   483     	}
       
   484 
       
   485     }
       
   486 
       
   487 // ---------------------------------------------------------------------------
       
   488 // CPresencePluginSession::RunError
       
   489 // ---------------------------------------------------------------------------
       
   490 //
       
   491 TInt CPresencePluginSession::RunError( TInt /*aError*/ )
       
   492     {
       
   493     DP_SDA("CPresencePluginSession::RunError"); 
       
   494     return KErrNone;
       
   495     }
       
   496 
       
   497  // End of file
       
   498