4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, 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"
58 #define SET_PATH 0 /* Store in globicons */
59 #define SET_COPY 3 /* Create .DirIcon */
61 static GHashTable
*glob_icons
= NULL
; /* Pathname -> Icon pathname */
63 /* Static prototypes */
64 static const char *process_globicons_line(gchar
*line
);
65 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
);
66 static void write_globicons(void);
67 static void add_globicon(const gchar
*path
, const gchar
*icon
);
68 static void drag_icon_dropped(GtkWidget
*drop_box
,
71 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
73 static void delete_globicon(const gchar
*path
);
74 static gboolean
convert_to_png(const gchar
*src
, const gchar
*dest
);
75 static void radios_changed(Radios
*radios
, gpointer data
);
77 /****************************************************************
78 * EXTERNAL INTERFACE *
79 ****************************************************************/
81 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
84 static time_t last_read
= (time_t) 0;
90 glob_icons
= g_hash_table_new(g_str_hash
, g_str_equal
);
92 path
= choices_find_xdg_path_load("globicons", PROJECT
, SITE
);
94 return; /* Nothing to load */
96 if (mc_stat(path
, &info
))
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
);
107 xmlNodePtr node
, icon
, root
;
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
)
119 if (strcmp(node
->name
, "rule") != 0)
121 icon
= get_subnode(node
, NULL
, "icon");
124 match
= xmlGetProp(node
, "match");
128 icon_path
= xmlNodeGetContent(icon
);
129 path
= expand_path(match
);
130 g_hash_table_insert(glob_icons
, path
, icon_path
);
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 */
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
)
156 g_return_if_fail(item
&& !item
->_image
);
158 gi
= g_hash_table_lookup(glob_icons
, path
);
160 item
->_image
= g_fscache_lookup(pixmap_cache
, gi
);
163 static gboolean
create_diricon(const guchar
*filepath
, const guchar
*iconpath
)
165 if (!convert_to_png(iconpath
, make_path(filepath
, ".DirIcon")))
168 dir_check_this(filepath
);
173 /* Add a globicon mapping for the given file to the given icon path */
174 static gboolean
set_icon_path(const guchar
*filepath
, const guchar
*iconpath
)
178 /* Check if file exists */
179 if (!file_exists(iconpath
))
181 delayed_error(_("The pathname you gave does not exist. "
182 "The icon has not been changed."));
186 /* Check if we can load the image, warn the user if not. */
187 pic
= g_fscache_lookup(pixmap_cache
, iconpath
);
191 _("Unable to load image file -- maybe it's not in a "
192 "format I understand, or maybe the permissions are "
194 "The icon has not been changed."));
199 /* Add the globicon mapping and update visible icons */
200 add_globicon(filepath
, iconpath
);
205 static void dialog_response(GtkWidget
*dialog
, gint response
, gpointer data
)
207 gtk_widget_destroy(dialog
);
210 static void clear_icon(DropBox
*drop_box
, GObject
*dialog
)
214 radios
= g_object_get_data(G_OBJECT(dialog
), "radios");
215 g_return_if_fail(radios
!= NULL
);
217 if (radios_get_value(radios
) == SET_PATH
)
221 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
222 g_return_if_fail(path
!= NULL
);
224 delete_globicon(path
);
232 drop_box
= g_object_get_data(G_OBJECT(dialog
), "rox-dropbox");
233 g_return_if_fail(drop_box
!= NULL
);
235 path
= drop_box_get_path(drop_box
);
236 g_return_if_fail(path
!= NULL
);
238 tmp
= g_strdup_printf(_("Really delete icon '%s'?"), path
);
239 if (confirm(tmp
, GTK_STOCK_DELETE
, NULL
))
242 delayed_error(_("Can't delete '%s':\n%s"),
243 path
, g_strerror(errno
));
249 radios_changed(g_object_get_data(dialog
, "radios"), dialog
);
252 /* Display a dialog box allowing the user to set the icon for
253 * a file or directory.
255 void icon_set_handler_dialog(DirItem
*item
, const guchar
*path
)
262 g_return_if_fail(item
!= NULL
&& path
!= NULL
);
264 dialog
= GTK_DIALOG(gtk_dialog_new());
265 gtk_dialog_set_has_separator(dialog
, FALSE
);
266 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
267 g_object_set_data_full(G_OBJECT(dialog
), "pathname",
268 strdup(path
), g_free
);
270 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set icon"));
272 radios
= radios_new(radios_changed
, dialog
);
274 g_object_set_data(G_OBJECT(dialog
), "radios", radios
);
275 g_object_set_data(G_OBJECT(dialog
), "mime-type", item
->mime_type
);
279 _("Use a copy of the image as the default for all "
280 "files of these MIME types."), SET_MEDIA
,
281 _("Set icon for all `%s/<anything>'"),
282 item
->mime_type
->media_type
);
286 _("Use a copy of the image for all files of this MIME "
288 _("For all files of type `%s' (%s/%s)"),
289 mime_type_comment(item
->mime_type
),
290 item
->mime_type
->media_type
,
291 item
->mime_type
->subtype
);
294 _("Add the file and image filenames to your "
295 "personal list. The setting will be lost if the image "
296 "or the file is moved."), SET_PATH
,
297 _("Only for the file `%s'"), path
);
299 radios_set_value(radios
, SET_PATH
);
301 /* If it's a directory, offer to create a .DirIcon */
302 if (mc_stat(path
, &info
) == 0 && S_ISDIR(info
.st_mode
))
305 _("Copy the image inside the directory, as "
306 "a hidden file called '.DirIcon'. "
307 "All users will then see the "
308 "icon, and you can move the directory around safely. "
309 "This is usually the best option if you can write to "
310 "the directory."), SET_COPY
,
311 _("Copy image into directory"));
312 if (access(path
, W_OK
) == 0)
313 radios_set_value(radios
, SET_COPY
);
317 frame
= drop_box_new(_("Drop an icon file here"));
318 g_object_set_data(G_OBJECT(dialog
), "rox-dropbox", frame
);
320 /* Make sure rox-dropbox is set before packing (calls changed) */
321 radios_pack(radios
, GTK_BOX(dialog
->vbox
));
322 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), frame
, TRUE
, TRUE
, 4);
324 g_signal_connect(frame
, "path_dropped",
325 G_CALLBACK(drag_icon_dropped
), dialog
);
326 g_signal_connect(frame
, "clear",
327 G_CALLBACK(clear_icon
), dialog
);
329 gtk_dialog_add_buttons(dialog
,
330 GTK_STOCK_CLOSE
, GTK_RESPONSE_OK
,
332 g_signal_connect(dialog
, "response", G_CALLBACK(dialog_response
), NULL
);
333 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
335 gtk_widget_show_all(GTK_WIDGET(dialog
));
339 /****************************************************************
340 * INTERNAL FUNCTIONS *
341 ****************************************************************/
343 /* The dropbox shows the path for the currently selected radio setting.
345 static void radios_changed(Radios
*radios
, gpointer data
)
347 GObject
*dialog
= G_OBJECT(data
);
350 MIME_type
*mime_type
;
352 path
= g_object_get_data(dialog
, "pathname");
353 drop_box
= g_object_get_data(dialog
, "rox-dropbox");
354 mime_type
= g_object_get_data(dialog
, "mime-type");
356 g_return_if_fail(radios
!= NULL
);
357 g_return_if_fail(path
!= NULL
);
358 g_return_if_fail(drop_box
!= NULL
);
359 g_return_if_fail(mime_type
!= NULL
);
361 switch (radios_get_value(radios
))
367 type
= g_strconcat(mime_type
->media_type
, ".png", NULL
);
368 path
= choices_find_xdg_path_load(type
, "MIME-icons",
371 drop_box_set_path(drop_box
, path
);
379 type
= g_strconcat(mime_type
->media_type
, "_",
380 mime_type
->subtype
, ".png", NULL
);
381 path
= choices_find_xdg_path_load(type
, "MIME-icons",
384 drop_box_set_path(drop_box
, path
);
391 gi
= g_hash_table_lookup(glob_icons
, path
);
392 drop_box_set_path(drop_box
, gi
);
398 diricon
= make_path(path
, ".DirIcon");
399 if (file_exists(diricon
))
400 drop_box_set_path(drop_box
, diricon
);
402 drop_box_set_path(drop_box
, NULL
);
406 drop_box_set_path(drop_box
, NULL
);
411 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
)
416 return TRUE
; /* For g_hash_table_foreach_remove() */
419 static void write_globicon(gpointer key
, gpointer value
, gpointer data
)
421 xmlNodePtr doc
= (xmlNodePtr
) data
;
424 tree
= xmlNewTextChild(doc
, NULL
, "rule", NULL
);
425 xmlSetProp(tree
, "match", key
);
426 xmlNewTextChild(tree
, NULL
, "icon", value
);
429 /* Write globicons file */
430 static void write_globicons(void)
432 gchar
*save
= NULL
, *save_new
= NULL
;
433 xmlDocPtr doc
= NULL
;
435 save
= choices_find_xdg_path_save("globicons", PROJECT
, SITE
, TRUE
);
438 return; /* Saving is disabled */
440 save_new
= g_strconcat(save
, ".new", NULL
);
442 doc
= xmlNewDoc("1.0");
443 xmlDocSetRootElement(doc
,
444 xmlNewDocNode(doc
, NULL
, "special-files", NULL
));
446 g_hash_table_foreach(glob_icons
, write_globicon
,
447 xmlDocGetRootElement(doc
));
449 if (save_xml_file(doc
, save_new
) || rename(save_new
, save
))
450 delayed_error(_("Error saving %s: %s"),
451 save
, g_strerror(errno
));
460 /* Process a globicon line. Format:
461 glob-pattern icon-path
463 /home/<*>/Mail /usr/local/icons/mailbox.xpm
464 (<*> represents a single asterisk, enclosed in brackets to not break
467 static const char *process_globicons_line(gchar
*line
)
469 guchar
*pattern
, *iconpath
;
471 pattern
= strtok(line
, " \t");
472 /* We ignore empty lines, but they are no cause for a message */
476 iconpath
= strtok(NULL
, " \t");
478 /* If there is no icon, then we worry */
479 g_return_val_if_fail(iconpath
!= NULL
,
480 "Invalid line in globicons: no icon specified");
482 g_hash_table_insert(glob_icons
, g_strdup(pattern
), g_strdup(iconpath
));
487 /* Add a globicon entry to the list. If another one with the same
488 * path exists, it is replaced. Otherwise, the new entry is
489 * added to the top of the list (so that it takes precedence over
492 static void add_globicon(const gchar
*path
, const gchar
*icon
)
494 g_hash_table_insert(glob_icons
, g_strdup(path
), g_strdup(icon
));
496 /* Rewrite the globicons file */
499 /* Make sure any visible icons for the file are updated */
503 /* Remove the globicon for a certain path */
504 static void delete_globicon(const gchar
*path
)
508 if (!g_hash_table_lookup_extended(glob_icons
, path
, &key
, &value
))
511 g_hash_table_remove(glob_icons
, path
);
520 /* Set the icon for this dialog's file to 'icon' */
521 static void do_set_icon(GtkWidget
*dialog
, const gchar
*icon
)
527 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
528 g_return_if_fail(path
!= NULL
);
530 radios
= g_object_get_data(G_OBJECT(dialog
), "radios");
531 g_return_if_fail(radios
!= NULL
);
533 op
= radios_get_value(radios
);
537 if (!set_icon_path(path
, icon
))
540 else if (op
== SET_COPY
)
542 if (!create_diricon(path
, icon
))
547 gboolean just_media
= (op
== SET_MEDIA
);
550 type
= g_object_get_data(G_OBJECT(dialog
), "mime-type");
552 if (!set_icon_for_type(type
, icon
, just_media
))
556 destroy_on_idle(dialog
);
559 /* Called when a URI list is dropped onto the box in the Set Icon
560 * dialog. Make that the default icon.
562 static void drag_icon_dropped(GtkWidget
*drop_box
,
566 do_set_icon(dialog
, path
);
569 /* Set the icon for the given MIME type. We copy the file. */
570 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
577 leaf
= g_strconcat(type
->media_type
, ".png", NULL
);
579 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
,
582 target
= choices_find_xdg_path_save(leaf
, "MIME-icons", SITE
, TRUE
);
587 delayed_error(_("Setting icon disabled by CHOICESPATH"));
591 if (!convert_to_png(iconpath
, target
))
604 /* Load image 'src', and save it as an icon-sized image in png format.
605 * TRUE on success, error is already reported on failure.
607 static gboolean
convert_to_png(const gchar
*src
, const gchar
*dest
)
610 GError
*error
= NULL
;
612 pic
= g_fscache_lookup(pixmap_cache
, src
);
616 _("Unable to load image file -- maybe it's not in a "
617 "format I understand, or maybe the permissions are "
619 "The icon has not been changed."));
623 gdk_pixbuf_save(pic
->src_pixbuf
, dest
,
625 "tEXt::Software", PROJECT
,
631 delayed_error(_("Error creating image '%s':\n%s"),
632 dest
, error
->message
);