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

/*$Id: wwhcomm.c,v 1.6 2004/03/11 21:11:18 allums Exp $*/


#include "wwhelp/wwhcomm.h"

#include "wwhelp/wwhurl.h"
#include "wwhelp/wwhplat.h"
#include <stdlib.h>
#include <string.h>


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

#ifdef __cplusplus
extern "C"
{
#endif

static int  sgWWHelp_APIInitialized = 0;

int  private_WWHelp_Comm_CopyURL(const char*   pParamURL,
                                 const size_t  ParamURLLength,
                                       char**  hParamCopyOfURL,
                                       size_t* pParamCopyOfURLLength);

int  private_WWHelp_Comm_SendURLOrLaunchWithURL(const WWHelp_BrowserInfo* pParamBrowserInfo,
                                                const int                 bParamUseSmartURL,
                                                const char*               pParamURL,
                                                const size_t              ParamURLLength);

#ifdef __cplusplus
}
#endif


/****************************************************************************/
/* private_WWHelp_Comm_CopyURL                                              */
/*                                                                          */
/****************************************************************************/

int
private_WWHelp_Comm_CopyURL(const char*   pParamURL,
                            const size_t  ParamURLLength,
                                  char**  hParamCopyOfURL,
                                  size_t* pParamCopyOfURLLength)
{
  int  ReturnCode = -1;


  if (ParamURLLength > 0)
  {
    *hParamCopyOfURL = (char*)malloc(ParamURLLength);
    if (*hParamCopyOfURL != NULL)
    {
      memcpy(*hParamCopyOfURL, pParamURL, ParamURLLength);
      *pParamCopyOfURLLength = ParamURLLength;

      ReturnCode = 0;
    }
  }

  return ReturnCode;
}


/****************************************************************************/
/* private_WWHelp_Comm_SendURLOrLaunchWithURL                               */
/*                                                                          */
/*  Send given URL to the default browser.                                  */
/*                                                                          */
/****************************************************************************/

