exiv2-utils.cpp 40.8 KB
Newer Older
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* -*- Mode: CPP; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2008-2009 Free Software Foundation, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
Paolo Bacchilega's avatar
Paolo Bacchilega committed
20
21
22
 */

#include <config.h>
23
#include <glib.h>
24
#define GDK_PIXBUF_ENABLE_BACKEND
25
#include <gdk-pixbuf/gdk-pixbuf.h>
Paolo Bacchilega's avatar
Paolo Bacchilega committed
26
27
28
29
30
31
32
33
#include <exiv2/basicio.hpp>
#include <exiv2/error.hpp>
#include <exiv2/image.hpp>
#include <exiv2/exif.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
34
#include <iomanip>
35
36
37
38
#include <exiv2/exiv2.hpp>
#if EXIV2_TEST_VERSION(0, 27, 0)
#include <exiv2/xmp_exiv2.hpp>
#else
Paolo Bacchilega's avatar
Paolo Bacchilega committed
39
#include <exiv2/xmp.hpp>
40
#endif
Paolo Bacchilega's avatar
Paolo Bacchilega committed
41
42
43
44
45
46
#include <gthumb.h>
#include "exiv2-utils.h"

using namespace std;


47
#define INVALID_VALUE N_("(invalid value)")
48
49


Paolo Bacchilega's avatar
Paolo Bacchilega committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Some bits of information may be contained in more than one metadata tag.
   The arrays below define the valid tags for a particular piece of
   information, in decreasing order of preference (best one first) */

const char *_DATE_TAG_NAMES[] = {
	"Exif::Image::DateTime",
	"Xmp::exif::DateTime",
	"Exif::Photo::DateTimeOriginal",
	"Xmp::exif::DateTimeOriginal",
	"Exif::Photo::DateTimeDigitized",
	"Xmp::exif::DateTimeDigitized",
	"Xmp::xmp::CreateDate",
	"Xmp::photoshop::DateCreated",
	"Xmp::xmp::ModifyDate",
	"Xmp::xmp::MetadataDate",
	NULL
};

68
69
70
71
72
73
74
75
const char *_LAST_DATE_TAG_NAMES[] = {
	"Exif::Image::DateTime",
	"Xmp::exif::DateTime",
	"Xmp::xmp::ModifyDate",
	"Xmp::xmp::MetadataDate",
	NULL
};

76
77
78
79
80
81
82
83
84
85
const char *_ORIGINAL_DATE_TAG_NAMES[] = {
	"Exif::Photo::DateTimeOriginal",
	"Xmp::exif::DateTimeOriginal",
	"Exif::Photo::DateTimeDigitized",
	"Xmp::exif::DateTimeDigitized",
	"Xmp::xmp::CreateDate",
	"Xmp::photoshop::DateCreated",
	NULL
};

Paolo Bacchilega's avatar
Paolo Bacchilega committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const char *_EXPOSURE_TIME_TAG_NAMES[] = {
	"Exif::Photo::ExposureTime",
	"Xmp::exif::ExposureTime",
	"Exif::Photo::ShutterSpeedValue",
	"Xmp::exif::ShutterSpeedValue",
	NULL
};

const char *_EXPOSURE_MODE_TAG_NAMES[] = {
	"Exif::Photo::ExposureMode",
	"Xmp::exif::ExposureMode",
	NULL
};

const char *_ISOSPEED_TAG_NAMES[] = {
	"Exif::Photo::ISOSpeedRatings",
	"Xmp::exif::ISOSpeedRatings",
	NULL
};

const char *_APERTURE_TAG_NAMES[] = {
	"Exif::Photo::ApertureValue",
	"Xmp::exif::ApertureValue",
	"Exif::Photo::FNumber",
	"Xmp::exif::FNumber",
	NULL
};

const char *_FOCAL_LENGTH_TAG_NAMES[] = {
	"Exif::Photo::FocalLength",
	"Xmp::exif::FocalLength",
	NULL
};

const char *_SHUTTER_SPEED_TAG_NAMES[] = {
	"Exif::Photo::ShutterSpeedValue",
	"Xmp::exif::ShutterSpeedValue",
	NULL
};

const char *_MAKE_TAG_NAMES[] = {
	"Exif::Image::Make",
	"Xmp::tiff::Make",
	NULL
};

const char *_MODEL_TAG_NAMES[] = {
	"Exif::Image::Model",
	"Xmp::tiff::Model",
	NULL
};

const char *_FLASH_TAG_NAMES[] = {
	"Exif::Photo::Flash",
	"Xmp::exif::Flash",
	NULL
};

const char *_ORIENTATION_TAG_NAMES[] = {
	"Exif::Image::Orientation",
	"Xmp::tiff::Orientation",
	NULL
};

150
const char *_DESCRIPTION_TAG_NAMES[] = {
151
152
	"Iptc::Application2::Caption",
	"Xmp::dc::description",
Paolo Bacchilega's avatar
Paolo Bacchilega committed
153
154
	"Exif::Photo::UserComment",
	"Exif::Image::ImageDescription",
155
	"Xmp::tiff::ImageDescription",
Paolo Bacchilega's avatar
Paolo Bacchilega committed
156
157
158
159
	"Iptc::Application2::Headline",
	NULL
};

160
161
162
163
164
const char *_TITLE_TAG_NAMES[] = {
	"Xmp::dc::title",
	NULL
};

Paolo Bacchilega's avatar
Paolo Bacchilega committed
165
166
167
168
169
170
171
172
const char *_LOCATION_TAG_NAMES[] = {
	"Xmp::iptc::Location",
	"Iptc::Application2::LocationName",
	NULL
};

const char *_KEYWORDS_TAG_NAMES[] = {
	"Iptc::Application2::Keywords",
Paolo Bacchilega's avatar
Paolo Bacchilega committed
173
174
	"Xmp::iptc::Keywords",
	"Xmp::dc::subject",
Paolo Bacchilega's avatar
Paolo Bacchilega committed
175
176
177
	NULL
};

