r4651: When the handler for a MIME type is a symlink to a program, run the program
[rox-filer/ma.git] / ROX-Filer / src / type.c
blob914d20945e6ecd64c1b4036e4fbd082d674f615f
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* type.c - code for dealing with filetypes */
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <time.h>
29 #include <sys/param.h>
30 #include <fnmatch.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
34 #ifdef WITH_GNOMEVFS
35 # include <libgnomevfs/gnome-vfs.h>
36 # include <libgnomevfs/gnome-vfs-mime.h>
37 # include <libgnomevfs/gnome-vfs-mime-handlers.h>
38 # include <libgnomevfs/gnome-vfs-application-registry.h>
39 #endif
41 #include "global.h"
43 #include "string.h"
44 #include "fscache.h"
45 #include "main.h"
46 #include "pixmaps.h"
47 #include "run.h"
48 #include "gui_support.h"
49 #include "choices.h"
50 #include "type.h"
51 #include "support.h"
52 #include "diritem.h"
53 #include "dnd.h"
54 #include "options.h"
55 #include "filer.h"
56 #include "action.h" /* (for action_chmod) */
57 #include "xml.h"
58 #include "dropbox.h"
59 #include "xdgmime.h"
60 #include "xtypes.h"
61 #include "run.h"
63 #define TYPE_NS "http://www.freedesktop.org/standards/shared-mime-info"
64 enum {SET_MEDIA, SET_TYPE};
66 /* Colours for file types (same order as base types) */
67 static gchar *opt_type_colours[][2] = {
68 {"display_err_colour", "#ff0000"},
69 {"display_unkn_colour", "#000000"},
70 {"display_dir_colour", "#000080"},
71 {"display_pipe_colour", "#444444"},
72 {"display_sock_colour", "#ff00ff"},
73 {"display_file_colour", "#000000"},
74 {"display_cdev_colour", "#000000"},
75 {"display_bdev_colour", "#000000"},
76 {"display_door_colour", "#ff00ff"},
77 {"display_exec_colour", "#006000"},
78 {"display_adir_colour", "#006000"}
80 #define NUM_TYPE_COLOURS\
81 (sizeof(opt_type_colours) / sizeof(opt_type_colours[0]))
83 /* Parsed colours for file types */
84 static Option o_type_colours[NUM_TYPE_COLOURS];
85 static GdkColor type_colours[NUM_TYPE_COLOURS];
87 /* Static prototypes */
88 static void alloc_type_colours(void);
89 static void options_changed(void);
90 static char *get_action_save_path(GtkWidget *dialog);
91 static MIME_type *get_mime_type(const gchar *type_name, gboolean can_create);
92 static gboolean remove_handler_with_confirm(const guchar *path);
93 static void set_icon_theme(void);
94 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label);
96 /* Hash of all allocated MIME types, indexed by "media/subtype".
97 * MIME_type structs are never freed; this table prevents memory leaks
98 * when rereading the config files.
100 static GHashTable *type_hash = NULL;
102 /* Most things on Unix are text files, so this is the default type */
103 MIME_type *text_plain;
104 MIME_type *inode_directory;
105 MIME_type *inode_mountpoint;
106 MIME_type *inode_pipe;
107 MIME_type *inode_socket;
108 MIME_type *inode_block_dev;
109 MIME_type *inode_char_dev;
110 MIME_type *application_executable;
111 MIME_type *application_octet_stream;
112 MIME_type *application_x_shellscript;
113 MIME_type *application_x_desktop;
114 MIME_type *inode_unknown;
115 MIME_type *inode_door;
117 static Option o_display_colour_types;
118 static Option o_icon_theme;
120 GtkIconTheme *icon_theme = NULL;
122 void type_init(void)
124 int i;
126 icon_theme = gtk_icon_theme_new();
128 type_hash = g_hash_table_new(g_str_hash, g_str_equal);
130 text_plain = get_mime_type("text/plain", TRUE);
131 inode_directory = get_mime_type("inode/directory", TRUE);
132 inode_mountpoint = get_mime_type("inode/mount-point", TRUE);
133 inode_pipe = get_mime_type("inode/fifo", TRUE);
134 inode_socket = get_mime_type("inode/socket", TRUE);
135 inode_block_dev = get_mime_type("inode/blockdevice", TRUE);
136 inode_char_dev = get_mime_type("inode/chardevice", TRUE);
137 application_executable = get_mime_type("application/x-executable", TRUE);
138 application_octet_stream = get_mime_type("application/octet-stream", TRUE);
139 application_x_shellscript = get_mime_type("application/x-shellscript", TRUE);
140 application_x_desktop = get_mime_type("application/x-desktop", TRUE);
141 application_x_desktop->executable = TRUE;
142 inode_unknown = get_mime_type("inode/unknown", TRUE);
143 inode_door = get_mime_type("inode/door", TRUE);
145 option_add_string(&o_icon_theme, "icon_theme", "ROX");
146 option_add_int(&o_display_colour_types, "display_colour_types", TRUE);
147 option_register_widget("icon-theme-chooser", build_icon_theme);
149 for (i = 0; i < NUM_TYPE_COLOURS; i++)
150 option_add_string(&o_type_colours[i],
151 opt_type_colours[i][0],
152 opt_type_colours[i][1]);
153 alloc_type_colours();
155 set_icon_theme();
157 option_add_notify(options_changed);
160 /* Read-load all the glob patterns.
161 * Note: calls filer_update_all.
163 void reread_mime_files(void)
165 gtk_icon_theme_rescan_if_needed(icon_theme);
167 xdg_mime_shutdown();
169 filer_update_all();
172 /* Returns the MIME_type structure for the given type name. It is looked
173 * up in type_hash and returned if found. If not found (and can_create is
174 * TRUE) then a new MIME_type is made, added to type_hash and returned.
175 * NULL is returned if type_name is not in type_hash and can_create is
176 * FALSE, or if type_name does not contain a '/' character.
178 static MIME_type *get_mime_type(const gchar *type_name, gboolean can_create)
180 MIME_type *mtype;
181 gchar *slash;
183 mtype = g_hash_table_lookup(type_hash, type_name);
184 if (mtype || !can_create)
185 return mtype;
187 slash = strchr(type_name, '/');
188 g_return_val_if_fail(slash != NULL, NULL); /* XXX: Report nicely */
190 mtype = g_new(MIME_type, 1);
191 mtype->media_type = g_strndup(type_name, slash - type_name);
192 mtype->subtype = g_strdup(slash + 1);
193 mtype->image = NULL;
194 mtype->comment = NULL;
196 mtype->executable = xdg_mime_mime_type_subclass(type_name,
197 "application/x-executable");
199 g_hash_table_insert(type_hash, g_strdup(type_name), mtype);
201 return mtype;
204 const char *basetype_name(DirItem *item)
206 if (item->flags & ITEM_FLAG_SYMLINK)
207 return _("Sym link");
208 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
209 return _("Mount point");
210 else if (item->flags & ITEM_FLAG_APPDIR)
211 return _("App dir");
213 switch (item->base_type)
215 case TYPE_FILE:
216 return _("File");
217 case TYPE_DIRECTORY:
218 return _("Dir");
219 case TYPE_CHAR_DEVICE:
220 return _("Char dev");
221 case TYPE_BLOCK_DEVICE:
222 return _("Block dev");
223 case TYPE_PIPE:
224 return _("Pipe");
225 case TYPE_SOCKET:
226 return _("Socket");
227 case TYPE_DOOR:
228 return _("Door");
231 return _("Unknown");
234 static void append_names(gpointer key, gpointer value, gpointer udata)
236 GList **list = (GList **) udata;
238 *list = g_list_prepend(*list, key);
241 /* Return list of all mime type names. Caller must free the list
242 * but NOT the strings it contains (which are never freed).
244 GList *mime_type_name_list(void)
246 GList *list = NULL;
248 g_hash_table_foreach(type_hash, append_names, &list);
249 list = g_list_sort(list, (GCompareFunc) strcmp);
251 return list;
254 /* MIME-type guessing */
256 /* Get the type of this file - stats the file and uses that if
257 * possible. For regular or missing files, uses the pathname.
259 MIME_type *type_get_type(const guchar *path)
261 DirItem *item;
262 MIME_type *type = NULL;
264 item = diritem_new("");
265 diritem_restat(path, item, NULL);
266 if (item->base_type != TYPE_ERROR)
267 type = item->mime_type;
268 diritem_free(item);
270 if (type)
271 return type;
273 type = type_from_path(path);
275 if (!type)
276 return text_plain;
278 return type;
281 /* Returns a pointer to the MIME-type.
283 * Tries all enabled methods:
284 * - Look for extended attribute
285 * - If no attribute, check file name
286 * - If no name rule, check contents
288 * NULL if we can't think of anything.
290 MIME_type *type_from_path(const char *path)
292 MIME_type *mime_type = NULL;
293 const char *type_name;
295 /* Check for extended attribute first */
296 mime_type = xtype_get(path);
297 if (mime_type)
298 return mime_type;
300 /* Try name and contents next */
301 type_name = xdg_mime_get_mime_type_for_file(path);
302 if (type_name)
303 return get_mime_type(type_name, TRUE);
305 return NULL;
308 /* Returns the file/dir in Choices for handling this type.
309 * NULL if there isn't one. g_free() the result.
311 char *handler_for(MIME_type *type)
313 char *type_name;
314 char *open;
315 char *target;
317 type_name = g_strconcat(type->media_type, "_", type->subtype, NULL);
318 open = choices_find_xdg_path_load(type_name, "MIME-types", SITE);
319 g_free(type_name);
321 if (!open)
322 open = choices_find_xdg_path_load(type->media_type,
323 "MIME-types", SITE);
325 if (!open)
326 return NULL;
328 /* Some programs behave differently depending on the command
329 * name (e.g., 'vim' vs 'gvim'), so symlinks need to be followed here.
331 target = readlink_dup(open);
332 if (!target)
334 return open;
337 if (target[0] == '/')
339 /* Absolute path */
340 g_free(open);
341 return target;
343 else
345 /* Relative path (shouldn't normally be needed) */
346 gchar *dir;
347 char *abs_path;
349 dir = g_path_get_dirname(open);
350 g_free(open);
352 abs_path = g_strconcat(dir, "/", target, NULL);
353 g_free(target);
354 g_free(dir);
356 return abs_path;
360 MIME_type *mime_type_lookup(const char *type)
362 return get_mime_type(type, TRUE);
365 /* Actions for types */
367 /* Return the image for this type, loading it if needed.
368 * Places to check are: (eg type="text_plain", base="text")
369 * 1. <Choices>/MIME-icons/base_subtype
370 * 2. Icon theme 'mime-base:subtype'
371 * 3. Icon theme 'mime-base'
372 * 4. Unknown type icon.
374 * Note: You must g_object_unref() the image afterwards.
376 MaskedPixmap *type_to_icon(MIME_type *type)
378 GtkIconInfo *full;
379 char *type_name, *path;
380 time_t now;
382 if (type == NULL)
384 g_object_ref(im_unknown);
385 return im_unknown;
388 now = time(NULL);
389 /* Already got an image? */
390 if (type->image)
392 /* Yes - don't recheck too often */
393 if (abs(now - type->image_time) < 2)
395 g_object_ref(type->image);
396 return type->image;
398 g_object_unref(type->image);
399 type->image = NULL;
402 type_name = g_strconcat(type->media_type, "_", type->subtype,
403 ".png", NULL);
404 path = choices_find_xdg_path_load(type_name, "MIME-icons", SITE);
405 g_free(type_name);
406 if (path)
408 type->image = g_fscache_lookup(pixmap_cache, path);
409 g_free(path);
412 if (type->image)
413 goto out;
415 type_name = g_strconcat("mime-", type->media_type, ":",
416 type->subtype, NULL);
417 full = gtk_icon_theme_lookup_icon(icon_theme, type_name, HUGE_HEIGHT, 0);
418 g_free(type_name);
419 if (!full)
421 /* Ugly hack... try for a GNOME icon */
422 if (type == inode_directory)
423 type_name = g_strdup("gnome-fs-directory");
424 else
425 type_name = g_strconcat("gnome-mime-", type->media_type,
426 "-", type->subtype, NULL);
427 full = gtk_icon_theme_lookup_icon(icon_theme,
428 type_name,
429 HUGE_HEIGHT, 0);
430 g_free(type_name);
432 if (!full)
434 /* Try for a media type */
435 type_name = g_strconcat("mime-", type->media_type, NULL);
436 full = gtk_icon_theme_lookup_icon(icon_theme,
437 type_name,
438 HUGE_HEIGHT, 0);
439 g_free(type_name);
441 if (!full)
443 /* Ugly hack... try for a GNOME default media icon */
444 type_name = g_strconcat("gnome-mime-", type->media_type, NULL);
446 full = gtk_icon_theme_lookup_icon(icon_theme,
447 type_name,
448 HUGE_HEIGHT, 0);
449 g_free(type_name);
451 if (full)
453 const char *icon_path;
454 /* Get the actual icon through our cache, not through GTK, because
455 * GTK doesn't cache icons.
457 icon_path = gtk_icon_info_get_filename(full);
458 if (icon_path != NULL)
459 type->image = g_fscache_lookup(pixmap_cache, icon_path);
460 /* else shouldn't happen, because we didn't use
461 * GTK_ICON_LOOKUP_USE_BUILTIN.
463 gtk_icon_info_free(full);
466 out:
467 if (!type->image)
469 /* One ref from the type structure, one returned */
470 type->image = im_unknown;
471 g_object_ref(im_unknown);
474 type->image_time = now;
476 g_object_ref(type->image);
477 return type->image;
480 GdkAtom type_to_atom(MIME_type *type)
482 char *str;
483 GdkAtom retval;
485 g_return_val_if_fail(type != NULL, GDK_NONE);
487 str = g_strconcat(type->media_type, "/", type->subtype, NULL);
488 retval = gdk_atom_intern(str, FALSE);
489 g_free(str);
491 return retval;
494 static void show_shell_help(gpointer data)
496 info_message(_("Enter a shell command which will load \"$@\" into "
497 "a suitable program. Eg:\n\n"
498 "gimp \"$@\""));
501 /* Called if the user clicks on the OK button. Returns FALSE if an error
502 * was displayed instead of performing the action.
504 static gboolean set_shell_action(GtkWidget *dialog)
506 GtkEntry *entry;
507 const guchar *command;
508 gchar *tmp, *path;
509 int error = 0, len;
510 int fd;
512 entry = g_object_get_data(G_OBJECT(dialog), "shell_command");
514 g_return_val_if_fail(entry != NULL, FALSE);
516 command = gtk_entry_get_text(entry);
518 if (!strchr(command, '$'))
520 show_shell_help(NULL);
521 return FALSE;
524 path = get_action_save_path(dialog);
525 if (!path)
526 return FALSE;
528 tmp = g_strdup_printf("#! /bin/sh\nexec %s\n", command);
529 len = strlen(tmp);
531 fd = open(path, O_CREAT | O_WRONLY, 0755);
532 if (fd == -1)
533 error = errno;
534 else
536 FILE *file;
538 file = fdopen(fd, "w");
539 if (file)
541 if (fwrite(tmp, 1, len, file) < len)
542 error = errno;
543 if (fclose(file) && error == 0)
544 error = errno;
546 else
547 error = errno;
550 if (error)
551 report_error(g_strerror(error));
553 g_free(tmp);
554 g_free(path);
556 gtk_widget_destroy(dialog);
558 return TRUE;
561 static void set_action_response(GtkWidget *dialog, gint response, gpointer data)
563 if (response == GTK_RESPONSE_OK)
564 if (!set_shell_action(dialog))
565 return;
566 gtk_widget_destroy(dialog);
569 /* Return the path of the file in choices that handles this type and
570 * radio setting.
571 * NULL if nothing is defined for it.
573 static guchar *handler_for_radios(GObject *dialog)
575 Radios *radios;
576 MIME_type *type;
578 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
579 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
581 g_return_val_if_fail(radios != NULL, NULL);
582 g_return_val_if_fail(type != NULL, NULL);
584 switch (radios_get_value(radios))
586 case SET_MEDIA:
587 return choices_find_xdg_path_load(type->media_type,
588 "MIME-types", SITE);
589 case SET_TYPE:
591 gchar *tmp, *handler;
592 tmp = g_strconcat(type->media_type, "_",
593 type->subtype, NULL);
594 handler = choices_find_xdg_path_load(tmp,
595 "MIME-types",
596 SITE);
597 g_free(tmp);
598 return handler;
600 default:
601 g_warning("Bad type");
602 return NULL;
606 /* (radios can be NULL if called from clear_run_action) */
607 static void run_action_update(Radios *radios, gpointer data)
609 guchar *handler;
610 DropBox *drop_box;
611 GObject *dialog = G_OBJECT(data);
613 drop_box = g_object_get_data(dialog, "rox-dropbox");
615 g_return_if_fail(drop_box != NULL);
617 handler = handler_for_radios(dialog);
619 if (handler)
621 char *old = handler;
623 handler = readlink_dup(old);
624 if (handler)
625 g_free(old);
626 else
627 handler = old;
630 drop_box_set_path(DROP_BOX(drop_box), handler);
631 g_free(handler);
634 static void clear_run_action(GtkWidget *drop_box, GtkWidget *dialog)
636 guchar *handler;
638 handler = handler_for_radios(G_OBJECT(dialog));
640 if (handler)
641 remove_handler_with_confirm(handler);
643 run_action_update(NULL, dialog);
646 /* Called when a URI list is dropped onto the box in the Set Run Action
647 * dialog. Make sure it's an application, and make that the default
648 * handler.
650 static void drag_app_dropped(GtkWidget *drop_box,
651 const guchar *app,
652 GtkWidget *dialog)
654 DirItem *item;
656 item = diritem_new("");
657 diritem_restat(app, item, NULL);
658 if (item->flags & ITEM_FLAG_APPDIR || EXECUTABLE_FILE(item))
660 guchar *path;
662 path = get_action_save_path(dialog);
664 if (path)
666 if (symlink(app, path))
667 delayed_error("symlink: %s",
668 g_strerror(errno));
669 else
670 destroy_on_idle(dialog);
672 g_free(path);
675 else
676 delayed_error(
677 _("This is not a program! Give me an application "
678 "instead!"));
680 diritem_free(item);
683 /* Find the current command which is used to run files of this type.
684 * Returns NULL on failure. g_free() the result.
686 static guchar *get_current_command(MIME_type *type)
688 struct stat info;
689 char *handler, *nl, *data = NULL;
690 long len;
691 guchar *command = NULL;
693 handler = handler_for(type);
695 if (!handler)
696 return NULL; /* No current handler */
698 if (stat(handler, &info))
699 goto out; /* Can't stat */
701 if ((!S_ISREG(info.st_mode)) || info.st_size > 256)
702 goto out; /* Only use small regular files */
704 if (!load_file(handler, &data, &len))
705 goto out; /* Didn't load OK */
707 if (strncmp(data, "#! /bin/sh\nexec ", 16) != 0)
708 goto out; /* Not one of ours */
710 nl = strchr(data + 16, '\n');
711 if (!nl)
712 goto out; /* No newline! */
714 command = g_strndup(data + 16, nl - data - 16);
715 out:
716 g_free(handler);
717 g_free(data);
718 return command;
721 /* Find the current command which is used to run files of this type,
722 * and return a textual description of it.
723 * Only call for non-executable files.
724 * g_free() the result.
726 gchar *describe_current_command(MIME_type *type)
728 char *handler;
729 char *desc = NULL;
730 struct stat info;
732 g_return_val_if_fail(type != NULL, NULL);
734 handler = handler_for(type);
736 if (!handler)
737 return g_strdup(_("No run action defined"));
739 if (mc_stat(handler, &info) !=0 )
741 desc = g_strdup_printf(_("Error in handler %s: %s"), handler,
742 g_strerror(errno));
743 goto out;
746 if (S_ISDIR(info.st_mode))
748 const guchar *tmp;
749 uid_t dir_uid = info.st_uid;
751 tmp = make_path(handler, "AppRun");
753 if (mc_lstat(tmp, &info) != 0 || info.st_uid != dir_uid
754 || !(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
755 desc = g_strdup_printf(
756 _("Invalid application %s (bad AppRun)"),
757 handler);
758 /* Else, just report handler... */
760 goto out;
763 /* It's not an application directory, and it's not a symlink... */
765 if (access(handler, X_OK) != 0)
767 desc = g_strdup_printf(_("Non-executable %s"), handler);
768 goto out;
771 desc = get_current_command(type);
772 out:
773 if (!desc)
774 desc = handler;
775 else
776 g_free(handler);
778 return desc;
781 /* Display a dialog box allowing the user to set the default run action
782 * for this type.
784 void type_set_handler_dialog(MIME_type *type)
786 guchar *tmp;
787 GtkDialog *dialog;
788 GtkWidget *frame, *entry, *label, *button;
789 GtkWidget *hbox;
790 Radios *radios;
792 g_return_if_fail(type != NULL);
794 dialog = GTK_DIALOG(gtk_dialog_new());
795 gtk_dialog_set_has_separator(dialog, FALSE);
796 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
798 g_object_set_data(G_OBJECT(dialog), "mime_type", type);
800 gtk_window_set_title(GTK_WINDOW(dialog), _("Set run action"));
802 radios = radios_new(run_action_update, dialog);
803 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
805 radios_add(radios,
806 _("If a handler for the specific type isn't set up, "
807 "use this as the default."), SET_MEDIA,
808 _("Set default for all `%s/<anything>'"),
809 type->media_type);
811 radios_add(radios,
812 _("Use this application for all files with this MIME "
813 "type."), SET_TYPE,
814 _("Only for the type `%s' (%s/%s)"),
815 mime_type_comment(type),
816 type->media_type, type->subtype);
818 radios_set_value(radios, SET_TYPE);
820 frame = drop_box_new(_("Drop a suitable application here"));
822 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
824 radios_pack(radios, GTK_BOX(dialog->vbox));
825 gtk_box_pack_start(GTK_BOX(dialog->vbox), frame, TRUE, TRUE, 0);
827 g_signal_connect(frame, "path_dropped",
828 G_CALLBACK(drag_app_dropped), dialog);
829 g_signal_connect(frame, "clear",
830 G_CALLBACK(clear_run_action), dialog);
832 hbox = gtk_hbox_new(FALSE, 4);
833 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 4);
834 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
835 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_("OR")),
836 FALSE, TRUE, 0);
837 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
839 hbox = gtk_hbox_new(FALSE, 4);
840 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
842 label = gtk_label_new(_("Enter a shell command:")),
843 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
844 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
846 gtk_box_pack_start(GTK_BOX(hbox),
847 new_help_button(show_shell_help, NULL), FALSE, TRUE, 0);
849 entry = gtk_entry_new();
850 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, FALSE, TRUE, 0);
851 gtk_widget_grab_focus(entry);
852 g_object_set_data(G_OBJECT(dialog), "shell_command", entry);
853 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
855 /* If possible, fill in the entry box with the current command */
856 tmp = get_current_command(type);
857 if (tmp)
859 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
860 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
861 g_free(tmp);
863 else
865 gtk_entry_set_text(GTK_ENTRY(entry), " \"$@\"");
866 gtk_editable_set_position(GTK_EDITABLE(entry), 0);
869 gtk_dialog_add_button(dialog, GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL);
871 button = button_new_mixed(GTK_STOCK_OK, _("_Use Command"));
872 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
873 gtk_dialog_add_action_widget(dialog, button, GTK_RESPONSE_OK);
875 hbox = gtk_hbox_new(TRUE, 4);
876 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
878 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
880 g_signal_connect(dialog, "response",
881 G_CALLBACK(set_action_response), NULL);
883 gtk_widget_show_all(GTK_WIDGET(dialog));
886 /* path is an entry in Choices. If it's a symlink or a very small executable
887 * then just get rid of it, otherwise confirm first. It it doesn't exist,
888 * do nothing.
890 * FALSE on error (abort operation).
892 static gboolean remove_handler_with_confirm(const guchar *path)
894 struct stat info;
896 if (lstat(path, &info) == 0)
898 /* A binding already exists... */
899 if (S_ISREG(info.st_mode) && info.st_size > 256)
901 if (!confirm(_("A run action already exists and is "
902 "quite a big program - are you sure "
903 "you want to delete it?"),
904 GTK_STOCK_DELETE, NULL))
906 return FALSE;
910 if (unlink(path))
912 report_error(_("Can't remove %s: %s"),
913 path, g_strerror(errno));
914 return FALSE;
918 return TRUE;
921 /* The user wants to set a new default action for files of this type (or just
922 * clear the action). Removes the current binding if possible and returns the
923 * path to save the new one to. NULL means cancel. g_free() the result.
925 static char *get_action_save_path(GtkWidget *dialog)
927 guchar *path = NULL;
928 guchar *type_name = NULL;
929 MIME_type *type;
930 Radios *radios;
932 g_return_val_if_fail(dialog != NULL, NULL);
934 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
935 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
937 g_return_val_if_fail(radios != NULL && type != NULL, NULL);
939 if (radios_get_value(radios) == SET_MEDIA)
940 type_name = g_strdup(type->media_type);
941 else
942 type_name = g_strconcat(type->media_type, "_",
943 type->subtype, NULL);
945 path = choices_find_xdg_path_save("", PROJECT, SITE, FALSE);
946 if (!path)
948 report_error(
949 _("Choices saving is disabled by CHOICESPATH variable"));
950 goto out;
952 g_free(path);
954 path = choices_find_xdg_path_save(type_name, "MIME-types", SITE, TRUE);
956 if (!remove_handler_with_confirm(path))
957 null_g_free(&path);
958 out:
959 g_free(type_name);
960 return path;
963 MIME_type *mime_type_from_base_type(int base_type)
965 switch (base_type)
967 case TYPE_FILE:
968 return text_plain;
969 case TYPE_DIRECTORY:
970 return inode_directory;
971 case TYPE_PIPE:
972 return inode_pipe;
973 case TYPE_SOCKET:
974 return inode_socket;
975 case TYPE_BLOCK_DEVICE:
976 return inode_block_dev;
977 case TYPE_CHAR_DEVICE:
978 return inode_char_dev;
979 case TYPE_DOOR:
980 return inode_door;
982 return inode_unknown;
985 /* Takes the st_mode field from stat() and returns the base type.
986 * Should not be a symlink.
988 int mode_to_base_type(int st_mode)
990 if (S_ISREG(st_mode))
991 return TYPE_FILE;
992 else if (S_ISDIR(st_mode))
993 return TYPE_DIRECTORY;
994 else if (S_ISBLK(st_mode))
995 return TYPE_BLOCK_DEVICE;
996 else if (S_ISCHR(st_mode))
997 return TYPE_CHAR_DEVICE;
998 else if (S_ISFIFO(st_mode))
999 return TYPE_PIPE;
1000 else if (S_ISSOCK(st_mode))
1001 return TYPE_SOCKET;
1002 else if (S_ISDOOR(st_mode))
1003 return TYPE_DOOR;
1005 return TYPE_ERROR;
1008 /* Returns TRUE is this is something that is run by looking up its type
1009 * in MIME-types and, hence, can have its run action set.
1011 gboolean can_set_run_action(DirItem *item)
1013 g_return_val_if_fail(item != NULL, FALSE);
1015 return item->base_type == TYPE_FILE && !EXECUTABLE_FILE(item);
1018 /* Parse file type colours and allocate/free them as necessary */
1019 static void alloc_type_colours(void)
1021 gboolean success[NUM_TYPE_COLOURS];
1022 int change_count = 0; /* No. needing realloc */
1023 int i;
1024 static gboolean allocated = FALSE;
1026 /* Parse colours */
1027 for (i = 0; i < NUM_TYPE_COLOURS; i++)
1029 GdkColor *c = &type_colours[i];
1030 gushort r = c->red;
1031 gushort g = c->green;
1032 gushort b = c->blue;
1034 gdk_color_parse(o_type_colours[i].value, &type_colours[i]);
1036 if (allocated && (c->red != r || c->green != g || c->blue != b))
1037 change_count++;
1040 /* Free colours if they were previously allocated and
1041 * have changed or become unneeded.
1043 if (allocated && (change_count || !o_display_colour_types.int_value))
1045 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1046 type_colours, NUM_TYPE_COLOURS);
1047 allocated = FALSE;
1050 /* Allocate colours, unless they are still allocated (=> they didn't
1051 * change) or we don't want them anymore.
1052 * XXX: what should be done if allocation fails?
1054 if (!allocated && o_display_colour_types.int_value)
1056 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1057 type_colours, NUM_TYPE_COLOURS,
1058 FALSE, TRUE, success);
1059 allocated = TRUE;
1063 static void expire_timer(gpointer key, gpointer value, gpointer data)
1065 MIME_type *type = value;
1067 type->image_time = 0;
1070 static void options_changed(void)
1072 alloc_type_colours();
1073 if (o_icon_theme.has_changed)
1075 set_icon_theme();
1076 g_hash_table_foreach(type_hash, expire_timer, NULL);
1077 full_refresh();
1081 /* Return a pointer to a (static) colour for this item. If colouring is
1082 * off, returns normal.
1084 GdkColor *type_get_colour(DirItem *item, GdkColor *normal)
1086 int type = item->base_type;
1088 if (!o_display_colour_types.int_value)
1089 return normal;
1091 if (EXECUTABLE_FILE(item))
1092 type = TYPE_EXEC;
1093 else if (item->flags & ITEM_FLAG_APPDIR)
1094 type = TYPE_APPDIR;
1096 g_return_val_if_fail(type >= 0 && type < NUM_TYPE_COLOURS, normal);
1098 return &type_colours[type];
1101 static char **get_xdg_data_dirs(int *n_dirs)
1103 const char *env;
1104 char **dirs;
1105 int i, n;
1107 env = getenv("XDG_DATA_DIRS");
1108 if (!env)
1109 env = "/usr/local/share/:/usr/share/";
1110 dirs = g_strsplit(env, ":", 0);
1111 g_return_val_if_fail(dirs != NULL, NULL);
1112 for (n = 0; dirs[n]; n++)
1114 for (i = n; i > 0; i--)
1115 dirs[i] = dirs[i - 1];
1116 env = getenv("XDG_DATA_HOME");
1117 if (env)
1118 dirs[0] = g_strdup(env);
1119 else
1120 dirs[0] = g_build_filename(g_get_home_dir(), ".local",
1121 "share", NULL);
1122 *n_dirs = n + 1;
1123 return dirs;
1126 /* Try to fill in 'type->comment' from this document */
1127 static void get_comment(MIME_type *type, const guchar *path)
1129 xmlNode *node;
1130 XMLwrapper *doc;
1132 doc = xml_cache_load(path);
1133 if (!doc)
1134 return;
1136 node = xml_get_section(doc, TYPE_NS, "comment");
1138 if (node)
1140 char *val;
1141 g_return_if_fail(type->comment == NULL);
1142 val= xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
1143 type->comment = g_strdup(val);
1144 xmlFree(val);
1147 g_object_unref(doc);
1150 /* Fill in the comment field for this MIME type */
1151 static void find_comment(MIME_type *type)
1153 char **dirs;
1154 int i, n_dirs = 0;
1156 if (type->comment)
1158 g_free(type->comment);
1159 type->comment = NULL;
1162 dirs = get_xdg_data_dirs(&n_dirs);
1163 g_return_if_fail(dirs != NULL);
1165 for (i = 0; i < n_dirs; i++)
1167 guchar *path;
1169 path = g_strdup_printf("%s/mime/%s/%s.xml", dirs[i],
1170 type->media_type, type->subtype);
1171 get_comment(type, path);
1172 g_free(path);
1173 if (type->comment)
1174 break;
1177 if (!type->comment)
1178 type->comment = g_strdup_printf("%s/%s", type->media_type,
1179 type->subtype);
1181 for (i = 0; i < n_dirs; i++)
1182 g_free(dirs[i]);
1183 g_free(dirs);
1186 const char *mime_type_comment(MIME_type *type)
1188 if (!type->comment)
1189 find_comment(type);
1191 return type->comment;
1194 static void set_icon_theme(void)
1196 GtkIconInfo *info;
1197 char *icon_home;
1198 const char *theme_name = o_icon_theme.value;
1200 if (!theme_name || !*theme_name)
1201 theme_name = "ROX";
1203 while (1)
1205 gtk_icon_theme_set_custom_theme(icon_theme, theme_name);
1206 info = gtk_icon_theme_lookup_icon(icon_theme,
1207 "mime-application:postscript",
1208 ICON_HEIGHT, 0);
1209 if (!info)
1211 info = gtk_icon_theme_lookup_icon(icon_theme,
1212 "gnome-mime-application-postscript",
1213 ICON_HEIGHT, 0);
1215 if (info)
1217 gtk_icon_info_free(info);
1218 return;
1221 if (strcmp(theme_name, "ROX") == 0)
1222 break;
1224 delayed_error(_("Icon theme '%s' does not contain MIME icons. "
1225 "Using ROX default theme instead."),
1226 theme_name);
1228 theme_name = "ROX";
1231 icon_home = g_build_filename(home_dir, ".icons", NULL);
1232 if (!file_exists(icon_home))
1233 mkdir(icon_home, 0755);
1234 g_free(icon_home);
1236 icon_home = g_build_filename(home_dir, ".icons", "ROX", NULL);
1237 if (symlink(make_path(app_dir, "ROX"), icon_home))
1239 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1240 "(this may mean that the ROX theme already exists there, but "
1241 "the 'mime-application:postscript' icon couldn't be loaded for "
1242 "some reason, or %s is a link to an invalid directory; try "
1243 "deleting it)"), icon_home, g_strerror(errno), icon_home);
1244 open_to_show(icon_home);
1246 g_free(icon_home);
1248 gtk_icon_theme_rescan_if_needed(icon_theme);
1251 static guchar *read_theme(Option *option)
1253 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1254 GtkLabel *item;
1256 item = GTK_LABEL(GTK_BIN(om)->child);
1258 g_return_val_if_fail(item != NULL, g_strdup("ROX"));
1260 return g_strdup(gtk_label_get_text(item));
1263 static void update_theme(Option *option)
1265 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1266 GtkWidget *menu;
1267 GList *kids, *next;
1268 int i = 0;
1270 menu = gtk_option_menu_get_menu(om);
1272 kids = gtk_container_get_children(GTK_CONTAINER(menu));
1273 for (next = kids; next; next = next->next, i++)
1275 GtkLabel *item = GTK_LABEL(GTK_BIN(next->data)->child);
1276 const gchar *label;
1278 /* The label actually moves from the menu!! */
1279 if (!item)
1280 item = GTK_LABEL(GTK_BIN(om)->child);
1282 label = gtk_label_get_text(item);
1284 g_return_if_fail(label != NULL);
1286 if (strcmp(label, option->value) == 0)
1287 break;
1289 g_list_free(kids);
1291 if (next)
1292 gtk_option_menu_set_history(om, i);
1293 else
1294 g_warning("Theme '%s' not found", option->value);
1297 static void add_themes_from_dir(GPtrArray *names, const char *dir)
1299 GPtrArray *list;
1300 int i;
1302 if (access(dir, F_OK) != 0)
1303 return;
1305 list = list_dir(dir);
1306 g_return_if_fail(list != NULL);
1308 for (i = 0; i < list->len; i++)
1310 char *index_path;
1312 index_path = g_build_filename(dir, list->pdata[i],
1313 "index.theme", NULL);
1315 if (access(index_path, F_OK) == 0)
1316 g_ptr_array_add(names, list->pdata[i]);
1317 else
1318 g_free(list->pdata[i]);
1320 g_free(index_path);
1323 g_ptr_array_free(list, TRUE);
1326 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label)
1328 GtkWidget *button, *menu, *hbox;
1329 GPtrArray *names;
1330 gchar **theme_dirs = NULL;
1331 gint n_dirs = 0;
1332 int i;
1334 g_return_val_if_fail(option != NULL, NULL);
1335 g_return_val_if_fail(label != NULL, NULL);
1337 hbox = gtk_hbox_new(FALSE, 4);
1339 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1340 FALSE, TRUE, 0);
1342 button = gtk_option_menu_new();
1343 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1345 menu = gtk_menu_new();
1346 gtk_option_menu_set_menu(GTK_OPTION_MENU(button), menu);
1348 gtk_icon_theme_get_search_path(icon_theme, &theme_dirs, &n_dirs);
1349 names = g_ptr_array_new();
1350 for (i = 0; i < n_dirs; i++)
1351 add_themes_from_dir(names, theme_dirs[i]);
1352 g_strfreev(theme_dirs);
1354 g_ptr_array_sort(names, strcmp2);
1356 for (i = 0; i < names->len; i++)
1358 GtkWidget *item;
1359 char *name = names->pdata[i];
1361 item = gtk_menu_item_new_with_label(name);
1362 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1363 gtk_widget_show_all(item);
1365 g_free(name);
1368 g_ptr_array_free(names, TRUE);
1370 option->update_widget = update_theme;
1371 option->read_widget = read_theme;
1372 option->widget = button;
1374 gtk_signal_connect_object(GTK_OBJECT(button), "changed",
1375 GTK_SIGNAL_FUNC(option_check_widget),
1376 (GtkObject *) option);
1378 return g_list_append(NULL, hbox);