use the workflow in utilops.c for metadata approving and writting
[geeqie.git] / src / metadata.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis, Laurent Monin
7  *
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!
11  */
12
13
14 #include "main.h"
15 #include "metadata.h"
16
17 #include "cache.h"
18 #include "exif.h"
19 #include "filedata.h"
20 #include "misc.h"
21 #include "secure_save.h"
22 #include "ui_fileops.h"
23 #include "ui_misc.h"
24 #include "utilops.h"
25
26 typedef enum {
27         MK_NONE,
28         MK_KEYWORDS,
29         MK_COMMENT
30 } MetadataKey;
31
32 #define COMMENT_KEY "Xmp.dc.description"
33 #define KEYWORD_KEY "Xmp.dc.subject"
34
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);
38
39
40 /*
41  *-------------------------------------------------------------------
42  * write queue
43  *-------------------------------------------------------------------
44  */
45
46 static GList *metadata_write_queue = NULL;
47 static gint metadata_write_idle_id = -1;
48
49
50 static void metadata_write_queue_add(FileData *fd)
51 {
52         if (g_list_find(metadata_write_queue, fd)) return;
53         
54         metadata_write_queue = g_list_prepend(metadata_write_queue, fd);
55         file_data_ref(fd);
56
57         if (metadata_write_idle_id == -1) metadata_write_idle_id = g_idle_add(metadata_write_queue_idle_cb, NULL);
58 }
59
60
61 gboolean metadata_write_queue_remove(FileData *fd)
62 {
63         g_hash_table_destroy(fd->modified_xmp);
64         fd->modified_xmp = NULL;
65
66         metadata_write_queue = g_list_remove(metadata_write_queue, fd);
67         file_data_unref(fd);
68         return TRUE;
69 }
70
71
72 static gboolean metadata_write_queue_idle_cb(gpointer data)
73 {
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 */
77         
78
79 //      if (metadata_write_queue) return TRUE;
80
81         /* confirm writting */
82         file_util_write_metadata(NULL, metadata_write_queue, NULL);
83
84         metadata_write_idle_id = -1;
85         return FALSE;
86 }
87
88 gboolean metadata_write_perform(FileData *fd)
89 {
90         if (options->save_metadata_in_image_file &&
91             exif_write_fd(fd))
92                 {
93                 metadata_legacy_delete(fd);
94                 }
95         else metadata_legacy_write(fd);
96         return TRUE;
97 }
98
99 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
100 {
101         if (!fd->modified_xmp)
102                 {
103                 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
104                 }
105         g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
106         if (fd->exif)
107                 {
108                 exif_update_metadata(fd->exif, key, values);
109                 }
110         metadata_write_queue_add(fd);
111         return TRUE;
112 }
113         
114 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
115 {
116         return metadata_write_list(fd, key, g_list_append(NULL, g_strdup(value)));
117 }
118
119
120 /*
121  *-------------------------------------------------------------------
122  * keyword / comment read/write
123  *-------------------------------------------------------------------
124  */
125
126 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
127 {
128         SecureSaveInfo *ssi;
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;
132
133         ssi = secure_open(path);
134         if (!ssi) return FALSE;
135
136         secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
137
138         secure_fprintf(ssi, "[keywords]\n");
139         while (keywords && secsave_errno == SS_ERR_NONE)
140                 {
141                 const gchar *word = keywords->data;
142                 keywords = keywords->next;
143
144                 secure_fprintf(ssi, "%s\n", word);
145                 }
146         secure_fputc(ssi, '\n');
147
148         secure_fprintf(ssi, "[comment]\n");
149         secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
150
151         secure_fprintf(ssi, "#end\n");
152
153         return (secure_close(ssi) == 0);
154 }
155
156 static gint metadata_legacy_write(FileData *fd)
157 {
158         gchar *metadata_path;
159         gint success = FALSE;
160
161         /* If an existing metadata file exists, we will try writing to
162          * it's location regardless of the user's preference.
163          */
164         metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
165         if (metadata_path && !access_file(metadata_path, W_OK))
166                 {
167                 g_free(metadata_path);
168                 metadata_path = NULL;
169                 }
170
171         if (!metadata_path)
172                 {
173                 gchar *metadata_dir;
174                 mode_t mode = 0755;
175
176                 metadata_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
177                 if (recursive_mkdir_if_not_exists(metadata_dir, mode))
178                         {
179                         gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);
180                         
181                         metadata_path = g_build_filename(metadata_dir, filename, NULL);
182                         g_free(filename);
183                         }
184                 g_free(metadata_dir);
185                 }
186
187         if (metadata_path)
188                 {
189                 gchar *metadata_pathl;
190
191                 DEBUG_1("Saving comment: %s", metadata_path);
192
193                 metadata_pathl = path_from_utf8(metadata_path);
194
195                 success = metadata_file_write(metadata_pathl, fd->modified_xmp);
196
197                 g_free(metadata_pathl);
198                 g_free(metadata_path);
199                 }
200
201         return success;
202 }
203
204 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
205 {
206         FILE *f;
207         gchar s_buf[1024];
208         MetadataKey key = MK_NONE;
209         GList *list = NULL;
210         GString *comment_build = NULL;
211
212         f = fopen(path, "r");
213         if (!f) return FALSE;
214
215         while (fgets(s_buf, sizeof(s_buf), f))
216                 {
217                 gchar *ptr = s_buf;
218
219                 if (*ptr == '#') continue;
220                 if (*ptr == '[' && key != MK_COMMENT)
221                         {
222                         gchar *keystr = ++ptr;
223                         
224                         key = MK_NONE;
225                         while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
226                         
227                         if (*ptr == ']')
228                                 {
229                                 *ptr = '\0';
230                                 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
231                                         key = MK_KEYWORDS;
232                                 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
233                                         key = MK_COMMENT;
234                                 }
235                         continue;
236                         }
237                 
238                 switch(key)
239                         {
240                         case MK_NONE:
241                                 break;
242                         case MK_KEYWORDS:
243                                 {
244                                 while (*ptr != '\n' && *ptr != '\0') ptr++;
245                                 *ptr = '\0';
246                                 if (strlen(s_buf) > 0)
247                                         {
248                                         gchar *kw = utf8_validate_or_convert(s_buf);
249
250                                         list = g_list_prepend(list, kw);
251                                         }
252                                 }
253                                 break;
254                         case MK_COMMENT:
255                                 if (!comment_build) comment_build = g_string_new("");
256                                 g_string_append(comment_build, s_buf);
257                                 break;
258                         }
259                 }
260         
261         fclose(f);
262
263         *keywords = g_list_reverse(list);
264         if (comment_build)
265                 {
266                 if (comment)
267                         {
268                         gint len;
269                         gchar *ptr = comment_build->str;
270
271                         /* strip leading and trailing newlines */
272                         while (*ptr == '\n') ptr++;
273                         len = strlen(ptr);
274                         while (len > 0 && ptr[len - 1] == '\n') len--;
275                         if (ptr[len] == '\n') len++; /* keep the last one */
276                         if (len > 0)
277                                 {
278                                 gchar *text = g_strndup(ptr, len);
279
280                                 *comment = utf8_validate_or_convert(text);
281                                 g_free(text);
282                                 }
283                         }
284                 g_string_free(comment_build, TRUE);
285                 }
286
287         return TRUE;
288 }
289
290 static gint metadata_legacy_delete(FileData *fd)
291 {
292         gchar *metadata_path;
293         gchar *metadata_pathl;
294         gint success = FALSE;
295         if (!fd) return FALSE;
296
297         metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
298         if (!metadata_path) return FALSE;
299
300         metadata_pathl = path_from_utf8(metadata_path);
301
302         success = !unlink(metadata_pathl);
303
304         g_free(metadata_pathl);
305         g_free(metadata_path);
306
307         return success;
308 }
309
310 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
311 {
312         gchar *metadata_path;
313         gchar *metadata_pathl;
314         gint success = FALSE;
315         if (!fd) return FALSE;
316
317         metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
318         if (!metadata_path) return FALSE;
319
320         metadata_pathl = path_from_utf8(metadata_path);
321
322         success = metadata_file_read(metadata_pathl, keywords, comment);
323
324         g_free(metadata_pathl);
325         g_free(metadata_path);
326
327         return success;
328 }
329
330 static GList *remove_duplicate_strings_from_list(GList *list)
331 {
332         GList *work = list;
333         GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
334         GList *newlist = NULL;
335
336         while (work)
337                 {
338                 gchar *key = work->data;
339
340                 if (g_hash_table_lookup(hashtable, key) == NULL)
341                         {
342                         g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
343                         newlist = g_list_prepend(newlist, key);
344                         }
345                 work = work->next;
346                 }
347
348         g_hash_table_destroy(hashtable);
349         g_list_free(list);
350
351         return g_list_reverse(newlist);
352 }
353
354
355 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
356 {
357         ExifData *exif;
358
359         exif = exif_read_fd(fd);
360         if (!exif) return FALSE;
361
362         if (comment)
363                 {
364                 gchar *text;
365                 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
366
367                 text = exif_item_get_string(item, 0);
368                 *comment = utf8_validate_or_convert(text);
369                 g_free(text);
370                 }
371
372         if (keywords)
373                 {
374                 ExifItem *item;
375                 guint i;
376                 
377                 *keywords = NULL;
378                 item = exif_get_item(exif, KEYWORD_KEY);
379                 for (i = 0; i < exif_item_get_elements(item); i++)
380                         {
381                         gchar *kw = exif_item_get_string(item, i);
382                         gchar *utf8_kw;
383
384                         if (!kw) break;
385
386                         utf8_kw = utf8_validate_or_convert(kw);
387                         *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
388                         g_free(kw);
389                         }
390
391                 /* FIXME:
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
395                  * the item list.
396                  */
397                 for (item = exif_get_first_item(exif);
398                      item;
399                      item = exif_get_next_item(exif))
400                         {
401                         guint tag;
402                 
403                         tag = exif_item_get_tag_id(item);
404                         if (tag == 0x0019)
405                                 {
406                                 gchar *tag_name = exif_item_get_tag_name(item);
407
408                                 if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
409                                         {
410                                         gchar *kw;
411                                         gchar *utf8_kw;
412
413                                         kw = exif_item_get_data_as_text(item);
414                                         if (!kw) continue;
415
416                                         utf8_kw = utf8_validate_or_convert(kw);
417                                         *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
418                                         g_free(kw);
419                                         }
420                                 g_free(tag_name);
421                                 }
422                         }
423                 }
424
425         exif_free_fd(fd, exif);
426
427         return (comment && *comment) || (keywords && *keywords);
428 }
429
430 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
431 {
432         gint success = TRUE;
433         gint write_comment = (comment && comment[0]);
434
435         if (!fd) return FALSE;
436
437         if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment);
438         
439         if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords));
440
441         return success;
442 }
443
444 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
445 {
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;
451
452         if (!fd) return FALSE;
453
454         result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
455         result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
456
457         if (!result_xmp && !result_legacy)
458                 {
459                 return FALSE;
460                 }
461
462         if (keywords)
463                 {
464                 if (result_xmp && result_legacy)
465                         *keywords = g_list_concat(keywords_xmp, keywords_legacy);
466                 else
467                         *keywords = result_xmp ? keywords_xmp : keywords_legacy;
468
469                 *keywords = remove_duplicate_strings_from_list(*keywords);
470                 }
471         else
472                 {
473                 if (result_xmp) string_list_free(keywords_xmp);
474                 if (result_legacy) string_list_free(keywords_legacy);
475                 }
476
477
478         if (comment)
479                 {
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);
482                 else
483                         *comment = result_xmp ? comment_xmp : comment_legacy;
484                 }
485
486         if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
487         if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
488         
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))
496                 return FALSE;
497
498         return TRUE;
499 }
500
501 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
502 {
503         gchar *comment = NULL;
504         GList *keywords = NULL;
505         GList *keywords_list = NULL;
506
507         metadata_read(fd, &keywords, &comment);
508         
509         if (new_comment)
510                 {
511                 if (append && comment && *comment)
512                         {
513                         gchar *tmp = comment;
514                                 
515                         comment = g_strconcat(tmp, new_comment, NULL);
516                         g_free(tmp);
517                         }
518                 else
519                         {
520                         g_free(comment);
521                         comment = g_strdup(new_comment);
522                         }
523                 }
524         
525         if (new_keywords)
526                 {
527                 if (append && keywords && g_list_length(keywords) > 0)
528                         {
529                         GList *work;
530
531                         work = new_keywords;
532                         while (work)
533                                 {
534                                 gchar *key;
535                                 GList *p;
536
537                                 key = work->data;
538                                 work = work->next;
539
540                                 p = keywords;
541                                 while (p && key)
542                                         {
543                                         gchar *needle = p->data;
544                                         p = p->next;
545
546                                         if (strcmp(needle, key) == 0) key = NULL;
547                                         }
548
549                                 if (key) keywords = g_list_append(keywords, g_strdup(key));
550                                 }
551                         keywords_list = keywords;
552                         }
553                 else
554                         {
555                         keywords_list = new_keywords;
556                         }
557                 }
558         
559         metadata_write(fd, keywords_list, comment);
560
561         string_list_free(keywords);
562         g_free(comment);
563 }
564
565 gboolean find_string_in_list(GList *list, const gchar *string)
566 {
567         while (list)
568                 {
569                 gchar *haystack = list->data;
570
571                 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
572
573                 list = list->next;
574                 }
575
576         return FALSE;
577 }
578
579 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
580
581 GList *string_to_keywords_list(const gchar *text)
582 {
583         GList *list = NULL;
584         const gchar *ptr = text;
585
586         while (*ptr != '\0')
587                 {
588                 const gchar *begin;
589                 gint l = 0;
590
591                 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
592                 begin = ptr;
593                 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
594                         {
595                         ptr++;
596                         l++;
597                         }
598
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--;
602
603                 if (l > 0)
604                         {
605                         gchar *keyword = g_strndup(begin, l);
606
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);
610                         else
611                                 g_free(keyword);
612                         }
613                 }
614
615         return list;
616 }
617
618 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */