commands/shutapp/shutapp.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // shutapp.cpp
       
     2 // 
       
     3 // Copyright (c) 2007 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <w32std.h>
       
    14 #include <apgtask.h>
       
    15 #include <apgwgnam.h>
       
    16 #include "shutapp.h"
       
    17 
       
    18 
       
    19 CCommandBase* CCmdShutApp::NewLC()
       
    20 	{
       
    21 	CCmdShutApp* self = new(ELeave) CCmdShutApp();
       
    22 	CleanupStack::PushL(self);
       
    23 	self->BaseConstructL();
       
    24 	return self;
       
    25 	}
       
    26 
       
    27 CCmdShutApp::~CCmdShutApp()
       
    28 	{
       
    29 	iWgIds.Close();
       
    30 	iWgNames.ResetAndDestroy();
       
    31 	}
       
    32 
       
    33 CCmdShutApp::CCmdShutApp()
       
    34 	: iTimeout(5)
       
    35 	{
       
    36 	}
       
    37 
       
    38 const TDesC& CCmdShutApp::Name() const
       
    39 	{
       
    40 	_LIT(KName, "shutapp");
       
    41 	return KName;
       
    42 	}
       
    43 
       
    44 void CCmdShutApp::DoRunL()
       
    45 	{
       
    46 	RWsSession wsSession;
       
    47 	User::LeaveIfError(wsSession.Connect());
       
    48 	CleanupClosePushL(wsSession);
       
    49 
       
    50 	const TInt numIds = iWgIds.Count();
       
    51 	for (TInt i = 0; i < numIds; ++i)
       
    52 		{
       
    53 		ShutL(wsSession, iWgIds[i]);
       
    54 		}
       
    55 
       
    56 	const TInt numNames = iWgNames.Count();
       
    57 	for (TInt i = 0; i < numNames; ++i)
       
    58 		{
       
    59 		const TDesC& name = *(iWgNames[i]);
       
    60 		TInt id = 0;
       
    61 		while ((id = wsSession.FindWindowGroupIdentifier(id, name)) != KErrNotFound)
       
    62 			{
       
    63 			ShutL(wsSession, id);
       
    64 			}
       
    65 		}
       
    66 
       
    67 	if (iAll)
       
    68 		{
       
    69 		TInt numWgs = wsSession.NumWindowGroups(0);
       
    70 		if (numWgs > 0)
       
    71 			{
       
    72 			CArrayFixFlat<TInt>* wgIds = new(ELeave) CArrayFixFlat<TInt>(numWgs);
       
    73 			CleanupStack::PushL(wgIds);
       
    74 			User::LeaveIfError(wsSession.WindowGroupList(0, wgIds));
       
    75 			numWgs = wgIds->Count();
       
    76 			for (TInt i = 0; i < numWgs; ++i)
       
    77 				{
       
    78 				ShutL(wsSession, (*wgIds)[i]);
       
    79 				}
       
    80 			CleanupStack::PopAndDestroy(wgIds);
       
    81 			}
       
    82 		}
       
    83 	CleanupStack::PopAndDestroy(&wsSession);
       
    84 	}
       
    85 	
       
    86 void CCmdShutApp::ShutL(RWsSession& aWsSession, TInt aWindowGroupId)
       
    87 	{
       
    88 	CApaWindowGroupName* wgn = CApaWindowGroupName::NewLC(aWsSession, aWindowGroupId);
       
    89 	if (iVerbose)
       
    90 		{
       
    91 		_LIT(KUnknown, "not named");
       
    92 		TPtrC caption(wgn->Caption());
       
    93 		if (caption.Length() == 0)
       
    94 			{
       
    95 			caption.Set(KUnknown);
       
    96 			}
       
    97 		Printf(_L("Shutting down %d (%S)\r\n"), aWindowGroupId, &caption);
       
    98 		}
       
    99 
       
   100 	TThreadId threadId;
       
   101 	User::LeaveIfError(aWsSession.GetWindowGroupClientThreadId(aWindowGroupId, threadId));
       
   102 
       
   103 	RThread thread;
       
   104 	User::LeaveIfError(thread.Open(threadId));
       
   105 	CleanupClosePushL(thread);
       
   106 
       
   107 	TRequestStatus threadStatus;
       
   108 	thread.Logon(threadStatus);
       
   109 	if (threadStatus != KRequestPending)
       
   110 		{
       
   111 		User::WaitForRequest(threadStatus);
       
   112 		User::Leave(KErrGeneral);
       
   113 		}
       
   114 
       
   115 	RTimer timer;
       
   116 	User::LeaveIfError(timer.CreateLocal());
       
   117 	CleanupClosePushL(timer);
       
   118 	TRequestStatus timeoutStatus;
       
   119 	timer.After(timeoutStatus, iTimeout * 1000000);
       
   120 
       
   121 	TApaTask task(aWsSession);
       
   122 	task.SetWgId(aWindowGroupId);
       
   123 #ifdef EKA2
       
   124 	if (iForce)
       
   125 		{
       
   126 		task.SendSystemEvent(EApaSystemEventSecureShutdown);
       
   127 		}
       
   128 	else
       
   129 		{
       
   130 		task.SendSystemEvent(EApaSystemEventShutdown);
       
   131 		}
       
   132 #else
       
   133 	task.SendSystemEvent(EApaSystemEventShutdown);
       
   134 #endif
       
   135 
       
   136 	User::WaitForRequest(threadStatus, timeoutStatus);
       
   137 
       
   138 	if (timeoutStatus != KRequestPending)
       
   139 		{
       
   140 		// Timeout occurred.
       
   141 		thread.LogonCancel(threadStatus);
       
   142 		User::WaitForRequest(threadStatus);
       
   143 		User::Leave(KErrTimedOut);
       
   144 		}
       
   145 	else
       
   146 		{
       
   147 		timer.Cancel();
       
   148 		User::WaitForRequest(timeoutStatus);
       
   149 		}
       
   150 
       
   151 	CleanupStack::PopAndDestroy(3, wgn);
       
   152 	}
       
   153 
       
   154 void CCmdShutApp::OptionsL(RCommandOptionList& aOptions)
       
   155 	{
       
   156 	_LIT(KOptWgId, "id");
       
   157 	aOptions.AppendUintL(iWgIds, KOptWgId);
       
   158 
       
   159 	_LIT(KOptWgPattern, "match");
       
   160 	aOptions.AppendStringL(iWgNames, KOptWgPattern);
       
   161 
       
   162 	_LIT(KOptTimeout, "timeout");
       
   163 	aOptions.AppendIntL(iTimeout, KOptTimeout);
       
   164 
       
   165 	_LIT(KOptVerbose, "verbose");
       
   166 	aOptions.AppendBoolL(iVerbose, KOptVerbose);
       
   167 
       
   168 	_LIT(KOptAll, "all");
       
   169 	aOptions.AppendBoolL(iAll, KOptAll);
       
   170 
       
   171 #ifdef EKA2
       
   172 	_LIT(KOptForce, "force");
       
   173 	aOptions.AppendBoolL(iForce, KOptForce);
       
   174 #endif
       
   175 	}
       
   176 
       
   177 void CCmdShutApp::ArgumentsL(RCommandArgumentList&)
       
   178 	{
       
   179 	}
       
   180 
       
   181 
       
   182 #ifdef EXE_BUILD
       
   183 EXE_BOILER_PLATE(CCmdShutApp)
       
   184 #endif
       
   185