summaryrefslogtreecommitdiff
path: root/frontends/amiga/filetype.c
blob: 0e535de07898286f489778d52fea5a47aab5b606 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 * Copyright 2008, 2011 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/>.
 */

#include "amiga/os3support.h"

#include <stdlib.h>
#include <string.h>
#include <proto/icon.h>
#include <proto/dos.h>
#include <proto/datatypes.h>
#include <proto/exec.h>
#include <workbench/icon.h>

#include "netsurf/content.h"
#include "utils/log.h"
#include "utils/utils.h"

#include "amiga/filetype.h"
#include "amiga/object.h"

/**
 * filetype -- determine the MIME type of a local file
 */

static struct MinList *ami_mime_list = NULL;

struct ami_mime_entry
{
	lwc_string *mimetype;
	lwc_string *datatype;
	lwc_string *filetype;
	lwc_string *plugincmd;
};

enum
{
	AMI_MIME_MIMETYPE,
	AMI_MIME_DATATYPE,
	AMI_MIME_FILETYPE,
	AMI_MIME_PLUGINCMD
};

const char *fetch_filetype(const char *unix_path)
{
	static char mimetype[50];
	struct DiskObject *dobj = NULL;
	struct DataType *dtn;
	BOOL found = FALSE;
	lwc_string *lwc_mimetype;

	/* First, check if we appear to have an icon.
	   We'll just do a filename check here for quickness, although the
	   first word ought to be checked against WB_DISKMAGIC really. */

	if(strncmp(unix_path + strlen(unix_path) - 5, ".info", 5) == 0) {
		strcpy(mimetype,"image/x-amiga-icon");
		found = TRUE;
	}


	/* Secondly try getting a tooltype "MIMETYPE" and use that as the MIME type.
	    Will fail over to default icons if the file doesn't have a real icon. */

	if(!found) {
		if((dobj = GetIconTags(unix_path,ICONGETA_FailIfUnavailable,FALSE,
						TAG_DONE))) {
			STRPTR ttype = NULL;
			ttype = FindToolType(dobj->do_ToolTypes, "MIMETYPE");
			if(ttype) {
				strcpy(mimetype,ttype);
				found = TRUE;
			}
			FreeDiskObject(dobj);
		}
	}

	/* If that didn't work, use the MIME file and DataTypes */

	if(!found) {
		BPTR lock;
		if ((lock = Lock(unix_path, ACCESS_READ))) {
			if ((dtn = ObtainDataTypeA (DTST_FILE, (APTR)lock, NULL))) {
				if(ami_mime_from_datatype(dtn, &lwc_mimetype, NULL)) {
					strcpy(mimetype, lwc_string_data(lwc_mimetype));
					found = TRUE;
					ReleaseDataType(dtn);
				}
			}
			UnLock(lock);
		}
	}

	/* Have a quick check for file extensions (inc RISC OS filetype).
	 * Makes detection a little more robust, and some of the redirects
	 * caused by links in the SVN tree prevent NetSurf from reading the
	 * MIME type from the icon (step two, above).
	 */

	if((!found) || (strcmp("text/plain", mimetype) == 0))
	{
		if((strncmp(unix_path + strlen(unix_path) - 4, ".css", 4) == 0) ||
			(strncmp(unix_path + strlen(unix_path) - 4, ",f79", 4) == 0))
		{
			strcpy(mimetype,"text/css");
			found = TRUE;
		}

		if((strncmp(unix_path + strlen(unix_path) - 4, ".htm", 4) == 0) ||
			(strncmp(unix_path + strlen(unix_path) - 5, ".html", 5) == 0) ||
			(strncmp(unix_path + strlen(unix_path) - 4, ",faf", 4) == 0))
		{
			strcpy(mimetype,"text/html");
			found = TRUE;
		}
		if(strncmp(unix_path + strlen(unix_path) - 3, ".js", 3) == 0) {
			strcpy(mimetype,"application/javascript");
			found = TRUE;
		}
	}

	if(!found) strcpy(mimetype,"text/plain"); /* If all else fails */

	return mimetype;
}

