r4089: Opening the menu with the menu key had broken. Also fixed a few minor leaks
[rox-filer/translations.git] / ROX-Filer / src / type.c
blob65222197f4cf54f981704915e19917c251143fc4
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* type.c - code for dealing with filetypes */
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <time.h>
31 #include <sys/param.h>
32 #include <fnmatch.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
36 #ifdef WITH_GNOMEVFS
37 # include <libgnomevfs/gnome-vfs.h>
38 # include <libgnomevfs/gnome-vfs-mime.h>
39 # include <libgnomevfs/gnome-vfs-mime-handlers.h>
40 # include <libgnomevfs/gnome-vfs-application-registry.h>
41 #endif
43 #include "global.h"
45 #include "string.h"
46 #include "fscache.h"
47 #include "main.h"
48 #include "pixmaps.h"
49 #include "run.h"
50 #include "gui_support.h"
51 #include "choices.h"
52 #include "type.h"
53 #include "support.h"
54 #include "diritem.h"
55 #include "dnd.h"
56 #include "options.h"
57 #include "filer.h"
58 #include "action.h" /* (for action_chmod) */
59 #include "xml.h"
60 #include "dropbox.h"
61 #include "xdgmime.h"
62 #include "xtypes.h"
64 #define TYPE_NS "http://www.freedesktop.org/standards/shared-mime-info"
65 enum {SET_MEDIA, SET_TYPE};
67 /* Colours for file types (same order as base types) */
68 static gchar *opt_type_colours[][2] = {
69 {"display_err_colour", "#ff0000"},
70 {"display_unkn_colour", "#000000"},
71 {"display_dir_colour", "#000080"},
72 {"display_pipe_colour", "#444444"},
73 {"display_sock_colour", "#ff00ff"},
74 {"display_file_colour", "#000000"},
75 {"display_cdev_colour", "#000000"},
76 {"display_bdev_colour", "#000000"},
77 {"display_door_colour", "#ff00ff"},
78 {"display_exec_colour", "#006000"},
79 {"display_adir_colour", "#006000"}
81 #define NUM_TYPE_COLOURS\
82 (sizeof(opt_type_colours) / sizeof(opt_type_colours[0]))
84 /* Parsed colours for file types */
85 static Option o_type_colours[NUM_TYPE_COLOURS];
86 static GdkColor type_colours[NUM_TYPE_COLOURS];
88 /* Static prototypes */
89 static void alloc_type_colours(void);
90 static void options_changed(void);
91 static char *get_action_save_path(GtkWidget *dialog);
92 static MIME_type *get_mime_type(const gchar *type_name, gboolean can_create);
93 static gboolean remove_handler_with_confirm(const guchar *path);
94 static void set_icon_theme(void);
95 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label);
97 /* Hash of all allocated MIME types, indexed by "media/subtype".
98 * MIME_type structs are never freed; this table prevents memory leaks
99 * when rereading the config files.
101 static GHashTable *type_hash = NULL;
103 /* Most things on Unix are text files, so this is the default type */
104 MIME_type *text_plain;
105 MIME_type *inode_directory;
106 MIME_type *inode_mountpoint;
107 MIME_type *inode_pipe;
108 MIME_type *inode_socket;
109 MIME_type *inode_block_dev;
110 MIME_type *inode_char_dev;
111 MIME_type *application_executable;
112 MIME_type *application_octet_stream;
113 MIME_type *application_x_shellscript;
114 MIME_type *inode_unknown;
115 MIME_type *inode_door;
117 static Option o_display_colour_types;
118 static Option o_icon_theme;
120 static GtkIconTheme *icon_theme = NULL;
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 inode_unknown = get_mime_type("inode/unknown", TRUE);
141 inode_door = get_mime_type("inode/door", TRUE);
143 option_add_string(&o_icon_theme, "icon_theme", "ROX");
144 option_add_int(&o_display_colour_types, "display_colour_types", TRUE);
145 option_register_widget("icon-theme-chooser", build_icon_theme);
147 for (i = 0; i < NUM_TYPE_COLOURS; i++)
148 option_add_string(&o_type_colours[i],
149 opt_type_colours[i][0],
150 opt_type_colours[i][1]);
151 alloc_type_colours();
153 set_icon_theme();
155 option_add_notify(options_changed);
158 /* Read-load all the glob patterns.
159 * Note: calls filer_update_all.
161 void reread_mime_files(void)
163 gtk_icon_theme_rescan_if_needed(icon_theme);
165 xdg_mime_shutdown();
168 /* Returns the MIME_type structure for the given type name. It is looked
169 * up in type_hash and returned if found. If not found (and can_create is
170 * TRUE) then a new MIME_type is made, added to type_hash and returned.
171 * NULL is returned if type_name is not in type_hash and can_create is
172 * FALSE, or if type_name does not contain a '/' character.
174 static MIME_type *get_mime_type(const gchar *type_name, gboolean can_create)
176 MIME_type *mtype;
177 gchar *slash;
179 mtype = g_hash_table_lookup(type_hash, type_name);
180 if (mtype || !can_create)
181 return mtype;
183 slash = strchr(type_name, '/');
184 g_return_val_if_fail(slash != NULL, NULL); /* XXX: Report nicely */
186 mtype = g_new(MIME_type, 1);
187 mtype->media_type = g_strndup(type_name, slash - type_name);
188 mtype->subtype = g_strdup(slash + 1);
189 mtype->image = NULL;
190 mtype->comment = NULL;
192 mtype->executable = xdg_mime_mime_type_subclass(type_name,
193 "application/x-executable");
195 g_hash_table_insert(type_hash, g_strdup(type_name), mtype);
197 return mtype;
200 const char *basetype_name(DirItem *item)
202 if (item->flags & ITEM_FLAG_SYMLINK)
203 return _("Sym link");
204 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
205 return _("Mount point");
206 else if (item->flags & ITEM_FLAG_APPDIR)
207 return _("App dir");
209 switch (item->base_type)
211 case TYPE_FILE:
212 return _("File");
213 case TYPE_DIRECTORY:
214 return _("Dir");
215 case TYPE_CHAR_DEVICE:
216 return _("Char dev");
217 case TYPE_BLOCK_DEVICE:
218 return _("Block dev");
219 case TYPE_PIPE:
220 return _("Pipe");
221 case TYPE_SOCKET:
222 return _("Socket");
223 case TYPE_DOOR:
224 return _("Door");
227 return _("Unknown");
230 static void append_names(gpointer key, gpointer value, gpointer udata)
232 GList **list = (GList **) udata;
234 *list = g_list_prepend(*list, key);
237 /* Return list of all mime type names. Caller must free the list
238 * but NOT the strings it contains (which are never freed).
240 GList *mime_type_name_list(void)
242 GList *list = NULL;
244 g_hash_table_foreach(type_hash, append_names, &list);
245 list = g_list_sort(list, (GCompareFunc) strcmp);
247 return list;
250 /* MIME-type guessing */
252 /* Get the type of this file - stats the file and uses that if
253 * possible. For regular or missing files, uses the pathname.
255 MIME_type *type_get_type(const guchar *path)
257 DirItem *item;
258 MIME_type *type = NULL;
260 item = diritem_new("");
261 diritem_restat(path, item, NULL);
262 if (item->base_type != TYPE_ERROR)
263 type = item->mime_type;
264 diritem_free(item);
266 if (type)
267 return type;
269 type = type_from_path(path);
271 if (!type)
272 return text_plain;
274 return type;
277 /* Returns a pointer to the MIME-type.
279 * Tries all enabled methods:
280 * - Look for extended attribute
281 * - If no attribute, check file name
282 * - If no name rule, check contents
284 * NULL if we can't think of anything.
286 MIME_type *type_from_path(const char *path)
288 MIME_type *mime_type = NULL;
289 const char *type_name;
291 /* Check for extended attribute first */
292 mime_type = xtype_get(path);
293 if (mime_type)
294 return mime_type;
296 /* Try name and contents next */
297 type_name = xdg_mime_get_mime_type_for_file(path);
298 if (type_name)
299 return get_mime_type(type_name, TRUE);
301 return NULL;
304 /* Returns the file/dir in Choices for handling this type.
305 * NULL if there isn't one. g_free() the result.
307 static char *handler_for(MIME_type *type)
309 char *type_name;
310 char *open;
312 type_name = g_strconcat(type->media_type, "_", type->subtype, NULL);
313 open = choices_find_xdg_path_load(type_name, "MIME-types", SITE);
314 g_free(type_name);
316 if (!open)
317 open = choices_find_xdg_path_load(type->media_type,
318 "MIME-types", SITE);
320 return open;
323 MIME_type *mime_type_lookup(const char *type)
325 return get_mime_type(type, TRUE);
328 /* Actions for types */
330 gboolean type_open(const char *path, MIME_type *type)
332 gchar *argv[] = {NULL, NULL, NULL};
333 char *open;
334 gboolean retval;
335 struct stat info;
337 argv[1] = (char *) path;
339 open = handler_for(type);
340 if (!open)
341 return FALSE;
343 if (stat(open, &info))
345 report_error("stat(%s): %s", open, g_strerror(errno));
346 g_free(open);
347 return FALSE;
350 if (info.st_mode & S_IWOTH)
352 gchar *choices_dir;
353 GList *paths;
355 report_error(_("Executable '%s' is world-writeable! Refusing "
356 "to run. Please change the permissions now (this "
357 "problem may have been caused by a bug in earlier "
358 "versions of the filer).\n\n"
359 "Having (non-symlink) run actions world-writeable "
360 "means that other people who use your computer can "
361 "replace your run actions with malicious versions.\n\n"
362 "If you trust everyone who could write to these files "
363 "then you needn't worry. Otherwise, you should check, "
364 "or even just delete, all the existing run actions."),
365 open);
366 choices_dir = g_path_get_dirname(open);
367 paths = g_list_append(NULL, choices_dir);
368 action_chmod(paths, TRUE, _("go-w (Fix security problem)"));
369 g_free(choices_dir);
370 g_list_free(paths);
371 g_free(open);
372 return TRUE;
375 if (S_ISDIR(info.st_mode))
376 argv[0] = g_strconcat(open, "/AppRun", NULL);
377 else
378 argv[0] = open;
380 retval = rox_spawn(home_dir, (const gchar **) argv) != 0;
382 if (argv[0] != open)
383 g_free(argv[0]);
385 g_free(open);
387 return retval;
390 /* Return the image for this type, loading it if needed.
391 * Places to check are: (eg type="text_plain", base="text")
392 * 1. <Choices>/MIME-icons/base_subtype
393 * 2. Icon theme 'mime-base:subtype'
394 * 3. Icon theme 'mime-base'
395 * 4. Unknown type icon.
397 * Note: You must g_object_unref() the image afterwards.
399 MaskedPixmap *type_to_icon(MIME_type *type)
401 GdkPixbuf *full;
402 char *type_name, *path;
403 time_t now;
405 if (type == NULL)
407 g_object_ref(im_unknown);
408 return im_unknown;
411 now = time(NULL);
412 /* Already got an image? */
413 if (type->image)
415 /* Yes - don't recheck too often */
416 if (abs(now - type->image_time) < 2)
418 g_object_ref(type->image);
419 return type->image;
421 g_object_unref(type->image);
422 type->image = NULL;
425 type_name = g_strconcat(type->media_type, "_", type->subtype,
426 ".png", NULL);
427 path = choices_find_xdg_path_load(type_name, "MIME-icons", SITE);
428 g_free(type_name);
429 if (path)
431 type->image = g_fscache_lookup(pixmap_cache, path);
432 g_free(path);
435 if (type->image)
436 goto out;
438 type_name = g_strconcat("mime-", type->media_type, ":",
439 type->subtype, NULL);
440 full = gtk_icon_theme_load_icon(icon_theme, type_name, HUGE_HEIGHT,
441 0, NULL);
442 g_free(type_name);
443 if (!full)
445 /* Ugly hack... try for a GNOME icon */
446 type_name = g_strconcat("gnome-mime-", type->media_type,
447 "-", type->subtype, NULL);
448 full = gtk_icon_theme_load_icon(icon_theme,
449 type_name,
450 HUGE_HEIGHT, 0, NULL);
451 g_free(type_name);
453 if (!full)
455 /* Try for a media type */
456 type_name = g_strconcat("mime-", type->media_type, NULL);
457 full = gtk_icon_theme_load_icon(icon_theme,
458 type_name,
459 HUGE_HEIGHT, 0, NULL);
460 g_free(type_name);
462 if (full)
464 type->image = masked_pixmap_new(full);
465 g_object_unref(full);
468 out:
469 if (!type->image)
471 /* One ref from the type structure, one returned */
472 type->image = im_unknown;
473 g_object_ref(im_unknown);
476 type->image_time = now;
478 g_object_ref(type->image);
479 return type->image;
482 GdkAtom type_to_atom(MIME_type *type)
484 char *str;
485 GdkAtom retval;
487 g_return_val_if_fail(type != NULL, GDK_NONE);
489 str = g_strconcat(type->media_type, "/", type->subtype, NULL);
490 retval = gdk_atom_intern(str, FALSE);
491 g_free(str);
493 return retval;
496 static void show_shell_help(gpointer data)
498 info_message(_("Enter a shell command which will load \"$@\" into "
499 "a suitable program. Eg:\n\n"
500 "gimp \"$@\""));
503 /* Called if the user clicks on the OK button. Returns FALSE if an error
504 * was displayed instead of performing the action.
506 static gboolean set_shell_action(GtkWidget *dialog)
508 GtkEntry *entry;
509 const guchar *command;
510 gchar *tmp, *path;
511 int error = 0, len;
512 int fd;
514 entry = g_object_get_data(G_OBJECT(dialog), "shell_command");
516 g_return_val_if_fail(entry != NULL, FALSE);
518 command = gtk_entry_get_text(entry);
520 if (!strchr(command, '$'))
522 show_shell_help(NULL);
523 return FALSE;
526 path = get_action_save_path(dialog);
527 if (!path)
528 return FALSE;
530 tmp = g_strdup_printf("#! /bin/sh\nexec %s\n", command);
531 len = strlen(tmp);
533 fd = open(path, O_CREAT | O_WRONLY, 0755);
534 if (fd == -1)
535 error = errno;
536 else
538 FILE *file;
540 file = fdopen(fd, "w");
541 if (file)
543 if (fwrite(tmp, 1, len, file) < len)
544 error = errno;
545 if (fclose(file) && error == 0)
546 error = errno;
548 else
549 error = errno;
552 if (error)
553 report_error(g_strerror(error));
555 g_free(tmp);
556 g_free(path);
558 gtk_widget_destroy(dialog);
560 return TRUE;
563 static void set_action_response(GtkWidget *dialog, gint response, gpointer data)
565 if (response == GTK_RESPONSE_OK)
566 if (!set_shell_action(dialog))
567 return;
568 gtk_widget_destroy(dialog);
571 /* Return the path of the file in choices that handles this type and
572 * radio setting.
573 * NULL if nothing is defined for it.
575 static guchar *handler_for_radios(GObject *dialog)
577 Radios *radios;
578 MIME_type *type;
580 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
581 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
583 g_return_val_if_fail(radios != NULL, NULL);
584 g_return_val_if_fail(type != NULL, NULL);
586 switch (radios_get_value(radios))
588 case SET_MEDIA:
589 return choices_find_xdg_path_load(type->media_type,
590 "MIME-types", SITE);
591 case SET_TYPE:
593 gchar *tmp, *handler;
594 tmp = g_strconcat(type->media_type, "_",
595 type->subtype, NULL);
596 handler = choices_find_xdg_path_load(tmp,
597 "MIME-types",
598 SITE);
599 g_free(tmp);
600 return handler;
602 default:
603 g_warning("Bad type");
604 return NULL;
608 /* (radios can be NULL if called from clear_run_action) */
609 static void run_action_update(Radios *radios, gpointer data)
611 guchar *handler;
612 DropBox *drop_box;
613 GObject *dialog = G_OBJECT(data);
615 drop_box = g_object_get_data(dialog, "rox-dropbox");
617 g_return_if_fail(drop_box != NULL);
619 handler = handler_for_radios(dialog);
621 if (handler)
623 char *old = handler;
625 handler = readlink_dup(old);
626 if (handler)
627 g_free(old);
628 else
629 handler = old;
632 drop_box_set_path(DROP_BOX(drop_box), handler);
633 g_free(handler);
636 static void clear_run_action(GtkWidget *drop_box, GtkWidget *dialog)
638 guchar *handler;
640 handler = handler_for_radios(G_OBJECT(dialog));
642 if (handler)
643 remove_handler_with_confirm(handler);
645 run_action_update(NULL, dialog);
648 /* Called when a URI list is dropped onto the box in the Set Run Action
649 * dialog. Make sure it's an application, and make that the default
650 * handler.
652 static void drag_app_dropped(GtkWidget *drop_box,
653 const guchar *app,
654 GtkWidget *dialog)
656 DirItem *item;
658 item = diritem_new("");
659 diritem_restat(app, item, NULL);
660 if (item->flags & ITEM_FLAG_APPDIR || EXECUTABLE_FILE(item))
662 guchar *path;
664 path = get_action_save_path(dialog);
666 if (path)
668 if (symlink(app, path))
669 delayed_error("symlink: %s",
670 g_strerror(errno));
671 else
672 destroy_on_idle(dialog);
674 g_free(path);
677 else
678 delayed_error(
679 _("This is not a program! Give me an application "
680 "instead!"));
682 diritem_free(item);
685 /* Find the current command which is used to run files of this type.
686 * Returns NULL on failure. g_free() the result.
688 static guchar *get_current_command(MIME_type *type)
690 struct stat info;
691 char *handler, *nl, *data = NULL;
692 long len;
693 guchar *command = NULL;
695 handler = handler_for(type);
697 if (!handler)
698 return NULL; /* No current handler */
700 if (stat(handler, &info))
701 goto out; /* Can't stat */
703 if ((!S_ISREG(info.st_mode)) || info.st_size > 256)
704 goto out; /* Only use small regular files */
706 if (!load_file(handler, &data, &len))
707 goto out; /* Didn't load OK */
709 if (strncmp(data, "#! /bin/sh\nexec ", 16) != 0)
710 goto out; /* Not one of ours */
712 nl = strchr(data + 16, '\n');
713 if (!nl)
714 goto out; /* No newline! */
716 command = g_strndup(data + 16, nl - data - 16);
717 out:
718 g_free(handler);
719 g_free(data);
720 return command;
723 /* Find the current command which is used to run files of this type,
724 * and return a textual description of it.
725 * Only call for non-executable files.
726 * g_free() the result.
728 gchar *describe_current_command(MIME_type *type)
730 char *handler;
731 char *desc = NULL;
732 struct stat info;
733 char *target;
735 g_return_val_if_fail(type != NULL, NULL);
737 handler = handler_for(type);
739 if (!handler)
740 return g_strdup(_("No run action defined"));
742 target = readlink_dup(handler);
743 if (target)
745 /* Cope with relative paths (shouldn't normally be needed) */
747 if (target[0] == '/')
749 g_free(handler);
750 handler = target;
752 else
754 gchar *dir;
756 dir = g_path_get_dirname(handler);
757 g_free(handler);
758 handler = g_strconcat(dir, "/", target, NULL);
759 g_free(target);
760 g_free(dir);
764 if (mc_stat(handler, &info) !=0 )
766 desc = g_strdup_printf(_("Error in handler %s: %s"), handler,
767 g_strerror(errno));
768 goto out;
771 if (S_ISDIR(info.st_mode))
773 const guchar *tmp;
774 uid_t dir_uid = info.st_uid;
776 tmp = make_path(handler, "AppRun");
778 if (mc_lstat(tmp, &info) != 0 || info.st_uid != dir_uid
779 || !(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
780 desc = g_strdup_printf(
781 _("Invalid application %s (bad AppRun)"),
782 handler);
783 /* Else, just report handler... */
785 goto out;
788 /* It's not an application directory, and it's not a symlink... */
790 if (access(handler, X_OK) != 0)
792 desc = g_strdup_printf(_("Non-executable %s"), handler);
793 goto out;
796 desc = get_current_command(type);
797 out:
798 if (!desc)
799 desc = handler;
800 else
801 g_free(handler);
803 return desc;
806 /* Display a dialog box allowing the user to set the default run action
807 * for this type.
809 void type_set_handler_dialog(MIME_type *type)
811 guchar *tmp;
812 GtkDialog *dialog;
813 GtkWidget *frame, *entry, *label, *button;
814 GtkWidget *hbox;
815 Radios *radios;
817 g_return_if_fail(type != NULL);
819 dialog = GTK_DIALOG(gtk_dialog_new());
820 gtk_dialog_set_has_separator(dialog, FALSE);
821 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
823 g_object_set_data(G_OBJECT(dialog), "mime_type", type);
825 gtk_window_set_title(GTK_WINDOW(dialog), _("Set run action"));
827 radios = radios_new(run_action_update, dialog);
828 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
830 radios_add(radios,
831 _("If a handler for the specific type isn't set up, "
832 "use this as the default."), SET_MEDIA,
833 _("Set default for all `%s/<anything>'"),
834 type->media_type);
836 radios_add(radios,
837 _("Use this application for all files with this MIME "
838 "type."), SET_TYPE,
839 _("Only for the type `%s' (%s/%s)"),
840 mime_type_comment(type),
841 type->media_type, type->subtype);
843 radios_set_value(radios, SET_TYPE);
845 frame = drop_box_new(_("Drop a suitable application here"));
847 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
849 radios_pack(radios, GTK_BOX(dialog->vbox));
850 gtk_box_pack_start(GTK_BOX(dialog->vbox), frame, TRUE, TRUE, 0);
852 g_signal_connect(frame, "path_dropped",
853 G_CALLBACK(drag_app_dropped), dialog);
854 g_signal_connect(frame, "clear",
855 G_CALLBACK(clear_run_action), dialog);
857 hbox = gtk_hbox_new(FALSE, 4);
858 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 4);
859 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
860 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_("OR")),
861 FALSE, TRUE, 0);
862 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
864 hbox = gtk_hbox_new(FALSE, 4);
865 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
867 label = gtk_label_new(_("Enter a shell command:")),
868 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
869 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
871 gtk_box_pack_start(GTK_BOX(hbox),
872 new_help_button(show_shell_help, NULL), FALSE, TRUE, 0);
874 entry = gtk_entry_new();
875 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, FALSE, TRUE, 0);
876 gtk_widget_grab_focus(entry);
877 g_object_set_data(G_OBJECT(dialog), "shell_command", entry);
878 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
880 /* If possible, fill in the entry box with the current command */
881 tmp = get_current_command(type);
882 if (tmp)
884 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
885 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
886 g_free(tmp);
888 else
890 gtk_entry_set_text(GTK_ENTRY(entry), " \"$@\"");
891 gtk_editable_set_position(GTK_EDITABLE(entry), 0);
894 gtk_dialog_add_button(dialog, GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL);
896 button = button_new_mixed(GTK_STOCK_OK, _("_Use Command"));
897 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
898 gtk_dialog_add_action_widget(dialog, button, GTK_RESPONSE_OK);
900 hbox = gtk_hbox_new(TRUE, 4);
901 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
903 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
905 g_signal_connect(dialog, "response",
906 G_CALLBACK(set_action_response), NULL);
908 gtk_widget_show_all(GTK_WIDGET(dialog));
911 /* path is an entry in Choices. If it's a symlink or a very small executable
912 * then just get rid of it, otherwise confirm first. It it doesn't exist,
913 * do nothing.
915 * FALSE on error (abort operation).
917 static gboolean remove_handler_with_confirm(const guchar *path)
919 struct stat info;
921 if (lstat(path, &info) == 0)
923 /* A binding already exists... */
924 if (S_ISREG(info.st_mode) && info.st_size > 256)
926 if (!confirm(_("A run action already exists and is "
927 "quite a big program - are you sure "
928 "you want to delete it?"),
929 GTK_STOCK_DELETE, NULL))
931 return FALSE;
935 if (unlink(path))
937 report_error(_("Can't remove %s: %s"),
938 path, g_strerror(errno));
939 return FALSE;
943 return TRUE;
946 /* The user wants to set a new default action for files of this type (or just
947 * clear the action). Removes the current binding if possible and returns the
948 * path to save the new one to. NULL means cancel. g_free() the result.
950 static char *get_action_save_path(GtkWidget *dialog)
952 guchar *path = NULL;
953 guchar *type_name = NULL;
954 MIME_type *type;
955 Radios *radios;
957 g_return_val_if_fail(dialog != NULL, NULL);
959 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
960 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
962 g_return_val_if_fail(radios != NULL && type != NULL, NULL);
964 if (radios_get_value(radios) == SET_MEDIA)
965 type_name = g_strdup(type->media_type);
966 else
967 type_name = g_strconcat(type->media_type, "_",
968 type->subtype, NULL);
970 path = choices_find_xdg_path_save("", PROJECT, SITE, FALSE);
971 if (!path)
973 report_error(
974 _("Choices saving is disabled by CHOICESPATH variable"));
975 goto out;
977 g_free(path);
979 path = choices_find_xdg_path_save(type_name, "MIME-types", SITE, TRUE);
981 if (!remove_handler_with_confirm(path))
982 null_g_free(&path);
983 out:
984 g_free(type_name);
985 return path;
988 MIME_type *mime_type_from_base_type(int base_type)
990 switch (base_type)
992 case TYPE_FILE:
993 return text_plain;
994 case TYPE_DIRECTORY:
995 return inode_directory;
996 case TYPE_PIPE:
997 return inode_pipe;
998 case TYPE_SOCKET:
999 return inode_socket;
1000 case TYPE_BLOCK_DEVICE:
1001 return inode_block_dev;
1002 case TYPE_CHAR_DEVICE:
1003 return inode_char_dev;
1004 case TYPE_DOOR:
1005 return inode_door;
1007 return inode_unknown;
1010 /* Takes the st_mode field from stat() and returns the base type.
1011 * Should not be a symlink.
1013 int mode_to_base_type(int st_mode)
1015 if (S_ISREG(st_mode))
1016 return TYPE_FILE;
1017 else if (S_ISDIR(st_mode))
1018 return TYPE_DIRECTORY;
1019 else if (S_ISBLK(st_mode))
1020 return TYPE_BLOCK_DEVICE;
1021 else if (S_ISCHR(st_mode))
1022 return TYPE_CHAR_DEVICE;
1023 else if (S_ISFIFO(st_mode))
1024 return TYPE_PIPE;
1025 else if (S_ISSOCK(st_mode))
1026 return TYPE_SOCKET;
1027 else if (S_ISDOOR(st_mode))
1028 return TYPE_DOOR;
1030 return TYPE_ERROR;
1033 /* Returns TRUE is this is something that is run by looking up its type
1034 * in MIME-types and, hence, can have its run action set.
1036 gboolean can_set_run_action(DirItem *item)
1038 g_return_val_if_fail(item != NULL, FALSE);
1040 return item->base_type == TYPE_FILE && !EXECUTABLE_FILE(item);
1043 /* Parse file type colours and allocate/free them as necessary */
1044 static void alloc_type_colours(void)
1046 gboolean success[NUM_TYPE_COLOURS];
1047 int change_count = 0; /* No. needing realloc */
1048 int i;
1049 static gboolean allocated = FALSE;
1051 /* Parse colours */
1052 for (i = 0; i < NUM_TYPE_COLOURS; i++)
1054 GdkColor *c = &type_colours[i];
1055 gushort r = c->red;
1056 gushort g = c->green;
1057 gushort b = c->blue;
1059 gdk_color_parse(o_type_colours[i].value, &type_colours[i]);
1061 if (allocated && (c->red != r || c->green != g || c->blue != b))
1062 change_count++;
1065 /* Free colours if they were previously allocated and
1066 * have changed or become unneeded.
1068 if (allocated && (change_count || !o_display_colour_types.int_value))
1070 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1071 type_colours, NUM_TYPE_COLOURS);
1072 allocated = FALSE;
1075 /* Allocate colours, unless they are still allocated (=> they didn't
1076 * change) or we don't want them anymore.
1077 * XXX: what should be done if allocation fails?
1079 if (!allocated && o_display_colour_types.int_value)
1081 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1082 type_colours, NUM_TYPE_COLOURS,
1083 FALSE, TRUE, success);
1084 allocated = TRUE;
1088 static void expire_timer(gpointer key, gpointer value, gpointer data)
1090 MIME_type *type = value;
1092 type->image_time = 0;
1095 static void options_changed(void)
1097 alloc_type_colours();
1098 if (o_icon_theme.has_changed)
1100 set_icon_theme();
1101 g_hash_table_foreach(type_hash, expire_timer, NULL);
1102 full_refresh();
1106 /* Return a pointer to a (static) colour for this item. If colouring is
1107 * off, returns normal.
1109 GdkColor *type_get_colour(DirItem *item, GdkColor *normal)
1111 int type = item->base_type;
1113 if (!o_display_colour_types.int_value)
1114 return normal;
1116 if (EXECUTABLE_FILE(item))
1117 type = TYPE_EXEC;
1118 else if (item->flags & ITEM_FLAG_APPDIR)
1119 type = TYPE_APPDIR;
1121 g_return_val_if_fail(type >= 0 && type < NUM_TYPE_COLOURS, normal);
1123 return &type_colours[type];
1126 static char **get_xdg_data_dirs(int *n_dirs)
1128 const char *env;
1129 char **dirs;
1130 int i, n;
1132 env = getenv("XDG_DATA_DIRS");
1133 if (!env)
1134 env = "/usr/local/share/:/usr/share/";
1135 dirs = g_strsplit(env, ":", 0);
1136 g_return_val_if_fail(dirs != NULL, NULL);
1137 for (n = 0; dirs[n]; n++)
1139 for (i = n; i > 0; i--)
1140 dirs[i] = dirs[i - 1];
1141 env = getenv("XDG_DATA_HOME");
1142 if (env)
1143 dirs[0] = g_strdup(env);
1144 else
1145 dirs[0] = g_build_filename(g_get_home_dir(), ".local",
1146 "share", NULL);
1147 *n_dirs = n + 1;
1148 return dirs;
1151 /* Try to fill in 'type->comment' from this document */
1152 static void get_comment(MIME_type *type, const guchar *path)
1154 xmlNode *node;
1155 XMLwrapper *doc;
1157 doc = xml_cache_load(path);
1158 if (!doc)
1159 return;
1161 node = xml_get_section(doc, TYPE_NS, "comment");
1163 if (node)
1165 char *val;
1166 g_return_if_fail(type->comment == NULL);
1167 val= xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
1168 type->comment = g_strdup(val);
1169 xmlFree(val);
1172 g_object_unref(doc);
1175 /* Fill in the comment field for this MIME type */
1176 static void find_comment(MIME_type *type)
1178 char **dirs;
1179 int i, n_dirs;
1181 if (type->comment)
1183 g_free(type->comment);
1184 type->comment = NULL;
1187 dirs = get_xdg_data_dirs(&n_dirs);
1188 g_return_if_fail(dirs != NULL);
1190 for (i = 0; i < n_dirs; i++)
1192 guchar *path;
1194 path = g_strdup_printf("%s/mime/%s/%s.xml", dirs[i],
1195 type->media_type, type->subtype);
1196 get_comment(type, path);
1197 g_free(path);
1198 if (type->comment)
1199 break;
1202 if (!type->comment)
1203 type->comment = g_strdup_printf("%s/%s", type->media_type,
1204 type->subtype);
1206 for (i = 0; i < n_dirs; i++)
1207 g_free(dirs[i]);
1208 g_free(dirs);
1211 const char *mime_type_comment(MIME_type *type)
1213 if (!type->comment)
1214 find_comment(type);
1216 return type->comment;
1219 static void set_icon_theme(void)
1221 GtkIconInfo *info;
1222 char *icon_home;
1223 const char *theme_name = o_icon_theme.value;
1225 if (!theme_name || !*theme_name)
1226 theme_name = "ROX";
1228 while (1)
1230 gtk_icon_theme_set_custom_theme(icon_theme, theme_name);
1231 info = gtk_icon_theme_lookup_icon(icon_theme,
1232 "mime-application:postscript",
1233 ICON_HEIGHT, 0);
1234 if (!info)
1236 info = gtk_icon_theme_lookup_icon(icon_theme,
1237 "gnome-mime-application-postscript",
1238 ICON_HEIGHT, 0);
1240 if (info)
1242 gtk_icon_info_free(info);
1243 return;
1246 if (strcmp(theme_name, "ROX") == 0)
1247 break;
1249 delayed_error(_("Icon theme '%s' does not contain MIME icons. "
1250 "Using ROX default theme instead."),
1251 theme_name);
1253 theme_name = "ROX";
1256 icon_home = g_build_filename(home_dir, ".icons", NULL);
1257 if (!file_exists(icon_home))
1258 mkdir(icon_home, 0755);
1259 g_free(icon_home);
1261 icon_home = g_build_filename(home_dir, ".icons", "ROX", NULL);
1262 if (symlink(make_path(app_dir, "ROX"), icon_home))
1263 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1264 "(this may mean that the ROX theme already exists there, but "
1265 "the 'mime-application:postscript' icon couldn't be loaded for "
1266 "some reason)"), icon_home, g_strerror(errno));
1267 g_free(icon_home);
1269 gtk_icon_theme_rescan_if_needed(icon_theme);
1272 static guchar *read_theme(Option *option)
1274 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1275 GtkLabel *item;
1277 item = GTK_LABEL(GTK_BIN(om)->child);
1279 g_return_val_if_fail(item != NULL, g_strdup("ROX"));
1281 return g_strdup(gtk_label_get_text(item));
1284 static void update_theme(Option *option)
1286 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1287 GtkWidget *menu;
1288 GList *kids, *next;
1289 int i = 0;
1291 menu = gtk_option_menu_get_menu(om);
1293 kids = gtk_container_get_children(GTK_CONTAINER(menu));
1294 for (next = kids; next; next = next->next, i++)
1296 GtkLabel *item = GTK_LABEL(GTK_BIN(next->data)->child);
1297 const gchar *label;
1299 /* The label actually moves from the menu!! */
1300 if (!item)
1301 item = GTK_LABEL(GTK_BIN(om)->child);
1303 label = gtk_label_get_text(item);
1305 g_return_if_fail(label != NULL);
1307 if (strcmp(label, option->value) == 0)
1308 break;
1310 g_list_free(kids);
1312 if (next)
1313 gtk_option_menu_set_history(om, i);
1314 else
1315 g_warning("Theme '%s' not found", option->value);
1318 static void add_themes_from_dir(GPtrArray *names, const char *dir)
1320 GPtrArray *list;
1321 int i;
1323 if (access(dir, F_OK) != 0)
1324 return;
1326 list = list_dir(dir);
1327 g_return_if_fail(list != NULL);
1329 for (i = 0; i < list->len; i++)
1331 char *index_path;
1333 index_path = g_build_filename(dir, list->pdata[i],
1334 "index.theme", NULL);
1336 if (access(index_path, F_OK) == 0)
1337 g_ptr_array_add(names, list->pdata[i]);
1338 else
1339 g_free(list->pdata[i]);
1341 g_free(index_path);
1344 g_ptr_array_free(list, TRUE);
1347 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label)
1349 GtkWidget *button, *menu, *hbox;
1350 GPtrArray *names;
1351 gchar **theme_dirs = NULL;
1352 gint n_dirs = 0;
1353 int i;
1355 g_return_val_if_fail(option != NULL, NULL);
1356 g_return_val_if_fail(label != NULL, NULL);
1358 hbox = gtk_hbox_new(FALSE, 4);
1360 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1361 FALSE, TRUE, 0);
1363 button = gtk_option_menu_new();
1364 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1366 menu = gtk_menu_new();
1367 gtk_option_menu_set_menu(GTK_OPTION_MENU(button), menu);
1369 gtk_icon_theme_get_search_path(icon_theme, &theme_dirs, &n_dirs);
1370 names = g_ptr_array_new();
1371 for (i = 0; i < n_dirs; i++)
1372 add_themes_from_dir(names, theme_dirs[i]);
1373 g_strfreev(theme_dirs);
1375 g_ptr_array_sort(names, strcmp2);
1377 for (i = 0; i < names->len; i++)
1379 GtkWidget *item;
1380 char *name = names->pdata[i];
1382 item = gtk_menu_item_new_with_label(name);
1383 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1384 gtk_widget_show_all(item);
1386 g_free(name);
1389 g_ptr_array_free(names, TRUE);
1391 option->update_widget = update_theme;
1392 option->read_widget = read_theme;
1393 option->widget = button;
1395 gtk_signal_connect_object(GTK_OBJECT(button), "changed",
1396 GTK_SIGNAL_FUNC(option_check_widget),
1397 (GtkObject *) option);
1399 return g_list_append(NULL, hbox);