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 static GHashTable
*glob_icons
= NULL
; /* Pathname -> Icon pathname */
59 /* Static prototypes */
60 static const char *process_globicons_line(gchar
*line
);
61 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
);
62 static void get_path_set_icon(GtkWidget
*dialog
);
63 static void show_icon_help(gpointer data
);
64 static void write_globicons(void);
65 static void show_current_dirs_menu(GtkWidget
*button
, gpointer data
);
66 static void add_globicon(const gchar
*path
, const gchar
*icon
);
67 static void drag_icon_dropped(GtkWidget
*frame
,
68 GdkDragContext
*context
,
71 GtkSelectionData
*selection_data
,
75 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
77 static void delete_globicon(const gchar
*path
);
79 /****************************************************************
80 * EXTERNAL INTERFACE *
81 ****************************************************************/
83 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
86 static time_t last_read
= (time_t) 0;
92 glob_icons
= g_hash_table_new(g_str_hash
, g_str_equal
);
94 path
= choices_find_path_load("globicons", PROJECT
);
96 return; /* Nothing to load */
98 if (mc_stat(path
, &info
) == -1)
101 if (info
.st_mtime
<= last_read
)
102 goto out
; /* File hasn't been modified since we last read it */
104 g_hash_table_foreach_remove(glob_icons
, free_globicon
, NULL
);
106 doc
= xmlParseFile(path
);
109 xmlNodePtr node
, icon
, root
;
112 root
= xmlDocGetRootElement(doc
);
114 /* Handle the new XML file format */
115 for (node
= root
->xmlChildrenNode
; node
; node
= node
->next
)
117 gchar
*path
, *icon_path
;
119 if (node
->type
!= XML_ELEMENT_NODE
)
121 if (strcmp(node
->name
, "rule") != 0)
123 icon
= get_subnode(node
, NULL
, "icon");
126 match
= xmlGetProp(node
, "match");
130 icon_path
= xmlNodeGetContent(icon
);
131 path
= icon_convert_path(match
);
132 g_hash_table_insert(glob_icons
, path
, icon_path
);
140 /* Handle the old non-XML format */
141 parse_file(path
, process_globicons_line
);
142 if (g_hash_table_size(glob_icons
))
143 write_globicons(); /* Upgrade to new format */
146 last_read
= time(NULL
); /* Update time stamp */
151 /* Set an item's image field according to the globicons patterns if
152 * it matches one of them and the file exists.
154 void check_globicon(const guchar
*path
, DirItem
*item
)
158 g_return_if_fail(item
&& !item
->image
);
160 gi
= g_hash_table_lookup(glob_icons
, path
);
162 item
->image
= g_fscache_lookup(pixmap_cache
, gi
);
165 /* Add a globicon mapping for the given file to the given icon path */
166 gboolean
set_icon_path(const guchar
*filepath
, const guchar
*iconpath
)
171 /* Check if file exists */
172 if (!mc_stat(iconpath
, &icon
) == 0) {
173 delayed_error(_("The pathname you gave does not exist. "
174 "The icon has not been changed."));
178 /* Check if we can load the image, warn the user if not. */
179 pic
= g_fscache_lookup(pixmap_cache
, iconpath
);
183 _("Unable to load image file -- maybe it's not in a "
184 "format I understand, or maybe the permissions are "
186 "The icon has not been changed."));
191 /* Add the globicon mapping and update visible icons */
192 add_globicon(filepath
, iconpath
);
197 static void dialog_response(GtkWidget
*dialog
, gint response
, gpointer data
)
199 if (response
== GTK_RESPONSE_OK
)
200 get_path_set_icon(dialog
);
201 else if (response
== GTK_RESPONSE_CANCEL
)
202 gtk_widget_destroy(dialog
);
203 else if (response
== DELETE_ICON
)
207 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
208 g_return_if_fail(path
!= NULL
);
210 delete_globicon(path
);
212 gtk_widget_destroy(dialog
);
216 /* Display a dialog box allowing the user to set the icon for
217 * a file or directory.
219 void icon_set_handler_dialog(DirItem
*item
, const guchar
*path
)
223 GtkWidget
*frame
, *hbox
, *vbox2
;
224 GtkWidget
*entry
, *label
, *button
, *align
, *icon
;
226 GtkTargetEntry targets
[] = {
227 {"text/uri-list", 0, TARGET_URI_LIST
},
231 g_return_if_fail(item
!= NULL
&& path
!= NULL
);
233 gi
= g_hash_table_lookup(glob_icons
, path
);
235 dialog
= GTK_DIALOG(gtk_dialog_new());
236 gtk_dialog_set_has_separator(dialog
, FALSE
);
237 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
238 g_object_set_data_full(G_OBJECT(dialog
), "pathname",
239 strdup(path
), g_free
);
241 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set icon"));
243 radio
= g_new(GtkWidget
*, 3);
245 tmp
= g_strdup_printf(_("Set icon for all `%s/<anything>'"),
246 item
->mime_type
->media_type
);
247 radio
[2] = gtk_radio_button_new_with_label(NULL
, tmp
);
248 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), radio
[2], FALSE
, TRUE
, 0);
251 tmp
= g_strdup_printf(_("Only for the type `%s/%s'"),
252 item
->mime_type
->media_type
,
253 item
->mime_type
->subtype
);
254 radio
[1] = gtk_radio_button_new_with_label_from_widget(
255 GTK_RADIO_BUTTON(radio
[2]), tmp
);
256 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), radio
[1], FALSE
, TRUE
, 0);
259 tmp
= g_strdup_printf(_("Only for the file `%s'"), path
);
260 radio
[0] = gtk_radio_button_new_with_label_from_widget(
261 GTK_RADIO_BUTTON(radio
[2]), tmp
);
262 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), radio
[0], FALSE
, TRUE
, 0);
265 g_object_set_data_full(G_OBJECT(dialog
), "radios", radio
, g_free
);
266 g_object_set_data(G_OBJECT(dialog
), "mime_type", item
->mime_type
);
268 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio
[0]), TRUE
);
270 frame
= gtk_frame_new(NULL
);
271 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), frame
, TRUE
, TRUE
, 4);
272 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_IN
);
273 gtk_container_set_border_width(GTK_CONTAINER(frame
), 4);
275 gtk_drag_dest_set(frame
, GTK_DEST_DEFAULT_ALL
,
276 targets
, sizeof(targets
) / sizeof(*targets
),
278 g_signal_connect(frame
, "drag_data_received",
279 G_CALLBACK(drag_icon_dropped
), dialog
);
281 vbox2
= gtk_vbox_new(FALSE
, 0);
282 gtk_container_add(GTK_CONTAINER(frame
), vbox2
);
284 label
= gtk_label_new(_("Drop an icon file here"));
285 gtk_misc_set_padding(GTK_MISC(label
), 10, 10);
286 gtk_box_pack_start(GTK_BOX(vbox2
), label
, TRUE
, TRUE
, 0);
287 align
= gtk_alignment_new(1, 1, 0, 0);
288 gtk_box_pack_start(GTK_BOX(vbox2
), align
, FALSE
, TRUE
, 0);
289 button
= gtk_button_new();
290 gtk_container_add(GTK_CONTAINER(align
), button
);
291 icon
= gtk_image_new_from_pixmap(im_dirs
->pixmap
, im_dirs
->mask
);
292 gtk_container_add(GTK_CONTAINER(button
), icon
);
293 gtk_tooltips_set_tip(tooltips
, button
,
294 _("Menu of directories previously used for icons"),
296 g_signal_connect(button
, "clicked",
297 G_CALLBACK(show_current_dirs_menu
), NULL
);
299 hbox
= gtk_hbox_new(FALSE
, 4);
300 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 4);
301 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
302 gtk_box_pack_start(GTK_BOX(hbox
), gtk_label_new(_("OR")),
304 gtk_box_pack_start(GTK_BOX(hbox
), gtk_hseparator_new(), TRUE
, TRUE
, 0);
306 hbox
= gtk_hbox_new(FALSE
, 4);
307 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), hbox
, FALSE
, TRUE
, 0);
309 label
= gtk_label_new(_("Enter the path of an icon file:")),
310 gtk_misc_set_alignment(GTK_MISC(label
), 0, .5);
311 gtk_box_pack_start(GTK_BOX(hbox
), label
, TRUE
, TRUE
, 4);
313 gtk_box_pack_start(GTK_BOX(hbox
),
314 new_help_button(show_icon_help
, NULL
), FALSE
, TRUE
, 0);
316 entry
= gtk_entry_new();
317 /* Set the current icon as the default text if there is one */
319 gtk_entry_set_text(GTK_ENTRY(entry
), gi
);
321 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), entry
, FALSE
, TRUE
, 0);
322 gtk_widget_grab_focus(entry
);
323 g_object_set_data(G_OBJECT(dialog
), "icon_path", entry
);
324 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
326 button
= button_new_mixed(GTK_STOCK_DELETE
, _("_Remove"));
327 GTK_WIDGET_SET_FLAGS(button
, GTK_CAN_DEFAULT
);
328 gtk_dialog_add_action_widget(dialog
, button
, DELETE_ICON
);
329 gtk_dialog_add_buttons(dialog
,
330 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
331 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
333 g_signal_connect(dialog
, "response", G_CALLBACK(dialog_response
), NULL
);
334 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
336 gtk_widget_show_all(GTK_WIDGET(dialog
));
340 /****************************************************************
341 * INTERNAL FUNCTIONS *
342 ****************************************************************/
344 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
)
349 return TRUE
; /* For g_hash_table_foreach_remove() */
352 static void write_globicon(gpointer key
, gpointer value
, gpointer data
)
354 xmlNodePtr doc
= (xmlNodePtr
) data
;
357 tree
= xmlNewTextChild(doc
, NULL
, "rule", NULL
);
358 xmlSetProp(tree
, "match", key
);
359 xmlNewChild(tree
, NULL
, "icon", value
);
362 /* Write globicons file */
363 static void write_globicons(void)
365 gchar
*save
= NULL
, *save_new
= NULL
;
366 xmlDocPtr doc
= NULL
;
368 save
= choices_find_path_save("globicons", PROJECT
, TRUE
);
371 return; /* Saving is disabled */
373 save_new
= g_strconcat(save
, ".new", NULL
);
375 doc
= xmlNewDoc("1.0");
376 xmlDocSetRootElement(doc
,
377 xmlNewDocNode(doc
, NULL
, "special-files", NULL
));
379 g_hash_table_foreach(glob_icons
, write_globicon
,
380 xmlDocGetRootElement(doc
));
382 if (save_xml_file(doc
, save_new
) || rename(save_new
, save
))
383 delayed_error(_("Error saving %s: %s"),
384 save
, g_strerror(errno
));
393 /* Process a globicon line. Format:
394 glob-pattern icon-path
396 /home/<*>/Mail /usr/local/icons/mailbox.xpm
397 (<*> represents a single asterisk, enclosed in brackets to not break
400 static const char *process_globicons_line(gchar
*line
)
402 guchar
*pattern
, *iconpath
;
404 pattern
= strtok(line
, " \t");
405 /* We ignore empty lines, but they are no cause for a message */
409 iconpath
= strtok(NULL
, " \t");
411 /* If there is no icon, then we worry */
412 g_return_val_if_fail(iconpath
!= NULL
,
413 "Invalid line in globicons: no icon specified");
415 g_hash_table_insert(glob_icons
, g_strdup(pattern
), g_strdup(iconpath
));
420 /* Add a globicon entry to the list. If another one with the same
421 * path exists, it is replaced. Otherwise, the new entry is
422 * added to the top of the list (so that it takes precedence over
425 static void add_globicon(const gchar
*path
, const gchar
*icon
)
427 g_hash_table_insert(glob_icons
, g_strdup(path
), g_strdup(icon
));
429 /* Rewrite the globicons file */
432 /* Make sure any visible icons for the file are updated */
436 /* Remove the globicon for a certain path */
437 static void delete_globicon(const gchar
*path
)
441 if (!g_hash_table_lookup_extended(glob_icons
, path
, &key
, &value
))
444 g_hash_table_remove(glob_icons
, path
);
453 /* Set the icon for this dialog's file to 'icon' */
454 static void do_set_icon(GtkWidget
*dialog
, const gchar
*icon
)
457 GtkToggleButton
**radio
;
459 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
460 g_return_if_fail(path
!= NULL
);
462 radio
= g_object_get_data(G_OBJECT(dialog
), "radios");
463 g_return_if_fail(radio
!= NULL
);
465 if (gtk_toggle_button_get_active(radio
[0]))
467 if (!set_icon_path(path
, icon
))
475 type
= g_object_get_data(G_OBJECT(dialog
), "mime_type");
476 just_media
= gtk_toggle_button_get_active(radio
[2]);
478 if (!set_icon_for_type(type
, icon
, just_media
))
482 destroy_on_idle(dialog
);
485 /* Called when a URI list is dropped onto the box in the Set Icon
486 * dialog. Make that the default icon.
488 static void drag_icon_dropped(GtkWidget
*frame
,
489 GdkDragContext
*context
,
492 GtkSelectionData
*selection_data
,
500 if (!selection_data
->data
)
503 uris
= uri_list_to_glist(selection_data
->data
);
505 if (g_list_length(uris
) == 1)
506 icon
= g_strdup(get_local_path((guchar
*) uris
->data
));
508 for (next
= uris
; next
; next
= next
->next
)
514 delayed_error(_("You should drop a single local icon file "
515 "onto the drop box - that icon will be "
516 "used for this file from now on."));
520 do_set_icon(dialog
, icon
);
525 /* Called if the user clicks on the OK button on the Set Icon dialog */
526 static void get_path_set_icon(GtkWidget
*dialog
)
531 entry
= g_object_get_data(G_OBJECT(dialog
), "icon_path");
532 g_return_if_fail(entry
!= NULL
);
534 icon
= gtk_entry_get_text(entry
);
536 do_set_icon(dialog
, icon
);
539 static void show_icon_help(gpointer data
)
542 _("Enter the full path of a file that contains a valid "
543 "image to be used as the icon for this file or directory."));
546 /* Set the icon for the given MIME type. We copy the file. */
547 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
555 /* XXX: Should convert to XPM format... */
558 leaf
= g_strconcat(type
->media_type
, ".xpm", NULL
);
560 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
,
563 target
= choices_find_path_save(leaf
, "MIME-icons", TRUE
);
566 delayed_error(_("Setting icon disabled by CHOICESPATH"));
571 dir
= g_dirname(target
);
572 paths
= g_list_append(NULL
, (gchar
*) iconpath
);
574 action_copy(paths
, dir
, leaf
, -1);
584 static void get_dir(gpointer key
, gpointer value
, gpointer data
)
586 GHashTable
*names
= (GHashTable
*) data
;
589 dir
= g_dirname(value
); /* Freed in add_dir_to_menu */
592 g_hash_table_insert(names
, dir
, NULL
);
596 static void open_icon_dir(GtkMenuItem
*item
, gpointer data
)
601 dir
= gtk_label_get_text(GTK_LABEL(GTK_BIN(item
)->child
));
602 filer
= filer_opendir(dir
, NULL
);
604 display_set_thumbs(filer
, TRUE
);
607 static void add_dir_to_menu(gpointer key
, gpointer value
, gpointer data
)
609 GtkMenuShell
*menu
= (GtkMenuShell
*) data
;
612 item
= gtk_menu_item_new_with_label(key
);
613 gtk_widget_set_accel_path(item
, NULL
, NULL
); /* XXX */
614 g_signal_connect(item
, "activate",
615 G_CALLBACK(open_icon_dir
), NULL
);
617 gtk_menu_shell_append(menu
, item
);
620 static void show_current_dirs_menu(GtkWidget
*button
, gpointer data
)
625 names
= g_hash_table_new(g_str_hash
, g_str_equal
);
627 g_hash_table_foreach(glob_icons
, get_dir
, names
);
628 if (g_hash_table_size(glob_icons
) == 0)
630 /* TODO: Include MIME-icons? */
631 delayed_error(_("You have not yet set any special icons; "
632 "therefore, I have no directories to show you"));
636 menu
= gtk_menu_new();
638 g_hash_table_foreach(names
, add_dir_to_menu
, menu
);
640 g_hash_table_destroy(names
);
642 show_popup_menu(menu
, gtk_get_current_event(), 0);