r4567: Get ATK headers through Zero Install.
[rox-filer/translations.git] / ROX-Filer / src / usericons.c
blob6eeb8c23e7995ef10756391512f0d33cee7d3742
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 add_globicon(const gchar *path, const gchar *icon);
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 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 */
80 void read_globicons()
82 static time_t last_read = (time_t) 0;
83 struct stat info;
84 guchar *path;
85 xmlDocPtr doc;
87 if (!glob_icons)
88 glob_icons = g_hash_table_new(g_str_hash, g_str_equal);
90 path = choices_find_xdg_path_load("globicons", PROJECT, SITE);
91 if (!path)
92 return; /* Nothing to load */
94 if (mc_stat(path, &info))
95 goto out;
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);
103 if (doc)
105 xmlNodePtr node, icon, root;
106 char *match;
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)
116 continue;
117 if (strcmp(node->name, "rule") != 0)
118 continue;
119 icon = get_subnode(node, NULL, "icon");
120 if (!icon)
121 continue;
122 match = xmlGetProp(node, "match");
123 if (!match)
124 continue;
126 icon_path = xmlNodeGetContent(icon);
127 path = expand_path(match);
128 g_hash_table_insert(glob_icons, path, icon_path);
129 g_free(match);
132 xmlFreeDoc(doc);
134 else
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 */
143 out:
144 g_free(path);
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)
152 gchar *gi;
154 g_return_if_fail(item && !item->_image);
156 gi = g_hash_table_lookup(glob_icons, path);
157 if (gi)
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")))
164 return FALSE;
166 dir_check_this(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;
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)
217 const guchar *path;
219 path = g_object_get_data(G_OBJECT(dialog), "pathname");
220 g_return_if_fail(path != NULL);
222 delete_globicon(path);
224 else
226 const guchar *path;
227 guchar *tmp;
228 DropBox *drop_box;
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))
239 if (unlink(path))
240 delayed_error(_("Can't delete '%s':\n%s"),
241 path, g_strerror(errno));
243 g_free(tmp);
246 full_refresh();
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)
255 struct stat info;
256 GtkDialog *dialog;
257 GtkWidget *frame;
258 Radios *radios;
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);
275 #if 0
276 radios_add(radios,
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);
281 #endif
283 radios_add(radios,
284 _("Use a copy of the image for all files of this MIME "
285 "type."), SET_TYPE,
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);
291 radios_add(radios,
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))
302 radios_add(radios,
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,
329 NULL);
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);
346 DropBox *drop_box;
347 const guchar *path;
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))
361 case SET_MEDIA:
363 char *path, *type;
365 type = g_strconcat(mime_type->media_type, ".png", NULL);
366 path = choices_find_xdg_path_load(type, "MIME-icons",
367 SITE);
368 g_free(type);
369 drop_box_set_path(drop_box, path);
370 g_free(path);
371 break;
373 case SET_TYPE:
375 char *path, *type;
377 type = g_strconcat(mime_type->media_type, "_",
378 mime_type->subtype, ".png", NULL);
379 path = choices_find_xdg_path_load(type, "MIME-icons",
380 SITE);
381 g_free(type);
382 drop_box_set_path(drop_box, path);
383 g_free(path);
384 break;
386 case SET_PATH:
388 const char *gi;
389 gi = g_hash_table_lookup(glob_icons, path);
390 drop_box_set_path(drop_box, gi);
391 break;
393 case SET_COPY:
395 const char *diricon;
396 diricon = make_path(path, ".DirIcon");
397 if (file_exists(diricon))
398 drop_box_set_path(drop_box, diricon);
399 else
400 drop_box_set_path(drop_box, NULL);
401 break;
403 default:
404 drop_box_set_path(drop_box, NULL);
405 break;
409 static gboolean free_globicon(gpointer key, gpointer value, gpointer data)
411 g_free(key);
412 g_free(value);
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;
420 xmlNodePtr tree;
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);
435 if (!save)
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));
451 g_free(save_new);
452 g_free(save);
454 if (doc)
455 xmlFreeDoc(doc);
458 /* Process a globicon line. Format:
459 glob-pattern icon-path
460 Example:
461 /home/<*>/Mail /usr/local/icons/mailbox.xpm
462 (<*> represents a single asterisk, enclosed in brackets to not break
463 the C comment).
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 */
471 if (pattern == NULL)
472 return NULL;
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));
482 return NULL;
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
488 * other entries).
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 */
495 write_globicons();
497 /* Make sure any visible icons for the file are updated */
498 examine(path);
501 /* Remove the globicon for a certain path */
502 static void delete_globicon(const gchar *path)
504 gpointer key, value;
506 if (!g_hash_table_lookup_extended(glob_icons, path, &key, &value))
507 return;
509 g_hash_table_remove(glob_icons, path);
511 g_free(key);
512 g_free(value);
514 write_globicons();
515 examine(path);
518 /* Set the icon for this dialog's file to 'icon' */
519 static void do_set_icon(GtkWidget *dialog, const gchar *icon)
521 guchar *path = NULL;
522 Radios *radios;
523 int op;
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);
533 if (op == SET_PATH)
535 if (!set_icon_path(path, icon))
536 return;
538 else if (op == SET_COPY)
540 if (!create_diricon(path, icon))
541 return;
543 else
545 gboolean just_media = (op == SET_MEDIA);
546 MIME_type *type;
548 type = g_object_get_data(G_OBJECT(dialog), "mime-type");
550 if (!set_icon_for_type(type, icon, just_media))
551 return;
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,
561 const guchar *path,
562 GtkWidget *dialog)
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,
569 gboolean just_media)
571 gchar *target;
572 gchar *leaf;
574 if (just_media)
575 leaf = g_strconcat(type->media_type, ".png", NULL);
576 else
577 leaf = g_strconcat(type->media_type, "_", type->subtype,
578 ".png", NULL);
580 target = choices_find_xdg_path_save(leaf, "MIME-icons", SITE, TRUE);
581 g_free(leaf);
583 if (!target)
585 delayed_error(_("Setting icon disabled by CHOICESPATH"));
586 return FALSE;
589 if (!convert_to_png(iconpath, target))
591 g_free(target);
592 return FALSE;
595 g_free(target);
597 full_refresh();
599 return TRUE;
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)
607 MaskedPixmap *pic;
608 GError *error = NULL;
610 pic = g_fscache_lookup(pixmap_cache, src);
611 if (!pic)
613 delayed_error(
614 _("Unable to load image file -- maybe it's not in a "
615 "format I understand, or maybe the permissions are "
616 "wrong?\n"
617 "The icon has not been changed."));
618 return FALSE;
621 gdk_pixbuf_save(pic->src_pixbuf, dest,
622 "png", &error,
623 "tEXt::Software", PROJECT,
624 NULL);
625 g_object_unref(pic);
627 if (error)
629 delayed_error(_("Error creating image '%s':\n%s"),
630 dest, error->message);
631 g_error_free(error);
632 return FALSE;
635 return TRUE;