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)
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
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 */
36 #include "gui_support.h"
42 /* Static prototypes */
43 static char *import_extensions(guchar
*line
);
45 /* Maps extensions to MIME_types (eg 'png'-> MIME_type *) */
46 static GHashTable
*extension_hash
= NULL
;
47 static char *current_type
= NULL
; /* (used while reading file) */
49 /* Most things on Unix are text files, so this is the default type */
50 MIME_type text_plain
= {"text", "plain", NULL
};
54 ChoicesList
*paths
, *next
;
56 extension_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
58 paths
= choices_find_load_all("guess", "MIME-types");
62 parse_file(paths
->path
, import_extensions
);
70 /* Add one entry to the extension_hash table */
71 static void add_ext(char *type_name
, char *ext
)
77 slash
= strchr(type_name
, '/');
78 g_return_if_fail(slash
!= NULL
); /* XXX: Report nicely */
79 len
= slash
- type_name
;
81 new = g_new(MIME_type
, 1);
82 new->media_type
= g_malloc(sizeof(char) * (len
+ 1));
83 memcpy(new->media_type
, type_name
, len
);
84 new->media_type
[len
] = '\0';
86 new->subtype
= g_strdup(slash
+ 1);
89 g_hash_table_insert(extension_hash
, g_strdup(ext
), new);
92 /* Parse one line from the file and add entries to extension_hash */
93 static char *import_extensions(guchar
*line
)
96 if (*line
== '\0' || *line
== '#')
97 return NULL
; /* Comment */
102 return "Missing MIME-type";
103 while (*line
&& isspace(*line
))
106 if (strncmp(line
, "ext:", 4) == 0)
113 while (*line
&& isspace(*line
))
118 while (*line
&& !isspace(*line
))
122 add_ext(current_type
, ext
);
130 while (*line
&& !isspace(*line
))
134 while (*line
&& isspace(*line
))
137 return "Trailing chars after MIME-type";
138 current_type
= g_strdup(type
);
143 char *basetype_name(DirItem
*item
)
145 if (item
->flags
& ITEM_FLAG_SYMLINK
)
147 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
148 return "Mount point";
149 else if (item
->flags
& ITEM_FLAG_APPDIR
)
152 switch (item
->base_type
)
158 case TYPE_CHAR_DEVICE
:
160 case TYPE_BLOCK_DEVICE
:
171 /* MIME-type guessing */
173 /* Returns a pointer to the MIME-type string, or NULL if we have
176 MIME_type
*type_from_path(char *path
)
180 dot
= strrchr(path
, '.');
184 type
= g_hash_table_lookup(extension_hash
, dot
+ 1);
192 /* Actions for types */
194 gboolean
type_open(char *path
, MIME_type
*type
)
196 char *argv
[] = {NULL
, NULL
, NULL
};
199 gboolean retval
= TRUE
;
204 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
205 open
= choices_find_path_load_shared(type_name
, "MIME-types");
209 open
= choices_find_path_load_shared(type
->media_type
,
215 if (stat(open
, &info
))
217 report_error("ROX-Filer", g_strerror(errno
));
221 if (S_ISDIR(info
.st_mode
))
222 argv
[0] = g_strconcat(open
, "/AppRun", NULL
);
226 if (!spawn_full(argv
, getenv("HOME"), 0))
228 report_error("ROX-Filer",
229 "Failed to fork() child process");
239 /* Return the image for this type, loading it if needed.
240 * Places to check are: (eg type="text_plain", base="text")
241 * 1. Choices:MIME-icons/<type>
242 * 2. Choices:MIME-icons/<base>
243 * 3. Unknown type icon.
245 * Note: You must pixmap_unref() the image afterwards.
247 MaskedPixmap
*type_to_icon(MIME_type
*type
)
253 g_return_val_if_fail(type
!= NULL
, default_pixmap
+ TYPE_UNKNOWN
);
256 /* Already got an image? */
259 /* Yes - don't recheck too often */
260 if (abs(now
- type
->image_time
) < 2)
262 pixmap_ref(type
->image
);
265 pixmap_unref(type
->image
);
269 type_name
= g_strconcat(type
->media_type
, "_",
270 type
->subtype
, ".xpm", NULL
);
271 path
= choices_find_path_load_shared(type_name
, "MIME-icons");
274 strcpy(type_name
+ strlen(type
->media_type
), ".xpm");
275 path
= choices_find_path_load_shared(type_name
, "MIME-icons");
281 type
->image
= g_fscache_lookup(pixmap_cache
, path
);
285 type
->image
= default_pixmap
+ TYPE_UNKNOWN
;
286 pixmap_ref(type
->image
);
289 type
->image_time
= now
;
291 pixmap_ref(type
->image
);
295 GdkAtom
type_to_atom(MIME_type
*type
)
300 g_return_val_if_fail(type
!= NULL
, GDK_NONE
);
302 str
= g_strconcat(type
->media_type
, "/", type
->subtype
, NULL
);
303 retval
= gdk_atom_intern(str
, FALSE
);