gth-comment.c 12.4 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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2009 The 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
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <string.h>
#include <gthumb.h>
#include "gth-comment.h"


#define COMMENT_VERSION "3.0"
#define GTH_COMMENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTH_TYPE_COMMENT, GthCommentPrivate))


struct _GthCommentPrivate { /* All strings in utf8 format. */
	char       *note;
	char       *place;           
	GPtrArray  *categories;
	GDate      *date;
	GthTime    *time_of_day;	
	gboolean    changed;
	gboolean    utf8;
};

static gpointer gth_comment_parent_class = NULL;


static void
gth_comment_free_data (GthComment *self)
{
	if (self->priv->place != NULL) {
		g_free (self->priv->place);
		self->priv->place = NULL;
	}

	if (self->priv->note != NULL) {
		g_free (self->priv->note);
		self->priv->note = NULL;
	}
}


static void 
gth_comment_finalize (GObject *obj) 
{
	GthComment *self = GTH_COMMENT (obj);
	
	gth_comment_free_data (self);
	gth_comment_clear_categories (self);
	g_ptr_array_free (self->priv->categories, TRUE);
	g_date_free (self->priv->date);
	gth_time_free (self->priv->time_of_day);
		
	G_OBJECT_CLASS (gth_comment_parent_class)->finalize (obj);
}


static void 
gth_comment_class_init (GthCommentClass *klass) 
{
	gth_comment_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GthCommentPrivate));
	
	G_OBJECT_CLASS (klass)->finalize = gth_comment_finalize;
}


static void 
gth_comment_instance_init (GthComment *self) 
{
	self->priv = GTH_COMMENT_GET_PRIVATE (self);
	self->priv->categories = g_ptr_array_new ();
	self->priv->date = g_date_new ();
	self->priv->time_of_day = gth_time_new ();
}


static GObject * 
gth_comment_real_duplicate (GthDuplicable *base) 
{
	return (GObject *) gth_comment_dup ((GthComment*) base);
}


static void 
gth_comment_gth_duplicable_interface_init (GthDuplicableIface *iface) 
{
	iface->duplicate = gth_comment_real_duplicate;
}


static DomElement*
gth_comment_real_create_element (DomDomizable *base,
				 DomDocument  *doc)
{
	GthComment *self;
	DomElement *element;
	char       *value;
	GPtrArray  *categories;
	DomElement *categories_element;
	int         i;
	
	g_return_val_if_fail (DOM_IS_DOCUMENT (doc), NULL);

	self = GTH_COMMENT (base);
	element = dom_document_create_element (doc, "comment",
					       "version", COMMENT_VERSION,
					       NULL);

	dom_element_append_child (element, dom_document_create_element_with_text (doc, self->priv->note, "note", NULL));
	dom_element_append_child (element, dom_document_create_element_with_text (doc, self->priv->place, "place", NULL));
	
	value = gth_comment_get_time_as_exif_format (self);
	if (value != NULL) {
		dom_element_append_child (element, dom_document_create_element (doc,  "time", "value", value, NULL));
		g_free (value);
	}

	categories = gth_comment_get_categories (self);
	categories_element = dom_document_create_element (doc, "categories", NULL);
	dom_element_append_child (element, categories_element);	
	for (i = 0; i < categories->len; i++) 
		dom_element_append_child (categories_element, dom_document_create_element (doc, "category", "value", g_ptr_array_index (categories, i), NULL));
	
	return element;
}


static void
gth_comment_real_load_from_element (DomDomizable *base,
				    DomElement   *element)
{
	GthComment *self;
	DomElement *node;

	g_return_if_fail (DOM_IS_ELEMENT (element));

	self = GTH_COMMENT (base);
	gth_comment_reset (self);

	if (g_strcmp0 (dom_element_get_attribute (element, "format"), "2.0") == 0) {
		for (node = element->first_child; node; node = node->next_sibling) {
			if (g_strcmp0 (node->tag_name, "Note") == 0) 
				gth_comment_set_note (self, dom_element_get_inner_text (node));
			else if (g_strcmp0 (node->tag_name, "Place") == 0) 
				gth_comment_set_place (self, dom_element_get_inner_text (node));
			else if (g_strcmp0 (node->tag_name, "Time") == 0) 
				gth_comment_set_time_from_time_t (self, atol (dom_element_get_inner_text (node)));
			else if (g_strcmp0 (node->tag_name, "Keywords") == 0) {
				char **categories;
				int    i;
				
				categories = g_strsplit (dom_element_get_inner_text (node), ",", -1);
				for (i = 0; categories[i] != NULL; i++)
					gth_comment_add_category (self, categories[i]);
				g_strfreev (categories);
			}
		}
	}
	else if (g_strcmp0 (dom_element_get_attribute (element, "version"), "3.0") == 0) {
		for (node = element->first_child; node; node = node->next_sibling) {
			if (g_strcmp0 (node->tag_name, "note") == 0) 
				gth_comment_set_note (self, dom_element_get_inner_text (node));
			else if (g_strcmp0 (node->tag_name, "place") == 0) 
				gth_comment_set_place (self, dom_element_get_inner_text (node));
			else if (g_strcmp0 (node->tag_name, "time") == 0) 
				gth_comment_set_time_from_exif_format (self, dom_element_get_attribute (node, "value"));
			else if (g_strcmp0 (node->tag_name, "categories") == 0) {
				DomElement *child;
				
				for (child = node->first_child; child != NULL; child = child->next_sibling)
					if (strcmp (child->tag_name, "category") == 0)
						gth_comment_add_category (self, dom_element_get_attribute (child, "value"));
			}
		}
	}
}