const char *ami_content_type_to_file_type(content_type type)
{
	switch(type)
	{
		case CONTENT_HTML:
			return "html";
		break;

		case CONTENT_TEXTPLAIN:
			return "ascii";
		break;

		case CONTENT_CSS:
			return "css";
		break;

		case CONTENT_IMAGE:
			return "picture";
		break;

		default:
			return "project";	
		break;
	}
}

static void ami_mime_entry_free(void *nso)
{
	struct ami_mime_entry *mimeentry = (struct ami_mime_entry *)nso;

	if(mimeentry->mimetype) lwc_string_unref(mimeentry->mimetype);
	if(mimeentry->datatype) lwc_string_unref(mimeentry->datatype);
	if(mimeentry->filetype) lwc_string_unref(mimeentry->filetype);
	if(mimeentry->plugincmd) lwc_string_unref(mimeentry->plugincmd);
}

nserror ami_mime_init(const char *mimefile)
{
	lwc_error lerror;
	char buffer[256];
	BPTR fh = 0;
	struct RDArgs *rargs = NULL;
	CONST_STRPTR template = "MIMETYPE/A,DT=DATATYPE/K,TYPE=DEFICON/K,CMD=PLUGINCMD/K";
	long rarray[] = {0,0,0,0};
	struct nsObject *node;
	struct ami_mime_entry *mimeentry;

	NSLOG(netsurf, INFO, "mimetypes file: %s", mimefile);

	if(ami_mime_list == NULL)
		ami_mime_list = NewObjList();

	rargs = AllocDosObjectTags(DOS_RDARGS,TAG_DONE);
	if(rargs == NULL) return NSERROR_NOMEM;

	if((fh = FOpen(mimefile, MODE_OLDFILE, 0)))
	{
		while((FGets(fh, (STRPTR)&buffer, 256) != 0))
		{
			rargs->RDA_Source.CS_Buffer = (char *)&buffer;
			rargs->RDA_Source.CS_Length = 256;
			rargs->RDA_Source.CS_CurChr = 0;

			rargs->RDA_DAList = NULL;
			rargs->RDA_Buffer = NULL;
			rargs->RDA_BufSiz = 0;
			rargs->RDA_ExtHelp = NULL;
			rargs->RDA_Flags = 0;

			rarray[AMI_MIME_MIMETYPE] = 0;
			rarray[AMI_MIME_DATATYPE] = 0;
			rarray[AMI_MIME_FILETYPE] = 0;
			rarray[AMI_MIME_PLUGINCMD] = 0;

			if(ReadArgs(template, rarray, rargs))
			{
				if ((node = AddObject(ami_mime_list, AMINS_MIME))) {
					ObjectCallback(node, ami_mime_entry_free);
					mimeentry = calloc(1, sizeof(struct ami_mime_entry));
					node->objstruct = mimeentry;

					if(rarray[AMI_MIME_MIMETYPE])
					{
						lerror = lwc_intern_string((char *)rarray[AMI_MIME_MIMETYPE],
									strlen((char *)rarray[AMI_MIME_MIMETYPE]), &mimeentry->mimetype);
						if (lerror != lwc_error_ok)
							return NSERROR_NOMEM;
					}

					if(rarray[AMI_MIME_DATATYPE])
					{
						lerror = lwc_intern_string((char *)rarray[AMI_MIME_DATATYPE],
									strlen((char *)rarray[AMI_MIME_DATATYPE]), &mimeentry->datatype);
						if (lerror != lwc_error_ok)
							return NSERROR_NOMEM;
					}

					if(rarray[AMI_MIME_FILETYPE])
					{
						lerror = lwc_intern_string((char *)rarray[AMI_MIME_FILETYPE],
									strlen((char *)rarray[AMI_MIME_FILETYPE]), &mimeentry->filetype);
						if (lerror != lwc_error_ok)
							return NSERROR_NOMEM;
					}

					if(rarray[AMI_MIME_PLUGINCMD])
					{
						lerror = lwc_intern_string((char *)rarray[AMI_MIME_PLUGINCMD],
									strlen((char *)rarray[AMI_MIME_PLUGINCMD]), &mimeentry->plugincmd);
						if (lerror != lwc_error_ok)
							return NSERROR_NOMEM;
					}
				}
				FreeArgs(rargs);
			}
		}
		FClose(fh);
	}
	FreeDosObject(DOS_RDARGS, rargs);

	return NSERROR_OK;
}

