main.c 6.64 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
/* -*- 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 <gtk/gtk.h>
#include <gthumb.h>
#include "gth-comment.h"
#include "gth-edit-comment-page.h"
#include "gth-metadata-provider-comment.h"
#include "gth-test-category.h"


GthMetadataCategory comments_metadata_category[] = {
	{ "comment", N_("Comment"), 20 },
	{ NULL, NULL, 0 }
};


GthMetadataInfo comments_metadata_info[] = {
40
41
42
43
44
	{ "comment::note", "", "comment", 1, GTH_METADATA_ALLOW_NOWHERE },
	{ "comment::place", "", "comment", 2, GTH_METADATA_ALLOW_NOWHERE },
	{ "comment::time", "", "comment", 3, GTH_METADATA_ALLOW_NOWHERE },
	{ "comment::categories", "", "comment", 4, GTH_METADATA_ALLOW_NOWHERE },
	{ "comment::rating", "", "comment", 5, GTH_METADATA_ALLOW_NOWHERE },
Paolo Bacchilega's avatar
Paolo Bacchilega committed
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
	{ NULL, NULL, NULL, 0, 0 }
};


static gint64
get_comment_for_test (GthTest       *test,
		      GthFileData   *file,
		      gconstpointer *data)
{
	*data = g_file_info_get_attribute_string (file->info, "comment::note");
	return 0;
}


static gint64
get_place_for_test (GthTest       *test,
		    GthFileData   *file,
		    gconstpointer *data)
{
	*data = g_file_info_get_attribute_string (file->info, "comment::place");
	return 0;
}


void
70
comments__add_sidecars_cb (GFile  *file,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
71
72
			   GList **sidecars)
{
73
	*sidecars = g_list_prepend (*sidecars, gth_comment_get_comment_file (file));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
74
75
76
}


77
78
79
80
void
comments__read_metadata_ready_cb (GthFileData *file_data,
				  const char  *attributes)
{
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
	gboolean       write_comment = FALSE;
	GthMetadata   *metadata;
	const char    *text;
	GthComment    *comment;
	GPtrArray     *keywords;
	int            i;
	GthStringList *categories;

	comment = gth_comment_new ();
	gth_comment_set_note (comment, g_file_info_get_attribute_string (file_data->info, "comment::note"));
	gth_comment_set_place (comment, g_file_info_get_attribute_string (file_data->info, "comment::place"));

	metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
	if (metadata != NULL)
		gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));

	keywords = gth_comment_get_categories (comment);
	for (i = 0; i < keywords->len; i++)
		gth_comment_add_category (comment, g_ptr_array_index (keywords, i));

	/* sync embedded data and .comment data if required */

	metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::comment");
	if (metadata != NULL) {
		text = g_file_info_get_attribute_string (file_data->info, "comment::note");
		if (g_strcmp0 (gth_metadata_get_raw (metadata), text) != 0) {
			gth_comment_set_note (comment, gth_metadata_get_raw (metadata));
			write_comment = TRUE;
		}
	}
111

112
113
114
115
116
117
	metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::location");
	if (metadata != NULL) {
		text = g_file_info_get_attribute_string (file_data->info, "comment::place");
		if (g_strcmp0 (gth_metadata_get_raw (metadata), text) != 0) {
			gth_comment_set_place (comment, gth_metadata_get_raw (metadata));
			write_comment = TRUE;
118
		}
119
	}
120

121
122
123
	metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::datetime");
	if (metadata != NULL) {
		metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
124
		if (metadata != NULL) {
125
			text = gth_metadata_get_raw (metadata);
126
			if (g_strcmp0 (gth_metadata_get_raw (metadata), text) != 0) {
127
				gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
128
129
130
				write_comment = TRUE;
			}
		}
131
	}
132

133
134
135
	categories = (GthStringList *) g_file_info_get_attribute_object (file_data->info, "general::tags");
	if (categories != NULL) {
		GthStringList *comment_categories;
136

137
138
139
140
		comment_categories = (GthStringList *) g_file_info_get_attribute_object (file_data->info, "comment::categories");
		if (! gth_string_list_equal (categories, comment_categories)) {
			GList *list;
			GList *scan;
141

142
143
144
145
146
			gth_comment_clear_categories (comment);
			list = gth_string_list_get_list (categories);
			for (scan = list; scan; scan = scan->next)
				gth_comment_add_category (comment, scan->data);
			write_comment = TRUE;
147
		}
148
	}
149

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
	if (write_comment) {
		GFile *comment_file;
		char  *buffer;
		gsize  size;

		buffer = gth_comment_to_data (comment, &size);
		comment_file = gth_comment_get_comment_file (file_data->file);
		g_write_file (comment_file,
			      FALSE,
			      G_FILE_CREATE_NONE,
			      buffer,
			      size,
			      NULL,
			      NULL);

		g_object_unref (comment_file);
		g_free (buffer);
167
	}
168
169

	g_object_unref (comment);
170
171
172
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
173
174
175
176
177
178
179
G_MODULE_EXPORT void
gthumb_extension_activate (void)
{
	gth_main_register_metadata_category (comments_metadata_category);
	gth_main_register_metadata_info_v (comments_metadata_info);
	gth_main_register_metadata_provider (GTH_TYPE_METADATA_PROVIDER_COMMENT);
	gth_main_register_type ("edit-metadata-dialog-page", GTH_TYPE_EDIT_COMMENT_PAGE);
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	gth_main_register_object (GTH_TYPE_TEST,
				  "comment::note",
				  GTH_TYPE_TEST_SIMPLE,
				  "attributes", "comment::note",
				  "display-name", _("Comment"),
				  "data-type", GTH_TEST_DATA_TYPE_STRING,
				  "get-data-func", get_comment_for_test,
				  NULL);
	gth_main_register_object (GTH_TYPE_TEST,
				  "comment::place",
				  GTH_TYPE_TEST_SIMPLE,
				  "attributes", "comment::place",
				  "display-name", _("Place"),
				  "data-type", GTH_TEST_DATA_TYPE_STRING,
				  "get-data-func", get_place_for_test,
				  NULL);
	gth_main_register_object (GTH_TYPE_TEST,
				  "comment::category",
				  GTH_TYPE_TEST_CATEGORY,
				  "display-name", _("Tag"),
				  NULL);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
201
	gth_hook_add_callback ("add-sidecars", 10, G_CALLBACK (comments__add_sidecars_cb), NULL);
202
	gth_hook_add_callback ("read-metadata-ready", 10, G_CALLBACK (comments__read_metadata_ready_cb), NULL);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
}


G_MODULE_EXPORT void
gthumb_extension_deactivate (void)
{
}


G_MODULE_EXPORT gboolean
gthumb_extension_is_configurable (void)
{
	return FALSE;
}


G_MODULE_EXPORT void
gthumb_extension_configure (GtkWindow *parent)
{
}