4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, 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 */
39 #include "gui_support.h"
45 /* Static prototypes */
46 static char *import_extensions(guchar
*line
);
47 static void import_for_dir(guchar
*path
);
49 /* Maps extensions to MIME_types (eg 'png'-> MIME_type *) */
50 static GHashTable
*extension_hash
= NULL
;
51 static char *current_type
= NULL
; /* (used while reading file) */
53 /* Most things on Unix are text files, so this is the default type */
54 MIME_type text_plain
= {"text", "plain", NULL
};
61 extension_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
64 parse_file(make_path(getenv("APP_DIR"), "MIME-info")->str
,
67 list
= choices_list_dirs("MIME-info");
68 for (i
= 0; i
< list
->len
; i
++)
69 import_for_dir((gchar
*) g_ptr_array_index(list
, i
));
70 choices_free_list(list
);
73 /* Parse every file in 'dir' */
74 static void import_for_dir(guchar
*path
)
83 while ((item
= readdir(dir
)))
85 if (item
->d_name
[0] == '.')
89 parse_file(make_path(path
, item
->d_name
)->str
,
96 /* Add one entry to the extension_hash table */
97 static void add_ext(char *type_name
, char *ext
)
103 slash
= strchr(type_name
, '/');
104 g_return_if_fail(slash
!= NULL
); /* XXX: Report nicely */
105 len
= slash
- type_name
;
107 new = g_new(MIME_type
, 1);
108 new->media_type
= g_malloc(sizeof(char) * (len
+ 1));
109 memcpy(new->media_type
, type_name
, len
);
110 new->media_type
[len
] = '\0';
112 new->subtype
= g_strdup(slash
+ 1);
115 g_hash_table_insert(extension_hash
, g_strdup(ext
), new);
118 /* Parse one line from the file and add entries to extension_hash */
119 static char *import_extensions(guchar
*line
)
122 if (*line
== '\0' || *line
== '#')
123 return NULL
; /* Comment */
128 return "Missing MIME-type";
129 while (*line
&& isspace(*line
))
132 if (strncmp(line
, "ext:", 4) == 0)
139 while (*line
&& isspace(*line
))
144 while (*line
&& !isspace(*line
))
148 add_ext(current_type
, ext
);
156 while (*line
&& *line
!= ':' && !isspace(*line
))
160 while (*line
&& isspace(*line
))
163 return "Trailing chars after MIME-type";
164 current_type
= g_strdup(type
);
169 char *basetype_name(DirItem
*item
)
171 if (item
->flags
& ITEM_FLAG_SYMLINK
)
173 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
174 return "Mount point";
175 else if (item
->flags
& ITEM_FLAG_APPDIR
)
178 switch (item
->base_type
)
184 case TYPE_CHAR_DEVICE
:
186 case TYPE_BLOCK_DEVICE
:
197 /* MIME-type guessing */
199 /* Returns a pointer to the MIME-type string, or NULL if we have
202 MIME_type
*type_from_path(char *path
)
206 dot
= strrchr(path
, '.');
210 type
= g_hash_table_lookup(extension_hash
, dot
+ 1);
218 /* Actions for types */
220 gboolean
type_open(char *path
, MIME_type
*type
)
222 char *argv
[] = {NULL
, NULL
, NULL
};
225 gboolean retval
= TRUE
;
230 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
231 open
= choices_find_path_load(type_name
, "MIME-types");
235 open
= choices_find_path_load(type
->media_type
,
241 if (stat(open
, &info
))
243 report_error("ROX-Filer", g_strerror(errno
));
247 if (S_ISDIR(info
.st_mode
))
248 argv
[0] = g_strconcat(open
, "/AppRun", NULL
);
252 if (!spawn_full(argv
, home_dir
))
254 report_error("ROX-Filer",
255 "Failed to fork() child process");
265 /* Return the image for this type, loading it if needed.
266 * Places to check are: (eg type="text_plain", base="text")
267 * 1. Choices:MIME-icons/<type>
268 * 2. Choices:MIME-icons/<base>
269 * 3. Unknown type icon.
271 * Note: You must pixmap_unref() the image afterwards.
273 MaskedPixmap
*type_to_icon(MIME_type
*type
)
279 g_return_val_if_fail(type
!= NULL
, default_pixmap
+ TYPE_UNKNOWN
);
282 /* Already got an image? */
285 /* Yes - don't recheck too often */
286 if (abs(now
- type
->image_time
) < 2)
288 pixmap_ref(type
->image
);
291 pixmap_unref(type
->image
);
295 type_name
= g_strconcat(type
->media_type
, "_",
296 type
->subtype
, ".xpm", NULL
);
297 path
= choices_find_path_load(type_name
, "MIME-icons");
300 strcpy(type_name
+ strlen(type
->media_type
), ".xpm");
301 path
= choices_find_path_load(type_name
, "MIME-icons");
307 type
->image
= g_fscache_lookup(pixmap_cache
, path
);
311 type
->image
= default_pixmap
+ TYPE_UNKNOWN
;
312 pixmap_ref(type
->image
);
315 type
->image_time
= now
;
317 pixmap_ref(type
->image
);
321 GdkAtom
type_to_atom(MIME_type
*type
)
326 g_return_val_if_fail(type
!= NULL
, GDK_NONE
);
328 str
= g_strconcat(type
->media_type
, "/", type
->subtype
, NULL
);
329 retval
= gdk_atom_intern(str
, FALSE
);