main.c 6.76 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
/* -*- 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[] = {
	{ "comment::note", N_("Comment"), "comment", 1, GTH_METADATA_ALLOW_NOWHERE },
	{ "comment::place", N_("Place"), "comment", 2, GTH_METADATA_ALLOW_EVERYWHERE },
42
	{ "comment::time", N_("Comment Date"), "comment", 3, GTH_METADATA_ALLOW_EVERYWHERE },
43
	{ "comment::categories", N_("Tags"), "comment", 4, GTH_METADATA_ALLOW_EVERYWHERE },
Paolo Bacchilega's avatar
Paolo Bacchilega committed
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
	{ "comment::rating", N_("Rating"), "comment", 5, GTH_METADATA_ALLOW_EVERYWHERE },
	{ 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
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
void
comments__read_metadata_ready_cb (GthFileData *file_data,
				  const char  *attributes)
{
	if (_g_file_attributes_matches (attributes, "Embedded::Image::*")
	    && _g_file_attributes_matches (attributes, "comment::*"))
	{
		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"));
		gth_comment_set_time_from_exif_format (comment, g_file_info_get_attribute_string (file_data->info, "comment::time"));
		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, "Embedded::Image::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;
			}
		}

		metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "Embedded::Image::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;
			}
		}

		metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "Embedded::Image::Date");
		if (metadata != NULL) {
			text = g_file_info_get_attribute_string (file_data->info, "comment::time");
			if (g_strcmp0 (gth_metadata_get_raw (metadata), text) != 0) {
				gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
				write_comment = TRUE;
			}
		}

		categories = (GthStringList *) g_file_info_get_attribute_object (file_data->info, "Embedded::Image::Keywords");
		if (categories != NULL) {
			GthStringList *comment_categories;

			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;

				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;
			}
		}

		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);
		}

		g_object_unref (comment);
	}
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
170
171
172
173
174
175
176
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);
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
	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
198
	gth_hook_add_callback ("add-sidecars", 10, G_CALLBACK (comments__add_sidecars_cb), NULL);
199
	gth_hook_add_callback ("read-metadata-ready", 10, G_CALLBACK (comments__read_metadata_ready_cb), NULL);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
}


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)
{
}