IMPSengine/imapi/src/imconnectionimpl.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2004 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: Implementation of the CImConnection interface (imconnection.h)
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "imconnectionimpl.h"
       
    22 #include    "imerrors.h"
       
    23 #include    "imclientimpl.h"
       
    24 #include    "cntdbreader.h"
       
    25 #include    "apiutils.h"
       
    26 #include    <impserrors.h>
       
    27 //#include    <centralrepository.h>
       
    28 #include    <wvengineInternalCRKeys.h>
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CImConnectionImpl::CImConnectionImpl
       
    34 // C++ default constructor can NOT contain any code, that
       
    35 // might leave.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CImConnectionImpl::CImConnectionImpl():
       
    39         iCurrentOpId( 0 ),
       
    40         iState( EImNotLogged )
       
    41     {
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CImConnectionImpl::ConstructL
       
    46 // Symbian 2nd phase constructor can leave.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void CImConnectionImpl::ConstructL( TDesC& aApplicationId )
       
    50     {
       
    51     User::LeaveIfError( iEngine.Connect() );
       
    52     iApplicationId = aApplicationId.AllocL();
       
    53     iContactReader = CContactDBReader::NewL();
       
    54 
       
    55     /*// Read the CSP version from CenRep
       
    56     TReal                   cspVersion; // the CSP version
       
    57     CRepository* repository = CRepository::NewLC(KCRUIDWVEngineVariation);
       
    58     repository->Get(KWVEngineCspVersion, cspVersion);
       
    59     CleanupStack::PopAndDestroy(); // repository
       
    60     if ( cspVersion < 1.2 )
       
    61         {
       
    62         // the CSP version is too low
       
    63         User::Leave(KErrNotSupported);
       
    64         }*/
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CImConnectionImpl::NewL
       
    69 // Two-phased constructor.
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 CImConnectionImpl* CImConnectionImpl::NewL( TDesC& aApplicationId )
       
    73     {
       
    74 #ifdef _DEBUG
       
    75     CImApiLogger::Log( _L( "CImConnectionImpl: NewL %S" ), &aApplicationId );
       
    76 #endif
       
    77     CImConnectionImpl* self = new( ELeave ) CImConnectionImpl();
       
    78 
       
    79     CleanupStack::PushL( self );
       
    80     self->ConstructL( aApplicationId );
       
    81     CleanupStack::Pop( );
       
    82 
       
    83     return self;
       
    84     }
       
    85 
       
    86 
       
    87 // Destructor
       
    88 CImConnectionImpl::~CImConnectionImpl()
       
    89     {
       
    90     delete iApplicationId;
       
    91     delete iContactReader;
       
    92     iEngine.Close();
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CImConnectionImpl::CreateImClientL
       
    97 // Factory function to create CImClient object
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 MImClient* CImConnectionImpl::CreateImClientL()
       
   101     {
       
   102 #ifdef _DEBUG
       
   103     CImApiLogger::Log( _L( "CImConnectionImpl: CreateImClientL" ) );
       
   104 #endif
       
   105 
       
   106     return CImClient::NewL( *this );
       
   107     }
       
   108 
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CImConnectionImpl::RegisterObserverL
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CImConnectionImpl::RegisterObserverL( MImConnectionObserver* aObserver )
       
   115     {
       
   116 #ifdef _DEBUG
       
   117     CImApiLogger::Log( _L( "CImConnectionImpl: RegisterObserverL" ) );
       
   118 #endif
       
   119 
       
   120     if ( iClientObserver )
       
   121         {
       
   122         User::Leave( KImApiErrAlreadyRegistered );
       
   123         }
       
   124 
       
   125 
       
   126     iAccessClient.RegisterL(
       
   127         iEngine,
       
   128         this );
       
   129     iAccessClient.RegisterErrorObserverL( *this );
       
   130     iAccessClient.RegisterStatusObserverL( this );
       
   131 
       
   132     iClientObserver = aObserver;
       
   133 
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CImConnectionImpl::UnregisterObserverL
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 void CImConnectionImpl::UnregisterObserver()
       
   141     {
       
   142 #ifdef _DEBUG
       
   143     CImApiLogger::Log( _L( "CImConnectionImpl: UnregisterObserver" ) );
       
   144 #endif
       
   145     if ( iClientObserver != NULL )
       
   146         {
       
   147         iClientObserver = NULL;
       
   148         iAccessClient.Unregister();
       
   149         TInt err;
       
   150         TRAP( err, iAccessClient.UnregisterErrorObserverL() );
       
   151         TRAP( err, iAccessClient.UnregisterStatusObserverL() );
       
   152         }
       
   153 
       
   154     }
       
   155 
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CImConnectionImpl::LoginL
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CImConnectionImpl::LoginL(
       
   162     const TDesC& aServer,
       
   163     const TDesC& aUserID,
       
   164     const TDesC& aPassword,
       
   165     const TUint32 aAP )
       
   166     {
       
   167 #ifdef _DEBUG
       
   168     CImApiLogger::Log( _L( "CImConnectionImpl: LoginL" ) );
       
   169 #endif
       
   170 
       
   171     switch ( iState )
       
   172         {
       
   173         case EImLoggingIn:
       
   174             User::Leave( KImApiErrLoginInProgress );
       
   175         case EImLoggingOut:
       
   176             User::Leave( KImApiErrLogoutInProgress );
       
   177         case EImCancelingLogin:
       
   178             User::Leave( KImApiErrCancelLoginInProgress );
       
   179         case EImLoggedIn:
       
   180             User::Leave( KImApiErrAlreadyLoggedIn );
       
   181         default: ; // EImNotLogged -> just continue
       
   182         }
       
   183 
       
   184     // check whether registered
       
   185     if ( !iClientObserver )
       
   186         {
       
   187         User::Leave( KImApiErrNotRegistered );
       
   188         }
       
   189 
       
   190     // We need to get the ClientID in from SAP Settings
       
   191     // some servers will not allow logging in without that
       
   192     const TDesC* clientId = NULL;
       
   193     clientId = SSapSettingsReader::ClientIdLC( aServer );
       
   194 
       
   195     iCurrentOpId = iAccessClient.LoginL(
       
   196                        TImpsCspIdentifier( aServer, aUserID ),
       
   197                        aPassword,
       
   198                        clientId ? *clientId : KNullDesC,
       
   199                        aAP,
       
   200                        NULL,
       
   201                        NULL
       
   202                    );
       
   203 
       
   204     if ( clientId != NULL )
       
   205         CleanupStack::PopAndDestroy( 1 ); // clientId
       
   206 
       
   207     SetState( EImLoggingIn );
       
   208 
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CImConnectionImpl::CancelLoginL
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void CImConnectionImpl::CancelLoginL()
       
   216     {
       
   217 #ifdef _DEBUG
       
   218     CImApiLogger::Log( _L( "CImConnectionImpl: CancelLoginL" ) );
       
   219 #endif
       
   220     switch ( iState )
       
   221         {
       
   222         case EImLoggedIn:
       
   223             User::Leave( KImApiErrAlreadyLoggedIn );
       
   224         case EImCancelingLogin:
       
   225             User::Leave( KImApiErrCancelLoginInProgress );
       
   226         case EImLoggingOut:
       
   227             User::Leave( KImApiErrLogoutInProgress );
       
   228         case EImNotLogged:
       
   229             User::Leave( KImApiErrNotLogged );
       
   230         default:; // EImLoggingIn -> just continue
       
   231         }
       
   232 
       
   233     iAccessClient.CancelLoginL( iCurrentOpId );
       
   234     SetState( EImCancelingLogin );
       
   235     }
       
   236 
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CImConnectionImpl::LoginL
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void CImConnectionImpl::LogoutL()
       
   243     {
       
   244     switch ( iState )
       
   245         {
       
   246         case EImLoggingIn:
       
   247             User::Leave( KImApiErrNotLogged );
       
   248         case EImCancelingLogin:
       
   249             User::Leave( KImApiErrCancelLoginInProgress );
       
   250         case EImLoggingOut:
       
   251             User::Leave( KImApiErrLogoutInProgress );
       
   252         case EImNotLogged:
       
   253             User::Leave( KImApiErrNotLogged );
       
   254         default:; // EImLoggedIn -> just continue
       
   255         }
       
   256 
       
   257     iAccessClient.LogoutL();
       
   258     SetState( EImLoggingOut );
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CImConnectionImpl::ImConnectionStatus
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 TImConnectionStatus CImConnectionImpl::ImConnectionStatus()
       
   266     {
       
   267     return ( iState == EImLoggedIn ) ? ELoggedIn : ENotLoggedIn;
       
   268     }
       
   269 // -----------------------------------------------------------------------------
       
   270 // CLoginManager::HandleLoginL
       
   271 //
       
   272 //
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 void CImConnectionImpl::HandleLoginL(
       
   276     TInt /*aId*/,
       
   277     TImpsCspIdentifier& /*aCspId*/ )
       
   278     {
       
   279     // Called when login has finished
       
   280 #ifdef _DEBUG
       
   281     CImApiLogger::Log( _L( "CImConnectionImpl: HandleLoginL" ) );
       
   282 #endif
       
   283     SetState( EImLoggedIn );
       
   284     if ( iClientObserver )
       
   285         {
       
   286         iClientObserver->HandleLoginL( KErrNone );
       
   287         }
       
   288 
       
   289 
       
   290     }
       
   291 
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CLoginManager::HandleLoginCancelL
       
   295 //
       
   296 // -----------------------------------------------------------------------------
       
   297 //
       
   298 void CImConnectionImpl::HandleLoginCancelL(
       
   299     TInt /*aCancelledOpId*/,
       
   300     TImpsCspIdentifier& /*aCspId*/ )
       
   301     {
       
   302 #ifdef _DEBUG
       
   303     CImApiLogger::Log( _L( "CImConnectionImpl: HandleLoginCancelL" ) );
       
   304 #endif
       
   305     SetState( EImNotLogged );
       
   306     if ( iClientObserver )
       
   307         {
       
   308         iClientObserver->HandleCancelLoginL( KErrNone );
       
   309         }
       
   310 
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // CLoginManager::HandleLogoutL
       
   315 //
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 void CImConnectionImpl::HandleLogoutL( TInt /*aId*/,
       
   319                                        TImpsCspIdentifier& /*aCspId*/ )
       
   320     {
       
   321 #ifdef _DEBUG
       
   322     CImApiLogger::Log( _L( "CImConnectionImpl: HandleLogoutL" ) );
       
   323 #endif
       
   324     SetState( EImNotLogged );
       
   325     if ( iClientObserver )
       
   326         {
       
   327         iClientObserver->HandleLogoutL( KErrNone );
       
   328         }
       
   329 
       
   330     }
       
   331 
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CLoginManager::HandleErrorL
       
   335 //
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 void CImConnectionImpl::HandleErrorL( TInt aStatus,
       
   339                                       TInt /*aOpId*/,
       
   340                                       const TDesC* /*aDescription*/,
       
   341                                       const CImpsDetailed* /*aDetailedRes*/,
       
   342                                       TImpsCspIdentifier& /*aCspId*/ )
       
   343     {
       
   344 #ifdef _DEBUG
       
   345     CImApiLogger::Log( _L( "CImConnectionImpl: Error received: %d in state: %d" ), aStatus, iState );
       
   346 #endif
       
   347     if ( iClientObserver )
       
   348         {
       
   349         TInt apiErrorCode = ConvertImpsEngineErrorCode( aStatus );
       
   350         // distribute the error to the correct handler
       
   351         switch ( iState )
       
   352             {
       
   353             case EImLoggingIn:
       
   354                 SetState( EImNotLogged );
       
   355                 iClientObserver->HandleLoginL( apiErrorCode );
       
   356                 break;
       
   357             case EImCancelingLogin:
       
   358                 iClientObserver->HandleCancelLoginL( apiErrorCode );
       
   359                 // here the state change is tricky, it is better to wait the
       
   360                 // status change
       
   361                 break;
       
   362             case EImLoggingOut:
       
   363                 SetState( EImNotLogged );
       
   364                 iClientObserver->HandleLogoutL( apiErrorCode );
       
   365                 break;
       
   366             default:
       
   367                 ;
       
   368             }
       
   369         }
       
   370 
       
   371     }
       
   372 
       
   373 // -----------------------------------------------------------------------------
       
   374 // CLoginManager::HandleStatusChangeL
       
   375 //
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 void CImConnectionImpl::HandleStatusChangeL(
       
   379     TImpsServiceStatus aStatus,
       
   380     TImpsCspIdentifier& /*aCspId*/ )
       
   381     {
       
   382 #ifdef _DEBUG
       
   383     CImApiLogger::Log( _L( "CLoginManager: Status changed: %d" ), aStatus );
       
   384 #endif
       
   385 
       
   386     // change our status according to the engine
       
   387     // this is a good opportunity to synch or state to engine's
       
   388     switch ( aStatus )
       
   389         {
       
   390         case EImps_NOT_LOGGED:
       
   391             SetState( EImNotLogged );
       
   392             break;
       
   393         case EImps_ON_LINE:
       
   394             SetState( EImLoggedIn );
       
   395             break;
       
   396 
       
   397         default:
       
   398             break;
       
   399         }
       
   400     }
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CLoginManager::HandleNbrSessionsL
       
   404 //
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 void CImConnectionImpl::HandleNbrSessionsL( TInt /*aId*/,
       
   408                                             TInt /*aNbr*/,
       
   409                                             TImpsCspIdentifier& /*aCspId*/ )
       
   410     {
       
   411     }
       
   412 
       
   413 // -----------------------------------------------------------------------------
       
   414 // CImConnectionImpl::CreateImStatusFetcherL
       
   415 // Factory function CIMStatusFetcherImpl object
       
   416 // -----------------------------------------------------------------------------
       
   417 //
       
   418 RImpsEng& CImConnectionImpl::ImpsEngine()
       
   419     {
       
   420     return iEngine;
       
   421     }
       
   422 
       
   423 // -----------------------------------------------------------------------------
       
   424 // CImConnectionImpl::ContactDBReader
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 CContactDBReader& CImConnectionImpl::ContactDBReader()
       
   428     {
       
   429     return *iContactReader;
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CImConnectionImpl::ClientId
       
   434 // -----------------------------------------------------------------------------
       
   435 //
       
   436 const TDesC& CImConnectionImpl::ApplicationId()
       
   437     {
       
   438     return *iApplicationId;
       
   439     }
       
   440 
       
   441 // -----------------------------------------------------------------------------
       
   442 // CImConnectionImpl::ConvertImpsEngineErrorCode
       
   443 // Converts the IMPS engine error code to an API error code
       
   444 // -----------------------------------------------------------------------------
       
   445 //
       
   446 TInt CImConnectionImpl::ConvertImpsEngineErrorCode( TInt aErrorCode )
       
   447     {
       
   448     // Do not try to convert KErrNone
       
   449     if ( aErrorCode == KErrNone )
       
   450         {
       
   451         return KErrNone;
       
   452         }
       
   453 
       
   454     TInt returnValue;
       
   455     switch ( aErrorCode )
       
   456         {
       
   457         case KImpsErrorNotLogged:
       
   458             returnValue = KImApiErrNotLogged;
       
   459             break;
       
   460 
       
   461         case Imps_ERROR_BASE-409: // Wrong password
       
   462             returnValue = KImApiErrWrongPassword;
       
   463             break;
       
   464 
       
   465         case Imps_ERROR_BASE-531: // User does not exist
       
   466             returnValue = KImApiErrInvalidUserId;
       
   467             break;
       
   468 
       
   469         case Imps_ERROR_BASE-201: // Partial success
       
   470             returnValue = KImApiErrPartialSuccess;
       
   471             break;
       
   472         default: // KImpsErrorOthersLogged
       
   473             // KImpsErrorShuttingDown,
       
   474             // KImpsErrorBearerSuspended
       
   475             returnValue = KImApiGeneralError;
       
   476         }
       
   477     return returnValue;
       
   478     }
       
   479 
       
   480 
       
   481 // -----------------------------------------------------------------------------
       
   482 // CImConnectionImpl::SetState
       
   483 // Changes the internal state of the object
       
   484 // -----------------------------------------------------------------------------
       
   485 //
       
   486 void CImConnectionImpl::SetState( TConnectionState aNewState )
       
   487     {
       
   488     iState = aNewState;
       
   489     }
       
   490 
       
   491 
       
   492 //  End of File