/********************************************************************
*
*   Copyright 1999-2000 Phone.com, Inc.  All rights reserved.
*
*   Subject to the terms and conditions of the SDK License Agreement, 
*   Phone.com, Inc. hereby grants you the right to use the UP.SDK 
*   software and its related documentation.
*
*   The Phone.com name and logo and the family of terms carrying the "UP."
*   prefix are trademarks of Phone.com, Inc. All other brands and product 
*   names may be trademarks of the respective companies with which they 
*   are associated.
*
********************************************************************/

#include <stdlib.h>
#include "stdafx.h"
#include "SendNtfn.h"
#include "NtfnDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// NtfnDlg

IMPLEMENT_DYNCREATE(NtfnDlg, CFormView)

NtfnDlg::NtfnDlg()
	: CFormView(NtfnDlg::IDD)
{
	long result;

	//{{AFX_DATA_INIT(NtfnDlg)
	m_AlertTitle = _T("v4.0 Pushed Alert");
	m_RequireSecureCheckBox = FALSE;
	m_UseSecureCheckBox = FALSE;
	m_HostName = _T("devgate2.phone.com");
	m_KeyFileName = _T("c:\\");
	m_Password = _T("");
	m_SubscriberID = _T("000000000-00_devgate2.phone.com");
	m_TTL = 0;
	m_Url = _T("http://updev.phone.com/dev/sdk.40/scripts/fetch.cgi?NEXT=MEETING&PREFETCH=YES");
	m_Charset = _T("United States (US-ASCII)");
	m_AlertUrgency = 1;
	m_AlertCount = _T("1");
	//}}AFX_DATA_INIT

	// Initialize COM.
	CoInitialize( NULL );

	// Create ptrs to the two Notification Interfaces
	//  (Secure and Non-Secure).
	result = CoCreateInstance( CLSID_Ntfn3SClient, NULL, CLSCTX_SERVER,
								IID_INtfn3SClient, (void**)&m_SNtfn);
	result = CoCreateInstance( CLSID_Ntfn3Client, NULL, CLSCTX_SERVER,
								IID_INtfn3Client, (void**)&m_Ntfn);
}

NtfnDlg::~NtfnDlg()
{
	m_SNtfn->Release();
	m_Ntfn->Release();

	CoUninitialize(); 
}

void NtfnDlg::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(NtfnDlg)
	DDX_Control(pDX, IDC_COMBO1, m_CharsetCombo);
	DDX_Control(pDX, IDC_BORDER, m_CBorder);
	DDX_Control(pDX, IDC_BTN_SEND_PREFETCH, m_SendPrefetch);
	DDX_Control(pDX, IDC_BTN_SEND_ALERT, m_SendAlert);
	DDX_Control(pDX, IDC_BTN_PREFETCH_STATUS, m_PrefetchStatus);
	DDX_Control(pDX, IDC_BTN_DELETE_PREFETCH, m_DeletePrefetch);
	DDX_Control(pDX, IDC_BTN_DELETE_ALERT, m_DeleteAlert);
	DDX_Control(pDX, IDC_BTN_ALERT_STATUS, m_AlertStatus);
	DDX_Text(pDX, IDC_ALERT_TITLE_TEXT, m_AlertTitle);
	DDX_Check(pDX, IDC_CHECK_REQ_SECURE, m_RequireSecureCheckBox);
	DDX_Check(pDX, IDC_CHECK_SECURE, m_UseSecureCheckBox);
	DDX_Text(pDX, IDC_HOST_TEXT, m_HostName);
	DDX_Text(pDX, IDC_KEYFILE_TEXT, m_KeyFileName);
	DDX_Text(pDX, IDC_PASSWD_TEXT, m_Password);
	DDX_Text(pDX, IDC_SUBS_ID_TEXT, m_SubscriberID);
	DDX_Text(pDX, IDC_TTL_TEXT, m_TTL);
	DDX_Text(pDX, IDC_URL_TEXT, m_Url);
	DDX_CBString(pDX, IDC_COMBO1, m_Charset);
	DDX_CBIndex(pDX, IDC_ALERT_URGENCY, m_AlertUrgency);
	DDX_Text(pDX, IDC_ALERT_COUNT_TEXT, m_AlertCount);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(NtfnDlg, CFormView)
	//{{AFX_MSG_MAP(NtfnDlg)
	ON_BN_CLICKED(IDC_BTN_ALERT_STATUS, OnBtnAlertStatus)
	ON_BN_CLICKED(IDC_BTN_DELETE_ALERT, OnBtnDeleteAlert)
	ON_BN_CLICKED(IDC_BTN_DELETE_PREFETCH, OnBtnDeletePrefetch)
	ON_BN_CLICKED(IDC_BTN_PREFETCH_STATUS, OnBtnPrefetchStatus)
	ON_BN_CLICKED(IDC_BTN_SEND_ALERT, OnBtnSendAlert)
	ON_BN_CLICKED(IDC_BTN_SEND_PREFETCH, OnBtnSendPrefetch)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_COMMAND(ID_SEND_PUSH, OnBtnSendPush)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateNeedSel)
	ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateNeedSel)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateNeedClip)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// NtfnDlg diagnostics