int
private_WWHelp_Comm_SendURLOrLaunchWithURL(const WWHelp_BrowserInfo* pParamBrowserInfo,
                                           const int                 bParamUseSmartURL,
                                           const char*               pParamURL,
                                           const size_t              ParamURLLength)
{
  int     ReturnCode = -1;
  int     Error;
  char*   pURLToSend;
  size_t  URLToSendLength;


  /* See if browser is already running */
  /*                                   */
  Error = WWHelp_Platform_CheckBrowserRunning(pParamBrowserInfo);
  if (Error == 0)
  {
    /* Create a smart URL if requested */
    /*                                 */
    Error = -1;
    if ((bParamUseSmartURL) &&
        (pParamBrowserInfo->mbAllowSmartURLs))
    {
      Error = WWHelp_URL_BuildSmartURL(pParamURL, ParamURLLength,
                                       &pURLToSend, &URLToSendLength);

      /* If a smart URL was not created, just use the given URL as is */
      /*                                                              */
      if (Error != 0)
      {
        Error = private_WWHelp_Comm_CopyURL(pParamURL, ParamURLLength,
                                            &pURLToSend, &URLToSendLength);
      }
    }
    else
    {
      /* Use the given URL as is */
      /*                         */
      Error = private_WWHelp_Comm_CopyURL(pParamURL, ParamURLLength,
                                          &pURLToSend, &URLToSendLength);
    }

    if (Error == 0)
    {
      /* Send URL to running browser */
      /*                             */
      Error = WWHelp_Platform_SendURL(pParamBrowserInfo,
                                      pURLToSend, URLToSendLength);
      if (Error == 0)
      {
        /* Success! */
        /*          */
        ReturnCode = 0;
      }

      /* Release formatted URL */
      /*                       */
      free(pURLToSend);
    }
  }

  /* See if URL already sent, otherwise launch a new browser */
  /*                                                         */
  if (ReturnCode != 0)
  {
    /* Browser is not running or communications failed so launch it */
    /* Send given URL without anything around it                    */
    /*                                                              */
    Error = WWHelp_Platform_LaunchWithURL(pParamBrowserInfo,
                                          pParamURL, ParamURLLength);
    if (Error == 0)
    {
      /* Success! */
      /*          */
      ReturnCode = 0;
    }
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Comm_Startup                                                      */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Comm_Startup()
{
  int  ReturnCode = -1;


  ReturnCode = WWHelp_Platform_Startup();
  if (ReturnCode == 0)
  {
    sgWWHelp_APIInitialized = 1;
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Comm_Shutdown                                                     */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Comm_Shutdown()
{
  int  ReturnCode = -1;


  ReturnCode = WWHelp_Platform_Shutdown();

  sgWWHelp_APIInitialized = 0;

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Comm_SetDefaultBrowserInfo                                        */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Comm_SetDefaultBrowserInfo(WWHelp_BrowserInfo* pParamBrowserInfo)
{
  int  ReturnCode = -1;


  if ((sgWWHelp_APIInitialized) &&
      (pParamBrowserInfo != NULL))
  {
    ReturnCode = WWHelp_Platform_SetDefaultBrowserInfo(pParamBrowserInfo);
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Comm_NewBrowserInfo                                               */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Comm_NewBrowserInfo(WWHelp_BrowserInfo* pParamBrowserInfo)
{
  int  ReturnCode = -1;


  if ((sgWWHelp_APIInitialized) &&
      (pParamBrowserInfo != NULL))
  {
    /* Reset members which aren't normally cleared */
    /*                                             */
#if defined(_WIN32)
    pParamBrowserInfo->mDDEWindowID = -1;
    pParamBrowserInfo->mpLocationID = NULL;
#elif TARGET_OS_MAC
#else
#endif

    /* Clear everything else */
    /*                       */
    ReturnCode = WWHelp_Comm_ClearBrowserInfo(pParamBrowserInfo);
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Comm_DeleteBrowserInfo                                            */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Comm_DeleteBrowserInfo(WWHelp_BrowserInfo* pParamBrowserInfo)
{
  int  ReturnCode = -1;


  if ((sgWWHelp_APIInitialized) &&
      (pParamBrowserInfo != NULL))
  {
    /* Cleanup any persistant members */
    /*                                */
#if defined(_WIN32)
    if (pParamBrowserInfo->mpLocationID != NULL)
    {
      free(pParamBrowserInfo->mpLocationID);
      pParamBrowserInfo->mpLocationID = NULL;
    }
#elif TARGET_OS_MAC
#else
#endif

    /* Clear everything as if it were new */
    /*                                    */
    ReturnCode = WWHelp_Comm_NewBrowserInfo(pParamBrowserInfo);
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Comm_ClearBrowserInfo                                             */
/*                                                                          */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Comm_ClearBrowserInfo(WWHelp_BrowserInfo* pParamBrowserInfo)
{
  int  ReturnCode = -1;


  if ((sgWWHelp_APIInitialized) &&
      (pParamBrowserInfo != NULL))
  {
    /* Clear WWHelp_BrowserInfo struct */
    /*                                 */
    pParamBrowserInfo->mbAllowSmartURLs      = 0;
    pParamBrowserInfo->mbNetscapeBrowser     = 0;
#if defined(_WIN32)
    pParamBrowserInfo->mbSupportsDDEWithFile = 0;
    pParamBrowserInfo->mbSendWithCOM         = 0;
    pParamBrowserInfo->mOpenCommand[0]       = '\0';
    pParamBrowserInfo->mDDEServiceName[0]    = '\0';

    /* Only reset window ID if it is 0.                    */
    /* If window ID is not 0, it might be a valid.         */
    /* If it isn't, retry logic will fall back gracefully. */
    /*                                               */
    if (pParamBrowserInfo->mDDEWindowID == 0)
    {
      pParamBrowserInfo->mDDEWindowID = -1;
    }

    /* Skip clearing mpLocationID        */
    /* It can be reused between sessions */
    /*                                   */
    /* pParamBrowserInfo->mpLocationID   */
#elif TARGET_OS_MAC
    pParamBrowserInfo->mCreatorCode = '????';
#else
    pParamBrowserInfo->mProgram[0]  = '\0';
    pParamBrowserInfo->mbNeverFails = 0;
#endif

    /* Success! */
    /*          */
    ReturnCode = 0;
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Comm_DisplayURL                                                   */
/*                                                                          */
/*  Send given URL to the default browser.                                  */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Comm_DisplayURL(const WWHelp_BrowserInfo* pParamBrowserInfo,
                       const char*               pParamURL,
                       const size_t              ParamURLLength)
{
  int  ReturnCode = -1;


  if ((sgWWHelp_APIInitialized) &&
      (pParamBrowserInfo != NULL) &&
      (pParamURL != NULL) &&
      (ParamURLLength > 0))
  {
    ReturnCode = private_WWHelp_Comm_SendURLOrLaunchWithURL(pParamBrowserInfo, 0,
                                                            pParamURL, ParamURLLength);
  }

  return ReturnCode;
}


/****************************************************************************/
/* WWHelp_Comm_DisplayHelpURL                                               */
/*                                                                          */
/*  Send given URL wrapped with JavaScript to reuse an existing help        */
/*  frameset to the default browser.                                        */
/*                                                                          */
/****************************************************************************/

int
WWHelp_Comm_DisplayHelpURL(const WWHelp_BrowserInfo* pParamBrowserInfo,
                           const char*               pParamURL,
                           const size_t              ParamURLLength)
{
  int  ReturnCode = -1;


  if ((sgWWHelp_APIInitialized) &&
      (pParamBrowserInfo != NULL) &&
      (pParamURL != NULL) &&
      (ParamURLLength > 0))
  {
    ReturnCode = private_WWHelp_Comm_SendURLOrLaunchWithURL(pParamBrowserInfo, 1,
                                                            pParamURL, ParamURLLength);
  }

  return ReturnCode;
}
