#include "stdafx.h"

static BOOL
UploadConfigCb(HKEY hKey,
			   LPTSTR szValName,
			   DWORD dwValSize,
			   DWORD dwValType,
			   PVOID pData)
{
	UploadConfig *ucfg = pData;
	if (!ucfg) return FALSE;

	CopyIfMatch(hKey, ucfg,
		Name, REG_SZ)
	else CopyIfMatch(hKey, ucfg,
		URL, REG_SZ)
	else CopyIfMatch(hKey, ucfg,
		Method, REG_SZ)
	else CopyIfMatch(hKey, ucfg,
		Username, REG_SZ)
	else CopyIfMatch(hKey, ucfg,
		Password, REG_SZ)
	else CopyIfMatch(hKey, ucfg,
		HTTPAuthMethod, REG_DWORD)
	else CopyIfMatch(hKey, ucfg,
		Headers, REG_MULTI_SZ)
	else CopyIfMatch(hKey, ucfg,
		Parameters, REG_MULTI_SZ)

	return TRUE;
}

UploadConfig *
GetUploadConfig(void)
{

	HKEY hkUploadKey = NULL;
	HANDLE hHeap = GetProcessHeap();
	LONG lRes;
	UploadConfig *ucfg = HeapAlloc(hHeap,
		HEAP_ZERO_MEMORY,
		sizeof(UploadConfig));

	if (!ucfg) return NULL;

	// TODO: defaults?
	lRes = RegOpenKeyEx(HKEY_CURRENT_USER,
		_T(ROOT_KEYNAME "\\Upload"),
		0,
		KEY_READ,
		&hkUploadKey);

	if (lRes) {
		SetLastError(lRes);
		HeapFree(hHeap, 0, ucfg);
		return NULL;
	}

	if (!RegGetAllValues(hkUploadKey, UploadConfigCb, ucfg)) {
		HeapFree(hHeap, 0, ucfg);
		return NULL;
	}

	return ucfg;
}