/*
 * A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * File: pin.h 
 *
 * ---------------------------------------------------------------------
 * Version - History
 * ---------------------------------------------------------------------
 *
 * #17 04.05.06, horchi - added:  plugin protection
 *                        bugfix: fixed problem with recording subdirecroties 
 *                                (bug was reported by Viking, thanks!)
 * #16 01.05.06, horchi - bugfix: fixed permissions ot system message queues.
 *                        added:  num keys navigation for pin menu
 *                        added:  Missing translation for 'Recording 
 *                                protected, enter pin code first'.
 *                                All this detected and reported by Michael Nausch, many thanks!
 * #15 29.04.06, horchi - added: french translations (thanks to Pat!)
 *                        changed: Makefile due to VDR's new APIVERSION feature
 * #14 08.04.06, horchi - added: protection feature for scripting
 * #13 26.03.06, horchi - bugfix: autoProtectionMode now respect start time
 *                                of event (needed since channels not 
 *                                protected every time)
 * #12 20.03.06, horchi - change: now using aux field in timer config patch
 *                        bugfix: autoProtectionMode will now restored from config
 *                                (Bug reported by rookie1, thanks!)
 *                        bugfix: autoProtectionMode now working even with vdr 1.3.44
 *                                (Bug reported by rookie1, thanks!)
 * #11 26.02.06, horchi - bugfix: create missing config directory at startup.
 *                        bugfix: keys 1-5 in pin menu now working.
 *                        added: hungary translations (thanks to Albert Papp!)
 *                        change: silend skip of channels works now again 
 *                                (fixed problems due to changes of VDR 1.3.40)
 * #10 28.01.06, horchi - added regular expressions for brodcasts locks.
 *                        added russian translations (appropriated by Monchenko Vladimir, thanks!)
 *                        bugfix: only 3 chars of pin in setup menu (reported by Monchenko Vladimir, thanks!)
 * #9  26.01.06, horchi - added lock for brodcasts.
 * #8  17.01.06, horchi - added channel lock time range.
 * #7  15.01.06, horchi - added channel lock list and improved configuration
 * #6f 13.01.06, horchi - removed pin request on demand
 * #6e 23.12.05, horchi - fixed syslog message at channel check
 * #6d 18.12.05, horchi - dynamic handling of menu entries
 * #6c 18.12.05, horchi - added internationalisation
 * #6b 18.12.05, horchi - change, reset time now in minutes
 * #6a 17.12.05, horchi - added reset time for pin code
 * #6  17.12.05, horchi - added protection flag to timer config
 * #5b 11.12.05, horchi - some bugfixes, added handling for recording
 * #5a 10.12.05, horchi - added pin request on demand, even for replay of recorings
 * #5  10.12.05, horchi - added pin request on demand
 * #4  08.12.05, horchi - added channel protection
 * #3  06.12.05, horchi - added protection of recording sub-directorys
 * #2  01.12.05, horchi - bugfixes
 * #1  11.04.05, horchi - initial release
 * 
 * ---------------------------------------------------------------------
 * Todos
 * ---------------------------------------------------------------------
 *
 * - lock for plugins
 * - broadcast locks via event description and/or epg text
 *
 * ---------------------------------------------------------------------
 */

#ifndef __PIN_H__
#define __PIN_H__

//***************************************************************************
// Includes
//***************************************************************************

#include <vdr/status.h>
#include <vdr/plugin.h>

#include "locks.h"
#include "def.h"
#include "i18n.h"
#include "msgservice.h"

//***************************************************************************
// Constants
//***************************************************************************

static const char *VERSION        = "0.0.17";
static const char *DESCRIPTION    = "Pin code plugin";
static const char *MAINMENUENTRY  = "Pin";

#define DEFAULT_PIN "1984"
#define PROTECTION_FILE "protection.fsk"

//***************************************************************************
// Message Reciver
//***************************************************************************

class MessageReceiver : public cThread, public MessageService
{
   public:
 
      MessageReceiver();
      ~MessageReceiver();

      int StartReceiver();
      int StopReceiver();
      const char* getMessage() { return message; }
      Header* getHeader()      { return header; }

   protected:

      void Action();
      int wait();

      // data

      int pid;
      bool active;
      int msgID;
      char buffer[sizeBuffer];
      char* message;
      Header* header;
};

//***************************************************************************
// Pin Status Monitor
//***************************************************************************

class cPinStatusMonitor : public cStatus 
{
   protected:

      // interface

      virtual void ChannelSwitch(const cDevice *Device, int ChannelNumber);
      virtual bool ChannelProtected(const cDevice *Device, const cChannel* Channel);
      virtual bool ReplayProtected(const cRecording* Recording, const char* Name, 
                                   const char* Base, bool isDirectory);
      virtual void RecordingFile(const char* FileName);     
      virtual void TimerCreation(cTimer* Timer, const cEvent *Event);
      virtual bool PluginProtected(cPlugin* Plugin);

      // internal stuff

      const cEvent* GetEventOf(const cChannel* Channel);
};

//***************************************************************************
// Pin
//***************************************************************************

class cPin : public cOsdObject 
{
   public:

      cPin(void);
      ~cPin();
      virtual void Show(void);
      virtual eOSState ProcessKey(eKeys Key);
      void clear();

   private:

      cOsd* osd;
      int textWidth;
      const char* pinTxt;
      char code[20+TB];
};

//***************************************************************************
// Pin Plugin
//***************************************************************************

class cPinPlugin : public cPlugin 
{
  public:
   
      enum Size
      {
         sizePinCode = 4
      };
   
      enum AutoProtectionMode
      {
         apmUnknown = na,
         
         apmAlways,
         apmIntelligent,
         apmNever,

         apmCount
      };

      cPinPlugin(void);
      virtual ~cPinPlugin();

      const char* CommandLineHelp(void)        { return NULL; }
      bool ProcessArgs(int argc, char *argv[]) { return true; }
      bool Start(void);
      void Stop(void);
      void Housekeeping(void)                  {}

      virtual cMenuSetupPage *SetupMenu(void);
      virtual bool SetupParse(const char *Name, const char *Value);
      const char* Version(void)                { return VERSION; }
      const char* Description(void)            { return DESCRIPTION; }
      virtual bool Initialize(void);
      virtual const char* MainMenuEntry(void)  { return tr(MAINMENUENTRY); }
      virtual cOsdObject* MainMenuAction(void);

      void addChannel();
      void delChannel();
      int channelProtected(const char* name, long startTime = 0);
      int pluginProtected(const char* name);
      int broadcastProtected(const char* title);
      void checkActionTime();
      int initPluginList();

      static cPinPlugin* getObject()            { return object; }

      // static config items

      static char pinCode[sizePinCode+TB];
      static bool skipChannelSilent;
      static int pinResetTime;
      static int autoProtectionMode;

      // other static stuff

      static const char* autoProtectionModes[apmCount+1];

  protected:

      // data

      cLockItems lockedChannels;
      cLockedBroadcasts lockedBroadcasts;
      cLockItems lockedPlugins;
      time_t lastAction;
      cPinStatusMonitor* statusMonitor;
      MessageReceiver* receiver;

      static cPinPlugin* object;    // the object
};

//***************************************************************************
#endif // __PIN_H__