void ami_mime_free(void)
{
	ami_mime_dump();
	FreeObjList(ami_mime_list);
}

/**
 * Return next matching MIME entry
 *
 * \param search lwc_string to search for (or NULL for all)
 * \param type of value being searched for (AMI_MIME_#?)
 * \param start_node node to continue search (updated on exit)
 * \return entry or NULL if no match
 */

static struct ami_mime_entry *ami_mime_entry_locate(lwc_string *search,
		int type, struct Node **start_node)
{
	struct nsObject *node;
	struct nsObject *nnode;
	struct ami_mime_entry *mimeentry;
	lwc_error lerror;
	bool ret = false;

	if(IsMinListEmpty(ami_mime_list)) return NULL;

	if(*start_node)
	{
		node = (struct nsObject *)GetSucc(*start_node);
		if(node == NULL) return NULL;
	}
	else
	{
		node = (struct nsObject *)GetHead((struct List *)ami_mime_list);
	}

	do
	{
		nnode=(struct nsObject *)GetSucc((struct Node *)node);
		mimeentry = node->objstruct;

		lerror = lwc_error_ok;

		switch(type)
		{
			case AMI_MIME_MIMETYPE:
				if(search != NULL)
					lerror = lwc_string_isequal(mimeentry->mimetype, search, &ret);
				else if(mimeentry->mimetype != NULL)
					ret = true;
			break;

			case AMI_MIME_DATATYPE:
				if(search != NULL)
					lerror = lwc_string_isequal(mimeentry->datatype, search, &ret);
				else if(mimeentry->datatype != NULL)
					ret = true;
			break;

			case AMI_MIME_FILETYPE:
				if(search != NULL)
					lerror = lwc_string_isequal(mimeentry->filetype, search, &ret);
				else if(mimeentry->filetype != NULL)
					ret = true;
			break;

			case AMI_MIME_PLUGINCMD:
				if(search != NULL)
					lerror = lwc_string_isequal(mimeentry->plugincmd, search, &ret);
				else if(mimeentry->plugincmd != NULL)
					ret = true;
			break;
		}

		if((lerror == lwc_error_ok) && (ret == true))
			break;

	} while((node=nnode));

	*start_node = (struct Node *)node;

	if(ret == true) return mimeentry;
		else return NULL;
}


