omadm/omadmappui/src/NSmlDMDbNotifier.cpp
changeset 0 3ce708148e4d
equal deleted inserted replaced
-1:000000000000 0:3ce708148e4d
       
     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:  Provides methods for CNSmlDMDbNotifier class. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "NSmlDMDbNotifier.h"
       
    20 #include "NSmlDMSyncDebug.h"
       
    21 #include "NSmlDMdef.h"
       
    22 
       
    23 // -----------------------------------------------------------------------------
       
    24 // CNSmlDMDbNotifier::CNSmlDMDbNotifier
       
    25 // -----------------------------------------------------------------------------
       
    26 //
       
    27 CNSmlDMDbNotifier::CNSmlDMDbNotifier( RSyncMLSession* aSyncSession,
       
    28                                       MNSmlDMDbEventHandler* aHandler)
       
    29                                      : iSyncSession(aSyncSession),
       
    30                                        iHandler( aHandler )
       
    31  	{
       
    32  	}
       
    33 
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CNSmlDMDbNotifier::NewL
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CNSmlDMDbNotifier* CNSmlDMDbNotifier::NewL( RSyncMLSession* aSyncSession,
       
    40                                             MNSmlDMDbEventHandler* aHandler)
       
    41 	{
       
    42 	CNSmlDMDbNotifier* self = new (ELeave) CNSmlDMDbNotifier( aSyncSession,
       
    43 	                                                          aHandler );
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL();
       
    46 	CleanupStack::Pop(self);
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CNSmlDMDbNotifier::ConstructL
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 void CNSmlDMDbNotifier::ConstructL()
       
    55 	{
       
    56 	iActiveCaller = CNSmlDMActiveCaller::NewL(this);
       
    57 	}
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // Destructor
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CNSmlDMDbNotifier::~CNSmlDMDbNotifier()
       
    64 	{		
       
    65 	delete iActiveCaller;
       
    66 	iList.Close();
       
    67 	}
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CNSmlDMDbNotifier::RequestL
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 void CNSmlDMDbNotifier::RequestL()
       
    74 	{
       
    75 	Session().RequestEventL( *this );  // request MSyncMLEventObserver events
       
    76 	}
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CNSmlDMDbNotifier::OnSyncMLSessionEvent
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 void CNSmlDMDbNotifier::OnSyncMLSessionEvent( TEvent aEvent,
       
    83                                               TInt aIdentifier,
       
    84                                               TInt aError,
       
    85                                               TInt /*aAdditionalData*/ )
       
    86 	{
       
    87 	TNSmlDMDbEvent event;
       
    88 
       
    89 	event.iType = aEvent;
       
    90 	event.iError = aError;
       
    91 		
       
    92 	if (aEvent == MSyncMLEventObserver::EProfileCreated || 
       
    93 	    aEvent == MSyncMLEventObserver::EProfileChanged ||
       
    94 	    aEvent == MSyncMLEventObserver::EProfileDeleted )
       
    95 		{
       
    96 		event.iProfileId = aIdentifier;
       
    97 		}
       
    98 	else
       
    99 		{
       
   100 		event.iProfileId = KErrNotFound;
       
   101 		}
       
   102 	
       
   103 	TRAP_IGNORE( iList.AppendL (event) );
       
   104 	
       
   105 	CallObserverWithDelay();
       
   106 	TRAP_IGNORE( RequestL() );
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CNSmlDMDbNotifier::SetDisabled
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CNSmlDMDbNotifier::SetDisabled( TBool aDisable )
       
   114 	{
       
   115     iDisabled = aDisable;
       
   116 	}
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CNSmlDMDbNotifier::Reset
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 void CNSmlDMDbNotifier::Reset()
       
   123 	{
       
   124     iDisabled = EFalse;
       
   125 	iList.Reset();
       
   126 	}
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CNSmlDMDbNotifier::EventCount
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 TInt CNSmlDMDbNotifier::EventCount()
       
   133 	{
       
   134 	return iList.Count();
       
   135 	}
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CNSmlDMDbNotifier::Event
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 TNSmlDMDbEvent CNSmlDMDbNotifier::Event( TInt aIndex )
       
   142 	{
       
   143 	__ASSERT_DEBUG( aIndex >= 0 && aIndex < iList.Count(),
       
   144 	                TUtil::Panic(KErrGeneral) );
       
   145 
       
   146 	return iList[aIndex];
       
   147 	}
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CNSmlDMDbNotifier::Session
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 RSyncMLSession& CNSmlDMDbNotifier::Session()
       
   154 	{
       
   155 	__ASSERT_DEBUG( iSyncSession, TUtil::Panic(KErrGeneral) );
       
   156 	
       
   157 	return *iSyncSession;
       
   158 	}
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CNSmlDMDbNotifier::CallObserverWithDelay
       
   162 //
       
   163 // This is needed to avoid unnecessary observer calls (eg. in case 10 events are
       
   164 // reported in short time only last is reported to observer). 
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 void CNSmlDMDbNotifier::CallObserverWithDelay()
       
   168 	{
       
   169     iActiveCaller->Cancel();
       
   170 	iActiveCaller->Start( KErrNone, KListBoxUpdateDelay );
       
   171 	}
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CNSmlDMDbNotifier::CallObserver
       
   175 //
       
   176 // this function investigates received events from last observer call
       
   177 // and calls observer to inform UI what to do. 
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CNSmlDMDbNotifier::CallObserver()
       
   181 	{
       
   182 	TNSmlDMDbEvent event;
       
   183 	event.iProfileId = 0;
       
   184 	event.iType = 0;
       
   185 	event.iError = 0;
       
   186 	//
       
   187 	// check for critical error
       
   188 	//
       
   189 	if ( FindCloseEvent() != KErrNotFound )
       
   190 		{
       
   191 		event.iType = EClose;
       
   192 		TRAP_IGNORE( iHandler->HandleDbEventL( event ) );
       
   193 		return; // some database problem - UI should close
       
   194 		}
       
   195 	
       
   196 	//
       
   197 	// check if only one profile has changed
       
   198 	//
       
   199 	TInt index = FindSingleProfileEvent();
       
   200 	if ( index != KErrNotFound )
       
   201 		{
       
   202 		TNSmlDMDbEvent e = iList[index];
       
   203         if (e.iType == MSyncMLEventObserver::EProfileDeleted)
       
   204 			{
       
   205 			event.iType = EDelete;
       
   206 			event.iProfileId = e.iProfileId; 
       
   207 			}
       
   208 		else
       
   209 			{
       
   210 			event.iType = EUpdate;
       
   211 			event.iProfileId = e.iProfileId; 
       
   212 			}
       
   213 	
       
   214 		TRAP_IGNORE( iHandler->HandleDbEventL( event ) );
       
   215         return;
       
   216 		}
       
   217 		
       
   218 	//
       
   219 	// check if 2 or more profiles have changed
       
   220 	//
       
   221 	if (FindProfileEvent() != KErrNotFound)
       
   222 		{
       
   223 	    event.iType = EUpdateAll;
       
   224     	TRAP_IGNORE( iHandler->HandleDbEventL( event ) );
       
   225         return;
       
   226     	}
       
   227 	}
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // CNSmlDMDbNotifier::FindCloseEvent
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 TInt CNSmlDMDbNotifier::FindCloseEvent()
       
   234 	{
       
   235 	TInt count = iList.Count();
       
   236 
       
   237 	for ( TInt index = 0; index < count; index++ )
       
   238 		{
       
   239 		TNSmlDMDbEvent event = iList[index];
       
   240 		if ( event.iType == MSyncMLEventObserver::EServerTerminated )
       
   241 			{
       
   242 			return index;
       
   243 			}
       
   244 		}
       
   245 
       
   246 	return KErrNotFound;
       
   247 	}
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CNSmlDMDbNotifier::FindProfileEvent
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 TInt CNSmlDMDbNotifier::FindProfileEvent()
       
   254 	{
       
   255 	TInt count = iList.Count();
       
   256 
       
   257     for ( TInt index = 0; index < count; index++ )
       
   258 		{
       
   259 		TNSmlDMDbEvent event = iList[index];
       
   260 		if ( event.iProfileId != KErrNotFound )
       
   261 			{
       
   262 			return index;
       
   263 			}
       
   264 		}
       
   265 
       
   266 	return KErrNotFound;
       
   267 	}
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // CNSmlDMDbNotifier::FindSingleProfileEvent
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 TInt CNSmlDMDbNotifier::FindSingleProfileEvent()
       
   274 	{
       
   275 	TInt count = iList.Count();
       
   276 
       
   277 	// find out whether list contains update events for one profile only
       
   278 	for ( TInt index = 0; index < count; index++ )
       
   279 		{
       
   280 		TNSmlDMDbEvent event = iList[index];
       
   281 		if ( event.iProfileId != KErrNotFound &&
       
   282 		     IsUniqueProfileId(event.iProfileId) )
       
   283 			{
       
   284 			return index;
       
   285         	}
       
   286 		}
       
   287 
       
   288 	return KErrNotFound;
       
   289 	}
       
   290 
       
   291 // -----------------------------------------------------------------------------
       
   292 // CNSmlDMDbNotifier::IsUniqueProfileId
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 TBool CNSmlDMDbNotifier::IsUniqueProfileId( TInt aId )
       
   296 	{
       
   297 	TInt count = iList.Count();
       
   298 
       
   299     for ( TInt index = 0; index < count; index++ )
       
   300 		{
       
   301 		TNSmlDMDbEvent event = iList[index];
       
   302 		if ( event.iProfileId != KErrNotFound && event.iProfileId != aId )
       
   303 			{
       
   304 			return EFalse;
       
   305 			}
       
   306 		}
       
   307 
       
   308 	return ETrue;
       
   309 	}
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CNSmlDMDbNotifier::HandleActiveCallL
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 void CNSmlDMDbNotifier::HandleActiveCallL()
       
   316 	{
       
   317 	if ( iDisabled )
       
   318 		{
       
   319 		return;  // UI has disabled notifications
       
   320 		}
       
   321 
       
   322 	if ( iSyncRunning )
       
   323 		{
       
   324 		return; // no UI updates while syncing
       
   325 		}
       
   326 
       
   327 	CallObserver();
       
   328 	Reset();
       
   329 	}
       
   330 
       
   331 // End of File