178
179
180
181
182
const char *_RATING_TAG_NAMES[] = {
	"Xmp::xmp::Rating",
	NULL
};

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const char *_AUTHOR_TAG_NAMES[] = {
	"Exif::Image::Artist",
	"Iptc::Application2::Byline",
	"Xmp::dc::creator",
	"Xmp::xmpDM::artist",
	"Xmp::tiff::Artist",
	"Xmp::plus::ImageCreator",
	"Xmp::plus::ImageCreatorName",
	NULL
};

const char *_COPYRIGHT_TAG_NAMES[] = {
	"Exif::Image::Copyright",
	"Iptc::Application2::Copyright",
	"Xmp::dc::rights",
	"Xmp::xmpDM::copyright",
	"Xmp::tiff::Copyright",
	"Xmp::plus::CopyrightOwner",
	"Xmp::plus::CopyrightOwnerName",
	NULL
};
204

205
206
207
208
209
210
211
212
213
214
/* Some evil camera fill in the ImageDescription or UserComment fields
   with useless fluff. Try to filter these out, so they do not show up
   as comments */
const char *stupid_comment_filter[] = {
	"OLYMPUS DIGITAL CAMERA",
	"SONY DSC",
	"KONICA MINOLTA DIGITAL CAMERA",
	"MINOLTA DIGITAL CAMERA",
	NULL };

Paolo Bacchilega's avatar
Paolo Bacchilega committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

inline static char *
exiv2_key_from_attribute (const char *attribute)
{
	return _g_replace (attribute, "::", ".");
}


inline static char *
exiv2_key_to_attribute (const char *key)
{
	return _g_replace (key, ".", "::");
}


230
231
232
233
234
235
236
237
238
239
240
241
242
243
static gboolean
attribute_is_date (const char *key)
{
	int i;

	for (i = 0; _DATE_TAG_NAMES[i] != NULL; i++) {
		if (strcmp (_DATE_TAG_NAMES[i], key) == 0)
			return TRUE;
	}

	return FALSE;
}


244
245
246
247
248
249
250
static GthMetadata *
create_metadata (const char *key,
		 const char *description,
		 const char *formatted_value,
		 const char *raw_value,
		 const char *category,
		 const char *type_name)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
251
{
252
	char            *formatted_value_utf8;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
253
254
255
256
257
	char            *attribute;
	GthMetadataInfo *metadata_info;
	GthMetadata     *metadata;
	char            *description_utf8;

258
259
	formatted_value_utf8 = _g_utf8_from_any (formatted_value);
	if (_g_utf8_all_spaces (formatted_value_utf8))
260
		return NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
261

262
263
	description_utf8 = _g_utf8_from_any (description);

Paolo Bacchilega's avatar
Paolo Bacchilega committed
264
	attribute = exiv2_key_to_attribute (key);
265
266
267
	if (attribute_is_date (attribute)) {
		GTimeVal time_;

268
269
270
		g_free (formatted_value_utf8);
		formatted_value_utf8 = NULL;

271
272
273
274
275
		if (_g_time_val_from_exif_date (raw_value, &time_))
			formatted_value_utf8 = _g_time_val_strftime (&time_, "%x %X");
		else
			formatted_value_utf8 = g_locale_to_utf8 (formatted_value, -1, NULL, NULL, NULL);
	}
276
277
278
279
280
281
282
283
	else if (_g_utf8_has_prefix (formatted_value_utf8, "lang=")) {
		int   pos;
		char *formatted_clean;

		pos = _g_utf8_first_ascii_space (formatted_value_utf8);
		formatted_clean = _g_utf8_remove_prefix (formatted_value_utf8, pos + 1);
		g_free (formatted_value_utf8);
		formatted_value_utf8 = formatted_clean;
284
	}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
285

286
287
288
	if (formatted_value_utf8 == NULL)
		formatted_value_utf8 = g_strdup (INVALID_VALUE);

Paolo Bacchilega's avatar
Paolo Bacchilega committed
289
290
291
292
293
	metadata_info = gth_main_get_metadata_info (attribute);
	if ((metadata_info == NULL) && (category != NULL)) {
		GthMetadataInfo info;

		info.id = attribute;
294
		info.type = (type_name != NULL) ? g_strdup (type_name) : NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
295
296
297
298
299
300
301
		info.display_name = description_utf8;
		info.category = category;
		info.sort_order = 500;
		info.flags = GTH_METADATA_ALLOW_IN_PROPERTIES_VIEW;
		metadata_info = gth_main_register_metadata_info (&info);
	}

Paolo Bacchilega's avatar
Paolo Bacchilega committed
302
303
304
	if ((metadata_info != NULL) && (metadata_info->type == NULL) && (type_name != NULL))
		metadata_info->type = g_strdup (type_name);

305
	if ((metadata_info != NULL) && (metadata_info->display_name == NULL) && (description_utf8 != NULL))
Paolo Bacchilega's avatar
Paolo Bacchilega committed
306
307
308
		metadata_info->display_name = g_strdup (description_utf8);

	metadata = gth_metadata_new ();
Paolo Bacchilega's avatar
Paolo Bacchilega committed
309
310
311
312
313
314
315
	g_object_set (metadata,
		      "id", key,
		      "description", description_utf8,
		      "formatted", formatted_value_utf8,
		      "raw", raw_value,
		      "value-type", type_name,
		      NULL);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
316
317
318
319

	g_free (formatted_value_utf8);
	g_free (description_utf8);
	g_free (attribute);
320
321
322
323
324

	return metadata;
}


325
static void
326
327
add_string_list_to_metadata (GthMetadata            *metadata,
			     const Exiv2::Metadatum &value)
