4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, 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)
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
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 /* usericons.c - handle user-defined icons. Diego Zamboni, Feb 7, 2001. */
32 #include <libxml/parser.h>
40 #include "gui_support.h"
47 #include "usericons.h"
57 /* Used to index the 'radios' array... */
60 #define SET_PATH 0 /* Store in globicons */
61 #define SET_COPY 3 /* Create .DirIcon */
63 static GHashTable
*glob_icons
= NULL
; /* Pathname -> Icon pathname */
65 /* Static prototypes */
66 static const char *process_globicons_line(gchar
*line
);
67 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
);
68 static void get_path_set_icon(GtkWidget
*dialog
);
69 static void show_icon_help(gpointer data
);
70 static void write_globicons(void);
71 static void show_current_dirs_menu(GtkWidget
*button
, gpointer data
);
72 static void add_globicon(const gchar
*path
, const gchar
*icon
);
73 static void drag_icon_dropped(GtkWidget
*frame
,
74 GdkDragContext
*context
,
77 GtkSelectionData
*selection_data
,
81 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
83 static void delete_globicon(const gchar
*path
);
85 /****************************************************************
86 * EXTERNAL INTERFACE *
87 ****************************************************************/
89 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
92 static time_t last_read
= (time_t) 0;
98 glob_icons
= g_hash_table_new(g_str_hash
, g_str_equal
);
100 path
= choices_find_path_load("globicons", PROJECT
);
102 return; /* Nothing to load */
104 if (mc_stat(path
, &info
) == -1)
107 if (info
.st_mtime
<= last_read
)
108 goto out
; /* File hasn't been modified since we last read it */
110 g_hash_table_foreach_remove(glob_icons
, free_globicon
, NULL
);
112 doc
= xmlParseFile(path
);
115 xmlNodePtr node
, icon
, root
;
118 root
= xmlDocGetRootElement(doc
);
120 /* Handle the new XML file format */
121 for (node
= root
->xmlChildrenNode
; node
; node
= node
->next
)
123 gchar
*path
, *icon_path
;
125 if (node
->type
!= XML_ELEMENT_NODE
)
127 if (strcmp(node
->name
, "rule") != 0)
129 icon
= get_subnode(node
, NULL
, "icon");
132 match
= xmlGetProp(node
, "match");
136 icon_path
= xmlNodeGetContent(icon
);
137 path
= expand_path(match
);
138 g_hash_table_insert(glob_icons
, path
, icon_path
);
146 /* Handle the old non-XML format */
147 parse_file(path
, process_globicons_line
);
148 if (g_hash_table_size(glob_icons
))
149 write_globicons(); /* Upgrade to new format */
152 last_read
= time(NULL
); /* Update time stamp */
157 /* Set an item's image field according to the globicons patterns if
158 * it matches one of them and the file exists.
160 void check_globicon(const guchar
*path
, DirItem
*item
)
164 g_return_if_fail(item
&& !item
->image
);
166 gi
= g_hash_table_lookup(glob_icons
, path
);
168 item
->image
= g_fscache_lookup(pixmap_cache
, gi
);
171 gboolean
create_diricon(const guchar
*filepath
, const guchar
*iconpath
)
175 GError
*error
= NULL
;
177 pic
= g_fscache_lookup(pixmap_cache
, iconpath
);
181 _("Unable to load image file -- maybe it's not in a "
182 "format I understand, or maybe the permissions are "
184 "The icon has not been changed."));
188 icon_path
= make_path(filepath
, ".DirIcon")->str
;
189 gdk_pixbuf_save(pic
->src_pixbuf
, icon_path
,
191 "tEXt::Software", PROJECT
,
197 delayed_error(_("Error creating image '%s':\n%s"),
198 icon_path
, error
->message
);
203 dir_check_this(filepath
);
208 /* Add a globicon mapping for the given file to the given icon path */
209 gboolean
set_icon_path(const guchar
*filepath
, const guchar
*iconpath
)
214 /* Check if file exists */
215 if (mc_stat(iconpath
, &icon
))
217 delayed_error(_("The pathname you gave does not exist. "
218 "The icon has not been changed."));
222 /* Check if we can load the image, warn the user if not. */
223 pic
= g_fscache_lookup(pixmap_cache
, iconpath
);
227 _("Unable to load image file -- maybe it's not in a "
228 "format I understand, or maybe the permissions are "
230 "The icon has not been changed."));
235 /* Add the globicon mapping and update visible icons */
236 add_globicon(filepath
, iconpath
);
241 static void dialog_response(GtkWidget
*dialog
, gint response
, gpointer data
)
243 if (response
== GTK_RESPONSE_OK
)
244 get_path_set_icon(dialog
);
245 else if (response
== GTK_RESPONSE_CANCEL
)
246 gtk_widget_destroy(dialog
);
247 else if (response
== DELETE_ICON
)
252 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
253 g_return_if_fail(path
!= NULL
);
255 delete_globicon(path
);
257 icon_path
= make_path(path
, ".DirIcon")->str
;
258 if (access(icon_path
, F_OK
) == 0)
262 list
= g_list_prepend(NULL
, icon_path
);
267 gtk_widget_destroy(dialog
);
271 /* Display a dialog box allowing the user to set the icon for
272 * a file or directory.
274 void icon_set_handler_dialog(DirItem
*item
, const guchar
*path
)
279 GtkWidget
*frame
, *hbox
, *vbox2
;
280 GtkWidget
*entry
, *label
, *button
, *align
, *icon
;
282 GtkRadioButton
*group
;
283 GtkTargetEntry targets
[] = {
284 {"text/uri-list", 0, TARGET_URI_LIST
},
288 g_return_if_fail(item
!= NULL
&& path
!= NULL
);
290 gi
= g_hash_table_lookup(glob_icons
, path
);
292 dialog
= GTK_DIALOG(gtk_dialog_new());
293 gtk_dialog_set_has_separator(dialog
, FALSE
);
294 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
295 g_object_set_data_full(G_OBJECT(dialog
), "pathname",
296 strdup(path
), g_free
);
298 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set icon"));
300 radio
= g_new(GtkWidget
*, 4);
302 tmp
= g_strdup_printf(_("Set icon for all `%s/<anything>'"),
303 item
->mime_type
->media_type
);
304 radio
[SET_MEDIA
] = gtk_radio_button_new_with_label(NULL
, tmp
);
305 gtk_box_pack_start(GTK_BOX(dialog
->vbox
),
306 radio
[SET_MEDIA
], FALSE
, TRUE
, 0);
308 group
= GTK_RADIO_BUTTON(radio
[SET_MEDIA
]);
310 tmp
= g_strdup_printf(_("Only for the type `%s/%s'"),
311 item
->mime_type
->media_type
,
312 item
->mime_type
->subtype
);
313 radio
[SET_TYPE
] = gtk_radio_button_new_with_label_from_widget(group
,
315 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), radio
[SET_TYPE
],
319 tmp
= g_strdup_printf(_("Only for the file `%s'"), path
);
320 radio
[SET_PATH
] = gtk_radio_button_new_with_label_from_widget(group
,
322 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), radio
[SET_PATH
],
324 gtk_tooltips_set_tip(tooltips
, radio
[SET_PATH
],
325 _("Add the file and image filenames to your "
326 "personal list. The setting will be lost if the image "
327 "or the file is moved."), NULL
);
329 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio
[SET_PATH
]), TRUE
);
331 /* If it's a directory, offer to create a .DirIcon */
332 if (mc_stat(path
, &info
) == 0 && S_ISDIR(info
.st_mode
))
334 radio
[3] = gtk_radio_button_new_with_label_from_widget(
335 group
, _("Copy image into directory"));
336 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), radio
[SET_COPY
],
338 gtk_tooltips_set_tip(tooltips
, radio
[SET_COPY
],
339 _("Copy the image inside the directory, as "
340 "a hidden file called '.DirIcon'. "
341 "All users will than see the "
342 "icon, and you can move the directory around safely. "
343 "This is usually the best option if you can write to "
344 "the directory."), NULL
);
346 if (access(path
, W_OK
) == 0)
347 gtk_toggle_button_set_active(
348 GTK_TOGGLE_BUTTON(radio
[SET_COPY
]),
352 radio
[SET_COPY
] = NULL
;
354 g_object_set_data_full(G_OBJECT(dialog
), "radios", radio
, g_free
);
355 g_object_set_data(G_OBJECT(dialog
), "mime_type", item
->mime_type
);
357 frame
= gtk_frame_new(NULL
);
358 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), frame
, TRUE
, TRUE
, 4);
359 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_IN
);
360 gtk_container_set_border_width(GTK_CONTAINER(frame
), 4);
362 gtk_drag_dest_set(frame
, GTK_DEST_DEFAULT_ALL
,
363 targets
, sizeof(targets
) / sizeof(*targets
),
365 g_signal_connect(frame
, "drag_data_received",
366 G_CALLBACK(drag_icon_dropped
), dialog
);
368 vbox2
= gtk_vbox_new(FALSE
, 0);
369 gtk_container_add(GTK_CONTAINER(frame
), vbox2
);
371 label
= gtk_label_new(_("Drop an icon file here"));
372 gtk_misc_set_padding(GTK_MISC(label
), 10, 10);
373 gtk_box_pack_start(GTK_BOX(vbox2
), label
, TRUE
, TRUE
, 0);
374 align
= gtk_alignment_new(1, 1, 0, 0);
375 gtk_box_pack_start(GTK_BOX(vbox2
), align
, FALSE
, TRUE
, 0);
376 button
= gtk_button_new();
377 gtk_container_add(GTK_CONTAINER(align
), button
);
378 icon
= gtk_image_new_from_pixbuf(im_dirs
->pixbuf
);
379 gtk_container_add(GTK_CONTAINER(button
), icon
);
380 gtk_tooltips_set_tip(tooltips
, button
,
381 _("Menu of directories previously used for icons"),
383 g_signal_connect(button
, "clicked",
384 G_CALLBACK(show_current_dirs_menu
), NULL
);
386 hbox
= gtk_hbox_new(FALSE
, 4);
387 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 4);
388 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
389 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
391 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
393 hbox
= gtk_hbox_new(FALSE
, 4);
394 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 0);
396 label
= gtk_label_new(_("Enter the path of an icon file:")),
397 gtk_misc_set_alignment(GTK_MISC(label
), 0, .5);
398 gtk_box_pack_start(GTK_BOX(hbox
), label
, TRUE
, TRUE
, 4);
400 gtk_box_pack_start(GTK_BOX(hbox
),
401 new_help_button(show_icon_help
, NULL
), FALSE
, TRUE
, 0);
403 entry
= gtk_entry_new();
404 /* Set the current icon as the default text if there is one */
406 gtk_entry_set_text(GTK_ENTRY(entry
), gi
);
408 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), entry
, FALSE
, TRUE
, 0);
409 gtk_widget_grab_focus(entry
);
410 g_object_set_data(G_OBJECT(dialog
), "icon_path", entry
);
411 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
413 button
= button_new_mixed(GTK_STOCK_DELETE
, _("_Remove"));
414 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
415 gtk_dialog_add_action_widget(dialog
, button
, DELETE_ICON
);
416 gtk_dialog_add_buttons(dialog
,
417 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
418 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
420 g_signal_connect(dialog
, "response", G_CALLBACK(dialog_response
), NULL
);
421 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
423 gtk_widget_show_all(GTK_WIDGET(dialog
));
427 /****************************************************************
428 * INTERNAL FUNCTIONS *
429 ****************************************************************/
431 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
)
436 return TRUE
; /* For g_hash_table_foreach_remove() */
439 static void write_globicon(gpointer key
, gpointer value
, gpointer data
)
441 xmlNodePtr doc
= (xmlNodePtr
) data
;
444 tree
= xmlNewTextChild(doc
, NULL
, "rule", NULL
);
445 xmlSetProp(tree
, "match", key
);
446 xmlNewChild(tree
, NULL
, "icon", value
);
449 /* Write globicons file */
450 static void write_globicons(void)
452 gchar
*save
= NULL
, *save_new
= NULL
;
453 xmlDocPtr doc
= NULL
;
455 save
= choices_find_path_save("globicons", PROJECT
, TRUE
);
458 return; /* Saving is disabled */
460 save_new
= g_strconcat(save
, ".new", NULL
);
462 doc
= xmlNewDoc("1.0");
463 xmlDocSetRootElement(doc
,
464 xmlNewDocNode(doc
, NULL
, "special-files", NULL
));
466 g_hash_table_foreach(glob_icons
, write_globicon
,
467 xmlDocGetRootElement(doc
));
469 if (save_xml_file(doc
, save_new
) || rename(save_new
, save
))
470 delayed_error(_("Error saving %s: %s"),
471 save
, g_strerror(errno
));
480 /* Process a globicon line. Format:
481 glob-pattern icon-path
483 /home/<*>/Mail /usr/local/icons/mailbox.xpm
484 (<*> represents a single asterisk, enclosed in brackets to not break
487 static const char *process_globicons_line(gchar
*line
)
489 guchar
*pattern
, *iconpath
;
491 pattern
= strtok(line
, " \t");
492 /* We ignore empty lines, but they are no cause for a message */
496 iconpath
= strtok(NULL
, " \t");
498 /* If there is no icon, then we worry */
499 g_return_val_if_fail(iconpath
!= NULL
,
500 "Invalid line in globicons: no icon specified");
502 g_hash_table_insert(glob_icons
, g_strdup(pattern
), g_strdup(iconpath
));
507 /* Add a globicon entry to the list. If another one with the same
508 * path exists, it is replaced. Otherwise, the new entry is
509 * added to the top of the list (so that it takes precedence over
512 static void add_globicon(const gchar
*path
, const gchar
*icon
)
514 g_hash_table_insert(glob_icons
, g_strdup(path
), g_strdup(icon
));
516 /* Rewrite the globicons file */
519 /* Make sure any visible icons for the file are updated */
523 /* Remove the globicon for a certain path */
524 static void delete_globicon(const gchar
*path
)
528 if (!g_hash_table_lookup_extended(glob_icons
, path
, &key
, &value
))
531 g_hash_table_remove(glob_icons
, path
);
540 /* Set the icon for this dialog's file to 'icon' */
541 static void do_set_icon(GtkWidget
*dialog
, const gchar
*icon
)
544 GtkToggleButton
**radio
;
546 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
547 g_return_if_fail(path
!= NULL
);
549 radio
= g_object_get_data(G_OBJECT(dialog
), "radios");
550 g_return_if_fail(radio
!= NULL
);
552 if (gtk_toggle_button_get_active(radio
[0]))
554 if (!set_icon_path(path
, icon
))
557 else if (radio
[SET_COPY
] &&
558 gtk_toggle_button_get_active(radio
[SET_COPY
]))
560 if (!create_diricon(path
, icon
))
568 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
569 just_media
= gtk_toggle_button_get_active(radio
[2]);
571 if (!set_icon_for_type(type
, icon
, just_media
))
575 destroy_on_idle(dialog
);
578 /* Called when a URI list is dropped onto the box in the Set Icon
579 * dialog. Make that the default icon.
581 static void drag_icon_dropped(GtkWidget
*frame
,
582 GdkDragContext
*context
,
585 GtkSelectionData
*selection_data
,
593 if (!selection_data
->data
)
596 uris
= uri_list_to_glist(selection_data
->data
);
598 if (g_list_length(uris
) == 1)
599 icon
= g_strdup(get_local_path((guchar
*) uris
->data
));
601 for (next
= uris
; next
; next
= next
->next
)
607 delayed_error(_("You should drop a single local icon file "
608 "onto the drop box - that icon will be "
609 "used for this file from now on."));
613 do_set_icon(dialog
, icon
);
618 /* Called if the user clicks on the OK button on the Set Icon dialog */
619 static void get_path_set_icon(GtkWidget
*dialog
)
624 entry
= g_object_get_data(G_OBJECT(dialog
), "icon_path");
625 g_return_if_fail(entry
!= NULL
);
627 icon
= gtk_entry_get_text(entry
);
629 do_set_icon(dialog
, icon
);
632 static void show_icon_help(gpointer data
)
635 _("Enter the full path of a file that contains a valid "
636 "image to be used as the icon for this file or directory."));
639 /* Set the icon for the given MIME type. We copy the file. */
640 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
648 /* XXX: Should convert to XPM format... */
651 leaf
= g_strconcat(type
->media_type
, ".xpm", NULL
);
653 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
,
656 target
= choices_find_path_save(leaf
, "MIME-icons", TRUE
);
659 delayed_error(_("Setting icon disabled by CHOICESPATH"));
664 dir
= g_dirname(target
);
665 paths
= g_list_append(NULL
, (gchar
*) iconpath
);
667 action_copy(paths
, dir
, leaf
, -1);
677 static void get_dir(gpointer key
, gpointer value
, gpointer data
)
679 GHashTable
*names
= (GHashTable
*) data
;
682 dir
= g_dirname(value
); /* Freed in add_dir_to_menu */
685 g_hash_table_insert(names
, dir
, NULL
);
689 static void open_icon_dir(GtkMenuItem
*item
, gpointer data
)
694 dir
= gtk_label_get_text(GTK_LABEL(GTK_BIN(item
)->child
));
695 filer
= filer_opendir(dir
, NULL
);
697 display_set_thumbs(filer
, TRUE
);
700 static void add_dir_to_menu(gpointer key
, gpointer value
, gpointer data
)
702 GtkMenuShell
*menu
= (GtkMenuShell
*) data
;
705 item
= gtk_menu_item_new_with_label(key
);
706 gtk_widget_set_accel_path(item
, NULL
, NULL
); /* XXX */
707 g_signal_connect(item
, "activate",
708 G_CALLBACK(open_icon_dir
), NULL
);
710 gtk_menu_shell_append(menu
, item
);
713 static void show_current_dirs_menu(GtkWidget
*button
, gpointer data
)
718 names
= g_hash_table_new(g_str_hash
, g_str_equal
);
720 g_hash_table_foreach(glob_icons
, get_dir
, names
);
721 if (g_hash_table_size(glob_icons
) == 0)
723 /* TODO: Include MIME-icons? */
724 delayed_error(_("You have not yet set any special icons; "
725 "therefore, I have no directories to show you"));
729 menu
= gtk_menu_new();
731 g_hash_table_foreach(names
, add_dir_to_menu
, menu
);
733 g_hash_table_destroy(names
);
735 show_popup_menu(menu
, gtk_get_current_event(), 0);