summaryrefslogtreecommitdiff
path: root/NetCmpts/ClearInetCache.cpp
blob: b4d5b4da05fc40c00e0295de065d2331f40b324f (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// ClearInetCache.cpp : Implementation of CClearInetCache
#include "stdafx.h"
#include "NetCmpts.h"
#include "ClearInetCache.h"
#include "ProgressDlg.h"
#include "..\common\defines.h"
#include <appmisc.h>
#include "parseurl.h"

#include <algorithm>
using std::find;

/////////////////////////////////////////////////////////////////////////////
// CClearInetCache

STDMETHODIMP CClearInetCache::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;
}

//////////////////////////////////////////////////////////////////
//  

#ifndef MAX_CACHE_ENTRY_INFO_SIZE
#define MAX_CACHE_ENTRY_INFO_SIZE 4096
#endif

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

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

	// Return Value
	HRESULT hRet = S_OK;

	MSG msg;

	// Handle for Searching
	HANDLE hFindURL;
	// Structure for Cache Info
	LPINTERNET_CACHE_ENTRY_INFO lpICEInfo;
	// We use MAX_CACHE_ENTRY_INFO_SIZE so we don't get ERROR_INSUFFICIENT_BUFFER's
	unsigned long nBufferSize = MAX_CACHE_ENTRY_INFO_SIZE;

	// Alloc Memory (Use malloc for readability)
	lpICEInfo = (LPINTERNET_CACHE_ENTRY_INFO)malloc(sizeof(INTERNET_CACHE_ENTRY_INFO) + MAX_CACHE_ENTRY_INFO_SIZE);
	if(lpICEInfo == NULL)
		return E_OUTOFMEMORY;


	lpICEInfo->dwStructSize = sizeof(INTERNET_CACHE_ENTRY_INFO);
	lpICEInfo->dwReserved = 0;		// Always has to be zero

	// Find First
	hFindURL =  FindFirstUrlCacheEntry (_T("*.*"), lpICEInfo, &nBufferSize);

	if(hFindURL != NULL) 
	{ 
		// Create Progress dialog
		CProgressDlg dlgProg;
		dlgProg.Create(hwndParent);
		dlgProg.DispEventAdvise(m_spUnkSite);	// Pointer from IObjectWithSite
		dlgProg.m_ctlProgress.SetRange(0, 2);
		dlgProg.m_ctlProgress.SetStep(1);

		bool bLoop = true;

		while(bLoop) 
		{

			// Update Progress Dialog
			dlgProg.SetDlgItemText(IDC_FILENAME, lpICEInfo->lpszSourceUrlName);
			dlgProg.m_ctlProgress.SetPos(0);

			// Check if we should delete this URL
			if(IsDeleteableURL(lpICEInfo->lpszSourceUrlName)) 
			{
				// Keep going even if error
				DeleteUrlCacheEntry(lpICEInfo->lpszSourceUrlName);

				// Update Progress Dialog
				dlgProg.m_ctlProgress.SetPos(2);
			}

			// nBufferSize Gets changed in FindNext... funcs
			// so have to reset
			nBufferSize = MAX_CACHE_ENTRY_INFO_SIZE;

			// Get Next One
			if(!((bLoop = FindNextUrlCacheEntry(hFindURL, lpICEInfo, &nBufferSize)) ? true : false))
			{
				DWORD dwError = ::GetLastError();
				switch(::GetLastError())
				{
				case ERROR_NO_MORE_FILES:
				case ERROR_NO_MORE_ITEMS:
					hRet = S_OK;
					break;
				// For Debugging
				case ERROR_INSUFFICIENT_BUFFER:
					hRet = E_OUTOFMEMORY;
					break;
				default:
					hRet = HRESULT_FROM_WIN32(::GetLastError());
					break;
				}
			}

			PEEK_ALL_MESSAGES(msg);

			// Check for Cancel
			if(dlgProg.IsCancelled())
			{
				bLoop = false;
				hRet = E_ABORT;
			}

		} // while bLoop

		// Clean up time
		FindCloseUrlCache(hFindURL);
		free(lpICEInfo);

		if(dlgProg.IsWindow())
			dlgProg.DestroyWindow();

		dlgProg.DispEventUnadvise(m_spUnkSite);	 // Pointer from IObjectWithSite

		return hRet;
	
	} // if handle is null

	// Clean up
	free(lpICEInfo);

	switch(::GetLastError())
	{
		case ERROR_NO_MORE_ITEMS:
		case ERROR_NO_MORE_FILES:
			return S_OK;
			break;
		default:
			return HRESULT_FROM_WIN32(::GetLastError());
			break;
	}

}

