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) metadata_write_idle_id = g_idle_add(metadata_write_queue_idle_cb, NULL);
130 gboolean metadata_write_queue_remove(FileData *fd)
132 FileData *main_fd = metadata_xmp_main_fd(fd);
134 if (main_fd) fd = main_fd;
136 g_hash_table_destroy(fd->modified_xmp);
137 fd->modified_xmp = NULL;
139 metadata_write_queue = g_list_remove(metadata_write_queue, fd);
141 file_data_increment_version(fd);
142 file_data_send_notification(fd, NOTIFY_TYPE_REREAD);
148 gboolean metadata_write_queue_remove_list(GList *list)
156 FileData *fd = work->data;
158 ret = ret && metadata_write_queue_remove(fd);
164 static gboolean metadata_write_queue_idle_cb(gpointer data)
167 GList *to_approve = NULL;
169 work = metadata_write_queue;
172 FileData *fd = work->data;
175 if (fd->change) continue; /* another operation in progress, skip this file for now */
177 FileData *to_approve_fd = metadata_xmp_sidecar_fd(fd);
179 if (!to_approve_fd) to_approve_fd = file_data_ref(fd); /* this is not a sidecar */
181 to_approve = g_list_prepend(to_approve, to_approve_fd);
184 file_util_write_metadata(NULL, to_approve, NULL);
186 filelist_free(to_approve);
188 metadata_write_idle_id = -1;
192 gboolean metadata_write_exif(FileData *fd, FileData *sfd)
197 /* we can either use cached metadata which have fd->modified_xmp already applied
198 or read metadata from file and apply fd->modified_xmp
199 metadata are read also if the file was modified meanwhile */
200 exif = exif_read_fd(fd);
201 if (!exif) return FALSE;
202 success = sfd ? exif_write_sidecar(exif, sfd->path) : exif_write(exif); /* write modified metadata */
203 exif_free_fd(fd, exif);
207 gboolean metadata_write_perform(FileData *fd)
209 FileData *sfd = NULL;
210 FileData *main_fd = metadata_xmp_main_fd(fd);
218 if (options->metadata.save_in_image_file &&
219 metadata_write_exif(fd, sfd))
221 metadata_legacy_delete(fd);
222 if (sfd) metadata_legacy_delete(sfd);
226 metadata_legacy_write(fd);
231 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
233 if (!fd->modified_xmp)
235 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
237 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
240 exif_update_metadata(fd->exif, key, values);
242 metadata_write_queue_add(fd);
246 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
248 return metadata_write_list(fd, key, g_list_append(NULL, g_strdup(value)));
253 *-------------------------------------------------------------------
254 * keyword / comment read/write
255 *-------------------------------------------------------------------
258 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
261 GList *keywords = g_hash_table_lookup(modified_xmp, KEYWORD_KEY);
262 GList *comment_l = g_hash_table_lookup(modified_xmp, COMMENT_KEY);
263 gchar *comment = comment_l ? comment_l->data : NULL;
265 ssi = secure_open(path);
266 if (!ssi) return FALSE;
268 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
270 secure_fprintf(ssi, "[keywords]\n");
271 while (keywords && secsave_errno == SS_ERR_NONE)
273 const gchar *word = keywords->data;
274 keywords = keywords->next;
276 secure_fprintf(ssi, "%s\n", word);
278 secure_fputc(ssi, '\n');
280 secure_fprintf(ssi, "[comment]\n");
281 secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
283 secure_fprintf(ssi, "#end\n");
285 return (secure_close(ssi) == 0);
288 static gint metadata_legacy_write(FileData *fd)
290 gchar *metadata_path;
291 gint success = FALSE;
293 /* If an existing metadata file exists, we will try writing to
294 * it's location regardless of the user's preference.
296 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
297 if (metadata_path && !access_file(metadata_path, W_OK))
299 g_free(metadata_path);
300 metadata_path = NULL;
308 metadata_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
309 if (recursive_mkdir_if_not_exists(metadata_dir, mode))
311 gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
313 metadata_path = g_build_filename(metadata_dir, filename, NULL);
316 g_free(metadata_dir);
321 gchar *metadata_pathl;
323 DEBUG_1("Saving comment: %s", metadata_path);
325 metadata_pathl = path_from_utf8(metadata_path);
327 success = metadata_file_write(metadata_pathl, fd->modified_xmp);
329 g_free(metadata_pathl);
330 g_free(metadata_path);
336 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
340 MetadataKey key = MK_NONE;
342 GString *comment_build = NULL;
344 f = fopen(path, "r");
345 if (!f) return FALSE;
347 while (fgets(s_buf, sizeof(s_buf), f))
351 if (*ptr == '#') continue;
352 if (*ptr == '[' && key != MK_COMMENT)
354 gchar *keystr = ++ptr;
357 while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
362 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
364 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
376 while (*ptr != '\n' && *ptr != '\0') ptr++;
378 if (strlen(s_buf) > 0)
380 gchar *kw = utf8_validate_or_convert(s_buf);
382 list = g_list_prepend(list, kw);
387 if (!comment_build) comment_build = g_string_new("");
388 g_string_append(comment_build, s_buf);
395 *keywords = g_list_reverse(list);
401 gchar *ptr = comment_build->str;
403 /* strip leading and trailing newlines */
404 while (*ptr == '\n') ptr++;
406 while (len > 0 && ptr[len - 1] == '\n') len--;
407 if (ptr[len] == '\n') len++; /* keep the last one */
410 gchar *text = g_strndup(ptr, len);
412 *comment = utf8_validate_or_convert(text);
416 g_string_free(comment_build, TRUE);
422 static gint metadata_legacy_delete(FileData *fd)
424 gchar *metadata_path;
425 gchar *metadata_pathl;
426 gint success = FALSE;
427 if (!fd) return FALSE;
429 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
430 if (!metadata_path) return FALSE;
432 metadata_pathl = path_from_utf8(metadata_path);
434 success = !unlink(metadata_pathl);
436 g_free(metadata_pathl);
437 g_free(metadata_path);
442 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
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 = metadata_file_read(metadata_pathl, keywords, comment);
456 g_free(metadata_pathl);
457 g_free(metadata_path);
462 static GList *remove_duplicate_strings_from_list(GList *list)
465 GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
466 GList *newlist = NULL;
470 gchar *key = work->data;
472 if (g_hash_table_lookup(hashtable, key) == NULL)
474 g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
475 newlist = g_list_prepend(newlist, key);
480 g_hash_table_destroy(hashtable);
483 return g_list_reverse(newlist);
487 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
491 exif = exif_read_fd(fd);
492 if (!exif) return FALSE;
497 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
499 text = exif_item_get_string(item, 0);
500 *comment = utf8_validate_or_convert(text);
510 item = exif_get_item(exif, KEYWORD_KEY);
511 for (i = 0; i < exif_item_get_elements(item); i++)
513 gchar *kw = exif_item_get_string(item, i);
518 utf8_kw = utf8_validate_or_convert(kw);
519 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
524 * Exiv2 handles Iptc keywords as multiple entries with the
525 * same key, thus exif_get_item returns only the first keyword
526 * and the only way to get all keywords is to iterate through
529 for (item = exif_get_first_item(exif);
531 item = exif_get_next_item(exif))
535 tag = exif_item_get_tag_id(item);
538 gchar *tag_name = exif_item_get_tag_name(item);
540 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
545 kw = exif_item_get_data_as_text(item);
548 utf8_kw = utf8_validate_or_convert(kw);
549 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
557 exif_free_fd(fd, exif);
559 return (comment && *comment) || (keywords && *keywords);
562 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
565 gint write_comment = (comment && comment[0]);
567 if (!fd) return FALSE;
569 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment);
570 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords));
572 if (options->metadata.sync_grouped_files)
574 GList *work = fd->sidecar_files;
578 FileData *sfd = work->data;
581 if (filter_file_class(sfd->extension, FORMAT_CLASS_META)) continue;
583 if (write_comment) success = success && metadata_write_string(sfd, COMMENT_KEY, comment);
584 if (keywords) success = success && metadata_write_list(sfd, KEYWORD_KEY, string_list_copy(keywords));
591 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
593 GList *keywords_xmp = NULL;
594 GList *keywords_legacy = NULL;
595 gchar *comment_xmp = NULL;
596 gchar *comment_legacy = NULL;
597 gint result_xmp, result_legacy;
599 if (!fd) return FALSE;
601 result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
602 result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
604 if (!result_xmp && !result_legacy)
611 if (result_xmp && result_legacy)
612 *keywords = g_list_concat(keywords_xmp, keywords_legacy);
614 *keywords = result_xmp ? keywords_xmp : keywords_legacy;
616 *keywords = remove_duplicate_strings_from_list(*keywords);
620 if (result_xmp) string_list_free(keywords_xmp);
621 if (result_legacy) string_list_free(keywords_legacy);
627 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
628 *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
630 *comment = result_xmp ? comment_xmp : comment_legacy;
633 if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
634 if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
636 // return FALSE in the following cases:
637 // - only looking for a comment and didn't find one
638 // - only looking for keywords and didn't find any
639 // - looking for either a comment or keywords, but found nothing
640 if ((!keywords && comment && !*comment) ||
641 (!comment && keywords && !*keywords) ||
642 ( comment && !*comment && keywords && !*keywords))
648 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
650 gchar *comment = NULL;
651 GList *keywords = NULL;
652 GList *keywords_list = NULL;
654 metadata_read(fd, &keywords, &comment);
658 if (append && comment && *comment)
660 gchar *tmp = comment;
662 comment = g_strconcat(tmp, new_comment, NULL);
668 comment = g_strdup(new_comment);
674 if (append && keywords && g_list_length(keywords) > 0)
690 gchar *needle = p->data;
693 if (strcmp(needle, key) == 0) key = NULL;
696 if (key) keywords = g_list_append(keywords, g_strdup(key));
698 keywords_list = keywords;
702 keywords_list = new_keywords;
706 metadata_write(fd, keywords_list, comment);
708 string_list_free(keywords);
712 gboolean find_string_in_list(GList *list, const gchar *string)
716 gchar *haystack = list->data;
718 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
726 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
728 GList *string_to_keywords_list(const gchar *text)
731 const gchar *ptr = text;
738 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
740 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
746 /* trim starting and ending whitespaces */
747 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
748 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
752 gchar *keyword = g_strndup(begin, l);
754 /* only add if not already in the list */
755 if (find_string_in_list(list, keyword) == FALSE)
756 list = g_list_append(list, keyword);
765 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */