PECengine/PresenceManager2/SrcNWSessionSlot/CPEngNWSessionSlotManager2Imp.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:  API implementation to manage NWSessionSlots.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <CPEngNWSessionSlotManager2.h>
       
    20 
       
    21 #include "CPEngNWSessionSlotManager2Imp.h"
       
    22 #include "CPEngNWSessionSlotID2.h"
       
    23 #include "CPEngNWSessionSlotID2Imp.h"
       
    24 
       
    25 
       
    26 #include "PEngStorageManager.h"
       
    27 #include "CPEngSessionSlotState.h"
       
    28 #include "CPEngSessionSlotId.h"
       
    29 
       
    30 
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CPEngNWSessionSlotManager2Imp::NewL()
       
    37 // Two-phased constructor.
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CPEngNWSessionSlotManager2Imp* CPEngNWSessionSlotManager2Imp::NewL(
       
    41     CPEngNWSessionSlotManager2& aInterface,
       
    42     TInt aPriority )
       
    43     {
       
    44     CPEngNWSessionSlotManager2Imp* self =
       
    45         new ( ELeave ) CPEngNWSessionSlotManager2Imp( aInterface, aPriority );
       
    46     CleanupStack::PushL( self );
       
    47     self->ConstructL();
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 
       
    53 
       
    54 // Destructor
       
    55 CPEngNWSessionSlotManager2Imp::~CPEngNWSessionSlotManager2Imp()
       
    56     {
       
    57 #if _BullseyeCoverage
       
    58     cov_write();
       
    59 #endif
       
    60     }
       
    61 
       
    62 
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CPEngNWSessionSlotManager2Imp::CPEngNWSessionSlotManager2Imp
       
    66 // C++ default constructor can NOT contain any code, that
       
    67 // might leave.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CPEngNWSessionSlotManager2Imp::CPEngNWSessionSlotManager2Imp( CPEngNWSessionSlotManager2& aInterface,
       
    71                                                               TInt aPriority )
       
    72         : iInterface( aInterface ),
       
    73         iCActivePriority( aPriority )
       
    74     {
       
    75     }
       
    76 
       
    77 
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CPEngNWSessionSlotManager2Imp::ConstructL()
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 void CPEngNWSessionSlotManager2Imp::ConstructL()
       
    84     {
       
    85     }
       
    86 
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CPEngNWSessionSlotManager2Imp::CreateNWSessionSlot()
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 TInt CPEngNWSessionSlotManager2Imp::CreateNWSessionSlot(
       
    93     const CPEngNWSessionSlotID2& aNWSessionSlotID )
       
    94     {
       
    95     const CPEngNWSessionSlotID2Imp* slotIDImp = aNWSessionSlotID.Implementation();
       
    96     if ( !slotIDImp->IsFullyDefined() )
       
    97         {
       
    98         return KErrArgument;
       
    99         }
       
   100 
       
   101     TRAPD( err, PEngStorageManager::CreateSessionFolderL( slotIDImp->BasePart(),
       
   102                                                           slotIDImp->AppId() ) );
       
   103 
       
   104     return err;
       
   105     }
       
   106 
       
   107 
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CPEngNWSessionSlotManager2Imp::RemoveNWSessionSlot()
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 TInt CPEngNWSessionSlotManager2Imp::RemoveNWSessionSlot(
       
   114     const CPEngNWSessionSlotID2& aNWSessionSlotID )
       
   115     {
       
   116     const CPEngNWSessionSlotID2Imp* slotIDImp = aNWSessionSlotID.Implementation();
       
   117     if ( !slotIDImp->IsFullyDefined() )
       
   118         {
       
   119         return KErrArgument;
       
   120         }
       
   121 
       
   122     TRAPD( err, PEngStorageManager::RemoveSessionFolderL( slotIDImp->BasePart(),
       
   123                                                           slotIDImp->AppId() ) );
       
   124 
       
   125     return err;
       
   126     }
       
   127 
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CPEngNWSessionSlotManager2Imp::GetNWSessionSlotsL()
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 void CPEngNWSessionSlotManager2Imp::GetNWSessionSlotsL(
       
   134     RPointerArray<CPEngNWSessionSlotID2>& aNWSessionSlotIDs,
       
   135     const CPEngNWSessionSlotID2* aMatchPattern,
       
   136     TPEngNWSessionSlotState* aMatchState ) const
       
   137     {
       
   138     //Get list of existing slot states
       
   139     RObjectArray< CPEngSessionSlotState > allSlots;
       
   140     CleanupClosePushL( allSlots );
       
   141     PEngStorageManager::AllSessionSlotsStatesL( allSlots );
       
   142 
       
   143 
       
   144     //Generate client side slotId's from existing and matching
       
   145     //NWSessionSlots
       
   146     TInt allSlotsCount = allSlots.Count();
       
   147     for ( TInt ix = 0; ix < allSlotsCount; ix++ )
       
   148         {
       
   149         const CPEngSessionSlotState* handledSlot = allSlots[ ix ];
       
   150         CPEngSessionSlotId& ssBaseId = handledSlot->SessionSlotId();
       
   151 
       
   152         const MDesC16Array& regApps = handledSlot->RegisteredApplications();
       
   153         const MDesC16Array& activeApps = handledSlot->ActiveApplications();
       
   154         DoGenerateMatchingSlotIdsL( aNWSessionSlotIDs,
       
   155                                     ssBaseId,
       
   156                                     regApps,
       
   157                                     activeApps,
       
   158                                     aMatchPattern,
       
   159                                     aMatchState );
       
   160         }
       
   161 
       
   162     CleanupStack::PopAndDestroy(); //slotStates
       
   163     }
       
   164 
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CPEngNWSessionSlotManager2Imp::ExistsNWSessionSlotL()
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void CPEngNWSessionSlotManager2Imp::ExistsNWSessionSlotL(
       
   171     const CPEngNWSessionSlotID2& aNWSessionSlotID ) const
       
   172     {
       
   173     const CPEngNWSessionSlotID2Imp* slotIDImp = aNWSessionSlotID.Implementation();
       
   174     if ( !slotIDImp->IsFullyDefined() )
       
   175         {
       
   176         User::Leave( KErrArgument );
       
   177         }
       
   178 
       
   179     //Try get the wanted slot
       
   180     const CPEngSessionSlotId& wantedBaseId = aNWSessionSlotID.Implementation()->BasePart();
       
   181     CPEngSessionSlotState* wantedSlotState = CPEngSessionSlotState::NewLC();
       
   182     PEngStorageManager::SessionSlotStateL( wantedBaseId, *wantedSlotState );
       
   183 
       
   184 
       
   185     //Check if base & AppId's match to given match pattern
       
   186     TInt  matchStatus = KErrNotFound;
       
   187     if ( wantedSlotState->SessionSlotId().Match( wantedBaseId ) == KErrNone )
       
   188         {
       
   189         const MDesC16Array& apps = wantedSlotState->RegisteredApplications();
       
   190 
       
   191         TInt appsCount = apps.MdcaCount();
       
   192         for ( TInt ii = 0; ii < appsCount; ii++ )
       
   193             {
       
   194             TPtrC16 appId( apps.MdcaPoint( ii ) );
       
   195             matchStatus = aNWSessionSlotID.Implementation()->MatchAppIdPart( appId );
       
   196             if ( matchStatus == KErrNone )
       
   197                 {
       
   198                 break;
       
   199                 }
       
   200             }
       
   201         }
       
   202 
       
   203     CleanupStack::PopAndDestroy( wantedSlotState );
       
   204     User::LeaveIfError( matchStatus );
       
   205     }
       
   206 
       
   207 
       
   208 
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // IsIdMatch()
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 TBool IsIdMatch( const CPEngNWSessionSlotID2* aSlotIdMatcher,
       
   215                  const CPEngNWSessionSlotID2& aSlotId )
       
   216     {
       
   217     TBool idMatch = ETrue;
       
   218 
       
   219     if ( aSlotIdMatcher )
       
   220         {
       
   221         idMatch = ( aSlotId.MatchFullId( *aSlotIdMatcher ) == KErrNone );
       
   222         }
       
   223 
       
   224     return idMatch;
       
   225     }
       
   226 
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // HasApp()
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 TBool HasApp( const MDesC16Array& aActiveApps, const TDesC& aAppId )
       
   233     {
       
   234     const TInt activeAppCount = aActiveApps.MdcaCount();
       
   235     for ( TInt ii = 0; ii < activeAppCount; ii++ )
       
   236         {
       
   237         TPtrC app = aActiveApps.MdcaPoint( ii );
       
   238         if ( app == aAppId )
       
   239             {
       
   240             return ETrue;
       
   241             }
       
   242         }
       
   243 
       
   244     return EFalse;
       
   245     }
       
   246 
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // IsStateMatch()
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 TBool IsStateMatch( const TPEngNWSessionSlotState* aSlotStateMatcher,
       
   253                     const MDesC16Array& aActiveApps,
       
   254                     const CPEngNWSessionSlotID2& aSlotId )
       
   255     {
       
   256     TBool stateMatch = ETrue;
       
   257 
       
   258     if ( aSlotStateMatcher )
       
   259         {
       
   260         if ( *aSlotStateMatcher == EPEngNWPresenceSessionClosed )
       
   261             {
       
   262             //only closed apps wanted -> app id may not exist in aActiveApps
       
   263             stateMatch = !HasApp( aActiveApps, aSlotId.AppId() );
       
   264             }
       
   265 
       
   266         if ( *aSlotStateMatcher == EPEngNWPresenceSessionOpen )
       
   267             {
       
   268             //only open apps wanted -> app id must be found from aActiveApps
       
   269             stateMatch = HasApp( aActiveApps, aSlotId.AppId() );
       
   270             }
       
   271         }
       
   272 
       
   273     return stateMatch;
       
   274     }
       
   275 
       
   276 
       
   277 
       
   278 // -----------------------------------------------------------------------------
       
   279 // CPEngNWSessionSlotManager2Imp::DoGenerateMatchingSlotIdsL()
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 void CPEngNWSessionSlotManager2Imp::DoGenerateMatchingSlotIdsL(
       
   283     RPointerArray<CPEngNWSessionSlotID2>& aNWSessionSlotIDs,
       
   284     CPEngSessionSlotId& aSessionSlotBaseId,
       
   285     const MDesC16Array& aRegApps,
       
   286     const MDesC16Array& aActiveApps,
       
   287     const CPEngNWSessionSlotID2* aSlotIdMatcher,
       
   288     TPEngNWSessionSlotState* aSlotStateMatcher ) const
       
   289     {
       
   290     const TInt regAppCount = aRegApps.MdcaCount();
       
   291 
       
   292     for ( TInt ii = 0; ii < regAppCount; ii++ )
       
   293         {
       
   294         CPEngNWSessionSlotID2* slotId = CPEngNWSessionSlotID2::NewLC();
       
   295         CPEngNWSessionSlotID2Imp* slotIdImp = slotId->Implementation();
       
   296 
       
   297         slotIdImp->SetAllL( aSessionSlotBaseId,
       
   298                             aRegApps.MdcaPoint( ii ) );
       
   299 
       
   300         TBool idMatch = IsIdMatch( aSlotIdMatcher, *slotId );
       
   301         TBool stateMatch = IsStateMatch( aSlotStateMatcher, aActiveApps, *slotId );
       
   302 
       
   303         if ( idMatch && stateMatch )
       
   304             {
       
   305             User::LeaveIfError( aNWSessionSlotIDs.Append( slotId ) );
       
   306             CleanupStack::Pop( slotId );
       
   307             }
       
   308         else
       
   309             {
       
   310             CleanupStack::PopAndDestroy( slotId );
       
   311             }
       
   312         }
       
   313     }
       
   314 
       
   315 
       
   316 
       
   317 // End of File
       
   318 
       
   319 
       
   320 
       
   321