//////////////////////////////////////////////////////////////////
// Load Server List from Registry

int CClearInetCache::GetServersToDelete()
{
	USES_CONVERSION;

	LoadFlags();

	int nCnt = 0;		// Number of URLs from Registry
	int nReadServers = 0;	// Number of 'sane' URLs
	string sURL = "";
	string sKeyName = "";

	// Format Key Name
	sKeyName.format("Server%.4d", nCnt);

	// Used in URL Parsing
	DWORD dwProtocol = 0;
	string sServer = "";
	string sObject = "";
	INTERNET_PORT inetPort;

	// Add URLs to List Box
	while((sURL = m_Data.GetString(sKeyName, NO_KEY)) != NO_KEY) 
	{
		// Get the Host/Server Part
		if(ParseURL(sURL, dwProtocol, sServer, sObject, inetPort)) 
		{
			// Add to Array
			m_asServerURLs.push_back(sServer);
			nReadServers++;
		}

		nCnt++;

		// Format Key Name
		sKeyName.format("Server%.4d", nCnt);

	}

	m_bInitialized = true;

	return nReadServers;
}

//////////////////////////////////////////////////////////////////
// Checks a URL against the list 

bool CClearInetCache::IsDeleteableURL(const string& sURL)
{
	// Sanity Check
	if(sURL.empty())
		return false;


	// Read Servers from Registry
	if(!m_bInitialized)

		// Only continue of more than one server
		GetServersToDelete();
 


	// Used in URL Parsing
	DWORD dwProtocol = 0;
	string sServer = "";
	string sObject = "";
	INTERNET_PORT inetPort;

	// Get the Host/Server Part
	if(ParseURL(sURL, dwProtocol, sServer, sObject, inetPort))
	{
		if(dwProtocol == INTERNET_SCHEME_UNKNOWN)
			return m_bCookies;

		if(m_bClearAll)
			return true;

		// Check it against our array
		return find(m_asServerURLs.begin(), m_asServerURLs.end(), sServer) 
				!= m_asServerURLs.end();
	
	}

	return false;

}

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

	CComBSTR bsRetVal;

	switch(nsItem)
	{
	case nsName:
		pvVal->vt = VT_BSTR;
		bsRetVal.LoadString(IDS_INETNAME);
		pvVal->bstrVal = bsRetVal.Detach();
		return S_OK;
	case nsHelpText:
		pvVal->vt = VT_BSTR;
		bsRetVal.LoadString(IDS_INETDESC);
		pvVal->bstrVal = bsRetVal.Detach();
		return S_OK;
	case nsCmdLine:
		pvVal->vt = VT_BSTR;
		bsRetVal.LoadString(IDS_INETPARAM);
		pvVal->bstrVal = bsRetVal.Detach();
		return S_OK;
	case nsHideNormal:
		pvVal->vt = VT_BOOL;
		pvVal->bVal = TRUE;
		return S_OK;
	}

	::VariantClear(pvVal);
	return S_FALSE;
}


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








/////////////////////////////////////////////////////////////////////
//
//	Property Sheet
//
/////////////////////////////////////////////////////////////////////

