r4646: Bugfix: when a panel icon's image changed, the display didn't update
[rox-filer/translations.git] / ROX-Filer / src / usericons.c
blob6b69a13ecb76f29a7e2dc60b372d9904a4a9dd7f
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* usericons.c - handle user-defined icons. Diego Zamboni, Feb 7, 2001. */
22 #include "config.h"
24 #include <gtk/gtk.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <fnmatch.h>
30 #include <libxml/parser.h>
31 #include <time.h>
33 #include "global.h"
35 #include "fscache.h"
36 #include "diritem.h"
37 #include "dir.h"
38 #include "gui_support.h"
39 #include "choices.h"
40 #include "pixmaps.h"
41 #include "type.h"
42 #include "run.h"
43 #include "dnd.h"
44 #include "support.h"
45 #include "usericons.h"
46 #include "main.h"
47 #include "menu.h"
48 #include "filer.h"
49 #include "action.h"
50 #include "display.h"
51 #include "xml.h"
52 #include "dropbox.h"
54 #define SET_MEDIA 2
55 #define SET_TYPE 1
56 #define SET_PATH 0 /* Store in globicons */
57 #define SET_COPY 3 /* Create .DirIcon */
59 static GHashTable *glob_icons = NULL; /* Pathname -> Icon pathname */
61 /* Static prototypes */
62 static const char *process_globicons_line(gchar *line);
63 static gboolean free_globicon(gpointer key, gpointer value, gpointer data);
64 static void write_globicons(void);
65 static void drag_icon_dropped(GtkWidget *drop_box,
66 const guchar *path,
67 GtkWidget *dialog);
68 static gboolean set_icon_for_type(MIME_type *type, const gchar *iconpath,
69 gboolean just_media);
70 static gboolean convert_to_png(const gchar *src, const gchar *dest);
71 static void radios_changed(Radios *radios, gpointer data);
73 /****************************************************************
74 * EXTERNAL INTERFACE *
75 ****************************************************************/
77 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
78 void read_globicons()
80 static time_t last_read = (time_t) 0;
81 struct stat info;
82 guchar *path;
83 xmlDocPtr doc;
85 if (!glob_icons)
86 glob_icons = g_hash_table_new(g_str_hash, g_str_equal);
88 path = choices_find_xdg_path_load("globicons", PROJECT, SITE);
89 if (!path)
90 return; /* Nothing to load */
92 if (mc_stat(path, &info))
93 goto out;
95 if (info.st_mtime <= last_read)
96 goto out; /* File hasn't been modified since we last read it */
98 g_hash_table_foreach_remove(glob_icons, free_globicon, NULL);
100 doc = xmlParseFile(path);
101 if (doc)
103 xmlNodePtr node, icon, root;
104 char *match;
106 root = xmlDocGetRootElement(doc);
108 /* Handle the new XML file format */
109 for (node = root->xmlChildrenNode; node; node = node->next)
111 gchar *path, *icon_path;
113 if (node->type != XML_ELEMENT_NODE)
114 continue;
115 if (strcmp(node->name, "rule") != 0)
116 continue;
117 icon = get_subnode(node, NULL, "icon");
118 if (!icon)
119 continue;
120 match = xmlGetProp(node, "match");
121 if (!match)
122 continue;
124 icon_path = xmlNodeGetContent(icon);
125 path = expand_path(match);
126 g_hash_table_insert(glob_icons, path, icon_path);
127 g_free(match);
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(const 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 static gboolean create_diricon(const guchar *filepath, const guchar *iconpath)
161 if (!convert_to_png(iconpath, make_path(filepath, ".DirIcon")))
162 return FALSE;
164 dir_check_this(filepath);
166 return TRUE;
169 /* Add a globicon mapping for the given file to the given icon path */
170 static gboolean set_icon_path(const guchar *filepath, const guchar *iconpath)
172 MaskedPixmap *pic;
174 /* Check if file exists */
175 if (!file_exists(iconpath))
177 delayed_error(_("The pathname you gave does not exist. "
178 "The icon has not been changed."));
179 return FALSE;
182 /* Check if we can load the image, warn the user if not. */
183 pic = g_fscache_lookup(pixmap_cache, iconpath);
184 if (!pic)
186 delayed_error(
187 _("Unable to load image file -- maybe it's not in a "
188 "format I understand, or maybe the permissions are "
189 "wrong?\n"
190 "The icon has not been changed."));
191 return FALSE;
193 g_object_unref(pic);
195 /* Add the globicon mapping and update visible icons */
196 add_globicon(filepath, iconpath);
198 return TRUE;
201 static void dialog_response(GtkWidget *dialog, gint response, gpointer data)
203 gtk_widget_destroy(dialog);
206 static void clear_icon(DropBox *drop_box, GObject *dialog)
208 Radios *radios;
210 radios = g_object_get_data(G_OBJECT(dialog), "radios");
211 g_return_if_fail(radios != NULL);
213 if (radios_get_value(radios) == SET_PATH)
215 const guchar *path;
217 path = g_object_get_data(G_OBJECT(dialog), "pathname");
218 g_return_if_fail(path != NULL);
220 delete_globicon(path);
222 else
224 const guchar *path;
225 guchar *tmp;
226 DropBox *drop_box;
228 drop_box = g_object_get_data(G_OBJECT(dialog), "rox-dropbox");
229 g_return_if_fail(drop_box != NULL);
231 path = drop_box_get_path(drop_box);
232 g_return_if_fail(path != NULL);
234 tmp = g_strdup_printf(_("Really delete icon '%s'?"), path);
235 if (confirm(tmp, GTK_STOCK_DELETE, NULL))
237 if (unlink(path))
238 delayed_error(_("Can't delete '%s':\n%s"),
239 path, g_strerror(errno));
241 g_free(tmp);
244 full_refresh();
245 radios_changed(g_object_get_data(dialog, "radios"), dialog);
248 /* Display a dialog box allowing the user to set the icon for
249 * a file or directory.
251 void icon_set_handler_dialog(DirItem *item, const guchar *path)
253 struct stat info;
254 GtkDialog *dialog;
255 GtkWidget *frame;
256 Radios *radios;
258 g_return_if_fail(item != NULL && path != NULL);
260 dialog = GTK_DIALOG(gtk_dialog_new());
261 gtk_dialog_set_has_separator(dialog, FALSE);
262 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
263 g_object_set_data_full(G_OBJECT(dialog), "pathname",
264 strdup(path), g_free);
266 gtk_window_set_title(GTK_WINDOW(dialog), _("Set icon"));
268 radios = radios_new(radios_changed, dialog);
270 g_object_set_data(G_OBJECT(dialog), "radios", radios);
271 g_object_set_data(G_OBJECT(dialog), "mime-type", item->mime_type);
273 #if 0
274 radios_add(radios,
275 _("Use a copy of the image as the default for all "
276 "files of these MIME types."), SET_MEDIA,
277 _("Set icon for all `%s/<anything>'"),
278 item->mime_type->media_type);
279 #endif
281 radios_add(radios,
282 _("Use a copy of the image for all files of this MIME "
283 "type."), SET_TYPE,
284 _("For all files of type `%s' (%s/%s)"),
285 mime_type_comment(item->mime_type),
286 item->mime_type->media_type,
287 item->mime_type->subtype);
289 radios_add(radios,
290 _("Add the file and image filenames to your "
291 "personal list. The setting will be lost if the image "
292 "or the file is moved."), SET_PATH,
293 _("Only for the file `%s'"), path);
295 radios_set_value(radios, SET_PATH);
297 /* If it's a directory, offer to create a .DirIcon */
298 if (mc_stat(path, &info) == 0 && S_ISDIR(info.st_mode))
300 radios_add(radios,
301 _("Copy the image inside the directory, as "
302 "a hidden file called '.DirIcon'. "
303 "All users will then see the "
304 "icon, and you can move the directory around safely. "
305 "This is usually the best option if you can write to "
306 "the directory."), SET_COPY,
307 _("Copy image into directory"));
308 if (access(path, W_OK) == 0)
309 radios_set_value(radios, SET_COPY);
313 frame = drop_box_new(_("Drop an icon file here"));
314 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
316 /* Make sure rox-dropbox is set before packing (calls changed) */
317 radios_pack(radios, GTK_BOX(dialog->vbox));
318 gtk_box_pack_start(GTK_BOX(dialog->vbox), frame, TRUE, TRUE, 4);
320 g_signal_connect(frame, "path_dropped",
321 G_CALLBACK(drag_icon_dropped), dialog);
322 g_signal_connect(frame, "clear",
323 G_CALLBACK(clear_icon), dialog);
325 gtk_dialog_add_buttons(dialog,
326 GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
327 NULL);
328 g_signal_connect(dialog, "response", G_CALLBACK(dialog_response), NULL);
329 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
331 gtk_widget_show_all(GTK_WIDGET(dialog));
335 /****************************************************************
336 * INTERNAL FUNCTIONS *
337 ****************************************************************/
339 /* The dropbox shows the path for the currently selected radio setting.
341 static void radios_changed(Radios *radios, gpointer data)
343 GObject *dialog = G_OBJECT(data);
344 DropBox *drop_box;
345 const guchar *path;
346 MIME_type *mime_type;
348 path = g_object_get_data(dialog, "pathname");
349 drop_box = g_object_get_data(dialog, "rox-dropbox");
350 mime_type = g_object_get_data(dialog, "mime-type");
352 g_return_if_fail(radios != NULL);
353 g_return_if_fail(path != NULL);
354 g_return_if_fail(drop_box != NULL);
355 g_return_if_fail(mime_type != NULL);
357 switch (radios_get_value(radios))
359 case SET_MEDIA:
361 char *path, *type;
363 type = g_strconcat(mime_type->media_type, ".png", NULL);
364 path = choices_find_xdg_path_load(type, "MIME-icons",
365 SITE);
366 g_free(type);
367 drop_box_set_path(drop_box, path);
368 g_free(path);
369 break;
371 case SET_TYPE:
373 char *path, *type;
375 type = g_strconcat(mime_type->media_type, "_",
376 mime_type->subtype, ".png", NULL);
377 path = choices_find_xdg_path_load(type, "MIME-icons",
378 SITE);
379 g_free(type);
380 drop_box_set_path(drop_box, path);
381 g_free(path);
382 break;
384 case SET_PATH:
386 const char *gi;
387 gi = g_hash_table_lookup(glob_icons, path);
388 drop_box_set_path(drop_box, gi);
389 break;
391 case SET_COPY:
393 const char *diricon;
394 diricon = make_path(path, ".DirIcon");
395 if (file_exists(diricon))
396 drop_box_set_path(drop_box, diricon);
397 else
398 drop_box_set_path(drop_box, NULL);
399 break;
401 default:
402 drop_box_set_path(drop_box, NULL);
403 break;
407 static gboolean free_globicon(gpointer key, gpointer value, gpointer data)
409 g_free(key);
410 g_free(value);
412 return TRUE; /* For g_hash_table_foreach_remove() */
415 static void write_globicon(gpointer key, gpointer value, gpointer data)
417 xmlNodePtr doc = (xmlNodePtr) data;
418 xmlNodePtr tree;
420 tree = xmlNewTextChild(doc, NULL, "rule", NULL);
421 xmlSetProp(tree, "match", key);
422 xmlNewTextChild(tree, NULL, "icon", value);
425 /* Write globicons file */
426 static void write_globicons(void)
428 gchar *save = NULL, *save_new = NULL;
429 xmlDocPtr doc = NULL;
431 save = choices_find_xdg_path_save("globicons", PROJECT, SITE, TRUE);
433 if (!save)
434 return; /* Saving is disabled */
436 save_new = g_strconcat(save, ".new", NULL);
438 doc = xmlNewDoc("1.0");
439 xmlDocSetRootElement(doc,
440 xmlNewDocNode(doc, NULL, "special-files", NULL));
442 g_hash_table_foreach(glob_icons, write_globicon,
443 xmlDocGetRootElement(doc));
445 if (save_xml_file(doc, save_new) || rename(save_new, save))
446 delayed_error(_("Error saving %s: %s"),
447 save, g_strerror(errno));
449 g_free(save_new);
450 g_free(save);
452 if (doc)
453 xmlFreeDoc(doc);
456 /* Process a globicon line. Format:
457 glob-pattern icon-path
458 Example:
459 /home/<*>/Mail /usr/local/icons/mailbox.xpm
460 (<*> represents a single asterisk, enclosed in brackets to not break
461 the C comment).
463 static const char *process_globicons_line(gchar *line)
465 guchar *pattern, *iconpath;
467 pattern = strtok(line, " \t");
468 /* We ignore empty lines, but they are no cause for a message */
469 if (pattern == NULL)
470 return NULL;
472 iconpath = strtok(NULL, " \t");
474 /* If there is no icon, then we worry */
475 g_return_val_if_fail(iconpath != NULL,
476 "Invalid line in globicons: no icon specified");
478 g_hash_table_insert(glob_icons, g_strdup(pattern), g_strdup(iconpath));
480 return NULL;
483 /* Add a globicon entry to the list. If another one with the same
484 * path exists, it is replaced. Otherwise, the new entry is
485 * added to the top of the list (so that it takes precedence over
486 * other entries).
488 void add_globicon(const gchar *path, const gchar *icon)
490 g_hash_table_insert(glob_icons, g_strdup(path), g_strdup(icon));
492 /* Rewrite the globicons file */
493 write_globicons();
495 /* Make sure any visible icons for the file are updated */
496 examine(path);
499 /* Remove the globicon for a certain path */
500 void delete_globicon(const gchar *path)
502 gpointer key, value;
504 if (!g_hash_table_lookup_extended(glob_icons, path, &key, &value))
505 return;
507 g_hash_table_remove(glob_icons, path);
509 g_free(key);
510 g_free(value);
512 write_globicons();
513 examine(path);
516 /* Set the icon for this dialog's file to 'icon' */
517 static void do_set_icon(GtkWidget *dialog, const gchar *icon)
519 guchar *path = NULL;
520 Radios *radios;
521 int op;
523 path = g_object_get_data(G_OBJECT(dialog), "pathname");
524 g_return_if_fail(path != NULL);
526 radios = g_object_get_data(G_OBJECT(dialog), "radios");
527 g_return_if_fail(radios != NULL);
529 op = radios_get_value(radios);
531 if (op == SET_PATH)
533 if (!set_icon_path(path, icon))
534 return;
536 else if (op == SET_COPY)
538 if (!create_diricon(path, icon))
539 return;
541 else
543 gboolean just_media = (op == SET_MEDIA);
544 MIME_type *type;
546 type = g_object_get_data(G_OBJECT(dialog), "mime-type");
548 if (!set_icon_for_type(type, icon, just_media))
549 return;
552 destroy_on_idle(dialog);
555 /* Called when a URI list is dropped onto the box in the Set Icon
556 * dialog. Make that the default icon.
558 static void drag_icon_dropped(GtkWidget *drop_box,
559 const guchar *path,
560 GtkWidget *dialog)
562 do_set_icon(dialog, path);
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;
572 if (just_media)
573 leaf = g_strconcat(type->media_type, ".png", NULL);
574 else
575 leaf = g_strconcat(type->media_type, "_", type->subtype,
576 ".png", NULL);
578 target = choices_find_xdg_path_save(leaf, "MIME-icons", SITE, TRUE);
579 g_free(leaf);
581 if (!target)
583 delayed_error(_("Setting icon disabled by CHOICESPATH"));
584 return FALSE;
587 if (!convert_to_png(iconpath, target))
589 g_free(target);
590 return FALSE;
593 g_free(target);
595 full_refresh();
597 return TRUE;
600 /* Load image 'src', and save it as an icon-sized image in png format.
601 * TRUE on success, error is already reported on failure.
603 static gboolean convert_to_png(const gchar *src, const gchar *dest)
605 MaskedPixmap *pic;
606 GError *error = NULL;
608 pic = g_fscache_lookup(pixmap_cache, src);
609 if (!pic)
611 delayed_error(
612 _("Unable to load image file -- maybe it's not in a "
613 "format I understand, or maybe the permissions are "
614 "wrong?\n"
615 "The icon has not been changed."));
616 return FALSE;
619 gdk_pixbuf_save(pic->src_pixbuf, dest,
620 "png", &error,
621 "tEXt::Software", PROJECT,
622 NULL);
623 g_object_unref(pic);
625 if (error)
627 delayed_error(_("Error creating image '%s':\n%s"),
628 dest, error->message);
629 g_error_free(error);
630 return FALSE;
633 return TRUE;