Maintainer's effort v3:
[mmediamanager.git] / src / mm-hit.c
blob88ff629f95b0ea0ed6ccec10f9bc0498293eea7b
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 static void
76 build_all_table_foreach (gpointer k,
77 gpointer v,
78 gpointer ud)
80 GHashTable *table = ud;
81 AttributeValuePair *pair = v;
83 g_hash_table_insert (table, pair->attr, pair->val);
86 /* public methods */
88 void
89 mm_hit_set_value (MMHit *h,
90 MMAttribute *attribute,
91 GValue *value)
93 const char *attribute_id;
94 AttributeValuePair *pair;
96 g_return_if_fail (MM_IS_HIT (h));
98 if (G_VALUE_TYPE (value) != mm_attribute_get_value_type (attribute)) {
99 g_warning ("Setting the wrong value type for attribute %s",
100 mm_attribute_get_name (attribute));
101 return;
104 pair = g_slice_new0 (AttributeValuePair);
105 pair->attr = attribute;
106 pair->val = value;
108 attribute_id = mm_attribute_get_id (attribute);
109 g_hash_table_insert (h->details->attributes,
110 (char *) attribute_id,
111 pair);
114 void
115 mm_hit_set_values (MMHit *hit,
116 GList *attributes,
117 GList *values)
119 GList *l, *m;
121 g_return_if_fail (MM_IS_HIT (hit));
122 g_return_if_fail (g_list_length (attributes) == g_list_length (values));
124 for (l = attributes, m = values; l && m; l = l->next, m = m->next) {
125 mm_hit_set_value (hit, l->data, m->data);
129 GHashTable *
130 mm_hit_get_values (MMHit *hit,
131 const char *ids)
133 GHashTable *table;
134 gchar **splitted_ids;
135 int idx;
136 AttributeValuePair *pair;
138 g_return_val_if_fail (MM_IS_HIT (hit), NULL);
139 table = g_hash_table_new (g_direct_hash, g_direct_equal);
141 splitted_ids = g_strsplit (ids, " ", 0);
142 for (idx = 0; idx < G_N_ELEMENTS (splitted_ids); idx++) {
143 pair = g_hash_table_lookup (hit->details->attributes,
144 splitted_ids[idx]);
145 g_hash_table_insert (table, pair->attr, pair->val);
148 g_strfreev (splitted_ids);
150 return table;
153 GHashTable *
154 mm_hit_get_all_values (MMHit *hit)
156 GHashTable *table;
158 g_return_val_if_fail (MM_IS_HIT (hit), NULL);
160 table = g_hash_table_new (g_direct_hash, g_direct_equal);
162 g_hash_table_foreach (hit->details->attributes,
163 build_all_table_foreach,
164 table);
165 return table;