/**
 *  GraphTFT plugin for the Video Disk Recorder 
 * 
 *  setup.c - A plugin for the Video Disk Recorder
 *
 *  (c) 2004-2006 Lars Tegeler, Sascha Volkenandt, Jörg Wendel
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 * $Id: setup.c,v 1.8 2007/11/07 06:22:50 root Exp $
 *
 **/

#include "common.h"
#include "setup.h"

#ifdef HAVE_DFB
# include "cache.h"
#endif

//***************************************************************************
// Log Devices
//***************************************************************************

static const char* logDevices[] = 
{
   "none",
   "stdout",
   "syslog",
   "file"
};

cGraphTFTSetup GraphTFTSetup;

//***************************************************************************
// Object
//***************************************************************************

cGraphTFTSetup::cGraphTFTSetup()
{
    Theme = "";
    index = 0;
    Level=0;
    LogDevice=0;
    Iso2Utf = 1;
    HideMainMenu = 0;
    UseStillPicture = 1;
    DvbRefresh = 100;
    yBorder = 5;
    xBorder = 25;
    xOffset = 0;
    yOffset = 0;
    DumpImage = 0;
    DumpRefresh = 5;
    DumpImageX = 720;
    DumpImageY = 576;
    PluginConfPath = strdup("/tmp");
    JpegQuality = 60;
    enableSpectrumAnalyzer = 0;
    normalMode = "Standard";
    width = 800;
    height = 600;

    snapshotWidth = 240;
    snapshotHeight = 180;
    snapshotQuality = 90;
    strcpy(snapshotPath, "/tmp");
}

//***************************************************************************
// Setup Parse
//***************************************************************************

bool cGraphTFTSetup::SetupParse(const char* Name, const char* Value)
{
    if      (!strcasecmp(Name, "Theme"))             Theme = Value;
    else if (!strcasecmp(Name, "HideMainMenu"))      HideMainMenu  = atoi(Value);
    else if (!strcasecmp(Name, "Iso2Utf"))           Iso2Utf  = atoi(Value);
    else if (!strcasecmp(Name, "UseStillPicture"))   UseStillPicture  = atoi(Value);
    else if (!strcasecmp(Name, "SpectrumAnalyzer"))  enableSpectrumAnalyzer  = atoi(Value);
    else if (!strcasecmp(Name, "DvbRefresh"))        DvbRefresh  = atoi(Value);
    else if (!strcasecmp(Name, "XBorder"))           xBorder  = atoi(Value);
    else if (!strcasecmp(Name, "YBorder"))           yBorder  = atoi(Value);
    else if (!strcasecmp(Name, "XOffset"))           xOffset  = atoi(Value);
    else if (!strcasecmp(Name, "YOffset"))           yOffset  = atoi(Value);
    else if (!strcasecmp(Name, "DumpImage"))         DumpImage  = atoi(Value);
    else if (!strcasecmp(Name, "DumpRefresh"))       DumpRefresh  = atoi(Value);
    else if (!strcasecmp(Name, "DumpImageX"))        DumpImageX  = atoi(Value);
    else if (!strcasecmp(Name, "DumpImageY"))        DumpImageY  = atoi(Value);
    else if (!strcasecmp(Name, "LogDevice"))         LogDevice  = atoi(Value);
    else if (!strcasecmp(Name, "Level"))             Level  = atoi(Value);
    else if (!strcasecmp(Name, "JpegQuality"))       JpegQuality  = atoi(Value);
    else if (!strcasecmp(Name, "flipOSD"))           flipOSD  = atoi(Value);
    else if (!strcasecmp(Name, "normalMode"))        normalMode = Value;
    else if (!strcasecmp(Name, "width"))             width = atoi(Value);
    else if (!strcasecmp(Name, "height"))            height = atoi(Value);

    else if (!strcasecmp(Name, "snapshotWidth"))     snapshotWidth = atoi(Value);
    else if (!strcasecmp(Name, "snapshotHeight"))    snapshotHeight = atoi(Value);
    else if (!strcasecmp(Name, "snapshotQuality"))   snapshotQuality = atoi(Value);
    else if (!strcasecmp(Name, "snapshotPath"))      strcpy(snapshotPath, Value);

    else return false;

    return true;
}

