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