]> www.fi.muni.cz Git - evince.git/blob - shell/ev-metadata.c
e69ecbdc6e906eb632ba47bd42d2714788597ff7
[evince.git] / shell / ev-metadata.c
1 /* ev-metadata.c
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2009 Carlos Garcia Campos  <carlosgc@gnome.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <gio/gio.h>
22 #include <string.h>
23
24 #include "ev-metadata.h"
25
26 struct _EvMetadata {
27         GObject base;
28
29         GFile      *file;
30         GHashTable *items;
31 };
32
33 struct _EvMetadataClass {
34         GObjectClass base_class;
35 };
36
37 G_DEFINE_TYPE (EvMetadata, ev_metadata, G_TYPE_OBJECT)
38
39 #define EV_METADATA_NAMESPACE "metadata::evince"
40
41 static void
42 ev_metadata_finalize (GObject *object)
43 {
44         EvMetadata *metadata = EV_METADATA (object);
45
46         if (metadata->items) {
47                 g_hash_table_destroy (metadata->items);
48                 metadata->items = NULL;
49         }
50
51         if (metadata->file) {
52                 g_object_unref (metadata->file);
53                 metadata->file = NULL;
54         }
55
56         G_OBJECT_CLASS (ev_metadata_parent_class)->finalize (object);
57 }
58
59 static void
60 ev_metadata_init (EvMetadata *metadata)
61 {
62         metadata->items = g_hash_table_new_full (g_str_hash,
63                                                  g_str_equal,
64                                                  g_free,
65                                                  g_free);
66 }
67
68 static void
69 ev_metadata_class_init (EvMetadataClass *klass)
70 {
71         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
72
73         gobject_class->finalize = ev_metadata_finalize;
74 }
75
76 static void
77 ev_metadata_load (EvMetadata *metadata)
78 {
79         GFileInfo *info;
80         gchar    **attrs;
81         gint       i;
82         GError    *error = NULL;
83
84         info = g_file_query_info (metadata->file, "metadata::*", 0, NULL, &error);
85         if (!info) {
86                 g_warning ("%s", error->message);
87                 g_error_free (error);
88
89                 return;
90         }
91
92         if (!g_file_info_has_namespace (info, "metadata")) {
93                 g_object_unref (info);
94
95                 return;
96         }
97
98         attrs = g_file_info_list_attributes (info, "metadata");
99         for (i = 0; attrs[i]; i++) {
100                 GFileAttributeType type;
101                 gpointer           value;
102                 const gchar       *key;
103
104                 if (!g_str_has_prefix (attrs[i], EV_METADATA_NAMESPACE))
105                         continue;
106
107                 if (!g_file_info_get_attribute_data (info, attrs[i],
108                                                      &type, &value, NULL)) {
109                         continue;
110                 }
111
112                 key = attrs[i] + strlen (EV_METADATA_NAMESPACE"::");
113
114                 if (type == G_FILE_ATTRIBUTE_TYPE_STRING) {
115                         g_hash_table_insert (metadata->items,
116                                              g_strdup (key),
117                                              g_strdup (value));
118                 }
119         }
120         g_strfreev (attrs);
121         g_object_unref (info);
122 }
123
124 EvMetadata *
125 ev_metadata_new (GFile *file)
126 {
127         EvMetadata *metadata;
128
129         g_return_val_if_fail (G_IS_FILE (file), NULL);
130
131         metadata = EV_METADATA (g_object_new (EV_TYPE_METADATA, NULL));
132         metadata->file = g_object_ref (file);
133
134         ev_metadata_load (metadata);
135
136         return metadata;
137 }
138
139 gboolean
140 ev_metadata_get_string (EvMetadata  *metadata,
141                         const gchar *key,
142                         gchar     **value)
143 {
144         gchar *v;
145
146         v = g_hash_table_lookup (metadata->items, key);
147         if (!v)
148                 return FALSE;
149
150         *value = v;
151         return TRUE;
152 }
153
154 static void
155 metadata_set_callback (GObject      *file,
156                        GAsyncResult *result,
157                        EvMetadata   *metadata)
158 {
159         GError *error = NULL;
160
161         if (!g_file_set_attributes_finish (G_FILE (file), result, NULL, &error)) {
162                 g_warning ("%s", error->message);
163                 g_error_free (error);
164         }
165 }
166
167 gboolean
168 ev_metadata_set_string (EvMetadata  *metadata,
169                         const gchar *key,
170                         const gchar *value)
171 {
172         GFileInfo *info;
173         gchar     *gio_key;
174
175         info = g_file_info_new ();
176
177         gio_key = g_strconcat (EV_METADATA_NAMESPACE"::", key, NULL);
178         if (value) {
179                 g_file_info_set_attribute_string (info, gio_key, value);
180         } else {
181                 g_file_info_set_attribute (info, gio_key,
182                                            G_FILE_ATTRIBUTE_TYPE_INVALID,
183                                            NULL);
184         }
185         g_free (gio_key);
186
187         g_hash_table_insert (metadata->items, g_strdup (key), g_strdup (value));
188         g_file_set_attributes_async (metadata->file,
189                                      info,
190                                      0,
191                                      G_PRIORITY_DEFAULT,
192                                      NULL,
193                                      (GAsyncReadyCallback)metadata_set_callback,
194                                      metadata);
195         g_object_unref (info);
196
197         return TRUE;
198 }
199
200 gboolean
201 ev_metadata_get_int (EvMetadata  *metadata,
202                      const gchar *key,
203                      gint        *value)
204 {
205         gchar *string_value;
206         gchar *endptr;
207         gint   int_value;
208
209         if (!ev_metadata_get_string (metadata, key, &string_value))
210                 return FALSE;
211
212         int_value = g_ascii_strtoull (string_value, &endptr, 0);
213         if (int_value == 0 && string_value == endptr)
214                 return FALSE;
215
216         *value = int_value;
217         return TRUE;
218 }
219
220 gboolean
221 ev_metadata_set_int (EvMetadata  *metadata,
222                      const gchar *key,
223                      gint         value)
224 {
225         gchar string_value[32];
226
227         g_snprintf (string_value, sizeof (string_value), "%d", value);
228
229         return ev_metadata_set_string (metadata, key, string_value);
230 }
231
232 gboolean
233 ev_metadata_get_double (EvMetadata  *metadata,
234                         const gchar *key,
235                         gdouble     *value)
236 {
237         gchar  *string_value;
238         gchar  *endptr;
239         gdouble double_value;
240
241         if (!ev_metadata_get_string (metadata, key, &string_value))
242                 return FALSE;
243
244         double_value = g_ascii_strtod (string_value, &endptr);
245         if (double_value == 0. && string_value == endptr)
246                 return FALSE;
247
248         *value = double_value;
249         return TRUE;
250 }
251
252 gboolean
253 ev_metadata_set_double (EvMetadata  *metadata,
254                         const gchar *key,
255                         gdouble      value)
256 {
257         gchar string_value[G_ASCII_DTOSTR_BUF_SIZE];
258
259         g_ascii_dtostr (string_value, G_ASCII_DTOSTR_BUF_SIZE, value);
260
261         return ev_metadata_set_string (metadata, key, string_value);
262 }
263
264 gboolean
265 ev_metadata_get_boolean (EvMetadata  *metadata,
266                          const gchar *key,
267                          gboolean    *value)
268 {
269         gint int_value;
270
271         if (!ev_metadata_get_int (metadata, key, &int_value))
272                 return FALSE;
273
274         *value = int_value;
275         return TRUE;
276 }
277
278 gboolean
279 ev_metadata_set_boolean (EvMetadata  *metadata,
280                          const gchar *key,
281                          gboolean     value)
282 {
283         return ev_metadata_set_string (metadata, key, value ? "1" : "0");
284 }
285
286 gboolean
287 ev_is_metadata_supported_for_file (GFile *file)
288 {
289         GFileAttributeInfoList *namespaces;
290         gint i;
291         gboolean retval = FALSE;
292
293         namespaces = g_file_query_writable_namespaces (file, NULL, NULL);
294         if (!namespaces)
295                 return retval;
296
297         for (i = 0; i < namespaces->n_infos; i++) {
298                 if (strcmp (namespaces->infos[i].name, "metadata") == 0) {
299                         retval = TRUE;
300                         break;
301                 }
302         }
303
304         g_file_attribute_info_list_unref (namespaces);
305
306         return retval;
307 }