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

/*
 *  GThumb
 *
 *  Copyright (C) 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
 *  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 <glib/gi18n.h>
#include <gio/gio.h>
#include "gth-edit-comment-page.h"
27
#include "gth-edit-metadata-dialog.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
28
29
30
31
32
33
34
35
36
37


#define GTH_EDIT_COMMENT_PAGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTH_TYPE_EDIT_COMMENT_PAGE, GthEditCommentPagePrivate))
#define GET_WIDGET(name) _gtk_builder_get_widget (self->priv->builder, (name))


typedef enum {
	NO_DATE = 0,
	FOLLOWING_DATE,
	CURRENT_DATE,
38
	PHOTO_DATE,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
39
40
41
42
43
44
45
46
47
48
	LAST_MODIFIED_DATE,
	CREATION_DATE,
	NO_CHANGE
} DateOption;


static gpointer gth_edit_comment_page_parent_class = NULL;


struct _GthEditCommentPagePrivate {
49
50
51
52
53
	GFileInfo  *info;
	GtkBuilder *builder;
	GtkWidget  *date_combobox;
	GtkWidget  *date_selector;
	GtkWidget  *tags_entry;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
54
55
56
57
58
59
60
};


void
gth_edit_comment_page_real_set_file (GthEditMetadataPage *base,
		 		     GthFileData         *file_data)
{
61
62
63
64
65
66
	GthEditCommentPage  *self;
	GtkTextBuffer       *buffer;
	GthMetadata         *metadata;
	GthStringList       *tags;
	GthMetadataProvider *provider;
	gboolean             no_provider;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
67
68
69

	self = GTH_EDIT_COMMENT_PAGE (base);

70
71
72
	_g_object_unref (self->priv->info);
	self->priv->info = g_file_info_new ();
	g_file_info_copy_into (file_data->info, self->priv->info);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
73
74

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (GET_WIDGET ("note_text")));
75
	metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::description");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
76
77
78
79
80
81
82
83
84
85
	if (metadata != NULL) {
		GtkTextIter iter;

		gtk_text_buffer_set_text (buffer, gth_metadata_get_formatted (metadata), -1);
		gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
		gtk_text_buffer_place_cursor (buffer, &iter);
	}
	else
		gtk_text_buffer_set_text (buffer, "", -1);

86
87
88
89
90
91
	metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::title");
	if (metadata != NULL)
		gtk_entry_set_text (GTK_ENTRY (GET_WIDGET ("title_entry")), gth_metadata_get_formatted (metadata));
	else
		gtk_entry_set_text (GTK_ENTRY (GET_WIDGET ("title_entry")), "");

92
	metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::location");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
93
94
95
96
97
	if (metadata != NULL)
		gtk_entry_set_text (GTK_ENTRY (GET_WIDGET ("place_entry")), gth_metadata_get_formatted (metadata));
	else
		gtk_entry_set_text (GTK_ENTRY (GET_WIDGET ("place_entry")), "");

98
	metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::datetime");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
99
100
	if (metadata != NULL) {
		gtk_combo_box_set_active (GTK_COMBO_BOX (self->priv->date_combobox), FOLLOWING_DATE);
101
		gth_time_selector_set_exif_date (GTH_TIME_SELECTOR (self->priv->date_selector), gth_metadata_get_raw (metadata));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
102
103
104
	}
	else {
		gtk_combo_box_set_active (GTK_COMBO_BOX (self->priv->date_combobox), NO_DATE);
105
		gth_time_selector_set_exif_date (GTH_TIME_SELECTOR (self->priv->date_selector), "");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
106
107
	}

108
	tags = (GthStringList *) g_file_info_get_attribute_object (file_data->info, "general::tags");
109
110
111
112
	if (tags != NULL) {
		char *value;

		value = gth_string_list_join (tags, ", ");
113
		gth_tags_entry_set_text (GTH_TAGS_ENTRY (self->priv->tags_entry), value);
114
115
116
117

		g_free (value);
	}
	else
118
		gth_tags_entry_set_text (GTH_TAGS_ENTRY (self->priv->tags_entry), "");
119

Paolo Bacchilega's avatar
Paolo Bacchilega committed
120
	gtk_widget_grab_focus (GET_WIDGET ("note_text"));
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

	no_provider = TRUE;

  	provider = gth_main_get_metadata_writer ("general::description", gth_file_data_get_mime_type (file_data));
	gtk_widget_set_sensitive (GET_WIDGET ("note_text"), provider != NULL);
	if (no_provider && (provider != NULL))
		no_provider = FALSE;
	_g_object_unref (provider);

	provider = gth_main_get_metadata_writer ("general::location", gth_file_data_get_mime_type (file_data));
	gtk_widget_set_sensitive (GET_WIDGET ("place_entry"), provider != NULL);
	if (no_provider && (provider != NULL))
		no_provider = FALSE;
	_g_object_unref (provider);

	provider = gth_main_get_metadata_writer ("general::datetime", gth_file_data_get_mime_type (file_data));
	gtk_widget_set_sensitive (self->priv->date_combobox, provider != NULL);
	gtk_widget_set_sensitive (self->priv->date_selector, provider != NULL);
	if (no_provider && (provider != NULL))
		no_provider = FALSE;
	_g_object_unref (provider);

	provider = gth_main_get_metadata_writer ("general::tags", gth_file_data_get_mime_type (file_data));
	gtk_widget_set_sensitive (self->priv->tags_entry, provider != NULL);
	if (no_provider && (provider != NULL))
		no_provider = FALSE;
	_g_object_unref (provider);

149
150
151
152
153
154
	provider = gth_main_get_metadata_writer ("general::rating", gth_file_data_get_mime_type (file_data));
	gtk_widget_set_sensitive (GET_WIDGET ("rating_spinbutton"), provider != NULL);
	if (no_provider && (provider != NULL))
		no_provider = FALSE;
	_g_object_unref (provider);

155
156
157
158
	if (no_provider)
		gtk_widget_hide (GTK_WIDGET (self));
	else
		gtk_widget_show (GTK_WIDGET (self));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
159
160
161
162
163
164
165
}


void
gth_edit_comment_page_real_update_info (GthEditMetadataPage *base,
					GFileInfo           *info)
{
166
167
168
169
170
	GthEditCommentPage  *self;
	GtkTextBuffer       *buffer;
	GtkTextIter          start;
	GtkTextIter          end;
	char                *text;
171
	GthMetadata         *metadata;
172
173
174
175
	int                  i;
	char               **tagv;
	GList               *tags;
	GthStringList       *string_list;
176
177
	GthDateTime         *date_time;
	char                *exif_date;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
178
179
180

	self = GTH_EDIT_COMMENT_PAGE (base);

181
182
183
184
185
186
187
188
189
190
	/* caption */

	metadata = g_object_new (GTH_TYPE_METADATA,
				 "id", "general::title",
				 "raw", gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("title_entry"))),
				 "formatted", gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("title_entry"))),
				 NULL);
	g_file_info_set_attribute_object (info, "general::title", G_OBJECT (metadata));
	g_object_unref (metadata);

191
192
	/* comment */

Paolo Bacchilega's avatar
Paolo Bacchilega committed
193
194
195
	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (GET_WIDGET ("note_text")));
	gtk_text_buffer_get_bounds (buffer, &start, &end);
	text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
196
	metadata = g_object_new (GTH_TYPE_METADATA,
197
				 "id", "general::description",
198
199
200
				 "raw", text,
				 "formatted", text,
				 NULL);
201
	g_file_info_set_attribute_object (info, "general::description", G_OBJECT (metadata));
202
	g_object_unref (metadata);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
203
204
	g_free (text);

205
206
207
	/* location */

	metadata = g_object_new (GTH_TYPE_METADATA,
208
				 "id", "general::location",
209
210
211
				 "raw", gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("place_entry"))),
				 "formatted", gtk_entry_get_text (GTK_ENTRY (GET_WIDGET ("place_entry"))),
				 NULL);
212
	g_file_info_set_attribute_object (info, "general::location", G_OBJECT (metadata));
213
214
215
216
	g_object_unref (metadata);

	/* date */

217
218
219
	date_time = gth_datetime_new ();
	gth_time_selector_get_value (GTH_TIME_SELECTOR (self->priv->date_selector), date_time);
	exif_date = gth_datetime_to_exif_date (date_time);