328
{
329
	GList         *list;
330
331
	GthStringList *string_list;

332
	list = NULL;
333
334
335
336
337
338
339
340
341
342
	for (int i = 0; i < value.count(); i++)
		list = g_list_prepend (list, g_strdup (value.toString(i).c_str()));
	string_list = gth_string_list_new (g_list_reverse (list));
	g_object_set (metadata, "string-list", string_list, NULL);

	g_object_unref (string_list);
	_g_string_list_free (list);
}


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
static void
set_file_info (GFileInfo  *info,
	       const char *key,
	       const char *description,
	       const char *formatted_value,
	       const char *raw_value,
	       const char *category,
	       const char *type_name)
{
	char        *attribute;
	GthMetadata *metadata;

	attribute = exiv2_key_to_attribute (key);
	metadata = create_metadata (key, description, formatted_value, raw_value, category, type_name);
	if (metadata != NULL) {
		g_file_info_set_attribute_object (info, attribute, G_OBJECT (metadata));
		g_object_unref (metadata);
	}

	g_free (attribute);
}


GHashTable *
create_metadata_hash (void)
{
	return g_hash_table_new_full (g_str_hash,
				      g_str_equal,
				      g_free,
				      g_object_unref);
}


static void
add_metadata_to_hash (GHashTable  *table,
		      GthMetadata *metadata)
{
	char     *key;
	gpointer  object;

	if (metadata == NULL)
		return;

	key = exiv2_key_to_attribute (gth_metadata_get_id (metadata));
	object = g_hash_table_lookup (table, key);
	if (object != NULL) {
		GthStringList *string_list;
		GList         *list;

		string_list = NULL;
393
394
		switch (gth_metadata_get_data_type (GTH_METADATA (object))) {
		case GTH_METADATA_TYPE_STRING:
395
			string_list = gth_string_list_new (NULL);
396
			list = g_list_append (NULL, g_strdup (gth_metadata_get_formatted (GTH_METADATA (object))));
397
			gth_string_list_set_list (string_list, list);
398
399
400
401
402
			break;

		case GTH_METADATA_TYPE_STRING_LIST:
			string_list = (GthStringList *) g_object_ref (gth_metadata_get_string_list (GTH_METADATA (object)));
			break;
403
404
		}

405
406
407
408
		if (string_list == NULL) {
			g_hash_table_insert (table,
					     g_strdup (key),
					     g_object_ref (metadata));
409
			return;
410
		}
411

412
413
414
415
416
417
418
419
420
421
422
		switch (gth_metadata_get_data_type (metadata)) {
		case GTH_METADATA_TYPE_STRING:
			list = gth_string_list_get_list (string_list);
			list = g_list_append (list, g_strdup (gth_metadata_get_formatted (metadata)));
			gth_string_list_set_list (string_list, list);
			break;

		case GTH_METADATA_TYPE_STRING_LIST:
			gth_string_list_concat (string_list, gth_metadata_get_string_list (metadata));
			break;
		}
423

424
		g_object_set (metadata, "string-list", string_list, NULL);
425
426
		g_hash_table_replace (table,
				      g_strdup (key),
427
				      g_object_ref (metadata));
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

		g_object_unref (string_list);
	}
	else
		g_hash_table_insert (table,
				     g_strdup (key),
				     g_object_ref (metadata));

	g_free (key);
}


static void
set_file_info_from_hash (GFileInfo  *info,
			 GHashTable *table)
{
	GHashTableIter iter;
	gpointer       key;
	gpointer       value;

	g_hash_table_iter_init (&iter, table);
	while (g_hash_table_iter_next (&iter, &key, &value))
		g_file_info_set_attribute_object (info, (char *)key, G_OBJECT (value));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
451
452
453
454
}


static void
455
456
457
set_attribute_from_metadata (GFileInfo  *info,
			     const char *attribute,
			     GObject    *metadata)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
458
{
459
460
461
462
	char *description;
	char *formatted_value;
	char *raw_value;
	char *type_name;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
463
464
465
466
467
468
469
470

	if (metadata == NULL)
		return;

	g_object_get (metadata,
		      "description", &description,
		      "formatted", &formatted_value,
		      "raw", &raw_value,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
471
		      "value-type", &type_name,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
472
		      NULL);
473

Paolo Bacchilega's avatar
Paolo Bacchilega committed
474
475
476
	set_file_info (info,
		       attribute,
		       description,
477
478
		       formatted_value,
		       raw_value,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
479
480
		       NULL,
		       type_name);
481
482
483
484
485

	g_free (description);
	g_free (formatted_value);
	g_free (raw_value);
	g_free (type_name);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
486
487
488
}


489
490
static GObject *
get_attribute_from_tagset (GFileInfo  *info,
491
492
493
494
495
496
497
498
499
			   const char *tagset[])
{
	GObject *metadata;
	int      i;

	metadata = NULL;
	for (i = 0; tagset[i] != NULL; i++) {
		metadata = g_file_info_get_attribute_object (info, tagset[i]);
		if (metadata != NULL)
500
			return metadata;
501
502
	}

503
504
505
506
507
508
509
510
511
512
513
514
	return NULL;
}


