summaryrefslogtreecommitdiff
path: root/NSCmpts/EmptyRecycleBin.cpp
blob: 8dd09ddaf731310364318e89f7b1e8b50d85c431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// EmptyRecycleBin.cpp : Implementation of CEmptyRecycleBin
#include "stdafx.h"
#include "NSCmpts.h"
#include "EmptyRecycleBin.h"
//#include "DummyDlg.h"
#include "..\common\defines.h"
#include <appmisc.h>

#include "PromptClose.h"

/////////////////////////////////////////////////////////////////////////////
// CEmptyRecycleBin

STDMETHODIMP CEmptyRecycleBin::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_ISecureShutdownWin,
	};
	for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

LRESULT CEmptyRecycleBin::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	// Load Clear All State
	CheckDlgButton(IDC_EMPTYRECYCLE, m_Data.GetInt(ER_REG_RECYCLE, true));
	CheckDlgButton(IDC_EMPTYNORTON, m_Data.GetInt(ER_REG_NORTON, true));

	return FALSE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

STDMETHODIMP CEmptyRecycleBin::DoShutdown(long hParent, long lMode)
{
	HWND hwndParent = (HWND)hParent;

	bool bPrompt = lMode & nsNoPrompt ? false : true;
	bool bSilent = lMode & nsQuiet ? true : false;

	LPITEMIDLIST ppidl;
	CComPtr<IShellFolder> psfDesktop;
	CComPtr<IMalloc> pMalloc;


	// Get the Malloc Interface for freeing Mem later
	HRESULT hr = SHGetMalloc(&pMalloc);
	
	if(FAILED(hr))
		return hr; 

	// Get Desktop Folder. Recylce is always a child of Desktop
	hr = ::SHGetDesktopFolder(&psfDesktop);

	if(FAILED(hr))
		return hr;
	

	// Get ITEMID of Recycle
	hr = ::SHGetSpecialFolderLocation(hwndParent, 
	   								  CSIDL_BITBUCKET, // Recycle Bin
									  &ppidl);
	if(FAILED(hr))
		return hr;

	// Start Major Stuff
	CComPtr<IContextMenu> pcm;
	DWORD dwAttribs = 0;
	int idCmd = 0;
	HRESULT hRet = S_OK;
	HMENU hMenu;

	// Get Context Menu Interface on Recycle ITEM ID
	hr = psfDesktop->GetUIObjectOf (hwndParent,
									1, // get attributes for only one object
									(const struct _ITEMIDLIST **)&ppidl,
									IID_IContextMenu,
									0,
									(LPVOID *)&pcm);

	if (SUCCEEDED(hr)) 
	{
		// Even though we won't display it we have to create it
		hMenu = CreatePopupMenu();

		if (hMenu)
		{
			// Get the context menu items for recycle
			hr = pcm->QueryContextMenu (hMenu, 0, 1, 0x7fff, CMF_EXPLORE);

			if(FAILED(hr))
				hRet = hr;

			else 
			{

				// Did the user want to Recycle?
				if(m_Data.GetInt(ER_REG_RECYCLE, true))
				{
					hr = InvokeMenuCommand(pcm, hMenu, IDS_EMPTYCOMMAND, hwndParent, true);

					if (FAILED(hr))
						hRet = hr;
				}

				// Did the use want to clear Norton?
				if(m_Data.GetInt(ER_REG_NORTON, true))
				{
					hr = InvokeMenuCommand(pcm, hMenu, IDS_NORTONCOMMAND, hwndParent, true);

					if (FAILED(hr))
						hRet = hr;
				}
			}

			DestroyMenu (hMenu);

		}

	} 

	else 	
	{
		hRet = E_FAIL;
	}

	// Free Recycle Bin Item ID
	pMalloc->Free(ppidl);

	if(hRet != S_OK)
		if (!bSilent) 
			MessageBox(_T("Couldn't Empty Recycle Bin"), _T("Empty Recycle Bin"), MB_OK | MB_ICONEXCLAMATION);

	return hRet;

}

STDMETHODIMP CEmptyRecycleBin::get_Info(NightSecInfo nsItem, VARIANT* pvVal)
{
	::VariantClear(pvVal);

	CComBSTR bsRetVal;

	switch(nsItem)
	{
	case nsName:
		pvVal->vt = VT_BSTR;
		bsRetVal.LoadString(IDS_RECYCLENAME);
		pvVal->bstrVal = bsRetVal.Detach();
		return S_OK;
	case nsHelpText:
		pvVal->vt = VT_BSTR;
		bsRetVal.LoadString(IDS_RECYCLEDESC);
		pvVal->bstrVal = bsRetVal.Detach();
		return S_OK;
	case nsCmdLine:
		pvVal->vt = VT_BSTR;
		bsRetVal.LoadString(IDS_RECYCLEPARAM);
		pvVal->bstrVal = bsRetVal.Detach();
		return S_OK;
	}

	::VariantClear(pvVal);
	return S_FALSE;
}


