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"
33 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
35 if (!fd->modified_xmp)
37 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
39 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
42 exif_update_metadata(fd->exif, key, values);
47 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
49 return metadata_write_list(fd, key, g_list_append(NULL, g_strdup(value)));
54 *-------------------------------------------------------------------
55 * keyword / comment read/write
56 *-------------------------------------------------------------------
59 static gint metadata_file_write(gchar *path, GList *keywords, const gchar *comment)
63 ssi = secure_open(path);
64 if (!ssi) return FALSE;
66 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
68 secure_fprintf(ssi, "[keywords]\n");
69 while (keywords && secsave_errno == SS_ERR_NONE)
71 const gchar *word = keywords->data;
72 keywords = keywords->next;
74 secure_fprintf(ssi, "%s\n", word);
76 secure_fputc(ssi, '\n');
78 secure_fprintf(ssi, "[comment]\n");
79 secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
81 secure_fprintf(ssi, "#end\n");
83 return (secure_close(ssi) == 0);
86 static gint metadata_legacy_write(FileData *fd, GList *keywords, const gchar *comment)
91 /* If an existing metadata file exists, we will try writing to
92 * it's location regardless of the user's preference.
94 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
95 if (metadata_path && !access_file(metadata_path, W_OK))
97 g_free(metadata_path);
106 metadata_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
107 if (recursive_mkdir_if_not_exists(metadata_dir, mode))
109 gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
111 metadata_path = g_build_filename(metadata_dir, filename, NULL);
114 g_free(metadata_dir);
119 gchar *metadata_pathl;
121 DEBUG_1("Saving comment: %s", metadata_path);
123 metadata_pathl = path_from_utf8(metadata_path);
125 success = metadata_file_write(metadata_pathl, keywords, comment);
127 g_free(metadata_pathl);
128 g_free(metadata_path);
134 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
138 MetadataKey key = MK_NONE;
140 GString *comment_build = NULL;
142 f = fopen(path, "r");
143 if (!f) return FALSE;
145 while (fgets(s_buf, sizeof(s_buf), f))
149 if (*ptr == '#') continue;
150 if (*ptr == '[' && key != MK_COMMENT)
152 gchar *keystr = ++ptr;
155 while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
160 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
162 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
174 while (*ptr != '\n' && *ptr != '\0') ptr++;
176 if (strlen(s_buf) > 0)
178 gchar *kw = utf8_validate_or_convert(s_buf);
180 list = g_list_prepend(list, kw);
185 if (!comment_build) comment_build = g_string_new("");
186 g_string_append(comment_build, s_buf);
193 *keywords = g_list_reverse(list);
199 gchar *ptr = comment_build->str;
201 /* strip leading and trailing newlines */
202 while (*ptr == '\n') ptr++;
204 while (len > 0 && ptr[len - 1] == '\n') len--;
205 if (ptr[len] == '\n') len++; /* keep the last one */
208 gchar *text = g_strndup(ptr, len);
210 *comment = utf8_validate_or_convert(text);
214 g_string_free(comment_build, TRUE);
220 static gint metadata_legacy_delete(FileData *fd)
222 gchar *metadata_path;
223 gchar *metadata_pathl;
224 gint success = FALSE;
225 if (!fd) return FALSE;
227 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
228 if (!metadata_path) return FALSE;
230 metadata_pathl = path_from_utf8(metadata_path);
232 success = !unlink(metadata_pathl);
234 g_free(metadata_pathl);
235 g_free(metadata_path);
240 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
242 gchar *metadata_path;
243 gchar *metadata_pathl;
244 gint success = FALSE;
245 if (!fd) return FALSE;
247 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
248 if (!metadata_path) return FALSE;
250 metadata_pathl = path_from_utf8(metadata_path);
252 success = metadata_file_read(metadata_pathl, keywords, comment);
254 g_free(metadata_pathl);
255 g_free(metadata_path);
260 static GList *remove_duplicate_strings_from_list(GList *list)
263 GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
264 GList *newlist = NULL;
268 gchar *key = work->data;
270 if (g_hash_table_lookup(hashtable, key) == NULL)
272 g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
273 newlist = g_list_prepend(newlist, key);
278 g_hash_table_destroy(hashtable);
281 return g_list_reverse(newlist);
284 #define COMMENT_KEY "Xmp.dc.description"
285 #define KEYWORD_KEY "Xmp.dc.subject"
287 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
291 exif = exif_read_fd(fd);
292 if (!exif) return FALSE;
297 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
299 text = exif_item_get_string(item, 0);
300 *comment = utf8_validate_or_convert(text);
310 item = exif_get_item(exif, KEYWORD_KEY);
311 for (i = 0; i < exif_item_get_elements(item); i++)
313 gchar *kw = exif_item_get_string(item, i);
318 utf8_kw = utf8_validate_or_convert(kw);
319 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
324 * Exiv2 handles Iptc keywords as multiple entries with the
325 * same key, thus exif_get_item returns only the first keyword
326 * and the only way to get all keywords is to iterate through
329 for (item = exif_get_first_item(exif);
331 item = exif_get_next_item(exif))
335 tag = exif_item_get_tag_id(item);
338 gchar *tag_name = exif_item_get_tag_name(item);
340 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
345 kw = exif_item_get_data_as_text(item);
348 utf8_kw = utf8_validate_or_convert(kw);
349 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
357 exif_free_fd(fd, exif);
359 return (comment && *comment) || (keywords && *keywords);
362 static gint metadata_xmp_write(FileData *fd, GList *keywords, const gchar *comment)
365 gint write_comment = (comment && comment[0]);
367 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment);
369 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords));
372 /* FIXME - actual writting should be triggered in metadata_write_list and should be delayed */
373 success = exif_write_fd(fd);
378 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
380 if (!fd) return FALSE;
382 if (options->save_metadata_in_image_file &&
383 metadata_xmp_write(fd, keywords, comment))
385 metadata_legacy_delete(fd);
389 return metadata_legacy_write(fd, keywords, comment);
392 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
394 GList *keywords_xmp = NULL;
395 GList *keywords_legacy = NULL;
396 gchar *comment_xmp = NULL;
397 gchar *comment_legacy = NULL;
398 gint result_xmp, result_legacy;
400 if (!fd) return FALSE;
402 result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
403 result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
405 if (!result_xmp && !result_legacy)
412 if (result_xmp && result_legacy)
413 *keywords = g_list_concat(keywords_xmp, keywords_legacy);
415 *keywords = result_xmp ? keywords_xmp : keywords_legacy;
417 *keywords = remove_duplicate_strings_from_list(*keywords);
421 if (result_xmp) string_list_free(keywords_xmp);
422 if (result_legacy) string_list_free(keywords_legacy);
428 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
429 *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
431 *comment = result_xmp ? comment_xmp : comment_legacy;
434 if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
435 if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
437 // return FALSE in the following cases:
438 // - only looking for a comment and didn't find one
439 // - only looking for keywords and didn't find any
440 // - looking for either a comment or keywords, but found nothing
441 if ((!keywords && comment && !*comment) ||
442 (!comment && keywords && !*keywords) ||
443 ( comment && !*comment && keywords && !*keywords))
449 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
451 gchar *comment = NULL;
452 GList *keywords = NULL;
453 GList *keywords_list = NULL;
455 metadata_read(fd, &keywords, &comment);
459 if (append && comment && *comment)
461 gchar *tmp = comment;
463 comment = g_strconcat(tmp, new_comment, NULL);
469 comment = g_strdup(new_comment);
475 if (append && keywords && g_list_length(keywords) > 0)
491 gchar *needle = p->data;
494 if (strcmp(needle, key) == 0) key = NULL;
497 if (key) keywords = g_list_append(keywords, g_strdup(key));
499 keywords_list = keywords;
503 keywords_list = new_keywords;
507 metadata_write(fd, keywords_list, comment);
509 string_list_free(keywords);
513 gboolean find_string_in_list(GList *list, const gchar *string)
517 gchar *haystack = list->data;
519 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
527 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
529 GList *string_to_keywords_list(const gchar *text)
532 const gchar *ptr = text;
539 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
541 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
547 /* trim starting and ending whitespaces */
548 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
549 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
553 gchar *keyword = g_strndup(begin, l);
555 /* only add if not already in the list */
556 if (find_string_in_list(list, keyword) == FALSE)
557 list = g_list_append(list, keyword);
566 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */