4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, 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. */
36 #include "gui_support.h"
43 #include "usericons.h"
49 /* Store glob-to-icon mappings */
50 typedef struct _GlobIcon
{
55 static GHashTable
*glob_icons
= NULL
; /* Pathname -> Icon pathname */
57 /* Static prototypes */
58 static char *process_globicons_line(guchar
*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(guchar
*path
, guchar
*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(const MIME_type
*type
, 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 if (node
->type
!= XML_ELEMENT_NODE
)
117 if (strcmp(node
->name
, "rule") != 0)
119 icon
= get_subnode(node
, NULL
, "icon");
122 match
= xmlGetProp(node
, "match");
126 g_hash_table_insert(glob_icons
, match
,
127 xmlNodeGetContent(icon
));
134 /* Handle the old non-XML format */
135 parse_file(path
, process_globicons_line
);
136 if (g_hash_table_size(glob_icons
))
137 write_globicons(); /* Upgrade to new format */
140 last_read
= time(NULL
); /* Update time stamp */
145 /* Set an item's image field according to the globicons patterns if
146 * it matches one of them and the file exists.
148 void check_globicon(guchar
*path
, DirItem
*item
)
152 g_return_if_fail(item
&& !item
->image
);
154 gi
= g_hash_table_lookup(glob_icons
, path
);
156 item
->image
= g_fscache_lookup(pixmap_cache
, gi
);
159 /* Add a globicon mapping for the given file to the given icon path */
160 gboolean
set_icon_path(guchar
*filepath
, guchar
*iconpath
)
165 /* Check if file exists */
166 if (!mc_stat(iconpath
, &icon
) == 0) {
167 delayed_rox_error(_("The pathname you gave does not exist. "
168 "The icon has not been changed."));
172 /* Check if we can load the image, warn the user if not. */
173 pic
= g_fscache_lookup(pixmap_cache
, iconpath
);
177 _("Unable to load image file -- maybe it's not in a "
178 "format I understand, or maybe the permissions are "
180 "The icon has not been changed."));
183 g_fscache_data_unref(pixmap_cache
, pic
);
185 /* Add the globicon mapping and update visible icons */
186 add_globicon(filepath
, iconpath
);
191 /* Display a dialog box allowing the user to set the icon for
192 * a file or directory.
194 void icon_set_handler_dialog(DirItem
*item
, guchar
*path
)
197 GtkWidget
*dialog
, *vbox
, *frame
, *hbox
, *vbox2
;
198 GtkWidget
*entry
, *label
, *button
, *align
, *icon
;
200 GtkTargetEntry targets
[] = {
201 {"text/uri-list", 0, TARGET_URI_LIST
},
205 g_return_if_fail(item
!= NULL
&& path
!= NULL
);
207 gi
= g_hash_table_lookup(glob_icons
, path
);
209 dialog
= gtk_window_new(GTK_WINDOW_DIALOG
);
210 gtk_object_set_data_full(GTK_OBJECT(dialog
),
215 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set icon"));
216 gtk_container_set_border_width(GTK_CONTAINER(dialog
), 10);
218 vbox
= gtk_vbox_new(FALSE
, 4);
219 gtk_container_add(GTK_CONTAINER(dialog
), vbox
);
221 radio
= g_new(GtkWidget
*, 3);
223 tmp
= g_strdup_printf(_("Set icon for all `%s/<anything>'"),
224 item
->mime_type
->media_type
);
225 radio
[2] = gtk_radio_button_new_with_label(NULL
, tmp
);
226 gtk_box_pack_start(GTK_BOX(vbox
), radio
[2], FALSE
, TRUE
, 0);
229 tmp
= g_strdup_printf(_("Only for the type `%s/%s'"),
230 item
->mime_type
->media_type
,
231 item
->mime_type
->subtype
);
232 radio
[1] = gtk_radio_button_new_with_label_from_widget(
233 GTK_RADIO_BUTTON(radio
[2]), tmp
);
234 gtk_box_pack_start(GTK_BOX(vbox
), radio
[1], FALSE
, TRUE
, 0);
237 tmp
= g_strdup_printf(_("Only for the file `%s'"), path
);
238 radio
[0] = gtk_radio_button_new_with_label_from_widget(
239 GTK_RADIO_BUTTON(radio
[2]), tmp
);
240 gtk_box_pack_start(GTK_BOX(vbox
), radio
[0], FALSE
, TRUE
, 0);
243 gtk_object_set_data_full(GTK_OBJECT(dialog
),
244 "radios", radio
, g_free
);
245 gtk_object_set_data(GTK_OBJECT(dialog
),
246 "mime_type", item
->mime_type
);
248 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio
[0]), TRUE
);
250 frame
= gtk_frame_new(NULL
);
251 gtk_box_pack_start(GTK_BOX(vbox
), frame
, TRUE
, TRUE
, 4);
252 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_IN
);
253 gtk_container_set_border_width(GTK_CONTAINER(frame
), 4);
255 gtk_drag_dest_set(frame
, GTK_DEST_DEFAULT_ALL
,
256 targets
, sizeof(targets
) / sizeof(*targets
),
258 gtk_signal_connect(GTK_OBJECT(frame
), "drag_data_received",
259 GTK_SIGNAL_FUNC(drag_icon_dropped
), dialog
);
261 vbox2
= gtk_vbox_new(FALSE
, 0);
262 gtk_container_add(GTK_CONTAINER(frame
), vbox2
);
264 label
= gtk_label_new(_("Drop an icon file here"));
265 gtk_misc_set_padding(GTK_MISC(label
), 10, 10);
266 gtk_box_pack_start(GTK_BOX(vbox2
), label
, TRUE
, TRUE
, 0);
267 align
= gtk_alignment_new(1, 1, 0, 0);
268 gtk_box_pack_start(GTK_BOX(vbox2
), align
, FALSE
, TRUE
, 0);
269 button
= gtk_button_new();
270 gtk_container_add(GTK_CONTAINER(align
), button
);
271 icon
= gtk_pixmap_new(im_dirs
->pixmap
, im_dirs
->mask
);
272 gtk_container_add(GTK_CONTAINER(button
), icon
);
273 gtk_tooltips_set_tip(tooltips
, button
,
274 _("Menu of directories previously used for icons"),
276 gtk_signal_connect(GTK_OBJECT(button
), "clicked",
277 GTK_SIGNAL_FUNC(show_current_dirs_menu
), NULL
);
279 hbox
= gtk_hbox_new(FALSE
, 4);
280 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 4);
281 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
282 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
284 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
286 hbox
= gtk_hbox_new(FALSE
, 4);
287 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 0);
289 label
= gtk_label_new(_("Enter the path of an icon file:")),
290 gtk_misc_set_alignment(GTK_MISC(label
), 0, .5);
291 gtk_box_pack_start(GTK_BOX(hbox
), label
, TRUE
, TRUE
, 4);
293 gtk_box_pack_start(GTK_BOX(hbox
),
294 new_help_button(show_icon_help
, NULL
), FALSE
, TRUE
, 0);
296 entry
= gtk_entry_new();
297 /* Set the current icon as the default text if there is one */
299 gtk_entry_set_text(GTK_ENTRY(entry
), gi
);
301 gtk_box_pack_start(GTK_BOX(vbox
), entry
, FALSE
, TRUE
, 0);
302 gtk_widget_grab_focus(entry
);
303 gtk_object_set_data(GTK_OBJECT(dialog
), "icon_path", entry
);
304 gtk_signal_connect_object(GTK_OBJECT(entry
), "activate",
305 GTK_SIGNAL_FUNC(get_path_set_icon
),
308 hbox
= gtk_hbox_new(FALSE
, 4);
309 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 4);
310 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
311 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
313 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
315 hbox
= gtk_hbox_new(TRUE
, 4);
316 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, TRUE
, 0);
318 button
= gtk_button_new_with_label(_("Remove custom icon"));
319 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
320 gtk_signal_connect_object(GTK_OBJECT(button
), "clicked",
321 GTK_SIGNAL_FUNC(remove_icon
),
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 gtk_signal_connect_object(GTK_OBJECT(button
), "clicked",
332 GTK_SIGNAL_FUNC(get_path_set_icon
),
335 button
= gtk_button_new_with_label(_("Cancel"));
336 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
337 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
338 gtk_signal_connect_object(GTK_OBJECT(button
), "clicked",
339 GTK_SIGNAL_FUNC(gtk_widget_destroy
),
342 gtk_widget_show_all(dialog
);
346 /****************************************************************
347 * INTERNAL FUNCTIONS *
348 ****************************************************************/
350 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
)
355 return TRUE
; /* For g_hash_table_foreach_remove() */
358 static void write_globicon(gpointer key
, gpointer value
, gpointer data
)
360 xmlNodePtr doc
= (xmlNodePtr
) data
;
363 tree
= xmlNewTextChild(doc
, NULL
, "rule", NULL
);
364 xmlSetProp(tree
, "match", key
);
365 xmlNewChild(tree
, NULL
, "icon", value
);
368 /* Write globicons file */
369 static void write_globicons(void)
371 gchar
*save
= NULL
, *save_new
= NULL
;
372 xmlDocPtr doc
= NULL
;
374 save
= choices_find_path_save("globicons", PROJECT
, TRUE
);
377 return; /* Saving is disabled */
379 save_new
= g_strconcat(save
, ".new", NULL
);
381 doc
= xmlNewDoc("1.0");
382 xmlDocSetRootElement(doc
,
383 xmlNewDocNode(doc
, NULL
, "special-files", NULL
));
385 g_hash_table_foreach(glob_icons
, write_globicon
,
386 xmlDocGetRootElement(doc
));
388 if (save_xml_file(doc
, save_new
) || rename(save_new
, save
))
389 delayed_rox_error(_("Error saving %s: %s"),
390 save
, g_strerror(errno
));
399 /* Process a globicon line. Format:
400 glob-pattern icon-path
402 /home/<*>/Mail /usr/local/icons/mailbox.xpm
403 (<*> represents a single asterisk, enclosed in brackets to not break
406 static char *process_globicons_line(guchar
*line
)
408 guchar
*pattern
, *iconpath
;
410 pattern
= strtok(line
, " \t");
411 /* We ignore empty lines, but they are no cause for a message */
415 iconpath
= strtok(NULL
, " \t");
417 /* If there is no icon, then we worry */
418 g_return_val_if_fail(iconpath
!= NULL
,
419 "Invalid line in globicons: no icon specified");
421 g_hash_table_insert(glob_icons
, g_strdup(pattern
), g_strdup(iconpath
));
426 /* Add a globicon entry to the list. If another one with the same
427 * path exists, it is replaced. Otherwise, the new entry is
428 * added to the top of the list (so that it takes precedence over
431 static void add_globicon(guchar
*path
, guchar
*icon
)
433 g_hash_table_insert(glob_icons
, g_strdup(path
), g_strdup(icon
));
435 /* Rewrite the globicons file */
438 /* Make sure any visible icons for the file are updated */
442 /* Remove the globicon for a certain path */
443 static void delete_globicon(guchar
*path
)
447 if (!g_hash_table_lookup_extended(glob_icons
, path
, &key
, &value
))
450 g_hash_table_remove(glob_icons
, path
);
459 /* Set the icon for this dialog's file to 'icon' */
460 static void do_set_icon(GtkWidget
*dialog
, gchar
*icon
)
463 GtkToggleButton
**radio
;
465 path
= gtk_object_get_data(GTK_OBJECT(dialog
), "pathname");
466 g_return_if_fail(path
!= NULL
);
468 radio
= gtk_object_get_data(GTK_OBJECT(dialog
), "radios");
469 g_return_if_fail(radio
!= NULL
);
471 if (gtk_toggle_button_get_active(radio
[0]))
473 if (!set_icon_path(path
, icon
))
481 type
= gtk_object_get_data(GTK_OBJECT(dialog
), "mime_type");
482 just_media
= gtk_toggle_button_get_active(radio
[2]);
484 if (!set_icon_for_type(type
, icon
, just_media
))
488 destroy_on_idle(dialog
);
491 /* Called when a URI list is dropped onto the box in the Set Icon
492 * dialog. Make that the default icon.
494 static void drag_icon_dropped(GtkWidget
*frame
,
495 GdkDragContext
*context
,
498 GtkSelectionData
*selection_data
,
506 if (!selection_data
->data
)
509 uris
= uri_list_to_glist(selection_data
->data
);
511 if (g_list_length(uris
) == 1)
512 icon
= g_strdup(get_local_path((guchar
*) uris
->data
));
514 for (next
= uris
; next
; next
= next
->next
)
520 delayed_rox_error(_("You should drop a single local icon file "
521 "onto the drop box - that icon will be "
522 "used for this file from now on."));
526 do_set_icon(dialog
, icon
);
531 /* Called if the user clicks on the OK button on the Set Icon dialog */
532 static void get_path_set_icon(GtkWidget
*dialog
)
537 entry
= gtk_object_get_data(GTK_OBJECT(dialog
), "icon_path");
538 g_return_if_fail(entry
!= NULL
);
540 icon
= gtk_entry_get_text(entry
);
542 do_set_icon(dialog
, icon
);
545 /* Called if the user clicks on the "Remove custom icon" button */
546 static void remove_icon(GtkWidget
*dialog
)
550 g_return_if_fail(dialog
!= NULL
);
552 path
= gtk_object_get_data(GTK_OBJECT(dialog
), "pathname");
553 g_return_if_fail(path
!= NULL
);
555 delete_globicon(path
);
557 destroy_on_idle(dialog
);
560 static void show_icon_help(gpointer data
)
563 _("Enter the full path of a file that contains a valid "
564 "image to be used as the icon for this file or directory."));
567 /* Set the icon for the given MIME type. We copy the file. */
568 static gboolean
set_icon_for_type(const MIME_type
*type
, gchar
*iconpath
,
576 /* XXX: Should convert to XPM format... */
579 leaf
= g_strconcat(type
->media_type
, ".xpm", NULL
);
581 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
,
584 target
= choices_find_path_save(leaf
, "MIME-icons", TRUE
);
587 delayed_rox_error(_("Setting icon disabled by CHOICESPATH"));
592 dir
= g_dirname(target
);
593 paths
= g_list_append(NULL
, iconpath
);
595 action_copy(paths
, dir
, leaf
);
605 static void get_dir(gpointer key
, gpointer value
, gpointer data
)
607 GHashTable
*names
= (GHashTable
*) data
;
610 dir
= g_dirname(value
); /* Freed in add_dir_to_menu */
613 g_hash_table_insert(names
, dir
, NULL
);
617 static void open_icon_dir(GtkMenuItem
*item
, gpointer data
)
622 gtk_label_get(GTK_LABEL(GTK_BIN(item
)->child
), &dir
);
623 filer
= filer_opendir(dir
, NULL
);
624 dir_rescan_with_thumbs(filer
->directory
, filer
->path
);
627 static void add_dir_to_menu(gpointer key
, gpointer value
, gpointer data
)
629 GtkMenuShell
*menu
= (GtkMenuShell
*) data
;
632 item
= gtk_menu_item_new_with_label(key
);
634 _gtk_widget_set_accel_path(item
, NULL
, NULL
);
636 gtk_widget_lock_accelerators(item
);
638 gtk_signal_connect(GTK_OBJECT(item
), "activate",
639 GTK_SIGNAL_FUNC(open_icon_dir
), NULL
);
641 gtk_menu_shell_append(menu
, item
);
644 static void show_current_dirs_menu(GtkWidget
*button
, gpointer data
)
649 names
= g_hash_table_new(g_str_hash
, g_str_equal
);
651 g_hash_table_foreach(glob_icons
, get_dir
, names
);
652 if (g_hash_table_size(glob_icons
) == 0)
654 /* TODO: Include MIME-icons? */
655 delayed_rox_error(_("You have not yet set any special icons; "
656 "therefore, I have no directories to show you"));
660 menu
= gtk_menu_new();
662 g_hash_table_foreach(names
, add_dir_to_menu
, menu
);
664 g_hash_table_destroy(names
);
666 show_popup_menu(menu
, gtk_get_current_event(), 0);