

#include "setup.h"
#include "vboxclient.h"
char *resolvenumber_script = NULL;

void cVBOXUser::_Free(void)
{

	if(m_sServer)
		free(m_sServer);
	if(m_sName)
		free(m_sName);
	if(m_sPassword)
		free(m_sPassword);
	if(m_sPIN)
		free(m_sPIN);
}

cVBOXUser::cVBOXUser()
{
	m_pVBOX = NULL;
	m_sServer = strdup("localhost");
	m_nPort = -1;
	m_sName = strdup("nobody");
	m_sPassword = strdup("guest");
	m_sPIN = strdup("0000");
}
bool cVBOXUser::Parse(char *s)
{

  _Free();
  int fields = sscanf(s, "%a[^:]:%d:%a[^:]:%a[^:]:%a[^:]",
		      &m_sServer, &m_nPort, &m_sName,&m_sPassword, &m_sPIN);

  if(fields != 5)
  {
	  _Free();
	  return false;
  }
  m_pVBOX = new cVBOXClient(m_sServer,m_nPort,m_sName,m_sPassword);
  return true;

}

bool cVBOXUser::Save(FILE *f)
{
  char * buffer = NULL;
  asprintf(&buffer, "%s:%d:%s:%s\n", m_sServer, m_nPort, m_sPassword, m_sPIN);
  bool ok = fprintf(f, buffer) > 0;
  free(buffer);
  return ok;
}

bool cVBOXUser::GetServerNewMsgCount(int *pCount,time_t *pTimeLatest)
{

	if(m_pVBOX == NULL || (m_pVBOX->Connect() !=  cVBOXClient::vboxSuccess))
		return false;

	bool ok = (m_pVBOX->GetServerNewMsgCount(pCount,pTimeLatest) ==  cVBOXClient::vboxSuccess);

	m_pVBOX->Disconnect();
	return ok;
}

cVBOXUser::~cVBOXUser()
{
	_Free();
	delete m_pVBOX;
}

// --- cMenuSetupVBOX --------------------------------------------------------

cMenuSetupVBOX::cMenuSetupVBOX(cVBOXSetup *pVBOXSetup)
{

  m_pVBOXSetup = pVBOXSetup;

  m_pVBOXSetup->AddMenuEntrys(this);
}

void cMenuSetupVBOX::Store(void)
{
	m_pVBOXSetup->Store(this);
}


// --- cVBOXSetup --------------------------------------------------------

#define SON_NOTIFYABOUTNEWCALLS "NotifyAboutNewCalls"
#define SON_CONFIRMDELETION "ConfirmDeletion"

void cVBOXSetup::AddMenuEntrys(cMenuSetupPage *pSetupMenu)
{
	pSetupMenu->Add(new cMenuEditBoolItem(tr("Notify about new calls"),   &m_bNotifyAboutNewCalls));
	pSetupMenu->Add(new cMenuEditBoolItem(tr("Confirm deletion of calls"),   &m_bConfirmDeletion));
}

bool cVBOXSetup::Parse(const char *Name, const char *Value)
{
 if (!strcasecmp(Name, SON_NOTIFYABOUTNEWCALLS)) m_bNotifyAboutNewCalls = atoi(Value);
  else if (!strcasecmp(Name, SON_CONFIRMDELETION)) m_bConfirmDeletion = atoi(Value);
  else
    return false;

  return true;

}

void cVBOXSetup::Store(cMenuSetupVBOX *pSetupMenu)
{
  pSetupMenu->SetupStore(SON_NOTIFYABOUTNEWCALLS, m_bNotifyAboutNewCalls);
  pSetupMenu->SetupStore(SON_CONFIRMDELETION, m_bConfirmDeletion);
}
