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

#include <vdr/plugin.h>
#include <vdr/menu.h>
#include <vdr/status.h>
#include "i18n.h"

#define MOUNTPOINT "/media/cdrom"

int hidemainmenu=false;
extern const char *VideoDirectory;
extern bool reload;
bool reload;

static const char *VERSION        = "0.3a";
static const char *DESCRIPTION    = "Play VDR recordings from DVD/CD";
static const char *MAINMENUENTRY  = "VDR Recordings on DVD/CD";

class VdrRecSetup:public cMenuSetupPage
{
 protected:
  virtual void Store(){SetupStore("hidemainmenu",hidemainmenu);}
 public:
  VdrRecSetup(){Add(new cMenuEditBoolItem(tr("Hide main menu entry?"),&hidemainmenu));}
};

class VdrRecStatus:public cStatus
{
 public:
  VdrRecStatus(void) { reload = false; }
 protected:
  virtual void OsdTitle(const char *Title)
  {
   if(!strncasecmp("VDR",Title,3) && reload)
   {
    Recordings.Load();
    reload = false;
   }
  }
};

class cPluginVdrrec : public cPlugin {
private:
  char *mountpoint;
  VdrRecStatus *status;
public:
  cPluginVdrrec(void);
  virtual ~cPluginVdrrec();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return tr(DESCRIPTION); }
  virtual const char *CommandLineHelp(void);
  virtual bool ProcessArgs(int argc, char *argv[]);
  virtual bool Initialize(void);
  virtual bool Start(void);
  virtual void Stop(void);
  virtual void Housekeeping(void);
  virtual const char *MainMenuEntry(void){return hidemainmenu?NULL:tr(MAINMENUENTRY);}
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  };

cPluginVdrrec::cPluginVdrrec(void)
{
 status=new VdrRecStatus;
 mountpoint=NULL;
}

cPluginVdrrec::~cPluginVdrrec()
{
 delete status;
 free(mountpoint);
}

const char *cPluginVdrrec::CommandLineHelp(void)
{
 return " -m mountpoint    directory where the cd/dvd-drive is mounted\n";
}

bool cPluginVdrrec::ProcessArgs(int argc, char *argv[])
{
 if(argc==3&&!strcmp("-m",argv[1]))mountpoint=strdup(argv[2]);
 return true;
}

bool cPluginVdrrec::Initialize(void)
{
  RegisterI18n(Phrases);
  return true;
}

bool cPluginVdrrec::Start(void)
{
 if(mountpoint==NULL)asprintf(&mountpoint,"%s",MOUNTPOINT);
 return true;
}

void cPluginVdrrec::Stop(void)
{
}

void cPluginVdrrec::Housekeeping(void)
{
}

cOsdObject *cPluginVdrrec::MainMenuAction(void)
{
 const char *oldVideoDir=VideoDirectory;
 reload = true;
 
 VideoDirectory=mountpoint;
#if VDRVERSNUM >= 10311
 Recordings.Load();
#endif
 cOsdObject *menu=new cMenuRecordings;

 VideoDirectory=oldVideoDir;

 return menu;
}

cMenuSetupPage *cPluginVdrrec::SetupMenu(void)
{
  return new VdrRecSetup;
}

bool cPluginVdrrec::SetupParse(const char *Name, const char *Value)
{
 if(!strcmp("hidemainmenu",Name))hidemainmenu=atoi(Value);
 return true;
}

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