connectivitymodules/SeCon/services/csc/src/sconsyncservice.cpp
branchRCL_3
changeset 19 0aa8cc770c8a
parent 18 453dfc402455
child 20 4a793f564d72
equal deleted inserted replaced
18:453dfc402455 19:0aa8cc770c8a
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  CSConSyncService implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // Includes
       
    20 #include <centralrepository.h>
       
    21 #include <seconsdkcrkeys.h>
       
    22 
       
    23 #include <swi/sisregistryentry.h>   // for searching app name from installer
       
    24 #include <swi/sisregistrypackage.h>
       
    25 #include <swi/sisregistrysession.h>
       
    26 #include <javaregistryincludes.h>
       
    27 using namespace Java;
       
    28 
       
    29 #include "sconsyncservice.h"
       
    30 #include "capability.h"
       
    31 #include "debug.h"
       
    32 
       
    33 // Constants
       
    34 const TInt KDsAppUi = 0x101F6DE4; // NSmlDSSync.exe
       
    35 const TSmlDataProviderId KCalendarId = 0x101f6dde;
       
    36 const TSmlDataProviderId KContactsId = 0x101f6ddd;
       
    37 const TSmlDataProviderId KSmsId = 0x10206b5c;
       
    38 const TSmlDataProviderId KBookmarkId = 0x102751ba;
       
    39 
       
    40 const TInt KDateFormatLength(16);
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CSConSyncService::NewLC()
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CSConSyncService* CSConSyncService::NewLC()
       
    47 	{
       
    48 	TRACE_FUNC_ENTRY;
       
    49 	CSConSyncService* self = new (ELeave) CSConSyncService();
       
    50 	CleanupStack::PushL( self );
       
    51 	self->ConstructL();
       
    52 	TRACE_FUNC_EXIT;
       
    53 	return self;
       
    54 	}
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CSConSyncService::CSConSyncService()
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CSConSyncService::CSConSyncService()
       
    61 	{
       
    62 	}
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CSConSyncService::~CSConSyncService()
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 CSConSyncService::~CSConSyncService()
       
    69 	{
       
    70 	TRACE_FUNC_ENTRY;
       
    71 	if ( iApaSessionConnected )
       
    72 		{
       
    73 		iApaSession.Close();
       
    74 		}
       
    75 	if ( iSyncSessionOpened )
       
    76 		{
       
    77 		iSyncSession.Close();
       
    78 		}
       
    79 	delete iRepository;
       
    80 	TRACE_FUNC_EXIT;
       
    81 	}
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CSConSyncService::ConstructL()
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void CSConSyncService::ConstructL()
       
    88 	{
       
    89 	iRepository = CRepository::NewL( KCRUidSecon );
       
    90 	}
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CSConSyncService::GetSolutionL()
       
    94 // Populate SolutionInfo according to content type.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CSConSyncService::GetSolutionL( const TUint aSeconContentType,
       
    98 		TSConSolutionInfo& aSolutionInfo )
       
    99 	{
       
   100 	TRACE_FUNC_ENTRY;
       
   101 	TInt appUid;
       
   102 	User::LeaveIfError( iRepository->Get( aSeconContentType, appUid ));
       
   103     LOGGER_WRITE_2( "repository readed ok, uid: %d, hex:%x", appUid, appUid );
       
   104     
       
   105     aSolutionInfo.iUid = appUid;
       
   106     if ( aSolutionInfo.iUid == KDsAppUi )
       
   107         {
       
   108         // read profile name
       
   109         LOGGER_WRITE( "Search correct profile" );
       
   110         GetLastUsedProfileNameL( aSolutionInfo.iSolutionName,
       
   111         	aSolutionInfo.iTime, ProviderId( aSeconContentType ) );
       
   112         }
       
   113     else if ( aSolutionInfo.iUid != 0 )
       
   114         {
       
   115         LOGGER_WRITE( "Search correct application name" );
       
   116         TUid appUid = {aSolutionInfo.iUid};
       
   117         GetApplicationCaptionL( aSolutionInfo.iSolutionName, appUid );
       
   118         
       
   119         // get solution timestamp
       
   120         TBuf<KDateFormatLength> timeBuffer;
       
   121         // Timestamp is stored always in next to Uid value on cenrep
       
   122         TInt err = iRepository->Get( aSeconContentType+1, timeBuffer );
       
   123         
       
   124         LOGGER_WRITE_1( "iRepository->Get(time) err: %d", err );
       
   125         if ( err == KErrNone )
       
   126         	{
       
   127 	        err = ParseTimestamp( aSolutionInfo.iTime, timeBuffer );
       
   128 	        LOGGER_WRITE_1( "ParseTimestamp err: %d", err );
       
   129         	}
       
   130         }
       
   131     TRACE_FUNC_EXIT;
       
   132 	}
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CSConSyncService::ProviderId()
       
   136 // Return correct providerId according to content type.
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 TSmlDataProviderId CSConSyncService::ProviderId( const TUint32 aSeconContentType ) const
       
   140 	{
       
   141 	TSmlDataProviderId providerId(KErrNotFound);
       
   142 	switch (aSeconContentType)
       
   143 		{
       
   144 		case KSeconCalendarUid:
       
   145 		providerId = KCalendarId;
       
   146 		break;
       
   147 		case KSeconContactsUid:
       
   148 		providerId = KContactsId;
       
   149 		break;
       
   150 		case KSeconSmsUid:
       
   151 		providerId = KSmsId;
       
   152 		break;
       
   153 		case KSeconBookmarkUid:
       
   154 		providerId = KBookmarkId;
       
   155 		break;
       
   156 		default:
       
   157 		LOGGER_WRITE_1( "Invalid content type: %d", aSeconContentType );
       
   158 		}
       
   159 	return providerId;
       
   160 	}
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CSConSyncService::ParseTimestamp()
       
   164 // Parses timestamp to TTime format
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 TInt CSConSyncService::ParseTimestamp( TTime& aTime, const TDesC& aTimestamp ) const
       
   168 	{
       
   169 	// timestamp must be in following format:
       
   170 	// YYYYMMDDTHHMMSSZ
       
   171 	// MM=1..12, DD=1..number of days in month, HH=0..23, MM;SS=0..59
       
   172 	// Example 20071224T162215Z = 24.12.2007, 16:22:15 UTC time
       
   173 	
       
   174 	if ( aTimestamp.Length() != KDateFormatLength )
       
   175 		{
       
   176 		LOGGER_WRITE_1( "aTimestamp.Length(): %d", aTimestamp.Length() );
       
   177 		return KErrArgument;
       
   178 		}
       
   179 	
       
   180 	TPtrC temp;
       
   181 	temp.Set(aTimestamp.Ptr());
       
   182 	
       
   183 	TLex lex(temp.Mid(0,4));
       
   184 	TInt year;
       
   185 	TInt month;
       
   186 	TInt day;
       
   187 	TInt hour;
       
   188 	TInt minute;
       
   189 	TInt second;
       
   190 	TInt err = lex.Val( year );
       
   191 	if (err != KErrNone) return err;
       
   192 	
       
   193 	lex = temp.Mid(4,2);
       
   194 	err = lex.Val( month );
       
   195 	if ( err != KErrNone) return err;
       
   196 	month--;
       
   197 	
       
   198 	lex = temp.Mid(6,2);
       
   199 	err = lex.Val( day );
       
   200 	if (err != KErrNone) return err;
       
   201 	day--;
       
   202 	
       
   203 	lex = temp.Mid(9,2);
       
   204 	err = lex.Val( hour );
       
   205 	if (err != KErrNone) return err;
       
   206 	
       
   207 	lex = temp.Mid(11,2);
       
   208 	err = lex.Val( minute );
       
   209 	if (err != KErrNone) return err;
       
   210 	
       
   211 	lex = temp.Mid(13,2);
       
   212 	err = lex.Val( second );
       
   213 	if (err != KErrNone) return err;
       
   214 	
       
   215 	TDateTime parsedTime;
       
   216 	err = parsedTime.Set(year, (TMonth)month, day, hour, minute, second, 0);
       
   217 	if (err != KErrNone) return err;
       
   218 	
       
   219 	aTime = parsedTime;
       
   220 	return err;
       
   221 	}
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CSConSyncService::GetApplicationCaptionL()
       
   225 // Get application caption.
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 void CSConSyncService::GetApplicationCaptionL( TDes& aCaption, const TUid aAppUid )
       
   229     {
       
   230     TRACE_FUNC_ENTRY;
       
   231     
       
   232     TInt err(KErrNotReady);
       
   233     // connect to RApaLsSession, if not yet ready
       
   234     if ( !iApaSessionConnected )
       
   235     	{
       
   236     	err = iApaSession.Connect();
       
   237     	if ( err == KErrNone )
       
   238     		{
       
   239     		iApaSessionConnected = ETrue;
       
   240     		}
       
   241     	}
       
   242     
       
   243     // Get application caption using RApaLsSession::GetAppInfo
       
   244 	TApaAppInfo appInfo;
       
   245 	if ( iApaSessionConnected )
       
   246 		{
       
   247 		err = iApaSession.GetAppInfo( appInfo ,aAppUid );
       
   248 		}
       
   249     
       
   250     LOGGER_WRITE_1( "GetAppInfo err: %d", err );
       
   251     if ( err == KErrNone )
       
   252     	{
       
   253     	aCaption = appInfo.iCaption;
       
   254     	}
       
   255     else
       
   256         {
       
   257         // Caption does not exist, search application name from installer
       
   258         GetInstPackageCaptionL( aCaption, aAppUid );
       
   259         }
       
   260     TRACE_FUNC_EXIT;
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CSConSyncService::GetInstPackageCaptionL()
       
   265 // Search UID from installed applications, return app caption
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 void CSConSyncService::GetInstPackageCaptionL( TDes& aCaption, const TUid aPackageUid )
       
   269 	{
       
   270 	TRACE_FUNC_ENTRY;
       
   271     LOGGER_WRITE("Name not found, search from installer");
       
   272     // Search application name from Installer.
       
   273     Swi::RSisRegistrySession sisRegistry;
       
   274     User::LeaveIfError( sisRegistry.Connect() );
       
   275     CleanupClosePushL( sisRegistry );
       
   276     
       
   277     //Check if uid belongs to SIS package
       
   278     if( sisRegistry.IsInstalledL( aPackageUid ) )
       
   279         {
       
   280         LOGGER_WRITE("SIS package found");
       
   281         Swi::RSisRegistryEntry entry;
       
   282         CleanupClosePushL(entry);
       
   283         User::LeaveIfError( entry.Open( sisRegistry, aPackageUid ) );
       
   284         aCaption.Copy( *entry.PackageNameL() );
       
   285         CleanupStack::PopAndDestroy( &entry );
       
   286         }
       
   287     else
       
   288         {
       
   289         // not sis app, might be java app.
       
   290         LOGGER_WRITE("SIS package not found");
       
   291         CJavaRegistry* javaRegistry = CJavaRegistry::NewLC( );
       
   292         TBool entryExist = javaRegistry->RegistryEntryExistsL( aPackageUid );
       
   293         if ( entryExist )
       
   294             {
       
   295             CJavaRegistryEntry* entry = javaRegistry->RegistryEntryL( aPackageUid );
       
   296             if ( entry )
       
   297                 {
       
   298                 aCaption.Copy( entry->Name() );
       
   299                 delete entry;
       
   300                 }
       
   301             }
       
   302         
       
   303         CleanupStack::PopAndDestroy( javaRegistry ); 
       
   304         }
       
   305     
       
   306     CleanupStack::PopAndDestroy( &sisRegistry );
       
   307     TRACE_FUNC_EXIT;
       
   308 	}
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 // CSConSyncService::GetLastUsedProfileNameL()
       
   312 // Gets last used profile for selected provider
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 void CSConSyncService::GetLastUsedProfileNameL( TDes& aProfileName, TTime& aTime,
       
   316 		const TSmlDataProviderId& aDataProviderId )
       
   317     {
       
   318     TRACE_FUNC_ENTRY;
       
   319     if ( !iSyncSessionOpened )
       
   320 		{
       
   321 		iSyncSession.OpenL();
       
   322 		iSyncSessionOpened = ETrue;
       
   323 		}
       
   324     RArray<TSmlProfileId> profiles;
       
   325     CleanupClosePushL( profiles );
       
   326     iSyncSession.ListProfilesL(profiles, ESmlDataSync );
       
   327     TSmlProfileId lastSyncProfile(KErrNotFound);
       
   328     TTime lastSyncTime(0);
       
   329     LOGGER_WRITE_1( "Profiles count: %d", profiles.Count() );
       
   330     for (TInt i=0; i<profiles.Count(); i++)
       
   331     	{
       
   332     	TTime syncTime(0);
       
   333     	GetLastSyncTimeL( syncTime, profiles[i], aDataProviderId );
       
   334     	if ( syncTime.Int64() != 0 && syncTime > lastSyncTime )
       
   335     		{
       
   336     		LOGGER_WRITE_1( "New record, profile: %d",profiles[i]);
       
   337     		lastSyncTime = syncTime;
       
   338     		lastSyncProfile = profiles[i];
       
   339     		}
       
   340     	}
       
   341     CleanupStack::PopAndDestroy( &profiles );
       
   342     LOGGER_WRITE_1( "lastSyncProfile: %d", lastSyncProfile );
       
   343     if ( lastSyncProfile != KErrNotFound )
       
   344     	{
       
   345     	aTime = lastSyncTime;
       
   346     	RSyncMLDataSyncProfile syncProfile;
       
   347     	syncProfile.OpenL( iSyncSession, lastSyncProfile, ESmlOpenRead );
       
   348     	TPtrC displayName = syncProfile.DisplayName();
       
   349     	aProfileName.Copy( displayName );
       
   350     	syncProfile.Close();
       
   351     	}
       
   352     TRACE_FUNC_EXIT;
       
   353     }
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // CSConSyncService::GetLastSyncTimeL()
       
   357 // Gets last sync time for certain profile and provider.
       
   358 // -----------------------------------------------------------------------------
       
   359 //
       
   360 void CSConSyncService::GetLastSyncTimeL( TTime& aTime, const TSmlProfileId aSyncProfileId,
       
   361 		const TSmlDataProviderId aDataProviderId  )
       
   362 	{
       
   363 	TRACE_FUNC_ENTRY;
       
   364 	LOGGER_WRITE_1( "aSyncProfileId: %d", aSyncProfileId );
       
   365 	LOGGER_WRITE_1( "aDataProviderId: 0x%08x", aDataProviderId );
       
   366 	if ( !iSyncSessionOpened )
       
   367 		{
       
   368 		LOGGER_WRITE("iSyncSession is not opened, Leave!");
       
   369 		User::Leave( KErrNotReady );
       
   370 		}
       
   371 	
       
   372 	RSyncMLDataSyncProfile syncProfile;
       
   373 	syncProfile.OpenL( iSyncSession, aSyncProfileId, ESmlOpenRead );
       
   374     CleanupClosePushL( syncProfile );
       
   375     LOGGER_WRITE( "syncProfile opened OK" );
       
   376     
       
   377     TSmlTaskId providerTask;
       
   378     GetProviderTaskL( providerTask, syncProfile, aDataProviderId  );
       
   379     if ( providerTask != KErrNone )
       
   380     	{
       
   381         RSyncMLHistoryLog historyLog;
       
   382         historyLog.OpenL( iSyncSession, aSyncProfileId );
       
   383         CleanupClosePushL( historyLog );
       
   384         historyLog.SortEntries(CSyncMLHistoryEntry::ESortByTime);
       
   385         LOGGER_WRITE_1( "historyLog.Count: %d", historyLog.Count() );
       
   386         const CSyncMLHistoryJob* historyJob = LatestHistoryJob( historyLog, providerTask );
       
   387         if ( historyJob )
       
   388             {
       
   389             aTime = historyJob->TimeStamp();
       
   390 #ifdef _DEBUG
       
   391             TDateTime timeStamp = historyJob->TimeStamp().DateTime();
       
   392             _LIT(KDateFormat, " (%04d%02d%02dT%02d%02d%02d)");
       
   393             const TInt KDateFormatLength(20);
       
   394             TBuf<KDateFormatLength> tempdate;
       
   395             tempdate.Format(KDateFormat, timeStamp.Year(), timeStamp.Month()+1, timeStamp.Day()+1,
       
   396                     timeStamp.Hour(), timeStamp.Minute(), timeStamp.Second());
       
   397             LOGGER_WRITE_1("history found, TimeStamp: %S", &tempdate);
       
   398 #endif
       
   399             }
       
   400     	
       
   401         CleanupStack::PopAndDestroy( &historyLog );
       
   402     	}
       
   403     CleanupStack::PopAndDestroy( &syncProfile );
       
   404     TRACE_FUNC_EXIT;
       
   405 	}
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // CSConSyncService::GetProviderTaskL()
       
   409 // Gets provider task id from profile
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 void CSConSyncService::GetProviderTaskL( TSmlTaskId& aProviderTask,
       
   413 		RSyncMLDataSyncProfile& aSyncProfile,
       
   414 		const TSmlDataProviderId aDataProviderId  ) const
       
   415 	{
       
   416 	TRACE_FUNC_ENTRY;
       
   417 	aProviderTask = KErrNotFound;
       
   418 	
       
   419 	RArray<TSmlTaskId> tasks;
       
   420 	CleanupClosePushL( tasks );
       
   421 	
       
   422 	aSyncProfile.ListTasksL( tasks );
       
   423 	LOGGER_WRITE_1( "tasks.Count: %d", tasks.Count() );
       
   424 	
       
   425 	for ( TInt i=0; i<tasks.Count() && aProviderTask==KErrNotFound; i++ )
       
   426 		{
       
   427 		RSyncMLTask syncTask;
       
   428 	    syncTask.OpenL( aSyncProfile, tasks[i] );
       
   429 	    if ( syncTask.DataProvider() == aDataProviderId )
       
   430 	        {
       
   431 	        LOGGER_WRITE_1( "dataprovider task found, taskId: %d", tasks[i] );
       
   432 	        aProviderTask = tasks[i];
       
   433 	        }
       
   434 	    syncTask.Close();
       
   435 		}
       
   436 	
       
   437 	CleanupStack::PopAndDestroy( &tasks );
       
   438 	TRACE_FUNC_EXIT;
       
   439 	}
       
   440 
       
   441 // -----------------------------------------------------------------------------
       
   442 // CSConSyncService::LatestHistoryJob()
       
   443 // Gets latest historyJob where task is correct.
       
   444 // -----------------------------------------------------------------------------
       
   445 //
       
   446  const CSyncMLHistoryJob* CSConSyncService::LatestHistoryJob(
       
   447 		 RSyncMLHistoryLog& aHistoryLog, TInt aTaskId ) const
       
   448     {
       
   449     TRACE_FUNC_ENTRY;
       
   450     
       
   451     TInt count = aHistoryLog.Count();
       
   452     if ( count == 0 )
       
   453         {
       
   454         LOGGER_WRITE( "profile has no history job" );
       
   455         TRACE_FUNC_EXIT;
       
   456         return NULL;  // profile has no history job
       
   457         }
       
   458 
       
   459     aHistoryLog.SortEntries(CSyncMLHistoryEntry::ESortByTime);    
       
   460 
       
   461     // try to find latest sync job (start from last array entry)
       
   462 
       
   463     for ( TInt i=count-1; i>=0; i-- )
       
   464         {
       
   465         const CSyncMLHistoryEntry& entry = aHistoryLog[i];
       
   466         const CSyncMLHistoryJob* jobEntry = CSyncMLHistoryJob::DynamicCast(&entry);
       
   467         if ( jobEntry )
       
   468             {
       
   469             if ( TaskExist(jobEntry, aTaskId) )
       
   470                 {
       
   471                 LOGGER_WRITE( "history job found" );
       
   472                 TRACE_FUNC_EXIT;
       
   473                 return jobEntry;
       
   474                 }
       
   475             }
       
   476         }
       
   477     LOGGER_WRITE( "history job not found" );
       
   478     TRACE_FUNC_EXIT;
       
   479     return NULL;
       
   480     }
       
   481 
       
   482  // -----------------------------------------------------------------------------
       
   483  // CSConSyncService::TaskExist()
       
   484  // Returns ETrue if task exists in HistoryJob.
       
   485  // -----------------------------------------------------------------------------
       
   486  //
       
   487 TBool CSConSyncService::TaskExist( const CSyncMLHistoryJob* aHistoryJob, TInt aTaskId ) const
       
   488     {
       
   489     TRACE_FUNC_ENTRY;
       
   490     TInt taskCount = aHistoryJob->TaskCount();
       
   491     for ( TInt i=0; i<taskCount; i++ )
       
   492         {
       
   493         const CSyncMLHistoryJob::TTaskInfo& taskInfo = aHistoryJob->TaskAt(i);
       
   494 
       
   495         if ( taskInfo.iTaskId == aTaskId )
       
   496             {
       
   497             LOGGER_WRITE( "task exists." );
       
   498             TRACE_FUNC_EXIT;
       
   499             return ETrue;
       
   500             }
       
   501         }
       
   502     TRACE_FUNC_EXIT;
       
   503     return EFalse;
       
   504     }
       
   505