220
	metadata = g_object_new (GTH_TYPE_METADATA,
221
				 "id", "general::datetime",
222
223
				 "raw", exif_date,
				 "formatted", exif_date,
224
				 NULL);
225
	g_file_info_set_attribute_object (info, "general::datetime", G_OBJECT (metadata));
226
	g_object_unref (metadata);
227
	gth_datetime_free (date_time);
228
229

	/* tags */
230

231
	tagv = gth_tags_entry_get_tags (GTH_TAGS_ENTRY (self->priv->tags_entry), TRUE);
232
	tags = NULL;
233
234
	for (i = 0; tagv[i] != NULL; i++)
		tags = g_list_prepend (tags, tagv[i]);
235
236
	tags = g_list_reverse (tags);
	string_list = gth_string_list_new (tags);
237
	g_file_info_set_attribute_object (info, "general::tags", G_OBJECT (string_list));
238

239
	g_free (exif_date);
240
241
	g_object_unref (string_list);
	g_strfreev (tagv);
242
	g_list_free (tags);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
243
244
245
246
247
248
}


const char *
gth_edit_comment_page_real_get_name (GthEditMetadataPage *self)
{
249
	return _("General");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
250
251
252
253
254
255
256
257
258
259
}


static void
gth_edit_comment_page_finalize (GObject *object)
{
	GthEditCommentPage *self;

	self = GTH_EDIT_COMMENT_PAGE (object);

260
	_g_object_unref (self->priv->info);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
	g_object_unref (self->priv->builder);

	G_OBJECT_CLASS (gth_edit_comment_page_parent_class)->finalize (object);
}


static void
gth_edit_comment_page_class_init (GthEditCommentPageClass *klass)
{
	gth_edit_comment_page_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GthEditCommentPagePrivate));

	G_OBJECT_CLASS (klass)->finalize = gth_edit_comment_page_finalize;
}


static char *
get_date_from_option (GthEditCommentPage *self,
		      DateOption          option)
{
	GTimeVal     timeval;
282
283
	GthDateTime *date_time;
	char        *exif_date;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
284
285
286
287
288
289
290
291
	GthMetadata *metadata;

	_g_time_val_reset (&timeval);

	switch (option) {
	case NO_DATE:
		return g_strdup ("");
	case FOLLOWING_DATE:
292
293
		date_time = gth_datetime_new ();
		gth_time_selector_get_value (GTH_TIME_SELECTOR (self->priv->date_selector), date_time);
294
		exif_date = gth_datetime_to_exif_date (date_time);
295
296
297
		_g_time_val_from_exif_date (exif_date, &timeval);
		g_free (exif_date);
		gth_datetime_free (date_time);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
298
299
300
301
		break;
	case CURRENT_DATE:
		g_get_current_time (&timeval);
		break;
302
	case PHOTO_DATE:
303
		metadata = (GthMetadata *) g_file_info_get_attribute_object (self->priv->info, "Embedded::Photo::DateTimeOriginal");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
304
305
306
307
308
309
		if (metadata != NULL)
			_g_time_val_from_exif_date (gth_metadata_get_raw (metadata), &timeval);
		else
			return g_strdup ("");
		break;
	case LAST_MODIFIED_DATE:
310
311
		timeval.tv_sec = g_file_info_get_attribute_uint64 (self->priv->info, "time::modified");
		timeval.tv_usec = g_file_info_get_attribute_uint32 (self->priv->info, "time::modified-usec");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
312
313
		break;
	case CREATION_DATE:
314
315
		timeval.tv_sec = g_file_info_get_attribute_uint64 (self->priv->info, "time::created");
		timeval.tv_usec = g_file_info_get_attribute_uint32 (self->priv->info, "time::created-usec");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
316
317
		break;
	case NO_CHANGE:
318
		metadata = (GthMetadata *) g_file_info_get_attribute_object (self->priv->info, "general::datetime");
319
320
		if (metadata != NULL)
			_g_time_val_from_exif_date (gth_metadata_get_raw (metadata), &timeval);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
		else
			return g_strdup ("");
		break;
	}

	return _g_time_val_to_exif_date (&timeval);
}


