r4649: Fixed compiler warning (Dennis Tomas).
[rox-filer/translations.git] / ROX-Filer / src / usericons.c
blob59b749262b5d2939ea04087e71e03bfa1c11c8b9
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"
53 #include "icon.h"
55 #define SET_MEDIA 2
56 #define SET_TYPE 1
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,
67 const guchar *path,
68 GtkWidget *dialog);
69 static gboolean set_icon_for_type(MIME_type *type, const gchar *iconpath,
70 gboolean just_media);
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 */
79 void read_globicons()
81 static time_t last_read = (time_t) 0;
82 struct stat info;
83 guchar *path;
84 xmlDocPtr doc;
86 if (!glob_icons)
87 glob_icons = g_hash_table_new(g_str_hash, g_str_equal);
89 path = choices_find_xdg_path_load("globicons", PROJECT, SITE);
90 if (!path)
91 return; /* Nothing to load */
93 if (mc_stat(path, &info))
94 goto out;
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);
102 if (doc)
104 xmlNodePtr node, icon, root;
105 char *match;
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)
115 continue;
116 if (strcmp(node->name, "rule") != 0)
117 continue;
118 icon = get_subnode(node, NULL, "icon");
119 if (!icon)
120 continue;
121 match = xmlGetProp(node, "match");
122 if (!match)
123 continue;
125 icon_path = xmlNodeGetContent(icon);
126 path = expand_path(match);
127 g_hash_table_insert(glob_icons, path, icon_path);
128 g_free(match);
131 xmlFreeDoc(doc);
133 else
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 */
142 out:
143 g_free(path);
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)
151 gchar *gi;
153 g_return_if_fail(item && !item->_image);
155 gi = g_hash_table_lookup(glob_icons, path);
156 if (gi)
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")))
163 return FALSE;
165 dir_check_this(filepath);
166 icons_may_update(filepath);
168 return TRUE;
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)
174 MaskedPixmap *pic;
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."));
181 return FALSE;
184 /* Check if we can load the image, warn the user if not. */
185 pic = g_fscache_lookup(pixmap_cache, iconpath);
186 if (!pic)
188 delayed_error(
189 _("Unable to load image file -- maybe it's not in a "
190 "format I understand, or maybe the permissions are "
191 "wrong?\n"
192 "The icon has not been changed."));
193 return FALSE;
195 g_object_unref(pic);
197 /* Add the globicon mapping and update visible icons */
198 add_globicon(filepath, iconpath);
200 return TRUE;
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)
210 Radios *radios;
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);
223 else
225 const guchar *path;
226 guchar *tmp;
227 DropBox *drop_box;
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))
238 if (unlink(path))
239 delayed_error(_("Can't delete '%s':\n%s"),
240 path, g_strerror(errno));
241 else
243 dir_check_this(pathname);
244 icons_may_update(pathname);
247 g_free(tmp);
250 full_refresh();
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)
259 struct stat info;
260 GtkDialog *dialog;
261 GtkWidget *frame;
262 Radios *radios;
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);
279 #if 0
280 radios_add(radios,
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);
285 #endif
287 radios_add(radios,
288 _("Use a copy of the image for all files of this MIME "
289 "type."), SET_TYPE,
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);
295 radios_add(radios,
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))
306 radios_add(radios,
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,
333 NULL);
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);
350 DropBox *drop_box;
351 const guchar *path;
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))
365 case SET_MEDIA:
367 char *path, *type;
369 type = g_strconcat(mime_type->media_type, ".png", NULL);
370 path = choices_find_xdg_path_load(type, "MIME-icons",
371 SITE);
372 g_free(type);
373 drop_box_set_path(drop_box, path);
374 g_free(path);
375 break;
377 case SET_TYPE:
379 char *path, *type;
381 type = g_strconcat(mime_type->media_type, "_",
382 mime_type->subtype, ".png", NULL);
383 path = choices_find_xdg_path_load(type, "MIME-icons",
384 SITE);
385 g_free(type);
386 drop_box_set_path(drop_box, path);
387 g_free(path);
388 break;
390 case SET_PATH:
392 const char *gi;
393 gi = g_hash_table_lookup(glob_icons, path);
394 drop_box_set_path(drop_box, gi);
395 break;
397 case SET_COPY:
399 const char *diricon;
400 diricon = make_path(path, ".DirIcon");
401 if (file_exists(diricon))
402 drop_box_set_path(drop_box, diricon);
403 else
404 drop_box_set_path(drop_box, NULL);
405 break;
407 default:
408 drop_box_set_path(drop_box, NULL);
409 break;
413 static gboolean free_globicon(gpointer key, gpointer value, gpointer data)
415 g_free(key);
416 g_free(value);
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;
424 xmlNodePtr tree;
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);
439 if (!save)
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));
455 g_free(save_new);
456 g_free(save);
458 if (doc)
459 xmlFreeDoc(doc);
462 /* Process a globicon line. Format:
463 glob-pattern icon-path
464 Example:
465 /home/<*>/Mail /usr/local/icons/mailbox.xpm
466 (<*> represents a single asterisk, enclosed in brackets to not break
467 the C comment).
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 */
475 if (pattern == NULL)
476 return NULL;
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));
486 return NULL;
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
492 * other entries).
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 */
499 write_globicons();
501 /* Make sure any visible icons for the file are updated */
502 examine(path);
505 /* Remove the globicon for a certain path */
506 void delete_globicon(const gchar *path)
508 gpointer key, value;
510 if (!g_hash_table_lookup_extended(glob_icons, path, &key, &value))
511 return;
513 g_hash_table_remove(glob_icons, path);
515 g_free(key);
516 g_free(value);
518 write_globicons();
519 examine(path);
522 /* Set the icon for this dialog's file to 'icon' */
523 static void do_set_icon(GtkWidget *dialog, const gchar *icon)
525 guchar *path = NULL;
526 Radios *radios;
527 int op;
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);
537 if (op == SET_PATH)
539 if (!set_icon_path(path, icon))
540 return;
542 else if (op == SET_COPY)
544 if (!create_diricon(path, icon))
545 return;
547 else
549 gboolean just_media = (op == SET_MEDIA);
550 MIME_type *type;
552 type = g_object_get_data(G_OBJECT(dialog), "mime-type");
554 if (!set_icon_for_type(type, icon, just_media))
555 return;
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,
565 const guchar *path,
566 GtkWidget *dialog)
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,
573 gboolean just_media)
575 gchar *target;
576 gchar *leaf;
578 if (just_media)
579 leaf = g_strconcat(type->media_type, ".png", NULL);
580 else
581 leaf = g_strconcat(type->media_type, "_", type->subtype,
582 ".png", NULL);
584 target = choices_find_xdg_path_save(leaf, "MIME-icons", SITE, TRUE);
585 g_free(leaf);
587 if (!target)
589 delayed_error(_("Setting icon disabled by CHOICESPATH"));
590 return FALSE;
593 if (!convert_to_png(iconpath, target))
595 g_free(target);
596 return FALSE;
599 g_free(target);
601 full_refresh();
603 return TRUE;
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)
611 MaskedPixmap *pic;
612 GError *error = NULL;
614 pic = g_fscache_lookup(pixmap_cache, src);
615 if (!pic)
617 delayed_error(
618 _("Unable to load image file -- maybe it's not in a "
619 "format I understand, or maybe the permissions are "
620 "wrong?\n"
621 "The icon has not been changed."));
622 return FALSE;
625 gdk_pixbuf_save(pic->src_pixbuf, dest,
626 "png", &error,
627 "tEXt::Software", PROJECT,
628 NULL);
629 g_object_unref(pic);
631 if (error)
633 delayed_error(_("Error creating image '%s':\n%s"),
634 dest, error->message);
635 g_error_free(error);
636 return FALSE;
639 return TRUE;