/*
 * xmlfile.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <iconv.h>
#include "xmlfile.h"
#include "i18n.h"

// --- cManualFileInfo -------------------------------------------------------------

cManualFileInfo::cManualFileInfo(const char * FileName)
:manual(NULL),
context(NULL),
langxpath(NULL),
filename(NULL),
type(NULL)
{
  filename = strdup(FileName);
  usedby = 0;
}

cManualFileInfo::~cManualFileInfo()
{
  free(type);
  free(langxpath);
  free(filename);
  xmlXPathFreeContext(context);
  xmlFreeDoc(manual);
}

void cManualFileInfo::decUsedBy(void)
{
  usedby += 1;
}

void cManualFileInfo::incUsedBy(void)
{
  usedby -= 1;
}

bool cManualFileInfo::Init(void)
{
  xmlNodePtr cur;
  xmlXPathObjectPtr result;
  char *xpath_buffer = NULL;
  xmlChar *xmlType = NULL;

  manual = xmlParseFile(AddDirectory(cfgDir, filename));
  if (manual == NULL)
  {
    esyslog("manual: %s/%s not parsed successfully.", cfgDir, filename);
    return false;
  }
  cur = xmlDocGetRootElement(manual);
  if (cur == NULL) {
    esyslog("manual: empty %s document.", filename);
    return false;
  }
  if (xmlStrcmp(cur->name, (const xmlChar *) "manual"))
  {
    esyslog("document of the wrong type, root node != manual");
    return false;
  }
  xmlType = xmlGetProp(cur, (const xmlChar *) "type");
  if(xmlType)
  {
    type = (char *)xmlType;
  }
  else
    asprintf(&type, "norm");
//   if(strcasecmp(type, "base"))
//   {
//     free(type);
//     asprintf(&type, "norm");
//   }
//   else
//   {
//     type = "norm";
//   }
  context = xmlXPathNewContext(manual);
  asprintf(&xpath_buffer,"/manual/language[normalize-space(@name)='%s']",tr("English"));
  free(langxpath);
  langxpath = strdup(xpath_buffer);
  free(xpath_buffer);
  result = GetNodeset((xmlChar *)langxpath);
  if (!result)
  {
    dsyslog("manual: No translation found for language: '%s'",tr("English"));
    free(langxpath);
    langxpath = "/manual/language[1]";
    result = GetNodeset((xmlChar *)langxpath);
    if (!result)
    {
      esyslog("manual: No language node found");
      return false;
    }
  }
  return true;
}

xmlXPathObjectPtr cManualFileInfo::GetNodeset(xmlChar *Xpath)
{
  xmlXPathObjectPtr result;
  result = xmlXPathEvalExpression(Xpath, context);
  if(xmlXPathNodeSetIsEmpty(result->nodesetval))
  {
    return NULL;
  }
  return result;
}

char *cManualFileInfo::GetString(xmlNodePtr list)
{
  char *str = NULL;
  xmlChar *xmlStr = NULL;
  xmlChar *xmlBuffer = xmlNodeListGetString(manual, list, 1);
  if(xmlBuffer)
  {
    xmlStr = xmlStrdup(xmlBuffer);
    xmlFree(xmlBuffer);
    str = (char *)xmlStr;
    if(!isempty(str))
      return Conv(xmlStr);
  }
  free(str);
  str = (char *)tr("no text!");
  return strdup(str);
}


char *cManualFileInfo::Conv(xmlChar *xmlText)
{
  char *in = (char *)xmlText;
  iconv_t cd;
  size_t incount = strlen(in);
  size_t outcount = incount*5;
  char *r=(char *)malloc(outcount+1);
  char *out=r;
  int v;

  cd = iconv_open("ISO-8859-15","UTF-8");
  if (cd == (iconv_t)-1)
  {
    esyslog("manual: iconv_test: iconv_open() failed");
    return (char *)xmlText;
  }
  v = iconv(cd,(char **) &in, &incount, &out, &outcount);
  if (v == -1)
  {
    esyslog("manual: iconv_test: iconv() failed");
    return (char *)xmlText;
  }
  *out = 0;
  r=(char *)realloc(r,strlen(r)+1);
  return r;
}