static void
set_attribute_from_tagset (GFileInfo  *info,
			   const char *attribute,
			   const char *tagset[])
{
	GObject *metadata;

	metadata = get_attribute_from_tagset (info, tagset);
515
516
517
518
519
	if (metadata != NULL)
		set_attribute_from_metadata (info, attribute, metadata);
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
520
static void
521
522
523
set_string_list_attribute_from_tagset (GFileInfo  *info,
				       const char *attribute,
				       const char *tagset[])
Paolo Bacchilega's avatar
Paolo Bacchilega committed
524
{
525
526
	GObject *metadata;
	int      i;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
527

528
529
530
531
532
533
534
535
536
537
	metadata = NULL;
	for (i = 0; tagset[i] != NULL; i++) {
		metadata = g_file_info_get_attribute_object (info, tagset[i]);
		if (metadata != NULL)
			break;
	}

	if (metadata == NULL)
		return;

538
	if (GTH_IS_METADATA (metadata) && (gth_metadata_get_data_type (GTH_METADATA (metadata)) != GTH_METADATA_TYPE_STRING_LIST)) {
539
		char           *raw;
540
		char           *utf8_raw;
541
542
		char          **keywords;
		GthStringList  *string_list;
543

544
		g_object_get (metadata, "raw", &raw, NULL);
545
546
547
548
549
		utf8_raw = _g_utf8_try_from_any (raw);
		if (utf8_raw == NULL)
			return;

		keywords = g_strsplit (utf8_raw, ",", -1);
550
		string_list = gth_string_list_new_from_strv (keywords);
551
552
		metadata = (GObject *) gth_metadata_new_for_string_list (string_list);
		g_file_info_set_attribute_object (info, attribute, metadata);
553

554
555
		g_object_unref (metadata);
		g_object_unref (string_list);
556
557
		g_strfreev (keywords);
		g_free (raw);
558
		g_free (utf8_raw);
559
	}
560
	else
561
		g_file_info_set_attribute_object (info, attribute, metadata);
562
563
}

564

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
static void
clear_studip_comments_from_tagset (GFileInfo  *info,
				   const char *tagset[])
{
	int i;

	for (i = 0; tagset[i] != NULL; i++) {
		GObject    *metadata;
		const char *value;
		int         j;

		metadata = g_file_info_get_attribute_object (info, tagset[i]);
		if ((metadata == NULL) || ! GTH_IS_METADATA (metadata))
			continue;

		value = gth_metadata_get_formatted (GTH_METADATA (metadata));
		for (j = 0; stupid_comment_filter[j] != NULL; j++) {
			if (strstr (value, stupid_comment_filter[j]) == value) {
				g_file_info_remove_attribute (info, tagset[i]);
				break;
			}
		}
	}
}


591
592
593
extern "C"
void
exiv2_update_general_attributes (GFileInfo *info)
594
{
595
	set_attribute_from_tagset (info, "general::datetime", _ORIGINAL_DATE_TAG_NAMES);
596
	set_attribute_from_tagset (info, "general::description", _DESCRIPTION_TAG_NAMES);
597
	set_attribute_from_tagset (info, "general::title", _TITLE_TAG_NAMES);
598

599
600
	/* if iptc::caption and iptc::headline are different use iptc::headline
	 * to set general::title, if not already set. */
601
602
603
604
605
606
607
608
609
610
611
612
613

	if (g_file_info_get_attribute_object (info, "general::title") == NULL) {
		GObject *iptc_caption;
		GObject *iptc_headline;

		iptc_caption = g_file_info_get_attribute_object (info, "Iptc::Application2::Caption");
		iptc_headline = g_file_info_get_attribute_object (info, "Iptc::Application2::Headline");

		if ((iptc_caption != NULL)
		    && (iptc_headline != NULL)
		    && (g_strcmp0 (gth_metadata_get_raw (GTH_METADATA (iptc_caption)),
				   gth_metadata_get_raw (GTH_METADATA (iptc_headline))) != 0))
		{
614
			set_attribute_from_metadata (info, "general::title", iptc_headline);
615
616
617
		}
	}

618
619
	set_attribute_from_tagset (info, "general::location", _LOCATION_TAG_NAMES);
	set_string_list_attribute_from_tagset (info, "general::tags", _KEYWORDS_TAG_NAMES);
620
	set_attribute_from_tagset (info, "general::rating", _RATING_TAG_NAMES);
621
622
623
}


624
625
626
#define EXPOSURE_SEPARATOR " · "


627
628
629
630
631
632
633
634
635
636
static void
set_attributes_from_tagsets (GFileInfo *info,
			     gboolean   update_general_attributes)
{
	clear_studip_comments_from_tagset (info, _DESCRIPTION_TAG_NAMES);
	clear_studip_comments_from_tagset (info, _TITLE_TAG_NAMES);

	if (update_general_attributes)
		exiv2_update_general_attributes (info);

637
	set_attribute_from_tagset (info, "Embedded::Photo::DateTimeOriginal", _ORIGINAL_DATE_TAG_NAMES);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
638
	set_attribute_from_tagset (info, "Embedded::Image::Orientation", _ORIENTATION_TAG_NAMES);
639

640
641
642
643
644
645
646
647
648
649
	set_attribute_from_tagset (info, "Embedded::Photo::Aperture", _APERTURE_TAG_NAMES);
	set_attribute_from_tagset (info, "Embedded::Photo::ISOSpeed", _ISOSPEED_TAG_NAMES);
	set_attribute_from_tagset (info, "Embedded::Photo::ExposureTime", _EXPOSURE_TIME_TAG_NAMES);
	set_attribute_from_tagset (info, "Embedded::Photo::ShutterSpeed", _SHUTTER_SPEED_TAG_NAMES);
	set_attribute_from_tagset (info, "Embedded::Photo::FocalLength", _FOCAL_LENGTH_TAG_NAMES);
	set_attribute_from_tagset (info, "Embedded::Photo::Flash", _FLASH_TAG_NAMES);
	set_attribute_from_tagset (info, "Embedded::Photo::CameraModel", _MODEL_TAG_NAMES);
	set_attribute_from_tagset (info, "Embedded::Photo::Author", _AUTHOR_TAG_NAMES);
	set_attribute_from_tagset (info, "Embedded::Photo::Copyright", _COPYRIGHT_TAG_NAMES);

650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
	/* Embedded::Photo::Exposure */

	GObject *aperture;
	GObject *iso_speed;
	GObject *shutter_speed;
	GObject *exposure_time;
	GString *exposure;

	aperture = get_attribute_from_tagset (info, _APERTURE_TAG_NAMES);
	iso_speed = get_attribute_from_tagset (info, _ISOSPEED_TAG_NAMES);
	shutter_speed = get_attribute_from_tagset (info, _SHUTTER_SPEED_TAG_NAMES);
	exposure_time = get_attribute_from_tagset (info, _EXPOSURE_TIME_TAG_NAMES);

	exposure = g_string_new ("");

	if (aperture != NULL) {
		char *formatted_value;

		g_object_get (aperture, "formatted", &formatted_value, NULL);
		if (formatted_value != NULL) {
			g_string_append (exposure, formatted_value);
			g_free (formatted_value);
		}
	}

	if (iso_speed != NULL) {
		char *formatted_value;

		g_object_get (iso_speed, "formatted", &formatted_value, NULL);
		if (formatted_value != NULL) {
			if (exposure->len > 0)
				g_string_append (exposure, EXPOSURE_SEPARATOR);
682
			g_string_append (exposure, "ISO ");
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
			g_string_append (exposure, formatted_value);
			g_free (formatted_value);
		}
	}

	if (shutter_speed != NULL) {
		char *formatted_value;

		g_object_get (shutter_speed, "formatted", &formatted_value, NULL);
		if (formatted_value != NULL) {
			if (exposure->len > 0)
				g_string_append (exposure, EXPOSURE_SEPARATOR);
			g_string_append (exposure, formatted_value);
			g_free (formatted_value);
		}
	}
	else if (exposure_time != NULL) {
		char *formatted_value;

		g_object_get (exposure_time, "formatted", &formatted_value, NULL);
		if (formatted_value != NULL) {
			if (exposure->len > 0)
				g_string_append (exposure, EXPOSURE_SEPARATOR);
			g_string_append (exposure, formatted_value);
			g_free (formatted_value);
		}
	}

	set_file_info (info,
		       "Embedded::Photo::Exposure",
		       _("Exposure"),
		       exposure->str,
		       NULL,
		       NULL,
		       NULL);

	g_string_free (exposure, TRUE);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
720
721
722
723
724
725
}


static const char *
get_exif_default_category (const Exiv2::Exifdatum &md)
{
726
727
728
#if EXIV2_TEST_VERSION(0, 21, 0)
	if (Exiv2::ExifTags::isMakerGroup(md.groupName()))
#else
Paolo Bacchilega's avatar
Paolo Bacchilega committed
729
	if (Exiv2::ExifTags::isMakerIfd(md.ifdId()))
730
#endif
Paolo Bacchilega's avatar
Paolo Bacchilega committed
731
732
		return "Exif::MakerNotes";

733
	if (md.groupName().compare("Thumbnail") == 0)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
734
		return "Exif::Thumbnail";
735
	else if (md.groupName().compare("GPSInfo") == 0)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
736
		return "Exif::GPS";
737
	else if (md.groupName().compare("Iop") == 0)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
738
739
740
741
742
743
		return "Exif::Versions";

	return "Exif::Other";
}


744
745
static void
exiv2_read_metadata (Exiv2::Image::AutoPtr  image,
746
747
		     GFileInfo             *info,
		     gboolean               update_general_attributes)
748
749
750
751
752
753
754
{
	image->readMetadata();

	Exiv2::ExifData &exifData = image->exifData();
	if (! exifData.empty()) {
		Exiv2::ExifData::const_iterator end = exifData.end();
		for (Exiv2::ExifData::const_iterator md = exifData.begin(); md != end; ++md) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
755
756
			stringstream raw_value;
			raw_value << md->value();
757

Paolo Bacchilega's avatar
Paolo Bacchilega committed
758
759
760
			stringstream description;
			if (! md->tagLabel().empty())
				description << md->tagLabel();
761
762
763
764
765
#if EXIV2_TEST_VERSION(0, 21, 0)
			else if (Exiv2::ExifTags::isMakerGroup(md->groupName()))
#else
			else if (Exiv2::ExifTags::isMakerIfd(md->ifdId()))
#endif
766
				// Must be a MakerNote - include group name
Paolo Bacchilega's avatar
Paolo Bacchilega committed
767
768
				description << md->groupName() << "." << md->tagName();
			else
769
				// Normal exif tag - just use tag name
Paolo Bacchilega's avatar
Paolo Bacchilega committed
770
				description << md->tagName();
771
772
773

			set_file_info (info,
				       md->key().c_str(),
Paolo Bacchilega's avatar
Paolo Bacchilega committed
774
				       description.str().c_str(),
775
				       md->print(&exifData).c_str(),
Paolo Bacchilega's avatar
Paolo Bacchilega committed
776
				       raw_value.str().c_str(),
777
778
				       get_exif_default_category (*md),
				       md->typeName());
779
780
781
782
783
		}
	}

	Exiv2::IptcData &iptcData = image->iptcData();
	if (! iptcData.empty()) {
784
785
		GHashTable *table = create_metadata_hash ();

786
787
		Exiv2::IptcData::iterator end = iptcData.end();
		for (Exiv2::IptcData::iterator md = iptcData.begin(); md != end; ++md) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
788
789
			stringstream raw_value;
			raw_value << md->value();
790

Paolo Bacchilega's avatar
Paolo Bacchilega committed
791
792
793
794
795
			stringstream description;
			if (! md->tagLabel().empty())
				description << md->tagLabel();
			else
				description << md->tagName();
796

797
798
799
800
801
802
803
			GthMetadata *metadata;
			metadata = create_metadata (md->key().c_str(),
						    description.str().c_str(),
						    md->print().c_str(),
						    raw_value.str().c_str(),
						    "Iptc",
						    md->typeName());
804
805
806
807
			if (metadata != NULL) {
				add_metadata_to_hash (table, metadata);
				g_object_unref (metadata);
			}
808
		}
809
810
811

		set_file_info_from_hash (info, table);
		g_hash_table_unref (table);
812
813
814
815
	}

	Exiv2::XmpData &xmpData = image->xmpData();
	if (! xmpData.empty()) {
816
817
		GHashTable *table = create_metadata_hash ();

818
819
		Exiv2::XmpData::iterator end = xmpData.end();
		for (Exiv2::XmpData::iterator md = xmpData.begin(); md != end; ++md) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
820
821
			stringstream raw_value;
			raw_value << md->value();
822

Paolo Bacchilega's avatar
Paolo Bacchilega committed
823
824
825
826
827
			stringstream description;
			if (! md->tagLabel().empty())
				description << md->tagLabel();
			else
				description << md->groupName() << "." << md->tagName();
828

829
830
831
832
833
834
835
			GthMetadata *metadata;
			metadata = create_metadata (md->key().c_str(),
						    description.str().c_str(),
						    md->print().c_str(),
						    raw_value.str().c_str(),
						    "Xmp::Embedded",
						    md->typeName());
836
837
838
839
840
841
			if (metadata != NULL) {
				if ((g_strcmp0 (md->typeName(), "XmpBag") == 0)
				    || (g_strcmp0 (md->typeName(), "XmpSeq") == 0))
				{
					add_string_list_to_metadata (metadata, *md);
				}
842

843
844
				add_metadata_to_hash (table, metadata);
				g_object_unref (metadata);
845
			}
846
		}
