~cordia-team/+junk/hildon-home

« back to all changes in this revision

Viewing changes to src/hd-multi-map.c

  • Committer: Thomas-Karl Pietrowski
  • Date: 2011-10-09 16:56:50 UTC
  • Revision ID: thopiekar@googlemail.com-20111009165650-4c3oct3pk33c2fb3
first release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of hildon-desktop
 
3
 *
 
4
 * Copyright (C) 2009 Nokia Corporation.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 * 02110-1301 USA
 
20
 *
 
21
 */
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#include "hd-multi-map.h"
 
28
 
 
29
#define HD_MULTI_MAP_GET_PRIVATE(object) \
 
30
  (G_TYPE_INSTANCE_GET_PRIVATE ((object), HD_TYPE_MULTI_MAP, HDMultiMapPrivate))
 
31
 
 
32
struct _HDMultiMapPrivate
 
33
{
 
34
  GHashTable *map;
 
35
};
 
36
 
 
37
static void hd_multi_map_dispose     (GObject *object);
 
38
 
 
39
static GList *remove_value_from_list (GList   *list,
 
40
                                      GObject *value);
 
41
 
 
42
static void remove_and_free_all_keys_and_values (GHashTable *map);
 
43
static void free_values_list (GList *list);
 
44
 
 
45
G_DEFINE_TYPE (HDMultiMap, hd_multi_map, G_TYPE_INITIALLY_UNOWNED);
 
46
 
 
47
HDMultiMap *
 
48
hd_multi_map_new (void)
 
49
{
 
50
  HDMultiMap *multi_map;
 
51
 
 
52
  multi_map = g_object_new (HD_TYPE_MULTI_MAP,
 
53
                            NULL);
 
54
 
 
55
  return multi_map;
 
56
}
 
57
 
 
58
static void
 
59
hd_multi_map_class_init (HDMultiMapClass *klass)
 
60
{
 
61
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
62
 
 
63
  object_class->dispose = hd_multi_map_dispose;
 
64
 
 
65
  g_type_class_add_private (klass, sizeof (HDMultiMapPrivate));
 
66
}
 
67
 
 
68
static void
 
69
hd_multi_map_init (HDMultiMap *multi_map)
 
70
{
 
71
  HDMultiMapPrivate *priv;
 
72
 
 
73
  priv = multi_map->priv = HD_MULTI_MAP_GET_PRIVATE (multi_map);
 
74
 
 
75
  priv->map = g_hash_table_new_full (g_direct_hash,
 
76
                                     g_direct_equal,
 
77
                                     (GDestroyNotify) g_object_unref,
 
78
                                     NULL);
 
79
}
 
80
 
 
81
static void
 
82
hd_multi_map_dispose (GObject *object)
 
83
{
 
84
  HDMultiMap *multi_map = HD_MULTI_MAP (object);
 
85
  HDMultiMapPrivate *priv = multi_map->priv;
 
86
 
 
87
  if (priv->map)
 
88
    {
 
89
      hd_multi_map_remove_all (multi_map);
 
90
      priv->map = (g_hash_table_destroy (priv->map), NULL);
 
91
    }
 
92
 
 
93
  G_OBJECT_CLASS (hd_multi_map_parent_class)->dispose (object);
 
94
}
 
95
 
 
96
void
 
97
hd_multi_map_insert (HDMultiMap *multi_map,
 
98
                     GObject    *key,
 
99
                     GObject    *value)
 
100
{
 
101
  HDMultiMapPrivate *priv = multi_map->priv;
 
102
  GList *values;
 
103
 
 
104
  g_return_if_fail (HD_IS_MULTI_MAP (multi_map));
 
105
 
 
106
  values = g_hash_table_lookup (priv->map,
 
107
                                key);
 
108
  values = g_list_append (values, g_object_ref (value));
 
109
  g_hash_table_insert (priv->map,
 
110
                       g_object_ref (key),
 
111
                       values);
 
112
}
 
113
 
 
114
void
 
115
hd_multi_map_remove (HDMultiMap *multi_map,
 
116
                     GObject    *key,
 
117
                     GObject    *value)
 
118
{
 
119
  HDMultiMapPrivate *priv = multi_map->priv;
 
120
  GList *values;
 
121
 
 
122
  g_return_if_fail (HD_IS_MULTI_MAP (multi_map));
 
123
 
 
124
  values = g_hash_table_lookup (priv->map,
 
125
                                key);
 
126
  values = remove_value_from_list (values,
 
127
                                   value);
 
128
  if (values)
 
129
    g_hash_table_insert (priv->map,
 
130
                         g_object_ref (key),
 
131
                         values);
 
132
  else
 
133
    g_hash_table_remove (priv->map,
 
134
                         key);
 
135
}
 
136
 
 
137
static GList *
 
138
remove_value_from_list (GList   *list,
 
139
                        GObject *value)
 
140
{
 
141
  GList *link;
 
142
 
 
143
  link = g_list_find (list, value);
 
144
  if (link)
 
145
    {
 
146
      g_object_unref (link->data);
 
147
      list = g_list_delete_link (list, link);
 
148
    }
 
149
 
 
150
  return list;
 
151
}
 
152
 
 
153
void
 
154
hd_multi_map_remove_all (HDMultiMap *multi_map)
 
155
{
 
156
  HDMultiMapPrivate *priv = multi_map->priv;
 
157
 
 
158
  g_return_if_fail (HD_IS_MULTI_MAP (multi_map));
 
159
 
 
160
  remove_and_free_all_keys_and_values (priv->map);
 
161
}
 
162
 
 
163
static void
 
164
remove_and_free_all_keys_and_values (GHashTable *map)
 
165
{
 
166
  GHashTableIter iter;
 
167
  gpointer key, values;
 
168
 
 
169
  g_hash_table_iter_init (&iter, map);
 
170
  while (g_hash_table_iter_next (&iter, &key, &values)) 
 
171
    {
 
172
      free_values_list (values);
 
173
      g_hash_table_iter_remove (&iter);
 
174
    }
 
175
}
 
176
 
 
177
static void
 
178
free_values_list (GList *list)
 
179
{
 
180
  g_list_foreach (list, (GFunc) g_object_unref, NULL);
 
181
  g_list_free (list);
 
182
}
 
183