#ifdef _DEBUG
void NtfnDlg::AssertValid() const
{
	CFormView::AssertValid();
}

void NtfnDlg::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// NtfnDlg message handlers

// OnBtnAlertStatus --------------------------------------------------------
//
//	This routine is called when the Alert Status Button is pressed.
//	The dialog box values are used to send an alert and any errors
//	are displayed.
//
void NtfnDlg::OnBtnAlertStatus() 
{
	OLECHAR host[256];	
	OLECHAR subs[256];	
	OLECHAR url[256];
	OLECHAR keyFileName[256];
	OLECHAR password[256];
	ENtfnStatus	status;
	HRESULT result;

	// Use DDX to update the member variables with the
	// most recent dialog values.
	UpdateData();
    
	// Convert the standard ansi strings to Unicode (WCS)
	// strings. The COM interface expects WCS BSTR's.
	mbstowcs(host, m_HostName, sizeof(host));
    mbstowcs(subs, m_SubscriberID, sizeof(subs));
    mbstowcs(url,  m_Url, sizeof(url));
    mbstowcs(keyFileName, m_KeyFileName, sizeof(keyFileName));
    mbstowcs(password,  m_Password, sizeof(password));

	if (m_UseSecureCheckBox)
	{
		result = m_SNtfn->NtfnSetHost (host);

		// This call is optional unless you MUST have only a
		// secure connection. The default is FALSE.
		result = m_SNtfn->NtfnRequireSecureConnection ((short)m_RequireSecureCheckBox);
		
		// If Making a secure call, we must supply the certificate file
		// location and the password.
		result = m_SNtfn->NtfnLoadCertAndKey (keyFileName, password);

		if (S_OK != m_SNtfn->NtfnGetAlertStatus (subs, url, &status))
		{
			MessageBox( "Alert Status Failed!", "Error", MB_ICONERROR );
			return;
		}
	}
	else
	{
		result = m_Ntfn->NtfnSetHost (host);
		if (S_OK != m_Ntfn->NtfnGetAlertStatus (subs, url, &status))
		{
			MessageBox( "Alert Status Failed!", "Error", MB_ICONERROR );
			return;
		}
	}
	
		
	// Report the status result.
	switch (status) {
		case CNtfnStatusNone:
	MessageBox( "Alert Status: CNtfnStatusNone!", "Information", MB_ICONERROR );
		break;
		case CNtfnStatusPending:
	MessageBox( "Alert Status: CNtfnStatusPending!", "Information", MB_ICONINFORMATION );
		break;
		case CNtfnStatusDelivered:
	MessageBox( "Alert Status: CNtfnStatusDelivered!", "Information", MB_ICONINFORMATION );
		break;
		case CNtfnStatusExpired:
	MessageBox( "Alert Status: CNtfnStatusExpired!", "Information", MB_ICONINFORMATION );
		break;
		case CNtfnStatusUndeliverable:
	MessageBox( "Alert Status: CNtfnStatusUndeliverable!", "Information", MB_ICONERROR );
		break;
	}
}


