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_is_empty (EvMetadata *metadata)
142 return g_hash_table_size (metadata->items) == 0;
146 ev_metadata_get_string (EvMetadata *metadata,
152 v = g_hash_table_lookup (metadata->items, key);
161 metadata_set_callback (GObject *file,
162 GAsyncResult *result,
163 EvMetadata *metadata)
165 GError *error = NULL;
167 if (!g_file_set_attributes_finish (G_FILE (file), result, NULL, &error)) {
168 g_warning ("%s", error->message);
169 g_error_free (error);
174 ev_metadata_set_string (EvMetadata *metadata,
181 info = g_file_info_new ();
183 gio_key = g_strconcat (EV_METADATA_NAMESPACE"::", key, NULL);
185 g_file_info_set_attribute_string (info, gio_key, value);
187 g_file_info_set_attribute (info, gio_key,
188 G_FILE_ATTRIBUTE_TYPE_INVALID,
193 g_hash_table_insert (metadata->items, g_strdup (key), g_strdup (value));
194 g_file_set_attributes_async (metadata->file,
199 (GAsyncReadyCallback)metadata_set_callback,
201 g_object_unref (info);
207 ev_metadata_get_int (EvMetadata *metadata,
215 if (!ev_metadata_get_string (metadata, key, &string_value))
218 int_value = g_ascii_strtoull (string_value, &endptr, 0);
219 if (int_value == 0 && string_value == endptr)
227 ev_metadata_set_int (EvMetadata *metadata,
231 gchar string_value[32];
233 g_snprintf (string_value, sizeof (string_value), "%d", value);
235 return ev_metadata_set_string (metadata, key, string_value);
239 ev_metadata_get_double (EvMetadata *metadata,
245 gdouble double_value;
247 if (!ev_metadata_get_string (metadata, key, &string_value))
250 double_value = g_ascii_strtod (string_value, &endptr);
251 if (double_value == 0. && string_value == endptr)
254 *value = double_value;
259 ev_metadata_set_double (EvMetadata *metadata,
263 gchar string_value[G_ASCII_DTOSTR_BUF_SIZE];
265 g_ascii_dtostr (string_value, G_ASCII_DTOSTR_BUF_SIZE, value);
267 return ev_metadata_set_string (metadata, key, string_value);
271 ev_metadata_get_boolean (EvMetadata *metadata,
277 if (!ev_metadata_get_int (metadata, key, &int_value))
285 ev_metadata_set_boolean (EvMetadata *metadata,
289 return ev_metadata_set_string (metadata, key, value ? "1" : "0");
293 ev_is_metadata_supported_for_file (GFile *file)
295 GFileAttributeInfoList *namespaces;
297 gboolean retval = FALSE;
299 namespaces = g_file_query_writable_namespaces (file, NULL, NULL);
303 for (i = 0; i < namespaces->n_infos; i++) {
304 if (strcmp (namespaces->infos[i].name, "metadata") == 0) {
310 g_file_attribute_info_list_unref (namespaces);