phonebookengines_old/contactsmodel/cntmodel/src/CCntNotificationMonitor.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19  @released
       
    20 */
       
    21 
       
    22 
       
    23 #include "ccontactprivate.h"
       
    24 #include "CCntNotificationMonitor.h"
       
    25 #include "rcntmodel.h"
       
    26 
       
    27 
       
    28 extern void DebugLogNotification(const TDesC& aMethod, const TContactDbObserverEvent &aEvent);
       
    29 
       
    30 
       
    31 CCntNotificationMonitor::CCntNotificationMonitor(RCntModel& aSession) 
       
    32 	: CActive(EPriorityStandard), iSession(aSession)
       
    33 	{}
       
    34 
       
    35 
       
    36 CCntNotificationMonitor::~CCntNotificationMonitor()
       
    37 	{
       
    38 	Cancel();
       
    39 	}
       
    40 
       
    41 
       
    42 void CCntNotificationMonitor::DoCancel() 
       
    43 	{
       
    44 	// TRequestStatus* status = &iStatus;
       
    45 	// User::RequestComplete(status, KErrNone);
       
    46 	}	
       
    47 
       
    48 
       
    49 /**
       
    50 This method is called each time the request for an event from the Server is
       
    51 completed.
       
    52 */
       
    53 void CCntDbNotifyMonitor::RunL()
       
    54 	{
       
    55 	if ( iStatus == KErrDied || iStatus == KErrServerTerminated )
       
    56 		{
       
    57 		// We have lost our connection to the Server.  Attempt to restart it,
       
    58 		// reconnect the session and open the Contacts database.
       
    59 		iSession.HandlePrematureServerTerminationL();
       
    60 		// Request another event.
       
    61 		Start();
       
    62 		return;
       
    63 		}
       
    64 	
       
    65 	
       
    66 
       
    67 	
       
    68 	TInt count = iObserverArray.Count();
       
    69 	TContactDbObserverEvent& event = iEventMsg();
       
    70 	
       
    71 #if defined(__VERBOSE_DEBUG__)	
       
    72 	DebugLogNotification(_L("[CNTMODEL] CCntDbNotifyMonitor::RunL"), event);	
       
    73 #endif
       
    74 
       
    75 	// Notify all registered observers of the event.
       
    76 	for (TInt i = 0 ; i < count ; ++i)
       
    77 		{
       
    78 		iCurrentProcessedObserver = i;
       
    79 		// Ignore any leave so we can notify all registered observers.
       
    80 		TRAP_IGNORE(iObserverArray[i]->HandleDatabaseEventL(event));
       
    81 		// The call to HandleDatabaseEventL() above may remove or/add observer/observers
       
    82 		// so get the count again to cover this scenario. Also we have to update current
       
    83 		// loop counter.
       
    84 		i = iCurrentProcessedObserver;
       
    85 		count = iObserverArray.Count();
       
    86 		}
       
    87 	
       
    88 	// Request another event.
       
    89 	Start();
       
    90 	}
       
    91 
       
    92 	
       
    93 /**
       
    94 First phase constructor.
       
    95 */
       
    96 CCntDbNotifyMonitor::CCntDbNotifyMonitor(RCntModel& aSession) 
       
    97 	: CCntNotificationMonitor(aSession), iCurrentProcessedObserver(KErrNotFound)
       
    98 	{}
       
    99 
       
   100 	
       
   101 /**
       
   102 Second phase constructor.
       
   103 
       
   104 @param aSession Client-side session handle for Contacts Model Server.
       
   105 */
       
   106 CCntDbNotifyMonitor* CCntDbNotifyMonitor::NewL(RCntModel& aSession)
       
   107 	{
       
   108 	CCntDbNotifyMonitor* self = new (ELeave) CCntDbNotifyMonitor(aSession);
       
   109 	CActiveScheduler::Add(self);
       
   110 	return(self);
       
   111 	}
       
   112 
       
   113 
       
   114 CCntDbNotifyMonitor::~CCntDbNotifyMonitor()
       
   115 	{
       
   116 	iObserverArray.Close();
       
   117 	}
       
   118 
       
   119 	
       
   120 /**
       
   121 This method is called to request a Contacts database event from the Server.
       
   122 The RunL() method will be called when an event is available (i.e. when the
       
   123 Server completes the request.
       
   124 */
       
   125 void CCntDbNotifyMonitor::Start()
       
   126 	{
       
   127 	// Check if a request for a Contacts database event from the Server has
       
   128 	// already been issued.
       
   129 	if (IsActive())
       
   130 		{
       
   131 		return;
       
   132 		}
       
   133 	iStatus=KRequestPending;
       
   134 	iSession.StartNotificationTransfer(iStatus, iEventMsg);	
       
   135 	SetActive();
       
   136 	}
       
   137 
       
   138 
       
   139 /**
       
   140 Add a Contacts database event observer.
       
   141 
       
   142 @param aObserver The Contacts database event observer to add.
       
   143 */
       
   144 void CCntDbNotifyMonitor::AddObserverL(MContactDbObserver& aObserver)
       
   145 	{
       
   146 	// Observers can be added from HandleDatabaseEventL() called from CCntDbNotifyMonitor::RunL()
       
   147 	// To avoid overcomplicated algorithms for concurent access to handlers array, new observer will
       
   148 	// be added always at the end of array. Whilst duplications are not allowed, a search is 
       
   149 	// needed to check if observer is already added to observers array
       
   150 	
       
   151 	const TInt index = iObserverArray.Find(&aObserver);
       
   152 	if( index != KErrNotFound )
       
   153 		{
       
   154 		User::Leave( KErrAlreadyExists );
       
   155 		}
       
   156 	
       
   157 	User::LeaveIfError( iObserverArray.Append(&aObserver) );
       
   158 	
       
   159 	// At least one observer now added so start request for Contacts database
       
   160 	// event from the Server (if it hasn't already been started i.e. if the AO
       
   161 	// is not already active).
       
   162 	Start();
       
   163 	}
       
   164 
       
   165 
       
   166 /**
       
   167 Remove a Contacts database event observer.
       
   168 
       
   169 @param aObserver The Contacts database event observer to remove.
       
   170 */
       
   171 void CCntDbNotifyMonitor::RemoveObserver(const MContactDbObserver& aObserver)
       
   172 	{
       
   173 	TInt index = iObserverArray.Find(&aObserver);
       
   174 	if (index != KErrNotFound)
       
   175 		{
       
   176 		iObserverArray.Remove(index);
       
   177 		
       
   178 		// Observers can be removed from HandleDatabaseEventL() called from CCntDbNotifyMonitor::RunL()
       
   179 		// In order to notify correctly remaining observers, we need to update current processed observer.
       
   180 		if( index <= iCurrentProcessedObserver )
       
   181 			{
       
   182 			--iCurrentProcessedObserver;
       
   183 			}
       
   184 			
       
   185 		}
       
   186 	// If there are no more observers then cancel the event request in the
       
   187 	// Server.
       
   188 	if (iObserverArray.Count() == 0)
       
   189 		{
       
   190 		iSession.EndNotificationTransfer();
       
   191 		}
       
   192 	}
       
   193 	
       
   194 
       
   195 /**
       
   196 Get the number of registered observers.
       
   197 
       
   198 @return The number of registered observers.
       
   199 */
       
   200 TInt CCntDbNotifyMonitor::NumberOfListeners() const
       
   201 	{
       
   202 	return iObserverArray.Count();
       
   203 	}
       
   204 
       
   205 
       
   206 TInt CCntDbNotifyMonitor::RunError(TInt /*aError*/)
       
   207 	{
       
   208 #if defined(__VERBOSE_DEBUG__)	
       
   209 	TContactDbObserverEvent& event = iEventMsg();
       
   210 	DebugLogNotification(_L("[CNTMODEL] CCntDbNotifyMonitor::RunError"), event);		
       
   211 #endif
       
   212 
       
   213 	Start();
       
   214 		
       
   215 	return KErrNone;
       
   216 	}