1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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
682
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
* Copyright (C) 2004 Red Hat, Inc
* Copyright (c) 2007 Novell, 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 along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: Alexander Larsson <alexl@redhat.com>
* XMP support by Hubert Figuiere <hfiguiere@novell.com>
*/
#include <config.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#ifdef HAVE_EXIF
#include <libexif/exif-data.h>
#include <libexif/exif-ifd.h>
#include <libexif/exif-loader.h>
#endif /*HAVE_EXIF*/
#ifdef HAVE_EXEMPI
#include <exempi/xmp.h>
#include <exempi/xmpconsts.h>
#endif /*HAVE_EXEMPI*/
#include <eel/eel-vfs-extensions.h>
#include <libcaja-extension/caja-property-page-provider.h>
#include <libcaja-private/caja-module.h>
#include "caja-image-properties-page.h"
#define LOAD_BUFFER_SIZE 8192
struct _CajaImagePropertiesPagePrivate
{
GCancellable *cancellable;
GtkWidget *vbox;
GtkWidget *loading_label;
GtkWidget *grid;
GdkPixbufLoader *loader;
gboolean got_size;
gboolean pixbuf_still_loading;
char buffer[LOAD_BUFFER_SIZE];
int width;
int height;
int row;
#ifdef HAVE_EXIF
ExifLoader *exifldr;
#endif /*HAVE_EXIF*/
#ifdef HAVE_EXEMPI
XmpPtr xmp;
#endif /*HAVE_EXEMPI*/
};
#ifdef HAVE_EXIF
struct ExifAttribute
{
ExifTag tag;
char *value;
gboolean found;
};
#endif /*HAVE_EXIF*/
enum
{
PROP_URI
};
typedef struct
{
GObject parent;
} CajaImagePropertiesPageProvider;
typedef struct
{
GObjectClass parent;
} CajaImagePropertiesPageProviderClass;
static GType caja_image_properties_page_provider_get_type (void);
static void property_page_provider_iface_init (CajaPropertyPageProviderIface *iface);
G_DEFINE_TYPE_WITH_PRIVATE (CajaImagePropertiesPage, caja_image_properties_page, GTK_TYPE_BOX);
G_DEFINE_TYPE_WITH_CODE (CajaImagePropertiesPageProvider, caja_image_properties_page_provider, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (CAJA_TYPE_PROPERTY_PAGE_PROVIDER,
property_page_provider_iface_init));
static void
caja_image_properties_page_finalize (GObject *object)
{
CajaImagePropertiesPage *page;
page = CAJA_IMAGE_PROPERTIES_PAGE (object);
if (page->details->cancellable)
{
g_cancellable_cancel (page->details->cancellable);
g_object_unref (page->details->cancellable);
page->details->cancellable = NULL;
}
G_OBJECT_CLASS (caja_image_properties_page_parent_class)->finalize (object);
}
static void
file_close_callback (GObject *object,
GAsyncResult *res,
gpointer data)
{
CajaImagePropertiesPage *page;
GInputStream *stream;
page = CAJA_IMAGE_PROPERTIES_PAGE (data);
stream = G_INPUT_STREAM (object);
g_input_stream_close_finish (stream, res, NULL);
g_object_unref (page->details->cancellable);
page->details->cancellable = NULL;
}
static GtkWidget *
append_label (GtkWidget *vbox,
const char *str)
{
GtkWidget *label;
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), str);
gtk_label_set_xalign (GTK_LABEL (label), 0);
gtk_label_set_yalign (GTK_LABEL (label), 0);
gtk_label_set_selectable (GTK_LABEL (label), TRUE);
/* setting can_focus to FALSE will allow to make the label
* selectable but without the cursor showing.
*/
gtk_widget_set_can_focus (label, FALSE);
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
gtk_widget_show (label);
return label;
}
static void
add_row (CajaImagePropertiesPage *page,
const char *col_name_str,
const char *col_value_str)
{
GtkWidget *label;
g_autofree char *value = NULL;
value = g_strdup_printf (_("<b>%s:</b>"), col_name_str);
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), value);
gtk_label_set_xalign (GTK_LABEL (label), 0);
gtk_label_set_yalign (GTK_LABEL (label), 0);
gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_widget_set_can_focus (label, FALSE);
gtk_grid_attach (GTK_GRID (page->details->grid), label, 0, page->details->row, 1, 1);
label = gtk_label_new (col_value_str);
gtk_label_set_xalign (GTK_LABEL (label), 0);
gtk_label_set_yalign (GTK_LABEL (label), 0);
gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_widget_set_can_focus (label, FALSE);
gtk_grid_attach (GTK_GRID (page->details->grid), label, 1, page->details->row, 1, 1);
page->details->row++;
}
#ifdef HAVE_EXIF
static char *
exif_string_to_utf8 (const char *exif_str)
{
char *utf8_str;
if (g_utf8_validate (exif_str, -1, NULL))
{
return g_strdup (exif_str);
}
utf8_str = g_locale_to_utf8 (exif_str, -1, NULL, NULL, NULL);
if (utf8_str != NULL)
{
return utf8_str;
}
return eel_make_valid_utf8 (exif_str);
}
static void
exif_content_callback (ExifContent *content, gpointer data)
{
struct ExifAttribute *attribute;
char b[1024];
attribute = (struct ExifAttribute *)data;
if (attribute->found)
{
return;
}
attribute->value = g_strdup (exif_content_get_value (content, attribute->tag, b, sizeof(b)));
if (attribute->value != NULL)
{
attribute->found = TRUE;
}
}
static char *
exifdata_get_tag_name_utf8 (ExifTag tag)
{
return exif_string_to_utf8 (exif_tag_get_name (tag));
}
static char *
exifdata_get_tag_value_utf8 (ExifData *data, ExifTag tag)
{
struct ExifAttribute attribute;
char *utf8_value;
attribute.tag = tag;
attribute.value = NULL;
attribute.found = FALSE;
exif_data_foreach_content (data, exif_content_callback, &attribute);
if (attribute.found)
{
utf8_value = exif_string_to_utf8 (attribute.value);
g_free (attribute.value);
}
else
{
utf8_value = NULL;
}
return utf8_value;
}
static gboolean
append_tag_value_pair (CajaImagePropertiesPage *page,
ExifData *data,
ExifTag tag,
char *description)
{
char *utf_attribute;
char *utf_value;
utf_attribute = exifdata_get_tag_name_utf8 (tag);
utf_value = exifdata_get_tag_value_utf8 (data, tag);
if ((utf_attribute == NULL) || (utf_value == NULL))
{
g_free (utf_attribute);
g_free (utf_value);
return FALSE;
}
add_row (page, description ? description : utf_attribute, utf_value);
g_free (utf_attribute);
g_free (utf_value);
return TRUE;
}
static void
append_exifdata_string (ExifData *exifdata, CajaImagePropertiesPage *page)
{
if (exifdata && exifdata->ifd[0] && exifdata->ifd[0]->count)
{
append_tag_value_pair (page, exifdata, EXIF_TAG_MAKE, _("Camera Brand"));
append_tag_value_pair (page, exifdata, EXIF_TAG_MODEL, _("Camera Model"));
/* Choose which date to show in order of relevance */
if (!append_tag_value_pair (page, exifdata, EXIF_TAG_DATE_TIME_ORIGINAL, _("Date Taken")))
{
if (!append_tag_value_pair (page, exifdata, EXIF_TAG_DATE_TIME_DIGITIZED, _("Date Digitized")))
{
append_tag_value_pair (page, exifdata, EXIF_TAG_DATE_TIME, _("Date Modified"));
}
}
append_tag_value_pair (page, exifdata, EXIF_TAG_EXPOSURE_TIME, _("Exposure Time"));
append_tag_value_pair (page, exifdata, EXIF_TAG_APERTURE_VALUE, _("Aperture Value"));
append_tag_value_pair (page, exifdata, EXIF_TAG_ISO_SPEED_RATINGS, _("ISO Speed Rating"));
append_tag_value_pair (page, exifdata, EXIF_TAG_FLASH,_("Flash Fired"));
append_tag_value_pair (page, exifdata, EXIF_TAG_METERING_MODE, _("Metering Mode"));
append_tag_value_pair (page, exifdata, EXIF_TAG_EXPOSURE_PROGRAM, _("Exposure Program"));
append_tag_value_pair (page, exifdata, EXIF_TAG_FOCAL_LENGTH,_("Focal Length"));
append_tag_value_pair (page, exifdata, EXIF_TAG_SOFTWARE, _("Software"));
}
}
#endif /*HAVE_EXIF*/
#ifdef HAVE_EXEMPI
static void
append_xmp_value_pair (CajaImagePropertiesPage *page,
XmpPtr xmp,
const char *ns,
const char *propname,
char *descr)
{
uint32_t options;
XmpStringPtr value;
value = xmp_string_new();
if (xmp_get_property (xmp, ns, propname, value, &options))
{
if (XMP_IS_PROP_SIMPLE (options))
{
add_row (page, descr, xmp_string_cstr (value));
}
else if (XMP_IS_PROP_ARRAY (options))
{
XmpIteratorPtr iter;
iter = xmp_iterator_new (xmp, ns, propname, XMP_ITER_JUSTLEAFNODES);
if (iter)
{
GString *str;
gboolean first = TRUE;
char *value_str;
str = g_string_new (NULL);
while (xmp_iterator_next (iter, NULL, NULL, value, &options)
&& !XMP_IS_PROP_QUALIFIER(options))
{
if (!first)
{
g_string_append_printf (str, ", ");
}
else
{
first = FALSE;
}
g_string_append_printf (str,
"%s",
xmp_string_cstr(value));
}
xmp_iterator_free(iter);
value_str = g_string_free (str, FALSE);
add_row (page, descr, value_str);
g_free (value_str);
}
}
}
xmp_string_free(value);
}
static void
append_xmpdata_string (XmpPtr xmp, CajaImagePropertiesPage *page)
{
if (xmp != NULL)
{
append_xmp_value_pair (page, xmp, NS_IPTC4XMP, "Location", _("Location"));
append_xmp_value_pair (page, xmp, NS_DC, "description", _("Description"));
append_xmp_value_pair (page, xmp, NS_DC, "subject", _("Keywords"));
append_xmp_value_pair (page, xmp, NS_DC, "creator", _("Creator"));
append_xmp_value_pair (page, xmp, NS_DC, "rights", _("Copyright"));
append_xmp_value_pair (page, xmp, NS_XAP,"Rating", _("Rating"));
/* TODO add CC licenses */
}
}
#endif /*HAVE EXEMPI*/
static void
load_finished (CajaImagePropertiesPage *page)
{
gtk_widget_destroy (page->details->loading_label);
if (page->details->loader != NULL) {
gdk_pixbuf_loader_close (page->details->loader, NULL);
}
if (page->details->got_size)
{
GdkPixbufFormat *format;
g_autofree char *name = NULL;
g_autofree char *desc = NULL;
g_autofree char *value = NULL;
#ifdef HAVE_EXIF
ExifData *exif_data;
#endif /*HAVE_EXIF*/
format = gdk_pixbuf_loader_get_format (page->details->loader);
name = gdk_pixbuf_format_get_name (format);
desc = gdk_pixbuf_format_get_description (format);
page->details->grid = gtk_grid_new ();
gtk_grid_set_column_spacing (GTK_GRID (page->details->grid), 12);
gtk_grid_set_row_spacing (GTK_GRID (page->details->grid), 6);
gtk_container_set_border_width (GTK_CONTAINER (page->details->grid), 12);
gtk_box_pack_start (GTK_BOX (page->details->vbox), page->details->grid, TRUE, TRUE, 0);
page->details->row = 0;
value = g_strdup_printf ("%s (%s)",name, desc);
add_row (page, _("Image Type"), value);
value = g_strdup_printf (ngettext ("%d pixel",
"%d pixels",
page->details->width),
page->details->width);
add_row (page, _("Width"), value);
value = g_strdup_printf (ngettext ("%d pixel",
"%d pixels",
page->details->height),
page->details->height);
add_row (page, _("Height"), value);
#ifdef HAVE_EXIF
exif_data = exif_loader_get_data (page->details->exifldr);
append_exifdata_string (exif_data, page);
exif_data_unref (exif_data);
#endif /*HAVE_EXIF*/
#ifdef HAVE_EXEMPI
append_xmpdata_string (page->details->xmp, page);
#endif /*HAVE_EXEMPI*/
gtk_widget_show_all (page->details->grid);
}
else
{
append_label (page->details->vbox,
_("Failed to load image information"));
}
if (page->details->loader != NULL)
{
g_object_unref (page->details->loader);
page->details->loader = NULL;
}
#ifdef HAVE_EXIF
if (page->details->exifldr != NULL)
{
exif_loader_unref (page->details->exifldr);
page->details->exifldr = NULL;
}
#endif /*HAVE_EXIF*/
#ifdef HAVE_EXEMPI
if (page->details->xmp != NULL)
{
xmp_free(page->details->xmp);
page->details->xmp = NULL;
}
#endif /*HAVE_EXEMPI*/
}
static void
file_read_callback (GObject *object,
GAsyncResult *res,
gpointer data)
{
CajaImagePropertiesPage *page;
GInputStream *stream;
gssize count_read;
GError *error;
gboolean done_reading;
page = CAJA_IMAGE_PROPERTIES_PAGE (data);
stream = G_INPUT_STREAM (object);
error = NULL;
done_reading = FALSE;
count_read = g_input_stream_read_finish (stream, res, &error);
if (count_read > 0)
{
int exif_still_loading;
g_assert (((size_t) count_read) <= sizeof (page->details->buffer));
#ifdef HAVE_EXIF
exif_still_loading = exif_loader_write (page->details->exifldr,
page->details->buffer,
count_read);
#else
exif_still_loading = 0;
#endif /*HAVE_EXIF*/
if (page->details->pixbuf_still_loading)
{
if (!gdk_pixbuf_loader_write (page->details->loader,
page->details->buffer,
count_read,
NULL))
{
page->details->pixbuf_still_loading = FALSE;
}
}
if (page->details->pixbuf_still_loading ||
(exif_still_loading == 1))
{
g_input_stream_read_async (G_INPUT_STREAM (stream),
page->details->buffer,
sizeof (page->details->buffer),
0,
page->details->cancellable,
file_read_callback,
page);
}
else
{
done_reading = TRUE;
}
}
else
{
/* either EOF, cancelled or an error occurred */
done_reading = TRUE;
}
if (done_reading)
{
load_finished (page);
g_input_stream_close_async (stream,
0,
page->details->cancellable,
file_close_callback,
page);
}
}
static void
size_prepared_callback (GdkPixbufLoader *loader,
int width,
int height,
gpointer callback_data)
{
CajaImagePropertiesPage *page;
page = CAJA_IMAGE_PROPERTIES_PAGE (callback_data);
page->details->height = height;
page->details->width = width;
page->details->got_size = TRUE;
page->details->pixbuf_still_loading = FALSE;
}
static void
file_open_callback (GObject *object,
GAsyncResult *res,
gpointer data)
{
CajaImagePropertiesPage *page;
GFile *file;
GFileInputStream *stream;
GError *error;
page = CAJA_IMAGE_PROPERTIES_PAGE (data);
file = G_FILE (object);
error = NULL;
stream = g_file_read_finish (file, res, &error);
if (stream)
{
page->details->loader = gdk_pixbuf_loader_new ();
page->details->pixbuf_still_loading = TRUE;
page->details->width = 0;
page->details->height = 0;
#ifdef HAVE_EXIF
page->details->exifldr = exif_loader_new ();
#endif /*HAVE_EXIF*/
g_signal_connect (page->details->loader,
"size_prepared",
G_CALLBACK (size_prepared_callback),
page);
g_input_stream_read_async (G_INPUT_STREAM (stream),
page->details->buffer,
sizeof (page->details->buffer),
0,
page->details->cancellable,
file_read_callback,
page);
g_object_unref (stream);
}
}
static void
load_location (CajaImagePropertiesPage *page,
const char *location)
{
GFile *file;
g_assert (CAJA_IS_IMAGE_PROPERTIES_PAGE (page));
g_assert (location != NULL);
page->details->cancellable = g_cancellable_new ();
file = g_file_new_for_uri (location);
#ifdef HAVE_EXEMPI
{
/* Current Exempi does not support setting custom IO to be able to use Mate-vfs */
/* So it will only work with local files. Future version might remove this limitation */
XmpFilePtr xf;
char *localname;
localname = g_filename_from_uri (location, NULL, NULL);
if (localname)
{
xf = xmp_files_open_new (localname, 0);
page->details->xmp = xmp_files_get_new_xmp (xf); /* only load when loading */
xmp_files_close (xf, 0);
g_free (localname);
}
else
{
page->details->xmp = NULL;
}
}
#endif /*HAVE_EXEMPI*/
g_file_read_async (file,
0,
page->details->cancellable,
file_open_callback,
page);
g_object_unref (file);
}
static void
caja_image_properties_page_class_init (CajaImagePropertiesPageClass *class)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (class);
object_class->finalize = caja_image_properties_page_finalize;
}
static void
caja_image_properties_page_init (CajaImagePropertiesPage *page)
{
page->details = caja_image_properties_page_get_instance_private (page);
gtk_orientable_set_orientation (GTK_ORIENTABLE (page), GTK_ORIENTATION_VERTICAL);
gtk_box_set_homogeneous (GTK_BOX (page), FALSE);
page->details->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
page->details->loading_label =
append_label (page->details->vbox,_("loading..."));
gtk_box_pack_start (GTK_BOX (page),
page->details->vbox,
FALSE, TRUE, 2);
gtk_widget_show_all (GTK_WIDGET (page));
}
static GList *
get_property_pages (CajaPropertyPageProvider *provider,
GList *files)
{
GList *pages;
CajaPropertyPage *real_page;
CajaFileInfo *file;
char *uri;
CajaImagePropertiesPage *page;
/* Only show the property page if 1 file is selected */
if (!files || files->next != NULL)
{
return NULL;
}
file = CAJA_FILE_INFO (files->data);
if (!
(caja_file_info_is_mime_type (file, "image/x-bmp") ||
caja_file_info_is_mime_type (file, "image/x-ico") ||
caja_file_info_is_mime_type (file, "image/jpeg") ||
caja_file_info_is_mime_type (file, "image/gif") ||
caja_file_info_is_mime_type (file, "image/png") ||
caja_file_info_is_mime_type (file, "image/pnm") ||
caja_file_info_is_mime_type (file, "image/ras") ||
caja_file_info_is_mime_type (file, "image/tga") ||
caja_file_info_is_mime_type (file, "image/tiff") ||
caja_file_info_is_mime_type (file, "image/wbmp") ||
caja_file_info_is_mime_type (file, "image/x-xbitmap") ||
caja_file_info_is_mime_type (file, "image/x-xpixmap")))
{
return NULL;
}
pages = NULL;
uri = caja_file_info_get_uri (file);
page = g_object_new (caja_image_properties_page_get_type (), NULL);
load_location (page, uri);
g_free (uri);
real_page = caja_property_page_new
("CajaImagePropertiesPage::property_page",
gtk_label_new (_("Image")),
GTK_WIDGET (page));
pages = g_list_append (pages, real_page);
return pages;
}
static void
property_page_provider_iface_init (CajaPropertyPageProviderIface *iface)
{
iface->get_pages = get_property_pages;
}
static void
caja_image_properties_page_provider_init (CajaImagePropertiesPageProvider *sidebar)
{
}
static void
caja_image_properties_page_provider_class_init (CajaImagePropertiesPageProviderClass *class)
{
}
void
caja_image_properties_page_register (void)
{
caja_module_add_type (caja_image_properties_page_provider_get_type ());
}
|