r4370: Added Vietnamese translation (Xomhau Newnick).
[rox-filer/ma.git] / ROX-Filer / src / type.c
blob2450fd516a10793731b7b64613457646c3aaaacd
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"
63 #include "run.h"
65 #define TYPE_NS "http://www.freedesktop.org/standards/shared-mime-info"
66 enum {SET_MEDIA, SET_TYPE};
68 /* Colours for file types (same order as base types) */
69 static gchar *opt_type_colours[][2] = {
70 {"display_err_colour", "#ff0000"},
71 {"display_unkn_colour", "#000000"},
72 {"display_dir_colour", "#000080"},
73 {"display_pipe_colour", "#444444"},
74 {"display_sock_colour", "#ff00ff"},
75 {"display_file_colour", "#000000"},
76 {"display_cdev_colour", "#000000"},
77 {"display_bdev_colour", "#000000"},
78 {"display_door_colour", "#ff00ff"},
79 {"display_exec_colour", "#006000"},
80 {"display_adir_colour", "#006000"}
82 #define NUM_TYPE_COLOURS\
83 (sizeof(opt_type_colours) / sizeof(opt_type_colours[0]))
85 /* Parsed colours for file types */
86 static Option o_type_colours[NUM_TYPE_COLOURS];
87 static GdkColor type_colours[NUM_TYPE_COLOURS];
89 /* Static prototypes */
90 static void alloc_type_colours(void);
91 static void options_changed(void);
92 static char *get_action_save_path(GtkWidget *dialog);
93 static MIME_type *get_mime_type(const gchar *type_name, gboolean can_create);
94 static gboolean remove_handler_with_confirm(const guchar *path);
95 static void set_icon_theme(void);
96 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label);
98 /* Hash of all allocated MIME types, indexed by "media/subtype".
99 * MIME_type structs are never freed; this table prevents memory leaks
100 * when rereading the config files.
102 static GHashTable *type_hash = NULL;
104 /* Most things on Unix are text files, so this is the default type */
105 MIME_type *text_plain;
106 MIME_type *inode_directory;
107 MIME_type *inode_mountpoint;
108 MIME_type *inode_pipe;
109 MIME_type *inode_socket;
110 MIME_type *inode_block_dev;
111 MIME_type *inode_char_dev;
112 MIME_type *application_executable;
113 MIME_type *application_octet_stream;
114 MIME_type *application_x_shellscript;
115 MIME_type *application_x_desktop;
116 MIME_type *inode_unknown;
117 MIME_type *inode_door;
119 static Option o_display_colour_types;
120 static Option o_icon_theme;
122 GtkIconTheme *icon_theme = NULL;
124 void type_init(void)
126 int i;
128 icon_theme = gtk_icon_theme_new();
130 type_hash = g_hash_table_new(g_str_hash, g_str_equal);
132 text_plain = get_mime_type("text/plain", TRUE);
133 inode_directory = get_mime_type("inode/directory", TRUE);
134 inode_mountpoint = get_mime_type("inode/mount-point", TRUE);
135 inode_pipe = get_mime_type("inode/fifo", TRUE);
136 inode_socket = get_mime_type("inode/socket", TRUE);
137 inode_block_dev = get_mime_type("inode/blockdevice", TRUE);
138 inode_char_dev = get_mime_type("inode/chardevice", TRUE);
139 application_executable = get_mime_type("application/x-executable", TRUE);
140 application_octet_stream = get_mime_type("application/octet-stream", TRUE);
141 application_x_shellscript = get_mime_type("application/x-shellscript", TRUE);
142 application_x_desktop = get_mime_type("application/x-desktop", TRUE);
143 application_x_desktop->executable = TRUE;
144 inode_unknown = get_mime_type("inode/unknown", TRUE);
145 inode_door = get_mime_type("inode/door", TRUE);
147 option_add_string(&o_icon_theme, "icon_theme", "ROX");
148 option_add_int(&o_display_colour_types, "display_colour_types", TRUE);
149 option_register_widget("icon-theme-chooser", build_icon_theme);
151 for (i = 0; i < NUM_TYPE_COLOURS; i++)
152 option_add_string(&o_type_colours[i],
153 opt_type_colours[i][0],
154 opt_type_colours[i][1]);
155 alloc_type_colours();
157 set_icon_theme();
159 option_add_notify(options_changed);
162 /* Read-load all the glob patterns.
163 * Note: calls filer_update_all.
165 void reread_mime_files(void)
167 gtk_icon_theme_rescan_if_needed(icon_theme);
169 xdg_mime_shutdown();
171 filer_update_all();
174 /* Returns the MIME_type structure for the given type name. It is looked
175 * up in type_hash and returned if found. If not found (and can_create is
176 * TRUE) then a new MIME_type is made, added to type_hash and returned.
177 * NULL is returned if type_name is not in type_hash and can_create is
178 * FALSE, or if type_name does not contain a '/' character.
180 static MIME_type *get_mime_type(const gchar *type_name, gboolean can_create)
182 MIME_type *mtype;
183 gchar *slash;
185 mtype = g_hash_table_lookup(type_hash, type_name);
186 if (mtype || !can_create)
187 return mtype;
189 slash = strchr(type_name, '/');
190 g_return_val_if_fail(slash != NULL, NULL); /* XXX: Report nicely */
192 mtype = g_new(MIME_type, 1);
193 mtype->media_type = g_strndup(type_name, slash - type_name);
194 mtype->subtype = g_strdup(slash + 1);
195 mtype->image = NULL;
196 mtype->comment = NULL;
198 mtype->executable = xdg_mime_mime_type_subclass(type_name,
199 "application/x-executable");
201 g_hash_table_insert(type_hash, g_strdup(type_name), mtype);
203 return mtype;
206 const char *basetype_name(DirItem *item)
208 if (item->flags & ITEM_FLAG_SYMLINK)
209 return _("Sym link");
210 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
211 return _("Mount point");
212 else if (item->flags & ITEM_FLAG_APPDIR)
213 return _("App dir");
215 switch (item->base_type)
217 case TYPE_FILE:
218 return _("File");
219 case TYPE_DIRECTORY:
220 return _("Dir");
221 case TYPE_CHAR_DEVICE:
222 return _("Char dev");
223 case TYPE_BLOCK_DEVICE:
224 return _("Block dev");
225 case TYPE_PIPE:
226 return _("Pipe");
227 case TYPE_SOCKET:
228 return _("Socket");
229 case TYPE_DOOR:
230 return _("Door");
233 return _("Unknown");
236 static void append_names(gpointer key, gpointer value, gpointer udata)
238 GList **list = (GList **) udata;
240 *list = g_list_prepend(*list, key);
243 /* Return list of all mime type names. Caller must free the list
244 * but NOT the strings it contains (which are never freed).
246 GList *mime_type_name_list(void)
248 GList *list = NULL;
250 g_hash_table_foreach(type_hash, append_names, &list);
251 list = g_list_sort(list, (GCompareFunc) strcmp);
253 return list;
256 /* MIME-type guessing */
258 /* Get the type of this file - stats the file and uses that if
259 * possible. For regular or missing files, uses the pathname.
261 MIME_type *type_get_type(const guchar *path)
263 DirItem *item;
264 MIME_type *type = NULL;
266 item = diritem_new("");
267 diritem_restat(path, item, NULL);
268 if (item->base_type != TYPE_ERROR)
269 type = item->mime_type;
270 diritem_free(item);
272 if (type)
273 return type;
275 type = type_from_path(path);
277 if (!type)
278 return text_plain;
280 return type;
283 /* Returns a pointer to the MIME-type.
285 * Tries all enabled methods:
286 * - Look for extended attribute
287 * - If no attribute, check file name
288 * - If no name rule, check contents
290 * NULL if we can't think of anything.
292 MIME_type *type_from_path(const char *path)
294 MIME_type *mime_type = NULL;
295 const char *type_name;
297 /* Check for extended attribute first */
298 mime_type = xtype_get(path);
299 if (mime_type)
300 return mime_type;
302 /* Try name and contents next */
303 type_name = xdg_mime_get_mime_type_for_file(path);
304 if (type_name)
305 return get_mime_type(type_name, TRUE);
307 return NULL;
310 /* Returns the file/dir in Choices for handling this type.
311 * NULL if there isn't one. g_free() the result.
313 char *handler_for(MIME_type *type)
315 char *type_name;
316 char *open;
318 type_name = g_strconcat(type->media_type, "_", type->subtype, NULL);
319 open = choices_find_xdg_path_load(type_name, "MIME-types", SITE);
320 g_free(type_name);
322 if (!open)
323 open = choices_find_xdg_path_load(type->media_type,
324 "MIME-types", SITE);
326 return open;
329 MIME_type *mime_type_lookup(const char *type)
331 return get_mime_type(type, TRUE);
334 /* Actions for types */
336 /* Return the image for this type, loading it if needed.
337 * Places to check are: (eg type="text_plain", base="text")
338 * 1. <Choices>/MIME-icons/base_subtype
339 * 2. Icon theme 'mime-base:subtype'
340 * 3. Icon theme 'mime-base'
341 * 4. Unknown type icon.
343 * Note: You must g_object_unref() the image afterwards.
345 MaskedPixmap *type_to_icon(MIME_type *type)
347 GdkPixbuf *full;
348 char *type_name, *path;
349 time_t now;
351 if (type == NULL)
353 g_object_ref(im_unknown);
354 return im_unknown;
357 now = time(NULL);
358 /* Already got an image? */
359 if (type->image)
361 /* Yes - don't recheck too often */
362 if (abs(now - type->image_time) < 2)
364 g_object_ref(type->image);
365 return type->image;
367 g_object_unref(type->image);
368 type->image = NULL;
371 type_name = g_strconcat(type->media_type, "_", type->subtype,
372 ".png", NULL);
373 path = choices_find_xdg_path_load(type_name, "MIME-icons", SITE);
374 g_free(type_name);
375 if (path)
377 type->image = g_fscache_lookup(pixmap_cache, path);
378 g_free(path);
381 if (type->image)
382 goto out;
384 type_name = g_strconcat("mime-", type->media_type, ":",
385 type->subtype, NULL);
386 full = gtk_icon_theme_load_icon(icon_theme, type_name, HUGE_HEIGHT,
387 0, NULL);
388 g_free(type_name);
389 if (!full)
391 /* Ugly hack... try for a GNOME icon */
392 if (type == inode_directory)
393 type_name = g_strdup("gnome-fs-directory");
394 else
395 type_name = g_strconcat("gnome-mime-", type->media_type,
396 "-", type->subtype, NULL);
397 full = gtk_icon_theme_load_icon(icon_theme,
398 type_name,
399 HUGE_HEIGHT, 0, NULL);
400 g_free(type_name);
402 if (!full)
404 /* Try for a media type */
405 type_name = g_strconcat("mime-", type->media_type, NULL);
406 full = gtk_icon_theme_load_icon(icon_theme,
407 type_name,
408 HUGE_HEIGHT, 0, NULL);
409 g_free(type_name);
411 if (!full)
413 /* Ugly hack... try for a GNOME default media icon */
414 type_name = g_strconcat("gnome-mime-", type->media_type, NULL);
416 full = gtk_icon_theme_load_icon(icon_theme,
417 type_name,
418 HUGE_HEIGHT, 0, NULL);
419 g_free(type_name);
421 if (full)
423 type->image = masked_pixmap_new(full);
424 g_object_unref(full);
427 out:
428 if (!type->image)
430 /* One ref from the type structure, one returned */
431 type->image = im_unknown;
432 g_object_ref(im_unknown);
435 type->image_time = now;
437 g_object_ref(type->image);
438 return type->image;
441 GdkAtom type_to_atom(MIME_type *type)
443 char *str;
444 GdkAtom retval;
446 g_return_val_if_fail(type != NULL, GDK_NONE);
448 str = g_strconcat(type->media_type, "/", type->subtype, NULL);
449 retval = gdk_atom_intern(str, FALSE);
450 g_free(str);
452 return retval;
455 static void show_shell_help(gpointer data)
457 info_message(_("Enter a shell command which will load \"$@\" into "
458 "a suitable program. Eg:\n\n"
459 "gimp \"$@\""));
462 /* Called if the user clicks on the OK button. Returns FALSE if an error
463 * was displayed instead of performing the action.
465 static gboolean set_shell_action(GtkWidget *dialog)
467 GtkEntry *entry;
468 const guchar *command;
469 gchar *tmp, *path;
470 int error = 0, len;
471 int fd;
473 entry = g_object_get_data(G_OBJECT(dialog), "shell_command");
475 g_return_val_if_fail(entry != NULL, FALSE);
477 command = gtk_entry_get_text(entry);
479 if (!strchr(command, '$'))
481 show_shell_help(NULL);
482 return FALSE;
485 path = get_action_save_path(dialog);
486 if (!path)
487 return FALSE;
489 tmp = g_strdup_printf("#! /bin/sh\nexec %s\n", command);
490 len = strlen(tmp);
492 fd = open(path, O_CREAT | O_WRONLY, 0755);
493 if (fd == -1)
494 error = errno;
495 else
497 FILE *file;
499 file = fdopen(fd, "w");
500 if (file)
502 if (fwrite(tmp, 1, len, file) < len)
503 error = errno;
504 if (fclose(file) && error == 0)
505 error = errno;
507 else
508 error = errno;
511 if (error)
512 report_error(g_strerror(error));
514 g_free(tmp);
515 g_free(path);
517 gtk_widget_destroy(dialog);
519 return TRUE;
522 static void set_action_response(GtkWidget *dialog, gint response, gpointer data)
524 if (response == GTK_RESPONSE_OK)
525 if (!set_shell_action(dialog))
526 return;
527 gtk_widget_destroy(dialog);
530 /* Return the path of the file in choices that handles this type and
531 * radio setting.
532 * NULL if nothing is defined for it.
534 static guchar *handler_for_radios(GObject *dialog)
536 Radios *radios;
537 MIME_type *type;
539 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
540 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
542 g_return_val_if_fail(radios != NULL, NULL);
543 g_return_val_if_fail(type != NULL, NULL);
545 switch (radios_get_value(radios))
547 case SET_MEDIA:
548 return choices_find_xdg_path_load(type->media_type,
549 "MIME-types", SITE);
550 case SET_TYPE:
552 gchar *tmp, *handler;
553 tmp = g_strconcat(type->media_type, "_",
554 type->subtype, NULL);
555 handler = choices_find_xdg_path_load(tmp,
556 "MIME-types",
557 SITE);
558 g_free(tmp);
559 return handler;
561 default:
562 g_warning("Bad type");
563 return NULL;
567 /* (radios can be NULL if called from clear_run_action) */
568 static void run_action_update(Radios *radios, gpointer data)
570 guchar *handler;
571 DropBox *drop_box;
572 GObject *dialog = G_OBJECT(data);
574 drop_box = g_object_get_data(dialog, "rox-dropbox");
576 g_return_if_fail(drop_box != NULL);
578 handler = handler_for_radios(dialog);
580 if (handler)
582 char *old = handler;
584 handler = readlink_dup(old);
585 if (handler)
586 g_free(old);
587 else
588 handler = old;
591 drop_box_set_path(DROP_BOX(drop_box), handler);
592 g_free(handler);
595 static void clear_run_action(GtkWidget *drop_box, GtkWidget *dialog)
597 guchar *handler;
599 handler = handler_for_radios(G_OBJECT(dialog));
601 if (handler)
602 remove_handler_with_confirm(handler);
604 run_action_update(NULL, dialog);
607 /* Called when a URI list is dropped onto the box in the Set Run Action
608 * dialog. Make sure it's an application, and make that the default
609 * handler.
611 static void drag_app_dropped(GtkWidget *drop_box,
612 const guchar *app,
613 GtkWidget *dialog)
615 DirItem *item;
617 item = diritem_new("");
618 diritem_restat(app, item, NULL);
619 if (item->flags & ITEM_FLAG_APPDIR || EXECUTABLE_FILE(item))
621 guchar *path;
623 path = get_action_save_path(dialog);
625 if (path)
627 if (symlink(app, path))
628 delayed_error("symlink: %s",
629 g_strerror(errno));
630 else
631 destroy_on_idle(dialog);
633 g_free(path);
636 else
637 delayed_error(
638 _("This is not a program! Give me an application "
639 "instead!"));
641 diritem_free(item);
644 /* Find the current command which is used to run files of this type.
645 * Returns NULL on failure. g_free() the result.
647 static guchar *get_current_command(MIME_type *type)
649 struct stat info;
650 char *handler, *nl, *data = NULL;
651 long len;
652 guchar *command = NULL;
654 handler = handler_for(type);
656 if (!handler)
657 return NULL; /* No current handler */
659 if (stat(handler, &info))
660 goto out; /* Can't stat */
662 if ((!S_ISREG(info.st_mode)) || info.st_size > 256)
663 goto out; /* Only use small regular files */
665 if (!load_file(handler, &data, &len))
666 goto out; /* Didn't load OK */
668 if (strncmp(data, "#! /bin/sh\nexec ", 16) != 0)
669 goto out; /* Not one of ours */
671 nl = strchr(data + 16, '\n');
672 if (!nl)
673 goto out; /* No newline! */
675 command = g_strndup(data + 16, nl - data - 16);
676 out:
677 g_free(handler);
678 g_free(data);
679 return command;
682 /* Find the current command which is used to run files of this type,
683 * and return a textual description of it.
684 * Only call for non-executable files.
685 * g_free() the result.
687 gchar *describe_current_command(MIME_type *type)
689 char *handler;
690 char *desc = NULL;
691 struct stat info;
692 char *target;
694 g_return_val_if_fail(type != NULL, NULL);
696 handler = handler_for(type);
698 if (!handler)
699 return g_strdup(_("No run action defined"));
701 target = readlink_dup(handler);
702 if (target)
704 /* Cope with relative paths (shouldn't normally be needed) */
706 if (target[0] == '/')
708 g_free(handler);
709 handler = target;
711 else
713 gchar *dir;
715 dir = g_path_get_dirname(handler);
716 g_free(handler);
717 handler = g_strconcat(dir, "/", target, NULL);
718 g_free(target);
719 g_free(dir);
723 if (mc_stat(handler, &info) !=0 )
725 desc = g_strdup_printf(_("Error in handler %s: %s"), handler,
726 g_strerror(errno));
727 goto out;
730 if (S_ISDIR(info.st_mode))
732 const guchar *tmp;
733 uid_t dir_uid = info.st_uid;
735 tmp = make_path(handler, "AppRun");
737 if (mc_lstat(tmp, &info) != 0 || info.st_uid != dir_uid
738 || !(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
739 desc = g_strdup_printf(
740 _("Invalid application %s (bad AppRun)"),
741 handler);
742 /* Else, just report handler... */
744 goto out;
747 /* It's not an application directory, and it's not a symlink... */
749 if (access(handler, X_OK) != 0)
751 desc = g_strdup_printf(_("Non-executable %s"), handler);
752 goto out;
755 desc = get_current_command(type);
756 out:
757 if (!desc)
758 desc = handler;
759 else
760 g_free(handler);
762 return desc;
765 /* Display a dialog box allowing the user to set the default run action
766 * for this type.
768 void type_set_handler_dialog(MIME_type *type)
770 guchar *tmp;
771 GtkDialog *dialog;
772 GtkWidget *frame, *entry, *label, *button;
773 GtkWidget *hbox;
774 Radios *radios;
776 g_return_if_fail(type != NULL);
778 dialog = GTK_DIALOG(gtk_dialog_new());
779 gtk_dialog_set_has_separator(dialog, FALSE);
780 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
782 g_object_set_data(G_OBJECT(dialog), "mime_type", type);
784 gtk_window_set_title(GTK_WINDOW(dialog), _("Set run action"));
786 radios = radios_new(run_action_update, dialog);
787 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
789 radios_add(radios,
790 _("If a handler for the specific type isn't set up, "
791 "use this as the default."), SET_MEDIA,
792 _("Set default for all `%s/<anything>'"),
793 type->media_type);
795 radios_add(radios,
796 _("Use this application for all files with this MIME "
797 "type."), SET_TYPE,
798 _("Only for the type `%s' (%s/%s)"),
799 mime_type_comment(type),
800 type->media_type, type->subtype);
802 radios_set_value(radios, SET_TYPE);
804 frame = drop_box_new(_("Drop a suitable application here"));
806 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
808 radios_pack(radios, GTK_BOX(dialog->vbox));
809 gtk_box_pack_start(GTK_BOX(dialog->vbox), frame, TRUE, TRUE, 0);
811 g_signal_connect(frame, "path_dropped",
812 G_CALLBACK(drag_app_dropped), dialog);
813 g_signal_connect(frame, "clear",
814 G_CALLBACK(clear_run_action), dialog);
816 hbox = gtk_hbox_new(FALSE, 4);
817 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 4);
818 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
819 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_("OR")),
820 FALSE, TRUE, 0);
821 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
823 hbox = gtk_hbox_new(FALSE, 4);
824 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
826 label = gtk_label_new(_("Enter a shell command:")),
827 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
828 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
830 gtk_box_pack_start(GTK_BOX(hbox),
831 new_help_button(show_shell_help, NULL), FALSE, TRUE, 0);
833 entry = gtk_entry_new();
834 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, FALSE, TRUE, 0);
835 gtk_widget_grab_focus(entry);
836 g_object_set_data(G_OBJECT(dialog), "shell_command", entry);
837 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
839 /* If possible, fill in the entry box with the current command */
840 tmp = get_current_command(type);
841 if (tmp)
843 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
844 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
845 g_free(tmp);
847 else
849 gtk_entry_set_text(GTK_ENTRY(entry), " \"$@\"");
850 gtk_editable_set_position(GTK_EDITABLE(entry), 0);
853 gtk_dialog_add_button(dialog, GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL);
855 button = button_new_mixed(GTK_STOCK_OK, _("_Use Command"));
856 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
857 gtk_dialog_add_action_widget(dialog, button, GTK_RESPONSE_OK);
859 hbox = gtk_hbox_new(TRUE, 4);
860 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
862 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
864 g_signal_connect(dialog, "response",
865 G_CALLBACK(set_action_response), NULL);
867 gtk_widget_show_all(GTK_WIDGET(dialog));
870 /* path is an entry in Choices. If it's a symlink or a very small executable
871 * then just get rid of it, otherwise confirm first. It it doesn't exist,
872 * do nothing.
874 * FALSE on error (abort operation).
876 static gboolean remove_handler_with_confirm(const guchar *path)
878 struct stat info;
880 if (lstat(path, &info) == 0)
882 /* A binding already exists... */
883 if (S_ISREG(info.st_mode) && info.st_size > 256)
885 if (!confirm(_("A run action already exists and is "
886 "quite a big program - are you sure "
887 "you want to delete it?"),
888 GTK_STOCK_DELETE, NULL))
890 return FALSE;
894 if (unlink(path))
896 report_error(_("Can't remove %s: %s"),
897 path, g_strerror(errno));
898 return FALSE;
902 return TRUE;
905 /* The user wants to set a new default action for files of this type (or just
906 * clear the action). Removes the current binding if possible and returns the
907 * path to save the new one to. NULL means cancel. g_free() the result.
909 static char *get_action_save_path(GtkWidget *dialog)
911 guchar *path = NULL;
912 guchar *type_name = NULL;
913 MIME_type *type;
914 Radios *radios;
916 g_return_val_if_fail(dialog != NULL, NULL);
918 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
919 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
921 g_return_val_if_fail(radios != NULL && type != NULL, NULL);
923 if (radios_get_value(radios) == SET_MEDIA)
924 type_name = g_strdup(type->media_type);
925 else
926 type_name = g_strconcat(type->media_type, "_",
927 type->subtype, NULL);
929 path = choices_find_xdg_path_save("", PROJECT, SITE, FALSE);
930 if (!path)
932 report_error(
933 _("Choices saving is disabled by CHOICESPATH variable"));
934 goto out;
936 g_free(path);
938 path = choices_find_xdg_path_save(type_name, "MIME-types", SITE, TRUE);
940 if (!remove_handler_with_confirm(path))
941 null_g_free(&path);
942 out:
943 g_free(type_name);
944 return path;
947 MIME_type *mime_type_from_base_type(int base_type)
949 switch (base_type)
951 case TYPE_FILE:
952 return text_plain;
953 case TYPE_DIRECTORY:
954 return inode_directory;
955 case TYPE_PIPE:
956 return inode_pipe;
957 case TYPE_SOCKET:
958 return inode_socket;
959 case TYPE_BLOCK_DEVICE:
960 return inode_block_dev;
961 case TYPE_CHAR_DEVICE:
962 return inode_char_dev;
963 case TYPE_DOOR:
964 return inode_door;
966 return inode_unknown;
969 /* Takes the st_mode field from stat() and returns the base type.
970 * Should not be a symlink.
972 int mode_to_base_type(int st_mode)
974 if (S_ISREG(st_mode))
975 return TYPE_FILE;
976 else if (S_ISDIR(st_mode))
977 return TYPE_DIRECTORY;
978 else if (S_ISBLK(st_mode))
979 return TYPE_BLOCK_DEVICE;
980 else if (S_ISCHR(st_mode))
981 return TYPE_CHAR_DEVICE;
982 else if (S_ISFIFO(st_mode))
983 return TYPE_PIPE;
984 else if (S_ISSOCK(st_mode))
985 return TYPE_SOCKET;
986 else if (S_ISDOOR(st_mode))
987 return TYPE_DOOR;
989 return TYPE_ERROR;
992 /* Returns TRUE is this is something that is run by looking up its type
993 * in MIME-types and, hence, can have its run action set.
995 gboolean can_set_run_action(DirItem *item)
997 g_return_val_if_fail(item != NULL, FALSE);
999 return item->base_type == TYPE_FILE && !EXECUTABLE_FILE(item);
1002 /* Parse file type colours and allocate/free them as necessary */
1003 static void alloc_type_colours(void)
1005 gboolean success[NUM_TYPE_COLOURS];
1006 int change_count = 0; /* No. needing realloc */
1007 int i;
1008 static gboolean allocated = FALSE;
1010 /* Parse colours */
1011 for (i = 0; i < NUM_TYPE_COLOURS; i++)
1013 GdkColor *c = &type_colours[i];
1014 gushort r = c->red;
1015 gushort g = c->green;
1016 gushort b = c->blue;
1018 gdk_color_parse(o_type_colours[i].value, &type_colours[i]);
1020 if (allocated && (c->red != r || c->green != g || c->blue != b))
1021 change_count++;
1024 /* Free colours if they were previously allocated and
1025 * have changed or become unneeded.
1027 if (allocated && (change_count || !o_display_colour_types.int_value))
1029 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1030 type_colours, NUM_TYPE_COLOURS);
1031 allocated = FALSE;
1034 /* Allocate colours, unless they are still allocated (=> they didn't
1035 * change) or we don't want them anymore.
1036 * XXX: what should be done if allocation fails?
1038 if (!allocated && o_display_colour_types.int_value)
1040 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1041 type_colours, NUM_TYPE_COLOURS,
1042 FALSE, TRUE, success);
1043 allocated = TRUE;
1047 static void expire_timer(gpointer key, gpointer value, gpointer data)
1049 MIME_type *type = value;
1051 type->image_time = 0;
1054 static void options_changed(void)
1056 alloc_type_colours();
1057 if (o_icon_theme.has_changed)
1059 set_icon_theme();
1060 g_hash_table_foreach(type_hash, expire_timer, NULL);
1061 full_refresh();
1065 /* Return a pointer to a (static) colour for this item. If colouring is
1066 * off, returns normal.
1068 GdkColor *type_get_colour(DirItem *item, GdkColor *normal)
1070 int type = item->base_type;
1072 if (!o_display_colour_types.int_value)
1073 return normal;
1075 if (EXECUTABLE_FILE(item))
1076 type = TYPE_EXEC;
1077 else if (item->flags & ITEM_FLAG_APPDIR)
1078 type = TYPE_APPDIR;
1080 g_return_val_if_fail(type >= 0 && type < NUM_TYPE_COLOURS, normal);
1082 return &type_colours[type];
1085 static char **get_xdg_data_dirs(int *n_dirs)
1087 const char *env;
1088 char **dirs;
1089 int i, n;
1091 env = getenv("XDG_DATA_DIRS");
1092 if (!env)
1093 env = "/usr/local/share/:/usr/share/";
1094 dirs = g_strsplit(env, ":", 0);
1095 g_return_val_if_fail(dirs != NULL, NULL);
1096 for (n = 0; dirs[n]; n++)
1098 for (i = n; i > 0; i--)
1099 dirs[i] = dirs[i - 1];
1100 env = getenv("XDG_DATA_HOME");
1101 if (env)
1102 dirs[0] = g_strdup(env);
1103 else
1104 dirs[0] = g_build_filename(g_get_home_dir(), ".local",
1105 "share", NULL);
1106 *n_dirs = n + 1;
1107 return dirs;
1110 /* Try to fill in 'type->comment' from this document */
1111 static void get_comment(MIME_type *type, const guchar *path)
1113 xmlNode *node;
1114 XMLwrapper *doc;
1116 doc = xml_cache_load(path);
1117 if (!doc)
1118 return;
1120 node = xml_get_section(doc, TYPE_NS, "comment");
1122 if (node)
1124 char *val;
1125 g_return_if_fail(type->comment == NULL);
1126 val= xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
1127 type->comment = g_strdup(val);
1128 xmlFree(val);
1131 g_object_unref(doc);
1134 /* Fill in the comment field for this MIME type */
1135 static void find_comment(MIME_type *type)
1137 char **dirs;
1138 int i, n_dirs = 0;
1140 if (type->comment)
1142 g_free(type->comment);
1143 type->comment = NULL;
1146 dirs = get_xdg_data_dirs(&n_dirs);
1147 g_return_if_fail(dirs != NULL);
1149 for (i = 0; i < n_dirs; i++)
1151 guchar *path;
1153 path = g_strdup_printf("%s/mime/%s/%s.xml", dirs[i],
1154 type->media_type, type->subtype);
1155 get_comment(type, path);
1156 g_free(path);
1157 if (type->comment)
1158 break;
1161 if (!type->comment)
1162 type->comment = g_strdup_printf("%s/%s", type->media_type,
1163 type->subtype);
1165 for (i = 0; i < n_dirs; i++)
1166 g_free(dirs[i]);
1167 g_free(dirs);
1170 const char *mime_type_comment(MIME_type *type)
1172 if (!type->comment)
1173 find_comment(type);
1175 return type->comment;
1178 static void set_icon_theme(void)
1180 GtkIconInfo *info;
1181 char *icon_home;
1182 const char *theme_name = o_icon_theme.value;
1184 if (!theme_name || !*theme_name)
1185 theme_name = "ROX";
1187 while (1)
1189 gtk_icon_theme_set_custom_theme(icon_theme, theme_name);
1190 info = gtk_icon_theme_lookup_icon(icon_theme,
1191 "mime-application:postscript",
1192 ICON_HEIGHT, 0);
1193 if (!info)
1195 info = gtk_icon_theme_lookup_icon(icon_theme,
1196 "gnome-mime-application-postscript",
1197 ICON_HEIGHT, 0);
1199 if (info)
1201 gtk_icon_info_free(info);
1202 return;
1205 if (strcmp(theme_name, "ROX") == 0)
1206 break;
1208 delayed_error(_("Icon theme '%s' does not contain MIME icons. "
1209 "Using ROX default theme instead."),
1210 theme_name);
1212 theme_name = "ROX";
1215 icon_home = g_build_filename(home_dir, ".icons", NULL);
1216 if (!file_exists(icon_home))
1217 mkdir(icon_home, 0755);
1218 g_free(icon_home);
1220 icon_home = g_build_filename(home_dir, ".icons", "ROX", NULL);
1221 if (symlink(make_path(app_dir, "ROX"), icon_home))
1223 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1224 "(this may mean that the ROX theme already exists there, but "
1225 "the 'mime-application:postscript' icon couldn't be loaded for "
1226 "some reason, or %s is a link to an invalid directory; try "
1227 "deleting it)"), icon_home, g_strerror(errno), icon_home);
1228 open_to_show(icon_home);
1230 g_free(icon_home);
1232 gtk_icon_theme_rescan_if_needed(icon_theme);
1235 static guchar *read_theme(Option *option)
1237 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1238 GtkLabel *item;
1240 item = GTK_LABEL(GTK_BIN(om)->child);
1242 g_return_val_if_fail(item != NULL, g_strdup("ROX"));
1244 return g_strdup(gtk_label_get_text(item));
1247 static void update_theme(Option *option)
1249 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1250 GtkWidget *menu;
1251 GList *kids, *next;
1252 int i = 0;
1254 menu = gtk_option_menu_get_menu(om);
1256 kids = gtk_container_get_children(GTK_CONTAINER(menu));
1257 for (next = kids; next; next = next->next, i++)
1259 GtkLabel *item = GTK_LABEL(GTK_BIN(next->data)->child);
1260 const gchar *label;
1262 /* The label actually moves from the menu!! */
1263 if (!item)
1264 item = GTK_LABEL(GTK_BIN(om)->child);
1266 label = gtk_label_get_text(item);
1268 g_return_if_fail(label != NULL);
1270 if (strcmp(label, option->value) == 0)
1271 break;
1273 g_list_free(kids);
1275 if (next)
1276 gtk_option_menu_set_history(om, i);
1277 else
1278 g_warning("Theme '%s' not found", option->value);
1281 static void add_themes_from_dir(GPtrArray *names, const char *dir)
1283 GPtrArray *list;
1284 int i;
1286 if (access(dir, F_OK) != 0)
1287 return;
1289 list = list_dir(dir);
1290 g_return_if_fail(list != NULL);
1292 for (i = 0; i < list->len; i++)
1294 char *index_path;
1296 index_path = g_build_filename(dir, list->pdata[i],
1297 "index.theme", NULL);
1299 if (access(index_path, F_OK) == 0)
1300 g_ptr_array_add(names, list->pdata[i]);
1301 else
1302 g_free(list->pdata[i]);
1304 g_free(index_path);
1307 g_ptr_array_free(list, TRUE);
1310 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label)
1312 GtkWidget *button, *menu, *hbox;
1313 GPtrArray *names;
1314 gchar **theme_dirs = NULL;
1315 gint n_dirs = 0;
1316 int i;
1318 g_return_val_if_fail(option != NULL, NULL);
1319 g_return_val_if_fail(label != NULL, NULL);
1321 hbox = gtk_hbox_new(FALSE, 4);
1323 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1324 FALSE, TRUE, 0);
1326 button = gtk_option_menu_new();
1327 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1329 menu = gtk_menu_new();
1330 gtk_option_menu_set_menu(GTK_OPTION_MENU(button), menu);
1332 gtk_icon_theme_get_search_path(icon_theme, &theme_dirs, &n_dirs);
1333 names = g_ptr_array_new();
1334 for (i = 0; i < n_dirs; i++)
1335 add_themes_from_dir(names, theme_dirs[i]);
1336 g_strfreev(theme_dirs);
1338 g_ptr_array_sort(names, strcmp2);
1340 for (i = 0; i < names->len; i++)
1342 GtkWidget *item;
1343 char *name = names->pdata[i];
1345 item = gtk_menu_item_new_with_label(name);
1346 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1347 gtk_widget_show_all(item);
1349 g_free(name);
1352 g_ptr_array_free(names, TRUE);
1354 option->update_widget = update_theme;
1355 option->read_widget = read_theme;
1356 option->widget = button;
1358 gtk_signal_connect_object(GTK_OBJECT(button), "changed",
1359 GTK_SIGNAL_FUNC(option_check_widget),
1360 (GtkObject *) option);
1362 return g_list_append(NULL, hbox);