sysstatemgmt/systemstatereferenceplugins/custcmd/src/cmdpublishsimownedandchanged.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19  @released
       
    20 */
       
    21 
       
    22 #include "ssmdebug.h"
       
    23 #include "ssmuiproviderdll.h"
       
    24 
       
    25 #include "cmdpublishsimownedandchanged.h"
       
    26 
       
    27 #include <centralrepository.h> 
       
    28 #include <e32property.h>
       
    29 #include <ssm/startupdomainpskeys.h>
       
    30 
       
    31 /**
       
    32 * Used for storing the IMSI code of current SIM card. This is needed for
       
    33 * determining if SIM card has changed from the previous boot.
       
    34 * Key value is an descriptor containing the IMSI code.
       
    35 */
       
    36 const TUint32 KSSMStoredImsi = 0x00000001;
       
    37 const TUid KCRUidSSMVals = {0x2001d2aa};
       
    38 
       
    39 CCustomCmdPublishSimOwnedAndChanged* CCustomCmdPublishSimOwnedAndChanged::NewL()
       
    40 	{
       
    41 	CCustomCmdPublishSimOwnedAndChanged* self = new (ELeave) CCustomCmdPublishSimOwnedAndChanged();
       
    42 	CleanupStack::PushL(self);
       
    43 	self->ConstructL();
       
    44 	CleanupStack::Pop(self);
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 CCustomCmdPublishSimOwnedAndChanged::CCustomCmdPublishSimOwnedAndChanged()
       
    49 									: CActive(EPriorityStandard),
       
    50 									iState(EPublishSimOwnedAndChangedIdle)
       
    51 	{
       
    52 	}
       
    53 
       
    54 void CCustomCmdPublishSimOwnedAndChanged::ConstructL()
       
    55 	{
       
    56     TInt errorCode = iServer.Connect();
       
    57 	if(KErrNone != errorCode)
       
    58 		{
       
    59 		DEBUGPRINT2A("Failed to connect to TelServer with error %d", errorCode);
       
    60 		User::Leave(errorCode);
       
    61 		}
       
    62 
       
    63     iTsyModuleName = CSsmUiSpecific::GetTsyModuleNameL();
       
    64     errorCode = iServer.LoadPhoneModule(*iTsyModuleName);
       
    65 
       
    66     if (KErrNone != errorCode && KErrAlreadyExists != errorCode)
       
    67         {
       
    68         // KErrAlreadyExists may be returned if some other has already loaded
       
    69         // the TSY module.
       
    70         DEBUGPRINT2A("Failed to load phone module with error %d", errorCode);
       
    71         User::Leave(errorCode);
       
    72         }
       
    73 
       
    74     HBufC* tsyPhoneName = CSsmUiSpecific::PhoneTsyNameL();
       
    75     errorCode = iPhone.Open(iServer, *tsyPhoneName);
       
    76     delete tsyPhoneName;
       
    77 
       
    78     if(KErrNone != errorCode)
       
    79     	{
       
    80     	DEBUGPRINT2A("Failed to open phone subsession with error %d", errorCode);
       
    81     	User::Leave(errorCode);	
       
    82     	}
       
    83 
       
    84 	//Add active object to active scheduler
       
    85 	CActiveScheduler::Add(this);
       
    86 	}
       
    87 
       
    88 CCustomCmdPublishSimOwnedAndChanged::~CCustomCmdPublishSimOwnedAndChanged()
       
    89 	{
       
    90 	ExecuteCancel();
       
    91 	
       
    92     iPhone.Close();
       
    93     if (iServer.Handle())
       
    94         {
       
    95         iServer.UnloadPhoneModule(*iTsyModuleName);
       
    96         iServer.Close();
       
    97         }
       
    98     delete iTsyModuleName;
       
    99 	}
       
   100 
       
   101 TInt CCustomCmdPublishSimOwnedAndChanged::Initialize(CSsmCustomCommandEnv* /*aCmdEnv*/)
       
   102 	{
       
   103 	//Connect to RSsmSimAdaptation
       
   104 	return iSsmSimAdaptation.Connect();
       
   105  	}
       
   106 
       
   107 void CCustomCmdPublishSimOwnedAndChanged::Close()
       
   108 	{
       
   109 	//Close RSsmSimRfAdaptation
       
   110 	iSsmSimAdaptation.Close();
       
   111 	}
       
   112 
       
   113 void CCustomCmdPublishSimOwnedAndChanged::Release()
       
   114 	{
       
   115 	delete this;
       
   116 	}
       
   117 
       
   118 void CCustomCmdPublishSimOwnedAndChanged::Execute(const TDesC8& /*aParams*/, TRequestStatus& aStatus)
       
   119 	{
       
   120 	//Set the user request to pending
       
   121 	aStatus = KRequestPending;
       
   122 	iExecuteRequest = &aStatus;
       
   123 	
       
   124 	// Request SIM owned status
       
   125 	iSsmSimAdaptation.GetSimOwned(iBooleanPckg, iStatus);
       
   126 	SetActive();
       
   127 
       
   128 	iState = EPublishSimOwned;
       
   129 	}
       
   130 
       
   131 void CCustomCmdPublishSimOwnedAndChanged::ExecuteCancel()
       
   132 	{
       
   133 	Cancel();
       
   134 	}
       
   135 
       
   136 void CCustomCmdPublishSimOwnedAndChanged::RunL()
       
   137 	{
       
   138 	DEBUGPRINT1A("CCustomCmdPublishSimOwnedAndChanged::RunL");
       
   139     switch (iState)
       
   140         {
       
   141         case EPublishSimOwned:
       
   142         	SimOwned();
       
   143             break;
       
   144         case EPublishSimChanged:
       
   145         	SimChanged();
       
   146             break;
       
   147         default:
       
   148         	DEBUGPRINT2A("CSsmSimStatusObserver unknown internal state: %d", iState);
       
   149         	CompleteClientRequest(KErrArgument);
       
   150             break;
       
   151         }
       
   152 	}
       
   153 
       
   154 void CCustomCmdPublishSimOwnedAndChanged::SimOwned()
       
   155 	{
       
   156 	// Default to SIM not being owned
       
   157 	TBool simOwned = EFalse;
       
   158 	if(KErrNone != iStatus.Int())
       
   159 		{
       
   160 		DEBUGPRINT2A("CSsmSimStatusObserver error getting SIM owned: %d", iStatus.Int());
       
   161 		// Go with default of SIM not being owned.
       
   162 		simOwned = EFalse;
       
   163 		}
       
   164 	else
       
   165 		{
       
   166 		simOwned = iBooleanPckg();
       
   167 		}
       
   168 	
       
   169 	// Publish SIM owned status
       
   170 	TUid simStatusUid = CSsmUiSpecific::StartupPSUid(); 
       
   171 	TInt err = RProperty::Set(simStatusUid, KPSSimOwned, simOwned ? ESimOwned : ESimNotOwned);
       
   172 	if(KErrNone != err)
       
   173 		{
       
   174 		DEBUGPRINT2A("CSsmSimStatusObserver error publishing SIM owned: %d", err);
       
   175 		CompleteClientRequest(err);
       
   176 		return;
       
   177 		}
       
   178 	
       
   179 	//Request for SIM changed status
       
   180     TUint32 caps = 0;
       
   181     TInt errorCode = 0;
       
   182 #ifdef TEST_CLAYER_MACRO
       
   183     errorCode = KErrNone;
       
   184 #else
       
   185     errorCode = iPhone.GetIdentityCaps(caps);
       
   186 #endif
       
   187     if (errorCode == KErrNone )
       
   188         {
       
   189         if (caps | RMobilePhone::KCapsGetSubscriberId)
       
   190             {
       
   191             iPhone.GetSubscriberId(iStatus, iSubscriberId);
       
   192             SetActive();
       
   193         	iState = EPublishSimChanged;
       
   194             }
       
   195         else
       
   196             {
       
   197             CompleteClientRequest(KErrNotSupported);
       
   198             }
       
   199         }
       
   200     else
       
   201         {
       
   202         CompleteClientRequest(errorCode);
       
   203         }
       
   204 	}
       
   205 
       
   206 void CCustomCmdPublishSimOwnedAndChanged::SimChanged()
       
   207 	{
       
   208 	// Default to SIM being changed
       
   209 	TBool simChanged = ETrue;
       
   210 	if(KErrNone == iStatus.Int())
       
   211 		{
       
   212 		simChanged = (CompareToLastStoredImsi(iSubscriberId) != 0);
       
   213 		}
       
   214 	else
       
   215 		{
       
   216 		// Couldn't get status so go with default of SIM having changed.
       
   217 		DEBUGPRINT2A("CSsmSimStatusObserver error getting SIM changed: %d", iStatus.Int());
       
   218 		}
       
   219 
       
   220 	// Publish SIM changed status
       
   221 	TUid simStatusUid = CSsmUiSpecific::StartupPSUid(); 
       
   222 	TInt err = RProperty::Set(simStatusUid, KPSSimChanged, simChanged ? ESimChanged : ESimNotChanged);
       
   223 	if(KErrNone != err)
       
   224 		{
       
   225 		DEBUGPRINT2A("CSsmSimStatusObserver error publishing SIM changed: %d", err);
       
   226 		CompleteClientRequest(err);
       
   227 		return;		
       
   228 		}
       
   229 
       
   230     CompleteClientRequest(iStatus.Int());
       
   231 	}
       
   232 
       
   233 void CCustomCmdPublishSimOwnedAndChanged::DoCancel()
       
   234 	{
       
   235 	iSsmSimAdaptation.GetCancel();
       
   236     iPhone.CancelAsyncRequest(EMobilePhoneGetSubscriberIdCancel);
       
   237     CompleteClientRequest(KErrCancel);
       
   238 	}
       
   239 
       
   240 TInt CCustomCmdPublishSimOwnedAndChanged::RunError(TInt aError)
       
   241 	{
       
   242 	//Call cancel method of CActive	
       
   243 	Cancel();
       
   244 	CompleteClientRequest(aError);
       
   245 	return KErrNone;
       
   246 	}
       
   247 
       
   248 void CCustomCmdPublishSimOwnedAndChanged::ReadAndUpdateStoredImsiL(
       
   249     RMobilePhone::TMobilePhoneSubscriberId& aReadValue,
       
   250     const RMobilePhone::TMobilePhoneSubscriberId aUpdateValue)
       
   251     {
       
   252     CRepository* repository = CRepository::NewLC(KCRUidSSMVals);
       
   253     User::LeaveIfError(repository->Get(KSSMStoredImsi, aReadValue));
       
   254     User::LeaveIfError(repository->Set(KSSMStoredImsi, aUpdateValue));
       
   255     CleanupStack::PopAndDestroy(repository);
       
   256     }
       
   257 
       
   258 TInt CCustomCmdPublishSimOwnedAndChanged::CompareToLastStoredImsi(
       
   259     const RMobilePhone::TMobilePhoneSubscriberId aNewValue)
       
   260     {
       
   261     RMobilePhone::TMobilePhoneSubscriberId oldValue;
       
   262     TRAPD(errorCode, ReadAndUpdateStoredImsiL(oldValue,aNewValue));
       
   263     if(KErrNone != errorCode)
       
   264     	{
       
   265     	DEBUGPRINT2A("Failed to read the stored IMSI code", errorCode);
       
   266     	return errorCode;
       
   267     	}
       
   268     
       
   269     DEBUGPRINT3A("Current IMSI: '%S', stored IMSI: '%S'", &aNewValue, &oldValue);
       
   270     return aNewValue.Compare(oldValue);
       
   271     }
       
   272 
       
   273 void CCustomCmdPublishSimOwnedAndChanged::CompleteClientRequest(TInt aReason)
       
   274 	{
       
   275 	//Complete client request with reason code
       
   276 	if (iExecuteRequest)
       
   277         {
       
   278         User::RequestComplete(iExecuteRequest, aReason);
       
   279         }
       
   280 	}