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