~xavi-garcia-mena/ubuntu/vivid/upower/percentages-power-off

« back to all changes in this revision

Viewing changes to src/up-device-list.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2010-02-16 10:16:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100216101624-2cmwqsr1ndftdd87
Tags: upstream-0.9.0+git20100216.72bb2
ImportĀ upstreamĀ versionĀ 0.9.0+git20100216.72bb2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2008-2009 Richard Hughes <richard@hughsie.com>
 
4
 *
 
5
 * Licensed under the GNU General Public License Version 2
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <glib.h>
 
27
 
 
28
#include "egg-debug.h"
 
29
 
 
30
#include "up-native.h"
 
31
#include "up-device-list.h"
 
32
 
 
33
static void     up_device_list_finalize (GObject                *object);
 
34
 
 
35
#define UP_DEVICE_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_DEVICE_LIST, UpDeviceListPrivate))
 
36
 
 
37
struct UpDeviceListPrivate
 
38
{
 
39
        GPtrArray               *array;
 
40
        GHashTable              *map_native_path_to_device;
 
41
};
 
42
 
 
43
G_DEFINE_TYPE (UpDeviceList, up_device_list, G_TYPE_OBJECT)
 
44
 
 
45
/**
 
46
 * up_device_list_lookup:
 
47
 *
 
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.
 
50
 *
 
51
 * Return value: the object, or %NULL if not found. Free with g_object_unref()
 
52
 **/
 
53
GObject *
 
54
up_device_list_lookup (UpDeviceList *list, GObject *native)
 
55
{
 
56
        GObject *device;
 
57
        const gchar *native_path;
 
58
 
 
59
        g_return_val_if_fail (UP_IS_DEVICE_LIST (list), NULL);
 
60
 
 
61
        /* does device exist in db? */
 
62
        native_path = up_native_get_native_path (native);
 
63
        if (native_path == NULL)
 
64
                return NULL;
 
65
        device = g_hash_table_lookup (list->priv->map_native_path_to_device, native_path);
 
66
        if (device == NULL)
 
67
                return NULL;
 
68
        return g_object_ref (device);
 
69
}
 
70
 
 
71
/**
 
72
 * up_device_list_insert:
 
73
 *
 
74
 * Insert a %GObject device and it's mapping to a backing %UpDevice
 
75
 * into a list of devices.
 
76
 **/
 
77
gboolean
 
78
up_device_list_insert (UpDeviceList *list, GObject *native, GObject *device)
 
79
{
 
80
        const gchar *native_path;
 
81
 
 
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);
 
85
 
 
86
        native_path = up_native_get_native_path (native);
 
87
        if (native_path == NULL) {
 
88
                egg_warning ("failed to get native path");
 
89
                return FALSE;
 
90
        }
 
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);
 
95
        return TRUE;
 
96
}
 
97
 
 
98
/**
 
99
 * up_device_list_remove_cb:
 
100
 **/
 
101
static gboolean
 
102
up_device_list_remove_cb (gpointer key, gpointer value, gpointer user_data)
 
103
{
 
104
        if (value == user_data) {
 
105
                egg_debug ("removed %s", (char *) key);
 
106
                return TRUE;
 
107
        }
 
108
        return FALSE;
 
109
}
 
110
 
 
111
/**
 
112
 * up_device_list_remove:
 
113
 **/
 
114
gboolean
 
115
up_device_list_remove (UpDeviceList *list, GObject *device)
 
116
{
 
117
        g_return_val_if_fail (UP_IS_DEVICE_LIST (list), FALSE);
 
118
        g_return_val_if_fail (device != NULL, FALSE);
 
119
 
 
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);
 
124
 
 
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);
 
128
                return FALSE;
 
129
        }
 
130
 
 
131
        return TRUE;
 
132
}
 
133
 
 
134
/**
 
135
 * up_device_list_get_array:
 
136
 *
 
137
 * This is quick to iterate when we don't have GObject's to resolve
 
138
 *
 
139
 * Return value: the array, free with g_ptr_array_unref()
 
140
 **/
 
141
GPtrArray       *
 
142
up_device_list_get_array (UpDeviceList *list)
 
143
{
 
144
        g_return_val_if_fail (UP_IS_DEVICE_LIST (list), NULL);
 
145
        return g_ptr_array_ref (list->priv->array);
 
146
}
 
147
 
 
148
/**
 
149
 * up_device_list_class_init:
 
150
 * @klass: The UpDeviceListClass
 
151
 **/
 
152
static void
 
153
up_device_list_class_init (UpDeviceListClass *klass)
 
154
{
 
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));
 
158
}
 
159
 
 
160
/**
 
161
 * up_device_list_init:
 
162
 * @list: This class instance
 
163
 **/
 
164
static void
 
165
up_device_list_init (UpDeviceList *list)
 
166
{
 
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);
 
170
}
 
171
 
 
172
/**
 
173
 * up_device_list_finalize:
 
174
 * @object: The object to finalize
 
175
 **/
 
176
static void
 
177
up_device_list_finalize (GObject *object)
 
178
{
 
179
        UpDeviceList *list;
 
180
 
 
181
        g_return_if_fail (UP_IS_DEVICE_LIST (object));
 
182
 
 
183
        list = UP_DEVICE_LIST (object);
 
184
 
 
185
        g_ptr_array_unref (list->priv->array);
 
186
        g_hash_table_unref (list->priv->map_native_path_to_device);
 
187
 
 
188
        G_OBJECT_CLASS (up_device_list_parent_class)->finalize (object);
 
189
}
 
190
 
 
191
/**
 
192
 * up_device_list_new:
 
193
 *
 
194
 * Return value: a new UpDeviceList object.
 
195
 **/
 
196
UpDeviceList *
 
197
up_device_list_new (void)
 
198
{
 
199
        UpDeviceList *list;
 
200
        list = g_object_new (UP_TYPE_DEVICE_LIST, NULL);
 
201
        return UP_DEVICE_LIST (list);
 
202
}
 
203
 
 
204
 
 
205
/***************************************************************************
 
206
 ***                          MAKE CHECK TESTS                           ***
 
207
 ***************************************************************************/
 
208
#ifdef EGG_TEST
 
209
#include "egg-test.h"
 
210
 
 
211
void
 
212
up_device_list_test (gpointer user_data)
 
213
{
 
214
        EggTest *test = (EggTest *) user_data;
 
215
        UpDeviceList *list;
 
216
        GObject *native;
 
217
        GObject *device;
 
218
        GObject *found;
 
219
        gboolean ret;
 
220
 
 
221
        if (!egg_test_start (test, "UpDeviceList"))
 
222
                return;
 
223
 
 
224
        /************************************************************/
 
225
        egg_test_title (test, "get instance");
 
226
        list = up_device_list_new ();
 
227
        egg_test_assert (test, list != NULL);
 
228
 
 
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);
 
235
 
 
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);
 
241
 
 
242
        /************************************************************/
 
243
        egg_test_title (test, "remove device");
 
244
        ret = up_device_list_remove (list, device);
 
245
        egg_test_assert (test, ret);
 
246
 
 
247
        /* unref */
 
248
        g_object_unref (native);
 
249
        g_object_unref (device);
 
250
        g_object_unref (list);
 
251
 
 
252
        egg_test_end (test);
 
253
}
 
254
#endif
 
255