'Script Copyright  2012 Ryan Marshall (Skarn)
'All Rights Reserved

Const HKEY_USERS = &H80000003

Dim objFolder
Dim strUserSID

Sub dxInit()
	'initialize the sink object - if it hasn't been already
	DesktopX.ScriptObject("dxEdit_sink").Object.Name = "dxEdit_sink"
	
	'create the swebm object to receive events.
	Set objWbemSink = DesktopX.ScriptObject("dxEdit_sink").swebm_Create(Object.Name)
	
	'read the current username and domain
	Set wshShell = CreateObject("WScript.Shell")
	strUsername = wshShell.ExpandEnvironmentStrings("%USERNAME%")
	strDomain = wshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
	
	'use the username and domain to retrieve the user SID
	Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
	Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & strUsername & "',Domain='" & strDomain & "'")
	strUserSID = objAccount.SID
	
	'associate the RegistryTreeChangeEvent for the user's recycle bin registry key to the event sink object
	strWQL = "SELECT * FROM RegistryTreeChangeEvent WHERE Hive='HKEY_USERS' AND RootPath='" & strUserSID & "\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CLSID\\{645FF040-5081-101B-9F08-00AA002F954E}\\DefaultIcon'"
	Call objWMIService.ExecNotificationQueryAsync(objWbemSink,strWQL)
	
	Call swebm_OnObjectReady(obj,objWbemObject,objWbemAsyncContext)
End Sub

Sub Object_OnScriptEnter()
	'On Error Resume Next
	Call dxInit()
End Sub

Function Object_OnLButtonUp(x,y,dragged)
	'when object is clicked - empty recycle bin
	Set objShell = CreateObject("shell.application")
	
	Set objFolder = objShell.NameSpace(&Ha&)
	Set objFolderItem = objFolder.Self
	
	Set colVerbs = objFolderItem.Verbs()
	For Each objVerb In colVerbs
		If objVerb.Name = "Empty Recycle &Bin" Then
			Call objVerb.DoIt()
			Exit For
		End If
	Next
End Function

Sub swebm_OnObjectReady(obj,objWbemObject,objWbemAsyncContext)
	'this sub is called when the RegistryTreeChangeEvent fires the OnObjectReady event within the sink object.
	
	'it indicates the recycle bin key has been changed
	'read current icon key values and compare then to set the recycle bin state.
	
	Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
	strKeyPath = strUserSID & "\Software\Microsoft\Windows\CurrentVersion\Explorer\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\DefaultIcon"
	
	objRegistry.GetStringValue HKEY_USERS,strKeyPath,"",strCurrentIcon
	objRegistry.GetStringValue HKEY_USERS,strKeyPath,"empty",strEmptyIcon
	objRegistry.GetStringValue HKEY_USERS,strKeyPath,"full",strFullIcon
	
	If strEmptyIcon = strCurrentIcon Then
		Object.State = "empty"
	Else
		Object.State = "full"
	End If
End Sub

Function swebm_OnCompleted(obj,iHResult,objWbemErrorObject,objWbemAsyncContext)
	'MsgBox("complete")
End Function
	
Function swebm_OnObjectPut(obj,objWbemObjectPath,objWbemAsyncContext)
	'MsgBox("put")
End Function

Function swebm_OnProgress(obj,iUpperBound,iCurrent,strMessage,objWbemAsyncContext)
	'MsgBox("progress")
End Function
