summaryrefslogtreecommitdiff
path: root/beos/beos_filetype.cpp
blob: 1fa6ff02735ea65f253f58f66ec9dfe8a53e5b28 (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
/*
 * Copyright 2008 Fran├žois Revol <mmu_man@users.sourceforge.net>
 *
 * 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/>.
 */

#define __STDBOOL_H__	1
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <Mime.h>
#include <NodeInfo.h>
#include <String.h>

extern "C" {
#include "content/fetch.h"
#include "utils/log.h"
#include "utils/hashtable.h"
#include "utils/utils.h"
}

#include "beos/beos_filetype.h"

void beos_fetch_filetype_init(void)
{
#if 0
	BMessage mimes;
	status_t err;

	err = BMimeType::GetInstalledTypes(&mimes);
	if (err < B_OK) {
		warn_user("Mime", strerror(err));
		return;
	}

	mime_hash = hash_create(117);

	// just in case
	hash_add(mime_hash, "css", "text/css");
	hash_add(mime_hash, "htm", "text/html");
	hash_add(mime_hash, "html", "text/html");
	hash_add(mime_hash, "jpg", "image/jpeg");
	hash_add(mime_hash, "jpeg", "image/jpeg");
	hash_add(mime_hash, "gif", "image/gif");
	hash_add(mime_hash, "png", "image/png");
	hash_add(mime_hash, "jng", "image/jng");


	BString type;
	int i, j;
	//mimes.PrintToStream();
	for (i = 0; mimes.FindString("types", i, &type) >= B_OK; i++) {
		BMimeType mime(type.String());
		if (!mime.IsValid())
			continue;
		BMessage extensions;
		if (mime.GetFileExtensions(&extensions) < B_OK)
			continue;
		BString ext;
		for (j = 0; extensions.FindString("extentions", i, &ext) >= B_OK; i++) {
			hash_add(mime_hash, ext.String(), type.String());
		}
	}
#endif
}

void beos_fetch_filetype_fin(void)
{
}

const char *fetch_filetype(const char *unix_path)
{
	struct stat statbuf;
	status_t err;

	stat(unix_path, &statbuf);
	if (S_ISDIR(statbuf.st_mode))
		return "application/x-netsurf-directory";

	if (strchr(unix_path, '.') == NULL) {
		/* no extension anywhere! */
		return "text/plain";
	}

	// force some types
	const char *ext;
	ext = strrchr(unix_path, '.');
	if (!strcmp(ext, ".css"))
		return "text/css";
	if (!strcmp(ext, ".html"))
		return "text/html";
	if (!strcmp(ext, ".htm"))
		return "text/html";

	BNode node(unix_path);
	if (node.InitCheck() < B_OK) {
		warn_user("Mime", strerror(err));
		return "text/plain";
	}

	BNodeInfo info(&node);
	if (info.InitCheck() < B_OK) {
		warn_user("Mime", strerror(err));
		return "text/plain";
	}

	// NOT THREADSAFE
	static char type[B_MIME_TYPE_LENGTH];
	if (info.GetType(type) < B_OK) {
		// it might not have been sniffed yet...
		update_mime_info(unix_path, false, true, false);
		// try again
		if (info.GetType(type) < B_OK) {
			warn_user("Mime", strerror(err));
			return "text/plain";
		}
	}

	return type;
}

char *fetch_mimetype(const char *unix_path)
{
	return strdup(fetch_filetype(unix_path));
}