REM >URL
REM
REM Copyright 2001-2014, Stephen Fryatt (info@stevefryatt.org.uk)
REM
REM This file is part of WimpLib:
REM
REM   http://www.stevefryatt.org.uk/software/
REM
REM Licensed under the EUPL, Version 1.2 only (the "Licence");
REM You may not use this work except in compliance with the
REM Licence.
REM
REM You may obtain a copy of the Licence at:
REM
REM   http://joinup.ec.europa.eu/software/page/eupl
REM
REM Unless required by applicable law or agreed to in
REM writing, software distributed under the Licence is
REM distributed on an "AS IS" basis, WITHOUT WARRANTIES
REM OR CONDITIONS OF ANY KIND, either express or implied.
REM
REM See the Licence for the specific language governing
REM permissions and limitations under the Licence.
:
REM Launch urls from Wimp applications.
REM
REM The following steps are taken in a url broadcast:
REM  1) Try ANT broadcast
REM  2) If that bounces, try Acorn URI dispatch [broadcast + load]
REM  3) If that succeeds, wait for success or failure wimp message
REM  4) If either fails, try ANT URL_Open
REM  5) If that fails, give error message
REM
REM This library requires the WimpError library to be present, along with a
REM function FNmessage_lookup() and a message token "URLFailed".
REM
REM The variable a% is reqired to point to 256 bytes of memory.


REM Attempt to launch a URL in whatever client is available.
REM
REM \param url$			The URL to launch.
:
DEF PROCurl_launch(url$)
IF FNurl_ant_broadcast(url$) THEN PROCwimperror_report(FNmessage_lookup("URLFailed"), 1, %01, "")
ENDPROC


REM Bounced message handler, to process bounces of Message_URIReturnResult
REM (&4E383) and Message_ANTOpenURL (&4AF80).
REM
REM \param message%		Pointer to the message block for the bounce.
:
DEF PROCurl_bounce(message%)
LOCAL len%, result%, url$
:
CASE message%!16 OF
WHEN &4E383 : REM Message_URIReturnResult
	IF (message%!20 AND &01) <> 0 THEN
		SYS "URI_RequestURI", 0, a%, 255, message%!24 TO ,,len%
		a%?len% = 13

		IF FNurl_ant_load($a%) THEN PROCwimperror_report(FNmessage_lookup("URLFailed"), 1, %01, "")
	ENDIF
WHEN &4AF80 : REM Message_ANTOpenURL
	SYS "XOS_GenerateError", message% + 20 TO url$
	IF FNurl_uri_dispatch(url$) THEN
		IF FNurl_ant_load(url$) THEN PROCwimperror_report(FNmessage_lookup("URLFailed"), 1, %01, "")
	ENDIF
ENDCASE
ENDPROC


REM Try to launch a URL via the ANT Broadcast method of sending a
REM Message_ANTOpenURL. Bounces will be picked up by PROCurl_bounce().
REM
REM \param url$			The URL to be launched.
REM \return			TRUE if the message failed to send; else FALSE.
:
DEF FNurl_ant_broadcast(url$)
LOCAL flags%

a%!0 = ((20 + LEN(url$) + 1) + 3) AND (NOT 3)
a%!12 = 0
a%!16 = &4AF80
$(a% + 20) = url$
SYS "XWimp_SendMessage", 18, a%, 0 TO ;flags%

=(flags% AND 1) = 1


REM Try to launch a URL through the Acorn URI system.
REM
REM \param url$			The URL to be launched.
REM \return			TRUE if the method failed to send; else FALSE.
:
DEF FNurl_uri_dispatch(url$)
LOCAL flags%, task%

SYS "XOS_SWINumberFromString",,"URI_Dispatch" TO ;flags%

IF (flags% AND 1) = 0 THEN
	SYS "Wimp_ReadSysInfo", 5 TO task%
	SYS "XURI_Dispatch", 1, url$, task% TO ;flags%
ENDIF

=(flags% AND 1) = 1


REM Try to launch a URL via the ANT Load method.
REM
REM
REM \param url$			The URL to be launched.
REM \return			TRUE if the method failed to send; else FALSE.
:
DEF FNurl_ant_load(url$)
LOCAL offset%, command$, result%

offset% = INSTR(url$, ":")

command$ = "Alias$URLOpen_" + LEFT$(url$, offset% - 1)

SYS "XOS_ReadVarVal", command$, 0, -1, 0, 0 TO ,,result%
IF result% = 0 THEN =TRUE

command$ += " " + url$
command$ = MID$(command$, LEN("Alias$") + 1)

SYS "XWimp_StartTask", command$ TO ;flags%

=(flags% AND 1) = 1

