summaryrefslogtreecommitdiff
path: root/amiga/launch.c
blob: 20505f3bcaaaae1ff6eb3a16faa8379d75392c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright 2008-10 Chris Young <chris@unsatisfactorysoftware.co.uk>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * Fetching of data from a file (implementation).
 */

#include "amiga/os3support.h"

#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/utility.h>
#include <proto/openurl.h>

#include "desktop/options.h"
#include "utils/url.h"

struct Library *OpenURLBase = NULL;
struct OpenURLIFace *IOpenURL = NULL;

struct MinList ami_unsupportedprotocols;

struct ami_protocol
{
	struct MinNode node;
	char *protocol;
};

struct ami_protocol *ami_openurl_add_protocol(const char *url)
{
	struct ami_protocol *ami_p =
		(struct ami_protocol *)AllocVec(sizeof(struct ami_protocol),
			MEMF_PRIVATE | MEMF_CLEAR);

	if(url_scheme(url, &ami_p->protocol) != URL_FUNC_OK)
	{
		FreeVec(ami_p);
		return NULL;
	}

	AddTail((struct List *)&ami_unsupportedprotocols, (struct Node *)ami_p);
	return ami_p;
}

void ami_openurl_free_list(struct MinList *list)
{
	struct ami_protocol *node;
	struct ami_protocol *nnode;

	if(IsMinListEmpty(list)) return;
	node = (struct ami_protocol *)GetHead((struct List *)list);

	do
	{
		nnode=(struct ami_protocol *)GetSucc((struct Node *)node);

		Remove((struct Node *)node);
		if(node->protocol) free(node->protocol);
		FreeVec(node);
		node = NULL;
	}while(node=nnode);
}

BOOL ami_openurl_check_list(struct MinList *list, const char *url)
{
	struct ami_protocol *node;
	struct ami_protocol *nnode;

	if(IsMinListEmpty(list)) return FALSE;
	node = (struct ami_protocol *)GetHead((struct List *)list);

	do
	{
		nnode=(struct ami_protocol *)GetSucc((struct Node *)node);

		if(!strncasecmp(url, node->protocol, strlen(node->protocol)))
			return TRUE;
	}while(node=nnode);

	return FALSE;
}

/**
 * Initialise the fetcher.
 *
 * Must be called once before any other function.
 */

void ami_openurl_open(void)
{
	struct ami_protocol *ami_p;

	if(nsoption_bool(use_openurl_lib)) {
		if(OpenURLBase = OpenLibrary("openurl.library",0))
			IOpenURL = (struct OpenURLIFace *)GetInterface(OpenURLBase,"main",1,NULL);
	}

	NewMinList(&ami_unsupportedprotocols);
	ami_openurl_add_protocol("javascript:");
}

void ami_openurl_close(const char *scheme)
{
	if(IOpenURL) DropInterface((struct Interface *)IOpenURL);
	if(OpenURLBase) CloseLibrary(OpenURLBase);

	ami_openurl_free_list(&ami_unsupportedprotocols);
}

void gui_launch_url(const char *url)
{
	APTR procwin = SetProcWindow((APTR)-1L);
	char *launchurl = NULL;
	BPTR fptr = 0;

	if(ami_openurl_check_list(&ami_unsupportedprotocols, url) == FALSE)
	{
		if(IOpenURL)
		{
			URL_OpenA((STRPTR)url,NULL);
		} else {
			if(launchurl = ASPrintf("URL:%s",url)) {
				fptr = Open(launchurl,MODE_OLDFILE);
				if(fptr) Close(fptr);
					else ami_openurl_add_protocol(url);
				FreeVec(launchurl);
			}
		}
	}

	SetProcWindow(procwin);		
}