write keywords and comments with separate functions
[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 #include "filefilter.h"
26
27 typedef enum {
28         MK_NONE,
29         MK_KEYWORDS,
30         MK_COMMENT
31 } MetadataKey;
32
33 static const gchar *group_keys[] = {KEYWORD_KEY, COMMENT_KEY, NULL}; /* tags that will be written to all files in a group */
34
35 static gboolean metadata_write_queue_idle_cb(gpointer data);
36 static gint metadata_legacy_write(FileData *fd);
37 static void metadata_legacy_delete(FileData *fd, const gchar *except);
38
39
40
41 /*
42  *-------------------------------------------------------------------
43  * write queue
44  *-------------------------------------------------------------------
45  */
46
47 static GList *metadata_write_queue = NULL;
48 static gint metadata_write_idle_id = -1;
49
50 static void metadata_write_queue_add(FileData *fd)
51 {
52         if (!g_list_find(metadata_write_queue, fd))
53                 {
54                 metadata_write_queue = g_list_prepend(metadata_write_queue, fd);
55                 file_data_ref(fd);
56                 }
57
58         if (metadata_write_idle_id != -1) 
59                 {
60                 g_source_remove(metadata_write_idle_id);
61                 metadata_write_idle_id = -1;
62                 }
63         
64         if (options->metadata.confirm_timeout > 0)
65                 {
66                 metadata_write_idle_id = g_timeout_add(options->metadata.confirm_timeout * 1000, metadata_write_queue_idle_cb, NULL);
67                 }
68 }
69
70
71 gboolean metadata_write_queue_remove(FileData *fd)
72 {
73         g_hash_table_destroy(fd->modified_xmp);
74         fd->modified_xmp = NULL;
75
76         metadata_write_queue = g_list_remove(metadata_write_queue, fd);
77         
78         file_data_increment_version(fd);
79         file_data_send_notification(fd, NOTIFY_TYPE_REREAD);
80
81         file_data_unref(fd);
82         return TRUE;
83 }
84
85 gboolean metadata_write_queue_remove_list(GList *list)
86 {
87         GList *work;
88         gboolean ret = TRUE;
89         
90         work = list;
91         while (work)
92                 {
93                 FileData *fd = work->data;
94                 work = work->next;
95                 ret = ret && metadata_write_queue_remove(fd);
96                 }
97         return ret;
98 }
99
100
101 gboolean metadata_write_queue_confirm(FileUtilDoneFunc done_func, gpointer done_data)
102 {
103         GList *work;
104         GList *to_approve = NULL;
105         
106         work = metadata_write_queue;
107         while (work)
108                 {
109                 FileData *fd = work->data;
110                 work = work->next;
111                 
112                 if (fd->change) continue; /* another operation in progress, skip this file for now */
113                 
114                 to_approve = g_list_prepend(to_approve, file_data_ref(fd));
115                 }
116
117         file_util_write_metadata(NULL, to_approve, NULL, done_func, done_data);
118         
119         filelist_free(to_approve);
120         
121         return (metadata_write_queue != NULL);
122 }
123
124 static gboolean metadata_write_queue_idle_cb(gpointer data)
125 {
126         metadata_write_queue_confirm(NULL, NULL);
127         metadata_write_idle_id = -1;
128         return FALSE;
129 }
130
131 gboolean metadata_write_perform(FileData *fd)
132 {
133         gboolean success;
134         ExifData *exif;
135         
136         g_assert(fd->change);
137         
138         if (fd->change->dest && 
139             strcmp(extension_from_path(fd->change->dest), GQ_CACHE_EXT_METADATA) == 0)
140                 {
141                 success = metadata_legacy_write(fd);
142                 if (success) metadata_legacy_delete(fd, fd->change->dest);
143                 return success;
144                 }
145
146         /* write via exiv2 */
147         /*  we can either use cached metadata which have fd->modified_xmp already applied 
148                                      or read metadata from file and apply fd->modified_xmp
149             metadata are read also if the file was modified meanwhile */
150         exif = exif_read_fd(fd); 
151         if (!exif) return FALSE;
152
153         success = (fd->change->dest) ? exif_write_sidecar(exif, fd->change->dest) : exif_write(exif); /* write modified metadata */
154         exif_free_fd(fd, exif);
155
156         if (success) metadata_legacy_delete(fd, fd->change->dest);
157         return success;
158 }
159
160 static gboolean metadata_check_key(const gchar *keys[], const gchar *key)
161 {
162         const gchar **k = keys;
163         
164         while (k)
165                 {
166                 if (strcmp(key, *k) == 0) return TRUE;
167                 k++;
168                 }
169         return FALSE;
170 }
171
172 gboolean metadata_write_list(FileData *fd, const gchar *key, const GList *values)
173 {
174         if (!fd->modified_xmp)
175                 {
176                 fd->modified_xmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)string_list_free);
177                 }
178         g_hash_table_insert(fd->modified_xmp, g_strdup(key), string_list_copy((GList *)values));
179         if (fd->exif)
180                 {
181                 exif_update_metadata(fd->exif, key, values);
182                 }
183         metadata_write_queue_add(fd);
184         file_data_increment_version(fd);
185         file_data_send_notification(fd, NOTIFY_TYPE_INTERNAL);
186
187         if (options->metadata.sync_grouped_files && metadata_check_key(group_keys, key))
188                 {
189                 GList *work = fd->sidecar_files;
190                 
191                 while (work)
192                         {
193                         FileData *sfd = work->data;
194                         work = work->next;
195                         
196                         if (filter_file_class(sfd->extension, FORMAT_CLASS_META)) continue; 
197
198                         metadata_write_list(sfd, key, values);
199                         }
200                 }
201
202
203         return TRUE;
204 }
205         
206 gboolean metadata_write_string(FileData *fd, const gchar *key, const char *value)
207 {
208         GList *list = g_list_append(NULL, g_strdup(value));
209         gboolean ret = metadata_write_list(fd, key, list);
210         string_list_free(list);
211         return ret;
212 }
213
214
215 /*
216  *-------------------------------------------------------------------
217  * keyword / comment read/write
218  *-------------------------------------------------------------------
219  */
220
221 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
222 {
223         SecureSaveInfo *ssi;
224         GList *keywords = g_hash_table_lookup(modified_xmp, KEYWORD_KEY);
225         GList *comment_l = g_hash_table_lookup(modified_xmp, COMMENT_KEY);
226         gchar *comment = comment_l ? comment_l->data : NULL;
227
228         ssi = secure_open(path);
229         if (!ssi) return FALSE;
230
231         secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
232
233         secure_fprintf(ssi, "[keywords]\n");
234         while (keywords && secsave_errno == SS_ERR_NONE)
235                 {
236                 const gchar *word = keywords->data;
237                 keywords = keywords->next;
238
239                 secure_fprintf(ssi, "%s\n", word);
240                 }
241         secure_fputc(ssi, '\n');
242
243         secure_fprintf(ssi, "[comment]\n");
244         secure_fprintf(ssi, "%s\n", (comment) ? comment : "");
245
246         secure_fprintf(ssi, "#end\n");
247
248         return (secure_close(ssi) == 0);
249 }
250
251 static gint metadata_legacy_write(FileData *fd)
252 {
253         gint success = FALSE;
254
255         g_assert(fd->change && fd->change->dest);
256         gchar *metadata_pathl;
257
258         DEBUG_1("Saving comment: %s", fd->change->dest);
259
260         metadata_pathl = path_from_utf8(fd->change->dest);
261
262         success = metadata_file_write(metadata_pathl, fd->modified_xmp);
263
264         g_free(metadata_pathl);
265
266         return success;
267 }
268
269 static gint metadata_file_read(gchar *path, GList **keywords, gchar **comment)
270 {
271         FILE *f;
272         gchar s_buf[1024];
273         MetadataKey key = MK_NONE;
274         GList *list = NULL;
275         GString *comment_build = NULL;
276
277         f = fopen(path, "r");
278         if (!f) return FALSE;
279
280         while (fgets(s_buf, sizeof(s_buf), f))
281                 {
282                 gchar *ptr = s_buf;
283
284                 if (*ptr == '#') continue;
285                 if (*ptr == '[' && key != MK_COMMENT)
286                         {
287                         gchar *keystr = ++ptr;
288                         
289                         key = MK_NONE;
290                         while (*ptr != ']' && *ptr != '\n' && *ptr != '\0') ptr++;
291                         
292                         if (*ptr == ']')
293                                 {
294                                 *ptr = '\0';
295                                 if (g_ascii_strcasecmp(keystr, "keywords") == 0)
296                                         key = MK_KEYWORDS;
297                                 else if (g_ascii_strcasecmp(keystr, "comment") == 0)
298                                         key = MK_COMMENT;
299                                 }
300                         continue;
301                         }
302                 
303                 switch(key)
304                         {
305                         case MK_NONE:
306                                 break;
307                         case MK_KEYWORDS:
308                                 {
309                                 while (*ptr != '\n' && *ptr != '\0') ptr++;
310                                 *ptr = '\0';
311                                 if (strlen(s_buf) > 0)
312                                         {
313                                         gchar *kw = utf8_validate_or_convert(s_buf);
314
315                                         list = g_list_prepend(list, kw);
316                                         }
317                                 }
318                                 break;
319                         case MK_COMMENT:
320                                 if (!comment_build) comment_build = g_string_new("");
321                                 g_string_append(comment_build, s_buf);
322                                 break;
323                         }
324                 }
325         
326         fclose(f);
327
328         *keywords = g_list_reverse(list);
329         if (comment_build)
330                 {
331                 if (comment)
332                         {
333                         gint len;
334                         gchar *ptr = comment_build->str;
335
336                         /* strip leading and trailing newlines */
337                         while (*ptr == '\n') ptr++;
338                         len = strlen(ptr);
339                         while (len > 0 && ptr[len - 1] == '\n') len--;
340                         if (ptr[len] == '\n') len++; /* keep the last one */
341                         if (len > 0)
342                                 {
343                                 gchar *text = g_strndup(ptr, len);
344
345                                 *comment = utf8_validate_or_convert(text);
346                                 g_free(text);
347                                 }
348                         }
349                 g_string_free(comment_build, TRUE);
350                 }
351
352         return TRUE;
353 }
354
355 static void metadata_legacy_delete(FileData *fd, const gchar *except)
356 {
357         gchar *metadata_path;
358         gchar *metadata_pathl;
359         if (!fd) return;
360
361         metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
362         if (metadata_path && (!except || strcmp(metadata_path, except) != 0)) 
363                 {
364                 metadata_pathl = path_from_utf8(metadata_path);
365                 unlink(metadata_pathl);
366                 g_free(metadata_pathl);
367                 g_free(metadata_path);
368                 }
369         metadata_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path);
370         if (metadata_path && (!except || strcmp(metadata_path, except) != 0)) 
371                 {
372                 metadata_pathl = path_from_utf8(metadata_path);
373                 unlink(metadata_pathl);
374                 g_free(metadata_pathl);
375                 g_free(metadata_path);
376                 }
377 }
378
379 static gint metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment)
380 {
381         gchar *metadata_path;
382         gchar *metadata_pathl;
383         gint success = FALSE;
384         if (!fd) return FALSE;
385
386         metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
387         if (!metadata_path) return FALSE;
388
389         metadata_pathl = path_from_utf8(metadata_path);
390
391         success = metadata_file_read(metadata_pathl, keywords, comment);
392
393         g_free(metadata_pathl);
394         g_free(metadata_path);
395
396         return success;
397 }
398
399 static GList *remove_duplicate_strings_from_list(GList *list)
400 {
401         GList *work = list;
402         GHashTable *hashtable = g_hash_table_new(g_str_hash, g_str_equal);
403         GList *newlist = NULL;
404
405         while (work)
406                 {
407                 gchar *key = work->data;
408
409                 if (g_hash_table_lookup(hashtable, key) == NULL)
410                         {
411                         g_hash_table_insert(hashtable, (gpointer) key, GINT_TO_POINTER(1));
412                         newlist = g_list_prepend(newlist, key);
413                         }
414                 work = work->next;
415                 }
416
417         g_hash_table_destroy(hashtable);
418         g_list_free(list);
419
420         return g_list_reverse(newlist);
421 }
422
423
424 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
425 {
426         ExifData *exif;
427
428         exif = exif_read_fd(fd);
429         if (!exif) return FALSE;
430
431         if (comment)
432                 {
433                 gchar *text;
434                 ExifItem *item = exif_get_item(exif, COMMENT_KEY);
435
436                 text = exif_item_get_string(item, 0);
437                 *comment = utf8_validate_or_convert(text);
438                 g_free(text);
439                 }
440
441         if (keywords)
442                 {
443                 ExifItem *item;
444                 guint i;
445                 
446                 *keywords = NULL;
447                 item = exif_get_item(exif, KEYWORD_KEY);
448                 for (i = 0; i < exif_item_get_elements(item); i++)
449                         {
450                         gchar *kw = exif_item_get_string(item, i);
451                         gchar *utf8_kw;
452
453                         if (!kw) break;
454
455                         utf8_kw = utf8_validate_or_convert(kw);
456                         *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
457                         g_free(kw);
458                         }
459
460                 /* FIXME:
461                  * Exiv2 handles Iptc keywords as multiple entries with the
462                  * same key, thus exif_get_item returns only the first keyword
463                  * and the only way to get all keywords is to iterate through
464                  * the item list.
465                  */
466                  /* Read IPTC keywords only if there are no XMP keywords
467                   * IPTC does not have standard charset, thus the encoding may differ
468                   * from XMP and keyword merging is not reliable.
469                   */
470                  if (!*keywords)
471                         {
472                         for (item = exif_get_first_item(exif);
473                              item;
474                              item = exif_get_next_item(exif))
475                                 {
476                                 guint tag;
477                         
478                                 tag = exif_item_get_tag_id(item);
479                                 if (tag == 0x0019)
480                                         {
481                                         gchar *tag_name = exif_item_get_tag_name(item);
482         
483                                         if (strcmp(tag_name, "Iptc.Application2.Keywords") == 0)
484                                                 {
485                                                 gchar *kw;
486                                                 gchar *utf8_kw;
487         
488                                                 kw = exif_item_get_data_as_text(item);
489                                                 if (!kw) continue;
490         
491                                                 utf8_kw = utf8_validate_or_convert(kw);
492                                                 *keywords = g_list_append(*keywords, (gpointer) utf8_kw);
493                                                 g_free(kw);
494                                                 }
495                                         g_free(tag_name);
496                                         }
497                                 }
498                         }
499                 }
500
501         exif_free_fd(fd, exif);
502
503         return (comment && *comment) || (keywords && *keywords);
504 }
505
506 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
507 {
508         GList *keywords_xmp = NULL;
509         GList *keywords_legacy = NULL;
510         gchar *comment_xmp = NULL;
511         gchar *comment_legacy = NULL;
512         gint result_xmp, result_legacy;
513
514         if (!fd) return FALSE;
515
516         result_xmp = metadata_xmp_read(fd, &keywords_xmp, &comment_xmp);
517         result_legacy = metadata_legacy_read(fd, &keywords_legacy, &comment_legacy);
518
519         if (!result_xmp && !result_legacy)
520                 {
521                 return FALSE;
522                 }
523
524         if (keywords)
525                 {
526                 if (result_xmp && result_legacy)
527                         *keywords = g_list_concat(keywords_xmp, keywords_legacy);
528                 else
529                         *keywords = result_xmp ? keywords_xmp : keywords_legacy;
530
531                 *keywords = remove_duplicate_strings_from_list(*keywords);
532                 }
533         else
534                 {
535                 if (result_xmp) string_list_free(keywords_xmp);
536                 if (result_legacy) string_list_free(keywords_legacy);
537                 }
538
539
540         if (comment)
541                 {
542                 if (result_xmp && result_legacy && comment_xmp && comment_legacy && *comment_xmp && *comment_legacy)
543                         *comment = g_strdup_printf("%s\n%s", comment_xmp, comment_legacy);
544                 else
545                         *comment = result_xmp ? comment_xmp : comment_legacy;
546                 }
547
548         if (result_xmp && (!comment || *comment != comment_xmp)) g_free(comment_xmp);
549         if (result_legacy && (!comment || *comment != comment_legacy)) g_free(comment_legacy);
550         
551         // return FALSE in the following cases:
552         //  - only looking for a comment and didn't find one
553         //  - only looking for keywords and didn't find any
554         //  - looking for either a comment or keywords, but found nothing
555         if ((!keywords && comment   && !*comment)  ||
556             (!comment  && keywords  && !*keywords) ||
557             ( comment  && !*comment &&   keywords && !*keywords))
558                 return FALSE;
559
560         return TRUE;
561 }
562
563 void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolean append)
564 {
565         gchar *comment = NULL;
566         GList *keywords = NULL;
567         GList *keywords_list = NULL;
568
569         metadata_read(fd, &keywords, &comment);
570         
571         if (new_comment)
572                 {
573                 if (append && comment && *comment)
574                         {
575                         gchar *tmp = comment;
576                                 
577                         comment = g_strconcat(tmp, new_comment, NULL);
578                         g_free(tmp);
579                         }
580                 else
581                         {
582                         g_free(comment);
583                         comment = g_strdup(new_comment);
584                         }
585                 }
586         
587         if (new_keywords)
588                 {
589                 if (append && keywords && g_list_length(keywords) > 0)
590                         {
591                         GList *work;
592
593                         work = new_keywords;
594                         while (work)
595                                 {
596                                 gchar *key;
597                                 GList *p;
598
599                                 key = work->data;
600                                 work = work->next;
601
602                                 p = keywords;
603                                 while (p && key)
604                                         {
605                                         gchar *needle = p->data;
606                                         p = p->next;
607
608                                         if (strcmp(needle, key) == 0) key = NULL;
609                                         }
610
611                                 if (key) keywords = g_list_append(keywords, g_strdup(key));
612                                 }
613                         keywords_list = keywords;
614                         }
615                 else
616                         {
617                         keywords_list = new_keywords;
618                         }
619                 }
620         
621         metadata_write_string(fd, COMMENT_KEY, comment);
622         metadata_write_list(fd, KEYWORD_KEY, keywords);
623
624         string_list_free(keywords);
625         g_free(comment);
626 }
627
628 gboolean find_string_in_list(GList *list, const gchar *string)
629 {
630         while (list)
631                 {
632                 gchar *haystack = list->data;
633
634                 if (haystack && string && strcmp(haystack, string) == 0) return TRUE;
635
636                 list = list->next;
637                 }
638
639         return FALSE;
640 }
641
642 #define KEYWORDS_SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '\n' || (c) == '\r' || (c) == '\b')
643
644 GList *string_to_keywords_list(const gchar *text)
645 {
646         GList *list = NULL;
647         const gchar *ptr = text;
648
649         while (*ptr != '\0')
650                 {
651                 const gchar *begin;
652                 gint l = 0;
653
654                 while (KEYWORDS_SEPARATOR(*ptr)) ptr++;
655                 begin = ptr;
656                 while (*ptr != '\0' && !KEYWORDS_SEPARATOR(*ptr))
657                         {
658                         ptr++;
659                         l++;
660                         }
661
662                 /* trim starting and ending whitespaces */
663                 while (l > 0 && g_ascii_isspace(*begin)) begin++, l--;
664                 while (l > 0 && g_ascii_isspace(begin[l-1])) l--;
665
666                 if (l > 0)
667                         {
668                         gchar *keyword = g_strndup(begin, l);
669
670                         /* only add if not already in the list */
671                         if (find_string_in_list(list, keyword) == FALSE)
672                                 list = g_list_append(list, keyword);
673                         else
674                                 g_free(keyword);
675                         }
676                 }
677
678         return list;
679 }
680
681 /*
682  * keywords to marks
683  */
684  
685
686 gboolean meta_data_get_keyword_mark(FileData *fd, gint n, gpointer data)
687 {
688         GList *keywords;
689         gboolean found = FALSE;
690         if (metadata_read(fd, &keywords, NULL))
691                 {
692                 GList *work = keywords;
693
694                 while (work)
695                         {
696                         gchar *kw = work->data;
697                         work = work->next;
698                         
699                         if (strcmp(kw, data) == 0)
700                                 {
701                                 found = TRUE;
702                                 break;
703                                 }
704                         }
705                 string_list_free(keywords);
706                 }
707         return found;
708 }
709
710 gboolean meta_data_set_keyword_mark(FileData *fd, gint n, gboolean value, gpointer data)
711 {
712         GList *keywords = NULL;
713         gboolean found = FALSE;
714         gboolean changed = FALSE;
715         GList *work;
716         metadata_read(fd, &keywords, NULL);
717
718         work = keywords;
719
720         while (work)
721                 {
722                 gchar *kw = work->data;
723                 
724                 if (strcmp(kw, data) == 0)
725                         {
726                         found = TRUE;
727                         if (!value) 
728                                 {
729                                 changed = TRUE;
730                                 keywords = g_list_delete_link(keywords, work);
731                                 g_free(kw);
732                                 }
733                         break;
734                         }
735                 work = work->next;
736                 }
737         if (value && !found) 
738                 {
739                 changed = TRUE;
740                 keywords = g_list_append(keywords, g_strdup(data));
741                 }
742         
743         if (changed) metadata_write_list(fd, KEYWORD_KEY, keywords);
744
745         string_list_free(keywords);
746         return TRUE;
747 }
748
749
750 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */