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)
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
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. */
30 #include <libxml/parser.h>
38 #include "gui_support.h"
45 #include "usericons.h"
57 #define SET_PATH 0 /* Store in globicons */
58 #define SET_COPY 3 /* Create .DirIcon */
60 static GHashTable
*glob_icons
= NULL
; /* Pathname -> Icon pathname */
62 /* Static prototypes */
63 static const char *process_globicons_line(gchar
*line
);
64 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
);
65 static void write_globicons(void);
66 static void drag_icon_dropped(GtkWidget
*drop_box
,
69 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
71 static gboolean
convert_to_png(const gchar
*src
, const gchar
*dest
);
72 static void radios_changed(Radios
*radios
, gpointer data
);
74 /****************************************************************
75 * EXTERNAL INTERFACE *
76 ****************************************************************/
78 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
81 static time_t last_read
= (time_t) 0;
87 glob_icons
= g_hash_table_new(g_str_hash
, g_str_equal
);
89 path
= choices_find_xdg_path_load("globicons", PROJECT
, SITE
);
91 return; /* Nothing to load */
93 if (mc_stat(path
, &info
))
96 if (info
.st_mtime
<= last_read
)
97 goto out
; /* File hasn't been modified since we last read it */
99 g_hash_table_foreach_remove(glob_icons
, free_globicon
, NULL
);
101 doc
= xmlParseFile(path
);
104 xmlNodePtr node
, icon
, root
;
107 root
= xmlDocGetRootElement(doc
);
109 /* Handle the new XML file format */
110 for (node
= root
->xmlChildrenNode
; node
; node
= node
->next
)
112 gchar
*path
, *icon_path
;
114 if (node
->type
!= XML_ELEMENT_NODE
)
116 if (strcmp(node
->name
, "rule") != 0)
118 icon
= get_subnode(node
, NULL
, "icon");
121 match
= xmlGetProp(node
, "match");
125 icon_path
= xmlNodeGetContent(icon
);
126 path
= expand_path(match
);
127 g_hash_table_insert(glob_icons
, path
, icon_path
);
135 /* Handle the old non-XML format */
136 parse_file(path
, process_globicons_line
);
137 if (g_hash_table_size(glob_icons
))
138 write_globicons(); /* Upgrade to new format */
141 last_read
= time(NULL
); /* Update time stamp */
146 /* Set an item's image field according to the globicons patterns if
147 * it matches one of them and the file exists.
149 void check_globicon(const guchar
*path
, DirItem
*item
)
153 g_return_if_fail(item
&& !item
->_image
);
155 gi
= g_hash_table_lookup(glob_icons
, path
);
157 item
->_image
= g_fscache_lookup(pixmap_cache
, gi
);
160 static gboolean
create_diricon(const guchar
*filepath
, const guchar
*iconpath
)
162 if (!convert_to_png(iconpath
, make_path(filepath
, ".DirIcon")))
165 dir_check_this(filepath
);
166 icons_may_update(filepath
);
171 /* Add a globicon mapping for the given file to the given icon path */
172 static gboolean
set_icon_path(const guchar
*filepath
, const guchar
*iconpath
)
176 /* Check if file exists */
177 if (!file_exists(iconpath
))
179 delayed_error(_("The pathname you gave does not exist. "
180 "The icon has not been changed."));
184 /* Check if we can load the image, warn the user if not. */
185 pic
= g_fscache_lookup(pixmap_cache
, iconpath
);
189 _("Unable to load image file -- maybe it's not in a "
190 "format I understand, or maybe the permissions are "
192 "The icon has not been changed."));
197 /* Add the globicon mapping and update visible icons */
198 add_globicon(filepath
, iconpath
);
203 static void dialog_response(GtkWidget
*dialog
, gint response
, gpointer data
)
205 gtk_widget_destroy(dialog
);
208 static void clear_icon(DropBox
*drop_box
, GObject
*dialog
)
211 const guchar
*pathname
;
213 pathname
= g_object_get_data(G_OBJECT(dialog
), "pathname");
214 g_return_if_fail(pathname
!= NULL
);
216 radios
= g_object_get_data(G_OBJECT(dialog
), "radios");
217 g_return_if_fail(radios
!= NULL
);
219 if (radios_get_value(radios
) == SET_PATH
)
221 delete_globicon(pathname
);
229 drop_box
= g_object_get_data(G_OBJECT(dialog
), "rox-dropbox");
230 g_return_if_fail(drop_box
!= NULL
);
232 path
= drop_box_get_path(drop_box
);
233 g_return_if_fail(path
!= NULL
);
235 tmp
= g_strdup_printf(_("Really delete icon '%s'?"), path
);
236 if (confirm(tmp
, GTK_STOCK_DELETE
, NULL
))
239 delayed_error(_("Can't delete '%s':\n%s"),
240 path
, g_strerror(errno
));
243 dir_check_this(pathname
);
244 icons_may_update(pathname
);
251 radios_changed(g_object_get_data(dialog
, "radios"), dialog
);
254 /* Display a dialog box allowing the user to set the icon for
255 * a file or directory.
257 void icon_set_handler_dialog(DirItem
*item
, const guchar
*path
)
264 g_return_if_fail(item
!= NULL
&& path
!= NULL
);
266 dialog
= GTK_DIALOG(gtk_dialog_new());
267 gtk_dialog_set_has_separator(dialog
, FALSE
);
268 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
269 g_object_set_data_full(G_OBJECT(dialog
), "pathname",
270 strdup(path
), g_free
);
272 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set icon"));
274 radios
= radios_new(radios_changed
, dialog
);
276 g_object_set_data(G_OBJECT(dialog
), "radios", radios
);
277 g_object_set_data(G_OBJECT(dialog
), "mime-type", item
->mime_type
);
281 _("Use a copy of the image as the default for all "
282 "files of these MIME types."), SET_MEDIA
,
283 _("Set icon for all `%s/<anything>'"),
284 item
->mime_type
->media_type
);
288 _("Use a copy of the image for all files of this MIME "
290 _("For all files of type `%s' (%s/%s)"),
291 mime_type_comment(item
->mime_type
),
292 item
->mime_type
->media_type
,
293 item
->mime_type
->subtype
);
296 _("Add the file and image filenames to your "
297 "personal list. The setting will be lost if the image "
298 "or the file is moved."), SET_PATH
,
299 _("Only for the file `%s'"), path
);
301 radios_set_value(radios
, SET_PATH
);
303 /* If it's a directory, offer to create a .DirIcon */
304 if (mc_stat(path
, &info
) == 0 && S_ISDIR(info
.st_mode
))
307 _("Copy the image inside the directory, as "
308 "a hidden file called '.DirIcon'. "
309 "All users will then see the "
310 "icon, and you can move the directory around safely. "
311 "This is usually the best option if you can write to "
312 "the directory."), SET_COPY
,
313 _("Copy image into directory"));
314 if (access(path
, W_OK
) == 0)
315 radios_set_value(radios
, SET_COPY
);
319 frame
= drop_box_new(_("Drop an icon file here"));
320 g_object_set_data(G_OBJECT(dialog
), "rox-dropbox", frame
);
322 /* Make sure rox-dropbox is set before packing (calls changed) */
323 radios_pack(radios
, GTK_BOX(dialog
->vbox
));
324 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), frame
, TRUE
, TRUE
, 4);
326 g_signal_connect(frame
, "path_dropped",
327 G_CALLBACK(drag_icon_dropped
), dialog
);
328 g_signal_connect(frame
, "clear",
329 G_CALLBACK(clear_icon
), dialog
);
331 gtk_dialog_add_buttons(dialog
,
332 GTK_STOCK_CLOSE
, GTK_RESPONSE_OK
,
334 g_signal_connect(dialog
, "response", G_CALLBACK(dialog_response
), NULL
);
335 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
337 gtk_widget_show_all(GTK_WIDGET(dialog
));
341 /****************************************************************
342 * INTERNAL FUNCTIONS *
343 ****************************************************************/
345 /* The dropbox shows the path for the currently selected radio setting.
347 static void radios_changed(Radios
*radios
, gpointer data
)
349 GObject
*dialog
= G_OBJECT(data
);
352 MIME_type
*mime_type
;
354 path
= g_object_get_data(dialog
, "pathname");
355 drop_box
= g_object_get_data(dialog
, "rox-dropbox");
356 mime_type
= g_object_get_data(dialog
, "mime-type");
358 g_return_if_fail(radios
!= NULL
);
359 g_return_if_fail(path
!= NULL
);
360 g_return_if_fail(drop_box
!= NULL
);
361 g_return_if_fail(mime_type
!= NULL
);
363 switch (radios_get_value(radios
))
369 type
= g_strconcat(mime_type
->media_type
, ".png", NULL
);
370 path
= choices_find_xdg_path_load(type
, "MIME-icons",
373 drop_box_set_path(drop_box
, path
);
381 type
= g_strconcat(mime_type
->media_type
, "_",
382 mime_type
->subtype
, ".png", NULL
);
383 path
= choices_find_xdg_path_load(type
, "MIME-icons",
386 drop_box_set_path(drop_box
, path
);
393 gi
= g_hash_table_lookup(glob_icons
, path
);
394 drop_box_set_path(drop_box
, gi
);
400 diricon
= make_path(path
, ".DirIcon");
401 if (file_exists(diricon
))
402 drop_box_set_path(drop_box
, diricon
);
404 drop_box_set_path(drop_box
, NULL
);
408 drop_box_set_path(drop_box
, NULL
);
413 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
)
418 return TRUE
; /* For g_hash_table_foreach_remove() */
421 static void write_globicon(gpointer key
, gpointer value
, gpointer data
)
423 xmlNodePtr doc
= (xmlNodePtr
) data
;
426 tree
= xmlNewTextChild(doc
, NULL
, "rule", NULL
);
427 xmlSetProp(tree
, "match", key
);
428 xmlNewTextChild(tree
, NULL
, "icon", value
);
431 /* Write globicons file */
432 static void write_globicons(void)
434 gchar
*save
= NULL
, *save_new
= NULL
;
435 xmlDocPtr doc
= NULL
;
437 save
= choices_find_xdg_path_save("globicons", PROJECT
, SITE
, TRUE
);
440 return; /* Saving is disabled */
442 save_new
= g_strconcat(save
, ".new", NULL
);
444 doc
= xmlNewDoc("1.0");
445 xmlDocSetRootElement(doc
,
446 xmlNewDocNode(doc
, NULL
, "special-files", NULL
));
448 g_hash_table_foreach(glob_icons
, write_globicon
,
449 xmlDocGetRootElement(doc
));
451 if (save_xml_file(doc
, save_new
) || rename(save_new
, save
))
452 delayed_error(_("Error saving %s: %s"),
453 save
, g_strerror(errno
));
462 /* Process a globicon line. Format:
463 glob-pattern icon-path
465 /home/<*>/Mail /usr/local/icons/mailbox.xpm
466 (<*> represents a single asterisk, enclosed in brackets to not break
469 static const char *process_globicons_line(gchar
*line
)
471 guchar
*pattern
, *iconpath
;
473 pattern
= strtok(line
, " \t");
474 /* We ignore empty lines, but they are no cause for a message */
478 iconpath
= strtok(NULL
, " \t");
480 /* If there is no icon, then we worry */
481 g_return_val_if_fail(iconpath
!= NULL
,
482 "Invalid line in globicons: no icon specified");
484 g_hash_table_insert(glob_icons
, g_strdup(pattern
), g_strdup(iconpath
));
489 /* Add a globicon entry to the list. If another one with the same
490 * path exists, it is replaced. Otherwise, the new entry is
491 * added to the top of the list (so that it takes precedence over
494 void add_globicon(const gchar
*path
, const gchar
*icon
)
496 g_hash_table_insert(glob_icons
, g_strdup(path
), g_strdup(icon
));
498 /* Rewrite the globicons file */
501 /* Make sure any visible icons for the file are updated */
505 /* Remove the globicon for a certain path */
506 void delete_globicon(const gchar
*path
)
510 if (!g_hash_table_lookup_extended(glob_icons
, path
, &key
, &value
))
513 g_hash_table_remove(glob_icons
, path
);
522 /* Set the icon for this dialog's file to 'icon' */
523 static void do_set_icon(GtkWidget
*dialog
, const gchar
*icon
)
529 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
530 g_return_if_fail(path
!= NULL
);
532 radios
= g_object_get_data(G_OBJECT(dialog
), "radios");
533 g_return_if_fail(radios
!= NULL
);
535 op
= radios_get_value(radios
);
539 if (!set_icon_path(path
, icon
))
542 else if (op
== SET_COPY
)
544 if (!create_diricon(path
, icon
))
549 gboolean just_media
= (op
== SET_MEDIA
);
552 type
= g_object_get_data(G_OBJECT(dialog
), "mime-type");
554 if (!set_icon_for_type(type
, icon
, just_media
))
558 destroy_on_idle(dialog
);
561 /* Called when a URI list is dropped onto the box in the Set Icon
562 * dialog. Make that the default icon.
564 static void drag_icon_dropped(GtkWidget
*drop_box
,
568 do_set_icon(dialog
, path
);
571 /* Set the icon for the given MIME type. We copy the file. */
572 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
579 leaf
= g_strconcat(type
->media_type
, ".png", NULL
);
581 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
,
584 target
= choices_find_xdg_path_save(leaf
, "MIME-icons", SITE
, TRUE
);
589 delayed_error(_("Setting icon disabled by CHOICESPATH"));
593 if (!convert_to_png(iconpath
, target
))
606 /* Load image 'src', and save it as an icon-sized image in png format.
607 * TRUE on success, error is already reported on failure.
609 static gboolean
convert_to_png(const gchar
*src
, const gchar
*dest
)
612 GError
*error
= NULL
;
614 pic
= g_fscache_lookup(pixmap_cache
, src
);
618 _("Unable to load image file -- maybe it's not in a "
619 "format I understand, or maybe the permissions are "
621 "The icon has not been changed."));
625 gdk_pixbuf_save(pic
->src_pixbuf
, dest
,
627 "tEXt::Software", PROJECT
,
633 delayed_error(_("Error creating image '%s':\n%s"),
634 dest
, error
->message
);