NetSurf
filetype.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 François Revol <mmu_man@users.sourceforge.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#define __STDBOOL_H__ 1
20#include <stdbool.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
25#include <ctype.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <unistd.h>
29
30#include <Mime.h>
31#include <NodeInfo.h>
32#include <String.h>
33
34extern "C" {
35#include "utils/log.h"
36#include "utils/hashtable.h"
37#include "utils/utils.h"
38}
39
40#include "beos/filetype.h"
41#include "beos/gui.h"
42
43static struct {
44 const char *type;
45 const char *ext1;
46 const char *ext2;
47} default_types[] = {
48 { "text/plain", "txt", NULL },
49 { "text/html", "htm", "html" },
50 { "text/css", "css", NULL },
51 { "image/gif", "gif", NULL },
52 { "image/jpeg", "jpg", "jpeg" },
53 { "image/png", "png", NULL },
54 { "image/jng", "jng", NULL },
55 { NULL, NULL, NULL }
56};
57
59{
60 BMimeType m;
61 status_t err;
62 int i;
63
64 // make sure we have basic mime types in the database
65 for (i = 0; default_types[i].type; i++) {
66 if (m.SetTo(default_types[i].type) < B_OK)
67 continue;
68 if (m.IsInstalled())
69 continue;
70 err = m.Install();
71 if (err < B_OK) {
72 beos_warn_user("Mime", strerror(err));
73 continue;
74 }
75 // the mime db doesn't know about it yet
76 BMessage extensions('exts');
77 if (default_types[i].ext1)
78 extensions.AddString("extensions", default_types[i].ext1);
79 if (default_types[i].ext2)
80 extensions.AddString("extensions", default_types[i].ext2);
81 err = m.SetFileExtensions(&extensions);
82 if (err < B_OK) {
83 beos_warn_user("Mime", strerror(err));
84 }
85 }
86}
87
89{
90}
91
92const char *fetch_filetype(const char *unix_path)
93{
94 // struct stat statbuf;
95 status_t err;
96 int i;
97 // NOT THREADSAFE
98 static char type[B_MIME_TYPE_LENGTH];
99
100 // override reading the mime type for known types
101 // avoids getting CSS files as text/x-source-code
102 // even though it's the mime sniffer rules that should be fixed.
103 BString ext(unix_path);
104 ext.Remove(0, ext.FindLast('.') + 1);
105 for (i = 0; default_types[i].type; i++) {
106 if (ext == default_types[i].ext1)
107 return default_types[i].type;
108 if (ext == default_types[i].ext2)
109 return default_types[i].type;
110 }
111
112 BEntry entry(unix_path, true);
113 BNode node(&entry);
114 err = node.InitCheck();
115 if (err < B_OK)
116 return "text/plain";
117
118 if (node.IsDirectory())
119 return "application/x-netsurf-directory";
120
121 BNodeInfo info(&node);
122 err = info.InitCheck();
123 if (err < B_OK)
124 return "test/plain";
125
126 err = info.GetType(type);
127 if (err < B_OK) {
128 // not there yet, sniff and retry
129 err = update_mime_info(unix_path, false, true, false);
130 if (err < B_OK)
131 return "text/plain";
132 err = info.GetType(type);
133 if (err < B_OK)
134 return "text/plain";
135 }
136
137 return type;
138}
const char * ext1
Definition: filetype.cpp:45
void beos_fetch_filetype_fin(void)
Definition: filetype.cpp:88
const char * ext2
Definition: filetype.cpp:46
const char * type
Definition: filetype.cpp:44
const char * fetch_filetype(const char *unix_path)
filetype – determine the MIME type of a local file
Definition: filetype.cpp:92
static struct @31 default_types[]
void beos_fetch_filetype_init(void)
Definition: filetype.cpp:58
nserror beos_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.cpp:116
Interface to Write-Once hash table for string to string mapping.
Interface to utility string handling.
Interface to a number of general purpose functionality.