// This is part of Total Recorder Developer Edition (DE).
// Copyright (C) 1998-2008 High Criteria Inc.
// All rights reserved.
//
// RecPlaySave sample application.
//
// This sample loads the Total Recorder object,
// saves Total Recorder's settings,
// records 10 sec. from sound board with phone quality,
// plays the recorded sound, saves it to a file,
// restores settings, and releases the Total Recorder object.
//
// EventProcessing.cpp - implementation of the class for processing
//                       Total Recorder events.

#include <crtdbg.h>
#include "..\..\..\TotalRecorder_i.h"
#include "RecPlaySave.h"
#include "EventProcessing.h"

// - - - - - - - - - - - - - - - - - - - -

CEventProcessing::CEventProcessing()
{
	m_pTypeInfo = NULL;
	m_uCnt      = 0;
}

// - - - - - - - - - - - - - - - - - - - -

CEventProcessing::~CEventProcessing()
{
	SafeRelease(m_pTypeInfo);
}

// - - - - - - - - - - - - - - - - - - - -

HRESULT CEventProcessing::Init()
{
	static METHODDATA aMethData[] = {
		//  Name           Args  DI I  CallConv  NA wFlags           RetType
		{ L"OnMajorEvent", NULL, 1, 7, CC_CDECL, 0, DISPATCH_METHOD, VT_EMPTY },
		{ L"OnMinorEvent", NULL, 2, 8, CC_CDECL, 0, DISPATCH_METHOD, VT_EMPTY } };
		//
		// DI - The DISPID of the method
		// I  - The index of the method in the VTBL of the interface
		//      (0 - QueryInterface, 1 - AddRef, ... 6 - Invoke)
		// NA - The number of arguments for the method

	static INTERFACEDATA IntrfData =
		{ aMethData, SIZEOF_ARRAY(aMethData) };

	if (m_pTypeInfo)
		return S_OK;
	return CreateDispTypeInfo(&IntrfData, LOCALE_SYSTEM_DEFAULT, &m_pTypeInfo);
}

// - - - - - - - - - - - - - - - - - - - -

HRESULT STDMETHODCALLTYPE CEventProcessing::QueryInterface(
	/* [in] */ REFIID riid,
	/* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
{
	if (! ppvObject)
		return E_POINTER;

	if (memcmp(&riid, &IID_IUnknown, sizeof(GUID)) == 0)
		*ppvObject = (IUnknown*) this;
	else if (memcmp(&riid, &IID_IDispatch, sizeof(GUID)) == 0)
		*ppvObject = (IDispatch*) this;
	else if (memcmp(&riid, &DIID__ITotalRecorderApplicationEvents, sizeof(GUID))
	 == 0)
		*ppvObject = (_ITotalRecorderApplicationEvents*) this;
	else
	{
		*ppvObject = NULL;
		return E_NOINTERFACE;
	}

	AddRef();
	return S_OK;
}

// - - - - - - - - - - - - - - - - - - - -

ULONG STDMETHODCALLTYPE CEventProcessing::AddRef( void)
{
	return ++m_uCnt;
}

// - - - - - - - - - - - - - - - - - - - -

ULONG STDMETHODCALLTYPE CEventProcessing::Release( void)
{
	_ASSERTE(m_uCnt > 0);
	return --m_uCnt;
}

// - - - - - - - - - - - - - - - - - - - -

HRESULT STDMETHODCALLTYPE CEventProcessing::GetTypeInfoCount(
	/* [out] */ UINT __RPC_FAR *pctinfo)
	// in help: IDispatch::GetTypeInfoCount
{
	if (! pctinfo)
		return E_POINTER;
	*pctinfo = 0;  // GetTypeInfo is not needed
	return S_OK;
}

// - - - - - - - - - - - - - - - - - - - -

HRESULT STDMETHODCALLTYPE CEventProcessing::GetTypeInfo(
	/* [in] */ UINT iTInfo,
	/* [in] */ LCID lcid,
	/* [out] */ ITypeInfo __RPC_FAR *__RPC_FAR *ppTInfo)
	// in help: IDispatch::GetTypeInfo
{
	return E_NOTIMPL;  // unused
}

// - - - - - - - - - - - - - - - - - - - -

HRESULT STDMETHODCALLTYPE CEventProcessing::GetIDsOfNames(
	/* [in] */ REFIID riid,
	/* [size_is][in] */ LPOLESTR __RPC_FAR *rgszNames,
	/* [in] */ UINT cNames,
	/* [in] */ LCID lcid,
	/* [size_is][out] */ DISPID __RPC_FAR *rgDispId)
	// in help: IDispatch::GetIDsOfNames
{
	HRESULT  hRes;

	_ASSERTE(m_pTypeInfo);
	hRes = DispGetIDsOfNames(m_pTypeInfo, rgszNames, cNames, rgDispId);
	return hRes;
}

// - - - - - - - - - - - - - - - - - - - -

HRESULT STDMETHODCALLTYPE CEventProcessing::Invoke(
	/* [in] */ DISPID dispIdMember,
	/* [in] */ REFIID riid,
	/* [in] */ LCID lcid,
	/* [in] */ WORD wFlags,
	/* [out][in] */ DISPPARAMS __RPC_FAR *pDispParams,
	/* [out] */ VARIANT __RPC_FAR *pVarResult,
	/* [out] */ EXCEPINFO __RPC_FAR *pExcepInfo,
	/* [out] */ UINT __RPC_FAR *puArgErr)
	// in help: IDispatch::Invoke
{
	HRESULT  hRes;

	_ASSERTE(m_pTypeInfo);
	hRes = DispInvoke(this, m_pTypeInfo,
		dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
	return hRes;
}

// - - - - - - - - - - - - - - - - - - - -

void CEventProcessing::OnMajorEvent()
{
	ProcessEvent();
}

// - - - - - - - - - - - - - - - - - - - -

void CEventProcessing::OnMinorEvent()
{
	ProcessEvent();
}