static APTR ami_mime_guess_add_datatype(struct DataType *dt, lwc_string **lwc_mimetype)
{
	struct nsObject *node;
	char mimetype[100];
	char *dt_name_lwr = NULL;
	struct ami_mime_entry *mimeentry;
	lwc_error lerror;
	struct DataTypeHeader *dth = dt->dtn_Header;
	char *p;

	node = AddObject(ami_mime_list, AMINS_MIME);
	if(node == NULL) return NULL;

	mimeentry = calloc(1, sizeof(struct ami_mime_entry));
	if(mimeentry == NULL) return NULL;

	node->objstruct = mimeentry;
	ObjectCallback(node, ami_mime_entry_free);

	lerror = lwc_intern_string(dth->dth_Name, strlen(dth->dth_Name), &mimeentry->datatype);
	if (lerror != lwc_error_ok)
		return NULL;

	dt_name_lwr = strdup(dth->dth_Name);
	if(dt_name_lwr == NULL) return NULL;

	strlwr(dt_name_lwr);
	p = dt_name_lwr;

	while(*p != '\0')
	{
		if(*p == ' ') *p = '-';
		if(*p == '/') *p = '-';
		p++;
	}

	switch(dth->dth_GroupID)
	{
		case GID_TEXT:
		case GID_DOCUMENT:
			if(strcmp("ascii", dt_name_lwr)==0)
			{
				strcpy(mimetype,"text/plain");
			}
			else
			{
				sprintf(mimetype,"text/%s", dt_name_lwr);
			}
		break;
		case GID_SOUND:
		case GID_INSTRUMENT:
		case GID_MUSIC:
			sprintf(mimetype,"audio/%s", dt_name_lwr);
		break;
		case GID_PICTURE:
			if(strcmp("sprite", dt_name_lwr)==0)
			{
				strcpy(mimetype,"image/x-riscos-sprite");
			}
			else
			{
				sprintf(mimetype,"image/%s", dt_name_lwr);
			}
		break;
		case GID_ANIMATION:
		case GID_MOVIE:
			sprintf(mimetype,"video/%s", dt_name_lwr);
		break;
		case GID_SYSTEM:
		default:
			if(strcmp("directory", dt_name_lwr)==0)
			{
				strcpy(mimetype,"application/x-netsurf-directory");
			}
			else if(strcmp("binary", dt_name_lwr)==0)
			{
				strcpy(mimetype,"application/octet-stream");
			}
			else sprintf(mimetype,"application/%s", dt_name_lwr);
		break;
	}

	lerror = lwc_intern_string(mimetype, strlen(mimetype), &mimeentry->mimetype);
	if (lerror != lwc_error_ok)
		return NULL;

	*lwc_mimetype = mimeentry->mimetype;

	lerror = lwc_intern_string(dt_name_lwr, strlen(dt_name_lwr), &mimeentry->filetype);
	if (lerror != lwc_error_ok)
		return NULL;

	free(dt_name_lwr);
	return node;
}

/**
 * Return a MIME Type matching a DataType
 *
 * \param dt a DataType structure
 * \param mimetype lwc_string to hold the MIME type
 * \param start_node node to feed back in to continue search
 * \return node or NULL if no match
 */

struct Node *ami_mime_from_datatype(struct DataType *dt,
		lwc_string **mimetype, struct Node *start_node)
{
	struct DataTypeHeader *dth;
	struct Node *node;
	struct ami_mime_entry *mimeentry;
	lwc_string *dt_name;
	lwc_error lerror;

	if(dt == NULL) return NULL;

	dth = dt->dtn_Header;
	lerror = lwc_intern_string(dth->dth_Name, strlen(dth->dth_Name), &dt_name);
	if (lerror != lwc_error_ok)
		return NULL;

	node = start_node;
	mimeentry = ami_mime_entry_locate(dt_name, AMI_MIME_DATATYPE, &node);
	lwc_string_unref(dt_name);

	if(mimeentry != NULL)
	{
		*mimetype = mimeentry->mimetype;
		return (struct Node *)node;
	}
	else
	{
		if(start_node == NULL)
		{
			/* If there are no matching entries in the file, guess */
			return ami_mime_guess_add_datatype(dt, mimetype);
		}
		else
		{
			return NULL;
		}
	}
}

/**
 * Return the DefIcons type matching a MIME type
 *
 * \param mimetype lwc_string MIME type
 * \param filetype ptr to lwc_string to hold DefIcons type
 * \param start_node node to feed back in to continue search
 * \return node or NULL if no match
 */

struct Node *ami_mime_to_filetype(lwc_string *mimetype,
		lwc_string **filetype, struct Node *start_node)
{
	struct Node *node;
	struct ami_mime_entry *mimeentry;

	node = start_node;
	mimeentry = ami_mime_entry_locate(mimetype, AMI_MIME_MIMETYPE, &node);

	if(mimeentry != NULL)
	{
		*filetype = mimeentry->filetype;
		return (struct Node *)node;
	}
	else
	{
		return NULL;
	}
}

