New style MIME typing; maps MIME types to DataTypes and DefIcon format names.

Currently only able to resolve DataTypes to MIME Types.

TODO:
- DT MIME type guessing needs mostly removing (only use if no matches in mimetypes
file)
- Migrate the rest of filetype.c to the new format (can filetype using icon.library
and reverse lookup MIME type, for example)
- Use to distinguish between CONTENT_IMAGE types
- Simplify so DefIcon type defaults to lowercase DataType name

svn path=/trunk/netsurf/; revision=12360
This commit is contained in:
Chris Young 2011-05-09 22:11:18 +00:00
parent 3276ce5db9
commit 1f1fe78c17
8 changed files with 270 additions and 142 deletions

View File

@ -70,72 +70,6 @@ static const content_handler amiga_dt_picture_content_handler = {
.no_share = false,
};
nserror amiga_dt_picture_init_from_mime(void)
{
lwc_string *type;
lwc_error lerror;
nserror error;
char buffer[256];
BPTR fh = 0;
struct RDArgs *rargs = NULL;
STRPTR template = "MIMETYPE/A,DEFICON/K,DTPICTURE/S,DTANIM/S,PLUGINCMD/K";
long rarray[] = {0,0,0,0,0};
enum
{
A_MIMETYPE,
A_DEFICON,
A_DTPICTURE,
A_DTANIM,
A_PLUGINCMD
};
rargs = AllocDosObjectTags(DOS_RDARGS,TAG_DONE);
if(fh = FOpen("PROGDIR:Resources/mimetypes", MODE_OLDFILE, 0))
{
while(FGets(fh, (UBYTE *)&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[A_DTPICTURE] = 0;
printf("buffer = %s\n", buffer);
if(ReadArgs(template, rarray, rargs))
{
if(rarray[A_DTPICTURE])
{
printf("mime = %s\n", rarray[A_MIMETYPE]);
lerror = lwc_intern_string((char *)rarray[A_MIMETYPE],
strlen((char *)rarray[A_MIMETYPE]), &type);
if (lerror != lwc_error_ok)
return NSERROR_NOMEM;
error = content_factory_register_handler(type,
&amiga_dt_picture_content_handler);
lwc_string_unref(type);
if (error != NSERROR_OK)
return error;
}
}
}
FClose(fh);
}
return NSERROR_OK;
}
nserror amiga_dt_picture_init(void)
{
@ -145,6 +79,7 @@ nserror amiga_dt_picture_init(void)
lwc_error lerror;
nserror error;
BPTR fh = 0;
struct Node *node;
while((dt = ObtainDataType(DTST_RAM, NULL,
DTA_DataType, prevdt,
@ -169,14 +104,24 @@ nserror amiga_dt_picture_init(void)
if (error != NSERROR_OK)
return error;
do {
node = ami_mime_from_datatype(dt, &type, node);
if(node)
{
error = content_factory_register_handler(type,
&amiga_dt_picture_content_handler);
if (error != NSERROR_OK)
return error;
}
}while (node != NULL);
}
ReleaseDataType(prevdt);
error = amiga_dt_picture_init_from_mime();
if (error != NSERROR_OK)
return error;
return NSERROR_OK;
}

View File

@ -19,6 +19,7 @@
#include <stdlib.h>
#include <string.h>
#include "amiga/filetype.h"
#include "amiga/object.h"
#include "content/fetch.h"
#include "content/content.h"
#include "utils/log.h"
@ -26,12 +27,23 @@
#include <proto/icon.h>
#include <proto/dos.h>
#include <proto/datatypes.h>
#include <proto/exec.h>
#include <workbench/icon.h>
/**
* filetype -- determine the MIME type of a local file
*/
struct MinList *ami_mime_list;
struct ami_mime_entry
{
lwc_string *mimetype;
lwc_string *datatype;
lwc_string *filetype;
lwc_string *plugincmd;
};
const char *fetch_filetype(const char *unix_path)
{
static char mimetype[50];
@ -215,3 +227,159 @@ bool ami_mime_compare(struct hlcache_handle *c, const char *type)
}
else return false;
}
nserror ami_mime_init(const char *mimefile)
{
lwc_string *type;
lwc_error lerror;
nserror error;
char buffer[256];
BPTR fh = 0;
struct RDArgs *rargs = NULL;
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;
enum
{
A_MIMETYPE,
A_DATATYPE,
A_FILETYPE,
A_PLUGINCMD
};
ami_mime_list = NewObjList();
rargs = AllocDosObjectTags(DOS_RDARGS,TAG_DONE);
if(fh = FOpen(mimefile, MODE_OLDFILE, 0))
{
while(FGets(fh, (UBYTE *)&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[A_MIMETYPE] = 0;
rarray[A_DATATYPE] = 0;
rarray[A_FILETYPE] = 0;
rarray[A_PLUGINCMD] = 0;
if(ReadArgs(template, rarray, rargs))
{
node = AddObject(ami_mime_list, AMINS_MIME);
mimeentry = AllocVec(sizeof(struct ami_mime_entry), MEMF_PRIVATE | MEMF_CLEAR);
node->objstruct = mimeentry;
if(rarray[A_MIMETYPE])
{
lerror = lwc_intern_string((char *)rarray[A_MIMETYPE],
strlen((char *)rarray[A_MIMETYPE]), &mimeentry->mimetype);
if (lerror != lwc_error_ok)
return NSERROR_NOMEM;
}
if(rarray[A_DATATYPE])
{
lerror = lwc_intern_string((char *)rarray[A_DATATYPE],
strlen((char *)rarray[A_DATATYPE]), &mimeentry->datatype);
if (lerror != lwc_error_ok)
return NSERROR_NOMEM;
}
if(rarray[A_FILETYPE])
{
lerror = lwc_intern_string((char *)rarray[A_FILETYPE],
strlen((char *)rarray[A_FILETYPE]), &mimeentry->filetype);
if (lerror != lwc_error_ok)
return NSERROR_NOMEM;
}
if(rarray[A_PLUGINCMD])
{
lerror = lwc_intern_string((char *)rarray[A_PLUGINCMD],
strlen((char *)rarray[A_PLUGINCMD]), &mimeentry->plugincmd);
if (lerror != lwc_error_ok)
return NSERROR_NOMEM;
}
}
}
FClose(fh);
}
}
void ami_mime_free(void)
{
FreeObjList(ami_mime_list);
}
void ami_mime_entry_free(struct ami_mime_entry *mimeentry)
{
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);
}
/**
* 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 = dt->dtn_Header;
struct nsObject *node;
struct nsObject *nnode;
struct ami_mime_entry *mimeentry;
lwc_string *dt_name;
lwc_error lerror;
bool ret = false;
if(IsMinListEmpty(ami_mime_list)) return NULL;
lerror = lwc_intern_string(dth->dth_Name, strlen(dth->dth_Name), &dt_name);
if (lerror != lwc_error_ok)
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_string_isequal(mimeentry->datatype, dt_name, &ret);
if((lerror == lwc_error_ok) && (ret == true))
{
*mimetype = mimeentry->mimetype;
break;
}
}while(node=nnode);
lwc_string_unref(dt_name);
if(ret == true) return (struct Node *)node;
else return NULL;
}

View File

@ -19,10 +19,20 @@
#ifndef AMIGA_FILETYPE_H
#define AMIGA_FILETYPE_H
#include <stdbool.h>
#include <libwapcaplet/libwapcaplet.h>
#include "content/content_type.h"
#include "utils/errors.h"
#include <datatypes/datatypes.h>
struct hlcache_handle;
struct ami_mime_entry;
nserror ami_mime_init(const char *mimefile);
void ami_mime_free(void);
void ami_mime_entry_free(struct ami_mime_entry *mimeentry);
struct Node *ami_mime_from_datatype(struct DataType *dt,
lwc_string **mimetype, struct Node *start_node);
const char *ami_content_type_to_file_type(content_type type);
void ami_datatype_to_mimetype(struct DataType *dtn, char *mimetype);

View File

@ -58,6 +58,7 @@
#include "amiga/misc.h"
#include "amiga/options.h"
#include "amiga/plotters.h"
#include "amiga/plugin_hack.h"
#include "amiga/print.h"
#include "amiga/schedule.h"
#include "amiga/search.h"
@ -751,9 +752,11 @@ int main(int argc, char** argv)
if(ami_locate_resource(messages, "Messages") == false)
die("Cannot open Messages file");
ami_mime_init("PROGDIR:Resources/mimetypes");
ami_schedule_open_timer();
ami_schedule_create();
amiga_plugin_hack_init();
amiga_datatypes_init();
netsurf_init(&argc, &argv, "PROGDIR:Resources/Options", messages);
@ -777,9 +780,12 @@ int main(int argc, char** argv)
netsurf_exit();
amiga_plugin_hack_fini();
amiga_datatypes_fini();
amiga_icon_fini();
ami_mime_free();
return 0;
}

View File

@ -53,6 +53,7 @@ void DelObjectInternal(struct nsObject *dtzo, BOOL free_obj)
{
Remove((struct Node *)dtzo);
if(dtzo->Type == AMINS_FONT) ami_font_close(dtzo->objstruct);
if(dtzo->Type == AMINS_MIME) ami_mime_entry_free(dtzo->objstruct);
if(dtzo->objstruct && free_obj) FreeVec(dtzo->objstruct);
if(dtzo->dtz_Node.ln_Name) free(dtzo->dtz_Node.ln_Name);
FreeVec(dtzo);

View File

@ -35,6 +35,7 @@ enum
AMINS_GUIOPTSWINDOW,
AMINS_PRINTWINDOW,
AMINS_FONT,
AMINS_MIME
};
struct nsObject

View File

@ -1,66 +0,0 @@
# This file contains a list of MIME types that can be handled by
# picture class datatypes (subclasses of picture.datatype)
#
# It does not matter if this file contains MIME Types for formats
# that a DataType is not installed for, unless a different
# subclass handles this type.
# BMP
application/bmp
application/preview
application/x-bmp
application/x-win-bitmap
image/bmp
image/ms-bmp
image/x-bitmap
image/x-bmp
image/x-ms-bmp
image/x-win-bitmap
image/x-windows-bmp
image/x-xbitmap
# GIF
# Expect this to cause problems with GIF anims if GIFANIM datatype installed
image/gif
# ICO
application/ico
application/x-ico
image/ico
image/vnd.microsoft.icon
image/x-icon
# .info
image/x-amiga-icon
# JNG
image/jng
image/x-jng
# JPEG
image/jpeg
image/jpg
image/pjpeg
# JPEG2000
image/jp2
image/jpeg2000
# MNG
image/mng
image/x-mng
video/mng
video/x-mng
# PNG
image/png
# RISC OS Sprite
image/x-riscos-sprite
# SVG
image/svg
image/svg+xml
# WebP
image/webp

View File

@ -1,7 +1,70 @@
; mimetypes
; this file is parsed using readargs
; This file contains a list of MIME types that can be handled by NetSurf,
; mapping them to DataType and DefIcons format names.
; It is parsed by ReadArgs with the following template:
; MIMETYPE/A,DT=DATATYPE/K,TYPE=DEFICON/K,CMD=PLUGINCMD/K
; BMP
application/bmp DT=BMP TYPE=bmp
application/preview DT=BMP TYPE=bmp
application/x-bmp DT=BMP TYPE=bmp
application/x-win-bitmap DT=BMP TYPE=bmp
image/bmp DT=BMP TYPE=bmp
image/ms-bmp DT=BMP TYPE=bmp
image/x-bitmap DT=BMP TYPE=bmp
image/x-bmp DT=BMP TYPE=bmp
image/x-ms-bmp DT=BMP TYPE=bmp
image/x-win-bitmap DT=BMP TYPE=bmp
image/x-windows-bmp DT=BMP TYPE=bmp
image/x-xbitmap DT=BMP TYPE=bmp
; GIF
; Expect problems with GIF anims if GIFANIM datatype installed
image/gif DT=GIF TYPE=gif
;image/gif DT=GIFANIM TYPE=gif
; ICO
application/ico DT=ICO TYPE=ico
application/x-ico DT=ICO TYPE=ico
image/ico DT=ICO TYPE=ico
image/vnd.microsoft.icon DT=ICO TYPE=ico
image/x-icon DT=ICO TYPE=ico
; .info
image/x-amiga-icon DT=INFO
; JNG
image/jng DT=JNG TYPE=jng
image/x-jng DT=JNG TYPE=jng
; JPEG
image/jpeg DT=JPEG TYPE=jpeg
image/jpg DT=JPEG TYPE=jpeg
image/pjpeg DT=JPEG TYPE=jpeg
; JPEG2000
image/jp2 DT=JP2 TYPE=jp2
image/jpeg2000 DT=JP2 TYPE=jp2
; MNG
image/mng DT=MNG TYPE=mng
image/x-mng DT=MNG TYPE=mng
video/mng DT=MNG TYPE=mng
video/x-mng DT=MNG TYPE=mng
; PNG
image/png DT=PNG TYPE=png
; RISC OS Sprite
image/x-riscos-sprite DT=Sprite TYPE=rosprite
; SVG
image/svg DT=SVG TYPE=svg
image/svg+xml DT=SVG TYPE=svg
; WebP
image/webp DT=WebP TYPE=webp
;
image/mng DTPICTURE
image/x-mng DTPICTURE
video/mng DTPICTURE
video/x-mng DTPICTURE
; Experimental
;
application/x-shockwave-flash CMD=gnash:aos4-gnash