Prepare for gtk-doc: make variable names in .h prototypes and .c methods
[mmediamanager.git] / libmmanager / mm-hit.c
blob1d5d154f604cbbd23b8a54ea37d7ab3a962091bd
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 /**
94 * mm_hit_set_value:
95 * @hit: a #MMHit.
96 * @attribute: a #MMAttribute.
97 * @value: a #GValue.
99 * Sets @value as value for @attribute inside the hit.
102 void
103 mm_hit_set_value (MMHit *hit,
104 MMAttribute *attribute,
105 GValue *value)
107 const char *attribute_id;
108 AttributeValuePair *pair;
110 g_return_if_fail (MM_IS_HIT (hit));
112 if (G_VALUE_TYPE (value) != mm_attribute_get_value_type (attribute)) {
113 g_warning ("Setting the wrong value type for attribute %s",
114 mm_attribute_get_name (attribute));
115 return;
118 pair = g_slice_new0 (AttributeValuePair);
119 pair->attr = attribute;
120 pair->val = mm_create_gvalue_for_attribute (attribute);
121 g_value_copy (value, pair->val);
123 attribute_id = mm_attribute_get_id (attribute);
124 g_hash_table_insert (hit->details->attributes,
125 (char *) attribute_id,
126 pair);
130 * mm_hit_set_values:
131 * @hit: a #MMHit.
132 * @attributes: a list of #MMAttribute<!-- -->s.
133 * @values: a list of #GValue<!-- -->s.
135 * Set on the hit the values specified in @values for the attributes specified
136 * in @attributes. The two lists should be of identical order and length.
139 void
140 mm_hit_set_values (MMHit *hit,
141 GList *attributes,
142 GList *values)
144 GList *l, *m;
146 g_return_if_fail (MM_IS_HIT (hit));
147 g_return_if_fail (g_list_length (attributes) == g_list_length (values));
149 for (l = attributes, m = values; l && m; l = l->next, m = m->next) {
150 mm_hit_set_value (hit, l->data, m->data);
155 * mm_hit_get_values:
156 * @hit: a #MMHit.
157 * @ids: a space-separated list of attribute ids.
159 * Similar to #mm_hit_get_all_values<!-- -->, but gets the values only
160 * for the attributes specified by @ids. The lookup is done in a "best effort"
161 * way, i.e. if an attribute id doesn't have a value in @hit, it won't be
162 * there in the returned #GHashTable.
164 * Return value: a #GHashTable. Use #g_hash_table_destroy when done with it.
167 GHashTable *
168 mm_hit_get_values (MMHit *hit,
169 const char *ids)
171 GHashTable *table;
172 gchar **splitted_ids;
173 int idx;
174 AttributeValuePair *pair;
176 g_return_val_if_fail (MM_IS_HIT (hit), NULL);
177 table = g_hash_table_new (g_direct_hash, g_direct_equal);
179 splitted_ids = g_strsplit (ids, " ", 0);
180 for (idx = 0; idx < G_N_ELEMENTS (splitted_ids); idx++) {
181 pair = g_hash_table_lookup (hit->details->attributes,
182 splitted_ids[idx]);
183 if (!pair) {
184 /* we can't find the relevant attribute, keep looking for the
185 * others.
187 continue;
189 g_hash_table_insert (table, pair->attr, pair->val);
192 g_strfreev (splitted_ids);
194 return table;
198 * mm_hit_get_all_values:
199 * @hit: a #MMHit.
201 * Gets all the attributes and the relevant values set to the hit, organized
202 * in a #GHashTable, where the keys are #MMAttribute structures and the values
203 * are #GValue<!-- -->s. This works, as #MMAttribute structures are
204 * always static.
206 * Return value: a #GHashTable. Use #g_hash_table_destroy when done with it.
209 GHashTable *
210 mm_hit_get_all_values (MMHit *hit)
212 GHashTable *table;
214 g_return_val_if_fail (MM_IS_HIT (hit), NULL);
216 table = g_hash_table_new (g_direct_hash, g_direct_equal);
218 g_hash_table_foreach (hit->details->attributes,
219 build_all_table_foreach,
220 table);
221 return table;