Debian Codegraph @

src/cd-device-array.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010-2011 Richard Hughes <[email protected]>
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 <glib-object.h>
25
26 #include "cd-common.h"
27 #include "cd-device-array.h"
28
29 static void cd_device_array_finalize (GObject *object);
30
31 #define GET_PRIVATE(o) (cd_device_array_get_instance_private (o))
32
33 typedef struct
34 {
35 GPtrArray *array;
36 } CdDeviceArrayPrivate;
37
38 G_DEFINE_TYPE_WITH_PRIVATE (CdDeviceArray, cd_device_array, G_TYPE_OBJECT)
39
40 static gpointer cd_device_array_object = NULL;
41
42 void
43 cd_device_array_add (CdDeviceArray *device_array, CdDevice *device)
44 {
45 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
46
47 g_return_if_fail (CD_IS_DEVICE_ARRAY (device_array));
48 g_return_if_fail (CD_IS_DEVICE (device));
49
50 g_ptr_array_add (priv->array,
51 g_object_ref (device));
52 }
53
54 void
55 cd_device_array_remove (CdDeviceArray *device_array, CdDevice *device)
56 {
57 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
58
59 g_return_if_fail (CD_IS_DEVICE_ARRAY (device_array));
60 g_return_if_fail (CD_IS_DEVICE (device));
61
62 g_ptr_array_remove (priv->array,
63 device);
64 }
65
66 CdDevice *
67 cd_device_array_get_by_id_owner (CdDeviceArray *device_array,
68 const gchar *id,
69 guint owner,
70 CdDeviceArrayFlags flags)
71 {
72 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
73 CdDevice *device_tmp;
74 guint i;
75
76 /* find device */
77 for (i = 0; i < priv->array->len; i++) {
78 device_tmp = g_ptr_array_index (priv->array, i);
79 if (cd_device_get_owner (device_tmp) != owner)
80 continue;
81 if (g_strcmp0 (cd_device_get_id (device_tmp), id) == 0)
82 return g_object_ref (device_tmp);
83 }
84 if (flags & CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL) {
85 for (i = 0; i < priv->array->len; i++) {
86 device_tmp = g_ptr_array_index (priv->array, i);
87 if (g_strcmp0 (cd_device_get_id (device_tmp), id) == 0)
88 return g_object_ref (device_tmp);
89 }
90 }
91 return NULL;
92 }
93
94 CdDevice *
95 cd_device_array_get_by_object_path (CdDeviceArray *device_array,
96 const gchar *object_path)
97 {
98 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
99 CdDevice *device_tmp;
100 guint i;
101
102 /* find device */
103 for (i = 0; i < priv->array->len; i++) {
104 device_tmp = g_ptr_array_index (priv->array, i);
105 if (g_strcmp0 (cd_device_get_object_path (device_tmp), object_path) == 0)
106 return g_object_ref (device_tmp);
107 }
108 return NULL;
109 }
110
111 CdDevice *
112 cd_device_array_get_by_property (CdDeviceArray *device_array,
113 const gchar *key,
114 const gchar *value)
115 {
116 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
117 CdDevice *device_tmp;
118 const gchar *value_tmp;
119 guint i;
120
121 /* find device */
122 for (i = 0; i < priv->array->len; i++) {
123 device_tmp = g_ptr_array_index (priv->array, i);
124 value_tmp = cd_device_get_metadata (device_tmp, key);
125 if (g_strcmp0 (value_tmp, value) == 0)
126 return g_object_ref (device_tmp);
127 }
128 return NULL;
129 }
130
131 GPtrArray *
132 cd_device_array_get_array (CdDeviceArray *device_array)
133 {
134 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
135 return g_ptr_array_ref (priv->array);
136 }
137
138 GPtrArray *
139 cd_device_array_get_by_kind (CdDeviceArray *device_array,
140 CdDeviceKind kind)
141 {
142 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
143 CdDevice *device_tmp;
144 GPtrArray *array_tmp = NULL;
145 guint i;
146
147 /* return all that match kind */
148 array_tmp = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
149 for (i = 0; i < priv->array->len; i++) {
150 device_tmp = g_ptr_array_index (priv->array, i);
151 if (cd_device_get_kind (device_tmp) == kind) {
152 g_ptr_array_add (array_tmp,
153 g_object_ref (device_tmp));
154 }
155 }
156
157 return array_tmp;
158 }
159
160 static void
161 cd_device_array_class_init (CdDeviceArrayClass *klass)
162 {
163 GObjectClass *object_class = G_OBJECT_CLASS (klass);
164 object_class->finalize = cd_device_array_finalize;
165 }
166
167 static void
168 cd_device_array_init (CdDeviceArray *device_array)
169 {
170 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
171 priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
172 }
173
174 static void
175 cd_device_array_finalize (GObject *object)
176 {
177 CdDeviceArray *device_array = CD_DEVICE_ARRAY (object);
178 CdDeviceArrayPrivate *priv = GET_PRIVATE (device_array);
179
180 g_ptr_array_unref (priv->array);
181
182 G_OBJECT_CLASS (cd_device_array_parent_class)->finalize (object);
183 }
184
185 CdDeviceArray *
186 cd_device_array_new (void)
187 {
188 if (cd_device_array_object != NULL) {
189 g_object_ref (cd_device_array_object);
190 } else {
191 cd_device_array_object = g_object_new (CD_TYPE_DEVICE_ARRAY, NULL);
192 g_object_add_weak_pointer (cd_device_array_object,
193 &cd_device_array_object);
194 }
195 return CD_DEVICE_ARRAY (cd_device_array_object);
196 }
197