2
* This file is part of hildon-desktop
4
* Copyright (C) 2009 Nokia Corporation.
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public License
8
* as published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27
#include "hd-multi-map.h"
29
#define HD_MULTI_MAP_GET_PRIVATE(object) \
30
(G_TYPE_INSTANCE_GET_PRIVATE ((object), HD_TYPE_MULTI_MAP, HDMultiMapPrivate))
32
struct _HDMultiMapPrivate
37
static void hd_multi_map_dispose (GObject *object);
39
static GList *remove_value_from_list (GList *list,
42
static void remove_and_free_all_keys_and_values (GHashTable *map);
43
static void free_values_list (GList *list);
45
G_DEFINE_TYPE (HDMultiMap, hd_multi_map, G_TYPE_INITIALLY_UNOWNED);
48
hd_multi_map_new (void)
50
HDMultiMap *multi_map;
52
multi_map = g_object_new (HD_TYPE_MULTI_MAP,
59
hd_multi_map_class_init (HDMultiMapClass *klass)
61
GObjectClass *object_class = G_OBJECT_CLASS (klass);
63
object_class->dispose = hd_multi_map_dispose;
65
g_type_class_add_private (klass, sizeof (HDMultiMapPrivate));
69
hd_multi_map_init (HDMultiMap *multi_map)
71
HDMultiMapPrivate *priv;
73
priv = multi_map->priv = HD_MULTI_MAP_GET_PRIVATE (multi_map);
75
priv->map = g_hash_table_new_full (g_direct_hash,
77
(GDestroyNotify) g_object_unref,
82
hd_multi_map_dispose (GObject *object)
84
HDMultiMap *multi_map = HD_MULTI_MAP (object);
85
HDMultiMapPrivate *priv = multi_map->priv;
89
hd_multi_map_remove_all (multi_map);
90
priv->map = (g_hash_table_destroy (priv->map), NULL);
93
G_OBJECT_CLASS (hd_multi_map_parent_class)->dispose (object);
97
hd_multi_map_insert (HDMultiMap *multi_map,
101
HDMultiMapPrivate *priv = multi_map->priv;
104
g_return_if_fail (HD_IS_MULTI_MAP (multi_map));
106
values = g_hash_table_lookup (priv->map,
108
values = g_list_append (values, g_object_ref (value));
109
g_hash_table_insert (priv->map,
115
hd_multi_map_remove (HDMultiMap *multi_map,
119
HDMultiMapPrivate *priv = multi_map->priv;
122
g_return_if_fail (HD_IS_MULTI_MAP (multi_map));
124
values = g_hash_table_lookup (priv->map,
126
values = remove_value_from_list (values,
129
g_hash_table_insert (priv->map,
133
g_hash_table_remove (priv->map,
138
remove_value_from_list (GList *list,
143
link = g_list_find (list, value);
146
g_object_unref (link->data);
147
list = g_list_delete_link (list, link);
154
hd_multi_map_remove_all (HDMultiMap *multi_map)
156
HDMultiMapPrivate *priv = multi_map->priv;
158
g_return_if_fail (HD_IS_MULTI_MAP (multi_map));
160
remove_and_free_all_keys_and_values (priv->map);
164
remove_and_free_all_keys_and_values (GHashTable *map)
167
gpointer key, values;
169
g_hash_table_iter_init (&iter, map);
170
while (g_hash_table_iter_next (&iter, &key, &values))
172
free_values_list (values);
173
g_hash_table_iter_remove (&iter);
178
free_values_list (GList *list)
180
g_list_foreach (list, (GFunc) g_object_unref, NULL);