/*
 * vdrc.c: VDR Commander plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <getopt.h>
#include <stdlib.h>
#include <vdr/interface.h>
#include <vdr/plugin.h>
#include "i18n.h"
#include "mainwnd.h"
#include "global.h"

static const char *VERSION        = "0.0.8";
static const char *DESCRIPTION    = "VDR Commander";
static const char *MAINMENUENTRY  = "VDRC";

class cPluginVDRC : public cPlugin {
private:
  // Add any member variables or functions you may need here.
public:
  cPluginVDRC(void);
  virtual ~cPluginVDRC();
  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 Start(void);
  virtual void Housekeeping(void);
  virtual const char *MainMenuEntry(void) { return tr(MAINMENUENTRY); }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  };

// Global variables that control the overall behaviour:

char	m_sDefDir[_DEF_MAX_WIN][255];	// Default Directories
bool	m_bConfirmCopy		= true;
bool	m_bConfirmDelete	= true;
bool	m_bConfirmRemove	= true;

// --- cMenuSetupVDRC -------------------------------------------------------

class cMenuSetupVDRC : public cMenuSetupPage {
private:
  int	bNewConfirmCopy;
  int	bNewConfirmDelete;
  int	bNewConfirmRemove;
protected:
  virtual void Store(void);
public:
  cMenuSetupVDRC(void);
  };

cMenuSetupVDRC::cMenuSetupVDRC(void)
{
  bNewConfirmCopy	= m_bConfirmCopy;
  bNewConfirmDelete	= m_bConfirmDelete;
  bNewConfirmRemove	= m_bConfirmRemove;
  Add(new cMenuEditBoolItem(tr("Confirm Copy"), &bNewConfirmCopy));
  Add(new cMenuEditBoolItem(tr("Confirm Delete"), &bNewConfirmDelete));
  Add(new cMenuEditBoolItem(tr("Confirm Remove"), &bNewConfirmRemove));
}

void cMenuSetupVDRC::Store(void)
{
  SetupStore("CopyConfirm",   m_bConfirmCopy = bNewConfirmCopy);
  SetupStore("DeleteConfirm", m_bConfirmDelete = bNewConfirmDelete);
  SetupStore("RemoveConfirm", m_bConfirmRemove = bNewConfirmRemove);
}

// --- cPluginVDRC ----------------------------------------------------------

const char *cPluginVDRC::CommandLineHelp(void)
{
    // Return a string that describes all known command line options.
    return NULL;
}
    
bool cPluginVDRC::ProcessArgs(int argc, char *argv[])
{
    // Implement command line argument processing here if applicable.
    return true;
}
	

cPluginVDRC::cPluginVDRC(void)
{
  // 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!

    for (int i = 0; i < _DEF_MAX_WIN; i++)
    {
	m_sDefDir[i][0] = '/';
	m_sDefDir[i][1] = '\0';
    }
}

cPluginVDRC::~cPluginVDRC()
{
  // Clean up after yourself!
    for (int i = 0; i < _DEF_MAX_WIN; i++)
    {
	char *s;
	
	asprintf(&s, "DefPathPanel%d", i);
	SetupStore(s, m_sDefDir[i]);
	
	free(s);
    }
    
    Setup.Save();
}

bool cPluginVDRC::Start(void)
{
  // Start any background activities the plugin shall perform.
  RegisterI18n(Phrases);
  return true;
}

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

cOsdObject *cPluginVDRC::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  return new cMainWnd;
}

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

bool cPluginVDRC::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  
  if      (!strcasecmp(Name, "CopyConfirm"))     m_bConfirmCopy = atoi(Value);
  else if (!strcasecmp(Name, "DeleteConfirm"))   m_bConfirmDelete = atoi(Value);
  else if (!strcasecmp(Name, "RemoveConfirm"))   m_bConfirmRemove = atoi(Value);
  else if (!strncmp(Name, "DefPathPanel", 12))
  {
    int iPanel;
    iPanel = atoi(Name + 12);
    if ((iPanel >= 0) && (iPanel < _DEF_MAX_WIN))
	strcpy(m_sDefDir[iPanel], Value);
  }
  else
     return false;

  return true;
}

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