static void
gth_comment_dom_domizable_interface_init (DomDomizableIface *iface)
{
	iface->create_element = gth_comment_real_create_element;
	iface->load_from_element = gth_comment_real_load_from_element;
}


GType 
gth_comment_get_type (void) 
{
	static GType gth_comment_type_id = 0;
	
	if (gth_comment_type_id == 0) {
		static const GTypeInfo g_define_type_info = { 
			sizeof (GthCommentClass), 
			(GBaseInitFunc) NULL, 
			(GBaseFinalizeFunc) NULL, 
			(GClassInitFunc) gth_comment_class_init, 
			(GClassFinalizeFunc) NULL, 
			NULL, 
			sizeof (GthComment), 
			0, 
			(GInstanceInitFunc) gth_comment_instance_init, 
			NULL 
		};
		static const GInterfaceInfo gth_duplicable_info = { 
			(GInterfaceInitFunc) gth_comment_gth_duplicable_interface_init, 
			(GInterfaceFinalizeFunc) NULL, 
			NULL
		};
		static const GInterfaceInfo dom_domizable_info = {
			(GInterfaceInitFunc) gth_comment_dom_domizable_interface_init,
			(GInterfaceFinalizeFunc) NULL,
			NULL
		};
		gth_comment_type_id = g_type_register_static (G_TYPE_OBJECT, "GthComment", &g_define_type_info, 0);
		g_type_add_interface_static (gth_comment_type_id, GTH_TYPE_DUPLICABLE, &gth_duplicable_info);
		g_type_add_interface_static (gth_comment_type_id, DOM_TYPE_DOMIZABLE, &dom_domizable_info);
	}
	
	return gth_comment_type_id;
}


GthComment * 
gth_comment_new (void) 
{
	return g_object_new (GTH_TYPE_COMMENT, NULL);
}


GFile *
gth_comment_get_comment_file (GFile *file)
{
	GFile *parent;
	char  *basename;
	char  *comment_basename;
	GFile *comment_file;
	
	parent = g_file_get_parent (file);
	if (parent == NULL)
		return NULL;
	
	basename = g_file_get_basename (file);
	comment_basename = g_strconcat (basename, ".xml", NULL);
	comment_file = _g_file_get_child (parent, ".comments", comment_basename, NULL);

	g_free (comment_basename);
	g_free (basename);
	g_object_unref (parent);
	
	return comment_file;
}


GthComment *
gth_comment_new_for_file (GFile   *file,
			  GError **error)
{
	GFile       *comment_file;
	GthComment  *comment;
	void        *zipped_buffer;
	gsize        zipped_size;
	void        *buffer;
	gsize        size;
	DomDocument *doc;
	
	comment_file = gth_comment_get_comment_file (file);
	if (! g_load_file_in_buffer (comment_file, &zipped_buffer, &zipped_size, error)) {
		g_object_unref (comment_file); 
		return NULL;
	}
	g_object_unref (comment_file);
		
	if ((zipped_buffer != NULL) && (((char *) zipped_buffer)[0] != '<')) {
		if (! zlib_decompress_buffer (zipped_buffer, zipped_size, &buffer, &size)) 
			return NULL;
	}
	else {
		buffer = zipped_buffer;
		size = zipped_size;
		
		zipped_buffer = NULL;
	}
	
	comment = gth_comment_new ();
	doc = dom_document_new ();
	if (dom_document_load (doc, buffer, size, error)) {
		dom_domizable_load_from_element (DOM_DOMIZABLE (comment), DOM_ELEMENT (doc)->first_child);
	}
	else {
		buffer = NULL;
		g_object_unref (comment);
		comment = NULL;
	}
	
	g_free (buffer);
	g_free (zipped_buffer);
	
	return comment;
}


