Don't allow SetIcon/UnsetIcon SOAP calls to (un)set icons set by the user.
[rox-filer/dt.git] / ROX-Filer / src / type.c
blob27097d202f500dbdbb5c0174722b4d6a817d3609
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 struct mime_list {
240 GList *list;
241 gboolean only_regular;
244 static void append_names(gpointer key, gpointer value, gpointer udata)
246 struct mime_list *mlist = (struct mime_list*) udata;
248 if(!mlist->only_regular || strncmp((char *)key, "inode/", 6)!=0)
249 mlist->list = g_list_prepend(mlist->list, key);
252 /* Return list of all mime type names. Caller must free the list
253 * but NOT the strings it contains (which are never freed).
254 If only_regular is true then inode types are excluded.
256 GList *mime_type_name_list(gboolean only_regular)
258 struct mime_list list;
260 list.list=NULL;
261 list.only_regular=only_regular;
263 g_hash_table_foreach(type_hash, append_names, &list);
264 list.list = g_list_sort(list.list, (GCompareFunc) strcmp);
266 return list.list;
269 /* MIME-type guessing */
271 /* Get the type of this file - stats the file and uses that if
272 * possible. For regular or missing files, uses the pathname.
274 MIME_type *type_get_type(const guchar *path)
276 DirItem *item;
277 MIME_type *type = NULL;
279 item = diritem_new("");
280 diritem_restat(path, item, NULL);
281 if (item->base_type != TYPE_ERROR)
282 type = item->mime_type;
283 diritem_free(item);
285 if (type)
286 return type;
288 type = type_from_path(path);
290 if (!type)
291 return text_plain;
293 return type;
296 /* Returns a pointer to the MIME-type.
298 * Tries all enabled methods:
299 * - Look for extended attribute
300 * - If no attribute, check file name
301 * - If no name rule, check contents
303 * NULL if we can't think of anything.
305 MIME_type *type_from_path(const char *path)
307 MIME_type *mime_type = NULL;
308 const char *type_name;
310 /* Check for extended attribute first */
311 mime_type = xtype_get(path);
312 if (mime_type)
313 return mime_type;
315 /* Try name and contents next */
316 type_name = xdg_mime_get_mime_type_for_file(path);
317 if (type_name)
318 return get_mime_type(type_name, TRUE);
320 return NULL;
323 /* Returns the file/dir in Choices for handling this type.
324 * NULL if there isn't one. g_free() the result.
326 char *handler_for(MIME_type *type)
328 char *type_name;
329 char *open;
330 char *target;
332 type_name = g_strconcat(type->media_type, "_", type->subtype, NULL);
333 open = choices_find_xdg_path_load(type_name, "MIME-types", SITE);
334 g_free(type_name);
336 if (!open)
337 open = choices_find_xdg_path_load(type->media_type,
338 "MIME-types", SITE);
340 if (!open)
341 return NULL;
343 /* Some programs behave differently depending on the command
344 * name (e.g., 'vim' vs 'gvim'), so symlinks need to be followed here.
346 target = readlink_dup(open);
347 if (!target)
349 return open;
352 if (target[0] == '/')
354 /* Absolute path */
355 g_free(open);
356 return target;
358 else
360 /* Relative path (shouldn't normally be needed) */
361 gchar *dir;
362 char *abs_path;
364 dir = g_path_get_dirname(open);
365 g_free(open);
367 abs_path = g_strconcat(dir, "/", target, NULL);
368 g_free(target);
369 g_free(dir);
371 return abs_path;
375 MIME_type *mime_type_lookup(const char *type)
377 return get_mime_type(type, TRUE);
380 /* Actions for types */
382 /* Return the image for this type, loading it if needed.
383 * Places to check are: (eg type="text_plain", base="text")
384 * 1. <Choices>/MIME-icons/base_subtype
385 * 2. Icon theme 'mime-base:subtype'
386 * 3. Icon theme 'mime-base'
387 * 4. Unknown type icon.
389 * Note: You must g_object_unref() the image afterwards.
391 MaskedPixmap *type_to_icon(MIME_type *type)
393 GtkIconInfo *full;
394 char *type_name, *path;
395 time_t now;
397 if (type == NULL)
399 g_object_ref(im_unknown);
400 return im_unknown;
403 now = time(NULL);
404 /* Already got an image? */
405 if (type->image)
407 /* Yes - don't recheck too often */
408 if (abs(now - type->image_time) < 2)
410 g_object_ref(type->image);
411 return type->image;
413 g_object_unref(type->image);
414 type->image = NULL;
417 type_name = g_strconcat(type->media_type, "_", type->subtype,
418 ".png", NULL);
419 path = choices_find_xdg_path_load(type_name, "MIME-icons", SITE);
420 g_free(type_name);
421 if (path)
423 type->image = g_fscache_lookup(pixmap_cache, path);
424 g_free(path);
427 if (type->image)
428 goto out;
430 type_name = g_strconcat("mime-", type->media_type, ":",
431 type->subtype, NULL);
432 full = gtk_icon_theme_lookup_icon(icon_theme, type_name, HUGE_HEIGHT, 0);
433 g_free(type_name);
434 if (!full)
436 /* Ugly hack... try for a GNOME icon */
437 if (type == inode_directory)
438 type_name = g_strdup("gnome-fs-directory");
439 else
440 type_name = g_strconcat("gnome-mime-", type->media_type,
441 "-", type->subtype, NULL);
442 full = gtk_icon_theme_lookup_icon(icon_theme,
443 type_name,
444 HUGE_HEIGHT, 0);
445 g_free(type_name);
447 if (!full)
449 /* Try for a media type */
450 type_name = g_strconcat("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 /* Ugly hack... try for a GNOME default media icon */
459 type_name = g_strconcat("gnome-mime-", type->media_type, NULL);
461 full = gtk_icon_theme_lookup_icon(icon_theme,
462 type_name,
463 HUGE_HEIGHT, 0);
464 g_free(type_name);
466 if (full)
468 const char *icon_path;
469 /* Get the actual icon through our cache, not through GTK, because
470 * GTK doesn't cache icons.
472 icon_path = gtk_icon_info_get_filename(full);
473 if (icon_path != NULL)
474 type->image = g_fscache_lookup(pixmap_cache, icon_path);
475 /* else shouldn't happen, because we didn't use
476 * GTK_ICON_LOOKUP_USE_BUILTIN.
478 gtk_icon_info_free(full);
481 out:
482 if (!type->image)
484 /* One ref from the type structure, one returned */
485 type->image = im_unknown;
486 g_object_ref(im_unknown);
489 type->image_time = now;
491 g_object_ref(type->image);
492 return type->image;
495 GdkAtom type_to_atom(MIME_type *type)
497 char *str;
498 GdkAtom retval;
500 g_return_val_if_fail(type != NULL, GDK_NONE);
502 str = g_strconcat(type->media_type, "/", type->subtype, NULL);
503 retval = gdk_atom_intern(str, FALSE);
504 g_free(str);
506 return retval;
509 static void show_shell_help(gpointer data)
511 info_message(_("Enter a shell command which will load \"$@\" into "
512 "a suitable program. Eg:\n\n"
513 "gimp \"$@\""));
516 /* Called if the user clicks on the OK button. Returns FALSE if an error
517 * was displayed instead of performing the action.
519 static gboolean set_shell_action(GtkWidget *dialog)
521 GtkEntry *entry;
522 const guchar *command;
523 gchar *tmp, *path;
524 int error = 0, len;
525 int fd;
527 entry = g_object_get_data(G_OBJECT(dialog), "shell_command");
529 g_return_val_if_fail(entry != NULL, FALSE);
531 command = gtk_entry_get_text(entry);
533 if (!strchr(command, '$'))
535 show_shell_help(NULL);
536 return FALSE;
539 path = get_action_save_path(dialog);
540 if (!path)
541 return FALSE;
543 tmp = g_strdup_printf("#! /bin/sh\nexec %s\n", command);
544 len = strlen(tmp);
546 fd = open(path, O_CREAT | O_WRONLY, 0755);
547 if (fd == -1)
548 error = errno;
549 else
551 FILE *file;
553 file = fdopen(fd, "w");
554 if (file)
556 if (fwrite(tmp, 1, len, file) < len)
557 error = errno;
558 if (fclose(file) && error == 0)
559 error = errno;
561 else
562 error = errno;
565 if (error)
566 report_error(g_strerror(error));
568 g_free(tmp);
569 g_free(path);
571 gtk_widget_destroy(dialog);
573 return TRUE;
576 static void set_action_response(GtkWidget *dialog, gint response, gpointer data)
578 if (response == GTK_RESPONSE_OK)
579 if (!set_shell_action(dialog))
580 return;
581 gtk_widget_destroy(dialog);
584 /* Return the path of the file in choices that handles this type and
585 * radio setting.
586 * NULL if nothing is defined for it.
588 static guchar *handler_for_radios(GObject *dialog)
590 Radios *radios;
591 MIME_type *type;
593 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
594 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
596 g_return_val_if_fail(radios != NULL, NULL);
597 g_return_val_if_fail(type != NULL, NULL);
599 switch (radios_get_value(radios))
601 case SET_MEDIA:
602 return choices_find_xdg_path_load(type->media_type,
603 "MIME-types", SITE);
604 case SET_TYPE:
606 gchar *tmp, *handler;
607 tmp = g_strconcat(type->media_type, "_",
608 type->subtype, NULL);
609 handler = choices_find_xdg_path_load(tmp,
610 "MIME-types",
611 SITE);
612 g_free(tmp);
613 return handler;
615 default:
616 g_warning("Bad type");
617 return NULL;
621 /* (radios can be NULL if called from clear_run_action) */
622 static void run_action_update(Radios *radios, gpointer data)
624 guchar *handler;
625 DropBox *drop_box;
626 GObject *dialog = G_OBJECT(data);
628 drop_box = g_object_get_data(dialog, "rox-dropbox");
630 g_return_if_fail(drop_box != NULL);
632 handler = handler_for_radios(dialog);
634 if (handler)
636 char *old = handler;
638 handler = readlink_dup(old);
639 if (handler)
640 g_free(old);
641 else
642 handler = old;
645 drop_box_set_path(DROP_BOX(drop_box), handler);
646 g_free(handler);
649 static void clear_run_action(GtkWidget *drop_box, GtkWidget *dialog)
651 guchar *handler;
653 handler = handler_for_radios(G_OBJECT(dialog));
655 if (handler)
656 remove_handler_with_confirm(handler);
658 run_action_update(NULL, dialog);
661 /* Called when a URI list is dropped onto the box in the Set Run Action
662 * dialog. Make sure it's an application, and make that the default
663 * handler.
665 static void drag_app_dropped(GtkWidget *drop_box,
666 const guchar *app,
667 GtkWidget *dialog)
669 DirItem *item;
671 item = diritem_new("");
672 diritem_restat(app, item, NULL);
673 if (item->flags & ITEM_FLAG_APPDIR || EXECUTABLE_FILE(item))
675 guchar *path;
677 path = get_action_save_path(dialog);
679 if (path)
681 if (symlink(app, path))
682 delayed_error("symlink: %s",
683 g_strerror(errno));
684 else
685 destroy_on_idle(dialog);
687 g_free(path);
690 else
691 delayed_error(
692 _("This is not a program! Give me an application "
693 "instead!"));
695 diritem_free(item);
698 /* Find the current command which is used to run files of this type.
699 * Returns NULL on failure. g_free() the result.
701 static guchar *get_current_command(MIME_type *type)
703 struct stat info;
704 char *handler, *nl, *data = NULL;
705 long len;
706 guchar *command = NULL;
708 handler = handler_for(type);
710 if (!handler)
711 return NULL; /* No current handler */
713 if (stat(handler, &info))
714 goto out; /* Can't stat */
716 if ((!S_ISREG(info.st_mode)) || info.st_size > 256)
717 goto out; /* Only use small regular files */
719 if (!load_file(handler, &data, &len))
720 goto out; /* Didn't load OK */
722 if (strncmp(data, "#! /bin/sh\nexec ", 16) != 0)
723 goto out; /* Not one of ours */
725 nl = strchr(data + 16, '\n');
726 if (!nl)
727 goto out; /* No newline! */
729 command = g_strndup(data + 16, nl - data - 16);
730 out:
731 g_free(handler);
732 g_free(data);
733 return command;
736 /* Find the current command which is used to run files of this type,
737 * and return a textual description of it.
738 * Only call for non-executable files.
739 * g_free() the result.
741 gchar *describe_current_command(MIME_type *type)
743 char *handler;
744 char *desc = NULL;
745 struct stat info;
747 g_return_val_if_fail(type != NULL, NULL);
749 handler = handler_for(type);
751 if (!handler)
752 return g_strdup(_("No run action defined"));
754 if (mc_stat(handler, &info) !=0 )
756 desc = g_strdup_printf(_("Error in handler %s: %s"), handler,
757 g_strerror(errno));
758 goto out;
761 if (S_ISDIR(info.st_mode))
763 const guchar *tmp;
764 uid_t dir_uid = info.st_uid;
766 tmp = make_path(handler, "AppRun");
768 if (mc_lstat(tmp, &info) != 0 || info.st_uid != dir_uid
769 || !(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
770 desc = g_strdup_printf(
771 _("Invalid application %s (bad AppRun)"),
772 handler);
773 /* Else, just report handler... */
775 goto out;
778 /* It's not an application directory, and it's not a symlink... */
780 if (access(handler, X_OK) != 0)
782 desc = g_strdup_printf(_("Non-executable %s"), handler);
783 goto out;
786 desc = get_current_command(type);
787 out:
788 if (!desc)
789 desc = handler;
790 else
791 g_free(handler);
793 return desc;
796 /* Display a dialog box allowing the user to set the default run action
797 * for this type.
799 void type_set_handler_dialog(MIME_type *type)
801 guchar *tmp;
802 GtkDialog *dialog;
803 GtkWidget *frame, *entry, *label, *button;
804 GtkWidget *hbox;
805 Radios *radios;
807 g_return_if_fail(type != NULL);
809 dialog = GTK_DIALOG(gtk_dialog_new());
810 gtk_dialog_set_has_separator(dialog, FALSE);
811 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
813 g_object_set_data(G_OBJECT(dialog), "mime_type", type);
815 gtk_window_set_title(GTK_WINDOW(dialog), _("Set run action"));
817 radios = radios_new(run_action_update, dialog);
818 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
820 radios_add(radios,
821 _("If a handler for the specific type isn't set up, "
822 "use this as the default."), SET_MEDIA,
823 _("Set default for all `%s/<anything>'"),
824 type->media_type);
826 radios_add(radios,
827 _("Use this application for all files with this MIME "
828 "type."), SET_TYPE,
829 _("Only for the type `%s' (%s/%s)"),
830 mime_type_comment(type),
831 type->media_type, type->subtype);
833 radios_set_value(radios, SET_TYPE);
835 frame = drop_box_new(_("Drop a suitable application here"));
837 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
839 radios_pack(radios, GTK_BOX(dialog->vbox));
840 gtk_box_pack_start(GTK_BOX(dialog->vbox), frame, TRUE, TRUE, 0);
842 g_signal_connect(frame, "path_dropped",
843 G_CALLBACK(drag_app_dropped), dialog);
844 g_signal_connect(frame, "clear",
845 G_CALLBACK(clear_run_action), dialog);
847 hbox = gtk_hbox_new(FALSE, 4);
848 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 4);
849 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
850 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_("OR")),
851 FALSE, TRUE, 0);
852 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
854 hbox = gtk_hbox_new(FALSE, 4);
855 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
857 label = gtk_label_new(_("Enter a shell command:")),
858 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
859 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
861 gtk_box_pack_start(GTK_BOX(hbox),
862 new_help_button(show_shell_help, NULL), FALSE, TRUE, 0);
864 entry = gtk_entry_new();
865 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, FALSE, TRUE, 0);
866 gtk_widget_grab_focus(entry);
867 g_object_set_data(G_OBJECT(dialog), "shell_command", entry);
868 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
870 /* If possible, fill in the entry box with the current command */
871 tmp = get_current_command(type);
872 if (tmp)
874 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
875 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
876 g_free(tmp);
878 else
880 gtk_entry_set_text(GTK_ENTRY(entry), " \"$@\"");
881 gtk_editable_set_position(GTK_EDITABLE(entry), 0);
884 gtk_dialog_add_button(dialog, GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL);
886 button = button_new_mixed(GTK_STOCK_OK, _("_Use Command"));
887 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
888 gtk_dialog_add_action_widget(dialog, button, GTK_RESPONSE_OK);
890 hbox = gtk_hbox_new(TRUE, 4);
891 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
893 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
895 g_signal_connect(dialog, "response",
896 G_CALLBACK(set_action_response), NULL);
898 gtk_widget_show_all(GTK_WIDGET(dialog));
901 /* path is an entry in Choices. If it's a symlink or a very small executable
902 * then just get rid of it, otherwise confirm first. It it doesn't exist,
903 * do nothing.
905 * FALSE on error (abort operation).
907 static gboolean remove_handler_with_confirm(const guchar *path)
909 struct stat info;
911 if (lstat(path, &info) == 0)
913 /* A binding already exists... */
914 if (S_ISREG(info.st_mode) && info.st_size > 256)
916 if (!confirm(_("A run action already exists and is "
917 "quite a big program - are you sure "
918 "you want to delete it?"),
919 GTK_STOCK_DELETE, NULL))
921 return FALSE;
925 if (unlink(path))
927 report_error(_("Can't remove %s: %s"),
928 path, g_strerror(errno));
929 return FALSE;
933 return TRUE;
936 /* The user wants to set a new default action for files of this type (or just
937 * clear the action). Removes the current binding if possible and returns the
938 * path to save the new one to. NULL means cancel. g_free() the result.
940 static char *get_action_save_path(GtkWidget *dialog)
942 guchar *path = NULL;
943 guchar *type_name = NULL;
944 MIME_type *type;
945 Radios *radios;
947 g_return_val_if_fail(dialog != NULL, NULL);
949 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
950 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
952 g_return_val_if_fail(radios != NULL && type != NULL, NULL);
954 if (radios_get_value(radios) == SET_MEDIA)
955 type_name = g_strdup(type->media_type);
956 else
957 type_name = g_strconcat(type->media_type, "_",
958 type->subtype, NULL);
960 path = choices_find_xdg_path_save("", PROJECT, SITE, FALSE);
961 if (!path)
963 report_error(
964 _("Choices saving is disabled by CHOICESPATH variable"));
965 goto out;
967 g_free(path);
969 path = choices_find_xdg_path_save(type_name, "MIME-types", SITE, TRUE);
971 if (!remove_handler_with_confirm(path))
972 null_g_free(&path);
973 out:
974 g_free(type_name);
975 return path;
978 MIME_type *mime_type_from_base_type(int base_type)
980 switch (base_type)
982 case TYPE_FILE:
983 return text_plain;
984 case TYPE_DIRECTORY:
985 return inode_directory;
986 case TYPE_PIPE:
987 return inode_pipe;
988 case TYPE_SOCKET:
989 return inode_socket;
990 case TYPE_BLOCK_DEVICE:
991 return inode_block_dev;
992 case TYPE_CHAR_DEVICE:
993 return inode_char_dev;
994 case TYPE_DOOR:
995 return inode_door;
997 return inode_unknown;
1000 /* Takes the st_mode field from stat() and returns the base type.
1001 * Should not be a symlink.
1003 int mode_to_base_type(int st_mode)
1005 if (S_ISREG(st_mode))
1006 return TYPE_FILE;
1007 else if (S_ISDIR(st_mode))
1008 return TYPE_DIRECTORY;
1009 else if (S_ISBLK(st_mode))
1010 return TYPE_BLOCK_DEVICE;
1011 else if (S_ISCHR(st_mode))
1012 return TYPE_CHAR_DEVICE;
1013 else if (S_ISFIFO(st_mode))
1014 return TYPE_PIPE;
1015 else if (S_ISSOCK(st_mode))
1016 return TYPE_SOCKET;
1017 else if (S_ISDOOR(st_mode))
1018 return TYPE_DOOR;
1020 return TYPE_ERROR;
1023 /* Returns TRUE is this is something that is run by looking up its type
1024 * in MIME-types and, hence, can have its run action set.
1026 gboolean can_set_run_action(DirItem *item)
1028 g_return_val_if_fail(item != NULL, FALSE);
1030 return item->base_type == TYPE_FILE && !EXECUTABLE_FILE(item);
1033 /* Parse file type colours and allocate/free them as necessary */
1034 static void alloc_type_colours(void)
1036 gboolean success[NUM_TYPE_COLOURS];
1037 int change_count = 0; /* No. needing realloc */
1038 int i;
1039 static gboolean allocated = FALSE;
1041 /* Parse colours */
1042 for (i = 0; i < NUM_TYPE_COLOURS; i++)
1044 GdkColor *c = &type_colours[i];
1045 gushort r = c->red;
1046 gushort g = c->green;
1047 gushort b = c->blue;
1049 gdk_color_parse(o_type_colours[i].value, &type_colours[i]);
1051 if (allocated && (c->red != r || c->green != g || c->blue != b))
1052 change_count++;
1055 /* Free colours if they were previously allocated and
1056 * have changed or become unneeded.
1058 if (allocated && (change_count || !o_display_colour_types.int_value))
1060 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1061 type_colours, NUM_TYPE_COLOURS);
1062 allocated = FALSE;
1065 /* Allocate colours, unless they are still allocated (=> they didn't
1066 * change) or we don't want them anymore.
1067 * XXX: what should be done if allocation fails?
1069 if (!allocated && o_display_colour_types.int_value)
1071 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1072 type_colours, NUM_TYPE_COLOURS,
1073 FALSE, TRUE, success);
1074 allocated = TRUE;
1078 static void expire_timer(gpointer key, gpointer value, gpointer data)
1080 MIME_type *type = value;
1082 type->image_time = 0;
1085 static void options_changed(void)
1087 alloc_type_colours();
1088 if (o_icon_theme.has_changed)
1090 set_icon_theme();
1091 g_hash_table_foreach(type_hash, expire_timer, NULL);
1092 full_refresh();
1096 /* Return a pointer to a (static) colour for this item. If colouring is
1097 * off, returns normal.
1099 GdkColor *type_get_colour(DirItem *item, GdkColor *normal)
1101 int type = item->base_type;
1103 if (!o_display_colour_types.int_value)
1104 return normal;
1106 if (EXECUTABLE_FILE(item))
1107 type = TYPE_EXEC;
1108 else if (item->flags & ITEM_FLAG_APPDIR)
1109 type = TYPE_APPDIR;
1111 g_return_val_if_fail(type >= 0 && type < NUM_TYPE_COLOURS, normal);
1113 return &type_colours[type];
1116 static char **get_xdg_data_dirs(int *n_dirs)
1118 const char *env;
1119 char **dirs;
1120 int i, n;
1122 env = getenv("XDG_DATA_DIRS");
1123 if (!env)
1124 env = "/usr/local/share/:/usr/share/";
1125 dirs = g_strsplit(env, ":", 0);
1126 g_return_val_if_fail(dirs != NULL, NULL);
1127 for (n = 0; dirs[n]; n++)
1129 for (i = n; i > 0; i--)
1130 dirs[i] = dirs[i - 1];
1131 env = getenv("XDG_DATA_HOME");
1132 if (env)
1133 dirs[0] = g_strdup(env);
1134 else
1135 dirs[0] = g_build_filename(g_get_home_dir(), ".local",
1136 "share", NULL);
1137 *n_dirs = n + 1;
1138 return dirs;
1141 /* Try to fill in 'type->comment' from this document */
1142 static void get_comment(MIME_type *type, const guchar *path)
1144 xmlNode *node;
1145 XMLwrapper *doc;
1147 doc = xml_cache_load(path);
1148 if (!doc)
1149 return;
1151 node = xml_get_section(doc, TYPE_NS, "comment");
1153 if (node)
1155 char *val;
1156 g_return_if_fail(type->comment == NULL);
1157 val= xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
1158 type->comment = g_strdup(val);
1159 xmlFree(val);
1162 g_object_unref(doc);
1165 /* Fill in the comment field for this MIME type */
1166 static void find_comment(MIME_type *type)
1168 char **dirs;
1169 int i, n_dirs = 0;
1171 if (type->comment)
1173 g_free(type->comment);
1174 type->comment = NULL;
1177 dirs = get_xdg_data_dirs(&n_dirs);
1178 g_return_if_fail(dirs != NULL);
1180 for (i = 0; i < n_dirs; i++)
1182 guchar *path;
1184 path = g_strdup_printf("%s/mime/%s/%s.xml", dirs[i],
1185 type->media_type, type->subtype);
1186 get_comment(type, path);
1187 g_free(path);
1188 if (type->comment)
1189 break;
1192 if (!type->comment)
1193 type->comment = g_strdup_printf("%s/%s", type->media_type,
1194 type->subtype);
1196 for (i = 0; i < n_dirs; i++)
1197 g_free(dirs[i]);
1198 g_free(dirs);
1201 const char *mime_type_comment(MIME_type *type)
1203 if (!type->comment)
1204 find_comment(type);
1206 return type->comment;
1209 static void set_icon_theme(void)
1211 GtkIconInfo *info;
1212 char *icon_home;
1213 const char *theme_name = o_icon_theme.value;
1215 if (!theme_name || !*theme_name)
1216 theme_name = "ROX";
1218 while (1)
1220 gtk_icon_theme_set_custom_theme(icon_theme, theme_name);
1221 info = gtk_icon_theme_lookup_icon(icon_theme,
1222 "mime-application:postscript",
1223 ICON_HEIGHT, 0);
1224 if (!info)
1226 info = gtk_icon_theme_lookup_icon(icon_theme,
1227 "gnome-mime-application-postscript",
1228 ICON_HEIGHT, 0);
1230 if (info)
1232 gtk_icon_info_free(info);
1233 return;
1236 if (strcmp(theme_name, "ROX") == 0)
1237 break;
1239 delayed_error(_("Icon theme '%s' does not contain MIME icons. "
1240 "Using ROX default theme instead."),
1241 theme_name);
1243 theme_name = "ROX";
1246 icon_home = g_build_filename(home_dir, ".icons", NULL);
1247 if (!file_exists(icon_home))
1248 mkdir(icon_home, 0755);
1249 g_free(icon_home);
1251 icon_home = g_build_filename(home_dir, ".icons", "ROX", NULL);
1252 if (symlink(make_path(app_dir, "ROX"), icon_home))
1254 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1255 "(this may mean that the ROX theme already exists there, but "
1256 "the 'mime-application:postscript' icon couldn't be loaded for "
1257 "some reason, or %s is a link to an invalid directory; try "
1258 "deleting it)"), icon_home, g_strerror(errno), icon_home);
1259 open_to_show(icon_home);
1261 g_free(icon_home);
1263 gtk_icon_theme_rescan_if_needed(icon_theme);
1266 static guchar *read_theme(Option *option)
1268 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1269 GtkLabel *item;
1271 item = GTK_LABEL(GTK_BIN(om)->child);
1273 g_return_val_if_fail(item != NULL, g_strdup("ROX"));
1275 return g_strdup(gtk_label_get_text(item));
1278 static void update_theme(Option *option)
1280 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1281 GtkWidget *menu;
1282 GList *kids, *next;
1283 int i = 0;
1285 menu = gtk_option_menu_get_menu(om);
1287 kids = gtk_container_get_children(GTK_CONTAINER(menu));
1288 for (next = kids; next; next = next->next, i++)
1290 GtkLabel *item = GTK_LABEL(GTK_BIN(next->data)->child);
1291 const gchar *label;
1293 /* The label actually moves from the menu!! */
1294 if (!item)
1295 item = GTK_LABEL(GTK_BIN(om)->child);
1297 label = gtk_label_get_text(item);
1299 g_return_if_fail(label != NULL);
1301 if (strcmp(label, option->value) == 0)
1302 break;
1304 g_list_free(kids);
1306 if (next)
1307 gtk_option_menu_set_history(om, i);
1308 else
1309 g_warning("Theme '%s' not found", option->value);
1312 static void add_themes_from_dir(GPtrArray *names, const char *dir)
1314 GPtrArray *list;
1315 int i;
1317 if (access(dir, F_OK) != 0)
1318 return;
1320 list = list_dir(dir);
1321 g_return_if_fail(list != NULL);
1323 for (i = 0; i < list->len; i++)
1325 char *index_path;
1327 index_path = g_build_filename(dir, list->pdata[i],
1328 "index.theme", NULL);
1330 if (access(index_path, F_OK) == 0)
1331 g_ptr_array_add(names, list->pdata[i]);
1332 else
1333 g_free(list->pdata[i]);
1335 g_free(index_path);
1338 g_ptr_array_free(list, TRUE);
1341 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label)
1343 GtkWidget *button, *menu, *hbox;
1344 GPtrArray *names;
1345 gchar **theme_dirs = NULL;
1346 gint n_dirs = 0;
1347 int i;
1349 g_return_val_if_fail(option != NULL, NULL);
1350 g_return_val_if_fail(label != NULL, NULL);
1352 hbox = gtk_hbox_new(FALSE, 4);
1354 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1355 FALSE, TRUE, 0);
1357 button = gtk_option_menu_new();
1358 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1360 menu = gtk_menu_new();
1361 gtk_option_menu_set_menu(GTK_OPTION_MENU(button), menu);
1363 gtk_icon_theme_get_search_path(icon_theme, &theme_dirs, &n_dirs);
1364 names = g_ptr_array_new();
1365 for (i = 0; i < n_dirs; i++)
1366 add_themes_from_dir(names, theme_dirs[i]);
1367 g_strfreev(theme_dirs);
1369 g_ptr_array_sort(names, strcmp2);
1371 for (i = 0; i < names->len; i++)
1373 GtkWidget *item;
1374 char *name = names->pdata[i];
1376 item = gtk_menu_item_new_with_label(name);
1377 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1378 gtk_widget_show_all(item);
1380 g_free(name);
1383 g_ptr_array_free(names, TRUE);
1385 option->update_widget = update_theme;
1386 option->read_widget = read_theme;
1387 option->widget = button;
1389 gtk_signal_connect_object(GTK_OBJECT(button), "changed",
1390 GTK_SIGNAL_FUNC(option_check_widget),
1391 (GtkObject *) option);
1393 return g_list_append(NULL, hbox);