//***************************************************************************
// cMenuSetupGraphTFT
//***************************************************************************

cMenuSetupGraphTFT::cMenuSetupGraphTFT(cGraphTFTDisplay* graphTFTdisplay)
{
   cGraphTFTTheme* t;
   char* buf = 0;
   int i;

   for (int i = 0; i < 100; i++)
      themeNames[i] = 0;

   _data = GraphTFTSetup;
   _graphTFTdisplay = graphTFTdisplay;

   SetSection(tr("GraphTFT"));

   _data.index = theTheme->Index();

   for (i = 0, t = themes.First(); t && i < 100; t = themes.Next(t), i++)
      asprintf(&themeNames[i], t->getName().c_str());

   if (themes.Count())
      Add(new cMenuEditStraItem(tr("Theme"), &_data.index, themes.Count(), themeNames));

   Add(new cMenuEditBoolItem(tr("Hide Mainmenu Entry"),        &_data.HideMainMenu));
   Add(new cMenuEditBoolItem(tr("Convert Iso to UTF-8"),       &_data.Iso2Utf));
   Add(new cMenuEditBoolItem(tr("Spectrum Analyzer"),          &_data.enableSpectrumAnalyzer));

   asprintf(&buf, "--- %s --------------------------------------------", tr("Dump Image"));
   Add(new cOsdItem(buf));
   free(buf);
   Add(new cMenuEditBoolItem(tr("Dump image to file"),         &_data.DumpImage));
   Add(new cMenuEditIntItem(tr("Dump Refresh"),                &_data.DumpRefresh,0,600));
   Add(new cMenuEditIntItem(tr("Dump image width"),            &_data.DumpImageX,1,1024));
   Add(new cMenuEditIntItem(tr("Dump image height"),           &_data.DumpImageY,1,768));

   asprintf(&buf, "--- %s --------------------------------------------", tr("Snapshot"));
   Add(new cOsdItem(buf));
   free(buf);
   Add(new cMenuEditIntItem(tr("Snapshot width"),            &_data.snapshotWidth, 1, 720));
   Add(new cMenuEditIntItem(tr("Snapshot height"),           &_data.snapshotHeight, 1, 576));
   Add(new cMenuEditIntItem(tr("Jpeg Quality"),              &_data.snapshotQuality, 0, 100));
   Add(new cMenuEditStrItem(tr("Snapshot path"),             _data.snapshotPath, 255, FileNameChars));

#ifdef HAVE_IMLIB
   asprintf(&buf, "--- %s --------------------------------------------", tr("DVB Device"));
   Add(new cOsdItem(buf));
   free(buf);
   Add(new cMenuEditBoolItem(tr("Use StillPicture"),           &_data.UseStillPicture));
   Add(new cMenuEditIntItem(tr("Refresh"),                     &_data.DvbRefresh,0,1024));
   asprintf(&buf, "--- %s --------------------------------------------", tr("DVB/FB Device"));
   Add(new cOsdItem(buf));
   free(buf);
   Add(new cMenuEditBoolItem(tr("Flip OSD"),                   &_data.flipOSD));
   Add(new cMenuEditIntItem(tr("Width"),                       &_data.width,1,9999));
   Add(new cMenuEditIntItem(tr("Height"),                      &_data.height,1,9999));
   Add(new cMenuEditIntItem(tr("X Offset"),                    &_data.xOffset,0,719));
   Add(new cMenuEditIntItem(tr("Y Offset"),                    &_data.yOffset,0,575));
   Add(new cMenuEditIntItem(tr("Border to Width"),             &_data.xBorder,0,575));
   Add(new cMenuEditIntItem(tr("Border to Height"),            &_data.yBorder,0,719));
   asprintf(&buf, "--- %s -----------------------------------------", tr("TCP Connection"));
   Add(new cOsdItem(buf));
   free(buf);
   Add(new cMenuEditIntItem(tr("Jpeg Quality"),                &_data.JpegQuality, 0, 100));
#endif

#ifdef HAVE_DFB
   asprintf(&buf, "--- %s --------------------------------------------", tr("directFB Device"));
   Add(new cOsdItem(buf));
   free(buf);
#endif

   asprintf(&buf, "--- %s --------------------------------------------", tr("Debug"));
   Add(new cOsdItem(buf));
   free(buf);
   Add(new cMenuEditStraItem(tr("Log Device"),                 &_data.LogDevice, 4, logDevices));
   Add(new cMenuEditIntItem(tr("Debug Level"),                 &_data.Level,0,10));

   SetHelp(0,0,0,0);
}

