/* c.urllaunch */

/* Toolbox apps provide in the prog info dialogue a 'web' button
 * to launch a url to check for a later version. The toolbox handles
 * everything.
 *
 * These routines are for a similar function in non-toolbox
 * applications.
 *
 * They are based on example BASIC code released into the public domain
 * (original author unknown) and simplified
 */



#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>


#include "h.os"
#include "h.wimp"
#include "h.wos"
#include "h.err"
#include "h.poll"
#include "h.werr"
//#include "h.deb"

#include "h.urllaunch"

//#define XOS_Bit  0x020000

int urllaunch_my_ref = 0;
/* buffer for url text needs only 236 bytes as it must fit in
 * wimp message block of total length 256 bytes
*/
static char url_to_launch[256];


#define module "RMEnsure AcornURI 0.12"
#define run_cmd "*RMLoad System:Modules.Network.URI"

static os_error * urllaunch_check_for_uri_module(void)

{
  /* Check for an active AcornURI module, since the whole launch process
   * will fail if the module is not active. RMEnsure the module, try to
   * load if absent, and recheck it is now active.
  */

  os_error * err = NULL;
  char cmd[256];

  sprintf (cmd, "%s %s/n", module, run_cmd);
  err = os_cli (cmd);
  sprintf (cmd, "%s/n", module);
  err = os_cli (cmd);
  if (err)
  {
    messagebox ("The URI module is not active and could not be loaded - ensure a browser is running and try again");
  }
  return(err);
}


static os_error * urllaunch_ack(wimp_msgstr * msg)
{
  return(urllaunch_launch(msg));
}


os_error * urllaunch_broadcast(char *url)
{
  /* The app only calls this function directly
   * Everything else is called as a result of wimp messages
   * passed during wimp poll
   *
   * ANT URL broadcast
   * url is url to broadcast
   * This only broadcasts the direct form of the message
   * The indirect form isn't really supported and is prone
   * to memory leaks
  */

  os_error * err = NULL;
  int len;
  wimp_msgstr msg;

  len = strlen(url);
  /* check length of url - to fit in message block
   * max length = 235 bytes + null terminator
  */
  if (len < 236)
  {
    /* take a copy of the url to be launched */
    strcpy (url_to_launch, url);
    /* message size is 20<=size<=256, rounded up to a multiple of 4 */
    msg.hdr.size = (20 + len + 4) & 0xfffffffc;

    msg.hdr.your_ref = 0;
    msg.hdr.action = wimp_MINETSUITE_OPENURL;
    strncpy(msg.data.chars, url, len);
    msg.data.chars[len] = '\0';

    err = wimp_sendmessage(wimp_ESENDWANTACK, &msg, 0);
    /* on exit my_ref field in message block should have been filled in by the wimp */
    /* take a copy to compare with my ref field of returned message */
    urllaunch_my_ref = msg.hdr.my_ref ;
    if(!err) err = addack(urllaunch_ack, urllaunch_my_ref);
  }
  else
  {
    /* Emit warning and skip any attempt to launch the url */
    messagebox ("URL is too long to fit within a wimp message block");
  }
  return (err);
}





#define LOAD_HTTP_ALIAS "Alias$URLOpen_http"

static os_error * urllaunch_load(char *url)
{
  /* simplified version - we know the fixed url and protocol */
  /* ANT URL protocol - load stage */
  os_error * err;

  err=NULL;
  char buffer[512];

  /* check whether the load alias has been set */
  if (os_read_var_size(LOAD_HTTP_ALIAS)==0)
  {
    /* variable doesn't exist */
    messagebox ("System variable Alias$URLOpen_http has not been set."
                " Load a browser and try again");
  }
  else
  {
    /* variable does exist - issue a load command */
    strcpy(buffer,"URLOpen_https ");
    strcat(buffer, url);
    err = wimp_starttask(buffer);
  }

  return(err);
}




os_error * urllaunch_launch(wimp_msgstr *msg)
{
  os_error * err, *e;
  os_regset reg;
  char url[256];

  err=NULL;
  /* use the url string from the bounced message block */
  strcpy (url, msg->data.chars);
  reg.r[0] = 1;
  reg.r[1] = (int)msg->data.chars;
  reg.r[2] = taskhandle;
  e = os_swix(URI_Dispatch, &reg);
  /* if this errors, then AcornURI is probably not loaded */
  /* not handled - last try to use url_load before giving up */
  if (e) err = urllaunch_load(url);
  return(err);
}



os_error * urllaunch_bounce(wimp_msgstr *msg)
{
  os_error * err;

  err = NULL;

  /* simplified version */

  /* check if url launch message was handled or not
   *
   * message format is:-
   * msg+20: flags
   * msg+24: url id
  */

  if ((msg->data.words[0] & 1) != 0)
  {
    /* not handled - last try to use url_load before giving up */
    err = urllaunch_load(url_to_launch);
  }

  return(err);
}







