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