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 void metadata_legacy_delete(FileData *fd, const gchar *except);
43 *-------------------------------------------------------------------
45 *-------------------------------------------------------------------
48 static GList *metadata_write_queue = NULL;
49 static gint metadata_write_idle_id = -1;
51 static void metadata_write_queue_add(FileData *fd)
53 if (!g_list_find(metadata_write_queue, fd))
55 metadata_write_queue = g_list_prepend(metadata_write_queue, fd);
59 if (metadata_write_idle_id != -1)
61 g_source_remove(metadata_write_idle_id);
62 metadata_write_idle_id = -1;
65 if (options->metadata.confirm_timeout > 0)
67 metadata_write_idle_id = g_timeout_add(options->metadata.confirm_timeout * 1000, metadata_write_queue_idle_cb, NULL);
72 gboolean metadata_write_queue_remove(FileData *fd)
74 g_hash_table_destroy(fd->modified_xmp);
75 fd->modified_xmp = NULL;
77 metadata_write_queue = g_list_remove(metadata_write_queue, fd);
79 file_data_increment_version(fd);
80 file_data_send_notification(fd, NOTIFY_TYPE_REREAD);
86 gboolean metadata_write_queue_remove_list(GList *list)
94 FileData *fd = work->data;
96 ret = ret && metadata_write_queue_remove(fd);
102 gboolean metadata_write_queue_confirm()
105 GList *to_approve = NULL;
107 work = metadata_write_queue;
110 FileData *fd = work->data;
113 if (fd->change) continue; /* another operation in progress, skip this file for now */
115 to_approve = g_list_prepend(to_approve, fd);
118 file_util_write_metadata(NULL, to_approve, NULL);
120 filelist_free(to_approve);
122 return (metadata_write_queue != NULL);
125 static gboolean metadata_write_queue_idle_cb(gpointer data)
127 metadata_write_queue_confirm();
128 metadata_write_idle_id = -1;
132 gboolean metadata_write_perform(FileData *fd)
137 g_assert(fd->change);
139 if (fd->change->dest &&
140 strcmp(extension_from_path(fd->path), GQ_CACHE_EXT_METADATA) == 0)
142 success = metadata_legacy_write(fd);
143 if (success) metadata_legacy_delete(fd, fd->change->dest);
147 /* write via exiv2 */
148 /* we can either use cached metadata which have fd->modified_xmp already applied
149 or read metadata from file and apply fd->modified_xmp
150 metadata are read also if the file was modified meanwhile */
151 exif = exif_read_fd(fd);
152 if (!exif) return FALSE;
154 success = (fd->change->dest) ? exif_write_sidecar(exif, fd->change->dest) : exif_write(exif); /* write modified metadata */
155 exif_free_fd(fd, exif);
157 if (success) metadata_legacy_delete(fd, fd->change->dest);
161 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
163 if (!fd->modified_xmp)
165 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
167 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
170 exif_update_metadata(fd->exif, key, values);
172 metadata_write_queue_add(fd);
173 file_data_increment_version(fd);
174 file_data_send_notification(fd, NOTIFY_TYPE_INTERNAL);
179 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
181 return metadata_write_list(fd, key, g_list_append(NULL, g_strdup(value)));
186 *-------------------------------------------------------------------
187 * keyword / comment read/write
188 *-------------------------------------------------------------------
191 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
194 GList *keywords = g_hash_table_lookup(modified_xmp, KEYWORD_KEY);
195 GList *comment_l = g_hash_table_lookup(modified_xmp, COMMENT_KEY);
196 gchar *comment = comment_l ? comment_l->data : NULL;
198 ssi = secure_open(path);
199 if (!ssi) return FALSE;
201 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
203 secure_fprintf(ssi, "[keywords]\n");
204 while (keywords && secsave_errno == SS_ERR_NONE)
206 const gchar *word = keywords->data;
207 keywords = keywords->next;
209 secure_fprintf(ssi, "%s\n", word);
211 secure_fputc(ssi, '\n');
213 secure_fprintf(ssi, "[comment]\n");
214 secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
216 secure_fprintf(ssi, "#end\n");
218 return (secure_close(ssi) == 0);
221 static gint metadata_legacy_write(FileData *fd)
223 gint success = FALSE;
225 g_assert(fd->change && fd->change->dest);
226 gchar *metadata_pathl;
228 DEBUG_1("Saving comment: %s", fd->change->dest);
230 metadata_pathl = path_from_utf8(fd->change->dest);
232 success = metadata_file_write(metadata_pathl, fd->modified_xmp);
234 g_free(metadata_pathl);
239 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
243 MetadataKey key = MK_NONE;
245 GString *comment_build = NULL;
247 f = fopen(path, "r");
248 if (!f) return FALSE;
250 while (fgets(s_buf, sizeof(s_buf), f))
254 if (*ptr == '#') continue;
255 if (*ptr == '[' && key != MK_COMMENT)
257 gchar *keystr = ++ptr;
260 while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
265 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
267 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
279 while (*ptr != '\n' && *ptr != '\0') ptr++;
281 if (strlen(s_buf) > 0)
283 gchar *kw = utf8_validate_or_convert(s_buf);
285 list = g_list_prepend(list, kw);
290 if (!comment_build) comment_build = g_string_new("");
291 g_string_append(comment_build, s_buf);
298 *keywords = g_list_reverse(list);
304 gchar *ptr = comment_build->str;
306 /* strip leading and trailing newlines */
307 while (*ptr == '\n') ptr++;
309 while (len > 0 && ptr[len - 1] == '\n') len--;
310 if (ptr[len] == '\n') len++; /* keep the last one */
313 gchar *text = g_strndup(ptr, len);
315 *comment = utf8_validate_or_convert(text);
319 g_string_free(comment_build, TRUE);
325 static void metadata_legacy_delete(FileData *fd, const gchar *except)
327 gchar *metadata_path;
328 gchar *metadata_pathl;
331 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
332 if (metadata_path && (!except || strcmp(metadata_path, except) != 0))
334 metadata_pathl = path_from_utf8(metadata_path);
335 unlink(metadata_pathl);
336 g_free(metadata_pathl);
337 g_free(metadata_path);
339 metadata_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path);
340 if (metadata_path && (!except || strcmp(metadata_path, except) != 0))
342 metadata_pathl = path_from_utf8(metadata_path);
343 unlink(metadata_pathl);
344 g_free(metadata_pathl);
345 g_free(metadata_path);
349 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
351 gchar *metadata_path;
352 gchar *metadata_pathl;
353 gint success = FALSE;
354 if (!fd) return FALSE;
356 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
357 if (!metadata_path) return FALSE;
359 metadata_pathl = path_from_utf8(metadata_path);
361 success = metadata_file_read(metadata_pathl, keywords, comment);
363 g_free(metadata_pathl);
364 g_free(metadata_path);
369 static GList *remove_duplicate_strings_from_list(GList *list)
372 GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
373 GList *newlist = NULL;
377 gchar *key = work->data;
379 if (g_hash_table_lookup(hashtable, key) == NULL)
381 g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
382 newlist = g_list_prepend(newlist, key);
387 g_hash_table_destroy(hashtable);
390 return g_list_reverse(newlist);
394 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
398 exif = exif_read_fd(fd);
399 if (!exif) return FALSE;
404 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
406 text = exif_item_get_string(item, 0);
407 *comment = utf8_validate_or_convert(text);
417 item = exif_get_item(exif, KEYWORD_KEY);
418 for (i = 0; i < exif_item_get_elements(item); i++)
420 gchar *kw = exif_item_get_string(item, i);
425 utf8_kw = utf8_validate_or_convert(kw);
426 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
431 * Exiv2 handles Iptc keywords as multiple entries with the
432 * same key, thus exif_get_item returns only the first keyword
433 * and the only way to get all keywords is to iterate through
436 /* Read IPTC keywords only if there are no XMP keywords
437 * IPTC does not have standard charset, thus the encoding may differ
438 * from XMP and keyword merging is not reliable.
442 for (item = exif_get_first_item(exif);
444 item = exif_get_next_item(exif))
448 tag = exif_item_get_tag_id(item);
451 gchar *tag_name = exif_item_get_tag_name(item);
453 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
458 kw = exif_item_get_data_as_text(item);
461 utf8_kw = utf8_validate_or_convert(kw);
462 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
471 exif_free_fd(fd, exif);
473 return (comment && *comment) || (keywords && *keywords);
476 gint metadata_write(FileData *fd, GList **keywords, gchar **comment)
479 gint write_comment = (comment && *comment);
481 if (!fd) return FALSE;
483 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, *comment);
484 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(*keywords));
486 if (options->metadata.sync_grouped_files)
488 GList *work = fd->sidecar_files;
492 FileData *sfd = work->data;
495 if (filter_file_class(sfd->extension, FORMAT_CLASS_META)) continue;
497 if (write_comment) success = success && metadata_write_string(sfd, COMMENT_KEY, *comment);
498 if (keywords) success = success && metadata_write_list(sfd, KEYWORD_KEY, string_list_copy(*keywords));
505 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
507 GList *keywords_xmp = NULL;
508 GList *keywords_legacy = NULL;
509 gchar *comment_xmp = NULL;
510 gchar *comment_legacy = NULL;
511 gint result_xmp, result_legacy;
513 if (!fd) return FALSE;
515 result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
516 result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
518 if (!result_xmp && !result_legacy)
525 if (result_xmp && result_legacy)
526 *keywords = g_list_concat(keywords_xmp, keywords_legacy);
528 *keywords = result_xmp ? keywords_xmp : keywords_legacy;
530 *keywords = remove_duplicate_strings_from_list(*keywords);
534 if (result_xmp) string_list_free(keywords_xmp);
535 if (result_legacy) string_list_free(keywords_legacy);
541 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
542 *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
544 *comment = result_xmp ? comment_xmp : comment_legacy;
547 if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
548 if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
550 // return FALSE in the following cases:
551 // - only looking for a comment and didn't find one
552 // - only looking for keywords and didn't find any
553 // - looking for either a comment or keywords, but found nothing
554 if ((!keywords && comment && !*comment) ||
555 (!comment && keywords && !*keywords) ||
556 ( comment && !*comment && keywords && !*keywords))
562 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
564 gchar *comment = NULL;
565 GList *keywords = NULL;
566 GList *keywords_list = NULL;
568 metadata_read(fd, &keywords, &comment);
572 if (append && comment && *comment)
574 gchar *tmp = comment;
576 comment = g_strconcat(tmp, new_comment, NULL);
582 comment = g_strdup(new_comment);
588 if (append && keywords && g_list_length(keywords) > 0)
604 gchar *needle = p->data;
607 if (strcmp(needle, key) == 0) key = NULL;
610 if (key) keywords = g_list_append(keywords, g_strdup(key));
612 keywords_list = keywords;
616 keywords_list = new_keywords;
620 metadata_write(fd, &keywords_list, &comment);
622 string_list_free(keywords);
626 gboolean find_string_in_list(GList *list, const gchar *string)
630 gchar *haystack = list->data;
632 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
640 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
642 GList *string_to_keywords_list(const gchar *text)
645 const gchar *ptr = text;
652 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
654 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
660 /* trim starting and ending whitespaces */
661 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
662 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
666 gchar *keyword = g_strndup(begin, l);
668 /* only add if not already in the list */
669 if (find_string_in_list(list, keyword) == FALSE)
670 list = g_list_append(list, keyword);
684 gboolean meta_data_get_keyword_mark(FileData *fd, gint n, gpointer data)
687 gboolean found = FALSE;
688 if (metadata_read(fd, &keywords, NULL))
690 GList *work = keywords;
694 gchar *kw = work->data;
697 if (strcmp(kw, data) == 0)
703 string_list_free(keywords);
708 gboolean meta_data_set_keyword_mark(FileData *fd, gint n, gboolean value, gpointer data)
710 GList *keywords = NULL;
711 gboolean found = FALSE;
712 gboolean changed = FALSE;
714 metadata_read(fd, &keywords, NULL);
720 gchar *kw = work->data;
722 if (strcmp(kw, data) == 0)
728 keywords = g_list_delete_link(keywords, work);
738 keywords = g_list_append(keywords, g_strdup(data));
741 if (changed) metadata_write(fd, &keywords, NULL);
743 string_list_free(keywords);
748 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */