/*
 * manual.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 "i18n.h"
#include "menu.h"
#include "setup.h"

char *cfgDir;
cManualFiles ManualFiles;

cPluginManual::cPluginManual(void)
{
  cfgDir = NULL;
  // Initialize any member variables here.
  // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
}

cPluginManual::~cPluginManual()
{
  // Clean up after yourself!
  delete[] cfgDir;
}

const char *cPluginManual::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return NULL;
}

bool cPluginManual::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  return true;
}

bool cPluginManual::Initialize(void)
{
  // Initialize any background activities the plugin shall perform.
  return true;
}

bool cPluginManual::Start(void)
{
  const char * str;

  i18n_name=Name();

  // Start any background activities the plugin shall perform.
  RegisterI18n(Phrases);

  str = ConfigDirectory("manual");
  if (!str)
    return false;

  cfgDir = strdup(str);
  return true;
}

void cPluginManual::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
}

cOsdMenu *cPluginManual::MainMenuAction(void)
{
  ManualFiles.Clear();
  cManualFileInfo *fi = new cManualFileInfo(ManualSetup.basefile);
  if(fi->Init())
  {
    ManualFiles.Add(fi);
    return SetManualOsd(fi, fi->LangXPath());
  }
  Skins.Message(mtError, tr("Error reading manual!"));
  return NULL;
}

cOsdMenu *cPluginManual::HelpByRef(const char * FileName, const char * Ref)
{
  char * filename = NULL;
  char * xpath = NULL;
  ManualFiles.Clear();
  filename = strdup(FileName);
  cManualFileInfo *fi = new cManualFileInfo(filename);
  if(fi->Init())
  {
    asprintf(&xpath,"%s/descendant::*[normalize-space(@ref)='%s']/parent::*",fi->LangXPath(),Ref);
    ManualFiles.Add(fi);
    return SetManualOsd(fi, xpath);
  }
  Skins.Message(mtError, tr("Error reading manual!"));
  return NULL;
}

// cOsdMenu *cPluginManual::HelpByXPath(const char * FileName, const char * XPath)
// {
//   char * filename = NULL;
//   char * xpath = NULL;
//   ManualFiles.Clear();
//   filenme = strdup(FileName);
//   cManualFileInfo fi = new cManualFileInfo(filename);
//   if(fi.Init())
//   {
//     if(XPath)
//       xpath = XPath;
//     else
//       asprintf(&xpath,"%s/child::*",fi.LangXPath());
//     ManualFiles.Add(fi);
//     return SetManualOsd(fi, xpath);
//   }
//   Skins.Message(mtError, tr("Error reading manual!"));
//   return NULL;  
// }

cMenuSetupPage *cPluginManual::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.
  return new cMenuSetupManual();
}

bool cPluginManual::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  return ManualSetup.SetupParse(Name, Value);
}

VDRPLUGINCREATOR(cPluginManual); // Don't touch this!
