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"
32 #define COMMENT_KEY "Xmp.dc.description"
33 #define KEYWORD_KEY "Xmp.dc.subject"
35 static gboolean metadata_write_queue_idle_cb(gpointer data);
36 static gint metadata_legacy_write(FileData *fd);
37 static gint metadata_legacy_delete(FileData *fd);
41 *-------------------------------------------------------------------
43 *-------------------------------------------------------------------
46 static GList *metadata_write_queue = NULL;
47 static gint metadata_write_idle_id = -1;
50 static void metadata_write_queue_add(FileData *fd)
52 if (g_list_find(metadata_write_queue, fd)) return;
54 metadata_write_queue = g_list_prepend(metadata_write_queue, fd);
57 if (metadata_write_idle_id == -1) metadata_write_idle_id = g_idle_add(metadata_write_queue_idle_cb, NULL);
61 gboolean metadata_write_queue_remove(FileData *fd)
63 g_hash_table_destroy(fd->modified_xmp);
64 fd->modified_xmp = NULL;
66 metadata_write_queue = g_list_remove(metadata_write_queue, fd);
72 static gboolean metadata_write_queue_idle_cb(gpointer data)
74 /* TODO: the queue should not be passed to file_util_write_metadata directly:
75 metatata under .geeqie/ can be written immediately,
76 for others we can decide if we write to the image file or to sidecar */
79 // if (metadata_write_queue) return TRUE;
81 /* confirm writting */
82 file_util_write_metadata(NULL, metadata_write_queue, NULL);
84 metadata_write_idle_id = -1;
88 gboolean metadata_write_perform(FileData *fd)
90 if (options->save_metadata_in_image_file &&
93 metadata_legacy_delete(fd);
95 else metadata_legacy_write(fd);
99 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
101 if (!fd->modified_xmp)
103 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
105 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
108 exif_update_metadata(fd->exif, key, values);
110 metadata_write_queue_add(fd);
114 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
116 return metadata_write_list(fd, key, g_list_append(NULL, g_strdup(value)));
121 *-------------------------------------------------------------------
122 * keyword / comment read/write
123 *-------------------------------------------------------------------
126 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
129 GList *keywords = g_hash_table_lookup(modified_xmp, KEYWORD_KEY);
130 GList *comment_l = g_hash_table_lookup(modified_xmp, COMMENT_KEY);
131 gchar *comment = comment_l ? comment_l->data : NULL;
133 ssi = secure_open(path);
134 if (!ssi) return FALSE;
136 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
138 secure_fprintf(ssi, "[keywords]\n");
139 while (keywords && secsave_errno == SS_ERR_NONE)
141 const gchar *word = keywords->data;
142 keywords = keywords->next;
144 secure_fprintf(ssi, "%s\n", word);
146 secure_fputc(ssi, '\n');
148 secure_fprintf(ssi, "[comment]\n");
149 secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
151 secure_fprintf(ssi, "#end\n");
153 return (secure_close(ssi) == 0);
156 static gint metadata_legacy_write(FileData *fd)
158 gchar *metadata_path;
159 gint success = FALSE;
161 /* If an existing metadata file exists, we will try writing to
162 * it's location regardless of the user's preference.
164 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
165 if (metadata_path && !access_file(metadata_path, W_OK))
167 g_free(metadata_path);
168 metadata_path = NULL;
176 metadata_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
177 if (recursive_mkdir_if_not_exists(metadata_dir, mode))
179 gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
181 metadata_path = g_build_filename(metadata_dir, filename, NULL);
184 g_free(metadata_dir);
189 gchar *metadata_pathl;
191 DEBUG_1("Saving comment: %s", metadata_path);
193 metadata_pathl = path_from_utf8(metadata_path);
195 success = metadata_file_write(metadata_pathl, fd->modified_xmp);
197 g_free(metadata_pathl);
198 g_free(metadata_path);
204 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
208 MetadataKey key = MK_NONE;
210 GString *comment_build = NULL;
212 f = fopen(path, "r");
213 if (!f) return FALSE;
215 while (fgets(s_buf, sizeof(s_buf), f))
219 if (*ptr == '#') continue;
220 if (*ptr == '[' && key != MK_COMMENT)
222 gchar *keystr = ++ptr;
225 while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
230 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
232 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
244 while (*ptr != '\n' && *ptr != '\0') ptr++;
246 if (strlen(s_buf) > 0)
248 gchar *kw = utf8_validate_or_convert(s_buf);
250 list = g_list_prepend(list, kw);
255 if (!comment_build) comment_build = g_string_new("");
256 g_string_append(comment_build, s_buf);
263 *keywords = g_list_reverse(list);
269 gchar *ptr = comment_build->str;
271 /* strip leading and trailing newlines */
272 while (*ptr == '\n') ptr++;
274 while (len > 0 && ptr[len - 1] == '\n') len--;
275 if (ptr[len] == '\n') len++; /* keep the last one */
278 gchar *text = g_strndup(ptr, len);
280 *comment = utf8_validate_or_convert(text);
284 g_string_free(comment_build, TRUE);
290 static gint metadata_legacy_delete(FileData *fd)
292 gchar *metadata_path;
293 gchar *metadata_pathl;
294 gint success = FALSE;
295 if (!fd) return FALSE;
297 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
298 if (!metadata_path) return FALSE;
300 metadata_pathl = path_from_utf8(metadata_path);
302 success = !unlink(metadata_pathl);
304 g_free(metadata_pathl);
305 g_free(metadata_path);
310 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
312 gchar *metadata_path;
313 gchar *metadata_pathl;
314 gint success = FALSE;
315 if (!fd) return FALSE;
317 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
318 if (!metadata_path) return FALSE;
320 metadata_pathl = path_from_utf8(metadata_path);
322 success = metadata_file_read(metadata_pathl, keywords, comment);
324 g_free(metadata_pathl);
325 g_free(metadata_path);
330 static GList *remove_duplicate_strings_from_list(GList *list)
333 GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
334 GList *newlist = NULL;
338 gchar *key = work->data;
340 if (g_hash_table_lookup(hashtable, key) == NULL)
342 g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
343 newlist = g_list_prepend(newlist, key);
348 g_hash_table_destroy(hashtable);
351 return g_list_reverse(newlist);
355 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
359 exif = exif_read_fd(fd);
360 if (!exif) return FALSE;
365 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
367 text = exif_item_get_string(item, 0);
368 *comment = utf8_validate_or_convert(text);
378 item = exif_get_item(exif, KEYWORD_KEY);
379 for (i = 0; i < exif_item_get_elements(item); i++)
381 gchar *kw = exif_item_get_string(item, i);
386 utf8_kw = utf8_validate_or_convert(kw);
387 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
392 * Exiv2 handles Iptc keywords as multiple entries with the
393 * same key, thus exif_get_item returns only the first keyword
394 * and the only way to get all keywords is to iterate through
397 for (item = exif_get_first_item(exif);
399 item = exif_get_next_item(exif))
403 tag = exif_item_get_tag_id(item);
406 gchar *tag_name = exif_item_get_tag_name(item);
408 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
413 kw = exif_item_get_data_as_text(item);
416 utf8_kw = utf8_validate_or_convert(kw);
417 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
425 exif_free_fd(fd, exif);
427 return (comment && *comment) || (keywords && *keywords);
430 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
433 gint write_comment = (comment && comment[0]);
435 if (!fd) return FALSE;
437 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment);
439 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords));
444 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
446 GList *keywords_xmp = NULL;
447 GList *keywords_legacy = NULL;
448 gchar *comment_xmp = NULL;
449 gchar *comment_legacy = NULL;
450 gint result_xmp, result_legacy;
452 if (!fd) return FALSE;
454 result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
455 result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
457 if (!result_xmp && !result_legacy)
464 if (result_xmp && result_legacy)
465 *keywords = g_list_concat(keywords_xmp, keywords_legacy);
467 *keywords = result_xmp ? keywords_xmp : keywords_legacy;
469 *keywords = remove_duplicate_strings_from_list(*keywords);
473 if (result_xmp) string_list_free(keywords_xmp);
474 if (result_legacy) string_list_free(keywords_legacy);
480 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
481 *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
483 *comment = result_xmp ? comment_xmp : comment_legacy;
486 if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
487 if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
489 // return FALSE in the following cases:
490 // - only looking for a comment and didn't find one
491 // - only looking for keywords and didn't find any
492 // - looking for either a comment or keywords, but found nothing
493 if ((!keywords && comment && !*comment) ||
494 (!comment && keywords && !*keywords) ||
495 ( comment && !*comment && keywords && !*keywords))
501 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
503 gchar *comment = NULL;
504 GList *keywords = NULL;
505 GList *keywords_list = NULL;
507 metadata_read(fd, &keywords, &comment);
511 if (append && comment && *comment)
513 gchar *tmp = comment;
515 comment = g_strconcat(tmp, new_comment, NULL);
521 comment = g_strdup(new_comment);
527 if (append && keywords && g_list_length(keywords) > 0)
543 gchar *needle = p->data;
546 if (strcmp(needle, key) == 0) key = NULL;
549 if (key) keywords = g_list_append(keywords, g_strdup(key));
551 keywords_list = keywords;
555 keywords_list = new_keywords;
559 metadata_write(fd, keywords_list, comment);
561 string_list_free(keywords);
565 gboolean find_string_in_list(GList *list, const gchar *string)
569 gchar *haystack = list->data;
571 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
579 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
581 GList *string_to_keywords_list(const gchar *text)
584 const gchar *ptr = text;
591 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
593 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
599 /* trim starting and ending whitespaces */
600 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
601 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
605 gchar *keyword = g_strndup(begin, l);
607 /* only add if not already in the list */
608 if (find_string_in_list(list, keyword) == FALSE)
609 list = g_list_append(list, keyword);
618 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */