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
};
58 MIME_type special_directory
= {"special", "directory", NULL
};
59 MIME_type special_pipe
= {"special", "pipe", NULL
};
60 MIME_type special_socket
= {"special", "socket", NULL
};
61 MIME_type special_block_dev
= {"special", "block-device", NULL
};
62 MIME_type special_char_dev
= {"special", "char-device", NULL
};
63 MIME_type special_unknown
= {"special", "unknown", NULL
};
70 extension_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
74 list
= choices_list_dirs("MIME-info");
75 for (i
= 0; i
< list
->len
; i
++)
76 import_for_dir((gchar
*) g_ptr_array_index(list
, i
));
77 choices_free_list(list
);
80 /* Parse every file in 'dir' */
81 static void import_for_dir(guchar
*path
)
90 while ((item
= readdir(dir
)))
92 if (item
->d_name
[0] == '.')
96 parse_file(make_path(path
, item
->d_name
)->str
,
103 /* Add one entry to the extension_hash table */
104 static void add_ext(char *type_name
, char *ext
)
110 slash
= strchr(type_name
, '/');
111 g_return_if_fail(slash
!= NULL
); /* XXX: Report nicely */
112 len
= slash
- type_name
;
114 new = g_new(MIME_type
, 1);
115 new->media_type
= g_malloc(sizeof(char) * (len
+ 1));
116 memcpy(new->media_type
, type_name
, len
);
117 new->media_type
[len
] = '\0';
119 new->subtype
= g_strdup(slash
+ 1);
122 g_hash_table_insert(extension_hash
, g_strdup(ext
), new);
125 /* Parse one line from the file and add entries to extension_hash */
126 static char *import_extensions(guchar
*line
)
129 if (*line
== '\0' || *line
== '#')
130 return NULL
; /* Comment */
135 return _("Missing MIME-type");
136 while (*line
&& isspace(*line
))
139 if (strncmp(line
, "ext:", 4) == 0)
146 while (*line
&& isspace(*line
))
151 while (*line
&& !isspace(*line
))
155 add_ext(current_type
, ext
);
163 while (*line
&& *line
!= ':' && !isspace(*line
))
167 while (*line
&& isspace(*line
))
170 return _("Trailing chars after MIME-type");
171 current_type
= g_strdup(type
);
176 char *basetype_name(DirItem
*item
)
178 if (item
->flags
& ITEM_FLAG_SYMLINK
)
179 return _("Sym link");
180 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
181 return _("Mount point");
182 else if (item
->flags
& ITEM_FLAG_APPDIR
)
185 switch (item
->base_type
)
191 case TYPE_CHAR_DEVICE
:
192 return _("Char dev");
193 case TYPE_BLOCK_DEVICE
:
194 return _("Block dev");
204 /* MIME-type guessing */
206 /* Returns a pointer to the MIME-type. Defaults to text/plain if we have
209 MIME_type
*type_from_path(char *path
)
213 dot
= strrchr(path
, '.');
217 type
= g_hash_table_lookup(extension_hash
, dot
+ 1);
225 /* Actions for types */
227 gboolean
type_open(char *path
, MIME_type
*type
)
229 char *argv
[] = {NULL
, NULL
, NULL
};
232 gboolean retval
= TRUE
;
237 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
238 open
= choices_find_path_load(type_name
, "MIME-types");
242 open
= choices_find_path_load(type
->media_type
,
248 if (stat(open
, &info
))
250 report_error(PROJECT
, g_strerror(errno
));
254 if (S_ISDIR(info
.st_mode
))
255 argv
[0] = g_strconcat(open
, "/AppRun", NULL
);
259 if (!spawn_full(argv
, home_dir
))
261 report_error(PROJECT
,
262 _("Failed to fork() child process"));
272 /* Return the image for this type, loading it if needed.
273 * Places to check are: (eg type="text_plain", base="text")
274 * 1. Choices:MIME-icons/<type>
275 * 2. Choices:MIME-icons/<base>
276 * 3. Unknown type icon.
278 * Note: You must pixmap_unref() the image afterwards.
280 MaskedPixmap
*type_to_icon(MIME_type
*type
)
286 g_return_val_if_fail(type
!= NULL
, im_unknown
);
289 /* Already got an image? */
292 /* Yes - don't recheck too often */
293 if (abs(now
- type
->image_time
) < 2)
295 pixmap_ref(type
->image
);
298 pixmap_unref(type
->image
);
302 type_name
= g_strconcat(type
->media_type
, "_",
303 type
->subtype
, ".xpm", NULL
);
304 path
= choices_find_path_load(type_name
, "MIME-icons");
307 strcpy(type_name
+ strlen(type
->media_type
), ".xpm");
308 path
= choices_find_path_load(type_name
, "MIME-icons");
314 type
->image
= g_fscache_lookup(pixmap_cache
, path
);
318 type
->image
= im_unknown
;
319 pixmap_ref(type
->image
);
322 type
->image_time
= now
;
324 pixmap_ref(type
->image
);
328 GdkAtom
type_to_atom(MIME_type
*type
)
333 g_return_val_if_fail(type
!= NULL
, GDK_NONE
);
335 str
= g_strconcat(type
->media_type
, "/", type
->subtype
, NULL
);
336 retval
= gdk_atom_intern(str
, FALSE
);
342 /* The user wants to set a new default action for files of this type.
343 * Ask the user if they want to set eg 'text/plain' or just 'text'.
344 * Removes the current binding if possible and returns the path to
345 * save the new one to. NULL means cancel.
347 char *type_ask_which_action(guchar
*media_type
, guchar
*subtype
)
350 guchar
*tmp
, *type_name
, *path
;
353 g_return_val_if_fail(media_type
!= NULL
, NULL
);
354 g_return_val_if_fail(subtype
!= NULL
, NULL
);
356 if (!choices_find_path_save("", PROJECT
, FALSE
))
358 report_error(PROJECT
,
359 _("Choices saving is disabled by CHOICESPATH variable"));
363 type_name
= g_strconcat(media_type
, "/", subtype
, NULL
);
364 tmp
= g_strdup_printf(
365 _("You can choose to set the action for just '%s' files, or "
366 "the default action for all '%s' files which don't already "
367 "have a run action:"), type_name
, media_type
);
368 r
= get_choice(PROJECT
, tmp
, 3, type_name
, media_type
, "Cancel");
374 type_name
= g_strconcat(media_type
, "_", subtype
, NULL
);
375 path
= choices_find_path_save(type_name
, "MIME-types", TRUE
);
379 path
= choices_find_path_save(media_type
, "MIME-types", TRUE
);
383 if (lstat(path
, &info
) == 0)
385 /* A binding already exists... */
386 if (S_ISREG(info
.st_mode
) && info
.st_size
> 256)
388 if (get_choice(PROJECT
,
389 _("A run action already exists and is quite "
390 "a big program - are you sure you want to "
391 "delete it?"), 2, "Delete", "Cancel") != 0)
397 tmp
= g_strdup_printf( _("Can't remove %s: %s"),
398 path
, g_strerror(errno
));
399 report_error(PROJECT
, tmp
);
408 MIME_type
*mime_type_from_base_type(int base_type
)
413 return &special_directory
;
415 return &special_pipe
;
417 return &special_socket
;
418 case TYPE_BLOCK_DEVICE
:
419 return &special_block_dev
;
420 case TYPE_CHAR_DEVICE
:
421 return &special_char_dev
;
423 return &special_unknown
;