//***************************************************************************
// 
//***************************************************************************

cMenuSetupGraphTFT::~cMenuSetupGraphTFT()
{
   for (int i = 0; themeNames[i] && i < 100; i++)
      free(themeNames[i]);
}

//***************************************************************************
// 
//***************************************************************************

eOSState cMenuSetupGraphTFT::ProcessKey(eKeys Key)
{
   eOSState state = cOsdMenu::ProcessKey(Key);

   switch (state)
   {
      case osUnknown:

         if (Key == kOk)
         {
#ifdef HAVE_DFB   

            // clear image cache

            if (GraphTFTSetup.Theme != _data.Theme)
               imageCache.Clear();
#endif
            Store();
            return osBack;
         }

      default:
         break;
   }

   return state;
}

//***************************************************************************
// Store
//***************************************************************************

void cMenuSetupGraphTFT::Store()
{
    GraphTFTSetup = _data;

    theTheme = themes.Get(GraphTFTSetup.index);
    GraphTFTSetup.Theme = theTheme->getName();

    SetupStore("Theme", GraphTFTSetup.Theme.c_str());
    SetupStore("HideMainMenu", GraphTFTSetup.HideMainMenu);
    SetupStore("Iso2Utf", GraphTFTSetup.Iso2Utf);
    SetupStore("UseStillPicture", GraphTFTSetup.UseStillPicture);
    SetupStore("SpectrumAnalyzer", GraphTFTSetup.enableSpectrumAnalyzer);
    SetupStore("DvbRefresh", GraphTFTSetup.DvbRefresh);
    SetupStore("XBorder", GraphTFTSetup.xBorder);
    SetupStore("YBorder", GraphTFTSetup.yBorder);
    SetupStore("XOffset", GraphTFTSetup.xOffset);
    SetupStore("YOffset", GraphTFTSetup.yOffset);
    SetupStore("DumpImage", GraphTFTSetup.DumpImage);
    SetupStore("DumpRefresh", GraphTFTSetup.DumpRefresh);
    SetupStore("DumpImageX", GraphTFTSetup.DumpImageX);
    SetupStore("DumpImageY", GraphTFTSetup.DumpImageY);
    SetupStore("LogDevice", GraphTFTSetup.LogDevice);
    SetupStore("Level", GraphTFTSetup.Level);
    SetupStore("JpegQuality", GraphTFTSetup.JpegQuality);
    SetupStore("flipOSD", GraphTFTSetup.flipOSD);
    SetupStore("Width", GraphTFTSetup.width);
    SetupStore("Height", GraphTFTSetup.height);

    SetupStore("snapshotWidth", GraphTFTSetup.snapshotWidth);
    SetupStore("snapshotHeight", GraphTFTSetup.snapshotHeight);
    SetupStore("snapshotQuality", GraphTFTSetup.snapshotQuality);
    SetupStore("snapshotPath", GraphTFTSetup.snapshotPath);

    if (_graphTFTdisplay)    
       _graphTFTdisplay->setupChanged();
}
