/****************************************************************************/
/* WebWorks Help API                                                        */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

/*$Id: wwhapi.c,v 1.14 2004/03/12 19:53:02 allums Exp $*/


#include "wwhelp/wwhapi.h"

#include "wwhelp/wwhcomm.h"
#include <stdlib.h>
#include <string.h>


/****************************************************************************/
/* Private datatypes, variables, and function prototypes                    */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

#ifdef __cplusplus
extern "C"
{
#endif

char* sgpWWHelp_Base = NULL;

static char*               sgpWWHelp_BaseURL = NULL;
static WWHelp_BrowserInfo  sgWWHelp_DefaultBrowserInfo;

typedef enum WWHelpTabs
{
  enumWWHelpTab_Default = 0,
  enumWWHelpTab_Contents,
  enumWWHelpTab_Index,
  enumWWHelpTab_Search,
  enumWWHelpTab_Favorites
} WWHelpTabsEnum;

int  private_WWHelp_DisplayHelp(const char*           pParamContext,
                                const char*           pParamTopic,
                                const int             bParamSingle,
                                const WWHelpTabsEnum  ParamInitialTab);

#ifdef __cplusplus
}
#endif


/****************************************************************************/
/* private_WWHelp_DisplayHelp                                               */
/*                                                                          */
/*  Displays the supplied context and topic in the default browser with     */
/*  single topic display set as the parameter specifies.                    */
/*                                                                          */
/****************************************************************************/