char *
gth_comment_to_data (GthComment *comment,
		     gsize      *length)
{
	DomDocument *doc;
	char        *data;
		
	doc = dom_document_new ();
	dom_element_append_child (DOM_ELEMENT (doc), dom_domizable_create_element (DOM_DOMIZABLE (comment), doc));
	data = dom_document_dump (doc, length);

	g_object_unref (doc);

	return data;
}


GthComment *
gth_comment_dup (GthComment *self)
{
	GthComment *comment;
343
344
	char       *time;
	int         i;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
345
346
347
348

	if (self == NULL)
		return NULL;

349
350
351
352
353
354
355
356
357
	comment = gth_comment_new ();
	gth_comment_set_note (comment, gth_comment_get_note (self));
	gth_comment_set_place (comment, gth_comment_get_place (self));
	time = gth_comment_get_time_as_exif_format (self);
	gth_comment_set_time_from_exif_format (comment, time);
	for (i = 0; i < self->priv->categories->len; i++)
		gth_comment_add_category (comment, g_ptr_array_index (self->priv->categories, i));

	g_free (time);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
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

	return comment;
}


void 
gth_comment_reset (GthComment *self)
{
	gth_comment_free_data (self);
	gth_comment_clear_categories (self);
	gth_comment_reset_time (self);
}


void 
gth_comment_set_note (GthComment *comment,
		      const char *value)
{
	g_free (comment->priv->note);
	comment->priv->note = NULL;
	
	if (value != NULL)
		comment->priv->note = g_strdup (value);
}


void
gth_comment_set_place (GthComment *comment,
		       const char *value)
{
	g_free (comment->priv->place);
	comment->priv->place = NULL;
	
	if (value != NULL)
		comment->priv->place = g_strdup (value);
}


void
gth_comment_clear_categories (GthComment *self)
{
	g_ptr_array_foreach (self->priv->categories, (GFunc) g_free, NULL);
	g_ptr_array_free (self->priv->categories, TRUE);
	self->priv->categories = g_ptr_array_new ();
}


void
gth_comment_add_category (GthComment *comment,
			  const char *value)
{
	g_return_if_fail (value != NULL);
	
	g_ptr_array_add (comment->priv->categories, g_strdup (value));
}


void
gth_comment_reset_time (GthComment *self) 
{
	g_date_clear (self->priv->date, 1);
	gth_time_clear (self->priv->time_of_day);
}


void
gth_comment_set_time_from_exif_format (GthComment *comment,
				       const char *value)
{
	unsigned int y, m, d, hh, mm, ss;
	
	gth_comment_reset_time (comment);
	
	if ((value == NULL) || (*value == '\0'))
		return;
	
	if (sscanf (value, "%u:%u:%u %u:%u:%u", &y, &m, &d, &hh, &mm, &ss) != 6) {
		g_warning ("invalid time format: %s", value);
		return;
	}
	
	g_date_set_dmy (comment->priv->date, d, m, y);
	gth_time_set_hms (comment->priv->time_of_day, hh, mm, ss, 0);
}


void
gth_comment_set_time_from_time_t (GthComment *comment,
				  time_t      value)
{
	struct tm *tm;
	
	if (value == 0)
		return;
		
	tm = localtime (&value);
	g_date_set_dmy (comment->priv->date, tm->tm_mday, tm->tm_mon + 1, 1900 + tm->tm_year);
	gth_time_set_hms (comment->priv->time_of_day, tm->tm_hour, tm->tm_min, tm->tm_sec, 0);
}


const char *
gth_comment_get_note (GthComment *comment)
{
	return comment->priv->note;
}


const char *
gth_comment_get_place (GthComment *comment)
{
	return comment->priv->place;
}


GPtrArray *
gth_comment_get_categories (GthComment *comment)
{
	return comment->priv->categories;
}


GDate * 
gth_comment_get_date (GthComment *comment)
{
	return comment->priv->date;
}


GthTime *
gth_comment_get_time_of_day (GthComment *comment)
{
	return comment->priv->time_of_day;
}


char *
gth_comment_get_time_as_exif_format (GthComment *comment)
{
	char *s;
	
	if (! g_date_valid (comment->priv->date))
		return NULL;
	
	s = g_strdup_printf ("%04u:%02u:%02u %02u:%02u:%02u",
			     g_date_get_year (comment->priv->date),
			     g_date_get_month (comment->priv->date),
			     g_date_get_day (comment->priv->date),
			     comment->priv->time_of_day->hour,
			     comment->priv->time_of_day->min,
			     comment->priv->time_of_day->sec);
			     
	return s;
}