// OnBtnDeleteAlert --------------------------------------------------------
//
//	This routine is called when the Delete Alert Button is pressed.
//	The dialog box values are used to delete an alert and any errors
//	are displayed.
//
void NtfnDlg::OnBtnDeleteAlert() 
{
	OLECHAR host[256];	
	OLECHAR subs[256];	
	OLECHAR url[256];
	OLECHAR keyFileName[256];
	OLECHAR password[256];
	HRESULT result;

	// Use DDX to update the member variables with the
	// most recent dialog values.
	UpdateData();
    
	// Convert the standard ansi strings to Unicode (WCS)
	// strings. The COM interface expects WCS BSTR's.
	mbstowcs(host, m_HostName, sizeof(host));
    mbstowcs(subs, m_SubscriberID, sizeof(subs));
    mbstowcs(url,  m_Url, sizeof(url));
    mbstowcs(keyFileName, m_KeyFileName, sizeof(keyFileName));
    mbstowcs(password,  m_Password, sizeof(password));

	if (m_UseSecureCheckBox)
	{
		result = m_SNtfn->NtfnSetHost (host);

		// This call is optional unless you MUST have only a
		// secure connection. The default is FALSE.
		result = m_SNtfn->NtfnRequireSecureConnection ((short)m_RequireSecureCheckBox);
		
		// If Making a secure call, we must supply the certificate file
		// location and the password.
		result = m_SNtfn->NtfnLoadCertAndKey (keyFileName, password);

		if (S_OK != m_SNtfn->NtfnDeleteAlert (subs, url))
		{
			BSTR err;
			char mbs_err[256];

			m_Ntfn->NtfnGetErrorDetail (&err);
			if (err != NULL && wcslen(err) > 0)
			{
				wcstombs(mbs_err, err, 255);
				MessageBox( mbs_err );
				SysFreeString(err);
			}
			else
				MessageBox( "Delete Alert Failed!", "Error", MB_ICONERROR );
		}
		else
			MessageBox( "Delete Alert Worked!", "Information", MB_ICONINFORMATION );
	}
	else
	{
		result = m_Ntfn->NtfnSetHost (host);
		if (S_OK != m_Ntfn->NtfnDeleteAlert (subs, url))
		{
			BSTR err;
			char mbs_err[256];

			m_Ntfn->NtfnGetErrorDetail (&err);
			if (err != NULL && wcslen(err) > 0)
			{
				wcstombs(mbs_err, err, 255);
				MessageBox( mbs_err );
				SysFreeString(err);
			}
			else
				MessageBox( "Delete Alert Failed!", "Error", MB_ICONERROR );
		}
		else
			MessageBox( "Delete Alert Worked!", "Information", MB_ICONINFORMATION );
	}
}


// OnBtnDeletePrefetch --------------------------------------------------------
//
//	This routine is called when the Delete Prefetch Button is pressed.
//	The dialog box values are used to delete an alert and any errors
//	are displayed.
//
void NtfnDlg::OnBtnDeletePrefetch() 
{
	OLECHAR host[256];	
	OLECHAR subs[256];	
	OLECHAR url[256];
	OLECHAR keyFileName[256];
	OLECHAR password[256];
	HRESULT result;

	// Use DDX to update the member variables with the
	// most recent dialog values.
	UpdateData();
    
	// Convert the standard ansi strings to Unicode (WCS)
	// strings. The COM interface expects WCS BSTR's.
	mbstowcs(host, m_HostName, sizeof(host));
    mbstowcs(subs, m_SubscriberID, sizeof(subs));
    mbstowcs(url,  m_Url, sizeof(url));
    mbstowcs(keyFileName, m_KeyFileName, sizeof(keyFileName));
    mbstowcs(password,  m_Password, sizeof(password));

	if (m_UseSecureCheckBox)
	{
		result = m_SNtfn->NtfnSetHost (host);

		// This call is optional unless you MUST have only a
		// secure connection. The default is FALSE.
		result = m_SNtfn->NtfnRequireSecureConnection ((short)m_RequireSecureCheckBox);
		
		// If Making a secure call, we must supply the certificate file
		// location and the password.
		result = m_SNtfn->NtfnLoadCertAndKey (keyFileName, password);

		if (S_OK != m_SNtfn->NtfnDeletePrefetch (subs, url))
		{
			BSTR err;
			char mbs_err[256];

			m_Ntfn->NtfnGetErrorDetail (&err);
			if (err != NULL && wcslen(err) > 0)
			{
				wcstombs(mbs_err, err, 255);
				MessageBox( mbs_err );
				SysFreeString(err);
			}
			else
				MessageBox( "Delete Prefetch Failed!", "Error", MB_ICONERROR );
		}
		else
			MessageBox( "Delete Prefetch Worked!", "Information", MB_ICONINFORMATION );
	}
	else
	{
		result = m_Ntfn->NtfnSetHost (host);
		if (S_OK != m_Ntfn->NtfnDeletePrefetch (subs, url))
		{
			BSTR err;
			char mbs_err[256];

			m_Ntfn->NtfnGetErrorDetail (&err);
			if (err != NULL && wcslen(err) > 0)
			{
				wcstombs(mbs_err, err, 255);
				MessageBox( mbs_err );
				SysFreeString(err);
			}
			else
				MessageBox( "Delete Prefetch Failed!", "Error", MB_ICONERROR );
		}
		else
			MessageBox( "Delete Prefetch Worked!", "Information", MB_ICONINFORMATION );
	}
}


// OnBtnPrefetchStatus --------------------------------------------------------
//
//	This routine is called when the Prefetch Status Button is pressed.
//	The dialog box values are used to get the status of an alert and
//	any errors are displayed.
//
void NtfnDlg::OnBtnPrefetchStatus() 
{
	OLECHAR host[256];	
	OLECHAR subs[256];	
	OLECHAR url[256];
	OLECHAR keyFileName[256];
	OLECHAR password[256];
	ENtfnStatus	status;
	HRESULT result;

	// Use DDX to update the member variables with the
	// most recent dialog values.
	UpdateData();
    
	// Convert the standard ansi strings to Unicode (WCS)
	// strings. The COM interface expects WCS BSTR's.
	mbstowcs(host, m_HostName, sizeof(host));
    mbstowcs(subs, m_SubscriberID, sizeof(subs));
    mbstowcs(url,  m_Url, sizeof(url));
    mbstowcs(keyFileName, m_KeyFileName, sizeof(keyFileName));
    mbstowcs(password,  m_Password, sizeof(password));

	if (m_UseSecureCheckBox)
	{
		result = m_SNtfn->NtfnSetHost (host);

		// This call is optional unless you MUST have only a
		// secure connection. The default is FALSE.
		result = m_SNtfn->NtfnRequireSecureConnection ((short)m_RequireSecureCheckBox);
		
		// If Making a secure call, we must supply the certificate file
		// location and the password.
		result = m_SNtfn->NtfnLoadCertAndKey (keyFileName, password);

		if (S_OK != m_SNtfn->NtfnGetPrefetchStatus (subs, url, &status))
		{
			MessageBox( "Prefetch Status Failed!", "Error", MB_ICONERROR );
			return;
		}
	}
	else
	{
		result = m_Ntfn->NtfnSetHost (host);
		if (S_OK != m_Ntfn->NtfnGetPrefetchStatus (subs, url, &status))
		{
			MessageBox( "Prefetch Status Failed!", "Error", MB_ICONERROR );
			return;
		}
	}

	// Report the status result.
	switch (status) {
		case CNtfnStatusNone:
	MessageBox( "Prefetch Status: CNtfnStatusNone!", "Information", MB_ICONERROR );
		break;
		case CNtfnStatusPending:
	MessageBox( "Prefetch Status: CNtfnStatusPending!", "Information", MB_ICONINFORMATION );
		break;
		case CNtfnStatusDelivered:
	MessageBox( "Prefetch Status: CNtfnStatusDelivered!", "Information", MB_ICONINFORMATION );
		break;
		case CNtfnStatusExpired:
	MessageBox( "Prefetch Status: CNtfnStatusExpired!", "Information", MB_ICONINFORMATION );
		break;
		case CNtfnStatusUndeliverable:
	MessageBox( "Prefetch Status: CNtfnStatusUndeliverable!", "Information", MB_ICONERROR );
		break;
	}
}


