/*
 * config.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */
#include <vdr/eit.h>
#include <vdr/osd.h>
#include <vdr/menuitems.h>
#include <vdr/tools.h>

#include "config.h"
#include "i18n.h"
#include "tools.h"

cXmsSetup XmsSetup;

cXmsSetup::cXmsSetup(void)
: dbi(NULL)	
, dbOpen(false)	
, DbPort(3306)	
{_
  strcpy(DbHost, "vdrserver");
  strcpy(DbName, "dbMedia");
  strcpy(DbUser, "vdrdummy");
  strcpy(DbPassword, "vdrdummy");
  strcpy(DbSocket, "");
  strcpy(DefaultType, "DVD");
}

bool cXmsSetup::SetupParse(const char *Name, const char *Value)
{_
  if (!strcasecmp(Name, "DbHost"))           strn0cpy(DbHost, Value, DBHOST_LEN);
  else if (!strcasecmp(Name, "DbName"))      strn0cpy(DbName, Value, DBNAME_LEN);
  else if (!strcasecmp(Name, "DbUser"))      strn0cpy(DbUser, Value, DBUSER_LEN);
  else if (!strcasecmp(Name, "DbPassword"))  strn0cpy(DbPassword, Value, DBPW_LEN);
  else if (!strcasecmp(Name, "DbPort"))      DbPort = atoi(Value);
  else if (!strcasecmp(Name, "DbSocket"))    strn0cpy(DbSocket, Value, MaxFileName);
  else if (!strcasecmp(Name, "DefaultType")) strn0cpy(DefaultType, Value, TYPE_NAME_LEN);
  else
    return false;
  return true;
}

bool cXmsSetup::DbOpen() 
{_
  dbi = mysql_init(NULL);
  if(dbi) {
	 SLDEBUG("read database-options from my.cnf ...");
	 mysql_options(dbi, MYSQL_READ_DEFAULT_GROUP, "vdr-xms");
	 if(!mysql_real_connect(dbi
				 			    , DbHost
								 , DbUser
								 , DbPassword
								 , DbName
								 , DbPort
								 , DbSocket
								 , 0)) {
		 SLERROR("could not connect to database. Error #%u: %s"
				 , mysql_errno(dbi)
				 , mysql_error(dbi));
 	    }
	 else {
		 dbOpen = true;
		 SLINFO("connected to database.");
		 } 
    }
  else {
	 SLERROR("no way to initialize database-connection 8(");
	 }
  return dbOpen;
}

void cXmsSetup::DbClose()
{_
  mysql_close(dbi);
  dbi = NULL;
  dbOpen = false;
}

cMenuSetupXms::cMenuSetupXms(void)
{_
  data = XmsSetup;
  SetSection("XMS");

  Add(new cMenuEditStrItem( tr("DbHost"),
        data.DbHost,
        DBHOST_LEN,
        "abcdefghijklmnopqrstuvwxyz0123456789_."
      ));
  Add(new cMenuEditStrItem( tr("DbName"),
        data.DbName,
        DBNAME_LEN,
        "abcdefghijklmnopqrstuvwxyz0123456789"
      ));
  Add(new cMenuEditStrItem( tr("DbUser"),
        data.DbUser,
        DBUSER_LEN,
        "abcdefghijklmnopqrstuvwxyz0123456789"
      ));
  Add(new cMenuEditStrItem( tr("DbPassword"),
        data.DbPassword,
        DBPW_LEN,
        " abcdefghijklmnopqrstuvwxyz0123456789-./"
      ));
  Add(new cMenuEditIntItem( tr("DbPort"),
       &data.DbPort,
        1000,
        99999
      ));
  Add(new cMenuEditStrItem( tr("DbSocket"),
        data.DbSocket,
        MaxFileName,
        " abcdefghijklmnopqrstuvwxyz0123456789-./"
      ));
  Add(new cMenuEditStrItem( tr("DefaultType"),
        data.DefaultType,
        TYPE_NAME_LEN,
        " abcdefghijklmnopqrstuvwxyz0123456789-./"
      ));
}

void cMenuSetupXms::Store(void)
{_
  XmsSetup = data;
  SetupStore( "DbHost", data.DbHost);
  SetupStore( "DbName", data.DbName);
  SetupStore( "DbUser", data.DbUser);
  SetupStore( "DbPassword", data.DbPassword);
  SetupStore( "DbPort", data.DbPort);
  SetupStore( "DbSocket", data.DbSocket);
  SetupStore( "DefaultType", data.DefaultType);
}
