Don't leak GValues.
[mmediamanager.git] / src / mm-hit.c
blob32273a5c1a96e13d655dbdcd703c67e25a8a438e
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"
26 #include "mm-utils.h"
28 #define MM_HIT_GET_PRIVATE(o) \
29 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_HIT, MMHitDetails))
31 G_DEFINE_TYPE (MMHit, mm_hit, G_TYPE_OBJECT);
33 struct _MMHitDetails {
34 GHashTable *attributes;
37 typedef struct {
38 MMAttribute *attr;
39 GValue *val;
40 } AttributeValuePair;
42 static void
43 mm_hit_finalize (GObject *o)
45 MMHitDetails *details = MM_HIT (o)->details;
47 g_hash_table_destroy (details->attributes);
49 G_OBJECT_CLASS (mm_hit_parent_class)->finalize (o);
52 static void
53 pair_free (gpointer data)
55 AttributeValuePair *pair = data;
57 g_value_unset (pair->val);
58 g_free (pair->val);
59 g_slice_free (AttributeValuePair, data);
62 static void
63 mm_hit_init (MMHit *h)
65 MMHitDetails *details = h->details = MM_HIT_GET_PRIVATE (h);
67 details->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, pair_free);
70 static void
71 mm_hit_class_init (MMHitClass *klass)
73 GObjectClass *oclass = G_OBJECT_CLASS (klass);
75 oclass->finalize = mm_hit_finalize;
77 g_type_class_add_private (klass, sizeof (MMHitDetails));
80 static void
81 build_all_table_foreach (gpointer k,
82 gpointer v,
83 gpointer ud)
85 GHashTable *table = ud;
86 AttributeValuePair *pair = v;
88 g_hash_table_insert (table, pair->attr, pair->val);
91 /* public methods */
93 void
94 mm_hit_set_value (MMHit *h,
95 MMAttribute *attribute,
96 GValue *value)
98 const char *attribute_id;
99 AttributeValuePair *pair;
101 g_return_if_fail (MM_IS_HIT (h));
103 if (G_VALUE_TYPE (value) != mm_attribute_get_value_type (attribute)) {
104 g_warning ("Setting the wrong value type for attribute %s",
105 mm_attribute_get_name (attribute));
106 return;
109 pair = g_slice_new0 (AttributeValuePair);
110 pair->attr = attribute;
111 pair->val = mm_create_gvalue_for_attribute (attribute);
112 g_value_copy (value, pair->val);
114 attribute_id = mm_attribute_get_id (attribute);
115 g_hash_table_insert (h->details->attributes,
116 (char *) attribute_id,
117 pair);
120 void
121 mm_hit_set_values (MMHit *hit,
122 GList *attributes,
123 GList *values)
125 GList *l, *m;
127 g_return_if_fail (MM_IS_HIT (hit));
128 g_return_if_fail (g_list_length (attributes) == g_list_length (values));
130 for (l = attributes, m = values; l && m; l = l->next, m = m->next) {
131 mm_hit_set_value (hit, l->data, m->data);
135 GHashTable *
136 mm_hit_get_values (MMHit *hit,
137 const char *ids)
139 GHashTable *table;
140 gchar **splitted_ids;
141 int idx;
142 AttributeValuePair *pair;
144 g_return_val_if_fail (MM_IS_HIT (hit), NULL);
145 table = g_hash_table_new (g_direct_hash, g_direct_equal);
147 splitted_ids = g_strsplit (ids, " ", 0);
148 for (idx = 0; idx < G_N_ELEMENTS (splitted_ids); idx++) {
149 pair = g_hash_table_lookup (hit->details->attributes,
150 splitted_ids[idx]);
151 if (!pair) {
152 /* we can't find the relevant attribute, keep looking for the
153 * others.
155 continue;
157 g_hash_table_insert (table, pair->attr, pair->val);
160 g_strfreev (splitted_ids);
162 return table;
165 GHashTable *
166 mm_hit_get_all_values (MMHit *hit)
168 GHashTable *table;
170 g_return_val_if_fail (MM_IS_HIT (hit), NULL);
172 table = g_hash_table_new (g_direct_hash, g_direct_equal);
174 g_hash_table_foreach (hit->details->attributes,
175 build_all_table_foreach,
176 table);
177 return table;