int
private_WWHelp_DisplayHelp(const char*           pParamContext,
                           const char*           pParamTopic,
                           const int             bParamSingle,
                           const WWHelpTabsEnum  ParamInitialTab)
{
  int     ReturnCode = -1;
  size_t  ContextLength;
  size_t  TopicLength;
  char*   pOptionSeparator;
  char*   pTabOption;
  int     Error;
  char*   pURL;


  /* Determine the maximum possible length of the URL with parameters */
  /*                                                                  */
  ContextLength = 0;
  TopicLength = 0;
  if ((pParamContext != NULL) &&
      (pParamTopic   != NULL) &&
      (strlen(pParamContext) > 0) &&
      (strlen(pParamTopic) > 0))
  {
    ContextLength = strlen(pParamContext);
    TopicLength   = strlen(pParamTopic);
  }

  /* Allocate space for the URL */
  /*                            */
  pURL = (char*)malloc(strlen(sgpWWHelp_BaseURL) + ContextLength
                                                 + TopicLength
                                                 + 54);
  if (pURL != NULL)
  {
    pOptionSeparator = "?";
    strcpy(pURL, sgpWWHelp_BaseURL);
    if ((ContextLength > 0) &&
        (TopicLength > 0))
    {
      strcat(pURL, pOptionSeparator);
      pOptionSeparator = "&";
      strcat(pURL, "context=");
      strcat(pURL, pParamContext);
      strcat(pURL, pOptionSeparator);
      pOptionSeparator = "&";
      strcat(pURL, "topic=");
      strcat(pURL, pParamTopic);
    }

    /* Add single option */
    /*                   */
    if (bParamSingle)
    {
      strcat(pURL, pOptionSeparator);
      pOptionSeparator = "&";
      strcat(pURL, "single=true");
    }

    /* Add initial tab option */
    /*                        */
    pTabOption = NULL;
    switch (ParamInitialTab)
    {
      case enumWWHelpTab_Contents:
        pTabOption = "contents";
        break;

      case enumWWHelpTab_Index:
        pTabOption = "index";
        break;

      case enumWWHelpTab_Search:
        pTabOption = "search";
        break;

      case enumWWHelpTab_Favorites:
        pTabOption = "favorites";
        break;
    }
    if (pTabOption != NULL)
    {
      strcat(pURL, pOptionSeparator);
      pOptionSeparator = "&";
      strcat(pURL, "tab=");
      strcat(pURL, pTabOption);
    }

    /* Send URL to the default browser */
    /*                                 */
    Error = WWHelp_Comm_SetDefaultBrowserInfo(&sgWWHelp_DefaultBrowserInfo);
    if (Error == 0)
    {
      Error = WWHelp_Comm_DisplayHelpURL(&sgWWHelp_DefaultBrowserInfo,
                                         pURL, strlen(pURL));
      if (Error == 0)
      {
        /* Success! */
        /*          */
        ReturnCode = 0;
      }
    }

    free(pURL);
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Startup                                                           */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Startup(const char* pParamBaseURL)
{
  int   ReturnCode = -1;
  char* pAPIFile = "/wwhelp/wwhimpl/api.htm";
  int   Error;


  if ((sgpWWHelp_Base == NULL) &&
      (sgpWWHelp_BaseURL == NULL) &&
      (pParamBaseURL != NULL) &&
      (strlen(pParamBaseURL) > 0))
  {
    sgpWWHelp_Base = (char*)malloc(strlen(pParamBaseURL) + 1);
    if (sgpWWHelp_Base != NULL)
    {
      strcpy(sgpWWHelp_Base, pParamBaseURL);

      sgpWWHelp_BaseURL = (char*)malloc(strlen(pParamBaseURL) + strlen(pAPIFile) + 1);
      if (sgpWWHelp_BaseURL != NULL)
      {
        strcpy(sgpWWHelp_BaseURL, pParamBaseURL);
        strcat(sgpWWHelp_BaseURL, pAPIFile);

        Error = WWHelp_Comm_Startup();
        if (Error == 0)
        {
          Error = WWHelp_Comm_NewBrowserInfo(&sgWWHelp_DefaultBrowserInfo);
          if (Error == 0)
          {
            /* Success! */
            /*          */
            ReturnCode = 0;
          }

          if (ReturnCode != 0)
          {
            WWHelp_Comm_Shutdown();
          }
        }

        if (ReturnCode != 0)
        {
          free(sgpWWHelp_BaseURL);
          sgpWWHelp_BaseURL = NULL;
        }
      }

      if (ReturnCode != 0)
      {
        free(sgpWWHelp_Base);
        sgpWWHelp_Base = NULL;
      }
    }
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Shutdown                                                          */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Shutdown()
{
  int  ReturnCode = -1;
  int  Error;
  int  TotalErrors = 0;


  if ((sgpWWHelp_Base != NULL) &&
      (sgpWWHelp_BaseURL != NULL))
  {
    free(sgpWWHelp_Base);
    sgpWWHelp_Base = NULL;

    free(sgpWWHelp_BaseURL);
    sgpWWHelp_BaseURL = NULL;

    Error = WWHelp_Comm_DeleteBrowserInfo(&sgWWHelp_DefaultBrowserInfo);
    if (Error != 0)
    {
      TotalErrors += 1;
    }

    Error = WWHelp_Comm_Shutdown();
    if (Error != 0)
    {
      TotalErrors += 1;
    }

    if (TotalErrors == 0)
    {
      /* Success! */
      /*          */
      ReturnCode = 0;
    }
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_DisplayHelp                                                       */
/*                                                                          */
/*  Displays the supplied context and topic in the default browser in       */
/*  single topic mode.                                                      */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelp(const char* pParamContext,
                   const char* pParamTopic)
{
  int  ReturnCode = -1;


  if ((pParamContext != NULL) &&
      (pParamTopic   != NULL) &&
      (strlen(pParamContext) > 0) &&
      (strlen(pParamTopic) > 0))
  {
    ReturnCode = private_WWHelp_DisplayHelp(pParamContext, pParamTopic, 1,
                                            enumWWHelpTab_Default);
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_DisplayHelpWithNavigation                                         */
/*                                                                          */
/*  Displays the supplied context and topic in the default browser with     */
/*  navigation aides.                                                       */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpWithNavigation(const char* pParamContext,
                                 const char* pParamTopic)
{
  int  ReturnCode = -1;


  if ((pParamContext != NULL) &&
      (pParamTopic   != NULL) &&
      (strlen(pParamContext) > 0) &&
      (strlen(pParamTopic) > 0))
  {
    ReturnCode = private_WWHelp_DisplayHelp(pParamContext, pParamTopic, 0,
                                            enumWWHelpTab_Default);
  }

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpWithContents                                           */
/*                                                                          */
/*  Displays the supplied context and topic in the default browser with     */
/*  the Contents tab initially displayed.                                   */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpWithContents(const char* pParamContext,
                               const char* pParamTopic)
{
  int  ReturnCode = -1;


  if ((pParamContext != NULL) &&
      (pParamTopic   != NULL) &&
      (strlen(pParamContext) > 0) &&
      (strlen(pParamTopic) > 0))
  {
    ReturnCode = private_WWHelp_DisplayHelp(pParamContext, pParamTopic, 0,
                                            enumWWHelpTab_Contents);
  }

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpWithIndex                                              */
/*                                                                          */
/*  Displays the supplied context and topic in the default browser with     */
/*  the Index tab initially displayed.                                      */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpWithIndex(const char* pParamContext,
                            const char* pParamTopic)
{
  int  ReturnCode = -1;


  if ((pParamContext != NULL) &&
      (pParamTopic   != NULL) &&
      (strlen(pParamContext) > 0) &&
      (strlen(pParamTopic) > 0))
  {
    ReturnCode = private_WWHelp_DisplayHelp(pParamContext, pParamTopic, 0,
                                            enumWWHelpTab_Index);
  }

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpWithSearch                                             */
/*                                                                          */
/*  Displays the supplied context and topic in the default browser with     */
/*  the Search tab initially displayed.                                     */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpWithSearch(const char* pParamContext,
                             const char* pParamTopic)
{
  int  ReturnCode = -1;


  if ((pParamContext != NULL) &&
      (pParamTopic   != NULL) &&
      (strlen(pParamContext) > 0) &&
      (strlen(pParamTopic) > 0))
  {
    ReturnCode = private_WWHelp_DisplayHelp(pParamContext, pParamTopic, 0,
                                            enumWWHelpTab_Search);
  }

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpWithFavorites                                          */
/*                                                                          */
/*  Displays the supplied context and topic in the default browser with     */
/*  the Favorites tab initially displayed.                                  */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpWithFavorites(const char* pParamContext,
                                const char* pParamTopic)
{
  int  ReturnCode = -1;


  if ((pParamContext != NULL) &&
      (pParamTopic   != NULL) &&
      (strlen(pParamContext) > 0) &&
      (strlen(pParamTopic) > 0))
  {
    ReturnCode = private_WWHelp_DisplayHelp(pParamContext, pParamTopic, 0,
                                            enumWWHelpTab_Favorites);
  }

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpNavigation                                             */
/*                                                                          */
/*  Displays the help set with navigation aides in the default browser.     */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpNavigation()
{
  int   ReturnCode = -1;
  char* pContext  = NULL;
  char* pTopic    = NULL;


  ReturnCode = private_WWHelp_DisplayHelp(pContext, pTopic, 0,
                                          enumWWHelpTab_Default);

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpContents                                               */
/*                                                                          */
/*  Displays the help set with the Contents tab initially displayed in the  */
/*  default browser.                                                        */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpContents()
{
  int   ReturnCode = -1;
  char* pContext  = NULL;
  char* pTopic    = NULL;


  ReturnCode = private_WWHelp_DisplayHelp(pContext, pTopic, 0,
                                          enumWWHelpTab_Contents);

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpIndex                                                  */
/*                                                                          */
/*  Displays the help set with the Index tab initially displayed in the     */
/*  default browser.                                                        */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpIndex()
{
  int   ReturnCode = -1;
  char* pContext  = NULL;
  char* pTopic    = NULL;


  ReturnCode = private_WWHelp_DisplayHelp(pContext, pTopic, 0,
                                          enumWWHelpTab_Index);

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpSearch                                                 */
/*                                                                          */
/*  Displays the help set with the Search tab initially displayed in the    */
/*  default browser.                                                        */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpSearch()
{
  int   ReturnCode = -1;
  char* pContext  = NULL;
  char* pTopic    = NULL;


  ReturnCode = private_WWHelp_DisplayHelp(pContext, pTopic, 0,
                                          enumWWHelpTab_Search);

  return ReturnCode;
}

/****************************************************************************/
/* WWHelp_DisplayHelpFavorites                                              */
/*                                                                          */
/*  Displays the help set with the Favorites tab initially displayed in the */
/*  default browser.                                                        */
/*                                                                          */
/****************************************************************************/

int
WWHelp_DisplayHelpFavorites()
{
  int   ReturnCode = -1;
  char* pContext  = NULL;
  char* pTopic    = NULL;


  ReturnCode = private_WWHelp_DisplayHelp(pContext, pTopic, 0,
                                          enumWWHelpTab_Favorites);

  return ReturnCode;
}
