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 static void metadata_write_queue_commit(FileData *fd)
63 if (options->save_metadata_in_image_file &&
66 metadata_legacy_delete(fd);
68 else metadata_legacy_write(fd);
70 g_hash_table_destroy(fd->modified_xmp);
71 fd->modified_xmp = NULL;
73 metadata_write_queue = g_list_remove(metadata_write_queue, fd);
77 static gboolean metadata_write_queue_idle_cb(gpointer data)
79 metadata_write_queue_commit(metadata_write_queue->data); /* the first entry */
81 if (metadata_write_queue) return TRUE;
83 metadata_write_idle_id = -1;
88 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
90 if (!fd->modified_xmp)
92 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
94 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
97 exif_update_metadata(fd->exif, key, values);
99 metadata_write_queue_add(fd);
103 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
105 return metadata_write_list(fd, key, g_list_append(NULL, g_strdup(value)));
110 *-------------------------------------------------------------------
111 * keyword / comment read/write
112 *-------------------------------------------------------------------
115 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
118 GList *keywords = g_hash_table_lookup(modified_xmp, KEYWORD_KEY);
119 GList *comment_l = g_hash_table_lookup(modified_xmp, COMMENT_KEY);
120 gchar *comment = comment_l ? comment_l->data : NULL;
122 ssi = secure_open(path);
123 if (!ssi) return FALSE;
125 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
127 secure_fprintf(ssi, "[keywords]\n");
128 while (keywords && secsave_errno == SS_ERR_NONE)
130 const gchar *word = keywords->data;
131 keywords = keywords->next;
133 secure_fprintf(ssi, "%s\n", word);
135 secure_fputc(ssi, '\n');
137 secure_fprintf(ssi, "[comment]\n");
138 secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
140 secure_fprintf(ssi, "#end\n");
142 return (secure_close(ssi) == 0);
145 static gint metadata_legacy_write(FileData *fd)
147 gchar *metadata_path;
148 gint success = FALSE;
150 /* If an existing metadata file exists, we will try writing to
151 * it's location regardless of the user's preference.
153 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
154 if (metadata_path && !access_file(metadata_path, W_OK))
156 g_free(metadata_path);
157 metadata_path = NULL;
165 metadata_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
166 if (recursive_mkdir_if_not_exists(metadata_dir, mode))
168 gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
170 metadata_path = g_build_filename(metadata_dir, filename, NULL);
173 g_free(metadata_dir);
178 gchar *metadata_pathl;
180 DEBUG_1("Saving comment: %s", metadata_path);
182 metadata_pathl = path_from_utf8(metadata_path);
184 success = metadata_file_write(metadata_pathl, fd->modified_xmp);
186 g_free(metadata_pathl);
187 g_free(metadata_path);
193 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
197 MetadataKey key = MK_NONE;
199 GString *comment_build = NULL;
201 f = fopen(path, "r");
202 if (!f) return FALSE;
204 while (fgets(s_buf, sizeof(s_buf), f))
208 if (*ptr == '#') continue;
209 if (*ptr == '[' && key != MK_COMMENT)
211 gchar *keystr = ++ptr;
214 while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
219 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
221 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
233 while (*ptr != '\n' && *ptr != '\0') ptr++;
235 if (strlen(s_buf) > 0)
237 gchar *kw = utf8_validate_or_convert(s_buf);
239 list = g_list_prepend(list, kw);
244 if (!comment_build) comment_build = g_string_new("");
245 g_string_append(comment_build, s_buf);
252 *keywords = g_list_reverse(list);
258 gchar *ptr = comment_build->str;
260 /* strip leading and trailing newlines */
261 while (*ptr == '\n') ptr++;
263 while (len > 0 && ptr[len - 1] == '\n') len--;
264 if (ptr[len] == '\n') len++; /* keep the last one */
267 gchar *text = g_strndup(ptr, len);
269 *comment = utf8_validate_or_convert(text);
273 g_string_free(comment_build, TRUE);
279 static gint metadata_legacy_delete(FileData *fd)
281 gchar *metadata_path;
282 gchar *metadata_pathl;
283 gint success = FALSE;
284 if (!fd) return FALSE;
286 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
287 if (!metadata_path) return FALSE;
289 metadata_pathl = path_from_utf8(metadata_path);
291 success = !unlink(metadata_pathl);
293 g_free(metadata_pathl);
294 g_free(metadata_path);
299 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
301 gchar *metadata_path;
302 gchar *metadata_pathl;
303 gint success = FALSE;
304 if (!fd) return FALSE;
306 metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
307 if (!metadata_path) return FALSE;
309 metadata_pathl = path_from_utf8(metadata_path);
311 success = metadata_file_read(metadata_pathl, keywords, comment);
313 g_free(metadata_pathl);
314 g_free(metadata_path);
319 static GList *remove_duplicate_strings_from_list(GList *list)
322 GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
323 GList *newlist = NULL;
327 gchar *key = work->data;
329 if (g_hash_table_lookup(hashtable, key) == NULL)
331 g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
332 newlist = g_list_prepend(newlist, key);
337 g_hash_table_destroy(hashtable);
340 return g_list_reverse(newlist);
344 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
348 exif = exif_read_fd(fd);
349 if (!exif) return FALSE;
354 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
356 text = exif_item_get_string(item, 0);
357 *comment = utf8_validate_or_convert(text);
367 item = exif_get_item(exif, KEYWORD_KEY);
368 for (i = 0; i < exif_item_get_elements(item); i++)
370 gchar *kw = exif_item_get_string(item, i);
375 utf8_kw = utf8_validate_or_convert(kw);
376 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
381 * Exiv2 handles Iptc keywords as multiple entries with the
382 * same key, thus exif_get_item returns only the first keyword
383 * and the only way to get all keywords is to iterate through
386 for (item = exif_get_first_item(exif);
388 item = exif_get_next_item(exif))
392 tag = exif_item_get_tag_id(item);
395 gchar *tag_name = exif_item_get_tag_name(item);
397 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
402 kw = exif_item_get_data_as_text(item);
405 utf8_kw = utf8_validate_or_convert(kw);
406 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
414 exif_free_fd(fd, exif);
416 return (comment && *comment) || (keywords && *keywords);
419 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
422 gint write_comment = (comment && comment[0]);
424 if (!fd) return FALSE;
426 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment);
428 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords));
433 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
435 GList *keywords_xmp = NULL;
436 GList *keywords_legacy = NULL;
437 gchar *comment_xmp = NULL;
438 gchar *comment_legacy = NULL;
439 gint result_xmp, result_legacy;
441 if (!fd) return FALSE;
443 result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
444 result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
446 if (!result_xmp && !result_legacy)
453 if (result_xmp && result_legacy)
454 *keywords = g_list_concat(keywords_xmp, keywords_legacy);
456 *keywords = result_xmp ? keywords_xmp : keywords_legacy;
458 *keywords = remove_duplicate_strings_from_list(*keywords);
462 if (result_xmp) string_list_free(keywords_xmp);
463 if (result_legacy) string_list_free(keywords_legacy);
469 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
470 *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
472 *comment = result_xmp ? comment_xmp : comment_legacy;
475 if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
476 if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
478 // return FALSE in the following cases:
479 // - only looking for a comment and didn't find one
480 // - only looking for keywords and didn't find any
481 // - looking for either a comment or keywords, but found nothing
482 if ((!keywords && comment && !*comment) ||
483 (!comment && keywords && !*keywords) ||
484 ( comment && !*comment && keywords && !*keywords))
490 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
492 gchar *comment = NULL;
493 GList *keywords = NULL;
494 GList *keywords_list = NULL;
496 metadata_read(fd, &keywords, &comment);
500 if (append && comment && *comment)
502 gchar *tmp = comment;
504 comment = g_strconcat(tmp, new_comment, NULL);
510 comment = g_strdup(new_comment);
516 if (append && keywords && g_list_length(keywords) > 0)
532 gchar *needle = p->data;
535 if (strcmp(needle, key) == 0) key = NULL;
538 if (key) keywords = g_list_append(keywords, g_strdup(key));
540 keywords_list = keywords;
544 keywords_list = new_keywords;
548 metadata_write(fd, keywords_list, comment);
550 string_list_free(keywords);
554 gboolean find_string_in_list(GList *list, const gchar *string)
558 gchar *haystack = list->data;
560 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
568 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
570 GList *string_to_keywords_list(const gchar *text)
573 const gchar *ptr = text;
580 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
582 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
588 /* trim starting and ending whitespaces */
589 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
590 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
594 gchar *keyword = g_strndup(begin, l);
596 /* only add if not already in the list */
597 if (find_string_in_list(list, keyword) == FALSE)
598 list = g_list_append(list, keyword);
607 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */