r1432: Removed all traces of Pixmaps from MaskedPixmap structure (!).
[rox-filer.git] / ROX-Filer / src / usericons.c
blob54a1c351d798a907dd2755d9f3e693fc9015520d
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 "support.h"
55 #define DELETE_ICON 1
57 /* Used to index the 'radios' array... */
58 #define SET_MEDIA 2
59 #define SET_TYPE 1
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,
75 gint x,
76 gint y,
77 GtkSelectionData *selection_data,
78 guint info,
79 guint32 time,
80 GtkWidget *dialog);
81 static gboolean set_icon_for_type(MIME_type *type, const gchar *iconpath,
82 gboolean just_media);
83 static void delete_globicon(const gchar *path);
85 /****************************************************************
86 * EXTERNAL INTERFACE *
87 ****************************************************************/
89 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
90 void read_globicons()
92 static time_t last_read = (time_t) 0;
93 struct stat info;
94 guchar *path;
95 xmlDocPtr doc;
97 if (!glob_icons)
98 glob_icons = g_hash_table_new(g_str_hash, g_str_equal);
100 path = choices_find_path_load("globicons", PROJECT);
101 if (!path)
102 return; /* Nothing to load */
104 if (mc_stat(path, &info) == -1)
105 goto out;
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);
113 if (doc)
115 xmlNodePtr node, icon, root;
116 char *match;
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)
126 continue;
127 if (strcmp(node->name, "rule") != 0)
128 continue;
129 icon = get_subnode(node, NULL, "icon");
130 if (!icon)
131 continue;
132 match = xmlGetProp(node, "match");
133 if (!match)
134 continue;
136 icon_path = xmlNodeGetContent(icon);
137 path = expand_path(match);
138 g_hash_table_insert(glob_icons, path, icon_path);
139 g_free(match);
142 xmlFreeDoc(doc);
144 else
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 */
153 out:
154 g_free(path);
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)
162 gchar *gi;
164 g_return_if_fail(item && !item->image);
166 gi = g_hash_table_lookup(glob_icons, path);
167 if (gi)
168 item->image = g_fscache_lookup(pixmap_cache, gi);
171 gboolean create_diricon(const guchar *filepath, const guchar *iconpath)
173 MaskedPixmap *pic;
174 gchar *icon_path;
175 GError *error = NULL;
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;
188 icon_path = make_path(filepath, ".DirIcon")->str;
189 gdk_pixbuf_save(pic->src_pixbuf, icon_path,
190 "png", &error,
191 "tEXt::Software", PROJECT,
192 NULL);
193 g_object_unref(pic);
195 if (error)
197 delayed_error(_("Error creating image '%s':\n%s"),
198 icon_path, error->message);
199 g_error_free(error);
200 return FALSE;
203 dir_check_this(filepath);
205 return TRUE;
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)
211 struct stat icon;
212 MaskedPixmap *pic;
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."));
219 return FALSE;
222 /* Check if we can load the image, warn the user if not. */
223 pic = g_fscache_lookup(pixmap_cache, iconpath);
224 if (!pic)
226 delayed_error(
227 _("Unable to load image file -- maybe it's not in a "
228 "format I understand, or maybe the permissions are "
229 "wrong?\n"
230 "The icon has not been changed."));
231 return FALSE;
233 g_object_unref(pic);
235 /* Add the globicon mapping and update visible icons */
236 add_globicon(filepath, iconpath);
238 return TRUE;
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)
249 const gchar *path;
250 gchar *icon_path;
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)
260 GList *list;
262 list = g_list_prepend(NULL, icon_path);
263 action_delete(list);
264 g_list_free(list);
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)
276 struct stat info;
277 guchar *tmp;
278 GtkDialog *dialog;
279 GtkWidget *frame, *hbox, *vbox2;
280 GtkWidget *entry, *label, *button, *align, *icon;
281 GtkWidget **radio;
282 GtkRadioButton *group;
283 GtkTargetEntry targets[] = {
284 {"text/uri-list", 0, TARGET_URI_LIST},
286 char *gi;
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);
307 g_free(tmp);
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,
314 tmp);
315 gtk_box_pack_start(GTK_BOX(dialog->vbox), radio[SET_TYPE],
316 FALSE, TRUE, 0);
317 g_free(tmp);
319 tmp = g_strdup_printf(_("Only for the file `%s'"), path);
320 radio[SET_PATH] = gtk_radio_button_new_with_label_from_widget(group,
321 tmp);
322 gtk_box_pack_start(GTK_BOX(dialog->vbox), radio[SET_PATH],
323 FALSE, TRUE, 0);
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);
328 g_free(tmp);
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],
337 FALSE, TRUE, 0);
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]),
349 TRUE);
351 else
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),
364 GDK_ACTION_COPY);
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"),
382 NULL);
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")),
390 FALSE, TRUE, 0);
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 */
405 if (gi)
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,
419 NULL);
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)
433 g_free(key);
434 g_free(value);
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;
442 xmlNodePtr tree;
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);
457 if (!save)
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));
473 g_free(save_new);
474 g_free(save);
476 if (doc)
477 xmlFreeDoc(doc);
480 /* Process a globicon line. Format:
481 glob-pattern icon-path
482 Example:
483 /home/<*>/Mail /usr/local/icons/mailbox.xpm
484 (<*> represents a single asterisk, enclosed in brackets to not break
485 the C comment).
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 */
493 if (pattern == NULL)
494 return NULL;
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));
504 return NULL;
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
510 * other entries).
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 */
517 write_globicons();
519 /* Make sure any visible icons for the file are updated */
520 examine(path);
523 /* Remove the globicon for a certain path */
524 static void delete_globicon(const gchar *path)
526 gpointer key, value;
528 if (!g_hash_table_lookup_extended(glob_icons, path, &key, &value))
529 return;
531 g_hash_table_remove(glob_icons, path);
533 g_free(key);
534 g_free(value);
536 write_globicons();
537 examine(path);
540 /* Set the icon for this dialog's file to 'icon' */
541 static void do_set_icon(GtkWidget *dialog, const gchar *icon)
543 guchar *path = NULL;
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))
555 return;
557 else if (radio[SET_COPY] &&
558 gtk_toggle_button_get_active(radio[SET_COPY]))
560 if (!create_diricon(path, icon))
561 return;
563 else
565 gboolean just_media;
566 MIME_type *type;
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))
572 return;
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,
583 gint x,
584 gint y,
585 GtkSelectionData *selection_data,
586 guint info,
587 guint32 time,
588 GtkWidget *dialog)
590 GList *uris, *next;
591 guchar *icon = NULL;
593 if (!selection_data->data)
594 return;
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)
602 g_free(next->data);
603 g_list_free(uris);
605 if (!icon)
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."));
610 return;
613 do_set_icon(dialog, icon);
615 g_free(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)
621 GtkEntry *entry;
622 const gchar *icon;
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)
634 info_message(
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,
641 gboolean just_media)
643 gchar *target;
644 gchar *leaf;
645 gchar *dir;
646 GList *paths;
648 /* XXX: Should convert to XPM format... */
650 if (just_media)
651 leaf = g_strconcat(type->media_type, ".xpm", NULL);
652 else
653 leaf = g_strconcat(type->media_type, "_", type->subtype,
654 ".xpm", NULL);
656 target = choices_find_path_save(leaf, "MIME-icons", TRUE);
657 if (!target)
659 delayed_error(_("Setting icon disabled by CHOICESPATH"));
660 g_free(leaf);
661 return FALSE;
664 dir = g_dirname(target);
665 paths = g_list_append(NULL, (gchar *) iconpath);
667 action_copy(paths, dir, leaf, -1);
669 g_free(leaf);
670 g_free(dir);
671 g_free(target);
672 g_list_free(paths);
674 return TRUE;
677 static void get_dir(gpointer key, gpointer value, gpointer data)
679 GHashTable *names = (GHashTable *) data;
680 gchar *dir;
682 dir = g_dirname(value); /* Freed in add_dir_to_menu */
683 if (dir)
685 g_hash_table_insert(names, dir, NULL);
689 static void open_icon_dir(GtkMenuItem *item, gpointer data)
691 FilerWindow *filer;
692 const char *dir;
694 dir = gtk_label_get_text(GTK_LABEL(GTK_BIN(item)->child));
695 filer = filer_opendir(dir, NULL);
696 if (filer)
697 display_set_thumbs(filer, TRUE);
700 static void add_dir_to_menu(gpointer key, gpointer value, gpointer data)
702 GtkMenuShell *menu = (GtkMenuShell *) data;
703 GtkWidget *item;
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);
709 g_free(key);
710 gtk_menu_shell_append(menu, item);
713 static void show_current_dirs_menu(GtkWidget *button, gpointer data)
715 GHashTable *names;
716 GtkWidget *menu;
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"));
726 return;
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);