PECengine/PresenceManager2/SrcContactList/CPEngContactListNotifier2Imp.cpp
branchRCL_3
changeset 13 a941bc465d9f
parent 0 094583676ce7
equal deleted inserted replaced
12:6ca72c0fe49a 13:a941bc465d9f
       
     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:  Notifier API to listen presence contact list changes.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "CPEngNWSessionSlotID2Imp.h"
       
    20 #include "CPEngContactListNotifier2Imp.h"
       
    21 #include "CPEngNWSessionSlotEventEntry.h"
       
    22 #include "GenObserverNotifyMediators.h"
       
    23 
       
    24 #include "CPEngNWSessionSlotStorageProxy.h"
       
    25 #include "MPEngStorageManagerWatcher.h"
       
    26 #include "PEngStorageManager.h"
       
    27 #include "TPEngMDesCArrayAdapter.h"
       
    28 
       
    29 #include <CPEngContactListNotifier2.h>
       
    30 #include <MPEngContactListObserver2.h>
       
    31 
       
    32 
       
    33 
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CPEngContactListNotifier2Imp::NewL()
       
    40 // Two-phased constructor.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CPEngContactListNotifier2Imp* CPEngContactListNotifier2Imp::NewL(
       
    44     CPEngContactListNotifier2& aInterface,
       
    45     TInt aPriority,
       
    46     const CPEngNWSessionSlotID2& aNWSessionSlotID )
       
    47     {
       
    48     CPEngContactListNotifier2Imp* self =
       
    49         new ( ELeave ) CPEngContactListNotifier2Imp( aInterface, aPriority );
       
    50     CleanupStack::PushL( self );
       
    51     self->ConstructL( aNWSessionSlotID );
       
    52     CleanupStack::Pop( self );
       
    53     return self;
       
    54     }
       
    55 
       
    56 
       
    57 
       
    58 // Destructor
       
    59 CPEngContactListNotifier2Imp::~CPEngContactListNotifier2Imp()
       
    60     {
       
    61     iDying = ETrue;
       
    62     Stop();
       
    63 
       
    64     if ( iWatcher )
       
    65         {
       
    66         iWatcher->UnregisterListenSIDsObserver( *this );
       
    67         iWatcher->Close();
       
    68         }
       
    69 
       
    70     iObsArray.Close();
       
    71     delete iUsedSlot;
       
    72     }
       
    73 
       
    74 
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CPEngContactListNotifier2Imp::CPEngContactListNotifier2Imp
       
    78 // C++ default constructor can NOT contain any code, that
       
    79 // might leave.
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CPEngContactListNotifier2Imp::CPEngContactListNotifier2Imp(
       
    83     CPEngContactListNotifier2& aInterface,
       
    84     TInt aPriority )
       
    85         : iInterface( aInterface ),
       
    86         iCActivePriority( aPriority )
       
    87     {
       
    88     }
       
    89 
       
    90 
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CPEngContactListNotifier2Imp::ConstructL()
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CPEngContactListNotifier2Imp::ConstructL(
       
    97     const CPEngNWSessionSlotID2& aNWSessionSlotID )
       
    98     {
       
    99     iUsedSlot = CPEngNWSessionSlotStorageProxy::NewL( aNWSessionSlotID );
       
   100     iWatcher = PEngStorageManager::GetStorageManagerWatcherL( iUsedSlot->BaseId() );
       
   101     }
       
   102 
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CPEngContactListNotifier2Imp::Start()
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 TInt CPEngContactListNotifier2Imp::Start( const MDesCArray& aContactListNames )
       
   109     {
       
   110     //sanity checks to encapsulate notifier behaviour
       
   111     if ( iDying )
       
   112         {
       
   113         //if dying, the notifier restart is silently ignored
       
   114         //because notifier is evidently shutting down
       
   115         return KErrNone;
       
   116         }
       
   117 
       
   118     if ( iStarted )
       
   119         {
       
   120         return KErrInUse;
       
   121         }
       
   122 
       
   123     // EFalse as not keeping previous observing Ids
       
   124     TRAPD( err,
       
   125         {
       
   126         // and register for SID change events
       
   127         iWatcher->RegisterListenSIDsObserverL(
       
   128             aContactListNames,
       
   129             *this,
       
   130             MPEngStorageManagerWatcher::EPEngObserverNormalPriority,
       
   131             EFalse );
       
   132         } );
       
   133 
       
   134     if ( err == KErrNone )
       
   135         {
       
   136         iStarted = ETrue;
       
   137         }
       
   138 
       
   139     else
       
   140         {
       
   141         //Cancel the notification requests
       
   142         iWatcher->UnregisterListenSIDsObserver( *this );
       
   143         }
       
   144 
       
   145     return err;
       
   146     }
       
   147 
       
   148 
       
   149 
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CPEngContactListNotifier2Imp::Stop()
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CPEngContactListNotifier2Imp::Stop()
       
   156     {
       
   157     // we cannot actually unregister all SIDs/possibly can be implemented in Sotrage
       
   158     // anyway, this will cause to ignore all possible notifications
       
   159     if ( !iStarted )
       
   160         {
       
   161         return;
       
   162         }
       
   163     iWatcher->RestartSIDsObserver( *this );
       
   164     iStarted = EFalse;
       
   165     }
       
   166 
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CPEngContactListNotifier2Imp::IsActive()
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 TBool CPEngContactListNotifier2Imp::IsActive() const
       
   173     {
       
   174     return iStarted;
       
   175     }
       
   176 
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CPEngContactListNotifier2Imp::Add()
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 TInt CPEngContactListNotifier2Imp::Add( const TDesC& aContactListName )
       
   183 
       
   184     {
       
   185     if ( !iStarted )
       
   186         {
       
   187         return KErrNotReady;
       
   188         }
       
   189 
       
   190     TPEngMDesCArrayAdapter adapter( aContactListName );
       
   191     TRAPD( err,
       
   192         {
       
   193         //Update SID change request
       
   194         iWatcher->RegisterListenSIDsObserverL(
       
   195             adapter,
       
   196             *this,
       
   197             MPEngStorageManagerWatcher::EPEngObserverNormalPriority,
       
   198             ETrue );
       
   199         } );
       
   200 
       
   201     //If in error, tidy the watcher
       
   202     if ( err != KErrNone  )
       
   203         {
       
   204         iWatcher->RemoveSIDsFromSIDsObserver( adapter, *this );
       
   205         }
       
   206 
       
   207     return err;
       
   208     }
       
   209 
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CPEngContactListNotifier2Imp::Remove()
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 TInt CPEngContactListNotifier2Imp::Remove( const TDesC& aContactListName )
       
   216 
       
   217     {
       
   218     if ( !iStarted )
       
   219         {
       
   220         return KErrNotReady;
       
   221         }
       
   222 
       
   223     //No exceptions allowed below...
       
   224     TPEngMDesCArrayAdapter adapter( aContactListName );
       
   225     iWatcher->RemoveSIDsFromSIDsObserver( adapter, *this );
       
   226     return KErrNone;
       
   227     }
       
   228 
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CPEngContactListNotifier2Imp::AddObserver()
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 TInt CPEngContactListNotifier2Imp::AddObserver(
       
   235     MPEngContactListObserver2& aObserver )
       
   236 
       
   237     {
       
   238     return iObsArray.AddObserver( &aObserver );
       
   239     }
       
   240 
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CPEngContactListNotifier2Imp::RemoveObserver()
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 TInt CPEngContactListNotifier2Imp::RemoveObserver(
       
   247     MPEngContactListObserver2& aObserver )
       
   248 
       
   249     {
       
   250     return iObsArray.RemoveObserver( &aObserver );
       
   251     }
       
   252 
       
   253 
       
   254 
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CPEngContactListNotifier2Imp::HandleSIDsChangeL()
       
   258 // From MPEngSIDChangeObserver
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 void CPEngContactListNotifier2Imp::HandleSIDsChangeL( CPtrCArray& aChangedSIDs )
       
   262     {
       
   263     //if not running, don't notify the observers
       
   264     if ( !iStarted )
       
   265         {
       
   266         return;
       
   267         }
       
   268 
       
   269     const TInt sidCount = aChangedSIDs.MdcaCount();
       
   270     for ( TInt ix = 0; ix < sidCount; ix++ )
       
   271         {
       
   272         MediateNotify( KErrNone, aChangedSIDs.MdcaPoint( ix ) );
       
   273         }
       
   274     }
       
   275 
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CPEngContactListNotifier2Imp::HandleSIDNotifyError()
       
   279 // From MPEngSIDChangeObserver
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 void CPEngContactListNotifier2Imp::HandleSIDNotifyError( TInt aError )
       
   283     {
       
   284     //if not running, don't notify the observers
       
   285     if ( !iStarted )
       
   286         {
       
   287         return;
       
   288         }
       
   289 
       
   290     MediateNotify( aError, KNullDesC );
       
   291     }
       
   292 
       
   293 
       
   294 // -----------------------------------------------------------------------------
       
   295 // CPEngContactListNotifier2Imp::MediateNotify()
       
   296 // -----------------------------------------------------------------------------
       
   297 //
       
   298 void CPEngContactListNotifier2Imp::MediateNotify( TInt aError,
       
   299                                                   const TDesC& aContactListName )
       
   300     {
       
   301     TGenNotifyMediator2 < MPEngContactListObserver2,
       
   302     CPEngContactListNotifier2&,
       
   303     const TDesC& >
       
   304     eventMediator( &MPEngContactListObserver2::HandleContactListChangeL,
       
   305                    &MPEngContactListObserver2::HandleContactListError,
       
   306                    iInterface,
       
   307                    aContactListName );
       
   308 
       
   309     if ( aError == KErrNone )
       
   310         {
       
   311         iObsArray.NotifyObservers( eventMediator );
       
   312         }
       
   313     else
       
   314         {
       
   315         iObsArray.NotifyErrorObservers( eventMediator, aError );
       
   316         }
       
   317     }
       
   318 
       
   319 
       
   320 
       
   321 // End of File
       
   322 
       
   323