Use right hashing function for the hash table in MMHit
[mmediamanager.git] / src / mm-hit.c
blob562369808c6eb104e4cf9594b70727df27d30ea0
1 /* MManager - a Desktop wide manager for multimedia applications.
3 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <glib.h>
22 #include <glib-object.h>
24 #include "mm-hit.h"
25 #include "mm-attribute.h"
27 #define MM_HIT_GET_PRIVATE(o) \
28 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_HIT, MMHitDetails))
30 G_DEFINE_TYPE (MMHit, mm_hit, G_TYPE_OBJECT);
32 struct _MMHitDetails {
33 GHashTable *attributes;
36 typedef struct {
37 MMAttribute *attr;
38 GValue *val;
39 } AttributeValuePair;
41 static void
42 mm_hit_finalize (GObject *o)
44 MMHitDetails *details = MM_HIT (o)->details;
46 g_hash_table_destroy (details->attributes);
48 G_OBJECT_CLASS (mm_hit_parent_class)->finalize (o);
51 static void
52 pair_free (gpointer data)
54 g_slice_free (AttributeValuePair, data);
57 static void
58 mm_hit_init (MMHit *h)
60 MMHitDetails *details = h->details = MM_HIT_GET_PRIVATE (h);
62 details->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, pair_free);
65 static void
66 mm_hit_class_init (MMHitClass *klass)
68 GObjectClass *oclass = G_OBJECT_CLASS (klass);
70 oclass->finalize = mm_hit_finalize;
72 g_type_class_add_private (klass, sizeof (MMHitDetails));
75 /* public methods */
77 void
78 mm_hit_set_value (MMHit *h,
79 MMAttribute *attribute,
80 GValue *value)
82 const char *attribute_id;
83 AttributeValuePair *pair;
85 g_return_if_fail (MM_IS_HIT (h));
87 if (G_VALUE_TYPE (value) != mm_attribute_get_value_type (attribute)) {
88 g_warning ("Setting the wrong value type for attribute %s",
89 mm_attribute_get_name (attribute));
90 return;
93 pair = g_slice_new0 (AttributeValuePair);
94 pair->attr = attribute;
95 pair->val = value;
97 attribute_id = mm_attribute_get_id (attribute);
98 g_hash_table_insert (h->details->attributes,
99 (char *) attribute_id,
100 pair);
103 void
104 mm_hit_set_values (MMHit *hit,
105 GList *attributes,
106 GList *values)
108 GList *l, *m;
110 g_return_if_fail (MM_IS_HIT (hit));
111 g_return_if_fail (g_list_length (attributes) == g_list_length (values));
113 for (l = attributes, m = values; l && m; l = l->next, m = m->next) {
114 mm_hit_set_value (hit, l->data, m->data);
118 GHashTable *
119 mm_hit_get_values (MMHit *hit,
120 const char *ids)
122 GHashTable *table;
123 gchar **splitted_ids;
124 int idx;
125 AttributeValuePair *pair;
127 g_return_val_if_fail (MM_IS_HIT (hit), NULL);
128 table = g_hash_table_new (g_direct_hash, g_direct_equal);
130 splitted_ids = g_strsplit (ids, " ", 0);
131 for (idx = 0; idx < G_N_ELEMENTS (splitted_ids); idx++) {
132 pair = g_hash_table_lookup (hit->details->attributes,
133 splitted_ids[idx]);
134 g_hash_table_insert (table, pair->attr, pair->val);
137 return table;
140 static void
141 build_all_table_foreach (gpointer k,
142 gpointer v,
143 gpointer ud)
145 GHashTable *table = ud;
146 AttributeValuePair *pair = v;
148 g_hash_table_insert (table, pair->attr, pair->val);
151 GHashTable *
152 mm_hit_get_all_values (MMHit *hit)
154 GHashTable *table;
156 g_return_val_if_fail (MM_IS_HIT (hit), NULL);
158 table = g_hash_table_new (g_direct_hash, g_direct_equal);
160 g_hash_table_foreach (hit->details->attributes,
161 build_all_table_foreach,
162 table);
163 return table;