r947: Added new SOAP methods Copy, Move, Link, Mount and FileType (Stephen Watson).
[rox-filer.git] / ROX-Filer / src / usericons.c
blobfb0254cca4ee71731739a61c560964baa3d98b3b
1 /*
2 * $Id$
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)
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 <parser.h>
34 #include "global.h"
35 #include "dir.h"
36 #include "gui_support.h"
37 #include "choices.h"
38 #include "pixmaps.h"
39 #include "type.h"
40 #include "run.h"
41 #include "dnd.h"
42 #include "support.h"
43 #include "usericons.h"
44 #include "main.h"
45 #include "menu.h"
46 #include "filer.h"
47 #include "action.h"
49 /* Store glob-to-icon mappings */
50 typedef struct _GlobIcon {
51 guchar *pattern;
52 guchar *iconpath;
53 } 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,
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(const MIME_type *type, 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 if (node->type != XML_ELEMENT_NODE)
116 continue;
117 if (strcmp(node->name, "rule") != 0)
118 continue;
119 icon = get_subnode(node, NULL, "icon");
120 if (!icon)
121 continue;
122 match = xmlGetProp(node, "match");
123 if (!match)
124 continue;
126 g_hash_table_insert(glob_icons, match,
127 xmlNodeGetContent(icon));
130 xmlFreeDoc(doc);
132 else
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 */
141 out:
142 g_free(path);
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)
150 gchar *gi;
152 g_return_if_fail(item && !item->image);
154 gi = g_hash_table_lookup(glob_icons, path);
155 if (gi)
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)
162 struct stat icon;
163 MaskedPixmap *pic;
165 /* Check if file exists */
166 if (!mc_stat(iconpath, &icon) == 0) {
167 delayed_error(_("The pathname you gave does not exist. "
168 "The icon has not been changed."));
169 return FALSE;
172 /* Check if we can load the image, warn the user if not. */
173 pic = g_fscache_lookup(pixmap_cache, iconpath);
174 if (!pic)
176 delayed_error(
177 _("Unable to load image file -- maybe it's not in a "
178 "format I understand, or maybe the permissions are "
179 "wrong?\n"
180 "The icon has not been changed."));
181 return FALSE;
183 g_fscache_data_unref(pixmap_cache, pic);
185 /* Add the globicon mapping and update visible icons */
186 add_globicon(filepath, iconpath);
188 return TRUE;
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)
196 guchar *tmp;
197 GtkWidget *dialog, *vbox, *frame, *hbox, *vbox2;
198 GtkWidget *entry, *label, *button, *align, *icon;
199 GtkWidget **radio;
200 GtkTargetEntry targets[] = {
201 {"text/uri-list", 0, TARGET_URI_LIST},
203 char *gi;
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),
211 "pathname",
212 strdup(path),
213 g_free);
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);
227 g_free(tmp);
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);
235 g_free(tmp);
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);
241 g_free(tmp);
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),
257 GDK_ACTION_COPY);
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"),
275 NULL);
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")),
283 FALSE, TRUE, 0);
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 */
298 if (gi)
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),
306 GTK_OBJECT(dialog));
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")),
312 FALSE, TRUE, 0);
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),
322 GTK_OBJECT(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 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
332 GTK_SIGNAL_FUNC(get_path_set_icon),
333 GTK_OBJECT(dialog));
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),
340 GTK_OBJECT(dialog));
342 gtk_widget_show_all(dialog);
346 /****************************************************************
347 * INTERNAL FUNCTIONS *
348 ****************************************************************/
350 static gboolean free_globicon(gpointer key, gpointer value, gpointer data)
352 g_free(key);
353 g_free(value);
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;
361 xmlNodePtr tree;
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);
376 if (!save)
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_error(_("Error saving %s: %s"),
390 save, g_strerror(errno));
392 g_free(save_new);
393 g_free(save);
395 if (doc)
396 xmlFreeDoc(doc);
399 /* Process a globicon line. Format:
400 glob-pattern icon-path
401 Example:
402 /home/<*>/Mail /usr/local/icons/mailbox.xpm
403 (<*> represents a single asterisk, enclosed in brackets to not break
404 the C comment).
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 */
412 if (pattern == NULL)
413 return NULL;
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));
423 return NULL;
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
429 * other entries).
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 */
436 write_globicons();
438 /* Make sure any visible icons for the file are updated */
439 examine(path);
442 /* Remove the globicon for a certain path */
443 static void delete_globicon(guchar *path)
445 gpointer key, value;
447 if (!g_hash_table_lookup_extended(glob_icons, path, &key, &value))
448 return;
450 g_hash_table_remove(glob_icons, path);
452 g_free(key);
453 g_free(value);
455 write_globicons();
456 examine(path);
459 /* Set the icon for this dialog's file to 'icon' */
460 static void do_set_icon(GtkWidget *dialog, gchar *icon)
462 guchar *path = NULL;
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))
474 return;
476 else
478 gboolean just_media;
479 MIME_type *type;
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))
485 return;
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,
496 gint x,
497 gint y,
498 GtkSelectionData *selection_data,
499 guint info,
500 guint32 time,
501 GtkWidget *dialog)
503 GList *uris, *next;
504 guchar *icon = NULL;
506 if (!selection_data->data)
507 return;
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)
515 g_free(next->data);
516 g_list_free(uris);
518 if (!icon)
520 delayed_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."));
523 return;
526 do_set_icon(dialog, icon);
528 g_free(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)
534 GtkEntry *entry;
535 guchar *icon;
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)
548 guchar *path;
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)
562 report_error(
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,
569 gboolean just_media)
571 gchar *target;
572 gchar *leaf;
573 gchar *dir;
574 GList *paths;
576 /* XXX: Should convert to XPM format... */
578 if (just_media)
579 leaf = g_strconcat(type->media_type, ".xpm", NULL);
580 else
581 leaf = g_strconcat(type->media_type, "_", type->subtype,
582 ".xpm", NULL);
584 target = choices_find_path_save(leaf, "MIME-icons", TRUE);
585 if (!target)
587 delayed_error(_("Setting icon disabled by CHOICESPATH"));
588 g_free(leaf);
589 return FALSE;
592 dir = g_dirname(target);
593 paths = g_list_append(NULL, iconpath);
595 action_copy(paths, dir, leaf, -1);
597 g_free(leaf);
598 g_free(dir);
599 g_free(target);
600 g_list_free(paths);
602 return TRUE;
605 static void get_dir(gpointer key, gpointer value, gpointer data)
607 GHashTable *names = (GHashTable *) data;
608 gchar *dir;
610 dir = g_dirname(value); /* Freed in add_dir_to_menu */
611 if (dir)
613 g_hash_table_insert(names, dir, NULL);
617 static void open_icon_dir(GtkMenuItem *item, gpointer data)
619 FilerWindow *filer;
620 char *dir;
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;
630 GtkWidget *item;
632 item = gtk_menu_item_new_with_label(key);
633 #ifdef GTK2
634 _gtk_widget_set_accel_path(item, NULL, NULL);
635 #else
636 gtk_widget_lock_accelerators(item);
637 #endif
638 gtk_signal_connect(GTK_OBJECT(item), "activate",
639 GTK_SIGNAL_FUNC(open_icon_dir), NULL);
640 g_free(key);
641 gtk_menu_shell_append(menu, item);
644 static void show_current_dirs_menu(GtkWidget *button, gpointer data)
646 GHashTable *names;
647 GtkWidget *menu;
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_error(_("You have not yet set any special icons; "
656 "therefore, I have no directories to show you"));
657 return;
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);