STDMETHODIMP CEmptyRecycleBin::SetData(IUnknown* pUnk)
{
	return m_Data.Initialize(pUnk);
}


HRESULT InvokeMenuCommand(IContextMenu* pcm, HMENU hMenu, UINT nMenuItem, HWND hWndParent, bool bClosePrompt) 
{
	HRESULT hRet = S_OK;
	int idCmd = 0;

	// Get the ID
	idCmd = GetMenuIdFromString(hMenu, nMenuItem);

	// Only Proceed if We Found it
	if(!idCmd)
		return S_FALSE;

	UINT nMenuState = ::GetMenuState(hMenu, idCmd, MF_BYCOMMAND);

	if(!(nMenuState & MF_GRAYED || nMenuState & MF_DISABLED))
	{
		if(bClosePrompt) 
		{
			CPromptClose closer;
			closer.CloseNext(IDYES);
		}

		// Do the Stuff
		hRet = InvokeMenuId(pcm, idCmd, hWndParent);

		return hRet;
	}

	return S_FALSE;
}


//////////////////////////////////////////////////////////////////
int GetMenuIdFromString(HMENU hMenu, UINT nStrID)
{

	string sSearch;
	if(sSearch.load_string(nStrID))
		return GetMenuIdFromString(hMenu, sSearch);
	else 
		ASSERT(false); // Need a valid Resource ID
		
	return 0;
}


//////////////////////////////////////////////////////////////////
int GetMenuIdFromString(HMENU hMenu, string sSearch)
{

	string sMenuItem;
	sSearch.make_upper();

	for(int nCnt = 0; nCnt < ::GetMenuItemCount(hMenu); nCnt++) 
	{
		if(::GetMenuString(hMenu, nCnt, 
							sMenuItem.get_buffer(MAX_PATH), MAX_PATH, 
							MF_BYPOSITION))
		{
			sMenuItem.release_buffer();
			sMenuItem.make_upper();

			if(sMenuItem.find(sSearch) != string::npos) 
				return ::GetMenuItemID(hMenu, nCnt);

		}
	}

	return 0;
}


HRESULT InvokeMenuId(IContextMenu* pcm, int nIDCmd, HWND hWndParent)
{
	CMINVOKECOMMANDINFO cmi;

	// Setup for Invoking.
	cmi.cbSize = sizeof (CMINVOKECOMMANDINFO);

	cmi.fMask = CMIC_MASK_FLAG_NO_UI;

	cmi.hwnd = hWndParent;
	cmi.lpVerb = MAKEINTRESOURCEA (nIDCmd - 1);
	cmi.lpParameters = NULL;
	cmi.lpDirectory = NULL;
	cmi.nShow = SW_HIDE;
	cmi.dwHotKey = 0;
	cmi.hIcon = NULL;

	// Do the Stuff
	return pcm->InvokeCommand (&cmi);
}


//////////////////////////////////////////////////////////////////
// If the user is at the computer switching windows this won't 
// work, but that's okay because then the user can close the
// window
/*
static DWORD WINAPI ClosePromptWindow( LPVOID pParam )
{

	HWND hwndParent = (HWND)pParam;		// The Parent of the Prompt Window
	HWND hwndTop = ::GetTopLevel(hwndParent);// The Top Level Parent of the Prompt Window

	HWND hwndDlg = NULL;					// Will Hold the Final Prompt Window to Close
	DWORD dwStartCount = ::GetTickCount();

	// Find the Active Popup
	// Give it at least 20 seconds to come
	while((hwndDlg = ::GetLastActivePopup(hwndTop)) == hwndParent
			&& ::GetTickCount() < dwStartCount + 20000)
	{
	}

	// Error: Window Didn't Pop up in 20 seconds
	if(hwndDlg == hwndParent)
		return 1;

	SetActiveWindow(hwndTop);

	SetWindowPos(hwndDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

	keybd_event(VK_RETURN, 0, 0, 0);

	return 0;

}

HWND GetTopLevel(HWND hwnd) 
{
	while(::GetParent(hwnd))
		hwnd = ::GetParent(hwnd);

	return hwnd;

}
*/