omads/omadsappui/AspSyncUtil/src/AspAutoSyncHandler.cpp
changeset 0 dab8a81a92de
equal deleted inserted replaced
-1:000000000000 0:dab8a81a92de
       
     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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "AspAutoSyncHandler.h"
       
    22 #include "AspUtil.h"
       
    23 #include "AspDialogUtil.h"
       
    24 #include "AspDebug.h"
       
    25 
       
    26 #include <schinfo.h> // TScheduleEntryInfo2
       
    27 #include <s32mem.h>  // RDesWriteStream
       
    28 #include <s32file.h> // RFileReadStream
       
    29 //#include <centralrepository.h>  // CRepository
       
    30 #include <rconnmon.h>
       
    31 
       
    32 
       
    33 
       
    34 
       
    35 _LIT(KPeakTaskData, "aspschedulehandler peak task");
       
    36 _LIT(KOffPeakTaskData, "aspschedulehandler off-peak task");
       
    37 
       
    38 
       
    39 // ============================ MEMBER FUNCTIONS ===============================
       
    40 
       
    41 
       
    42 	
       
    43 /*******************************************************************************
       
    44  * class CAspAutoSyncHandler
       
    45  *******************************************************************************/
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CAspAutoSyncHandler::NewL
       
    49 //
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CAspAutoSyncHandler* CAspAutoSyncHandler::NewL()
       
    53     {
       
    54     CAspAutoSyncHandler* self = new (ELeave) CAspAutoSyncHandler();
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57     CleanupStack::Pop(self);
       
    58 
       
    59     return(self);
       
    60     }
       
    61 
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CAspAutoSyncHandler::CAspAutoSyncHandler
       
    65 // 
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 CAspAutoSyncHandler::CAspAutoSyncHandler()
       
    69 	{
       
    70 	iSyncError = KErrNone;
       
    71 	iSyncSessionOpen = EFalse;
       
    72 	iSyncRunning = EFalse;
       
    73     }
       
    74 
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CAspAutoSyncHandler::ConstructL
       
    78 //
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CAspAutoSyncHandler::ConstructL()
       
    82     {
       
    83     iWait = new (ELeave) CActiveSchedulerWait();
       
    84     
       
    85     iSchedule = CAspSchedule::NewL();
       
    86     } 
       
    87 
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // Destructor
       
    91 //
       
    92 // ----------------------------------------------------------------------------
       
    93 //
       
    94 CAspAutoSyncHandler::~CAspAutoSyncHandler()
       
    95     {
       
    96     if (iWait && iWait->IsStarted())
       
    97         {
       
    98         iWait->AsyncStop();
       
    99         }
       
   100     delete iWait;
       
   101     
       
   102     delete iSchedule;
       
   103     
       
   104     CloseSyncSession();
       
   105     }
       
   106 
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CAspAutoSyncHandler::OnSyncMLSessionEvent (from MSyncMLEventObserver)
       
   110 //
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CAspAutoSyncHandler::OnSyncMLSessionEvent(TEvent aEvent, TInt /*aIdentifier*/,
       
   114                                            TInt aError, TInt /*aAdditionalData*/)
       
   115 	{
       
   116 	FLOG( _L("CAspAutoSyncHandler::OnSyncMLSessionEvent START") );
       
   117 	
       
   118 	if (aEvent == EJobStart)
       
   119 		{
       
   120         FLOG( _L("EJobStart") );
       
   121 		}
       
   122 
       
   123 	if (aEvent == EJobStop)
       
   124 		{
       
   125     	if (aError != KErrNone)
       
   126 			{
       
   127 			iSyncError = aError;
       
   128     		}
       
   129 
       
   130         FLOG( _L("EJobStop") );
       
   131         
       
   132     	iWait->AsyncStop();
       
   133 		}
       
   134     	
       
   135 	if (aEvent == EJobStartFailed || aEvent == EJobRejected || aEvent == EServerTerminated)
       
   136 		{
       
   137 		FLOG( _L("EJobStartFailed or EJobRejected or EServerTerminated") );
       
   138 		iWait->AsyncStop();
       
   139 		}
       
   140 		
       
   141 	FLOG( _L("CAspAutoSyncHandler::OnSyncMLSessionEvent END") );
       
   142     }
       
   143 
       
   144 
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // CAspAutoSyncHandler::OnSyncMLSyncError (from MSyncMLProgressObserver)
       
   148 //
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CAspAutoSyncHandler::OnSyncMLSyncError(TErrorLevel aErrorLevel,
       
   152                                                 TInt aError, TInt /*aTaskId*/,
       
   153                                                 TInt /*aInfo1*/, TInt /*aInfo2*/)
       
   154 	{
       
   155 	FLOG( _L("CAspAutoSyncHandler::OnSyncMLSyncError START") );
       
   156 	
       
   157 	if (aErrorLevel == ESmlFatalError)
       
   158 		{
       
   159 		iSyncError = aError;
       
   160 		iWait->AsyncStop();
       
   161 		}
       
   162 
       
   163 	FLOG( _L("CAspAutoSyncHandler::OnSyncMLSyncError END") );
       
   164 	}
       
   165 
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CAspAutoSyncHandler::OnSyncMLSyncProgress (from MSyncMLProgressObserver)
       
   169 //
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CAspAutoSyncHandler::OnSyncMLSyncProgress(TStatus /*aStatus*/,
       
   173                                                    TInt /*aInfo1*/, TInt /*aInfo2*/)
       
   174 	{
       
   175 	// not needed in scheduled sync
       
   176 	}
       
   177 
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // CAspAutoSyncHandler::OnSyncMLDataSyncModifications (from MSyncMLProgressObserver)
       
   181 //
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 void CAspAutoSyncHandler::OnSyncMLDataSyncModifications(TInt /*aTaskId*/, 
       
   185 	         const TSyncMLDataSyncModifications& /*aClientModifications*/,
       
   186 	         const TSyncMLDataSyncModifications& /*aServerModifications*/)
       
   187 	{
       
   188 	// not needed in scheduled sync
       
   189 	}
       
   190 	
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CAspAutoSyncHandler::OpenSyncSessionL
       
   194 // 
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 void CAspAutoSyncHandler::OpenSyncSessionL()
       
   198 	{
       
   199 	if (!iSyncSessionOpen)
       
   200 		{
       
   201         TRAPD(err, iSyncSession.OpenL());
       
   202         
       
   203         if (err != KErrNone)
       
   204         	{
       
   205         	FLOG( _L("### CAspAutoSyncHandler: RSyncMLSession::OpenL failed (%d) ###"), err );
       
   206         	User::Leave(err);
       
   207         	}
       
   208 
       
   209 		iSyncSessionOpen = ETrue;
       
   210 		}
       
   211     }
       
   212 
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CloseSyncSession
       
   216 // 
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 void CAspAutoSyncHandler::CloseSyncSession()
       
   220 	{
       
   221 	if (iSyncSessionOpen)
       
   222 		{
       
   223 		iSyncJob.Close(); // this can be done when job has not been opened
       
   224 		iSyncSession.Close();
       
   225 		iSyncSessionOpen = EFalse;
       
   226 		}
       
   227     }
       
   228 
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CAspAutoSyncHandler::CheckMandatoryData
       
   232 // 
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 TInt CAspAutoSyncHandler::CheckMandatoryDataL(TInt aProfileId)
       
   236 	{
       
   237     TAspParam param(EApplicationIdSync, &iSyncSession);
       
   238 
       
   239     CAspProfile* profile = CAspProfile::NewLC(param);
       
   240     profile->OpenL(aProfileId, CAspProfile::EOpenRead, CAspProfile::EAllProperties);
       
   241     
       
   242 	param.iProfile = profile;
       
   243 	param.iMode = CAspContentList::EInitAll;
       
   244 	CAspContentList* list = CAspContentList::NewLC(param);
       
   245 
       
   246    	//TInt contentCount = 0;
       
   247 	TInt mandatoryConnectionData = CAspProfile::CheckMandatoryConnData(profile);
       
   248 	//TInt mandatoryContentData    = list->CheckMandatoryDataL(contentCount);
       
   249 	
       
   250 	TInt mandatoryBearerType = EMandatoryOk;
       
   251 	if (profile->BearerType() != EAspBearerInternet)
       
   252 		{
       
   253 		mandatoryBearerType = EMandatoryIncorrectBearerType;
       
   254 		}
       
   255 		
       
   256 	TInt mandatoryAccessPoint = EMandatoryOk;
       
   257 	/*if (profile->AccessPointL() == CAspAccessPointHandler::KAskAlways)
       
   258 		{
       
   259 		mandatoryAccessPoint = EMandatoryIncorrectAccessPoint;
       
   260 		}*/
       
   261 	
       
   262 	CleanupStack::PopAndDestroy(list);
       
   263 	CleanupStack::PopAndDestroy(profile);
       
   264 	
       
   265 	if (mandatoryBearerType != EMandatoryOk)
       
   266 		{
       
   267 		return mandatoryBearerType;
       
   268 		}
       
   269 
       
   270 	if (mandatoryAccessPoint != EMandatoryOk)
       
   271 		{
       
   272 		return mandatoryAccessPoint;
       
   273 		}
       
   274 
       
   275 	if (mandatoryConnectionData != EMandatoryOk)
       
   276 		{
       
   277 		return mandatoryConnectionData;
       
   278 		}
       
   279 		
       
   280 	return EMandatoryOk;//mandatoryContentData;
       
   281     }
       
   282 
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CAspAutoSyncHandler::SynchronizeL
       
   286 // 
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CAspAutoSyncHandler::SynchronizeL(RFile& aTaskFile)
       
   290 	{
       
   291 	
       
   292 	FLOG( _L("CAspAutoSyncHandler::SynchronizeL START") );
       
   293 
       
   294 	CFileStore*         store;
       
   295     RStoreReadStream    instream;
       
   296     // Get tasks from scheduler's store
       
   297     store = CDirectFileStore::FromLC(aTaskFile);
       
   298     instream.OpenLC(*store,store->Root());
       
   299     TInt count = instream.ReadInt32L();
       
   300 
       
   301 	CScheduledTask* task = CScheduledTask::NewLC(instream);
       
   302     HBufC* taskData = const_cast<HBufC*>(&(task->Data()));
       
   303     TPtr ptr = taskData->Des();
       
   304     TBool doCleanUp = ETrue;
       
   305 
       
   306 	if (ptr.Compare(KPeakTaskData) == 0)
       
   307 		{
       
   308 	 FLOG( _L("Peak Sync Scheduled") );
       
   309 		TInt interval = iSchedule->SyncPeakSchedule();
       
   310 		if (iSchedule->IntervalType(interval) == EHourly
       
   311 					&& interval != CAspSchedule::EIntervalManual)
       
   312 			{
       
   313 			if (iSchedule->IsValidPeakScheduleL() != 0)
       
   314 				{
       
   315 				//postpone peak schedule until next peak start
       
   316 				iSchedule->UpdatePeakScheduleL();
       
   317 				CleanupStack::PopAndDestroy(task);
       
   318     			CleanupStack::PopAndDestroy( &instream ); 
       
   319 				CleanupStack::PopAndDestroy( store ); 
       
   320 				doCleanUp = EFalse;
       
   321 				return;
       
   322 				}
       
   323 			else
       
   324 				{
       
   325 				//schedule next sync
       
   326 				if(!iSchedule->IsValidNextPeakScheduleL())
       
   327 					{
       
   328 					iSchedule->UpdatePeakScheduleL();
       
   329 					}
       
   330 				else 
       
   331 					{
       
   332 					CAspSyncSchedule* syncSchedule = CAspSyncSchedule::NewLC();
       
   333 					syncSchedule->EditPeakScheduleL(iSchedule);
       
   334 					CleanupStack::PopAndDestroy(syncSchedule);
       
   335 					}
       
   336 				}
       
   337 			}
       
   338 		else
       
   339 			{
       
   340 			//schedule next sync
       
   341 			CAspSyncSchedule* syncSchedule = CAspSyncSchedule::NewLC();
       
   342 			syncSchedule->EditPeakScheduleL(iSchedule);
       
   343 			CleanupStack::PopAndDestroy(syncSchedule);
       
   344 			}
       
   345 		}
       
   346 			
       
   347 	if (ptr.Compare(KOffPeakTaskData) == 0)
       
   348 		{
       
   349 		FLOG( _L("Off-Peak Sync Scheduled") );
       
   350 		TInt interval = iSchedule->SyncOffPeakSchedule();
       
   351 		if (iSchedule->IntervalType(interval) == EHourly)
       
   352 			{
       
   353 			if (iSchedule->IsValidOffPeakScheduleL() != 0)
       
   354 				{
       
   355 				//postpone off-schedule until next off-peak start
       
   356 				iSchedule->UpdateOffPeakScheduleL();
       
   357 				CleanupStack::PopAndDestroy(task);
       
   358 				CleanupStack::PopAndDestroy( &instream ); 
       
   359 				CleanupStack::PopAndDestroy( store ); 
       
   360 				doCleanUp = EFalse;
       
   361 				return;
       
   362 				}
       
   363 			else
       
   364 				{
       
   365 				//schedule next sync
       
   366 				if(!iSchedule->IsValidNextOffPeakScheduleL())
       
   367 					{
       
   368 					iSchedule->UpdateOffPeakScheduleL();
       
   369 					}
       
   370 				else
       
   371 					{
       
   372 					CAspSyncSchedule* syncSchedule = CAspSyncSchedule::NewLC();
       
   373 					syncSchedule->EditOffPeakScheduleL(iSchedule);
       
   374 					CleanupStack::PopAndDestroy(syncSchedule);
       
   375 					}
       
   376 				}
       
   377 		
       
   378 			}
       
   379 		}
       
   380 
       
   381 	if(doCleanUp)	
       
   382 		{
       
   383 			CleanupStack::PopAndDestroy(task);
       
   384 			CleanupStack::PopAndDestroy( &instream ); 
       
   385 			CleanupStack::PopAndDestroy( store ); 
       
   386 			doCleanUp = EFalse;
       
   387 		}
       
   388 	
       
   389 	iSchedule->SetError(KErrNone);
       
   390 	iSchedule->SaveL();
       
   391 	
       
   392 	TRAPD (err, DoSynchronizeL());
       
   393 	User::LeaveIfError(err);
       
   394 
       
   395 }
       
   396 
       
   397 // -----------------------------------------------------------------------------
       
   398 // CAspAutoSyncHandler::DoSynchronizeL
       
   399 // 
       
   400 // -----------------------------------------------------------------------------
       
   401 //
       
   402 void CAspAutoSyncHandler::DoSynchronizeL()
       
   403 {
       
   404 	
       
   405 	OpenSyncSessionL();
       
   406 	
       
   407 	if (CAspProfile::OtherSyncRunning(&iSyncSession))
       
   408 		{
       
   409 		FLOG( _L("### other sync running ###") );
       
   410 		TInt currentJobId = CAspProfile::CurrentJob(&iSyncSession);
       
   411 		if (currentJobId != KErrNotFound)
       
   412 			{
       
   413 			iSyncJob.OpenL(iSyncSession, currentJobId);
       
   414 			if (iSchedule->ProfileId() != iSyncJob.Profile())
       
   415 				{
       
   416 				FLOG( _L("CAspAutoSyncHandler::SynchronizeL Set Error") );
       
   417 				iSchedule->SetError(EOtherSyncRunning);
       
   418 				iSchedule->SaveL();
       
   419 				}
       
   420 			iSyncJob.Close();
       
   421 			}
       
   422 		FLOG( _L("CAspAutoSyncHandler::SynchronizeL END") );
       
   423 		return;
       
   424 		}
       
   425 	
       
   426     TInt profileId = iSchedule->AutoSyncProfileId();
       
   427 	iSchedule->UpdateProfileSettingsL();
       
   428 	iSchedule->SaveL();
       
   429 
       
   430 	
       
   431 #ifdef _DEBUG
       
   432 	iSchedule->LogScheduleL();
       
   433 #endif
       
   434 	
       
   435 	if (!iSchedule->CanSynchronizeL())
       
   436 		{
       
   437 		FLOG( _L("### incorrect auto sync settings ###") );
       
   438 		FLOG( _L("CAspAutoSyncHandler::SynchronizeL END") );
       
   439 	    //iSchedule->SetError(EIncorrectSchedule);
       
   440 		//iSchedule->SaveL();
       
   441 		return;
       
   442 		}
       
   443 
       
   444 #if 0 //roaming check not supported		
       
   445 	if (!iSchedule->RoamingAllowed())
       
   446 		{
       
   447 		if (IsRoaming())
       
   448 			{
       
   449 		    FLOG( _L("### cannot sync when roaming ###") );
       
   450 		    FLOG( _L("CAspAutoSyncHandler::SynchronizeL END") );
       
   451 		    return;
       
   452 			}
       
   453 		}
       
   454 #endif
       
   455 
       
   456 	
       
   457 		
       
   458 	if (CheckMandatoryDataL(profileId) != EMandatoryOk)
       
   459 		{
       
   460 		FLOG( _L("### incorrect sync profile settings ###") );
       
   461 		FLOG( _L("CAspAutoSyncHandler::SynchronizeL END") );
       
   462 		//iSchedule->SetError(EIncorrectProfile);
       
   463 		//iSchedule->SaveL();
       
   464 		return;
       
   465 		}
       
   466 
       
   467 
       
   468     iSyncSession.RequestEventL(*this);     // for MSyncMLEventObserver events
       
   469     iSyncSession.RequestProgressL(*this);  // for MSyncMLProgressObserver events
       
   470     
       
   471 
       
   472     iSyncJob.CreateL(iSyncSession, profileId);  // start sync
       
   473     
       
   474     iSyncRunning = ETrue;
       
   475     
       
   476     iWait->Start();  // stop here until sync has completed
       
   477     
       
   478    	iSyncRunning = EFalse;
       
   479 
       
   480 	iSchedule->UpdateServerIdL();
       
   481    	
       
   482    	if (iSyncError != KErrNone)
       
   483 		{
       
   484 		iSchedule->SetError(iSyncError);
       
   485 		iSchedule->SaveL();
       
   486      	FLOG(_L("### auto sync failed (%d) ###"), iSyncError);
       
   487 		}
       
   488 	
       
   489     FLOG( _L("CAspAutoSyncHandler::SynchronizeL END") );
       
   490 
       
   491 }
       
   492 
       
   493 
       
   494 // -----------------------------------------------------------------------------
       
   495 // CAspAutoSyncHandler::IsRoamingL
       
   496 // 
       
   497 // -----------------------------------------------------------------------------
       
   498 //
       
   499 TBool CAspAutoSyncHandler::IsRoamingL()
       
   500 	{
       
   501 	RConnectionMonitor conMon;
       
   502 	
       
   503 	conMon.ConnectL();
       
   504 
       
   505     TRequestStatus status;
       
   506     TInt registrationStatus(0);
       
   507 
       
   508     //check network status
       
   509     conMon.GetIntAttribute(EBearerIdGSM, 0, KNetworkRegistration, 
       
   510                             registrationStatus, status);
       
   511 
       
   512     User::WaitForRequest(status);
       
   513     conMon.Close();
       
   514     
       
   515     if (registrationStatus == ENetworkRegistrationRoaming)
       
   516     	{
       
   517     	return ETrue;	
       
   518     	}
       
   519     	
       
   520     return EFalse;
       
   521     }
       
   522 
       
   523 
       
   524 // -----------------------------------------------------------------------------
       
   525 // CAspAutoSyncHandler::IsRoaming
       
   526 // 
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 TBool CAspAutoSyncHandler::IsRoaming()
       
   530 	{
       
   531 	TBool ret = EFalse;
       
   532 	TRAPD(err, ret = IsRoamingL());
       
   533 	
       
   534 	if (err != KErrNone)
       
   535 		{
       
   536 		ret = EFalse; // default is not roaming	
       
   537 		}
       
   538 		
       
   539 	return ret;
       
   540     }
       
   541 
       
   542 
       
   543 
       
   544 /*******************************************************************************
       
   545  * class CAspSyncSchedule
       
   546  *******************************************************************************/
       
   547 
       
   548 
       
   549 // -----------------------------------------------------------------------------
       
   550 // CAspSyncSchedule::NewL
       
   551 //
       
   552 // -----------------------------------------------------------------------------
       
   553 //
       
   554 CAspSyncSchedule* CAspSyncSchedule::NewL()
       
   555     {
       
   556     CAspSyncSchedule* self = new (ELeave) CAspSyncSchedule();
       
   557     CleanupStack::PushL(self);
       
   558     self->ConstructL();
       
   559     CleanupStack::Pop(self);
       
   560 
       
   561     return(self);
       
   562     }
       
   563 
       
   564 
       
   565 // -----------------------------------------------------------------------------
       
   566 // CAspSyncSchedule::NewLC
       
   567 //
       
   568 // -----------------------------------------------------------------------------
       
   569 //
       
   570 CAspSyncSchedule* CAspSyncSchedule::NewLC()
       
   571     {
       
   572     CAspSyncSchedule* self = new (ELeave) CAspSyncSchedule();
       
   573     CleanupStack::PushL(self);
       
   574     self->ConstructL();
       
   575 
       
   576     return(self);
       
   577     }
       
   578 
       
   579 
       
   580 // -----------------------------------------------------------------------------
       
   581 // CAspSyncSchedule::CAspSyncSchedule
       
   582 // 
       
   583 // -----------------------------------------------------------------------------
       
   584 //
       
   585 CAspSyncSchedule::CAspSyncSchedule()
       
   586 	{
       
   587     }
       
   588 
       
   589 
       
   590 // -----------------------------------------------------------------------------
       
   591 // CAspSyncSchedule::ConstructL
       
   592 //
       
   593 // -----------------------------------------------------------------------------
       
   594 //
       
   595 void CAspSyncSchedule::ConstructL()
       
   596     {
       
   597         
       
   598     _LIT(KScheduleHandler, "z:\\sys\\bin\\aspschedulehandler.exe");
       
   599     const TInt KDefaultSchedulePriority = 1;
       
   600     
       
   601     TInt err = iScheduler.Connect();
       
   602 	User::LeaveIfError(err);
       
   603 	
       
   604     TFileName name(KScheduleHandler);
       
   605     err = iScheduler.Register(name, KDefaultSchedulePriority);
       
   606     User::LeaveIfError(err);
       
   607     
       
   608    	ipeakEntryList = new (ELeave) CArrayFixFlat<TScheduleEntryInfo2>(1);
       
   609 	ioffPeakEntryList = new (ELeave) CArrayFixFlat<TScheduleEntryInfo2>(1);
       
   610    	ipeakTaskList  = new (ELeave) CArrayFixFlat<TTaskInfo>(1);
       
   611 	ioffPeakTaskList  = new (ELeave) CArrayFixFlat<TTaskInfo>(1);
       
   612     } 
       
   613 
       
   614 
       
   615 // ----------------------------------------------------------------------------
       
   616 // Destructor
       
   617 //
       
   618 // ----------------------------------------------------------------------------
       
   619 //
       
   620 CAspSyncSchedule::~CAspSyncSchedule()
       
   621     {
       
   622     delete ipeakEntryList;
       
   623 	delete ioffPeakEntryList;
       
   624     delete ipeakTaskList;
       
   625     delete ioffPeakTaskList;
       
   626 	
       
   627     iScheduler.Close();
       
   628     }
       
   629 
       
   630 // ----------------------------------------------------------------------------
       
   631 // CAspSyncSchedule::DoDeleteScheduleL
       
   632 // - Delete schedules if exist
       
   633 // ----------------------------------------------------------------------------
       
   634 //
       
   635 void CAspSyncSchedule::DoDeleteScheduleL(CAspSchedule* aAspSchedule)
       
   636 	{
       
   637 	ipeakEntryList->Reset();
       
   638 	ioffPeakEntryList->Reset();
       
   639 	ipeakTaskList->Reset();
       
   640     ioffPeakTaskList->Reset();
       
   641 	
       
   642     TScheduleState2 state;
       
   643     TTsTime time;
       
   644     
       
   645     TInt peakHandle = aAspSchedule->PeakScheduleHandle();
       
   646 	TInt offPeakHandle = aAspSchedule->OffPeakScheduleHandle();
       
   647 	
       
   648     if (peakHandle == KErrNotFound && offPeakHandle == KErrNotFound)
       
   649     	{
       
   650     	return; // nothing to delete
       
   651     	}
       
   652     if (peakHandle != KErrNotFound)
       
   653     	{
       
   654     	
       
   655     	TInt err = iScheduler.GetScheduleL(peakHandle, state,
       
   656                                   *ipeakEntryList, *ipeakTaskList, time);
       
   657 
       
   658     	TInt count = ipeakTaskList->Count();
       
   659 
       
   660 		for (TInt i=0; i<count; i++)
       
   661     		{
       
   662     		TTaskInfo& task = (*ipeakTaskList)[i];
       
   663     		err = iScheduler.DeleteTask(task.iTaskId);
       
   664     		User::LeaveIfError(err);
       
   665     		}
       
   666     	
       
   667     	err = iScheduler.DeleteSchedule(peakHandle);
       
   668 		aAspSchedule->SetPeakScheduleHandle(KErrNotFound);
       
   669 		aAspSchedule->SetDailySyncEnabled(EFalse);
       
   670 		aAspSchedule->SetPeakSyncEnabled(EFalse);
       
   671 		
       
   672     	User::LeaveIfError(err);
       
   673     	}
       
   674 
       
   675 	if (offPeakHandle != KErrNotFound)
       
   676     	{
       
   677     	
       
   678     	TInt err = iScheduler.GetScheduleL(offPeakHandle, state,
       
   679                                   *ioffPeakEntryList, *ioffPeakTaskList, time);
       
   680 
       
   681     	TInt count = ioffPeakTaskList->Count();
       
   682 
       
   683 		for (TInt i=0; i<count; i++)
       
   684     		{
       
   685     		TTaskInfo& task = (*ioffPeakTaskList)[i];
       
   686     		err = iScheduler.DeleteTask(task.iTaskId);
       
   687     		User::LeaveIfError(err);
       
   688     		}
       
   689     	
       
   690     	err = iScheduler.DeleteSchedule(offPeakHandle);
       
   691 		aAspSchedule->SetOffPeakScheduleHandle(KErrNotFound);
       
   692 		aAspSchedule->SetOffPeakSyncEnabled(EFalse);
       
   693     	User::LeaveIfError(err);
       
   694     	}
       
   695 	}
       
   696 
       
   697 
       
   698 // ----------------------------------------------------------------------------
       
   699 // CAspSyncSchedule::DeleteScheduleL
       
   700 //
       
   701 // ----------------------------------------------------------------------------
       
   702 //
       
   703 void CAspSyncSchedule::DeleteScheduleL(CAspSchedule* aAspSchedule)
       
   704 	{
       
   705 
       
   706 	TRAPD(err, DoDeleteScheduleL(aAspSchedule));
       
   707 	
       
   708 	if (err == KErrNone)
       
   709 		{
       
   710 		FLOG( _L("CAspSyncSchedule::DeleteScheduleL ok"));
       
   711 		}
       
   712 	else
       
   713 		{
       
   714 		FLOG( _L("CAspSyncSchedule::DeleteSchedule failed ,err: %d"), err );
       
   715 		}
       
   716 		
       
   717 	if (err != KErrNone && err != KErrNotFound)
       
   718 		{
       
   719 		User::Leave(err);
       
   720 		}
       
   721 	}
       
   722 
       
   723 
       
   724 // ----------------------------------------------------------------------------
       
   725 // CAspSyncSchedule::CreatePeakScheduleL
       
   726 //
       
   727 // ----------------------------------------------------------------------------
       
   728 //
       
   729 void CAspSyncSchedule::CreatePeakScheduleL(CAspSchedule* aAspSchedule)
       
   730 	{
       
   731 	
       
   732 	const TInt KRepeatForever = -1;
       
   733 	const TInt KTaskPriority = 2;
       
   734 		
       
   735 	CAspSchedule* schedule = aAspSchedule;
       
   736 	schedule->SetPeakScheduleHandle(KErrNotFound);
       
   737 	
       
   738 	TTsTime startTime;
       
   739 	TIntervalType intervalType;
       
   740 	TInt interval;
       
   741 	TTime now;
       
   742 	now.HomeTime();
       
   743 	TInt peakschedule;
       
   744     TInt peakStatus = schedule->IsValidPeakScheduleL();
       
   745 
       
   746 	TDateTime startday;
       
   747 	TInt syncFrequency = schedule->SyncFrequency();
       
   748 
       
   749 	//Daily schedule can be done using peak schedule
       
   750 	if (IntervalType(syncFrequency) == EDaily)
       
   751 		{
       
   752 		   	startday = now.DateTime();
       
   753 			TTime start = schedule->AutoSyncScheduleTime();
       
   754 			TDateTime date = start.DateTime();
       
   755 			TDateTime time(startday.Year(), startday.Month(), startday.Day(), date.Hour(), date.Minute(), 0, 0);
       
   756 			TTime syncTime(time);
       
   757 			startTime.SetLocalTime(syncTime);
       
   758 		}
       
   759 	else
       
   760 		{
       
   761 		TInt dayCount = 0;
       
   762 	    TInt dayIndex = 0;
       
   763         schedule->SelectedDayInfo(dayCount, dayIndex); 
       
   764 		if (dayCount == 0)
       
   765 			{
       
   766 			//no need to schedule if peak days are not selected
       
   767 			schedule->SetPeakSyncEnabled(EFalse);
       
   768 			return;
       
   769 			}
       
   770 
       
   771 		if(schedule->WeekdayEnabled(now.DayNoInWeek()))
       
   772 			{
       
   773 			startday = now.DateTime();
       
   774 			//If the current time is peak, start now
       
   775 			if (peakStatus == 0)
       
   776 				{
       
   777 				startTime.SetLocalTime(now);
       
   778 				}
       
   779 			//current time is before peak
       
   780 			else if (peakStatus > 0)
       
   781 				{
       
   782 				TTime start = schedule->StartPeakTime();
       
   783 				TDateTime date = start.DateTime();
       
   784 				TDateTime time(startday.Year(), startday.Month(), startday.Day(), date.Hour(), date.Minute(), 0, 0);
       
   785 				TTime syncTime(time);
       
   786 				startTime.SetLocalTime(syncTime);
       
   787 				}
       
   788 			//current time is after peak.
       
   789 			else if (peakStatus < 0)
       
   790 				{
       
   791 				TTime start = schedule->StartPeakTime();
       
   792 				TDateTime date = start.DateTime();
       
   793 				now = now + (TTimeIntervalDays)1;
       
   794 				while(!schedule->WeekdayEnabled(now.DayNoInWeek()))
       
   795 		 			{
       
   796 					now = now + (TTimeIntervalDays)1;
       
   797 		 			}
       
   798 				startday = now.DateTime();
       
   799 				TDateTime time(startday.Year(), startday.Month(), startday.Day(), date.Hour(), date.Minute(), 0, 0);
       
   800 				TTime syncTime(time);
       
   801 				startTime.SetLocalTime(syncTime);
       
   802 				}
       
   803 			}
       
   804 			
       
   805 		else
       
   806 			{
       
   807 			//find the next day selected for sync
       
   808 		 	while(!schedule->WeekdayEnabled(now.DayNoInWeek()))
       
   809 		 		{
       
   810 				now = now + (TTimeIntervalDays)1;
       
   811 		 		}
       
   812 		 
       
   813 		 	startday = now.DateTime();
       
   814 		 	TTime start = schedule->StartPeakTime();
       
   815 			TDateTime date = start.DateTime();
       
   816 			TDateTime time(startday.Year(), startday.Month(), startday.Day(), date.Hour(), date.Minute(), 0, 0);
       
   817 			TTime syncTime(time);
       
   818 			startTime.SetLocalTime(syncTime);
       
   819 		 	}
       
   820 
       
   821 		}
       
   822 	
       
   823 	TInt validityPeriod = KMaxTInt;
       
   824 	if (schedule->DailySyncEnabled())
       
   825 		{
       
   826 		peakschedule = schedule->SyncFrequency();
       
   827 		}
       
   828 	else
       
   829 		{
       
   830 	 	peakschedule = schedule->SyncPeakSchedule();
       
   831 		}
       
   832     intervalType = IntervalType(peakschedule);
       
   833 	interval = Interval(peakschedule);
       
   834 
       
   835 	TScheduleEntryInfo2 peakEntry(startTime, intervalType, interval, (TTimeIntervalMinutes)validityPeriod);
       
   836 	
       
   837 	
       
   838 	#ifdef _DEBUG
       
   839 	TBuf<64> buf;
       
   840 	LogTsTime(startTime, buf);
       
   841 	FLOG(_L("Auto sync start time: %S"), &buf);
       
   842 	#endif
       
   843     
       
   844 	
       
   845 	ipeakEntryList->Reset();
       
   846 	ipeakEntryList->AppendL(peakEntry);
       
   847 	
       
   848 	TSchedulerItemRef ref;
       
   849 	TInt err = iScheduler.CreatePersistentSchedule(ref, *ipeakEntryList);
       
   850 	User::LeaveIfError(err);
       
   851 	
       
   852 	//keep the scheduler disabled until settings are saved
       
   853 	iScheduler.DisableSchedule(ref.iHandle);
       
   854 	
       
   855 	schedule->SetPeakScheduleHandle(ref.iHandle);
       
   856 	
       
   857 	HBufC* hBuf = HBufC::NewMaxLC(KBufSize);
       
   858 	hBuf->Des() = KPeakTaskData;
       
   859 	
       
   860 	TTaskInfo taskInfo;
       
   861 	taskInfo.iName = KPeakTaskData;
       
   862 	taskInfo.iRepeat = KRepeatForever;
       
   863 	taskInfo.iPriority = KTaskPriority;
       
   864 
       
   865     err = iScheduler.ScheduleTask(taskInfo, *hBuf, ref.iHandle);
       
   866     User::LeaveIfError(err);
       
   867 	
       
   868 	CleanupStack::PopAndDestroy(hBuf);
       
   869 	}
       
   870 
       
   871 
       
   872 // ----------------------------------------------------------------------------
       
   873 // CAspSyncSchedule::CreateOffPeakScheduleL
       
   874 // 
       
   875 // ----------------------------------------------------------------------------
       
   876 //
       
   877 void CAspSyncSchedule::CreateOffPeakScheduleL(CAspSchedule* aAspSchedule)
       
   878 	{
       
   879 	
       
   880 	const TInt KRepeatForever = -1;
       
   881 	const TInt KTaskPriority = 1;
       
   882 		
       
   883 	CAspSchedule* schedule = aAspSchedule;
       
   884 	schedule->SetOffPeakScheduleHandle(KErrNotFound);
       
   885 	
       
   886 	TTime opStart= schedule->EndPeakTime()+ (TTimeIntervalMinutes)1;
       
   887 	TDateTime opStartDate = opStart.DateTime();
       
   888 	TTime pStart = schedule->StartPeakTime();
       
   889 	TDateTime pStartDate = pStart.DateTime();
       
   890 	
       
   891 	//Dont create off-peak schedule if the duration is zero
       
   892 	if (opStartDate.Hour() == pStartDate.Hour()
       
   893 						&& opStartDate.Minute() == pStartDate.Minute())
       
   894 		{
       
   895 		schedule->SetOffPeakSyncEnabled(EFalse);
       
   896 		return;
       
   897 		}
       
   898 	
       
   899 	TTsTime startTime;
       
   900 	TIntervalType intervalType;
       
   901 	TInt interval;
       
   902 
       
   903 	TTime now;
       
   904 	now.HomeTime();
       
   905 	
       
   906 	TInt offpeakschedule = schedule->SyncOffPeakSchedule();
       
   907 	intervalType = IntervalType(offpeakschedule);
       
   908 
       
   909 	TInt offPeakStatus = schedule->IsValidOffPeakScheduleL();
       
   910 	TDateTime startday;
       
   911 	
       
   912 	startday = now.DateTime();
       
   913 	//If the current time is off peak, start now
       
   914 	if (offPeakStatus == 0)
       
   915 		{
       
   916 		startTime.SetLocalTime(now);
       
   917 		}
       
   918 	//current time is peak
       
   919 	else if (offPeakStatus > 0)
       
   920 		{
       
   921 		TTime start= schedule->EndPeakTime()+ (TTimeIntervalMinutes)1;
       
   922 		TDateTime date = start.DateTime();
       
   923 		TDateTime time(startday.Year(), startday.Month(), startday.Day(), date.Hour(), date.Minute(), 0, 0);
       
   924 		TTime syncTime(time);
       
   925 		startTime.SetLocalTime(syncTime);
       
   926 		}
       
   927 	
       
   928 	TInt validityPeriod = KMaxTInt;
       
   929 	
       
   930 	interval = Interval(offpeakschedule);
       
   931 	
       
   932 	TScheduleEntryInfo2 offPeakEntry(startTime, intervalType, interval, (TTimeIntervalMinutes)validityPeriod);
       
   933 	
       
   934 	#ifdef _DEBUG
       
   935 	TBuf<64> buf;
       
   936 	LogTsTime(startTime, buf);
       
   937 	FLOG(_L("Auto sync start time: %S"), &buf);
       
   938 	#endif
       
   939     
       
   940 	ioffPeakEntryList->Reset();
       
   941 	ioffPeakEntryList->AppendL(offPeakEntry);
       
   942 	
       
   943 	TSchedulerItemRef ref;
       
   944 	TInt err = iScheduler.CreatePersistentSchedule(ref, *ioffPeakEntryList);
       
   945 	User::LeaveIfError(err);
       
   946 
       
   947 	//keep the scheduler disabled until settings are saved
       
   948 	iScheduler.DisableSchedule(ref.iHandle);
       
   949 	
       
   950 	schedule->SetOffPeakScheduleHandle(ref.iHandle);
       
   951 	
       
   952 	HBufC* hBuf = HBufC::NewMaxLC(KBufSize);
       
   953 	hBuf->Des() = KOffPeakTaskData;
       
   954 	
       
   955 	TTaskInfo taskInfo;
       
   956 	taskInfo.iName = KOffPeakTaskData;
       
   957 	taskInfo.iRepeat = KRepeatForever;
       
   958 	taskInfo.iPriority = KTaskPriority;
       
   959 
       
   960     err = iScheduler.ScheduleTask(taskInfo, *hBuf, ref.iHandle);
       
   961     User::LeaveIfError(err);
       
   962 	
       
   963 	CleanupStack::PopAndDestroy(hBuf);
       
   964 	}
       
   965 
       
   966 // -----------------------------------------------------------------------------
       
   967 // CAspSyncSchedule::EnableSchedule()
       
   968 // -Enable schedules after settings are saved.
       
   969 // -----------------------------------------------------------------------------
       
   970 //
       
   971 void CAspSyncSchedule::EnableSchedule(CAspSchedule* aAspSchedule)
       
   972 	{
       
   973 	TInt peakHandle = aAspSchedule->PeakScheduleHandle();
       
   974 	TInt offPeakHandle = aAspSchedule->OffPeakScheduleHandle();
       
   975 
       
   976 	if (peakHandle != KErrNotFound)
       
   977 		{
       
   978 		iScheduler.EnableSchedule(peakHandle);
       
   979 		}
       
   980 	if (offPeakHandle != KErrNotFound)
       
   981 		{
       
   982 		iScheduler.EnableSchedule(offPeakHandle);
       
   983 		}
       
   984 
       
   985 	}
       
   986 
       
   987 // -----------------------------------------------------------------------------
       
   988 // CAspSyncSchedule::EditPeakScheduleL
       
   989 // -Edit peak schedule entry
       
   990 // -----------------------------------------------------------------------------
       
   991 //
       
   992 
       
   993 void CAspSyncSchedule::EditPeakScheduleL(CAspSchedule* aAspSchedule)
       
   994 	{
       
   995 
       
   996 	ipeakEntryList->Reset();
       
   997 	ipeakTaskList->Reset();
       
   998 
       
   999 	TTsTime tsTime;
       
  1000 	TScheduleState2 state;
       
  1001 	TInt peakHandle = aAspSchedule->PeakScheduleHandle();
       
  1002 		
       
  1003 	if (peakHandle == KErrNotFound)
       
  1004 		{
       
  1005 		User::Leave(KErrNotFound);	
       
  1006 		}
       
  1007 	
       
  1008 	TInt err = iScheduler.GetScheduleL(peakHandle, state, *ipeakEntryList, *ipeakTaskList, tsTime);
       
  1009 	
       
  1010 	User::LeaveIfError(err);
       
  1011 	
       
  1012 	TInt count = ipeakEntryList->Count();
       
  1013 	__ASSERT_DEBUG(count == 1, TUtil::Panic(KErrArgument));
       
  1014 
       
  1015 	TInt peakInterval;
       
  1016 	if (aAspSchedule->DailySyncEnabled())
       
  1017 		{
       
  1018 		peakInterval = aAspSchedule->SyncFrequency();
       
  1019 		}
       
  1020 	else
       
  1021 		{
       
  1022 	 	peakInterval = aAspSchedule->SyncPeakSchedule();
       
  1023 		}
       
  1024 	TInt interval = Interval(peakInterval);
       
  1025 	TIntervalType intervalType = IntervalType(peakInterval);
       
  1026 	TTime now;
       
  1027 	now.HomeTime();
       
  1028 	TTsTime startTime ;
       
  1029 	if (intervalType == EHourly)
       
  1030 		{
       
  1031 		if (peakInterval == CAspSchedule::EInterval15Mins
       
  1032 				       		|| peakInterval == CAspSchedule::EInterval30Mins)
       
  1033 			{
       
  1034 			now = now + (TTimeIntervalMinutes)interval ;
       
  1035 			}
       
  1036 		else
       
  1037 			{
       
  1038 			now = now + (TTimeIntervalHours)interval ;
       
  1039 			}
       
  1040 		startTime.SetLocalTime(now);
       
  1041 		}
       
  1042 	else
       
  1043 		{
       
  1044 		now = now + (TTimeIntervalDays)interval;
       
  1045 		TDateTime startday = now.DateTime();
       
  1046 		TTime start = aAspSchedule->AutoSyncScheduleTime();
       
  1047 		TDateTime date = start.DateTime();
       
  1048 		TDateTime time(startday.Year(), startday.Month(), startday.Day(), date.Hour(), date.Minute(), 0, 0);
       
  1049 		TTime syncTime(time);
       
  1050 		startTime.SetLocalTime(syncTime);
       
  1051 		}
       
  1052 	
       
  1053 	TScheduleEntryInfo2& info = (*ipeakEntryList)[0];
       
  1054 
       
  1055 	info.SetStartTime(startTime);
       
  1056 	info.SetInterval(interval);
       
  1057 	info.SetIntervalType(intervalType);
       
  1058 	
       
  1059 	err = iScheduler.EditSchedule(peakHandle, *ipeakEntryList);
       
  1060 	User::LeaveIfError(err);
       
  1061 	
       
  1062 	}
       
  1063 
       
  1064 
       
  1065 // -----------------------------------------------------------------------------
       
  1066 // CAspSyncSchedule::EditOffPeakScheduleL
       
  1067 // -Edit off-peak schedule entry
       
  1068 // -----------------------------------------------------------------------------
       
  1069 //
       
  1070 void CAspSyncSchedule::EditOffPeakScheduleL(CAspSchedule* aAspSchedule)
       
  1071 	{
       
  1072 
       
  1073 	ioffPeakEntryList->Reset();
       
  1074 	ioffPeakTaskList->Reset();
       
  1075 
       
  1076 	TTsTime tsTime;
       
  1077 	TScheduleState2 state;
       
  1078 	TInt offPeakHandle = aAspSchedule->OffPeakScheduleHandle();
       
  1079 		
       
  1080 	if (offPeakHandle == KErrNotFound)
       
  1081 		{
       
  1082 		User::Leave(KErrNotFound);	
       
  1083 		}
       
  1084 	
       
  1085 	TInt err = iScheduler.GetScheduleL(offPeakHandle, state, *ioffPeakEntryList, *ioffPeakTaskList, tsTime);
       
  1086 	
       
  1087 	User::LeaveIfError(err);
       
  1088 	
       
  1089 	TInt count = ioffPeakEntryList->Count();
       
  1090 	__ASSERT_DEBUG(count == 1, TUtil::Panic(KErrArgument));
       
  1091 	
       
  1092 	TInt offPeakInterval = aAspSchedule->SyncOffPeakSchedule();
       
  1093 	TInt interval = Interval(offPeakInterval);
       
  1094 	TIntervalType intervalType = IntervalType(offPeakInterval);
       
  1095 	TTime now;
       
  1096 	now.HomeTime();
       
  1097 	TTsTime startTime ;
       
  1098 	
       
  1099 	if (offPeakInterval == CAspSchedule::EInterval15Mins
       
  1100 				       		|| offPeakInterval == CAspSchedule::EInterval30Mins)
       
  1101 		{
       
  1102 		now = now + (TTimeIntervalMinutes)interval ;
       
  1103 		}
       
  1104 	else
       
  1105 		{
       
  1106 		now = now + (TTimeIntervalHours)interval ;
       
  1107 		}
       
  1108 	startTime.SetLocalTime(now);
       
  1109 	
       
  1110 	
       
  1111 	TScheduleEntryInfo2& info = (*ioffPeakEntryList)[0];
       
  1112 
       
  1113 	info.SetStartTime(startTime);
       
  1114 	info.SetInterval(interval);
       
  1115 	info.SetIntervalType(intervalType);
       
  1116 	
       
  1117 	err = iScheduler.EditSchedule(offPeakHandle, *ioffPeakEntryList);
       
  1118 	User::LeaveIfError(err);
       
  1119 	
       
  1120 	}
       
  1121 
       
  1122 // -----------------------------------------------------------------------------
       
  1123 // CAspSyncSchedule::UpdatePeakScheduleL
       
  1124 // -Postpone peak schedule till next peak start
       
  1125 // -----------------------------------------------------------------------------
       
  1126 //
       
  1127 void CAspSyncSchedule::UpdatePeakScheduleL(CAspSchedule* aAspSchedule)
       
  1128 	{
       
  1129 
       
  1130 	ipeakEntryList->Reset();
       
  1131 	ipeakTaskList->Reset();
       
  1132 
       
  1133 	TTime startPeak = aAspSchedule->StartPeakTime();
       
  1134 		
       
  1135 	TTime now;
       
  1136 	now.HomeTime();
       
  1137 
       
  1138 		
       
  1139 	TDateTime startDate = startPeak.DateTime();
       
  1140 	
       
  1141 	TDateTime day = now.DateTime();
       
  1142 	if (day.Hour() > KStartPeakHour)
       
  1143 		{
       
  1144 		now = now + (TTimeIntervalDays)1;
       
  1145 		}
       
  1146 	TDateTime tomorrow = now.DateTime();
       
  1147 	
       
  1148     TDateTime time(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), startDate.Hour(), startDate.Minute(), 0, 0);
       
  1149 	TTime nextPeakStart(time);
       
  1150 	
       
  1151 	while(!aAspSchedule->WeekdayEnabled(nextPeakStart.DayNoInWeek()))
       
  1152 		{
       
  1153 		nextPeakStart = nextPeakStart + (TTimeIntervalDays)1;
       
  1154 		}
       
  1155 	TTsTime tsTime;
       
  1156 	TScheduleState2 state;
       
  1157 	TInt peakHandle = aAspSchedule->PeakScheduleHandle();
       
  1158 		
       
  1159 	if (peakHandle == KErrNotFound)
       
  1160 		{
       
  1161 		User::Leave(KErrNotFound);	
       
  1162 		}
       
  1163 	
       
  1164 	TInt err = iScheduler.GetScheduleL(peakHandle, state, *ipeakEntryList, *ipeakTaskList, tsTime);
       
  1165 	
       
  1166 	User::LeaveIfError(err);
       
  1167 	
       
  1168 	TInt count = ipeakEntryList->Count();
       
  1169 	__ASSERT_DEBUG(count == 1, TUtil::Panic(KErrArgument));
       
  1170 
       
  1171 	
       
  1172 	TTsTime startTime ;
       
  1173 	startTime.SetLocalTime(nextPeakStart);
       
  1174 	TIntervalType intervalType = IntervalType(aAspSchedule->SyncPeakSchedule());
       
  1175 	TInt interval = Interval(aAspSchedule->SyncPeakSchedule());	
       
  1176 	
       
  1177 	TScheduleEntryInfo2& info = (*ipeakEntryList)[0];
       
  1178 
       
  1179 	info.SetStartTime(startTime);
       
  1180 	info.SetInterval(interval);
       
  1181 	info.SetIntervalType(intervalType);
       
  1182 	
       
  1183 	err = iScheduler.EditSchedule(peakHandle, *ipeakEntryList);
       
  1184 	User::LeaveIfError(err);
       
  1185 	
       
  1186 	
       
  1187 	}
       
  1188 
       
  1189 // -----------------------------------------------------------------------------
       
  1190 // CAspSyncSchedule::UpdateOffPeakScheduleL
       
  1191 // -Postpone off-peak schedule till next off-peak start
       
  1192 // -----------------------------------------------------------------------------
       
  1193 //
       
  1194 
       
  1195 void CAspSyncSchedule::UpdateOffPeakScheduleL(CAspSchedule* aAspSchedule)
       
  1196 	{
       
  1197 
       
  1198 	ioffPeakEntryList->Reset();
       
  1199 	ioffPeakTaskList->Reset();
       
  1200 
       
  1201 	TTime startoffPeak = aAspSchedule->EndPeakTime() + (TTimeIntervalMinutes)1;
       
  1202 		
       
  1203 	TTime now;
       
  1204 	now.HomeTime();
       
  1205 
       
  1206 	TInt interval = aAspSchedule->SyncOffPeakSchedule();
       
  1207 	TIntervalType intervalType = IntervalType(interval);
       
  1208 	TInt intervalVal = Interval(interval);
       
  1209 
       
  1210 	if (interval == CAspSchedule::EInterval15Mins
       
  1211 				|| interval == CAspSchedule::EInterval30Mins)
       
  1212 
       
  1213 		{
       
  1214 		now = now + (TTimeIntervalMinutes)intervalVal;
       
  1215 		}
       
  1216 	else if (intervalType == EHourly)
       
  1217 		{
       
  1218 		now = now + (TTimeIntervalHours)intervalVal;
       
  1219 		}
       
  1220 	
       
  1221 	TDateTime startday = startoffPeak.DateTime();
       
  1222 	TDateTime today = now.DateTime();
       
  1223 	
       
  1224     TDateTime time(today.Year(), today.Month(), today.Day(), startday.Hour(), startday.Minute(), 0, 0);
       
  1225 	
       
  1226 	TTime nextOffPeakStart(time);
       
  1227 	TTsTime tsTime;
       
  1228 	TScheduleState2 state;
       
  1229 	TInt offpeakHandle = aAspSchedule->OffPeakScheduleHandle();
       
  1230 		
       
  1231 	if (offpeakHandle == KErrNotFound)
       
  1232 		{
       
  1233 		User::Leave(KErrNotFound);	
       
  1234 		}
       
  1235 	
       
  1236 	TInt err = iScheduler.GetScheduleL(offpeakHandle, state, *ioffPeakEntryList, *ioffPeakTaskList, tsTime);
       
  1237 	
       
  1238 	User::LeaveIfError(err);
       
  1239 	
       
  1240 	TInt count = ioffPeakEntryList->Count();
       
  1241 	__ASSERT_DEBUG(count == 1, TUtil::Panic(KErrArgument));
       
  1242 
       
  1243 	
       
  1244 	TTsTime startTime ;
       
  1245 	startTime.SetLocalTime(nextOffPeakStart);
       
  1246 	TScheduleEntryInfo2& info = (*ioffPeakEntryList)[0];
       
  1247 
       
  1248 	info.SetStartTime(startTime);
       
  1249 	info.SetInterval(interval);
       
  1250 	info.SetIntervalType(intervalType);
       
  1251 	
       
  1252 	err = iScheduler.EditSchedule(offpeakHandle, *ioffPeakEntryList);
       
  1253 	User::LeaveIfError(err);
       
  1254 	
       
  1255 	
       
  1256 	}
       
  1257 // ----------------------------------------------------------------------------
       
  1258 // CAspSyncSchedule::IntervalType
       
  1259 // Return  interval type
       
  1260 // ----------------------------------------------------------------------------
       
  1261 //
       
  1262 TIntervalType CAspSyncSchedule::IntervalType(TInt aInterval)
       
  1263 	{
       
  1264 	if (aInterval == CAspSchedule::EInterval24hours
       
  1265 			  || aInterval == CAspSchedule::EInterval2days
       
  1266 			  || aInterval == CAspSchedule::EInterval4days
       
  1267 			  || aInterval == CAspSchedule::EInterval7days
       
  1268 			  || aInterval == CAspSchedule::EInterval14days
       
  1269 			  || aInterval == CAspSchedule::EInterval30days)
       
  1270 		{
       
  1271 		return EDaily;
       
  1272 		}
       
  1273 		
       
  1274 	return EHourly;
       
  1275 	}
       
  1276 	
       
  1277 	
       
  1278 // ----------------------------------------------------------------------------
       
  1279 // CAspSyncSchedule::Interval
       
  1280 // Return  interval value
       
  1281 // ----------------------------------------------------------------------------
       
  1282 //
       
  1283 TInt CAspSyncSchedule::Interval(TInt aInterval)
       
  1284 	{
       
  1285 	const TInt KIntervalManual  = 0;
       
  1286 	const TInt KInterval15Mins  = 15;
       
  1287 	const TInt KInterval30Mins  = 30;
       
  1288 	const TInt KInterval1hour   = 1;
       
  1289 	const TInt KInterval2hours  = 2;
       
  1290 	const TInt KInterval4hours  = 4;
       
  1291 	const TInt KInterval8hours  = 8;
       
  1292 	const TInt KInterval12hours = 12;		
       
  1293 	const TInt KInterval24hours = 1;
       
  1294 	const TInt KInterval2days   = 2;
       
  1295 	const TInt KInterval4days   = 4;
       
  1296 	const TInt KInterval7ays    = 7;
       
  1297 	const TInt KInterval14days  = 14;
       
  1298 	const TInt KInterval30days  = 30;
       
  1299 	
       
  1300 	    TInt ret = KIntervalManual;
       
  1301     
       
  1302     switch (aInterval)
       
  1303     	{
       
  1304     	case CAspSchedule::EIntervalManual:
       
  1305     	    ret = KIntervalManual;
       
  1306     	    break;
       
  1307     	case CAspSchedule::EInterval15Mins:
       
  1308     	    ret = KInterval15Mins;
       
  1309     	    break;
       
  1310     	case CAspSchedule::EInterval30Mins:
       
  1311     	    ret = KInterval30Mins;
       
  1312     	    break;
       
  1313     	case CAspSchedule::EInterval1hour:
       
  1314     	    ret = KInterval1hour;
       
  1315     	    break;
       
  1316     	case CAspSchedule::EInterval2hours:
       
  1317     	    ret = KInterval2hours;
       
  1318     	    break;
       
  1319     	case CAspSchedule::EInterval4hours:
       
  1320     	    ret = KInterval4hours;
       
  1321     	    break;
       
  1322 		case CAspSchedule::EInterval8hours:
       
  1323     	    ret = KInterval8hours;
       
  1324     	    break;
       
  1325 		case CAspSchedule::EInterval12hours:
       
  1326     	    ret = KInterval12hours;
       
  1327     	    break;
       
  1328 		case CAspSchedule::EInterval24hours:
       
  1329     	    ret = KInterval24hours;
       
  1330     	    break;
       
  1331 		case CAspSchedule::EInterval2days:
       
  1332     	    ret = KInterval2days;
       
  1333     	    break;
       
  1334 		case CAspSchedule::EInterval4days:
       
  1335     	    ret = KInterval4days;
       
  1336     	    break;
       
  1337 		case CAspSchedule::EInterval7days:
       
  1338     	    ret = KInterval7ays;
       
  1339     	    break;
       
  1340 		case CAspSchedule::EInterval14days:
       
  1341     	    ret = KInterval14days;
       
  1342     	    break;
       
  1343 		case CAspSchedule::EInterval30days:	
       
  1344     	    ret = KInterval30days;
       
  1345     	    break;
       
  1346 
       
  1347 			
       
  1348         default:
       
  1349             break;
       
  1350     	}
       
  1351     	
       
  1352     return ret;	
       
  1353 	}
       
  1354 
       
  1355 
       
  1356 // ----------------------------------------------------------------------------
       
  1357 // CAspSyncSchedule::StartTime
       
  1358 //
       
  1359 // ----------------------------------------------------------------------------
       
  1360 //
       
  1361 TTsTime CAspSyncSchedule::StartTime(TInt aStartHour, TInt aInterval)
       
  1362 	{
       
  1363 
       
  1364 	if (IntervalType(aInterval) == EDaily)
       
  1365 		{
       
  1366 	    TTime time = CAspSchedule::LocalStartTime(aStartHour);
       
  1367 	    TTsTime tsTime(time, EFalse);
       
  1368         return tsTime;
       
  1369 		}
       
  1370 	
       
  1371 	TTime time = CAspSchedule::UniversalStartTime(aStartHour);
       
  1372     TTsTime tsTime(time, ETrue);
       
  1373     return tsTime;
       
  1374     }
       
  1375 
       
  1376 
       
  1377 // -----------------------------------------------------------------------------
       
  1378 // CAspSyncSchedule::GetPeakStartTimeL
       
  1379 //
       
  1380 // -----------------------------------------------------------------------------
       
  1381 //
       
  1382 void CAspSyncSchedule::GetPeakStartTimeL(CAspSchedule* aAspSchedule, TTime& aTime, TBool aHomeTime)
       
  1383 	{
       
  1384 
       
  1385 	ipeakEntryList->Reset();
       
  1386 	ipeakTaskList->Reset();
       
  1387 	
       
  1388 	TTsTime tsTime;
       
  1389 	TScheduleState2 state;
       
  1390 	TInt peakHandle = aAspSchedule->PeakScheduleHandle();
       
  1391 	
       
  1392 	TInt err = iScheduler.GetScheduleL(peakHandle, state, *ipeakEntryList, *ipeakTaskList, tsTime);
       
  1393 	User::LeaveIfError(err);
       
  1394 	if (aHomeTime)
       
  1395 		{
       
  1396 		aTime = tsTime.GetLocalTime();	
       
  1397 		}
       
  1398 	else
       
  1399 		{
       
  1400 	    aTime = tsTime.GetUtcTime();		
       
  1401 		}
       
  1402 
       
  1403 	}
       
  1404 
       
  1405 
       
  1406 // -----------------------------------------------------------------------------
       
  1407 // CAspSyncSchedule::GetOffPeakStartTimeL
       
  1408 //
       
  1409 // -----------------------------------------------------------------------------
       
  1410 //
       
  1411 void CAspSyncSchedule::GetOffPeakStartTimeL(CAspSchedule* aAspSchedule, TTime& aTime, TBool aHomeTime)
       
  1412 	{
       
  1413 
       
  1414 	ioffPeakEntryList->Reset();
       
  1415 	ioffPeakTaskList->Reset();
       
  1416 	
       
  1417 	TTsTime tsTime;
       
  1418 	TScheduleState2 state;
       
  1419 	TInt offPeakHandle = aAspSchedule->OffPeakScheduleHandle();
       
  1420 	
       
  1421 	TInt err = iScheduler.GetScheduleL(offPeakHandle, state, *ioffPeakEntryList, *ioffPeakTaskList, tsTime);
       
  1422 	User::LeaveIfError(err);
       
  1423 	if (aHomeTime)
       
  1424 		{
       
  1425 		aTime = tsTime.GetLocalTime();	
       
  1426 		}
       
  1427 	else
       
  1428 		{
       
  1429 	    aTime = tsTime.GetUtcTime();		
       
  1430 		}
       
  1431 
       
  1432 	}
       
  1433 
       
  1434 
       
  1435 #ifdef _DEBUG
       
  1436 
       
  1437 // ----------------------------------------------------------------------------
       
  1438 // CAspSyncSchedule::LogScheduleL
       
  1439 //
       
  1440 // ----------------------------------------------------------------------------
       
  1441 //
       
  1442 void CAspSyncSchedule::LogScheduleL(CAspSchedule* aAspSchedule)
       
  1443 	{
       
  1444 
       
  1445 	FLOG( _L("---- scheduler settings ----") );
       
  1446 
       
  1447 	TTsTime tsTime;
       
  1448 	TTime time;
       
  1449 	TScheduleState2 state;
       
  1450 	TInt peakhandle = aAspSchedule->PeakScheduleHandle();
       
  1451 	if (peakhandle != KErrNotFound )
       
  1452 		{
       
  1453 		FLOG( _L("---- Peak Time Sync Enabled -> Settings") );
       
  1454 		ipeakEntryList->Reset();
       
  1455 		ipeakTaskList->Reset();
       
  1456 		TInt err = iScheduler.GetScheduleL(peakhandle, state, *ipeakEntryList, *ipeakTaskList, tsTime);
       
  1457 		User::LeaveIfError(err);
       
  1458 	
       
  1459 		TScheduleEntryInfo2 info = (*ipeakEntryList)[0];
       
  1460 	
       
  1461 		TBuf<KBufSize> buf;
       
  1462 	
       
  1463 		FLOG(_L("peak schedule handle: %d"), peakhandle);
       
  1464 	
       
  1465 		LogInterval(info, buf);
       
  1466 		FLOG(_L("peak interval: %S"), &buf);
       
  1467 
       
  1468 		LogIntervalType(info, buf);
       
  1469 		FLOG(_L("peak interval type: %S"), &buf);
       
  1470 	
       
  1471 		LogTsTime(tsTime, buf);
       
  1472 		FLOG(_L("peak sync start time: %S"), &buf);
       
  1473 			
       
  1474 		TTaskInfo taskInfo = (*ipeakTaskList)[0];
       
  1475 		LogTaskInfo(taskInfo, buf);
       
  1476 		FLOG(_L("peak task info: %S"), &buf);
       
  1477 
       
  1478 		}
       
  1479 	TInt offPeakhandle = aAspSchedule->OffPeakScheduleHandle();
       
  1480 	if (offPeakhandle != KErrNotFound )
       
  1481 		{
       
  1482 		FLOG( _L("---- Off-Peak Time Sync Enabled -> Settings") );
       
  1483 		ioffPeakEntryList->Reset();
       
  1484 	    ioffPeakTaskList->Reset();
       
  1485 		TInt err = iScheduler.GetScheduleL(offPeakhandle, state, *ioffPeakEntryList, *ioffPeakTaskList, tsTime);
       
  1486 		User::LeaveIfError(err);
       
  1487 	
       
  1488 		TScheduleEntryInfo2 info = (*ioffPeakEntryList)[0];
       
  1489 	
       
  1490 		TBuf<KBufSize> buf;
       
  1491 	
       
  1492 		FLOG(_L("off-peak schedule handle: %d"), peakhandle);
       
  1493 	
       
  1494 		LogInterval(info, buf);
       
  1495 		FLOG(_L("off-peak interval: %S"), &buf);
       
  1496 
       
  1497 		LogIntervalType(info, buf);
       
  1498 		FLOG(_L("off-peak interval type: %S"), &buf);
       
  1499 	
       
  1500 		LogTsTime(tsTime, buf);
       
  1501 		FLOG(_L("off-peak sync start time: %S"), &buf);
       
  1502 			
       
  1503 		TTaskInfo taskInfo = (*ioffPeakTaskList)[0];
       
  1504 		LogTaskInfo(taskInfo, buf);
       
  1505 		FLOG(_L("off-peak task info: %S"), &buf);
       
  1506 
       
  1507 		}
       
  1508 		
       
  1509 	
       
  1510 		
       
  1511 	
       
  1512 	FLOG( _L("---- scheduler settings ----") );
       
  1513 
       
  1514     }
       
  1515 
       
  1516 
       
  1517 // ----------------------------------------------------------------------------
       
  1518 // CAspSyncSchedule::LogSchedule
       
  1519 //
       
  1520 // ----------------------------------------------------------------------------
       
  1521 //
       
  1522 TInt CAspSyncSchedule::LogSchedule(CAspSchedule* aAspSchedule)
       
  1523 	{
       
  1524 	TInt err = KErrNone;
       
  1525 	TRAP(err, LogScheduleL(aAspSchedule));
       
  1526 	
       
  1527 	return err;
       
  1528     }
       
  1529 
       
  1530 
       
  1531 // ----------------------------------------------------------------------------
       
  1532 // CAspSyncSchedule::LogTaskInfo
       
  1533 //
       
  1534 // ----------------------------------------------------------------------------
       
  1535 //
       
  1536 void CAspSyncSchedule::LogTaskInfo(TTaskInfo& aInfo, TDes& aText)
       
  1537 	{
       
  1538 	aText.Format(_L("name: %S, repeat: %d, id: %d, priority: %d"), 
       
  1539 	             &aInfo.iName, aInfo.iRepeat, aInfo.iTaskId, aInfo.iPriority);
       
  1540     }
       
  1541 
       
  1542 
       
  1543 // ----------------------------------------------------------------------------
       
  1544 // CAspSyncSchedule::LogIntervalType
       
  1545 //
       
  1546 // ----------------------------------------------------------------------------
       
  1547 //
       
  1548 void CAspSyncSchedule::LogIntervalType(TScheduleEntryInfo2& aInfo, TDes& aText)
       
  1549 	{
       
  1550 	aText = _L("unknown");
       
  1551 	
       
  1552 	TIntervalType type = aInfo.IntervalType();
       
  1553 	
       
  1554 	if (type == EDaily)
       
  1555 		{
       
  1556 		aText = _L("EDaily");
       
  1557 		}
       
  1558 	if (type == EHourly)
       
  1559 		{
       
  1560 		aText = _L("EHourly");
       
  1561 		}
       
  1562     }
       
  1563 
       
  1564 
       
  1565 // ----------------------------------------------------------------------------
       
  1566 // CAspSyncSchedule::LogInterval
       
  1567 //
       
  1568 // ----------------------------------------------------------------------------
       
  1569 //
       
  1570 void CAspSyncSchedule::LogInterval(TScheduleEntryInfo2& aInfo, TDes& aText)
       
  1571 	{
       
  1572 	TInt num = aInfo.Interval();
       
  1573 	aText.Format(_L("interval: %d"), num);
       
  1574     }
       
  1575 
       
  1576 
       
  1577 // ----------------------------------------------------------------------------
       
  1578 // CAspSyncSchedule::LogTime
       
  1579 //
       
  1580 // ----------------------------------------------------------------------------
       
  1581 //
       
  1582 
       
  1583 void CAspSyncSchedule::LogTsTime(TTsTime aTsTime, TDes& aText)
       
  1584 	{
       
  1585 	TBuf<64> buf1; TBuf<64> buf2;
       
  1586 
       
  1587 	TTime time = aTsTime.GetLocalTime();
       
  1588 	TRAP_IGNORE(TUtil::GetDateTimeTextL(buf1, time));
       
  1589 	time = aTsTime.GetUtcTime();
       
  1590 	TRAP_IGNORE(TUtil::GetDateTimeTextL(buf2, time));
       
  1591 	
       
  1592 	aText.Format(_L("local: %S  utc: %S"), &buf1, &buf2);
       
  1593     }
       
  1594 
       
  1595 #endif //  _DEBUG
       
  1596 
       
  1597