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"
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 add_globicon(const gchar
*path
, const gchar
*icon
);
66 static void drag_icon_dropped(GtkWidget
*drop_box
,
69 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
71 static void delete_globicon(const gchar
*path
);
72 static gboolean
convert_to_png(const gchar
*src
, const gchar
*dest
);
73 static void radios_changed(Radios
*radios
, gpointer data
);
75 /****************************************************************
76 * EXTERNAL INTERFACE *
77 ****************************************************************/
79 /* Read glob-pattern -> icon mappings from "globicons" in Choices */
82 static time_t last_read
= (time_t) 0;
88 glob_icons
= g_hash_table_new(g_str_hash
, g_str_equal
);
90 path
= choices_find_xdg_path_load("globicons", PROJECT
, SITE
);
92 return; /* Nothing to load */
94 if (mc_stat(path
, &info
))
97 if (info
.st_mtime
<= last_read
)
98 goto out
; /* File hasn't been modified since we last read it */
100 g_hash_table_foreach_remove(glob_icons
, free_globicon
, NULL
);
102 doc
= xmlParseFile(path
);
105 xmlNodePtr node
, icon
, root
;
108 root
= xmlDocGetRootElement(doc
);
110 /* Handle the new XML file format */
111 for (node
= root
->xmlChildrenNode
; node
; node
= node
->next
)
113 gchar
*path
, *icon_path
;
115 if (node
->type
!= XML_ELEMENT_NODE
)
117 if (strcmp(node
->name
, "rule") != 0)
119 icon
= get_subnode(node
, NULL
, "icon");
122 match
= xmlGetProp(node
, "match");
126 icon_path
= xmlNodeGetContent(icon
);
127 path
= expand_path(match
);
128 g_hash_table_insert(glob_icons
, path
, icon_path
);
136 /* Handle the old non-XML format */
137 parse_file(path
, process_globicons_line
);
138 if (g_hash_table_size(glob_icons
))
139 write_globicons(); /* Upgrade to new format */
142 last_read
= time(NULL
); /* Update time stamp */
147 /* Set an item's image field according to the globicons patterns if
148 * it matches one of them and the file exists.
150 void check_globicon(const guchar
*path
, DirItem
*item
)
154 g_return_if_fail(item
&& !item
->_image
);
156 gi
= g_hash_table_lookup(glob_icons
, path
);
158 item
->_image
= g_fscache_lookup(pixmap_cache
, gi
);
161 static gboolean
create_diricon(const guchar
*filepath
, const guchar
*iconpath
)
163 if (!convert_to_png(iconpath
, make_path(filepath
, ".DirIcon")))
166 dir_check_this(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
)
212 radios
= g_object_get_data(G_OBJECT(dialog
), "radios");
213 g_return_if_fail(radios
!= NULL
);
215 if (radios_get_value(radios
) == SET_PATH
)
219 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
220 g_return_if_fail(path
!= NULL
);
222 delete_globicon(path
);
230 drop_box
= g_object_get_data(G_OBJECT(dialog
), "rox-dropbox");
231 g_return_if_fail(drop_box
!= NULL
);
233 path
= drop_box_get_path(drop_box
);
234 g_return_if_fail(path
!= NULL
);
236 tmp
= g_strdup_printf(_("Really delete icon '%s'?"), path
);
237 if (confirm(tmp
, GTK_STOCK_DELETE
, NULL
))
240 delayed_error(_("Can't delete '%s':\n%s"),
241 path
, g_strerror(errno
));
247 radios_changed(g_object_get_data(dialog
, "radios"), dialog
);
250 /* Display a dialog box allowing the user to set the icon for
251 * a file or directory.
253 void icon_set_handler_dialog(DirItem
*item
, const guchar
*path
)
260 g_return_if_fail(item
!= NULL
&& path
!= NULL
);
262 dialog
= GTK_DIALOG(gtk_dialog_new());
263 gtk_dialog_set_has_separator(dialog
, FALSE
);
264 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
265 g_object_set_data_full(G_OBJECT(dialog
), "pathname",
266 strdup(path
), g_free
);
268 gtk_window_set_title(GTK_WINDOW(dialog
), _("Set icon"));
270 radios
= radios_new(radios_changed
, dialog
);
272 g_object_set_data(G_OBJECT(dialog
), "radios", radios
);
273 g_object_set_data(G_OBJECT(dialog
), "mime-type", item
->mime_type
);
277 _("Use a copy of the image as the default for all "
278 "files of these MIME types."), SET_MEDIA
,
279 _("Set icon for all `%s/<anything>'"),
280 item
->mime_type
->media_type
);
284 _("Use a copy of the image for all files of this MIME "
286 _("For all files of type `%s' (%s/%s)"),
287 mime_type_comment(item
->mime_type
),
288 item
->mime_type
->media_type
,
289 item
->mime_type
->subtype
);
292 _("Add the file and image filenames to your "
293 "personal list. The setting will be lost if the image "
294 "or the file is moved."), SET_PATH
,
295 _("Only for the file `%s'"), path
);
297 radios_set_value(radios
, SET_PATH
);
299 /* If it's a directory, offer to create a .DirIcon */
300 if (mc_stat(path
, &info
) == 0 && S_ISDIR(info
.st_mode
))
303 _("Copy the image inside the directory, as "
304 "a hidden file called '.DirIcon'. "
305 "All users will then see the "
306 "icon, and you can move the directory around safely. "
307 "This is usually the best option if you can write to "
308 "the directory."), SET_COPY
,
309 _("Copy image into directory"));
310 if (access(path
, W_OK
) == 0)
311 radios_set_value(radios
, SET_COPY
);
315 frame
= drop_box_new(_("Drop an icon file here"));
316 g_object_set_data(G_OBJECT(dialog
), "rox-dropbox", frame
);
318 /* Make sure rox-dropbox is set before packing (calls changed) */
319 radios_pack(radios
, GTK_BOX(dialog
->vbox
));
320 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), frame
, TRUE
, TRUE
, 4);
322 g_signal_connect(frame
, "path_dropped",
323 G_CALLBACK(drag_icon_dropped
), dialog
);
324 g_signal_connect(frame
, "clear",
325 G_CALLBACK(clear_icon
), dialog
);
327 gtk_dialog_add_buttons(dialog
,
328 GTK_STOCK_CLOSE
, GTK_RESPONSE_OK
,
330 g_signal_connect(dialog
, "response", G_CALLBACK(dialog_response
), NULL
);
331 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
333 gtk_widget_show_all(GTK_WIDGET(dialog
));
337 /****************************************************************
338 * INTERNAL FUNCTIONS *
339 ****************************************************************/
341 /* The dropbox shows the path for the currently selected radio setting.
343 static void radios_changed(Radios
*radios
, gpointer data
)
345 GObject
*dialog
= G_OBJECT(data
);
348 MIME_type
*mime_type
;
350 path
= g_object_get_data(dialog
, "pathname");
351 drop_box
= g_object_get_data(dialog
, "rox-dropbox");
352 mime_type
= g_object_get_data(dialog
, "mime-type");
354 g_return_if_fail(radios
!= NULL
);
355 g_return_if_fail(path
!= NULL
);
356 g_return_if_fail(drop_box
!= NULL
);
357 g_return_if_fail(mime_type
!= NULL
);
359 switch (radios_get_value(radios
))
365 type
= g_strconcat(mime_type
->media_type
, ".png", NULL
);
366 path
= choices_find_xdg_path_load(type
, "MIME-icons",
369 drop_box_set_path(drop_box
, path
);
377 type
= g_strconcat(mime_type
->media_type
, "_",
378 mime_type
->subtype
, ".png", NULL
);
379 path
= choices_find_xdg_path_load(type
, "MIME-icons",
382 drop_box_set_path(drop_box
, path
);
389 gi
= g_hash_table_lookup(glob_icons
, path
);
390 drop_box_set_path(drop_box
, gi
);
396 diricon
= make_path(path
, ".DirIcon");
397 if (file_exists(diricon
))
398 drop_box_set_path(drop_box
, diricon
);
400 drop_box_set_path(drop_box
, NULL
);
404 drop_box_set_path(drop_box
, NULL
);
409 static gboolean
free_globicon(gpointer key
, gpointer value
, gpointer data
)
414 return TRUE
; /* For g_hash_table_foreach_remove() */
417 static void write_globicon(gpointer key
, gpointer value
, gpointer data
)
419 xmlNodePtr doc
= (xmlNodePtr
) data
;
422 tree
= xmlNewTextChild(doc
, NULL
, "rule", NULL
);
423 xmlSetProp(tree
, "match", key
);
424 xmlNewTextChild(tree
, NULL
, "icon", value
);
427 /* Write globicons file */
428 static void write_globicons(void)
430 gchar
*save
= NULL
, *save_new
= NULL
;
431 xmlDocPtr doc
= NULL
;
433 save
= choices_find_xdg_path_save("globicons", PROJECT
, SITE
, TRUE
);
436 return; /* Saving is disabled */
438 save_new
= g_strconcat(save
, ".new", NULL
);
440 doc
= xmlNewDoc("1.0");
441 xmlDocSetRootElement(doc
,
442 xmlNewDocNode(doc
, NULL
, "special-files", NULL
));
444 g_hash_table_foreach(glob_icons
, write_globicon
,
445 xmlDocGetRootElement(doc
));
447 if (save_xml_file(doc
, save_new
) || rename(save_new
, save
))
448 delayed_error(_("Error saving %s: %s"),
449 save
, g_strerror(errno
));
458 /* Process a globicon line. Format:
459 glob-pattern icon-path
461 /home/<*>/Mail /usr/local/icons/mailbox.xpm
462 (<*> represents a single asterisk, enclosed in brackets to not break
465 static const char *process_globicons_line(gchar
*line
)
467 guchar
*pattern
, *iconpath
;
469 pattern
= strtok(line
, " \t");
470 /* We ignore empty lines, but they are no cause for a message */
474 iconpath
= strtok(NULL
, " \t");
476 /* If there is no icon, then we worry */
477 g_return_val_if_fail(iconpath
!= NULL
,
478 "Invalid line in globicons: no icon specified");
480 g_hash_table_insert(glob_icons
, g_strdup(pattern
), g_strdup(iconpath
));
485 /* Add a globicon entry to the list. If another one with the same
486 * path exists, it is replaced. Otherwise, the new entry is
487 * added to the top of the list (so that it takes precedence over
490 static void add_globicon(const gchar
*path
, const gchar
*icon
)
492 g_hash_table_insert(glob_icons
, g_strdup(path
), g_strdup(icon
));
494 /* Rewrite the globicons file */
497 /* Make sure any visible icons for the file are updated */
501 /* Remove the globicon for a certain path */
502 static void delete_globicon(const gchar
*path
)
506 if (!g_hash_table_lookup_extended(glob_icons
, path
, &key
, &value
))
509 g_hash_table_remove(glob_icons
, path
);
518 /* Set the icon for this dialog's file to 'icon' */
519 static void do_set_icon(GtkWidget
*dialog
, const gchar
*icon
)
525 path
= g_object_get_data(G_OBJECT(dialog
), "pathname");
526 g_return_if_fail(path
!= NULL
);
528 radios
= g_object_get_data(G_OBJECT(dialog
), "radios");
529 g_return_if_fail(radios
!= NULL
);
531 op
= radios_get_value(radios
);
535 if (!set_icon_path(path
, icon
))
538 else if (op
== SET_COPY
)
540 if (!create_diricon(path
, icon
))
545 gboolean just_media
= (op
== SET_MEDIA
);
548 type
= g_object_get_data(G_OBJECT(dialog
), "mime-type");
550 if (!set_icon_for_type(type
, icon
, just_media
))
554 destroy_on_idle(dialog
);
557 /* Called when a URI list is dropped onto the box in the Set Icon
558 * dialog. Make that the default icon.
560 static void drag_icon_dropped(GtkWidget
*drop_box
,
564 do_set_icon(dialog
, path
);
567 /* Set the icon for the given MIME type. We copy the file. */
568 static gboolean
set_icon_for_type(MIME_type
*type
, const gchar
*iconpath
,
575 leaf
= g_strconcat(type
->media_type
, ".png", NULL
);
577 leaf
= g_strconcat(type
->media_type
, "_", type
->subtype
,
580 target
= choices_find_xdg_path_save(leaf
, "MIME-icons", SITE
, TRUE
);
585 delayed_error(_("Setting icon disabled by CHOICESPATH"));
589 if (!convert_to_png(iconpath
, target
))
602 /* Load image 'src', and save it as an icon-sized image in png format.
603 * TRUE on success, error is already reported on failure.
605 static gboolean
convert_to_png(const gchar
*src
, const gchar
*dest
)
608 GError
*error
= NULL
;
610 pic
= g_fscache_lookup(pixmap_cache
, src
);
614 _("Unable to load image file -- maybe it's not in a "
615 "format I understand, or maybe the permissions are "
617 "The icon has not been changed."));
621 gdk_pixbuf_save(pic
->src_pixbuf
, dest
,
623 "tEXt::Software", PROJECT
,
629 delayed_error(_("Error creating image '%s':\n%s"),
630 dest
, error
->message
);