847
848
849

		set_file_info_from_hash (info, table);
		g_hash_table_unref (table);
850
851
	}

852
	set_attributes_from_tagsets (info, update_general_attributes);
853
854
855
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
856
/*
857
 * exiv2_read_metadata_from_file
Paolo Bacchilega's avatar
Paolo Bacchilega committed
858
859
860
861
862
863
 * reads metadata from image files
 * code relies heavily on example1 from the exiv2 website
 * http://www.exiv2.org/example1.html
 */
extern "C"
gboolean
864
865
exiv2_read_metadata_from_file (GFile         *file,
			       GFileInfo     *info,
866
			       gboolean       update_general_attributes,
867
868
			       GCancellable  *cancellable,
			       GError       **error)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
869
870
871
872
873
{
	try {
		char *path;

		path = g_file_get_path (file);
874
875
876
877
878
879
		if (path == NULL) {
			if (error != NULL)
				*error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED, _("Invalid file format"));
			return FALSE;
		}

Paolo Bacchilega's avatar
Paolo Bacchilega committed
880
881
882
883
		Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(path);
		g_free (path);

		if (image.get() == 0) {
884
885
			if (error != NULL)
				*error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED, _("Invalid file format"));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
886
887
			return FALSE;
		}
888
889
		// Set the log level to only show errors (and suppress warnings, informational and debug messages)
		Exiv2::LogMsg::setLevel(Exiv2::LogMsg::error);
