idlefw/plugins/profileplugin/src/caiprofileengine.cpp
changeset 0 79c6a41cd166
child 8 d0529222e3f0
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  The engine class of profile plugin. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "caiprofileengine.h"
       
    21 #include "maiprofilepluginnotifier.h"
       
    22 #include "aiprofileplugincontentmodel.h"
       
    23 
       
    24 #include <w32std.h> 
       
    25 #include <MProfileEngine.h>
       
    26 #include <MProfile.h>
       
    27 #include <MProfilesNamesArray.h>
       
    28 #include <MProfileName.h>
       
    29 #include <Profile.hrh>
       
    30 #include <CProfileChangeNotifyHandler.h>
       
    31 #include <PUAcodes.hrh> 
       
    32 #include <AknUtils.h>
       
    33 #include <data_caging_path_literals.hrh>
       
    34 #include <bautils.h>
       
    35 #include <StringLoader.h>
       
    36 #include <AknQueryDialog.h>
       
    37 #include <aknnotewrappers.h>
       
    38 #include <RSSSettings.h>
       
    39 
       
    40 
       
    41 #include <aiprofilepluginres.rsg>
       
    42 
       
    43 #include <startupdomainpskeys.h>
       
    44 
       
    45 const TInt KMaxProfileNameLength( 64 );
       
    46 const TInt KGeneralProfileId( 0 );
       
    47 const TInt KSilentProfileId( 1 );
       
    48 const TInt KOfflineProfileId( 5 );
       
    49 
       
    50 const TInt KMaxActiveProfileLength( 64 );
       
    51 const TUid KUidProfileApp = { 0x100058F8 }; 
       
    52 const TUid KProfileAppSettingViewId = { 2 };
       
    53 
       
    54 _LIT( KAiProfilePluginResourceFileName, "z:aiprofilepluginres.rsc");
       
    55 
       
    56 // ============================ MEMBER FUNCTIONS ===============================
       
    57 // ---------------------------------------------------------
       
    58 // Default constructor
       
    59 // ---------------------------------------------------------
       
    60 //
       
    61 CAiProfileEngine::CAiProfileEngine( MAiProfilePluginNotifier* aProfilePluginNotifier ) :
       
    62     iProfilePluginNotifier ( aProfilePluginNotifier ),
       
    63     iResourceLoader( *CCoeEnv::Static() )
       
    64     {
       
    65     }
       
    66     
       
    67 // ---------------------------------------------------------
       
    68 // Two-phased constructor.
       
    69 // Create instance of concrete ECOM interface implementation
       
    70 // ---------------------------------------------------------
       
    71 //
       
    72 CAiProfileEngine* CAiProfileEngine::NewL( MAiProfilePluginNotifier* aProfilePluginNotifier )
       
    73     {
       
    74     CAiProfileEngine* self = new( ELeave ) CAiProfileEngine( aProfilePluginNotifier );
       
    75     CleanupStack::PushL( self );
       
    76     self->ConstructL();
       
    77     CleanupStack::Pop( self );
       
    78 
       
    79     return self;
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------
       
    83 // Symbian 2nd phase constructor can leave
       
    84 // ---------------------------------------------------------
       
    85 //
       
    86 void CAiProfileEngine::ConstructL()
       
    87     {
       
    88     TParse parse;
       
    89     parse.Set( KAiProfilePluginResourceFileName, &KDC_RESOURCE_FILES_DIR, NULL );
       
    90     TFileName fileName( parse.FullName() );
       
    91 
       
    92     // Get language of resource file.
       
    93     BaflUtils::NearestLanguageFile( CCoeEnv::Static()->FsSession(), fileName );
       
    94 
       
    95     // Open resource file.
       
    96     TRAP_IGNORE( iResourceLoader.OpenL( fileName ) );
       
    97     
       
    98     User::LeaveIfError( iSSSettings.Open() );
       
    99 
       
   100     iProfileEngine = CreateProfileEngineL();    
       
   101    }
       
   102     
       
   103     
       
   104 // ---------------------------------------------------------
       
   105 // Destructor.
       
   106 // ---------------------------------------------------------
       
   107 //
       
   108 CAiProfileEngine::~CAiProfileEngine()
       
   109     {
       
   110     iSSSettings.CancelAll( *this );
       
   111     iSSSettings.Close();
       
   112     delete iProfileNotifier;
       
   113 	delete iActiveProfileName;
       
   114 	delete iSwapProfileName;
       
   115 	
       
   116 	if( iProfileNamePointerArray.Count() )
       
   117     	{
       
   118         iProfileNamePointerArray.ResetAndDestroy();
       
   119         }
       
   120 
       
   121     if( iProfileEngine )
       
   122         {
       
   123         iProfileEngine->Release();
       
   124         }
       
   125     
       
   126     iResourceLoader.Close();            	 
       
   127     }
       
   128     
       
   129 // ---------------------------------------------------------
       
   130 // Updates profiles
       
   131 // ---------------------------------------------------------
       
   132 //
       
   133 void CAiProfileEngine::UpdateProfileNamesL()
       
   134 	{
       
   135 	//update active profile name
       
   136 	HBufC* activeProfileName = NULL;
       
   137 	
       
   138 	MProfile* profile = iProfileEngine->ActiveProfileLC();	
       
   139 	const MProfileName& name = profile->ProfileName();	
       
   140 	activeProfileName = name.Name().AllocLC();
       
   141 
       
   142 	SetActiveProfileNameL( *activeProfileName );
       
   143       
       
   144     //update profile name list	
       
   145 	MProfilesNamesArray* profileNamesArray = iProfileEngine->ProfilesNamesArrayLC();
       
   146     
       
   147     SetProfileNameListL( *profileNamesArray );
       
   148     
       
   149     //update swap profile name
       
   150     HBufC* swapProfileName = NULL;
       
   151     
       
   152     TInt activeProfileId = iProfileEngine->ActiveProfileId();
       
   153     
       
   154     if( activeProfileId == KSilentProfileId )
       
   155     	{
       
   156         const MProfileName* generalProfileName = profileNamesArray->ProfileName( KGeneralProfileId );	
       
   157 		swapProfileName = generalProfileName->Name().AllocLC();	
       
   158     	}
       
   159     else
       
   160         {
       
   161         const MProfileName* silentProfileName = profileNamesArray->ProfileName( KSilentProfileId );	
       
   162 		swapProfileName = silentProfileName->Name().AllocLC();	
       
   163         }
       
   164     
       
   165     TPtrC swapProfileNamePtr( *swapProfileName );
       
   166 	HBufC* activateProfileString = NULL;
       
   167     activateProfileString = StringLoader::LoadLC( R_AI_PERS_PROF_TOGGLE, swapProfileNamePtr );    	
       
   168     
       
   169     SetSwapProfileNameL( *activateProfileString );
       
   170 
       
   171     CleanupStack::PopAndDestroy( 5 ); //profile, profileName, profileNamesArray, swapProfileName, activateProfileString 
       
   172 	}
       
   173 
       
   174 // ---------------------------------------------------------
       
   175 // Checks SIM card status
       
   176 // ---------------------------------------------------------
       
   177 //
       
   178 TBool CAiProfileEngine::ShowOfflineMessageL()
       
   179 	{
       
   180 	TInt result = ETrue;
       
   181 	
       
   182     TInt simCardStatus( ESimNotPresent );
       
   183         
       
   184     RProperty simStatus;
       
   185     User::LeaveIfError( simStatus.Attach( KPSUidStartup, KPSSimStatus ) );
       
   186     User::LeaveIfError( simStatus.Get( simCardStatus ) );
       
   187     simStatus.Close();
       
   188 
       
   189  	if( simCardStatus == ESimNotPresent )
       
   190 		{
       
   191 		// SIM card does not exist.
       
   192 	    HBufC* infoNoteText = StringLoader::LoadLC( R_SU_NOTE_INSERT_SIM );
       
   193 	    CAknInformationNote* note = new( ELeave ) CAknInformationNote( ETrue );
       
   194 	    note->ExecuteLD( *infoNoteText );
       
   195 	    CleanupStack::PopAndDestroy( infoNoteText );
       
   196 	    result = EFalse;	
       
   197 		}
       
   198 	else
       
   199 		{
       
   200 		CAknQueryDialog* dlg = CAknQueryDialog::NewL();
       
   201 		result = dlg->ExecuteLD( R_AI_LEAVE_OFFLINE_MODE_QUERY );	
       
   202 		}
       
   203        
       
   204 	return result;
       
   205 	}
       
   206 
       
   207 									  
       
   208 // ---------------------------------------------------------
       
   209 // 
       
   210 // ---------------------------------------------------------
       
   211 //
       
   212 void CAiProfileEngine::SetActiveProfileNameL( const TDesC& aName )
       
   213     {
       
   214     HBufC* temp = aName.AllocL();
       
   215     delete iActiveProfileName;
       
   216     iActiveProfileName = NULL;
       
   217     TPtr profileNamePtr = temp->Des();
       
   218     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( profileNamePtr );
       
   219     iActiveProfileName = temp;  
       
   220     }
       
   221     
       
   222 // ---------------------------------------------------------
       
   223 // 
       
   224 // ---------------------------------------------------------
       
   225 //
       
   226 const TDesC& CAiProfileEngine::ActiveProfileName() const
       
   227     {
       
   228     if( iActiveProfileName )
       
   229         {
       
   230         return *iActiveProfileName;
       
   231         }
       
   232     
       
   233     return KNullDesC();
       
   234     }
       
   235 
       
   236 // ---------------------------------------------------------
       
   237 // 
       
   238 // ---------------------------------------------------------
       
   239 //
       
   240 void CAiProfileEngine::SetSwapProfileNameL( const TDesC& aName )
       
   241     {
       
   242     HBufC* temp = aName.AllocL();
       
   243     delete iSwapProfileName;
       
   244     iSwapProfileName = NULL;
       
   245     TPtr profileNamePtr = temp->Des();
       
   246     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( profileNamePtr );
       
   247     iSwapProfileName = temp;  
       
   248     }
       
   249     
       
   250 // ---------------------------------------------------------
       
   251 // 
       
   252 // ---------------------------------------------------------
       
   253 //
       
   254 const TDesC& CAiProfileEngine::SwapProfileName() const
       
   255     {
       
   256     if( iSwapProfileName )
       
   257         {
       
   258         return *iSwapProfileName;
       
   259         }
       
   260     
       
   261     return KNullDesC();
       
   262     }
       
   263 
       
   264 
       
   265 // ---------------------------------------------------------
       
   266 // Set profile names
       
   267 // ---------------------------------------------------------
       
   268 //
       
   269 void CAiProfileEngine::SetProfileNameListL( const MProfilesNamesArray& aArray )
       
   270     {
       
   271     if( iProfileNamePointerArray.Count() )
       
   272     	{
       
   273         iProfileNamePointerArray.ResetAndDestroy();
       
   274         }
       
   275       
       
   276     const TInt count = aArray.MdcaCount();
       
   277     TBufC<KMaxProfileNameLength> profileName;
       
   278          
       
   279     for( TInt i = 0; i < count; i++ )
       
   280     	{
       
   281     	profileName = aArray.MdcaPoint( i );
       
   282     	TPtr profileNamePtr = profileName.Des();
       
   283     	AknTextUtils::DisplayTextLanguageSpecificNumberConversion( profileNamePtr );
       
   284     	HBufC* profile = profileNamePtr.AllocLC();
       
   285     	User::LeaveIfError( iProfileNamePointerArray.Append( profile ));
       
   286     	CleanupStack::Pop( profile );
       
   287     	}
       
   288     }
       
   289     
       
   290 
       
   291 // ---------------------------------------------------------
       
   292 // 
       
   293 // ---------------------------------------------------------
       
   294 //
       
   295 const TDesC& CAiProfileEngine::ProfileNameByIndex( TInt aIndex ) const
       
   296     {
       
   297  	if( iProfileNamePointerArray.Count() )
       
   298  		{
       
   299  		return *iProfileNamePointerArray[aIndex];
       
   300  		}
       
   301  	
       
   302  	return KNullDesC();
       
   303     }
       
   304 
       
   305 // ---------------------------------------------------------
       
   306 // 
       
   307 // ---------------------------------------------------------
       
   308 //
       
   309 TBool CAiProfileEngine::IsActiveProfileSilentL()
       
   310 	{
       
   311 	TBool isSilent = EFalse;
       
   312 	MProfile* profile = iProfileEngine->ActiveProfileLC();
       
   313 	isSilent = profile->IsSilent();
       
   314 	CleanupStack::PopAndDestroy();
       
   315 	return isSilent; 
       
   316 	}
       
   317 
       
   318 // ---------------------------------------------------------
       
   319 // 
       
   320 // ---------------------------------------------------------
       
   321 //
       
   322 TBool CAiProfileEngine::IsActiveProfileTimedL()
       
   323 	{
       
   324 	return iProfileEngine->IsActiveProfileTimedL();
       
   325 	}
       
   326 
       
   327 // ---------------------------------------------------------
       
   328 // Number of profiles
       
   329 // ---------------------------------------------------------
       
   330 //
       
   331 TInt CAiProfileEngine::NumberOfProfiles()
       
   332 	{	
       
   333 	return iProfileNamePointerArray.Count();
       
   334 	}
       
   335 
       
   336 
       
   337 // ---------------------------------------------------------
       
   338 // 
       
   339 // ---------------------------------------------------------
       
   340 //
       
   341 void CAiProfileEngine::HandleAiEventL( TInt aEvent, const TDesC& aParam )
       
   342 	{
       
   343 	switch ( aEvent )
       
   344         {
       
   345         case EAiProfileEventSwitchByIndex:
       
   346             HandleSwitchByIndexL( aParam );
       
   347             break;
       
   348 
       
   349         case EAiProfileEventSwitchByName:
       
   350             HandleSwitchByNameL( aParam );
       
   351             break;
       
   352 
       
   353         case EAiProfileEventSwap:
       
   354             HandleSwapL( aParam );
       
   355             break;
       
   356             
       
   357         case EAiProfileEditActive:
       
   358             HandleEditActiveProfileL();
       
   359             break;
       
   360         default:
       
   361             break;
       
   362         }
       
   363 	}
       
   364 
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // Handles profile switch by index event
       
   368 // -----------------------------------------------------------------------------
       
   369 //
       
   370 void CAiProfileEngine::HandleSwitchByIndexL( const TDesC& aParam )
       
   371 	{
       
   372     if ( aParam.Length() > 0 )
       
   373     	{
       
   374         TInt profileId = KErrNotFound;
       
   375 		TPtrC ptr( aParam );
       
   376 		TLex lexer( ptr );
       
   377 			
       
   378     	TInt err = lexer.Val( profileId );  
       
   379 		if ( err == KErrNone )
       
   380 			{
       
   381 			MProfilesNamesArray* profileNamesArray = iProfileEngine->ProfilesNamesArrayLC();
       
   382 			const MProfileName* profileName = profileNamesArray->ProfileName( profileId );
       
   383 		 
       
   384 		    if( profileName )
       
   385         		{
       
   386 	            profileId = profileName->Id();
       
   387 		    	SetActiveProfileL( profileId );
       
   388         		}
       
   389 		    
       
   390 		    CleanupStack::PopAndDestroy();
       
   391 		   	}
       
   392     	}
       
   393 	}
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // Handles profile switch by name event
       
   397 // -----------------------------------------------------------------------------
       
   398 //
       
   399 void CAiProfileEngine::HandleSwitchByNameL( const TDesC& aParam )
       
   400 	{
       
   401     if ( aParam.Length() > 0 )
       
   402     	{
       
   403     	TInt profileId = KErrNotFound;
       
   404         MProfilesNamesArray* profileNamesArray = iProfileEngine->ProfilesNamesArrayLC();
       
   405         profileId = profileNamesArray->FindByName( aParam );
       
   406         if( profileId != KErrNotFound )
       
   407         	{
       
   408         	SetActiveProfileL( profileId );
       
   409         	}
       
   410     	
       
   411     	CleanupStack::PopAndDestroy();
       
   412     	}
       
   413 	}
       
   414 
       
   415 // -----------------------------------------------------------------------------
       
   416 // Handles profile swap event
       
   417 // -----------------------------------------------------------------------------
       
   418 //
       
   419 void CAiProfileEngine::HandleSwapL( const TDesC& aParam )
       
   420 	{
       
   421 	if ( aParam.Length() > 0 )
       
   422 		{
       
   423 		TInt profileId = KErrNotFound;
       
   424 		TPtrC ptr( aParam );
       
   425 		TLex lexer( ptr );
       
   426 			
       
   427     	TInt err = lexer.Val( profileId );  
       
   428 		if ( err == KErrNone )
       
   429 			{
       
   430 			TInt activeProfile = iProfileEngine->ActiveProfileId();
       
   431 			
       
   432 			if( activeProfile != profileId )
       
   433 				{
       
   434 				MProfilesNamesArray* profileNamesArray = iProfileEngine->ProfilesNamesArrayLC();
       
   435         		profileId = profileNamesArray->FindById( profileId );
       
   436 		    
       
   437 		    	if( profileId != KErrNotFound )
       
   438         			{
       
   439 		    		TRAP_IGNORE( SetActiveProfileL( profileId ) );
       
   440         			}
       
   441         		
       
   442         		CleanupStack::PopAndDestroy();	
       
   443 				}
       
   444 			else
       
   445 				{
       
   446 				SetActiveProfileL( KGeneralProfileId );	
       
   447 				}
       
   448 		   	}
       
   449 		}
       
   450 	}
       
   451 
       
   452 // -----------------------------------------------------------------------------
       
   453 // Handles edit active profile event
       
   454 // -----------------------------------------------------------------------------
       
   455 //
       
   456 void CAiProfileEngine::HandleEditActiveProfileL()
       
   457     {
       
   458     RWsSession ws;
       
   459     User::LeaveIfError(ws.Connect());
       
   460     CleanupClosePushL(ws);
       
   461 
       
   462     // Find the task with uid
       
   463     TApaTaskList taskList(ws);
       
   464     TApaTask task = taskList.FindApp( KUidProfileApp );
       
   465 
       
   466     if ( task.Exists() )
       
   467         {
       
   468         task.EndTask();
       
   469         User::After( 500000 );
       
   470         }
       
   471     CleanupStack::PopAndDestroy(&ws);
       
   472     
       
   473     TVwsViewId viewid( KUidProfileApp, KProfileAppSettingViewId );
       
   474     TInt profileId = iProfileEngine->ActiveProfileId();
       
   475     TBuf8<KMaxActiveProfileLength> buf;
       
   476     buf.AppendNum(profileId);
       
   477     CEikonEnv::Static()->AppUi()->ActivateViewL( viewid ,KProfileAppSettingViewId,buf);
       
   478     
       
   479 
       
   480     }
       
   481 
       
   482 // -----------------------------------------------------------------------------
       
   483 //  Set active profile
       
   484 // -----------------------------------------------------------------------------
       
   485 //
       
   486 void CAiProfileEngine::SetActiveProfileL( const TInt aProfileId )
       
   487 	{
       
   488 	TInt activeProfileId = iProfileEngine->ActiveProfileId();
       
   489 	
       
   490 	if ( activeProfileId == KOfflineProfileId && aProfileId != KOfflineProfileId )
       
   491    		{
       
   492     	if( !ShowOfflineMessageL() )
       
   493     		{
       
   494     		// User doesn't want to activate RF or
       
   495     		// SIM card does not exist.
       
   496     		return;
       
   497     		}
       
   498     	}
       
   499     	
       
   500    	iProfileEngine->SetActiveProfileL( aProfileId );   	
       
   501  	}
       
   502 		
       
   503 // ---------------------------------------------------------------------------
       
   504 // Resumes the engine
       
   505 // ---------------------------------------------------------------------------
       
   506 //
       
   507 void CAiProfileEngine::ResumeL()
       
   508     {
       
   509     User::LeaveIfError( iSSSettings.Open() );
       
   510  
       
   511     //Register to listen ALS activation, if ALS status changes,
       
   512 	//profile must be republished.
       
   513     TInt err = iSSSettings.Register( ESSSettingsAls, *this );
       
   514 
       
   515     if( err == KErrNotSupported || err == KErrAlreadyExists )
       
   516         {
       
   517         //ALS not supported or already registered, that's fine
       
   518         err = KErrNone;
       
   519         }
       
   520 
       
   521     User::LeaveIfError( err );
       
   522 
       
   523 	//Start to listen profile changes.
       
   524 	delete iProfileNotifier;
       
   525     iProfileNotifier = NULL;	
       
   526 		
       
   527     iProfileNotifier = CProfileChangeNotifyHandler::NewL( this );
       
   528    	}
       
   529 
       
   530 // ---------------------------------------------------------------------------
       
   531 // Suspends the engine
       
   532 // ---------------------------------------------------------------------------
       
   533 //
       
   534 void CAiProfileEngine::Suspend()
       
   535     {
       
   536  	iSSSettings.CancelAll( *this );
       
   537     iSSSettings.Close();
       
   538    	delete iProfileNotifier;
       
   539    	iProfileNotifier = NULL;	
       
   540     }
       
   541 
       
   542 // ---------------------------------------------------------------------------
       
   543 // From class MProfileChangeObserver
       
   544 // ---------------------------------------------------------------------------
       
   545 //
       
   546 
       
   547 void CAiProfileEngine::HandleActiveProfileEventL(
       
   548 							TProfileEvent aProfileEvent,
       
   549 							TInt /*aProfileId*/ )
       
   550     {
       
   551 	//Profile activated or modified.
       
   552     if( ( aProfileEvent == EProfileNewActiveProfile ) ||
       
   553     	( aProfileEvent == EProfileActiveProfileModified ) )
       
   554         {
       
   555         UpdateProfileNamesL();
       
   556         iProfilePluginNotifier->NotifyContentUpdate();
       
   557         }
       
   558     }
       
   559     
       
   560 // ---------------------------------------------------------------------------
       
   561 // From class MSSSettingsObserver.
       
   562 // ---------------------------------------------------------------------------
       
   563 //
       
   564 void CAiProfileEngine::PhoneSettingChanged( 
       
   565 								TSSSettingsSetting aSetting,
       
   566 								TInt /*aNewValue*/ )
       
   567     {
       
   568     if( aSetting == ESSSettingsAls )
       
   569         {
       
   570         TRAP_IGNORE( UpdateProfileNamesL() );
       
   571         iProfilePluginNotifier->NotifyContentUpdate();
       
   572         }
       
   573     }
       
   574 
       
   575 
       
   576 TBool CAiProfileEngine::IsOffline()
       
   577 	{
       
   578 	return iProfileEngine->ActiveProfileId() == KOfflineProfileId;
       
   579 	}
       
   580