r39: Dropping on the panel now works correctly.
[rox-filer.git] / ROX-Filer / src / type.c
blob72617238b7e737b9db8321a031ca91a58524d2fc
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 GString *type_path;
79 char *open;
80 struct stat info;
81 gboolean needs_free = FALSE;
83 type_path = g_string_new(type->name);
84 g_string_append(type_path, "/open");
85 open = choices_find_path_load_shared(type_path->str, "MIME-types");
86 g_string_free(type_path, TRUE);
88 if (!open)
89 return FALSE;
91 if (stat(open, &info))
93 report_error("ROX-Filer", g_strerror(errno));
94 return FALSE;
97 if (S_ISDIR(info.st_mode))
99 argv[0] = g_strconcat(open, "/AppRun");
100 needs_free = TRUE;
102 else
103 argv[0] = open;
105 if (!spawn(argv))
106 report_error("ROX-Filer",
107 "Failed to fork() child process");
109 if (needs_free)
110 g_free(argv[0]);
112 return TRUE;
115 MaskedPixmap *type_to_icon(GtkWidget *window, MIME_type *type)
117 if (!type)
118 return NULL;
120 if (!type->image)
122 char *path, *open;
124 path = g_strconcat(type->name, "/icon.xpm", NULL);
125 open = choices_find_path_load_shared(path, "MIME-types");
126 type->image = load_pixmap_from(window, open);
127 if (!type->image)
128 type->image = default_pixmap + TYPE_UNKNOWN;
129 g_free(path);
132 return type->image;