omads/omadsappui/AspSyncUtil/src/AspDbNotifier.cpp
changeset 0 dab8a81a92de
equal deleted inserted replaced
-1:000000000000 0:dab8a81a92de
       
     1 /*
       
     2 * Copyright (c) 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "AspDbNotifier.h"
       
    21 #include "AspDebug.h"
       
    22 
       
    23 
       
    24 // ============================ MEMBER FUNCTIONS ===============================
       
    25 
       
    26 
       
    27 /******************************************************************************
       
    28  * class TAspDbEvent
       
    29  ******************************************************************************/
       
    30 
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // TAspDbEvent::TAspDbEvent
       
    34 //
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 TAspDbEvent::TAspDbEvent(TInt aType)
       
    38  : iType(aType), iProfileId(KErrNotFound), iError(KErrNone)
       
    39 	 {
       
    40 	 }
       
    41 
       
    42 
       
    43 
       
    44 /******************************************************************************
       
    45  * class CAspDbNotifier
       
    46  ******************************************************************************/
       
    47 
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CAspDbNotifier::CAspDbNotifier
       
    51 //
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CAspDbNotifier::CAspDbNotifier(const TAspParam& aParam, MAspDbEventHandler* aHandler)
       
    55  : iSyncSession(aParam.iSyncSession), iHandler(aHandler)
       
    56  	{
       
    57  	iSessionEventRequested = EFalse;
       
    58  	}
       
    59 
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CAspDbNotifier::NewL
       
    63 //
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CAspDbNotifier* CAspDbNotifier::NewL(const TAspParam& aParam, MAspDbEventHandler* aHandler)
       
    67 	{
       
    68 	CAspDbNotifier* self = new (ELeave) CAspDbNotifier(aParam, aHandler);
       
    69 	CleanupStack::PushL(self);
       
    70 	self->ConstructL();
       
    71 	CleanupStack::Pop(self);
       
    72 	return self;
       
    73 	}
       
    74 
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CAspDbNotifier::ConstructL
       
    78 //
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CAspDbNotifier::ConstructL()
       
    82 	{
       
    83     FLOG( _L("CAspDbNotifier::ConstructL START") );
       
    84     
       
    85 	iActiveCaller = CAspActiveCaller::NewL(this);
       
    86     
       
    87     FLOG( _L("CAspDbNotifier::ConstructL END") );
       
    88 	}
       
    89 
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // Destructor
       
    93 //
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 CAspDbNotifier::~CAspDbNotifier()
       
    97 	{		
       
    98     FLOG( _L("CAspDbNotifier::~CAspDbNotifier START") );
       
    99     
       
   100 	delete iActiveCaller;
       
   101 	iList.Close();
       
   102     
       
   103     if (iSessionEventRequested)
       
   104     	{
       
   105     	Session().CancelEvent();
       
   106     	}
       
   107     	
       
   108     if (iDestroyedPtr)
       
   109         {
       
   110         *iDestroyedPtr = ETrue;
       
   111         iDestroyedPtr = NULL;
       
   112         }
       
   113 
       
   114     FLOG( _L("CAspDbNotifier::~CAspDbNotifier END") );
       
   115 	}
       
   116 
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CAspDbNotifier::RequestL
       
   120 //
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 void CAspDbNotifier::RequestL()
       
   124 	{
       
   125 	if (!iSessionEventRequested)
       
   126 		{
       
   127 		Session().RequestEventL(*this);  // request MSyncMLEventObserver events
       
   128 		iSessionEventRequested = ETrue;
       
   129 		}
       
   130 	}
       
   131 
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CAspDbNotifier::OnSyncMLSessionEvent (from MSyncMLEventObserver)
       
   135 //
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CAspDbNotifier::OnSyncMLSessionEvent(TEvent aEvent, TInt aIdentifier, TInt aError, TInt aAdditionalData)
       
   139 	{
       
   140     FLOG( _L("CAspDbNotifier::OnSyncMLSessionEvent START") );
       
   141 
       
   142 #ifdef _DEBUG
       
   143     LogSessionEvent(aEvent, aIdentifier, aError);
       
   144 #endif
       
   145 
       
   146     TRAP_IGNORE(HandleSessionEventL(aEvent, aIdentifier, aError, aAdditionalData));
       
   147 	
       
   148     FLOG( _L("CAspDbNotifier::OnSyncMLSessionEvent END") );
       
   149     }
       
   150 
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CAspDbNotifier::HandleSessionEventL
       
   154 //
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 void CAspDbNotifier::HandleSessionEventL(TEvent aEvent, TInt aIdentifier, TInt aError, TInt /*aAdditionalData*/)
       
   158 	{
       
   159 	if (aEvent != MSyncMLEventObserver::EProfileCreated &&
       
   160 	    aEvent != MSyncMLEventObserver::EProfileChanged &&
       
   161 	    aEvent != MSyncMLEventObserver::EProfileDeleted &&
       
   162 	    aEvent != MSyncMLEventObserver::EServerTerminated)
       
   163 		{
       
   164 		return; // setting dialog does not need this event
       
   165 		}
       
   166 	
       
   167 	TAspDbEvent event(aEvent);
       
   168 
       
   169     event.iError = aError;
       
   170 		
       
   171 	if (aEvent == MSyncMLEventObserver::EProfileCreated || 
       
   172 	    aEvent == MSyncMLEventObserver::EProfileChanged ||
       
   173 	    aEvent == MSyncMLEventObserver::EProfileDeleted )
       
   174 		{
       
   175 		event.iProfileId = aIdentifier;
       
   176 		}
       
   177 	else
       
   178 		{
       
   179 		event.iProfileId = KErrNotFound;
       
   180 		}
       
   181 	
       
   182 	iList.AppendL (event);
       
   183 	
       
   184 	CallObserverWithDelay();
       
   185 	RequestL();
       
   186     }
       
   187 
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CAspDbNotifier::CreateUpdateEventL
       
   191 //
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 void CAspDbNotifier::CreateUpdateEventL(TInt aIdentifier, TInt aError)
       
   195 	{
       
   196 	TAspDbEvent event(MSyncMLEventObserver::EProfileChanged);
       
   197 	event.iProfileId = aIdentifier;
       
   198     event.iError = aError;
       
   199 	
       
   200 	iList.Reset();
       
   201 	iList.AppendL (event);
       
   202 	
       
   203 	CallObserverWithDelay();
       
   204 	RequestL();
       
   205     }
       
   206 
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CAspDbNotifier::CheckUpdateEventL
       
   210 //
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CAspDbNotifier::CheckUpdateEventL()
       
   214 	{
       
   215 	if (iList.Count() == 0)
       
   216 		{
       
   217 		return; // no database events - no need to update UI
       
   218 		}
       
   219 		
       
   220 	CallObserverWithDelay();
       
   221 	RequestL();
       
   222     }
       
   223 
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CAspDbNotifier::SetDisabled
       
   227 //
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 void CAspDbNotifier::SetDisabled(TBool aDisable)
       
   231 	{
       
   232     iDisabled = aDisable;
       
   233 	}
       
   234 
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CAspDbNotifier::Reset
       
   238 //
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 void CAspDbNotifier::Reset()
       
   242 	{
       
   243     iDisabled = EFalse;
       
   244 	iList.Reset();
       
   245 	}
       
   246 
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CAspDbNotifier::EventCount
       
   250 //
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 TInt CAspDbNotifier::EventCount()
       
   254 	{
       
   255 	return iList.Count();
       
   256 	}
       
   257 
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CAspDbNotifier::Event
       
   261 //
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 TAspDbEvent CAspDbNotifier::Event(TInt aIndex)
       
   265 	{
       
   266 	__ASSERT_DEBUG(aIndex>=0 && aIndex<iList.Count(), TUtil::Panic(KErrGeneral));
       
   267 
       
   268 	return iList[aIndex];
       
   269 	}
       
   270 
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CAspDbNotifier::Session
       
   274 // 
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 RSyncMLSession& CAspDbNotifier::Session()
       
   278 	{
       
   279 	__ASSERT_DEBUG(iSyncSession, TUtil::Panic(KErrGeneral));
       
   280 	
       
   281 	return *iSyncSession;
       
   282 	}
       
   283 
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CAspDbNotifier::CallObserverWithDelay
       
   287 //
       
   288 // This is needed to avoid unnecessary observer calls (eg. in case 10 events are
       
   289 // reported in short time only last is reported to observer). 
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CAspDbNotifier::CallObserverWithDelay(void)
       
   293 	{
       
   294     FLOG( _L("CAspDbNotifier::CallObserverWithDelay START") );
       
   295     
       
   296     iActiveCaller->Cancel();
       
   297 	iActiveCaller->Start(KErrNone, KListBoxUpdateDelay);
       
   298 		
       
   299     FLOG( _L("CAspDbNotifier::CallObserverWithDelay END") );
       
   300 	}
       
   301 
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CAspDbNotifier::CallObserver
       
   305 //
       
   306 // this function investigates received events from last observer call
       
   307 // and calls observer to inform UI what to do. 
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 void CAspDbNotifier::CallObserver()
       
   311 	{
       
   312     FLOG( _L("CAspDbNotifier::CallObserver START") );
       
   313 	
       
   314 	TAspDbEvent event(EUpdateAll);
       
   315 
       
   316 	//
       
   317 	// check for critical error
       
   318 	//
       
   319 	if (FindCloseEvent() != KErrNotFound)
       
   320 		{
       
   321 		event.iType = EClose;
       
   322 		TRAP_IGNORE(iHandler->HandleDbEventL(event));
       
   323 		
       
   324 		FLOG( _L("CAspDbNotifier::CallObserver END") );
       
   325 		return; // some database problem - UI should close
       
   326 		}
       
   327 	
       
   328 	
       
   329 	//
       
   330 	// check if only one profile has changed
       
   331 	//
       
   332 	TInt index = FindSingleProfileEvent();
       
   333 	if (index != KErrNotFound)
       
   334 		{
       
   335 		TAspDbEvent& e = iList[index];
       
   336         event.iProfileId = e.iProfileId; 
       
   337         
       
   338         event.iType = EUpdate;
       
   339         if (ProfileDeleted())
       
   340 			{
       
   341 			event.iType = EDelete;
       
   342 			}
       
   343 	
       
   344 		TRAP_IGNORE(iHandler->HandleDbEventL(event));
       
   345 		
       
   346         FLOG( _L("CAspDbNotifier::CallObserver END") );
       
   347         return;
       
   348 		}
       
   349 	
       
   350 		
       
   351 	//
       
   352 	// check if 2 or more profiles have changed
       
   353 	//
       
   354 	if (FindProfileEvent() != KErrNotFound)
       
   355 		{
       
   356 	    event.iType = EUpdateAll;
       
   357     	TRAP_IGNORE(iHandler->HandleDbEventL(event));
       
   358 	
       
   359         FLOG( _L("CAspDbNotifier::CallObserver END") );
       
   360         return;
       
   361     	}
       
   362     
       
   363     FLOG( _L("CAspDbNotifier::CallObserver END") );
       
   364 	}
       
   365 
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CAspDbNotifier::FindCloseEvent
       
   369 //
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 TInt CAspDbNotifier::FindCloseEvent()
       
   373 	{
       
   374 	TInt count = iList.Count();
       
   375 
       
   376 	for (TInt i=0; i<count; i++)
       
   377 		{
       
   378 		TAspDbEvent& event = iList[i];
       
   379 		if (event.iType == MSyncMLEventObserver::EServerTerminated)
       
   380 			{
       
   381 			return i;
       
   382 			}
       
   383 		}
       
   384 
       
   385 	return KErrNotFound;
       
   386 	}
       
   387 
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CAspDbNotifier::FindProfileEvent
       
   391 //
       
   392 // -----------------------------------------------------------------------------
       
   393 //
       
   394 TInt CAspDbNotifier::FindProfileEvent()
       
   395 	{
       
   396 	TInt count = iList.Count();
       
   397 
       
   398     for (TInt i=0; i<count; i++)
       
   399 		{
       
   400 		TAspDbEvent& event = iList[i];
       
   401 		if (event.iProfileId != KErrNotFound)
       
   402 			{
       
   403 			return i;
       
   404 			}
       
   405 		}
       
   406 
       
   407 	return KErrNotFound;
       
   408 	}
       
   409 
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CAspDbNotifier::FindSingleProfileEvent
       
   413 //
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 TInt CAspDbNotifier::FindSingleProfileEvent()
       
   417 	{
       
   418 	TInt count = iList.Count();
       
   419 
       
   420 	// find out whether list contains update events for one profile only
       
   421 	for (TInt i=0; i<count; i++)
       
   422 		{
       
   423 		TAspDbEvent& event = iList[i];
       
   424 		if (event.iProfileId != KErrNotFound && IsUniqueProfileId(event.iProfileId))
       
   425 			{
       
   426 			return i;
       
   427         	}
       
   428 		}
       
   429 
       
   430 	return KErrNotFound;
       
   431 	}
       
   432 
       
   433 
       
   434 // -----------------------------------------------------------------------------
       
   435 // CAspDbNotifier::ProfileDeleted
       
   436 //
       
   437 // -----------------------------------------------------------------------------
       
   438 //
       
   439 TBool CAspDbNotifier::ProfileDeleted()
       
   440 	{
       
   441 	TInt count = iList.Count();
       
   442 
       
   443 	for (TInt i=0; i<count; i++)
       
   444 		{
       
   445 		TAspDbEvent& event = iList[i];
       
   446 		if (event.iType == MSyncMLEventObserver::EProfileDeleted)
       
   447 			{
       
   448 			return ETrue;
       
   449 			}
       
   450 		}
       
   451 
       
   452 	return EFalse;
       
   453 	}
       
   454 
       
   455 
       
   456 // -----------------------------------------------------------------------------
       
   457 // CAspDbNotifier::IsUniqueProfileId
       
   458 //
       
   459 // -----------------------------------------------------------------------------
       
   460 //
       
   461 TBool CAspDbNotifier::IsUniqueProfileId(TInt aId)
       
   462 	{
       
   463 	TInt count = iList.Count();
       
   464 
       
   465     for (TInt i=0; i<count; i++)
       
   466 		{
       
   467 		TAspDbEvent& event = iList[i];
       
   468 		if (event.iProfileId != KErrNotFound && event.iProfileId != aId)
       
   469 			{
       
   470 			return EFalse;
       
   471 			}
       
   472 		}
       
   473 
       
   474 	return ETrue;
       
   475 	}
       
   476 
       
   477 
       
   478 // -----------------------------------------------------------------------------
       
   479 // CAspDbNotifier::HandleActiveCallL (from MAspActiveCallerObserver)
       
   480 //
       
   481 // -----------------------------------------------------------------------------
       
   482 //
       
   483 void CAspDbNotifier::HandleActiveCallL(TInt /*aCallId*/)
       
   484 	{
       
   485     FLOG( _L("CAspDbNotifier::HandleActiveCallL START") );
       
   486 
       
   487 	if (iDisabled)
       
   488 		{
       
   489 		FLOG( _L("CAspDbNotifier::HandleActiveCallL: event observing disabled") );
       
   490 		FLOG( _L("CAspDbNotifier::HandleActiveCallL END") );
       
   491 		return;  // UI has disabled notifications
       
   492 		}
       
   493 
       
   494     TBool destroyed(EFalse);
       
   495     iDestroyedPtr = &destroyed;
       
   496 
       
   497 	CallObserver();
       
   498 	
       
   499     if (!destroyed)   // destructor sets this to ETrue
       
   500         {
       
   501     	Reset();
       
   502     	iDestroyedPtr = NULL;
       
   503         }
       
   504 	
       
   505     FLOG( _L("CAspDbNotifier::HandleActiveCallL END") );
       
   506 	}
       
   507 
       
   508 
       
   509 
       
   510 #ifdef _DEBUG
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // LogSessionEvent
       
   514 //
       
   515 // -----------------------------------------------------------------------------
       
   516 //
       
   517 void CAspDbNotifier::LogSessionEvent(TEvent& aEvent, TInt aIdentifier, TInt aError)
       
   518 	{
       
   519     TBuf<KBufSize> eventStr;
       
   520     GetSyncEventText(eventStr, aEvent);
       
   521 
       
   522 	if (aError == KErrNone)
       
   523 		{
       
   524 		FTRACE( RDebug::Print(_L("event='%S' id=%d"), &eventStr, aIdentifier) );
       
   525 		}
       
   526 	else
       
   527 		{
       
   528 		FTRACE( RDebug::Print(_L("### event='%S' id=%d err=%d ###"), &eventStr, aIdentifier, aError) );
       
   529 		}
       
   530 	}
       
   531 
       
   532 
       
   533 // -----------------------------------------------------------------------------
       
   534 // GetSyncEventText
       
   535 //
       
   536 // -----------------------------------------------------------------------------
       
   537 //
       
   538 void CAspDbNotifier::GetSyncEventText(TDes& aText, MSyncMLEventObserver::TEvent aEvent)
       
   539 	{
       
   540     aText = _L("Unknown");
       
   541     		
       
   542 	if (aEvent == MSyncMLEventObserver::EJobStart)
       
   543 		{
       
   544 		aText = _L("EJobStart");
       
   545 		}
       
   546 	if (aEvent == MSyncMLEventObserver::EJobStartFailed)
       
   547 		{
       
   548 		aText = _L("EJobStartFailed");
       
   549 		}
       
   550 	if (aEvent == MSyncMLEventObserver::EJobStop)
       
   551 		{
       
   552 		aText = _L("EJobStop");
       
   553 		}
       
   554 	if (aEvent == MSyncMLEventObserver::EJobRejected)
       
   555 		{
       
   556 		aText = _L("EJobRejected");
       
   557 		}
       
   558 	if (aEvent == MSyncMLEventObserver::EProfileCreated)
       
   559 		{
       
   560 		aText = _L("EProfileCreated");
       
   561 		}
       
   562 	if (aEvent == MSyncMLEventObserver::EProfileChanged)
       
   563 		{
       
   564 		aText = _L("EProfileChanged");
       
   565 		}
       
   566 	if (aEvent == MSyncMLEventObserver::EProfileDeleted)
       
   567 		{
       
   568 		aText = _L("EProfileDeleted");
       
   569 		}
       
   570 	if (aEvent == MSyncMLEventObserver::ETransportTimeout)
       
   571 		{
       
   572 		aText = _L("ETransportTimeout");
       
   573 		}
       
   574 	if (aEvent == MSyncMLEventObserver::EServerSuspended)
       
   575 		{
       
   576 		aText = _L("EServerSuspended");
       
   577 		}
       
   578 	if (aEvent == MSyncMLEventObserver::EServerTerminated)
       
   579 		{
       
   580 		aText = _L("EServerTerminated");
       
   581 		}
       
   582 	}
       
   583 
       
   584 #endif
       
   585 
       
   586 // End of file