wvuing/wvcommandutils/Src/CCASessionHandlerCmd.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     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:  Network session handling (login, logout, etc.)
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CCASessionHandlerCmd.h"
       
    22 #include "ChatDebugPrint.h"
       
    23 #include "MCAImpsFundClient.h"
       
    24 #include "CCAEngine.h"
       
    25 #include "mcagroupmanagerinterface.h"
       
    26 #include "MCAContactLists.h"
       
    27 #include "MCAPresence.h"
       
    28 #include "CAPresenceManager.h"
       
    29 #include "MCAStoredContact.h"
       
    30 #include "MCAStoredContacts.h"
       
    31 #include "CCAStorageManagerFactory.h"
       
    32 #include "ImpsCSPAllErrors.h"
       
    33 #include "MCASettings.h"
       
    34 #include "MCAChatInterface.h"
       
    35 #include "MCAContactFetchObserver.h"
       
    36 #include "MCAAccessInterface.h"
       
    37 #include "mcaservicestateobserver.h"
       
    38 
       
    39 #include "MCABackgroundTaskObserver.h"
       
    40 #include "SServerPrefers.h"
       
    41 #include "impsbuilddefinitions.h"
       
    42 
       
    43 #include <bautils.h>
       
    44 #include <ChatNG.rsg>
       
    45 #include <cimpspresenceconnectionuing.h>
       
    46 #include <cimpsconnuiclntfilteredpreseventnotifierng.h>
       
    47 #include <CIMPSSAPSettingsStore.h>
       
    48 #include <CIMPSSAPSettings.h>
       
    49 #include <CIMPSSAPSettingsList.h>
       
    50 #include <CWVSettingsUINGDialog.h>
       
    51 #include <WVSettingsSharedDataNG.h>
       
    52 #include <bldvariant.hrh>
       
    53 #include <centralrepository.h>
       
    54 #include <IMPSServiceSettingsUINGInternalCRKeys.h>
       
    55 #include <aknenv.h>
       
    56 #include <MProfileEngine.h>
       
    57 #include <MProfile.h>
       
    58 #include <Profile.hrh>
       
    59 #include <impspresenceconnectionuiconstsng.h>
       
    60 #include <CPEngNWSessionSlotID2.h>
       
    61 #include <CPEngNWSessionSlot2.h>
       
    62 #include <eikenv.h>
       
    63 #include <e32property.h>
       
    64 #include <StringLoader.h>
       
    65 
       
    66 #include "IMUtils.h"
       
    67 #include "IMDialogUtils.h"
       
    68 #include "IMNoteMapper.h"
       
    69 #include "CCACommandManagerFactory.h"
       
    70 #include "CCACommandManager.h"
       
    71 #include "MCALoginPC.h"
       
    72 #include "MCAProcessManager.h"
       
    73 #include "MCASettingsPC.h"
       
    74 #include "MCAConversationPC.h"
       
    75 #include "MCAUiLoginCmdCB.h"
       
    76 
       
    77 // The Settings have been moved to Cenrep (also retained in the Resource file)
       
    78 // so the enums for keys and central repository header is added here
       
    79 
       
    80 #include 	"VariantKeys.h"
       
    81 
       
    82 
       
    83 // CONSTANTS
       
    84 // defines whether first login to server has been done
       
    85 _LIT( KIMFirstLoginToServer, "IMFirstLoginToServer" );
       
    86 
       
    87 
       
    88 
       
    89 // ============================ MEMBER FUNCTIONS ===============================
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CCASessionHandlerCmd::CCASessionHandlerCmd
       
    93 // C++ default constructor can NOT contain any code, that
       
    94 // might leave.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 CCASessionHandlerCmd::CCASessionHandlerCmd(
       
    98     MCAProcessManager& aProcessManager,
       
    99     MCAUiLoginCmdCB& aUiLoginCmdCB )
       
   100         : iLastState( EIMPSPresenceServiceUnknownEvent ),
       
   101         iPresenceInitializationSuccesfull( EFalse ),
       
   102         iIsContactFetchDone( EFalse ),
       
   103         iProcessManager( aProcessManager ),
       
   104         iUiLoginCmdCB( aUiLoginCmdCB ),
       
   105 
       
   106         iBackgroundTaskRunning( ETrue )
       
   107     {
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CCASessionHandlerCmd::ConstructL
       
   112 // Symbian 2nd phase constructor can leave.
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 void CCASessionHandlerCmd::ConstructL()
       
   116     {
       
   117 
       
   118     iProfileApi = CreateProfileEngineL();
       
   119 
       
   120     iConnUI = CIMPSPresenceConnectionUi::NewL( EIMPSConnClientIM );
       
   121 
       
   122     iConnUIEventNotifier = CIMPSConnUiClntFilteredPresEventNotifier::NewL(
       
   123                                EIMPSConnClientIM );
       
   124     iConnUIEventNotifier->AddObserverL( this );
       
   125 
       
   126     iConnUIEventNotifier->StartL();
       
   127 
       
   128     iUiLoginCmdCB.SetStatusPaneIconsL();
       
   129 
       
   130 
       
   131     MCAPresence* presence = CAPresenceManager::InstanceL();
       
   132 
       
   133 
       
   134     if ( IsLoggedIn() )
       
   135         // If already logged in, just update engine state and fetch contact lists
       
   136         {
       
   137 
       
   138         iBackgroundTaskRunning = EFalse;
       
   139 
       
   140         // we can ignore the returnvalue here,
       
   141         // since the member variable is set inside anyway
       
   142         NetworkSessionSlotIDL();
       
   143         TRAPD( err, presence->InitializePresenceAPIL( *iSessionSlotID ) );
       
   144 
       
   145         // ATM only occasion for this to fail is that PEC has not fully
       
   146         // synchronized contact lists. We will get online event when the
       
   147         // lists are in sync.
       
   148         if ( err == KErrNone )
       
   149             {
       
   150             iPresenceInitializationSuccesfull = ETrue;
       
   151 
       
   152             TBool redraw( GetServicesL() );
       
   153 
       
   154             iUiLoginCmdCB.SetIMPSServices( iImpsServices, ETrue, redraw );
       
   155 
       
   156             // make sure chat has been constructed before notifying
       
   157             // the engine. if we're logged in, IMPS server should be up,
       
   158             // so this is fast
       
   159             iUiLoginCmdCB.FinalizeChatConstructionL();
       
   160 
       
   161             NotifyEngineL( ELoggedIn, iSessionSlotID );
       
   162 
       
   163 
       
   164             iUiLoginCmdCB.CaptureEventsL();
       
   165 
       
   166             // Idle loop for starting default list selection
       
   167 
       
   168             delete iSelectIdle;
       
   169             iSelectIdle = NULL;
       
   170             iSelectIdle = CIdle::NewL( CActive::EPriorityIdle );
       
   171             iSelectIdle->Start( TCallBack( BackgroundSelect, this ) );
       
   172 
       
   173 
       
   174             // set variated settings
       
   175             TRAPD( err, UpdateDefaultSettingsL() );
       
   176             if ( err != KErrNone ) // Signal user about error
       
   177                 {
       
   178                 CActiveScheduler::Current()->Error( err );
       
   179                 }
       
   180 
       
   181 
       
   182             // Store current logged in SAP as previous
       
   183             CIMPSSAPSettings* temp = CurrentSAPLC();
       
   184             iPreviousSAP = temp;    // Take ownership
       
   185             CleanupStack::Pop( temp );
       
   186             }
       
   187         }
       
   188     else
       
   189         {
       
   190 #ifndef CHAT_UNIT_TEST
       
   191         // Idle loop for starting login sequence login
       
   192         iIdle = CIdle::NewL( CActive::EPriorityIdle );
       
   193         iIdle->Start( TCallBack( BackgroundTasks, this ) );
       
   194 #endif
       
   195         }
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CCASessionHandlerCmd::NewL
       
   200 // Two-phased constructor.
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 CCASessionHandlerCmd* CCASessionHandlerCmd::NewL(
       
   204     MCAProcessManager& aProcessManager,
       
   205     MCAUiLoginCmdCB& aUiLoginCmdCB )
       
   206     {
       
   207     CCASessionHandlerCmd* self = new( ELeave ) CCASessionHandlerCmd(
       
   208         aProcessManager,
       
   209         aUiLoginCmdCB );
       
   210     CleanupStack::PushL( self );
       
   211     self->ConstructL();
       
   212     CleanupStack::Pop( self );
       
   213 
       
   214     return self;
       
   215     }
       
   216 
       
   217 
       
   218 // Destructor
       
   219 CCASessionHandlerCmd::~CCASessionHandlerCmd()
       
   220     {
       
   221     if ( iProfileApi )
       
   222         {
       
   223         iProfileApi->Release();
       
   224         }
       
   225 
       
   226     if ( iConnUIEventNotifier )
       
   227         {
       
   228         iConnUIEventNotifier->Stop();
       
   229         iConnUIEventNotifier->RemoveObserver( this );
       
   230         }
       
   231 
       
   232     delete iConnUIEventNotifier;
       
   233     delete iConnUI;
       
   234     delete iIdle;
       
   235     delete iSelectIdle;
       
   236 
       
   237     delete iPreviousSAP;
       
   238     delete iSessionSlotID;
       
   239 
       
   240     iFetchObservers.Close();
       
   241     iServiceStateObservers.Close();
       
   242 
       
   243     }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // CCASessionHandlerCmd::IsLoggedIn
       
   247 // (other items were commented in a header).
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 TBool CCASessionHandlerCmd::IsLoggedIn() const
       
   251     {
       
   252     TBool returnValue( EFalse );
       
   253 
       
   254     if ( IsOfflineProfileOn() )
       
   255         {
       
   256         CHAT_DP( D_CHAT_LIT(
       
   257                      "CCASessionHandlerCmd::IsLoggedIn, offline profile is ON, returning %d" ),
       
   258                  returnValue );
       
   259         return returnValue;
       
   260         }
       
   261 
       
   262 
       
   263     TRAPD( err, returnValue = iConnUI->LoggedInL( EIMPSConnClientIM ) );
       
   264 
       
   265     if ( err )
       
   266         {
       
   267         returnValue = EFalse;
       
   268         }
       
   269 
       
   270     CHAT_DP( D_CHAT_LIT( "CCASessionHandlerCmd::IsLoggedIn returning %d" ),
       
   271              returnValue );
       
   272 
       
   273     // if there are no slots in the system then we get KErrNotFound
       
   274     // and we don't show it to the user
       
   275     if ( ( err != KErrNone ) && ( err != KErrNotFound ) )
       
   276         {
       
   277         CActiveScheduler::Current()->Error( err );
       
   278         }
       
   279     return returnValue;
       
   280     }
       
   281 
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CCASessionHandlerCmd::LoginL
       
   285 // (other items were commented in a header).
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 TBool CCASessionHandlerCmd::LoginL( TBool aAskConfirmation /*= ETrue */,
       
   289                                     TBool aIsStartupLogin /*= EFalse */ )
       
   290     {
       
   291     CHAT_DP_TXT( "CCASessionHandlerCmd::LoginL started" );
       
   292 
       
   293     if ( IsOfflineProfileOn() )
       
   294         {
       
   295         IMDialogUtils::DisplayInformationNoteL( R_QTN_OFFLINE_NOT_POSSIBLE );
       
   296         return EFalse;
       
   297         }
       
   298 
       
   299     if ( IsLoggedIn() )
       
   300         {
       
   301         return ETrue; // already logged in
       
   302         }
       
   303 
       
   304     TInt ret( EAknSoftkeyNo );
       
   305     if ( aAskConfirmation )
       
   306         {
       
   307         TPtrC serverNamePtr( KNullDesC );
       
   308 
       
   309         CIMPSSAPSettings* sap = NULL;
       
   310         if ( iPreviousSAP )
       
   311             {
       
   312 
       
   313             // get current SAP
       
   314             CIMPSSAPSettings* currentSap = CurrentSAPLC();
       
   315             delete iPreviousSAP;
       
   316 
       
   317             // replace the previous SAP
       
   318             iPreviousSAP = currentSap;
       
   319             CleanupStack::Pop( currentSap );
       
   320 
       
   321 
       
   322             // get the last logged in server name
       
   323 
       
   324             serverNamePtr.Set( iPreviousSAP->SAPName() );
       
   325             }
       
   326         else
       
   327             {
       
   328             sap = CurrentSAPLC();
       
   329             serverNamePtr.Set( sap->SAPName() );
       
   330 
       
   331 
       
   332             CleanupStack::PopAndDestroy( sap );
       
   333             }
       
   334         // load the promp from lOC file + server name
       
   335         HBufC* prompt = StringLoader::LoadLC( R_QTN_CHAT_CONNECTION_NEEDED_TO,
       
   336                                               serverNamePtr );
       
   337 
       
   338         ret = IMDialogUtils::DisplayQueryDialogL( R_GENERIC_YES_NO_CONFIRMATION_QUERY,
       
   339                                                   *prompt );
       
   340 
       
   341         CleanupStack::PopAndDestroy( prompt );
       
   342         }
       
   343 
       
   344     if ( ( ret == EAknSoftkeyOk ) || ( ret == EAknSoftkeyYes )
       
   345          || ( !aAskConfirmation ) )
       
   346         {
       
   347         // check if we have valid server data
       
   348         if ( iUiLoginCmdCB.CheckCurrentServerDataL() )
       
   349             {
       
   350             CHAT_DP_TXT(
       
   351                 "PEC is offline, server data is ok -> execute PEC login" );
       
   352 
       
   353             TIMPSLoginType loginType = LoginType(); // default to manual login
       
   354             if ( ( loginType == EIMPSAAConnectionStart ||
       
   355                    loginType == EIMPSApplicationLaunch ) &&
       
   356                  !aIsStartupLogin )
       
   357                 {
       
   358                 //defaulting to manual if login type is automatic
       
   359                 // since in this case we want to see the server selection dialog
       
   360                 loginType = EIMPSManualLogin;
       
   361                 }
       
   362 
       
   363             if ( loginType != EIMPSAAConnectionStart )
       
   364                 {
       
   365                 iLogging = ETrue;
       
   366 
       
   367                 if ( aAskConfirmation )
       
   368                     {
       
   369                     loginType = EIMPSApplicationLaunch;
       
   370                     }
       
   371 
       
   372                 CPEngNWSessionSlotID2* tempID = CPEngNWSessionSlotID2::NewL();
       
   373                 delete iSessionSlotID;
       
   374                 iSessionSlotID = tempID;
       
   375 
       
   376                 iCurrentLoginIsFirst = EFalse;
       
   377                 TInt err = KErrNone;
       
   378 
       
   379                 if ( aAskConfirmation && iPreviousSAP )
       
   380                     {
       
   381                     //  user want to connect to last logged in server
       
   382                     // hence pass the previous sap
       
   383                     // aAskConfirmation will allow to load from sapsetting any more
       
   384                     err = iConnUI->LoginL( EIMPSConnClientIM, loginType,
       
   385                                            *iSessionSlotID, this , iPreviousSAP, aAskConfirmation ) ;
       
   386                     }
       
   387                 else
       
   388                     {
       
   389                     err = iConnUI->LoginL( EIMPSConnClientIM, loginType,
       
   390                                            *iSessionSlotID, this ) ;
       
   391                     }
       
   392 
       
   393                 CHAT_DP( D_CHAT_LIT(
       
   394                              "CCASessionHandlerCmd::LoginL, \
       
   395                             iConnUI->LoginL( EIMPSConnClientIM, %d ) returned %d" ),
       
   396                          loginType, err );
       
   397                 iLogging = EFalse;
       
   398 
       
   399                 if ( err == KErrNone )
       
   400                     {
       
   401 
       
   402                     TBool redraw( GetServicesL() );
       
   403 
       
   404                     iUiLoginCmdCB.SetIMPSServices( iImpsServices, ETrue, redraw );
       
   405 
       
   406                     NotifyEngineL( EServerPrefers );
       
   407 
       
   408                     iUiLoginCmdCB.SetStatusPaneIconsL();
       
   409 
       
   410                     CCAStorageManagerFactory::ContactListInterfaceL()->
       
   411                     ResetContactLists();
       
   412 
       
   413                     CIMPSSAPSettings* sap = CurrentSAPLC();
       
   414 
       
   415                     MCASettingsPC* settingsPC = iProcessManager.GetSettingsInterface();
       
   416                     settingsPC->SetDefaultSapL( sap );
       
   417                     CleanupStack::PopAndDestroy( sap );
       
   418                     }
       
   419 
       
   420                 iUiLoginCmdCB.FinalizeChatConstructionL();
       
   421 
       
   422                 }
       
   423             }
       
   424         }
       
   425 
       
   426     iUiLoginCmdCB.ReleaseCapturingL();
       
   427     return IsLoggedIn(); // Login was perhaps done
       
   428     }
       
   429 
       
   430 // -----------------------------------------------------------------------------
       
   431 // CCASessionHandlerCmd::LogoutL
       
   432 // (other items were commented in a header).
       
   433 // -----------------------------------------------------------------------------
       
   434 //
       
   435 TBool CCASessionHandlerCmd::LogoutL()
       
   436     {
       
   437     CHAT_DP_TXT( "CCASessionHandlerCmd::LogoutL started" );
       
   438     //Check for background tasks completion.
       
   439     //only then give perform logout
       
   440 
       
   441     if ( !IsBackgroundTaskPendingL() )
       
   442         {
       
   443 
       
   444         iProcessManager.GetEngine()->CancelAllRequests();
       
   445 
       
   446         return DoLogoutL();
       
   447         }
       
   448     CHAT_DP_TXT( "CCASessionHandler::LogoutL done" );
       
   449 
       
   450     return IsLoggedIn();
       
   451     }
       
   452 
       
   453 // -----------------------------------------------------------------------------
       
   454 // CCASessionHandlerCmd::LogoutL
       
   455 // (other items were commented in a header).
       
   456 // -----------------------------------------------------------------------------
       
   457 
       
   458 TBool CCASessionHandlerCmd::DoLogoutL()
       
   459     {
       
   460     CHAT_DP_TXT( "CCASessionHandlerCmd::DoLogoutL started" );
       
   461 
       
   462     MCAStoredContacts* contactsInterface =
       
   463         CCAStorageManagerFactory::ContactListInterfaceL();
       
   464 
       
   465     contactsInterface->SetContactListLock( ETrue );
       
   466 
       
   467     iLogging = ETrue;
       
   468     // this should not even be called if there is no session slot ID
       
   469     if ( iSessionSlotID )
       
   470         {
       
   471 
       
   472         iProcessManager.GetLogInInterface()->LogoutL();
       
   473         iConnUI->LogoutL( *iSessionSlotID, this );
       
   474         delete iSessionSlotID;
       
   475         iSessionSlotID = NULL;
       
   476 
       
   477         }
       
   478 
       
   479     iLogging = EFalse;
       
   480     iCurrentLoginIsFirst = EFalse;
       
   481     NotifyEngineL( ELoggingOut );
       
   482 
       
   483 
       
   484     contactsInterface->SetContactListLock( EFalse );
       
   485 
       
   486 
       
   487     CHAT_DP_TXT( "CCASessionHandlerCmd::DoLogoutL done" );
       
   488     return IsLoggedIn();
       
   489     }
       
   490 
       
   491 // ---------------------------------------------------------
       
   492 // CCASessionHandler::HandleSapAccessEventEventL
       
   493 // (other items were commented in a header).
       
   494 // ---------------------------------------------------------
       
   495 //
       
   496 void CCASessionHandlerCmd::HandleSapAccessEventEventL( const TDesC& aServerName,
       
   497                                                        const TDesC& aServerURL,
       
   498                                                        MIMPSConnProcessUi* aConnProcessUi )
       
   499     {
       
   500     iUiLoginCmdCB.HandleSapAccessEventEventL( aServerName, aServerURL,
       
   501                                               aConnProcessUi );
       
   502     }
       
   503 
       
   504 
       
   505 // ---------------------------------------------------------
       
   506 // CCASessionHandlerCmd::IsSupported
       
   507 // (other items were commented in a header).
       
   508 // ---------------------------------------------------------
       
   509 //
       
   510 TBool CCASessionHandlerCmd::IsSupported( TSupportedFeature aFeature )
       
   511     {
       
   512     switch ( aFeature )
       
   513         {
       
   514         case EGroup :
       
   515             {
       
   516             return iImpsServices.iGroupFeat.FeatureSupported();
       
   517             }
       
   518         case ESearch :
       
   519             {
       
   520             return iImpsServices.iFundamentalFeat.FunctionSupported(
       
   521                        KPEngFFSearchFunction );
       
   522             }
       
   523         case EInvite :
       
   524             {
       
   525             return iImpsServices.iFundamentalFeat.FunctionSupported(
       
   526                        KPEngFFInviteFunction );
       
   527             }
       
   528         case EBlock :
       
   529             {
       
   530             return iImpsServices.iIMFeat.FunctionSupported(
       
   531                        KPEngIMAuthorFunctions );
       
   532             }
       
   533         case EAttrList :
       
   534             {
       
   535             return iImpsServices.iPresenceFeat.FunctionSupported(
       
   536                        KPEngWVAttrListFunction );
       
   537             }
       
   538         case ECLIMod:
       
   539             {
       
   540             // contact list creation/deletion is possible only if
       
   541             // both CCLI and DCLI are supported
       
   542             TBool returnValue( ETrue );
       
   543             returnValue &=
       
   544                 iImpsServices.iPresenceFeat.SubFunctionSupported( KPEngWVSubFuncCCLI );
       
   545             returnValue &=
       
   546                 iImpsServices.iPresenceFeat.SubFunctionSupported( KPEngWVSubFuncDCLI );
       
   547             return returnValue;
       
   548             }
       
   549         case EGroupRejectList:
       
   550             {
       
   551             return iImpsServices.iGroupFeat.SubFunctionSupported(
       
   552                        KPEngGRSubFuncREJEC );
       
   553             }
       
   554         default:
       
   555             {
       
   556             return EFalse; // Unknown features are not supported
       
   557             }
       
   558         }
       
   559     }
       
   560 
       
   561 // -----------------------------------------------------------------------------
       
   562 // CCASessionHandlerCmd::HandlePresenceEventL
       
   563 // (other items were commented in a header).
       
   564 // -----------------------------------------------------------------------------
       
   565 //
       
   566 void CCASessionHandlerCmd::HandlePresenceEventL(
       
   567     CIMPSConnUiPresEventNotifier* /*aNotifier*/,
       
   568     const CPEngNWSessionSlotID2& /*aSessionSlotID*/,
       
   569     TIMPSConnectionClient /*aClient*/,
       
   570     TIMPSPresenceServiceEvent aEvent )
       
   571     {
       
   572     CHAT_DP( D_CHAT_LIT(
       
   573                  "CCASessionHandlerCmd::HandlePresenceEventL, TIMPSPresenceServiceEvent: %d" ),
       
   574              aEvent );
       
   575 
       
   576     // Same state as previous, no need for action
       
   577     if ( aEvent == iLastState )
       
   578         {
       
   579         return;
       
   580         }
       
   581 
       
   582     switch ( aEvent )
       
   583         {
       
   584         case EIMPSPresenceServiceUnknownEvent:
       
   585             {
       
   586             CHAT_DP_TXT(
       
   587                 "CCASessionHandlerCmd::HandlePresenceEventL\
       
   588             ->EIMPSPresenceServiceUnknownEvent" );
       
   589             break;
       
   590             }
       
   591         case EIMPSPresenceServiceOffline:
       
   592             {
       
   593             CHAT_DP_TXT(
       
   594                 "CCASessionHandlerCmd::HandlePresenceEventL\
       
   595             ->EIMPSPresenceServiceOffline" );
       
   596             iTemporaryNetworkDown = EFalse;
       
   597 
       
   598             if ( iLastState != EIMPSPresenceServiceForceLogOut )
       
   599                 {
       
   600                 NotifyEngineL( ELoggedOut );
       
   601                 }
       
   602 
       
   603             iAlreadyLoggedOut = ETrue;
       
   604 
       
   605             iProcessManager.GetLogInInterface()->LogoutL();
       
   606 
       
   607             TRAPD( error, iUiLoginCmdCB.SetStatusPaneIconsL() );
       
   608 
       
   609             iAlreadyLoggedOut = EFalse;
       
   610 
       
   611             User::LeaveIfError( error );
       
   612 
       
   613             // nuke all contacts from IM Storage
       
   614             MCAStoredContacts* contacts =
       
   615                 CCAStorageManagerFactory::ContactListInterfaceL();
       
   616             // Update own status item
       
   617             contacts->OwnStatus().SetOnlineStatus( TStorageManagerGlobals::EOffline );
       
   618             // nuke all contacts from IM Storage
       
   619             contacts->RemoveAllContactsL();
       
   620 
       
   621             iUiLoginCmdCB.IMUIPIConForcedClear();
       
   622 
       
   623             break;
       
   624             }
       
   625         case EIMPSPresenceServiceOnline:
       
   626             {
       
   627             CHAT_DP_TXT(
       
   628                 "CCASessionHandlerCmd::HandlePresenceEventL \
       
   629                 ->EIMPSPresenceServiceOnline" );
       
   630 
       
   631             if ( iPresenceInitializationSuccesfull )
       
   632                 {
       
   633                 // Construction is already done, so no need to do it here
       
   634                 // again. If user have launched IM after contact list
       
   635                 // synchronization and before this online event, we might
       
   636                 // initialize IM twice.
       
   637                 break;
       
   638                 }
       
   639 
       
   640             iUiLoginCmdCB.FinalizeChatConstructionL();
       
   641 
       
   642 
       
   643             if ( !iLogging )
       
   644                 {
       
   645                 // this login was not initiated by us
       
   646                 // -> we need to refresh the session slot id
       
   647                 CPEngNWSessionSlotID2* tempId =
       
   648                     iConnUI->GetActiveNWSessionSlotIDL( EIMPSConnClientIM );
       
   649                 delete iSessionSlotID;
       
   650                 iSessionSlotID = tempId;
       
   651 
       
   652                 CIMPSSAPSettings* sap = CurrentSAPLC();
       
   653                 MCASettingsPC* settingsPC = iProcessManager.GetSettingsInterface();
       
   654                 settingsPC->SetDefaultSapL( sap );
       
   655 
       
   656                 CleanupStack::PopAndDestroy( sap );
       
   657                 }
       
   658             if ( !iTemporaryNetworkDown )
       
   659                 {
       
   660 
       
   661                 TBool redraw( GetServicesL() );
       
   662 
       
   663                 CAPresenceManager::InstanceL()->InitializePresenceAPIL(
       
   664                     *iSessionSlotID );
       
   665 
       
   666                 iUiLoginCmdCB.SetIMPSServices( iImpsServices, ETrue, redraw );
       
   667                 NotifyEngineL( ELoggedIn, iSessionSlotID );
       
   668 
       
   669                 iUiLoginCmdCB.SetStatusPaneIconsL();
       
   670 
       
   671                 // Idle loop for starting default list selection
       
   672                 delete iSelectIdle;
       
   673                 iSelectIdle = NULL;
       
   674                 iSelectIdle = CIdle::NewL( CActive::EPriorityIdle );
       
   675                 iSelectIdle->Start( TCallBack( BackgroundSelect, this ) );
       
   676 
       
   677                 // set variated settings
       
   678                 TRAPD( err, UpdateDefaultSettingsL() );
       
   679                 if ( err != KErrNone ) // Signal user about error
       
   680                     {
       
   681                     CActiveScheduler::Current()->Error( err );
       
   682                     }
       
   683 
       
   684                 CIMPSSAPSettings* currentSap = CurrentSAPLC();
       
   685                 // reomoved extra code from here
       
   686 
       
   687                 // take the ownership
       
   688                 delete iPreviousSAP;
       
   689                 iPreviousSAP = currentSap;
       
   690                 CleanupStack::Pop( currentSap );
       
   691 
       
   692                 // check if this was first login or not
       
   693                 UpdateFirstLoginL();
       
   694 
       
   695                 iUiLoginCmdCB.CaptureEventsL();
       
   696                 }
       
   697             else
       
   698                 {
       
   699                 iTemporaryNetworkDown = EFalse;
       
   700                 }
       
   701 
       
   702             break;
       
   703             }
       
   704         case EIMPSPresenceServiceForceLogOut:
       
   705             {
       
   706             CHAT_DP_TXT(
       
   707                 "CCASessionHandlerCmd::HandlePresenceEventL ->\
       
   708                 EIMPSPresenceServiceForceLogOut" );
       
   709 
       
   710 
       
   711             NotifyEngineL( ELoggedOut );
       
   712             iUiLoginCmdCB.SetStatusPaneIconsL();
       
   713 
       
   714             // nuke all contacts from IM Storage
       
   715             MCAStoredContacts* contacts =
       
   716                 CCAStorageManagerFactory::ContactListInterfaceL();
       
   717             // Update own status item
       
   718             contacts->OwnStatus().SetOnlineStatus( TStorageManagerGlobals::EOffline );
       
   719 
       
   720             contacts->RemoveAllContactsL();
       
   721 
       
   722             break;
       
   723             }
       
   724         default:
       
   725             {
       
   726             break;
       
   727             }
       
   728         }
       
   729 
       
   730     if ( aEvent == EIMPSPresenceServiceForceLogOut ||
       
   731          aEvent == EIMPSPresenceServiceOffline ||
       
   732          aEvent == EIMPSPresenceServiceOnline )
       
   733         {
       
   734         // Reset initialization flag.
       
   735         iPresenceInitializationSuccesfull = EFalse;
       
   736         iIsContactFetchDone = EFalse;
       
   737         }
       
   738 
       
   739     // Offline events
       
   740     if ( aEvent == EIMPSPresenceServiceForceLogOut ||
       
   741          aEvent == EIMPSPresenceServiceOffline )
       
   742         {
       
   743         // notify observers just in case some post login task failed
       
   744         NotifyContactFetchObserversL( KErrCancel );
       
   745         }
       
   746 
       
   747     iLastState = aEvent;
       
   748     
       
   749     // Notify Service state change to MCAServiceStateObservers
       
   750     NotifyServiceStateObserversL( aEvent );
       
   751     }
       
   752 
       
   753 // -----------------------------------------------------------------------------
       
   754 // CCASessionHandlerCmd::HandlePresenceEventNotifyError
       
   755 // (other items were commented in a header).
       
   756 // -----------------------------------------------------------------------------
       
   757 //
       
   758 void CCASessionHandlerCmd::HandlePresenceEventNotifyError(
       
   759     CIMPSConnUiPresEventNotifier* /*aNotifier*/,
       
   760     TInt aError )
       
   761     {
       
   762     CHAT_DP( D_CHAT_LIT(
       
   763                  "CCASessionHandlerCmd::HandlePresenceEventNotifyError, Error: %d" ),
       
   764              aError );
       
   765 
       
   766     // Show note, if appropriate found
       
   767     if ( aError != KErrCancel ) // don't show error note for cancel events
       
   768         {
       
   769         TRAPD( err, IMNoteMapper::ShowNoteL( aError ) );
       
   770         if ( err )
       
   771             {
       
   772             CActiveScheduler::Current()->Error( err );
       
   773             }
       
   774         }
       
   775     }
       
   776 
       
   777 
       
   778 // ---------------------------------------------------------
       
   779 // CCASessionHandlerCmd::LoginType
       
   780 // Central repository version
       
   781 // (other items were commented in a header).
       
   782 // ---------------------------------------------------------
       
   783 //
       
   784 TIMPSLoginType CCASessionHandlerCmd::LoginType()
       
   785     {
       
   786     CHAT_DP( D_CHAT_LIT( "CCASessionHandlerCmd::LoginType" ) );
       
   787 
       
   788     TInt readValue( KErrCouldNotConnect ); // initialize to an error value
       
   789 
       
   790     TIMPSLoginType loginType( EIMPSManualLogin );
       
   791 
       
   792     CRepository* cenrep = NULL;
       
   793     TRAPD( err, cenrep = CRepository::NewL( KWVSettingsCenRepUid ) );
       
   794 
       
   795     if ( err != KErrNone )
       
   796         {
       
   797         // creation of cenrep failed -> return
       
   798         return loginType;
       
   799         }
       
   800 
       
   801     TRAP( err,
       
   802           CleanupStack::PushL( cenrep );
       
   803           err = cenrep->Get( KIMPSCRChatLogin, readValue );
       
   804           CleanupStack::PopAndDestroy( cenrep );
       
   805         ); // TRAP
       
   806     cenrep = NULL;
       
   807 
       
   808     if ( err != KErrNone )
       
   809         {
       
   810         // there was an error
       
   811         return loginType;
       
   812         }
       
   813 
       
   814     if ( ( readValue == EWVSettingsChatLoginAutoAlways ) ||
       
   815          ( readValue == EWVSettingsChatLoginAutoInHomeNW ) )
       
   816         {
       
   817         return EIMPSAAConnectionStart;
       
   818         }
       
   819     else if ( readValue == EWVSettingsChatLoginApplicationLaunch )
       
   820         {
       
   821         return EIMPSApplicationLaunch;
       
   822         }
       
   823     else
       
   824         {
       
   825         return EIMPSManualLogin;
       
   826         }
       
   827     }
       
   828 
       
   829 // -----------------------------------------------------------------------------
       
   830 // CCASessionHandlerCmd::BackgroundTasks
       
   831 // (other items were commented in a header).
       
   832 // -----------------------------------------------------------------------------
       
   833 //
       
   834 TInt CCASessionHandlerCmd::BackgroundTasks( TAny *aInstance )
       
   835     {
       
   836     return static_cast<CCASessionHandlerCmd*>( aInstance )->DoBackgroundTasks();
       
   837     }
       
   838 
       
   839 // -----------------------------------------------------------------------------
       
   840 // CCASessionHandlerCmd::DoBackgroundTasks
       
   841 // (other items were commented in a header).
       
   842 // -----------------------------------------------------------------------------
       
   843 //
       
   844 TInt CCASessionHandlerCmd::DoBackgroundTasks()
       
   845     {
       
   846     CHAT_DP_TXT( "CCASessionHandlerCmd::DoBackgroundTasks()" );
       
   847 
       
   848     TBool dataOk( EFalse );
       
   849     TRAPD( err, dataOk = iUiLoginCmdCB.CheckCurrentServerDataL() );
       
   850 
       
   851     if ( err != KErrNone ) // Signal user about error
       
   852         {
       
   853         CActiveScheduler::Current()->Error( err );
       
   854         }
       
   855 
       
   856 
       
   857     // do not even try to log in if data check failed or data is invalid
       
   858     if ( err != KErrNone || !dataOk )
       
   859         {
       
   860 
       
   861 
       
   862         // update to platform using the new task.
       
   863 
       
   864         // If the server data is invalid, then set the flag back
       
   865         iBackgroundTaskRunning = EFalse;
       
   866 
       
   867         // create the views
       
   868         TRAPD( err2, iUiLoginCmdCB.FinalizeChatConstructionL() );
       
   869         if ( err2 != KErrNone )
       
   870             {
       
   871             // show e.g. OOM
       
   872             CActiveScheduler::Current()->Error( err2 );
       
   873             }
       
   874         return EFalse;
       
   875         }
       
   876 
       
   877 
       
   878     TBool loginDone = EFalse;
       
   879     TRAP( err, loginDone = LoginL( EFalse, ETrue ) );
       
   880 
       
   881     if ( err != KErrNone || ! loginDone ) // Signal user about error
       
   882         {
       
   883         TRAPD( err2, iUiLoginCmdCB.FinalizeChatConstructionL() );
       
   884         if ( err2 != KErrNone )
       
   885             {
       
   886             // show e.g. OOM
       
   887             CActiveScheduler::Current()->Error( err2 );
       
   888             }
       
   889         if ( err != KErrNone )
       
   890             {
       
   891             // some error, the views should exist now.
       
   892             // must check err, because loginDone condition could bring us here,
       
   893             // in which case errors have been shown by IMPS Common UI.
       
   894             CActiveScheduler::Current()->Error( err );
       
   895             }
       
   896         }
       
   897 
       
   898 
       
   899     // Set it back.
       
   900     iBackgroundTaskRunning = EFalse;
       
   901 
       
   902     return EFalse;
       
   903     }
       
   904 
       
   905 // -----------------------------------------------------------------------------
       
   906 // CCASessionHandlerCmd::BackgroundSelect
       
   907 // (other items were commented in a header).
       
   908 // -----------------------------------------------------------------------------
       
   909 //
       
   910 TInt CCASessionHandlerCmd::BackgroundSelect( TAny *aInstance )
       
   911     {
       
   912     // CodeScanner warning ignored because CS does not regognize TRAP being used
       
   913     // after line break
       
   914 
       
   915     TRAPD( err, static_cast<CCASessionHandlerCmd*>( aInstance )->DoBackgroundSelectL() );
       
   916     // CSI: 42 # See comment above
       
   917 
       
   918     if ( err != KErrNone ) // Signal user about error
       
   919         {
       
   920         CActiveScheduler::Current()->Error( err );
       
   921         }
       
   922 
       
   923     return EFalse;
       
   924     }
       
   925 
       
   926 // -----------------------------------------------------------------------------
       
   927 // CCASessionHandlerCmd::DoBackgroundSelectL
       
   928 // (other items were commented in a header).
       
   929 // -----------------------------------------------------------------------------
       
   930 //
       
   931 void CCASessionHandlerCmd::DoBackgroundSelectL()
       
   932     {
       
   933     CHAT_DP_TXT( "CCASessionHandlerCmd::DoBackgroundSelectL()" );
       
   934 
       
   935 
       
   936     iUiLoginCmdCB.ReleaseCapturingL();
       
   937 
       
   938     if ( ! IsLoggedIn() )
       
   939         {
       
   940         // perhaps we couldn't login
       
   941         return;
       
   942         }
       
   943 
       
   944     TBool supportAlias = IMUtils::IntResourceValueL( RSC_CHAT_VARIATION_HAVE_ALIAS );
       
   945 
       
   946     if ( supportAlias )
       
   947         {
       
   948         // subscribe also the alias attribute
       
   949         MCAPresence* presence = CAPresenceManager::InstanceL();
       
   950         presence->AddAttributeL( MCAPresence::EAliasAttr );
       
   951         }
       
   952 
       
   953     TInt chatLoginType( IMUtils::WVSettingsChatLoginValue() );
       
   954     TBool manualLogin = chatLoginType != EWVSettingsChatLoginAutoAlways &&
       
   955                         chatLoginType != EWVSettingsChatLoginAutoInHomeNW;
       
   956 
       
   957     iUiLoginCmdCB.ShowPostLoginNotesL( iCurrentLoginIsFirst, manualLogin );
       
   958 
       
   959 
       
   960     // Check P&S flag for forced login.
       
   961     // If forced login is made in Common UI side
       
   962     // contact list base sync is needed here although
       
   963     // login type value is allways online at this point.
       
   964     TBool forcedLogin = EFalse;
       
   965     // Ignore errors
       
   966     TInt err = RProperty::Get( KBrandingUid, KForcedLoginKey, forcedLogin );
       
   967 
       
   968     if ( forcedLogin )
       
   969         {
       
   970         // Force contact list base sync when starting the fetch
       
   971         manualLogin = ETrue;
       
   972         }
       
   973 
       
   974     // fetch all contact lists
       
   975     FetchContactListsL( !manualLogin );
       
   976 
       
   977     if ( forcedLogin )
       
   978         {
       
   979         // Flag used, clear it
       
   980         // Ignore errors
       
   981         TInt err = RProperty::Set( KBrandingUid, KForcedLoginKey, EFalse );
       
   982         }
       
   983 
       
   984     GetServicesL();
       
   985     iUiLoginCmdCB.SetIMPSServices( iImpsServices );
       
   986     NotifyEngineL( ELoggedIn );
       
   987 
       
   988     if ( ! iUiLoginCmdCB.CancelLoginValue() )
       
   989         {
       
   990         // flush RA stuff only if login was not cancelled
       
   991 
       
   992         // flush any reactive authorizations now that we are done with the login
       
   993         MCAPresence* presence = CAPresenceManager::InstanceL();
       
   994         presence->PostLoginRAFlushL();
       
   995         }
       
   996     else
       
   997         {
       
   998         // user wanted to cancel login
       
   999         if ( ! iLogging )
       
  1000             {
       
  1001             // invoked by Always Online, so must do logout here
       
  1002             LogoutL();
       
  1003             }
       
  1004         }
       
  1005     }
       
  1006 
       
  1007 
       
  1008 
       
  1009 // -----------------------------------------------------------------------------
       
  1010 // CCASessionHandlerCmd::CurrentSAPLC
       
  1011 // (other items were commented in a header).
       
  1012 // -----------------------------------------------------------------------------
       
  1013 //
       
  1014 CIMPSSAPSettings* CCASessionHandlerCmd::CurrentSAPLC()
       
  1015     {
       
  1016     CIMPSSAPSettings* sap = CIMPSSAPSettings::NewLC();
       
  1017 
       
  1018     // If session is active, then current user-id should be retrieved from
       
  1019     // connection UI instead of SAP settings store
       
  1020     if ( IsLoggedIn() )
       
  1021         {
       
  1022         CHAT_DP_TXT(
       
  1023             "CCASessionHandlerCmd::CurrentSAPLC returning SAP based on ConnUI" );
       
  1024         if ( !iSessionSlotID )
       
  1025             {
       
  1026             iSessionSlotID =
       
  1027                 iConnUI->GetActiveNWSessionSlotIDL( EIMPSConnClientIM );
       
  1028             }
       
  1029         iConnUI->GetLoggedInSapL( *iSessionSlotID, *sap );
       
  1030         }
       
  1031     else
       
  1032         {
       
  1033         CHAT_DP_TXT(
       
  1034             "CCASessionHandlerCmd:: \
       
  1035                 CurrentSAPLC returning SAP based on SAPSettingsStore" );
       
  1036         CIMPSSAPSettingsStore* sapStore = CIMPSSAPSettingsStore::NewLC();
       
  1037         sapStore->GetDefaultL( sap, EIMPSIMAccessGroup );
       
  1038         CleanupStack::PopAndDestroy( sapStore );
       
  1039         }
       
  1040 
       
  1041     CHAT_DP( D_CHAT_LIT( "CCASessionHandlerCmd::CurrentSAPLC sap uid %d" ),
       
  1042              sap->Uid() );
       
  1043     return sap;
       
  1044     }
       
  1045 
       
  1046 
       
  1047 // -----------------------------------------------------------------------------
       
  1048 // CCASessionHandlerCmd::GetServicesL
       
  1049 // (other items were commented in a header).
       
  1050 // -----------------------------------------------------------------------------
       
  1051 //
       
  1052 TBool CCASessionHandlerCmd::GetServicesL()
       
  1053     {
       
  1054     TBool redraw( EFalse );
       
  1055 
       
  1056     TBool groupsBefore( IsSupported( EGroup ) );
       
  1057 
       
  1058     CPEngNWSessionSlot2* slot = CPEngNWSessionSlot2::NewLC( *iSessionSlotID );
       
  1059     TPckg<TPEngWVCspServicesTree2> cspTreePtr( iImpsServices );
       
  1060     TInt err = slot->GetOption( KPEngWVCspServicesTree2,
       
  1061                                 KPEngNWSessionSlotTransportQuery,
       
  1062                                 cspTreePtr );
       
  1063 
       
  1064     CleanupStack::PopAndDestroy( slot );
       
  1065     if ( err <= Imps_ERROR_BASE )
       
  1066         {
       
  1067         IMNoteMapper::ShowNoteL( err );
       
  1068         }
       
  1069     else
       
  1070         {
       
  1071         User::LeaveIfError( err );
       
  1072 
       
  1073         // redraw if group feature has changed
       
  1074         redraw = ( groupsBefore != IsSupported( EGroup ) );
       
  1075         }
       
  1076     CHAT_DP( D_CHAT_LIT( "*getservices, need redraw %d" ), redraw );
       
  1077     return redraw;
       
  1078     }
       
  1079 
       
  1080 // -----------------------------------------------------------------------------
       
  1081 // CCASessionHandlerCmd::NotifyEngineL
       
  1082 // (other items were commented in a header).
       
  1083 // -----------------------------------------------------------------------------
       
  1084 //
       
  1085 void CCASessionHandlerCmd::NotifyEngineL( TNetworkState aState,
       
  1086                                           CPEngNWSessionSlotID2* aSessionSlotID /* = NULL */ )
       
  1087     {
       
  1088 
       
  1089     CIMPSSAPSettings* sap = CurrentSAPLC();
       
  1090 
       
  1091     if ( aState == ELoggedOut )
       
  1092         {
       
  1093         // clear the contacts refreshed flag also
       
  1094         iContactsRefreshed = EFalse;
       
  1095         MCALoginPC* login = iProcessManager.GetLogInInterface();
       
  1096 
       
  1097         if ( login )
       
  1098             {
       
  1099             login->NotifyEngineForLogoutL( aState, aSessionSlotID, iImpsServices );
       
  1100             iUiLoginCmdCB.ShutDialogsL();
       
  1101             }
       
  1102         else
       
  1103             return; // don't anything, becoz login object is null.
       
  1104         }
       
  1105 
       
  1106     else if ( aState == ELoggedIn )
       
  1107         {
       
  1108         CHAT_DP( D_CHAT_LIT( "NotifyEngineL - ELoggedIn" ) );
       
  1109 
       
  1110         if ( aSessionSlotID )
       
  1111             {
       
  1112 
       
  1113             MCALoginPC* login = iProcessManager.GetLogInInterface();
       
  1114             login->LoginL( aState, sap, aSessionSlotID, iImpsServices );
       
  1115 
       
  1116             }
       
  1117 
       
  1118         }
       
  1119 
       
  1120 
       
  1121     CleanupStack::PopAndDestroy();
       
  1122 
       
  1123 
       
  1124     }
       
  1125 
       
  1126 
       
  1127 // -----------------------------------------------------------------------------
       
  1128 // CCASessionHandlerCmd::IsOfflineProfileOn
       
  1129 // (other items were commented in a header).
       
  1130 // -----------------------------------------------------------------------------
       
  1131 //
       
  1132 TBool CCASessionHandlerCmd::IsOfflineProfileOn() const
       
  1133     {
       
  1134     return ( EProfileOffLineId == iProfileApi->ActiveProfileId()  );
       
  1135     }
       
  1136 
       
  1137 
       
  1138 
       
  1139 // ---------------------------------------------------------
       
  1140 // CCASessionHandlerCmd::FetchContactListsL
       
  1141 // (other items were commented in a header).
       
  1142 // ---------------------------------------------------------
       
  1143 //
       
  1144 void CCASessionHandlerCmd::FetchContactListsL( TBool aAlwaysOnlineActive )
       
  1145     {
       
  1146     MCAPresence* presence = CAPresenceManager::InstanceL();
       
  1147     if ( !presence )
       
  1148         {
       
  1149         User::Leave( KErrNotFound );
       
  1150         }
       
  1151     MCAContactLists* listInterface = presence->ContactLists();
       
  1152 
       
  1153 #ifndef IMPS_CONTACT_FETCH_BACKGROUND
       
  1154 
       
  1155     iUiLoginCmdCB.ShowProcessingNoteLC();
       
  1156     TInt err( listInterface->FetchContactsL( aAlwaysOnlineActive ) );
       
  1157     CleanupStack::PopAndDestroy(); // waitdialog
       
  1158 
       
  1159     iIsContactFetchDone = ETrue;
       
  1160     NotifyContactFetchObserversL( err );
       
  1161 
       
  1162     if ( err != KErrNone )
       
  1163         {
       
  1164 
       
  1165         HBufC* text = CCoeEnv::Static()->AllocReadResourceLC(
       
  1166                           R_QTN_CHAT_FETCHING_CONTACT_LISTS_FAILED );
       
  1167         IMDialogUtils::DisplayErrorNoteL( *text );
       
  1168         CleanupStack::PopAndDestroy( text );
       
  1169 
       
  1170         }
       
  1171 #else
       
  1172     // If we have AO active, then base sync should be done already
       
  1173     listInterface->FetchContactsL( aAlwaysOnlineActive );
       
  1174 #endif //IMPS_CONTACT_FETCH_BACKGROUND
       
  1175     }
       
  1176 
       
  1177 // ---------------------------------------------------------
       
  1178 // CCASessionHandlerCmd::CloseConversationsL
       
  1179 // (other items were commented in a header).
       
  1180 // ---------------------------------------------------------
       
  1181 //
       
  1182 void CCASessionHandlerCmd::CloseConversations()
       
  1183     {
       
  1184 
       
  1185     iProcessManager.GetConversationInterface()->CloseConversations();
       
  1186 
       
  1187     }
       
  1188 
       
  1189 
       
  1190 // ---------------------------------------------------------
       
  1191 // CCASessionHandlerCmd::NetworkSessionSlotID
       
  1192 // (other items were commented in a header).
       
  1193 // ---------------------------------------------------------
       
  1194 //
       
  1195 const CPEngNWSessionSlotID2& CCASessionHandlerCmd::NetworkSessionSlotIDL()
       
  1196     {
       
  1197     if ( !iSessionSlotID )
       
  1198         {
       
  1199         iSessionSlotID = iConnUI->GetActiveNWSessionSlotIDL(
       
  1200                              EIMPSConnClientIM );
       
  1201         }
       
  1202 
       
  1203     return *iSessionSlotID;
       
  1204     }
       
  1205 
       
  1206 // ---------------------------------------------------------
       
  1207 // CCASessionHandlerCmd::CloseConversationsL
       
  1208 // (other items were commented in a header).
       
  1209 // ---------------------------------------------------------
       
  1210 //
       
  1211 void CCASessionHandlerCmd::DoApplicationExitL()
       
  1212     {
       
  1213     if ( IsLoggedIn() )
       
  1214         {
       
  1215         // Leave groups when exiting application in AO.
       
  1216 
       
  1217         TRAP_IGNORE( iProcessManager.GetConversationInterface()->LeaveJoinedGroupsL() );
       
  1218 
       
  1219         if ( !iSessionSlotID )
       
  1220             {
       
  1221             NetworkSessionSlotIDL();
       
  1222             }
       
  1223         iConnUI->HandleApplicationExitL( EIMPSConnClientIM,
       
  1224                                          EIMPSLeaveSessionOpenExit,
       
  1225                                          *iSessionSlotID );
       
  1226 
       
  1227         iProcessManager.GetLogInInterface()->LogoutL();
       
  1228 
       
  1229         }
       
  1230     }
       
  1231 
       
  1232 // ---------------------------------------------------------
       
  1233 // CCASessionHandlerCmd::UpdateFirstLoginL
       
  1234 // (other items were commented in a header).
       
  1235 // ---------------------------------------------------------
       
  1236 //
       
  1237 void CCASessionHandlerCmd::UpdateFirstLoginL()
       
  1238     {
       
  1239     CIMPSSAPSettings* currentSAP = CurrentSAPLC();
       
  1240 
       
  1241     // check if this is first login to server
       
  1242     TInt firstLogin = 0;
       
  1243     TInt sapError = -1;
       
  1244     sapError = currentSAP->GetOpaqueInt( KIMFirstLoginToServer, firstLogin );
       
  1245 
       
  1246     if ( sapError != KErrNotFound )
       
  1247         {
       
  1248         User::LeaveIfError( sapError );
       
  1249         }
       
  1250 
       
  1251     if ( ! firstLogin )
       
  1252         {
       
  1253         // first login has not been done. so this is the first login
       
  1254 
       
  1255         iCurrentLoginIsFirst = ETrue;
       
  1256 
       
  1257         CIMPSSAPSettingsStore* sapStore = CIMPSSAPSettingsStore::NewLC();
       
  1258         CIMPSSAPSettingsList* sapList = CIMPSSAPSettingsList::NewLC();
       
  1259         sapStore->PopulateSAPSettingsListL( *sapList, EIMPSIMAccessGroup );
       
  1260 
       
  1261         // update information that this server has now logged in
       
  1262         // at least once and find our sap.. because logged in SAP has UID of zero, we must
       
  1263         // find the correct UID manually from list
       
  1264         TInt index( KErrNotFound );
       
  1265         sapList->FindNameL( currentSAP->SAPName(), index );
       
  1266         if ( index == KErrNotFound )
       
  1267             {
       
  1268             User::Leave( index );
       
  1269             }
       
  1270 
       
  1271         if ( index != KErrNotFound )
       
  1272             {
       
  1273             // found it, update the correct sap
       
  1274             CIMPSSAPSettings* storedSap = CIMPSSAPSettings::NewLC();
       
  1275 
       
  1276             TUint32 sapUid = sapList->UidForIndex( index );
       
  1277             sapStore->GetSAPL( sapUid, storedSap );
       
  1278             storedSap->SetOpaqueInt( KIMFirstLoginToServer, ETrue );
       
  1279             sapStore->UpdateOldSAPL( storedSap, sapUid );
       
  1280 
       
  1281             CleanupStack::PopAndDestroy( storedSap );
       
  1282             }
       
  1283 
       
  1284         CleanupStack::PopAndDestroy( 2, sapStore );
       
  1285         }
       
  1286     else
       
  1287         {
       
  1288         iCurrentLoginIsFirst = EFalse;
       
  1289         }
       
  1290 
       
  1291     CleanupStack::PopAndDestroy( currentSAP );
       
  1292     }
       
  1293 
       
  1294 // ---------------------------------------------------------
       
  1295 // CCASessionHandlerCmd::UpdateDefaultSettingsL
       
  1296 // (other items were commented in a header).
       
  1297 // ---------------------------------------------------------
       
  1298 //
       
  1299 void CCASessionHandlerCmd::UpdateDefaultSettingsL()
       
  1300     {
       
  1301 
       
  1302     MCASettingsPC* settingsPC = iProcessManager.GetSettingsInterface();
       
  1303 
       
  1304 
       
  1305     // set variated presence update mode
       
  1306 
       
  1307     TInt presenceUpdate = IMUtils::IntResourceValueL( RSC_CHAT_VARIATION_UPDATE_CONTACTS_SETTING );
       
  1308 
       
  1309     CHAT_DP( D_CHAT_LIT(
       
  1310                  "CCASessionHandlerCmd::UpdateDefaultSettingsL, presenceUpdate %d" ),
       
  1311              presenceUpdate );
       
  1312 
       
  1313     if ( presenceUpdate == 1 )     // 1: only automatic update supported
       
  1314         {
       
  1315         settingsPC->SetBoolValuePCL( TEnumsPC::EAutomaticPresenceUpdate, ETrue );
       
  1316         }
       
  1317     else if ( presenceUpdate == 2 ) // 2: only manual update supported
       
  1318         {
       
  1319         settingsPC->SetBoolValuePCL( TEnumsPC::EAutomaticPresenceUpdate, EFalse );
       
  1320         }
       
  1321 
       
  1322     }
       
  1323 
       
  1324 // ---------------------------------------------------------
       
  1325 // CCASessionHandlerCmd::NotifyContactFetchObserversL
       
  1326 // (other items were commented in a header).
       
  1327 // ---------------------------------------------------------
       
  1328 //
       
  1329 void CCASessionHandlerCmd::NotifyContactFetchObserversL( TInt aError )
       
  1330     {
       
  1331     TInt count = iFetchObservers.Count();
       
  1332     for ( TInt i = 0; i < count; ++i )
       
  1333         {
       
  1334         iFetchObservers[ i ]->HandleFetchCompleteL( aError );
       
  1335         }
       
  1336 
       
  1337     }
       
  1338 
       
  1339 // ---------------------------------------------------------
       
  1340 // CCASessionHandlerCmd::AddContactFetchObserverL
       
  1341 // (other items were commented in a header).
       
  1342 // ---------------------------------------------------------
       
  1343 //
       
  1344 void CCASessionHandlerCmd::AddContactFetchObserverL( MCAContactFetchObserver* aObserver )
       
  1345     {
       
  1346     if ( iFetchObservers.Find( aObserver ) == KErrNotFound )
       
  1347         {
       
  1348         iFetchObservers.AppendL( aObserver );
       
  1349         }
       
  1350     }
       
  1351 
       
  1352 // ---------------------------------------------------------
       
  1353 // CCASessionHandlerCmd::RemoveContactFetchObserver
       
  1354 // (other items were commented in a header).
       
  1355 // ---------------------------------------------------------
       
  1356 //
       
  1357 void CCASessionHandlerCmd::RemoveContactFetchObserver( MCAContactFetchObserver* aObserver )
       
  1358     {
       
  1359     TInt index = iFetchObservers.Find( aObserver );
       
  1360     if ( index >= 0 )
       
  1361         {
       
  1362         iFetchObservers.Remove( index );
       
  1363         }
       
  1364     }
       
  1365 
       
  1366 // ---------------------------------------------------------
       
  1367 // CCASessionHandlerCmd::NotifyServiceStateObserversL
       
  1368 // (other items were commented in a header).
       
  1369 // ---------------------------------------------------------
       
  1370 //
       
  1371 void CCASessionHandlerCmd::NotifyServiceStateObserversL( TIMPSPresenceServiceEvent aEvent )
       
  1372     {
       
  1373     MCAServiceStateObserver::TServiceState state =
       
  1374         MCAServiceStateObserver::EUnknown;
       
  1375 
       
  1376     // Convert TIMPSPresenceServiceEvent to TServiceState
       
  1377     switch ( aEvent )
       
  1378         {
       
  1379         case EIMPSPresenceServiceOffline:
       
  1380         case EIMPSPresenceServiceForceLogOut: // flowthrough
       
  1381             {
       
  1382             state = MCAServiceStateObserver::EOffline;
       
  1383             break;
       
  1384             }
       
  1385         case EIMPSPresenceServiceOnline:
       
  1386             {
       
  1387             state = MCAServiceStateObserver::EOnline;
       
  1388             break;
       
  1389             }
       
  1390         default:
       
  1391             {
       
  1392             // Not important event.
       
  1393             return;
       
  1394             }
       
  1395         }
       
  1396 
       
  1397     // Notify observers
       
  1398     TInt count = iServiceStateObservers.Count();
       
  1399     for ( TInt i = 0; i < count; ++i )
       
  1400         {
       
  1401         TRAP_IGNORE( iServiceStateObservers[ i ]->
       
  1402                      HandleServiceStateChangeL( state ) );
       
  1403         }
       
  1404 
       
  1405     }
       
  1406 
       
  1407 // ---------------------------------------------------------
       
  1408 // CCASessionHandlerCmd::AddServiceStateObserversL
       
  1409 // (other items were commented in a header).
       
  1410 // ---------------------------------------------------------
       
  1411 //
       
  1412 void CCASessionHandlerCmd::AddServiceStateObserversL( MCAServiceStateObserver* aObserver )
       
  1413     {
       
  1414     if ( iServiceStateObservers.Find( aObserver ) == KErrNotFound )
       
  1415         {
       
  1416         iServiceStateObservers.AppendL( aObserver );
       
  1417         }
       
  1418     }
       
  1419 
       
  1420 // ---------------------------------------------------------
       
  1421 // CCASessionHandlerCmd::RemoveServiceStateObservers
       
  1422 // (other items were commented in a header).
       
  1423 // ---------------------------------------------------------
       
  1424 //
       
  1425 void CCASessionHandlerCmd::RemoveServiceStateObservers( MCAServiceStateObserver* aObserver )
       
  1426     {
       
  1427     TInt index = iServiceStateObservers.Find( aObserver );
       
  1428     if ( index >= 0 )
       
  1429         {
       
  1430         iServiceStateObservers.Remove( index );
       
  1431         }
       
  1432     }
       
  1433 
       
  1434 // ---------------------------------------------------------
       
  1435 // CCASessionHandlerCmd::IsFetchDone
       
  1436 // (other items were commented in a header).
       
  1437 // ---------------------------------------------------------
       
  1438 //
       
  1439 TBool CCASessionHandlerCmd::IsFetchDone()
       
  1440     {
       
  1441     return iIsContactFetchDone;
       
  1442     }
       
  1443 
       
  1444 // -----------------------------------------------------------------------------
       
  1445 // CCASessionHandlerCmd::ContactsRefreshed()
       
  1446 // (other items were commented in a header).
       
  1447 // -----------------------------------------------------------------------------
       
  1448 //
       
  1449 TBool CCASessionHandlerCmd::ContactsRefreshed() const
       
  1450     {
       
  1451     return iContactsRefreshed;
       
  1452     }
       
  1453 // -----------------------------------------------------------------------------
       
  1454 // CCASessionHandlerCmd::ContactsRefreshed()
       
  1455 // (other items were commented in a header).
       
  1456 // -----------------------------------------------------------------------------
       
  1457 //
       
  1458 TBool CCASessionHandlerCmd::IsAlreadyLoggedOut() const
       
  1459     {
       
  1460     return iAlreadyLoggedOut;
       
  1461     }
       
  1462 // -----------------------------------------------------------------------------
       
  1463 // CCASessionHandlerCmd::SetContactsRefreshed()
       
  1464 // (other items were commented in a header).
       
  1465 // -----------------------------------------------------------------------------
       
  1466 
       
  1467 void CCASessionHandlerCmd::SetContactsRefreshed( TBool aRefreshed )
       
  1468     {
       
  1469     iContactsRefreshed = aRefreshed;
       
  1470     }
       
  1471 
       
  1472 // -----------------------------------------------------------------------------
       
  1473 // CCASessionHandlerCmd::SetContactsRefreshed()
       
  1474 // (other items were commented in a header).
       
  1475 // -----------------------------------------------------------------------------
       
  1476 
       
  1477 TBool CCASessionHandlerCmd::IsBackgroundTaskRunning() const
       
  1478     {
       
  1479     return iBackgroundTaskRunning;
       
  1480     }
       
  1481 
       
  1482 // ---------------------------------------------------------
       
  1483 // CCASessionHandler::IsBackgroundGroupSyncPending
       
  1484 // (other items were commented in a header).
       
  1485 // ---------------------------------------------------------
       
  1486 
       
  1487 TBool CCASessionHandlerCmd::IsBackgroundTaskPendingL()
       
  1488     {
       
  1489     TBool ret = EFalse;
       
  1490 
       
  1491     CHAT_DP_TXT( "CCASessionHandler::IsBackgroundGroupSyncPending started" );
       
  1492 
       
  1493     MCALoginPC* loginPC = iProcessManager.GetLogInInterface();
       
  1494 
       
  1495     iBackgroundTaskPending = loginPC->IsBackgroundTaskPending();
       
  1496 
       
  1497     // this call minimize the synch time
       
  1498     // it cancel all runing request
       
  1499     loginPC->CancelAllRequests();
       
  1500 
       
  1501     // check for background task state
       
  1502     if ( iBackgroundTaskPending )
       
  1503         {
       
  1504         loginPC->RegisterBackGroundTaskObserver( this );
       
  1505 
       
  1506         ret = WaitToCompleteBackgroundTaskL();
       
  1507 
       
  1508         }
       
  1509 
       
  1510     CHAT_DP_TXT( "CCASessionHandler::IsBackgroundGroupSyncPending completed" );
       
  1511 
       
  1512     return ret;
       
  1513 
       
  1514     }
       
  1515 
       
  1516 // ---------------------------------------------------------
       
  1517 // CCASessionHandler::HandleGroupSync
       
  1518 // (other items were commented in a header).
       
  1519 // ---------------------------------------------------------
       
  1520 
       
  1521 void CCASessionHandlerCmd::HandleBackGroundTaskCompleteL( TBool aCompleted )
       
  1522     {
       
  1523 
       
  1524     CHAT_DP_TXT( "CCASessionHandler::HandleBackGroundTaskComplete started" );
       
  1525 
       
  1526     MCALoginPC* loginPC = iProcessManager.GetLogInInterface();
       
  1527 
       
  1528     iBackgroundTaskPending = aCompleted;
       
  1529 
       
  1530     if ( ! iBackgroundTaskPending ) //group synchro is complete
       
  1531         {
       
  1532         // hide wait note
       
  1533         if ( iWaitVisible )
       
  1534             {
       
  1535             TRAPD( err, iUiLoginCmdCB.DismissProcessingNoteL( KErrNone , ETrue ) );
       
  1536             if ( err != KErrNone )
       
  1537                 {
       
  1538                 User::Leave( err );
       
  1539                 }
       
  1540             iWaitVisible = EFalse;
       
  1541             }
       
  1542 
       
  1543         loginPC->UnRegisterBackGroundTaskObserver( this );
       
  1544 
       
  1545         // every thinh is ok ,now call logout
       
  1546         DoLogoutL() ;
       
  1547 
       
  1548         }
       
  1549     CHAT_DP_TXT( "CCASessionHandler::HandleGroupSync done" );
       
  1550     }
       
  1551 // -----------------------------------------------------------------------------
       
  1552 // CCASessionHandler::GroupSyncL
       
  1553 // (other items were commented in a header).
       
  1554 // -----------------------------------------------------------------------------
       
  1555 //
       
  1556 TBool CCASessionHandlerCmd::WaitToCompleteBackgroundTaskL()
       
  1557     {
       
  1558 
       
  1559     CHAT_DP_TXT( "CCASessionHandler::GroupSyncL started" );
       
  1560 
       
  1561     if ( ! iWaitVisible )
       
  1562         {
       
  1563         CHAT_DP_TXT(
       
  1564             "CCASessionHandler::GroupSyncL - ! iWaitVisible && iGroupSyncProgress" );
       
  1565         // launch the wait note
       
  1566         iWaitVisible = ETrue;
       
  1567 
       
  1568         iUiLoginCmdCB.ShowProcessingNoteLC();
       
  1569 
       
  1570         CleanupStack::Pop();// waitnote
       
  1571 
       
  1572         CHAT_DP_TXT( "CCASessionHandler::GroupSyncL done" );
       
  1573         }
       
  1574     return ETrue;
       
  1575     }
       
  1576 
       
  1577 
       
  1578 //  End of File
       
  1579