const char *ami_mime_content_to_filetype(struct hlcache_handle *c)
{
	struct Node *node;
	lwc_string *filetype;
	lwc_string *mimetype;

	mimetype = content_get_mime_type(c);

	node = ami_mime_to_filetype(mimetype, &filetype, NULL);

	if(node && (filetype != NULL))
		return lwc_string_data(filetype);
	else
		return ami_content_type_to_file_type(content_get_type(c));
}

/**
 * Return all MIME types containing a plugincmd
 *
 * \param mimetype ptr to lwc_string MIME type
 * \param start_node node to feed back in to continue search
 * \return node or NULL if no match
 */

struct Node *ami_mime_has_cmd(lwc_string **mimetype, struct Node *start_node)
{
	struct Node *node;
	struct ami_mime_entry *mimeentry;

	node = start_node;
	mimeentry = ami_mime_entry_locate(NULL, AMI_MIME_PLUGINCMD, &node);

	if(mimeentry != NULL)
	{
		*mimetype = mimeentry->mimetype;
		return (struct Node *)node;
	}
	else
	{
		return NULL;
	}
}

/**
 * Return the plugincmd matching a MIME type
 *
 * \param mimetype lwc_string MIME type
 * \param plugincmd ptr to lwc_string to hold plugincmd
 * \param start_node node to feed back in to continue search
 * \return node or NULL if no match
 */

static struct Node *ami_mime_to_plugincmd(lwc_string *mimetype,
		lwc_string **plugincmd, struct Node *start_node)
{
	struct Node *node;
	struct ami_mime_entry *mimeentry;

	node = start_node;
	mimeentry = ami_mime_entry_locate(mimetype, AMI_MIME_MIMETYPE, &node);

	if(mimeentry != NULL)
	{
		*plugincmd = mimeentry->plugincmd;
		return (struct Node *)node;
	}
	else
	{
		return NULL;
	}
}

lwc_string *ami_mime_content_to_cmd(struct hlcache_handle *c)
{
	struct Node *node;
	lwc_string *plugincmd;
	lwc_string *mimetype;

	mimetype = content_get_mime_type(c);

	node = ami_mime_to_plugincmd(mimetype,
		&plugincmd, NULL);

	if(node && (plugincmd != NULL)) return plugincmd;
		else return NULL;
}

/**
 * Compare the MIME type of an hlcache_handle to a DefIcons type
 */

bool ami_mime_compare(struct hlcache_handle *c, const char *type)
{
	bool ret = false;
	lwc_error lerror;
	lwc_string *filetype;
	lwc_string *mime_filetype;
	lwc_string *mime = content_get_mime_type(c);

	if(ami_mime_to_filetype(mime, &mime_filetype, NULL) == NULL)
		return false;

	lerror = lwc_intern_string(type, strlen(type), &filetype);
	if (lerror != lwc_error_ok)
		return false;

	lerror = lwc_string_isequal(filetype, mime_filetype, &ret);
	if (lerror != lwc_error_ok)
		return false;

	lwc_string_unref(filetype);

	return ret;
}


void ami_mime_dump(void)
{
	struct Node *node = NULL;
	struct ami_mime_entry *mimeentry;

	while((mimeentry = ami_mime_entry_locate(NULL, AMI_MIME_MIMETYPE, &node))) {
		NSLOG(netsurf, INFO, "%s DT=\"%s\" TYPE=\"%s\" CMD=\"%s\"",
		      mimeentry->mimetype ? lwc_string_data(mimeentry->mimetype) : "",
		      mimeentry->datatype ? lwc_string_data(mimeentry->datatype) : "",
		      mimeentry->filetype ? lwc_string_data(mimeentry->filetype) : "",
		      mimeentry->plugincmd ? lwc_string_data(mimeentry->plugincmd) : "");
	};
}