static void
date_combobox_changed_cb (GtkComboBox *widget,
			  gpointer     user_data)
{
	GthEditCommentPage *self = user_data;
	char               *value;

	value = get_date_from_option (self, gtk_combo_box_get_active (widget));
338
	gth_time_selector_set_exif_date (GTH_TIME_SELECTOR (self->priv->date_selector), value);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
339
340
341
342
343
344
345
346
347

	g_free (value);
}


static void
gth_edit_comment_page_init (GthEditCommentPage *self)
{
	self->priv = GTH_EDIT_COMMENT_PAGE_GET_PRIVATE (self);
348
	self->priv->info = NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
349
350
351

	gtk_container_set_border_width (GTK_CONTAINER (self), 12);

352
	self->priv->builder = _gtk_builder_new_from_file ("edit-comment-page.ui", "edit_metadata");
Paolo Bacchilega's avatar
Paolo Bacchilega committed
353
354
355
356
357
358
359
  	gtk_box_pack_start (GTK_BOX (self), _gtk_builder_get_widget (self->priv->builder, "content"), TRUE, TRUE, 0);

  	self->priv->date_combobox = gtk_combo_box_new_text ();
  	_gtk_combo_box_append_texts (GTK_COMBO_BOX (self->priv->date_combobox),
  				     _("No date"),
  				     _("The following date"),
  				     _("Current date"),
360
  				     _("Date photo was taken"),
Paolo Bacchilega's avatar
Paolo Bacchilega committed
361
362
363
364
365
  				     _("Last modified date"),
  				     _("File creation date"),
  				     _("Do not modify"),
  				     NULL);
  	gtk_widget_show (self->priv->date_combobox);
366
  	gtk_box_pack_start (GTK_BOX (GET_WIDGET ("date_combobox_container")), self->priv->date_combobox, TRUE, TRUE, 0);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
367
368

  	g_signal_connect (self->priv->date_combobox,
369
370
371
			  "changed",
			  G_CALLBACK (date_combobox_changed_cb),
			  self);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
372

373
374
375
  	self->priv->date_selector = gth_time_selector_new ();
  	gtk_widget_show (self->priv->date_selector);
  	gtk_box_pack_start (GTK_BOX (GET_WIDGET ("date_selector_container")), self->priv->date_selector, FALSE, FALSE, 0);
376

377
  	self->priv->tags_entry = gth_tags_entry_new ();
378
  	gtk_widget_show (self->priv->tags_entry);
379
  	gtk_box_pack_start (GTK_BOX (GET_WIDGET ("tags_entry_container")), self->priv->tags_entry, FALSE, FALSE, 0);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
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
}


static void
gth_edit_comment_page_gth_edit_comment_page_interface_init (GthEditMetadataPageIface *iface)
{
	iface->set_file = gth_edit_comment_page_real_set_file;
	iface->update_info = gth_edit_comment_page_real_update_info;
	iface->get_name = gth_edit_comment_page_real_get_name;
}


GType
gth_edit_comment_page_get_type (void)
{
	static GType type = 0;

	if (type == 0) {
		static const GTypeInfo g_define_type_info = {
			sizeof (GthEditCommentPageClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) gth_edit_comment_page_class_init,
			(GClassFinalizeFunc) NULL,
			NULL,
			sizeof (GthEditCommentPage),
			0,
			(GInstanceInitFunc) gth_edit_comment_page_init,
			NULL
		};
		static const GInterfaceInfo gth_edit_comment_page_info = {
			(GInterfaceInitFunc) gth_edit_comment_page_gth_edit_comment_page_interface_init,
			(GInterfaceFinalizeFunc) NULL,
			NULL
		};
		type = g_type_register_static (GTK_TYPE_VBOX,
					       "GthEditCommentPage",
					       &g_define_type_info,
					       0);
		g_type_add_interface_static (type, GTH_TYPE_EDIT_METADATA_PAGE, &gth_edit_comment_page_info);
	}

	return type;
}