#include "setup.h"

#include <vdr/menuitems.h>

cClusterSetup ClusterSetup;

cClusterSetup::cClusterSetup(void) {
	NumPreBufs = 2;
	MinBufSize = 16;
	RemoteNFS = false;
	strcpy(NFSPrefix, "");
	strcpy(RecodeIp, "127.0.0.1");
	RecodePort = 2003;
	HideMenuEntry = false;
}

bool cClusterSetup::SetupParse(const char *Name, const char *Value) {
	if (strcmp(Name, "NumPreBufs") == 0) NumPreBufs = atoi(Value);
	else if (strcmp(Name, "MinBufSize") == 0) MinBufSize = atoi(Value);
	else if (strcmp(Name, "NFSPrefix") == 0) strcpy(NFSPrefix, Value);
	else if (strcmp(Name, "RemoteNFS") == 0) RemoteNFS = atoi(Value);
	else if (strcmp(Name, "RecodePort") == 0) RecodePort = atoi(Value);
	else if (strcmp(Name, "RecodeIp") == 0) strcpy(RecodeIp, Value);
	else if (strcmp(Name, "HideMenuEntry") == 0) HideMenuEntry = atoi(Value);
	else return false;
	return true;
}

const char cClusterMenuSetupPage::IpChars[] = "0123456789.";
const char *cClusterMenuSetupPage::PathChars = NULL;

cClusterMenuSetupPage::cClusterMenuSetupPage(void) {
	m_NewClusterSetup = ClusterSetup;

	if (PathChars == NULL) {
		size_t len;
		char *table;

		len = strlen(FileNameChars);
		table = new char[len + 2];
		memcpy(table, FileNameChars, len);
		table[len] = '/';
		table[len + 1] = '\0';

		PathChars = table;
	}

	Add(new cMenuEditIntItem(tr("Number of Prebuffers"), 
			&m_NewClusterSetup.NumPreBufs));
	Add(new cMenuEditIntItem(tr("Minimum Buffersize (kB)"), 
			&m_NewClusterSetup.MinBufSize));
	Add(new cMenuEditBoolItem(tr("Use Remote NFS"),
			&m_NewClusterSetup.RemoteNFS));
	Add(new cMenuEditStrItem(tr("NFS Prefix"),
			m_NewClusterSetup.NFSPrefix, sizeof(m_NewClusterSetup.NFSPrefix), 
			PathChars));
	Add(new cMenuEditIntItem(tr("Recode node Port"), 
			&m_NewClusterSetup.RecodePort));
	Add(new cMenuEditStrItem(tr("Recode node IP"), 
			m_NewClusterSetup.RecodeIp, sizeof(m_NewClusterSetup.RecodeIp), IpChars));
	Add(new cMenuEditBoolItem(tr("Hide Mainmenu Entry"),
			&m_NewClusterSetup.HideMenuEntry));
}

cClusterMenuSetupPage::~cClusterMenuSetupPage() {
}

void cClusterMenuSetupPage::Store(void) {
	SetupStore("NumPreBufs", m_NewClusterSetup.NumPreBufs);
	SetupStore("MinBufSize", m_NewClusterSetup.MinBufSize);
	SetupStore("RemoteNFS", m_NewClusterSetup.RemoteNFS);
	SetupStore("NFSPrefix", m_NewClusterSetup.NFSPrefix);
	SetupStore("RecodePort", m_NewClusterSetup.RecodePort);
	SetupStore("RecodeIp", m_NewClusterSetup.RecodeIp);
	SetupStore("HideMenuEntry", m_NewClusterSetup.HideMenuEntry);

	ClusterSetup = m_NewClusterSetup;
}