890
		exiv2_read_metadata (image, info, update_general_attributes);
891
	}
892
	catch (Exiv2::AnyError& e) {
893
		if (error != NULL)
894
			*error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED, e.what());
895
896
		return FALSE;
	}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
897

898
899
	return TRUE;
}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
900
901


902
903
904
905
906
extern "C"
gboolean
exiv2_read_metadata_from_buffer (void       *buffer,
				 gsize       buffer_size,
				 GFileInfo  *info,
907
				 gboolean    update_general_attributes,
908
909
910
911
				 GError    **error)
{
	try {
		Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open ((Exiv2::byte*) buffer, buffer_size);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
912

913
914
915
916
		if (image.get() == 0) {
			if (error != NULL)
				*error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED, _("Invalid file format"));
			return FALSE;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
917
918
		}

919
		exiv2_read_metadata (image, info, update_general_attributes);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
920
	}
921
	catch (Exiv2::AnyError& e) {
922
		if (error != NULL)
923
			*error = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_FAILED, e.what());
Paolo Bacchilega's avatar
Paolo Bacchilega committed
924
925
926
927
928
929
930
		return FALSE;
	}

	return TRUE;
}


931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
extern "C"
GFile *
exiv2_get_sidecar (GFile *file)
{
	char  *uri;
	char  *uri_wo_ext;
	char  *sidecar_uri;
	GFile *sidecar;

	uri = g_file_get_uri (file);
	uri_wo_ext = _g_uri_remove_extension (uri);
	sidecar_uri = g_strconcat (uri_wo_ext, ".xmp", NULL);
	sidecar = g_file_new_for_uri (sidecar_uri);

	g_free (sidecar_uri);
	g_free (uri_wo_ext);
	g_free (uri);

	return sidecar;
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
953
954
955
extern "C"
gboolean
exiv2_read_sidecar (GFile     *file,
956
957
		    GFileInfo *info,
		    gboolean   update_general_attributes)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
958
959
960
961
962
{
	try {
		char *path;

		path = g_file_get_path (file);
963
964
965
		if (path == NULL)
			return FALSE;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
966
967
968
969
970
971
972
973
974
975
976
		Exiv2::DataBuf buf = Exiv2::readFile(path);
		g_free (path);

		std::string xmpPacket;
		xmpPacket.assign(reinterpret_cast<char*>(buf.pData_), buf.size_);
		Exiv2::XmpData xmpData;

		if (0 != Exiv2::XmpParser::decode(xmpData, xmpPacket))
			return FALSE;

		if (! xmpData.empty()) {
977
978
			GHashTable *table = create_metadata_hash ();

Paolo Bacchilega's avatar
Paolo Bacchilega committed
979
980
			Exiv2::XmpData::iterator end = xmpData.end();
			for (Exiv2::XmpData::iterator md = xmpData.begin(); md != end; ++md) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
981
982
				stringstream raw_value;
				raw_value << md->value();
Paolo Bacchilega's avatar
Paolo Bacchilega committed
983

Paolo Bacchilega's avatar
Paolo Bacchilega committed
984
985
986
987
988
				stringstream description;
				if (! md->tagLabel().empty())
					description << md->tagLabel();
				else
					description << md->groupName() << "." << md->tagName();
Paolo Bacchilega's avatar
Paolo Bacchilega committed
989

990
991
992
993
994
995
996
				GthMetadata *metadata;
				metadata = create_metadata (md->key().c_str(),
							    description.str().c_str(),
							    md->print().c_str(),
							    raw_value.str().c_str(),
							    "Xmp::Sidecar",
							    md->typeName());
997
998
999
1000
1001
1002
1003
1004
1005
				if (metadata != NULL) {
					if ((g_strcmp0 (md->typeName(), "XmpBag") == 0)
					    || (g_strcmp0 (md->typeName(), "XmpSeq") == 0))
					{
						add_string_list_to_metadata (metadata, *md);
					}

					add_metadata_to_hash (table, metadata);
					g_object_unref (metadata);
1006
				}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1007
			}
1008
1009
1010

			set_file_info_from_hash (info, table);
			g_hash_table_unref (table);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1011
1012
1013
		}
		Exiv2::XmpParser::terminate();

1014
		set_attributes_from_tagsets (info, update_general_attributes);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1015
1016
	}
	catch (Exiv2::AnyError& e) {
1017
		std::cerr << "Caught Exiv2 exception '" << e << "'\n";
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
		return FALSE;
	}

	return TRUE;
}


