//-----------------------------------------------------------------------------
/*
 * Description: This file contains the class AxPluginSetup
 *
 * See the file README in the main directory for a description of
 * this software, copyright information, and how to reach the author.
 *
 * Author: alex
 * Date:   15.11.2003
 *
 * Last modfied:
 *   $Author: alex $
 *   $Date: 2004/05/31 12:52:21 $
 *   $Revision: 1.14 $
 */
//-----------------------------------------------------------------------------

//=============================================================================
//     includes
//=============================================================================

//----- multiple defs of swap() workaround ------------------------------------
#include "AxPluginGlobals.h"

//----- qt --------------------------------------------------------------------
//----- CORBA -----------------------------------------------------------------
//----- ICE -------------------------------------------------------------------
//----- C++ -------------------------------------------------------------------
//----- C ---------------------------------------------------------------------
//----- vdr -------------------------------------------------------------------
#include <vdr/plugin.h>

//----- AxLib -----------------------------------------------------------------
#include <Ax/Tools/Trace.h>
// #include <Ax/Tools/TraceOn.h>
#include <Ax/Mail/MailBoxSetup.h>

//----- local -----------------------------------------------------------------
#include "AxPluginSetup.h"

//----- locals ----------------------------------------------------------------

//=============================================================================
//     using namespaces
//=============================================================================
using namespace std;
using namespace Ax;

//=============================================================================
//     consts
//=============================================================================
const std::string AccountsFileName = std::string("accounts.conf");

//=============================================================================
//     class AxPluginSettings
//=============================================================================

//-------------------------------------------------------------------------
//     AxPluginSettings::AxPluginSettings()
//-------------------------------------------------------------------------
AxPluginSettings::AxPluginSettings()
{
  clear();
} // AxPluginSettings::AxPluginSettings()

//-------------------------------------------------------------------------
//     AxPluginSettings::AxPluginSettings()
//-------------------------------------------------------------------------
AxPluginSettings::AxPluginSettings(const AxPluginSettings &that)
                : BGCheckDelay    (that.BGCheckDelay    )
                , DebugCClient    (that.DebugCClient    )
#if ALEX_NOPORT
                , MBV_Font        (that.MBV_Font        )
                , MLV_Font        (that.MLV_Font        )
#endif
                , MLV_SortOrder   (that.MLV_SortOrder   )
                , MLV_MaxMails    (that.MLV_MaxMails    )
#if ALEX_NOPORT
                , MV_HeadFont     (that.MV_HeadFont     )
                , MV_BodyFont     (that.MV_BodyFont     )
                , MV_JumpSize     (that.MV_JumpSize     )
#endif
                , MV_ViewMode     (that.MV_ViewMode     )
{
  strcpy(MailCmd, that.MailCmd);
} // AxPluginSettings::AxPluginSettings()

//-------------------------------------------------------------------------
//     AxPluginSettings::operator=()
//-------------------------------------------------------------------------
AxPluginSettings &AxPluginSettings::operator=(const AxPluginSettings &that)
{
  if (this != &that)
  {
    BGCheckDelay     = that.BGCheckDelay ;
    DebugCClient     = that.DebugCClient ;

#if ALEX_NOPORT
    MBV_Font         = that.MBV_Font     ;

    MLV_Font         = that.MLV_Font     ;
#endif
    MLV_SortOrder    = that.MLV_SortOrder;
    MLV_MaxMails     = that.MLV_MaxMails ;

#if ALEX_NOPORT
    MV_HeadFont      = that.MV_HeadFont  ;
    MV_BodyFont      = that.MV_BodyFont  ;
    MV_JumpSize      = that.MV_JumpSize  ;
#endif
    MV_ViewMode      = that.MV_ViewMode  ;

    strcpy(MailCmd, that.MailCmd);
  } // if
  return *this;
} // AxPluginSettings::operator=()

//-------------------------------------------------------------------------
//     AxPluginSettings::clear()
//-------------------------------------------------------------------------
void AxPluginSettings::clear()
{
  BGCheckDelay     = 0;
  DebugCClient     = 0;

#if ALEX_NOPORT
  MBV_Font         = 0;

  MLV_Font         = 0;
#endif
  MLV_SortOrder    = 0;
  MLV_MaxMails     = 100;

#if ALEX_NOPORT
  MV_HeadFont      = 0;
  MV_BodyFont      = 0;
  MV_JumpSize      = 1;
#endif
#if ALEX_NOPORT
  MV_ViewMode      = AxOsdMailItem::MVM_FLOWED;
#else
  MV_ViewMode      = 2;
#endif

  memset(MailCmd, 0, sizeof(MailCmd));
} // AxPluginSettings::clear()

