//-----------------------------------------------------------------------------
/*
 * Description: This file contains the class AxPluginMaiBox
 *
 * 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 15:30:07 $
 *   $Revision: 1.38 $
 */
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//     includes
//-----------------------------------------------------------------------------

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

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

//----- vdr -------------------------------------------------------------------
#include <vdr/plugin.h>
#include <vdr/interface.h>

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

//----- local -----------------------------------------------------------------
#include "i18n.h"
#include "AxPluginMailBox.h"
#include "AxMenuConfigPlugin.h"
#include "AxMenuMailBoxView.h"
#include "AxPluginSetup.h"
#include "AxVdrTools.h"

//----- local -----------------------------------------------------------------
//----- c-client --------------------------------------------------------------

//=============================================================================
//     VDRPLUGINCREATOR
//=============================================================================
/// Macro for plugin entry point
VDRPLUGINCREATOR(AxPluginMailBox);

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

//=============================================================================
//     defines
//=============================================================================
static const char *VERSION        = "0.3.0";
static const char *DESCRIPTION    = "IMAP/POP3 mail-reader plugin";

//=============================================================================
//     globals
//=============================================================================

//=============================================================================
//     class PluginMail
//=============================================================================

//-----------------------------------------------------------------------------
//     PluginMail::AxPluginMailBox()
//-----------------------------------------------------------------------------
AxPluginMailBox::AxPluginMailBox()
               : mySetup      (this)
               , myMailChecker(&mySetup.PluginSettings)
{
  wsdebug(("> AxPluginMailBox::AxPluginMailBox()"))

  // Initialize any member varaiables here.
  // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!

  wsdebug(("< AxPluginMailBox::AxPluginMailBox()"))
} // AxPluginMailBox::AxPluginMailBox()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::~AxPluginMailBox()
//-----------------------------------------------------------------------------
AxPluginMailBox::~AxPluginMailBox()
{
  wsdebug(("> AxPluginMailBox::~AxPluginMailBox()"));

  wsdebug(("  AxPluginMailBox::~AxPluginMailBox() terminating mail-checker-thread..."));
  myMailChecker.stopChecking();

  wsdebug(("  AxPluginMailBox::~AxPluginMailBox() terminating MailBoxMgr..."));
  Mail::MailBoxMgr::terminate();

  wsdebug(("< AxPluginMailBox::~AxPluginMailBox()"));
} // AxPluginMailBox::~AxPluginMailBox()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::MainMenuEntry()
//-----------------------------------------------------------------------------
const char *AxPluginMailBox::MainMenuEntry(void)
{
  return tr("Mailbox");
} // AxPluginMailBox::MainMenuEntry()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::Version()
//-----------------------------------------------------------------------------
const char *AxPluginMailBox::Version(void)
{
  return VERSION;
} // AxPluginMailBox::Version()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::Description()
//-----------------------------------------------------------------------------
const char *AxPluginMailBox::Description(void)
{
  return DESCRIPTION;
} // AxPluginMailBox::Description()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::CommandLineHelp()
//-----------------------------------------------------------------------------
const char *AxPluginMailBox::CommandLineHelp(void)
{
  wsdebug(("  AxPluginMailBox::CommandLineHelp()"));

  return "  -m CMD,   --mailcmd=CMD   CMD is called when the number of new mails changed\n"
         ;
} // AxPluginMailBox::CommandLineHelp()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::ProcessArgs()
//-----------------------------------------------------------------------------
bool AxPluginMailBox::ProcessArgs(int argc, char *argv[])
{
  static struct option long_options[] =
  {
    { "mailcmd",      required_argument, NULL, 'm' },
    { NULL }
  };

  int c;
  while ((c = getopt_long(argc, argv, "m:", long_options, NULL)) != -1)
  {
    switch (c)
    {
      case 'm':
      {
        strn0cpy(mySetup.PluginSettings.MailCmd, optarg, sizeof(mySetup.PluginSettings.MailCmd));
        break;
      }
      default:
      {
        return false;
      }
    } // switch
  } // while

  wsdebug(("< AxPluginMailBox::ProcessArgs() MailCmd: '%s'", mySetup.PluginSettings.MailCmd));
  return true;
} // AxPluginMailBox::ProcessArgs()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::Start()
//-----------------------------------------------------------------------------
bool AxPluginMailBox::Start(void)
{
  wsdebug(("> AxPluginMailBox::Start()"));
  // Start any background activities the plugin shall perform.

  // register i18n phrases
  RegisterI18n(Phrases);

  // load the account definitions
  if (!mySetup.loadAccounts())
  {
    // error message already in loadAccounts()
  } // if

  createMailBoxes();

  if (!myMailChecker.startChecking())
  {
    // error message already in startChecking()
  } // if

  wsdebug(("< AxPluginMailBox::Start()"));
  return true;
} // AxPluginMailBox::Start()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::Housekeeping()
//-----------------------------------------------------------------------------
void AxPluginMailBox::Housekeeping(void)
{
//  wsdebug(("> AxPluginMailBox::Housekeeping()"));
//  wsdebug(("< AxPluginMailBox::Housekeeping()"));
} // AxPluginMailBox::Housekeeping()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::MainMenuAction()
//-----------------------------------------------------------------------------
cOsdMenu *AxPluginMailBox::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  wsdebug(("> AxPluginMailBox::MainMenueAction()"));

  cOsdMenu *anOsdMenu = 0;

  if (mySetup.Accounts.size() == 0)
  {
    wswarn(("AxPluginMailBox::MainMenuAction() No mail-accounts defined!"));
    Skins.Message(mtError, tr("No mail accounts defined!"));
  }
  else
  {
    myMailChecker.stopChecking();

    // initialize (translate) some strings in AxVdrTools
    AxVdrTools::initialize();

    anOsdMenu = new AxMenuMailBoxView(this);
  } // if

  wsdebug(("< AxPluginMailBox::MainMenueAction()"));

  return anOsdMenu;
} // AxPluginMailBox::MainMenuAction()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::SetupMenu()
//-----------------------------------------------------------------------------
cMenuSetupPage *AxPluginMailBox::SetupMenu(void)
{
  myMailChecker.stopChecking();

  // initialize (translate) some strings in AxVdrTools
  AxVdrTools::initialize();

  AxMenuConfigPlugin *aSetupPage = new AxMenuConfigPlugin(this, &mySetup);
  return aSetupPage;
} // AxPluginMailBox::SetupMenu()

