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"
34 *-------------------------------------------------------------------
35 * keyword / comment read/write
36 *-------------------------------------------------------------------
39 static gint metadata_file_write(gchar *path, GList *keywords, const gchar *comment)
43 ssi = secure_open(path);
44 if (!ssi) return FALSE;
46 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
48 secure_fprintf(ssi, "[keywords]\n");
49 while (keywords && secsave_errno == SS_ERR_NONE)
51 const gchar *word = keywords->data;
52 keywords = keywords->next;
54 secure_fprintf(ssi, "%s\n", word);
56 secure_fputc(ssi, '\n');
58 secure_fprintf(ssi, "[comment]\n");
59 secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
61 secure_fprintf(ssi, "#end\n");
63 return (secure_close(ssi) == 0);
66 static gint metadata_legacy_write(FileData *fd, GList *keywords, const gchar *comment)
71 /* If an existing metadata file exists, we will try writing to
72 * it's location regardless of the user's preference.
74 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
75 if (metadata_path && !access_file(metadata_path, W_OK))
77 g_free(metadata_path);
86 metadata_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
87 if (recursive_mkdir_if_not_exists(metadata_dir, mode))
89 gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
91 metadata_path = g_build_filename(metadata_dir, filename, NULL);
99 gchar *metadata_pathl;
101 DEBUG_1("Saving comment: %s", metadata_path);
103 metadata_pathl = path_from_utf8(metadata_path);
105 success = metadata_file_write(metadata_pathl, keywords, comment);
107 g_free(metadata_pathl);
108 g_free(metadata_path);
114 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
118 MetadataKey key = MK_NONE;
120 GString *comment_build = NULL;
122 f = fopen(path, "r");
123 if (!f) return FALSE;
125 while (fgets(s_buf, sizeof(s_buf), f))
129 if (*ptr == '#') continue;
130 if (*ptr == '[' && key != MK_COMMENT)
132 gchar *keystr = ++ptr;
135 while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
140 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
142 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
154 while (*ptr != '\n' && *ptr != '\0') ptr++;
156 if (strlen(s_buf) > 0)
158 gchar *kw = utf8_validate_or_convert(s_buf);
160 list = g_list_prepend(list, kw);
165 if (!comment_build) comment_build = g_string_new("");
166 g_string_append(comment_build, s_buf);
173 *keywords = g_list_reverse(list);
179 gchar *ptr = comment_build->str;
181 /* strip leading and trailing newlines */
182 while (*ptr == '\n') ptr++;
184 while (len > 0 && ptr[len - 1] == '\n') len--;
185 if (ptr[len] == '\n') len++; /* keep the last one */
188 gchar *text = g_strndup(ptr, len);
190 *comment = utf8_validate_or_convert(text);
194 g_string_free(comment_build, TRUE);
200 static gint metadata_legacy_delete(FileData *fd)
202 gchar *metadata_path;
203 gchar *metadata_pathl;
204 gint success = FALSE;
205 if (!fd) return FALSE;
207 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
208 if (!metadata_path) return FALSE;
210 metadata_pathl = path_from_utf8(metadata_path);
212 success = !unlink(metadata_pathl);
214 g_free(metadata_pathl);
215 g_free(metadata_path);
220 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
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 = metadata_file_read(metadata_pathl, keywords, comment);
234 g_free(metadata_pathl);
235 g_free(metadata_path);
240 static GList *remove_duplicate_strings_from_list(GList *list)
243 GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
244 GList *newlist = NULL;
248 gchar *key = work->data;
250 if (g_hash_table_lookup(hashtable, key) == NULL)
252 g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
253 newlist = g_list_prepend(newlist, key);
258 g_hash_table_destroy(hashtable);
261 return g_list_reverse(newlist);
264 #define COMMENT_KEY "Xmp.dc.description"
265 #define KEYWORD_KEY "Xmp.dc.subject"
267 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
271 exif = exif_read_fd(fd);
272 if (!exif) return FALSE;
277 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
279 text = exif_item_get_string(item, 0);
280 *comment = utf8_validate_or_convert(text);
290 item = exif_get_item(exif, KEYWORD_KEY);
291 for (i = 0; i < exif_item_get_elements(item); i++)
293 gchar *kw = exif_item_get_string(item, i);
298 utf8_kw = utf8_validate_or_convert(kw);
299 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
304 * Exiv2 handles Iptc keywords as multiple entries with the
305 * same key, thus exif_get_item returns only the first keyword
306 * and the only way to get all keywords is to iterate through
309 for (item = exif_get_first_item(exif);
311 item = exif_get_next_item(exif))
315 tag = exif_item_get_tag_id(item);
318 gchar *tag_name = exif_item_get_tag_name(item);
320 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
325 kw = exif_item_get_data_as_text(item);
328 utf8_kw = utf8_validate_or_convert(kw);
329 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
337 exif_free_fd(fd, exif);
339 return (comment && *comment) || (keywords && *keywords);
342 static gint metadata_xmp_write(FileData *fd, GList *keywords, const gchar *comment)
345 gint write_comment = (comment && comment[0]);
349 exif = exif_read_fd(fd);
350 if (!exif) return FALSE;
352 item = exif_get_item(exif, COMMENT_KEY);
353 if (item && !write_comment)
355 exif_item_delete(exif, item);
359 if (!item && write_comment) item = exif_add_item(exif, COMMENT_KEY);
360 if (item) exif_item_set_string(item, comment);
362 while ((item = exif_get_item(exif, KEYWORD_KEY)))
364 exif_item_delete(exif, item);
371 item = exif_add_item(exif, KEYWORD_KEY);
376 exif_item_set_string(item, (gchar *) work->data);
381 success = exif_write(exif);
383 exif_free_fd(fd, exif);
388 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
390 if (!fd) return FALSE;
392 if (options->save_metadata_in_image_file &&
393 metadata_xmp_write(fd, keywords, comment))
395 metadata_legacy_delete(fd);
399 return metadata_legacy_write(fd, keywords, comment);
402 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
404 GList *keywords_xmp = NULL;
405 GList *keywords_legacy = NULL;
406 gchar *comment_xmp = NULL;
407 gchar *comment_legacy = NULL;
408 gint result_xmp, result_legacy;
410 if (!fd) return FALSE;
412 result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
413 result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
415 if (!result_xmp && !result_legacy)
422 if (result_xmp && result_legacy)
423 *keywords = g_list_concat(keywords_xmp, keywords_legacy);
425 *keywords = result_xmp ? keywords_xmp : keywords_legacy;
427 *keywords = remove_duplicate_strings_from_list(*keywords);
431 if (result_xmp) string_list_free(keywords_xmp);
432 if (result_legacy) string_list_free(keywords_legacy);
438 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
439 *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
441 *comment = result_xmp ? comment_xmp : comment_legacy;
444 if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
445 if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
447 // return FALSE in the following cases:
448 // - only looking for a comment and didn't find one
449 // - only looking for keywords and didn't find any
450 // - looking for either a comment or keywords, but found nothing
451 if ((!keywords && comment && !*comment) ||
452 (!comment && keywords && !*keywords) ||
453 ( comment && !*comment && keywords && !*keywords))
459 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
461 gchar *comment = NULL;
462 GList *keywords = NULL;
463 GList *keywords_list = NULL;
465 metadata_read(fd, &keywords, &comment);
469 if (append && comment && *comment)
471 gchar *tmp = comment;
473 comment = g_strconcat(tmp, new_comment, NULL);
479 comment = g_strdup(new_comment);
485 if (append && keywords && g_list_length(keywords) > 0)
501 gchar *needle = p->data;
504 if (strcmp(needle, key) == 0) key = NULL;
507 if (key) keywords = g_list_append(keywords, g_strdup(key));
509 keywords_list = keywords;
513 keywords_list = new_keywords;
517 metadata_write(fd, keywords_list, comment);
519 string_list_free(keywords);
523 gboolean find_string_in_list(GList *list, const gchar *string)
527 gchar *haystack = list->data;
529 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
537 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
539 GList *string_to_keywords_list(const gchar *text)
542 const gchar *ptr = text;
549 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
551 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
557 /* trim starting and ending whitespaces */
558 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
559 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
563 gchar *keyword = g_strndup(begin, l);
565 /* only add if not already in the list */
566 if (find_string_in_list(list, keyword) == FALSE)
567 list = g_list_append(list, keyword);
576 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */