4 * Copyright (C) 2008 The Geeqie Team
6 * Author: John Ellis, Laurent Monin
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
21 #include "secure_save.h"
22 #include "ui_fileops.h"
25 #include "filefilter.h"
33 #define COMMENT_KEY "Xmp.dc.description"
34 #define KEYWORD_KEY "Xmp.dc.subject"
36 static gboolean metadata_write_queue_idle_cb(gpointer data);
37 static gint metadata_legacy_write(FileData *fd);
38 static gint metadata_legacy_delete(FileData *fd);
42 gboolean metadata_can_write_directly(FileData *fd)
44 return (filter_file_class(fd->extension, FORMAT_CLASS_IMAGE));
45 /* FIXME: detect what exiv2 really supports */
48 gboolean metadata_can_write_sidecar(FileData *fd)
50 return (filter_file_class(fd->extension, FORMAT_CLASS_RAWIMAGE));
51 /* FIXME: detect what exiv2 really supports */
56 *-------------------------------------------------------------------
58 *-------------------------------------------------------------------
61 static GList *metadata_write_queue = NULL;
62 static gint metadata_write_idle_id = -1;
64 static FileData *metadata_xmp_sidecar_fd(FileData *fd)
67 gchar *base, *new_name;
70 if (!metadata_can_write_sidecar(fd)) return NULL;
73 if (fd->parent) fd = fd->parent;
75 if (filter_file_class(fd->extension, FORMAT_CLASS_META))
76 return file_data_ref(fd);
78 work = fd->sidecar_files;
81 FileData *sfd = work->data;
83 if (filter_file_class(sfd->extension, FORMAT_CLASS_META))
84 return file_data_ref(sfd);
87 /* sidecar does not exist yet */
88 base = remove_extension_from_path(fd->path);
89 new_name = g_strconcat(base, ".xmp", NULL);
91 ret = file_data_new_simple(new_name);
96 static FileData *metadata_xmp_main_fd(FileData *fd)
98 if (filter_file_class(fd->extension, FORMAT_CLASS_META) && !g_list_find(metadata_write_queue, fd))
100 /* fd is a sidecar, we have to find the original file */
102 GList *work = metadata_write_queue;
105 FileData *ofd = work->data;
106 FileData *osfd = metadata_xmp_sidecar_fd(ofd);
108 file_data_unref(osfd);
111 return ofd; /* this is the main file */
119 static void metadata_write_queue_add(FileData *fd)
121 if (g_list_find(metadata_write_queue, fd)) return;
123 metadata_write_queue = g_list_prepend(metadata_write_queue, fd);
126 if (metadata_write_idle_id != -1)
128 g_source_remove(metadata_write_idle_id);
129 metadata_write_idle_id = -1;
132 if (options->metadata.confirm_timeout > 0)
134 metadata_write_idle_id = g_timeout_add(options->metadata.confirm_timeout * 1000, metadata_write_queue_idle_cb, NULL);
139 gboolean metadata_write_queue_remove(FileData *fd)
141 FileData *main_fd = metadata_xmp_main_fd(fd);
143 if (main_fd) fd = main_fd;
145 g_hash_table_destroy(fd->modified_xmp);
146 fd->modified_xmp = NULL;
148 metadata_write_queue = g_list_remove(metadata_write_queue, fd);
150 file_data_increment_version(fd);
151 file_data_send_notification(fd, NOTIFY_TYPE_REREAD);
157 gboolean metadata_write_queue_remove_list(GList *list)
165 FileData *fd = work->data;
167 ret = ret && metadata_write_queue_remove(fd);
173 gboolean metadata_write_queue_confirm()
176 GList *to_approve = NULL;
178 work = metadata_write_queue;
181 FileData *fd = work->data;
184 if (fd->change) continue; /* another operation in progress, skip this file for now */
186 FileData *to_approve_fd = metadata_xmp_sidecar_fd(fd);
188 if (!to_approve_fd) to_approve_fd = file_data_ref(fd); /* this is not a sidecar */
190 to_approve = g_list_prepend(to_approve, to_approve_fd);
193 file_util_write_metadata(NULL, to_approve, NULL);
195 filelist_free(to_approve);
197 return (metadata_write_queue != NULL);
200 static gboolean metadata_write_queue_idle_cb(gpointer data)
202 metadata_write_queue_confirm();
203 metadata_write_idle_id = -1;
208 gboolean metadata_write_exif(FileData *fd, FileData *sfd)
213 /* we can either use cached metadata which have fd->modified_xmp already applied
214 or read metadata from file and apply fd->modified_xmp
215 metadata are read also if the file was modified meanwhile */
216 exif = exif_read_fd(fd);
217 if (!exif) return FALSE;
218 success = sfd ? exif_write_sidecar(exif, sfd->path) : exif_write(exif); /* write modified metadata */
219 exif_free_fd(fd, exif);
223 gboolean metadata_write_perform(FileData *fd)
225 FileData *sfd = NULL;
226 FileData *main_fd = metadata_xmp_main_fd(fd);
234 if (options->metadata.save_in_image_file &&
235 metadata_write_exif(fd, sfd))
237 metadata_legacy_delete(fd);
238 if (sfd) metadata_legacy_delete(sfd);
242 metadata_legacy_write(fd);
247 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
249 if (!fd->modified_xmp)
251 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
253 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
256 exif_update_metadata(fd->exif, key, values);
258 metadata_write_queue_add(fd);
262 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
264 return metadata_write_list(fd, key, g_list_append(NULL, g_strdup(value)));
269 *-------------------------------------------------------------------
270 * keyword / comment read/write
271 *-------------------------------------------------------------------
274 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
277 GList *keywords = g_hash_table_lookup(modified_xmp, KEYWORD_KEY);
278 GList *comment_l = g_hash_table_lookup(modified_xmp, COMMENT_KEY);
279 gchar *comment = comment_l ? comment_l->data : NULL;
281 ssi = secure_open(path);
282 if (!ssi) return FALSE;
284 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
286 secure_fprintf(ssi, "[keywords]\n");
287 while (keywords && secsave_errno == SS_ERR_NONE)
289 const gchar *word = keywords->data;
290 keywords = keywords->next;
292 secure_fprintf(ssi, "%s\n", word);
294 secure_fputc(ssi, '\n');
296 secure_fprintf(ssi, "[comment]\n");
297 secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
299 secure_fprintf(ssi, "#end\n");
301 return (secure_close(ssi) == 0);
304 static gint metadata_legacy_write(FileData *fd)
306 gchar *metadata_path;
307 gint success = FALSE;
309 /* If an existing metadata file exists, we will try writing to
310 * it's location regardless of the user's preference.
312 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
313 if (metadata_path && !access_file(metadata_path, W_OK))
315 g_free(metadata_path);
316 metadata_path = NULL;
324 metadata_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
325 if (recursive_mkdir_if_not_exists(metadata_dir, mode))
327 gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
329 metadata_path = g_build_filename(metadata_dir, filename, NULL);
332 g_free(metadata_dir);
337 gchar *metadata_pathl;
339 DEBUG_1("Saving comment: %s", metadata_path);
341 metadata_pathl = path_from_utf8(metadata_path);
343 success = metadata_file_write(metadata_pathl, fd->modified_xmp);
345 g_free(metadata_pathl);
346 g_free(metadata_path);
352 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
356 MetadataKey key = MK_NONE;
358 GString *comment_build = NULL;
360 f = fopen(path, "r");
361 if (!f) return FALSE;
363 while (fgets(s_buf, sizeof(s_buf), f))
367 if (*ptr == '#') continue;
368 if (*ptr == '[' && key != MK_COMMENT)
370 gchar *keystr = ++ptr;
373 while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
378 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
380 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
392 while (*ptr != '\n' && *ptr != '\0') ptr++;
394 if (strlen(s_buf) > 0)
396 gchar *kw = utf8_validate_or_convert(s_buf);
398 list = g_list_prepend(list, kw);
403 if (!comment_build) comment_build = g_string_new("");
404 g_string_append(comment_build, s_buf);
411 *keywords = g_list_reverse(list);
417 gchar *ptr = comment_build->str;
419 /* strip leading and trailing newlines */
420 while (*ptr == '\n') ptr++;
422 while (len > 0 && ptr[len - 1] == '\n') len--;
423 if (ptr[len] == '\n') len++; /* keep the last one */
426 gchar *text = g_strndup(ptr, len);
428 *comment = utf8_validate_or_convert(text);
432 g_string_free(comment_build, TRUE);
438 static gint metadata_legacy_delete(FileData *fd)
440 gchar *metadata_path;
441 gchar *metadata_pathl;
442 gint success = FALSE;
443 if (!fd) return FALSE;
445 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
446 if (!metadata_path) return FALSE;
448 metadata_pathl = path_from_utf8(metadata_path);
450 success = !unlink(metadata_pathl);
452 g_free(metadata_pathl);
453 g_free(metadata_path);
458 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
460 gchar *metadata_path;
461 gchar *metadata_pathl;
462 gint success = FALSE;
463 if (!fd) return FALSE;
465 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
466 if (!metadata_path) return FALSE;
468 metadata_pathl = path_from_utf8(metadata_path);
470 success = metadata_file_read(metadata_pathl, keywords, comment);
472 g_free(metadata_pathl);
473 g_free(metadata_path);
478 static GList *remove_duplicate_strings_from_list(GList *list)
481 GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
482 GList *newlist = NULL;
486 gchar *key = work->data;
488 if (g_hash_table_lookup(hashtable, key) == NULL)
490 g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
491 newlist = g_list_prepend(newlist, key);
496 g_hash_table_destroy(hashtable);
499 return g_list_reverse(newlist);
503 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
507 exif = exif_read_fd(fd);
508 if (!exif) return FALSE;
513 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
515 text = exif_item_get_string(item, 0);
516 *comment = utf8_validate_or_convert(text);
526 item = exif_get_item(exif, KEYWORD_KEY);
527 for (i = 0; i < exif_item_get_elements(item); i++)
529 gchar *kw = exif_item_get_string(item, i);
534 utf8_kw = utf8_validate_or_convert(kw);
535 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
540 * Exiv2 handles Iptc keywords as multiple entries with the
541 * same key, thus exif_get_item returns only the first keyword
542 * and the only way to get all keywords is to iterate through
545 for (item = exif_get_first_item(exif);
547 item = exif_get_next_item(exif))
551 tag = exif_item_get_tag_id(item);
554 gchar *tag_name = exif_item_get_tag_name(item);
556 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
561 kw = exif_item_get_data_as_text(item);
564 utf8_kw = utf8_validate_or_convert(kw);
565 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
573 exif_free_fd(fd, exif);
575 return (comment && *comment) || (keywords && *keywords);
578 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
581 gint write_comment = (comment && comment[0]);
583 if (!fd) return FALSE;
585 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment);
586 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords));
588 if (options->metadata.sync_grouped_files)
590 GList *work = fd->sidecar_files;
594 FileData *sfd = work->data;
597 if (filter_file_class(sfd->extension, FORMAT_CLASS_META)) continue;
599 if (write_comment) success = success && metadata_write_string(sfd, COMMENT_KEY, comment);
600 if (keywords) success = success && metadata_write_list(sfd, KEYWORD_KEY, string_list_copy(keywords));
607 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
609 GList *keywords_xmp = NULL;
610 GList *keywords_legacy = NULL;
611 gchar *comment_xmp = NULL;
612 gchar *comment_legacy = NULL;
613 gint result_xmp, result_legacy;
615 if (!fd) return FALSE;
617 result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
618 result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
620 if (!result_xmp && !result_legacy)
627 if (result_xmp && result_legacy)
628 *keywords = g_list_concat(keywords_xmp, keywords_legacy);
630 *keywords = result_xmp ? keywords_xmp : keywords_legacy;
632 *keywords = remove_duplicate_strings_from_list(*keywords);
636 if (result_xmp) string_list_free(keywords_xmp);
637 if (result_legacy) string_list_free(keywords_legacy);
643 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
644 *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
646 *comment = result_xmp ? comment_xmp : comment_legacy;
649 if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
650 if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
652 // return FALSE in the following cases:
653 // - only looking for a comment and didn't find one
654 // - only looking for keywords and didn't find any
655 // - looking for either a comment or keywords, but found nothing
656 if ((!keywords && comment && !*comment) ||
657 (!comment && keywords && !*keywords) ||
658 ( comment && !*comment && keywords && !*keywords))
664 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
666 gchar *comment = NULL;
667 GList *keywords = NULL;
668 GList *keywords_list = NULL;
670 metadata_read(fd, &keywords, &comment);
674 if (append && comment && *comment)
676 gchar *tmp = comment;
678 comment = g_strconcat(tmp, new_comment, NULL);
684 comment = g_strdup(new_comment);
690 if (append && keywords && g_list_length(keywords) > 0)
706 gchar *needle = p->data;
709 if (strcmp(needle, key) == 0) key = NULL;
712 if (key) keywords = g_list_append(keywords, g_strdup(key));
714 keywords_list = keywords;
718 keywords_list = new_keywords;
722 metadata_write(fd, keywords_list, comment);
724 string_list_free(keywords);
728 gboolean find_string_in_list(GList *list, const gchar *string)
732 gchar *haystack = list->data;
734 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
742 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
744 GList *string_to_keywords_list(const gchar *text)
747 const gchar *ptr = text;
754 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
756 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
762 /* trim starting and ending whitespaces */
763 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
764 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
768 gchar *keyword = g_strndup(begin, l);
770 /* only add if not already in the list */
771 if (find_string_in_list(list, keyword) == FALSE)
772 list = g_list_append(list, keyword);
781 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */