omads/omadsappui/AspSyncUtil/src/AspProfile.cpp
changeset 0 dab8a81a92de
child 1 95fdac6ccb5c
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 // INCLUDES
       
    21 
       
    22 #include <SyncMLTransportProperties.h>
       
    23 #include <featmgr.h>   // FeatureManager
       
    24 
       
    25 #include "AspProfile.h"
       
    26 #include "AspResHandler.h"
       
    27 #include "AspDialogUtil.h"
       
    28 #include "AspDebug.h"
       
    29 #include "AspSchedule.h"
       
    30 #include <centralrepository.h> //CRepository
       
    31 
       
    32 
       
    33 
       
    34 /*******************************************************************************
       
    35  * class TAspProviderItem
       
    36  *******************************************************************************/
       
    37 
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // TAspProviderItem::CompareItems
       
    41 //
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 TInt TAspProviderItem::CompareItems(const TAspProviderItem& aFirst, const TAspProviderItem& aSecond)
       
    45 	{
       
    46 	const TInt KNegative = -1;
       
    47 	const TInt KPositive = 1;
       
    48 	
       
    49 	TInt firstOrder = SortOrder(aFirst.iDataProviderId);
       
    50 	TInt secondOrder = SortOrder(aSecond.iDataProviderId);
       
    51 	
       
    52 	if (firstOrder > secondOrder)
       
    53 		{
       
    54 		return KNegative;
       
    55 		}
       
    56 	else if (secondOrder > firstOrder)
       
    57 		{
       
    58 		return KPositive;
       
    59 		}
       
    60 	else
       
    61 		{
       
    62 		return 0;
       
    63 		}
       
    64 	}
       
    65 
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // TAspProviderItem::CompareItems
       
    69 //
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 TInt TAspProviderItem::SortOrder(TInt aDataProviderId)
       
    73 	{
       
    74 	TInt ret = EOrderOther;
       
    75 	if (aDataProviderId == KUidNSmlAdapterContact.iUid)
       
    76 		{
       
    77 		ret = EOrderContact;
       
    78 		}
       
    79 	else if (aDataProviderId == KUidNSmlAdapterCalendar.iUid)
       
    80 		{
       
    81 		ret = EOrderCalendar;
       
    82 		}
       
    83 	else if (aDataProviderId == KUidNSmlAdapterNote.iUid)
       
    84 		{
       
    85 		ret = EOrderNotes;
       
    86 		}
       
    87 	else if (aDataProviderId == KUidNSmlAdapterEMail.iUid)
       
    88 		{
       
    89 		ret = EOrderEmail;
       
    90 		}
       
    91 	return ret;
       
    92 	}
       
    93 	
       
    94 	
       
    95 
       
    96 /*******************************************************************************
       
    97  * class CAspContentList
       
    98  *******************************************************************************/
       
    99 
       
   100 
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CAspContentList::NewLC
       
   104 //
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 CAspContentList* CAspContentList::NewLC(const TAspParam& aParam)
       
   108     {
       
   109    	FLOG( _L("CAspContentList::NewLC START") );
       
   110    	
       
   111     CAspContentList* self = new (ELeave) CAspContentList(aParam);
       
   112 	CleanupStack::PushL(self);
       
   113 	self->ConstructL();
       
   114 
       
   115     FLOG( _L("CAspContentList::NewLC END") );
       
   116 	return self;
       
   117     }
       
   118 
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CAspContentList::NewL
       
   122 //
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 CAspContentList* CAspContentList::NewL(const TAspParam& aParam)
       
   126     {
       
   127    	FLOG( _L("CAspContentList::NewL START") );
       
   128    	
       
   129     CAspContentList* self = new (ELeave) CAspContentList(aParam);
       
   130 	CleanupStack::PushL(self);
       
   131 	self->ConstructL();
       
   132 	CleanupStack::Pop(self);
       
   133 
       
   134     FLOG( _L("CAspContentList::NewL END") );
       
   135 	return self;
       
   136     }
       
   137 
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // Destructor
       
   141 //
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 CAspContentList::~CAspContentList()
       
   145     {
       
   146 	iProviderList.Close();
       
   147 	iTaskList.Close();
       
   148     }
       
   149 
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CAspContentList::ConstructL
       
   153 //
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CAspContentList::ConstructL(void)
       
   157     {
       
   158 	if (iListMode == EInitDataProviders)
       
   159 		{
       
   160         InitDataProvidersL();
       
   161 		}
       
   162 	else if (iListMode == EInitTasks)
       
   163 		{
       
   164 		__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
   165 		
       
   166         InitAllTasksL();
       
   167 		}
       
   168 	else 
       
   169 		{
       
   170 		__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
   171 		
       
   172 		InitDataProvidersL();
       
   173 		InitAllTasksL();
       
   174 		}
       
   175     }
       
   176 
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CAspContentList::CAspContentList
       
   180 //
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 CAspContentList::CAspContentList(const TAspParam& aParam)
       
   184 	{
       
   185 	iSyncSession = aParam.iSyncSession;
       
   186 	iProfile = aParam.iProfile;
       
   187 	iListMode = aParam.iMode;
       
   188 	iApplicationId = aParam.iApplicationId;
       
   189 
       
   190 	__ASSERT_ALWAYS(iSyncSession, TUtil::Panic(KErrGeneral));
       
   191 	}
       
   192 
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CAspContentList::SetProfile
       
   196 //
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 void CAspContentList::SetProfile(CAspProfile* aProfile)
       
   200 	{
       
   201 	if (aProfile)
       
   202 		{
       
   203 
       
   204 	    __ASSERT_ALWAYS(iProfile == NULL, TUtil::Panic(KErrGeneral));
       
   205 
       
   206 		}
       
   207 
       
   208 	iProfile = aProfile;
       
   209 	}
       
   210 
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CAspContentList::IsLocked
       
   214 //
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 TBool CAspContentList::IsLocked()
       
   218 	{
       
   219 	if (iProfile)
       
   220 		{
       
   221 		return ETrue;
       
   222 		}
       
   223 		
       
   224 	return EFalse;
       
   225 	}
       
   226 
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CAspContentList::ProviderCount
       
   230 //
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 TInt CAspContentList::ProviderCount()
       
   234 	{
       
   235 	return iProviderList.Count();
       
   236 	}
       
   237 
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CAspContentList::ProviderItem
       
   241 // 
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 TAspProviderItem& CAspContentList::ProviderItem(TInt aIndex)
       
   245 	{
       
   246 	if (aIndex < 0 || aIndex >= ProviderCount())
       
   247 	    {
       
   248 		TUtil::Panic(KErrArgument);
       
   249 	    }
       
   250 
       
   251 	return iProviderList[aIndex];
       
   252 	}
       
   253 
       
   254 
       
   255 // -----------------------------------------------------------------------------
       
   256 // CAspContentList::FindProviderIndex
       
   257 //
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 TInt CAspContentList::FindProviderIndex(TInt aId)
       
   261 	{
       
   262 	TInt count = iProviderList.Count();
       
   263 	
       
   264 	for (TInt i=0; i<count; i++)
       
   265 		{
       
   266 		TAspProviderItem& item = iProviderList[i];
       
   267 		if (item.iDataProviderId == aId)
       
   268 			{
       
   269 			return i;
       
   270 			}
       
   271 		}
       
   272 		
       
   273 	return KErrNotFound;
       
   274 	}
       
   275 
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CAspContentList::FindProviderIndexForTask
       
   279 //
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 TInt CAspContentList::FindProviderIndexForTask(TInt aId)
       
   283 	{
       
   284 	TInt index = FindTaskIndex(aId);
       
   285 	if (index == KErrNotFound)
       
   286 		{
       
   287 		return KErrNotFound;
       
   288 		}
       
   289 	
       
   290 	TAspTaskItem& item = TaskItem(index);
       
   291 	
       
   292 	return FindProviderIndex(item.iDataProviderId);
       
   293 	}
       
   294 
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CAspContentList::TaskCount
       
   298 //
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 TInt CAspContentList::TaskCount()
       
   302 	{
       
   303 	return iTaskList.Count();
       
   304 	}
       
   305 
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CAspContentList::TaskItem
       
   309 // 
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 TAspTaskItem& CAspContentList::TaskItem(TInt aIndex)
       
   313 	{
       
   314 	if ((aIndex < 0) || (aIndex >= TaskCount()))
       
   315 	    {
       
   316 		TUtil::Panic(KErrArgument);
       
   317 	    }
       
   318 
       
   319 	return iTaskList[aIndex];
       
   320 	}
       
   321 
       
   322 
       
   323 // -----------------------------------------------------------------------------
       
   324 // CAspContentList::FindTaskIndex
       
   325 //
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 TInt CAspContentList::FindTaskIndex(TInt aId)
       
   329 	{
       
   330 	TInt count = iTaskList.Count();
       
   331 	
       
   332 	for (TInt i=0; i<count; i++)
       
   333 		{
       
   334 		TAspTaskItem& task = iTaskList[i];
       
   335 		if (task.iTaskId == aId)
       
   336 			{
       
   337 			return i;
       
   338 			}
       
   339 		}
       
   340 		
       
   341 	return KErrNotFound;
       
   342 	}
       
   343 
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CAspContentList::FindTaskIndexForProvider
       
   347 //
       
   348 // -----------------------------------------------------------------------------
       
   349 //
       
   350 TInt CAspContentList::FindTaskIndexForProvider(TInt aId)
       
   351 	{
       
   352 	TInt count = iTaskList.Count();
       
   353 	
       
   354 	for (TInt i=0; i<count; i++)
       
   355 		{
       
   356 		TAspTaskItem& item = iTaskList[i];
       
   357 		if (item.iDataProviderId == aId)
       
   358 			{
       
   359 			return i;
       
   360 			}
       
   361 		}
       
   362 		
       
   363 	return KErrNotFound;
       
   364 	}
       
   365 
       
   366 
       
   367 // -----------------------------------------------------------------------------
       
   368 // CAspContentList::FindTaskIdForProvider
       
   369 //
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 TInt CAspContentList::FindTaskIdForProvider(TInt aId)
       
   373 	{
       
   374 	TInt index = FindTaskIndexForProvider(aId);
       
   375 	if (index == KErrNotFound)
       
   376 		{
       
   377 		return KErrNotFound;
       
   378 		}
       
   379 		
       
   380 	TAspTaskItem& item = iTaskList[index];
       
   381 	return item.iTaskId;
       
   382 	}
       
   383 
       
   384 
       
   385 // -----------------------------------------------------------------------------
       
   386 // CAspContentList::DoInitDataProvidersL
       
   387 // 
       
   388 // Initialize data provider list.
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 void CAspContentList::DoInitDataProvidersL()
       
   392 	{
       
   393 	FLOG( _L("CAspContentList::DoInitDataProvidersL START") );
       
   394 	
       
   395 	iProviderList.Reset();
       
   396 	
       
   397 	RArray<TSmlDataProviderId> arr;
       
   398 	
       
   399 	Session().ListDataProvidersL(arr);
       
   400 	CleanupClosePushL(arr);
       
   401 
       
   402 	TInt count = arr.Count();
       
   403 	for (TInt i=0; i<count; i++)
       
   404 		{
       
   405 		RSyncMLDataProvider provider;
       
   406 		TInt id = arr[i];
       
   407 		
       
   408 		TRAPD(err, provider.OpenL(Session(), id));
       
   409 	
       
   410 		if (err == KErrNone)
       
   411 			{
       
   412     		CleanupClosePushL(provider);
       
   413 
       
   414 	    	TAspProviderItem item;
       
   415 		    ReadDataProviderItemL(provider, item);
       
   416             item.iIsIncludedInSync = EFalse;
       
   417 		    
       
   418 		    if (TUtil::IsEmpty(item.iDisplayName))
       
   419 		    	{
       
   420 		    	// providers must have display name
       
   421 		    	CleanupStack::PopAndDestroy(&provider);
       
   422 		    	FTRACE( RDebug::Print(_L("### provider has no display name (id=%x) ###"), item.iDataProviderId) );
       
   423 		    	continue;
       
   424 		    	}
       
   425 	
       
   426 		    User::LeaveIfError(iProviderList.Append(item));
       
   427 	        
       
   428 	        CleanupStack::PopAndDestroy(&provider);
       
   429 		    }
       
   430 		    
       
   431 		if (err != KErrNone)
       
   432 			{
       
   433 			FTRACE( RDebug::Print(_L("### RSyncMLDataProvider::Open failed (id=%x, err=%d) ###"), id, err) );
       
   434 			}
       
   435 		
       
   436 		}
       
   437 		
       
   438 	CleanupStack::PopAndDestroy(&arr);
       
   439 	
       
   440 	FLOG( _L("CAspContentList::DoInitDataProvidersL END") );
       
   441 	}
       
   442 
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // CAspContentList::UpdateDataProviderL
       
   446 // 
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CAspContentList::UpdateDataProviderL(TInt aDataProviderId)
       
   450 	{
       
   451 	TInt index = FindProviderIndex(aDataProviderId);
       
   452 	if (index == KErrNotFound)
       
   453 		{
       
   454 		return;  // unknown provider
       
   455 		}
       
   456 	TAspProviderItem& providerItem = ProviderItem(index);
       
   457 		
       
   458 	RArray<TSmlDataProviderId> arr;
       
   459 	Session().ListDataProvidersL(arr);
       
   460 	CleanupClosePushL(arr);
       
   461 
       
   462 	TInt count = arr.Count();
       
   463 	for (TInt i=0; i<count; i++)
       
   464 		{
       
   465 		RSyncMLDataProvider provider;
       
   466 		TInt id = arr[i];
       
   467 		
       
   468 		TRAPD(err, provider.OpenL(Session(), id));
       
   469 	
       
   470 		if (err == KErrNone)
       
   471 			{
       
   472     		CleanupClosePushL(provider);
       
   473 
       
   474 		    TInt id = provider.Identifier();
       
   475 		    if (id == aDataProviderId)
       
   476 		    	{
       
   477        		    ReadDataProviderItemL(provider, providerItem);
       
   478        		    CleanupStack::PopAndDestroy(&provider);
       
   479        		    break;
       
   480 		    	}
       
   481        
       
   482 	        CleanupStack::PopAndDestroy(&provider);
       
   483 		    }
       
   484 		}
       
   485 		
       
   486 	CleanupStack::PopAndDestroy(&arr);
       
   487 	}
       
   488 
       
   489 
       
   490 // -----------------------------------------------------------------------------
       
   491 // CAspContentList::ReadDataProviderItemL
       
   492 // 
       
   493 // -----------------------------------------------------------------------------
       
   494 //
       
   495 void CAspContentList::ReadDataProviderItemL(RSyncMLDataProvider& aProvider, 
       
   496                                             TAspProviderItem& aItem)
       
   497 	{
       
   498     aItem.iTaskId=KErrNotFound;
       
   499 	aItem.iDataProviderId = aProvider.Identifier();
       
   500 		    
       
   501     aItem.iSyncProtocol = ESmlVersion1_2;
       
   502     //item.iSyncProtocol = provider.RequiredProtocolVersionL();
       
   503 		    
       
   504 		    
       
   505     aItem.iAllowMultipleDataStores = aProvider.AllowsMultipleDataStores();
       
   506     TUtil::StrCopy(aItem.iDisplayName, aProvider.DisplayName());
       
   507 	
       
   508     TPtrC ptr = aProvider.DefaultDataStoreName();
       
   509     if (TUtil::IsEmpty(ptr))
       
   510     	{
       
   511     	aItem.iHasDefaultDataStore = EFalse;
       
   512     	aItem.iDefaultDataStore = KNullDesC;
       
   513     	}
       
   514     else
       
   515     	{
       
   516     	aItem.iHasDefaultDataStore = ETrue;
       
   517     	TUtil::StrCopy(aItem.iDefaultDataStore, ptr);
       
   518     	}
       
   519 	}
       
   520 
       
   521 
       
   522 // -----------------------------------------------------------------------------
       
   523 // CAspContentList::InitDataProvidersL
       
   524 // 
       
   525 // Initialize data provider list.
       
   526 // -----------------------------------------------------------------------------
       
   527 //
       
   528 void CAspContentList::InitDataProvidersL()
       
   529 	{
       
   530 
       
   531 #ifdef _DEBUG
       
   532    	TTime time_1 = TUtil::TimeBefore();
       
   533 #endif
       
   534 
       
   535     DoInitDataProvidersL();
       
   536     Sort();
       
   537     
       
   538 #ifdef _DEBUG
       
   539    	TUtil::TimeAfter(time_1, _L("CAspContentList::DoInitDataProvidersL took"));
       
   540 #endif
       
   541 
       
   542 
       
   543 #ifndef __SYNCML_DS_EMAIL   // KFeatureIdSyncMlDsEmail
       
   544     RemoveDataProvider(KUidNSmlAdapterEMail.iUid);
       
   545 #endif
       
   546 
       
   547 	}
       
   548 	
       
   549 	
       
   550 // -----------------------------------------------------------------------------
       
   551 // CAspContentList::RemoveDataProvider
       
   552 //
       
   553 // -----------------------------------------------------------------------------
       
   554 //
       
   555 void CAspContentList::RemoveDataProvider(TInt aDataProviderId)
       
   556 	{
       
   557     TInt index = FindProviderIndex(aDataProviderId);
       
   558         
       
   559     // remove data provider with id aDataProviderId
       
   560     if (index != KErrNotFound)
       
   561     	{
       
   562     	iProviderList.Remove(index);
       
   563     	}
       
   564 	}
       
   565 
       
   566 
       
   567 // -----------------------------------------------------------------------------
       
   568 // CAspContentList::InitAllTasksL
       
   569 //
       
   570 // Initialize task list.
       
   571 // -----------------------------------------------------------------------------
       
   572 //
       
   573 void CAspContentList::InitAllTasksL()
       
   574 	{
       
   575 	FLOG( _L("CAspContentList::InitAllTasksL START") );
       
   576 	
       
   577 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
   578 	
       
   579 	iTaskList.Reset();
       
   580 	
       
   581 	RArray<TSmlTaskId> arr;
       
   582     
       
   583 	iProfile->Profile().ListTasksL(arr); // IPC call
       
   584 	CleanupClosePushL(arr);
       
   585 	
       
   586 	TInt count = arr.Count();
       
   587 	for (TInt i=0; i<count; i++)
       
   588 		{
       
   589 		TAspTaskItem item = ReadTaskItemL(iProfile->Profile(), arr[i]);
       
   590 		
       
   591 		item.iLastSync = iProfile->LastSync(item.iTaskId);
       
   592 		
       
   593     	User::LeaveIfError(iTaskList.Append(item));
       
   594 		}
       
   595 		
       
   596 	
       
   597     CleanupStack::PopAndDestroy(&arr);
       
   598     
       
   599     FLOG( _L("CAspContentList::InitAllTasksL END") );
       
   600 	}
       
   601 
       
   602 // -----------------------------------------------------------------------------
       
   603 // CAspContentList::UpdateLocalDatabaseL()
       
   604 //
       
   605 // -----------------------------------------------------------------------------
       
   606 //
       
   607 void CAspContentList::UpdateLocalDatabaseL()
       
   608 {
       
   609 	InitDataProvidersL();
       
   610 	InitAllTasksL();
       
   611 	RSyncMLDataProvider provider;
       
   612 	TRAPD(err, provider.OpenL(Session(), KUidNSmlAdapterEMail.iUid));
       
   613 	
       
   614 	if (err == KErrNone)
       
   615 		{
       
   616     	CleanupClosePushL(provider);
       
   617     	TAspProviderItem item;
       
   618 		ReadDataProviderItemL(provider, item);
       
   619 		}
       
   620 	TInt index = FindTaskIdForProvider( KUidNSmlAdapterEMail.iUid);
       
   621 	if (index != KErrNotFound)
       
   622 		{
       
   623 		TAspTaskItem emailItem = ReadTaskItemL(iProfile->Profile(), index);
       
   624 
       
   625 		CDesCArray* databaseList = new (ELeave) CDesCArrayFlat(KDefaultArraySize);
       
   626 		CleanupStack::PushL(databaseList);
       
   627 		GetLocalDatabaseList(KUidNSmlAdapterEMail.iUid, databaseList);
       
   628 	
       
   629 		TInt count = databaseList->Count();
       
   630 		TBool taskUpdated = EFalse;
       
   631 		for (TInt i = 0; i < count ;i++)
       
   632 			{
       
   633 			
       
   634 			if ((*databaseList)[i].Compare(emailItem.iClientDataSource) == 0)
       
   635 				{
       
   636 				taskUpdated = ETrue;
       
   637 				break;
       
   638 				}
       
   639 			}
       
   640 		if (!taskUpdated)
       
   641 			{
       
   642 			TBuf<KBufSize> aLocalDatabase;
       
   643 			TBuf<KBufSize> aRemoteDatabase;
       
   644 			TInt aSyncDirection;
       
   645 			TBool aTaskEnabled;
       
   646 			ReadTaskL(KUidNSmlAdapterEMail.iUid, aLocalDatabase, aRemoteDatabase,
       
   647 		    	               aTaskEnabled, aSyncDirection);
       
   648 			
       
   649 			if(count == 0) 
       
   650 			{
       
   651 				if(aLocalDatabase != KNullDesC)
       
   652 				{
       
   653 					aLocalDatabase = KNullDesC;
       
   654 					aTaskEnabled = EFalse;
       
   655 					
       
   656 					CreateTaskL(KUidNSmlAdapterEMail.iUid,aLocalDatabase , aRemoteDatabase,
       
   657 	                              aTaskEnabled, aSyncDirection);
       
   658 	                				
       
   659 				}
       
   660 			}
       
   661 			else
       
   662 			{
       
   663 				aLocalDatabase = provider.DefaultDataStoreName();
       
   664 				CreateTaskL(KUidNSmlAdapterEMail.iUid,aLocalDatabase , aRemoteDatabase,
       
   665 	                              aTaskEnabled, aSyncDirection);
       
   666 			
       
   667 			}
       
   668 			
       
   669 			
       
   670 
       
   671 			}
       
   672 		
       
   673 		
       
   674 		CleanupStack::PopAndDestroy(databaseList);
       
   675 		}
       
   676 
       
   677 	if (err == KErrNone)
       
   678 	{
       
   679 	CleanupStack::PopAndDestroy(&provider);
       
   680 	}
       
   681 }
       
   682 
       
   683 // -----------------------------------------------------------------------------
       
   684 // CAspContentList::InitTaskL
       
   685 //
       
   686 // -----------------------------------------------------------------------------
       
   687 //
       
   688 void CAspContentList::InitTaskL(TInt aTaskId)
       
   689 	{
       
   690 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
   691 	
       
   692     TAspTaskItem task = ReadTaskItemL(iProfile->Profile(), aTaskId);
       
   693     
       
   694     // try to remove old task
       
   695     TInt index = FindTaskIndex(aTaskId);
       
   696     if (index != KErrNotFound)
       
   697     	{
       
   698     	iTaskList.Remove(index);
       
   699     	}
       
   700     
       
   701     task.iLastSync = iProfile->LastSync(aTaskId);
       
   702 	
       
   703 	// add new task
       
   704 	User::LeaveIfError(iTaskList.Append(task));
       
   705 	}
       
   706 
       
   707 
       
   708 // -----------------------------------------------------------------------------
       
   709 // CAspContentList::RemoveTask
       
   710 //
       
   711 // -----------------------------------------------------------------------------
       
   712 //
       
   713 void CAspContentList::RemoveTask(TInt aDataProviderId)
       
   714 	{
       
   715     TInt index = FindTaskIndexForProvider(aDataProviderId);
       
   716         
       
   717     // remove task that uses dataprovider aDataProviderId
       
   718     if (index != KErrNotFound)
       
   719     	{
       
   720     	iTaskList.Remove(index);
       
   721     	}
       
   722 	}
       
   723 
       
   724 
       
   725 // -----------------------------------------------------------------------------
       
   726 // CAspContentList::RemoveAllTasks
       
   727 //
       
   728 // -----------------------------------------------------------------------------
       
   729 //
       
   730 void CAspContentList::RemoveAllTasks()
       
   731 	{
       
   732    	iTaskList.Reset();
       
   733 	}
       
   734 
       
   735 
       
   736 // -----------------------------------------------------------------------------
       
   737 // CAspContentList::ReadTaskItemL
       
   738 //
       
   739 // Reads one task from sync profile.
       
   740 // -----------------------------------------------------------------------------
       
   741 //
       
   742 TAspTaskItem CAspContentList::ReadTaskItemL(RSyncMLDataSyncProfile& aProfile, TInt aTaskId)
       
   743 	{
       
   744 	RSyncMLTask task;
       
   745 	TAspTaskItem item;
       
   746 
       
   747 #ifdef _DEBUG	
       
   748 	TTime _time = TUtil::TimeBefore();
       
   749 #endif
       
   750 	
       
   751 	TRAPD(err, task.OpenL(aProfile, aTaskId));  // IPC call
       
   752 	if (err != KErrNone)
       
   753 		{
       
   754 		FLOG( _L("### RSyncMLTask::OpenL failed (id=%d, err=%d) ###"), aTaskId, err );
       
   755 	    User::Leave(err);
       
   756 		}
       
   757 
       
   758 #ifdef _DEBUG		
       
   759 	TUtil::TimeAfter(_time, _L("*RSyncMLTask::Open took"));
       
   760 #endif	
       
   761 		
       
   762    	item.iTaskId = aTaskId; 
       
   763 	TUtil::StrCopy(item.iDisplayName, task.DisplayName());
       
   764 	item.iSyncDirection = TAspTask::SyncDirection(task.DefaultSyncType());
       
   765 	
       
   766 	item.iDataProviderId = task.DataProvider();
       
   767 	item.iEnabled = task.Enabled();
       
   768 
       
   769 	item.iClientDataSource = task.ClientDataSource();
       
   770     item.iLocalDatabaseDefined = EFalse;
       
   771     item.iRemoteDatabaseDefined = EFalse;
       
   772 	if (!TUtil::IsEmpty(task.ClientDataSource()))
       
   773 		{
       
   774 		item.iLocalDatabaseDefined = ETrue;
       
   775 		}
       
   776 	if (!TUtil::IsEmpty(task.ServerDataSource()))
       
   777 		{
       
   778 		item.iRemoteDatabaseDefined = ETrue;
       
   779 		}
       
   780 	
       
   781 	task.Close();
       
   782 	
       
   783 	return item;
       
   784 	}
       
   785 
       
   786 
       
   787 // -----------------------------------------------------------------------------
       
   788 // CAspContentList::Session
       
   789 // 
       
   790 // -----------------------------------------------------------------------------
       
   791 //
       
   792 RSyncMLSession& CAspContentList::Session()
       
   793 	{
       
   794 	__ASSERT_DEBUG(iSyncSession, TUtil::Panic(KErrGeneral));
       
   795 	
       
   796 	return *iSyncSession;
       
   797 	}
       
   798 
       
   799 
       
   800 // -----------------------------------------------------------------------------
       
   801 // CAspContentList::CheckMandatoryDataL
       
   802 //
       
   803 // Function checks that all tasks have mandatory data.
       
   804 // -----------------------------------------------------------------------------
       
   805 //
       
   806 TInt CAspContentList::CheckMandatoryDataL(TInt& aContentCount)
       
   807 	{
       
   808 	aContentCount = 0;
       
   809 	TInt goodContentCount = 0;
       
   810 
       
   811 	TInt count = iTaskList.Count();
       
   812 
       
   813 	for (TInt i=0; i<count; i++)
       
   814 		{
       
   815 		TAspTaskItem& task = iTaskList[i];
       
   816 
       
   817 		if (!task.iEnabled)
       
   818 			{
       
   819 			continue; // task not part of sync - no need to check mandatory fields
       
   820 			}
       
   821 			
       
   822 		if (iApplicationId != KErrNotFound && iApplicationId != EApplicationIdSync)
       
   823 			{
       
   824 			TInt appId = TUtil::AppIdFromProviderId(task.iDataProviderId);
       
   825 			if (appId != iApplicationId)
       
   826 				{
       
   827 				continue; // task not part of sync - no need to check mandatory fields
       
   828 				}
       
   829 			}
       
   830 			
       
   831        	if (!task.iRemoteDatabaseDefined)
       
   832 			{
       
   833 			aContentCount = goodContentCount;
       
   834 		    return EMandatoryNoRemoteDatabase;
       
   835 			}
       
   836 
       
   837 		goodContentCount++;
       
   838 		}
       
   839 
       
   840     aContentCount = goodContentCount;
       
   841     
       
   842     if (goodContentCount > 0)
       
   843 		{
       
   844        	return EMandatoryOk;
       
   845 		}
       
   846 	else
       
   847 		{
       
   848 		return EMandatoryNoContent;
       
   849 		}
       
   850 	}
       
   851 
       
   852 
       
   853 // -----------------------------------------------------------------------------
       
   854 // CAspContentList::IncludeTasks
       
   855 //
       
   856 // -----------------------------------------------------------------------------
       
   857 //
       
   858 void CAspContentList::IncludeTasks(const CArrayFix<TInt>* aIncludedProviderList)
       
   859 	{
       
   860 	// update iIsIncludedInSync for all provider items (used in IncludeTaskL)
       
   861 	SetIncludedProviders(aIncludedProviderList);
       
   862 	
       
   863 	TInt count = iProviderList.Count();
       
   864 	
       
   865 	for (TInt i=0; i<count; i++)
       
   866 		{
       
   867 		TAspProviderItem& item = iProviderList[i];
       
   868 		TRAPD(err, IncludeTaskL(item));
       
   869 		
       
   870 		if (err != KErrNone)
       
   871 			{
       
   872 			FLOG( _L("### CAspContentList::IncludeTaskL failed (id=%x, err=%d) ###"), item.iDataProviderId, err );
       
   873 			}
       
   874 		}
       
   875 	}
       
   876 
       
   877 
       
   878 // -----------------------------------------------------------------------------
       
   879 // CAspContentList::SetIncludedProviders
       
   880 //
       
   881 // -----------------------------------------------------------------------------
       
   882 //
       
   883 void CAspContentList::SetIncludedProviders(const CArrayFix<TInt>* aIncludedProviderList)
       
   884 	{
       
   885 	// exclude all providers
       
   886 	TInt providerCount = iProviderList.Count();
       
   887 	for (TInt i=0; i<providerCount; i++)
       
   888 		{
       
   889         (iProviderList[i]).iIsIncludedInSync = EFalse;
       
   890 		}
       
   891 		
       
   892 	// include selected providers (aIncludedProviderList comes from ui)
       
   893 	TInt count = aIncludedProviderList->Count();
       
   894 	for (TInt i=0; i<count; i++)
       
   895 		{
       
   896         TInt index = (*aIncludedProviderList)[i];
       
   897         
       
   898         __ASSERT_ALWAYS(index>=0, TUtil::Panic(KErrGeneral));
       
   899         __ASSERT_ALWAYS(index<providerCount, TUtil::Panic(KErrGeneral));
       
   900         
       
   901         (iProviderList[index]).iIsIncludedInSync = ETrue;
       
   902 		}
       
   903     }
       
   904 
       
   905 
       
   906 // -----------------------------------------------------------------------------
       
   907 // CAspContentList::GetIncludedProviders
       
   908 //
       
   909 // -----------------------------------------------------------------------------
       
   910 //
       
   911 void CAspContentList::GetIncludedProviders(CArrayFix<TInt>* aIncludedProviderList)
       
   912 	{
       
   913 
       
   914 	// get list indexes of data providers with enabled task
       
   915 	
       
   916 	TInt count = iProviderList.Count();
       
   917 	
       
   918 	for (TInt i=0; i<count; i++)
       
   919 		{
       
   920         TAspProviderItem& item = iProviderList[i];
       
   921         TInt index = FindTaskIndexForProvider(item.iDataProviderId);
       
   922         if (index != KErrNotFound)
       
   923         	{
       
   924         	TAspTaskItem& item2 = iTaskList[index];
       
   925         	if (item2.iEnabled)
       
   926         		{
       
   927         		TRAP_IGNORE(aIncludedProviderList->AppendL(i));
       
   928         		}
       
   929         	}
       
   930 		}
       
   931     }
       
   932 
       
   933 
       
   934 // -----------------------------------------------------------------------------
       
   935 // CAspContentList::IncludeTaskL
       
   936 //
       
   937 // -----------------------------------------------------------------------------
       
   938 //
       
   939 void CAspContentList::IncludeTaskL(TAspProviderItem& aDataProvider)
       
   940 	{
       
   941 	if (!aDataProvider.iIsIncludedInSync)
       
   942 		{
       
   943 		// exclude task (if exists) from sync
       
   944 		ModifyTaskIncludedL(aDataProvider, EFalse, KNullDesC);
       
   945 		return;
       
   946 		}
       
   947 	
       
   948 	TInt index = FindTaskIndexForProvider(aDataProvider.iDataProviderId);
       
   949 	if (index == KErrNotFound)
       
   950 		{
       
   951 		// task does not exist - create new
       
   952 	    TInt id = CreateTaskL(aDataProvider);
       
   953 	    InitTaskL(id);
       
   954 		}
       
   955 	else
       
   956 	 	{
       
   957 	 	// include in sync
       
   958 		ModifyTaskIncludedL(aDataProvider, ETrue, KNullDesC);
       
   959 	 	}
       
   960 	}
       
   961 
       
   962 
       
   963 // -----------------------------------------------------------------------------
       
   964 // CAspContentList::ModifyTaskIncludedL
       
   965 //
       
   966 // -----------------------------------------------------------------------------
       
   967 //
       
   968 void CAspContentList::ModifyTaskIncludedL(TInt aDataProviderId, TBool aIsIncluded, const TDesC& aLocalDatabase)
       
   969 	{
       
   970 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
   971 	
       
   972     TInt index = FindProviderIndex(aDataProviderId);
       
   973 	if (index != KErrNotFound)
       
   974 		{
       
   975 		TAspProviderItem& item = iProviderList[index];
       
   976 		if (!TUtil::IsEmpty(aLocalDatabase))
       
   977 		{
       
   978 			ModifyTaskIncludedL(item, aIsIncluded, aLocalDatabase);	
       
   979 		}
       
   980 		else
       
   981 		{
       
   982 			ModifyTaskIncludedL(item, aIsIncluded, KNullDesC);
       
   983 		}
       
   984 	}
       
   985 	}
       
   986 
       
   987 // -----------------------------------------------------------------------------
       
   988 // CAspContentList::ModifyTaskIncludedL
       
   989 //
       
   990 // -----------------------------------------------------------------------------
       
   991 //
       
   992 void CAspContentList::ModifyTaskIncludedL(TAspProviderItem& aProvider, TBool aIsIncluded, const TDesC& aLocalDatabase)
       
   993 	{
       
   994 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
   995 	
       
   996 	TInt index = FindTaskIndexForProvider(aProvider.iDataProviderId);
       
   997 	if (index == KErrNotFound)
       
   998 		{
       
   999 		if (!TUtil::IsEmpty(aLocalDatabase) && (aProvider.iDataProviderId == KUidNSmlAdapterEMail.iUid))
       
  1000 		{
       
  1001 			CreateTaskL(aProvider.iDataProviderId,aLocalDatabase,KNullDesC,ETrue,TAspTask::SyncDirection(SmlSyncDirection()));
       
  1002 	 		iProfile->Profile().UpdateL();
       
  1003 	 			 
       
  1004 	    }
       
  1005 		return; // no task for this provider
       
  1006 		}
       
  1007 		
       
  1008 	TAspTaskItem& item = iTaskList[index];
       
  1009 	if ((item.iEnabled == aIsIncluded) && (aProvider.iDataProviderId != KUidNSmlAdapterEMail.iUid))
       
  1010 		{
       
  1011 		return; // nothing to modify
       
  1012 		}
       
  1013 	
       
  1014 	if(aProvider.iDataProviderId == KUidNSmlAdapterEMail.iUid)
       
  1015 	{
       
  1016 	RSyncMLTask task;
       
  1017 	CleanupClosePushL(task);
       
  1018     
       
  1019 	task.OpenL(iProfile->Profile(), item.iTaskId);
       
  1020 	if (!TUtil::IsEmpty(aLocalDatabase))
       
  1021 	{
       
  1022 		CreateTaskL(aProvider.iDataProviderId,aLocalDatabase,task.ServerDataSource(),
       
  1023 	 			 ETrue,TAspTask::SyncDirection(task.DefaultSyncType()));
       
  1024 	
       
  1025 	}
       
  1026 
       
  1027 	task.SetEnabledL(aIsIncluded);
       
  1028 	// store task into database
       
  1029 	task.UpdateL();
       
  1030 	
       
  1031 	iProfile->Profile().UpdateL();
       
  1032 	
       
  1033 	item.iEnabled = aIsIncluded;
       
  1034 	CleanupStack::PopAndDestroy(&task);
       
  1035 	}
       
  1036 	else
       
  1037 	{
       
  1038 		RSyncMLTask task;
       
  1039 		CleanupClosePushL(task);
       
  1040     
       
  1041 	task.OpenL(iProfile->Profile(), item.iTaskId);
       
  1042 	task.SetEnabledL(aIsIncluded);
       
  1043 	
       
  1044 	// store task into database
       
  1045 	task.UpdateL();
       
  1046 	iProfile->Profile().UpdateL();
       
  1047 	
       
  1048 	item.iEnabled = aIsIncluded;
       
  1049 	
       
  1050 	CleanupStack::PopAndDestroy(&task);
       
  1051 	}
       
  1052 	}
       
  1053 
       
  1054 // -----------------------------------------------------------------------------
       
  1055 // CAspContentList::ModifyTaskDirectionL
       
  1056 //
       
  1057 // -----------------------------------------------------------------------------
       
  1058 //
       
  1059 void CAspContentList::ModifyTaskDirectionL(TAspProviderItem& aProvider, TInt aSyncDirection)
       
  1060 	{
       
  1061 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
  1062 	
       
  1063 	TInt index = FindTaskIndexForProvider(aProvider.iDataProviderId);
       
  1064 	if (index == KErrNotFound)
       
  1065 		{
       
  1066 		return; // no task for this provider
       
  1067 		}
       
  1068 		
       
  1069 	TAspTaskItem& item = iTaskList[index];
       
  1070 	if (item.iSyncDirection == aSyncDirection)
       
  1071 		{
       
  1072 		return; // nothing to modify
       
  1073 		}
       
  1074 	
       
  1075 	RSyncMLTask task;
       
  1076 	CleanupClosePushL(task);
       
  1077     
       
  1078 	task.OpenL(iProfile->Profile(), item.iTaskId);
       
  1079 	task.SetDefaultSyncTypeL(TAspTask::SmlSyncDirection(aSyncDirection));
       
  1080 	
       
  1081 	// store task into database
       
  1082 	task.UpdateL();
       
  1083 	iProfile->Profile().UpdateL();
       
  1084 	
       
  1085 	item.iSyncDirection = aSyncDirection;
       
  1086 	
       
  1087 	CleanupStack::PopAndDestroy(&task);
       
  1088 	}
       
  1089 
       
  1090 
       
  1091 // -----------------------------------------------------------------------------
       
  1092 // CAspContentList::ModifyTaskDirectionsL
       
  1093 //
       
  1094 // -----------------------------------------------------------------------------
       
  1095 //
       
  1096 void CAspContentList::ModifyTaskDirectionsL(TInt aSyncDirection)
       
  1097 	{
       
  1098 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
  1099 	
       
  1100 	TInt count = iProviderList.Count();
       
  1101 	for (TInt i=0; i<count; i++)
       
  1102 		{
       
  1103         TAspProviderItem& item = iProviderList[i];
       
  1104         ModifyTaskDirectionL(item, aSyncDirection);
       
  1105 		}
       
  1106 	}
       
  1107 
       
  1108 
       
  1109 // -----------------------------------------------------------------------------
       
  1110 // CAspContentList::CheckTaskDirectionsL
       
  1111 //
       
  1112 // -----------------------------------------------------------------------------
       
  1113 //
       
  1114 TBool CAspContentList::CheckTaskDirectionsL()
       
  1115 	{
       
  1116 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
  1117 
       
  1118     TInt previous = KErrNotFound;
       
  1119     
       
  1120 	TInt count = iTaskList.Count();
       
  1121 	for (TInt i=0; i<count; i++)
       
  1122 		{
       
  1123         TAspTaskItem& item = iTaskList[i];
       
  1124         if (item.iSyncDirection != previous && previous != KErrNotFound)
       
  1125         	{
       
  1126         	return EFalse;
       
  1127         	}
       
  1128         	
       
  1129         previous = item.iSyncDirection;
       
  1130 		}
       
  1131 		
       
  1132 	return ETrue;
       
  1133 	}
       
  1134 
       
  1135 
       
  1136 // -----------------------------------------------------------------------------
       
  1137 // CAspContentList::CreateTaskL
       
  1138 //
       
  1139 // -----------------------------------------------------------------------------
       
  1140 //
       
  1141 TInt CAspContentList::CreateTaskL(TAspProviderItem& aDataProvider)
       
  1142 	{
       
  1143 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
  1144 		
       
  1145 	if (!aDataProvider.iHasDefaultDataStore)
       
  1146 		{
       
  1147 		User::Leave(KErrNotFound);
       
  1148 		}
       
  1149 		
       
  1150 	RSyncMLTask task;
       
  1151 	CleanupClosePushL(task);
       
  1152 	
       
  1153 	task.CreateL(iProfile->Profile(), aDataProvider.iDataProviderId, 
       
  1154 	              KNullDesC, aDataProvider.iDefaultDataStore);
       
  1155 	
       
  1156 	task.SetEnabledL(aDataProvider.iIsIncludedInSync);
       
  1157 	task.SetDefaultSyncTypeL(SmlSyncDirection());
       
  1158 	
       
  1159 	// store task into database
       
  1160 	task.UpdateL();
       
  1161 	iProfile->Profile().UpdateL();
       
  1162 	
       
  1163 	TInt id = task.Identifier();
       
  1164 	
       
  1165 	CleanupStack::PopAndDestroy(&task);
       
  1166 	
       
  1167 	return id;
       
  1168 	}
       
  1169 
       
  1170 
       
  1171 // -----------------------------------------------------------------------------
       
  1172 // CAspContentList::CreateTaskL
       
  1173 // 
       
  1174 // -----------------------------------------------------------------------------
       
  1175 //
       
  1176 void CAspContentList::CreateTaskL(TInt aDataProviderId,
       
  1177                                   const TDesC& aLocalDatabase,
       
  1178                                   const TDesC& aRemoteDatabase,
       
  1179                                   TBool aEnabled, TInt aSyncDirection)
       
  1180 	{
       
  1181 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
  1182 		
       
  1183 	TInt providerIndex = FindProviderIndex(aDataProviderId);
       
  1184 	if (providerIndex == KErrNotFound)
       
  1185 		{
       
  1186 		return;
       
  1187 		}
       
  1188 	 
       
  1189 	// remove existing task for aDataProviderId
       
  1190     TInt taskIndex = FindTaskIndexForProvider(aDataProviderId);
       
  1191     if (taskIndex != KErrNotFound)
       
  1192     	{
       
  1193     	TAspTaskItem& item = TaskItem(taskIndex);
       
  1194         iProfile->Profile().DeleteTaskL(item.iTaskId);
       
  1195         iProfile->Profile().UpdateL();
       
  1196         RemoveTask(aDataProviderId);  // remove deleted task from task list   	
       
  1197     	}
       
  1198     
       
  1199     TAspProviderItem& item = ProviderItem(providerIndex);
       
  1200     
       
  1201 	RSyncMLTask task;
       
  1202 	CleanupClosePushL(task);
       
  1203 	
       
  1204 	if (TUtil::IsEmpty(aLocalDatabase))
       
  1205 		{
       
  1206 		if(!ProviderItem(providerIndex).iHasDefaultDataStore)
       
  1207 			{
       
  1208 			// defult data store name was probably truncated
       
  1209 			task.CreateL(iProfile->Profile(), aDataProviderId, 
       
  1210 			             aRemoteDatabase, KNullDesC);
       
  1211 			aEnabled = EFalse;
       
  1212 			}
       
  1213 		else
       
  1214 			{
       
  1215 			task.CreateL(iProfile->Profile(), aDataProviderId, 
       
  1216 			             aRemoteDatabase, item.iDefaultDataStore);
       
  1217 			}
       
  1218 		}
       
  1219 	else
       
  1220 		{
       
  1221 		task.CreateL(iProfile->Profile(), aDataProviderId, 
       
  1222 		                                  aRemoteDatabase, aLocalDatabase);
       
  1223 		}
       
  1224 	
       
  1225 	task.SetEnabledL(aEnabled);
       
  1226 	task.SetDefaultSyncTypeL(TAspTask::SmlSyncDirection(aSyncDirection));
       
  1227 	
       
  1228 	// store task into database
       
  1229 	task.UpdateL();
       
  1230 	iProfile->Profile().UpdateL();
       
  1231 	
       
  1232 	CleanupStack::PopAndDestroy(&task);
       
  1233 	}
       
  1234 
       
  1235 
       
  1236 // -----------------------------------------------------------------------------
       
  1237 // CAspContentList::CreateTask
       
  1238 // 
       
  1239 // -----------------------------------------------------------------------------
       
  1240 //
       
  1241 void CAspContentList::CreateTask(TInt aDataProviderId, 
       
  1242                                  const TDesC& aLocalDatabase,
       
  1243                                  const TDesC& aRemoteDatabase,
       
  1244                                  TBool aEnabled, TInt aSyncDirection)
       
  1245 	{
       
  1246 	FLOG( _L("CAspContentList::CreateTask START") );
       
  1247 	
       
  1248 	TRAPD(err, CreateTaskL(aDataProviderId, aLocalDatabase, aRemoteDatabase, aEnabled, aSyncDirection));
       
  1249 	
       
  1250 	if (err != KErrNone)
       
  1251 		{
       
  1252 		FLOG( _L("### CreateTask failed (id=%x err=%d) ###"), aDataProviderId, err );
       
  1253 		}
       
  1254 		
       
  1255 	FLOG( _L("CAspContentList::CreateTask END") );
       
  1256 	}
       
  1257 
       
  1258 
       
  1259 // -----------------------------------------------------------------------------
       
  1260 // CAspContentList::ReadTaskL
       
  1261 // 
       
  1262 // -----------------------------------------------------------------------------
       
  1263 //
       
  1264 void CAspContentList::ReadTaskL(TInt aDataProviderId, TDes& aLocalDatabase,
       
  1265                                 TDes& aRemoteDatabase, TBool& aEnable, TInt& aSyncDirection)
       
  1266 
       
  1267 	{
       
  1268 	__ASSERT_ALWAYS(iProfile, TUtil::Panic(KErrGeneral));
       
  1269 
       
  1270 	aLocalDatabase = KNullDesC;
       
  1271 	aRemoteDatabase = KNullDesC;
       
  1272 	aSyncDirection = SyncDirection();
       
  1273 	aEnable = EFalse;
       
  1274 
       
  1275 	TInt index = FindTaskIndexForProvider(aDataProviderId);
       
  1276 	if (index == KErrNotFound)
       
  1277 		{
       
  1278 		return;
       
  1279 		}
       
  1280 	 
       
  1281     TAspTaskItem& item = TaskItem(index);
       
  1282     
       
  1283 	RSyncMLTask task;
       
  1284 	CleanupClosePushL(task);
       
  1285 	
       
  1286 	task.OpenL(iProfile->Profile(), item.iTaskId);
       
  1287 	
       
  1288 	aLocalDatabase = task.ClientDataSource();
       
  1289 	aRemoteDatabase = task.ServerDataSource();
       
  1290 	aEnable = task.Enabled();
       
  1291 	aSyncDirection = TAspTask::SyncDirection(task.DefaultSyncType());
       
  1292 		
       
  1293 	CleanupStack::PopAndDestroy(&task);
       
  1294 	}
       
  1295 
       
  1296 
       
  1297 // -----------------------------------------------------------------------------
       
  1298 // CAspContentList::GetLocalDatabaseListL
       
  1299 // 
       
  1300 // -----------------------------------------------------------------------------
       
  1301 //
       
  1302 void CAspContentList::GetLocalDatabaseListL(TInt aDataProviderId, CDesCArray* aList)
       
  1303 	{
       
  1304 	__ASSERT_ALWAYS(iSyncSession, TUtil::Panic(KErrGeneral));
       
  1305 	
       
  1306     RSyncMLDataProvider provider;	
       
  1307     CleanupClosePushL(provider);
       
  1308 
       
  1309     provider.OpenL(*iSyncSession, aDataProviderId);
       
  1310     provider.GetDataStoreNamesL(*aList);
       
  1311     
       
  1312     CleanupStack::PopAndDestroy(&provider);
       
  1313     }
       
  1314 
       
  1315 
       
  1316 // -----------------------------------------------------------------------------
       
  1317 // CAspContentList::GetLocalDatabaseList
       
  1318 // 
       
  1319 // -----------------------------------------------------------------------------
       
  1320 //
       
  1321 void CAspContentList::GetLocalDatabaseList(TInt aDataProviderId, CDesCArray* aList)
       
  1322 	{
       
  1323 	TRAP_IGNORE(GetLocalDatabaseListL(aDataProviderId, aList));
       
  1324     }
       
  1325 
       
  1326 
       
  1327 // -----------------------------------------------------------------------------
       
  1328 // CAspContentList::SmlSyncDirection
       
  1329 //
       
  1330 // -----------------------------------------------------------------------------
       
  1331 //
       
  1332 TSmlSyncType CAspContentList::SmlSyncDirection()
       
  1333 	{
       
  1334 	TInt syncDirection = SyncDirection();
       
  1335 	return TAspTask::SmlSyncDirection(syncDirection);
       
  1336 	}
       
  1337 
       
  1338 
       
  1339 // -----------------------------------------------------------------------------
       
  1340 // CAspContentList::SyncDirection
       
  1341 //
       
  1342 // -----------------------------------------------------------------------------
       
  1343 //
       
  1344 TInt CAspContentList::SyncDirection()
       
  1345 	{
       
  1346 	if (iTaskList.Count() == 0)
       
  1347 		{
       
  1348 		return ESyncDirectionTwoWay; // default sync direction
       
  1349 		}
       
  1350 		
       
  1351 	TAspTaskItem& item = iTaskList[0];
       
  1352 	return item.iSyncDirection;  // all tasks have same sync direction
       
  1353 	}
       
  1354 
       
  1355 
       
  1356 // -----------------------------------------------------------------------------
       
  1357 // CAspContentList::TaskEnabled
       
  1358 //
       
  1359 // -----------------------------------------------------------------------------
       
  1360 //
       
  1361 TBool CAspContentList::TaskEnabled(TInt aDataProviderId)
       
  1362 	{
       
  1363 	TBool ret = EFalse;
       
  1364 	
       
  1365     TInt index = FindTaskIndexForProvider(aDataProviderId);
       
  1366     if (index != KErrNotFound)
       
  1367     	{
       
  1368     	TAspTaskItem& item = TaskItem(index);
       
  1369     	if (item.iEnabled)
       
  1370     		{
       
  1371     		ret = ETrue;
       
  1372     		}
       
  1373     	}
       
  1374     	
       
  1375 	return ret;
       
  1376 	}
       
  1377 
       
  1378 
       
  1379 // -----------------------------------------------------------------------------
       
  1380 // CAspContentList::Sort
       
  1381 //
       
  1382 // -----------------------------------------------------------------------------
       
  1383 //
       
  1384 void CAspContentList::Sort()
       
  1385 	{
       
  1386 	iProviderList.Sort(TLinearOrder<TAspProviderItem>(TAspProviderItem::CompareItems));
       
  1387 	}
       
  1388 
       
  1389 
       
  1390 #ifdef _DEBUG
       
  1391 
       
  1392 // -----------------------------------------------------------------------------
       
  1393 // CAspContentList::LogL
       
  1394 //
       
  1395 // -----------------------------------------------------------------------------
       
  1396 //
       
  1397 void CAspContentList::LogL(const TDesC& aText)
       
  1398 	{
       
  1399 	FLOG( _L("CAspContentList::LogL START") );
       
  1400 
       
  1401 	if (aText.Length() > 0)
       
  1402 		{
       
  1403 		FTRACE( RDebug::Print(_L("Profile = '%S'"), &aText) );
       
  1404 		}
       
  1405 		
       
  1406 	LogTasksL();
       
  1407 	LogDataProvidersL();
       
  1408 	
       
  1409 	FLOG( _L("CAspContentList::LogL END") );
       
  1410 	}
       
  1411 	
       
  1412 
       
  1413 // -----------------------------------------------------------------------------
       
  1414 // CAspContentList::LogTasksL
       
  1415 //
       
  1416 // -----------------------------------------------------------------------------
       
  1417 //
       
  1418 void CAspContentList::LogTasksL()
       
  1419 	{
       
  1420     FLOG( _L("---- sync tasks ----") );
       
  1421     
       
  1422 	TInt count = iTaskList.Count();
       
  1423 	
       
  1424 	for (TInt i=0; i<count; i++)
       
  1425 		{
       
  1426 		TAspTaskItem& t = iTaskList[i];
       
  1427 		TBuf<128> buf;
       
  1428 		GetTaskLogText(buf, t);
       
  1429 		FLOG(buf);
       
  1430 		}
       
  1431 		
       
  1432 	FLOG( _L("---- sync tasks ----") );
       
  1433 	}
       
  1434 
       
  1435 
       
  1436 // -----------------------------------------------------------------------------
       
  1437 // CAspContentList::GetTaskLogText
       
  1438 //
       
  1439 // -----------------------------------------------------------------------------
       
  1440 //
       
  1441 void CAspContentList::GetTaskLogText(TDes& aText, TAspTaskItem& aTask)
       
  1442 	{
       
  1443 	TBuf<KBufSize64> buf;
       
  1444 	GetDataProviderIdText(buf, aTask.iDataProviderId);
       
  1445 	
       
  1446 	aText.Format(_L("id=%d, remote_db=%d, local_db=%d, enabled=%d, providerId=%x, providername='%S'"),
       
  1447 	             aTask.iTaskId, aTask.iRemoteDatabaseDefined, aTask.iLocalDatabaseDefined,
       
  1448 	             aTask.iEnabled, aTask.iDataProviderId, &buf);
       
  1449     }
       
  1450 
       
  1451 
       
  1452 // -----------------------------------------------------------------------------
       
  1453 // CAspContentList::LogDataProvidersL
       
  1454 //
       
  1455 // -----------------------------------------------------------------------------
       
  1456 //
       
  1457 void CAspContentList::LogDataProvidersL()
       
  1458 	{
       
  1459     FLOG( _L("---- data providers ----") );
       
  1460     
       
  1461 	TInt count = iProviderList.Count();
       
  1462 	
       
  1463 	for (TInt i=0; i<count; i++)
       
  1464 		{
       
  1465 		TAspProviderItem& item = iProviderList[i];
       
  1466 		TBuf<32> buf;
       
  1467 		GetDataProviderIdText(buf, item.iDataProviderId);
       
  1468 		if (item.iHasDefaultDataStore)
       
  1469 			{
       
  1470 			FTRACE( RDebug::Print(_L("id=%S (%x), name='%S'"), &buf, item.iDataProviderId, &item.iDisplayName) );
       
  1471 			}
       
  1472 		else
       
  1473 			{
       
  1474 			FTRACE( RDebug::Print(_L("id=%S (%x), name='%S', ### no default data provider ###"), &buf, item.iDataProviderId, &item.iDisplayName) );
       
  1475 			}
       
  1476 		}
       
  1477 		
       
  1478 	FLOG( _L("---- data providers ----") );
       
  1479 	}
       
  1480 
       
  1481     
       
  1482 // -----------------------------------------------------------------------------
       
  1483 // CAspContentList::GetDataProviderIdText
       
  1484 //
       
  1485 // -----------------------------------------------------------------------------
       
  1486 //
       
  1487 void CAspContentList::GetDataProviderIdText(TDes& aText, TInt aId)
       
  1488 	{
       
  1489 	aText = _L("unknown");
       
  1490 	
       
  1491 	if (aId == KUidNSmlAdapterContact.iUid)
       
  1492 		{
       
  1493 		aText = _L("KUidNSmlAdapterContact");
       
  1494 		}
       
  1495 	if (aId == KUidNSmlAdapterCalendar.iUid)
       
  1496 		{
       
  1497 		aText = _L("KUidNSmlAdapterCalendar");
       
  1498 		}
       
  1499 	if (aId == KUidNSmlAdapterEMail.iUid)
       
  1500 		{
       
  1501 		aText = _L("KUidNSmlAdapterEMail");
       
  1502 		}
       
  1503 	if (aId == KUidNSmlAdapterNote.iUid)
       
  1504 		{
       
  1505 		aText = _L("KUidNSmlAdapterNote");
       
  1506     	}
       
  1507 	if (aId == KUidNSmlAdapterSms.iUid)
       
  1508 		{
       
  1509 		aText = _L("KUidNSmlAdapterSms");
       
  1510     	}
       
  1511 	if (aId == KUidNSmlAdapterMMS.iUid)
       
  1512 		{
       
  1513 		aText = _L("KUidNSmlAdapterMMS");
       
  1514     	}
       
  1515 	if (aId == KUidNSmlAdapterBookmarks.iUid)
       
  1516 		{
       
  1517 		aText = _L("KUidNSmlAdapterBookmarks");
       
  1518     	}
       
  1519 	}
       
  1520 	
       
  1521 #endif
       
  1522 
       
  1523 
       
  1524 
       
  1525 /*******************************************************************************
       
  1526  * class TAspProfileItem
       
  1527  *******************************************************************************/
       
  1528 
       
  1529 
       
  1530 // -----------------------------------------------------------------------------
       
  1531 // TAspProfileItem::Init
       
  1532 //
       
  1533 // -----------------------------------------------------------------------------
       
  1534 //
       
  1535 void TAspProfileItem::Init()
       
  1536 	{
       
  1537 	iProfileName = KNullDesC;
       
  1538 	iProfileId = KErrNotFound;
       
  1539 	iSynced = EFalse;
       
  1540 	iLastSync = 0;
       
  1541 	iBearer = KErrNotFound;
       
  1542 	iActive = EFalse;
       
  1543 	iDeleteAllowed = EFalse;
       
  1544 	iApplicationId = KErrNotFound;
       
  1545 	iMandatoryCheck = KErrNotFound;
       
  1546 	iTaskCount = KErrNotFound;
       
  1547 	iTaskId = KErrNotFound;
       
  1548 	}
       
  1549 
       
  1550 
       
  1551 
       
  1552 
       
  1553 // -----------------------------------------------------------------------------
       
  1554 // TAspProfileItem::NameLC
       
  1555 //
       
  1556 // -----------------------------------------------------------------------------
       
  1557 //
       
  1558 HBufC* TAspProfileItem::NameLC()
       
  1559 	{
       
  1560 	return iProfileName.AllocLC();
       
  1561 	}
       
  1562 
       
  1563 
       
  1564 // -----------------------------------------------------------------------------
       
  1565 // TAspProfileItem::Name
       
  1566 //
       
  1567 // -----------------------------------------------------------------------------
       
  1568 //
       
  1569 const TDesC& TAspProfileItem::Name()
       
  1570 	{
       
  1571 	return iProfileName;
       
  1572 	}
       
  1573 
       
  1574 
       
  1575 // -----------------------------------------------------------------------------
       
  1576 // TAspProfileItem::SetName
       
  1577 //
       
  1578 // -----------------------------------------------------------------------------
       
  1579 //
       
  1580 void TAspProfileItem::SetName(const TDesC& aName)
       
  1581 	{
       
  1582 	TUtil::StrCopy(iProfileName, aName);
       
  1583 	}
       
  1584 
       
  1585 
       
  1586 // -----------------------------------------------------------------------------
       
  1587 // TAspProfileItem::CompareItems
       
  1588 //
       
  1589 // -----------------------------------------------------------------------------
       
  1590 //
       
  1591 TInt TAspProfileItem::CompareItems(const TAspProfileItem& aFirst, const TAspProfileItem& aSecond)
       
  1592 	{
       
  1593 	return aFirst.iProfileName.CompareC(aSecond.iProfileName);
       
  1594 	}
       
  1595 
       
  1596 
       
  1597 
       
  1598 
       
  1599 
       
  1600 
       
  1601 /*******************************************************************************
       
  1602  * class CAspProfileList
       
  1603  *******************************************************************************/
       
  1604 
       
  1605 
       
  1606 
       
  1607 // -----------------------------------------------------------------------------
       
  1608 // CAspProfileList::NewLC
       
  1609 //
       
  1610 // -----------------------------------------------------------------------------
       
  1611 //
       
  1612 CAspProfileList* CAspProfileList::NewLC(const TAspParam& aParam)
       
  1613     {
       
  1614     FLOG( _L("CAspProfileList::NewLC START") );
       
  1615     
       
  1616     CAspProfileList* self = new (ELeave) CAspProfileList(aParam);
       
  1617 	CleanupStack::PushL(self);
       
  1618 	self->ConstructL();
       
  1619 	
       
  1620 	
       
  1621     FLOG( _L("CAspProfileList::NewLC END") );
       
  1622 	return self;
       
  1623     }
       
  1624 
       
  1625 
       
  1626 // -----------------------------------------------------------------------------
       
  1627 // CAspProfileList::NewL
       
  1628 //
       
  1629 // -----------------------------------------------------------------------------
       
  1630 //
       
  1631 CAspProfileList* CAspProfileList::NewL(const TAspParam& aParam)
       
  1632     {
       
  1633     FLOG( _L("CAspProfileList::NewL START") );
       
  1634     
       
  1635     CAspProfileList* self = new (ELeave) CAspProfileList(aParam);
       
  1636 	CleanupStack::PushL(self);
       
  1637 	self->ConstructL();
       
  1638 	CleanupStack::Pop(self);
       
  1639 
       
  1640     FLOG( _L("CAspProfileList::NewL END") );
       
  1641 	return self;
       
  1642     }
       
  1643 
       
  1644 
       
  1645 // -----------------------------------------------------------------------------
       
  1646 // Destructor
       
  1647 //
       
  1648 // -----------------------------------------------------------------------------
       
  1649 //
       
  1650 CAspProfileList::~CAspProfileList()
       
  1651     {
       
  1652 	iList.Close();
       
  1653     }
       
  1654 
       
  1655 
       
  1656 // -----------------------------------------------------------------------------
       
  1657 // CAspProfileList::ConstructL
       
  1658 //
       
  1659 // -----------------------------------------------------------------------------
       
  1660 //
       
  1661 void CAspProfileList::ConstructL(void)
       
  1662     {
       
  1663     }
       
  1664 
       
  1665 
       
  1666 // -----------------------------------------------------------------------------
       
  1667 // CAspProfileList::CAspProfileList
       
  1668 //
       
  1669 // -----------------------------------------------------------------------------
       
  1670 //
       
  1671 CAspProfileList::CAspProfileList(const TAspParam& aParam)
       
  1672 	{
       
  1673 	__ASSERT_ALWAYS(aParam.iSyncSession, TUtil::Panic(KErrGeneral));
       
  1674 
       
  1675 	iSyncSession = aParam.iSyncSession;
       
  1676 	iApplicationId = aParam.iApplicationId;
       
  1677 	}
       
  1678 
       
  1679 
       
  1680 // -----------------------------------------------------------------------------
       
  1681 // CAspProfileList::Count
       
  1682 //
       
  1683 // -----------------------------------------------------------------------------
       
  1684 //
       
  1685 TInt CAspProfileList::Count()
       
  1686 	{
       
  1687 	return iList.Count();
       
  1688 	}
       
  1689 
       
  1690 
       
  1691 // -----------------------------------------------------------------------------
       
  1692 // CAspProfileList::Count
       
  1693 //
       
  1694 // -----------------------------------------------------------------------------
       
  1695 //
       
  1696 TInt CAspProfileList::Count(TInt aApplicationId)
       
  1697 	{
       
  1698 	TInt profileCount = 0;
       
  1699 	TInt count = iList.Count();
       
  1700 
       
  1701 	for (TInt i=0; i<count; i++)
       
  1702 		{
       
  1703 		TAspProfileItem& item = iList[i];
       
  1704 		if (item.iApplicationId == aApplicationId)
       
  1705 			{
       
  1706 			profileCount++;
       
  1707 			}
       
  1708 		}
       
  1709 	
       
  1710 	return profileCount;
       
  1711 	}
       
  1712 
       
  1713 
       
  1714 // -----------------------------------------------------------------------------
       
  1715 // CAspProfileList::DeletableProfileCount
       
  1716 //
       
  1717 // -----------------------------------------------------------------------------
       
  1718 //
       
  1719 TInt CAspProfileList::DeletableProfileCount()
       
  1720 	{
       
  1721 	TInt profileCount = 0;
       
  1722 	TInt count = iList.Count();
       
  1723 
       
  1724 	for (TInt i=0; i<count; i++)
       
  1725 		{
       
  1726 		TAspProfileItem& item = iList[i];
       
  1727 		if (item.iDeleteAllowed)
       
  1728 			{
       
  1729 			profileCount++;
       
  1730 			}
       
  1731 		}
       
  1732 	
       
  1733 	return profileCount;
       
  1734 	}
       
  1735 
       
  1736 
       
  1737 // -----------------------------------------------------------------------------
       
  1738 // CAspProfileList::Item
       
  1739 // 
       
  1740 // -----------------------------------------------------------------------------
       
  1741 //
       
  1742 TAspProfileItem& CAspProfileList::Item(TInt aIndex)
       
  1743 	{
       
  1744 	if ((aIndex < 0) || (aIndex >= Count()))
       
  1745 	    {
       
  1746 		TUtil::Panic(KErrArgument);
       
  1747 	    }
       
  1748 
       
  1749 	return iList[aIndex];
       
  1750 	}
       
  1751 
       
  1752 
       
  1753 // -----------------------------------------------------------------------------
       
  1754 // CAspProfileList::FindProfileIndex
       
  1755 //
       
  1756 // -----------------------------------------------------------------------------
       
  1757 //
       
  1758 TInt CAspProfileList::FindProfileIndex(const TDesC& aProfileName)
       
  1759 	{
       
  1760 	TInt count = iList.Count();
       
  1761 
       
  1762 	for (TInt i=0; i<count; i++)
       
  1763 		{
       
  1764 		TPtrC ptr = Item(i).Name();
       
  1765 		if (ptr.Compare(aProfileName) == 0)
       
  1766 			{
       
  1767 			return i;
       
  1768 			}
       
  1769 		}
       
  1770 
       
  1771 	return KErrNotFound;
       
  1772 	}
       
  1773 
       
  1774 // -----------------------------------------------------------------------------
       
  1775 // CAspProfileList::GetDefaultProfileIdL
       
  1776 //
       
  1777 // -----------------------------------------------------------------------------
       
  1778 //
       
  1779 void CAspProfileList::GetDefaultProfileIdL(TInt& aValue)
       
  1780 	{
       
  1781 	const TUid KRepositoryId = KCRUidDSDefaultProfileInternalKeys;
       
  1782 	
       
  1783     CRepository* rep = CRepository::NewLC(KRepositoryId);
       
  1784     TInt err = rep->Get(KNsmlDsDefaultProfile, aValue);
       
  1785 	User::LeaveIfError(err);
       
  1786 	CleanupStack::PopAndDestroy(rep);    
       
  1787 	}
       
  1788 	
       
  1789 // -----------------------------------------------------------------------------
       
  1790 // CAspProfileList::FindLastSyncedProfileIndexL
       
  1791 //
       
  1792 // -----------------------------------------------------------------------------
       
  1793 //
       
  1794 TInt CAspProfileList::FindLastSyncedProfileIndexL()
       
  1795 	{
       
  1796 	TInt index = KErrNotFound;
       
  1797 	TTime syncTime = 0;
       
  1798 	
       
  1799 	TInt count = iList.Count();
       
  1800 
       
  1801 	for (TInt i=0; i<count; i++)
       
  1802 		{
       
  1803 		TAspProfileItem& item = iList[i];
       
  1804 		if (item.iLastSync > syncTime)
       
  1805 			{
       
  1806 			syncTime = item.iLastSync;
       
  1807 			index = i;
       
  1808 			}
       
  1809 		}
       
  1810 	
       
  1811 	if (index == KErrNotFound && count > 0)
       
  1812 		{
       
  1813 		//Read the Default Profile Id from the Cenrep
       
  1814 		TInt defaultprofileId;
       
  1815 		GetDefaultProfileIdL(defaultprofileId);
       
  1816 		for (TInt i=0; i<count; i++)
       
  1817 			{
       
  1818 			TAspProfileItem& item = iList[i];
       
  1819 			if ( item.iProfileId == defaultprofileId )
       
  1820 				{
       
  1821 				index = i;
       
  1822 				break;
       
  1823 				}
       
  1824 			}
       
  1825 		
       
  1826 		if(index == KErrNotFound)
       
  1827 			{
       
  1828 			TAspParam param(iApplicationId, iSyncSession);
       
  1829 	    	    	
       
  1830 			for( TInt i=0; i<count; i++)
       
  1831 				{
       
  1832 				CAspProfile* profile = CAspProfile::NewLC(param);
       
  1833 				TAspProfileItem& item = iList[i];
       
  1834 				profile->OpenL(item.iProfileId, CAspProfile::EOpenRead, CAspProfile::EAllProperties);	
       
  1835 				if( CAspProfile::IsPCSuiteProfile(profile) )
       
  1836 					{
       
  1837 					index = i;
       
  1838 					CleanupStack::PopAndDestroy(profile);
       
  1839 					break;
       
  1840 					}
       
  1841 				else
       
  1842 					{
       
  1843 					CleanupStack::PopAndDestroy(profile);
       
  1844 					}			
       
  1845 				}
       
  1846 			}
       
  1847 		}
       
  1848 	return index;
       
  1849 	}
       
  1850 
       
  1851 
       
  1852 // -----------------------------------------------------------------------------
       
  1853 // CAspProfileList::FilteredList
       
  1854 //
       
  1855 // -----------------------------------------------------------------------------
       
  1856 //
       
  1857 CAspProfileList* CAspProfileList::FilteredListL(TAspFilterInfo& aFilterInfo)
       
  1858 	{
       
  1859 	TInt type = aFilterInfo.iFilterType;
       
  1860 	
       
  1861 	if (type != TAspFilterInfo::EIncludeDeletableProfile &&
       
  1862 	    type != TAspFilterInfo::EIncludeRemoteProfile)
       
  1863 		{
       
  1864 		TUtil::Panic(KErrNotSupported);
       
  1865 		}
       
  1866 		
       
  1867 	TAspParam param(iApplicationId, iSyncSession);
       
  1868 	CAspProfileList* profileList = CAspProfileList::NewLC(param);
       
  1869 	
       
  1870 	TInt count = iList.Count();
       
  1871 
       
  1872 	for (TInt i=0; i<count; i++)
       
  1873 		{
       
  1874 		TAspProfileItem& item = iList[i];
       
  1875 		
       
  1876 		if (type == TAspFilterInfo::EIncludeDeletableProfile)
       
  1877 			{
       
  1878 		    if (item.iDeleteAllowed)
       
  1879 			    {
       
  1880 			    profileList->AddL(item);
       
  1881 			    }
       
  1882 			}
       
  1883 
       
  1884 		if (type == TAspFilterInfo::EIncludeRemoteProfile)
       
  1885 			{
       
  1886 		    if (item.iBearer == EAspBearerInternet)
       
  1887 			    {
       
  1888 			    profileList->AddL(item);
       
  1889 			    }
       
  1890 			}
       
  1891     	}
       
  1892 	
       
  1893 		
       
  1894 	CleanupStack::Pop(profileList);
       
  1895 	return profileList;
       
  1896 	}
       
  1897 
       
  1898 
       
  1899 // -----------------------------------------------------------------------------
       
  1900 // CAspProfileList::ReplaceProfileItemL
       
  1901 //
       
  1902 // -----------------------------------------------------------------------------
       
  1903 //
       
  1904 void CAspProfileList::ReplaceProfileItemL(TAspProfileItem& aProfileItem)
       
  1905 	{
       
  1906 	// remove old profile with same id
       
  1907 	Remove(aProfileItem.iProfileId);
       
  1908 	
       
  1909 	// add new profile 
       
  1910     User::LeaveIfError(iList.Append(aProfileItem));
       
  1911 	}
       
  1912 	
       
  1913 	
       
  1914 // -----------------------------------------------------------------------------
       
  1915 // CAspProfileList::ReadProfileL
       
  1916 //
       
  1917 // -----------------------------------------------------------------------------
       
  1918 //
       
  1919 void CAspProfileList::ReadProfileL(TInt aProfileId)
       
  1920 	{
       
  1921 	FLOG( _L("CAspProfileList::ReadProfileL START") );
       
  1922 	
       
  1923 	TAspParam param(iApplicationId, iSyncSession);
       
  1924     CAspProfile* profile = CAspProfile::NewLC(param);
       
  1925 	
       
  1926 	profile->OpenL(aProfileId, CAspProfile::EOpenRead, CAspProfile::EAllProperties);
       
  1927 	
       
  1928 	TAspProfileItem item;
       
  1929 	item.Init();
       
  1930 
       
  1931 	if (iApplicationId != EApplicationIdSync)
       
  1932 		{
       
  1933 		TInt dataProviderId = TUtil::ProviderIdFromAppId(iApplicationId);
       
  1934 		TInt taskId = TAspTask::FindTaskIdL(profile, dataProviderId, KNullDesC);
       
  1935 		item.iTaskId = taskId; // needed for syncing only one content 
       
  1936 		}
       
  1937 	
       
  1938 	ReadProfileItemL(profile, item);
       
  1939 	
       
  1940 	//item.iMandatoryCheck = CAspProfile::CheckMandatoryData(profile, item.iTaskCount);
       
  1941 	
       
  1942 	ReplaceProfileItemL(item);
       
  1943 
       
  1944 #ifdef _DEBUG
       
  1945     TAspParam param2(iApplicationId, iSyncSession);
       
  1946 	param2.iProfile = profile;
       
  1947 	param2.iMode = CAspContentList::EInitAll;
       
  1948 	CAspContentList* contentList = CAspContentList::NewLC(param2);
       
  1949 
       
  1950     contentList->LogL(item.iProfileName);
       
  1951     
       
  1952     CleanupStack::PopAndDestroy(contentList);
       
  1953 #endif	
       
  1954 	
       
  1955 	CleanupStack::PopAndDestroy(profile);
       
  1956 	
       
  1957 	FLOG( _L("CAspProfileList::ReadProfileL END") );
       
  1958 	}
       
  1959 	
       
  1960 	
       
  1961 // -----------------------------------------------------------------------------
       
  1962 // CAspProfileList::ReadAllProfilesL
       
  1963 //
       
  1964 // -----------------------------------------------------------------------------
       
  1965 //
       
  1966 void CAspProfileList::ReadAllProfilesL(TInt aListMode)
       
  1967 	{
       
  1968 	FLOG( _L("CAspProfileList::ReadAllProfilesL START") );
       
  1969 	
       
  1970 #ifdef _DEBUG
       
  1971     	TTime time_1 = TUtil::TimeBefore();
       
  1972 #endif
       
  1973 	
       
  1974 	iList.Reset();
       
  1975 	
       
  1976 	RArray<TSmlProfileId> arr;
       
  1977     Session().ListProfilesL(arr, ESmlDataSync);
       
  1978    	CleanupClosePushL(arr);
       
  1979 
       
  1980 	TInt count = arr.Count();
       
  1981 
       
  1982 	for (TInt i=0; i<count; i++)
       
  1983 		{
       
  1984 		TAspParam param(iApplicationId, iSyncSession);
       
  1985 		CAspProfile* profile = CAspProfile::NewLC(param);
       
  1986 		TInt id = arr[i];
       
  1987 		
       
  1988 		if (aListMode == EBasePropertiesOnly)
       
  1989 			{
       
  1990 			profile->OpenL(id, CAspProfile::EOpenRead, CAspProfile::EBaseProperties);
       
  1991 			}
       
  1992 		else
       
  1993 			{
       
  1994 			profile->OpenL(id, CAspProfile::EOpenRead, CAspProfile::EAllProperties);
       
  1995 			}
       
  1996 			
       
  1997 
       
  1998 		TAspProfileItem item;
       
  1999 		item.Init();
       
  2000 		
       
  2001 		if (iApplicationId != EApplicationIdSync)
       
  2002 			{
       
  2003 			TInt dataProviderId = TUtil::ProviderIdFromAppId(iApplicationId);
       
  2004 			TInt taskId = TAspTask::FindTaskIdL(profile, dataProviderId, KNullDesC);
       
  2005 			item.iTaskId = taskId; // needed for syncing only one content 
       
  2006 			}
       
  2007 		
       
  2008 		if (aListMode == EBasePropertiesOnly)
       
  2009 			{
       
  2010 			profile->GetName(item.iProfileName);
       
  2011 			if (TUtil::IsEmpty(item.iProfileName))
       
  2012 				{
       
  2013 				CAspResHandler::ReadL(item.iProfileName,R_ASP_UNNAMED_SET);
       
  2014 				}
       
  2015 			item.iApplicationId = profile->CreatorId();
       
  2016 			item.iProfileId = profile->ProfileId();
       
  2017 			}
       
  2018 		else
       
  2019 			{
       
  2020 			ReadProfileItemL(profile, item);
       
  2021 			}
       
  2022 		
       
  2023 		if (aListMode == EMandatoryCheck || aListMode == EMandatoryCheckEx)
       
  2024 			{
       
  2025 			item.iMandatoryCheck = CAspProfile::CheckMandatoryData(profile, item.iTaskCount);
       
  2026 			}
       
  2027 
       
  2028 		if (aListMode == EMandatoryCheckEx && item.iMandatoryCheck != EMandatoryOk)
       
  2029 			{
       
  2030 			CleanupStack::PopAndDestroy(profile);
       
  2031 			continue;  // ignore profile that misses some mandatory data
       
  2032 			}
       
  2033 		
       
  2034 		if(IsAutoSyncProfile(profile))
       
  2035 			{
       
  2036 			CleanupStack::PopAndDestroy(profile);
       
  2037 			continue;  // auto sync profile should be hidden
       
  2038 			}
       
  2039 		
       
  2040 		User::LeaveIfError(iList.Append(item));
       
  2041 		
       
  2042 		CleanupStack::PopAndDestroy(profile);
       
  2043 		}
       
  2044         
       
  2045     CleanupStack::PopAndDestroy(&arr);
       
  2046     
       
  2047 #ifdef _DEBUG    
       
  2048     TUtil::TimeAfter(time_1, _L("ReadAllProfilesL took"));
       
  2049 #endif
       
  2050     
       
  2051     FLOG( _L("CAspProfileList::ReadAllProfilesL END") );
       
  2052 	}
       
  2053 
       
  2054 
       
  2055 // -----------------------------------------------------------------------------
       
  2056 // CAspProfileList::ReadEMailProfilesL
       
  2057 //
       
  2058 // -----------------------------------------------------------------------------
       
  2059 //
       
  2060 void CAspProfileList::ReadEMailProfilesL(const TDesC& aLocalDatabase, TInt /*aListMode*/)
       
  2061 	{
       
  2062 	FLOG( _L("CAspProfileList::ReadEMailProfilesL START") );
       
  2063 	
       
  2064 	iList.Reset();
       
  2065 	
       
  2066 	RArray<TSmlProfileId> arr;
       
  2067     Session().ListProfilesL(arr, ESmlDataSync);
       
  2068    	CleanupClosePushL(arr);
       
  2069 
       
  2070 	TInt count = arr.Count();
       
  2071 	for (TInt i=0; i<count; i++)
       
  2072 		{
       
  2073 		TAspParam param(iApplicationId, iSyncSession);
       
  2074 		CAspProfile* profile = CAspProfile::NewLC(param);
       
  2075 		
       
  2076 		TInt id = arr[i];
       
  2077 		profile->OpenL(id, CAspProfile::EOpenRead, CAspProfile::EBaseProperties);
       
  2078 		
       
  2079 		TAspProfileItem item;
       
  2080 		item.Init();
       
  2081 		
       
  2082 		TInt taskId = TAspTask::FindTaskIdL(profile, KUidNSmlAdapterEMail.iUid, aLocalDatabase);
       
  2083 		item.iTaskId = taskId; // needed for syncing only one content
       
  2084 		
       
  2085 		if( taskId != KErrNotFound )
       
  2086 		{
       
  2087 		profile->GetName(item.iProfileName);
       
  2088 		item.iApplicationId = profile->CreatorId();
       
  2089 		item.iProfileId = profile->ProfileId();
       
  2090 		
       
  2091 		if(IsAutoSyncProfile(profile))
       
  2092 			{
       
  2093 			CleanupStack::PopAndDestroy(profile);
       
  2094 			continue;  // auto sync profile should be hidden
       
  2095 			}
       
  2096 		
       
  2097 		User::LeaveIfError(iList.Append(item));
       
  2098 		}
       
  2099 		CleanupStack::PopAndDestroy(profile);
       
  2100 		}
       
  2101         
       
  2102     CleanupStack::PopAndDestroy(&arr);
       
  2103     
       
  2104     FLOG( _L("CAspProfileList::ReadEMailProfilesL END") );
       
  2105 	}
       
  2106 
       
  2107 
       
  2108 
       
  2109 // -----------------------------------------------------------------------------
       
  2110 // CAspProfileList::ReadProfileItemL
       
  2111 //
       
  2112 // -----------------------------------------------------------------------------
       
  2113 //
       
  2114 void CAspProfileList::ReadProfileItemL(CAspProfile* aProfile, TAspProfileItem& aItem)
       
  2115 	{
       
  2116 	aProfile->GetName(iBuf);
       
  2117 	if (TUtil::IsEmpty(iBuf))
       
  2118 		{
       
  2119 		CAspResHandler::ReadL(iBuf, R_ASP_UNNAMED_SET);
       
  2120 		}
       
  2121     aItem.SetName(iBuf);
       
  2122 	
       
  2123 	aProfile->GetServerId(iBuf);
       
  2124 	TInt len = iBuf.Size();  // number of bytes
       
  2125 	TUint16 checkSum = 0;
       
  2126 	if (len > 0)
       
  2127 		{
       
  2128 		Mem::Crc(checkSum, iBuf.Ptr(), len);
       
  2129 		}
       
  2130 	aItem.iServerIdCheckSum = checkSum;
       
  2131 
       
  2132 	aItem.iProfileId = aProfile->ProfileId();
       
  2133 	aItem.iSynced    = aProfile->IsSynced();
       
  2134 	aItem.iLastSync  = aProfile->LastSync();
       
  2135 	aItem.iBearer    = aProfile->BearerType();
       
  2136     
       
  2137     aItem.iActive = EFalse;
       
  2138     if (aProfile->SASyncState() != ESASyncStateDisable)
       
  2139     	{
       
  2140     	aItem.iActive = ETrue;
       
  2141     	}
       
  2142     	
       
  2143 	aItem.iDeleteAllowed = aProfile->DeleteAllowed();
       
  2144 	aItem.iMandatoryCheck = EMandatoryOk;
       
  2145 	aItem.iApplicationId = aProfile->CreatorId();
       
  2146 	
       
  2147 	aItem.iTaskCount = 0;
       
  2148     }
       
  2149 		
       
  2150 	
       
  2151 // -----------------------------------------------------------------------------
       
  2152 // CAspProfileList::Sort
       
  2153 //
       
  2154 // -----------------------------------------------------------------------------
       
  2155 //
       
  2156 void CAspProfileList::Sort()
       
  2157 	{
       
  2158 	iList.Sort(TLinearOrder<TAspProfileItem>(TAspProfileItem::CompareItems));
       
  2159 	}
       
  2160 
       
  2161 
       
  2162 // -----------------------------------------------------------------------------
       
  2163 // CAspProfileList::Remove
       
  2164 //
       
  2165 // -----------------------------------------------------------------------------
       
  2166 //
       
  2167 void CAspProfileList::Remove(TInt aProfileId)
       
  2168 	{
       
  2169 	TInt count = iList.Count();
       
  2170 	for (TInt i=0; i<count; i++)
       
  2171 		{
       
  2172 		TAspProfileItem& item = iList[i];
       
  2173 		if (item.iProfileId == aProfileId)
       
  2174 			{
       
  2175 			iList.Remove(i);
       
  2176 			return;
       
  2177 			}
       
  2178 		}
       
  2179 	}
       
  2180 
       
  2181 
       
  2182 // -----------------------------------------------------------------------------
       
  2183 // CAspProfileList::AddL
       
  2184 //
       
  2185 // -----------------------------------------------------------------------------
       
  2186 //
       
  2187 void CAspProfileList::AddL(TAspProfileItem& aProfileItem)
       
  2188 	{
       
  2189 	User::LeaveIfError(iList.Append(aProfileItem));
       
  2190 	}
       
  2191 
       
  2192 
       
  2193 // -----------------------------------------------------------------------------
       
  2194 // CAspProfileList::ListIndex
       
  2195 // 
       
  2196 // -----------------------------------------------------------------------------
       
  2197 //
       
  2198 TInt CAspProfileList::ListIndex(TInt aProfileId)
       
  2199 	{
       
  2200 	TInt count = iList.Count();
       
  2201 	for (TInt i=0; i<count; i++)
       
  2202 		{
       
  2203 		TAspProfileItem& item = iList[i];
       
  2204 		if (item.iProfileId == aProfileId)
       
  2205 			{
       
  2206 			return i;
       
  2207 			}
       
  2208 		}
       
  2209 
       
  2210 	return KErrNotFound;
       
  2211 	}
       
  2212 
       
  2213 
       
  2214 // -----------------------------------------------------------------------------
       
  2215 // CAspProfileList::Session
       
  2216 // 
       
  2217 // -----------------------------------------------------------------------------
       
  2218 //
       
  2219 RSyncMLSession& CAspProfileList::Session()
       
  2220 	{
       
  2221 	__ASSERT_DEBUG(iSyncSession, TUtil::Panic(KErrGeneral));
       
  2222 	
       
  2223 	return *iSyncSession;
       
  2224 	}
       
  2225 
       
  2226 
       
  2227 // -----------------------------------------------------------------------------
       
  2228 // CAspProfileList::IsUniqueServerId
       
  2229 // 
       
  2230 // -----------------------------------------------------------------------------
       
  2231 //
       
  2232 TBool CAspProfileList::IsUniqueServerId(const TDesC& aServerId, TInt aProfileId)
       
  2233 	{
       
  2234 	TInt serverIdLen = aServerId.Length();
       
  2235 	if (serverIdLen == 0)
       
  2236 		{
       
  2237 		return ETrue;
       
  2238 		}
       
  2239 		
       
  2240 	TBool serverIdFound = EFalse;
       
  2241 	
       
  2242 	TInt count = iList.Count();
       
  2243 	for (TInt i=0; i<count; i++)
       
  2244 		{
       
  2245 	   	TAspProfileItem& item = iList[i];
       
  2246 		if (item.iProfileId == aProfileId)
       
  2247 			{
       
  2248 			continue;
       
  2249 			}
       
  2250 
       
  2251 		TUint16 checkSum = 0;
       
  2252 		TInt len = aServerId.Size(); // number of bytes
       
  2253 	
       
  2254 		Mem::Crc(checkSum, aServerId.Ptr(), len);
       
  2255 		if (item.iServerIdCheckSum == checkSum)
       
  2256 			{
       
  2257 			serverIdFound = ETrue;
       
  2258 			break;
       
  2259 			}
       
  2260 		}
       
  2261 
       
  2262     if (serverIdFound)
       
  2263     	{
       
  2264     	return EFalse;
       
  2265     	}
       
  2266     	
       
  2267     return ETrue;
       
  2268 	}
       
  2269 
       
  2270 
       
  2271 TBool CAspProfileList::IsAutoSyncProfile(CAspProfile* aProfile)
       
  2272 	{
       
  2273 	TBuf<KBufSize> profileName;
       
  2274 	aProfile->GetName(profileName);
       
  2275 	if (profileName.Compare(KAutoSyncProfileName) == 0)
       
  2276 		{
       
  2277 		return ETrue;
       
  2278 		}
       
  2279 	return EFalse;
       
  2280 	}
       
  2281 
       
  2282 /******************************************************************************
       
  2283  * class CAspProfile
       
  2284  ******************************************************************************/
       
  2285 
       
  2286 
       
  2287 // -----------------------------------------------------------------------------
       
  2288 // CAspProfile::NewLC
       
  2289 //
       
  2290 // -----------------------------------------------------------------------------
       
  2291 //
       
  2292 CAspProfile* CAspProfile::NewLC(const TAspParam& aParam)
       
  2293     {
       
  2294     FLOG( _L("CAspProfile::NewLC START") );
       
  2295     
       
  2296     CAspProfile* self = new (ELeave) CAspProfile(aParam);
       
  2297 	CleanupStack::PushL(self);
       
  2298 	self->ConstructL();
       
  2299 
       
  2300 	FLOG( _L("CAspProfile::NewLC END") );
       
  2301 	return self;
       
  2302     }
       
  2303 
       
  2304 // -----------------------------------------------------------------------------
       
  2305 // CAspProfile::NewL
       
  2306 //
       
  2307 // -----------------------------------------------------------------------------
       
  2308 //
       
  2309 CAspProfile* CAspProfile::NewL (const TAspParam& aParam)
       
  2310     {
       
  2311     FLOG( _L("CAspProfile::NewL START") );
       
  2312     
       
  2313     CAspProfile* self = new (ELeave) CAspProfile(aParam);
       
  2314 	CleanupStack::PushL(self);
       
  2315 	self->ConstructL();
       
  2316 	CleanupStack::Pop(self);
       
  2317 
       
  2318     FLOG( _L("CAspProfile::NewL END") );
       
  2319 	return self;
       
  2320     }
       
  2321 
       
  2322 
       
  2323 // -----------------------------------------------------------------------------
       
  2324 // Destructor
       
  2325 //
       
  2326 // -----------------------------------------------------------------------------
       
  2327 //
       
  2328 CAspProfile::~CAspProfile()
       
  2329     {
       
  2330     if (iHistoryLogOpen)
       
  2331     	{
       
  2332     	iHistoryLog.Close();
       
  2333     	}
       
  2334 
       
  2335     if (iConnectionOpen)
       
  2336     	{
       
  2337     	iConnection.Close();
       
  2338     	}
       
  2339     
       
  2340     iProfile.Close();
       
  2341     }
       
  2342 
       
  2343 
       
  2344 // -----------------------------------------------------------------------------
       
  2345 // CAspProfile::ConstructL
       
  2346 //
       
  2347 // -----------------------------------------------------------------------------
       
  2348 //
       
  2349 void CAspProfile::ConstructL()
       
  2350     {
       
  2351     }
       
  2352 
       
  2353 
       
  2354 // -----------------------------------------------------------------------------
       
  2355 // CAspProfile::CAspProfile
       
  2356 //
       
  2357 // -----------------------------------------------------------------------------
       
  2358 //
       
  2359 CAspProfile::CAspProfile(const TAspParam& aParam)
       
  2360 	{
       
  2361 	__ASSERT_ALWAYS(aParam.iSyncSession, TUtil::Panic(KErrGeneral));
       
  2362 
       
  2363 	iSyncSession = aParam.iSyncSession;
       
  2364 	iApplicationId = aParam.iApplicationId;
       
  2365 	
       
  2366 	
       
  2367     iHistoryLogOpen = EFalse;
       
  2368     iConnectionOpen = EFalse;
       
  2369 	}
       
  2370 
       
  2371 
       
  2372 // -----------------------------------------------------------------------------
       
  2373 // CAspProfile::OpenL
       
  2374 //
       
  2375 // -----------------------------------------------------------------------------
       
  2376 //
       
  2377 void CAspProfile::OpenL(TInt aProfileId, TInt aReadWrite, TInt aOpenMode)
       
  2378     {
       
  2379     FLOG( _L("CAspProfile::OpenL START") );
       
  2380     
       
  2381     if (aReadWrite == EOpenRead)
       
  2382     	{
       
  2383     	iProfile.OpenL(*iSyncSession, aProfileId, ESmlOpenRead);
       
  2384     	}
       
  2385     else
       
  2386     	{
       
  2387     	iProfile.OpenL(*iSyncSession, aProfileId, ESmlOpenReadWrite);
       
  2388     	}
       
  2389     
       
  2390     if (aOpenMode == EAllProperties)
       
  2391     	{
       
  2392     	OpenConnection();  // this updates iConnectionOpen
       
  2393     	OpenHistoryLog();  // this updates iHistoryLogOpen
       
  2394     	}
       
  2395     	
       
  2396     FLOG( _L("CAspProfile::OpenL END") );
       
  2397     }
       
  2398 
       
  2399 
       
  2400 // -----------------------------------------------------------------------------
       
  2401 // CAspProfile::CreateL
       
  2402 //
       
  2403 // -----------------------------------------------------------------------------
       
  2404 //
       
  2405 void CAspProfile::CreateL(TInt aOpenMode)
       
  2406     {
       
  2407     FLOG( _L("CAspProfile::CreateL START") );
       
  2408     
       
  2409    	iProfile.CreateL(*iSyncSession);
       
  2410    	iProfile.SetCreatorId(iApplicationId);  // creator application id
       
  2411    	iProfile.UpdateL();
       
  2412    	
       
  2413 	// create connection for new profile (convergence)
       
  2414     //TInt transportId = DefaultTransportIdL();
       
  2415     //iConnection.CreateL(iProfile, transportId);
       
  2416     //iConnection.UpdateL();
       
  2417     //iConnection.Close();
       
  2418     //iProfile.UpdateL();	
       
  2419    	
       
  2420     if (aOpenMode == EAllProperties)
       
  2421     	{
       
  2422     	OpenConnection();  // this updates iConnectionOpen
       
  2423     	OpenHistoryLog();  // this updates iHistoryLogOpen
       
  2424     	}
       
  2425     	
       
  2426     FLOG( _L("CAspProfile::CreateL END") );
       
  2427     }
       
  2428 
       
  2429 
       
  2430 // -----------------------------------------------------------------------------
       
  2431 // CAspProfile::DefaultTransportIdL 
       
  2432 // -----------------------------------------------------------------------------
       
  2433 //
       
  2434 TInt CAspProfile::DefaultTransportIdL()
       
  2435     {
       
  2436     TAspParam param(EApplicationIdSync, iSyncSession);
       
  2437     CAspBearerHandler* bearerHandler = CAspBearerHandler::NewL(param);
       
  2438     CleanupStack::PushL(bearerHandler);
       
  2439     
       
  2440     TInt id = bearerHandler->DefaultBearer(); 
       
  2441     
       
  2442     CleanupStack::PopAndDestroy(bearerHandler);
       
  2443     
       
  2444     if (id == KErrNotFound)
       
  2445     	{
       
  2446     	User::Leave(KErrNotFound);
       
  2447     	}
       
  2448     	
       
  2449     return CAspBearerHandler::SmlBearerId(id);
       
  2450     }
       
  2451 
       
  2452 
       
  2453 // -----------------------------------------------------------------------------
       
  2454 // CAspProfile::CreateCopyL
       
  2455 //
       
  2456 // -----------------------------------------------------------------------------
       
  2457 //
       
  2458 void CAspProfile::CreateCopyL(TInt aProfileId)
       
  2459     {
       
  2460     FLOG( _L("CAspProfile::CreateCopyL START") );
       
  2461     
       
  2462     CreateL(EAllProperties);
       
  2463     
       
  2464     TAspParam param(iApplicationId, iSyncSession);
       
  2465     CAspProfile* profile = CAspProfile::NewLC(param);
       
  2466     profile->OpenL(aProfileId, CAspProfile::EOpenRead, CAspProfile::EAllProperties);
       
  2467     
       
  2468     CopyValuesL(profile);
       
  2469     
       
  2470     CleanupStack::PopAndDestroy(profile);
       
  2471     
       
  2472     FLOG( _L("CAspProfile::CreateCopyL END") );
       
  2473     }
       
  2474 
       
  2475 
       
  2476 // -----------------------------------------------------------------------------
       
  2477 // CAspProfile::SaveL
       
  2478 //
       
  2479 // -----------------------------------------------------------------------------
       
  2480 //
       
  2481 void CAspProfile::SaveL()
       
  2482     {
       
  2483     //iProfile.UpdateL();  // convergence
       
  2484     
       
  2485     if (iConnectionOpen)
       
  2486     	{
       
  2487     	iConnection.UpdateL();
       
  2488     	}
       
  2489     	
       
  2490    	iProfile.UpdateL();
       
  2491     }
       
  2492 
       
  2493 
       
  2494 // -----------------------------------------------------------------------------
       
  2495 // CAspProfile::Save
       
  2496 //
       
  2497 // -----------------------------------------------------------------------------
       
  2498 //
       
  2499 void CAspProfile::Save()
       
  2500     {
       
  2501     TRAPD(err, SaveL());
       
  2502     
       
  2503     if (err != KErrNone)
       
  2504     	{
       
  2505     	FLOG( _L("###  CAspProfile::SaveL failed (%d) ###"), err );
       
  2506     	}
       
  2507     }
       
  2508 
       
  2509 
       
  2510 // -----------------------------------------------------------------------------
       
  2511 // CAspProfile::GetName
       
  2512 //
       
  2513 // -----------------------------------------------------------------------------
       
  2514 //
       
  2515 void CAspProfile::GetName(TDes& aText)
       
  2516 	{
       
  2517  		if(!IsPCSuiteProfile(this))
       
  2518  		{
       
  2519 			TUtil::StrCopy(aText, iProfile.DisplayName());
       
  2520 			return;
       
  2521  		}
       
  2522  		//For PC suite profile Localise as per specifications
       
  2523  		GetLocalisedPCSuite(aText);
       
  2524 	}
       
  2525 
       
  2526 
       
  2527 // -----------------------------------------------------------------------------
       
  2528 // CAspProfile::NameL
       
  2529 //
       
  2530 // -----------------------------------------------------------------------------
       
  2531 //
       
  2532 HBufC* CAspProfile::NameL()
       
  2533 	{
       
  2534 	return iProfile.DisplayName().AllocL();
       
  2535 	}
       
  2536 
       
  2537 
       
  2538 // -----------------------------------------------------------------------------
       
  2539 // CAspProfile::SetNameL
       
  2540 //
       
  2541 // -----------------------------------------------------------------------------
       
  2542 //
       
  2543 void CAspProfile::SetNameL(const TDesC& aText)
       
  2544 	{
       
  2545 	iProfile.SetDisplayNameL(aText);
       
  2546 	}
       
  2547 
       
  2548 
       
  2549 // -----------------------------------------------------------------------------
       
  2550 // CAspProfile::CreatorId
       
  2551 //
       
  2552 // -----------------------------------------------------------------------------
       
  2553 //
       
  2554 TInt CAspProfile::CreatorId()
       
  2555 	{
       
  2556 	return iProfile.CreatorId();
       
  2557 	}
       
  2558 
       
  2559 
       
  2560 // -----------------------------------------------------------------------------
       
  2561 // CAspProfile::SetCreatorId
       
  2562 //
       
  2563 // -----------------------------------------------------------------------------
       
  2564 //
       
  2565 void CAspProfile::SetCreatorId(TInt aCreatorId)
       
  2566 	{
       
  2567 	iProfile.SetCreatorId(aCreatorId);
       
  2568 	}
       
  2569 
       
  2570 
       
  2571 // -----------------------------------------------------------------------------
       
  2572 // CAspProfile::ProfileId
       
  2573 //
       
  2574 // -----------------------------------------------------------------------------
       
  2575 //
       
  2576 TInt CAspProfile::ProfileId()
       
  2577 	{
       
  2578 	return iProfile.Identifier();
       
  2579 	}
       
  2580 
       
  2581 
       
  2582 // -----------------------------------------------------------------------------
       
  2583 // CAspProfile::DeleteAllowed
       
  2584 //
       
  2585 // -----------------------------------------------------------------------------
       
  2586 //
       
  2587 TBool CAspProfile::DeleteAllowed()
       
  2588 	{
       
  2589 	return iProfile.DeleteAllowed();
       
  2590 	}
       
  2591 
       
  2592 
       
  2593 // -----------------------------------------------------------------------------
       
  2594 // CAspProfile::IsSynced
       
  2595 //
       
  2596 // -----------------------------------------------------------------------------
       
  2597 //
       
  2598 TBool CAspProfile::IsSynced()
       
  2599     {
       
  2600     if (!iHistoryLogOpen)
       
  2601     	{
       
  2602     	return EFalse;
       
  2603     	}
       
  2604     	
       
  2605     if (iHistoryLog.Count() == 0)
       
  2606         {
       
  2607         return EFalse;
       
  2608         }
       
  2609     
       
  2610     return ETrue;
       
  2611     }
       
  2612 
       
  2613 
       
  2614 // -----------------------------------------------------------------------------
       
  2615 // CAspProfile::LastSync
       
  2616 //
       
  2617 // -----------------------------------------------------------------------------
       
  2618 //
       
  2619 TTime CAspProfile::LastSync()
       
  2620     {
       
  2621     if (!iHistoryLogOpen)
       
  2622     	{
       
  2623     	return 0;
       
  2624     	}
       
  2625 
       
  2626     if (iHistoryLog.Count() == 0)
       
  2627         {
       
  2628         return 0;  // profile has not been synced
       
  2629         }
       
  2630         
       
  2631     const CSyncMLHistoryJob* job = LatestHistoryJob();
       
  2632     if (!job)
       
  2633     	{
       
  2634     	return 0;
       
  2635     	}
       
  2636     
       
  2637     return job->TimeStamp();
       
  2638     }
       
  2639 
       
  2640 
       
  2641 // -----------------------------------------------------------------------------
       
  2642 // CAspProfile::LastSync
       
  2643 //
       
  2644 // -----------------------------------------------------------------------------
       
  2645 //
       
  2646 TTime CAspProfile::LastSync(TInt aTaskId)
       
  2647     {
       
  2648     if (!iHistoryLogOpen)
       
  2649     	{
       
  2650     	return 0;
       
  2651     	}
       
  2652 
       
  2653     if (iHistoryLog.Count() == 0)
       
  2654         {
       
  2655         return 0;  // profile has not been synced
       
  2656         }
       
  2657         
       
  2658     const CSyncMLHistoryJob* job = LatestHistoryJob(aTaskId);
       
  2659     if (!job)
       
  2660     	{
       
  2661     	return 0;
       
  2662     	}
       
  2663     
       
  2664     return job->TimeStamp();
       
  2665     }
       
  2666 
       
  2667 
       
  2668 // -----------------------------------------------------------------------------
       
  2669 // CAspProfile::GetServerId
       
  2670 //
       
  2671 // -----------------------------------------------------------------------------
       
  2672 //
       
  2673 void CAspProfile::GetServerId(TDes& aText)
       
  2674 	{
       
  2675 	TUtil::StrCopy(aText, iProfile.ServerId());  // convert TDes8 to TDes
       
  2676 	}
       
  2677 
       
  2678 
       
  2679 // -----------------------------------------------------------------------------
       
  2680 // CAspProfile::SetGetServerIdL
       
  2681 //
       
  2682 // -----------------------------------------------------------------------------
       
  2683 //
       
  2684 void CAspProfile::SetServerIdL(const TDesC& aText)
       
  2685 	{
       
  2686 	TUtil::StrCopy(iBuf8, aText);  // convert TDes to TDes8
       
  2687 	iProfile.SetServerIdL(iBuf8);
       
  2688 	}
       
  2689 
       
  2690 
       
  2691 // -----------------------------------------------------------------------------
       
  2692 // CAspProfile::ProtocolVersion
       
  2693 //
       
  2694 // -----------------------------------------------------------------------------
       
  2695 //
       
  2696 TInt CAspProfile::ProtocolVersion()
       
  2697 	{
       
  2698 	TSmlProtocolVersion version = iProfile.ProtocolVersion();
       
  2699 	if (version == ESmlVersion1_1_2)
       
  2700 		{
       
  2701 		return EAspProtocol_1_1;
       
  2702 		}
       
  2703 		
       
  2704 	return EAspProtocol_1_2;
       
  2705 	}
       
  2706 	
       
  2707 	
       
  2708 // -----------------------------------------------------------------------------
       
  2709 // CAspProfile::SetProtocolVersionL
       
  2710 //
       
  2711 // -----------------------------------------------------------------------------
       
  2712 //
       
  2713 void CAspProfile::SetProtocolVersionL(TInt aProtocolVersion)
       
  2714 	{
       
  2715 	if (aProtocolVersion == EAspProtocol_1_1)
       
  2716 		{
       
  2717 		iProfile.SetProtocolVersionL(ESmlVersion1_1_2);
       
  2718 		}
       
  2719 	else
       
  2720 		{
       
  2721 		iProfile.SetProtocolVersionL(ESmlVersion1_2);
       
  2722 		}
       
  2723     }
       
  2724 
       
  2725 
       
  2726 // -----------------------------------------------------------------------------
       
  2727 // CAspProfile::BearerType
       
  2728 //
       
  2729 // -----------------------------------------------------------------------------
       
  2730 //
       
  2731 TInt CAspProfile::BearerType()
       
  2732 	{
       
  2733 	if (!iConnectionOpen)
       
  2734 		{
       
  2735 	    return KErrNotFound;	
       
  2736 		}
       
  2737 
       
  2738 	TInt id = iConnection.Identifier();
       
  2739    	return CAspBearerHandler::AspBearerId(id);
       
  2740     }
       
  2741 
       
  2742 
       
  2743 // -----------------------------------------------------------------------------
       
  2744 // CAspProfile::SetBearerTypeL
       
  2745 //
       
  2746 // -----------------------------------------------------------------------------
       
  2747 //
       
  2748 void CAspProfile::SetBearerTypeL(TInt aId)
       
  2749 	{
       
  2750 	if (!iConnectionOpen)
       
  2751 		{
       
  2752 		return;
       
  2753 		}
       
  2754 	
       
  2755 	TInt newBearer = CAspBearerHandler::SmlBearerId(aId);
       
  2756     
       
  2757     if (newBearer != KErrNotFound)
       
  2758     	{
       
  2759     	TInt currentBearer = iConnection.Identifier();
       
  2760     	if (currentBearer != newBearer)
       
  2761     		{
       
  2762     		iConnection.CreateL(Profile(), newBearer);
       
  2763     		}
       
  2764     	}
       
  2765 	}
       
  2766 
       
  2767 
       
  2768 
       
  2769 // -----------------------------------------------------------------------------
       
  2770 // CAspProfile::SetBearerTypeL (convergence)
       
  2771 //
       
  2772 // -----------------------------------------------------------------------------
       
  2773 //
       
  2774 /*
       
  2775 void CAspProfile::SetBearerTypeL(TInt aId)
       
  2776 	{
       
  2777 	TInt newBearer = CAspBearerHandler::SmlBearerId(aId);
       
  2778 	if (newBearer == KErrNotFound)
       
  2779 		{
       
  2780 		return;  // unknown bearer
       
  2781 		}
       
  2782 	
       
  2783    	TInt currentBearer = KErrNotFound;
       
  2784    	if (iConnectionOpen)
       
  2785    	    {
       
  2786    		currentBearer = iConnection.Identifier();
       
  2787    	    }
       
  2788    	if (currentBearer == newBearer)
       
  2789    		{
       
  2790    		return;	// bearer has not changed
       
  2791    		}
       
  2792 
       
  2793 	if (iConnectionOpen)
       
  2794 	    {
       
  2795 		iConnection.Close();
       
  2796 		iConnectionOpen = EFalse;
       
  2797 	    }
       
  2798 
       
  2799 	DeleteConnectionsL(0);
       
  2800     			
       
  2801 	TRAPD(err, iConnection.CreateL(iProfile, newBearer));
       
  2802    	if (err == KErrNone)
       
  2803     	{
       
  2804     	iConnection.UpdateL();
       
  2805         iConnection.Close();
       
  2806         iProfile.UpdateL();
       
  2807         iConnection.OpenL(iProfile, newBearer);
       
  2808         iConnectionOpen = ETrue;    		    				    
       
  2809       	}
       
  2810     }
       
  2811 */
       
  2812 
       
  2813 
       
  2814 // -----------------------------------------------------------------------------
       
  2815 // CAspProfile::DeleteConnectionsL (convergence)
       
  2816 //
       
  2817 // -----------------------------------------------------------------------------
       
  2818 //
       
  2819 /*
       
  2820 void CAspProfile::DeleteConnectionsL()
       
  2821     {
       
  2822    	RArray<TSmlConnectionId> arr;
       
  2823    	Profile().ListConnectionsL(arr);
       
  2824    	CleanupClosePushL(arr);
       
  2825    	
       
  2826    	TInt count = arr.Count();
       
  2827     	
       
  2828    	for (TInt i=0; i<count; i++)
       
  2829    	    {
       
  2830    	    TInt id = arr[i];
       
  2831    	    iProfile.DeleteConnectionL(id);
       
  2832    	    iProfile.UpdateL();
       
  2833    	    }
       
  2834    	    
       
  2835    	CleanupStack::PopAndDestroy(&arr);
       
  2836     }
       
  2837 */
       
  2838 
       
  2839 
       
  2840 
       
  2841 // -----------------------------------------------------------------------------
       
  2842 // CAspProfile::AccessPointL
       
  2843 //
       
  2844 // -----------------------------------------------------------------------------
       
  2845 //
       
  2846 TInt CAspProfile::AccessPointL()
       
  2847 	{
       
  2848 	if (!iConnectionOpen)
       
  2849 		{
       
  2850     	return KErrNotFound;
       
  2851 		}
       
  2852 	
       
  2853 	if (iConnection.Identifier() != KUidNSmlMediumTypeInternet.iUid)
       
  2854 		{
       
  2855 		return KErrNotFound; // this setting is only for internet bearer
       
  2856 		}
       
  2857 		
       
  2858 	TBuf8<KBufSize32> key;
       
  2859 	TBuf<KBufSize32> value;
       
  2860 	TInt num = KErrNotFound;
       
  2861 
       
  2862 	GetConnectionPropertyNameL(key, EPropertyIntenetAccessPoint);
       
  2863 	TUtil::StrCopy(value, iConnection.GetPropertyL(key)); // convert TDes8 to TDes
       
  2864 	if (TUtil::IsEmpty(value))
       
  2865 		{
       
  2866 		return KErrNotFound;
       
  2867 		}
       
  2868 		
       
  2869 	User::LeaveIfError(TUtil::StrToInt(value, num));
       
  2870 	return num;
       
  2871 	}
       
  2872 		
       
  2873 	
       
  2874 // -----------------------------------------------------------------------------
       
  2875 // CAspProfile::SetAccessPointL
       
  2876 //
       
  2877 // -----------------------------------------------------------------------------
       
  2878 //
       
  2879 void CAspProfile::SetAccessPointL(const TInt aId)
       
  2880 	{
       
  2881     if (!iConnectionOpen)	
       
  2882     	{
       
  2883     	return;
       
  2884     	}
       
  2885     	
       
  2886 	if (iConnection.Identifier() != KUidNSmlMediumTypeInternet.iUid)
       
  2887 		{
       
  2888 		return; // this setting is only for internet bearer
       
  2889 		}
       
  2890 
       
  2891 	TBuf<KBufSize32> buf;
       
  2892 	TBuf8<KBufSize32> key;
       
  2893 	TBuf8<KBufSize32> value;
       
  2894 	
       
  2895 	buf.Num(aId);
       
  2896 	TUtil::StrCopy(value, buf);  // convwert TDes to TDes8
       
  2897 	
       
  2898 	GetConnectionPropertyNameL(key, EPropertyIntenetAccessPoint);
       
  2899 	iConnection.SetPropertyL(key, value);
       
  2900     }
       
  2901 
       
  2902 
       
  2903 // -----------------------------------------------------------------------------
       
  2904 // CAspProfile::GetHostAddress
       
  2905 //
       
  2906 // -----------------------------------------------------------------------------
       
  2907 //
       
  2908 void CAspProfile::GetHostAddress(TDes& aText, TInt& aPort)
       
  2909 	{
       
  2910 	aText = KNullDesC;
       
  2911 	aPort = TURIParser::EDefaultHttpPort;
       
  2912 	
       
  2913     if (!iConnectionOpen)	
       
  2914     	{
       
  2915       	return;
       
  2916     	}
       
  2917 	
       
  2918 	if (BearerType() != EAspBearerInternet)
       
  2919 		{
       
  2920 		TUtil::StrCopy(aText, iConnection.ServerURI()); // convert TDes8 to TDes
       
  2921 		return;  // port number handling is for internet bearer only 
       
  2922 		}
       
  2923 
       
  2924 	TUtil::StrCopy(iBuf, iConnection.ServerURI()); // convert TDes8 to TDes
       
  2925 	
       
  2926 	TURIParser parser(iBuf);
       
  2927 	
       
  2928 	parser.GetUriWithoutPort(aText);
       
  2929 
       
  2930 	aPort = parser.Port();
       
  2931 	if (aPort == KErrNotFound)
       
  2932 		{
       
  2933 		aPort = parser.DefaultPort();
       
  2934 		}
       
  2935 	}
       
  2936 	
       
  2937 	
       
  2938 // -----------------------------------------------------------------------------
       
  2939 // CAspProfile::SetHostAddressL
       
  2940 //
       
  2941 // -----------------------------------------------------------------------------
       
  2942 //
       
  2943 void CAspProfile::SetHostAddressL(const TDesC& aText, const TInt aPort)
       
  2944 	{
       
  2945     if (!iConnectionOpen)	
       
  2946     	{
       
  2947     	return;
       
  2948     	}
       
  2949 	
       
  2950 	if (BearerType() != EAspBearerInternet)
       
  2951 		{
       
  2952 		TUtil::StrCopy(iBuf, aText); // port number handling is for inteernet bearer only 
       
  2953 		}
       
  2954 	else
       
  2955 		{
       
  2956 		TURIParser parser(aText); // add port number to internet host address
       
  2957 	    parser.GetUriWithPort(iBuf, aPort);
       
  2958 		}
       
  2959 	
       
  2960 	TUtil::StrCopy(iBuf8, iBuf);  // convert TDes to TDes8
       
  2961 	iConnection.SetServerURIL(iBuf8);
       
  2962 	}
       
  2963 
       
  2964 
       
  2965 // -----------------------------------------------------------------------------
       
  2966 // CAspProfile::GetUserName
       
  2967 //
       
  2968 // -----------------------------------------------------------------------------
       
  2969 //
       
  2970 void CAspProfile::GetUserName(TDes& aText)
       
  2971 	{
       
  2972 	TUtil::StrCopy(aText, iProfile.UserName());   // convert TDes8 to TDes
       
  2973 	}
       
  2974 	
       
  2975 	
       
  2976 // -----------------------------------------------------------------------------
       
  2977 // CAspProfile::SetUserNameL
       
  2978 //
       
  2979 // -----------------------------------------------------------------------------
       
  2980 //
       
  2981 void CAspProfile::SetUserNameL(const TDesC& aText)
       
  2982 	{
       
  2983 	TUtil::StrCopy(iBuf8, aText);  // conver TDes to TDes8
       
  2984 	iProfile.SetUserNameL(iBuf8);
       
  2985 	}
       
  2986 
       
  2987 
       
  2988 // -----------------------------------------------------------------------------
       
  2989 // CAspProfile::GetPassword
       
  2990 //
       
  2991 // -----------------------------------------------------------------------------
       
  2992 //
       
  2993 void CAspProfile::GetPassword(TDes& aText)
       
  2994 	{
       
  2995 	TUtil::StrCopy(aText, iProfile.Password());   // convert TDes8 to TDes
       
  2996 	}
       
  2997 	
       
  2998 	
       
  2999 // -----------------------------------------------------------------------------
       
  3000 // CAspProfile::SetPasswordL
       
  3001 //
       
  3002 // -----------------------------------------------------------------------------
       
  3003 //
       
  3004 void CAspProfile::SetPasswordL(const TDesC& aText)
       
  3005 	{
       
  3006 	TUtil::StrCopy(iBuf8, aText);    // conver TDes to TDes8
       
  3007 	iProfile.SetPasswordL(iBuf8);
       
  3008 	}
       
  3009 
       
  3010 	
       
  3011 // -----------------------------------------------------------------------------
       
  3012 // CAspProfile::SASyncState
       
  3013 //
       
  3014 // -----------------------------------------------------------------------------
       
  3015 //
       
  3016 TInt CAspProfile::SASyncState()
       
  3017 	{
       
  3018 	TSmlServerAlertedAction state = iProfile.SanUserInteraction();
       
  3019 
       
  3020 	if (state == ESmlConfirmSync)
       
  3021 		{
       
  3022 		return ESASyncStateConfirm;
       
  3023 		}
       
  3024 	else if (state == ESmlDisableSync)
       
  3025 		{
       
  3026 		return ESASyncStateDisable;
       
  3027 		}
       
  3028 	else 
       
  3029 		{
       
  3030 		return ESASyncStateEnable;
       
  3031 		}
       
  3032 	}
       
  3033 
       
  3034 
       
  3035 // -----------------------------------------------------------------------------
       
  3036 // CAspProfile::SetSASyncStateL
       
  3037 //
       
  3038 // -----------------------------------------------------------------------------
       
  3039 //
       
  3040 void CAspProfile::SetSASyncStateL(TInt aState)
       
  3041     {
       
  3042 	if (aState == ESASyncStateConfirm)
       
  3043 		{
       
  3044 		Profile().SetSanUserInteractionL(ESmlConfirmSync);
       
  3045 		}
       
  3046 	else if (aState == ESASyncStateDisable)
       
  3047 		{
       
  3048 		Profile().SetSanUserInteractionL(ESmlDisableSync);
       
  3049 		}
       
  3050 	else
       
  3051 		{
       
  3052 		Profile().SetSanUserInteractionL(ESmlEnableSync);
       
  3053 		}
       
  3054     }
       
  3055 
       
  3056 
       
  3057 // -----------------------------------------------------------------------------
       
  3058 // CAspProfile::SetHttpUsedL
       
  3059 //
       
  3060 // -----------------------------------------------------------------------------
       
  3061 //
       
  3062 void CAspProfile::SetHttpUsedL(TBool aEnable)
       
  3063 	{
       
  3064     if (!iConnectionOpen)	
       
  3065     	{
       
  3066     	return;
       
  3067     	}
       
  3068 
       
  3069 	if (iConnection.Identifier() != KUidNSmlMediumTypeInternet.iUid)
       
  3070 		{
       
  3071 		return; // this setting is only for internet bearer
       
  3072 		}
       
  3073 	
       
  3074 	TBuf8<KBufSize32> key;
       
  3075 	TBuf8<KBufSize32> value;
       
  3076 	
       
  3077 	if (aEnable)
       
  3078 		{
       
  3079 		value.Num(1); 
       
  3080 		}
       
  3081 	else
       
  3082 		{
       
  3083 		value.Num(0);
       
  3084 		}
       
  3085 		
       
  3086 	GetConnectionPropertyNameL(key, EPropertyHttpUsed);
       
  3087 	iConnection.SetPropertyL(key, value);
       
  3088 	}
       
  3089 	
       
  3090 	
       
  3091 // -----------------------------------------------------------------------------
       
  3092 // CAspProfile::HttpUsedL
       
  3093 //
       
  3094 // -----------------------------------------------------------------------------
       
  3095 //
       
  3096 TBool CAspProfile::HttpUsedL()
       
  3097 	{
       
  3098     if (!iConnectionOpen)	
       
  3099     	{
       
  3100     	return EFalse;
       
  3101     	}
       
  3102 	
       
  3103 	if (iConnection.Identifier() != KUidNSmlMediumTypeInternet.iUid)
       
  3104 		{
       
  3105 		return EFalse; // this setting is only for internet bearer
       
  3106 		}
       
  3107 
       
  3108 	TBuf8<KBufSize32> key; 
       
  3109 	TBuf<KBufSize32> value;
       
  3110 	TInt intValue;
       
  3111 	
       
  3112 	GetConnectionPropertyNameL(key, EPropertyHttpUsed);
       
  3113 	TUtil::StrCopy(value, iConnection.GetPropertyL(key));  // convert TDes8 to TDes
       
  3114 	
       
  3115     User::LeaveIfError(TUtil::StrToInt(value, intValue));
       
  3116 	
       
  3117     if (intValue == 0)
       
  3118     	{
       
  3119     	return EFalse; 
       
  3120     	}
       
  3121     return ETrue;
       
  3122 	}
       
  3123 	
       
  3124 
       
  3125 // -----------------------------------------------------------------------------
       
  3126 // CAspProfile::GetHttpUsernameL
       
  3127 //
       
  3128 // -----------------------------------------------------------------------------
       
  3129 //
       
  3130 void CAspProfile::GetHttpUserNameL(TDes& aText)
       
  3131 	{
       
  3132 	aText = KNullDesC;
       
  3133 	
       
  3134     if (!iConnectionOpen)	
       
  3135     	{
       
  3136     	return;
       
  3137     	}
       
  3138 
       
  3139 	if (iConnection.Identifier() != KUidNSmlMediumTypeInternet.iUid)
       
  3140 		{
       
  3141 		return; // this setting is only for internet bearer
       
  3142 		}
       
  3143 	
       
  3144 
       
  3145 	TBuf8<KBufSize32> key; 
       
  3146 	
       
  3147 	GetConnectionPropertyNameL(key, EPropertyHttpUserName);
       
  3148 	TUtil::StrCopy(aText, iConnection.GetPropertyL(key));  // convert TDes8 to TDes
       
  3149   	}
       
  3150 	
       
  3151 
       
  3152 // -----------------------------------------------------------------------------
       
  3153 // CAspProfile::SetHttpUsernameL
       
  3154 //
       
  3155 // -----------------------------------------------------------------------------
       
  3156 //
       
  3157 void CAspProfile::SetHttpUserNameL(const TDesC& aText)
       
  3158 	{
       
  3159     if (!iConnectionOpen)	
       
  3160     	{
       
  3161     	return;
       
  3162     	}
       
  3163 	
       
  3164 	if (iConnection.Identifier() != KUidNSmlMediumTypeInternet.iUid)
       
  3165 		{
       
  3166 		return; // this setting is only for internet bearer
       
  3167 		}
       
  3168 
       
  3169 	TBuf8<KBufSize32> key;
       
  3170 	
       
  3171 	GetConnectionPropertyNameL(key, EPropertyHttpUserName);
       
  3172 	TUtil::StrCopy(iBuf8, aText);  // convert TDes to TDes8
       
  3173 	iConnection.SetPropertyL(key, iBuf8);
       
  3174 	}
       
  3175 
       
  3176 
       
  3177 // -----------------------------------------------------------------------------
       
  3178 // CAspProfile::GetHttpPasswordL
       
  3179 //
       
  3180 // -----------------------------------------------------------------------------
       
  3181 //
       
  3182 void CAspProfile::GetHttpPasswordL(TDes& aText)
       
  3183 	{
       
  3184    	aText = KNullDesC;
       
  3185    	
       
  3186     if (!iConnectionOpen)	
       
  3187     	{
       
  3188     	return;
       
  3189     	}
       
  3190 
       
  3191 	if (iConnection.Identifier() != KUidNSmlMediumTypeInternet.iUid)
       
  3192 		{
       
  3193 		return; // this setting is only for internet bearer
       
  3194 		}
       
  3195 
       
  3196 
       
  3197 	TBuf8<KBufSize32> key; 
       
  3198 	
       
  3199 	GetConnectionPropertyNameL(key, EPropertyHttpPassword);
       
  3200 	TUtil::StrCopy(aText, iConnection.GetPropertyL(key));  // convert TDes8 to TDes
       
  3201 	}
       
  3202 	
       
  3203 
       
  3204 // -----------------------------------------------------------------------------
       
  3205 // CAspProfile::SetHttpPasswordL
       
  3206 //
       
  3207 // -----------------------------------------------------------------------------
       
  3208 //
       
  3209 void CAspProfile::SetHttpPasswordL(const TDesC& aText)
       
  3210 	{
       
  3211     if (!iConnectionOpen)	
       
  3212     	{
       
  3213     	return;
       
  3214     	}
       
  3215 	
       
  3216 	if (iConnection.Identifier() != KUidNSmlMediumTypeInternet.iUid)
       
  3217 		{
       
  3218 		return; // this setting is only for internet bearer
       
  3219 		}
       
  3220     		
       
  3221 	TBuf8<KBufSize32> key;
       
  3222 	
       
  3223 	GetConnectionPropertyNameL(key, EPropertyHttpPassword);
       
  3224 	TUtil::StrCopy(iBuf8, aText);  // convert TDes to TDes8
       
  3225 	iConnection.SetPropertyL(key, iBuf8);
       
  3226 	}
       
  3227 
       
  3228 
       
  3229 // -----------------------------------------------------------------------------
       
  3230 // CAspProfile::Profile
       
  3231 //
       
  3232 // -----------------------------------------------------------------------------
       
  3233 //
       
  3234 RSyncMLDataSyncProfile& CAspProfile::Profile()
       
  3235     {
       
  3236     return iProfile;
       
  3237     }
       
  3238 
       
  3239 
       
  3240 // -----------------------------------------------------------------------------
       
  3241 // CAspProfile::LatestHistoryJob
       
  3242 //
       
  3243 // -----------------------------------------------------------------------------
       
  3244 //
       
  3245 const CSyncMLHistoryJob* CAspProfile::LatestHistoryJob()
       
  3246     {
       
  3247     if (!iHistoryLogOpen)
       
  3248     	{
       
  3249     	return NULL;
       
  3250     	}
       
  3251 
       
  3252     TInt count = iHistoryLog.Count();
       
  3253     if (count == 0)
       
  3254         {
       
  3255         return NULL;  // profile has no history job
       
  3256         }
       
  3257         
       
  3258     iHistoryLog.SortEntries(CSyncMLHistoryEntry::ESortByTime);    
       
  3259     
       
  3260     // try to find latest sync job (start from last array entry)
       
  3261     for (TInt i=count-1; i>=0; i--)
       
  3262     	{
       
  3263         const CSyncMLHistoryEntry& entry = iHistoryLog[i];
       
  3264         const CSyncMLHistoryJob* jobEntry = CSyncMLHistoryJob::DynamicCast(&entry);
       
  3265         if (jobEntry)
       
  3266         	{
       
  3267         	return jobEntry;
       
  3268         	}
       
  3269     	}
       
  3270 
       
  3271     return NULL;  // profile has no history job
       
  3272     }
       
  3273 
       
  3274 
       
  3275 // -----------------------------------------------------------------------------
       
  3276 // CAspProfile::LatestHistoryJob
       
  3277 //
       
  3278 // -----------------------------------------------------------------------------
       
  3279 //
       
  3280 const CSyncMLHistoryJob* CAspProfile::LatestHistoryJob(TInt aTaskId)
       
  3281     {
       
  3282     if (!iHistoryLogOpen)
       
  3283     	{
       
  3284     	return NULL;
       
  3285     	}
       
  3286 
       
  3287     TInt count = iHistoryLog.Count();
       
  3288     if (count == 0)
       
  3289         {
       
  3290         return NULL;  // profile has no history job
       
  3291         }
       
  3292         
       
  3293     iHistoryLog.SortEntries(CSyncMLHistoryEntry::ESortByTime);    
       
  3294     
       
  3295     // try to find latest sync job (start from last array entry)
       
  3296     for (TInt i=count-1; i>=0; i--)
       
  3297     	{
       
  3298         const CSyncMLHistoryEntry& entry = iHistoryLog[i];
       
  3299         const CSyncMLHistoryJob* jobEntry = CSyncMLHistoryJob::DynamicCast(&entry);
       
  3300         if (jobEntry)
       
  3301         	{
       
  3302         	if (TaskExist(jobEntry, aTaskId))
       
  3303         		{
       
  3304         		return jobEntry;
       
  3305         		}
       
  3306         	}
       
  3307     	}
       
  3308 
       
  3309     return NULL;
       
  3310     }
       
  3311 
       
  3312 // -----------------------------------------------------------------------------
       
  3313 // CAspProfile::DeleteHistory
       
  3314 //
       
  3315 // -----------------------------------------------------------------------------
       
  3316 //
       
  3317 void CAspProfile::DeleteHistory()
       
  3318 	{
       
  3319 	if (iHistoryLogOpen)
       
  3320     	{
       
  3321     	iHistoryLog.DeleteAllEntriesL();	
       
  3322     	}  
       
  3323     	
       
  3324 	}
       
  3325 
       
  3326 
       
  3327 // -----------------------------------------------------------------------------
       
  3328 // CAspProfile::TaskExist
       
  3329 // 
       
  3330 // -----------------------------------------------------------------------------
       
  3331 TBool CAspProfile::TaskExist(const CSyncMLHistoryJob* aHistoryJob, TInt aTaskId)
       
  3332 	{
       
  3333 	TInt taskCount = aHistoryJob->TaskCount();
       
  3334 	for (TInt i=0; i<taskCount; i++)
       
  3335 		{
       
  3336 		const CSyncMLHistoryJob::TTaskInfo& taskInfo = aHistoryJob->TaskAt(i);
       
  3337         
       
  3338         if (taskInfo.iTaskId == aTaskId)
       
  3339         	{
       
  3340         	return ETrue;
       
  3341         	}
       
  3342 		}
       
  3343 		
       
  3344 	return EFalse;
       
  3345 	}
       
  3346 
       
  3347 
       
  3348 
       
  3349 /* 
       
  3350 // -----------------------------------------------------------------------------
       
  3351 // CAspProfile::HistoryJobsLC (debugging code)
       
  3352 //
       
  3353 // -----------------------------------------------------------------------------
       
  3354 //
       
  3355 HBufC* CAspProfile::HistoryJobsLC()
       
  3356     {
       
  3357    	HBufC* hBuf = HBufC::NewLC(1024);
       
  3358 	TPtr ptr = hBuf->Des();
       
  3359 
       
  3360     if (!iHistoryLogOpen)
       
  3361     	{
       
  3362     	return hBuf;
       
  3363     	}
       
  3364 
       
  3365     TInt count = iHistoryLog.Count();
       
  3366     if (count == 0)
       
  3367         {
       
  3368         return hBuf;  // profile has no history job
       
  3369         }
       
  3370         
       
  3371     // sort array
       
  3372     iHistoryLog.SortEntries(CSyncMLHistoryEntry::ESortByTime);    
       
  3373     
       
  3374     // try to find latest sync job (start from last array entry)
       
  3375     for (TInt i=count-1; i>=0; i--)
       
  3376     //for (TInt i=0; i<count; i++)
       
  3377     	{
       
  3378         const CSyncMLHistoryEntry& entry = iHistoryLog[i];
       
  3379         const CSyncMLHistoryJob* jobEntry = CSyncMLHistoryJob::DynamicCast(&entry);
       
  3380         if (jobEntry)
       
  3381         	{
       
  3382         	TTime t = jobEntry->TimeStamp();
       
  3383         	TBuf<128> buf;
       
  3384         	TUtil::GetDateTimeTextL(buf, t);
       
  3385         	ptr.Append(buf);
       
  3386         	ptr.Append(_L("\n"));
       
  3387         	}
       
  3388     	}
       
  3389 
       
  3390     return hBuf;
       
  3391     }
       
  3392 */
       
  3393 
       
  3394 
       
  3395 // -----------------------------------------------------------------------------
       
  3396 // CAspProfile::OpenHistoryLog
       
  3397 //
       
  3398 // -----------------------------------------------------------------------------
       
  3399 //
       
  3400 void CAspProfile::OpenHistoryLog()
       
  3401     {
       
  3402     TInt id = ProfileId();
       
  3403    	TRAPD(err, iHistoryLog.OpenL(Session(), id));
       
  3404    	if (err == KErrNone)
       
  3405    		{
       
  3406    		iHistoryLogOpen = ETrue;
       
  3407     	}
       
  3408     }
       
  3409 
       
  3410 
       
  3411 // -----------------------------------------------------------------------------
       
  3412 // CAspProfile::OpenConnection
       
  3413 //
       
  3414 // -----------------------------------------------------------------------------
       
  3415 //
       
  3416 void CAspProfile::OpenConnection()
       
  3417     {
       
  3418    	TInt err = KErrNone;
       
  3419    	RArray<TSmlTransportId> arr;
       
  3420    	
       
  3421     TRAP(err, Profile().ListConnectionsL(arr));
       
  3422     if (err != KErrNone)
       
  3423     	{
       
  3424     	return; 
       
  3425     	}
       
  3426     if (arr.Count() == 0)
       
  3427     	{
       
  3428      	arr.Close();  
       
  3429     	return; // no connection
       
  3430     	}
       
  3431         	
       
  3432     TInt transportId = arr[0];
       
  3433     arr.Close();
       
  3434     	
       
  3435     TRAP(err, iConnection.OpenL(iProfile, transportId));
       
  3436     if (err == KErrNone)
       
  3437     	{
       
  3438     	iConnectionOpen = ETrue;
       
  3439     	}
       
  3440     }
       
  3441 
       
  3442 
       
  3443 // -----------------------------------------------------------------------------
       
  3444 // CAspProfile::Session
       
  3445 // 
       
  3446 // -----------------------------------------------------------------------------
       
  3447 //
       
  3448 RSyncMLSession& CAspProfile::Session()
       
  3449 	{
       
  3450 	__ASSERT_DEBUG(iSyncSession, TUtil::Panic(KErrGeneral));
       
  3451 	
       
  3452 	return *iSyncSession;
       
  3453 	}
       
  3454 
       
  3455 
       
  3456 // -----------------------------------------------------------------------------
       
  3457 // CAspProfile::ApplicationId
       
  3458 // 
       
  3459 // -----------------------------------------------------------------------------
       
  3460 //
       
  3461 TInt CAspProfile::ApplicationId()
       
  3462 	{
       
  3463 	return iApplicationId;
       
  3464 	}
       
  3465 
       
  3466 
       
  3467 // -----------------------------------------------------------------------------
       
  3468 // CAspProfile::GetConnectionPropertyNameL
       
  3469 //
       
  3470 // -----------------------------------------------------------------------------
       
  3471 //
       
  3472 void CAspProfile::GetConnectionPropertyNameL(TDes8& aText, TInt aPropertyPos)
       
  3473 	{
       
  3474 	//
       
  3475 	// at the moment RSyncMLTransport is only needed for internet connection settings
       
  3476 	//
       
  3477 	RSyncMLTransport transport;
       
  3478 	transport.OpenL(Session(), KUidNSmlMediumTypeInternet.iUid);  // no ICP call
       
  3479 	CleanupClosePushL(transport);
       
  3480 	
       
  3481 	const CSyncMLTransportPropertiesArray&  arr = transport.Properties();
       
  3482 	
       
  3483     __ASSERT_DEBUG(arr.Count()>aPropertyPos, TUtil::Panic(KErrGeneral));
       
  3484     	
       
  3485 	const TSyncMLTransportPropertyInfo& info = arr.At(aPropertyPos);
       
  3486 	aText = info.iName;
       
  3487 	
       
  3488 	CleanupStack::PopAndDestroy(&transport);
       
  3489 	}
       
  3490 
       
  3491 
       
  3492 // -----------------------------------------------------------------------------
       
  3493 // CAspProfile::CopyValuesL
       
  3494 //
       
  3495 // -----------------------------------------------------------------------------
       
  3496 //
       
  3497 void CAspProfile::CopyValuesL(CAspProfile* aSource)
       
  3498     {
       
  3499     TBuf<KBufSize255> buf;
       
  3500     TInt num = 0;
       
  3501     
       
  3502     num = aSource->ProtocolVersion();
       
  3503     SetProtocolVersionL(num);
       
  3504     if (num == EAspProtocol_1_2)
       
  3505     	{
       
  3506     	if (!IsPCSuiteProfile(aSource))
       
  3507     		{
       
  3508         	aSource->GetServerId(buf);
       
  3509         	SetServerIdL(buf);
       
  3510     		}
       
  3511     	}
       
  3512     	
       
  3513     num = aSource->BearerType();
       
  3514     SetBearerTypeL(num);
       
  3515     
       
  3516     num = aSource->AccessPointL();
       
  3517 
       
  3518 	//check if the access point is valid
       
  3519 	TAspAccessPointItem item;
       
  3520 	item.iUid = num;
       
  3521 	TAspParam param(iApplicationId, iSyncSession);
       
  3522 	CAspAccessPointHandler* apHandler = CAspAccessPointHandler::NewL(param);
       
  3523 	CleanupStack::PushL(apHandler);
       
  3524 	TInt ret = apHandler->GetInternetApInfo(item);
       
  3525 	if (ret == KErrNone)
       
  3526 	    {
       
  3527 	    SetAccessPointL(num);
       
  3528 	    }
       
  3529 	else
       
  3530 	    {
       
  3531 	    SetAccessPointL(CAspAccessPointHandler::KDefaultConnection);
       
  3532 	    }    
       
  3533     CleanupStack::PopAndDestroy(apHandler);
       
  3534 	
       
  3535     aSource->GetHostAddress(buf, num);
       
  3536     SetHostAddressL(buf, num);
       
  3537      
       
  3538     aSource->GetUserName(buf);
       
  3539     SetUserNameL(buf);
       
  3540      
       
  3541     aSource->GetPassword(buf);
       
  3542     SetPasswordL(buf);
       
  3543     
       
  3544     num = aSource->SASyncState();
       
  3545     SetSASyncStateL(num);
       
  3546     
       
  3547     if (aSource->BearerType() == EAspBearerInternet)
       
  3548     	{
       
  3549     	num = aSource->HttpUsedL();
       
  3550     	SetHttpUsedL(num);
       
  3551     	
       
  3552     	aSource->GetHttpUserNameL(buf);
       
  3553     	SetHttpUserNameL(buf);
       
  3554     	
       
  3555     	aSource->GetHttpPasswordL(buf);
       
  3556     	SetHttpPasswordL(buf);
       
  3557     	}
       
  3558     }
       
  3559 
       
  3560 
       
  3561 // -----------------------------------------------------------------------------
       
  3562 // CAspProfile::SetDefaultValuesL
       
  3563 // 
       
  3564 // -----------------------------------------------------------------------------
       
  3565 //
       
  3566 void CAspProfile::SetDefaultValuesL(CAspProfile* aProfile)
       
  3567 	{
       
  3568 	aProfile->SetSASyncStateL(ESASyncStateConfirm);
       
  3569 	aProfile->SetHttpUsedL(EFalse);
       
  3570 	aProfile->SetAccessPointL(CAspAccessPointHandler::KDefaultConnection);
       
  3571 	aProfile->SetProtocolVersionL(EAspProtocol_1_2);
       
  3572     }
       
  3573 
       
  3574 
       
  3575 // -----------------------------------------------------------------------------
       
  3576 // CAspProfile::GetNewProfileNameL
       
  3577 // 
       
  3578 // -----------------------------------------------------------------------------
       
  3579 //
       
  3580 HBufC* CAspProfile::GetNewProfileNameLC(CAspProfileList* aList, TInt aApplicationId)
       
  3581 	{
       
  3582     HBufC* hBuf = NULL;
       
  3583     
       
  3584     for (TInt i=1; i<=KMaxProfileCount; i++)
       
  3585 		{
       
  3586         hBuf = CAspResHandler::GetNewProfileNameLC(i, aApplicationId);
       
  3587 		if (aList->FindProfileIndex(hBuf->Des()) == KErrNotFound)
       
  3588 			{
       
  3589 			break;
       
  3590      		}
       
  3591      	
       
  3592      	if (i == KMaxProfileCount)
       
  3593      		{
       
  3594      		break; // too many profiles - use existing name
       
  3595      		}
       
  3596      		
       
  3597      	CleanupStack::PopAndDestroy(hBuf);
       
  3598      	hBuf = NULL;
       
  3599 		}
       
  3600 		
       
  3601 	if (!hBuf)
       
  3602 		{
       
  3603 		User::Leave(KErrNotFound);
       
  3604 		}
       
  3605 		
       
  3606 	return hBuf;
       
  3607 	}
       
  3608 
       
  3609 
       
  3610 // -----------------------------------------------------------------------------
       
  3611 // CAspProfile::CheckMandatoryDataL
       
  3612 // 
       
  3613 // -----------------------------------------------------------------------------
       
  3614 //
       
  3615 TInt CAspProfile::CheckMandatoryData(CAspProfile* aProfile, TInt& aContentCount)
       
  3616 	{
       
  3617 	aContentCount = 0;
       
  3618 	
       
  3619 	TInt ret = CheckMandatoryConnData(aProfile);
       
  3620 	if (ret != EMandatoryOk)
       
  3621 		{
       
  3622 		return ret;
       
  3623 		}
       
  3624 
       
  3625     ret = CheckMandatoryTaskData(aProfile, aContentCount);
       
  3626     
       
  3627     return ret;
       
  3628 	}
       
  3629 
       
  3630 
       
  3631 // -----------------------------------------------------------------------------
       
  3632 // CAspProfile::CheckMandatoryConnData
       
  3633 //
       
  3634 // -----------------------------------------------------------------------------
       
  3635 //
       
  3636 TInt CAspProfile::CheckMandatoryConnData(CAspProfile* aProfile)
       
  3637 	{
       
  3638 	const TInt KMaxHostAddressLength = KAspMaxURILength + 5;
       
  3639 	
       
  3640 	TBuf<KMaxHostAddressLength> buf;
       
  3641 	TInt num;
       
  3642 
       
  3643 	aProfile->GetName(buf);
       
  3644 	if (TUtil::IsEmpty(buf))
       
  3645 		{
       
  3646 		return EMandatoryNoProfileName;
       
  3647 		}
       
  3648 
       
  3649 	aProfile->GetHostAddress(buf, num);
       
  3650 	if (TUtil::IsEmpty(buf))
       
  3651 		{
       
  3652 		return EMandatoryNoHostAddress;
       
  3653 		}
       
  3654 
       
  3655     return EMandatoryOk;
       
  3656 	}
       
  3657 
       
  3658 
       
  3659 // -----------------------------------------------------------------------------
       
  3660 // CAspProfile::CheckMandatoryTaskDataL
       
  3661 // 
       
  3662 // -----------------------------------------------------------------------------
       
  3663 //
       
  3664 TInt CAspProfile::CheckMandatoryTaskDataL(CAspProfile* aProfile, TInt& aContentCount)
       
  3665 	{
       
  3666     TAspParam param(aProfile->ApplicationId(), &(aProfile->Session()));
       
  3667 	param.iProfile = aProfile;
       
  3668 	param.iMode = CAspContentList::EInitTasks;
       
  3669 	
       
  3670 	CAspContentList* list = CAspContentList::NewLC(param);
       
  3671     
       
  3672     TInt ret = list->CheckMandatoryDataL(aContentCount);
       
  3673 	CleanupStack::PopAndDestroy(list);
       
  3674 	
       
  3675 	return ret;
       
  3676 	}
       
  3677 
       
  3678 
       
  3679 // -----------------------------------------------------------------------------
       
  3680 // CAspProfile::CheckMandatoryTaskData
       
  3681 // 
       
  3682 // -----------------------------------------------------------------------------
       
  3683 //
       
  3684 TInt CAspProfile::CheckMandatoryTaskData(CAspProfile* aProfile, TInt& aContentCount)
       
  3685 	{
       
  3686 	TInt ret = EMandatoryNoContent;
       
  3687 	
       
  3688 	TRAP_IGNORE(ret = CheckMandatoryTaskDataL(aProfile, aContentCount));
       
  3689 	
       
  3690 	return ret;
       
  3691 	}
       
  3692 
       
  3693 
       
  3694 // -----------------------------------------------------------------------------
       
  3695 // CAspProfile::OtherSyncRunning
       
  3696 //
       
  3697 // -----------------------------------------------------------------------------
       
  3698 //
       
  3699 TBool CAspProfile::OtherSyncRunning(RSyncMLSession* aSyncSession)
       
  3700 	{
       
  3701 	TInt currentJob = CurrentJob(aSyncSession);
       
  3702 
       
  3703     if (currentJob != KErrNotFound)
       
  3704 		{
       
  3705 		return ETrue;
       
  3706 		}
       
  3707 	
       
  3708 	return EFalse;
       
  3709 	}
       
  3710 
       
  3711 
       
  3712 // -----------------------------------------------------------------------------
       
  3713 // CAspProfile::CurrentJob
       
  3714 //
       
  3715 // -----------------------------------------------------------------------------
       
  3716 //
       
  3717 TInt CAspProfile::CurrentJob(RSyncMLSession* aSyncSession)
       
  3718 	{
       
  3719 	TInt currentJob = KErrNotFound;
       
  3720 	TSmlUsageType usageType = ESmlDataSync;
       
  3721 	TRAP_IGNORE(aSyncSession->CurrentJobL(currentJob, usageType));
       
  3722 		
       
  3723 	if (currentJob >= 0)
       
  3724 		{
       
  3725 		return currentJob;	
       
  3726 		}
       
  3727 	return KErrNotFound;
       
  3728 	}
       
  3729 
       
  3730 
       
  3731 //-----------------------------------------------------------------------------
       
  3732 // CAspProfile:::IsPCSuiteProfile
       
  3733 // 
       
  3734 //-----------------------------------------------------------------------------
       
  3735 //
       
  3736 TBool CAspProfile::IsPCSuiteProfile(CAspProfile* aProfile)
       
  3737 	{
       
  3738 	_LIT(KPCSuite, "pc suite");
       
  3739 
       
  3740 	if (aProfile->DeleteAllowed())
       
  3741 		{
       
  3742 		return EFalse; // this is not "PC Suite" profile 
       
  3743 		}
       
  3744 	
       
  3745 	TInt num = KErrNotFound;
       
  3746 	TBuf<KBufSize64> buf;
       
  3747 	aProfile->GetHostAddress(buf, num);
       
  3748 	buf.LowerCase();
       
  3749 	
       
  3750 	if (buf.Compare(KPCSuite) == 0)
       
  3751 		{
       
  3752 		return ETrue;
       
  3753 		}
       
  3754 		
       
  3755 	return EFalse;
       
  3756 	}
       
  3757 
       
  3758 // -----------------------------------------------------------------------------
       
  3759 // CAspProfile::GetLocalisedPCSuite
       
  3760 //
       
  3761 // Returns "PC Suite" string localised to specific language
       
  3762 // 
       
  3763 // Current requirement is to localise to 2 languages TaiwanChinese and ELangHongKongChinese
       
  3764 // For all the rest it will be mapped to "PC suite" only
       
  3765 // -----------------------------------------------------------------------------
       
  3766 //
       
  3767 void CAspProfile::GetLocalisedPCSuite(TDes& aText)
       
  3768 {
       
  3769  		TLanguage language = User::Language();
       
  3770          
       
  3771  		switch(language)
       
  3772  		{
       
  3773  			case ELangTaiwanChinese:
       
  3774             case ELangHongKongChinese:
       
  3775  			case ELangEnglish_HongKong:
       
  3776  			case ELangPrcChinese:
       
  3777  			case ELangEnglish_Taiwan:
       
  3778 			case ELangEnglish_Prc:
       
  3779  			case ELangMalay_Apac:
       
  3780  			case ELangIndon_Apac: 
       
  3781             {
       
  3782             	HBufC* hBuf = CAspResHandler::ReadLC(R_ASP_PC_SUITE_PROFILE);
       
  3783             	TUtil::StrCopy(aText,*hBuf);
       
  3784             	CleanupStack::PopAndDestroy(hBuf);
       
  3785             	return;
       
  3786             }
       
  3787             default:
       
  3788             {
       
  3789             	TUtil::StrCopy(aText, iProfile.DisplayName());
       
  3790             	return;
       
  3791             }
       
  3792  		}
       
  3793 }
       
  3794 
       
  3795 
       
  3796 
       
  3797 /*******************************************************************************
       
  3798  * class TAspTask
       
  3799  *******************************************************************************/
       
  3800 
       
  3801 
       
  3802 
       
  3803 // -----------------------------------------------------------------------------
       
  3804 // TAspTask::SmlSyncDirection
       
  3805 //
       
  3806 // -----------------------------------------------------------------------------
       
  3807 //
       
  3808 TSmlSyncType TAspTask::SmlSyncDirection(TInt aSyncDirection)
       
  3809 	{
       
  3810 	TSmlSyncType type = ESmlTwoWay;
       
  3811 	
       
  3812 	if (aSyncDirection == ESyncDirectionOneWayFromDevice)
       
  3813 		{
       
  3814 		type = ESmlOneWayFromClient;
       
  3815 		}
       
  3816 	if (aSyncDirection == ESyncDirectionOneWayIntoDevice)
       
  3817 		{
       
  3818 		type = ESmlOneWayFromServer;
       
  3819 		}
       
  3820 	if (aSyncDirection == ESyncDirectionRefreshFromServer)
       
  3821 		{
       
  3822 		type = ESmlRefreshFromServer;
       
  3823 		}
       
  3824 		
       
  3825 	return type;
       
  3826 	}
       
  3827 
       
  3828 
       
  3829 // -----------------------------------------------------------------------------
       
  3830 // TAspTask::SmlSyncDirection
       
  3831 //
       
  3832 // -----------------------------------------------------------------------------
       
  3833 //
       
  3834 TInt TAspTask::SyncDirection(TSmlSyncType aSmlSyncDirection)
       
  3835     {
       
  3836 	TInt type = ESyncDirectionTwoWay;
       
  3837 	
       
  3838 	if (aSmlSyncDirection == ESmlOneWayFromClient)
       
  3839 		{
       
  3840 		type = ESyncDirectionOneWayFromDevice;
       
  3841 		}
       
  3842 	if (aSmlSyncDirection == ESmlOneWayFromServer)
       
  3843 		{
       
  3844 		type = ESyncDirectionOneWayIntoDevice;
       
  3845 		}
       
  3846 	if (aSmlSyncDirection == ESmlRefreshFromServer)
       
  3847 		{
       
  3848 		type = ESyncDirectionRefreshFromServer;
       
  3849 		}
       
  3850 		
       
  3851 
       
  3852 	return type;
       
  3853 	}
       
  3854 
       
  3855 
       
  3856 // -----------------------------------------------------------------------------
       
  3857 // TAspTask::FindTaskIdL
       
  3858 //
       
  3859 // -----------------------------------------------------------------------------
       
  3860 //
       
  3861 
       
  3862 TInt TAspTask::FindTaskIdL(CAspProfile* aProfile, TInt aDataProviderId, const TDesC& aLocalDatabase)
       
  3863 	{
       
  3864 	TInt ret = KErrNotFound;
       
  3865 	
       
  3866 	RArray<TSmlTaskId> arr;
       
  3867 	aProfile->Profile().ListTasksL(arr); // IPC call
       
  3868 	CleanupClosePushL(arr);
       
  3869 	
       
  3870 	TInt count = arr.Count();
       
  3871 	for (TInt i=0; i<count; i++)
       
  3872 		{
       
  3873 		TInt taskId = arr[i];
       
  3874 	    RSyncMLTask task;
       
  3875     	task.OpenL(aProfile->Profile(), taskId);  // IPC call
       
  3876 		
       
  3877 	    if (aDataProviderId != task.DataProvider())
       
  3878 	    	{
       
  3879 	    	task.Close();
       
  3880 	    	continue; // wrong data provider
       
  3881 	    	}
       
  3882     	if (!task.Enabled())
       
  3883     		{
       
  3884     		task.Close();
       
  3885     		continue; // task not in use
       
  3886     		}
       
  3887 
       
  3888 	    if (TUtil::IsEmpty(aLocalDatabase))
       
  3889 	    	{
       
  3890 	    	ret = taskId;  // parameter aLocalDatabase is empty - no db name check
       
  3891 	    	}
       
  3892 	    else
       
  3893 	    	{
       
  3894 	    	TPtrC ptr = task.ClientDataSource();
       
  3895 	        if (ptr.Compare(aLocalDatabase) == 0)
       
  3896 	    	    {
       
  3897 	    	    ret = taskId;
       
  3898 	    	    }
       
  3899 	    	}
       
  3900 	
       
  3901 	    task.Close();
       
  3902 		
       
  3903 		if (ret != KErrNotFound)
       
  3904 			{
       
  3905 			break;
       
  3906 			}
       
  3907 		}
       
  3908 		
       
  3909     CleanupStack::PopAndDestroy(&arr);		
       
  3910     
       
  3911     return ret;
       
  3912 	}
       
  3913 
       
  3914 
       
  3915 // -----------------------------------------------------------------------------
       
  3916 // TAspTask::CheckLocalDatabase
       
  3917 // 
       
  3918 // -----------------------------------------------------------------------------
       
  3919 //
       
  3920 TInt TAspTask::CheckLocalDatabase(CAspProfile* aProfile, TInt& aDataProviderId)
       
  3921 	{
       
  3922 	TBool ret = ETrue;
       
  3923 	
       
  3924 	TRAPD(err, ret = CheckLocalDatabaseL(aProfile, aDataProviderId));
       
  3925 	if (err != KErrNone)
       
  3926 		{
       
  3927 		return ETrue; // check failed, ETrue means profile has local database
       
  3928 		}
       
  3929 		
       
  3930 	return ret;
       
  3931 	}
       
  3932 
       
  3933 
       
  3934 // -----------------------------------------------------------------------------
       
  3935 // TAspTask::CheckLocalDatabaseL
       
  3936 //
       
  3937 // -----------------------------------------------------------------------------
       
  3938 //
       
  3939 
       
  3940 TBool TAspTask::CheckLocalDatabaseL(CAspProfile* aProfile, TInt& aDataProviderId)
       
  3941 	{
       
  3942 	FLOG( _L("TAspTask::CheckLocalDatabaseL START") );
       
  3943 	
       
  3944 	TInt ret = ETrue;
       
  3945 	aDataProviderId = KErrNotFound;
       
  3946 	
       
  3947 	RArray<TSmlTaskId> arr;
       
  3948 	aProfile->Profile().ListTasksL(arr); // IPC call
       
  3949 	CleanupClosePushL(arr);
       
  3950 	
       
  3951 	TInt count = arr.Count();
       
  3952 	for (TInt i=0; i<count; i++)
       
  3953 		{
       
  3954 		// open sync task
       
  3955 		RSyncMLTask task;
       
  3956         TInt taskId = arr[i];
       
  3957     	TRAPD(err, task.OpenL(aProfile->Profile(), taskId));  // IPC call
       
  3958 	    if (err != KErrNone)
       
  3959 		    {
       
  3960 		    FTRACE( RDebug::Print(_L("### RSyncMLTask::OpenL failed (id=%d, err=%d) ###"), taskId, err) );
       
  3961 	        User::Leave(err);
       
  3962 		    }
       
  3963 		    
       
  3964 		CleanupClosePushL(task);
       
  3965 		    
       
  3966     	TInt dataProviderId = task.DataProvider();
       
  3967     	TBool enabled = task.Enabled();
       
  3968     	
       
  3969         if (dataProviderId != KUidNSmlAdapterEMail.iUid)
       
  3970         	{
       
  3971 	    	CleanupStack::PopAndDestroy(&task);
       
  3972 	    	continue;  // only email sync task is checked
       
  3973         	}
       
  3974 
       
  3975 	    if (!enabled)
       
  3976 	    	{
       
  3977 	    	CleanupStack::PopAndDestroy(&task);
       
  3978 	    	continue;  // this task is not included in sync
       
  3979 	    	}
       
  3980      	
       
  3981         
       
  3982         // open data provider
       
  3983        	RSyncMLDataProvider dataProvider;
       
  3984         TRAPD(err2, dataProvider.OpenL(aProfile->Session(), dataProviderId));
       
  3985    	    if (err2 != KErrNone)
       
  3986 		    {
       
  3987 		    FTRACE( RDebug::Print(_L("### RSyncMLDataProvider::OpenL failed (id=%xd, err=%d) ###"), dataProviderId, err2) );
       
  3988 	        User::Leave(err);
       
  3989 		    }
       
  3990 		CleanupClosePushL(dataProvider);
       
  3991         
       
  3992         TPtrC localDatabase = task.ClientDataSource();
       
  3993         
       
  3994         if (TUtil::IsEmpty(localDatabase))
       
  3995         	{
       
  3996         	TPtrC defaultDatabase = dataProvider.DefaultDataStoreName();
       
  3997         	if (TUtil::IsEmpty(defaultDatabase))
       
  3998         		{
       
  3999         		aDataProviderId = dataProviderId;
       
  4000         		ret = EFalse;
       
  4001         		}
       
  4002         	}
       
  4003         else
       
  4004         	{
       
  4005         	CDesCArrayFlat* localDataStores= new (ELeave) CDesCArrayFlat(KDefaultArraySize);
       
  4006         	CleanupStack::PushL(localDataStores);
       
  4007         	dataProvider.GetDataStoreNamesL(*localDataStores);
       
  4008         	TInt dataStorecount = localDataStores->Count();
       
  4009         	
       
  4010         	if (dataStorecount > 0)
       
  4011         		{
       
  4012 	        	TInt found = localDataStores->Find(localDatabase, count);
       
  4013         	   	if (found != 0)  // 0 means database was found
       
  4014         		    {
       
  4015         		    aDataProviderId = dataProviderId;
       
  4016         		    ret = EFalse;
       
  4017         		    }
       
  4018         		}
       
  4019         	else
       
  4020         		{
       
  4021         		aDataProviderId = dataProviderId;
       
  4022         		ret = EFalse;
       
  4023         		}
       
  4024         		
       
  4025         	CleanupStack::PopAndDestroy(localDataStores);
       
  4026         	}
       
  4027 	
       
  4028 	    CleanupStack::PopAndDestroy(&dataProvider);
       
  4029 	    CleanupStack::PopAndDestroy(&task);
       
  4030 		
       
  4031 		if (!ret)
       
  4032 			{
       
  4033 			break;
       
  4034 			}
       
  4035 		}
       
  4036 		
       
  4037     CleanupStack::PopAndDestroy(&arr);
       
  4038     
       
  4039     FLOG( _L("TAspTask::CheckLocalDatabaseL END") );
       
  4040     return ret;
       
  4041 	}
       
  4042 
       
  4043 
       
  4044 
       
  4045 
       
  4046 
       
  4047 // End of file
       
  4048