Fixed some problems and cleaned up some things in my new Panel Options code spotted...
[rox-filer.git] / ROX-Filer / src / type.c
blobba0a0da98518ee6d14e9d9b7a2c0a3cbe73b82cd
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 static GtkIconTheme *icon_theme = NULL;
121 static GtkIconTheme *rox_theme = NULL;
122 static GtkIconTheme *gnome_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 if (slash == NULL)
192 g_warning("MIME type '%s' does not contain a '/' character!",
193 type_name);
194 return NULL;
197 mtype = g_new(MIME_type, 1);
198 mtype->media_type = g_strndup(type_name, slash - type_name);
199 mtype->subtype = g_strdup(slash + 1);
200 mtype->image = NULL;
201 mtype->comment = NULL;
203 mtype->executable = xdg_mime_mime_type_subclass(type_name,
204 "application/x-executable");
206 g_hash_table_insert(type_hash, g_strdup(type_name), mtype);
208 return mtype;
211 const char *basetype_name(DirItem *item)
213 if (item->flags & ITEM_FLAG_SYMLINK)
214 return _("Sym link");
215 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
216 return _("Mount point");
217 else if (item->flags & ITEM_FLAG_APPDIR)
218 return _("App dir");
220 switch (item->base_type)
222 case TYPE_FILE:
223 return _("File");
224 case TYPE_DIRECTORY:
225 return _("Dir");
226 case TYPE_CHAR_DEVICE:
227 return _("Char dev");
228 case TYPE_BLOCK_DEVICE:
229 return _("Block dev");
230 case TYPE_PIPE:
231 return _("Pipe");
232 case TYPE_SOCKET:
233 return _("Socket");
234 case TYPE_DOOR:
235 return _("Door");
238 return _("Unknown");
241 struct mime_list {
242 GList *list;
243 gboolean only_regular;
246 static void append_names(gpointer key, gpointer value, gpointer udata)
248 struct mime_list *mlist = (struct mime_list*) udata;
250 if(!mlist->only_regular || strncmp((char *)key, "inode/", 6)!=0)
251 mlist->list = g_list_prepend(mlist->list, key);
254 /* Return list of all mime type names. Caller must free the list
255 * but NOT the strings it contains (which are never freed).
256 If only_regular is true then inode types are excluded.
258 GList *mime_type_name_list(gboolean only_regular)
260 struct mime_list list;
262 list.list=NULL;
263 list.only_regular=only_regular;
265 g_hash_table_foreach(type_hash, append_names, &list);
266 list.list = g_list_sort(list.list, (GCompareFunc) strcmp);
268 return list.list;
271 /* MIME-type guessing */
273 /* Get the type of this file - stats the file and uses that if
274 * possible. For regular or missing files, uses the pathname.
276 MIME_type *type_get_type(const guchar *path)
278 DirItem *item;
279 MIME_type *type = NULL;
281 item = diritem_new("");
282 diritem_restat(path, item, NULL);
283 if (item->base_type != TYPE_ERROR)
284 type = item->mime_type;
285 diritem_free(item);
287 if (type)
288 return type;
290 type = type_from_path(path);
292 if (!type)
293 return text_plain;
295 return type;
298 /* Returns a pointer to the MIME-type.
300 * Tries all enabled methods:
301 * - Look for extended attribute
302 * - If no attribute, check file name
303 * - If no name rule, check contents
305 * NULL if we can't think of anything.
307 MIME_type *type_from_path(const char *path)
309 MIME_type *mime_type = NULL;
310 const char *type_name;
312 /* Check for extended attribute first */
313 mime_type = xtype_get(path);
314 if (mime_type)
315 return mime_type;
317 /* Try name and contents next */
318 type_name = xdg_mime_get_mime_type_for_file(path, NULL);
319 if (type_name)
320 return get_mime_type(type_name, TRUE);
322 return NULL;
325 /* Returns the file/dir in Choices for handling this type.
326 * NULL if there isn't one. g_free() the result.
328 char *handler_for(MIME_type *type)
330 char *type_name;
331 char *open;
332 char *target;
334 type_name = g_strconcat(type->media_type, "_", type->subtype, NULL);
335 open = choices_find_xdg_path_load(type_name, "MIME-types", SITE);
336 g_free(type_name);
338 if (!open)
339 open = choices_find_xdg_path_load(type->media_type,
340 "MIME-types", SITE);
342 if (!open)
343 return NULL;
345 /* Some programs behave differently depending on the command
346 * name (e.g., 'vim' vs 'gvim'), so symlinks need to be followed here.
348 target = readlink_dup(open);
349 if (!target)
351 return open;
354 if (target[0] == '/')
356 /* Absolute path */
357 g_free(open);
358 return target;
360 else
362 /* Relative path (shouldn't normally be needed) */
363 gchar *dir;
364 char *abs_path;
366 dir = g_path_get_dirname(open);
367 g_free(open);
369 abs_path = g_strconcat(dir, "/", target, NULL);
370 g_free(target);
371 g_free(dir);
373 return abs_path;
377 MIME_type *mime_type_lookup(const char *type)
379 return get_mime_type(type, TRUE);
382 static void init_aux_theme(GtkIconTheme **ptheme, const char *name)
384 if (*ptheme)
385 return;
386 *ptheme = gtk_icon_theme_new();
387 gtk_icon_theme_set_custom_theme(*ptheme, name);
390 inline static void init_rox_theme(void)
392 init_aux_theme(&rox_theme, "ROX");
395 inline static void init_gnome_theme(void)
397 init_aux_theme(&gnome_theme, "gnome");
400 /* We don't want ROX to override configured theme so try all possibilities
401 * in icon_theme first */
402 static GtkIconInfo *mime_type_lookup_icon_info(GtkIconTheme *theme,
403 MIME_type *type)
405 char *type_name = g_strconcat("mime-", type->media_type, ":",
406 type->subtype, NULL);
407 GtkIconInfo *full = gtk_icon_theme_lookup_icon(theme, type_name,
408 HUGE_HEIGHT, 0);
410 g_free(type_name);
411 if (!full)
413 /* Ugly hack... try for a GNOME icon */
414 if (type == inode_directory)
415 type_name = g_strdup("gnome-fs-directory");
416 else
417 type_name = g_strconcat("gnome-mime-", type->media_type,
418 "-", type->subtype, NULL);
419 full = gtk_icon_theme_lookup_icon(theme, type_name, HUGE_HEIGHT, 0);
420 g_free(type_name);
422 if (!full)
424 /* Try for a media type */
425 type_name = g_strconcat("mime-", type->media_type, NULL);
426 full = gtk_icon_theme_lookup_icon(theme, type_name, HUGE_HEIGHT, 0);
427 g_free(type_name);
429 if (!full)
431 /* Ugly hack... try for a GNOME default media icon */
432 type_name = g_strconcat("gnome-mime-", type->media_type, NULL);
434 full = gtk_icon_theme_lookup_icon(theme, type_name, HUGE_HEIGHT, 0);
435 g_free(type_name);
437 return full;
440 /* Actions for types */
442 /* Return the image for this type, loading it if needed.
443 * Places to check are: (eg type="text_plain", base="text")
444 * 1. <Choices>/MIME-icons/base_subtype
445 * 2. Icon theme 'mime-base:subtype'
446 * 3. Icon theme 'mime-base'
447 * 4. Unknown type icon.
449 * Note: You must g_object_unref() the image afterwards.
451 MaskedPixmap *type_to_icon(MIME_type *type)
453 GtkIconInfo *full;
454 char *type_name, *path;
455 time_t now;
457 if (type == NULL)
459 g_object_ref(im_unknown);
460 return im_unknown;
463 now = time(NULL);
464 /* Already got an image? */
465 if (type->image)
467 /* Yes - don't recheck too often */
468 if (abs(now - type->image_time) < 2)
470 g_object_ref(type->image);
471 return type->image;
473 g_object_unref(type->image);
474 type->image = NULL;
477 type_name = g_strconcat(type->media_type, "_", type->subtype,
478 ".png", NULL);
479 path = choices_find_xdg_path_load(type_name, "MIME-icons", SITE);
480 g_free(type_name);
481 if (path)
483 type->image = g_fscache_lookup(pixmap_cache, path);
484 g_free(path);
487 if (type->image)
488 goto out;
490 full = mime_type_lookup_icon_info(icon_theme, type);
491 if (!full && icon_theme != rox_theme)
493 init_rox_theme();
494 full = mime_type_lookup_icon_info(rox_theme, type);
496 if (!full && icon_theme != gnome_theme)
498 init_gnome_theme();
499 full = mime_type_lookup_icon_info(gnome_theme, type);
501 if (full)
503 const char *icon_path;
504 /* Get the actual icon through our cache, not through GTK, because
505 * GTK doesn't cache icons.
507 icon_path = gtk_icon_info_get_filename(full);
508 if (icon_path != NULL)
509 type->image = g_fscache_lookup(pixmap_cache, icon_path);
510 /* else shouldn't happen, because we didn't use
511 * GTK_ICON_LOOKUP_USE_BUILTIN.
513 gtk_icon_info_free(full);
516 out:
517 if (!type->image)
519 /* One ref from the type structure, one returned */
520 type->image = im_unknown;
521 g_object_ref(im_unknown);
524 type->image_time = now;
526 g_object_ref(type->image);
527 return type->image;
530 GdkAtom type_to_atom(MIME_type *type)
532 char *str;
533 GdkAtom retval;
535 g_return_val_if_fail(type != NULL, GDK_NONE);
537 str = g_strconcat(type->media_type, "/", type->subtype, NULL);
538 retval = gdk_atom_intern(str, FALSE);
539 g_free(str);
541 return retval;
544 static void show_shell_help(gpointer data)
546 info_message(_("Enter a shell command which will load \"$@\" into "
547 "a suitable program. Eg:\n\n"
548 "gimp \"$@\""));
551 /* Called if the user clicks on the OK button. Returns FALSE if an error
552 * was displayed instead of performing the action.
554 static gboolean set_shell_action(GtkWidget *dialog)
556 GtkEntry *entry;
557 const guchar *command;
558 gchar *tmp, *path;
559 int error = 0, len;
560 int fd;
562 entry = g_object_get_data(G_OBJECT(dialog), "shell_command");
564 g_return_val_if_fail(entry != NULL, FALSE);
566 command = gtk_entry_get_text(entry);
568 if (!strchr(command, '$'))
570 show_shell_help(NULL);
571 return FALSE;
574 path = get_action_save_path(dialog);
575 if (!path)
576 return FALSE;
578 tmp = g_strdup_printf("#! /bin/sh\nexec %s\n", command);
579 len = strlen(tmp);
581 fd = open(path, O_CREAT | O_WRONLY, 0755);
582 if (fd == -1)
583 error = errno;
584 else
586 FILE *file;
588 file = fdopen(fd, "w");
589 if (file)
591 if (fwrite(tmp, 1, len, file) < len)
592 error = errno;
593 if (fclose(file) && error == 0)
594 error = errno;
596 else
597 error = errno;
600 if (error)
601 report_error(g_strerror(error));
603 g_free(tmp);
604 g_free(path);
606 gtk_widget_destroy(dialog);
608 return TRUE;
611 static void set_action_response(GtkWidget *dialog, gint response, gpointer data)
613 if (response == GTK_RESPONSE_OK)
614 if (!set_shell_action(dialog))
615 return;
616 gtk_widget_destroy(dialog);
619 /* Return the path of the file in choices that handles this type and
620 * radio setting.
621 * NULL if nothing is defined for it.
623 static guchar *handler_for_radios(GObject *dialog)
625 Radios *radios;
626 MIME_type *type;
628 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
629 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
631 g_return_val_if_fail(radios != NULL, NULL);
632 g_return_val_if_fail(type != NULL, NULL);
634 switch (radios_get_value(radios))
636 case SET_MEDIA:
637 return choices_find_xdg_path_load(type->media_type,
638 "MIME-types", SITE);
639 case SET_TYPE:
641 gchar *tmp, *handler;
642 tmp = g_strconcat(type->media_type, "_",
643 type->subtype, NULL);
644 handler = choices_find_xdg_path_load(tmp,
645 "MIME-types",
646 SITE);
647 g_free(tmp);
648 return handler;
650 default:
651 g_warning("Bad type");
652 return NULL;
656 /* (radios can be NULL if called from clear_run_action) */
657 static void run_action_update(Radios *radios, gpointer data)
659 guchar *handler;
660 DropBox *drop_box;
661 GObject *dialog = G_OBJECT(data);
663 drop_box = g_object_get_data(dialog, "rox-dropbox");
665 g_return_if_fail(drop_box != NULL);
667 handler = handler_for_radios(dialog);
669 if (handler)
671 char *old = handler;
673 handler = readlink_dup(old);
674 if (handler)
675 g_free(old);
676 else
677 handler = old;
680 drop_box_set_path(DROP_BOX(drop_box), handler);
681 g_free(handler);
684 static void clear_run_action(GtkWidget *drop_box, GtkWidget *dialog)
686 guchar *handler;
688 handler = handler_for_radios(G_OBJECT(dialog));
690 if (handler)
691 remove_handler_with_confirm(handler);
693 run_action_update(NULL, dialog);
696 /* Called when a URI list is dropped onto the box in the Set Run Action
697 * dialog. Make sure it's an application, and make that the default
698 * handler.
700 static void drag_app_dropped(GtkWidget *drop_box,
701 const guchar *app,
702 GtkWidget *dialog)
704 DirItem *item;
706 item = diritem_new("");
707 diritem_restat(app, item, NULL);
708 if (item->flags & ITEM_FLAG_APPDIR || EXECUTABLE_FILE(item))
710 guchar *path;
712 path = get_action_save_path(dialog);
714 if (path)
716 if (symlink(app, path))
717 delayed_error("symlink: %s",
718 g_strerror(errno));
719 else
720 destroy_on_idle(dialog);
722 g_free(path);
725 else
726 delayed_error(
727 _("This is not a program! Give me an application "
728 "instead!"));
730 diritem_free(item);
733 /* Find the current command which is used to run files of this type.
734 * Returns NULL on failure. g_free() the result.
736 static guchar *get_current_command(MIME_type *type)
738 struct stat info;
739 char *handler, *nl, *data = NULL;
740 long len;
741 guchar *command = NULL;
743 handler = handler_for(type);
745 if (!handler)
746 return NULL; /* No current handler */
748 if (stat(handler, &info))
749 goto out; /* Can't stat */
751 if ((!S_ISREG(info.st_mode)) || info.st_size > 256)
752 goto out; /* Only use small regular files */
754 if (!load_file(handler, &data, &len))
755 goto out; /* Didn't load OK */
757 if (strncmp(data, "#! /bin/sh\nexec ", 16) != 0)
758 goto out; /* Not one of ours */
760 nl = strchr(data + 16, '\n');
761 if (!nl)
762 goto out; /* No newline! */
764 command = g_strndup(data + 16, nl - data - 16);
765 out:
766 g_free(handler);
767 g_free(data);
768 return command;
771 /* Find the current command which is used to run files of this type,
772 * and return a textual description of it.
773 * Only call for non-executable files.
774 * g_free() the result.
776 gchar *describe_current_command(MIME_type *type)
778 char *handler;
779 char *desc = NULL;
780 struct stat info;
782 g_return_val_if_fail(type != NULL, NULL);
784 handler = handler_for(type);
786 if (!handler)
787 return g_strdup(_("No run action defined"));
789 if (mc_stat(handler, &info) !=0 )
791 desc = g_strdup_printf(_("Error in handler %s: %s"), handler,
792 g_strerror(errno));
793 goto out;
796 if (S_ISDIR(info.st_mode))
798 const guchar *tmp;
799 uid_t dir_uid = info.st_uid;
801 tmp = make_path(handler, "AppRun");
803 if (mc_lstat(tmp, &info) != 0 || info.st_uid != dir_uid
804 || !(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
805 desc = g_strdup_printf(
806 _("Invalid application %s (bad AppRun)"),
807 handler);
808 /* Else, just report handler... */
810 goto out;
813 /* It's not an application directory, and it's not a symlink... */
815 if (access(handler, X_OK) != 0)
817 desc = g_strdup_printf(_("Non-executable %s"), handler);
818 goto out;
821 desc = get_current_command(type);
822 out:
823 if (!desc)
824 desc = handler;
825 else
826 g_free(handler);
828 return desc;
831 /* Display a dialog box allowing the user to set the default run action
832 * for this type.
834 void type_set_handler_dialog(MIME_type *type)
836 guchar *tmp;
837 GtkDialog *dialog;
838 GtkWidget *frame, *entry, *label, *button;
839 GtkWidget *hbox;
840 Radios *radios;
842 g_return_if_fail(type != NULL);
844 dialog = GTK_DIALOG(gtk_dialog_new());
845 gtk_dialog_set_has_separator(dialog, FALSE);
846 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
848 g_object_set_data(G_OBJECT(dialog), "mime_type", type);
850 gtk_window_set_title(GTK_WINDOW(dialog), _("Set run action"));
852 radios = radios_new(run_action_update, dialog);
853 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
855 radios_add(radios,
856 _("If a handler for the specific type isn't set up, "
857 "use this as the default."), SET_MEDIA,
858 _("Set default for all `%s/<anything>'"),
859 type->media_type);
861 radios_add(radios,
862 _("Use this application for all files with this MIME "
863 "type."), SET_TYPE,
864 _("Only for the type `%s' (%s/%s)"),
865 mime_type_comment(type),
866 type->media_type, type->subtype);
868 radios_set_value(radios, SET_TYPE);
870 frame = drop_box_new(_("Drop a suitable application here"));
872 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
874 radios_pack(radios, GTK_BOX(dialog->vbox));
875 gtk_box_pack_start(GTK_BOX(dialog->vbox), frame, TRUE, TRUE, 0);
877 g_signal_connect(frame, "path_dropped",
878 G_CALLBACK(drag_app_dropped), dialog);
879 g_signal_connect(frame, "clear",
880 G_CALLBACK(clear_run_action), dialog);
882 hbox = gtk_hbox_new(FALSE, 4);
883 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 4);
884 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
885 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_("OR")),
886 FALSE, TRUE, 0);
887 gtk_box_pack_start(GTK_BOX(hbox), gtk_hseparator_new(), TRUE, TRUE, 0);
889 hbox = gtk_hbox_new(FALSE, 4);
890 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
892 label = gtk_label_new(_("Enter a shell command:")),
893 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
894 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 4);
896 gtk_box_pack_start(GTK_BOX(hbox),
897 new_help_button(show_shell_help, NULL), FALSE, TRUE, 0);
899 entry = gtk_entry_new();
900 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, FALSE, TRUE, 0);
901 gtk_widget_grab_focus(entry);
902 g_object_set_data(G_OBJECT(dialog), "shell_command", entry);
903 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
905 /* If possible, fill in the entry box with the current command */
906 tmp = get_current_command(type);
907 if (tmp)
909 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
910 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
911 g_free(tmp);
913 else
915 gtk_entry_set_text(GTK_ENTRY(entry), " \"$@\"");
916 gtk_editable_set_position(GTK_EDITABLE(entry), 0);
919 gtk_dialog_add_button(dialog, GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL);
921 button = button_new_mixed(GTK_STOCK_OK, _("_Use Command"));
922 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
923 gtk_dialog_add_action_widget(dialog, button, GTK_RESPONSE_OK);
925 hbox = gtk_hbox_new(TRUE, 4);
926 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, FALSE, TRUE, 0);
928 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
930 g_signal_connect(dialog, "response",
931 G_CALLBACK(set_action_response), NULL);
933 gtk_widget_show_all(GTK_WIDGET(dialog));
936 /* path is an entry in Choices. If it's a symlink or a very small executable
937 * then just get rid of it, otherwise confirm first. It it doesn't exist,
938 * do nothing.
940 * FALSE on error (abort operation).
942 static gboolean remove_handler_with_confirm(const guchar *path)
944 struct stat info;
946 if (lstat(path, &info) == 0)
948 /* A binding already exists... */
949 if (S_ISREG(info.st_mode) && info.st_size > 256)
951 if (!confirm(_("A run action already exists and is "
952 "quite a big program - are you sure "
953 "you want to delete it?"),
954 GTK_STOCK_DELETE, NULL))
956 return FALSE;
960 if (unlink(path))
962 report_error(_("Can't remove %s: %s"),
963 path, g_strerror(errno));
964 return FALSE;
968 return TRUE;
971 /* The user wants to set a new default action for files of this type (or just
972 * clear the action). Removes the current binding if possible and returns the
973 * path to save the new one to. NULL means cancel. g_free() the result.
975 static char *get_action_save_path(GtkWidget *dialog)
977 guchar *path = NULL;
978 guchar *type_name = NULL;
979 MIME_type *type;
980 Radios *radios;
982 g_return_val_if_fail(dialog != NULL, NULL);
984 type = g_object_get_data(G_OBJECT(dialog), "mime_type");
985 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
987 g_return_val_if_fail(radios != NULL && type != NULL, NULL);
989 if (radios_get_value(radios) == SET_MEDIA)
990 type_name = g_strdup(type->media_type);
991 else
992 type_name = g_strconcat(type->media_type, "_",
993 type->subtype, NULL);
995 path = choices_find_xdg_path_save("", PROJECT, SITE, FALSE);
996 if (!path)
998 report_error(
999 _("Choices saving is disabled by CHOICESPATH variable"));
1000 goto out;
1002 g_free(path);
1004 path = choices_find_xdg_path_save(type_name, "MIME-types", SITE, TRUE);
1006 if (!remove_handler_with_confirm(path))
1007 null_g_free(&path);
1008 out:
1009 g_free(type_name);
1010 return path;
1013 MIME_type *mime_type_from_base_type(int base_type)
1015 switch (base_type)
1017 case TYPE_FILE:
1018 return text_plain;
1019 case TYPE_DIRECTORY:
1020 return inode_directory;
1021 case TYPE_PIPE:
1022 return inode_pipe;
1023 case TYPE_SOCKET:
1024 return inode_socket;
1025 case TYPE_BLOCK_DEVICE:
1026 return inode_block_dev;
1027 case TYPE_CHAR_DEVICE:
1028 return inode_char_dev;
1029 case TYPE_DOOR:
1030 return inode_door;
1032 return inode_unknown;
1035 /* Takes the st_mode field from stat() and returns the base type.
1036 * Should not be a symlink.
1038 int mode_to_base_type(int st_mode)
1040 if (S_ISREG(st_mode))
1041 return TYPE_FILE;
1042 else if (S_ISDIR(st_mode))
1043 return TYPE_DIRECTORY;
1044 else if (S_ISBLK(st_mode))
1045 return TYPE_BLOCK_DEVICE;
1046 else if (S_ISCHR(st_mode))
1047 return TYPE_CHAR_DEVICE;
1048 else if (S_ISFIFO(st_mode))
1049 return TYPE_PIPE;
1050 else if (S_ISSOCK(st_mode))
1051 return TYPE_SOCKET;
1052 else if (S_ISDOOR(st_mode))
1053 return TYPE_DOOR;
1055 return TYPE_ERROR;
1058 /* Returns TRUE is this is something that is run by looking up its type
1059 * in MIME-types and, hence, can have its run action set.
1061 gboolean can_set_run_action(DirItem *item)
1063 g_return_val_if_fail(item != NULL, FALSE);
1065 return item->base_type == TYPE_FILE && !EXECUTABLE_FILE(item);
1068 /* Parse file type colours and allocate/free them as necessary */
1069 static void alloc_type_colours(void)
1071 gboolean success[NUM_TYPE_COLOURS];
1072 int change_count = 0; /* No. needing realloc */
1073 int i;
1074 static gboolean allocated = FALSE;
1076 /* Parse colours */
1077 for (i = 0; i < NUM_TYPE_COLOURS; i++)
1079 GdkColor *c = &type_colours[i];
1080 gushort r = c->red;
1081 gushort g = c->green;
1082 gushort b = c->blue;
1084 gdk_color_parse(o_type_colours[i].value, &type_colours[i]);
1086 if (allocated && (c->red != r || c->green != g || c->blue != b))
1087 change_count++;
1090 /* Free colours if they were previously allocated and
1091 * have changed or become unneeded.
1093 if (allocated && (change_count || !o_display_colour_types.int_value))
1095 gdk_colormap_free_colors(gdk_rgb_get_colormap(),
1096 type_colours, NUM_TYPE_COLOURS);
1097 allocated = FALSE;
1100 /* Allocate colours, unless they are still allocated (=> they didn't
1101 * change) or we don't want them anymore.
1102 * XXX: what should be done if allocation fails?
1104 if (!allocated && o_display_colour_types.int_value)
1106 gdk_colormap_alloc_colors(gdk_rgb_get_colormap(),
1107 type_colours, NUM_TYPE_COLOURS,
1108 FALSE, TRUE, success);
1109 allocated = TRUE;
1113 static void expire_timer(gpointer key, gpointer value, gpointer data)
1115 MIME_type *type = value;
1117 type->image_time = 0;
1120 static void options_changed(void)
1122 alloc_type_colours();
1123 if (o_icon_theme.has_changed)
1125 set_icon_theme();
1126 g_hash_table_foreach(type_hash, expire_timer, NULL);
1127 full_refresh();
1131 /* Return a pointer to a (static) colour for this item. If colouring is
1132 * off, returns normal.
1134 GdkColor *type_get_colour(DirItem *item, GdkColor *normal)
1136 int type = item->base_type;
1138 if (!o_display_colour_types.int_value)
1139 return normal;
1141 if (EXECUTABLE_FILE(item))
1142 type = TYPE_EXEC;
1143 else if (item->flags & ITEM_FLAG_APPDIR)
1144 type = TYPE_APPDIR;
1146 g_return_val_if_fail(type >= 0 && type < NUM_TYPE_COLOURS, normal);
1148 return &type_colours[type];
1151 static char **get_xdg_data_dirs(int *n_dirs)
1153 const char *env;
1154 char **dirs;
1155 int i, n;
1157 env = getenv("XDG_DATA_DIRS");
1158 if (!env)
1159 env = "/usr/local/share/:/usr/share/";
1160 dirs = g_strsplit(env, ":", 0);
1161 g_return_val_if_fail(dirs != NULL, NULL);
1162 for (n = 0; dirs[n]; n++)
1164 for (i = n; i > 0; i--)
1165 dirs[i] = dirs[i - 1];
1166 env = getenv("XDG_DATA_HOME");
1167 if (env)
1168 dirs[0] = g_strdup(env);
1169 else
1170 dirs[0] = g_build_filename(g_get_home_dir(), ".local",
1171 "share", NULL);
1172 *n_dirs = n + 1;
1173 return dirs;
1176 /* Try to fill in 'type->comment' from this document */
1177 static void get_comment(MIME_type *type, const guchar *path)
1179 xmlNode *node;
1180 XMLwrapper *doc;
1182 doc = xml_cache_load(path);
1183 if (!doc)
1184 return;
1186 node = xml_get_section(doc, TYPE_NS, "comment");
1188 if (node)
1190 char *val;
1191 g_return_if_fail(type->comment == NULL);
1192 val= xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1);
1193 type->comment = g_strdup(val);
1194 xmlFree(val);
1197 g_object_unref(doc);
1200 /* Fill in the comment field for this MIME type */
1201 static void find_comment(MIME_type *type)
1203 char **dirs;
1204 int i, n_dirs = 0;
1206 if (type->comment)
1208 g_free(type->comment);
1209 type->comment = NULL;
1212 dirs = get_xdg_data_dirs(&n_dirs);
1213 g_return_if_fail(dirs != NULL);
1215 for (i = 0; i < n_dirs; i++)
1217 guchar *path;
1219 path = g_strdup_printf("%s/mime/%s/%s.xml", dirs[i],
1220 type->media_type, type->subtype);
1221 get_comment(type, path);
1222 g_free(path);
1223 if (type->comment)
1224 break;
1227 if (!type->comment)
1228 type->comment = g_strdup_printf("%s/%s", type->media_type,
1229 type->subtype);
1231 for (i = 0; i < n_dirs; i++)
1232 g_free(dirs[i]);
1233 g_free(dirs);
1236 const char *mime_type_comment(MIME_type *type)
1238 if (!type->comment)
1239 find_comment(type);
1241 return type->comment;
1244 static void unref_icon_theme(void)
1246 if (icon_theme && icon_theme != rox_theme && icon_theme != gnome_theme)
1247 g_object_unref(icon_theme);
1250 static void set_icon_theme(void)
1252 GtkIconInfo *info;
1253 char *icon_home;
1254 const char *theme_name = o_icon_theme.value;
1256 if (!theme_name || !*theme_name)
1257 theme_name = "ROX";
1259 if (!strcmp(theme_name, "ROX"))
1261 unref_icon_theme();
1262 init_rox_theme();
1263 icon_theme = rox_theme;
1265 else if (!strcmp(theme_name, "gnome"))
1267 unref_icon_theme();
1268 init_gnome_theme();
1269 icon_theme = gnome_theme;
1271 else
1273 if (icon_theme == rox_theme || icon_theme == gnome_theme)
1274 icon_theme = gtk_icon_theme_new();
1275 gtk_icon_theme_set_custom_theme(icon_theme, theme_name);
1278 info = theme_lookup_icon("mime-application:postscript",
1279 ICON_HEIGHT, 0);
1280 if (!info)
1282 info = theme_lookup_icon("gnome-mime-application-postscript",
1283 ICON_HEIGHT, 0);
1285 if (info)
1287 gtk_icon_info_free(info);
1288 return;
1291 icon_home = g_build_filename(home_dir, ".icons", NULL);
1292 if (!file_exists(icon_home))
1293 mkdir(icon_home, 0755);
1294 g_free(icon_home);
1296 icon_home = g_build_filename(home_dir, ".icons", "ROX", NULL);
1297 if (symlink(make_path(app_dir, "ROX"), icon_home))
1299 delayed_error(_("Failed to create symlink '%s':\n%s\n\n"
1300 "(this may mean that %s already exists as a link to an invalid "
1301 "directory; try deleting it)"),
1302 icon_home, g_strerror(errno), icon_home);
1303 open_to_show(icon_home);
1305 g_free(icon_home);
1307 gtk_icon_theme_rescan_if_needed(icon_theme);
1310 static guchar *read_theme(Option *option)
1312 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1313 GtkLabel *item;
1315 item = GTK_LABEL(GTK_BIN(om)->child);
1317 g_return_val_if_fail(item != NULL, g_strdup("ROX"));
1319 return g_strdup(gtk_label_get_text(item));
1322 static void update_theme(Option *option)
1324 GtkOptionMenu *om = GTK_OPTION_MENU(option->widget);
1325 GtkWidget *menu;
1326 GList *kids, *next;
1327 int i = 0;
1329 menu = gtk_option_menu_get_menu(om);
1331 kids = gtk_container_get_children(GTK_CONTAINER(menu));
1332 for (next = kids; next; next = next->next, i++)
1334 GtkLabel *item = GTK_LABEL(GTK_BIN(next->data)->child);
1335 const gchar *label;
1337 /* The label actually moves from the menu!! */
1338 if (!item)
1339 item = GTK_LABEL(GTK_BIN(om)->child);
1341 label = gtk_label_get_text(item);
1343 g_return_if_fail(label != NULL);
1345 if (strcmp(label, option->value) == 0)
1346 break;
1348 g_list_free(kids);
1350 if (next)
1351 gtk_option_menu_set_history(om, i);
1352 else
1353 g_warning("Theme '%s' not found", option->value);
1356 static void add_themes_from_dir(GPtrArray *names, const char *dir)
1358 GPtrArray *list;
1359 int i;
1361 if (access(dir, F_OK) != 0)
1362 return;
1364 list = list_dir(dir);
1365 g_return_if_fail(list != NULL);
1367 for (i = 0; i < list->len; i++)
1369 char *index_path;
1371 index_path = g_build_filename(dir, list->pdata[i],
1372 "index.theme", NULL);
1374 if (access(index_path, F_OK) == 0)
1375 g_ptr_array_add(names, list->pdata[i]);
1376 else
1377 g_free(list->pdata[i]);
1379 g_free(index_path);
1382 g_ptr_array_free(list, TRUE);
1385 static GList *build_icon_theme(Option *option, xmlNode *node, guchar *label)
1387 GtkWidget *button, *menu, *hbox;
1388 GPtrArray *names;
1389 gchar **theme_dirs = NULL;
1390 gint n_dirs = 0;
1391 int i;
1393 g_return_val_if_fail(option != NULL, NULL);
1394 g_return_val_if_fail(label != NULL, NULL);
1396 hbox = gtk_hbox_new(FALSE, 4);
1398 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(_(label)),
1399 FALSE, TRUE, 0);
1401 button = gtk_option_menu_new();
1402 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1404 menu = gtk_menu_new();
1405 gtk_option_menu_set_menu(GTK_OPTION_MENU(button), menu);
1407 gtk_icon_theme_get_search_path(icon_theme, &theme_dirs, &n_dirs);
1408 names = g_ptr_array_new();
1409 for (i = 0; i < n_dirs; i++)
1410 add_themes_from_dir(names, theme_dirs[i]);
1411 g_strfreev(theme_dirs);
1413 g_ptr_array_sort(names, strcmp2);
1415 for (i = 0; i < names->len; i++)
1417 GtkWidget *item;
1418 char *name = names->pdata[i];
1420 item = gtk_menu_item_new_with_label(name);
1421 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1422 gtk_widget_show_all(item);
1424 g_free(name);
1427 g_ptr_array_free(names, TRUE);
1429 option->update_widget = update_theme;
1430 option->read_widget = read_theme;
1431 option->widget = button;
1433 gtk_signal_connect_object(GTK_OBJECT(button), "changed",
1434 GTK_SIGNAL_FUNC(option_check_widget),
1435 (GtkObject *) option);
1437 return g_list_append(NULL, hbox);
1440 GtkIconInfo *theme_lookup_icon(const gchar *icon_name, gint size,
1441 GtkIconLookupFlags flags)
1443 GtkIconInfo *result = gtk_icon_theme_lookup_icon(icon_theme,
1444 icon_name, size, flags);
1446 if (!result && icon_theme != rox_theme)
1448 init_rox_theme();
1449 result = gtk_icon_theme_lookup_icon(rox_theme,
1450 icon_name, size, flags);
1452 if (!result && icon_theme != gnome_theme)
1454 init_gnome_theme();
1455 result = gtk_icon_theme_lookup_icon(gnome_theme,
1456 icon_name, size, flags);
1458 return result;
1461 GdkPixbuf *theme_load_icon(const gchar *icon_name, gint size,
1462 GtkIconLookupFlags flags, GError **perror)
1464 GError *err = NULL;
1465 GdkPixbuf *result = gtk_icon_theme_load_icon(icon_theme,
1466 icon_name, size, flags, &err);
1468 if (!result && icon_theme != gnome_theme)
1470 if (err)
1472 g_error_free(err);
1473 err = NULL;
1475 init_gnome_theme();
1476 result = gtk_icon_theme_load_icon(gnome_theme,
1477 icon_name, size, flags, &err);
1479 if (!result && icon_theme != rox_theme)
1481 if (err)
1483 g_error_free(err);
1484 err = NULL;
1486 init_rox_theme();
1487 result = gtk_icon_theme_load_icon(rox_theme,
1488 icon_name, size, flags, &err);
1490 if (perror)
1491 *perror = err;
1492 return result;