//=============================================================================
//     class AxPluginSetup
//=============================================================================

//-------------------------------------------------------------------------
//     AxPluginSetup::AxPluginSetup()
//-------------------------------------------------------------------------
AxPluginSetup::AxPluginSetup(cPlugin *thePlugin)
             : myPlugin     (thePlugin)
{
} // AxPluginSetup::AxPluginSeSortOrdertup()

//-------------------------------------------------------------------------
//     AxPluginSetup::AxPluginSetup()
//-------------------------------------------------------------------------
AxPluginSetup::AxPluginSetup(const AxPluginSetup &that)
{
  copyFrom(that);
} // AxPluginSetup::AxPluginSetup()

//-------------------------------------------------------------------------
//     AxPluginSetup::operator=()
//-------------------------------------------------------------------------
AxPluginSetup &AxPluginSetup::operator=(const AxPluginSetup &that)
{
  if (this != &that)
  {
    copyFrom(that);
  } // if

  return *this;
} // AxPluginSetup::operator=()

//-------------------------------------------------------------------------
//     AxPluginSetup::~AxPluginSetup()
//-------------------------------------------------------------------------
AxPluginSetup::~AxPluginSetup()
{
  clear();
} // AxPluginSetup::~AxPluginSetup()

//-------------------------------------------------------------------------
//     AxPluginSetup::copyFrom()
//-------------------------------------------------------------------------
void AxPluginSetup::copyFrom(const AxPluginSetup &that)
{
  clear();

  PluginSettings = that.PluginSettings;
  myPlugin       = that.myPlugin;

  for (Ax::Mail::MailBoxSetupList::const_iterator anIter  = that.Accounts.begin();
                                                  anIter != that.Accounts.end();
                                                ++anIter)
  {
    Accounts.push_back(new Mail::MailBoxSetup(**anIter));
  } // for

} // AxPluginSetup::copyFrom()

//-------------------------------------------------------------------------
//     AxPluginSetup::clear()
//-------------------------------------------------------------------------
void AxPluginSetup::clear()
{
  PluginSettings.clear();
  clearAccounts();
} // AxPluginSetup::clear()

//-------------------------------------------------------------------------
//     AxPluginSetup::clearAccounts()
//-------------------------------------------------------------------------
void AxPluginSetup::clearAccounts()
{
  for (Ax::Mail::MailBoxSetupList::iterator anIter  = Accounts.begin();
                                            anIter != Accounts.end();
                                          ++anIter)
  {
    delete *anIter;
  } // for
  Accounts.clear();
} // AxPluginSetup::clearAccounts()

