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)
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
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 */
29 #include <sys/param.h>
31 #include <sys/types.h>
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>
48 #include "gui_support.h"
56 #include "action.h" /* (for action_chmod) */
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 static GtkIconTheme
*icon_theme
= NULL
;
121 static GtkIconTheme
*rox_theme
= NULL
;
122 static GtkIconTheme
*gnome_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
, '/');
192 g_warning("MIME type '%s' does not contain a '/' character!",
197 mtype
= g_new(MIME_type
, 1);
198 mtype
->media_type
= g_strndup(type_name
, slash
- type_name
);
199 mtype
->subtype
= g_strdup(slash
+ 1);
201 mtype
->comment
= NULL
;
203 mtype
->executable
= xdg_mime_mime_type_subclass(type_name
,
204 "application/x-executable");
206 g_hash_table_insert(type_hash
, g_strdup(type_name
), mtype
);
211 const char *basetype_name(DirItem
*item
)
213 if (item
->flags
& ITEM_FLAG_SYMLINK
)
214 return _("Sym link");
215 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
216 return _("Mount point");
217 else if (item
->flags
& ITEM_FLAG_APPDIR
)
220 switch (item
->base_type
)
226 case TYPE_CHAR_DEVICE
:
227 return _("Char dev");
228 case TYPE_BLOCK_DEVICE
:
229 return _("Block dev");
243 gboolean only_regular
;
246 static void append_names(gpointer key
, gpointer value
, gpointer udata
)
248 struct mime_list
*mlist
= (struct mime_list
*) udata
;
250 if(!mlist
->only_regular
|| strncmp((char *)key
, "inode/", 6)!=0)
251 mlist
->list
= g_list_prepend(mlist
->list
, key
);
254 /* Return list of all mime type names. Caller must free the list
255 * but NOT the strings it contains (which are never freed).
256 If only_regular is true then inode types are excluded.
258 GList
*mime_type_name_list(gboolean only_regular
)
260 struct mime_list list
;
263 list
.only_regular
=only_regular
;
265 g_hash_table_foreach(type_hash
, append_names
, &list
);
266 list
.list
= g_list_sort(list
.list
, (GCompareFunc
) strcmp
);
271 /* MIME-type guessing */
273 /* Get the type of this file - stats the file and uses that if
274 * possible. For regular or missing files, uses the pathname.
276 MIME_type
*type_get_type(const guchar
*path
)
279 MIME_type
*type
= NULL
;
281 item
= diritem_new("");
282 diritem_restat(path
, item
, NULL
);
283 if (item
->base_type
!= TYPE_ERROR
)
284 type
= item
->mime_type
;
290 type
= type_from_path(path
);
298 /* Returns a pointer to the MIME-type.
300 * Tries all enabled methods:
301 * - Look for extended attribute
302 * - If no attribute, check file name
303 * - If no name rule, check contents
305 * NULL if we can't think of anything.
307 MIME_type
*type_from_path(const char *path
)
309 MIME_type
*mime_type
= NULL
;
310 const char *type_name
;
312 /* Check for extended attribute first */
313 mime_type
= xtype_get(path
);
317 /* Try name and contents next */
318 type_name
= xdg_mime_get_mime_type_for_file(path
, NULL
);
320 return get_mime_type(type_name
, TRUE
);
325 /* Returns the file/dir in Choices for handling this type.
326 * NULL if there isn't one. g_free() the result.
328 char *handler_for(MIME_type
*type
)
334 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
, NULL
);
335 open
= choices_find_xdg_path_load(type_name
, "MIME-types", SITE
);
339 open
= choices_find_xdg_path_load(type
->media_type
,
345 /* Some programs behave differently depending on the command
346 * name (e.g., 'vim' vs 'gvim'), so symlinks need to be followed here.
348 target
= readlink_dup(open
);
354 if (target
[0] == '/')
362 /* Relative path (shouldn't normally be needed) */
366 dir
= g_path_get_dirname(open
);
369 abs_path
= g_strconcat(dir
, "/", target
, NULL
);
377 MIME_type
*mime_type_lookup(const char *type
)
379 return get_mime_type(type
, TRUE
);
382 static void init_aux_theme(GtkIconTheme
**ptheme
, const char *name
)
386 *ptheme
= gtk_icon_theme_new();
387 gtk_icon_theme_set_custom_theme(*ptheme
, name
);
390 inline static void init_rox_theme(void)
392 init_aux_theme(&rox_theme
, "ROX");
395 inline static void init_gnome_theme(void)
397 init_aux_theme(&gnome_theme
, "gnome");
400 /* We don't want ROX to override configured theme so try all possibilities
401 * in icon_theme first */
402 static GtkIconInfo
*mime_type_lookup_icon_info(GtkIconTheme
*theme
,
405 char *type_name
= g_strconcat("mime-", type
->media_type
, ":",
406 type
->subtype
, NULL
);
407 GtkIconInfo
*full
= gtk_icon_theme_lookup_icon(theme
, type_name
,
413 /* Ugly hack... try for a GNOME icon */
414 if (type
== inode_directory
)
415 type_name
= g_strdup("gnome-fs-directory");
417 type_name
= g_strconcat("gnome-mime-", type
->media_type
,
418 "-", type
->subtype
, NULL
);
419 full
= gtk_icon_theme_lookup_icon(theme
, type_name
, HUGE_HEIGHT
, 0);
424 /* Try for a media type */
425 type_name
= g_strconcat("mime-", type
->media_type
, NULL
);
426 full
= gtk_icon_theme_lookup_icon(theme
, type_name
, HUGE_HEIGHT
, 0);
431 /* Ugly hack... try for a GNOME default media icon */
432 type_name
= g_strconcat("gnome-mime-", type
->media_type
, NULL
);
434 full
= gtk_icon_theme_lookup_icon(theme
, type_name
, HUGE_HEIGHT
, 0);
440 /* Actions for types */
442 /* Return the image for this type, loading it if needed.
443 * Places to check are: (eg type="text_plain", base="text")
444 * 1. <Choices>/MIME-icons/base_subtype
445 * 2. Icon theme 'mime-base:subtype'
446 * 3. Icon theme 'mime-base'
447 * 4. Unknown type icon.
449 * Note: You must g_object_unref() the image afterwards.
451 MaskedPixmap
*type_to_icon(MIME_type
*type
)
454 char *type_name
, *path
;
459 g_object_ref(im_unknown
);
464 /* Already got an image? */
467 /* Yes - don't recheck too often */
468 if (abs(now
- type
->image_time
) < 2)
470 g_object_ref(type
->image
);
473 g_object_unref(type
->image
);
477 type_name
= g_strconcat(type
->media_type
, "_", type
->subtype
,
479 path
= choices_find_xdg_path_load(type_name
, "MIME-icons", SITE
);
483 type
->image
= g_fscache_lookup(pixmap_cache
, path
);
490 full
= mime_type_lookup_icon_info(icon_theme
, type
);
491 if (!full
&& icon_theme
!= rox_theme
)
494 full
= mime_type_lookup_icon_info(rox_theme
, type
);
496 if (!full
&& icon_theme
!= gnome_theme
)
499 full
= mime_type_lookup_icon_info(gnome_theme
, type
);
503 const char *icon_path
;
504 /* Get the actual icon through our cache, not through GTK, because
505 * GTK doesn't cache icons.
507 icon_path
= gtk_icon_info_get_filename(full
);
508 if (icon_path
!= NULL
)
509 type
->image
= g_fscache_lookup(pixmap_cache
, icon_path
);
510 /* else shouldn't happen, because we didn't use
511 * GTK_ICON_LOOKUP_USE_BUILTIN.
513 gtk_icon_info_free(full
);
519 /* One ref from the type structure, one returned */
520 type
->image
= im_unknown
;
521 g_object_ref(im_unknown
);
524 type
->image_time
= now
;
526 g_object_ref(type
->image
);
530 GdkAtom
type_to_atom(MIME_type
*type
)
535 g_return_val_if_fail(type
!= NULL
, GDK_NONE
);
537 str
= g_strconcat(type
->media_type
, "/", type
->subtype
, NULL
);
538 retval
= gdk_atom_intern(str
, FALSE
);
544 static void show_shell_help(gpointer data
)
546 info_message(_("Enter a shell command which will load \"$@\" into "
547 "a suitable program. Eg:\n\n"
551 /* Called if the user clicks on the OK button. Returns FALSE if an error
552 * was displayed instead of performing the action.
554 static gboolean
set_shell_action(GtkWidget
*dialog
)
557 const guchar
*command
;
562 entry
= g_object_get_data(G_OBJECT(dialog
), "shell_command");
564 g_return_val_if_fail(entry
!= NULL
, FALSE
);
566 command
= gtk_entry_get_text(entry
);
568 if (!strchr(command
, '$'))
570 show_shell_help(NULL
);
574 path
= get_action_save_path(dialog
);
578 tmp
= g_strdup_printf("#! /bin/sh\nexec %s\n", command
);
581 fd
= open(path
, O_CREAT
| O_WRONLY
, 0755);
588 file
= fdopen(fd
, "w");
591 if (fwrite(tmp
, 1, len
, file
) < len
)
593 if (fclose(file
) && error
== 0)
601 report_error(g_strerror(error
));
606 gtk_widget_destroy(dialog
);
611 static void set_action_response(GtkWidget
*dialog
, gint response
, gpointer data
)
613 if (response
== GTK_RESPONSE_OK
)
614 if (!set_shell_action(dialog
))
616 gtk_widget_destroy(dialog
);
619 /* Return the path of the file in choices that handles this type and
621 * NULL if nothing is defined for it.
623 static guchar
*handler_for_radios(GObject
*dialog
)
628 radios
= g_object_get_data(G_OBJECT(dialog
), "rox-radios");
629 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
631 g_return_val_if_fail(radios
!= NULL
, NULL
);
632 g_return_val_if_fail(type
!= NULL
, NULL
);
634 switch (radios_get_value(radios
))
637 return choices_find_xdg_path_load(type
->media_type
,
641 gchar
*tmp
, *handler
;
642 tmp
= g_strconcat(type
->media_type
, "_",
643 type
->subtype
, NULL
);
644 handler
= choices_find_xdg_path_load(tmp
,
651 g_warning("Bad type");
656 /* (radios can be NULL if called from clear_run_action) */
657 static void run_action_update(Radios
*radios
, gpointer data
)
661 GObject
*dialog
= G_OBJECT(data
);
663 drop_box
= g_object_get_data(dialog
, "rox-dropbox");
665 g_return_if_fail(drop_box
!= NULL
);
667 handler
= handler_for_radios(dialog
);
673 handler
= readlink_dup(old
);
680 drop_box_set_path(DROP_BOX(drop_box
), handler
);
684 static void clear_run_action(GtkWidget
*drop_box
, GtkWidget
*dialog
)
688 handler
= handler_for_radios(G_OBJECT(dialog
));
691 remove_handler_with_confirm(handler
);
693 run_action_update(NULL
, dialog
);
696 /* Called when a URI list is dropped onto the box in the Set Run Action
697 * dialog. Make sure it's an application, and make that the default
700 static void drag_app_dropped(GtkWidget
*drop_box
,
706 item
= diritem_new("");
707 diritem_restat(app
, item
, NULL
);
708 if (item
->flags
& ITEM_FLAG_APPDIR
|| EXECUTABLE_FILE(item
))
712 path
= get_action_save_path(dialog
);
716 if (symlink(app
, path
))
717 delayed_error("symlink: %s",
720 destroy_on_idle(dialog
);
727 _("This is not a program! Give me an application "
733 /* Find the current command which is used to run files of this type.
734 * Returns NULL on failure. g_free() the result.
736 static guchar
*get_current_command(MIME_type
*type
)
739 char *handler
, *nl
, *data
= NULL
;
741 guchar
*command
= NULL
;
743 handler
= handler_for(type
);
746 return NULL
; /* No current handler */
748 if (stat(handler
, &info
))
749 goto out
; /* Can't stat */
751 if ((!S_ISREG(info
.st_mode
)) || info
.st_size
> 256)
752 goto out
; /* Only use small regular files */
754 if (!load_file(handler
, &data
, &len
))
755 goto out
; /* Didn't load OK */
757 if (strncmp(data
, "#! /bin/sh\nexec ", 16) != 0)
758 goto out
; /* Not one of ours */
760 nl
= strchr(data
+ 16, '\n');
762 goto out
; /* No newline! */
764 command
= g_strndup(data
+ 16, nl
- data
- 16);
771 /* Find the current command which is used to run files of this type,
772 * and return a textual description of it.
773 * Only call for non-executable files.
774 * g_free() the result.
776 gchar
*describe_current_command(MIME_type
*type
)
782 g_return_val_if_fail(type
!= NULL
, NULL
);
784 handler
= handler_for(type
);
787 return g_strdup(_("No run action defined"));
789 if (mc_stat(handler
, &info
) !=0 )
791 desc
= g_strdup_printf(_("Error in handler %s: %s"), handler
,
796 if (S_ISDIR(info
.st_mode
))
799 uid_t dir_uid
= info
.st_uid
;
801 tmp
= make_path(handler
, "AppRun");
803 if (mc_lstat(tmp
, &info
) != 0 || info
.st_uid
!= dir_uid
804 || !(info
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)))
805 desc
= g_strdup_printf(
806 _("Invalid application %s (bad AppRun)"),
808 /* Else, just report handler... */
813 /* It's not an application directory, and it's not a symlink... */
815 if (access(handler
, X_OK
) != 0)
817 desc
= g_strdup_printf(_("Non-executable %s"), handler
);
821 desc
= get_current_command(type
);
831 /* Display a dialog box allowing the user to set the default run action
834 void type_set_handler_dialog(MIME_type
*type
)
838 GtkWidget
*frame
, *entry
, *label
, *button
;
842 g_return_if_fail(type
!= NULL
);
844 dialog
= GTK_DIALOG(gtk_dialog_new());
845 gtk_dialog_set_has_separator(dialog
, FALSE
);
846 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
848 g_object_set_data(G_OBJECT(dialog
), "mime_type", type
);
850 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set run action"));
852 radios
= radios_new(run_action_update
, dialog
);
853 g_object_set_data(G_OBJECT(dialog
), "rox-radios", radios
);
856 _("If a handler for the specific type isn't set up, "
857 "use this as the default."), SET_MEDIA
,
858 _("Set default for all `%s/<anything>'"),
862 _("Use this application for all files with this MIME "
864 _("Only for the type `%s' (%s/%s)"),
865 mime_type_comment(type
),
866 type
->media_type
, type
->subtype
);
868 radios_set_value(radios
, SET_TYPE
);
870 frame
= drop_box_new(_("Drop a suitable application here"));
872 g_object_set_data(G_OBJECT(dialog
), "rox-dropbox", frame
);
874 radios_pack(radios
, GTK_BOX(dialog
->vbox
));
875 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), frame
, TRUE
, TRUE
, 0);
877 g_signal_connect(frame
, "path_dropped",
878 G_CALLBACK(drag_app_dropped
), dialog
);
879 g_signal_connect(frame
, "clear",
880 G_CALLBACK(clear_run_action
), dialog
);
882 hbox
= gtk_hbox_new(FALSE
, 4);
883 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 4);
884 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
885 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
887 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
889 hbox
= gtk_hbox_new(FALSE
, 4);
890 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 0);
892 label
= gtk_label_new(_("Enter a shell command:")),
893 gtk_misc_set_alignment(GTK_MISC(label
), 0, .5);
894 gtk_box_pack_start(GTK_BOX(hbox
), label
, TRUE
, TRUE
, 4);
896 gtk_box_pack_start(GTK_BOX(hbox
),
897 new_help_button(show_shell_help
, NULL
), FALSE
, TRUE
, 0);
899 entry
= gtk_entry_new();
900 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), entry
, FALSE
, TRUE
, 0);
901 gtk_widget_grab_focus(entry
);
902 g_object_set_data(G_OBJECT(dialog
), "shell_command", entry
);
903 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
905 /* If possible, fill in the entry box with the current command */
906 tmp
= get_current_command(type
);
909 gtk_entry_set_text(GTK_ENTRY(entry
), tmp
);
910 gtk_editable_set_position(GTK_EDITABLE(entry
), -1);
915 gtk_entry_set_text(GTK_ENTRY(entry
), " \"$@\"");
916 gtk_editable_set_position(GTK_EDITABLE(entry
), 0);
919 gtk_dialog_add_button(dialog
, GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
);
921 button
= button_new_mixed(GTK_STOCK_OK
, _("_Use Command"));
922 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
923 gtk_dialog_add_action_widget(dialog
, button
, GTK_RESPONSE_OK
);
925 hbox
= gtk_hbox_new(TRUE
, 4);
926 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 0);
928 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
930 g_signal_connect(dialog
, "response",
931 G_CALLBACK(set_action_response
), NULL
);
933 gtk_widget_show_all(GTK_WIDGET(dialog
));
936 /* path is an entry in Choices. If it's a symlink or a very small executable
937 * then just get rid of it, otherwise confirm first. It it doesn't exist,
940 * FALSE on error (abort operation).
942 static gboolean
remove_handler_with_confirm(const guchar
*path
)
946 if (lstat(path
, &info
) == 0)
948 /* A binding already exists... */
949 if (S_ISREG(info
.st_mode
) && info
.st_size
> 256)
951 if (!confirm(_("A run action already exists and is "
952 "quite a big program - are you sure "
953 "you want to delete it?"),
954 GTK_STOCK_DELETE
, NULL
))
962 report_error(_("Can't remove %s: %s"),
963 path
, g_strerror(errno
));
971 /* The user wants to set a new default action for files of this type (or just
972 * clear the action). Removes the current binding if possible and returns the
973 * path to save the new one to. NULL means cancel. g_free() the result.
975 static char *get_action_save_path(GtkWidget
*dialog
)
978 guchar
*type_name
= NULL
;
982 g_return_val_if_fail(dialog
!= NULL
, NULL
);
984 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
985 radios
= g_object_get_data(G_OBJECT(dialog
), "rox-radios");
987 g_return_val_if_fail(radios
!= NULL
&& type
!= NULL
, NULL
);
989 if (radios_get_value(radios
) == SET_MEDIA
)
990 type_name
= g_strdup(type
->media_type
);
992 type_name
= g_strconcat(type
->media_type
, "_",
993 type
->subtype
, NULL
);
995 path
= choices_find_xdg_path_save("", PROJECT
, SITE
, FALSE
);
999 _("Choices saving is disabled by CHOICESPATH variable"));
1004 path
= choices_find_xdg_path_save(type_name
, "MIME-types", SITE
, TRUE
);
1006 if (!remove_handler_with_confirm(path
))
1013 MIME_type
*mime_type_from_base_type(int base_type
)
1019 case TYPE_DIRECTORY
:
1020 return inode_directory
;
1024 return inode_socket
;
1025 case TYPE_BLOCK_DEVICE
:
1026 return inode_block_dev
;
1027 case TYPE_CHAR_DEVICE
:
1028 return inode_char_dev
;
1032 return inode_unknown
;
1035 /* Takes the st_mode field from stat() and returns the base type.
1036 * Should not be a symlink.
1038 int mode_to_base_type(int st_mode
)
1040 if (S_ISREG(st_mode
))
1042 else if (S_ISDIR(st_mode
))
1043 return TYPE_DIRECTORY
;
1044 else if (S_ISBLK(st_mode
))
1045 return TYPE_BLOCK_DEVICE
;
1046 else if (S_ISCHR(st_mode
))
1047 return TYPE_CHAR_DEVICE
;
1048 else if (S_ISFIFO(st_mode
))
1050 else if (S_ISSOCK(st_mode
))
1052 else if (S_ISDOOR(st_mode
))
1058 /* Returns TRUE is this is something that is run by looking up its type
1059 * in MIME-types and, hence, can have its run action set.
1061 gboolean
can_set_run_action(DirItem
*item
)
1063 g_return_val_if_fail(item
!= NULL
, FALSE
);
1065 return item
->base_type
== TYPE_FILE
&& !EXECUTABLE_FILE(item
);
1068 /* Parse file type colours and allocate/free them as necessary */
1069 static void alloc_type_colours(void)
1071 gboolean success
[NUM_TYPE_COLOURS
];
1072 int change_count
= 0; /* No. needing realloc */
1074 static gboolean allocated
= FALSE
;
1077 for (i
= 0; i
< NUM_TYPE_COLOURS
; i
++)
1079 GdkColor
*c
= &type_colours
[i
];
1081 gushort g
= c
->green
;
1082 gushort b
= c
->blue
;
1084 gdk_color_parse(o_type_colours
[i
].value
, &type_colours
[i
]);
1086 if (allocated
&& (c
->red
!= r
|| c
->green
!= g
|| c
->blue
!= b
))
1090 /* Free colours if they were previously allocated and
1091 * have changed or become unneeded.
1093 if (allocated
&& (change_count
|| !o_display_colour_types
.int_value
))
1095 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1096 type_colours
, NUM_TYPE_COLOURS
);
1100 /* Allocate colours, unless they are still allocated (=> they didn't
1101 * change) or we don't want them anymore.
1102 * XXX: what should be done if allocation fails?
1104 if (!allocated
&& o_display_colour_types
.int_value
)
1106 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1107 type_colours
, NUM_TYPE_COLOURS
,
1108 FALSE
, TRUE
, success
);
1113 static void expire_timer(gpointer key
, gpointer value
, gpointer data
)
1115 MIME_type
*type
= value
;
1117 type
->image_time
= 0;
1120 static void options_changed(void)
1122 alloc_type_colours();
1123 if (o_icon_theme
.has_changed
)
1126 g_hash_table_foreach(type_hash
, expire_timer
, NULL
);
1131 /* Return a pointer to a (static) colour for this item. If colouring is
1132 * off, returns normal.
1134 GdkColor
*type_get_colour(DirItem
*item
, GdkColor
*normal
)
1136 int type
= item
->base_type
;
1138 if (!o_display_colour_types
.int_value
)
1141 if (EXECUTABLE_FILE(item
))
1143 else if (item
->flags
& ITEM_FLAG_APPDIR
)
1146 g_return_val_if_fail(type
>= 0 && type
< NUM_TYPE_COLOURS
, normal
);
1148 return &type_colours
[type
];
1151 static char **get_xdg_data_dirs(int *n_dirs
)
1157 env
= getenv("XDG_DATA_DIRS");
1159 env
= "/usr/local/share/:/usr/share/";
1160 dirs
= g_strsplit(env
, ":", 0);
1161 g_return_val_if_fail(dirs
!= NULL
, NULL
);
1162 for (n
= 0; dirs
[n
]; n
++)
1164 for (i
= n
; i
> 0; i
--)
1165 dirs
[i
] = dirs
[i
- 1];
1166 env
= getenv("XDG_DATA_HOME");
1168 dirs
[0] = g_strdup(env
);
1170 dirs
[0] = g_build_filename(g_get_home_dir(), ".local",
1176 /* Try to fill in 'type->comment' from this document */
1177 static void get_comment(MIME_type
*type
, const guchar
*path
)
1182 doc
= xml_cache_load(path
);
1186 node
= xml_get_section(doc
, TYPE_NS
, "comment");
1191 g_return_if_fail(type
->comment
== NULL
);
1192 val
= xmlNodeListGetString(node
->doc
, node
->xmlChildrenNode
, 1);
1193 type
->comment
= g_strdup(val
);
1197 g_object_unref(doc
);
1200 /* Fill in the comment field for this MIME type */
1201 static void find_comment(MIME_type
*type
)
1208 g_free(type
->comment
);
1209 type
->comment
= NULL
;
1212 dirs
= get_xdg_data_dirs(&n_dirs
);
1213 g_return_if_fail(dirs
!= NULL
);
1215 for (i
= 0; i
< n_dirs
; i
++)
1219 path
= g_strdup_printf("%s/mime/%s/%s.xml", dirs
[i
],
1220 type
->media_type
, type
->subtype
);
1221 get_comment(type
, path
);
1228 type
->comment
= g_strdup_printf("%s/%s", type
->media_type
,
1231 for (i
= 0; i
< n_dirs
; i
++)
1236 const char *mime_type_comment(MIME_type
*type
)
1241 return type
->comment
;
1244 static void unref_icon_theme(void)
1246 if (icon_theme
&& icon_theme
!= rox_theme
&& icon_theme
!= gnome_theme
)
1247 g_object_unref(icon_theme
);
1250 static void set_icon_theme(void)
1254 const char *theme_name
= o_icon_theme
.value
;
1256 if (!theme_name
|| !*theme_name
)
1259 if (!strcmp(theme_name
, "ROX"))
1263 icon_theme
= rox_theme
;
1265 else if (!strcmp(theme_name
, "gnome"))
1269 icon_theme
= gnome_theme
;
1273 if (icon_theme
== rox_theme
|| icon_theme
== gnome_theme
)
1274 icon_theme
= gtk_icon_theme_new();
1275 gtk_icon_theme_set_custom_theme(icon_theme
, theme_name
);
1278 info
= theme_lookup_icon("mime-application:postscript",
1282 info
= theme_lookup_icon("gnome-mime-application-postscript",
1287 gtk_icon_info_free(info
);
1291 icon_home
= g_build_filename(home_dir
, ".icons", NULL
);
1292 if (!file_exists(icon_home
))
1293 mkdir(icon_home
, 0755);
1296 icon_home
= g_build_filename(home_dir
, ".icons", "ROX", NULL
);
1297 if (symlink(make_path(app_dir
, "ROX"), icon_home
))
1299 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1300 "(this may mean that %s already exists as a link to an invalid "
1301 "directory; try deleting it)"),
1302 icon_home
, g_strerror(errno
), icon_home
);
1303 open_to_show(icon_home
);
1307 gtk_icon_theme_rescan_if_needed(icon_theme
);
1310 static guchar
*read_theme(Option
*option
)
1312 GtkOptionMenu
*om
= GTK_OPTION_MENU(option
->widget
);
1315 item
= GTK_LABEL(GTK_BIN(om
)->child
);
1317 g_return_val_if_fail(item
!= NULL
, g_strdup("ROX"));
1319 return g_strdup(gtk_label_get_text(item
));
1322 static void update_theme(Option
*option
)
1324 GtkOptionMenu
*om
= GTK_OPTION_MENU(option
->widget
);
1329 menu
= gtk_option_menu_get_menu(om
);
1331 kids
= gtk_container_get_children(GTK_CONTAINER(menu
));
1332 for (next
= kids
; next
; next
= next
->next
, i
++)
1334 GtkLabel
*item
= GTK_LABEL(GTK_BIN(next
->data
)->child
);
1337 /* The label actually moves from the menu!! */
1339 item
= GTK_LABEL(GTK_BIN(om
)->child
);
1341 label
= gtk_label_get_text(item
);
1343 g_return_if_fail(label
!= NULL
);
1345 if (strcmp(label
, option
->value
) == 0)
1351 gtk_option_menu_set_history(om
, i
);
1353 g_warning("Theme '%s' not found", option
->value
);
1356 static void add_themes_from_dir(GPtrArray
*names
, const char *dir
)
1361 if (access(dir
, F_OK
) != 0)
1364 list
= list_dir(dir
);
1365 g_return_if_fail(list
!= NULL
);
1367 for (i
= 0; i
< list
->len
; i
++)
1371 index_path
= g_build_filename(dir
, list
->pdata
[i
],
1372 "index.theme", NULL
);
1374 if (access(index_path
, F_OK
) == 0)
1375 g_ptr_array_add(names
, list
->pdata
[i
]);
1377 g_free(list
->pdata
[i
]);
1382 g_ptr_array_free(list
, TRUE
);
1385 static GList
*build_icon_theme(Option
*option
, xmlNode
*node
, guchar
*label
)
1387 GtkWidget
*button
, *menu
, *hbox
;
1389 gchar
**theme_dirs
= NULL
;
1393 g_return_val_if_fail(option
!= NULL
, NULL
);
1394 g_return_val_if_fail(label
!= NULL
, NULL
);
1396 hbox
= gtk_hbox_new(FALSE
, 4);
1398 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_(label
)),
1401 button
= gtk_option_menu_new();
1402 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
1404 menu
= gtk_menu_new();
1405 gtk_option_menu_set_menu(GTK_OPTION_MENU(button
), menu
);
1407 gtk_icon_theme_get_search_path(icon_theme
, &theme_dirs
, &n_dirs
);
1408 names
= g_ptr_array_new();
1409 for (i
= 0; i
< n_dirs
; i
++)
1410 add_themes_from_dir(names
, theme_dirs
[i
]);
1411 g_strfreev(theme_dirs
);
1413 g_ptr_array_sort(names
, strcmp2
);
1415 for (i
= 0; i
< names
->len
; i
++)
1418 char *name
= names
->pdata
[i
];
1420 item
= gtk_menu_item_new_with_label(name
);
1421 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
1422 gtk_widget_show_all(item
);
1427 g_ptr_array_free(names
, TRUE
);
1429 option
->update_widget
= update_theme
;
1430 option
->read_widget
= read_theme
;
1431 option
->widget
= button
;
1433 gtk_signal_connect_object(GTK_OBJECT(button
), "changed",
1434 GTK_SIGNAL_FUNC(option_check_widget
),
1435 (GtkObject
*) option
);
1437 return g_list_append(NULL
, hbox
);
1440 GtkIconInfo
*theme_lookup_icon(const gchar
*icon_name
, gint size
,
1441 GtkIconLookupFlags flags
)
1443 GtkIconInfo
*result
= gtk_icon_theme_lookup_icon(icon_theme
,
1444 icon_name
, size
, flags
);
1446 if (!result
&& icon_theme
!= rox_theme
)
1449 result
= gtk_icon_theme_lookup_icon(rox_theme
,
1450 icon_name
, size
, flags
);
1452 if (!result
&& icon_theme
!= gnome_theme
)
1455 result
= gtk_icon_theme_lookup_icon(gnome_theme
,
1456 icon_name
, size
, flags
);
1461 GdkPixbuf
*theme_load_icon(const gchar
*icon_name
, gint size
,
1462 GtkIconLookupFlags flags
, GError
**perror
)
1465 GdkPixbuf
*result
= gtk_icon_theme_load_icon(icon_theme
,
1466 icon_name
, size
, flags
, &err
);
1468 if (!result
&& icon_theme
!= gnome_theme
)
1476 result
= gtk_icon_theme_load_icon(gnome_theme
,
1477 icon_name
, size
, flags
, &err
);
1479 if (!result
&& icon_theme
!= rox_theme
)
1487 result
= gtk_icon_theme_load_icon(rox_theme
,
1488 icon_name
, size
, flags
, &err
);