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"
55 static GHashTable
*glob_icons
= NULL
; /* Pathname -> Icon pathname */
57 /* Static prototypes */
58 static const char *process_globicons_line(gchar
*line
);
59 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
);
60 static void get_path_set_icon(GtkWidget
*dialog
);
61 static void show_icon_help(gpointer data
);
62 static void write_globicons(void);
63 static void show_current_dirs_menu(GtkWidget
*button
, gpointer data
);
64 static void add_globicon(const gchar
*path
, const gchar
*icon
);
65 static void drag_icon_dropped(GtkWidget
*frame
,
66 GdkDragContext
*context
,
69 GtkSelectionData
*selection_data
,
73 static void remove_icon(GtkWidget
*dialog
);
74 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
77 /****************************************************************
78 * EXTERNAL INTERFACE *
79 ****************************************************************/
81 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
84 static time_t last_read
= (time_t) 0;
90 glob_icons
= g_hash_table_new(g_str_hash
, g_str_equal
);
92 path
= choices_find_path_load("globicons", PROJECT
);
94 return; /* Nothing to load */
96 if (mc_stat(path
, &info
) == -1)
99 if (info
.st_mtime
<= last_read
)
100 goto out
; /* File hasn't been modified since we last read it */
102 g_hash_table_foreach_remove(glob_icons
, free_globicon
, NULL
);
104 doc
= xmlParseFile(path
);
107 xmlNodePtr node
, icon
, root
;
110 root
= xmlDocGetRootElement(doc
);
112 /* Handle the new XML file format */
113 for (node
= root
->xmlChildrenNode
; node
; node
= node
->next
)
115 gchar
*path
, *icon_path
;
117 if (node
->type
!= XML_ELEMENT_NODE
)
119 if (strcmp(node
->name
, "rule") != 0)
121 icon
= get_subnode(node
, NULL
, "icon");
124 match
= xmlGetProp(node
, "match");
128 icon_path
= xmlNodeGetContent(icon
);
129 path
= icon_convert_path(match
);
130 g_hash_table_insert(glob_icons
, path
, icon_path
);
138 /* Handle the old non-XML format */
139 parse_file(path
, process_globicons_line
);
140 if (g_hash_table_size(glob_icons
))
141 write_globicons(); /* Upgrade to new format */
144 last_read
= time(NULL
); /* Update time stamp */
149 /* Set an item's image field according to the globicons patterns if
150 * it matches one of them and the file exists.
152 void check_globicon(const guchar
*path
, DirItem
*item
)
156 g_return_if_fail(item
&& !item
->image
);
158 gi
= g_hash_table_lookup(glob_icons
, path
);
160 item
->image
= g_fscache_lookup(pixmap_cache
, gi
);
163 /* Add a globicon mapping for the given file to the given icon path */
164 gboolean
set_icon_path(const guchar
*filepath
, const guchar
*iconpath
)
169 /* Check if file exists */
170 if (!mc_stat(iconpath
, &icon
) == 0) {
171 delayed_error(_("The pathname you gave does not exist. "
172 "The icon has not been changed."));
176 /* Check if we can load the image, warn the user if not. */
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."));
187 g_fscache_data_unref(pixmap_cache
, pic
);
189 /* Add the globicon mapping and update visible icons */
190 add_globicon(filepath
, iconpath
);
195 /* Display a dialog box allowing the user to set the icon for
196 * a file or directory.
198 void icon_set_handler_dialog(DirItem
*item
, const guchar
*path
)
201 GtkWidget
*dialog
, *vbox
, *frame
, *hbox
, *vbox2
;
202 GtkWidget
*entry
, *label
, *button
, *align
, *icon
;
204 GtkTargetEntry targets
[] = {
205 {"text/uri-list", 0, TARGET_URI_LIST
},
209 g_return_if_fail(item
!= NULL
&& path
!= NULL
);
211 gi
= g_hash_table_lookup(glob_icons
, path
);
213 dialog
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
214 gtk_window_set_type_hint(GTK_WINDOW(dialog
),
215 GDK_WINDOW_TYPE_HINT_DIALOG
);
216 g_object_set_data_full(G_OBJECT(dialog
), "pathname",
217 strdup(path
), g_free
);
219 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set icon"));
220 gtk_container_set_border_width(GTK_CONTAINER(dialog
), 10);
222 vbox
= gtk_vbox_new(FALSE
, 4);
223 gtk_container_add(GTK_CONTAINER(dialog
), vbox
);
225 radio
= g_new(GtkWidget
*, 3);
227 tmp
= g_strdup_printf(_("Set icon for all `%s/<anything>'"),
228 item
->mime_type
->media_type
);
229 radio
[2] = gtk_radio_button_new_with_label(NULL
, tmp
);
230 gtk_box_pack_start(GTK_BOX(vbox
), radio
[2], FALSE
, TRUE
, 0);
233 tmp
= g_strdup_printf(_("Only for the type `%s/%s'"),
234 item
->mime_type
->media_type
,
235 item
->mime_type
->subtype
);
236 radio
[1] = gtk_radio_button_new_with_label_from_widget(
237 GTK_RADIO_BUTTON(radio
[2]), tmp
);
238 gtk_box_pack_start(GTK_BOX(vbox
), radio
[1], FALSE
, TRUE
, 0);
241 tmp
= g_strdup_printf(_("Only for the file `%s'"), path
);
242 radio
[0] = gtk_radio_button_new_with_label_from_widget(
243 GTK_RADIO_BUTTON(radio
[2]), tmp
);
244 gtk_box_pack_start(GTK_BOX(vbox
), radio
[0], FALSE
, TRUE
, 0);
247 g_object_set_data_full(G_OBJECT(dialog
), "radios", radio
, g_free
);
248 g_object_set_data(G_OBJECT(dialog
), "mime_type", item
->mime_type
);
250 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio
[0]), TRUE
);
252 frame
= gtk_frame_new(NULL
);
253 gtk_box_pack_start(GTK_BOX(vbox
), frame
, TRUE
, TRUE
, 4);
254 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_IN
);
255 gtk_container_set_border_width(GTK_CONTAINER(frame
), 4);
257 gtk_drag_dest_set(frame
, GTK_DEST_DEFAULT_ALL
,
258 targets
, sizeof(targets
) / sizeof(*targets
),
260 g_signal_connect(frame
, "drag_data_received",
261 G_CALLBACK(drag_icon_dropped
), dialog
);
263 vbox2
= gtk_vbox_new(FALSE
, 0);
264 gtk_container_add(GTK_CONTAINER(frame
), vbox2
);
266 label
= gtk_label_new(_("Drop an icon file here"));
267 gtk_misc_set_padding(GTK_MISC(label
), 10, 10);
268 gtk_box_pack_start(GTK_BOX(vbox2
), label
, TRUE
, TRUE
, 0);
269 align
= gtk_alignment_new(1, 1, 0, 0);
270 gtk_box_pack_start(GTK_BOX(vbox2
), align
, FALSE
, TRUE
, 0);
271 button
= gtk_button_new();
272 gtk_container_add(GTK_CONTAINER(align
), button
);
273 icon
= gtk_image_new_from_pixmap(im_dirs
->pixmap
, im_dirs
->mask
);
274 gtk_container_add(GTK_CONTAINER(button
), icon
);
275 gtk_tooltips_set_tip(tooltips
, button
,
276 _("Menu of directories previously used for icons"),
278 g_signal_connect(button
, "clicked",
279 G_CALLBACK(show_current_dirs_menu
), NULL
);
281 hbox
= gtk_hbox_new(FALSE
, 4);
282 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 4);
283 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
284 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
286 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
288 hbox
= gtk_hbox_new(FALSE
, 4);
289 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 0);
291 label
= gtk_label_new(_("Enter the path of an icon file:")),
292 gtk_misc_set_alignment(GTK_MISC(label
), 0, .5);
293 gtk_box_pack_start(GTK_BOX(hbox
), label
, TRUE
, TRUE
, 4);
295 gtk_box_pack_start(GTK_BOX(hbox
),
296 new_help_button(show_icon_help
, NULL
), FALSE
, TRUE
, 0);
298 entry
= gtk_entry_new();
299 /* Set the current icon as the default text if there is one */
301 gtk_entry_set_text(GTK_ENTRY(entry
), gi
);
303 gtk_box_pack_start(GTK_BOX(vbox
), entry
, FALSE
, TRUE
, 0);
304 gtk_widget_grab_focus(entry
);
305 g_object_set_data(G_OBJECT(dialog
), "icon_path", entry
);
306 g_signal_connect_swapped(entry
, "activate",
307 G_CALLBACK(get_path_set_icon
), dialog
);
309 hbox
= gtk_hbox_new(FALSE
, 4);
310 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 4);
311 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
312 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
314 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
316 hbox
= gtk_hbox_new(TRUE
, 4);
317 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 0);
319 button
= gtk_button_new_with_label(_("Remove custom icon"));
320 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
321 g_signal_connect_swapped(button
, "clicked",
322 G_CALLBACK(remove_icon
), dialog
);
324 hbox
= gtk_hbox_new(TRUE
, 4);
325 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 0);
327 button
= gtk_button_new_with_label(_("OK"));
328 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
329 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
330 gtk_window_set_default(GTK_WINDOW(dialog
), button
);
331 g_signal_connect_swapped(button
, "clicked",
332 G_CALLBACK(get_path_set_icon
), dialog
);
334 button
= gtk_button_new_with_label(_("Cancel"));
335 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
336 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
337 g_signal_connect_swapped(button
, "clicked",
338 G_CALLBACK(gtk_widget_destroy
), dialog
);
340 gtk_widget_show_all(dialog
);
344 /****************************************************************
345 * INTERNAL FUNCTIONS *
346 ****************************************************************/
348 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
)
353 return TRUE
; /* For g_hash_table_foreach_remove() */
356 static void write_globicon(gpointer key
, gpointer value
, gpointer data
)
358 xmlNodePtr doc
= (xmlNodePtr
) data
;
361 tree
= xmlNewTextChild(doc
, NULL
, "rule", NULL
);
362 xmlSetProp(tree
, "match", key
);
363 xmlNewChild(tree
, NULL
, "icon", value
);
366 /* Write globicons file */
367 static void write_globicons(void)
369 gchar
*save
= NULL
, *save_new
= NULL
;
370 xmlDocPtr doc
= NULL
;
372 save
= choices_find_path_save("globicons", PROJECT
, TRUE
);
375 return; /* Saving is disabled */
377 save_new
= g_strconcat(save
, ".new", NULL
);
379 doc
= xmlNewDoc("1.0");
380 xmlDocSetRootElement(doc
,
381 xmlNewDocNode(doc
, NULL
, "special-files", NULL
));
383 g_hash_table_foreach(glob_icons
, write_globicon
,
384 xmlDocGetRootElement(doc
));
386 if (save_xml_file(doc
, save_new
) || rename(save_new
, save
))
387 delayed_error(_("Error saving %s: %s"),
388 save
, g_strerror(errno
));
397 /* Process a globicon line. Format:
398 glob-pattern icon-path
400 /home/<*>/Mail /usr/local/icons/mailbox.xpm
401 (<*> represents a single asterisk, enclosed in brackets to not break
404 static const char *process_globicons_line(gchar
*line
)
406 guchar
*pattern
, *iconpath
;
408 pattern
= strtok(line
, " \t");
409 /* We ignore empty lines, but they are no cause for a message */
413 iconpath
= strtok(NULL
, " \t");
415 /* If there is no icon, then we worry */
416 g_return_val_if_fail(iconpath
!= NULL
,
417 "Invalid line in globicons: no icon specified");
419 g_hash_table_insert(glob_icons
, g_strdup(pattern
), g_strdup(iconpath
));
424 /* Add a globicon entry to the list. If another one with the same
425 * path exists, it is replaced. Otherwise, the new entry is
426 * added to the top of the list (so that it takes precedence over
429 static void add_globicon(const gchar
*path
, const gchar
*icon
)
431 g_hash_table_insert(glob_icons
, g_strdup(path
), g_strdup(icon
));
433 /* Rewrite the globicons file */
436 /* Make sure any visible icons for the file are updated */
440 /* Remove the globicon for a certain path */
441 static void delete_globicon(guchar
*path
)
445 if (!g_hash_table_lookup_extended(glob_icons
, path
, &key
, &value
))
448 g_hash_table_remove(glob_icons
, path
);
457 /* Set the icon for this dialog's file to 'icon' */
458 static void do_set_icon(GtkWidget
*dialog
, const gchar
*icon
)
461 GtkToggleButton
**radio
;
463 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
464 g_return_if_fail(path
!= NULL
);
466 radio
= g_object_get_data(G_OBJECT(dialog
), "radios");
467 g_return_if_fail(radio
!= NULL
);
469 if (gtk_toggle_button_get_active(radio
[0]))
471 if (!set_icon_path(path
, icon
))
479 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
480 just_media
= gtk_toggle_button_get_active(radio
[2]);
482 if (!set_icon_for_type(type
, icon
, just_media
))
486 destroy_on_idle(dialog
);
489 /* Called when a URI list is dropped onto the box in the Set Icon
490 * dialog. Make that the default icon.
492 static void drag_icon_dropped(GtkWidget
*frame
,
493 GdkDragContext
*context
,
496 GtkSelectionData
*selection_data
,
504 if (!selection_data
->data
)
507 uris
= uri_list_to_glist(selection_data
->data
);
509 if (g_list_length(uris
) == 1)
510 icon
= g_strdup(get_local_path((guchar
*) uris
->data
));
512 for (next
= uris
; next
; next
= next
->next
)
518 delayed_error(_("You should drop a single local icon file "
519 "onto the drop box - that icon will be "
520 "used for this file from now on."));
524 do_set_icon(dialog
, icon
);
529 /* Called if the user clicks on the OK button on the Set Icon dialog */
530 static void get_path_set_icon(GtkWidget
*dialog
)
535 entry
= g_object_get_data(G_OBJECT(dialog
), "icon_path");
536 g_return_if_fail(entry
!= NULL
);
538 icon
= gtk_entry_get_text(entry
);
540 do_set_icon(dialog
, icon
);
543 /* Called if the user clicks on the "Remove custom icon" button */
544 static void remove_icon(GtkWidget
*dialog
)
548 g_return_if_fail(dialog
!= NULL
);
550 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
551 g_return_if_fail(path
!= NULL
);
553 delete_globicon(path
);
555 destroy_on_idle(dialog
);
558 static void show_icon_help(gpointer data
)
561 _("Enter the full path of a file that contains a valid "
562 "image to be used as the icon for this file or directory."));
565 /* Set the icon for the given MIME type. We copy the file. */
566 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
574 /* XXX: Should convert to XPM format... */
577 leaf
= g_strconcat(type
->media_type
, ".xpm", NULL
);
579 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
,
582 target
= choices_find_path_save(leaf
, "MIME-icons", TRUE
);
585 delayed_error(_("Setting icon disabled by CHOICESPATH"));
590 dir
= g_dirname(target
);
591 paths
= g_list_append(NULL
, (gchar
*) iconpath
);
593 action_copy(paths
, dir
, leaf
, -1);
603 static void get_dir(gpointer key
, gpointer value
, gpointer data
)
605 GHashTable
*names
= (GHashTable
*) data
;
608 dir
= g_dirname(value
); /* Freed in add_dir_to_menu */
611 g_hash_table_insert(names
, dir
, NULL
);
615 static void open_icon_dir(GtkMenuItem
*item
, gpointer data
)
620 dir
= gtk_label_get_text(GTK_LABEL(GTK_BIN(item
)->child
));
621 filer
= filer_opendir(dir
, NULL
);
623 display_set_thumbs(filer
, TRUE
);
626 static void add_dir_to_menu(gpointer key
, gpointer value
, gpointer data
)
628 GtkMenuShell
*menu
= (GtkMenuShell
*) data
;
631 item
= gtk_menu_item_new_with_label(key
);
632 gtk_widget_set_accel_path(item
, NULL
, NULL
); /* XXX */
633 g_signal_connect(item
, "activate",
634 G_CALLBACK(open_icon_dir
), NULL
);
636 gtk_menu_shell_append(menu
, item
);
639 static void show_current_dirs_menu(GtkWidget
*button
, gpointer data
)
644 names
= g_hash_table_new(g_str_hash
, g_str_equal
);
646 g_hash_table_foreach(glob_icons
, get_dir
, names
);
647 if (g_hash_table_size(glob_icons
) == 0)
649 /* TODO: Include MIME-icons? */
650 delayed_error(_("You have not yet set any special icons; "
651 "therefore, I have no directories to show you"));
655 menu
= gtk_menu_new();
657 g_hash_table_foreach(names
, add_dir_to_menu
, menu
);
659 g_hash_table_destroy(names
);
661 show_popup_menu(menu
, gtk_get_current_event(), 0);