r4523: Updated headers:
[rox-filer/ma.git] / ROX-Filer / src / type.c
blob40b3172b061d422eacc2a585076d1c08f8fc4d6b
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 g_return_val_if_fail(slash != NULL, NULL); /* XXX: Report nicely */
190 mtype = g_new(MIME_type, 1);
191 mtype->media_type = g_strndup(type_name, slash - type_name);
192 mtype->subtype = g_strdup(slash + 1);
193 mtype->image = NULL;
194 mtype->comment = NULL;
196 mtype->executable = xdg_mime_mime_type_subclass(type_name,
197 "application/x-executable");
199 g_hash_table_insert(type_hash, g_strdup(type_name), mtype);
201 return mtype;
204 const char *basetype_name(DirItem *item)
206 if (item->flags & ITEM_FLAG_SYMLINK)
207 return _("Sym link");
208 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
209 return _("Mount point");
210 else if (item->flags & ITEM_FLAG_APPDIR)
211 return _("App dir");
213 switch (item->base_type)
215 case TYPE_FILE:
216 return _("File");
217 case TYPE_DIRECTORY:
218 return _("Dir");
219 case TYPE_CHAR_DEVICE:
220 return _("Char dev");
221 case TYPE_BLOCK_DEVICE:
222 return _("Block dev");
223 case TYPE_PIPE:
224 return _("Pipe");
225 case TYPE_SOCKET:
226 return _("Socket");
227 case TYPE_DOOR:
228 return _("Door");
231 return _("Unknown");
234 static void append_names(gpointer key, gpointer value, gpointer udata)
236 GList **list = (GList **) udata;
238 *list = g_list_prepend(*list, key);
241 /* Return list of all mime type names. Caller must free the list
242 * but NOT the strings it contains (which are never freed).
244 GList *mime_type_name_list(void)
246 GList *list = NULL;
248 g_hash_table_foreach(type_hash, append_names, &list);
249 list = g_list_sort(list, (GCompareFunc) strcmp);
251 return list;
254 /* MIME-type guessing */
256 /* Get the type of this file - stats the file and uses that if
257 * possible. For regular or missing files, uses the pathname.
259 MIME_type *type_get_type(const guchar *path)
261 DirItem *item;
262 MIME_type *type = NULL;
264 item = diritem_new("");
265 diritem_restat(path, item, NULL);
266 if (item->base_type != TYPE_ERROR)
267 type = item->mime_type;
268 diritem_free(item);
270 if (type)
271 return type;
273 type = type_from_path(path);
275 if (!type)
276 return text_plain;
278 return type;
281 /* Returns a pointer to the MIME-type.
283 * Tries all enabled methods:
284 * - Look for extended attribute
285 * - If no attribute, check file name
286 * - If no name rule, check contents
288 * NULL if we can't think of anything.
290 MIME_type *type_from_path(const char *path)
292 MIME_type *mime_type = NULL;
293 const char *type_name;
295 /* Check for extended attribute first */
296 mime_type = xtype_get(path);
297 if (mime_type)
298 return mime_type;
300 /* Try name and contents next */
301 type_name = xdg_mime_get_mime_type_for_file(path);
302 if (type_name)
303 return get_mime_type(type_name, TRUE);
305 return NULL;
308 /* Returns the file/dir in Choices for handling this type.
309 * NULL if there isn't one. g_free() the result.
311 char *handler_for(MIME_type *type)
313 char *type_name;
314 char *open;
316 type_name = g_strconcat(type->media_type, "_", type->subtype, NULL);
317 open = choices_find_xdg_path_load(type_name, "MIME-types", SITE);
318 g_free(type_name);
320 if (!open)
321 open = choices_find_xdg_path_load(type->media_type,
322 "MIME-types", SITE);
324 return open;
327 MIME_type *mime_type_lookup(const char *type)
329 return get_mime_type(type, TRUE);
332 /* Actions for types */
334 /* Return the image for this type, loading it if needed.
335 * Places to check are: (eg type="text_plain", base="text")
336 * 1. <Choices>/MIME-icons/base_subtype
337 * 2. Icon theme 'mime-base:subtype'
338 * 3. Icon theme 'mime-base'
339 * 4. Unknown type icon.
341 * Note: You must g_object_unref() the image afterwards.
343 MaskedPixmap *type_to_icon(MIME_type *type)
345 GdkPixbuf *full;
346 char *type_name, *path;
347 time_t now;
349 if (type == NULL)
351 g_object_ref(im_unknown);
352 return im_unknown;
355 now = time(NULL);
356 /* Already got an image? */
357 if (type->image)
359 /* Yes - don't recheck too often */
360 if (abs(now - type->image_time) < 2)
362 g_object_ref(type->image);
363 return type->image;
365 g_object_unref(type->image);
366 type->image = NULL;
369 type_name = g_strconcat(type->media_type, "_", type->subtype,
370 ".png", NULL);
371 path = choices_find_xdg_path_load(type_name, "MIME-icons", SITE);
372 g_free(type_name);
373 if (path)
375 type->image = g_fscache_lookup(pixmap_cache, path);
376 g_free(path);
379 if (type->image)
380 goto out;
382 type_name = g_strconcat("mime-", type->media_type, ":",
383 type->subtype, NULL);
384 full = gtk_icon_theme_load_icon(icon_theme, type_name, HUGE_HEIGHT,
385 0, NULL);
386 g_free(type_name);
387 if (!full)
389 /* Ugly hack... try for a GNOME icon */
390 if (type == inode_directory)
391 type_name = g_strdup("gnome-fs-directory");
392 else
393 type_name = g_strconcat("gnome-mime-", type->media_type,
394 "-", type->subtype, NULL);
395 full = gtk_icon_theme_load_icon(icon_theme,
396 type_name,
397 HUGE_HEIGHT, 0, NULL);
398 g_free(type_name);
400 if (!full)
402 /* Try for a media type */
403 type_name = g_strconcat("mime-", type->media_type, NULL);
404 full = gtk_icon_theme_load_icon(icon_theme,
405 type_name,
406 HUGE_HEIGHT, 0, NULL);
407 g_free(type_name);
409 if (!full)
411 /* Ugly hack... try for a GNOME default media icon */
412 type_name = g_strconcat("gnome-mime-", type->media_type, NULL);
414 full = gtk_icon_theme_load_icon(icon_theme,
415 type_name,
416 HUGE_HEIGHT, 0, NULL);
417 g_free(type_name);
419 if (full)
421 type->image = masked_pixmap_new(full);
422 g_object_unref(full);
425 out:
426 if (!type->image)
428 /* One ref from the type structure, one returned */
429 type->image = im_unknown;
430 g_object_ref(im_unknown);
433 type->image_time = now;
435 g_object_ref(type->image);
436 return type->image;
439 GdkAtom type_to_atom(MIME_type *type)
441 char *str;
442 GdkAtom retval;
444 g_return_val_if_fail(type != NULL, GDK_NONE);
446 str = g_strconcat(type->media_type, "/", type->subtype, NULL);
447 retval = gdk_atom_intern(str, FALSE);
448 g_free(str);
450 return retval;
453 static void show_shell_help(gpointer data)
455 info_message(_("Enter a shell command which will load \"$@\" into "
456 "a suitable program. Eg:\n\n"
457 "gimp \"$@\""));
460 /* Called if the user clicks on the OK button. Returns FALSE if an error
461 * was displayed instead of performing the action.
463 static gboolean set_shell_action(GtkWidget *dialog)
465 GtkEntry *entry;
466 const guchar *command;
467 gchar *tmp, *path;
468 int error = 0, len;
469 int fd;
471 entry = g_object_get_data(G_OBJECT(dialog), "shell_command");
473 g_return_val_if_fail(entry != NULL, FALSE);
475 command = gtk_entry_get_text(entry);
477 if (!strchr(command, '$'))
479 show_shell_help(NULL);
480 return FALSE;
483 path = get_action_save_path(dialog);
484 if (!path)
485 return FALSE;
487 tmp = g_strdup_printf("#! /bin/sh\nexec %s\n", command);
488 len = strlen(tmp);
490 fd = open(path, O_CREAT | O_WRONLY, 0755);
491 if (fd == -1)
492 error = errno;
493 else
495 FILE *file;
497 file = fdopen(fd, "w");
498 if (file)
500 if (fwrite(tmp, 1, len, file) < len)
501 error = errno;
502 if (fclose(file) && error == 0)
503 error = errno;
505 else
506 error = errno;
509 if (error)
510 report_error(g_strerror(error));
512 g_free(tmp);
513 g_free(path);
515 gtk_widget_destroy(dialog);
517 return TRUE;
520 static void set_action_response(GtkWidget *dialog, gint response, gpointer data)
522 if (response == GTK_RESPONSE_OK)
523 if (!set_shell_action(dialog))
524 return;
525 gtk_widget_destroy(dialog);
528 /* Return the path of the file in choices that handles this type and
529 * radio setting.
530 * NULL if nothing is defined for it.
532 static guchar *handler_for_radios(GObject *dialog)
534 Radios *radios;
535 MIME_type *type;
537 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
538 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
540 g_return_val_if_fail(radios != NULL, NULL);
541 g_return_val_if_fail(type != NULL, NULL);
543 switch (radios_get_value(radios))
545 case SET_MEDIA:
546 return choices_find_xdg_path_load(type->media_type,
547 "MIME-types", SITE);
548 case SET_TYPE:
550 gchar *tmp, *handler;
551 tmp = g_strconcat(type->media_type, "_",
552 type->subtype, NULL);
553 handler = choices_find_xdg_path_load(tmp,
554 "MIME-types",
555 SITE);
556 g_free(tmp);
557 return handler;
559 default:
560 g_warning("Bad type");
561 return NULL;
565 /* (radios can be NULL if called from clear_run_action) */
566 static void run_action_update(Radios *radios, gpointer data)
568 guchar *handler;
569 DropBox *drop_box;
570 GObject *dialog = G_OBJECT(data);
572 drop_box = g_object_get_data(dialog, "rox-dropbox");
574 g_return_if_fail(drop_box != NULL);
576 handler = handler_for_radios(dialog);
578 if (handler)
580 char *old = handler;
582 handler = readlink_dup(old);
583 if (handler)
584 g_free(old);
585 else
586 handler = old;
589 drop_box_set_path(DROP_BOX(drop_box), handler);
590 g_free(handler);
593 static void clear_run_action(GtkWidget *drop_box, GtkWidget *dialog)
595 guchar *handler;
597 handler = handler_for_radios(G_OBJECT(dialog));
599 if (handler)
600 remove_handler_with_confirm(handler);
602 run_action_update(NULL, dialog);
605 /* Called when a URI list is dropped onto the box in the Set Run Action
606 * dialog. Make sure it's an application, and make that the default
607 * handler.
609 static void drag_app_dropped(GtkWidget *drop_box,
610 const guchar *app,
611 GtkWidget *dialog)
613 DirItem *item;
615 item = diritem_new("");
616 diritem_restat(app, item, NULL);
617 if (item->flags & ITEM_FLAG_APPDIR || EXECUTABLE_FILE(item))
619 guchar *path;
621 path = get_action_save_path(dialog);
623 if (path)
625 if (symlink(app, path))
626 delayed_error("symlink: %s",
627 g_strerror(errno));
628 else
629 destroy_on_idle(dialog);
631 g_free(path);
634 else
635 delayed_error(
636 _("This is not a program! Give me an application "
637 "instead!"));
639 diritem_free(item);
642 /* Find the current command which is used to run files of this type.
643 * Returns NULL on failure. g_free() the result.
645 static guchar *get_current_command(MIME_type *type)
647 struct stat info;
648 char *handler, *nl, *data = NULL;
649 long len;
650 guchar *command = NULL;
652 handler = handler_for(type);
654 if (!handler)
655 return NULL; /* No current handler */
657 if (stat(handler, &info))
658 goto out; /* Can't stat */
660 if ((!S_ISREG(info.st_mode)) || info.st_size > 256)
661 goto out; /* Only use small regular files */
663 if (!load_file(handler, &data, &len))
664 goto out; /* Didn't load OK */
666 if (strncmp(data, "#! /bin/sh\nexec ", 16) != 0)
667 goto out; /* Not one of ours */
669 nl = strchr(data + 16, '\n');
670 if (!nl)
671 goto out; /* No newline! */
673 command = g_strndup(data + 16, nl - data - 16);
674 out:
675 g_free(handler);
676 g_free(data);
677 return command;
680 /* Find the current command which is used to run files of this type,
681 * and return a textual description of it.
682 * Only call for non-executable files.
683 * g_free() the result.
685 gchar *describe_current_command(MIME_type *type)
687 char *handler;
688 char *desc = NULL;
689 struct stat info;
690 char *target;
692 g_return_val_if_fail(type != NULL, NULL);
694 handler = handler_for(type);
696 if (!handler)
697 return g_strdup(_("No run action defined"));
699 target = readlink_dup(handler);
700 if (target)
702 /* Cope with relative paths (shouldn't normally be needed) */
704 if (target[0] == '/')
706 g_free(handler);
707 handler = target;
709 else
711 gchar *dir;
713 dir = g_path_get_dirname(handler);
714 g_free(handler);
715 handler = g_strconcat(dir, "/", target, NULL);
716 g_free(target);
717 g_free(dir);
721 if (mc_stat(handler, &info) !=0 )
723 desc = g_strdup_printf(_("Error in handler %s: %s"), handler,
724 g_strerror(errno));
725 goto out;
728 if (S_ISDIR(info.st_mode))
730 const guchar *tmp;
731 uid_t dir_uid = info.st_uid;
733 tmp = make_path(handler, "AppRun");
735 if (mc_lstat(tmp, &info) != 0 || info.st_uid != dir_uid
736 || !(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
737 desc = g_strdup_printf(
738 _("Invalid application %s (bad AppRun)"),
739 handler);
740 /* Else, just report handler... */
742 goto out;
745 /* It's not an application directory, and it's not a symlink... */
747 if (access(handler, X_OK) != 0)
749 desc = g_strdup_printf(_("Non-executable %s"), handler);
750 goto out;
753 desc = get_current_command(type);
754 out:
755 if (!desc)
756 desc = handler;
757 else
758 g_free(handler);
760 return desc;
763 /* Display a dialog box allowing the user to set the default run action
764 * for this type.
766 void type_set_handler_dialog(MIME_type *type)
768 guchar *tmp;
769 GtkDialog *dialog;
770 GtkWidget *frame, *entry, *label, *button;
771 GtkWidget *hbox;
772 Radios *radios;
774 g_return_if_fail(type != NULL);
776 dialog = GTK_DIALOG(gtk_dialog_new());
777 gtk_dialog_set_has_separator(dialog, FALSE);
778 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
780 g_object_set_data(G_OBJECT(dialog), "mime_type", type);
782 gtk_window_set_title(GTK_WINDOW(dialog), _("Set run action"));
784 radios = radios_new(run_action_update, dialog);
785 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
787 radios_add(radios,
788 _("If a handler for the specific type isn't set up, "
789 "use this as the default."), SET_MEDIA,
790 _("Set default for all `%s/<anything>'"),
791 type->media_type);
793 radios_add(radios,
794 _("Use this application for all files with this MIME "
795 "type."), SET_TYPE,
796 _("Only for the type `%s' (%s/%s)"),
797 mime_type_comment(type),
798 type->media_type, type->subtype);
800 radios_set_value(radios, SET_TYPE);
802 frame = drop_box_new(_("Drop a suitable application here"));
804 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
806 radios_pack(radios, GTK_BOX(dialog->vbox));
807 gtk_box_pack_start(GTK_BOX(dialog->vbox), frame, TRUE, TRUE, 0);
809 g_signal_connect(frame, "path_dropped",
810 G_CALLBACK(drag_app_dropped), dialog);
811 g_signal_connect(frame, "clear",
812 G_CALLBACK(clear_run_action), dialog);
814 hbox = gtk_hbox_new(FALSE, 4);
815 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 4);
816 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
817 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_("OR")),
818 FALSE, TRUE, 0);
819 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
821 hbox = gtk_hbox_new(FALSE, 4);
822 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
824 label = gtk_label_new(_("Enter a shell command:")),
825 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
826 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
828 gtk_box_pack_start(GTK_BOX(hbox),
829 new_help_button(show_shell_help, NULL), FALSE, TRUE, 0);
831 entry = gtk_entry_new();
832 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, FALSE, TRUE, 0);
833 gtk_widget_grab_focus(entry);
834 g_object_set_data(G_OBJECT(dialog), "shell_command", entry);
835 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
837 /* If possible, fill in the entry box with the current command */
838 tmp = get_current_command(type);
839 if (tmp)
841 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
842 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
843 g_free(tmp);
845 else
847 gtk_entry_set_text(GTK_ENTRY(entry), " \"$@\"");
848 gtk_editable_set_position(GTK_EDITABLE(entry), 0);
851 gtk_dialog_add_button(dialog, GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL);
853 button = button_new_mixed(GTK_STOCK_OK, _("_Use Command"));
854 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
855 gtk_dialog_add_action_widget(dialog, button, GTK_RESPONSE_OK);
857 hbox = gtk_hbox_new(TRUE, 4);
858 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
860 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
862 g_signal_connect(dialog, "response",
863 G_CALLBACK(set_action_response), NULL);
865 gtk_widget_show_all(GTK_WIDGET(dialog));
868 /* path is an entry in Choices. If it's a symlink or a very small executable
869 * then just get rid of it, otherwise confirm first. It it doesn't exist,
870 * do nothing.
872 * FALSE on error (abort operation).
874 static gboolean remove_handler_with_confirm(const guchar *path)
876 struct stat info;
878 if (lstat(path, &info) == 0)
880 /* A binding already exists... */
881 if (S_ISREG(info.st_mode) && info.st_size > 256)
883 if (!confirm(_("A run action already exists and is "
884 "quite a big program - are you sure "
885 "you want to delete it?"),
886 GTK_STOCK_DELETE, NULL))
888 return FALSE;
892 if (unlink(path))
894 report_error(_("Can't remove %s: %s"),
895 path, g_strerror(errno));
896 return FALSE;
900 return TRUE;
903 /* The user wants to set a new default action for files of this type (or just
904 * clear the action). Removes the current binding if possible and returns the
905 * path to save the new one to. NULL means cancel. g_free() the result.
907 static char *get_action_save_path(GtkWidget *dialog)
909 guchar *path = NULL;
910 guchar *type_name = NULL;
911 MIME_type *type;
912 Radios *radios;
914 g_return_val_if_fail(dialog != NULL, NULL);
916 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
917 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
919 g_return_val_if_fail(radios != NULL && type != NULL, NULL);
921 if (radios_get_value(radios) == SET_MEDIA)
922 type_name = g_strdup(type->media_type);
923 else
924 type_name = g_strconcat(type->media_type, "_",
925 type->subtype, NULL);
927 path = choices_find_xdg_path_save("", PROJECT, SITE, FALSE);
928 if (!path)
930 report_error(
931 _("Choices saving is disabled by CHOICESPATH variable"));
932 goto out;
934 g_free(path);
936 path = choices_find_xdg_path_save(type_name, "MIME-types", SITE, TRUE);
938 if (!remove_handler_with_confirm(path))
939 null_g_free(&path);
940 out:
941 g_free(type_name);
942 return path;
945 MIME_type *mime_type_from_base_type(int base_type)
947 switch (base_type)
949 case TYPE_FILE:
950 return text_plain;
951 case TYPE_DIRECTORY:
952 return inode_directory;
953 case TYPE_PIPE:
954 return inode_pipe;
955 case TYPE_SOCKET:
956 return inode_socket;
957 case TYPE_BLOCK_DEVICE:
958 return inode_block_dev;
959 case TYPE_CHAR_DEVICE:
960 return inode_char_dev;
961 case TYPE_DOOR:
962 return inode_door;
964 return inode_unknown;
967 /* Takes the st_mode field from stat() and returns the base type.
968 * Should not be a symlink.
970 int mode_to_base_type(int st_mode)
972 if (S_ISREG(st_mode))
973 return TYPE_FILE;
974 else if (S_ISDIR(st_mode))
975 return TYPE_DIRECTORY;
976 else if (S_ISBLK(st_mode))
977 return TYPE_BLOCK_DEVICE;
978 else if (S_ISCHR(st_mode))
979 return TYPE_CHAR_DEVICE;
980 else if (S_ISFIFO(st_mode))
981 return TYPE_PIPE;
982 else if (S_ISSOCK(st_mode))
983 return TYPE_SOCKET;
984 else if (S_ISDOOR(st_mode))
985 return TYPE_DOOR;
987 return TYPE_ERROR;
990 /* Returns TRUE is this is something that is run by looking up its type
991 * in MIME-types and, hence, can have its run action set.
993 gboolean can_set_run_action(DirItem *item)
995 g_return_val_if_fail(item != NULL, FALSE);
997 return item->base_type == TYPE_FILE && !EXECUTABLE_FILE(item);
1000 /* Parse file type colours and allocate/free them as necessary */
1001 static void alloc_type_colours(void)
1003 gboolean success[NUM_TYPE_COLOURS];
1004 int change_count = 0; /* No. needing realloc */
1005 int i;
1006 static gboolean allocated = FALSE;
1008 /* Parse colours */
1009 for (i = 0; i < NUM_TYPE_COLOURS; i++)
1011 GdkColor *c = &type_colours[i];
1012 gushort r = c->red;
1013 gushort g = c->green;
1014 gushort b = c->blue;
1016 gdk_color_parse(o_type_colours[i].value, &type_colours[i]);
1018 if (allocated && (c->red != r || c->green != g || c->blue != b))
1019 change_count++;
1022 /* Free colours if they were previously allocated and
1023 * have changed or become unneeded.
1025 if (allocated && (change_count || !o_display_colour_types.int_value))
1027 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1028 type_colours, NUM_TYPE_COLOURS);
1029 allocated = FALSE;
1032 /* Allocate colours, unless they are still allocated (=> they didn't
1033 * change) or we don't want them anymore.
1034 * XXX: what should be done if allocation fails?
1036 if (!allocated && o_display_colour_types.int_value)
1038 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1039 type_colours, NUM_TYPE_COLOURS,
1040 FALSE, TRUE, success);
1041 allocated = TRUE;
1045 static void expire_timer(gpointer key, gpointer value, gpointer data)
1047 MIME_type *type = value;
1049 type->image_time = 0;
1052 static void options_changed(void)
1054 alloc_type_colours();
1055 if (o_icon_theme.has_changed)
1057 set_icon_theme();
1058 g_hash_table_foreach(type_hash, expire_timer, NULL);
1059 full_refresh();
1063 /* Return a pointer to a (static) colour for this item. If colouring is
1064 * off, returns normal.
1066 GdkColor *type_get_colour(DirItem *item, GdkColor *normal)
1068 int type = item->base_type;
1070 if (!o_display_colour_types.int_value)
1071 return normal;
1073 if (EXECUTABLE_FILE(item))
1074 type = TYPE_EXEC;
1075 else if (item->flags & ITEM_FLAG_APPDIR)
1076 type = TYPE_APPDIR;
1078 g_return_val_if_fail(type >= 0 && type < NUM_TYPE_COLOURS, normal);
1080 return &type_colours[type];
1083 static char **get_xdg_data_dirs(int *n_dirs)
1085 const char *env;
1086 char **dirs;
1087 int i, n;
1089 env = getenv("XDG_DATA_DIRS");
1090 if (!env)
1091 env = "/usr/local/share/:/usr/share/";
1092 dirs = g_strsplit(env, ":", 0);
1093 g_return_val_if_fail(dirs != NULL, NULL);
1094 for (n = 0; dirs[n]; n++)
1096 for (i = n; i > 0; i--)
1097 dirs[i] = dirs[i - 1];
1098 env = getenv("XDG_DATA_HOME");
1099 if (env)
1100 dirs[0] = g_strdup(env);
1101 else
1102 dirs[0] = g_build_filename(g_get_home_dir(), ".local",
1103 "share", NULL);
1104 *n_dirs = n + 1;
1105 return dirs;
1108 /* Try to fill in 'type->comment' from this document */
1109 static void get_comment(MIME_type *type, const guchar *path)
1111 xmlNode *node;
1112 XMLwrapper *doc;
1114 doc = xml_cache_load(path);
1115 if (!doc)
1116 return;
1118 node = xml_get_section(doc, TYPE_NS, "comment");
1120 if (node)
1122 char *val;
1123 g_return_if_fail(type->comment == NULL);
1124 val= xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
1125 type->comment = g_strdup(val);
1126 xmlFree(val);
1129 g_object_unref(doc);
1132 /* Fill in the comment field for this MIME type */
1133 static void find_comment(MIME_type *type)
1135 char **dirs;
1136 int i, n_dirs = 0;
1138 if (type->comment)
1140 g_free(type->comment);
1141 type->comment = NULL;
1144 dirs = get_xdg_data_dirs(&n_dirs);
1145 g_return_if_fail(dirs != NULL);
1147 for (i = 0; i < n_dirs; i++)
1149 guchar *path;
1151 path = g_strdup_printf("%s/mime/%s/%s.xml", dirs[i],
1152 type->media_type, type->subtype);
1153 get_comment(type, path);
1154 g_free(path);
1155 if (type->comment)
1156 break;
1159 if (!type->comment)
1160 type->comment = g_strdup_printf("%s/%s", type->media_type,
1161 type->subtype);
1163 for (i = 0; i < n_dirs; i++)
1164 g_free(dirs[i]);
1165 g_free(dirs);
1168 const char *mime_type_comment(MIME_type *type)
1170 if (!type->comment)
1171 find_comment(type);
1173 return type->comment;
1176 static void set_icon_theme(void)
1178 GtkIconInfo *info;
1179 char *icon_home;
1180 const char *theme_name = o_icon_theme.value;
1182 if (!theme_name || !*theme_name)
1183 theme_name = "ROX";
1185 while (1)
1187 gtk_icon_theme_set_custom_theme(icon_theme, theme_name);
1188 info = gtk_icon_theme_lookup_icon(icon_theme,
1189 "mime-application:postscript",
1190 ICON_HEIGHT, 0);
1191 if (!info)
1193 info = gtk_icon_theme_lookup_icon(icon_theme,
1194 "gnome-mime-application-postscript",
1195 ICON_HEIGHT, 0);
1197 if (info)
1199 gtk_icon_info_free(info);
1200 return;
1203 if (strcmp(theme_name, "ROX") == 0)
1204 break;
1206 delayed_error(_("Icon theme '%s' does not contain MIME icons. "
1207 "Using ROX default theme instead."),
1208 theme_name);
1210 theme_name = "ROX";
1213 icon_home = g_build_filename(home_dir, ".icons", NULL);
1214 if (!file_exists(icon_home))
1215 mkdir(icon_home, 0755);
1216 g_free(icon_home);
1218 icon_home = g_build_filename(home_dir, ".icons", "ROX", NULL);
1219 if (symlink(make_path(app_dir, "ROX"), icon_home))
1221 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1222 "(this may mean that the ROX theme already exists there, but "
1223 "the 'mime-application:postscript' icon couldn't be loaded for "
1224 "some reason, or %s is a link to an invalid directory; try "
1225 "deleting it)"), icon_home, g_strerror(errno), icon_home);
1226 open_to_show(icon_home);
1228 g_free(icon_home);
1230 gtk_icon_theme_rescan_if_needed(icon_theme);
1233 static guchar *read_theme(Option *option)
1235 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1236 GtkLabel *item;
1238 item = GTK_LABEL(GTK_BIN(om)->child);
1240 g_return_val_if_fail(item != NULL, g_strdup("ROX"));
1242 return g_strdup(gtk_label_get_text(item));
1245 static void update_theme(Option *option)
1247 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1248 GtkWidget *menu;
1249 GList *kids, *next;
1250 int i = 0;
1252 menu = gtk_option_menu_get_menu(om);
1254 kids = gtk_container_get_children(GTK_CONTAINER(menu));
1255 for (next = kids; next; next = next->next, i++)
1257 GtkLabel *item = GTK_LABEL(GTK_BIN(next->data)->child);
1258 const gchar *label;
1260 /* The label actually moves from the menu!! */
1261 if (!item)
1262 item = GTK_LABEL(GTK_BIN(om)->child);
1264 label = gtk_label_get_text(item);
1266 g_return_if_fail(label != NULL);
1268 if (strcmp(label, option->value) == 0)
1269 break;
1271 g_list_free(kids);
1273 if (next)
1274 gtk_option_menu_set_history(om, i);
1275 else
1276 g_warning("Theme '%s' not found", option->value);
1279 static void add_themes_from_dir(GPtrArray *names, const char *dir)
1281 GPtrArray *list;
1282 int i;
1284 if (access(dir, F_OK) != 0)
1285 return;
1287 list = list_dir(dir);
1288 g_return_if_fail(list != NULL);
1290 for (i = 0; i < list->len; i++)
1292 char *index_path;
1294 index_path = g_build_filename(dir, list->pdata[i],
1295 "index.theme", NULL);
1297 if (access(index_path, F_OK) == 0)
1298 g_ptr_array_add(names, list->pdata[i]);
1299 else
1300 g_free(list->pdata[i]);
1302 g_free(index_path);
1305 g_ptr_array_free(list, TRUE);
1308 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label)
1310 GtkWidget *button, *menu, *hbox;
1311 GPtrArray *names;
1312 gchar **theme_dirs = NULL;
1313 gint n_dirs = 0;
1314 int i;
1316 g_return_val_if_fail(option != NULL, NULL);
1317 g_return_val_if_fail(label != NULL, NULL);
1319 hbox = gtk_hbox_new(FALSE, 4);
1321 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1322 FALSE, TRUE, 0);
1324 button = gtk_option_menu_new();
1325 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1327 menu = gtk_menu_new();
1328 gtk_option_menu_set_menu(GTK_OPTION_MENU(button), menu);
1330 gtk_icon_theme_get_search_path(icon_theme, &theme_dirs, &n_dirs);
1331 names = g_ptr_array_new();
1332 for (i = 0; i < n_dirs; i++)
1333 add_themes_from_dir(names, theme_dirs[i]);
1334 g_strfreev(theme_dirs);
1336 g_ptr_array_sort(names, strcmp2);
1338 for (i = 0; i < names->len; i++)
1340 GtkWidget *item;
1341 char *name = names->pdata[i];
1343 item = gtk_menu_item_new_with_label(name);
1344 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1345 gtk_widget_show_all(item);
1347 g_free(name);
1350 g_ptr_array_free(names, TRUE);
1352 option->update_widget = update_theme;
1353 option->read_widget = read_theme;
1354 option->widget = button;
1356 gtk_signal_connect_object(GTK_OBJECT(button), "changed",
1357 GTK_SIGNAL_FUNC(option_check_widget),
1358 (GtkObject *) option);
1360 return g_list_append(NULL, hbox);