HRESULT CClearInetCache::SaveChanges()
{
	int nServer = 0;
	string sKeyName = "";
	string sURL;
	int iItem = -1;

	// Setup Structures
	LV_ITEM lv;

	lv.mask = LVIF_TEXT;
	lv.iSubItem = 0;
	lv.cchTextMax = MAX_PATH * 2;

	HRESULT hr;
	HRESULT hrRet = S_OK;

	// Loop through selected items
	while((iItem = m_ctlServerList.GetNextItem(iItem, LVNI_ALL)) != -1) 
	{
		lv.iItem = iItem;
		lv.iSubItem = 0;
		lv.pszText = sURL.get_buffer(MAX_PATH * 2);

		if(m_ctlServerList.GetItem(&lv)) 
		{
			// Format Registry Key
			sKeyName.format("Server%.4d", nServer);

			sURL.release_buffer();

			hr = m_Data.WriteString(sKeyName, sURL);
			if(FAILED(hr)) hrRet = hr;

		}

		nServer++;
	
	}

	for(nServer = nServer; nServer < m_nLoadedURLs; nServer++) 
	{
		// Format Registry Key
		sKeyName.format("Server%.4d", nServer);

		m_Data.DeleteProperty(sKeyName);
	}


	// Save Clear All State
	hr = m_Data.WriteInt("Clear All", IsDlgButtonChecked(IDC_CLEARALLSERVERS));
	if(FAILED(hr)) hrRet = hr;
		
	// Save Cookie State
	hr = m_Data.WriteInt("Clear Cookies", IsDlgButtonChecked(IDC_COOKIES));
	if(FAILED(hr)) hrRet = hr;

	return hrRet;
}

LRESULT CClearInetCache::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	LoadFlags();


	m_ctlServerList = GetDlgItem(IDC_SERVERURLLIST);

	// Create the small icon image list.
	m_ImageListSmall.Create(16,
			16,
			ILC_MASK,		// list does not include masks
			1,
			0 );			// list won't grow

	
	// Set Mask color for Image Lists
	m_ImageListSmall.SetBkColor(::GetSysColor(COLOR_WINDOW));

	
	// Load the icon and add to the image list.
	if(m_ImageListSmall.AddIcon(::LoadIcon(_Module.m_hInstResource, 
										   MAKEINTRESOURCE(IDI_INETSHORTCUT))) == -1)
		return false;
		

	// Associate the image lists with the list view control.
	m_ctlServerList.SetImageList(m_ImageListSmall, LVSIL_SMALL);

	
	// Fill List Control
	string sURL;
	string sKeyName = "";
	int nCnt = 0;

	// Format Key Name
	sKeyName.format("Server%.4d", nCnt);

	while((sURL = m_Data.GetString(sKeyName, NO_KEY)) != NO_KEY) 
	{
		m_ctlServerList.InsertItem(nCnt, sURL, 0);

		// Make sure each one has a unique lParam
		m_dwIDCounter++;
		nCnt++;

		// Format Key Name
		sKeyName.format("Server%.4d", nCnt);
	}

	m_nLoadedURLs = nCnt + 1;

	CheckDlgButton(IDC_CLEARALLSERVERS, m_bClearAll);
	CheckDlgButton(IDC_COOKIES, m_bCookies);
	UpdateClearAll();

	m_hIconNew = ::LoadIcon(_Module.m_hInstResource, MAKEINTRESOURCE(IDI_NEW));
	m_hIconDel = ::LoadIcon(_Module.m_hInstResource, MAKEINTRESOURCE(IDI_DEL));

	CButton btn = GetDlgItem(IDC_NEWURL);
	btn.SetIcon(m_hIconNew);

	btn = GetDlgItem(IDC_DELETE);
	btn.SetIcon(m_hIconDel);
	
	return false;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE

}