//-------------------------------------------------------------------------
//     AxPluginMailBox::MainMenuActionFinished()
//-------------------------------------------------------------------------
void AxPluginMailBox::MainMenuActionFinished()
{
  wsdebug(("> AxPluginMailBox::MainMenuActionFinished()"));

  if (!myMailChecker.startChecking())
  {
    // error message already in startChecking()
  } // if

  wsdebug(("< AxPluginMailBox::MainMenuActionFinished()"));
} // AxPluginMailBox::MainMenuActionFinished(void)

//-------------------------------------------------------------------------
//     AxPluginMailBox::SetupMenuActionFinished()
//-------------------------------------------------------------------------
void AxPluginMailBox::SetupMenuActionFinished()
{
  wsdebug(("> AxPluginMailBox::SetupMenuActionFinished()"));

  createMailBoxes();

  if (!myMailChecker.startChecking())
  {
    // error message already in startChecking()
  } // if

  wsdebug(("< AxPluginMailBox::SetupMenuActionFinished()"));
} // AxPluginMailBox::SetupMenuActionFinished()

//-------------------------------------------------------------------------
//     AxPluginMailBox::createMailBoxes()
//-------------------------------------------------------------------------
void AxPluginMailBox::createMailBoxes()
{
  wsdebug(("> AxPluginMailBox::createMailBoxes()"));

  wsdebug(("  AxPluginMailBox::createMailBoxes() destroying mailboxes..."));
  Mail::MailBoxMgr::instance()->destroyAll();

  wsdebug(("  AxPluginMailBox::createMailBoxes() creating mailboxes..."));
  for (Mail::MailBoxSetupList::iterator anIter  = mySetup.Accounts.begin();
                                        anIter != mySetup.Accounts.end();
                                      ++anIter)
  {
    //----- create MailBox-instance -----
    Mail::MailBoxSetup *aMailBoxSetup = *anIter;
    wsdebug(("  AxPluginMailBox::createMailBoxes() creating MailBox: '%s'"
           , aMailBoxSetup->AccountName
           ));

    //----- the instances will register themselves at the MailBoxMgr
    new Mail::MailBox(aMailBoxSetup);
  } // for

  wsdebug(("< AxPluginMailBox::createMailBoxes()"));
} // AxPluginMailBox::createMailBoxes()

//-----------------------------------------------------------------------------
//     AxPluginMailBox::SetupParse()
//-----------------------------------------------------------------------------
bool AxPluginMailBox::SetupParse(const char *theName, const char *theValue)
{
  // Parse your own setup parameters and store their values.
  wsdebug(("> AxPluginMailBox::SetupParse() Name: '%s'  Value: '%s'"
         , theName
         , theValue ? theValue : "(NULL)"
         ));

  bool fConsumed = true;

  // the settings are stored in AxMenuConfigPlugin::Store()

  // Parse your own setup parameters and store their values.
  if      (!strcasecmp(theName, "BGCheckDelay" )) mySetup.PluginSettings.BGCheckDelay     = atoi(theValue);
  else if (!strcasecmp(theName, "DebugCClient" )) mySetup.PluginSettings.DebugCClient     = atoi(theValue);
#if ALEX_NOPORT
  else if (!strcasecmp(theName, "MBV_Font"     )) mySetup.PluginSettings.MBV_Font         = atoi(theValue);
  else if (!strcasecmp(theName, "MLV_Font"     )) mySetup.PluginSettings.MLV_Font         = atoi(theValue);
#endif
  else if (!strcasecmp(theName, "MLV_SortOrder")) mySetup.PluginSettings.MLV_SortOrder    = atoi(theValue);
  else if (!strcasecmp(theName, "MLV_MaxMails" )) mySetup.PluginSettings.MLV_MaxMails     = atoi(theValue);
#if ALEX_NOPORT
  else if (!strcasecmp(theName, "MV_HeadFont"  )) mySetup.PluginSettings.MV_HeadFont      = atoi(theValue);
  else if (!strcasecmp(theName, "MV_BodyFont"  )) mySetup.PluginSettings.MV_BodyFont      = atoi(theValue);
  else if (!strcasecmp(theName, "MV_JumpSize"  )) mySetup.PluginSettings.MV_JumpSize      = atoi(theValue);
#endif
  else if (!strcasecmp(theName, "MV_ViewMode"  )) mySetup.PluginSettings.MV_ViewMode      = atoi(theValue);
  else return fConsumed = false;

  wsdebug(("< AxPluginMailBox::SetupParse() Name: '%s'  Value: '%s'"
         , theName
         , theValue ? theValue : "(NULL)"
         ));

  return fConsumed;
} // AxPluginMailBox::SetupParse()

