r1298: Panel icons highlight when the pointer is over them.
[rox-filer.git] / ROX-Filer / src / usericons.c
blob30e0ae598e009c1f3e2f55bcdf7427ef5904c0c0
1 /*
2 * $Id$
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)
10 * any later version.
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
15 * more details.
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. */
24 #include "config.h"
26 #include <gtk/gtk.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <fnmatch.h>
32 #include <libxml/parser.h>
33 #include <time.h>
35 #include "global.h"
37 #include "fscache.h"
38 #include "diritem.h"
39 #include "dir.h"
40 #include "gui_support.h"
41 #include "choices.h"
42 #include "pixmaps.h"
43 #include "type.h"
44 #include "run.h"
45 #include "dnd.h"
46 #include "support.h"
47 #include "usericons.h"
48 #include "main.h"
49 #include "menu.h"
50 #include "filer.h"
51 #include "action.h"
52 #include "display.h"
53 #include "icon.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,
67 gint x,
68 gint y,
69 GtkSelectionData *selection_data,
70 guint info,
71 guint32 time,
72 GtkWidget *dialog);
73 static void remove_icon(GtkWidget *dialog);
74 static gboolean set_icon_for_type(MIME_type *type, const gchar *iconpath,
75 gboolean just_media);
77 /****************************************************************
78 * EXTERNAL INTERFACE *
79 ****************************************************************/
81 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
82 void read_globicons()
84 static time_t last_read = (time_t) 0;
85 struct stat info;
86 guchar *path;
87 xmlDocPtr doc;
89 if (!glob_icons)
90 glob_icons = g_hash_table_new(g_str_hash, g_str_equal);
92 path = choices_find_path_load("globicons", PROJECT);
93 if (!path)
94 return; /* Nothing to load */
96 if (mc_stat(path, &info) == -1)
97 goto out;
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);
105 if (doc)
107 xmlNodePtr node, icon, root;
108 char *match;
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)
118 continue;
119 if (strcmp(node->name, "rule") != 0)
120 continue;
121 icon = get_subnode(node, NULL, "icon");
122 if (!icon)
123 continue;
124 match = xmlGetProp(node, "match");
125 if (!match)
126 continue;
128 icon_path = xmlNodeGetContent(icon);
129 path = icon_convert_path(match);
130 g_hash_table_insert(glob_icons, path, icon_path);
131 g_free(match);
134 xmlFreeDoc(doc);
136 else
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 */
145 out:
146 g_free(path);
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)
154 gchar *gi;
156 g_return_if_fail(item && !item->image);
158 gi = g_hash_table_lookup(glob_icons, path);
159 if (gi)
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)
166 struct stat icon;
167 MaskedPixmap *pic;
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."));
173 return FALSE;
176 /* Check if we can load the image, warn the user if not. */
177 pic = g_fscache_lookup(pixmap_cache, iconpath);
178 if (!pic)
180 delayed_error(
181 _("Unable to load image file -- maybe it's not in a "
182 "format I understand, or maybe the permissions are "
183 "wrong?\n"
184 "The icon has not been changed."));
185 return FALSE;
187 g_fscache_data_unref(pixmap_cache, pic);
189 /* Add the globicon mapping and update visible icons */
190 add_globicon(filepath, iconpath);
192 return TRUE;
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)
200 guchar *tmp;
201 GtkWidget *dialog, *vbox, *frame, *hbox, *vbox2;
202 GtkWidget *entry, *label, *button, *align, *icon;
203 GtkWidget **radio;
204 GtkTargetEntry targets[] = {
205 {"text/uri-list", 0, TARGET_URI_LIST},
207 char *gi;
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);
231 g_free(tmp);
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);
239 g_free(tmp);
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);
245 g_free(tmp);
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),
259 GDK_ACTION_COPY);
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"),
277 NULL);
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")),
285 FALSE, TRUE, 0);
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 */
300 if (gi)
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")),
313 FALSE, TRUE, 0);
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)
350 g_free(key);
351 g_free(value);
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;
359 xmlNodePtr tree;
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);
374 if (!save)
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));
390 g_free(save_new);
391 g_free(save);
393 if (doc)
394 xmlFreeDoc(doc);
397 /* Process a globicon line. Format:
398 glob-pattern icon-path
399 Example:
400 /home/<*>/Mail /usr/local/icons/mailbox.xpm
401 (<*> represents a single asterisk, enclosed in brackets to not break
402 the C comment).
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 */
410 if (pattern == NULL)
411 return NULL;
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));
421 return NULL;
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
427 * other entries).
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 */
434 write_globicons();
436 /* Make sure any visible icons for the file are updated */
437 examine(path);
440 /* Remove the globicon for a certain path */
441 static void delete_globicon(guchar *path)
443 gpointer key, value;
445 if (!g_hash_table_lookup_extended(glob_icons, path, &key, &value))
446 return;
448 g_hash_table_remove(glob_icons, path);
450 g_free(key);
451 g_free(value);
453 write_globicons();
454 examine(path);
457 /* Set the icon for this dialog's file to 'icon' */
458 static void do_set_icon(GtkWidget *dialog, const gchar *icon)
460 guchar *path = NULL;
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))
472 return;
474 else
476 gboolean just_media;
477 MIME_type *type;
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))
483 return;
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,
494 gint x,
495 gint y,
496 GtkSelectionData *selection_data,
497 guint info,
498 guint32 time,
499 GtkWidget *dialog)
501 GList *uris, *next;
502 guchar *icon = NULL;
504 if (!selection_data->data)
505 return;
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)
513 g_free(next->data);
514 g_list_free(uris);
516 if (!icon)
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."));
521 return;
524 do_set_icon(dialog, icon);
526 g_free(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)
532 GtkEntry *entry;
533 const gchar *icon;
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)
546 guchar *path;
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)
560 report_error(
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,
567 gboolean just_media)
569 gchar *target;
570 gchar *leaf;
571 gchar *dir;
572 GList *paths;
574 /* XXX: Should convert to XPM format... */
576 if (just_media)
577 leaf = g_strconcat(type->media_type, ".xpm", NULL);
578 else
579 leaf = g_strconcat(type->media_type, "_", type->subtype,
580 ".xpm", NULL);
582 target = choices_find_path_save(leaf, "MIME-icons", TRUE);
583 if (!target)
585 delayed_error(_("Setting icon disabled by CHOICESPATH"));
586 g_free(leaf);
587 return FALSE;
590 dir = g_dirname(target);
591 paths = g_list_append(NULL, (gchar *) iconpath);
593 action_copy(paths, dir, leaf, -1);
595 g_free(leaf);
596 g_free(dir);
597 g_free(target);
598 g_list_free(paths);
600 return TRUE;
603 static void get_dir(gpointer key, gpointer value, gpointer data)
605 GHashTable *names = (GHashTable *) data;
606 gchar *dir;
608 dir = g_dirname(value); /* Freed in add_dir_to_menu */
609 if (dir)
611 g_hash_table_insert(names, dir, NULL);
615 static void open_icon_dir(GtkMenuItem *item, gpointer data)
617 FilerWindow *filer;
618 const char *dir;
620 dir = gtk_label_get_text(GTK_LABEL(GTK_BIN(item)->child));
621 filer = filer_opendir(dir, NULL);
622 if (filer)
623 display_set_thumbs(filer, TRUE);
626 static void add_dir_to_menu(gpointer key, gpointer value, gpointer data)
628 GtkMenuShell *menu = (GtkMenuShell *) data;
629 GtkWidget *item;
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);
635 g_free(key);
636 gtk_menu_shell_append(menu, item);
639 static void show_current_dirs_menu(GtkWidget *button, gpointer data)
641 GHashTable *names;
642 GtkWidget *menu;
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"));
652 return;
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);