/************************************************************
**
** Application: TranJPEG
**
** Title:       c.urllaunch
**
*************************************************************/

/*
*
* Copyright (c) 2015, Chris Johnson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the copyright holder nor the names of their
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

/* 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 to work with fixed upgrade url for Snapper
 */



/* Include files */
/* from standard clib */
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>


#include "TaskLib:h.os"
#include "TaskLib:h.wimp"
#include "TaskLib:h.wos"
#include "TaskLib:h.err"
#include "TaskLib:h.poll"
#include "TaskLib:h.werr"


#include "h.main"
#include "h.reslink"
#include "h.urllaunch"

#define XOS_Bit  0x020000

int my_reference=0;



static os_error * urlack(wimp_msgstr * msg)
{
  return(uri_launch(msg));
}


os_error * url_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;


  /* check length of url - to fit in message block */
  len = strlen(url);
  /* Truncating the url may not be very sensible */
  /* However in this usage, it should never happen as the url is predefined */
  if (len>235)
  {
    /* message size is 20<=size<=256, rounded up to a multiple of 4 */
    msg.hdr.size = (20 + len + 3) & 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 */
    my_reference = msg.hdr.my_ref ;
    if(!err) err = addack(urlack, my_reference);
  }
  else
  {
    main_issuewarning (WURLTOOLONG);
  }
  return (err);
}


#define LOAD_HTTP_ALIAS "Alias$URLOpen_http"

static os_error * url_load(void)
{
  /* 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 */
    main_issuewarning (WHTTPNOALIAS);
  }
  else
  {
    /* variable does exist - issue a load command */
    strcpy(buffer,LOAD_HTTP_ALIAS);
    strcat(buffer," ");
    strcat(buffer, WEB_URL);
    err = wimp_starttask(buffer);
  }

  return(err);
}




os_error * uri_launch(wimp_msgstr *msg)
{
  os_error * err, *e;
  os_regset reg;

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



os_error * uri_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 = url_load();
  }

  return(err);
}