static void
mandatory_int (Exiv2::ExifData &checkdata,
	       const char      *tag,
	       int              value)
{
	Exiv2::ExifKey key = Exiv2::ExifKey(tag);
	if (checkdata.findKey(key) == checkdata.end())
		checkdata[tag] = value;
}


static void
mandatory_string (Exiv2::ExifData &checkdata,
		  const char      *tag,
		  const char      *value)
{
	Exiv2::ExifKey key = Exiv2::ExifKey(tag);
	if (checkdata.findKey(key) == checkdata.end())
		checkdata[tag] = value;
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
1047
const char *
1048
1049
gth_main_get_metadata_type (gpointer    metadata,
			    const char *attribute)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1050
{
1051
	const char      *value_type = NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1052
1053
	GthMetadataInfo *metadatum_info;

1054
1055
	if (GTH_IS_METADATA (metadata)) {
		value_type = gth_metadata_get_value_type (GTH_METADATA (metadata));
1056
		if ((g_strcmp0 (value_type, "Undefined") == 0) || (g_strcmp0 (value_type, "") == 0))
1057
			value_type = NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1058

1059
1060
1061
		if (value_type != NULL)
			return value_type;
	}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1062

1063
	metadatum_info = gth_main_get_metadata_info (attribute);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1064
1065
1066
1067
1068
1069
1070
	if (metadatum_info != NULL)
		value_type = metadatum_info->type;

	return value_type;
}


1071
1072
1073
1074
1075
1076
1077
1078
static void
dump_exif_data (Exiv2::ExifData &exifData,
		const char      *prefix)
{
	std::cout << prefix << "\n";

	try {
		if (exifData.empty()) {
1079
1080
1081
#if EXIV2_TEST_VERSION(0, 27, 0)
			throw Exiv2::Error(Exiv2::kerErrorMessage, " No Exif data found in the file");
#else
1082
			throw Exiv2::Error(1, " No Exif data found in the file");
1083
#endif
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
		}
		Exiv2::ExifData::const_iterator end = exifData.end();
		for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
			const char* tn = i->typeName();
			std::cout << std::setw(44) << std::setfill(' ') << std::left
				  << i->key() << " "
				  << "0x" << std::setw(4) << std::setfill('0') << std::right
				  << std::hex << i->tag() << " "
				  << std::setw(9) << std::setfill(' ') << std::left
				  << (tn ? tn : "Unknown") << " "
				  << std::dec << std::setw(3)
				  << std::setfill(' ') << std::right
				  << i->count() << "  "
				  << std::dec << i->value()
				  << "\n";
		}
		std::cout << "\n";
	}
	catch (Exiv2::Error& e) {
	    std::cout << "Caught Exiv2 exception '" << e.what() << "'\n";
	    return;
	}
}


1109
1110
1111
static Exiv2::DataBuf
exiv2_write_metadata_private (Exiv2::Image::AutoPtr  image,
			      GFileInfo             *info,
1112
			      GthImage              *image_data)
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1113
{
1114
1115
1116
	static char  *software_name = NULL;
	char        **attributes;
	int           i;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1117

1118
	image->clearMetadata();
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1119

1120
	// EXIF Data
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1121

1122
	Exiv2::ExifData ed;
1123
1124
1125
1126
	attributes = g_file_info_list_attributes (info, "Exif");
	for (i = 0; attributes[i] != NULL; i++) {
		GthMetadata *metadatum;
		char *key;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1127

1128
1129
		metadatum = (GthMetadata *) g_file_info_get_attribute_object (info, attributes[i]);
		key = exiv2_key_from_attribute (attributes[i]);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1130

1131
		try {
1132
1133
1134
			/* If the metadatum has no value yet, a new empty value
			 * is created. The type is taken from Exiv2's tag
			 * lookup tables. If the tag is not found in the table,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1135
1136
			 * the type defaults to ASCII.
			 * We always create the metadatum explicitly if the
1137
1138
			 * type is available to avoid type errors.
			 * See bug #610389 for more details.  The original
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1139
			 * explanation is here:
1140
1141
1142
			 * http://uk.groups.yahoo.com/group/exiv2/message/1472
			 */

Paolo Bacchilega's avatar
Paolo Bacchilega committed
1143
			const char *raw_value = gth_metadata_get_raw (metadatum);
1144
			const char *value_type = gth_main_get_metadata_type (metadatum, attributes[i]);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1145

1146
			if ((raw_value != NULL) && (strcmp (raw_value, "") != 0) && (value_type != NULL)) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1147
1148
				Exiv2::Value::AutoPtr value = Exiv2::Value::create (Exiv2::TypeInfo::typeId (value_type));
				value->read (raw_value);
1149
1150
1151
				Exiv2::ExifKey exif_key(key);
				ed.add (exif_key, value.get());
			}
1152
		}
1153
		catch (Exiv2::AnyError& e) {
1154
			/* we don't care about invalid key errors */
1155
			g_warning ("%s", e.what());
1156
		}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1157

1158
1159
1160
		g_free (key);
	}
	g_strfreev (attributes);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1161

1162
	// Mandatory tags - add if not already present
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1163

