omads/omadsappui/AspSyncUtil/src/AspProfile.cpp
changeset 1 95fdac6ccb5c
parent 0 dab8a81a92de
child 2 19dc812fb587
equal deleted inserted replaced
0:dab8a81a92de 1:95fdac6ccb5c
    19 
    19 
    20 // INCLUDES
    20 // INCLUDES
    21 
    21 
    22 #include <SyncMLTransportProperties.h>
    22 #include <SyncMLTransportProperties.h>
    23 #include <featmgr.h>   // FeatureManager
    23 #include <featmgr.h>   // FeatureManager
       
    24 #include <calsession.h>
       
    25 #include <calcalendarinfo.h>
       
    26 #include <e32math.h>
       
    27 #include <gdi.h>
    24 
    28 
    25 #include "AspProfile.h"
    29 #include "AspProfile.h"
    26 #include "AspResHandler.h"
    30 #include "AspResHandler.h"
    27 #include "AspDialogUtil.h"
    31 #include "AspDialogUtil.h"
    28 #include "AspDebug.h"
    32 #include "AspDebug.h"
    29 #include "AspSchedule.h"
    33 #include "AspSchedule.h"
    30 #include <centralrepository.h> //CRepository
    34 #include <centralrepository.h> //CRepository
    31 
    35 #include <calenmulticaluids.hrh> // Calendar File Meta Data Properties
    32 
    36 #include <calenmulticalutil.h>
       
    37 #include <CalenInterimUtils2.h>
       
    38 
       
    39 _LIT(KDrive ,"C:");
       
    40 _LIT(KCharUnderscore, "_");
    33 
    41 
    34 /*******************************************************************************
    42 /*******************************************************************************
    35  * class TAspProviderItem
    43  * class TAspProviderItem
    36  *******************************************************************************/
    44  *******************************************************************************/
    37 
    45 
   831        	if (!task.iRemoteDatabaseDefined)
   839        	if (!task.iRemoteDatabaseDefined)
   832 			{
   840 			{
   833 			aContentCount = goodContentCount;
   841 			aContentCount = goodContentCount;
   834 		    return EMandatoryNoRemoteDatabase;
   842 		    return EMandatoryNoRemoteDatabase;
   835 			}
   843 			}
       
   844        	
       
   845        	if (task.iDataProviderId == KUidNSmlAdapterCalendar.iUid)
       
   846        	    {
       
   847        	    CCalSession* session = CCalSession::NewL();
       
   848        	    CleanupStack::PushL(session);
       
   849        	    TRAPD (err, session->OpenL(task.iClientDataSource));
       
   850        	    if (err == KErrNotFound)
       
   851        	        {
       
   852        	        TInt index = FindProviderIndex(task.iDataProviderId);
       
   853        	        TAspProviderItem& provider = ProviderItem(index);
       
   854        	              	        
       
   855                 TBuf<KBufSize> localDatabase;
       
   856        	        TBuf<KBufSize> remoteDatabase;
       
   857        	        TInt syncDirection;
       
   858        	        TBool taskEnabled;
       
   859        	        ReadTaskL(provider.iDataProviderId, localDatabase, remoteDatabase,
       
   860        	           taskEnabled, syncDirection);
       
   861        	        
       
   862        	        //Creating new task creates new calendar local database
       
   863                 CreateTaskL(provider.iDataProviderId, localDatabase, remoteDatabase,
       
   864                    taskEnabled, syncDirection);
       
   865                 
       
   866                 InitAllTasksL();
       
   867                 InitDataProvidersL();
       
   868        	        }
       
   869        	    CleanupStack::PopAndDestroy(session);
       
   870        	    }
   836 
   871 
   837 		goodContentCount++;
   872 		goodContentCount++;
   838 		}
   873 		}
   839 
   874 
   840     aContentCount = goodContentCount;
   875     aContentCount = goodContentCount;
  1148 		}
  1183 		}
  1149 		
  1184 		
  1150 	RSyncMLTask task;
  1185 	RSyncMLTask task;
  1151 	CleanupClosePushL(task);
  1186 	CleanupClosePushL(task);
  1152 	
  1187 	
  1153 	task.CreateL(iProfile->Profile(), aDataProvider.iDataProviderId, 
  1188 	if (aDataProvider.iDataProviderId == KUidNSmlAdapterCalendar.iUid )
       
  1189         {
       
  1190         TBuf<128> calLocalDb ;
       
  1191         CreateCalLocalDatabaseL(calLocalDb);
       
  1192         task.CreateL(iProfile->Profile(), aDataProvider.iDataProviderId, 
       
  1193                 KNullDesC, calLocalDb);
       
  1194         }
       
  1195     else
       
  1196         {
       
  1197         task.CreateL(iProfile->Profile(), aDataProvider.iDataProviderId, 
  1154 	              KNullDesC, aDataProvider.iDefaultDataStore);
  1198 	              KNullDesC, aDataProvider.iDefaultDataStore);
  1155 	
  1199         }
  1156 	task.SetEnabledL(aDataProvider.iIsIncludedInSync);
  1200 	task.SetEnabledL(aDataProvider.iIsIncludedInSync);
  1157 	task.SetDefaultSyncTypeL(SmlSyncDirection());
  1201 	task.SetDefaultSyncTypeL(SmlSyncDirection());
  1158 	
  1202 	
  1159 	// store task into database
  1203 	// store task into database
  1160 	task.UpdateL();
  1204 	task.UpdateL();
  1210 			             aRemoteDatabase, KNullDesC);
  1254 			             aRemoteDatabase, KNullDesC);
  1211 			aEnabled = EFalse;
  1255 			aEnabled = EFalse;
  1212 			}
  1256 			}
  1213 		else
  1257 		else
  1214 			{
  1258 			{
  1215 			task.CreateL(iProfile->Profile(), aDataProviderId, 
  1259 			if (aDataProviderId == KUidNSmlAdapterCalendar.iUid )
       
  1260                 {
       
  1261                 TBuf<128> calLocalDb ;
       
  1262                 CreateCalLocalDatabaseL(calLocalDb);
       
  1263                 task.CreateL(iProfile->Profile(), aDataProviderId, 
       
  1264                                               aRemoteDatabase, calLocalDb);
       
  1265                 }
       
  1266             else
       
  1267                 {
       
  1268                 task.CreateL(iProfile->Profile(), aDataProviderId, 
  1216 			             aRemoteDatabase, item.iDefaultDataStore);
  1269 			             aRemoteDatabase, item.iDefaultDataStore);
       
  1270                 }
  1217 			}
  1271 			}
  1218 		}
  1272 		}
  1219 	else
  1273 	else
  1220 		{
  1274 		{
  1221 		task.CreateL(iProfile->Profile(), aDataProviderId, 
  1275 		if (aDataProviderId == KUidNSmlAdapterCalendar.iUid )
  1222 		                                  aRemoteDatabase, aLocalDatabase);
  1276 		    {
       
  1277 		    TBuf<128> calLocalDb ;
       
  1278 		    TRAPD(err ,RetrieveCalLocalDatabaseL(calLocalDb));
       
  1279 		    if (err != KErrNone)
       
  1280 		        {
       
  1281 		        CreateCalLocalDatabaseL(calLocalDb);
       
  1282 		        }
       
  1283 		    
       
  1284 		    task.CreateL(iProfile->Profile(), aDataProviderId, 
       
  1285 		                                  aRemoteDatabase, calLocalDb);
       
  1286 		    }
       
  1287 		else
       
  1288 		    {
       
  1289 		    task.CreateL(iProfile->Profile(), aDataProviderId, 
       
  1290 		                                              aRemoteDatabase, aLocalDatabase);
       
  1291 		    }
  1223 		}
  1292 		}
  1224 	
  1293 	
  1225 	task.SetEnabledL(aEnabled);
  1294 	task.SetEnabledL(aEnabled);
  1226 	task.SetDefaultSyncTypeL(TAspTask::SmlSyncDirection(aSyncDirection));
  1295 	task.SetDefaultSyncTypeL(TAspTask::SmlSyncDirection(aSyncDirection));
  1227 	
  1296 	
  1253 		}
  1322 		}
  1254 		
  1323 		
  1255 	FLOG( _L("CAspContentList::CreateTask END") );
  1324 	FLOG( _L("CAspContentList::CreateTask END") );
  1256 	}
  1325 	}
  1257 
  1326 
       
  1327 // -----------------------------------------------------------------------------
       
  1328 // CAspContentList::CreateTask
       
  1329 // 
       
  1330 // -----------------------------------------------------------------------------
       
  1331 //
       
  1332 void CAspContentList::CreateCalLocalDatabaseL(TDes& aCalName)
       
  1333     {
       
  1334             
       
  1335     aCalName.Copy(KDrive);
       
  1336     
       
  1337     TBuf<128> buffer;
       
  1338     iProfile->GetName(buffer);
       
  1339      
       
  1340     CCalSession* calSession = CCalSession::NewL();
       
  1341     CleanupStack::PushL(calSession);
       
  1342     
       
  1343         
       
  1344     TInt suffix = 0;
       
  1345     TInt suffixLen = 0;
       
  1346     TInt delPos = buffer.Length();
       
  1347     
       
  1348     while (!IsCalNameAvailableL(*calSession ,buffer))
       
  1349         {
       
  1350         //A db with profile name already exists , try profile_1 ,profile_2 etc..
       
  1351         if (!suffix)
       
  1352             {
       
  1353             buffer.Append(KCharUnderscore);
       
  1354             ++delPos;
       
  1355             }
       
  1356         else
       
  1357             {
       
  1358             while (suffix/10)
       
  1359                 {
       
  1360                 ++suffixLen;
       
  1361                 }
       
  1362             
       
  1363             buffer.Delete(delPos ,++suffixLen);
       
  1364             suffixLen = 0;
       
  1365             }   
       
  1366         buffer.AppendNum(++suffix);
       
  1367         }
       
  1368     
       
  1369     TBuf8<128> keyBuff;
       
  1370     TUint calValue = 0;
       
  1371     CCalCalendarInfo* calinfo = CCalCalendarInfo::NewL();
       
  1372     CleanupStack::PushL(calinfo);
       
  1373     //Visibility
       
  1374     calinfo->SetEnabled(ETrue);
       
  1375     
       
  1376     calinfo->SetNameL(buffer);
       
  1377     calinfo->SetColor(Math::Random());
       
  1378     
       
  1379     // Set Meta Data Properties
       
  1380     // LUID Meta Property
       
  1381     keyBuff.Zero();
       
  1382     keyBuff.AppendNum( EFolderLUID );
       
  1383     calValue = CCalenMultiCalUtil::GetNextAvailableOffsetL();
       
  1384     FLOG(_L("CNSmlOviAgendaAdapterPlugin::CreateFolderItemL: nextoffset: '%d'"), calValue);
       
  1385     TPckgC<TUint> pckgUidValue( calValue );
       
  1386     calinfo->SetPropertyL( keyBuff, pckgUidValue );
       
  1387     
       
  1388     // Create & Modified Time Meta Property
       
  1389     keyBuff.Zero();
       
  1390     keyBuff.AppendNum( ECreationTime );
       
  1391     TTime time;
       
  1392     time.HomeTime();
       
  1393     TPckgC<TTime> pckgCreateTimeValue( time );
       
  1394     calinfo->SetPropertyL( keyBuff, pckgCreateTimeValue );
       
  1395     keyBuff.Zero();
       
  1396     keyBuff.AppendNum( EModificationTime );
       
  1397     calinfo->SetPropertyL( keyBuff, pckgCreateTimeValue );
       
  1398     
       
  1399     // Sync Status
       
  1400     keyBuff.Zero();
       
  1401     keyBuff.AppendNum( ESyncStatus );
       
  1402     TBool syncstatus( ETrue );
       
  1403     TPckgC<TBool> pckgSyncStatusValue( syncstatus );
       
  1404     calinfo->SetPropertyL( keyBuff, pckgSyncStatusValue );
       
  1405     
       
  1406     // Global UID MetaDataProperty 
       
  1407     keyBuff.Zero();
       
  1408     keyBuff.AppendNum( EGlobalUUID );
       
  1409     CCalenInterimUtils2* interimUtils = CCalenInterimUtils2::NewL();
       
  1410     CleanupStack::PushL( interimUtils );
       
  1411     HBufC8* guuid = interimUtils->CalFileGlobalUidL();
       
  1412     TPtr8 guuidPtr = guuid->Des();
       
  1413     CleanupStack::PushL( guuid );
       
  1414     calinfo->SetPropertyL( keyBuff, guuidPtr );
       
  1415     CleanupStack::PopAndDestroy( guuid );
       
  1416     CleanupStack::PopAndDestroy( interimUtils );
       
  1417           
       
  1418     // Owner Name
       
  1419     keyBuff.Zero();
       
  1420     keyBuff.AppendNum( EDeviceSyncServiceOwner );
       
  1421     TPckgC<TInt> pckgAppUIDValue( KCRUidNSmlDSApp.iUid );    
       
  1422     calinfo->SetPropertyL( keyBuff, pckgAppUIDValue );
       
  1423 
       
  1424 
       
  1425     keyBuff.Zero();
       
  1426     keyBuff.AppendNum( EDeviceSyncProfileID );
       
  1427     TPckgC<TInt> pckgProfileIdValue( iProfile->ProfileId() );    
       
  1428     calinfo->SetPropertyL( keyBuff, pckgProfileIdValue );
       
  1429 
       
  1430     
       
  1431     // Create the CalFile
       
  1432     HBufC* calfilename = CCalenMultiCalUtil::GetNextAvailableCalFileL();
       
  1433     calSession->CreateCalFileL( calfilename->Des(), *calinfo );
       
  1434 	
       
  1435 	aCalName.Copy( calfilename->Des() );
       
  1436     
       
  1437     delete calfilename;
       
  1438     
       
  1439     CleanupStack::PopAndDestroy(calinfo);
       
  1440     CleanupStack::PopAndDestroy(calSession);
       
  1441     }
       
  1442 
       
  1443 // -----------------------------------------------------------------------------
       
  1444 // CAspContentList::IsCalNameAvailableL
       
  1445 // 
       
  1446 // -----------------------------------------------------------------------------
       
  1447 //
       
  1448 TBool CAspContentList::IsCalNameAvailableL(CCalSession& aSession ,TDes& aCalName)
       
  1449     {
       
  1450     CCalSession* vCalSubSession = NULL;  
       
  1451     CDesCArray* calfilearr = aSession.ListCalFilesL();
       
  1452     
       
  1453     for(TInt i = 0; i < calfilearr->Count(); i++)
       
  1454         {
       
  1455         vCalSubSession = CCalSession::NewL(aSession);
       
  1456         CleanupStack::PushL(vCalSubSession);
       
  1457         vCalSubSession->OpenL(calfilearr->MdcaPoint(i));
       
  1458     
       
  1459         CCalCalendarInfo* caleninfo = vCalSubSession->CalendarInfoL(); 
       
  1460         
       
  1461         if (aCalName == caleninfo->NameL())
       
  1462             {
       
  1463             delete caleninfo;
       
  1464             delete calfilearr;
       
  1465             CleanupStack::PopAndDestroy(vCalSubSession);
       
  1466             return EFalse;
       
  1467             }
       
  1468     
       
  1469         delete caleninfo;    
       
  1470         CleanupStack::PopAndDestroy(vCalSubSession); 
       
  1471         }
       
  1472     delete calfilearr;
       
  1473     return ETrue;
       
  1474     
       
  1475     }
       
  1476 // -----------------------------------------------------------------------------
       
  1477 // CAspContentList::RetrieveCalLocalDatabaseL
       
  1478 // 
       
  1479 // -----------------------------------------------------------------------------
       
  1480 //
       
  1481 void CAspContentList::RetrieveCalLocalDatabaseL(TDes& aCalName)
       
  1482     {
       
  1483       
       
  1484     FLOG(_L("CAspContentList::RetrieveCalLocalDatabaseL: BEGIN"));   
       
  1485       
       
  1486     TBuf8<128> keyBuff;
       
  1487     CCalSession* vCalSession = NULL;
       
  1488     CCalSession* vCalSubSession = NULL;   
       
  1489     
       
  1490     vCalSession = CCalSession::NewL();
       
  1491     CleanupStack::PushL(vCalSession);
       
  1492     
       
  1493     CDesCArray* calfilearr = vCalSession->ListCalFilesL();            
       
  1494     
       
  1495     TBool dbFound = EFalse;
       
  1496     for(TInt i = 0; i < calfilearr->Count(); i++)
       
  1497         {
       
  1498         vCalSubSession = CCalSession::NewL(*vCalSession);
       
  1499         CleanupStack::PushL(vCalSubSession);
       
  1500         vCalSubSession->OpenL(calfilearr->MdcaPoint(i));
       
  1501         
       
  1502         CCalCalendarInfo* caleninfo = vCalSubSession->CalendarInfoL(); 
       
  1503         CleanupStack::PushL(caleninfo);
       
  1504         
       
  1505         TInt profileId;
       
  1506         keyBuff.Zero();
       
  1507         keyBuff.AppendNum( EDeviceSyncProfileID );
       
  1508         TPckgC<TInt> intBuf(profileId);
       
  1509         TRAP_IGNORE(intBuf.Set(caleninfo->PropertyValueL(keyBuff)));
       
  1510                 
       
  1511         profileId = intBuf();
       
  1512         
       
  1513         if ( profileId == iProfile->ProfileId())
       
  1514             {
       
  1515             aCalName.Append(caleninfo->FileNameL());
       
  1516             dbFound = ETrue;
       
  1517             CleanupStack::PopAndDestroy(caleninfo);   
       
  1518             CleanupStack::PopAndDestroy(vCalSubSession);
       
  1519             break;
       
  1520             }
       
  1521         CleanupStack::PopAndDestroy(caleninfo);    
       
  1522         CleanupStack::PopAndDestroy(vCalSubSession); 
       
  1523         }
       
  1524 		
       
  1525 		if( dbFound == EFalse )
       
  1526 			{
       
  1527 			delete calfilearr; 
       
  1528 			User::Leave( KErrNotFound );
       
  1529 			}
       
  1530  
       
  1531     delete calfilearr;     
       
  1532     CleanupStack::PopAndDestroy(vCalSession);
       
  1533     }
  1258 
  1534 
  1259 // -----------------------------------------------------------------------------
  1535 // -----------------------------------------------------------------------------
  1260 // CAspContentList::ReadTaskL
  1536 // CAspContentList::ReadTaskL
  1261 // 
  1537 // 
  1262 // -----------------------------------------------------------------------------
  1538 // -----------------------------------------------------------------------------