r40: Changed MIME system slightly - now uses MIME-open, MIME-icons, etc.
[rox-filer.git] / ROX-Filer / src / type.c
blobd793ecede5eb25d43bebf783dd7f3033330dfd28
1 /* vi: set cindent:
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * By Thomas Leonard, <tal197@ecs.soton.ac.uk>.
6 */
8 /* type.c - code for dealing with filetypes */
10 #include <glib.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <errno.h>
15 #include "filer.h"
16 #include "pixmaps.h"
17 #include "apps.h"
18 #include "gui_support.h"
19 #include "choices.h"
20 #include "type.h"
21 #include "support.h"
23 /* Static prototypes */
25 /* XXX: Just for testing... */
26 static MIME_type text_plain = {"text/plain"};
27 static MIME_type image_xpm = {"image/x-xpixmap"};
29 void type_init()
33 char *basetype_name(FileItem *item)
35 if (item->flags & ITEM_FLAG_SYMLINK)
36 return "Sym link";
37 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
38 return "Mount point";
39 else if (item->flags & ITEM_FLAG_APPDIR)
40 return "App dir";
42 switch (item->base_type)
44 case TYPE_FILE:
45 return "File";
46 case TYPE_DIRECTORY:
47 return "Dir";
48 case TYPE_CHAR_DEVICE:
49 return "Char dev";
50 case TYPE_BLOCK_DEVICE:
51 return "Block dev";
52 case TYPE_PIPE:
53 return "Pipe";
54 case TYPE_SOCKET:
55 return "Socket";
58 return "Unknown";
61 /* MIME-type guessing */
63 /* Returns a pointer to the MIME-type string, or NULL if we have
64 * no opinion.
66 MIME_type *type_from_path(char *path)
68 if (strstr(path, ".xpm"))
69 return &image_xpm;
70 return &text_plain;
73 /* Actions for types */
75 gboolean type_open(char *path, MIME_type *type)
77 char *argv[] = {NULL, path, NULL};
78 char *open;
79 struct stat info;
80 gboolean needs_free = FALSE;
82 open = choices_find_path_load_shared(type->name, "MIME-open");
83 if (!open)
84 return FALSE;
86 if (stat(open, &info))
88 report_error("ROX-Filer", g_strerror(errno));
89 return FALSE;
92 if (S_ISDIR(info.st_mode))
94 argv[0] = g_strconcat(open, "/AppRun");
95 needs_free = TRUE;
97 else
98 argv[0] = open;
100 if (!spawn(argv))
101 report_error("ROX-Filer",
102 "Failed to fork() child process");
104 if (needs_free)
105 g_free(argv[0]);
107 return TRUE;
110 MaskedPixmap *type_to_icon(GtkWidget *window, MIME_type *type)
112 if (!type)
113 return NULL;
115 if (!type->image)
117 char *open, *path;
119 path = g_strconcat(type->name, ".xpm", NULL);
120 open = choices_find_path_load_shared(path, "MIME-icons");
121 g_free(path);
122 type->image = load_pixmap_from(window, open);
123 if (!type->image)
124 type->image = default_pixmap + TYPE_UNKNOWN;
127 return type->image;