LRESULT CClearInetCache::OnURLListEndLabelEdit(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pnmh;
	
	// Only Change if there is Text
	if(pDispInfo->item.pszText != NULL)
	{
		// Used in Checking URL
		DWORD dwProtocol = 0;
		string sTemp1 = "";
		string sTemp2 = "";
		INTERNET_PORT inetPort;


		// Do a Check on URL
		if(ParseURL(pDispInfo->item.pszText, dwProtocol, sTemp1, sTemp2, inetPort))
		{
			// Set Item text
			m_ctlServerList.SetItem(&(pDispInfo->item));
			SetDirty(true);
		}
		else 
		{
			// Try adding http://
			string sNewURL(_T("http://"));
			sNewURL += pDispInfo->item.pszText;
			if(ParseURL(sNewURL, dwProtocol, sTemp1, sTemp2, inetPort))
			{
				// Set Item text
				pDispInfo->item.pszText = sNewURL.get_buffer();
				m_ctlServerList.SetItem(&(pDispInfo->item));
				SetDirty(true);
			}
			else 
			{
				// Give an Error Box
				string sMsg;
				sMsg.load_string(IDS_NOTVALIDURL);
				MessageBox(sMsg, "Night Security", MB_ICONSTOP | MB_OK);
			}

		}
	}
	
	return 0;

}


LRESULT CClearInetCache::OnURLListKeyDown(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pnmh;

	// if F2 then Edit
	if(pLVKeyDow->wVKey == VK_F2) 
	{
		m_ctlServerList.SetFocus(); // Needs to have the Focus in order to edit
		m_ctlServerList.EditLabel(m_ctlServerList.GetNextItem(-1, LVNI_ALL | LVNI_FOCUSED));
	}

	// If Delete then call Delete Handler
	if(pLVKeyDow->wVKey == VK_DELETE) 
		OnDelete(0, 0, 0, bHandled);
	
	return 0;

}

LRESULT CClearInetCache::OnListSetFocus(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	// Disclaimer: This is a kludge fix
	// SysListView32 wouldn't redraw itself when it had focus 
	// and app was activated 
	m_ctlServerList.RedrawWindow(NULL, NULL, RDW_FRAME | RDW_INVALIDATE);
	return 0;
}


LRESULT CClearInetCache::OnNewURL(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	int iItem = -1;
	string sNewURL;
	sNewURL.load_string(IDS_NEWURL);

	// Insert Item
	if((iItem = m_ctlServerList.InsertItem(0, sNewURL, 0)) != -1) 
	{
		m_dwIDCounter++;

		// Open for Editing
		m_ctlServerList.SetFocus();	// Needs to have the Focus in order to edit
		m_ctlServerList.EditLabel(iItem);

		SetDirty(true);

	}

	return 0;

}

LRESULT CClearInetCache::OnDelete(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	int iItem = -1;
	
	LV_ITEM lv;

	lv.mask = LVIF_TEXT;
	lv.iSubItem = 0;
	
	// Loop through selected items
	while((iItem = m_ctlServerList.GetNextItem(-1, LVNI_ALL | LVNI_SELECTED)) != -1) 
	{
		// Take it out of List Box
		m_ctlServerList.DeleteItem(iItem);
		SetDirty(true);
	}

	return 0;

}


LRESULT CClearInetCache::OnClearAll(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	UpdateClearAll();

	SetDirty(true);

	return 0;
}

void CClearInetCache::UpdateClearAll()
{
	bool bEnable = !IsDlgButtonChecked(IDC_CLEARALLSERVERS);

	::EnableWindow(GetDlgItem(IDC_SERVERURLLIST), bEnable);
	::EnableWindow(GetDlgItem(IDC_NEWURL), bEnable);
	::EnableWindow(GetDlgItem(IDC_DELETE), bEnable);

	SetDirty(true);
}

void CClearInetCache::LoadFlags()
{
	m_bClearAll = m_Data.GetInt("Clear All", true) ? true : false;
	m_bCookies = m_Data.GetInt("Clear Cookies", false) ? true : false;
}