1 From eeac836f256fe07004d67e13c3c89532982af9bd Mon Sep 17 00:00:00 2001
2 From: Richard Hughes <richard@hughsie.com>
3 Date: Mon, 20 Jul 2015 10:57:28 +0100
4 Subject: [PATCH] Allow creating devices with the same device ID from different
8 src/cd-device-array.c | 13 ++++++++-----
9 src/cd-device-array.h | 9 ++++++++-
10 src/cd-main.c | 15 ++++++++++-----
11 src/cd-self-test.c | 7 +++++--
12 4 files changed, 31 insertions(+), 13 deletions(-)
14 diff --git a/src/cd-device-array.c b/src/cd-device-array.c
15 index 88d4b2c..41a9597 100644
16 --- a/src/cd-device-array.c
17 +++ b/src/cd-device-array.c
18 @@ -76,7 +76,8 @@ cd_device_array_remove (CdDeviceArray *device_array, CdDevice *device)
20 cd_device_array_get_by_id_owner (CdDeviceArray *device_array,
24 + CdDeviceArrayFlags flags)
26 CdDeviceArrayPrivate *priv = device_array->priv;
28 @@ -90,10 +91,12 @@ cd_device_array_get_by_id_owner (CdDeviceArray *device_array,
29 if (g_strcmp0 (cd_device_get_id (device_tmp), id) == 0)
30 return g_object_ref (device_tmp);
32 - for (i = 0; i < priv->array->len; i++) {
33 - device_tmp = g_ptr_array_index (priv->array, i);
34 - if (g_strcmp0 (cd_device_get_id (device_tmp), id) == 0)
35 - return g_object_ref (device_tmp);
36 + if (flags & CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL) {
37 + for (i = 0; i < priv->array->len; i++) {
38 + device_tmp = g_ptr_array_index (priv->array, i);
39 + if (g_strcmp0 (cd_device_get_id (device_tmp), id) == 0)
40 + return g_object_ref (device_tmp);
45 diff --git a/src/cd-device-array.h b/src/cd-device-array.h
46 index b625dbf..31dc067 100644
47 --- a/src/cd-device-array.h
48 +++ b/src/cd-device-array.h
49 @@ -50,6 +50,12 @@ struct _CdDeviceArrayClass
50 GObjectClass parent_class;
54 + CD_DEVICE_ARRAY_FLAG_NONE = 0,
55 + CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL = 1,
56 + CD_DEVICE_ARRAY_FLAG_LAST,
57 +} CdDeviceArrayFlags;
59 GType cd_device_array_get_type (void);
60 CdDeviceArray *cd_device_array_new (void);
62 @@ -59,7 +65,8 @@ void cd_device_array_remove (CdDeviceArray *device_array,
64 CdDevice *cd_device_array_get_by_id_owner (CdDeviceArray *device_array,
68 + CdDeviceArrayFlags flags);
69 CdDevice *cd_device_array_get_by_object_path (CdDeviceArray *device_array,
70 const gchar *object_path);
71 CdDevice *cd_device_array_get_by_property (CdDeviceArray *device_array,
72 diff --git a/src/cd-main.c b/src/cd-main.c
73 index b5b3a4f..37817e3 100644
76 @@ -720,7 +720,8 @@ cd_main_profile_auto_add_from_db (CdMainPrivate *priv,
77 device_id_tmp = g_ptr_array_index (array, i);
78 device_tmp = cd_device_array_get_by_id_owner (priv->devices_array,
80 - cd_profile_get_owner (profile));
81 + cd_profile_get_owner (profile),
82 + CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
83 if (device_tmp == NULL)
86 @@ -747,7 +748,8 @@ cd_main_profile_auto_add_from_md (CdMainPrivate *priv,
88 device = cd_device_array_get_by_id_owner (priv->devices_array,
90 - cd_profile_get_owner (profile));
91 + cd_profile_get_owner (profile),
92 + CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
95 cd_main_auto_add_from_md (priv, device, profile);
96 @@ -1022,7 +1024,8 @@ cd_main_daemon_method_call (GDBusConnection *connection, const gchar *sender,
98 device = cd_device_array_get_by_id_owner (priv->devices_array,
102 + CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
103 if (device == NULL) {
104 g_dbus_method_invocation_return_error (invocation,
106 @@ -1279,7 +1282,8 @@ cd_main_daemon_method_call (GDBusConnection *connection, const gchar *sender,
108 device = cd_device_array_get_by_id_owner (priv->devices_array,
112 + CD_DEVICE_ARRAY_FLAG_NONE);
113 if (device != NULL) {
114 /* where we try to manually add an existing
115 * virtual device, which means promoting it to
116 @@ -1399,7 +1403,8 @@ cd_main_daemon_method_call (GDBusConnection *connection, const gchar *sender,
118 device = cd_device_array_get_by_id_owner (priv->devices_array,
122 + CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
123 if (device == NULL) {
124 /* fall back to checking the object path */
125 device = cd_device_array_get_by_object_path (priv->devices_array,
126 diff --git a/src/cd-self-test.c b/src/cd-self-test.c
127 index 4a76d07..6536d3c 100644
128 --- a/src/cd-self-test.c
129 +++ b/src/cd-self-test.c
130 @@ -178,10 +178,13 @@ colord_device_array_func (void)
131 cd_device_array_add (device_array, device);
132 g_object_unref (device);
134 - device = cd_device_array_get_by_id_owner (device_array, "does not exist", 0);
135 + device = cd_device_array_get_by_id_owner (device_array, "does not exist", 0, CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
136 g_assert (device == NULL);
138 - device = cd_device_array_get_by_id_owner (device_array, "dave", 0);
139 + device = cd_device_array_get_by_id_owner (device_array, "dave", 999, CD_DEVICE_ARRAY_FLAG_NONE);
140 + g_assert (device == NULL);
142 + device = cd_device_array_get_by_id_owner (device_array, "dave", 0, CD_DEVICE_ARRAY_FLAG_OWNER_OPTIONAL);
143 g_assert (device != NULL);
144 g_assert_cmpstr (cd_device_get_id (device), ==, "dave");
145 g_object_unref (device);