/*
 * setupmenu.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */
 
 #include "manual.h"
 #include "setup.h"
 #include "xmlfile.h"
 
 #define FINDCMD  "cd '%s' && find '%s' -maxdepth 1 -follow -iname 'manual-*.xml' -printf '%%f\n' 2> /dev/null"

cManualSetup ManualSetup;

// --- cManualSetup ------------------------------------------------------------------------

cManualSetup::cManualSetup(void) 
{
  strcpy(basefile, "manual-base.xml");
}
 
bool cManualSetup::SetupParse(const char *Name, const char *Value) 
{
  if (!strcasecmp(Name, "basefile")) strn0cpy(ManualSetup.basefile,Value,MaxFileName);
  else
    return false;
  return true;
}

// --- cMenuSetupManual ---------------------------------------------------------------------

cMenuSetupManual::cMenuSetupManual(void)
{
  char *cmd = NULL;
  char *buf = NULL;
  int i;
  cReadLine ReadLine;
 
  SetSection(tr("Manual"));
  
  asprintf(&buf, "%s: %s", tr("Current basefile"), ManualSetup.basefile);
  Add(new cManualTextItem(buf));
  free(buf);
  Add(new cManualTextItem("__________________________________________________"));
  
  Add(new cManualTextItem(tr("Available basefiles:")));
  Add(new cManualTextItem(""));

  asprintf(&cmd, FINDCMD, cfgDir, cfgDir);
  FILE *p = popen(cmd, "r");
  if (p) 
  {
    char *s;
    while ((s = ReadLine.Read(p)) != NULL) 
    {
      cManualFileInfo *fi = new cManualFileInfo(s);
      if(fi->Init())
      {
        if(!strcasecmp(fi->Type(), "base"))
        {
          Add(new cOsdItem(s));
        }
      }
      delete fi;
    }
    pclose(p);
  }
  free(cmd);

  newManualSetup = ManualSetup;

  for (i = 0; i < Count(); i++)
  {
    if (!strcasecmp(Get(i)->Text(), newManualSetup.basefile))
    {
      break;
    }
  }
  SetCurrent(Get(i));
  SetHelp(tr("Help"));
  Display();
}

void cMenuSetupManual::Store(void)
{
  SetupStore("basefile", newManualSetup.basefile);
  ManualSetup = newManualSetup;
}

eOSState cMenuSetupManual::ProcessKey(eKeys Key)
{
  cPluginManual *p;
  cOsdMenu *helpmenu = NULL;
  
  eOSState state = cOsdMenu::ProcessKey(Key);
  
  if (state == osUnknown) 
  {    
    switch (Key) 
    {
    case kRed:
      p = (cPluginManual *)cPluginManager::GetPlugin("manual");
      helpmenu = p->HelpByRef("manual-manual.xml", "SetupMenu");
      AddSubMenu(helpmenu);
      break;
    case kOk:
      if(((cOsdItem*)Get(Current()))->Selectable() == true)
      {
        strn0cpy(newManualSetup.basefile,Get(Current())->Text(),MaxFileName);
        Store();
      }
      state = osBack;
      break;
    default:
      break;
    }//switch
  }//if    
  return state;
}

// --- cManualTextItem -----------------------------------------------------------------------

cManualTextItem::cManualTextItem(const char *Text)
:cOsdItem(Text)
{
  SetSelectable(false);
}