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) */
65 #define TYPE_NS "http://www.freedesktop.org/standards/shared-mime-info"
66 enum {SET_MEDIA
, SET_TYPE
};
68 /* Colours for file types (same order as base types) */
69 static gchar
*opt_type_colours
[][2] = {
70 {"display_err_colour", "#ff0000"},
71 {"display_unkn_colour", "#000000"},
72 {"display_dir_colour", "#000080"},
73 {"display_pipe_colour", "#444444"},
74 {"display_sock_colour", "#ff00ff"},
75 {"display_file_colour", "#000000"},
76 {"display_cdev_colour", "#000000"},
77 {"display_bdev_colour", "#000000"},
78 {"display_door_colour", "#ff00ff"},
79 {"display_exec_colour", "#006000"},
80 {"display_adir_colour", "#006000"}
82 #define NUM_TYPE_COLOURS\
83 (sizeof(opt_type_colours) / sizeof(opt_type_colours[0]))
85 /* Parsed colours for file types */
86 static Option o_type_colours
[NUM_TYPE_COLOURS
];
87 static GdkColor type_colours
[NUM_TYPE_COLOURS
];
89 /* Static prototypes */
90 static void alloc_type_colours(void);
91 static void options_changed(void);
92 static char *get_action_save_path(GtkWidget
*dialog
);
93 static MIME_type
*get_mime_type(const gchar
*type_name
, gboolean can_create
);
94 static gboolean
remove_handler_with_confirm(const guchar
*path
);
95 static void set_icon_theme(void);
96 static GList
*build_icon_theme(Option
*option
, xmlNode
*node
, guchar
*label
);
98 /* Hash of all allocated MIME types, indexed by "media/subtype".
99 * MIME_type structs are never freed; this table prevents memory leaks
100 * when rereading the config files.
102 static GHashTable
*type_hash
= NULL
;
104 /* Most things on Unix are text files, so this is the default type */
105 MIME_type
*text_plain
;
106 MIME_type
*inode_directory
;
107 MIME_type
*inode_mountpoint
;
108 MIME_type
*inode_pipe
;
109 MIME_type
*inode_socket
;
110 MIME_type
*inode_block_dev
;
111 MIME_type
*inode_char_dev
;
112 MIME_type
*application_executable
;
113 MIME_type
*application_octet_stream
;
114 MIME_type
*application_x_shellscript
;
115 MIME_type
*application_x_desktop
;
116 MIME_type
*inode_unknown
;
117 MIME_type
*inode_door
;
119 static Option o_display_colour_types
;
120 static Option o_icon_theme
;
122 GtkIconTheme
*icon_theme
= NULL
;
128 icon_theme
= gtk_icon_theme_new();
130 type_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
132 text_plain
= get_mime_type("text/plain", TRUE
);
133 inode_directory
= get_mime_type("inode/directory", TRUE
);
134 inode_mountpoint
= get_mime_type("inode/mount-point", TRUE
);
135 inode_pipe
= get_mime_type("inode/fifo", TRUE
);
136 inode_socket
= get_mime_type("inode/socket", TRUE
);
137 inode_block_dev
= get_mime_type("inode/blockdevice", TRUE
);
138 inode_char_dev
= get_mime_type("inode/chardevice", TRUE
);
139 application_executable
= get_mime_type("application/x-executable", TRUE
);
140 application_octet_stream
= get_mime_type("application/octet-stream", TRUE
);
141 application_x_shellscript
= get_mime_type("application/x-shellscript", TRUE
);
142 application_x_desktop
= get_mime_type("application/x-desktop", TRUE
);
143 application_x_desktop
->executable
= TRUE
;
144 inode_unknown
= get_mime_type("inode/unknown", TRUE
);
145 inode_door
= get_mime_type("inode/door", TRUE
);
147 option_add_string(&o_icon_theme
, "icon_theme", "ROX");
148 option_add_int(&o_display_colour_types
, "display_colour_types", TRUE
);
149 option_register_widget("icon-theme-chooser", build_icon_theme
);
151 for (i
= 0; i
< NUM_TYPE_COLOURS
; i
++)
152 option_add_string(&o_type_colours
[i
],
153 opt_type_colours
[i
][0],
154 opt_type_colours
[i
][1]);
155 alloc_type_colours();
159 option_add_notify(options_changed
);
162 /* Read-load all the glob patterns.
163 * Note: calls filer_update_all.
165 void reread_mime_files(void)
167 gtk_icon_theme_rescan_if_needed(icon_theme
);
174 /* Returns the MIME_type structure for the given type name. It is looked
175 * up in type_hash and returned if found. If not found (and can_create is
176 * TRUE) then a new MIME_type is made, added to type_hash and returned.
177 * NULL is returned if type_name is not in type_hash and can_create is
178 * FALSE, or if type_name does not contain a '/' character.
180 static MIME_type
*get_mime_type(const gchar
*type_name
, gboolean can_create
)
185 mtype
= g_hash_table_lookup(type_hash
, type_name
);
186 if (mtype
|| !can_create
)
189 slash
= strchr(type_name
, '/');
190 g_return_val_if_fail(slash
!= NULL
, NULL
); /* XXX: Report nicely */
192 mtype
= g_new(MIME_type
, 1);
193 mtype
->media_type
= g_strndup(type_name
, slash
- type_name
);
194 mtype
->subtype
= g_strdup(slash
+ 1);
196 mtype
->comment
= NULL
;
198 mtype
->executable
= xdg_mime_mime_type_subclass(type_name
,
199 "application/x-executable");
201 g_hash_table_insert(type_hash
, g_strdup(type_name
), mtype
);
206 const char *basetype_name(DirItem
*item
)
208 if (item
->flags
& ITEM_FLAG_SYMLINK
)
209 return _("Sym link");
210 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
211 return _("Mount point");
212 else if (item
->flags
& ITEM_FLAG_APPDIR
)
215 switch (item
->base_type
)
221 case TYPE_CHAR_DEVICE
:
222 return _("Char dev");
223 case TYPE_BLOCK_DEVICE
:
224 return _("Block dev");
236 static void append_names(gpointer key
, gpointer value
, gpointer udata
)
238 GList
**list
= (GList
**) udata
;
240 *list
= g_list_prepend(*list
, key
);
243 /* Return list of all mime type names. Caller must free the list
244 * but NOT the strings it contains (which are never freed).
246 GList
*mime_type_name_list(void)
250 g_hash_table_foreach(type_hash
, append_names
, &list
);
251 list
= g_list_sort(list
, (GCompareFunc
) strcmp
);
256 /* MIME-type guessing */
258 /* Get the type of this file - stats the file and uses that if
259 * possible. For regular or missing files, uses the pathname.
261 MIME_type
*type_get_type(const guchar
*path
)
264 MIME_type
*type
= NULL
;
266 item
= diritem_new("");
267 diritem_restat(path
, item
, NULL
);
268 if (item
->base_type
!= TYPE_ERROR
)
269 type
= item
->mime_type
;
275 type
= type_from_path(path
);
283 /* Returns a pointer to the MIME-type.
285 * Tries all enabled methods:
286 * - Look for extended attribute
287 * - If no attribute, check file name
288 * - If no name rule, check contents
290 * NULL if we can't think of anything.
292 MIME_type
*type_from_path(const char *path
)
294 MIME_type
*mime_type
= NULL
;
295 const char *type_name
;
297 /* Check for extended attribute first */
298 mime_type
= xtype_get(path
);
302 /* Try name and contents next */
303 type_name
= xdg_mime_get_mime_type_for_file(path
);
305 return get_mime_type(type_name
, TRUE
);
310 /* Returns the file/dir in Choices for handling this type.
311 * NULL if there isn't one. g_free() the result.
313 char *handler_for(MIME_type
*type
)
318 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
319 open
= choices_find_xdg_path_load(type_name
, "MIME-types", SITE
);
323 open
= choices_find_xdg_path_load(type
->media_type
,
329 MIME_type
*mime_type_lookup(const char *type
)
331 return get_mime_type(type
, TRUE
);
334 /* Actions for types */
336 /* Return the image for this type, loading it if needed.
337 * Places to check are: (eg type="text_plain", base="text")
338 * 1. <Choices>/MIME-icons/base_subtype
339 * 2. Icon theme 'mime-base:subtype'
340 * 3. Icon theme 'mime-base'
341 * 4. Unknown type icon.
343 * Note: You must g_object_unref() the image afterwards.
345 MaskedPixmap
*type_to_icon(MIME_type
*type
)
348 char *type_name
, *path
;
353 g_object_ref(im_unknown
);
358 /* Already got an image? */
361 /* Yes - don't recheck too often */
362 if (abs(now
- type
->image_time
) < 2)
364 g_object_ref(type
->image
);
367 g_object_unref(type
->image
);
371 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
,
373 path
= choices_find_xdg_path_load(type_name
, "MIME-icons", SITE
);
377 type
->image
= g_fscache_lookup(pixmap_cache
, path
);
384 type_name
= g_strconcat("mime-", type
->media_type
, ":",
385 type
->subtype
, NULL
);
386 full
= gtk_icon_theme_load_icon(icon_theme
, type_name
, HUGE_HEIGHT
,
391 /* Ugly hack... try for a GNOME icon */
392 if (type
== inode_directory
)
393 type_name
= g_strdup("gnome-fs-directory");
395 type_name
= g_strconcat("gnome-mime-", type
->media_type
,
396 "-", type
->subtype
, NULL
);
397 full
= gtk_icon_theme_load_icon(icon_theme
,
399 HUGE_HEIGHT
, 0, NULL
);
404 /* Try for a media type */
405 type_name
= g_strconcat("mime-", type
->media_type
, NULL
);
406 full
= gtk_icon_theme_load_icon(icon_theme
,
408 HUGE_HEIGHT
, 0, NULL
);
413 /* Ugly hack... try for a GNOME default media icon */
414 type_name
= g_strconcat("gnome-mime-", type
->media_type
, NULL
);
416 full
= gtk_icon_theme_load_icon(icon_theme
,
418 HUGE_HEIGHT
, 0, NULL
);
423 type
->image
= masked_pixmap_new(full
);
424 g_object_unref(full
);
430 /* One ref from the type structure, one returned */
431 type
->image
= im_unknown
;
432 g_object_ref(im_unknown
);
435 type
->image_time
= now
;
437 g_object_ref(type
->image
);
441 GdkAtom
type_to_atom(MIME_type
*type
)
446 g_return_val_if_fail(type
!= NULL
, GDK_NONE
);
448 str
= g_strconcat(type
->media_type
, "/", type
->subtype
, NULL
);
449 retval
= gdk_atom_intern(str
, FALSE
);
455 static void show_shell_help(gpointer data
)
457 info_message(_("Enter a shell command which will load \"$@\" into "
458 "a suitable program. Eg:\n\n"
462 /* Called if the user clicks on the OK button. Returns FALSE if an error
463 * was displayed instead of performing the action.
465 static gboolean
set_shell_action(GtkWidget
*dialog
)
468 const guchar
*command
;
473 entry
= g_object_get_data(G_OBJECT(dialog
), "shell_command");
475 g_return_val_if_fail(entry
!= NULL
, FALSE
);
477 command
= gtk_entry_get_text(entry
);
479 if (!strchr(command
, '$'))
481 show_shell_help(NULL
);
485 path
= get_action_save_path(dialog
);
489 tmp
= g_strdup_printf("#! /bin/sh\nexec %s\n", command
);
492 fd
= open(path
, O_CREAT
| O_WRONLY
, 0755);
499 file
= fdopen(fd
, "w");
502 if (fwrite(tmp
, 1, len
, file
) < len
)
504 if (fclose(file
) && error
== 0)
512 report_error(g_strerror(error
));
517 gtk_widget_destroy(dialog
);
522 static void set_action_response(GtkWidget
*dialog
, gint response
, gpointer data
)
524 if (response
== GTK_RESPONSE_OK
)
525 if (!set_shell_action(dialog
))
527 gtk_widget_destroy(dialog
);
530 /* Return the path of the file in choices that handles this type and
532 * NULL if nothing is defined for it.
534 static guchar
*handler_for_radios(GObject
*dialog
)
539 radios
= g_object_get_data(G_OBJECT(dialog
), "rox-radios");
540 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
542 g_return_val_if_fail(radios
!= NULL
, NULL
);
543 g_return_val_if_fail(type
!= NULL
, NULL
);
545 switch (radios_get_value(radios
))
548 return choices_find_xdg_path_load(type
->media_type
,
552 gchar
*tmp
, *handler
;
553 tmp
= g_strconcat(type
->media_type
, "_",
554 type
->subtype
, NULL
);
555 handler
= choices_find_xdg_path_load(tmp
,
562 g_warning("Bad type");
567 /* (radios can be NULL if called from clear_run_action) */
568 static void run_action_update(Radios
*radios
, gpointer data
)
572 GObject
*dialog
= G_OBJECT(data
);
574 drop_box
= g_object_get_data(dialog
, "rox-dropbox");
576 g_return_if_fail(drop_box
!= NULL
);
578 handler
= handler_for_radios(dialog
);
584 handler
= readlink_dup(old
);
591 drop_box_set_path(DROP_BOX(drop_box
), handler
);
595 static void clear_run_action(GtkWidget
*drop_box
, GtkWidget
*dialog
)
599 handler
= handler_for_radios(G_OBJECT(dialog
));
602 remove_handler_with_confirm(handler
);
604 run_action_update(NULL
, dialog
);
607 /* Called when a URI list is dropped onto the box in the Set Run Action
608 * dialog. Make sure it's an application, and make that the default
611 static void drag_app_dropped(GtkWidget
*drop_box
,
617 item
= diritem_new("");
618 diritem_restat(app
, item
, NULL
);
619 if (item
->flags
& ITEM_FLAG_APPDIR
|| EXECUTABLE_FILE(item
))
623 path
= get_action_save_path(dialog
);
627 if (symlink(app
, path
))
628 delayed_error("symlink: %s",
631 destroy_on_idle(dialog
);
638 _("This is not a program! Give me an application "
644 /* Find the current command which is used to run files of this type.
645 * Returns NULL on failure. g_free() the result.
647 static guchar
*get_current_command(MIME_type
*type
)
650 char *handler
, *nl
, *data
= NULL
;
652 guchar
*command
= NULL
;
654 handler
= handler_for(type
);
657 return NULL
; /* No current handler */
659 if (stat(handler
, &info
))
660 goto out
; /* Can't stat */
662 if ((!S_ISREG(info
.st_mode
)) || info
.st_size
> 256)
663 goto out
; /* Only use small regular files */
665 if (!load_file(handler
, &data
, &len
))
666 goto out
; /* Didn't load OK */
668 if (strncmp(data
, "#! /bin/sh\nexec ", 16) != 0)
669 goto out
; /* Not one of ours */
671 nl
= strchr(data
+ 16, '\n');
673 goto out
; /* No newline! */
675 command
= g_strndup(data
+ 16, nl
- data
- 16);
682 /* Find the current command which is used to run files of this type,
683 * and return a textual description of it.
684 * Only call for non-executable files.
685 * g_free() the result.
687 gchar
*describe_current_command(MIME_type
*type
)
694 g_return_val_if_fail(type
!= NULL
, NULL
);
696 handler
= handler_for(type
);
699 return g_strdup(_("No run action defined"));
701 target
= readlink_dup(handler
);
704 /* Cope with relative paths (shouldn't normally be needed) */
706 if (target
[0] == '/')
715 dir
= g_path_get_dirname(handler
);
717 handler
= g_strconcat(dir
, "/", target
, NULL
);
723 if (mc_stat(handler
, &info
) !=0 )
725 desc
= g_strdup_printf(_("Error in handler %s: %s"), handler
,
730 if (S_ISDIR(info
.st_mode
))
733 uid_t dir_uid
= info
.st_uid
;
735 tmp
= make_path(handler
, "AppRun");
737 if (mc_lstat(tmp
, &info
) != 0 || info
.st_uid
!= dir_uid
738 || !(info
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)))
739 desc
= g_strdup_printf(
740 _("Invalid application %s (bad AppRun)"),
742 /* Else, just report handler... */
747 /* It's not an application directory, and it's not a symlink... */
749 if (access(handler
, X_OK
) != 0)
751 desc
= g_strdup_printf(_("Non-executable %s"), handler
);
755 desc
= get_current_command(type
);
765 /* Display a dialog box allowing the user to set the default run action
768 void type_set_handler_dialog(MIME_type
*type
)
772 GtkWidget
*frame
, *entry
, *label
, *button
;
776 g_return_if_fail(type
!= NULL
);
778 dialog
= GTK_DIALOG(gtk_dialog_new());
779 gtk_dialog_set_has_separator(dialog
, FALSE
);
780 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
782 g_object_set_data(G_OBJECT(dialog
), "mime_type", type
);
784 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set run action"));
786 radios
= radios_new(run_action_update
, dialog
);
787 g_object_set_data(G_OBJECT(dialog
), "rox-radios", radios
);
790 _("If a handler for the specific type isn't set up, "
791 "use this as the default."), SET_MEDIA
,
792 _("Set default for all `%s/<anything>'"),
796 _("Use this application for all files with this MIME "
798 _("Only for the type `%s' (%s/%s)"),
799 mime_type_comment(type
),
800 type
->media_type
, type
->subtype
);
802 radios_set_value(radios
, SET_TYPE
);
804 frame
= drop_box_new(_("Drop a suitable application here"));
806 g_object_set_data(G_OBJECT(dialog
), "rox-dropbox", frame
);
808 radios_pack(radios
, GTK_BOX(dialog
->vbox
));
809 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), frame
, TRUE
, TRUE
, 0);
811 g_signal_connect(frame
, "path_dropped",
812 G_CALLBACK(drag_app_dropped
), dialog
);
813 g_signal_connect(frame
, "clear",
814 G_CALLBACK(clear_run_action
), dialog
);
816 hbox
= gtk_hbox_new(FALSE
, 4);
817 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 4);
818 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
819 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
821 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
823 hbox
= gtk_hbox_new(FALSE
, 4);
824 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 0);
826 label
= gtk_label_new(_("Enter a shell command:")),
827 gtk_misc_set_alignment(GTK_MISC(label
), 0, .5);
828 gtk_box_pack_start(GTK_BOX(hbox
), label
, TRUE
, TRUE
, 4);
830 gtk_box_pack_start(GTK_BOX(hbox
),
831 new_help_button(show_shell_help
, NULL
), FALSE
, TRUE
, 0);
833 entry
= gtk_entry_new();
834 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), entry
, FALSE
, TRUE
, 0);
835 gtk_widget_grab_focus(entry
);
836 g_object_set_data(G_OBJECT(dialog
), "shell_command", entry
);
837 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
839 /* If possible, fill in the entry box with the current command */
840 tmp
= get_current_command(type
);
843 gtk_entry_set_text(GTK_ENTRY(entry
), tmp
);
844 gtk_editable_set_position(GTK_EDITABLE(entry
), -1);
849 gtk_entry_set_text(GTK_ENTRY(entry
), " \"$@\"");
850 gtk_editable_set_position(GTK_EDITABLE(entry
), 0);
853 gtk_dialog_add_button(dialog
, GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
);
855 button
= button_new_mixed(GTK_STOCK_OK
, _("_Use Command"));
856 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
857 gtk_dialog_add_action_widget(dialog
, button
, GTK_RESPONSE_OK
);
859 hbox
= gtk_hbox_new(TRUE
, 4);
860 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 0);
862 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
864 g_signal_connect(dialog
, "response",
865 G_CALLBACK(set_action_response
), NULL
);
867 gtk_widget_show_all(GTK_WIDGET(dialog
));
870 /* path is an entry in Choices. If it's a symlink or a very small executable
871 * then just get rid of it, otherwise confirm first. It it doesn't exist,
874 * FALSE on error (abort operation).
876 static gboolean
remove_handler_with_confirm(const guchar
*path
)
880 if (lstat(path
, &info
) == 0)
882 /* A binding already exists... */
883 if (S_ISREG(info
.st_mode
) && info
.st_size
> 256)
885 if (!confirm(_("A run action already exists and is "
886 "quite a big program - are you sure "
887 "you want to delete it?"),
888 GTK_STOCK_DELETE
, NULL
))
896 report_error(_("Can't remove %s: %s"),
897 path
, g_strerror(errno
));
905 /* The user wants to set a new default action for files of this type (or just
906 * clear the action). Removes the current binding if possible and returns the
907 * path to save the new one to. NULL means cancel. g_free() the result.
909 static char *get_action_save_path(GtkWidget
*dialog
)
912 guchar
*type_name
= NULL
;
916 g_return_val_if_fail(dialog
!= NULL
, NULL
);
918 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
919 radios
= g_object_get_data(G_OBJECT(dialog
), "rox-radios");
921 g_return_val_if_fail(radios
!= NULL
&& type
!= NULL
, NULL
);
923 if (radios_get_value(radios
) == SET_MEDIA
)
924 type_name
= g_strdup(type
->media_type
);
926 type_name
= g_strconcat(type
->media_type
, "_",
927 type
->subtype
, NULL
);
929 path
= choices_find_xdg_path_save("", PROJECT
, SITE
, FALSE
);
933 _("Choices saving is disabled by CHOICESPATH variable"));
938 path
= choices_find_xdg_path_save(type_name
, "MIME-types", SITE
, TRUE
);
940 if (!remove_handler_with_confirm(path
))
947 MIME_type
*mime_type_from_base_type(int base_type
)
954 return inode_directory
;
959 case TYPE_BLOCK_DEVICE
:
960 return inode_block_dev
;
961 case TYPE_CHAR_DEVICE
:
962 return inode_char_dev
;
966 return inode_unknown
;
969 /* Takes the st_mode field from stat() and returns the base type.
970 * Should not be a symlink.
972 int mode_to_base_type(int st_mode
)
974 if (S_ISREG(st_mode
))
976 else if (S_ISDIR(st_mode
))
977 return TYPE_DIRECTORY
;
978 else if (S_ISBLK(st_mode
))
979 return TYPE_BLOCK_DEVICE
;
980 else if (S_ISCHR(st_mode
))
981 return TYPE_CHAR_DEVICE
;
982 else if (S_ISFIFO(st_mode
))
984 else if (S_ISSOCK(st_mode
))
986 else if (S_ISDOOR(st_mode
))
992 /* Returns TRUE is this is something that is run by looking up its type
993 * in MIME-types and, hence, can have its run action set.
995 gboolean
can_set_run_action(DirItem
*item
)
997 g_return_val_if_fail(item
!= NULL
, FALSE
);
999 return item
->base_type
== TYPE_FILE
&& !EXECUTABLE_FILE(item
);
1002 /* Parse file type colours and allocate/free them as necessary */
1003 static void alloc_type_colours(void)
1005 gboolean success
[NUM_TYPE_COLOURS
];
1006 int change_count
= 0; /* No. needing realloc */
1008 static gboolean allocated
= FALSE
;
1011 for (i
= 0; i
< NUM_TYPE_COLOURS
; i
++)
1013 GdkColor
*c
= &type_colours
[i
];
1015 gushort g
= c
->green
;
1016 gushort b
= c
->blue
;
1018 gdk_color_parse(o_type_colours
[i
].value
, &type_colours
[i
]);
1020 if (allocated
&& (c
->red
!= r
|| c
->green
!= g
|| c
->blue
!= b
))
1024 /* Free colours if they were previously allocated and
1025 * have changed or become unneeded.
1027 if (allocated
&& (change_count
|| !o_display_colour_types
.int_value
))
1029 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1030 type_colours
, NUM_TYPE_COLOURS
);
1034 /* Allocate colours, unless they are still allocated (=> they didn't
1035 * change) or we don't want them anymore.
1036 * XXX: what should be done if allocation fails?
1038 if (!allocated
&& o_display_colour_types
.int_value
)
1040 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1041 type_colours
, NUM_TYPE_COLOURS
,
1042 FALSE
, TRUE
, success
);
1047 static void expire_timer(gpointer key
, gpointer value
, gpointer data
)
1049 MIME_type
*type
= value
;
1051 type
->image_time
= 0;
1054 static void options_changed(void)
1056 alloc_type_colours();
1057 if (o_icon_theme
.has_changed
)
1060 g_hash_table_foreach(type_hash
, expire_timer
, NULL
);
1065 /* Return a pointer to a (static) colour for this item. If colouring is
1066 * off, returns normal.
1068 GdkColor
*type_get_colour(DirItem
*item
, GdkColor
*normal
)
1070 int type
= item
->base_type
;
1072 if (!o_display_colour_types
.int_value
)
1075 if (EXECUTABLE_FILE(item
))
1077 else if (item
->flags
& ITEM_FLAG_APPDIR
)
1080 g_return_val_if_fail(type
>= 0 && type
< NUM_TYPE_COLOURS
, normal
);
1082 return &type_colours
[type
];
1085 static char **get_xdg_data_dirs(int *n_dirs
)
1091 env
= getenv("XDG_DATA_DIRS");
1093 env
= "/usr/local/share/:/usr/share/";
1094 dirs
= g_strsplit(env
, ":", 0);
1095 g_return_val_if_fail(dirs
!= NULL
, NULL
);
1096 for (n
= 0; dirs
[n
]; n
++)
1098 for (i
= n
; i
> 0; i
--)
1099 dirs
[i
] = dirs
[i
- 1];
1100 env
= getenv("XDG_DATA_HOME");
1102 dirs
[0] = g_strdup(env
);
1104 dirs
[0] = g_build_filename(g_get_home_dir(), ".local",
1110 /* Try to fill in 'type->comment' from this document */
1111 static void get_comment(MIME_type
*type
, const guchar
*path
)
1116 doc
= xml_cache_load(path
);
1120 node
= xml_get_section(doc
, TYPE_NS
, "comment");
1125 g_return_if_fail(type
->comment
== NULL
);
1126 val
= xmlNodeListGetString(node
->doc
, node
->xmlChildrenNode
, 1);
1127 type
->comment
= g_strdup(val
);
1131 g_object_unref(doc
);
1134 /* Fill in the comment field for this MIME type */
1135 static void find_comment(MIME_type
*type
)
1142 g_free(type
->comment
);
1143 type
->comment
= NULL
;
1146 dirs
= get_xdg_data_dirs(&n_dirs
);
1147 g_return_if_fail(dirs
!= NULL
);
1149 for (i
= 0; i
< n_dirs
; i
++)
1153 path
= g_strdup_printf("%s/mime/%s/%s.xml", dirs
[i
],
1154 type
->media_type
, type
->subtype
);
1155 get_comment(type
, path
);
1162 type
->comment
= g_strdup_printf("%s/%s", type
->media_type
,
1165 for (i
= 0; i
< n_dirs
; i
++)
1170 const char *mime_type_comment(MIME_type
*type
)
1175 return type
->comment
;
1178 static void set_icon_theme(void)
1182 const char *theme_name
= o_icon_theme
.value
;
1184 if (!theme_name
|| !*theme_name
)
1189 gtk_icon_theme_set_custom_theme(icon_theme
, theme_name
);
1190 info
= gtk_icon_theme_lookup_icon(icon_theme
,
1191 "mime-application:postscript",
1195 info
= gtk_icon_theme_lookup_icon(icon_theme
,
1196 "gnome-mime-application-postscript",
1201 gtk_icon_info_free(info
);
1205 if (strcmp(theme_name
, "ROX") == 0)
1208 delayed_error(_("Icon theme '%s' does not contain MIME icons. "
1209 "Using ROX default theme instead."),
1215 icon_home
= g_build_filename(home_dir
, ".icons", NULL
);
1216 if (!file_exists(icon_home
))
1217 mkdir(icon_home
, 0755);
1220 icon_home
= g_build_filename(home_dir
, ".icons", "ROX", NULL
);
1221 if (symlink(make_path(app_dir
, "ROX"), icon_home
))
1223 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1224 "(this may mean that the ROX theme already exists there, but "
1225 "the 'mime-application:postscript' icon couldn't be loaded for "
1226 "some reason, or %s is a link to an invalid directory; try "
1227 "deleting it)"), icon_home
, g_strerror(errno
), icon_home
);
1228 open_to_show(icon_home
);
1232 gtk_icon_theme_rescan_if_needed(icon_theme
);
1235 static guchar
*read_theme(Option
*option
)
1237 GtkOptionMenu
*om
= GTK_OPTION_MENU(option
->widget
);
1240 item
= GTK_LABEL(GTK_BIN(om
)->child
);
1242 g_return_val_if_fail(item
!= NULL
, g_strdup("ROX"));
1244 return g_strdup(gtk_label_get_text(item
));
1247 static void update_theme(Option
*option
)
1249 GtkOptionMenu
*om
= GTK_OPTION_MENU(option
->widget
);
1254 menu
= gtk_option_menu_get_menu(om
);
1256 kids
= gtk_container_get_children(GTK_CONTAINER(menu
));
1257 for (next
= kids
; next
; next
= next
->next
, i
++)
1259 GtkLabel
*item
= GTK_LABEL(GTK_BIN(next
->data
)->child
);
1262 /* The label actually moves from the menu!! */
1264 item
= GTK_LABEL(GTK_BIN(om
)->child
);
1266 label
= gtk_label_get_text(item
);
1268 g_return_if_fail(label
!= NULL
);
1270 if (strcmp(label
, option
->value
) == 0)
1276 gtk_option_menu_set_history(om
, i
);
1278 g_warning("Theme '%s' not found", option
->value
);
1281 static void add_themes_from_dir(GPtrArray
*names
, const char *dir
)
1286 if (access(dir
, F_OK
) != 0)
1289 list
= list_dir(dir
);
1290 g_return_if_fail(list
!= NULL
);
1292 for (i
= 0; i
< list
->len
; i
++)
1296 index_path
= g_build_filename(dir
, list
->pdata
[i
],
1297 "index.theme", NULL
);
1299 if (access(index_path
, F_OK
) == 0)
1300 g_ptr_array_add(names
, list
->pdata
[i
]);
1302 g_free(list
->pdata
[i
]);
1307 g_ptr_array_free(list
, TRUE
);
1310 static GList
*build_icon_theme(Option
*option
, xmlNode
*node
, guchar
*label
)
1312 GtkWidget
*button
, *menu
, *hbox
;
1314 gchar
**theme_dirs
= NULL
;
1318 g_return_val_if_fail(option
!= NULL
, NULL
);
1319 g_return_val_if_fail(label
!= NULL
, NULL
);
1321 hbox
= gtk_hbox_new(FALSE
, 4);
1323 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_(label
)),
1326 button
= gtk_option_menu_new();
1327 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
1329 menu
= gtk_menu_new();
1330 gtk_option_menu_set_menu(GTK_OPTION_MENU(button
), menu
);
1332 gtk_icon_theme_get_search_path(icon_theme
, &theme_dirs
, &n_dirs
);
1333 names
= g_ptr_array_new();
1334 for (i
= 0; i
< n_dirs
; i
++)
1335 add_themes_from_dir(names
, theme_dirs
[i
]);
1336 g_strfreev(theme_dirs
);
1338 g_ptr_array_sort(names
, strcmp2
);
1340 for (i
= 0; i
< names
->len
; i
++)
1343 char *name
= names
->pdata
[i
];
1345 item
= gtk_menu_item_new_with_label(name
);
1346 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
1347 gtk_widget_show_all(item
);
1352 g_ptr_array_free(names
, TRUE
);
1354 option
->update_widget
= update_theme
;
1355 option
->read_widget
= read_theme
;
1356 option
->widget
= button
;
1358 gtk_signal_connect_object(GTK_OBJECT(button
), "changed",
1359 GTK_SIGNAL_FUNC(option_check_widget
),
1360 (GtkObject
*) option
);
1362 return g_list_append(NULL
, hbox
);