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.
22 #include <glib-object.h>
25 #include "mm-attribute.h"
28 #define MM_HIT_GET_PRIVATE(o) \
29 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_HIT, MMHitPrivate))
31 G_DEFINE_TYPE (MMHit
, mm_hit
, G_TYPE_OBJECT
);
33 struct _MMHitPrivate
{
34 GHashTable
*attributes
;
43 mm_hit_finalize (GObject
*o
)
45 MMHitPrivate
*details
= MM_HIT (o
)->details
;
47 g_hash_table_destroy (details
->attributes
);
49 G_OBJECT_CLASS (mm_hit_parent_class
)->finalize (o
);
53 pair_free (gpointer data
)
55 AttributeValuePair
*pair
= data
;
57 g_value_unset (pair
->val
);
59 g_slice_free (AttributeValuePair
, data
);
63 mm_hit_init (MMHit
*h
)
65 MMHitPrivate
*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
);
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 (MMHitPrivate
));
81 build_all_table_foreach (gpointer k
,
85 GHashTable
*table
= ud
;
86 AttributeValuePair
*pair
= v
;
88 g_hash_table_insert (table
, pair
->attr
, pair
->val
);
96 * @attribute: a #MMAttribute.
99 * Sets @value as value for @attribute inside the hit.
103 mm_hit_set_value (MMHit
*hit
,
104 MMAttribute
*attribute
,
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
));
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
,
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.
140 mm_hit_set_values (MMHit
*hit
,
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
);
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.
168 mm_hit_get_values (MMHit
*hit
,
172 gchar
**splitted_ids
;
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
,
184 /* we can't find the relevant attribute, keep looking for the
189 g_hash_table_insert (table
, pair
->attr
, pair
->val
);
192 g_strfreev (splitted_ids
);
198 * mm_hit_get_all_values:
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
206 * Return value: a #GHashTable. Use #g_hash_table_destroy when done with it.
210 mm_hit_get_all_values (MMHit
*hit
)
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
,