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 *keywords1 = NULL;
405 GList *keywords2 = NULL;
406 gchar *comment1 = NULL;
407 gchar *comment2 = NULL;
410 if (!fd) return FALSE;
412 res1 = metadata_xmp_read(fd, &keywords1, &comment1);
413 res2 = metadata_legacy_read(fd, &keywords2, &comment2);
423 *keywords = g_list_concat(keywords1, keywords2);
425 *keywords = res1 ? keywords1 : keywords2;
427 *keywords = remove_duplicate_strings_from_list(*keywords);
431 if (res1) string_list_free(keywords1);
432 if (res2) string_list_free(keywords2);
438 if (res1 && res2 && comment1 && comment2 && comment1[0] && comment2[0])
439 *comment = g_strdup_printf("%s\n%s", comment1, comment2);
441 *comment = res1 ? comment1 : comment2;
443 if (res1 && (!comment || *comment != comment1)) g_free(comment1);
444 if (res2 && (!comment || *comment != comment2)) g_free(comment2);
446 // return FALSE in the following cases:
447 // - only looking for a comment and didn't find one
448 // - only looking for keywords and didn't find any
449 // - looking for either a comment or keywords, but found nothing
450 if ((!keywords && comment && !*comment) ||
451 (!comment && keywords && !*keywords) ||
452 ( comment && !*comment && keywords && !*keywords))
458 void metadata_set(FileData *fd, GList *keywords_to_use, gchar *comment_to_use, gboolean append)
460 gchar *comment = NULL;
461 GList *keywords = NULL;
462 GList *save_list = NULL;
464 metadata_read(fd, &keywords, &comment);
468 if (append && comment && *comment)
470 gchar *tmp = comment;
472 comment = g_strconcat(tmp, comment_to_use, NULL);
478 comment = g_strdup(comment_to_use);
484 if (append && keywords && g_list_length(keywords) > 0)
488 work = keywords_to_use;
500 gchar *needle = p->data;
503 if (strcmp(needle, key) == 0) key = NULL;
506 if (key) keywords = g_list_append(keywords, g_strdup(key));
508 save_list = keywords;
512 save_list = keywords_to_use;
516 metadata_write(fd, save_list, comment);
518 string_list_free(keywords);
522 gboolean find_string_in_list(GList *list, const gchar *string)
526 gchar *haystack = list->data;
528 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
536 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
538 GList *string_to_keywords_list(const gchar *text)
541 const gchar *ptr = text;
548 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
550 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
556 /* trim starting and ending whitespaces */
557 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
558 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
562 gchar *keyword = g_strndup(begin, l);
564 /* only add if not already in the list */
565 if (find_string_in_list(list, keyword) == FALSE)
566 list = g_list_append(list, keyword);
575 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */