1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2008-2009 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28
#include "egg-debug.h"
30
#include "up-native.h"
31
#include "up-device-list.h"
33
static void up_device_list_finalize (GObject *object);
35
#define UP_DEVICE_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_DEVICE_LIST, UpDeviceListPrivate))
37
struct UpDeviceListPrivate
40
GHashTable *map_native_path_to_device;
43
G_DEFINE_TYPE (UpDeviceList, up_device_list, G_TYPE_OBJECT)
46
* up_device_list_lookup:
48
* Convert a native %GObject into a %UpDevice -- we use the native path
49
* to look these up as it's the only thing they share.
51
* Return value: the object, or %NULL if not found. Free with g_object_unref()
54
up_device_list_lookup (UpDeviceList *list, GObject *native)
57
const gchar *native_path;
59
g_return_val_if_fail (UP_IS_DEVICE_LIST (list), NULL);
61
/* does device exist in db? */
62
native_path = up_native_get_native_path (native);
63
if (native_path == NULL)
65
device = g_hash_table_lookup (list->priv->map_native_path_to_device, native_path);
68
return g_object_ref (device);
72
* up_device_list_insert:
74
* Insert a %GObject device and it's mapping to a backing %UpDevice
75
* into a list of devices.
78
up_device_list_insert (UpDeviceList *list, GObject *native, GObject *device)
80
const gchar *native_path;
82
g_return_val_if_fail (UP_IS_DEVICE_LIST (list), FALSE);
83
g_return_val_if_fail (native != NULL, FALSE);
84
g_return_val_if_fail (device != NULL, FALSE);
86
native_path = up_native_get_native_path (native);
87
if (native_path == NULL) {
88
egg_warning ("failed to get native path");
91
g_hash_table_insert (list->priv->map_native_path_to_device,
92
g_strdup (native_path), g_object_ref (device));
93
g_ptr_array_add (list->priv->array, g_object_ref (device));
94
egg_debug ("added %s", native_path);
99
* up_device_list_remove_cb:
102
up_device_list_remove_cb (gpointer key, gpointer value, gpointer user_data)
104
if (value == user_data) {
105
egg_debug ("removed %s", (char *) key);
112
* up_device_list_remove:
115
up_device_list_remove (UpDeviceList *list, GObject *device)
117
g_return_val_if_fail (UP_IS_DEVICE_LIST (list), FALSE);
118
g_return_val_if_fail (device != NULL, FALSE);
120
/* remove the device from the db */
121
g_hash_table_foreach_remove (list->priv->map_native_path_to_device,
122
up_device_list_remove_cb, device);
123
g_ptr_array_remove (list->priv->array, device);
125
/* we're removed the last instance? */
126
if (!G_IS_OBJECT (device)) {
127
egg_warning ("INTERNAL STATE CORRUPT: we've removed the last instance of %p", device);
135
* up_device_list_get_array:
137
* This is quick to iterate when we don't have GObject's to resolve
139
* Return value: the array, free with g_ptr_array_unref()
142
up_device_list_get_array (UpDeviceList *list)
144
g_return_val_if_fail (UP_IS_DEVICE_LIST (list), NULL);
145
return g_ptr_array_ref (list->priv->array);
149
* up_device_list_class_init:
150
* @klass: The UpDeviceListClass
153
up_device_list_class_init (UpDeviceListClass *klass)
155
GObjectClass *object_class = G_OBJECT_CLASS (klass);
156
object_class->finalize = up_device_list_finalize;
157
g_type_class_add_private (klass, sizeof (UpDeviceListPrivate));
161
* up_device_list_init:
162
* @list: This class instance
165
up_device_list_init (UpDeviceList *list)
167
list->priv = UP_DEVICE_LIST_GET_PRIVATE (list);
168
list->priv->array = g_ptr_array_new_with_free_func (g_object_unref);
169
list->priv->map_native_path_to_device = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
173
* up_device_list_finalize:
174
* @object: The object to finalize
177
up_device_list_finalize (GObject *object)
181
g_return_if_fail (UP_IS_DEVICE_LIST (object));
183
list = UP_DEVICE_LIST (object);
185
g_ptr_array_unref (list->priv->array);
186
g_hash_table_unref (list->priv->map_native_path_to_device);
188
G_OBJECT_CLASS (up_device_list_parent_class)->finalize (object);
192
* up_device_list_new:
194
* Return value: a new UpDeviceList object.
197
up_device_list_new (void)
200
list = g_object_new (UP_TYPE_DEVICE_LIST, NULL);
201
return UP_DEVICE_LIST (list);
205
/***************************************************************************
206
*** MAKE CHECK TESTS ***
207
***************************************************************************/
209
#include "egg-test.h"
212
up_device_list_test (gpointer user_data)
214
EggTest *test = (EggTest *) user_data;
221
if (!egg_test_start (test, "UpDeviceList"))
224
/************************************************************/
225
egg_test_title (test, "get instance");
226
list = up_device_list_new ();
227
egg_test_assert (test, list != NULL);
229
/************************************************************/
230
egg_test_title (test, "add device");
231
native = g_object_new (G_TYPE_OBJECT, NULL);
232
device = g_object_new (G_TYPE_OBJECT, NULL);
233
ret = up_device_list_insert (list, native, device);
234
egg_test_assert (test, ret);
236
/************************************************************/
237
egg_test_title (test, "find device");
238
found = up_device_list_lookup (list, native);
239
egg_test_assert (test, (found != NULL));
240
g_object_unref (found);
242
/************************************************************/
243
egg_test_title (test, "remove device");
244
ret = up_device_list_remove (list, device);
245
egg_test_assert (test, ret);
248
g_object_unref (native);
249
g_object_unref (device);
250
g_object_unref (list);