1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * ev-metadata-manager.c
4 * This file is part of ev
6 * Copyright (C) 2003 Paolo Maggi
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * Modified by the ev Team, 2003. See the AUTHORS file for a
26 * list of people on the ev Team.
27 * See the ChangeLog files for a list of changes.
37 #include <libxml/xmlreader.h>
39 #include "ev-metadata-manager.h"
40 #include "ev-file-helpers.h"
42 #define METADATA_FILE "ev-metadata.xml"
46 typedef struct _EvMetadataManager EvMetadataManager;
48 typedef struct _Item Item;
52 time_t atime; /* time of last access */
57 struct _EvMetadataManager
59 gboolean values_loaded; /* It is true if the file
67 static gboolean ev_metadata_manager_save (gpointer data);
70 static EvMetadataManager *ev_metadata_manager = NULL;
74 * @data: a pointer to a #Item data
76 * It does free the values on the #GHashTable where data points.
79 item_free (gpointer data)
83 g_return_if_fail (data != NULL);
87 if (item->values != NULL)
88 g_hash_table_destroy (item->values);
94 * ev_metadata_arm_timeout
96 * Setup a timeout for saving the metadata to disk.
99 ev_metadata_arm_timeout(void)
101 if (ev_metadata_manager->timeout_id)
103 ev_metadata_manager->timeout_id =
104 g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE,
106 (GSourceFunc)ev_metadata_manager_save,
112 * ev_metadata_manager_init:
114 * Creates an EvMetadataManager with default values.
116 * values_loaded -> %FALSE.
117 * timeout_id -> the id of the event source.
118 * items -> a new full empty #GHashTable.
121 ev_metadata_manager_init (void)
123 ev_metadata_manager = g_new0 (EvMetadataManager, 1);
125 ev_metadata_manager->values_loaded = FALSE;
127 ev_metadata_manager->items =
128 g_hash_table_new_full (g_str_hash,
134 /* This function must be called before exiting ev */
136 ev_metadata_manager_shutdown (void)
138 if (ev_metadata_manager == NULL)
141 if (ev_metadata_manager->timeout_id) {
142 g_source_remove (ev_metadata_manager->timeout_id);
143 ev_metadata_manager->timeout_id = 0;
144 ev_metadata_manager_save (NULL);
147 if (ev_metadata_manager->items != NULL)
148 g_hash_table_destroy (ev_metadata_manager->items);
150 g_free (ev_metadata_manager);
151 ev_metadata_manager = NULL;
155 value_free (gpointer data)
157 GValue *value = (GValue *)data;
159 g_value_unset (value);
164 parse_value (xmlChar *value, xmlChar *type)
169 ret_type = g_type_from_name ((char *)type);
170 ret = g_new0 (GValue, 1);
171 g_value_init (ret, ret_type);
175 g_value_set_string (ret, (char *)value);
178 g_value_set_int (ret, g_ascii_strtoull ((char *)value, NULL, 0));
181 g_value_set_double (ret, g_ascii_strtod ((char *)value, NULL));
184 g_value_set_boolean (ret, g_ascii_strtoull ((char *)value, NULL, 0));
192 parseItem (xmlDocPtr doc, xmlNodePtr cur)
199 if (xmlStrcmp (cur->name, (const xmlChar *)"document") != 0)
202 uri = xmlGetProp (cur, (const xmlChar *)"uri");
206 atime = xmlGetProp (cur, (const xmlChar *)"atime");
213 item = g_new0 (Item, 1);
215 item->atime = g_ascii_strtoull((char*)atime, NULL, 0);
217 item->values = g_hash_table_new_full (g_str_hash,
222 cur = cur->xmlChildrenNode;
226 if (xmlStrcmp (cur->name, (const xmlChar *)"entry") == 0)
233 key = xmlGetProp (cur, (const xmlChar *)"key");
234 xml_value = xmlGetProp (cur, (const xmlChar *)"value");
235 type = xmlGetProp (cur, (const xmlChar *)"type");
236 value = parse_value (xml_value, type);
238 if ((key != NULL) && (value != NULL))
239 g_hash_table_insert (item->values,
247 if (xml_value != NULL)
254 g_hash_table_insert (ev_metadata_manager->items,
269 g_return_val_if_fail (ev_metadata_manager != NULL, FALSE);
270 g_return_val_if_fail (ev_metadata_manager->values_loaded == FALSE, FALSE);
272 ev_metadata_manager->values_loaded = TRUE;
274 xmlKeepBlanksDefault (0);
276 /* FIXME: file locking - Paolo */
277 file_name = g_build_filename (ev_dot_dir (), METADATA_FILE, NULL);
278 if (!g_file_test (file_name, G_FILE_TEST_EXISTS))
284 doc = xmlParseFile (file_name);
292 cur = xmlDocGetRootElement (doc);
295 g_message ("The metadata file “%s” is empty", METADATA_FILE);
301 if (xmlStrcmp (cur->name, (const xmlChar *) "metadata"))
303 g_message ("File “%s” is of the wrong type", METADATA_FILE);
309 cur = xmlDocGetRootElement (doc);
310 cur = cur->xmlChildrenNode;
314 parseItem (doc, cur);
324 #define LAST_URI "last-used-value"
327 ev_metadata_manager_get_last (const gchar *key,
334 g_assert (ev_metadata_manager->values_loaded);
339 item = (Item *)g_hash_table_lookup (ev_metadata_manager->items,
345 item->atime = time (NULL);
347 if (item->values == NULL)
350 ret = (GValue *)g_hash_table_lookup (item->values, key);
353 g_value_init (value, G_VALUE_TYPE (ret));
354 g_value_copy (ret, value);
362 ev_metadata_manager_set_last (const gchar *key,
367 g_assert (ev_metadata_manager->values_loaded);
369 item = (Item *)g_hash_table_lookup (ev_metadata_manager->items,
374 item = g_new0 (Item, 1);
376 g_hash_table_insert (ev_metadata_manager->items,
381 if (item->values == NULL)
382 item->values = g_hash_table_new_full (g_str_hash,
389 new = g_new0 (GValue, 1);
390 g_value_init (new, G_VALUE_TYPE (value));
391 g_value_copy (value, new);
393 g_hash_table_insert (item->values,
397 g_hash_table_remove (item->values,
401 item->atime = time (NULL);
402 ev_metadata_arm_timeout ();
407 * ev_metadata_manager_get:
408 * @uri: Uri to set data for, if @NULL, we return default value
409 * @key: Key to set uri
410 * @value: GValue struct filled up with value
411 * @ignore_last: if @TRUE, default value is ignored
413 * Retrieve value for uri in metadata database
415 * Returns: @TRUE if value was taken.
418 ev_metadata_manager_get (const gchar *uri,
421 gboolean ignore_last)
426 g_return_val_if_fail (key != NULL, FALSE);
428 if (ev_metadata_manager == NULL)
431 if (!ev_metadata_manager->values_loaded)
435 res = load_values ();
438 return ev_metadata_manager_get_last (key, value, ignore_last);
442 return ev_metadata_manager_get_last (key, value, ignore_last);
444 item = (Item *)g_hash_table_lookup (ev_metadata_manager->items,
448 return ev_metadata_manager_get_last (key, value, ignore_last);
450 item->atime = time (NULL);
452 if (item->values == NULL)
453 return ev_metadata_manager_get_last (key, value, ignore_last);
455 ret = (GValue *)g_hash_table_lookup (item->values, key);
458 g_value_init (value, G_VALUE_TYPE (ret));
459 g_value_copy (ret, value);
463 return ev_metadata_manager_get_last (key, value, ignore_last);
467 * ev_metadata_manager_set:
468 * @uri: Uri to set data for, if @NULL, we set default value
469 * @key: Key to set uri
470 * @value: GValue struct containing value
472 * Set value for key in metadata database
475 ev_metadata_manager_set (const gchar *uri,
481 g_return_if_fail (key != NULL);
483 if (ev_metadata_manager == NULL)
486 if (!ev_metadata_manager->values_loaded)
490 res = load_values ();
498 ev_metadata_manager_set_last (key, value);
502 item = (Item *)g_hash_table_lookup (ev_metadata_manager->items,
507 item = g_new0 (Item, 1);
509 g_hash_table_insert (ev_metadata_manager->items,
514 if (item->values == NULL)
515 item->values = g_hash_table_new_full (g_str_hash,
522 new = g_new0 (GValue, 1);
523 g_value_init (new, G_VALUE_TYPE (value));
524 g_value_copy (value, new);
526 g_hash_table_insert (item->values,
529 ev_metadata_manager_set_last (key, value);
531 g_hash_table_remove (item->values,
535 item->atime = time (NULL);
537 ev_metadata_arm_timeout ();
541 save_values (const gchar *key, GValue *value, xmlNodePtr parent)
546 g_return_if_fail (key != NULL);
551 xml_node = xmlNewChild (parent, NULL, (const xmlChar *)"entry", NULL);
553 xmlSetProp (xml_node, (const xmlChar *)"key", (const xmlChar *)key);
554 xmlSetProp (xml_node,
555 (const xmlChar *)"type",
556 (const xmlChar *)g_type_name (G_VALUE_TYPE (value)));
558 switch (G_VALUE_TYPE (value)) {
560 string_value = g_strdup (g_value_get_string (value));
563 string_value = g_strdup_printf ("%d", g_value_get_int (value));
567 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
568 g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE, g_value_get_double (value));
569 string_value = g_strdup_printf ("%s", buf);
573 string_value = g_strdup_printf ("%d", g_value_get_boolean (value));
577 g_assert_not_reached ();
580 xmlSetProp (xml_node, (const xmlChar *)"value", (const xmlChar *)string_value);
582 g_free (string_value);
586 save_item (const gchar *key, const gpointer *data, xmlNodePtr parent)
589 const Item *item = (const Item *)data;
592 g_return_if_fail (key != NULL);
597 xml_node = xmlNewChild (parent, NULL, (const xmlChar *)"document", NULL);
599 xmlSetProp (xml_node, (const xmlChar *)"uri", (const xmlChar *)key);
601 atime = g_strdup_printf ("%ld", item->atime);
602 xmlSetProp (xml_node, (const xmlChar *)"atime", (const xmlChar *)atime);
605 g_hash_table_foreach (item->values,
606 (GHFunc)save_values, xml_node);
610 get_oldest (const gchar *key, const gpointer value, const gchar ** key_to_remove)
612 const Item *item = (const Item *)value;
614 if (*key_to_remove == NULL)
616 *key_to_remove = key;
620 const Item *item_to_remove =
621 g_hash_table_lookup (ev_metadata_manager->items,
624 g_return_if_fail (item_to_remove != NULL);
626 if (item->atime < item_to_remove->atime)
628 *key_to_remove = key;
636 while (g_hash_table_size (ev_metadata_manager->items) > MAX_ITEMS)
638 gpointer key_to_remove = NULL;
640 g_hash_table_foreach (ev_metadata_manager->items,
644 g_return_if_fail (key_to_remove != NULL);
646 g_hash_table_remove (ev_metadata_manager->items,
652 ev_metadata_manager_save (gpointer data)
658 ev_metadata_manager->timeout_id = 0;
662 xmlIndentTreeOutput = TRUE;
664 doc = xmlNewDoc ((const xmlChar *)"1.0");
668 /* Create metadata root */
669 root = xmlNewDocNode (doc, NULL, (const xmlChar *)"metadata", NULL);
670 xmlDocSetRootElement (doc, root);
672 g_hash_table_foreach (ev_metadata_manager->items,
673 (GHFunc)save_item, root);
675 /* FIXME: lock file - Paolo */
676 file_name = g_build_filename (ev_dot_dir (), METADATA_FILE, NULL);
677 xmlSaveFormatFile (file_name, doc, 1);
686 ev_metadata_manager_set_int (const gchar *uri, const gchar *key, int value)
690 g_value_init (&val, G_TYPE_INT);
691 g_value_set_int (&val, value);
693 ev_metadata_manager_set (uri, key, &val);
695 g_value_unset (&val);
699 ev_metadata_manager_set_double (const gchar *uri, const gchar *key, double value)
703 g_value_init (&val, G_TYPE_DOUBLE);
704 g_value_set_double (&val, value);
706 ev_metadata_manager_set (uri, key, &val);
708 g_value_unset (&val);
712 ev_metadata_manager_set_string (const gchar *uri, const gchar *key, const gchar *value)
716 g_value_init (&val, G_TYPE_STRING);
717 g_value_set_string (&val, value);
719 ev_metadata_manager_set (uri, key, &val);
721 g_value_unset (&val);
725 ev_metadata_manager_set_boolean (const gchar *uri, const gchar *key, gboolean value)
729 g_value_init (&val, G_TYPE_BOOLEAN);
730 g_value_set_boolean (&val, value);
732 ev_metadata_manager_set (uri, key, &val);
734 g_value_unset (&val);