commonuisupport/uikon/coresrc/EIKSHUT.CPP
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 // Copyright (c) 1997-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 // CEikShutter
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "EIKSHUT.H"
       
    19 #include <eikenv.h>
       
    20 #include <eikappui.h>
       
    21 #include <basched.h>
       
    22 #include <coemain.h>
       
    23 #include <uikon.hrh>
       
    24 
       
    25 const TUid KEikShutterSingletonUid = {0x1028306A};
       
    26 
       
    27 NONSHARABLE_CLASS(CLocalScheduler) : public CBaActiveScheduler
       
    28 	{
       
    29 public:
       
    30 	inline TInt PublicLevel() const { return Level(); }
       
    31 	};
       
    32 
       
    33 const TInt KMaxNumEscKeysToSend=50;
       
    34 
       
    35 void CEikShutter::DeferredExecuteL(CEikonEnv& aEikEnv)
       
    36 	{
       
    37 	if(aEikEnv.FindStatic(KEikShutterSingletonUid))
       
    38 		return;	// a shutter is already invoked with the intention of closing this app
       
    39 			
       
    40 	new(ELeave) CEikShutter(aEikEnv, KNullUid, NULL);
       
    41 	}
       
    42 
       
    43 void CEikShutter::DeferredExecuteL(CEikonEnv& aEikEnv, TUid aMessageUid, const TDesC8& aMessageParameters)
       
    44 	{
       
    45 	if(aEikEnv.FindStatic(KEikShutterSingletonUid))
       
    46 		return;	// a shutter is already invoked with the intention of closing this app
       
    47 		
       
    48 	new(ELeave) CEikShutter(aEikEnv, aMessageUid, aMessageParameters.AllocLC());
       
    49 	CleanupStack::Pop(); // aMessageParameters.AllocLC()
       
    50 	}
       
    51 
       
    52 CEikShutter::CEikShutter(CEikonEnv& aEikEnv, TUid aMessageUid, HBufC8* aMessageParameters)
       
    53 	:CActive(EActivePriorityWsEvents+1), CCoeStatic(KEikShutterSingletonUid, CCoeStatic::EThread),
       
    54 	 iEikEnv(aEikEnv),
       
    55 	 iMessageUid(aMessageUid),
       
    56 	 iMessageParameters(aMessageParameters),
       
    57 	 iFlags(0),
       
    58 	 iCount(0),
       
    59 	 iStartLevel(StartLevel())
       
    60 	{
       
    61 	CActiveScheduler::Add(this);
       
    62 	Queue();
       
    63 	}
       
    64 
       
    65 TInt CEikShutter::StartLevel()
       
    66 	{
       
    67 	return ((CLocalScheduler*)CActiveScheduler::Current())->PublicLevel();
       
    68 	}
       
    69 
       
    70 CEikShutter::~CEikShutter()
       
    71 	{
       
    72 	Cancel();
       
    73 	delete iMessageParameters;
       
    74 	}
       
    75 
       
    76 void CEikShutter::DoCancel()
       
    77 	{
       
    78 	}
       
    79 
       
    80 void CEikShutter::Queue()
       
    81 	{
       
    82 	TRequestStatus *pS=(&iStatus);
       
    83 	User::RequestComplete(pS,0);
       
    84 	SetActive();
       
    85 	}
       
    86 
       
    87 void CEikShutter::Terminate()
       
    88 	{
       
    89 	if (iFlags&ETryingToTerminateApp)
       
    90 		{
       
    91 		Cancel();
       
    92 		iFlags|=EDestroySelfPending;
       
    93 		}
       
    94 	else
       
    95 		delete this;
       
    96 	}
       
    97 
       
    98 void CEikShutter::RunL()
       
    99 	{
       
   100 	if (iCount++>KMaxNumEscKeysToSend)
       
   101 		{
       
   102 		Terminate();
       
   103 		return;
       
   104 		}
       
   105 	TInt startLevel=StartLevel();
       
   106 	if (startLevel>iStartLevel)
       
   107 		{
       
   108 		Terminate();
       
   109 		return;
       
   110 		}
       
   111 	iStartLevel=startLevel;
       
   112 	CEikAppUi* appUi=iEikEnv.EikAppUi();
       
   113 	const TBool displayingDialog=appUi->IsDisplayingDialog();
       
   114 	if (appUi->IsDisplayingMenuOrDialog() && (displayingDialog || !(iFlags&ETriedDismissMenu)))
       
   115 		{
       
   116 		if (!displayingDialog)
       
   117 			{
       
   118 			iFlags|=ETriedDismissMenu;
       
   119 			}
       
   120 		Queue(); // before any call to CActiveScheduler::Start() from below
       
   121 		TKeyEvent key;
       
   122 		key.iCode=EKeyEscape;
       
   123 		key.iScanCode=0;
       
   124 		key.iModifiers=0;
       
   125 		key.iRepeats=0;
       
   126 		iEikEnv.SimulateKeyEventL(key,EEventKey);
       
   127 		}
       
   128 	else if (appUi->ContainerAppUi() || (iMessageParameters==NULL))
       
   129 		{
       
   130 		iFlags&=~ETriedDismissMenu;
       
   131 		Queue(); // before any call to CActiveScheduler::Start() from below
       
   132 		iFlags |= ETryingToTerminateApp;
       
   133 		TRAPD(error, appUi->HandleCommandL(EEikCmdExit)); // will leave when really exits
       
   134 				
       
   135 		iFlags &= ~ETryingToTerminateApp;
       
   136 		User::LeaveIfError(error);
       
   137 		if (iFlags&EDestroySelfPending)
       
   138 			{
       
   139 			iFlags&=~EDestroySelfPending;
       
   140 			delete this;
       
   141 			return;
       
   142 			}
       
   143 		}
       
   144 	else
       
   145 		{ // don't call Queue() in this case, else crash if switch files throws up dialog with wait
       
   146 		appUi->ProcessMessageL(iMessageUid, *iMessageParameters);
       
   147 		Terminate();
       
   148 		return;
       
   149 		}
       
   150 	}
       
   151 
       
   152 TInt CEikShutter::RunError(TInt aError)
       
   153 	{
       
   154 	Terminate();
       
   155 	return aError;
       
   156 	}