From 5f4e9ef07957e4b7575bcb930c976c899befe77d Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sat, 4 Jul 2009 23:58:59 +0100 Subject: [PATCH] Implement meta data in KatzeItem Now KatzeItem can store arbitrary integers and strings as meta data. --- katze/katze-item.c | 95 +++++++++++++++++++++++++++++++++++++++++++++- katze/katze-item.h | 17 +++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/katze/katze-item.c b/katze/katze-item.c index 006adc4b..700f599f 100644 --- a/katze/katze-item.c +++ b/katze/katze-item.c @@ -1,5 +1,5 @@ /* - Copyright (C) 2008 Christian Dywan + Copyright (C) 2008-2009 Christian Dywan This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -147,7 +147,8 @@ katze_item_class_init (KatzeItemClass* class) static void katze_item_init (KatzeItem* item) { - /* Nothing to do here */ + item->metadata = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, g_free); } static void @@ -161,6 +162,8 @@ katze_item_finalize (GObject* object) g_free (item->icon); g_free (item->token); + g_hash_table_unref (item->metadata); + G_OBJECT_CLASS (katze_item_parent_class)->finalize (object); } @@ -451,6 +454,94 @@ katze_item_set_added (KatzeItem* item, g_object_notify (G_OBJECT (item), "added"); } +/** + * katze_item_get_meta_keys: + * @item: a #KatzeItem + * + * Retrieves a list of all meta keys. + * + * Return value: a newly allocated #GList of constant strings + * + * Since: 0.1.8 + **/ +GList* +katze_item_get_meta_keys (KatzeItem* item) +{ + g_return_val_if_fail (KATZE_IS_ITEM (item), NULL); + + return g_hash_table_get_keys (item->metadata); +} + +/** + * katze_item_get_meta_string: + * @item: a #KatzeItem + * @key: the name of an integer value + * + * Retrieves a string value by the specified key from the + * meta data of the item. + * + * Return value: a string, or %NULL + * + * Since: 0.1.8 + **/ +const gchar* +katze_item_get_meta_string (KatzeItem* item, + const gchar* key) +{ + g_return_val_if_fail (KATZE_IS_ITEM (item), NULL); + + return g_hash_table_lookup (item->metadata, key); +} + +/** + * katze_item_get_meta_integer: + * @item: a #KatzeItem + * @key: the name of an integer value + * + * Retrieves an integer value by the specified key from the + * meta data of the item. + * + * If the key is present but not representable as an + * integer, -1 is returned. + * + * Return value: an integer value, or -1 + * + * Since: 0.1.8 + **/ +gint64 +katze_item_get_meta_integer (KatzeItem* item, + const gchar* key) +{ + gpointer value; + + g_return_val_if_fail (KATZE_IS_ITEM (item), -1); + + if (g_hash_table_lookup_extended (item->metadata, key, NULL, &value)) + return g_ascii_strtoll (value, NULL, 0); + return -1; +} + +/** + * katze_item_set_meta_integer: + * @item: a #KatzeItem + * @key: the name of an integer value + * + * Saves the specified integer value in the meta data of + * the item under the specified key. + * + * Since: 0.1.8 + **/ +void +katze_item_set_meta_integer (KatzeItem* item, + const gchar* key, + gint64 value) +{ + g_return_if_fail (KATZE_IS_ITEM (item)); + + g_hash_table_insert (item->metadata, g_strdup (key), g_strdup_printf ("%lu", value)); + /* TODO: Emit meta-key-changed */ +} + /** * katze_item_get_parent: * @item: a #KatzeItem diff --git a/katze/katze-item.h b/katze/katze-item.h index 8e40110a..27e0d550 100644 --- a/katze/katze-item.h +++ b/katze/katze-item.h @@ -42,6 +42,7 @@ struct _KatzeItem gchar* icon; gchar* token; gint64 added; + GHashTable* metadata; KatzeItem* parent; }; @@ -102,6 +103,22 @@ void katze_item_set_added (KatzeItem* item, gint64 added); +GList* +katze_item_get_meta_keys (KatzeItem* item); + +const gchar* +katze_item_get_meta_string (KatzeItem* item, + const gchar* key); + +gint64 +katze_item_get_meta_integer (KatzeItem* item, + const gchar* key); + +void +katze_item_set_meta_integer (KatzeItem* item, + const gchar* key, + gint64 value); + gpointer katze_item_get_parent (KatzeItem* item);