1164
1165
1166
1167
1168
1169
1170
1171
	mandatory_int (ed, "Exif.Image.XResolution", 72);
	mandatory_int (ed, "Exif.Image.YResolution", 72);
	mandatory_int (ed, "Exif.Image.ResolutionUnit", 2);
	mandatory_int (ed, "Exif.Image.YCbCrPositioning", 1);
	mandatory_int (ed, "Exif.Photo.ColorSpace", 1);
	mandatory_string (ed, "Exif.Photo.ExifVersion", "48 50 50 49");
	mandatory_string (ed, "Exif.Photo.ComponentsConfiguration", "1 2 3 0");
	mandatory_string (ed, "Exif.Photo.FlashpixVersion", "48 49 48 48");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1172

1173
	// Overwrite the software tag if the image content was modified
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1174

1175
1176
	if (g_file_info_get_attribute_boolean (info, "gth::file::image-changed")) {
		if (software_name == NULL)
1177
			software_name = g_strconcat (g_get_application_name (), " ", PACKAGE_VERSION, NULL);
1178
		ed["Exif.Image.ProcessingSoftware"] = software_name;
1179
	}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1180

1181
	// Update the dimension tags with actual image values
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1182

1183
	cairo_surface_t *surface = NULL;
1184
1185
	int width = 0;
	int height = 0;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1186

1187
1188
1189
1190
1191
	if (image_data != NULL)
		surface = gth_image_get_cairo_surface (image_data);

	if (surface != NULL) {
		width = cairo_image_surface_get_width (surface);
1192
		if (width > 0) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1193
			ed["Exif.Photo.PixelXDimension"] = width;
1194
1195
			ed["Exif.Image.ImageWidth"] = width;
		}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1196

1197
		height = cairo_image_surface_get_height (surface);
1198
		if (height > 0) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1199
			ed["Exif.Photo.PixelYDimension"] = height;
1200
1201
			ed["Exif.Image.ImageLength"] = height;
		}
1202

1203
		ed["Exif.Image.Orientation"] = 1;
1204
	}
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1205

1206
1207
	// Update the thumbnail

1208
	Exiv2::ExifThumb thumb(ed);
1209
1210
1211
1212
1213
	if ((surface != NULL) && (width > 0) && (height > 0)) {
		cairo_surface_t *thumbnail;
		GthImage        *thumbnail_data;
		char            *buffer;
		gsize            buffer_size;
1214
1215

		scale_keeping_ratio (&width, &height, 128, 128, FALSE);
1216
1217
		thumbnail = _cairo_image_surface_scale (surface, width, height, SCALE_FILTER_BEST, NULL);
		thumbnail_data = gth_image_new_for_surface (thumbnail);
1218
1219
1220
1221
1222
		if (gth_image_save_to_buffer (thumbnail_data,
					      "image/jpeg",
					      NULL,
					      &buffer,
					      &buffer_size,
1223
					      NULL,
1224
1225
					      NULL))
		{
1226
			thumb.setJpegThumbnail ((Exiv2::byte *) buffer, buffer_size);
1227
1228
1229
1230
			ed["Exif.Thumbnail.XResolution"] = 72;
			ed["Exif.Thumbnail.YResolution"] = 72;
			ed["Exif.Thumbnail.ResolutionUnit"] =  2;
			g_free (buffer);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1231
1232
1233
1234
		}
		else
			thumb.erase();

1235
1236
		g_object_unref (thumbnail_data);
		cairo_surface_destroy (thumbnail);
1237
1238
1239
	}
	else
		thumb.erase();
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1240

1241
1242
1243
	if (surface != NULL)
		cairo_surface_destroy (surface);

1244
	// Update the DateTime tag
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1245

1246
1247
1248
1249
1250
1251
1252
	if (g_file_info_get_attribute_object (info, "Exif::Image::DateTime") == NULL) {
		GTimeVal current_time;
		g_get_current_time (&current_time);
		char *date_time = _g_time_val_to_exif_date (&current_time);
		ed["Exif.Image.DateTime"] = date_time;
		g_free (date_time);
	}
1253
	ed.sortByKey();
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1254

1255
	// IPTC Data
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1256

1257
	Exiv2::IptcData id;
1258
1259
	attributes = g_file_info_list_attributes (info, "Iptc");
	for (i = 0; attributes[i] != NULL; i++) {
1260
1261
		gpointer  metadatum = (GthMetadata *) g_file_info_get_attribute_object (info, attributes[i]);
		char     *key = exiv2_key_from_attribute (attributes[i]);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1262

1263
		try {
1264
			const char *value_type;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1265

1266
1267
			value_type = gth_main_get_metadata_type (metadatum, attributes[i]);
			if (value_type != NULL) {
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1268
1269
				/* See the exif data code above for an explanation. */
				Exiv2::Value::AutoPtr value = Exiv2::Value::create (Exiv2::TypeInfo::typeId (value_type));
1270
				Exiv2::IptcKey iptc_key(key);
1271

1272
				const char *raw_value;
1273

1274
1275
				switch (gth_metadata_get_data_type (GTH_METADATA (metadatum))) {
				case GTH_METADATA_TYPE_STRING:
1276
1277
1278
1279
1280
					raw_value = gth_metadata_get_raw (GTH_METADATA (metadatum));
					if ((raw_value != NULL) && (strcmp (raw_value, "") != 0)) {
						value->read (raw_value);
						id.add (iptc_key, value.get());
					}
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
					break;

				case GTH_METADATA_TYPE_STRING_LIST:
					GthStringList *string_list = gth_metadata_get_string_list (GTH_METADATA (metadatum));
					for (GList *scan = gth_string_list_get_list (string_list); scan; scan = scan->next) {
						char *single_value = (char *) scan->data;

						value->read (single_value);
						id.add (iptc_key, value.get());
					}
					break;
1292
				}
1293
			}
1294
		}
1295
		catch (Exiv2::AnyError& e) {
1296
			/* we don't care about invalid key errors */
1297
			g_warning ("%s", e.what());
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1298
1299
		}

1300
1301
		g_free (key);
	}
1302
	id.sortByKey();