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 */
37 #include "gui_support.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
};
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");
63 parse_file(paths
->path
, import_extensions
);
71 /* Add one entry to the extension_hash table */
72 static void add_ext(char *type_name
, char *ext
)
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);
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 */
103 return "Missing MIME-type";
104 while (*line
&& isspace(*line
))
107 if (strncmp(line
, "ext:", 4) == 0)
114 while (*line
&& isspace(*line
))
119 while (*line
&& !isspace(*line
))
123 add_ext(current_type
, ext
);
131 while (*line
&& *line
!= ':' && !isspace(*line
))
135 while (*line
&& isspace(*line
))
138 return "Trailing chars after MIME-type";
139 current_type
= g_strdup(type
);
144 char *basetype_name(DirItem
*item
)
146 if (item
->flags
& ITEM_FLAG_SYMLINK
)
148 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
149 return "Mount point";
150 else if (item
->flags
& ITEM_FLAG_APPDIR
)
153 switch (item
->base_type
)
159 case TYPE_CHAR_DEVICE
:
161 case TYPE_BLOCK_DEVICE
:
172 /* MIME-type guessing */
174 /* Returns a pointer to the MIME-type string, or NULL if we have
177 MIME_type
*type_from_path(char *path
)
181 dot
= strrchr(path
, '.');
185 type
= g_hash_table_lookup(extension_hash
, dot
+ 1);
193 /* Actions for types */
195 gboolean
type_open(char *path
, MIME_type
*type
)
197 char *argv
[] = {NULL
, NULL
, NULL
};
200 gboolean retval
= TRUE
;
205 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
206 open
= choices_find_path_load_shared(type_name
, "MIME-types");
210 open
= choices_find_path_load_shared(type
->media_type
,
216 if (stat(open
, &info
))
218 report_error("ROX-Filer", g_strerror(errno
));
222 if (S_ISDIR(info
.st_mode
))
223 argv
[0] = g_strconcat(open
, "/AppRun", NULL
);
227 if (!spawn_full(argv
, home_dir
))
229 report_error("ROX-Filer",
230 "Failed to fork() child process");
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
)
254 g_return_val_if_fail(type
!= NULL
, default_pixmap
+ TYPE_UNKNOWN
);
257 /* Already got an image? */
260 /* Yes - don't recheck too often */
261 if (abs(now
- type
->image_time
) < 2)
263 pixmap_ref(type
->image
);
266 pixmap_unref(type
->image
);
270 type_name
= g_strconcat(type
->media_type
, "_",
271 type
->subtype
, ".xpm", NULL
);
272 path
= choices_find_path_load_shared(type_name
, "MIME-icons");
275 strcpy(type_name
+ strlen(type
->media_type
), ".xpm");
276 path
= choices_find_path_load_shared(type_name
, "MIME-icons");
282 type
->image
= g_fscache_lookup(pixmap_cache
, path
);
286 type
->image
= default_pixmap
+ TYPE_UNKNOWN
;
287 pixmap_ref(type
->image
);
290 type
->image_time
= now
;
292 pixmap_ref(type
->image
);
296 GdkAtom
type_to_atom(MIME_type
*type
)
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
);