#include <windows.h>
#include "resource.h"
#include "main.h"

extern DWORD g_AuthenticationLevel, g_ImpersonationLevel ;

BOOL CALLBACK SecurityDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static MYSECURITYDLGPARMS *psdp ;

	switch (msg)
	{
		case WM_INITDIALOG:
		{
/*
Populate dropdown lists with items describing security choices.
*/
			int index ;

			index = SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_ADDSTRING, 0, (LPARAM)"Anonymous") ;
			SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_SETITEMDATA, index, RPC_C_IMP_LEVEL_ANONYMOUS) ;
			index = SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_ADDSTRING, 0, (LPARAM)"Identify") ;
			SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_SETITEMDATA, index, RPC_C_IMP_LEVEL_IDENTIFY) ;
			index = SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_ADDSTRING, 0, (LPARAM)"Impersonate") ;
			SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_SETITEMDATA, index, RPC_C_IMP_LEVEL_IMPERSONATE) ;
			
			index = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_ADDSTRING, 0, (LPARAM)"Default") ;
			SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_SETITEMDATA, index, RPC_C_AUTHN_LEVEL_DEFAULT) ;
			index = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_ADDSTRING, 0, (LPARAM)"None") ;
			SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_SETITEMDATA, index, RPC_C_AUTHN_LEVEL_NONE) ;
			index = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_ADDSTRING, 0, (LPARAM)"Connect") ;
			SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_SETITEMDATA, index, RPC_C_AUTHN_LEVEL_CONNECT) ;
			index = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_ADDSTRING, 0, (LPARAM)"Call") ;
			SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_SETITEMDATA, index, RPC_C_AUTHN_LEVEL_CALL) ;
			index = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_ADDSTRING, 0, (LPARAM)"Packet") ;
			SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_SETITEMDATA, index, RPC_C_AUTHN_LEVEL_PKT) ;
			index = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_ADDSTRING, 0, (LPARAM)"Packet Integrity") ;
			SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_SETITEMDATA, index, RPC_C_AUTHN_LEVEL_PKT_INTEGRITY) ;
			index = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_ADDSTRING, 0, (LPARAM)"Packet Privacy") ;
			SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_SETITEMDATA, index, RPC_C_AUTHN_LEVEL_PKT_PRIVACY) ;

/*
Figure out if this box is setting global security choices or individual
proxy security choices. Get authentication and impersonation values to
match based on this choice, and also change dialog box text.
*/
			psdp = (MYSECURITYDLGPARMS *) lParam ;
			
			DWORD AuthToMatch, ImpToMatch ;

			if (psdp != NULL)
			{
				SetWindowText (hDlg, "Set security of IDataObject proxy") ;

				AuthToMatch = psdp->AuthenticationLevel ;		
				ImpToMatch = psdp->ImpersonationLevel ;
			}
			else
			{
				AuthToMatch = g_AuthenticationLevel ;
				ImpToMatch = g_ImpersonationLevel;
			}


/*
Set choices
*/
			int i ;
			DWORD data ;

			for (i = 0;;i++) 
			{
				data = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_GETITEMDATA, i, 0) ;
				if (data == AuthToMatch)
				{
					SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_SETCURSEL, i, 0) ;
					break ;
				}
			}

			for (i = 0 ;; i++) 
			{
				data = SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_GETITEMDATA, i, 0) ;
				if (data == ImpToMatch)
				{
					SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_SETCURSEL, i, 0) ;
					break ;
				}
			}

			return FALSE ;
		}
		case WM_COMMAND:
		{
			if (LOWORD(wParam) == IDCANCEL)
			{
				EndDialog (hDlg, IDCANCEL) ;
			}
			else if (LOWORD(wParam) == IDOK)
			{
/*
Get security selections from controls.
*/
				DWORD index, auth, imp ;
					
				index = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_GETCURSEL, 0, 0) ;
				auth = SendDlgItemMessage (hDlg, IDC_AUTHCOMBO, CB_GETITEMDATA, index, 0) ;
				if (psdp == NULL)
				{
					g_AuthenticationLevel = auth ;
				}
				else
				{
					psdp->AuthenticationLevel = auth ;
				}

				index = SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_GETCURSEL, 0, 0) ;
				imp = SendDlgItemMessage (hDlg, IDC_IMPCOMBO, CB_GETITEMDATA, index, 0) ;
				if (psdp == NULL)
				{
					g_ImpersonationLevel = imp ;
				}
				else
				{
					psdp->ImpersonationLevel = imp ;
				}
				EndDialog (hDlg, IDOK) ;
			}

			return TRUE ;
		}
	}
	return FALSE ;
}
