PECengine/PresenceManager2/SrcNWSessionSlot/CPEngNWSessionSlotNotifier2Imp.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  NWSessionSlot notifier API implementation
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "CPEngNWSessionSlotID2Imp.h"
       
    20 #include "CPEngNWSessionSlotNotifier2Imp.h"
       
    21 #include "CPEngNWSessionSlotEventEntry.h"
       
    22 #include "GenObserverNotifyMediators.h"
       
    23 
       
    24 #include "PEngStorageManager.h"
       
    25 #include "MPEngStorageManagerWatcher.h"
       
    26 #include "CPEngSessionSlotEvent.h"
       
    27 #include "CPEngSessionSlotState.h"
       
    28 #include "PresenceDebugPrint.h"
       
    29 
       
    30 
       
    31 
       
    32 
       
    33 #include <CPEngNWSessionSlotID2.h>
       
    34 #include <CPEngNWSessionSlotNotifier2.h>
       
    35 #include <CPEngNWSessionSlotEvent2.h>
       
    36 #include <MPEngNWSessionSlotObserver2.h>
       
    37 
       
    38 
       
    39 
       
    40 // ============================ MEMBER FUNCTIONS ===============================
       
    41 
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CPEngNWSessionSlotNotifier2Imp::NewL()
       
    45 // Two-phased constructor.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CPEngNWSessionSlotNotifier2Imp* CPEngNWSessionSlotNotifier2Imp::NewL(
       
    49     CPEngNWSessionSlotNotifier2& aInterface,
       
    50     TInt aPriority )
       
    51     {
       
    52     CPEngNWSessionSlotNotifier2Imp* self =
       
    53         new ( ELeave ) CPEngNWSessionSlotNotifier2Imp( aInterface, aPriority );
       
    54     CleanupStack::PushL( self );
       
    55     self->ConstructL();
       
    56     CleanupStack::Pop( self );
       
    57     return self;
       
    58     }
       
    59 
       
    60 
       
    61 
       
    62 // Destructor
       
    63 CPEngNWSessionSlotNotifier2Imp::~CPEngNWSessionSlotNotifier2Imp()
       
    64     {
       
    65     iDying = ETrue;
       
    66     Stop();
       
    67 
       
    68     DeleteFilters();
       
    69     iObsArray.Close();
       
    70     delete iEventEntry;
       
    71     }
       
    72 
       
    73 
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CPEngNWSessionSlotNotifier2Imp::CPEngNWSessionSlotNotifier2Imp
       
    77 // C++ default constructor can NOT contain any code, that
       
    78 // might leave.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CPEngNWSessionSlotNotifier2Imp::CPEngNWSessionSlotNotifier2Imp( CPEngNWSessionSlotNotifier2& aInterface,
       
    82                                                                 TInt aPriority )
       
    83         : iInterface( aInterface ),
       
    84         iCActivePriority( aPriority )
       
    85     {
       
    86     }
       
    87 
       
    88 
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CPEngNWSessionSlotNotifier2Imp::ConstructL()
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CPEngNWSessionSlotNotifier2Imp::ConstructL()
       
    95     {
       
    96     iEventEntry = CPEngNWSessionSlotEvent2Copy::NewL();
       
    97     }
       
    98 
       
    99 
       
   100 
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CPEngNWSessionSlotNotifier2Imp::StartL()
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 void CPEngNWSessionSlotNotifier2Imp::StartL(
       
   107     const CPEngNWSessionSlotID2Imp* aSlotIdFilter,
       
   108     const TArray<TPEngNWSessionSlotEvent>* aEventFilter )
       
   109     {
       
   110     //Pre checks
       
   111     if ( iStarted )
       
   112         {
       
   113         User::Leave( KErrInUse );
       
   114         }
       
   115 
       
   116 
       
   117     //Clear possibly existing filters
       
   118     DeleteFilters();
       
   119 
       
   120     //And initialize new ones
       
   121     if ( aSlotIdFilter )
       
   122         {
       
   123         iSlotIdFilter = aSlotIdFilter->CloneL();
       
   124         }
       
   125 
       
   126     if ( aEventFilter )
       
   127         {
       
   128         const TInt eventsCount = aEventFilter->Count();
       
   129         if ( eventsCount == 0 )
       
   130             {
       
   131             User::Leave( KErrArgument );
       
   132             }
       
   133 
       
   134         iEventFilter = new ( ELeave ) RArray<TPEngNWSessionSlotEvent>;
       
   135         for ( TInt ii = 0; ii < eventsCount; ii++ )
       
   136             {
       
   137             iEventFilter->InsertInSignedKeyOrderL( ( *aEventFilter )[ ii ] );
       
   138             }
       
   139         }
       
   140 
       
   141     //Register for events
       
   142     PEngStorageManager::RegisterGlobalEventObserverL( *this );
       
   143     iStarted = ETrue;
       
   144     }
       
   145 
       
   146 
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CPEngNWSessionSlotNotifier2Imp::Stop()
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CPEngNWSessionSlotNotifier2Imp::Stop()
       
   153     {
       
   154     if ( !iStarted )
       
   155         {
       
   156         return;
       
   157         }
       
   158 
       
   159     PEngStorageManager::UnregisterGlobalObserver( *this );
       
   160     iStarted = EFalse;
       
   161 
       
   162     DeleteFilters();
       
   163     }
       
   164 
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CPEngNWSessionSlotNotifier2Imp::IsActive()
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 TBool CPEngNWSessionSlotNotifier2Imp::IsActive() const
       
   171     {
       
   172     return iStarted;
       
   173     }
       
   174 
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CPEngNWSessionSlotNotifier2Imp::PushNWSessionSlotStateToObservers()
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 TInt CPEngNWSessionSlotNotifier2Imp::PushNWSessionSlotStateToObservers(
       
   181     const CPEngNWSessionSlotID2& aMatchPattern )
       
   182 
       
   183     {
       
   184     if ( iSynchNotifyRunning || iObsArray.NotifyLoopRunning() )
       
   185         {
       
   186         return KErrInUse;
       
   187         }
       
   188 
       
   189     iSynchNotifyRunning = ETrue;
       
   190     TRAPD( err, DoNotifyNWSessionSlotStatesL( *aMatchPattern.Implementation() ) );
       
   191     iSynchNotifyRunning = EFalse;
       
   192 
       
   193     return err;
       
   194     }
       
   195 
       
   196 
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CPEngNWSessionSlotNotifier2Imp::GetNWSessionSlotState()
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 TInt CPEngNWSessionSlotNotifier2Imp::GetNWSessionSlotState(
       
   203     const CPEngNWSessionSlotID2& aNWSessionSlotID,
       
   204     TPEngNWSessionSlotState& aState ) const
       
   205     {
       
   206     TRAPD( err,
       
   207         {
       
   208         CPEngSessionSlotState* state = CPEngSessionSlotState::NewLC();
       
   209         PEngStorageManager::SessionSlotStateL( aNWSessionSlotID.Implementation()->BasePart(),
       
   210                                                *state );
       
   211 
       
   212         aState = state->AppState( aNWSessionSlotID.AppId() );
       
   213         CleanupStack::PopAndDestroy( state );
       
   214         } );
       
   215 
       
   216 
       
   217     return err;
       
   218     }
       
   219 
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CPEngNWSessionSlotNotifier2Imp::AddObserver()
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 TInt CPEngNWSessionSlotNotifier2Imp::AddObserver(
       
   226     MPEngNWSessionSlotObserver2& aObserver )
       
   227 
       
   228     {
       
   229     //Observer array takes care from possible NULL pointers
       
   230     return iObsArray.AddObserver( &aObserver );
       
   231     }
       
   232 
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CPEngNWSessionSlotNotifier2Imp::RemoveObserver()
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 TInt CPEngNWSessionSlotNotifier2Imp::RemoveObserver(
       
   239     MPEngNWSessionSlotObserver2& aObserver )
       
   240 
       
   241     {
       
   242     return iObsArray.RemoveObserver( &aObserver );
       
   243     }
       
   244 
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CPEngNWSessionSlotNotifier2Imp::HandleNewSessionSlotEventL()
       
   248 // From MPEngGlobalEventObserver
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 void CPEngNWSessionSlotNotifier2Imp::HandleNewSessionSlotEventL(
       
   252     const CPEngSessionSlotEvent& aSessionEvent )
       
   253     {
       
   254     if ( iDying )
       
   255         {
       
   256         return;
       
   257         }
       
   258 
       
   259     iEventEntry->Implementation()->SetFullEventReferenceL( aSessionEvent );
       
   260     TBool eventAccepted = IsEventAccepted( iEventEntry->NWSessionSlotID(),
       
   261                                            iEventEntry->Event() );
       
   262 
       
   263     if ( eventAccepted )
       
   264         {
       
   265         MediateNotify( KErrNone, *iEventEntry );
       
   266         }
       
   267 
       
   268     iEventEntry->Implementation()->ResetReferences();
       
   269     }
       
   270 
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CPEngNWSessionSlotNotifier2Imp::HandleObserverError()
       
   274 // From MPEngGlobalEventObserver
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 void CPEngNWSessionSlotNotifier2Imp::HandleObserverError( TInt aError )
       
   278     {
       
   279     if ( iDying )
       
   280         {
       
   281         return;
       
   282         }
       
   283 
       
   284     iEventEntry->Implementation()->ResetReferences();
       
   285     MediateNotify( aError, *iEventEntry );
       
   286     }
       
   287 
       
   288 
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CPEngNWSessionSlotNotifier2Imp::DoNotifyNWSessionSlotStatesL()
       
   292 // -----------------------------------------------------------------------------
       
   293 //
       
   294 void CPEngNWSessionSlotNotifier2Imp::DoNotifyNWSessionSlotStatesL(
       
   295     const CPEngNWSessionSlotID2Imp& aSlotIdMatcher )
       
   296     {
       
   297     //this might be called during the event notify loop
       
   298     //==> use here temp objects so the original data isn't messed up
       
   299 
       
   300     RObjectArray< CPEngSessionSlotState > ssStates;
       
   301     CleanupClosePushL( ssStates );
       
   302 
       
   303     CPEngNWSessionSlotEvent2Copy* eventEntry = CPEngNWSessionSlotEvent2Copy::NewL();
       
   304     CleanupStack::PushL( eventEntry );
       
   305 
       
   306     //Get list of existing slot states
       
   307     PEngStorageManager::AllSessionSlotsStatesL( ssStates );
       
   308 
       
   309     TInt matchCount = 0;
       
   310 
       
   311     //Generate client side events from requested NWSessionSlots
       
   312     const TInt ssStatesCount = ssStates.Count();
       
   313     for ( TInt ii = 0; ii < ssStatesCount; ii++ )
       
   314         {
       
   315         const CPEngSessionSlotState& ssState = *ssStates[ ii ];
       
   316         CPEngSessionSlotId& ssBaseId = ssState.SessionSlotId();
       
   317 
       
   318         TBool baseMatch = ( aSlotIdMatcher.MatchBasePart( ssBaseId ) == KErrNone );
       
   319         if ( baseMatch )
       
   320             {
       
   321             const MDesC16Array& ssApplications = ssState.RegisteredApplications();
       
   322             const TInt appCount = ssApplications.MdcaCount();
       
   323             for ( TInt jj = 0; jj < appCount; jj++ )
       
   324                 {
       
   325                 TPtrC appId = ssApplications.MdcaPoint( jj );
       
   326                 TBool appMatch = ( aSlotIdMatcher.MatchAppIdPart( appId ) == KErrNone );
       
   327                 if ( appMatch )
       
   328                     {
       
   329                     matchCount++;
       
   330                     eventEntry->Implementation()->SetPseudoEventReferenceL(
       
   331                         ssBaseId,
       
   332                         appId,
       
   333                         ssState.AppState( appId ),
       
   334                         ssState.SessionSlotState() );
       
   335                     MediateNotify( KErrNone, *eventEntry );
       
   336                     eventEntry->Implementation()->ResetReferences();
       
   337                     }
       
   338                 }
       
   339             }
       
   340         }
       
   341 
       
   342     if ( matchCount == 0 )
       
   343         {
       
   344         User::Leave( KErrNotFound );
       
   345         }
       
   346 
       
   347     CleanupStack::PopAndDestroy( 2 ); //ssStates, eventEntry
       
   348     }
       
   349 
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CPEngNWSessionSlotNotifier2Imp::DeleteFilters()
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 void CPEngNWSessionSlotNotifier2Imp::DeleteFilters()
       
   356     {
       
   357     delete iSlotIdFilter;
       
   358     iSlotIdFilter = NULL;
       
   359 
       
   360     if ( iEventFilter )
       
   361         {
       
   362         iEventFilter->Reset();
       
   363         }
       
   364 
       
   365     delete iEventFilter;
       
   366     iEventFilter = NULL;
       
   367     }
       
   368 
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CPEngNWSessionSlotNotifier2Imp::IsEventAccepted()
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 TBool CPEngNWSessionSlotNotifier2Imp::IsEventAccepted(
       
   375     const CPEngNWSessionSlotID2& aNWSessionSlotID,
       
   376     TPEngNWSessionSlotEvent aEvent ) const
       
   377     {
       
   378     TBool accepted = ETrue;
       
   379 
       
   380     if ( iSlotIdFilter )
       
   381         {
       
   382         if ( iSlotIdFilter->MatchFullId( *aNWSessionSlotID.Implementation() ) == KErrNotFound )
       
   383             {
       
   384             accepted = EFalse;
       
   385             }
       
   386         }
       
   387 
       
   388 
       
   389     if ( iEventFilter && accepted )
       
   390         {
       
   391         if ( iEventFilter->Find( aEvent ) == KErrNotFound )
       
   392             {
       
   393             accepted = EFalse;
       
   394             }
       
   395         }
       
   396 
       
   397 
       
   398     return accepted;
       
   399     }
       
   400 
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CPEngNWSessionSlotNotifier2Imp::MediateNotify()
       
   404 // -----------------------------------------------------------------------------
       
   405 //
       
   406 void CPEngNWSessionSlotNotifier2Imp::MediateNotify( TInt aError,
       
   407                                                     CPEngNWSessionSlotEvent2& aEvent )
       
   408     {
       
   409     TGenNotifyMediator2 < MPEngNWSessionSlotObserver2,
       
   410     CPEngNWSessionSlotNotifier2&,
       
   411     CPEngNWSessionSlotEvent2& >
       
   412     eventMediator( &MPEngNWSessionSlotObserver2::HandleNWSessionSlotChangeL,
       
   413                    &MPEngNWSessionSlotObserver2::HandleNWSessionSlotError,
       
   414                    iInterface,
       
   415                    aEvent );
       
   416 
       
   417     if ( aError == KErrNone )
       
   418         {
       
   419         PENG_DP( D_PENG_LIT( "CPEngNWSessionSlotNotifier2 - Handle event: %d" ),
       
   420                  aEvent.Event() );
       
   421 
       
   422         iObsArray.NotifyObservers( eventMediator );
       
   423         }
       
   424     else
       
   425         {
       
   426         PENG_DP( D_PENG_LIT( "CPEngNWSessionSlotNotifier2 - Handle event error: %d" ),
       
   427                  aError );
       
   428 
       
   429         iObsArray.NotifyErrorObservers( eventMediator, aError );
       
   430         }
       
   431     }
       
   432 
       
   433 
       
   434 
       
   435 
       
   436 // End of File
       
   437 
       
   438