4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
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 */
31 #include <sys/param.h>
33 #include <sys/types.h>
37 # include <libgnomevfs/gnome-vfs.h>
38 # include <libgnomevfs/gnome-vfs-mime.h>
39 # include <libgnomevfs/gnome-vfs-mime-handlers.h>
40 # include <libgnomevfs/gnome-vfs-application-registry.h>
50 #include "gui_support.h"
58 #include "action.h" /* (for action_chmod) */
64 #define TYPE_NS "http://www.freedesktop.org/standards/shared-mime-info"
65 enum {SET_MEDIA
, SET_TYPE
};
67 /* Colours for file types (same order as base types) */
68 static gchar
*opt_type_colours
[][2] = {
69 {"display_err_colour", "#ff0000"},
70 {"display_unkn_colour", "#000000"},
71 {"display_dir_colour", "#000080"},
72 {"display_pipe_colour", "#444444"},
73 {"display_sock_colour", "#ff00ff"},
74 {"display_file_colour", "#000000"},
75 {"display_cdev_colour", "#000000"},
76 {"display_bdev_colour", "#000000"},
77 {"display_door_colour", "#ff00ff"},
78 {"display_exec_colour", "#006000"},
79 {"display_adir_colour", "#006000"}
81 #define NUM_TYPE_COLOURS\
82 (sizeof(opt_type_colours) / sizeof(opt_type_colours[0]))
84 /* Parsed colours for file types */
85 static Option o_type_colours
[NUM_TYPE_COLOURS
];
86 static GdkColor type_colours
[NUM_TYPE_COLOURS
];
88 /* Static prototypes */
89 static void alloc_type_colours(void);
90 static void options_changed(void);
91 static char *get_action_save_path(GtkWidget
*dialog
);
92 static MIME_type
*get_mime_type(const gchar
*type_name
, gboolean can_create
);
93 static gboolean
remove_handler_with_confirm(const guchar
*path
);
94 static void set_icon_theme(void);
95 static GList
*build_icon_theme(Option
*option
, xmlNode
*node
, guchar
*label
);
97 /* Hash of all allocated MIME types, indexed by "media/subtype".
98 * MIME_type structs are never freed; this table prevents memory leaks
99 * when rereading the config files.
101 static GHashTable
*type_hash
= NULL
;
103 /* Most things on Unix are text files, so this is the default type */
104 MIME_type
*text_plain
;
105 MIME_type
*inode_directory
;
106 MIME_type
*inode_mountpoint
;
107 MIME_type
*inode_pipe
;
108 MIME_type
*inode_socket
;
109 MIME_type
*inode_block_dev
;
110 MIME_type
*inode_char_dev
;
111 MIME_type
*application_executable
;
112 MIME_type
*application_octet_stream
;
113 MIME_type
*application_x_shellscript
;
114 MIME_type
*inode_unknown
;
115 MIME_type
*inode_door
;
117 static Option o_display_colour_types
;
118 static Option o_icon_theme
;
120 static GtkIconTheme
*icon_theme
= NULL
;
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 inode_unknown
= get_mime_type("inode/unknown", TRUE
);
141 inode_door
= get_mime_type("inode/door", TRUE
);
143 option_add_string(&o_icon_theme
, "icon_theme", "ROX");
144 option_add_int(&o_display_colour_types
, "display_colour_types", TRUE
);
145 option_register_widget("icon-theme-chooser", build_icon_theme
);
147 for (i
= 0; i
< NUM_TYPE_COLOURS
; i
++)
148 option_add_string(&o_type_colours
[i
],
149 opt_type_colours
[i
][0],
150 opt_type_colours
[i
][1]);
151 alloc_type_colours();
155 option_add_notify(options_changed
);
158 /* Read-load all the glob patterns.
159 * Note: calls filer_update_all.
161 void reread_mime_files(void)
163 gtk_icon_theme_rescan_if_needed(icon_theme
);
168 /* Returns the MIME_type structure for the given type name. It is looked
169 * up in type_hash and returned if found. If not found (and can_create is
170 * TRUE) then a new MIME_type is made, added to type_hash and returned.
171 * NULL is returned if type_name is not in type_hash and can_create is
172 * FALSE, or if type_name does not contain a '/' character.
174 static MIME_type
*get_mime_type(const gchar
*type_name
, gboolean can_create
)
179 mtype
= g_hash_table_lookup(type_hash
, type_name
);
180 if (mtype
|| !can_create
)
183 slash
= strchr(type_name
, '/');
184 g_return_val_if_fail(slash
!= NULL
, NULL
); /* XXX: Report nicely */
186 mtype
= g_new(MIME_type
, 1);
187 mtype
->media_type
= g_strndup(type_name
, slash
- type_name
);
188 mtype
->subtype
= g_strdup(slash
+ 1);
190 mtype
->comment
= NULL
;
192 mtype
->executable
= xdg_mime_mime_type_subclass(type_name
,
193 "application/x-executable");
195 g_hash_table_insert(type_hash
, g_strdup(type_name
), mtype
);
200 const char *basetype_name(DirItem
*item
)
202 if (item
->flags
& ITEM_FLAG_SYMLINK
)
203 return _("Sym link");
204 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
205 return _("Mount point");
206 else if (item
->flags
& ITEM_FLAG_APPDIR
)
209 switch (item
->base_type
)
215 case TYPE_CHAR_DEVICE
:
216 return _("Char dev");
217 case TYPE_BLOCK_DEVICE
:
218 return _("Block dev");
230 static void append_names(gpointer key
, gpointer value
, gpointer udata
)
232 GList
**list
= (GList
**) udata
;
234 *list
= g_list_prepend(*list
, key
);
237 /* Return list of all mime type names. Caller must free the list
238 * but NOT the strings it contains (which are never freed).
240 GList
*mime_type_name_list(void)
244 g_hash_table_foreach(type_hash
, append_names
, &list
);
245 list
= g_list_sort(list
, (GCompareFunc
) strcmp
);
250 /* MIME-type guessing */
252 /* Get the type of this file - stats the file and uses that if
253 * possible. For regular or missing files, uses the pathname.
255 MIME_type
*type_get_type(const guchar
*path
)
258 MIME_type
*type
= NULL
;
260 item
= diritem_new("");
261 diritem_restat(path
, item
, NULL
);
262 if (item
->base_type
!= TYPE_ERROR
)
263 type
= item
->mime_type
;
269 type
= type_from_path(path
);
277 /* Returns a pointer to the MIME-type.
279 * Tries all enabled methods:
280 * - Look for extended attribute
281 * - If no attribute, check file name
282 * - If no name rule, check contents
284 * NULL if we can't think of anything.
286 MIME_type
*type_from_path(const char *path
)
288 MIME_type
*mime_type
= NULL
;
289 const char *type_name
;
291 /* Check for extended attribute first */
292 mime_type
= xtype_get(path
);
296 /* Try name and contents next */
297 type_name
= xdg_mime_get_mime_type_for_file(path
);
299 return get_mime_type(type_name
, TRUE
);
304 /* Returns the file/dir in Choices for handling this type.
305 * NULL if there isn't one. g_free() the result.
307 static char *handler_for(MIME_type
*type
)
312 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
313 open
= choices_find_xdg_path_load(type_name
, "MIME-types", SITE
);
317 open
= choices_find_xdg_path_load(type
->media_type
,
323 MIME_type
*mime_type_lookup(const char *type
)
325 return get_mime_type(type
, TRUE
);
328 /* Actions for types */
330 gboolean
type_open(const char *path
, MIME_type
*type
)
332 gchar
*argv
[] = {NULL
, NULL
, NULL
};
337 argv
[1] = (char *) path
;
339 open
= handler_for(type
);
343 if (stat(open
, &info
))
345 report_error("stat(%s): %s", open
, g_strerror(errno
));
350 if (info
.st_mode
& S_IWOTH
)
355 report_error(_("Executable '%s' is world-writeable! Refusing "
356 "to run. Please change the permissions now (this "
357 "problem may have been caused by a bug in earlier "
358 "versions of the filer).\n\n"
359 "Having (non-symlink) run actions world-writeable "
360 "means that other people who use your computer can "
361 "replace your run actions with malicious versions.\n\n"
362 "If you trust everyone who could write to these files "
363 "then you needn't worry. Otherwise, you should check, "
364 "or even just delete, all the existing run actions."),
366 choices_dir
= g_path_get_dirname(open
);
367 paths
= g_list_append(NULL
, choices_dir
);
368 action_chmod(paths
, TRUE
, _("go-w (Fix security problem)"));
375 if (S_ISDIR(info
.st_mode
))
376 argv
[0] = g_strconcat(open
, "/AppRun", NULL
);
380 retval
= rox_spawn(home_dir
, (const gchar
**) argv
) != 0;
390 /* Return the image for this type, loading it if needed.
391 * Places to check are: (eg type="text_plain", base="text")
392 * 1. <Choices>/MIME-icons/base_subtype
393 * 2. Icon theme 'mime-base:subtype'
394 * 3. Icon theme 'mime-base'
395 * 4. Unknown type icon.
397 * Note: You must g_object_unref() the image afterwards.
399 MaskedPixmap
*type_to_icon(MIME_type
*type
)
402 char *type_name
, *path
;
407 g_object_ref(im_unknown
);
412 /* Already got an image? */
415 /* Yes - don't recheck too often */
416 if (abs(now
- type
->image_time
) < 2)
418 g_object_ref(type
->image
);
421 g_object_unref(type
->image
);
425 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
,
427 path
= choices_find_xdg_path_load(type_name
, "MIME-icons", SITE
);
431 type
->image
= g_fscache_lookup(pixmap_cache
, path
);
438 type_name
= g_strconcat("mime-", type
->media_type
, ":",
439 type
->subtype
, NULL
);
440 full
= gtk_icon_theme_load_icon(icon_theme
, type_name
, HUGE_HEIGHT
,
445 /* Ugly hack... try for a GNOME icon */
446 type_name
= g_strconcat("gnome-mime-", type
->media_type
,
447 "-", type
->subtype
, NULL
);
448 full
= gtk_icon_theme_load_icon(icon_theme
,
450 HUGE_HEIGHT
, 0, NULL
);
455 /* Try for a media type */
456 type_name
= g_strconcat("mime-", type
->media_type
, NULL
);
457 full
= gtk_icon_theme_load_icon(icon_theme
,
459 HUGE_HEIGHT
, 0, NULL
);
464 type
->image
= masked_pixmap_new(full
);
465 g_object_unref(full
);
471 /* One ref from the type structure, one returned */
472 type
->image
= im_unknown
;
473 g_object_ref(im_unknown
);
476 type
->image_time
= now
;
478 g_object_ref(type
->image
);
482 GdkAtom
type_to_atom(MIME_type
*type
)
487 g_return_val_if_fail(type
!= NULL
, GDK_NONE
);
489 str
= g_strconcat(type
->media_type
, "/", type
->subtype
, NULL
);
490 retval
= gdk_atom_intern(str
, FALSE
);
496 static void show_shell_help(gpointer data
)
498 info_message(_("Enter a shell command which will load \"$@\" into "
499 "a suitable program. Eg:\n\n"
503 /* Called if the user clicks on the OK button. Returns FALSE if an error
504 * was displayed instead of performing the action.
506 static gboolean
set_shell_action(GtkWidget
*dialog
)
509 const guchar
*command
;
514 entry
= g_object_get_data(G_OBJECT(dialog
), "shell_command");
516 g_return_val_if_fail(entry
!= NULL
, FALSE
);
518 command
= gtk_entry_get_text(entry
);
520 if (!strchr(command
, '$'))
522 show_shell_help(NULL
);
526 path
= get_action_save_path(dialog
);
530 tmp
= g_strdup_printf("#! /bin/sh\nexec %s\n", command
);
533 fd
= open(path
, O_CREAT
| O_WRONLY
, 0755);
540 file
= fdopen(fd
, "w");
543 if (fwrite(tmp
, 1, len
, file
) < len
)
545 if (fclose(file
) && error
== 0)
553 report_error(g_strerror(error
));
558 gtk_widget_destroy(dialog
);
563 static void set_action_response(GtkWidget
*dialog
, gint response
, gpointer data
)
565 if (response
== GTK_RESPONSE_OK
)
566 if (!set_shell_action(dialog
))
568 gtk_widget_destroy(dialog
);
571 /* Return the path of the file in choices that handles this type and
573 * NULL if nothing is defined for it.
575 static guchar
*handler_for_radios(GObject
*dialog
)
580 radios
= g_object_get_data(G_OBJECT(dialog
), "rox-radios");
581 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
583 g_return_val_if_fail(radios
!= NULL
, NULL
);
584 g_return_val_if_fail(type
!= NULL
, NULL
);
586 switch (radios_get_value(radios
))
589 return choices_find_xdg_path_load(type
->media_type
,
593 gchar
*tmp
, *handler
;
594 tmp
= g_strconcat(type
->media_type
, "_",
595 type
->subtype
, NULL
);
596 handler
= choices_find_xdg_path_load(tmp
,
603 g_warning("Bad type");
608 /* (radios can be NULL if called from clear_run_action) */
609 static void run_action_update(Radios
*radios
, gpointer data
)
613 GObject
*dialog
= G_OBJECT(data
);
615 drop_box
= g_object_get_data(dialog
, "rox-dropbox");
617 g_return_if_fail(drop_box
!= NULL
);
619 handler
= handler_for_radios(dialog
);
625 handler
= readlink_dup(old
);
632 drop_box_set_path(DROP_BOX(drop_box
), handler
);
636 static void clear_run_action(GtkWidget
*drop_box
, GtkWidget
*dialog
)
640 handler
= handler_for_radios(G_OBJECT(dialog
));
643 remove_handler_with_confirm(handler
);
645 run_action_update(NULL
, dialog
);
648 /* Called when a URI list is dropped onto the box in the Set Run Action
649 * dialog. Make sure it's an application, and make that the default
652 static void drag_app_dropped(GtkWidget
*drop_box
,
658 item
= diritem_new("");
659 diritem_restat(app
, item
, NULL
);
660 if (item
->flags
& ITEM_FLAG_APPDIR
|| EXECUTABLE_FILE(item
))
664 path
= get_action_save_path(dialog
);
668 if (symlink(app
, path
))
669 delayed_error("symlink: %s",
672 destroy_on_idle(dialog
);
679 _("This is not a program! Give me an application "
685 /* Find the current command which is used to run files of this type.
686 * Returns NULL on failure. g_free() the result.
688 static guchar
*get_current_command(MIME_type
*type
)
691 char *handler
, *nl
, *data
= NULL
;
693 guchar
*command
= NULL
;
695 handler
= handler_for(type
);
698 return NULL
; /* No current handler */
700 if (stat(handler
, &info
))
701 goto out
; /* Can't stat */
703 if ((!S_ISREG(info
.st_mode
)) || info
.st_size
> 256)
704 goto out
; /* Only use small regular files */
706 if (!load_file(handler
, &data
, &len
))
707 goto out
; /* Didn't load OK */
709 if (strncmp(data
, "#! /bin/sh\nexec ", 16) != 0)
710 goto out
; /* Not one of ours */
712 nl
= strchr(data
+ 16, '\n');
714 goto out
; /* No newline! */
716 command
= g_strndup(data
+ 16, nl
- data
- 16);
723 /* Find the current command which is used to run files of this type,
724 * and return a textual description of it.
725 * Only call for non-executable files.
726 * g_free() the result.
728 gchar
*describe_current_command(MIME_type
*type
)
735 g_return_val_if_fail(type
!= NULL
, NULL
);
737 handler
= handler_for(type
);
740 return g_strdup(_("No run action defined"));
742 target
= readlink_dup(handler
);
745 /* Cope with relative paths (shouldn't normally be needed) */
747 if (target
[0] == '/')
756 dir
= g_path_get_dirname(handler
);
758 handler
= g_strconcat(dir
, "/", target
, NULL
);
764 if (mc_stat(handler
, &info
) !=0 )
766 desc
= g_strdup_printf(_("Error in handler %s: %s"), handler
,
771 if (S_ISDIR(info
.st_mode
))
774 uid_t dir_uid
= info
.st_uid
;
776 tmp
= make_path(handler
, "AppRun");
778 if (mc_lstat(tmp
, &info
) != 0 || info
.st_uid
!= dir_uid
779 || !(info
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)))
780 desc
= g_strdup_printf(
781 _("Invalid application %s (bad AppRun)"),
783 /* Else, just report handler... */
788 /* It's not an application directory, and it's not a symlink... */
790 if (access(handler
, X_OK
) != 0)
792 desc
= g_strdup_printf(_("Non-executable %s"), handler
);
796 desc
= get_current_command(type
);
806 /* Display a dialog box allowing the user to set the default run action
809 void type_set_handler_dialog(MIME_type
*type
)
813 GtkWidget
*frame
, *entry
, *label
, *button
;
817 g_return_if_fail(type
!= NULL
);
819 dialog
= GTK_DIALOG(gtk_dialog_new());
820 gtk_dialog_set_has_separator(dialog
, FALSE
);
821 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
823 g_object_set_data(G_OBJECT(dialog
), "mime_type", type
);
825 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set run action"));
827 radios
= radios_new(run_action_update
, dialog
);
828 g_object_set_data(G_OBJECT(dialog
), "rox-radios", radios
);
831 _("If a handler for the specific type isn't set up, "
832 "use this as the default."), SET_MEDIA
,
833 _("Set default for all `%s/<anything>'"),
837 _("Use this application for all files with this MIME "
839 _("Only for the type `%s' (%s/%s)"),
840 mime_type_comment(type
),
841 type
->media_type
, type
->subtype
);
843 radios_set_value(radios
, SET_TYPE
);
845 frame
= drop_box_new(_("Drop a suitable application here"));
847 g_object_set_data(G_OBJECT(dialog
), "rox-dropbox", frame
);
849 radios_pack(radios
, GTK_BOX(dialog
->vbox
));
850 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), frame
, TRUE
, TRUE
, 0);
852 g_signal_connect(frame
, "path_dropped",
853 G_CALLBACK(drag_app_dropped
), dialog
);
854 g_signal_connect(frame
, "clear",
855 G_CALLBACK(clear_run_action
), dialog
);
857 hbox
= gtk_hbox_new(FALSE
, 4);
858 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 4);
859 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
860 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
862 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
864 hbox
= gtk_hbox_new(FALSE
, 4);
865 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 0);
867 label
= gtk_label_new(_("Enter a shell command:")),
868 gtk_misc_set_alignment(GTK_MISC(label
), 0, .5);
869 gtk_box_pack_start(GTK_BOX(hbox
), label
, TRUE
, TRUE
, 4);
871 gtk_box_pack_start(GTK_BOX(hbox
),
872 new_help_button(show_shell_help
, NULL
), FALSE
, TRUE
, 0);
874 entry
= gtk_entry_new();
875 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), entry
, FALSE
, TRUE
, 0);
876 gtk_widget_grab_focus(entry
);
877 g_object_set_data(G_OBJECT(dialog
), "shell_command", entry
);
878 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
880 /* If possible, fill in the entry box with the current command */
881 tmp
= get_current_command(type
);
884 gtk_entry_set_text(GTK_ENTRY(entry
), tmp
);
885 gtk_editable_set_position(GTK_EDITABLE(entry
), -1);
890 gtk_entry_set_text(GTK_ENTRY(entry
), " \"$@\"");
891 gtk_editable_set_position(GTK_EDITABLE(entry
), 0);
894 gtk_dialog_add_button(dialog
, GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
);
896 button
= button_new_mixed(GTK_STOCK_OK
, _("_Use Command"));
897 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
898 gtk_dialog_add_action_widget(dialog
, button
, GTK_RESPONSE_OK
);
900 hbox
= gtk_hbox_new(TRUE
, 4);
901 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 0);
903 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
905 g_signal_connect(dialog
, "response",
906 G_CALLBACK(set_action_response
), NULL
);
908 gtk_widget_show_all(GTK_WIDGET(dialog
));
911 /* path is an entry in Choices. If it's a symlink or a very small executable
912 * then just get rid of it, otherwise confirm first. It it doesn't exist,
915 * FALSE on error (abort operation).
917 static gboolean
remove_handler_with_confirm(const guchar
*path
)
921 if (lstat(path
, &info
) == 0)
923 /* A binding already exists... */
924 if (S_ISREG(info
.st_mode
) && info
.st_size
> 256)
926 if (!confirm(_("A run action already exists and is "
927 "quite a big program - are you sure "
928 "you want to delete it?"),
929 GTK_STOCK_DELETE
, NULL
))
937 report_error(_("Can't remove %s: %s"),
938 path
, g_strerror(errno
));
946 /* The user wants to set a new default action for files of this type (or just
947 * clear the action). Removes the current binding if possible and returns the
948 * path to save the new one to. NULL means cancel. g_free() the result.
950 static char *get_action_save_path(GtkWidget
*dialog
)
953 guchar
*type_name
= NULL
;
957 g_return_val_if_fail(dialog
!= NULL
, NULL
);
959 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
960 radios
= g_object_get_data(G_OBJECT(dialog
), "rox-radios");
962 g_return_val_if_fail(radios
!= NULL
&& type
!= NULL
, NULL
);
964 if (radios_get_value(radios
) == SET_MEDIA
)
965 type_name
= g_strdup(type
->media_type
);
967 type_name
= g_strconcat(type
->media_type
, "_",
968 type
->subtype
, NULL
);
970 path
= choices_find_xdg_path_save("", PROJECT
, SITE
, FALSE
);
974 _("Choices saving is disabled by CHOICESPATH variable"));
979 path
= choices_find_xdg_path_save(type_name
, "MIME-types", SITE
, TRUE
);
981 if (!remove_handler_with_confirm(path
))
988 MIME_type
*mime_type_from_base_type(int base_type
)
995 return inode_directory
;
1000 case TYPE_BLOCK_DEVICE
:
1001 return inode_block_dev
;
1002 case TYPE_CHAR_DEVICE
:
1003 return inode_char_dev
;
1007 return inode_unknown
;
1010 /* Takes the st_mode field from stat() and returns the base type.
1011 * Should not be a symlink.
1013 int mode_to_base_type(int st_mode
)
1015 if (S_ISREG(st_mode
))
1017 else if (S_ISDIR(st_mode
))
1018 return TYPE_DIRECTORY
;
1019 else if (S_ISBLK(st_mode
))
1020 return TYPE_BLOCK_DEVICE
;
1021 else if (S_ISCHR(st_mode
))
1022 return TYPE_CHAR_DEVICE
;
1023 else if (S_ISFIFO(st_mode
))
1025 else if (S_ISSOCK(st_mode
))
1027 else if (S_ISDOOR(st_mode
))
1033 /* Returns TRUE is this is something that is run by looking up its type
1034 * in MIME-types and, hence, can have its run action set.
1036 gboolean
can_set_run_action(DirItem
*item
)
1038 g_return_val_if_fail(item
!= NULL
, FALSE
);
1040 return item
->base_type
== TYPE_FILE
&& !EXECUTABLE_FILE(item
);
1043 /* Parse file type colours and allocate/free them as necessary */
1044 static void alloc_type_colours(void)
1046 gboolean success
[NUM_TYPE_COLOURS
];
1047 int change_count
= 0; /* No. needing realloc */
1049 static gboolean allocated
= FALSE
;
1052 for (i
= 0; i
< NUM_TYPE_COLOURS
; i
++)
1054 GdkColor
*c
= &type_colours
[i
];
1056 gushort g
= c
->green
;
1057 gushort b
= c
->blue
;
1059 gdk_color_parse(o_type_colours
[i
].value
, &type_colours
[i
]);
1061 if (allocated
&& (c
->red
!= r
|| c
->green
!= g
|| c
->blue
!= b
))
1065 /* Free colours if they were previously allocated and
1066 * have changed or become unneeded.
1068 if (allocated
&& (change_count
|| !o_display_colour_types
.int_value
))
1070 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1071 type_colours
, NUM_TYPE_COLOURS
);
1075 /* Allocate colours, unless they are still allocated (=> they didn't
1076 * change) or we don't want them anymore.
1077 * XXX: what should be done if allocation fails?
1079 if (!allocated
&& o_display_colour_types
.int_value
)
1081 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1082 type_colours
, NUM_TYPE_COLOURS
,
1083 FALSE
, TRUE
, success
);
1088 static void expire_timer(gpointer key
, gpointer value
, gpointer data
)
1090 MIME_type
*type
= value
;
1092 type
->image_time
= 0;
1095 static void options_changed(void)
1097 alloc_type_colours();
1098 if (o_icon_theme
.has_changed
)
1101 g_hash_table_foreach(type_hash
, expire_timer
, NULL
);
1106 /* Return a pointer to a (static) colour for this item. If colouring is
1107 * off, returns normal.
1109 GdkColor
*type_get_colour(DirItem
*item
, GdkColor
*normal
)
1111 int type
= item
->base_type
;
1113 if (!o_display_colour_types
.int_value
)
1116 if (EXECUTABLE_FILE(item
))
1118 else if (item
->flags
& ITEM_FLAG_APPDIR
)
1121 g_return_val_if_fail(type
>= 0 && type
< NUM_TYPE_COLOURS
, normal
);
1123 return &type_colours
[type
];
1126 static char **get_xdg_data_dirs(int *n_dirs
)
1132 env
= getenv("XDG_DATA_DIRS");
1134 env
= "/usr/local/share/:/usr/share/";
1135 dirs
= g_strsplit(env
, ":", 0);
1136 g_return_val_if_fail(dirs
!= NULL
, NULL
);
1137 for (n
= 0; dirs
[n
]; n
++)
1139 for (i
= n
; i
> 0; i
--)
1140 dirs
[i
] = dirs
[i
- 1];
1141 env
= getenv("XDG_DATA_HOME");
1143 dirs
[0] = g_strdup(env
);
1145 dirs
[0] = g_build_filename(g_get_home_dir(), ".local",
1151 /* Try to fill in 'type->comment' from this document */
1152 static void get_comment(MIME_type
*type
, const guchar
*path
)
1157 doc
= xml_cache_load(path
);
1161 node
= xml_get_section(doc
, TYPE_NS
, "comment");
1166 g_return_if_fail(type
->comment
== NULL
);
1167 val
= xmlNodeListGetString(node
->doc
, node
->xmlChildrenNode
, 1);
1168 type
->comment
= g_strdup(val
);
1172 g_object_unref(doc
);
1175 /* Fill in the comment field for this MIME type */
1176 static void find_comment(MIME_type
*type
)
1183 g_free(type
->comment
);
1184 type
->comment
= NULL
;
1187 dirs
= get_xdg_data_dirs(&n_dirs
);
1188 g_return_if_fail(dirs
!= NULL
);
1190 for (i
= 0; i
< n_dirs
; i
++)
1194 path
= g_strdup_printf("%s/mime/%s/%s.xml", dirs
[i
],
1195 type
->media_type
, type
->subtype
);
1196 get_comment(type
, path
);
1203 type
->comment
= g_strdup_printf("%s/%s", type
->media_type
,
1206 for (i
= 0; i
< n_dirs
; i
++)
1211 const char *mime_type_comment(MIME_type
*type
)
1216 return type
->comment
;
1219 static void set_icon_theme(void)
1223 const char *theme_name
= o_icon_theme
.value
;
1225 if (!theme_name
|| !*theme_name
)
1230 gtk_icon_theme_set_custom_theme(icon_theme
, theme_name
);
1231 info
= gtk_icon_theme_lookup_icon(icon_theme
,
1232 "mime-application:postscript",
1236 info
= gtk_icon_theme_lookup_icon(icon_theme
,
1237 "gnome-mime-application-postscript",
1242 gtk_icon_info_free(info
);
1246 if (strcmp(theme_name
, "ROX") == 0)
1249 delayed_error(_("Icon theme '%s' does not contain MIME icons. "
1250 "Using ROX default theme instead."),
1256 icon_home
= g_build_filename(home_dir
, ".icons", NULL
);
1257 if (!file_exists(icon_home
))
1258 mkdir(icon_home
, 0755);
1261 icon_home
= g_build_filename(home_dir
, ".icons", "ROX", NULL
);
1262 if (symlink(make_path(app_dir
, "ROX"), icon_home
))
1263 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1264 "(this may mean that the ROX theme already exists there, but "
1265 "the 'mime-application:postscript' icon couldn't be loaded for "
1266 "some reason)"), icon_home
, g_strerror(errno
));
1269 gtk_icon_theme_rescan_if_needed(icon_theme
);
1272 static guchar
*read_theme(Option
*option
)
1274 GtkOptionMenu
*om
= GTK_OPTION_MENU(option
->widget
);
1277 item
= GTK_LABEL(GTK_BIN(om
)->child
);
1279 g_return_val_if_fail(item
!= NULL
, g_strdup("ROX"));
1281 return g_strdup(gtk_label_get_text(item
));
1284 static void update_theme(Option
*option
)
1286 GtkOptionMenu
*om
= GTK_OPTION_MENU(option
->widget
);
1291 menu
= gtk_option_menu_get_menu(om
);
1293 kids
= gtk_container_get_children(GTK_CONTAINER(menu
));
1294 for (next
= kids
; next
; next
= next
->next
, i
++)
1296 GtkLabel
*item
= GTK_LABEL(GTK_BIN(next
->data
)->child
);
1299 /* The label actually moves from the menu!! */
1301 item
= GTK_LABEL(GTK_BIN(om
)->child
);
1303 label
= gtk_label_get_text(item
);
1305 g_return_if_fail(label
!= NULL
);
1307 if (strcmp(label
, option
->value
) == 0)
1313 gtk_option_menu_set_history(om
, i
);
1315 g_warning("Theme '%s' not found", option
->value
);
1318 static void add_themes_from_dir(GPtrArray
*names
, const char *dir
)
1323 if (access(dir
, F_OK
) != 0)
1326 list
= list_dir(dir
);
1327 g_return_if_fail(list
!= NULL
);
1329 for (i
= 0; i
< list
->len
; i
++)
1333 index_path
= g_build_filename(dir
, list
->pdata
[i
],
1334 "index.theme", NULL
);
1336 if (access(index_path
, F_OK
) == 0)
1337 g_ptr_array_add(names
, list
->pdata
[i
]);
1339 g_free(list
->pdata
[i
]);
1344 g_ptr_array_free(list
, TRUE
);
1347 static GList
*build_icon_theme(Option
*option
, xmlNode
*node
, guchar
*label
)
1349 GtkWidget
*button
, *menu
, *hbox
;
1351 gchar
**theme_dirs
= NULL
;
1355 g_return_val_if_fail(option
!= NULL
, NULL
);
1356 g_return_val_if_fail(label
!= NULL
, NULL
);
1358 hbox
= gtk_hbox_new(FALSE
, 4);
1360 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_(label
)),
1363 button
= gtk_option_menu_new();
1364 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
1366 menu
= gtk_menu_new();
1367 gtk_option_menu_set_menu(GTK_OPTION_MENU(button
), menu
);
1369 gtk_icon_theme_get_search_path(icon_theme
, &theme_dirs
, &n_dirs
);
1370 names
= g_ptr_array_new();
1371 for (i
= 0; i
< n_dirs
; i
++)
1372 add_themes_from_dir(names
, theme_dirs
[i
]);
1373 g_strfreev(theme_dirs
);
1375 g_ptr_array_sort(names
, strcmp2
);
1377 for (i
= 0; i
< names
->len
; i
++)
1380 char *name
= names
->pdata
[i
];
1382 item
= gtk_menu_item_new_with_label(name
);
1383 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
1384 gtk_widget_show_all(item
);
1389 g_ptr_array_free(names
, TRUE
);
1391 option
->update_widget
= update_theme
;
1392 option
->read_widget
= read_theme
;
1393 option
->widget
= button
;
1395 gtk_signal_connect_object(GTK_OBJECT(button
), "changed",
1396 GTK_SIGNAL_FUNC(option_check_widget
),
1397 (GtkObject
*) option
);
1399 return g_list_append(NULL
, hbox
);