wvuing/wvuipresence/src/CCAPEngPresenceManager.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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:  Manager for presence operations (fetcher/notifier/publisher).
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CCAPEngPresenceManager.h"
       
    22 #include "CAPresenceDefinitions.h"
       
    23 #include "CAPresenceUtils.h"
       
    24 #include "CCAPEngListManager.h"
       
    25 
       
    26 // Contact handling
       
    27 #include "CCAStorageManagerFactory.h"
       
    28 #include "MCAStoredContacts.h"
       
    29 
       
    30 #include "impsbuilddefinitions.h"
       
    31 
       
    32 #include "MCAContactList.h"
       
    33 
       
    34 #include "MCAStoredContact.h"
       
    35 
       
    36 #include "ChatDebugPrint.h"
       
    37 
       
    38 #include "MCAPresenceObserver.h"
       
    39 #include "MCAWatcherObserver.h"
       
    40 #include "MCASettings.h"
       
    41 
       
    42 #include "CCAPresenceErrors.h"
       
    43 #include "CAPresenceConst.h"
       
    44 #include "MCAPresence.h"
       
    45 #include "MCAContactLists.h"
       
    46 #include "TCAWrappers.h"
       
    47 
       
    48 #include "MCAReactiveAuthObserver.h"
       
    49 #include "SServerPrefers.h"
       
    50 #include "TDecodeAttrParams.h"
       
    51 
       
    52 #include "ImpsCSPAllErrors.h"
       
    53 
       
    54 #include <WVUIPresenceVariationNG.rsg>
       
    55 
       
    56 #include <PEngWVPresenceAttributes2.h>
       
    57 #include <PEngWVPresenceErrors2.h>
       
    58 #include <CPEngPresenceNotifier2.h>
       
    59 #include <CPEngAttributeTransaction2.h>
       
    60 #include <CPEngAttributeStore2.h>
       
    61 #include <CPEngTrackedPresenceIDs2.h>
       
    62 #include <CPEngTrackedPresenceID2.h>
       
    63 #include <CIMPSSAPSettingsStore.h>
       
    64 #include <PEngPresenceEngineConsts2.h>
       
    65 #include <CPEngNWSessionSlotID2.h>
       
    66 
       
    67 #include <MPEngPresenceAttrModel2.h>
       
    68 #include <MPEngTransactionStatus2.h>
       
    69 
       
    70 #include <CPEngReactAuthStore.h>
       
    71 #include <CPEngReactAuthTransaction.h>
       
    72 #include <CPEngReactAuthNotifier.h>
       
    73 #include <CPEngReactAuthTransaction.h>
       
    74 #include <MPEngReactAuthObserver.h>
       
    75 #include <MPEngReactAuthTransactionObserver.h>
       
    76 #include <MPEngAuthorizationRequest.h>
       
    77 #include <MPEngAuthorizationRespond.h>
       
    78 #include <MPEngAuthorizationStatus.h>
       
    79 
       
    80 // ============================ MEMBER FUNCTIONS ===============================
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CCAPEngPresenceManager::CCAPEngPresenceManager
       
    84 // C++ default constructor can NOT contain any code, that
       
    85 // might leave.
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 CCAPEngPresenceManager::CCAPEngPresenceManager()
       
    89         : iAuthMode( KUndefined ),
       
    90         iNetworkState( KUndefined ),
       
    91         iPEngAPIInitialized( EFalse ),
       
    92         iObserverQueued( EFalse ),
       
    93         iCachedStatus( EFalse ),
       
    94         iAttributeProcessing( ETrue )
       
    95     {
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CCAPEngPresenceManager::ConstructL
       
   100 // Symbian 2nd phase constructor can leave.
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CCAPEngPresenceManager::ConstructL( MCASettings* aApplicationSettings )
       
   104     {
       
   105     // Set settings API
       
   106     SetSettingsAPIL( aApplicationSettings );
       
   107 
       
   108     // Assign attributes that we are handling
       
   109     ResetAttributesL( EFalse );
       
   110 
       
   111     // Create detailed error container
       
   112     iErrors = CCAPresenceErrors::NewL();
       
   113 
       
   114     iIdle = CIdle::NewL( CActive::EPriorityIdle );
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CCAPEngPresenceManager::NewL
       
   119 // Two-phased constructor.
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 CCAPEngPresenceManager* CCAPEngPresenceManager::NewL(
       
   123     MCASettings* aApplicationSettings )
       
   124     {
       
   125     CCAPEngPresenceManager* self = new( ELeave ) CCAPEngPresenceManager;
       
   126 
       
   127     CleanupStack::PushL( self );
       
   128     self->ConstructL( aApplicationSettings );
       
   129     CleanupStack::Pop( self );
       
   130 
       
   131     return self;
       
   132     }
       
   133 
       
   134 
       
   135 // Destructor
       
   136 CCAPEngPresenceManager::~CCAPEngPresenceManager()
       
   137     {
       
   138     delete iPEngAttributeTransaction;
       
   139     delete iListManager;
       
   140     delete iPEngAttributeStore;
       
   141     delete iErrors;
       
   142     if ( iOwnPresenceNotifier )
       
   143         {
       
   144         iOwnPresenceNotifier->RemoveObserver( *this );
       
   145         }
       
   146     delete iOwnPresenceNotifier;
       
   147     delete iCachedStatusText;
       
   148 
       
   149     iAttributes.Close();
       
   150 
       
   151     if ( iRANotifier )
       
   152         {
       
   153         iRANotifier->RemoveObserver( *this );
       
   154         iRANotifier->Stop();
       
   155         }
       
   156 
       
   157     delete iRAStore;
       
   158     delete iRANotifier;
       
   159     delete iRATransaction;
       
   160 
       
   161     delete iSessionSlotID;
       
   162 
       
   163     iOwnStates.ResetAndDestroy();
       
   164     iPresenceStates.ResetAndDestroy();
       
   165     iFetchObjects.ResetAndDestroy();
       
   166 
       
   167     TInt count( iAttrArrays.Count() );
       
   168     for ( TInt a( 0 ); a < count; ++a )
       
   169         {
       
   170         iAttrArrays[ a ].iArray.ResetAndDestroy();
       
   171         }
       
   172     iAttrArrays.Close();
       
   173 
       
   174     delete iIdle;
       
   175     }
       
   176 
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CCAPEngPresenceManager::AddWatcherL
       
   180 // Sets watcher flag for given contact
       
   181 // (other items were commented in a header).
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 void CCAPEngPresenceManager::AddWatcherL( const TDesC& aWVId )
       
   185     {
       
   186     CHAT_DP_TXT( "CCAPEngPresenceManager::AddWatcherL" );
       
   187     CCAStorageManagerFactory::ContactListInterfaceL()->SetWatched( aWVId, ETrue );
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CCAPEngPresenceManager::GetOnlineFriendsL
       
   192 // Fetches list of online-friends
       
   193 // (other items were commented in a header).
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 void CCAPEngPresenceManager::GetOnlineFriendsL( CDesCArray &aOnlineList,
       
   197                                                 TBool aFetchFromNetwork )
       
   198     {
       
   199     CHAT_DP_TXT( "CCAPEngPresenceManager::GetOnlineFriendsL()...starts" );
       
   200 
       
   201     CPtrCArray* friends = CAPresenceUtils::GenerateFriendsArrayLC(
       
   202                               CCAStorageManagerFactory::ContactListInterfaceL() );
       
   203 
       
   204     // Reset given array
       
   205     aOnlineList.Reset();
       
   206 
       
   207     // update our lists
       
   208     if ( aFetchFromNetwork )
       
   209         {
       
   210         CHAT_DP_TXT( "CCAPEngPresenceManager::GetOnlineFriendsL starting \
       
   211                       network fetch" );
       
   212         FetchAttributesL( *friends, &aOnlineList, NULL, NULL, EFalse );
       
   213         }
       
   214 
       
   215     // Populate aOnlineList with online friends
       
   216     TInt friendCount( friends->Count() );
       
   217     MCAStoredContact* contact = NULL;
       
   218 
       
   219     for ( TInt i( 0 ); i < friendCount; ++i )
       
   220         {
       
   221         contact = CCAStorageManagerFactory::ContactListInterfaceL()->
       
   222                   FindAnyContact( ( *friends )[i] );
       
   223         if ( contact )
       
   224             {
       
   225             TStorageManagerGlobals::TPresenceStatus status =
       
   226                 contact->OnlineStatus();
       
   227             if ( status == TStorageManagerGlobals::EOnline ||
       
   228                  status == TStorageManagerGlobals::EAway ||
       
   229                  status == TStorageManagerGlobals::EBusy )
       
   230                 {
       
   231                 aOnlineList.AppendL( contact->UserId() );
       
   232                 }
       
   233             }
       
   234         }
       
   235 
       
   236     CleanupStack::PopAndDestroy( friends );
       
   237 
       
   238     CHAT_DP_TXT( "CCAPEngPresenceManager::GetOnlineFriendsL...over" );
       
   239     }
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CCAPEngPresenceManager::GetOnlineUsersL
       
   243 // Fetches list of online users from given user-list
       
   244 // (other items were commented in a header).
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 void CCAPEngPresenceManager::GetOnlineUsersL( const CDesCArray &aUserList,
       
   248                                               CDesCArray *aOnlineList,
       
   249                                               CDesCArray *aOffLineList,
       
   250                                               TBool aUpdateStorage /*= EFalse*/ )
       
   251     {
       
   252     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::GetOnlineUsersL, \
       
   253                           aUserList.Count() = %d, &aOnlineList = %d, \
       
   254                           &aOffLineList = %d" ),
       
   255              aUserList.Count(), aOnlineList, aOffLineList );
       
   256 
       
   257     FetchAttributesL( aUserList, aOnlineList, aOffLineList, NULL,
       
   258                       aUpdateStorage );
       
   259 
       
   260     CHAT_DP_TXT( "CCAPEngPresenceManager::GetOnlineUsersL done" );
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CCAPEngPresenceManager::PresenceObserver
       
   265 // Returns presence-observer pointer
       
   266 // (other items were commented in a header).
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 MCAPresenceObserver* CCAPEngPresenceManager::PresenceObserver() const
       
   270     {
       
   271     return iPresenceObserver;
       
   272     }
       
   273 
       
   274 // -----------------------------------------------------------------------------
       
   275 // CCAPEngPresenceManager::RefreshFriendsL
       
   276 // Refreshes the presence status of friends according to refresh-flags
       
   277 // (other items were commented in a header).
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 void CCAPEngPresenceManager::RefreshFriendsL()
       
   281     {
       
   282     // Create needed MDesCArray-based array for presence operations
       
   283     CPtrCArray* contactsArray = CAPresenceUtils::GenerateFriendsArrayLC(
       
   284                                     CCAStorageManagerFactory::ContactListInterfaceL()/*, EFalse*/ );
       
   285     FetchAttributesL( *contactsArray, NULL, NULL, NULL, ETrue );
       
   286     CleanupStack::PopAndDestroy( contactsArray );
       
   287     }
       
   288 
       
   289 // -----------------------------------------------------------------------------
       
   290 // CCAPEngPresenceManager::SubscribeFriendsL
       
   291 // Subscribes friends based on update-flag
       
   292 // (other items were commented in a header).
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CCAPEngPresenceManager::SubscribeFriendsL( const TSubscribeMode aMode )
       
   296     {
       
   297     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::SubscribeFriendsL, mode = %d" ),
       
   298              aMode );
       
   299 
       
   300     if ( !iPEngAPIInitialized )
       
   301         {
       
   302         return;
       
   303         }
       
   304     iListManager->SubscribeListsL( aMode == MCAPresence::ESubscribe );
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CCAPEngPresenceManager::ContactLists
       
   309 // Returns contact-list-handling interface
       
   310 // (other items were commented in a header).
       
   311 // -----------------------------------------------------------------------------
       
   312 //
       
   313 MCAContactLists* CCAPEngPresenceManager::ContactLists()
       
   314     {
       
   315     return iListManager;
       
   316     }
       
   317 
       
   318 // -----------------------------------------------------------------------------
       
   319 // CCAPEngPresenceManager::LastOperationResult
       
   320 // Returns last operation's error results
       
   321 // (other items were commented in a header).
       
   322 // -----------------------------------------------------------------------------
       
   323 //
       
   324 const CCAPresenceErrors& CCAPEngPresenceManager::LastOperationResult() const
       
   325     {
       
   326     return *iErrors;
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CCAPEngPresenceManager::SettingsAPI
       
   331 // Return settings-API
       
   332 // (other items were commented in a header).
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 MCASettings* CCAPEngPresenceManager::SettingsAPI() const
       
   336     {
       
   337     return iApplicationSettings;
       
   338     }
       
   339 
       
   340 // -----------------------------------------------------------------------------
       
   341 // CCAPEngPresenceManager::SetSettingsAPIL
       
   342 // Sets settings API
       
   343 // (other items were commented in a header).
       
   344 // -----------------------------------------------------------------------------
       
   345 //
       
   346 void CCAPEngPresenceManager::SetSettingsAPIL(
       
   347     MCASettings* aApplicationSettings )
       
   348     {
       
   349     // CCAPEngPresenceManager need settings API to function correctly
       
   350     if ( !iApplicationSettings && !aApplicationSettings )
       
   351         {
       
   352         User::Leave( ECANoSettingsAPI );
       
   353         }
       
   354 
       
   355     // If settings API handle provided, then update current handle
       
   356     if ( aApplicationSettings )
       
   357         {
       
   358         iApplicationSettings = aApplicationSettings;
       
   359         }
       
   360     }
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CCAPEngPresenceManager::RemoveWatcherL
       
   364 // Removes watcher from given user
       
   365 // (other items were commented in a header).
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 void CCAPEngPresenceManager::RemoveWatcherL( const TDesC &aWVId )
       
   369     {
       
   370     CHAT_DP_TXT( "CCAPEngPresenceManager::RemoveWatcherL" );
       
   371     CCAStorageManagerFactory::ContactListInterfaceL()->
       
   372     SetWatched( aWVId, EFalse );
       
   373     }
       
   374 
       
   375 // -----------------------------------------------------------------------------
       
   376 // CCAPEngPresenceManager::SetPresenceObserver
       
   377 // Sets presence observer
       
   378 // (other items were commented in a header).
       
   379 // -----------------------------------------------------------------------------
       
   380 //
       
   381 void CCAPEngPresenceManager::SetPresenceObserver(
       
   382     MCAPresenceObserver* aObserver )
       
   383     {
       
   384     iPresenceObserver = aObserver;
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CCAPEngPresenceManager::SetWatcherObserver
       
   389 // Sets watcher observer
       
   390 // (other items were commented in a header).
       
   391 // -----------------------------------------------------------------------------
       
   392 //
       
   393 void CCAPEngPresenceManager::SetWatcherObserver( MCAWatcherObserver* aObserver )
       
   394     {
       
   395     iWatcherObserver = aObserver;
       
   396     }
       
   397 
       
   398 // -----------------------------------------------------------------------------
       
   399 // CCAPEngPresenceManager::HandleNetworkStateL
       
   400 // To be called when network state changes
       
   401 // (other items were commented in a header).
       
   402 // -----------------------------------------------------------------------------
       
   403 //
       
   404 void CCAPEngPresenceManager::HandleNetworkStateL( TNetworkStatus aState,
       
   405                                                   const SServerPrefers& aServerPreferences,
       
   406                                                   CPEngNWSessionSlotID2* aSessionSlotID )
       
   407     {
       
   408     CHAT_DP_FUNC_ENTER( "CCAPEngPresenceManager::HandleNetworkState" );
       
   409 
       
   410     // read own states from resource
       
   411     CAPresenceUtils::ReadStatesFromResourceL(
       
   412         RSC_CHAT_VARIATION_OWN_PRESENCE_STATES, iOwnStates );
       
   413 
       
   414     // read incoming presence states from resource
       
   415     CAPresenceUtils::ReadStatesFromResourceL(
       
   416         RSC_CHAT_VARIATION_PRESENCE_STATES, iPresenceStates );
       
   417 
       
   418     iServerPrefers = aServerPreferences;
       
   419     iAliasUsed = aServerPreferences.iAliasUsed;
       
   420 
       
   421     if ( aState != EUpdateBrand )
       
   422         {
       
   423         // notification of branding must not keep the state
       
   424         // changed, but should be returned to the existing state
       
   425         iNetworkState = aState;
       
   426         }
       
   427 
       
   428     if ( aState == ELoggedIn )
       
   429         {
       
   430         // initialize own presence observer, if one is queued
       
   431         InitializeOwnPresenceObserverL();
       
   432         // check if we have cached values
       
   433         if ( iCachedAppSettingsAuthValue )
       
   434             {
       
   435             SetPresenceAuthorizationL( iCachedAppSettingsAuthValue );
       
   436             iCachedAppSettingsAuthValue = 0;
       
   437             }
       
   438         if ( iCachedStatus )
       
   439             {
       
   440             ChangeStatusL( iCachedPresenceStatus, *iCachedStatusText );
       
   441             iCachedStatus = EFalse;
       
   442             delete iCachedStatusText;
       
   443             iCachedStatusText = NULL;
       
   444             }
       
   445         else if ( iCachedStatusText )
       
   446             {
       
   447             ChangeStatusMessageL( *iCachedStatusText );
       
   448             delete iCachedStatusText;
       
   449             iCachedStatusText = NULL;
       
   450             }
       
   451         }
       
   452 
       
   453     // update own state and notify observers
       
   454     NotifyOwnPresenceObserverL();
       
   455 
       
   456     if ( aState == EUpdateBrand )
       
   457         {
       
   458         iRAUsed = aServerPreferences.iReactiveAuthorization;
       
   459         iAliasUsed = aServerPreferences.iAliasUsed;
       
   460         return;
       
   461         }
       
   462 
       
   463     if ( iListManager )
       
   464         {
       
   465         iListManager->SetLoggedIn( iNetworkState == ELoggedIn, aSessionSlotID );
       
   466         }
       
   467 
       
   468     if ( !IsLoggedIn() )
       
   469         {
       
   470         // logging out
       
   471         CancelPendingRequests();
       
   472 
       
   473         // stop the RA notifier
       
   474         if ( iRANotifier )
       
   475             {
       
   476             iRANotifier->Stop();
       
   477             }
       
   478         }
       
   479 
       
   480     CHAT_DP_FUNC_DONE( "CCAPEngPresenceManager::HandleNetworkState" );
       
   481     }
       
   482 
       
   483 // -----------------------------------------------------------------------------
       
   484 // CCAPEngPresenceManager::InitializePEngAPIL
       
   485 // Handles changes in setting values
       
   486 // (other items were commented in a header).
       
   487 // -----------------------------------------------------------------------------
       
   488 //
       
   489 void CCAPEngPresenceManager::InitializePEngAPIL()
       
   490     {
       
   491     // we can straight delete the old one, since
       
   492     // if creating does not work, then we cannot do anything anyway
       
   493     delete iListManager;
       
   494     iListManager = NULL;
       
   495     // Create list manager
       
   496     iListManager = CCAPEngListManager::NewL(
       
   497                        *iApplicationSettings,
       
   498                        iAttributes,
       
   499                        *this,             // presence observer
       
   500                        iSessionSlotID,
       
   501                        *this );           // presence updater
       
   502 
       
   503     delete iPEngAttributeTransaction;
       
   504     iPEngAttributeTransaction = NULL;
       
   505     // Create publisher
       
   506     iPEngAttributeTransaction =
       
   507         CPEngAttributeTransaction2::NewL( *iSessionSlotID );
       
   508 
       
   509     delete iPEngAttributeStore;
       
   510     iPEngAttributeStore = NULL;
       
   511     // Create attribute store
       
   512     iPEngAttributeStore = CPEngAttributeStore2::NewL( *iSessionSlotID );
       
   513 
       
   514     // reactive authorization stuffs
       
   515     delete iRAStore;
       
   516     iRAStore = NULL;
       
   517     iRAStore = CPEngReactAuthStore::NewL( *iSessionSlotID );
       
   518 
       
   519     delete iRANotifier;
       
   520     iRANotifier = NULL;
       
   521     iRANotifier = CPEngReactAuthNotifier::NewL( *iSessionSlotID );
       
   522     iRANotifier->AddObserver( *this );
       
   523 
       
   524     delete iRATransaction;
       
   525     iRATransaction = NULL;
       
   526     iRATransaction = CPEngReactAuthTransaction::NewL( *iSessionSlotID );
       
   527 
       
   528     iPEngAPIInitialized = ETrue;
       
   529 
       
   530     MCAStoredContacts* contacts =
       
   531         CCAStorageManagerFactory::ContactListInterfaceL();
       
   532     contacts->OwnStatus().SetUserIdL( iSessionSlotID->UserId() );
       
   533     contacts->OwnStatus().SetAliasL( KNullDesC );
       
   534     iListManager->SetLoggedIn( ETrue, iSessionSlotID );
       
   535     }
       
   536 
       
   537 // -----------------------------------------------------------------------------
       
   538 // CCAPEngPresenceManager::HandleSettingsChangeL
       
   539 // Handles changes in setting values
       
   540 // (other items were commented in a header).
       
   541 // -----------------------------------------------------------------------------
       
   542 //
       
   543 void CCAPEngPresenceManager::HandleSettingsChangeL( TInt aChangedSettingEnum )
       
   544     {
       
   545     if ( ! IsLoggedIn() )
       
   546         {
       
   547         // we might not have a server, unless we're logged in, so we can't do
       
   548         // any presence stuff
       
   549         return;
       
   550         }
       
   551 
       
   552     // Presence update mode change
       
   553     if ( aChangedSettingEnum == MCASettings::EAutomaticPresenceUpdate )
       
   554         {
       
   555         if ( iApplicationSettings->Value(
       
   556                  MCASettings::EAutomaticPresenceUpdate ) )
       
   557             {
       
   558             SubscribeFriendsL( MCAPresence::ESubscribe );
       
   559             }
       
   560         else
       
   561             {
       
   562             SubscribeFriendsL( MCAPresence::EUnSubscribe );
       
   563             }
       
   564         }
       
   565 
       
   566     if ( aChangedSettingEnum == MCASettings::EOwnAlias )
       
   567         {
       
   568         // this branch will not get done unless alias usage has been enabled
       
   569         // through resource variation.
       
   570         UpdateAliasAttributeL();
       
   571         }
       
   572 
       
   573     // Presence authorization mode change
       
   574     if ( aChangedSettingEnum == MCASettings::EAuthorizeIMPresence )
       
   575         {
       
   576         SetPresenceAuthorizationL( iApplicationSettings->Value(
       
   577                                        MCASettings::EAuthorizeIMPresence ) );
       
   578         }
       
   579     }
       
   580 
       
   581 // -----------------------------------------------------------------------------
       
   582 // CCAPEngPresenceManager::HandleAttributeTransactionError
       
   583 // Handler for fetcher errors
       
   584 // (other items were commented in a header).
       
   585 // -----------------------------------------------------------------------------
       
   586 //
       
   587 void CCAPEngPresenceManager::HandleAttributeTransactionError(
       
   588     TInt aError,
       
   589     CPEngAttributeTransaction2& /*aTransaction*/,
       
   590     TInt aTransactionOperation )
       
   591     {
       
   592     if ( aTransactionOperation == EPEngTransOpOwnAttributePublish )
       
   593         {
       
   594         CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::\
       
   595                               HandlePresencePublishError, error %d" ), aError );
       
   596         iRequestQueue.ResponseReceived( TCARequestQueue::EPublisherWait );
       
   597         }
       
   598     else if ( ( aTransactionOperation == EPEngTransOpAttributeFetchToCache ) ||
       
   599               ( aTransactionOperation == EPEngTransOpAttributeFetchToObjects ) )
       
   600         {
       
   601         CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::HandleAttributeFetchError, \
       
   602                               error = %d" ), aError );
       
   603         iRequestQueue.ResponseReceived( TCARequestQueue::EFetcherWait );
       
   604 
       
   605         // delete previous results, so that we don't accidentally use them
       
   606         if ( aTransactionOperation == EPEngTransOpAttributeFetchToObjects )
       
   607             {
       
   608             iFetchObjects.ResetAndDestroy();
       
   609             }
       
   610         }
       
   611 
       
   612     iOperationError = CAPresenceUtils::MapErrorPECtoCSP( aError );
       
   613     }
       
   614 
       
   615 // -----------------------------------------------------------------------------
       
   616 // CCAPEngPresenceManager::HandleAttributeTransactionCompleteL
       
   617 // Handler for fetcher complete
       
   618 // (other items were commented in a header).
       
   619 // -----------------------------------------------------------------------------
       
   620 //
       
   621 void CCAPEngPresenceManager::HandleAttributeTransactionCompleteL(
       
   622     MPEngTransactionStatus2& aStatus,
       
   623     CPEngAttributeTransaction2& aTransaction,
       
   624     TInt aTransactionOperation )
       
   625     {
       
   626     if ( aTransactionOperation == EPEngTransOpOwnAttributePublish )
       
   627         {
       
   628         CHAT_DP_TXT( "CCAPEngPresenceManager::HandlePresencePublishCompletedL" );
       
   629         NotifyOwnPresenceObserverL();
       
   630         iRequestQueue.ResponseReceived( TCARequestQueue::EPublisherWait );
       
   631         }
       
   632     else if ( aTransactionOperation == EPEngTransOpAttributeFetchToCache )
       
   633         {
       
   634         CHAT_DP_TXT( "CCAPEngPresenceManager::HandleAttributeFetchCompletedL" );
       
   635         iRequestQueue.ResponseReceived( TCARequestQueue::EFetcherWait );
       
   636         }
       
   637     else if ( aTransactionOperation == EPEngTransOpAttributeFetchToObjects )
       
   638         {
       
   639         CHAT_DP_TXT( "CCAPEngPresenceManager::HandleAttributeFetchCompletedL" );
       
   640         iFetchObjects.ResetAndDestroy();
       
   641         aTransaction.GetFetchedAttributes( iFetchObjects );
       
   642         iRequestQueue.ResponseReceived( TCARequestQueue::EFetcherWait );
       
   643         }
       
   644     iOperationError = CAPresenceUtils::MapErrorPECtoCSP( aStatus.Status() );
       
   645     CAPresenceUtils::HandleTransactionStatusL( aStatus, *iErrors );
       
   646     }
       
   647 
       
   648 // -----------------------------------------------------------------------------
       
   649 // CCAPEngPresenceManager::HandlePresenceChangeL
       
   650 // Handler for attribute change event
       
   651 // (other items were commented in a header).
       
   652 // -----------------------------------------------------------------------------
       
   653 //
       
   654 void CCAPEngPresenceManager::HandlePresenceChangeL(
       
   655     CPEngPresenceNotifier2& /*aNotifier*/,
       
   656     CPEngTrackedPresenceIDs2& aChangedPresenceIDs )
       
   657     {
       
   658     CHAT_DP_TXT( "CCAPEngPresenceManager::HandlePresenceChangeL" );
       
   659 
       
   660     DecodeNotifierDataL( &aChangedPresenceIDs, NULL );
       
   661     }
       
   662 
       
   663 // -----------------------------------------------------------------------------
       
   664 // CCAPEngPresenceManager::HandlePresenceError
       
   665 // Handler for attribute notifier errors
       
   666 // (other items were commented in a header).
       
   667 // -----------------------------------------------------------------------------
       
   668 //
       
   669 void CCAPEngPresenceManager::HandlePresenceError( TInt aError,
       
   670                                                   CPEngPresenceNotifier2& /* aNotifier */ )
       
   671     {
       
   672     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::HandlePresenceNotifyError: %d" ),
       
   673              aError );
       
   674 
       
   675     iOperationError = CAPresenceUtils::MapErrorPECtoCSP( aError );
       
   676     }
       
   677 
       
   678 
       
   679 // -----------------------------------------------------------------------------
       
   680 // CCAPEngPresenceManager::DecodeFetcherDataL
       
   681 // Decodes fetcher data
       
   682 // (other items were commented in a header).
       
   683 // -----------------------------------------------------------------------------
       
   684 //
       
   685 void CCAPEngPresenceManager::DecodeFetcherDataL(
       
   686     const MDesCArray& aUserList,
       
   687     CDesCArray *aStateOnline,
       
   688     CDesCArray *aStateOffline,
       
   689     CDesCArray *aStateUnknown,
       
   690     TBool aUpdateStorage )
       
   691     {
       
   692     MCAStoredContacts* contacts = aUpdateStorage ?
       
   693                                   CCAStorageManagerFactory::ContactListInterfaceL() : NULL;
       
   694 
       
   695     User::LeaveIfError( iAttrArrays.Append( TDecodeAttrParams() ) );
       
   696     // newly created array entry must be filled completely, so no leaving allowed!
       
   697     TInt pos( iAttrArrays.Count() - 1 );
       
   698     TDecodeAttrParams& decodeParams = iAttrArrays[ pos ];
       
   699     decodeParams.iPresenceStates = &iPresenceStates;
       
   700     decodeParams.iContactStorage = contacts;
       
   701     decodeParams.iStateOnline = aStateOnline;
       
   702     decodeParams.iStateOffline = aStateOffline;
       
   703     decodeParams.iStateUnknown = aStateUnknown;
       
   704     decodeParams.iEntryIndex = 0;
       
   705     decodeParams.iServerPrefers = iServerPrefers;
       
   706     decodeParams.iWait = TCARequestQueue::EDecodeAttrWait;
       
   707     decodeParams.iWatcherObserver = NULL;
       
   708     TRAPD( err, PopulateAttrModelsL( decodeParams.iArray, &aUserList ) );
       
   709     if ( err != KErrNone )
       
   710         {
       
   711         // array entry could not be filled properly => remove it
       
   712         decodeParams.iArray.ResetAndDestroy();
       
   713         iAttrArrays.Remove( pos );
       
   714         // now it's safe to leave
       
   715         User::Leave( err );
       
   716         }
       
   717 
       
   718     iOperationError = KErrNone;
       
   719 
       
   720     if ( !iIdle->IsActive() )
       
   721         {
       
   722         iIdle->Start( TCallBack( DecodeAttrModels, this ) );
       
   723         }
       
   724 
       
   725     iRequestQueue.WaitForResponseL( TCARequestQueue::EDecodeAttrWait );
       
   726 
       
   727     User::LeaveIfError( iOperationError );
       
   728     }
       
   729 
       
   730 
       
   731 TInt CCAPEngPresenceManager::DecodeAttrModels( TAny *aInstance )
       
   732     {
       
   733     return static_cast<CCAPEngPresenceManager*>( aInstance )->
       
   734            DoDecodeAttrModels();
       
   735     }
       
   736 
       
   737 TInt CCAPEngPresenceManager::DoDecodeAttrModels()
       
   738     {
       
   739     CHAT_DP_TXT( "CCAPEngPresenceManager::DoDecodeAttrModels" );
       
   740     TBool retval( EFalse );
       
   741 
       
   742     if ( !iAttrArrays.Count() ) // No modelarray to decode.
       
   743         {
       
   744         return EFalse;
       
   745         }
       
   746 
       
   747     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::DoDecodeAttrModels, \
       
   748                           Count of attr packets %d" ), iAttrArrays.Count() );
       
   749 
       
   750     TDecodeAttrParams& decodeParams = iAttrArrays[ 0 ];
       
   751 
       
   752     TRAP( iOperationError, retval = CAPresenceUtils::DecodeAttrModelsL(
       
   753                                         decodeParams ) );
       
   754 
       
   755     if ( iOperationError != KErrNone || !retval )
       
   756         {
       
   757         // Ready.
       
   758         TCARequestQueue::TWaitCategory wait = decodeParams.iWait;
       
   759         decodeParams.iArray.ResetAndDestroy();
       
   760         iAttrArrays.Remove( 0 );
       
   761         if ( wait != TCARequestQueue::ENoWaitNeeded )
       
   762             {
       
   763             iRequestQueue.ResponseReceived( wait );
       
   764             }
       
   765 
       
   766         if ( iOperationError != KErrNone )
       
   767             {
       
   768             CActiveScheduler::Current()->Error( iOperationError );
       
   769             }
       
   770         }
       
   771 
       
   772     return TBool( iAttrArrays.Count() );
       
   773     }
       
   774 
       
   775 // -----------------------------------------------------------------------------
       
   776 // CCAPEngPresenceManager::DecodeNotifierDataL
       
   777 // Decodes notifier data
       
   778 // (other items were commented in a header).
       
   779 // -----------------------------------------------------------------------------
       
   780 //
       
   781 void CCAPEngPresenceManager::DecodeNotifierDataL(
       
   782     CPEngTrackedPresenceIDs2* aChangedPresenceIDs,
       
   783     const MDesCArray* aUserList )
       
   784     {
       
   785     CHAT_DP_TXT( "CCAPEngPresenceManager::DecodeNotifierDataL - Enter" );
       
   786 
       
   787     if ( !aChangedPresenceIDs && aUserList )
       
   788         {
       
   789         return;
       
   790         }
       
   791 
       
   792     MCAStoredContacts* contacts = CCAStorageManagerFactory::ContactListInterfaceL();
       
   793 
       
   794     User::LeaveIfError( iAttrArrays.Append( TDecodeAttrParams() ) );
       
   795     // newly created array entry must be filled completely, so no leaving allowed!
       
   796     TInt pos( iAttrArrays.Count() - 1 );
       
   797     TDecodeAttrParams& decodeParams = iAttrArrays[ pos ];
       
   798     decodeParams.iPresenceStates = &iPresenceStates;
       
   799     decodeParams.iContactStorage = contacts;
       
   800     decodeParams.iStateOnline = NULL;
       
   801     decodeParams.iStateOffline = NULL;
       
   802     decodeParams.iStateUnknown = NULL;
       
   803     decodeParams.iEntryIndex = 0;
       
   804     decodeParams.iServerPrefers = iServerPrefers;
       
   805     decodeParams.iWait = TCARequestQueue::ENoWaitNeeded;
       
   806     decodeParams.iWatcherObserver = iWatcherObserver;
       
   807     TRAPD( err, PopulateAttrModelsL( decodeParams.iArray, aUserList, aChangedPresenceIDs ) );
       
   808     if ( err != KErrNone )
       
   809         {
       
   810         // array entry could not be filled properly => remove it
       
   811         decodeParams.iArray.ResetAndDestroy();
       
   812         iAttrArrays.Remove( pos );
       
   813         // now it's safe to leave
       
   814         User::Leave( err );
       
   815         }
       
   816 
       
   817     if ( iAttributeProcessing && !iIdle->IsActive() )
       
   818         {
       
   819         iIdle->Start( TCallBack( DecodeAttrModels, this ) );
       
   820         }
       
   821 
       
   822     CHAT_DP_TXT( "CCAPEngPresenceManager::DecodeNotifierDataL - Done" );
       
   823     }
       
   824 
       
   825 // -----------------------------------------------------------------------------
       
   826 // CCAPEngPresenceManager::IsLoggedIn
       
   827 // Returns the network state of module
       
   828 // (other items were commented in a header).
       
   829 // -----------------------------------------------------------------------------
       
   830 //
       
   831 TBool CCAPEngPresenceManager::IsLoggedIn() const
       
   832     {
       
   833     return iNetworkState == ELoggedIn ;
       
   834     }
       
   835 
       
   836 // -----------------------------------------------------------------------------
       
   837 // CCAPEngPresenceManager::PopulateAttrModelsL
       
   838 // Populates attribute-model-array with given data. If aNotifierData is given,
       
   839 // then uses notifier data, otherwise uses fetcher data
       
   840 // (other items were commented in a header).
       
   841 // -----------------------------------------------------------------------------
       
   842 //
       
   843 void CCAPEngPresenceManager::PopulateAttrModelsL(
       
   844     RPointerArray<MPEngPresenceAttrModel2>& aArray,
       
   845     const MDesCArray* aUserList,
       
   846     CPEngTrackedPresenceIDs2* aChangedPresenceIDs /*=NULL*/ )
       
   847     {
       
   848     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::PopulateAttrModelsL, \
       
   849                           &aChangedPresenceIDs=%d" ), aChangedPresenceIDs );
       
   850 
       
   851     if ( !iPEngAPIInitialized )
       
   852         {
       
   853         CHAT_DP_TXT( "iPEngAPIInitialized not initialized" );
       
   854         return;
       
   855         }
       
   856 
       
   857     TLinearOrder<MPEngPresenceAttrModel2> rule(
       
   858         CAPresenceUtils::CompareAttrModelArray );
       
   859 
       
   860     if ( aChangedPresenceIDs ) // Notifier
       
   861         {
       
   862         // wrap changed ids and update models for those users
       
   863         TTrackedPresenceIds updatedIds;
       
   864         updatedIds.InitializeLC( *aChangedPresenceIDs );
       
   865         ExtractAttributeModelsForUsersL( updatedIds, aArray );
       
   866         CleanupStack::PopAndDestroy(); // updatedIds
       
   867         }
       
   868     else // Fetcher
       
   869         {
       
   870         // we are using fetcher which has fetched the attributes to cache.
       
   871         // so we must use the cache to decode the stuff
       
   872         ExtractAttributeModelsForUsersL( *aUserList, aArray );
       
   873         }
       
   874     }
       
   875 
       
   876 // -----------------------------------------------------------------------------
       
   877 // CCAPEngPresenceManager::ExtractAttributeModelL
       
   878 // (other items were commented in a header).
       
   879 // -----------------------------------------------------------------------------
       
   880 //
       
   881 void CCAPEngPresenceManager::ExtractAttributeModelL( const TDesC& aId,
       
   882                                                      RPointerArray<MPEngPresenceAttrModel2>& aArray, TUint32 aAttribute )
       
   883     {
       
   884     MPEngPresenceAttrModel2* attributeModel = NULL;
       
   885     TInt error( KErrNone );
       
   886     if ( aId.Length() == 0 )
       
   887         {
       
   888         // no id
       
   889         error = iPEngAttributeStore->GetOwnAttribute( aAttribute,
       
   890                                                       attributeModel );
       
   891         }
       
   892     else
       
   893         {
       
   894         // have id
       
   895         error = iPEngAttributeStore->GetCachedAttribute( aId, aAttribute,
       
   896                                                          attributeModel );
       
   897         }
       
   898 
       
   899     if ( error == KErrNone )
       
   900         {
       
   901         CleanupClosePushL( *attributeModel );
       
   902         aArray.AppendL( attributeModel );   // ownership transfers
       
   903         CleanupStack::Pop(); // attributeModel
       
   904         }
       
   905     else if ( ( error != KErrNotSupported ) && ( error != KErrNotFound ) )
       
   906         {
       
   907         User::Leave( error );
       
   908         }
       
   909     }
       
   910 
       
   911 // -----------------------------------------------------------------------------
       
   912 // CCAPEngPresenceManager::SetPresenceAuthorizationL
       
   913 // Sets presence authorization mode
       
   914 // (other items were commented in a header).
       
   915 // -----------------------------------------------------------------------------
       
   916 //
       
   917 void CCAPEngPresenceManager::SetPresenceAuthorizationL(
       
   918     TInt aAppSettingsAuthValue )
       
   919     {
       
   920     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::SetPresenceAuthorizationL, \
       
   921                          aAppSettingsAuthValue = %d" ), aAppSettingsAuthValue );
       
   922 
       
   923     if ( !iPEngAPIInitialized )
       
   924         {
       
   925         iCachedAppSettingsAuthValue = aAppSettingsAuthValue;
       
   926         return;
       
   927         }
       
   928 
       
   929     if ( iRAUsed )
       
   930         {
       
   931         CHAT_DP_TXT( "CCAPEngPresenceManager::SetPresenceAuthorizationL - \
       
   932                       Reactive authorization in use, ignoring call" );
       
   933         return;
       
   934         }
       
   935 
       
   936     CHAT_DP_TXT( "CCAPEngPresenceManager::SetPresenceAuthorizationL - \
       
   937                   Reactive authorization not in use" );
       
   938 
       
   939     switch ( aAppSettingsAuthValue )
       
   940         {
       
   941         case MCASettings::EAll:
       
   942             {
       
   943             CHAT_DP_TXT( "Own presence publish: to all" );
       
   944             iListManager->SetAuthorizationToAllL();
       
   945             break;
       
   946             }
       
   947         case MCASettings::EFriends:
       
   948             {
       
   949             CHAT_DP_TXT( "Own presence publish: to friends" );
       
   950             iListManager->SetAuthorizationToFriendsL();
       
   951             break;
       
   952             }
       
   953         case MCASettings::ENobody:
       
   954             {
       
   955             CHAT_DP_TXT( "Own presence publish: to nobody" );
       
   956             iListManager->SetAuthorizationToNoneL();
       
   957             break;
       
   958             }
       
   959         default:
       
   960             {
       
   961             User::Leave( KErrNotSupported );
       
   962             break;
       
   963             }
       
   964         }
       
   965     }
       
   966 
       
   967 
       
   968 // -----------------------------------------------------------------------------
       
   969 // CCAPEngPresenceManager::FetchAttributesL
       
   970 // Fetches attributes for given users
       
   971 // (other items were commented in a header).
       
   972 // -----------------------------------------------------------------------------
       
   973 //
       
   974 void CCAPEngPresenceManager::FetchAttributesL(  const MDesCArray& aUsers,
       
   975                                                 CDesCArray *aStateOnline,
       
   976                                                 CDesCArray *aStateOffline,
       
   977                                                 CDesCArray *aStateUnknown,
       
   978                                                 TBool aUpdateStorage  )
       
   979     {
       
   980     CHAT_DP_TXT( "CCAPEngPresenceManager::FetchAttributesL" );
       
   981 
       
   982     if ( IsLoggedIn() && aUsers.MdcaCount() > 0 )
       
   983         {
       
   984         CHAT_DP_TXT( "CCAPEngPresenceManager::FetchAttributesL, \
       
   985                       fetching attributes" );
       
   986 #ifdef _DEBUG
       
   987         TInt count( aUsers.MdcaCount() );
       
   988         for ( TInt i( 0 ); i < count; ++i )
       
   989             {
       
   990             TPtrC data( aUsers.MdcaPoint( i ) );
       
   991             CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::FetchAttributesL, \
       
   992                                   user %d: %S" ), i, &data );
       
   993             }
       
   994 #endif
       
   995         iOperationError = KErrNone;
       
   996 
       
   997         // Fetching of attributes may return error
       
   998         // Leave to avoid hanging in WaitResponseL
       
   999         User::LeaveIfError( iPEngAttributeTransaction->
       
  1000                             FetchAttributesToCache( aUsers,
       
  1001                                                     iAttributes.Array(),
       
  1002                                                     *this ) );
       
  1003 
       
  1004         iRequestQueue.WaitForResponseL( TCARequestQueue::EFetcherWait );
       
  1005         CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::FetchAttributesL, \
       
  1006                               attribute fetch done (%d)" ), iOperationError );
       
  1007 
       
  1008         // Decode retrieved data if we still have memory left
       
  1009         if ( iOperationError != KErrNoMemory && iOperationError != KErrDiskFull )
       
  1010             {
       
  1011             DecodeFetcherDataL( aUsers, aStateOnline, aStateOffline,
       
  1012                                 aStateUnknown, aUpdateStorage );
       
  1013             }
       
  1014 
       
  1015         User::LeaveIfError( iOperationError );
       
  1016         }
       
  1017     }
       
  1018 
       
  1019 // -----------------------------------------------------------------------------
       
  1020 // CCAPEngPresenceManager::UpdateAliasAttributeL
       
  1021 // Update alias attribute to the network.
       
  1022 // (other items were commented in a header).
       
  1023 // -----------------------------------------------------------------------------
       
  1024 //
       
  1025 void CCAPEngPresenceManager::UpdateAliasAttributeL()
       
  1026     {
       
  1027     CHAT_DP_TXT( "CCAPEngPresenceManager::UpdateAliasAttributeL" );
       
  1028 
       
  1029     if ( iNetworkState != ELoggedIn && iNetworkState != ELoggingOut )
       
  1030         {
       
  1031         CHAT_DP_TXT( "CCAPEngPresenceManager::UpdateAliasAttributeL: No active \
       
  1032                       network state -> ignore!" );
       
  1033         return;
       
  1034         }
       
  1035 
       
  1036     // check if alias is enabled at all
       
  1037     if ( ! iAliasUsed )
       
  1038         {
       
  1039         CHAT_DP_TXT( "CCAPEngPresenceManager::UpdateAliasAttributeL: Alias \
       
  1040                       support disabled" );
       
  1041         return;
       
  1042         }
       
  1043 
       
  1044     HBufC* alias = iApplicationSettings->OwnAliasL();
       
  1045     if ( !alias )
       
  1046         {
       
  1047         CHAT_DP_TXT( "CCAPEngPresenceManager::UpdateAliasAttributeL: Alias \
       
  1048                       is not defined" );
       
  1049         return;
       
  1050         }
       
  1051 
       
  1052     CleanupStack::PushL( alias );
       
  1053     MPEngPresenceAttrModel2* aliasAttr = NULL;
       
  1054     User::LeaveIfError( iPEngAttributeStore->GetAndLockOwnAttribute(
       
  1055                             KUidPrAttrAlias, aliasAttr ) );
       
  1056     CleanupClosePushL( *aliasAttr );
       
  1057     aliasAttr->SetDataDesC16L( *alias, EPEngAlias );
       
  1058 
       
  1059     CHAT_DP_TXT( "CCAPEngPresenceManager::UpdateAliasAttributeL, publishing \
       
  1060                   attribute" );
       
  1061     iOperationError = KErrNone;
       
  1062     TInt err = iPEngAttributeTransaction->PublishAndUnLockOwnAttribute(
       
  1063                    aliasAttr, *this );
       
  1064     if ( err )
       
  1065         {
       
  1066         CleanupStack::PopAndDestroy(); // aliasAttr
       
  1067         }
       
  1068     else
       
  1069         {
       
  1070         CleanupStack::Pop( ); // aliasAttr
       
  1071         }
       
  1072     CleanupStack::PopAndDestroy( alias );
       
  1073 
       
  1074     iRequestQueue.WaitForResponseL( TCARequestQueue::EPublisherWait );
       
  1075 
       
  1076     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::UpdateAliasAttributeL, \
       
  1077                           attribute publishing done (%d)" ), iOperationError );
       
  1078 
       
  1079     if ( iOperationError == ECSPInvalidPresenceValue )
       
  1080         {
       
  1081         User::Leave( iOperationError );
       
  1082         }
       
  1083     }
       
  1084 
       
  1085 // -----------------------------------------------------------------------------
       
  1086 // CCAPEngPresenceManager::SetReactiveAuthObserver
       
  1087 // Reactive authorization observer for internal events
       
  1088 // (other items were commented in a header).
       
  1089 // -----------------------------------------------------------------------------
       
  1090 //
       
  1091 void CCAPEngPresenceManager::SetReactiveAuthObserver(
       
  1092     MCAReactiveAuthObserver* aRAObserver )
       
  1093     {
       
  1094     iRAObserver = aRAObserver;
       
  1095     }
       
  1096 
       
  1097 // -----------------------------------------------------------------------------
       
  1098 // CCAPEngPresenceManager::CancelPendingRequests
       
  1099 // Fetches attributes for given users
       
  1100 // (other items were commented in a header).
       
  1101 // -----------------------------------------------------------------------------
       
  1102 //
       
  1103 void CCAPEngPresenceManager::CancelPendingRequests()
       
  1104     {
       
  1105     CHAT_DP_FUNC_ENTER( "CancelPendingRequests" );
       
  1106 
       
  1107     if ( iPEngAttributeTransaction )
       
  1108         {
       
  1109         iPEngAttributeTransaction->CancelFetchAttributes();
       
  1110         iPEngAttributeTransaction->CancelPublishOwnAttributes();
       
  1111         }
       
  1112 
       
  1113     if ( iListManager )
       
  1114         {
       
  1115         iListManager->CancelPendingRequests();
       
  1116         }
       
  1117 
       
  1118     CHAT_DP_FUNC_DONE( "CancelPendingRequests" );
       
  1119     }
       
  1120 
       
  1121 // -----------------------------------------------------------------------------
       
  1122 // CCAPEngPresenceManager::SynchronizePresenceSettingsL
       
  1123 // Make sure server state is in sync with our presence settings
       
  1124 // (other items were commented in a header).
       
  1125 // -----------------------------------------------------------------------------
       
  1126 //
       
  1127 void CCAPEngPresenceManager::SynchronizePresenceSettingsL(
       
  1128     const SServerPrefers& /* aServerPreferences */ )
       
  1129     {
       
  1130     // authorization
       
  1131     SetPresenceAuthorizationL( iApplicationSettings->Value(
       
  1132                                    MCASettings::EAuthorizeIMPresence ) );
       
  1133     }
       
  1134 
       
  1135 // -----------------------------------------------------------------------------
       
  1136 // CCAPEngPresenceManager::AliasL
       
  1137 // Fetch alias for given userid
       
  1138 // (other items were commented in a header).
       
  1139 // -----------------------------------------------------------------------------
       
  1140 //
       
  1141 HBufC* CCAPEngPresenceManager::AliasL( const TDesC& aUserId )
       
  1142     {
       
  1143     if ( aUserId.Length() == 0 )
       
  1144         {
       
  1145         // can't find alias in this case...
       
  1146         User::Leave( KErrNotFound );
       
  1147         return NULL; // some compilers demand this
       
  1148         }
       
  1149     HBufC* aliasName = NULL;
       
  1150 
       
  1151     RArray<TUint32> attribute;
       
  1152     CleanupClosePushL( attribute );
       
  1153     attribute.AppendL( KUidPrAttrAlias );
       
  1154 
       
  1155     // get alias from network
       
  1156     iOperationError = KErrNone;
       
  1157 
       
  1158     iPEngAttributeTransaction->FetchAttributesToObjects( aUserId,
       
  1159                                                          attribute.Array(),
       
  1160                                                          *this );
       
  1161     iRequestQueue.WaitForResponseL( TCARequestQueue::EFetcherWait );
       
  1162 
       
  1163     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::AliasL, attribute fetch \
       
  1164                           done (%d)" ), iOperationError );
       
  1165     CleanupStack::PopAndDestroy(); // attribute.Close()
       
  1166 
       
  1167     if ( !iOperationError )
       
  1168         {
       
  1169         // Fetched attributes are in iFetchObjects
       
  1170         const MPEngPresenceAttrModel2* aliasAttr =
       
  1171             CAPresenceUtils::FindAttr( KUidPrAttrAlias, iFetchObjects );
       
  1172 
       
  1173         if ( aliasAttr )
       
  1174             {
       
  1175             // got an alias from the network
       
  1176             TPtrC text = aliasAttr->DataDesC16( EPEngAlias );
       
  1177             aliasName = text.AllocL();
       
  1178             }
       
  1179         else
       
  1180             {
       
  1181             // didn't get an alias, so use an empty alias
       
  1182             aliasName = KNullDesC().AllocL();
       
  1183             }
       
  1184         }
       
  1185     else
       
  1186         {
       
  1187         // got some problem
       
  1188         User::LeaveIfError( iOperationError );
       
  1189         return NULL; // some compilers demand this
       
  1190         }
       
  1191 
       
  1192     // release fetched attributes
       
  1193     iFetchObjects.ResetAndDestroy();
       
  1194 
       
  1195     // ownership transfers
       
  1196     return aliasName;
       
  1197     }
       
  1198 
       
  1199 // -----------------------------------------------------------------------------
       
  1200 // CCAPEngPresenceManager::AliasL
       
  1201 // Fetch alias for given userid
       
  1202 // (other items were commented in a header).
       
  1203 // -----------------------------------------------------------------------------
       
  1204 //
       
  1205 HBufC* CCAPEngPresenceManager::AliasL( MCAStoredContact* aContact )
       
  1206     {
       
  1207     if ( !aContact )
       
  1208         {
       
  1209         return NULL;
       
  1210         }
       
  1211 
       
  1212     HBufC* aliasName = AliasL( aContact->UserId() );
       
  1213     CleanupStack::PushL( aliasName );
       
  1214 
       
  1215     if ( KErrNone != aContact->Alias().Compare( *aliasName ) )
       
  1216         {
       
  1217         CCAStorageManagerFactory::ContactListInterfaceL()->ResortContactInAll( aContact );
       
  1218         aContact->SetAliasL( *aliasName );
       
  1219         aContact->SignalChanges();
       
  1220         }
       
  1221 
       
  1222     CleanupStack::Pop( aliasName );
       
  1223 
       
  1224     // ownership transfers
       
  1225     return aliasName;
       
  1226     }
       
  1227 
       
  1228 // -----------------------------------------------------------------------------
       
  1229 // CCAPEngPresenceManager::ChangeStatusL
       
  1230 // (other items were commented in a header).
       
  1231 // -----------------------------------------------------------------------------
       
  1232 //
       
  1233 TInt CCAPEngPresenceManager::ChangeStatusL( TPresenceStatus aStatus,
       
  1234                                             const TDesC& aStatusMessage )
       
  1235     {
       
  1236     CHAT_DP_FUNC_ENTER( "ChangeStatusL" );
       
  1237 
       
  1238     if ( !iPEngAPIInitialized )
       
  1239         {
       
  1240         // cache the status
       
  1241         iCachedPresenceStatus = aStatus;
       
  1242         HBufC* tempStatusText = aStatusMessage.AllocL();
       
  1243         delete iCachedStatusText;
       
  1244         iCachedStatusText = tempStatusText;
       
  1245         iCachedStatus = ETrue;
       
  1246         return KErrNone;
       
  1247         }
       
  1248 
       
  1249     RPointerArray<MPEngPresenceAttrModel2> models;
       
  1250     CleanupStack::PushL( TCleanupItem( CAPresenceUtils::DestroyCloseModelArray, &models ) );
       
  1251 
       
  1252     CCAState* state = CAPresenceUtils::FindStateL( aStatus, iOwnStates );
       
  1253 
       
  1254     if ( state )
       
  1255         {
       
  1256         TInt attrCount( state->iAttributes.Count() );
       
  1257         for ( TInt i( 0 ); i < attrCount; ++i )
       
  1258             {
       
  1259             TCAAttribute& attr = state->iAttributes[i];
       
  1260             MPEngPresenceAttrModel2* pengAttr = NULL;
       
  1261             User::LeaveIfError( iPEngAttributeStore->GetAndLockOwnAttribute(
       
  1262                                     attr.iAttribute, pengAttr ) );
       
  1263             CleanupClosePushL( *pengAttr );
       
  1264 
       
  1265             if ( attr.iAttribute == KUidPrAttrStatusText )
       
  1266                 {
       
  1267                 // special case, set the status text
       
  1268                 pengAttr->SetDataDesC16L( aStatusMessage, EPEngStatusText );
       
  1269                 }
       
  1270             else
       
  1271                 {
       
  1272                 // otherwise set the value from list
       
  1273                 pengAttr->SetDataIntL( attr.iData, attr.iField, attr.iGroup );
       
  1274                 }
       
  1275 
       
  1276             // set qualifier and append to list
       
  1277             pengAttr->SetQualifier( attr.iQualifier );
       
  1278             models.AppendL( pengAttr );
       
  1279             CleanupStack::Pop(); // pengAttr
       
  1280             }
       
  1281         }
       
  1282 
       
  1283 #ifdef _DEBUG
       
  1284     else
       
  1285         {
       
  1286         CHAT_DP_TXT( "!!UI offered presence state that was not defined in resource!!" );
       
  1287         }
       
  1288 #endif // _DEBUG        
       
  1289 
       
  1290     iOperationError = KErrNone;
       
  1291     if ( models.Count() > 0 )
       
  1292         {
       
  1293         // we have some attributes, publish them
       
  1294         CHAT_DP_TXT( "CCAPEngPresenceManager::ChangeStatusL, publishing attributes" );
       
  1295         User::LeaveIfError( iPEngAttributeTransaction->PublishAndUnLockOwnAttributes(
       
  1296                                 models,
       
  1297                                 *this ) );
       
  1298         CleanupStack::Pop(); // models
       
  1299 
       
  1300         iRequestQueue.WaitForResponseL( TCARequestQueue::EPublisherWait );
       
  1301         CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::ChangeStatusL, attribute \
       
  1302                               publishing done (%d)" ), iOperationError );
       
  1303         }
       
  1304     else
       
  1305         {
       
  1306         CleanupStack::PopAndDestroy(); // models
       
  1307         }
       
  1308 
       
  1309     CHAT_DP_FUNC_DONE( "ChangeStatusL" );
       
  1310     return iOperationError;
       
  1311     }
       
  1312 
       
  1313 
       
  1314 // -----------------------------------------------------------------------------
       
  1315 // CCAPEngPresenceManager::ChangeStatusMessageL
       
  1316 // (other items were commented in a header).
       
  1317 // -----------------------------------------------------------------------------
       
  1318 //
       
  1319 TInt CCAPEngPresenceManager::ChangeStatusMessageL( const TDesC& aStatusMessage )
       
  1320     {
       
  1321     CHAT_DP_FUNC_ENTER( "ChangeStatusMessageL" );
       
  1322 
       
  1323     if ( !iPEngAPIInitialized )
       
  1324         {
       
  1325         // cache the status text
       
  1326         HBufC* tempText = aStatusMessage.AllocL();
       
  1327         delete iCachedStatusText;
       
  1328         iCachedStatusText = tempText;
       
  1329         return KErrNone;
       
  1330         }
       
  1331 
       
  1332     MPEngPresenceAttrModel2* statusMessage = NULL;
       
  1333     User::LeaveIfError( iPEngAttributeStore->GetAndLockOwnAttribute(
       
  1334                             KUidPrAttrStatusText, statusMessage ) );
       
  1335     CleanupStack::PushL( statusMessage );
       
  1336 
       
  1337     statusMessage->SetDataDesC16L( aStatusMessage, EPEngStatusText );
       
  1338     statusMessage->SetQualifier( ETrue );
       
  1339     CHAT_DP_TXT( "CCAPEngPresenceManager::ChangeStatusMessageL, publishing \
       
  1340                   attributes" );
       
  1341     iOperationError = KErrNone;
       
  1342     iPEngAttributeTransaction->PublishAndUnLockOwnAttribute( statusMessage,
       
  1343                                                              *this );
       
  1344     CleanupStack::Pop( ); // statusMessage
       
  1345     iRequestQueue.WaitForResponseL( TCARequestQueue::EPublisherWait );
       
  1346     CHAT_DP( D_CHAT_LIT( "CCAPEngPresenceManager::ChangeStatusMessageL, \
       
  1347                           attribute publishing done (%d)" ), iOperationError );
       
  1348 
       
  1349     CHAT_DP_FUNC_DONE( "ChangeStatusMessageL" );
       
  1350     return iOperationError;
       
  1351     }
       
  1352 
       
  1353 // -----------------------------------------------------------------------------
       
  1354 // CCAPEngPresenceManager::StatusL
       
  1355 // (other items were commented in a header).
       
  1356 // -----------------------------------------------------------------------------
       
  1357 //
       
  1358 MCAPresence::TPresenceStatus CCAPEngPresenceManager::StatusL()
       
  1359     {
       
  1360     CHAT_DP_FUNC_ENTER( "StatusL" );
       
  1361     MCAPresence::TPresenceStatus currentStatus( MCAPresence::EOffline );
       
  1362 
       
  1363     if ( !iPEngAPIInitialized )
       
  1364         {
       
  1365         // if the api is not initialized -> we are offline
       
  1366         return currentStatus;
       
  1367         }
       
  1368 
       
  1369     RPointerArray<MPEngPresenceAttrModel2> attributes;
       
  1370     CAPresenceUtils::PushModelArrayL( attributes, ETrue );
       
  1371 
       
  1372     MPEngPresenceAttrModel2* userAvailability = NULL;
       
  1373     User::LeaveIfError( iPEngAttributeStore->GetOwnAttribute(
       
  1374                             KUidPrAttrUserAvailability, userAvailability ) );
       
  1375     CleanupClosePushL( *userAvailability );
       
  1376     attributes.AppendL( userAvailability );
       
  1377     CleanupStack::Pop(); // userAvailability
       
  1378 
       
  1379     MPEngPresenceAttrModel2* commCap = NULL;
       
  1380     User::LeaveIfError( iPEngAttributeStore->GetOwnAttribute(
       
  1381                             KUidPrAttrCommCap, commCap ) );
       
  1382     CleanupClosePushL( *commCap );
       
  1383     attributes.AppendL( commCap );
       
  1384     CleanupStack::Pop(); // commCap
       
  1385 
       
  1386     MPEngPresenceAttrModel2* onlineStatus = NULL;
       
  1387     User::LeaveIfError( iPEngAttributeStore->GetOwnAttribute(
       
  1388                             KUidPrAttrOnlineStatus, onlineStatus ) );
       
  1389     CleanupClosePushL( *onlineStatus );
       
  1390     attributes.AppendL( onlineStatus );
       
  1391     CleanupStack::Pop(); // onlineStatus
       
  1392 
       
  1393     MPEngPresenceAttrModel2* clientInfo = NULL;
       
  1394     User::LeaveIfError( iPEngAttributeStore->GetOwnAttribute(
       
  1395                             KUidPrAttrClientInfo, clientInfo ) );
       
  1396     CleanupClosePushL( *clientInfo );
       
  1397     attributes.AppendL( clientInfo );
       
  1398     CleanupStack::Pop(); // clientInfo
       
  1399 
       
  1400     currentStatus =
       
  1401         CAPresenceUtils::DecodeOnlineState(
       
  1402             iPresenceStates, attributes );
       
  1403 
       
  1404     CleanupStack::PopAndDestroy(); // attributes
       
  1405 
       
  1406     CHAT_DP_FUNC_DONE( "StatusL" );
       
  1407     return currentStatus;
       
  1408     }
       
  1409 
       
  1410 
       
  1411 // -----------------------------------------------------------------------------
       
  1412 // CCAPEngPresenceManager::AddOwnPresenceStatusObserver
       
  1413 // (other items were commented in a header).
       
  1414 // -----------------------------------------------------------------------------
       
  1415 //
       
  1416 void CCAPEngPresenceManager::AddOwnPresenceStatusObserverL(
       
  1417     MCAPresenceObserver* aObserver )
       
  1418     {
       
  1419     iOwnStatusObserver = aObserver;
       
  1420     if ( !iSessionSlotID )
       
  1421         {
       
  1422         // we are not yet online so we queue this observer
       
  1423         iObserverQueued = ETrue;
       
  1424         return;
       
  1425         }
       
  1426     InitializeOwnPresenceObserverL();
       
  1427     }
       
  1428 
       
  1429 // -----------------------------------------------------------------------------
       
  1430 // CCAPEngPresenceManager::InitializeOwnPresenceObserverL
       
  1431 // (other items were commented in a header).
       
  1432 // -----------------------------------------------------------------------------
       
  1433 //
       
  1434 void CCAPEngPresenceManager::InitializeOwnPresenceObserverL()
       
  1435     {
       
  1436     if ( !iObserverQueued )
       
  1437         {
       
  1438         // no queued observer, no need to do anything
       
  1439         return;
       
  1440         }
       
  1441     // we don't have to use a temp variable since this
       
  1442     // notifier has to be always created and if creating fails we cannot
       
  1443     // use the old notifier
       
  1444 
       
  1445     if ( iOwnPresenceNotifier )
       
  1446         {
       
  1447         iOwnPresenceNotifier->RemoveObserver( *this );
       
  1448         }
       
  1449 
       
  1450     delete iOwnPresenceNotifier;
       
  1451     iOwnPresenceNotifier = NULL;
       
  1452 
       
  1453     iOwnPresenceNotifier = CPEngPresenceNotifier2::NewL( *iSessionSlotID );
       
  1454     iOwnPresenceNotifier->AddObserver( *this );
       
  1455 
       
  1456     CArrayFixFlat<TUint32>* observedAttributes =
       
  1457         new CArrayFixFlat<TUint32>( iAttributes.Count() ); // N observed attributes
       
  1458     CleanupStack::PushL( observedAttributes );
       
  1459 
       
  1460     // just add all attributes to be observed
       
  1461     for ( TInt i = iAttributes.Count() - 1; i >= 0; i-- )
       
  1462         {
       
  1463         observedAttributes->AppendL( iAttributes[ i ] );
       
  1464         }
       
  1465     iOwnPresenceNotifier->Start( KPEngUserOwnPresenceId(),
       
  1466                                  observedAttributes->Array() );
       
  1467 
       
  1468     CleanupStack::PopAndDestroy( observedAttributes );
       
  1469     }
       
  1470 
       
  1471 // -----------------------------------------------------------------------------
       
  1472 // CCAPEngPresenceManager::NotifyOwnPresenceObserver
       
  1473 // (other items were commented in a header).
       
  1474 // -----------------------------------------------------------------------------
       
  1475 //
       
  1476 void CCAPEngPresenceManager::NotifyOwnPresenceObserverL()
       
  1477     {
       
  1478     TPresenceStatus status = StatusL();
       
  1479     TStorageManagerGlobals::TPresenceStatus storageStatus;
       
  1480     switch ( status )
       
  1481         {
       
  1482         case MCAPresence::EOnline:
       
  1483             {
       
  1484             storageStatus = TStorageManagerGlobals::EOnline;
       
  1485             break;
       
  1486             }
       
  1487         case MCAPresence::EAway:
       
  1488             {
       
  1489             storageStatus = TStorageManagerGlobals::EAway;
       
  1490             break;
       
  1491             }
       
  1492         case MCAPresence::EBusy:
       
  1493             {
       
  1494             storageStatus = TStorageManagerGlobals::EBusy;
       
  1495             break;
       
  1496             }
       
  1497         case MCAPresence::EOffline: // Flowthrough
       
  1498         default:
       
  1499             {
       
  1500             // If we're online, offline means invisible
       
  1501             storageStatus = IsLoggedIn() ?
       
  1502                             TStorageManagerGlobals::EInvisible :
       
  1503                             TStorageManagerGlobals::EOffline;
       
  1504             break;
       
  1505             }
       
  1506         }
       
  1507 
       
  1508     if ( iPEngAttributeStore )
       
  1509         {
       
  1510         // Get own status message
       
  1511         MPEngPresenceAttrModel2* statusMessage = NULL;
       
  1512 
       
  1513         TInt err = iPEngAttributeStore->GetOwnAttribute(
       
  1514                        KUidPrAttrStatusText, statusMessage );
       
  1515         CleanupClosePushL( *statusMessage );
       
  1516 
       
  1517         TPtrC16 statusText( KNullDesC );
       
  1518         if ( err == KErrNone && statusMessage )
       
  1519             {
       
  1520             statusText.Set( statusMessage->DataDesC16( EPEngStatusText ) );
       
  1521             }
       
  1522 
       
  1523         // Update own status
       
  1524         MCAStoredContact& ownStatus =
       
  1525             CCAStorageManagerFactory::ContactListInterfaceL()->OwnStatus();
       
  1526 
       
  1527         ownStatus.SetOnlineStatus( storageStatus );
       
  1528         ownStatus.SetStatusTextL( statusText );
       
  1529         CleanupStack::PopAndDestroy( statusMessage ); // now we can delete this
       
  1530 
       
  1531         ownStatus.SignalChanges();
       
  1532         }
       
  1533 
       
  1534     if ( iOwnStatusObserver )
       
  1535         {
       
  1536         // notify observer with current status
       
  1537         iOwnStatusObserver->HandleOwnStatusChange( status );
       
  1538         }
       
  1539     }
       
  1540 
       
  1541 // -----------------------------------------------------------------------------
       
  1542 // CCAPEngPresenceManager::NotifyOwnPresenceObserver
       
  1543 // (other items were commented in a header).
       
  1544 // -----------------------------------------------------------------------------
       
  1545 //
       
  1546 void CCAPEngPresenceManager::InitializePresenceAPIL(
       
  1547     CPEngNWSessionSlotID2& aSessionSlotID )
       
  1548     {
       
  1549     delete iSessionSlotID;
       
  1550     iSessionSlotID = NULL;
       
  1551     iSessionSlotID = aSessionSlotID.CloneL();
       
  1552 
       
  1553     InitializePEngAPIL();
       
  1554     }
       
  1555 
       
  1556 // -----------------------------------------------------------------------------
       
  1557 // CCAPEngPresenceManager::AddAttributeL
       
  1558 // (other items were commented in a header).
       
  1559 // -----------------------------------------------------------------------------
       
  1560 //
       
  1561 void CCAPEngPresenceManager::AddAttributeL( TInt aAttribute )
       
  1562     {
       
  1563     // add attribute to the end of the attribute list
       
  1564     iAttributes.Append( InternalToPECAttrL(
       
  1565                             ( MCAPresence::TDynamicAttributes ) aAttribute ) );
       
  1566 
       
  1567     // restart the notifier, if any
       
  1568     iListManager->ReplaceAttributesL( iAttributes );
       
  1569 
       
  1570     // next time something is done, the attributes should be updated
       
  1571     }
       
  1572 
       
  1573 // -----------------------------------------------------------------------------
       
  1574 // CCAPEngPresenceManager::RemoveAttributeL
       
  1575 // (other items were commented in a header).
       
  1576 // -----------------------------------------------------------------------------
       
  1577 //
       
  1578 void CCAPEngPresenceManager::RemoveAttributeL( TInt aAttribute )
       
  1579     {
       
  1580     // remove attribute from the attribute list
       
  1581     TUint32 pecAttr =
       
  1582         InternalToPECAttrL( ( MCAPresence::TDynamicAttributes ) aAttribute );
       
  1583     for ( TInt i = iAttributes.Count() - 1; i >= 0; i-- )
       
  1584         {
       
  1585         if ( iAttributes[ i ] == pecAttr )
       
  1586             {
       
  1587             iAttributes.Remove( i );
       
  1588             }
       
  1589         }
       
  1590 
       
  1591     // restart the notifier, if any
       
  1592     iAttributes.Compress();
       
  1593     iListManager->ReplaceAttributesL( iAttributes );
       
  1594 
       
  1595     // next time something is done, the attributes should be updated
       
  1596     }
       
  1597 
       
  1598 // -----------------------------------------------------------------------------
       
  1599 // CCAPEngPresenceManager::ResetAttributesL
       
  1600 // (other items were commented in a header).
       
  1601 // -----------------------------------------------------------------------------
       
  1602 //
       
  1603 void CCAPEngPresenceManager::ResetAttributesL(
       
  1604     TBool aNotifyListManager /* = ETrue */ )
       
  1605     {
       
  1606     // always supported list of "base attributes",
       
  1607     // dynamically branded attributes are not to be added here
       
  1608     iAttributes.Reset();
       
  1609     User::LeaveIfError( iAttributes.Append( KUidPrAttrOnlineStatus ) );
       
  1610     User::LeaveIfError( iAttributes.Append( KUidPrAttrCommCap ) );
       
  1611     User::LeaveIfError( iAttributes.Append( KUidPrAttrUserAvailability ) );
       
  1612     User::LeaveIfError( iAttributes.Append( KUidPrAttrStatusText ) );
       
  1613     User::LeaveIfError( iAttributes.Append( KUidPrAttrClientInfo ) );
       
  1614 
       
  1615     if ( aNotifyListManager )
       
  1616         {
       
  1617         iListManager->ReplaceAttributesL( iAttributes );
       
  1618         }
       
  1619     }
       
  1620 
       
  1621 // -----------------------------------------------------------------------------
       
  1622 // CCAPEngPresenceManager::InternalToPECAttrL
       
  1623 // (other items were commented in a header).
       
  1624 // -----------------------------------------------------------------------------
       
  1625 //
       
  1626 TUint32 CCAPEngPresenceManager::InternalToPECAttrL(
       
  1627     TDynamicAttributes aAttribute )
       
  1628     {
       
  1629     switch ( aAttribute )
       
  1630         {
       
  1631         case EAliasAttr :
       
  1632             {
       
  1633             return KUidPrAttrAlias;
       
  1634             }
       
  1635         default :
       
  1636             {
       
  1637             User::Leave( KErrNotFound );
       
  1638             break;
       
  1639             }
       
  1640         }
       
  1641     // this is never reached
       
  1642     return 0;
       
  1643     }
       
  1644 
       
  1645 // reactive authorization things
       
  1646 //
       
  1647 // -----------------------------------------------------------------------------
       
  1648 // CCAPEngPresenceManager::HandlePendingAuthorizationReqL
       
  1649 // (other items were commented in a header).
       
  1650 // -----------------------------------------------------------------------------
       
  1651 //
       
  1652 void CCAPEngPresenceManager::HandlePendingAuthorizationReqL(
       
  1653     CPEngReactAuthNotifier& /* aNotifier */,
       
  1654     TArray< MPEngAuthorizationRequest* >& aPendingAuthReqs )
       
  1655     {
       
  1656     CHAT_DP_FUNC_ENTER( "HandlePendingAuthorizationReqL" );
       
  1657 
       
  1658     if ( aPendingAuthReqs.Count() )
       
  1659         {
       
  1660         iRAObserver->HandleRAPendingReqL();
       
  1661         }
       
  1662 
       
  1663     CHAT_DP_FUNC_DONE( "HandlePendingAuthorizationReqL" );
       
  1664     }
       
  1665 
       
  1666 // -----------------------------------------------------------------------------
       
  1667 // CCAPEngPresenceManager::HandlerAuthorizationStatusL
       
  1668 // (other items were commented in a header).
       
  1669 // -----------------------------------------------------------------------------
       
  1670 //
       
  1671 void CCAPEngPresenceManager::HandlerAuthorizationStatusL(
       
  1672     CPEngReactAuthNotifier& /* aNotifier */,
       
  1673     TArray< const MPEngAuthorizationStatus* > /*aAuthStatuses*/ )
       
  1674     {
       
  1675     CHAT_DP_FUNC_ENTER( "HandlerAuthorizationStatusL" );
       
  1676 
       
  1677     // will be done only once, since the observer will
       
  1678     // ask for all of the requests once it's called.
       
  1679     iRAObserver->HandleRAStatusL();
       
  1680 
       
  1681     CHAT_DP_FUNC_DONE( "HandlerAuthorizationStatusL" );
       
  1682     }
       
  1683 
       
  1684 // -----------------------------------------------------------------------------
       
  1685 // CCAPEngPresenceManager::HandleRAError
       
  1686 // (other items were commented in a header).
       
  1687 // -----------------------------------------------------------------------------
       
  1688 //
       
  1689 void CCAPEngPresenceManager::HandleReactAuthError(
       
  1690     TInt aError,
       
  1691     CPEngReactAuthNotifier& /* aNotifier */ )
       
  1692     {
       
  1693     CHAT_DP_FUNC_ENTER( "HandleReactAuthError" );
       
  1694 
       
  1695     iRAObserver->HandleRAError( aError );
       
  1696 
       
  1697     CHAT_DP_FUNC_DONE( "HandleReactAuthError" );
       
  1698     }
       
  1699 
       
  1700 // reactive authorization transactions
       
  1701 
       
  1702 // -----------------------------------------------------------------------------
       
  1703 // CCAPEngPresenceManager::HandleReactAuthTransactionCompleteL
       
  1704 // (other items were commented in a header).
       
  1705 // -----------------------------------------------------------------------------
       
  1706 //
       
  1707 void CCAPEngPresenceManager::HandleReactAuthTransactionCompleteL(
       
  1708     MPEngTransactionStatus2& /* aStatus */,
       
  1709     CPEngReactAuthTransaction& /* aTransaction */,
       
  1710     TInt /* aTransactionOperation */ )
       
  1711     {
       
  1712     CHAT_DP_FUNC_ENTER( "HandleReactAuthTransactionCompleteL" );
       
  1713 
       
  1714     iRequestQueue.ResponseReceived( TCARequestQueue::EReactiveAuthWait );
       
  1715 
       
  1716     CHAT_DP_FUNC_DONE( "HandleReactAuthTransactionCompleteL" );
       
  1717     }
       
  1718 
       
  1719 // -----------------------------------------------------------------------------
       
  1720 // CCAPEngPresenceManager::HandleReactAuthTransactionError
       
  1721 // (other items were commented in a header).
       
  1722 // -----------------------------------------------------------------------------
       
  1723 //
       
  1724 void CCAPEngPresenceManager::HandleReactAuthTransactionError(
       
  1725     TInt /* aError */,
       
  1726     CPEngReactAuthTransaction& /* aTransaction */,
       
  1727     TInt /* aTransactionOperation */ )
       
  1728     {
       
  1729     CHAT_DP_FUNC_ENTER( "HandleReactAuthTransactionError" );
       
  1730 
       
  1731     iRequestQueue.ResponseReceived( TCARequestQueue::EReactiveAuthWait );
       
  1732 
       
  1733     CHAT_DP_FUNC_DONE( "HandleReactAuthTransactionError" );
       
  1734     }
       
  1735 
       
  1736 // -----------------------------------------------------------------------------
       
  1737 // CCAPEngPresenceManager::UpdatePresenceL
       
  1738 // (other items were commented in a header).
       
  1739 // -----------------------------------------------------------------------------
       
  1740 //
       
  1741 void CCAPEngPresenceManager::UpdatePresenceL( const TDesC& aUserId )
       
  1742     {
       
  1743     TOneDesWrapper wvId( aUserId );
       
  1744     DecodeFetcherDataL( wvId, NULL, NULL, NULL, ETrue );
       
  1745     }
       
  1746 
       
  1747 // -----------------------------------------------------------------------------
       
  1748 // CCAPEngPresenceManager::ExtractAttributeModelsForUserL
       
  1749 // (other items were commented in a header).
       
  1750 // -----------------------------------------------------------------------------
       
  1751 //
       
  1752 void CCAPEngPresenceManager::ExtractAttributeModelsForUserL(
       
  1753     const TDesC& aUserId,
       
  1754     RPointerArray<MPEngPresenceAttrModel2>& aAttrModels )
       
  1755     {
       
  1756     // the base attribute set
       
  1757     ExtractAttributeModelL( aUserId, aAttrModels, KUidPrAttrOnlineStatus );
       
  1758 
       
  1759     ExtractAttributeModelL( aUserId, aAttrModels, KUidPrAttrCommCap );
       
  1760 
       
  1761     ExtractAttributeModelL( aUserId, aAttrModels, KUidPrAttrUserAvailability );
       
  1762 
       
  1763     ExtractAttributeModelL( aUserId, aAttrModels, KUidPrAttrStatusText );
       
  1764 
       
  1765     ExtractAttributeModelL( aUserId, aAttrModels, KUidPrAttrClientInfo );
       
  1766 
       
  1767     // add more dynamically branded attributes here
       
  1768     if ( iAliasUsed )
       
  1769         {
       
  1770         ExtractAttributeModelL( aUserId, aAttrModels, KUidPrAttrAlias );
       
  1771         }
       
  1772     }
       
  1773 
       
  1774 // -----------------------------------------------------------------------------
       
  1775 // CCAPEngPresenceManager::PresenceStates
       
  1776 // (other items were commented in a header).
       
  1777 // -----------------------------------------------------------------------------
       
  1778 //
       
  1779 const RPointerArray<CCAState>& CCAPEngPresenceManager::PresenceStates()
       
  1780     {
       
  1781     return iPresenceStates;
       
  1782     }
       
  1783 
       
  1784 void CCAPEngPresenceManager::SetAttributeProcessing( TBool aEnabled )
       
  1785     {
       
  1786     iAttributeProcessing = aEnabled;
       
  1787     if ( iAttributeProcessing )
       
  1788         {
       
  1789         if ( iAttrArrays.Count()  && !iIdle->IsActive() )
       
  1790             {
       
  1791             // we have something to process
       
  1792             iIdle->Start( TCallBack( DecodeAttrModels, this ) );
       
  1793             }
       
  1794         }
       
  1795     else
       
  1796         {
       
  1797         if ( iIdle->IsActive() )
       
  1798             {
       
  1799             // we had something ongoing, so pause it
       
  1800             iIdle->Cancel();
       
  1801             }
       
  1802         }
       
  1803     }
       
  1804 
       
  1805 // -----------------------------------------------------------------------------
       
  1806 // CCAPEngPresenceManager::AllowReactiveAuthRequestL
       
  1807 // (other items were commented in a header).
       
  1808 // -----------------------------------------------------------------------------
       
  1809 //
       
  1810 void CCAPEngPresenceManager::SendReactiveAuthResponseL( TInt aId,
       
  1811                                                         TBool aResponse )
       
  1812     {
       
  1813     CHAT_DP_FUNC_ENTER( "SendReactiveAuthResponseL" );
       
  1814 
       
  1815     MPEngAuthorizationRequest& authReq =
       
  1816         iRAStore->PendingAuthorizationReq( aId );
       
  1817 
       
  1818     // lock the response so nobody else will mess with it
       
  1819     MPEngAuthorizationRespond* response = NULL;
       
  1820     iRAStore->GetAndLockAuthorizationRespond( response );
       
  1821     CleanupClosePushL( *response );
       
  1822 
       
  1823     if ( aResponse )
       
  1824         {
       
  1825         // accepted
       
  1826         TArray<TUint32> attr = iAttributes.Array();
       
  1827         TInt ignore;
       
  1828         TRAP( ignore, authReq.SetAuthorizedAttributesL( attr ) );
       
  1829         }
       
  1830     else
       
  1831         {
       
  1832         // rejected
       
  1833         TInt ignore;
       
  1834         TRAP( ignore, authReq.DenyAuthorizationL() );
       
  1835         }
       
  1836 
       
  1837     // now we can publish it. PEC engine knows which one we just changed
       
  1838     if ( KErrNone !=
       
  1839          iRATransaction->PublishReactAuthResponds( response, *this ) )
       
  1840         {
       
  1841         // got error, the lock stays with us
       
  1842         CleanupStack::PopAndDestroy(); // response
       
  1843         }
       
  1844     else
       
  1845         {
       
  1846         iRequestQueue.WaitForResponseL( TCARequestQueue::EReactiveAuthWait );
       
  1847         CleanupStack::Pop(); // response
       
  1848         // all done, unlocking happens by the store as it took ownership
       
  1849         }
       
  1850 
       
  1851     CHAT_DP_FUNC_DONE( "SendReactiveAuthResponseL" );
       
  1852     }
       
  1853 
       
  1854 // -----------------------------------------------------------------------------
       
  1855 // CCAPEngPresenceManager::GetNextPendingRARequestL
       
  1856 // (other items were commented in a header).
       
  1857 // -----------------------------------------------------------------------------
       
  1858 //
       
  1859 TBool CCAPEngPresenceManager::GetNextPendingRARequestL( TInt& aId,
       
  1860                                                         TDes& aUserId,
       
  1861                                                         TInt& aLastPos  )
       
  1862     {
       
  1863     CHAT_DP_FUNC_ENTER( "GetNextPendingRARequestL" );
       
  1864     TInt count = iRAStore->PendingAuthorizationReqCount();
       
  1865     for ( TInt i( 0 ) ; i < count ; ++i )
       
  1866         {
       
  1867         MPEngAuthorizationRequest& authReq =
       
  1868             iRAStore->PendingAuthorizationReq( i );
       
  1869 
       
  1870         if ( MPEngAuthorizationRequest::EPEngAuthPending ==
       
  1871              authReq.AuthorizationState() )
       
  1872             {
       
  1873             // found a pending one
       
  1874 
       
  1875             // caller is interested in these
       
  1876             aId = i;
       
  1877             aUserId.Copy( authReq.UserId() );
       
  1878             aLastPos = i + 1;
       
  1879             CHAT_DP_FUNC_DONE( "GetNextPendingRARequestL" );
       
  1880             return ETrue;
       
  1881             }
       
  1882         }
       
  1883     return EFalse;
       
  1884     /*
       
  1885 
       
  1886         if ( ( count > 0 ) && ( aLastPos < ( count-1 ) ) )
       
  1887             {
       
  1888             // take the first pending one from the queue for processing,
       
  1889             // find out which one it is.
       
  1890             TBool getOut = EFalse;
       
  1891 
       
  1892             // for ( TInt i = 0; ! getOut && ( i < count ); i++ )
       
  1893             // queue even after it's responded!
       
  1894             for ( i = aLastPos; ! getOut && ( i < count ); i++ )
       
  1895                 {
       
  1896                 MPEngAuthorizationRequest& authReq =
       
  1897                             iRAStore->PendingAuthorizationReq( i );
       
  1898 
       
  1899                 if ( MPEngAuthorizationRequest::EPEngAuthPending ==
       
  1900                                             authReq.AuthorizationState() )
       
  1901                     {
       
  1902                     // found a pending one
       
  1903 
       
  1904                     // caller is interested in these
       
  1905                     aId = i;
       
  1906                     aUserId.Copy( authReq.UserId() );
       
  1907                     aLastPos = i;
       
  1908                     CHAT_DP_FUNC_DONE("GetNextPendingRARequestL");
       
  1909                     return ETrue;
       
  1910                     }
       
  1911                 }
       
  1912             aLastPos = i;
       
  1913             retVal = EFalse;
       
  1914             }
       
  1915         else
       
  1916             {
       
  1917             // nothing in queue
       
  1918             retVal = EFalse;
       
  1919             }
       
  1920 
       
  1921         CHAT_DP_FUNC_DONE("GetNextPendingRARequestL");
       
  1922         return retVal;
       
  1923     */
       
  1924     }
       
  1925 
       
  1926 // -----------------------------------------------------------------------------
       
  1927 // CCAPEngPresenceManager::GetNextRAStatusL
       
  1928 // (other items were commented in a header).
       
  1929 // -----------------------------------------------------------------------------
       
  1930 //
       
  1931 TBool CCAPEngPresenceManager::GetNextRAStatusL( TInt& aId, TBool& aStatus,
       
  1932                                                 TDes& aUserId, TInt& aLastPos )
       
  1933     {
       
  1934     CHAT_DP_FUNC_ENTER( "GetNextRAStatusL" );
       
  1935 
       
  1936     TBool retVal = EFalse;
       
  1937 
       
  1938     TInt count = iRAStore->RespondedAuthorizationCount();
       
  1939 
       
  1940     if ( ( count > 0 ) && ( aLastPos < ( count - 1 ) ) )
       
  1941         {
       
  1942         // take the next from the queue
       
  1943         aLastPos++;
       
  1944 
       
  1945         const MPEngAuthorizationStatus& authStatus =
       
  1946             iRAStore->RespondedAuthorization( aLastPos );
       
  1947 
       
  1948         // caller is interested in these
       
  1949         aId = aLastPos;
       
  1950         aStatus = authStatus.AuthorizationStatus() ==
       
  1951                   MPEngAuthorizationStatus::EPEngAuthAccepted;
       
  1952         aUserId.Copy( authStatus.UserId() );
       
  1953 
       
  1954         // stuff left in queue?
       
  1955         retVal = aLastPos < ( iRAStore->RespondedAuthorizationCount() - 1 )
       
  1956                  ? ETrue : EFalse;
       
  1957         }
       
  1958     else
       
  1959         {
       
  1960         // nothing in queue
       
  1961         retVal = EFalse;
       
  1962         }
       
  1963 
       
  1964     CHAT_DP_FUNC_DONE( "GetNextRAStatusL" );
       
  1965     return retVal;
       
  1966     }
       
  1967 
       
  1968 // -----------------------------------------------------------------------------
       
  1969 // CCAPEngPresenceManager::PendingRARequestCount
       
  1970 // (other items were commented in a header).
       
  1971 // -----------------------------------------------------------------------------
       
  1972 //
       
  1973 TInt CCAPEngPresenceManager::PendingRARequestCount()
       
  1974     {
       
  1975     CHAT_DP_FUNC_ENTER( "PendingRARequestCount" );
       
  1976     TInt count = iRAStore->PendingAuthorizationReqCount();
       
  1977     CHAT_DP_FUNC_DONE( "PendingRARequestCount" );
       
  1978     return count;
       
  1979     }
       
  1980 
       
  1981 // -----------------------------------------------------------------------------
       
  1982 // CCAPEngPresenceManager::RAStatusCount
       
  1983 // (other items were commented in a header).
       
  1984 // -----------------------------------------------------------------------------
       
  1985 //
       
  1986 TInt CCAPEngPresenceManager::RAStatusCount()
       
  1987     {
       
  1988     CHAT_DP_FUNC_ENTER( "RAStatusCount" );
       
  1989     TInt count = iRAStore->RespondedAuthorizationCount();
       
  1990     CHAT_DP_FUNC_DONE( "RAStatusCount" );
       
  1991     return count;
       
  1992     }
       
  1993 
       
  1994 // -----------------------------------------------------------------------------
       
  1995 // CCAPEngPresenceManager::PostLoginRAFlushL
       
  1996 // (other items were commented in a header).
       
  1997 // -----------------------------------------------------------------------------
       
  1998 //
       
  1999 void CCAPEngPresenceManager::PostLoginRAFlushL()
       
  2000     {
       
  2001     CHAT_DP_FUNC_ENTER( "PostLoginRAFlushL" );
       
  2002 
       
  2003     // handle any reactive authorization events which came during login,
       
  2004     // when IM Presence was not listening yet
       
  2005 
       
  2006     // start the RA notifier
       
  2007     if ( !iRANotifier->IsActive() )
       
  2008         {
       
  2009         iRANotifier->Start();
       
  2010         }
       
  2011 
       
  2012     if ( iRAStore->PendingAuthorizationReqCount() )
       
  2013         {
       
  2014         // pending authorization requests
       
  2015         iRAObserver->HandleRAPendingReqL();
       
  2016         }
       
  2017 
       
  2018     if ( iRAStore->RespondedAuthorizationCount() )
       
  2019         {
       
  2020         // incoming status stuff
       
  2021         iRAObserver->HandleRAStatusL();
       
  2022         }
       
  2023 
       
  2024     CHAT_DP_FUNC_DONE( "PostLoginRAFlushL" );
       
  2025     }
       
  2026 
       
  2027 // -----------------------------------------------------------------------------
       
  2028 // CCAPEngPresenceManager::ExtractAttributeModelsForUsersL
       
  2029 // (other items were commented in a header).
       
  2030 // -----------------------------------------------------------------------------
       
  2031 //
       
  2032 void CCAPEngPresenceManager::ExtractAttributeModelsForUsersL(
       
  2033     const MDesCArray& aUserList,
       
  2034     RPointerArray<MPEngPresenceAttrModel2>& aAttrModels )
       
  2035     {
       
  2036     CHAT_DP_FUNC_ENTER( "ExtractAttributeModelsForUsersL" );
       
  2037 
       
  2038     TInt count = aUserList.MdcaCount();
       
  2039     for ( TInt i = 0 ; i < count; ++i )
       
  2040         {
       
  2041         // the base attribute set
       
  2042         ExtractAttributeModelL( aUserList.MdcaPoint( i ), aAttrModels,
       
  2043                                 KUidPrAttrOnlineStatus );
       
  2044         ExtractAttributeModelL( aUserList.MdcaPoint( i ), aAttrModels,
       
  2045                                 KUidPrAttrCommCap );
       
  2046         ExtractAttributeModelL( aUserList.MdcaPoint( i ), aAttrModels,
       
  2047                                 KUidPrAttrUserAvailability );
       
  2048         ExtractAttributeModelL( aUserList.MdcaPoint( i ), aAttrModels,
       
  2049                                 KUidPrAttrStatusText );
       
  2050         ExtractAttributeModelL( aUserList.MdcaPoint( i ), aAttrModels,
       
  2051                                 KUidPrAttrClientInfo );
       
  2052 
       
  2053         // add more dynamically branded attributes here
       
  2054         if ( iAliasUsed )
       
  2055             {
       
  2056             ExtractAttributeModelL( aUserList.MdcaPoint( i ), aAttrModels,
       
  2057                                     KUidPrAttrAlias );
       
  2058             }
       
  2059         }
       
  2060 
       
  2061     CHAT_DP_FUNC_DONE( "ExtractAttributeModelsForUsersL" );
       
  2062     }
       
  2063 
       
  2064 //  End of File