syncmlfw/syncservice/SRC/SyncService.cpp
branchRCL_3
changeset 25 b183ec05bd8c
parent 24 13d7c31c74e0
child 26 19bba8228ff0
equal deleted inserted replaced
24:13d7c31c74e0 25:b183ec05bd8c
     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:  Client for DS and DM initiated syncs
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include <bldvariant.hrh>
       
    23 
       
    24 #include <centralrepository.h>
       
    25 #include <SyncMLNotifierDomainCRKeys.h>
       
    26 #include <SyncServiceSession.h>
       
    27 #include <SyncServiceParams.h>
       
    28 #include <SyncService.h>
       
    29 #include <SyncServiceConst.h>
       
    30 #include "SyncServiceDebug.h"
       
    31 
       
    32 #include <apgcli.h>
       
    33 #include <AknServerApp.h>
       
    34 #include <barsread2.h>
       
    35 
       
    36 // ----------------------------------------------------------------------------
       
    37 // RSyncService::StartSyncL
       
    38 // ----------------------------------------------------------------------------
       
    39 //
       
    40 void RSyncService::StartSyncL( TSyncServiceParam aParam )
       
    41 	{
       
    42 	FLOG( _L( "[RSyncService] StartSyncL" ) );
       
    43 	
       
    44 	TSyncParameters parameters;
       
    45 	parameters.iCommand = KSyncServCmdStartSync;
       
    46     parameters.iJobId = aParam.iJobId;
       
    47     parameters.iProfileId = aParam.iProfileId;
       
    48     parameters.iSilent = aParam.iSilent;
       
    49     TInt err = 
       
    50 		SendReceive( ESyncServiceCommand, TIpcArgs( parameters.iCommand,
       
    51 		                                            parameters.iJobId,
       
    52 		                                            parameters.iProfileId,
       
    53 		                                            parameters.iSilent ) );
       
    54 	User::LeaveIfError( err );
       
    55 	}
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // RSyncService::EnableProgressNoteL
       
    59 // ----------------------------------------------------------------------------
       
    60 //
       
    61 void RSyncService::EnableProgressNoteL( TBool aEnabled )
       
    62 	{
       
    63 	FLOG( _L("[RSyncService] EnableProgressNoteL") );
       
    64 
       
    65 	TSyncParameters parameters;
       
    66 	parameters.iCommand = KSyncServCmdShowProgress;
       
    67 
       
    68     if ( aEnabled )
       
    69         {
       
    70         parameters.iJobId = KSyncServEnabled;
       
    71         }
       
    72     else
       
    73         {
       
    74         parameters.iJobId = KSyncServDisabled;
       
    75         }
       
    76     
       
    77     TInt err = 
       
    78 		SendReceive( ESyncServiceCommand, TIpcArgs( parameters.iCommand,
       
    79 		                                            parameters.iJobId,
       
    80 		                                            parameters.iProfileId,
       
    81 		                                            parameters.iSilent ) );
       
    82 	User::LeaveIfError( err );
       
    83 	}
       
    84 
       
    85 // ----------------------------------------------------------------------------
       
    86 // RSyncService::ServiceUid
       
    87 // ----------------------------------------------------------------------------
       
    88 //
       
    89 TUid RSyncService::ServiceUid() const
       
    90 	{
       
    91 	FLOG( _L( "[RSyncService] ServiceUid" ) );
       
    92 	
       
    93 	return TUid::Uid( KSyncServiceUid );
       
    94 	}
       
    95 
       
    96 // ----------------------------------------------------------------------------
       
    97 // CSyncService::CSyncService
       
    98 // ----------------------------------------------------------------------------
       
    99 //
       
   100 CSyncService::CSyncService( )
       
   101 	{
       
   102 	FLOG( _L( "[CSyncService] CSyncService" ) );
       
   103 	
       
   104 	}
       
   105 
       
   106 // ----------------------------------------------------------------------------
       
   107 // CSyncService::~CSyncService
       
   108 // ----------------------------------------------------------------------------
       
   109 //
       
   110 CSyncService::~CSyncService( )
       
   111 	{
       
   112 	FLOG( _L( "[CSyncService] ~CSyncService" ) );
       
   113 	
       
   114 	delete iMonitor;
       
   115 	iService.Close();
       
   116 	}
       
   117 
       
   118 // ----------------------------------------------------------------------------
       
   119 // CSyncService::NewL
       
   120 // ----------------------------------------------------------------------------
       
   121 //
       
   122 EXPORT_C CSyncService* CSyncService::NewL( MAknServerAppExitObserver* aObserver,
       
   123                                            TUint aServiceId )
       
   124     {
       
   125     FLOG( _L( "[CSyncService] NewL" ) );
       
   126     
       
   127 	CSyncService* self = new(ELeave) CSyncService();
       
   128 	CleanupStack::PushL(self);
       
   129     self->ConstructL( aObserver, aServiceId );
       
   130 	CleanupStack::Pop(self);
       
   131 	return self;
       
   132     }
       
   133 
       
   134 // ----------------------------------------------------------------------------
       
   135 // CSyncService::ConstructL
       
   136 // ----------------------------------------------------------------------------
       
   137 //
       
   138 void CSyncService::ConstructL( MAknServerAppExitObserver* /*aObserver*/,
       
   139                                TUint aServiceId )
       
   140     {
       
   141 	FLOG( _L( "[CSyncService] ConstructL" ) );
       
   142     iServiceId = aServiceId;
       
   143     }
       
   144 
       
   145 // ----------------------------------------------------------------------------
       
   146 // CSyncService::StartSyncL
       
   147 // ----------------------------------------------------------------------------
       
   148 //
       
   149 EXPORT_C TUid CSyncService::StartSyncL( TSyncServiceParam aParam )
       
   150     {
       
   151     FLOG( _L( "[CSyncService] StartSyncL" ) );
       
   152 
       
   153     iServiceId = aParam.iServiceId;
       
   154 	iUid = DiscoverL( iServiceId );
       
   155 	
       
   156     TInt error = KErrNotFound;
       
   157         
       
   158     if( iService.ServerName().Length() > 0 )
       
   159         {
       
   160         FLOG( _L( "[CSyncService] ConstructL connect to existing app" ) );
       
   161   	    TRAP( error, iService.StartSyncL( aParam ) );
       
   162   	    if ( error == KErrNone )
       
   163   	        {
       
   164   	        return iUid;
       
   165   	        }
       
   166         }
       
   167    	if ( error )
       
   168 		{
       
   169 		TFullName aServerName;
       
   170 		if ( iServiceId == KDevManServiceStart )
       
   171 		    {
       
   172     		CRepository* centrep = NULL;
       
   173     		centrep = CRepository::NewL( KCRUidNSmlNotifierDomainKeys );
       
   174     		centrep->Get( KNSmlDMAppName, aServerName );
       
   175     		delete centrep;
       
   176 		    }
       
   177 		else
       
   178 		    {
       
   179 		    aServerName = KDataSyncServiceName;
       
   180 		    }
       
   181         FLOG( _L( "[CSyncService] ConstructL connect to existing app with predefined name" ) );
       
   182         iService.Close();
       
   183 		TRAP( error, iService.ConnectExistingByNameL( aServerName ) );
       
   184 		}
       
   185    	if ( error )
       
   186 		{
       
   187 		FLOG( _L( "[CSyncService] ConstructL connect to new app" ) );
       
   188     	TRAP( error, iService.ConnectNewAppL( iUid ) );
       
   189     	if( error )
       
   190     		{
       
   191     		FLOG( _L( "[CSyncService] ConnectNewAppL: Error not supported" ) );
       
   192     		User::Leave( KErrNotSupported );
       
   193     		}
       
   194  		}
       
   195     
       
   196     iService.StartSyncL( aParam );
       
   197     return iUid;
       
   198     }
       
   199 
       
   200 // ----------------------------------------------------------------------------
       
   201 // CSyncService::EnableProgressNoteL
       
   202 // ----------------------------------------------------------------------------
       
   203 //
       
   204 EXPORT_C void CSyncService::EnableProgressNoteL( TBool aEnabled )
       
   205     {
       
   206     FLOG( _L( "[CSyncService] EnableProgressNoteL" ) );
       
   207     
       
   208     TFullName DmAppName;
       
   209     CRepository* centrep = NULL;
       
   210     centrep = CRepository::NewL( KCRUidNSmlNotifierDomainKeys );
       
   211     centrep->Get( KNSmlDMAppName, DmAppName );
       
   212     delete centrep;
       
   213     
       
   214     if( iService.ServerName().Length() > 0 )
       
   215         {
       
   216 		TRAPD(error, iService.EnableProgressNoteL( aEnabled ) );
       
   217 		if ( error != KErrNone )
       
   218 		    {
       
   219     		TFullName aServerName;
       
   220     		if ( iServiceId == KDevManServiceStart )
       
   221     		    {
       
   222                 aServerName = DmAppName;
       
   223     		    }
       
   224     		else
       
   225     		    {
       
   226     		    aServerName = KDataSyncServiceName;
       
   227     		    }
       
   228             FLOG( _L( "[CSyncService] EnableProgressNoteL connect to existing app with predefined name" ) );
       
   229             iService.Close();
       
   230     		iService.ConnectExistingByNameL( aServerName );        
       
   231             iService.EnableProgressNoteL( aEnabled );
       
   232 		    }
       
   233         }
       
   234     else
       
   235         {
       
   236         // Sync has been started from APP UI so connect to the service and enable progressnote
       
   237 		TFullName aServerName;
       
   238 		if ( iServiceId == KDevManServiceStart )
       
   239 		    {
       
   240 		    aServerName = DmAppName;
       
   241 		    }
       
   242 		else
       
   243 		    {
       
   244 		    aServerName = KDataSyncServiceName;
       
   245 		    }
       
   246         FLOG( _L( "[CSyncService] EnableProgressNoteL connect to existing app with predefined name" ) );
       
   247 		iService.ConnectExistingByNameL( aServerName );        
       
   248         iService.EnableProgressNoteL( aEnabled );
       
   249         }
       
   250     FLOG( _L( "[CSyncService] EnableProgressNoteL" ) );
       
   251     }
       
   252 
       
   253 // ----------------------------------------------------------------------------
       
   254 // CSyncService::DiscoverL
       
   255 // ----------------------------------------------------------------------------
       
   256 //
       
   257 TUid CSyncService::DiscoverL( TUint aServiceId )
       
   258 	{
       
   259 	FLOG( _L( "[CSyncService] DiscoverL" ) );
       
   260 	
       
   261 	CApaAppServiceInfoArray* services = NULL;	
       
   262 	RApaLsSession ls;
       
   263 	TUid appUid = TUid::Uid( 0x0 ); // Set the uid as NULL
       
   264 	
       
   265 	if( ls.Connect() == KErrNone )
       
   266 		{
       
   267 	    CleanupClosePushL( ls );
       
   268 		TInt ret = ls.GetServerApps( TUid::Uid( KSyncServiceUid ) );
       
   269 
       
   270 		if( ret != KErrNone )
       
   271 			{
       
   272 		    CleanupStack::PopAndDestroy( );  // closes RApaLsSession		
       
   273 		    return appUid; // return NULL appUid
       
   274 			}
       
   275 
       
   276 		services = ls.GetServiceImplementationsLC( TUid::Uid( KSyncServiceUid ) );			
       
   277 		
       
   278 		if( services )
       
   279 			{
       
   280 			TArray<TApaAppServiceInfo> s = services->Array( );
       
   281 			
       
   282 			TInt count = s.Count( );
       
   283 		
       
   284 			if( count != 0)
       
   285 				{	// 
       
   286 				for( TInt i = 0; i < count; i++ )
       
   287 					{
       
   288 					if( TryMatchL( s[i], appUid, aServiceId ) )
       
   289 						{ // If one of the services match, return the uid set by TryMatchL
       
   290 						break;
       
   291 						}
       
   292 					}
       
   293 				}		
       
   294 			}
       
   295 	    CleanupStack::PopAndDestroy( 2 );  // destroys services + closes RApaLsSession
       
   296 		}
       
   297 	return appUid;
       
   298 	}
       
   299 
       
   300 // ----------------------------------------------------------------------------
       
   301 // CSyncService::TryMatchL
       
   302 // ----------------------------------------------------------------------------
       
   303 //
       
   304 TBool CSyncService::TryMatchL( const TApaAppServiceInfo aInfo,
       
   305                                TUid& aAppUid,
       
   306                                TUint aServiceId )
       
   307 	{
       
   308 	FLOG( _L( "[CSyncService] TryMatchL" ) );
       
   309 	
       
   310 	RResourceReader r;
       
   311 	r.OpenLC( aInfo.OpaqueData() );
       
   312 
       
   313 	TInt serviceType = r.ReadUint32L( ); // the service type (sync start)
       
   314 
       
   315     CleanupStack::PopAndDestroy( );				
       
   316 	
       
   317 	if( serviceType == aServiceId )
       
   318 		{
       
   319 		aAppUid.iUid = aInfo.Uid( ).iUid;
       
   320 		FLOG( _L( "[CSyncService] TryMatchL return ETrue" ) );
       
   321 		return ETrue;
       
   322 		}
       
   323     
       
   324     FLOG( _L( "[CSyncService] TryMatchL return EFalse" ) );
       
   325 	return EFalse;	
       
   326 	}
       
   327 	
       
   328 // End of file