r208: Added Bernard Jungen's patch:
[rox-filer.git] / ROX-Filer / src / type.c
blob48d2d50a10bd5e73476ac9119d54ad543375f2f0
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* type.c - code for dealing with filetypes */
24 #include <glib.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <time.h>
32 #include "string.h"
33 #include "main.h"
34 #include "filer.h"
35 #include "pixmaps.h"
36 #include "run.h"
37 #include "gui_support.h"
38 #include "choices.h"
39 #include "type.h"
40 #include "support.h"
41 #include "options.h"
43 /* Static prototypes */
44 static char *import_extensions(guchar *line);
46 /* Maps extensions to MIME_types (eg 'png'-> MIME_type *) */
47 static GHashTable *extension_hash = NULL;
48 static char *current_type = NULL; /* (used while reading file) */
50 /* Most things on Unix are text files, so this is the default type */
51 MIME_type text_plain = {"text", "plain", NULL};
53 void type_init()
55 ChoicesList *paths, *next;
57 extension_hash = g_hash_table_new(g_str_hash, g_str_equal);
59 paths = choices_find_load_all("guess", "MIME-types");
60 while (paths)
62 current_type = NULL;
63 parse_file(paths->path, import_extensions);
64 next = paths->next;
65 g_free(paths->path);
66 g_free(paths);
67 paths = next;
71 /* Add one entry to the extension_hash table */
72 static void add_ext(char *type_name, char *ext)
74 MIME_type *new;
75 char *slash;
76 int len;
78 slash = strchr(type_name, '/');
79 g_return_if_fail(slash != NULL); /* XXX: Report nicely */
80 len = slash - type_name;
82 new = g_new(MIME_type, 1);
83 new->media_type = g_malloc(sizeof(char) * (len + 1));
84 memcpy(new->media_type, type_name, len);
85 new->media_type[len] = '\0';
87 new->subtype = g_strdup(slash + 1);
88 new->image = NULL;
90 g_hash_table_insert(extension_hash, g_strdup(ext), new);
93 /* Parse one line from the file and add entries to extension_hash */
94 static char *import_extensions(guchar *line)
97 if (*line == '\0' || *line == '#')
98 return NULL; /* Comment */
100 if (isspace(*line))
102 if (!current_type)
103 return "Missing MIME-type";
104 while (*line && isspace(*line))
105 line++;
107 if (strncmp(line, "ext:", 4) == 0)
109 char *ext;
110 line += 4;
112 for (;;)
114 while (*line && isspace(*line))
115 line++;
116 if (*line == '\0')
117 break;
118 ext = line;
119 while (*line && !isspace(*line))
120 line++;
121 if (*line)
122 *line++ = '\0';
123 add_ext(current_type, ext);
126 /* else ignore */
128 else
130 char *type = line;
131 while (*line && *line != ':' && !isspace(*line))
132 line++;
133 if (*line)
134 *line++ = '\0';
135 while (*line && isspace(*line))
136 line++;
137 if (*line)
138 return "Trailing chars after MIME-type";
139 current_type = g_strdup(type);
141 return NULL;
144 char *basetype_name(DirItem *item)
146 if (item->flags & ITEM_FLAG_SYMLINK)
147 return "Sym link";
148 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
149 return "Mount point";
150 else if (item->flags & ITEM_FLAG_APPDIR)
151 return "App dir";
153 switch (item->base_type)
155 case TYPE_FILE:
156 return "File";
157 case TYPE_DIRECTORY:
158 return "Dir";
159 case TYPE_CHAR_DEVICE:
160 return "Char dev";
161 case TYPE_BLOCK_DEVICE:
162 return "Block dev";
163 case TYPE_PIPE:
164 return "Pipe";
165 case TYPE_SOCKET:
166 return "Socket";
169 return "Unknown";
172 /* MIME-type guessing */
174 /* Returns a pointer to the MIME-type string, or NULL if we have
175 * no opinion.
177 MIME_type *type_from_path(char *path)
179 char *dot;
181 dot = strrchr(path, '.');
182 if (dot)
184 MIME_type *type;
185 type = g_hash_table_lookup(extension_hash, dot + 1);
186 if (type)
187 return type;
190 return &text_plain;
193 /* Actions for types */
195 gboolean type_open(char *path, MIME_type *type)
197 char *argv[] = {NULL, NULL, NULL};
198 char *open;
199 char *type_name;
200 gboolean retval = TRUE;
201 struct stat info;
203 argv[1] = path;
205 type_name = g_strconcat(type->media_type, "_", type->subtype, NULL);
206 open = choices_find_path_load_shared(type_name, "MIME-types");
207 g_free(type_name);
208 if (!open)
210 open = choices_find_path_load_shared(type->media_type,
211 "MIME-types");
212 if (!open)
213 return FALSE;
216 if (stat(open, &info))
218 report_error("ROX-Filer", g_strerror(errno));
219 return FALSE;
222 if (S_ISDIR(info.st_mode))
223 argv[0] = g_strconcat(open, "/AppRun", NULL);
224 else
225 argv[0] = open;
227 if (!spawn_full(argv, home_dir))
229 report_error("ROX-Filer",
230 "Failed to fork() child process");
231 retval = FALSE;
234 if (argv[0] != open)
235 g_free(argv[0]);
237 return retval;
240 /* Return the image for this type, loading it if needed.
241 * Places to check are: (eg type="text_plain", base="text")
242 * 1. Choices:MIME-icons/<type>
243 * 2. Choices:MIME-icons/<base>
244 * 3. Unknown type icon.
246 * Note: You must pixmap_unref() the image afterwards.
248 MaskedPixmap *type_to_icon(MIME_type *type)
250 char *path;
251 char *type_name;
252 time_t now;
254 g_return_val_if_fail(type != NULL, default_pixmap + TYPE_UNKNOWN);
256 now = time(NULL);
257 /* Already got an image? */
258 if (type->image)
260 /* Yes - don't recheck too often */
261 if (abs(now - type->image_time) < 2)
263 pixmap_ref(type->image);
264 return type->image;
266 pixmap_unref(type->image);
267 type->image = NULL;
270 type_name = g_strconcat(type->media_type, "_",
271 type->subtype, ".xpm", NULL);
272 path = choices_find_path_load_shared(type_name, "MIME-icons");
273 if (!path)
275 strcpy(type_name + strlen(type->media_type), ".xpm");
276 path = choices_find_path_load_shared(type_name, "MIME-icons");
279 g_free(type_name);
281 if (path)
282 type->image = g_fscache_lookup(pixmap_cache, path);
284 if (!type->image)
286 type->image = default_pixmap + TYPE_UNKNOWN;
287 pixmap_ref(type->image);
290 type->image_time = now;
292 pixmap_ref(type->image);
293 return type->image;
296 GdkAtom type_to_atom(MIME_type *type)
298 char *str;
299 GdkAtom retval;
301 g_return_val_if_fail(type != NULL, GDK_NONE);
303 str = g_strconcat(type->media_type, "/", type->subtype, NULL);
304 retval = gdk_atom_intern(str, FALSE);
305 g_free(str);
307 return retval;