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))
123 metadata_write_queue = g_list_prepend(metadata_write_queue, fd);
127 if (metadata_write_idle_id != -1)
129 g_source_remove(metadata_write_idle_id);
130 metadata_write_idle_id = -1;
133 if (options->metadata.confirm_timeout > 0)
135 metadata_write_idle_id = g_timeout_add(options->metadata.confirm_timeout * 1000, metadata_write_queue_idle_cb, NULL);
140 gboolean metadata_write_queue_remove(FileData *fd)
142 FileData *main_fd = metadata_xmp_main_fd(fd);
144 if (main_fd) fd = main_fd;
146 g_hash_table_destroy(fd->modified_xmp);
147 fd->modified_xmp = NULL;
149 metadata_write_queue = g_list_remove(metadata_write_queue, fd);
151 file_data_increment_version(fd);
152 file_data_send_notification(fd, NOTIFY_TYPE_REREAD);
158 gboolean metadata_write_queue_remove_list(GList *list)
166 FileData *fd = work->data;
168 ret = ret && metadata_write_queue_remove(fd);
174 gboolean metadata_write_queue_confirm()
177 GList *to_approve = NULL;
179 work = metadata_write_queue;
182 FileData *fd = work->data;
185 if (fd->change) continue; /* another operation in progress, skip this file for now */
187 FileData *to_approve_fd = metadata_xmp_sidecar_fd(fd);
189 if (!to_approve_fd) to_approve_fd = file_data_ref(fd); /* this is not a sidecar */
191 to_approve = g_list_prepend(to_approve, to_approve_fd);
194 file_util_write_metadata(NULL, to_approve, NULL);
196 filelist_free(to_approve);
198 return (metadata_write_queue != NULL);
201 static gboolean metadata_write_queue_idle_cb(gpointer data)
203 metadata_write_queue_confirm();
204 metadata_write_idle_id = -1;
209 gboolean metadata_write_exif(FileData *fd, FileData *sfd)
214 /* we can either use cached metadata which have fd->modified_xmp already applied
215 or read metadata from file and apply fd->modified_xmp
216 metadata are read also if the file was modified meanwhile */
217 exif = exif_read_fd(fd);
218 if (!exif) return FALSE;
219 success = sfd ? exif_write_sidecar(exif, sfd->path) : exif_write(exif); /* write modified metadata */
220 exif_free_fd(fd, exif);
224 gboolean metadata_write_perform(FileData *fd)
226 FileData *sfd = NULL;
227 FileData *main_fd = metadata_xmp_main_fd(fd);
235 if (options->metadata.save_in_image_file &&
236 metadata_write_exif(fd, sfd))
238 metadata_legacy_delete(fd);
239 if (sfd) metadata_legacy_delete(sfd);
243 metadata_legacy_write(fd);
248 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
250 if (!fd->modified_xmp)
252 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
254 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
257 exif_update_metadata(fd->exif, key, values);
259 metadata_write_queue_add(fd);
260 file_data_increment_version(fd);
261 file_data_send_notification(fd, NOTIFY_TYPE_INTERNAL);
266 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
268 return metadata_write_list(fd, key, g_list_append(NULL, g_strdup(value)));
273 *-------------------------------------------------------------------
274 * keyword / comment read/write
275 *-------------------------------------------------------------------
278 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
281 GList *keywords = g_hash_table_lookup(modified_xmp, KEYWORD_KEY);
282 GList *comment_l = g_hash_table_lookup(modified_xmp, COMMENT_KEY);
283 gchar *comment = comment_l ? comment_l->data : NULL;
285 ssi = secure_open(path);
286 if (!ssi) return FALSE;
288 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
290 secure_fprintf(ssi, "[keywords]\n");
291 while (keywords && secsave_errno == SS_ERR_NONE)
293 const gchar *word = keywords->data;
294 keywords = keywords->next;
296 secure_fprintf(ssi, "%s\n", word);
298 secure_fputc(ssi, '\n');
300 secure_fprintf(ssi, "[comment]\n");
301 secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
303 secure_fprintf(ssi, "#end\n");
305 return (secure_close(ssi) == 0);
308 static gint metadata_legacy_write(FileData *fd)
310 gchar *metadata_path;
311 gint success = FALSE;
313 /* If an existing metadata file exists, we will try writing to
314 * it's location regardless of the user's preference.
316 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
317 if (metadata_path && !access_file(metadata_path, W_OK))
319 g_free(metadata_path);
320 metadata_path = NULL;
328 metadata_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
329 if (recursive_mkdir_if_not_exists(metadata_dir, mode))
331 gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
333 metadata_path = g_build_filename(metadata_dir, filename, NULL);
336 g_free(metadata_dir);
341 gchar *metadata_pathl;
343 DEBUG_1("Saving comment: %s", metadata_path);
345 metadata_pathl = path_from_utf8(metadata_path);
347 success = metadata_file_write(metadata_pathl, fd->modified_xmp);
349 g_free(metadata_pathl);
350 g_free(metadata_path);
356 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
360 MetadataKey key = MK_NONE;
362 GString *comment_build = NULL;
364 f = fopen(path, "r");
365 if (!f) return FALSE;
367 while (fgets(s_buf, sizeof(s_buf), f))
371 if (*ptr == '#') continue;
372 if (*ptr == '[' && key != MK_COMMENT)
374 gchar *keystr = ++ptr;
377 while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
382 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
384 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
396 while (*ptr != '\n' && *ptr != '\0') ptr++;
398 if (strlen(s_buf) > 0)
400 gchar *kw = utf8_validate_or_convert(s_buf);
402 list = g_list_prepend(list, kw);
407 if (!comment_build) comment_build = g_string_new("");
408 g_string_append(comment_build, s_buf);
415 *keywords = g_list_reverse(list);
421 gchar *ptr = comment_build->str;
423 /* strip leading and trailing newlines */
424 while (*ptr == '\n') ptr++;
426 while (len > 0 && ptr[len - 1] == '\n') len--;
427 if (ptr[len] == '\n') len++; /* keep the last one */
430 gchar *text = g_strndup(ptr, len);
432 *comment = utf8_validate_or_convert(text);
436 g_string_free(comment_build, TRUE);
442 static gint metadata_legacy_delete(FileData *fd)
444 gchar *metadata_path;
445 gchar *metadata_pathl;
446 gint success = FALSE;
447 if (!fd) return FALSE;
449 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
450 if (!metadata_path) return FALSE;
452 metadata_pathl = path_from_utf8(metadata_path);
454 success = !unlink(metadata_pathl);
456 g_free(metadata_pathl);
457 g_free(metadata_path);
462 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
464 gchar *metadata_path;
465 gchar *metadata_pathl;
466 gint success = FALSE;
467 if (!fd) return FALSE;
469 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
470 if (!metadata_path) return FALSE;
472 metadata_pathl = path_from_utf8(metadata_path);
474 success = metadata_file_read(metadata_pathl, keywords, comment);
476 g_free(metadata_pathl);
477 g_free(metadata_path);
482 static GList *remove_duplicate_strings_from_list(GList *list)
485 GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
486 GList *newlist = NULL;
490 gchar *key = work->data;
492 if (g_hash_table_lookup(hashtable, key) == NULL)
494 g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
495 newlist = g_list_prepend(newlist, key);
500 g_hash_table_destroy(hashtable);
503 return g_list_reverse(newlist);
507 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
511 exif = exif_read_fd(fd);
512 if (!exif) return FALSE;
517 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
519 text = exif_item_get_string(item, 0);
520 *comment = utf8_validate_or_convert(text);
530 item = exif_get_item(exif, KEYWORD_KEY);
531 for (i = 0; i < exif_item_get_elements(item); i++)
533 gchar *kw = exif_item_get_string(item, i);
538 utf8_kw = utf8_validate_or_convert(kw);
539 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
544 * Exiv2 handles Iptc keywords as multiple entries with the
545 * same key, thus exif_get_item returns only the first keyword
546 * and the only way to get all keywords is to iterate through
549 /* Read IPTC keywords only if there are no XMP keywords
550 * IPTC does not have standard charset, thus the encoding may differ
551 * from XMP and keyword merging is not reliable.
555 for (item = exif_get_first_item(exif);
557 item = exif_get_next_item(exif))
561 tag = exif_item_get_tag_id(item);
564 gchar *tag_name = exif_item_get_tag_name(item);
566 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
571 kw = exif_item_get_data_as_text(item);
574 utf8_kw = utf8_validate_or_convert(kw);
575 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
584 exif_free_fd(fd, exif);
586 return (comment && *comment) || (keywords && *keywords);
589 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
592 gint write_comment = (comment && comment[0]);
594 if (!fd) return FALSE;
596 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment);
597 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords));
599 if (options->metadata.sync_grouped_files)
601 GList *work = fd->sidecar_files;
605 FileData *sfd = work->data;
608 if (filter_file_class(sfd->extension, FORMAT_CLASS_META)) continue;
610 if (write_comment) success = success && metadata_write_string(sfd, COMMENT_KEY, comment);
611 if (keywords) success = success && metadata_write_list(sfd, KEYWORD_KEY, string_list_copy(keywords));
618 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
620 GList *keywords_xmp = NULL;
621 GList *keywords_legacy = NULL;
622 gchar *comment_xmp = NULL;
623 gchar *comment_legacy = NULL;
624 gint result_xmp, result_legacy;
626 if (!fd) return FALSE;
628 result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
629 result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
631 if (!result_xmp && !result_legacy)
638 if (result_xmp && result_legacy)
639 *keywords = g_list_concat(keywords_xmp, keywords_legacy);
641 *keywords = result_xmp ? keywords_xmp : keywords_legacy;
643 *keywords = remove_duplicate_strings_from_list(*keywords);
647 if (result_xmp) string_list_free(keywords_xmp);
648 if (result_legacy) string_list_free(keywords_legacy);
654 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
655 *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
657 *comment = result_xmp ? comment_xmp : comment_legacy;
660 if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
661 if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
663 // return FALSE in the following cases:
664 // - only looking for a comment and didn't find one
665 // - only looking for keywords and didn't find any
666 // - looking for either a comment or keywords, but found nothing
667 if ((!keywords && comment && !*comment) ||
668 (!comment && keywords && !*keywords) ||
669 ( comment && !*comment && keywords && !*keywords))
675 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
677 gchar *comment = NULL;
678 GList *keywords = NULL;
679 GList *keywords_list = NULL;
681 metadata_read(fd, &keywords, &comment);
685 if (append && comment && *comment)
687 gchar *tmp = comment;
689 comment = g_strconcat(tmp, new_comment, NULL);
695 comment = g_strdup(new_comment);
701 if (append && keywords && g_list_length(keywords) > 0)
717 gchar *needle = p->data;
720 if (strcmp(needle, key) == 0) key = NULL;
723 if (key) keywords = g_list_append(keywords, g_strdup(key));
725 keywords_list = keywords;
729 keywords_list = new_keywords;
733 metadata_write(fd, keywords_list, comment);
735 string_list_free(keywords);
739 gboolean find_string_in_list(GList *list, const gchar *string)
743 gchar *haystack = list->data;
745 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
753 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
755 GList *string_to_keywords_list(const gchar *text)
758 const gchar *ptr = text;
765 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
767 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
773 /* trim starting and ending whitespaces */
774 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
775 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
779 gchar *keyword = g_strndup(begin, l);
781 /* only add if not already in the list */
782 if (find_string_in_list(list, keyword) == FALSE)
783 list = g_list_append(list, keyword);
792 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */