2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "ev-metadata.h"
33 struct _EvMetadataClass {
34 GObjectClass base_class;
37 G_DEFINE_TYPE (EvMetadata, ev_metadata, G_TYPE_OBJECT)
39 #define EV_METADATA_NAMESPACE "metadata::evince"
42 ev_metadata_finalize (GObject *object)
44 EvMetadata *metadata = EV_METADATA (object);
46 if (metadata->items) {
47 g_hash_table_destroy (metadata->items);
48 metadata->items = NULL;
52 g_object_unref (metadata->file);
53 metadata->file = NULL;
56 G_OBJECT_CLASS (ev_metadata_parent_class)->finalize (object);
60 ev_metadata_init (EvMetadata *metadata)
62 metadata->items = g_hash_table_new_full (g_str_hash,
69 ev_metadata_class_init (EvMetadataClass *klass)
71 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
73 gobject_class->finalize = ev_metadata_finalize;
77 ev_metadata_load (EvMetadata *metadata)
84 info = g_file_query_info (metadata->file, "metadata::*", 0, NULL, &error);
86 g_warning ("%s", error->message);
92 if (!g_file_info_has_namespace (info, "metadata")) {
93 g_object_unref (info);
98 attrs = g_file_info_list_attributes (info, "metadata");
99 for (i = 0; attrs[i]; i++) {
100 GFileAttributeType type;
104 if (!g_str_has_prefix (attrs[i], EV_METADATA_NAMESPACE))
107 if (!g_file_info_get_attribute_data (info, attrs[i],
108 &type, &value, NULL)) {
112 key = attrs[i] + strlen (EV_METADATA_NAMESPACE"::");
114 if (type == G_FILE_ATTRIBUTE_TYPE_STRING) {
115 g_hash_table_insert (metadata->items,
121 g_object_unref (info);
125 ev_metadata_new (GFile *file)
127 EvMetadata *metadata;
129 g_return_val_if_fail (G_IS_FILE (file), NULL);
131 metadata = EV_METADATA (g_object_new (EV_TYPE_METADATA, NULL));
132 metadata->file = g_object_ref (file);
134 ev_metadata_load (metadata);
140 ev_metadata_get_string (EvMetadata *metadata,
146 v = g_hash_table_lookup (metadata->items, key);
155 metadata_set_callback (GObject *file,
156 GAsyncResult *result,
157 EvMetadata *metadata)
159 GError *error = NULL;
161 if (!g_file_set_attributes_finish (G_FILE (file), result, NULL, &error)) {
162 g_warning ("%s", error->message);
163 g_error_free (error);
168 ev_metadata_set_string (EvMetadata *metadata,
175 info = g_file_info_new ();
177 gio_key = g_strconcat (EV_METADATA_NAMESPACE"::", key, NULL);
179 g_file_info_set_attribute_string (info, gio_key, value);
181 g_file_info_set_attribute (info, gio_key,
182 G_FILE_ATTRIBUTE_TYPE_INVALID,
187 g_hash_table_insert (metadata->items, g_strdup (key), g_strdup (value));
188 g_file_set_attributes_async (metadata->file,
193 (GAsyncReadyCallback)metadata_set_callback,
195 g_object_unref (info);
201 ev_metadata_get_int (EvMetadata *metadata,
209 if (!ev_metadata_get_string (metadata, key, &string_value))
212 int_value = g_ascii_strtoull (string_value, &endptr, 0);
213 if (int_value == 0 && string_value == endptr)
221 ev_metadata_set_int (EvMetadata *metadata,
225 gchar string_value[32];
227 g_snprintf (string_value, sizeof (string_value), "%d", value);
229 return ev_metadata_set_string (metadata, key, string_value);
233 ev_metadata_get_double (EvMetadata *metadata,
239 gdouble double_value;
241 if (!ev_metadata_get_string (metadata, key, &string_value))
244 double_value = g_ascii_strtod (string_value, &endptr);
245 if (double_value == 0. && string_value == endptr)
248 *value = double_value;
253 ev_metadata_set_double (EvMetadata *metadata,
257 gchar string_value[G_ASCII_DTOSTR_BUF_SIZE];
259 g_ascii_dtostr (string_value, G_ASCII_DTOSTR_BUF_SIZE, value);
261 return ev_metadata_set_string (metadata, key, string_value);
265 ev_metadata_get_boolean (EvMetadata *metadata,
271 if (!ev_metadata_get_int (metadata, key, &int_value))
279 ev_metadata_set_boolean (EvMetadata *metadata,
283 return ev_metadata_set_string (metadata, key, value ? "1" : "0");
287 ev_is_metadata_supported_for_file (GFile *file)
289 GFileAttributeInfoList *namespaces;
291 gboolean retval = FALSE;
293 namespaces = g_file_query_writable_namespaces (file, NULL, NULL);
297 for (i = 0; i < namespaces->n_infos; i++) {
298 if (strcmp (namespaces->infos[i].name, "metadata") == 0) {
304 g_file_attribute_info_list_unref (namespaces);