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