// OnBtnSendAlert --------------------------------------------------------
//
//	This routine is called when the Send Alert Button is pressed.
//	The dialog box values are used to send an alert and
//	any errors are displayed.
//
void NtfnDlg::OnBtnSendAlert() 
{
	OLECHAR host[256];	
	OLECHAR subs[256];	
	OLECHAR url[256];
	OLECHAR keyFileName[256];
	OLECHAR password[256];
	OLECHAR alertTitle[256];
	OLECHAR alertType[256];
	OLECHAR charset[256];
	HRESULT result;

	UCHAR entity_tmp[1024];
	OLECHAR entity[1024];
	OLECHAR ContentType[80];
	OLECHAR nid[2];
	EHTTPStatus AlertResult;
	ENtfnProtocol ProtoType;

		
	// Use DDX to update the member variables with the
	// most recent dialog values.
	UpdateData();
    
	// Convert the standard ansi strings to Unicode (WCS)
	// strings. The COM interface expects WCS BSTR's.
	mbstowcs(host, m_HostName, sizeof(host));
    mbstowcs(subs, m_SubscriberID, sizeof(subs));
    mbstowcs(url,  m_Url, sizeof(url));
    mbstowcs(alertTitle, m_AlertTitle, sizeof(alertTitle));
    mbstowcs(keyFileName, m_KeyFileName, sizeof(keyFileName));
    mbstowcs(password,  m_Password, sizeof(password));

	// build xml based entity type
	_mbscpy(entity_tmp,(UCHAR *)"<?xml version=\"1.0\"?>\n");
	_mbscat(entity_tmp,(UCHAR *)"<!DOCTYPE ALERT\n");
	_mbscat(entity_tmp,(UCHAR *)"PUBLIC \"-//PHONE.COM//DTD ALERT 1.0//EN\"\n");
	_mbscat(entity_tmp,(UCHAR *)"\"http://www.phone.com/dtd/alert1.xml\">\n");
	_mbscat(entity_tmp,(UCHAR *)"<ALERT LABEL = \"");
	_mbscat(entity_tmp,(unsigned char *)(LPCTSTR)m_AlertTitle);
	_mbscat(entity_tmp,(UCHAR *)"\"\n");
	_mbscat(entity_tmp,(UCHAR *)"COUNT = \"");
	_mbscat(entity_tmp,(unsigned char *)(LPCSTR)m_AlertCount);
	_mbscat(entity_tmp,(UCHAR *)"\"\n");
	_mbscat(entity_tmp,(UCHAR *)"HREF = \"");
	_mbscat(entity_tmp,(UCHAR *)"\"\n");
	_mbscat(entity_tmp,(UCHAR *)"URGENCY = \"");
	switch (m_AlertUrgency)
	{
		case 0 :	_mbscat(entity_tmp,(UCHAR *)"HIGH");
					break;
		case 2 :	_mbscat(entity_tmp,(UCHAR *)"LOW");
					break;
		case 3 :	_mbscat(entity_tmp,(UCHAR *)"NONE");
					break;
		default:	_mbscat(entity_tmp,(UCHAR *)"MEDIUM");
					break;
	}
	_mbscat(entity_tmp,(UCHAR *)"\" />\n");


	mbstowcs(nid,"",sizeof(nid));
	mbstowcs(ContentType,"application/vnd.uplanet.alert",sizeof(ContentType));
	mbstowcs(entity,(const char *)entity_tmp,sizeof(entity));

	// If user has specified a charset other than NULL, use it.
	if (!m_Charset.IsEmpty())
	{
		mbstowcs(charset,  m_Charset, sizeof(charset));
		m_SNtfn->NtfnSetCharset(charset);
		m_Ntfn->NtfnSetCharset(charset);
	}

	if (m_UseSecureCheckBox)
	{
		result = m_SNtfn->NtfnSetHost (host);

		// This call is optional unless you MUST have only a
		// secure connection. The default is FALSE.
		result = m_SNtfn->NtfnRequireSecureConnection ((short)m_RequireSecureCheckBox);

		// If Making a secure call, we must supply the certificate file
		// location and the password.
		result = m_SNtfn->NtfnLoadCertAndKey (keyFileName, password);

		// send secure v4.0 protocol push alert then check status
		m_SNtfn->NtfnPush (subs,url,nid,m_TTL,ContentType,entity);
		
		m_SNtfn->NtfnGetLastResult(&AlertResult);

		if (AlertResult == CHTTPStatusNoContent)
			{	
				m_Ntfn->NtfnGetProtocolType(&ProtoType);
				switch (ProtoType)
				{
					case CNtfnProtoUpnotifyp :	MessageBox ("Secure Pushed 4.0 Alert Sent Sucessfully!");
												break;
					case CNtfnProtoUpnotifyps :	MessageBox ("Something Went Wrong: NtfnGetProtocolType Returned CNtfnProtoUpnotifyps!");
												break;
					case CNtfnProtoNone :		MessageBox ("ERROR: Secure Pushed 4.0 NtfnGetProtocolType Returned CNtfnProtoNone.");
												break;
				}
			}
		else  //check for unsupported media type, then send secure protocol v3 post alert
			if (AlertResult==415)
			{
				//re-map v4.0 -> v3.0 alert type for backwards compatibility
				switch (m_AlertUrgency)
				{
					case 0 :	mbstowcs(alertType, "-999", sizeof(host));
								break;
					case 2 :	mbstowcs(alertType, "--1-", sizeof(host));
								break;
					case 3 :	mbstowcs(alertType, "----", sizeof(host));
								break;
					default:	mbstowcs(alertType, "D---", sizeof(host));
								break;
				}
				// send secure v3.0 post alert then check result
				m_SNtfn->NtfnPostAlert (subs, url, m_TTL, alertType, alertTitle, 0);

				m_SNtfn->NtfnGetLastResult(&AlertResult);

				if (AlertResult == CHTTPStatusNoContent)
				{	
					m_SNtfn->NtfnGetProtocolType(&ProtoType);
					switch (ProtoType)
					{
						case CNtfnProtoUpnotifyps :	MessageBox ("Secure 3.0 Post Alert Sent Sucessfully!");
													break;
						case CNtfnProtoUpnotifyp :	MessageBox ("Something Went Wrong: NtfnGetProtocolType Returned CNtfnProtoUpnotifyps!");
													break;
						case CNtfnProtoNone :		MessageBox ("ERROR: Se=cure Post Alert v3.0 NtfnGetProtocolType Returned CNtfnProtoNone.");
													break;
					}
				}
				else
					{
						BSTR err;
						char mbs_err[256];
	
						m_SNtfn->NtfnGetErrorDetail (&err);
						if (err != NULL && wcslen(err) > 0)
						{
							wcstombs(mbs_err, err, 255);
							MessageBox( mbs_err );
							SysFreeString(err);
						}
						else
							MessageBox( "Alert Failed!", "Error", MB_ICONERROR );
					}
				
			}	//un-supp-push-media-type-415
			else
				{
						BSTR err;
						char mbs_err[256];
	
						m_SNtfn->NtfnGetErrorDetail (&err);
						if (err != NULL && wcslen(err) > 0)
						{
							wcstombs(mbs_err, err, 255);
							MessageBox( mbs_err );
							SysFreeString(err);
						}
						else
							MessageBox("Alert Failed!", "Error", MB_ICONERROR );
				} 

	}
	else  // !m_UseSecureCheckBox
	{	
		result = m_Ntfn->NtfnSetHost (host);
		// send non-secure v4.0 protocol push alert then check status
		m_Ntfn->NtfnPush (subs,url,nid,m_TTL,ContentType,entity);

		m_Ntfn->NtfnGetLastResult(&AlertResult);

		if (AlertResult == CHTTPStatusNoContent)
			{	
				m_Ntfn->NtfnGetProtocolType(&ProtoType);
				switch (ProtoType)
				{
					case CNtfnProtoUpnotifyp :	MessageBox ("Pushed 4.0 Alert Sent Sucessfully!");
												break;
					case CNtfnProtoUpnotifyps :	MessageBox ("Something Went Wrong: NtfnGetProtocolType Returned CNtfnProtoUpnotifyps!");
												break;
					case CNtfnProtoNone :		MessageBox ("ERROR: Pushed 4.0 NtfnGetProtocolType Returned CNtfnProtoNone.");
												break;
				}
			}
		else  //check for unsupported media type, then send protocol v3 post alert
			if (AlertResult==415)
			{
				//re-map alert v4.0 -> v3.0 type for backwards compatibility
				switch (m_AlertUrgency)
				{
					case 0 :	mbstowcs(alertType, "-999", sizeof(host));
								break;
					case 2 :	mbstowcs(alertType, "--1-", sizeof(host));
								break;
					case 3 :	mbstowcs(alertType, "----", sizeof(host));
								break;
					default:	mbstowcs(alertType, "D---", sizeof(host));
								break;
				}
				// send non-secure v3.0 post alert then check result
				m_Ntfn->NtfnPostAlert (subs, url, m_TTL, alertType, alertTitle, 0);
				m_Ntfn->NtfnGetLastResult(&AlertResult);
				if (AlertResult == CHTTPStatusNoContent)
				{	
					m_Ntfn->NtfnGetProtocolType(&ProtoType);
					switch (ProtoType)
					{
						case CNtfnProtoUpnotifyp :	MessageBox ("Post Alert 3.0 Sent Sucessfully!");
													break;
						case CNtfnProtoUpnotifyps :	MessageBox ("Something Went Wrong: NtfnGetProtocolType Returned CNtfnProtoUpnotifyps!");
													break;
						case CNtfnProtoNone :		MessageBox ("ERROR: Post Alert v3.0 NtfnGetProtocolType Returned CNtfnProtoNone.");
													break;
					}
				}
				else
					{
						BSTR err;
						char mbs_err[256];
	
						m_Ntfn->NtfnGetErrorDetail (&err);
						if (err != NULL && wcslen(err) > 0)
						{
							wcstombs(mbs_err, err, 255);
							MessageBox( mbs_err );
							SysFreeString(err);
						}
						else
							MessageBox( "Alert Failed!", "Error", MB_ICONERROR );
					}
				
			}	//un-supp-push-media-type-415
			else
				{
						BSTR err;
						char mbs_err[256];
	
						m_Ntfn->NtfnGetErrorDetail (&err);
						if (err != NULL && wcslen(err) > 0)
						{
							wcstombs(mbs_err, err, 255);
							MessageBox( mbs_err );
							SysFreeString(err);
						}
						else
							MessageBox( "Alert Failed!", "Error", MB_ICONERROR );
				} 
	}	// else non-secure alert
}	// NtfnDlg::OnBtnSendAlert()



// OnBtnSendPrefetch --------------------------------------------------------
//
//	This routine is called when the Send Prefetch Button is pressed.
//	The dialog box values are used to send an prefetch and
//	any errors are displayed.
//
void NtfnDlg::OnBtnSendPrefetch() 
{
	OLECHAR host[256];	
	OLECHAR subs[256];	
	OLECHAR url[256];
	OLECHAR keyFileName[256];
	OLECHAR password[256];
	OLECHAR alertTitle[256];
	HRESULT result;

	// Use DDX to update the member variables with the
	// most recent dialog values.
	UpdateData();
    
	// Convert the standard ansi strings to Unicode (WCS)
	// strings. The COM interface expects WCS BSTR's.
	mbstowcs(host, m_HostName, sizeof(host));
    mbstowcs(subs, m_SubscriberID, sizeof(subs));
    mbstowcs(url,  m_Url, sizeof(url));
    mbstowcs(alertTitle, m_AlertTitle, sizeof(alertTitle));
    mbstowcs(keyFileName, m_KeyFileName, sizeof(keyFileName));
    mbstowcs(password,  m_Password, sizeof(password));

	if (m_UseSecureCheckBox)
	{
		result = m_SNtfn->NtfnSetHost (host);

		// This call is optional unless you MUST have only a
		// secure connection. The default is FALSE.
		result = m_SNtfn->NtfnRequireSecureConnection ((short)m_RequireSecureCheckBox);
		
		// If Making a secure call, we must supply the certificate file
		// location and the password.
		result = m_SNtfn->NtfnLoadCertAndKey (keyFileName, password);

		if (S_OK != m_SNtfn->NtfnPostPrefetch (subs, url, m_TTL))
		{
			BSTR err;
			char mbs_err[256];

			m_Ntfn->NtfnGetErrorDetail (&err);
			if (err != NULL && wcslen(err) > 0)
			{
				wcstombs(mbs_err, err, 255);
				MessageBox( mbs_err );
				SysFreeString(err);
			}
			else
				MessageBox( "Prefetch Failed!", "Error", MB_ICONERROR );
		}
		else
			MessageBox( "Prefetch Worked!", "Information", MB_ICONINFORMATION );
	}
	else
	{
		result = m_Ntfn->NtfnSetHost (host);

		if (S_OK != m_Ntfn->NtfnPostPrefetch (subs, url, m_TTL))
		{
			BSTR err;
			char mbs_err[256];

			m_Ntfn->NtfnGetErrorDetail (&err);
			if (err != NULL && wcslen(err) > 0)
			{
				wcstombs(mbs_err, err, 255);
				MessageBox( mbs_err );
				SysFreeString(err);
			}
			else
				MessageBox( "Prefetch Failed!", "Error", MB_ICONERROR );
		}
		else
			MessageBox( "Prefetch Worked!", "Information", MB_ICONINFORMATION );
	}
}


// OnBtnSendPush --------------------------------------------------------
//
//	This routine is never called. It is included as an internal test.
//	Any errors are displayed.
//
void NtfnDlg::OnBtnSendPush() 
{
	OLECHAR host[256];	
	OLECHAR subs[256];	
	OLECHAR url[256];
	OLECHAR keyFileName[256];
	OLECHAR password[256];
	OLECHAR contentType[256];
	OLECHAR entity[256];
	HRESULT result;

	// Use DDX to update the member variables with the
	// most recent dialog values.
	UpdateData();
    
	// Convert the standard ansi strings to Unicode (WCS)
	// strings. The COM interface expects WCS BSTR's.
	mbstowcs(host, m_HostName, sizeof(host));
    mbstowcs(subs, m_SubscriberID, sizeof(subs));
    mbstowcs(url,  m_Url, sizeof(url));
    mbstowcs(contentType, "test/test", sizeof(contentType));
    mbstowcs(entity,  "nadanadanada", sizeof(entity));
    mbstowcs(keyFileName, m_KeyFileName, sizeof(keyFileName));
    mbstowcs(password,  m_Password, sizeof(password));

	if (m_UseSecureCheckBox)
	{
		result = m_SNtfn->NtfnSetHost (host);
		result = m_SNtfn->NtfnRequireSecureConnection ((short)m_RequireSecureCheckBox);
		result = m_SNtfn->NtfnLoadCertAndKey (keyFileName, password);

		if (S_OK != m_SNtfn->NtfnPush (subs, url, NULL, m_TTL, contentType, entity))
		{
			BSTR err;
			char mbs_err[256];

			m_Ntfn->NtfnGetErrorDetail (&err);
			if (err != NULL && wcslen(err) > 0)
			{
				wcstombs(mbs_err, err, 255);
				MessageBox( mbs_err );
				SysFreeString(err);
			}
			else
				MessageBox( "Push Failed!", "Error", MB_ICONERROR );
		}
		else
			MessageBox( "Push Worked!", "Information", MB_ICONINFORMATION );
	}
	else
	{
		result = m_Ntfn->NtfnSetHost (host);

		if (S_OK != m_Ntfn->NtfnPush (subs, url, NULL, m_TTL, contentType, entity))
		{
			BSTR err;
			char mbs_err[256];

			m_Ntfn->NtfnGetErrorDetail (&err);
			if (err != NULL && wcslen(err) > 0)
			{
				wcstombs(mbs_err, err, 255);
				MessageBox( mbs_err );
				SysFreeString(err);
			}
			else
				MessageBox( "Push Failed!", "Error", MB_ICONERROR );
		}
		else
			MessageBox( "Push Worked!", "Information", MB_ICONINFORMATION );
	}
}



// We override this routine in order to
// get the size of the dialog border and set
// the main frame large enough to display it.
//
void NtfnDlg::OnInitialUpdate() 
{
	CRect	r;
	CWnd*	pWnd = AfxGetMainWnd();

	CFormView::OnInitialUpdate();
	
	m_CBorder.GetWindowRect((LPRECT)r);
	r.InflateRect(0, 0, 30, 56);
	pWnd->MoveWindow( (LPRECT)r);
}


/* **********************************************************

	THE ROUTINES BELOW ARE INCLUDED TO ADD CUT/COPY/PASTE
	SUPPORT TO THE CFormView USED IN THIS EXAMPLE.

	They are interesting, but not relevant to using the 
	Notification COM server object in your C++ applications.

 * **********************************************************/

// IsEdit: a helper function to determine if a given CWnd pointer
// points to a CEDit control.
// Use the SDK ::GetClassName() function because MFC IsKindOf
// fails if no CEdit variable has been created for the control you're
// trying to test.

BOOL NtfnDlg::IsEdit( CWnd* pWnd )
{
	ASSERT( pWnd != NULL );
	HWND hWnd = pWnd->GetSafeHwnd();
	if (hWnd == NULL)
		return FALSE;

	TCHAR szClassName[6];
	return ::GetClassName(hWnd, szClassName, 6) &&
			_tcsicmp(szClassName, _T("Edit")) == 0;
}


// Handle these COMMANDS so that Cut/Copy/Paste
// will work from the menu and accelarator keys.
void NtfnDlg::OnEditCopy() 
{
	CEdit* pEdit = (CEdit*)GetFocus();
                 
	ASSERT( IsEdit( pEdit) );             
	pEdit->Copy();
}

void NtfnDlg::OnEditCut() 
{ 
	CEdit* pEdit = (CEdit*)GetFocus();
                 
	ASSERT( IsEdit( pEdit) );             
	pEdit->Cut();
}

void NtfnDlg::OnEditPaste() 
{
	CEdit* pEdit = (CEdit*)GetFocus();
                 
	ASSERT( IsEdit( pEdit) );
	ASSERT(::IsClipboardFormatAvailable(CF_TEXT));
	pEdit->Paste();
}


BOOL NtfnDlg::PreTranslateMessage(MSG* pMsg) 
{
	return CFormView::PreTranslateMessage(pMsg);
}

void NtfnDlg::OnUpdateNeedSel(CCmdUI* pCmdUI) 
{
    // get the current focus & determine if its on a CEdit control
	CWnd* pWnd = GetFocus();
	
	if (NULL == pWnd || !IsEdit( pWnd ))
	{
		pCmdUI->Enable( FALSE );
	}
	else
	{
		CEdit* pEdit = (CEdit*)pWnd;
		int nBeg, nEnd;

		pEdit->GetSel( nBeg, nEnd );
		pCmdUI->Enable( nBeg != nEnd );
	}
}

void NtfnDlg::OnUpdateNeedClip(CCmdUI* pCmdUI) 
{
	// get the current focus & determine if its on a CEdit control
	// also check to see if the control is read-only.
	CWnd* pWnd = GetFocus();
	
	if (NULL == pWnd || !IsEdit( pWnd ) || (pWnd->GetStyle() & ES_READONLY) != 0 )
	{
		pCmdUI->Enable( FALSE );
	}
	else
		pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));
}