//-------------------------------------------------------------------------
//     AxPluginSetup::loadAccounts()
//-------------------------------------------------------------------------
bool AxPluginSetup::loadAccounts()
{
  std::string aFileName = cPlugin::ConfigDirectory(myPlugin->Name())
                        + std::string("/")
                        + AccountsFileName
                        ;

  wsdebug(("> AxPluginSetup::loadAccounts() FileName: '%s'", aFileName.c_str()));
  clearAccounts();

  FILE *aFile = fopen(aFileName.c_str(), "r");

  if (aFile == 0)
  {
    wserror(("AxPluginSetup::loadAccounts() unable to open file '%s'", aFileName.c_str()));
    return false;
  } // if

  Mail::MailBoxSetup *aSetup = 0;

  while (!feof(aFile))
  {
    char szLine[256];

    if (fgets(szLine, sizeof(szLine), aFile) == 0)
    {
      break;
    } // if

    int aLineLen = strlen(szLine);
    if (aLineLen == 0)
    {
      continue;
    } // if

    if (szLine[aLineLen - 1] == '\n')
    {
      szLine[aLineLen - 1] = '\0';
      aLineLen--;
    } // if

    if (strcmp(szLine, "[Account]") == 0)
    {
      if (aSetup)
      {
        Accounts.push_back(aSetup);
      } // if
      aSetup = new Mail::MailBoxSetup();

      continue;
    } // if

    char *aKey = szLine;
    char *aVal = strchr(szLine, '=');
    if (aVal == 0 || strlen(aVal) < 3)
    {
      // no value -> continue
      continue;
    } // if
    *aVal = '\0';
    aVal++; // skip \0

    aKey = compactspace(aKey);
    aVal = compactspace(aVal);

         if (strcmp(aKey, "AccountName"    ) == 0) strncpy(aSetup->AccountName     , aVal, sizeof(aSetup->AccountName     ) - 1);
    else if (strcmp(aKey, "LoginName"      ) == 0) strncpy(aSetup->LoginName       , aVal, sizeof(aSetup->LoginName       ) - 1);
    else if (strcmp(aKey, "LoginPass"      ) == 0) strncpy(aSetup->LoginPass       , aVal, sizeof(aSetup->LoginPass       ) - 1);
    else if (strcmp(aKey, "AccessMode"     ) == 0)         aSetup->AccessMode      = atoi(aVal);
    else if (strcmp(aKey, "AccessCode"     ) == 0) strncpy(aSetup->AccessCode      , aVal, sizeof(aSetup->AccessCode      ) - 1);
    else if (strcmp(aKey, "MailBox"        ) == 0) strncpy(aSetup->MailBox         , aVal, sizeof(aSetup->MailBox         ) - 1);
    else if (strcmp(aKey, "AutoMarkSeen"   ) == 0)         aSetup->fAutoMarkSeen   = atoi(aVal);
    else if (strcmp(aKey, "Expunge"        ) == 0)         aSetup->fExpungeOnClose = atoi(aVal);
    else if (strcmp(aKey, "BackgroundCheck") == 0)         aSetup->BGCheckMode     = atoi(aVal);
  } // while

  if (aSetup)
  {
    Accounts.push_back(aSetup);
  } // if

  fclose(aFile);

  wsdebug(("< AxPluginSetup::loadAccounts() FileName: '%s'", aFileName.c_str()));
  return true;
} // AxPluginSetup::loadAccounts()

//-------------------------------------------------------------------------
//     AxPluginSetup::saveAccounts()
//-------------------------------------------------------------------------
bool AxPluginSetup::saveAccounts()
{
  std::string aFileName = cPlugin::ConfigDirectory(myPlugin->Name())
                        + std::string("/")
                        + AccountsFileName
                        ;

  wsdebug(("> AxPluginSetup::saveAccounts() FileName: '%s'", aFileName.c_str()));

  // following code is really hacked together

  // save current umask
  mode_t anOldUMASK = umask(0x477);

  FILE *aFile = fopen(aFileName.c_str(), "w");

  // restore previouse umask
  umask(anOldUMASK);

  if (aFile == 0)
  {
    wserror(("AxPluginSetup::saveAccounts() unable to open file '%s'", aFileName.c_str()));
    return false;
  } // if

  bool fOK = true;

  for (Ax::Mail::MailBoxSetupList::const_iterator anIter  = Accounts.begin();
                                                  anIter != Accounts.end() && fOK;
                                                ++anIter)
  {
    Mail::MailBoxSetup *aSetup = *anIter;
    fOK &= fprintf(aFile, "\n[Account]\n"                                    ) > 0;
    fOK &= fprintf(aFile, "  AccountName     = %s\n", aSetup->AccountName    ) > 0;
    fOK &= fprintf(aFile, "  LoginName       = %s\n", aSetup->LoginName      ) > 0;
    fOK &= fprintf(aFile, "  LoginPass       = %s\n", aSetup->LoginPass      ) > 0;
    fOK &= fprintf(aFile, "  AccessMode      = %d\n", aSetup->AccessMode     ) > 0;
    fOK &= fprintf(aFile, "  AccessCode      = %s\n", aSetup->AccessCode     ) > 0;
    fOK &= fprintf(aFile, "  MailBox         = %s\n", aSetup->MailBox        ) > 0;
    fOK &= fprintf(aFile, "  AutoMarkSeen    = %d\n", aSetup->fAutoMarkSeen  ) > 0;
    fOK &= fprintf(aFile, "  Expunge         = %d\n", aSetup->fExpungeOnClose) > 0;
    fOK &= fprintf(aFile, "  BackgroundCheck = %d\n", aSetup->BGCheckMode    ) > 0;
  } // for

  if (!fOK)
  {
    wserror(("AxPluginSetup::saveAccounts() error while writing config file '%s'", aFileName.c_str()));
  } // if

  fclose(aFile);

  wsdebug(("< AxPluginSetup::saveAccounts() FileName: '%s'", aFileName.c_str()));

  return fOK;
} // AxPluginSetup::saveAccounts()

