comparison src/metadata.c @ 1204:fa91098e4949

queue metadata and write them in an idle callback
author nadvornik
date Tue, 16 Dec 2008 12:59:23 +0000
parents 43bfcbb62cd6
children 3ff2aa99108b
comparison
equal deleted inserted replaced
1203:43bfcbb62cd6 1204:fa91098e4949
27 MK_NONE, 27 MK_NONE,
28 MK_KEYWORDS, 28 MK_KEYWORDS,
29 MK_COMMENT 29 MK_COMMENT
30 } MetadataKey; 30 } MetadataKey;
31 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
32 87
33 gint metadata_write_list(FileData *fd, const gchar *key, GList *values) 88 gint metadata_write_list(FileData *fd, const gchar *key, GList *values)
34 { 89 {
35 if (!fd->modified_xmp) 90 if (!fd->modified_xmp)
36 { 91 {
39 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values); 94 g_hash_table_insert(fd->modified_xmp, g_strdup(key), values);
40 if (fd->exif) 95 if (fd->exif)
41 { 96 {
42 exif_update_metadata(fd->exif, key, values); 97 exif_update_metadata(fd->exif, key, values);
43 } 98 }
99 metadata_write_queue_add(fd);
44 return TRUE; 100 return TRUE;
45 } 101 }
46 102
47 gint metadata_write_string(FileData *fd, const gchar *key, const char *value) 103 gint metadata_write_string(FileData *fd, const gchar *key, const char *value)
48 { 104 {
54 *------------------------------------------------------------------- 110 *-------------------------------------------------------------------
55 * keyword / comment read/write 111 * keyword / comment read/write
56 *------------------------------------------------------------------- 112 *-------------------------------------------------------------------
57 */ 113 */
58 114
59 static gint metadata_file_write(gchar *path, GList *keywords, const gchar *comment) 115 static gint metadata_file_write(gchar *path, GHashTable *modified_xmp)
60 { 116 {
61 SecureSaveInfo *ssi; 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;
62 121
63 ssi = secure_open(path); 122 ssi = secure_open(path);
64 if (!ssi) return FALSE; 123 if (!ssi) return FALSE;
65 124
66 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION); 125 secure_fprintf(ssi, "#%s comment (%s)\n\n", GQ_APPNAME, VERSION);
81 secure_fprintf(ssi, "#end\n"); 140 secure_fprintf(ssi, "#end\n");
82 141
83 return (secure_close(ssi) == 0); 142 return (secure_close(ssi) == 0);
84 } 143 }
85 144
86 static gint metadata_legacy_write(FileData *fd, GList *keywords, const gchar *comment) 145 static gint metadata_legacy_write(FileData *fd)
87 { 146 {
88 gchar *metadata_path; 147 gchar *metadata_path;
89 gint success = FALSE; 148 gint success = FALSE;
90 149
91 /* If an existing metadata file exists, we will try writing to 150 /* If an existing metadata file exists, we will try writing to
120 179
121 DEBUG_1("Saving comment: %s", metadata_path); 180 DEBUG_1("Saving comment: %s", metadata_path);
122 181
123 metadata_pathl = path_from_utf8(metadata_path); 182 metadata_pathl = path_from_utf8(metadata_path);
124 183
125 success = metadata_file_write(metadata_pathl, keywords, comment); 184 success = metadata_file_write(metadata_pathl, fd->modified_xmp);
126 185
127 g_free(metadata_pathl); 186 g_free(metadata_pathl);
128 g_free(metadata_path); 187 g_free(metadata_path);
129 } 188 }
130 189
279 g_list_free(list); 338 g_list_free(list);
280 339
281 return g_list_reverse(newlist); 340 return g_list_reverse(newlist);
282 } 341 }
283 342
284 #define COMMENT_KEY "Xmp.dc.description"
285 #define KEYWORD_KEY "Xmp.dc.subject"
286 343
287 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment) 344 static gint metadata_xmp_read(FileData *fd, GList **keywords, gchar **comment)
288 { 345 {
289 ExifData *exif; 346 ExifData *exif;
290 347
357 exif_free_fd(fd, exif); 414 exif_free_fd(fd, exif);
358 415
359 return (comment && *comment) || (keywords && *keywords); 416 return (comment && *comment) || (keywords && *keywords);
360 } 417 }
361 418
362 static gint metadata_xmp_write(FileData *fd, GList *keywords, const gchar *comment) 419 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
363 { 420 {
364 gint success = TRUE; 421 gint success = TRUE;
365 gint write_comment = (comment && comment[0]); 422 gint write_comment = (comment && comment[0]);
366 423
424 if (!fd) return FALSE;
425
367 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment); 426 if (write_comment) success = success && metadata_write_string(fd, COMMENT_KEY, comment);
368 427
369 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords)); 428 if (keywords) success = success && metadata_write_list(fd, KEYWORD_KEY, string_list_copy(keywords));
370 429
371
372 /* FIXME - actual writting should be triggered in metadata_write_list and should be delayed */
373 success = exif_write_fd(fd);
374
375 return success; 430 return success;
376 }
377
378 gint metadata_write(FileData *fd, GList *keywords, const gchar *comment)
379 {
380 if (!fd) return FALSE;
381
382 if (options->save_metadata_in_image_file &&
383 metadata_xmp_write(fd, keywords, comment))
384 {
385 metadata_legacy_delete(fd);
386 return TRUE;
387 }
388
389 return metadata_legacy_write(fd, keywords, comment);
390 } 431 }
391 432
392 gint metadata_read(FileData *fd, GList **keywords, gchar **comment) 433 gint metadata_read(FileData *fd, GList **keywords, gchar **comment)
393 { 434 {
394 GList *keywords_xmp = NULL; 435 GList *keywords_xmp = NULL;