r4716: Fixes for the filter directories option: Initialize properly, inherit from
[rox-filer.git] / ROX-Filer / src / type.c
blob2dc2c1825091c472b9304fb8e0fa210adaba6e89
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 if (slash == NULL)
190 g_warning("MIME type '%s' does not contain a '/' character!",
191 type_name);
192 return NULL;
195 mtype = g_new(MIME_type, 1);
196 mtype->media_type = g_strndup(type_name, slash - type_name);
197 mtype->subtype = g_strdup(slash + 1);
198 mtype->image = NULL;
199 mtype->comment = NULL;
201 mtype->executable = xdg_mime_mime_type_subclass(type_name,
202 "application/x-executable");
204 g_hash_table_insert(type_hash, g_strdup(type_name), mtype);
206 return mtype;
209 const char *basetype_name(DirItem *item)
211 if (item->flags & ITEM_FLAG_SYMLINK)
212 return _("Sym link");
213 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
214 return _("Mount point");
215 else if (item->flags & ITEM_FLAG_APPDIR)
216 return _("App dir");
218 switch (item->base_type)
220 case TYPE_FILE:
221 return _("File");
222 case TYPE_DIRECTORY:
223 return _("Dir");
224 case TYPE_CHAR_DEVICE:
225 return _("Char dev");
226 case TYPE_BLOCK_DEVICE:
227 return _("Block dev");
228 case TYPE_PIPE:
229 return _("Pipe");
230 case TYPE_SOCKET:
231 return _("Socket");
232 case TYPE_DOOR:
233 return _("Door");
236 return _("Unknown");
239 static void append_names(gpointer key, gpointer value, gpointer udata)
241 GList **list = (GList **) udata;
243 *list = g_list_prepend(*list, key);
246 /* Return list of all mime type names. Caller must free the list
247 * but NOT the strings it contains (which are never freed).
249 GList *mime_type_name_list(void)
251 GList *list = NULL;
253 g_hash_table_foreach(type_hash, append_names, &list);
254 list = g_list_sort(list, (GCompareFunc) strcmp);
256 return list;
259 /* MIME-type guessing */
261 /* Get the type of this file - stats the file and uses that if
262 * possible. For regular or missing files, uses the pathname.
264 MIME_type *type_get_type(const guchar *path)
266 DirItem *item;
267 MIME_type *type = NULL;
269 item = diritem_new("");
270 diritem_restat(path, item, NULL);
271 if (item->base_type != TYPE_ERROR)
272 type = item->mime_type;
273 diritem_free(item);
275 if (type)
276 return type;
278 type = type_from_path(path);
280 if (!type)
281 return text_plain;
283 return type;
286 /* Returns a pointer to the MIME-type.
288 * Tries all enabled methods:
289 * - Look for extended attribute
290 * - If no attribute, check file name
291 * - If no name rule, check contents
293 * NULL if we can't think of anything.
295 MIME_type *type_from_path(const char *path)
297 MIME_type *mime_type = NULL;
298 const char *type_name;
300 /* Check for extended attribute first */
301 mime_type = xtype_get(path);
302 if (mime_type)
303 return mime_type;
305 /* Try name and contents next */
306 type_name = xdg_mime_get_mime_type_for_file(path);
307 if (type_name)
308 return get_mime_type(type_name, TRUE);
310 return NULL;
313 /* Returns the file/dir in Choices for handling this type.
314 * NULL if there isn't one. g_free() the result.
316 char *handler_for(MIME_type *type)
318 char *type_name;
319 char *open;
320 char *target;
322 type_name = g_strconcat(type->media_type, "_", type->subtype, NULL);
323 open = choices_find_xdg_path_load(type_name, "MIME-types", SITE);
324 g_free(type_name);
326 if (!open)
327 open = choices_find_xdg_path_load(type->media_type,
328 "MIME-types", SITE);
330 if (!open)
331 return NULL;
333 /* Some programs behave differently depending on the command
334 * name (e.g., 'vim' vs 'gvim'), so symlinks need to be followed here.
336 target = readlink_dup(open);
337 if (!target)
339 return open;
342 if (target[0] == '/')
344 /* Absolute path */
345 g_free(open);
346 return target;
348 else
350 /* Relative path (shouldn't normally be needed) */
351 gchar *dir;
352 char *abs_path;
354 dir = g_path_get_dirname(open);
355 g_free(open);
357 abs_path = g_strconcat(dir, "/", target, NULL);
358 g_free(target);
359 g_free(dir);
361 return abs_path;
365 MIME_type *mime_type_lookup(const char *type)
367 return get_mime_type(type, TRUE);
370 /* Actions for types */
372 /* Return the image for this type, loading it if needed.
373 * Places to check are: (eg type="text_plain", base="text")
374 * 1. <Choices>/MIME-icons/base_subtype
375 * 2. Icon theme 'mime-base:subtype'
376 * 3. Icon theme 'mime-base'
377 * 4. Unknown type icon.
379 * Note: You must g_object_unref() the image afterwards.
381 MaskedPixmap *type_to_icon(MIME_type *type)
383 GtkIconInfo *full;
384 char *type_name, *path;
385 time_t now;
387 if (type == NULL)
389 g_object_ref(im_unknown);
390 return im_unknown;
393 now = time(NULL);
394 /* Already got an image? */
395 if (type->image)
397 /* Yes - don't recheck too often */
398 if (abs(now - type->image_time) < 2)
400 g_object_ref(type->image);
401 return type->image;
403 g_object_unref(type->image);
404 type->image = NULL;
407 type_name = g_strconcat(type->media_type, "_", type->subtype,
408 ".png", NULL);
409 path = choices_find_xdg_path_load(type_name, "MIME-icons", SITE);
410 g_free(type_name);
411 if (path)
413 type->image = g_fscache_lookup(pixmap_cache, path);
414 g_free(path);
417 if (type->image)
418 goto out;
420 type_name = g_strconcat("mime-", type->media_type, ":",
421 type->subtype, NULL);
422 full = gtk_icon_theme_lookup_icon(icon_theme, type_name, HUGE_HEIGHT, 0);
423 g_free(type_name);
424 if (!full)
426 /* Ugly hack... try for a GNOME icon */
427 if (type == inode_directory)
428 type_name = g_strdup("gnome-fs-directory");
429 else
430 type_name = g_strconcat("gnome-mime-", type->media_type,
431 "-", type->subtype, NULL);
432 full = gtk_icon_theme_lookup_icon(icon_theme,
433 type_name,
434 HUGE_HEIGHT, 0);
435 g_free(type_name);
437 if (!full)
439 /* Try for a media type */
440 type_name = g_strconcat("mime-", type->media_type, NULL);
441 full = gtk_icon_theme_lookup_icon(icon_theme,
442 type_name,
443 HUGE_HEIGHT, 0);
444 g_free(type_name);
446 if (!full)
448 /* Ugly hack... try for a GNOME default media icon */
449 type_name = g_strconcat("gnome-mime-", type->media_type, NULL);
451 full = gtk_icon_theme_lookup_icon(icon_theme,
452 type_name,
453 HUGE_HEIGHT, 0);
454 g_free(type_name);
456 if (full)
458 const char *icon_path;
459 /* Get the actual icon through our cache, not through GTK, because
460 * GTK doesn't cache icons.
462 icon_path = gtk_icon_info_get_filename(full);
463 if (icon_path != NULL)
464 type->image = g_fscache_lookup(pixmap_cache, icon_path);
465 /* else shouldn't happen, because we didn't use
466 * GTK_ICON_LOOKUP_USE_BUILTIN.
468 gtk_icon_info_free(full);
471 out:
472 if (!type->image)
474 /* One ref from the type structure, one returned */
475 type->image = im_unknown;
476 g_object_ref(im_unknown);
479 type->image_time = now;
481 g_object_ref(type->image);
482 return type->image;
485 GdkAtom type_to_atom(MIME_type *type)
487 char *str;
488 GdkAtom retval;
490 g_return_val_if_fail(type != NULL, GDK_NONE);
492 str = g_strconcat(type->media_type, "/", type->subtype, NULL);
493 retval = gdk_atom_intern(str, FALSE);
494 g_free(str);
496 return retval;
499 static void show_shell_help(gpointer data)
501 info_message(_("Enter a shell command which will load \"$@\" into "
502 "a suitable program. Eg:\n\n"
503 "gimp \"$@\""));
506 /* Called if the user clicks on the OK button. Returns FALSE if an error
507 * was displayed instead of performing the action.
509 static gboolean set_shell_action(GtkWidget *dialog)
511 GtkEntry *entry;
512 const guchar *command;
513 gchar *tmp, *path;
514 int error = 0, len;
515 int fd;
517 entry = g_object_get_data(G_OBJECT(dialog), "shell_command");
519 g_return_val_if_fail(entry != NULL, FALSE);
521 command = gtk_entry_get_text(entry);
523 if (!strchr(command, '$'))
525 show_shell_help(NULL);
526 return FALSE;
529 path = get_action_save_path(dialog);
530 if (!path)
531 return FALSE;
533 tmp = g_strdup_printf("#! /bin/sh\nexec %s\n", command);
534 len = strlen(tmp);
536 fd = open(path, O_CREAT | O_WRONLY, 0755);
537 if (fd == -1)
538 error = errno;
539 else
541 FILE *file;
543 file = fdopen(fd, "w");
544 if (file)
546 if (fwrite(tmp, 1, len, file) < len)
547 error = errno;
548 if (fclose(file) && error == 0)
549 error = errno;
551 else
552 error = errno;
555 if (error)
556 report_error(g_strerror(error));
558 g_free(tmp);
559 g_free(path);
561 gtk_widget_destroy(dialog);
563 return TRUE;
566 static void set_action_response(GtkWidget *dialog, gint response, gpointer data)
568 if (response == GTK_RESPONSE_OK)
569 if (!set_shell_action(dialog))
570 return;
571 gtk_widget_destroy(dialog);
574 /* Return the path of the file in choices that handles this type and
575 * radio setting.
576 * NULL if nothing is defined for it.
578 static guchar *handler_for_radios(GObject *dialog)
580 Radios *radios;
581 MIME_type *type;
583 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
584 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
586 g_return_val_if_fail(radios != NULL, NULL);
587 g_return_val_if_fail(type != NULL, NULL);
589 switch (radios_get_value(radios))
591 case SET_MEDIA:
592 return choices_find_xdg_path_load(type->media_type,
593 "MIME-types", SITE);
594 case SET_TYPE:
596 gchar *tmp, *handler;
597 tmp = g_strconcat(type->media_type, "_",
598 type->subtype, NULL);
599 handler = choices_find_xdg_path_load(tmp,
600 "MIME-types",
601 SITE);
602 g_free(tmp);
603 return handler;
605 default:
606 g_warning("Bad type");
607 return NULL;
611 /* (radios can be NULL if called from clear_run_action) */
612 static void run_action_update(Radios *radios, gpointer data)
614 guchar *handler;
615 DropBox *drop_box;
616 GObject *dialog = G_OBJECT(data);
618 drop_box = g_object_get_data(dialog, "rox-dropbox");
620 g_return_if_fail(drop_box != NULL);
622 handler = handler_for_radios(dialog);
624 if (handler)
626 char *old = handler;
628 handler = readlink_dup(old);
629 if (handler)
630 g_free(old);
631 else
632 handler = old;
635 drop_box_set_path(DROP_BOX(drop_box), handler);
636 g_free(handler);
639 static void clear_run_action(GtkWidget *drop_box, GtkWidget *dialog)
641 guchar *handler;
643 handler = handler_for_radios(G_OBJECT(dialog));
645 if (handler)
646 remove_handler_with_confirm(handler);
648 run_action_update(NULL, dialog);
651 /* Called when a URI list is dropped onto the box in the Set Run Action
652 * dialog. Make sure it's an application, and make that the default
653 * handler.
655 static void drag_app_dropped(GtkWidget *drop_box,
656 const guchar *app,
657 GtkWidget *dialog)
659 DirItem *item;
661 item = diritem_new("");
662 diritem_restat(app, item, NULL);
663 if (item->flags & ITEM_FLAG_APPDIR || EXECUTABLE_FILE(item))
665 guchar *path;
667 path = get_action_save_path(dialog);
669 if (path)
671 if (symlink(app, path))
672 delayed_error("symlink: %s",
673 g_strerror(errno));
674 else
675 destroy_on_idle(dialog);
677 g_free(path);
680 else
681 delayed_error(
682 _("This is not a program! Give me an application "
683 "instead!"));
685 diritem_free(item);
688 /* Find the current command which is used to run files of this type.
689 * Returns NULL on failure. g_free() the result.
691 static guchar *get_current_command(MIME_type *type)
693 struct stat info;
694 char *handler, *nl, *data = NULL;
695 long len;
696 guchar *command = NULL;
698 handler = handler_for(type);
700 if (!handler)
701 return NULL; /* No current handler */
703 if (stat(handler, &info))
704 goto out; /* Can't stat */
706 if ((!S_ISREG(info.st_mode)) || info.st_size > 256)
707 goto out; /* Only use small regular files */
709 if (!load_file(handler, &data, &len))
710 goto out; /* Didn't load OK */
712 if (strncmp(data, "#! /bin/sh\nexec ", 16) != 0)
713 goto out; /* Not one of ours */
715 nl = strchr(data + 16, '\n');
716 if (!nl)
717 goto out; /* No newline! */
719 command = g_strndup(data + 16, nl - data - 16);
720 out:
721 g_free(handler);
722 g_free(data);
723 return command;
726 /* Find the current command which is used to run files of this type,
727 * and return a textual description of it.
728 * Only call for non-executable files.
729 * g_free() the result.
731 gchar *describe_current_command(MIME_type *type)
733 char *handler;
734 char *desc = NULL;
735 struct stat info;
737 g_return_val_if_fail(type != NULL, NULL);
739 handler = handler_for(type);
741 if (!handler)
742 return g_strdup(_("No run action defined"));
744 if (mc_stat(handler, &info) !=0 )
746 desc = g_strdup_printf(_("Error in handler %s: %s"), handler,
747 g_strerror(errno));
748 goto out;
751 if (S_ISDIR(info.st_mode))
753 const guchar *tmp;
754 uid_t dir_uid = info.st_uid;
756 tmp = make_path(handler, "AppRun");
758 if (mc_lstat(tmp, &info) != 0 || info.st_uid != dir_uid
759 || !(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
760 desc = g_strdup_printf(
761 _("Invalid application %s (bad AppRun)"),
762 handler);
763 /* Else, just report handler... */
765 goto out;
768 /* It's not an application directory, and it's not a symlink... */
770 if (access(handler, X_OK) != 0)
772 desc = g_strdup_printf(_("Non-executable %s"), handler);
773 goto out;
776 desc = get_current_command(type);
777 out:
778 if (!desc)
779 desc = handler;
780 else
781 g_free(handler);
783 return desc;
786 /* Display a dialog box allowing the user to set the default run action
787 * for this type.
789 void type_set_handler_dialog(MIME_type *type)
791 guchar *tmp;
792 GtkDialog *dialog;
793 GtkWidget *frame, *entry, *label, *button;
794 GtkWidget *hbox;
795 Radios *radios;
797 g_return_if_fail(type != NULL);
799 dialog = GTK_DIALOG(gtk_dialog_new());
800 gtk_dialog_set_has_separator(dialog, FALSE);
801 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
803 g_object_set_data(G_OBJECT(dialog), "mime_type", type);
805 gtk_window_set_title(GTK_WINDOW(dialog), _("Set run action"));
807 radios = radios_new(run_action_update, dialog);
808 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
810 radios_add(radios,
811 _("If a handler for the specific type isn't set up, "
812 "use this as the default."), SET_MEDIA,
813 _("Set default for all `%s/<anything>'"),
814 type->media_type);
816 radios_add(radios,
817 _("Use this application for all files with this MIME "
818 "type."), SET_TYPE,
819 _("Only for the type `%s' (%s/%s)"),
820 mime_type_comment(type),
821 type->media_type, type->subtype);
823 radios_set_value(radios, SET_TYPE);
825 frame = drop_box_new(_("Drop a suitable application here"));
827 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
829 radios_pack(radios, GTK_BOX(dialog->vbox));
830 gtk_box_pack_start(GTK_BOX(dialog->vbox), frame, TRUE, TRUE, 0);
832 g_signal_connect(frame, "path_dropped",
833 G_CALLBACK(drag_app_dropped), dialog);
834 g_signal_connect(frame, "clear",
835 G_CALLBACK(clear_run_action), dialog);
837 hbox = gtk_hbox_new(FALSE, 4);
838 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 4);
839 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
840 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_("OR")),
841 FALSE, TRUE, 0);
842 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
844 hbox = gtk_hbox_new(FALSE, 4);
845 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
847 label = gtk_label_new(_("Enter a shell command:")),
848 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
849 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
851 gtk_box_pack_start(GTK_BOX(hbox),
852 new_help_button(show_shell_help, NULL), FALSE, TRUE, 0);
854 entry = gtk_entry_new();
855 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, FALSE, TRUE, 0);
856 gtk_widget_grab_focus(entry);
857 g_object_set_data(G_OBJECT(dialog), "shell_command", entry);
858 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
860 /* If possible, fill in the entry box with the current command */
861 tmp = get_current_command(type);
862 if (tmp)
864 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
865 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
866 g_free(tmp);
868 else
870 gtk_entry_set_text(GTK_ENTRY(entry), " \"$@\"");
871 gtk_editable_set_position(GTK_EDITABLE(entry), 0);
874 gtk_dialog_add_button(dialog, GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL);
876 button = button_new_mixed(GTK_STOCK_OK, _("_Use Command"));
877 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
878 gtk_dialog_add_action_widget(dialog, button, GTK_RESPONSE_OK);
880 hbox = gtk_hbox_new(TRUE, 4);
881 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
883 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
885 g_signal_connect(dialog, "response",
886 G_CALLBACK(set_action_response), NULL);
888 gtk_widget_show_all(GTK_WIDGET(dialog));
891 /* path is an entry in Choices. If it's a symlink or a very small executable
892 * then just get rid of it, otherwise confirm first. It it doesn't exist,
893 * do nothing.
895 * FALSE on error (abort operation).
897 static gboolean remove_handler_with_confirm(const guchar *path)
899 struct stat info;
901 if (lstat(path, &info) == 0)
903 /* A binding already exists... */
904 if (S_ISREG(info.st_mode) && info.st_size > 256)
906 if (!confirm(_("A run action already exists and is "
907 "quite a big program - are you sure "
908 "you want to delete it?"),
909 GTK_STOCK_DELETE, NULL))
911 return FALSE;
915 if (unlink(path))
917 report_error(_("Can't remove %s: %s"),
918 path, g_strerror(errno));
919 return FALSE;
923 return TRUE;
926 /* The user wants to set a new default action for files of this type (or just
927 * clear the action). Removes the current binding if possible and returns the
928 * path to save the new one to. NULL means cancel. g_free() the result.
930 static char *get_action_save_path(GtkWidget *dialog)
932 guchar *path = NULL;
933 guchar *type_name = NULL;
934 MIME_type *type;
935 Radios *radios;
937 g_return_val_if_fail(dialog != NULL, NULL);
939 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
940 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
942 g_return_val_if_fail(radios != NULL && type != NULL, NULL);
944 if (radios_get_value(radios) == SET_MEDIA)
945 type_name = g_strdup(type->media_type);
946 else
947 type_name = g_strconcat(type->media_type, "_",
948 type->subtype, NULL);
950 path = choices_find_xdg_path_save("", PROJECT, SITE, FALSE);
951 if (!path)
953 report_error(
954 _("Choices saving is disabled by CHOICESPATH variable"));
955 goto out;
957 g_free(path);
959 path = choices_find_xdg_path_save(type_name, "MIME-types", SITE, TRUE);
961 if (!remove_handler_with_confirm(path))
962 null_g_free(&path);
963 out:
964 g_free(type_name);
965 return path;
968 MIME_type *mime_type_from_base_type(int base_type)
970 switch (base_type)
972 case TYPE_FILE:
973 return text_plain;
974 case TYPE_DIRECTORY:
975 return inode_directory;
976 case TYPE_PIPE:
977 return inode_pipe;
978 case TYPE_SOCKET:
979 return inode_socket;
980 case TYPE_BLOCK_DEVICE:
981 return inode_block_dev;
982 case TYPE_CHAR_DEVICE:
983 return inode_char_dev;
984 case TYPE_DOOR:
985 return inode_door;
987 return inode_unknown;
990 /* Takes the st_mode field from stat() and returns the base type.
991 * Should not be a symlink.
993 int mode_to_base_type(int st_mode)
995 if (S_ISREG(st_mode))
996 return TYPE_FILE;
997 else if (S_ISDIR(st_mode))
998 return TYPE_DIRECTORY;
999 else if (S_ISBLK(st_mode))
1000 return TYPE_BLOCK_DEVICE;
1001 else if (S_ISCHR(st_mode))
1002 return TYPE_CHAR_DEVICE;
1003 else if (S_ISFIFO(st_mode))
1004 return TYPE_PIPE;
1005 else if (S_ISSOCK(st_mode))
1006 return TYPE_SOCKET;
1007 else if (S_ISDOOR(st_mode))
1008 return TYPE_DOOR;
1010 return TYPE_ERROR;
1013 /* Returns TRUE is this is something that is run by looking up its type
1014 * in MIME-types and, hence, can have its run action set.
1016 gboolean can_set_run_action(DirItem *item)
1018 g_return_val_if_fail(item != NULL, FALSE);
1020 return item->base_type == TYPE_FILE && !EXECUTABLE_FILE(item);
1023 /* Parse file type colours and allocate/free them as necessary */
1024 static void alloc_type_colours(void)
1026 gboolean success[NUM_TYPE_COLOURS];
1027 int change_count = 0; /* No. needing realloc */
1028 int i;
1029 static gboolean allocated = FALSE;
1031 /* Parse colours */
1032 for (i = 0; i < NUM_TYPE_COLOURS; i++)
1034 GdkColor *c = &type_colours[i];
1035 gushort r = c->red;
1036 gushort g = c->green;
1037 gushort b = c->blue;
1039 gdk_color_parse(o_type_colours[i].value, &type_colours[i]);
1041 if (allocated && (c->red != r || c->green != g || c->blue != b))
1042 change_count++;
1045 /* Free colours if they were previously allocated and
1046 * have changed or become unneeded.
1048 if (allocated && (change_count || !o_display_colour_types.int_value))
1050 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1051 type_colours, NUM_TYPE_COLOURS);
1052 allocated = FALSE;
1055 /* Allocate colours, unless they are still allocated (=> they didn't
1056 * change) or we don't want them anymore.
1057 * XXX: what should be done if allocation fails?
1059 if (!allocated && o_display_colour_types.int_value)
1061 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1062 type_colours, NUM_TYPE_COLOURS,
1063 FALSE, TRUE, success);
1064 allocated = TRUE;
1068 static void expire_timer(gpointer key, gpointer value, gpointer data)
1070 MIME_type *type = value;
1072 type->image_time = 0;
1075 static void options_changed(void)
1077 alloc_type_colours();
1078 if (o_icon_theme.has_changed)
1080 set_icon_theme();
1081 g_hash_table_foreach(type_hash, expire_timer, NULL);
1082 full_refresh();
1086 /* Return a pointer to a (static) colour for this item. If colouring is
1087 * off, returns normal.
1089 GdkColor *type_get_colour(DirItem *item, GdkColor *normal)
1091 int type = item->base_type;
1093 if (!o_display_colour_types.int_value)
1094 return normal;
1096 if (EXECUTABLE_FILE(item))
1097 type = TYPE_EXEC;
1098 else if (item->flags & ITEM_FLAG_APPDIR)
1099 type = TYPE_APPDIR;
1101 g_return_val_if_fail(type >= 0 && type < NUM_TYPE_COLOURS, normal);
1103 return &type_colours[type];
1106 static char **get_xdg_data_dirs(int *n_dirs)
1108 const char *env;
1109 char **dirs;
1110 int i, n;
1112 env = getenv("XDG_DATA_DIRS");
1113 if (!env)
1114 env = "/usr/local/share/:/usr/share/";
1115 dirs = g_strsplit(env, ":", 0);
1116 g_return_val_if_fail(dirs != NULL, NULL);
1117 for (n = 0; dirs[n]; n++)
1119 for (i = n; i > 0; i--)
1120 dirs[i] = dirs[i - 1];
1121 env = getenv("XDG_DATA_HOME");
1122 if (env)
1123 dirs[0] = g_strdup(env);
1124 else
1125 dirs[0] = g_build_filename(g_get_home_dir(), ".local",
1126 "share", NULL);
1127 *n_dirs = n + 1;
1128 return dirs;
1131 /* Try to fill in 'type->comment' from this document */
1132 static void get_comment(MIME_type *type, const guchar *path)
1134 xmlNode *node;
1135 XMLwrapper *doc;
1137 doc = xml_cache_load(path);
1138 if (!doc)
1139 return;
1141 node = xml_get_section(doc, TYPE_NS, "comment");
1143 if (node)
1145 char *val;
1146 g_return_if_fail(type->comment == NULL);
1147 val= xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
1148 type->comment = g_strdup(val);
1149 xmlFree(val);
1152 g_object_unref(doc);
1155 /* Fill in the comment field for this MIME type */
1156 static void find_comment(MIME_type *type)
1158 char **dirs;
1159 int i, n_dirs = 0;
1161 if (type->comment)
1163 g_free(type->comment);
1164 type->comment = NULL;
1167 dirs = get_xdg_data_dirs(&n_dirs);
1168 g_return_if_fail(dirs != NULL);
1170 for (i = 0; i < n_dirs; i++)
1172 guchar *path;
1174 path = g_strdup_printf("%s/mime/%s/%s.xml", dirs[i],
1175 type->media_type, type->subtype);
1176 get_comment(type, path);
1177 g_free(path);
1178 if (type->comment)
1179 break;
1182 if (!type->comment)
1183 type->comment = g_strdup_printf("%s/%s", type->media_type,
1184 type->subtype);
1186 for (i = 0; i < n_dirs; i++)
1187 g_free(dirs[i]);
1188 g_free(dirs);
1191 const char *mime_type_comment(MIME_type *type)
1193 if (!type->comment)
1194 find_comment(type);
1196 return type->comment;
1199 static void set_icon_theme(void)
1201 GtkIconInfo *info;
1202 char *icon_home;
1203 const char *theme_name = o_icon_theme.value;
1205 if (!theme_name || !*theme_name)
1206 theme_name = "ROX";
1208 while (1)
1210 gtk_icon_theme_set_custom_theme(icon_theme, theme_name);
1211 info = gtk_icon_theme_lookup_icon(icon_theme,
1212 "mime-application:postscript",
1213 ICON_HEIGHT, 0);
1214 if (!info)
1216 info = gtk_icon_theme_lookup_icon(icon_theme,
1217 "gnome-mime-application-postscript",
1218 ICON_HEIGHT, 0);
1220 if (info)
1222 gtk_icon_info_free(info);
1223 return;
1226 if (strcmp(theme_name, "ROX") == 0)
1227 break;
1229 delayed_error(_("Icon theme '%s' does not contain MIME icons. "
1230 "Using ROX default theme instead."),
1231 theme_name);
1233 theme_name = "ROX";
1236 icon_home = g_build_filename(home_dir, ".icons", NULL);
1237 if (!file_exists(icon_home))
1238 mkdir(icon_home, 0755);
1239 g_free(icon_home);
1241 icon_home = g_build_filename(home_dir, ".icons", "ROX", NULL);
1242 if (symlink(make_path(app_dir, "ROX"), icon_home))
1244 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1245 "(this may mean that the ROX theme already exists there, but "
1246 "the 'mime-application:postscript' icon couldn't be loaded for "
1247 "some reason, or %s is a link to an invalid directory; try "
1248 "deleting it)"), icon_home, g_strerror(errno), icon_home);
1249 open_to_show(icon_home);
1251 g_free(icon_home);
1253 gtk_icon_theme_rescan_if_needed(icon_theme);
1256 static guchar *read_theme(Option *option)
1258 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1259 GtkLabel *item;
1261 item = GTK_LABEL(GTK_BIN(om)->child);
1263 g_return_val_if_fail(item != NULL, g_strdup("ROX"));
1265 return g_strdup(gtk_label_get_text(item));
1268 static void update_theme(Option *option)
1270 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1271 GtkWidget *menu;
1272 GList *kids, *next;
1273 int i = 0;
1275 menu = gtk_option_menu_get_menu(om);
1277 kids = gtk_container_get_children(GTK_CONTAINER(menu));
1278 for (next = kids; next; next = next->next, i++)
1280 GtkLabel *item = GTK_LABEL(GTK_BIN(next->data)->child);
1281 const gchar *label;
1283 /* The label actually moves from the menu!! */
1284 if (!item)
1285 item = GTK_LABEL(GTK_BIN(om)->child);
1287 label = gtk_label_get_text(item);
1289 g_return_if_fail(label != NULL);
1291 if (strcmp(label, option->value) == 0)
1292 break;
1294 g_list_free(kids);
1296 if (next)
1297 gtk_option_menu_set_history(om, i);
1298 else
1299 g_warning("Theme '%s' not found", option->value);
1302 static void add_themes_from_dir(GPtrArray *names, const char *dir)
1304 GPtrArray *list;
1305 int i;
1307 if (access(dir, F_OK) != 0)
1308 return;
1310 list = list_dir(dir);
1311 g_return_if_fail(list != NULL);
1313 for (i = 0; i < list->len; i++)
1315 char *index_path;
1317 index_path = g_build_filename(dir, list->pdata[i],
1318 "index.theme", NULL);
1320 if (access(index_path, F_OK) == 0)
1321 g_ptr_array_add(names, list->pdata[i]);
1322 else
1323 g_free(list->pdata[i]);
1325 g_free(index_path);
1328 g_ptr_array_free(list, TRUE);
1331 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label)
1333 GtkWidget *button, *menu, *hbox;
1334 GPtrArray *names;
1335 gchar **theme_dirs = NULL;
1336 gint n_dirs = 0;
1337 int i;
1339 g_return_val_if_fail(option != NULL, NULL);
1340 g_return_val_if_fail(label != NULL, NULL);
1342 hbox = gtk_hbox_new(FALSE, 4);
1344 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1345 FALSE, TRUE, 0);
1347 button = gtk_option_menu_new();
1348 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1350 menu = gtk_menu_new();
1351 gtk_option_menu_set_menu(GTK_OPTION_MENU(button), menu);
1353 gtk_icon_theme_get_search_path(icon_theme, &theme_dirs, &n_dirs);
1354 names = g_ptr_array_new();
1355 for (i = 0; i < n_dirs; i++)
1356 add_themes_from_dir(names, theme_dirs[i]);
1357 g_strfreev(theme_dirs);
1359 g_ptr_array_sort(names, strcmp2);
1361 for (i = 0; i < names->len; i++)
1363 GtkWidget *item;
1364 char *name = names->pdata[i];
1366 item = gtk_menu_item_new_with_label(name);
1367 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1368 gtk_widget_show_all(item);
1370 g_free(name);
1373 g_ptr_array_free(names, TRUE);
1375 option->update_widget = update_theme;
1376 option->read_widget = read_theme;
1377 option->widget = button;
1379 gtk_signal_connect_object(GTK_OBJECT(button), "changed",
1380 GTK_SIGNAL_FUNC(option_check_widget),
1381 (GtkObject *) option);
1383 return g_list_append(NULL, hbox);