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 */
32 #include <sys/param.h>
41 #include "gui_support.h"
47 /* Static prototypes */
48 static char *import_extensions(guchar
*line
);
49 static void import_for_dir(guchar
*path
);
51 /* Maps extensions to MIME_types (eg 'png'-> MIME_type *) */
52 static GHashTable
*extension_hash
= NULL
;
53 static char *current_type
= NULL
; /* (used while reading file) */
55 /* Most things on Unix are text files, so this is the default type */
56 MIME_type text_plain
= {"text", "plain", NULL
};
63 extension_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
66 parse_file(make_path(getenv("APP_DIR"), "MIME-info")->str
,
69 list
= choices_list_dirs("MIME-info");
70 for (i
= 0; i
< list
->len
; i
++)
71 import_for_dir((gchar
*) g_ptr_array_index(list
, i
));
72 choices_free_list(list
);
75 /* Parse every file in 'dir' */
76 static void import_for_dir(guchar
*path
)
85 while ((item
= readdir(dir
)))
87 if (item
->d_name
[0] == '.')
91 parse_file(make_path(path
, item
->d_name
)->str
,
98 /* Add one entry to the extension_hash table */
99 static void add_ext(char *type_name
, char *ext
)
105 slash
= strchr(type_name
, '/');
106 g_return_if_fail(slash
!= NULL
); /* XXX: Report nicely */
107 len
= slash
- type_name
;
109 new = g_new(MIME_type
, 1);
110 new->media_type
= g_malloc(sizeof(char) * (len
+ 1));
111 memcpy(new->media_type
, type_name
, len
);
112 new->media_type
[len
] = '\0';
114 new->subtype
= g_strdup(slash
+ 1);
117 g_hash_table_insert(extension_hash
, g_strdup(ext
), new);
120 /* Parse one line from the file and add entries to extension_hash */
121 static char *import_extensions(guchar
*line
)
124 if (*line
== '\0' || *line
== '#')
125 return NULL
; /* Comment */
130 return _("Missing MIME-type");
131 while (*line
&& isspace(*line
))
134 if (strncmp(line
, "ext:", 4) == 0)
141 while (*line
&& isspace(*line
))
146 while (*line
&& !isspace(*line
))
150 add_ext(current_type
, ext
);
158 while (*line
&& *line
!= ':' && !isspace(*line
))
162 while (*line
&& isspace(*line
))
165 return _("Trailing chars after MIME-type");
166 current_type
= g_strdup(type
);
171 char *basetype_name(DirItem
*item
)
173 if (item
->flags
& ITEM_FLAG_SYMLINK
)
174 return _("Sym link");
175 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
176 return _("Mount point");
177 else if (item
->flags
& ITEM_FLAG_APPDIR
)
180 switch (item
->base_type
)
186 case TYPE_CHAR_DEVICE
:
187 return _("Char dev");
188 case TYPE_BLOCK_DEVICE
:
189 return _("Block dev");
199 /* MIME-type guessing */
201 /* Returns a pointer to the MIME-type. Defaults to text/plain if we have
204 MIME_type
*type_from_path(char *path
)
208 dot
= strrchr(path
, '.');
212 type
= g_hash_table_lookup(extension_hash
, dot
+ 1);
220 /* Actions for types */
222 gboolean
type_open(char *path
, MIME_type
*type
)
224 char *argv
[] = {NULL
, NULL
, NULL
};
227 gboolean retval
= TRUE
;
232 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
233 open
= choices_find_path_load(type_name
, "MIME-types");
237 open
= choices_find_path_load(type
->media_type
,
243 if (stat(open
, &info
))
245 report_error(PROJECT
, g_strerror(errno
));
249 if (S_ISDIR(info
.st_mode
))
250 argv
[0] = g_strconcat(open
, "/AppRun", NULL
);
254 if (!spawn_full(argv
, home_dir
))
256 report_error(PROJECT
,
257 _("Failed to fork() child process"));
267 /* Return the image for this type, loading it if needed.
268 * Places to check are: (eg type="text_plain", base="text")
269 * 1. Choices:MIME-icons/<type>
270 * 2. Choices:MIME-icons/<base>
271 * 3. Unknown type icon.
273 * Note: You must pixmap_unref() the image afterwards.
275 MaskedPixmap
*type_to_icon(MIME_type
*type
)
281 g_return_val_if_fail(type
!= NULL
, default_pixmap
[TYPE_UNKNOWN
]);
284 /* Already got an image? */
287 /* Yes - don't recheck too often */
288 if (abs(now
- type
->image_time
) < 2)
290 pixmap_ref(type
->image
);
293 pixmap_unref(type
->image
);
297 type_name
= g_strconcat(type
->media_type
, "_",
298 type
->subtype
, ".xpm", NULL
);
299 path
= choices_find_path_load(type_name
, "MIME-icons");
302 strcpy(type_name
+ strlen(type
->media_type
), ".xpm");
303 path
= choices_find_path_load(type_name
, "MIME-icons");
309 type
->image
= g_fscache_lookup(pixmap_cache
, path
);
313 type
->image
= default_pixmap
[TYPE_UNKNOWN
];
314 pixmap_ref(type
->image
);
317 type
->image_time
= now
;
319 pixmap_ref(type
->image
);
323 GdkAtom
type_to_atom(MIME_type
*type
)
328 g_return_val_if_fail(type
!= NULL
, GDK_NONE
);
330 str
= g_strconcat(type
->media_type
, "/", type
->subtype
, NULL
);
331 retval
= gdk_atom_intern(str
, FALSE
);
337 /* The user wants to set a new default action for files of this type.
338 * Ask the user if they want to set eg 'text/plain' or just 'text'.
339 * Removes the current binding if possible and returns the path to
340 * save the new one to. NULL means cancel.
342 char *type_ask_which_action(guchar
*media_type
, guchar
*subtype
)
345 guchar
*tmp
, *type_name
, *path
;
347 g_return_val_if_fail(media_type
!= NULL
, NULL
);
348 g_return_val_if_fail(subtype
!= NULL
, NULL
);
350 if (!choices_find_path_save("", PROJECT
, FALSE
))
352 report_error(PROJECT
,
353 _("Choices saving is disabled by CHOICESPATH variable"));
357 type_name
= g_strconcat(media_type
, "/", subtype
, NULL
);
358 tmp
= g_strdup_printf(
359 _("You can choose to set the action for just '%s' files, or "
360 "the default action for all '%s' files which don't already "
361 "have a run action:"), type_name
, media_type
);
362 r
= get_choice(PROJECT
, tmp
, 3, type_name
, media_type
, "Cancel");
368 type_name
= g_strconcat(media_type
, "_", subtype
, NULL
);
369 path
= choices_find_path_save(type_name
, "MIME-types", TRUE
);
373 path
= choices_find_path_save(media_type
, "MIME-types", TRUE
);
377 if (access(path
, F_OK
) == 0)
381 tmp
= g_strdup_printf( _("Can't remove %s: %s"),
382 path
, g_strerror(errno
));
383 report_error(PROJECT
, tmp
);