Make DkpDeviceList work with GObject types, and not with specific DkpDevice types

This commit is contained in:
Richard Hughes 2009-07-02 15:51:53 +01:00
parent 64033ba35f
commit 5e2477607f
3 changed files with 44 additions and 39 deletions

View file

@ -489,16 +489,18 @@ out:
static void
dkp_daemon_device_changed (DkpDaemon *daemon, DevkitDevice *d, gboolean synthesized)
{
GObject *object;
DkpDevice *device;
gboolean ret;
/* first, change the device and add it if it doesn't exist */
device = dkp_device_list_lookup (daemon->priv->list, d);
if (device != NULL) {
object = dkp_device_list_lookup (daemon->priv->list, d);
if (object != NULL) {
device = DKP_DEVICE (object);
egg_debug ("changed %s", dkp_device_get_object_path (device));
dkp_device_changed (device, d, synthesized);
} else {
egg_debug ("treating change event as add on %s", dkp_device_get_object_path (device));
egg_debug ("treating change event as add on %s", devkit_device_get_native_path (d));
dkp_daemon_device_add (daemon, d, TRUE);
}
@ -524,10 +526,9 @@ dkp_daemon_device_changed (DkpDaemon *daemon, DevkitDevice *d, gboolean synthesi
* dkp_daemon_device_went_away:
**/
static void
dkp_daemon_device_went_away (gpointer user_data, GObject *_device)
dkp_daemon_device_went_away (gpointer user_data, GObject *device)
{
DkpDaemon *daemon = DKP_DAEMON (user_data);
DkpDevice *device = DKP_DEVICE (_device);
dkp_device_list_remove (daemon->priv->list, device);
}
@ -617,12 +618,14 @@ out:
static gboolean
dkp_daemon_device_add (DkpDaemon *daemon, DevkitDevice *d, gboolean emit_event)
{
GObject *object;
DkpDevice *device;
gboolean ret = TRUE;
/* does device exist in db? */
device = dkp_device_list_lookup (daemon->priv->list, d);
if (device != NULL) {
object = dkp_device_list_lookup (daemon->priv->list, d);
if (object != NULL) {
device = DKP_DEVICE (object);
/* we already have the device; treat as change event */
egg_debug ("treating add event as change event on %s", dkp_device_get_object_path (device));
dkp_daemon_device_changed (daemon, d, FALSE);
@ -639,7 +642,7 @@ dkp_daemon_device_add (DkpDaemon *daemon, DevkitDevice *d, gboolean emit_event)
* it's unreffed. So if we ref it, it'll never go away.
*/
g_object_weak_ref (G_OBJECT (device), dkp_daemon_device_went_away, daemon);
dkp_device_list_insert (daemon->priv->list, d, device);
dkp_device_list_insert (daemon->priv->list, d, G_OBJECT (device));
if (emit_event) {
g_signal_emit (daemon, signals[DEVICE_ADDED_SIGNAL], 0,
dkp_device_get_object_path (device));
@ -655,13 +658,15 @@ out:
static void
dkp_daemon_device_remove (DkpDaemon *daemon, DevkitDevice *d)
{
GObject *object;
DkpDevice *device;
/* does device exist in db? */
device = dkp_device_list_lookup (daemon->priv->list, d);
if (device == NULL) {
object = dkp_device_list_lookup (daemon->priv->list, d);
if (object == NULL) {
egg_debug ("ignoring remove event on %s", devkit_device_get_native_path (d));
} else {
device = DKP_DEVICE (object);
dkp_device_removed (device);
g_signal_emit (daemon, signals[DEVICE_REMOVED_SIGNAL], 0,
dkp_device_get_object_path (device));

View file

@ -1,6 +1,6 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2008-2009 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
@ -26,7 +26,7 @@
#include <glib.h>
#include "egg-debug.h"
#include "dkp-device.h"
#include "dkp-device-list.h"
static void dkp_device_list_class_init (DkpDeviceListClass *klass);
@ -38,7 +38,7 @@ static void dkp_device_list_finalize (GObject *object);
struct DkpDeviceListPrivate
{
GPtrArray *array;
GHashTable *map_native_path_to_device;
GHashTable *map_native_path_to_object;
};
G_DEFINE_TYPE (DkpDeviceList, dkp_device_list, G_TYPE_OBJECT)
@ -46,42 +46,42 @@ G_DEFINE_TYPE (DkpDeviceList, dkp_device_list, G_TYPE_OBJECT)
/**
* dkp_device_list_lookup:
*
* Convert a %DevkitDevice into a %DkpDevice -- we use the native path
* Convert a %DevkitDevice into a %GObject -- we use the native path
* to look these up as it's the only thing they share.
**/
DkpDevice *
dkp_device_list_lookup (DkpDeviceList *list, DevkitDevice *d)
GObject *
dkp_device_list_lookup (DkpDeviceList *list, DevkitDevice *device)
{
DkpDevice *device;
GObject *object;
const gchar *native_path;
g_return_val_if_fail (DKP_IS_DEVICE_LIST (list), NULL);
/* does device exist in db? */
native_path = devkit_device_get_native_path (d);
device = g_hash_table_lookup (list->priv->map_native_path_to_device, native_path);
return device;
native_path = devkit_device_get_native_path (device);
object = g_hash_table_lookup (list->priv->map_native_path_to_object, native_path);
return object;
}
/**
* dkp_device_list_insert:
*
* Insert a %DevkitDevice device and it's mapping to a %DkpDevice device
* Insert a %DevkitDevice device and it's mapping to a backing %GObject
* into a list of devices.
**/
gboolean
dkp_device_list_insert (DkpDeviceList *list, DevkitDevice *d, DkpDevice *device)
dkp_device_list_insert (DkpDeviceList *list, DevkitDevice *device, GObject *object)
{
const gchar *native_path;
g_return_val_if_fail (DKP_IS_DEVICE_LIST (list), FALSE);
g_return_val_if_fail (d != NULL, FALSE);
g_return_val_if_fail (device != NULL, FALSE);
g_return_val_if_fail (object != NULL, FALSE);
native_path = devkit_device_get_native_path (d);
g_hash_table_insert (list->priv->map_native_path_to_device,
g_strdup (native_path), device);
g_ptr_array_add (list->priv->array, device);
native_path = devkit_device_get_native_path (device);
g_hash_table_insert (list->priv->map_native_path_to_object,
g_strdup (native_path), object);
g_ptr_array_add (list->priv->array, object);
egg_debug ("added %s", native_path);
return TRUE;
}
@ -103,15 +103,15 @@ dkp_device_list_remove_cb (gpointer key, gpointer value, gpointer user_data)
* dkp_device_list_remove:
**/
gboolean
dkp_device_list_remove (DkpDeviceList *list, DkpDevice *device)
dkp_device_list_remove (DkpDeviceList *list, GObject *object)
{
g_return_val_if_fail (DKP_IS_DEVICE_LIST (list), FALSE);
g_return_val_if_fail (device != NULL, FALSE);
g_return_val_if_fail (object != NULL, FALSE);
/* remove the device from the db */
g_hash_table_foreach_remove (list->priv->map_native_path_to_device,
dkp_device_list_remove_cb, device);
g_ptr_array_remove (list->priv->array, device);
g_hash_table_foreach_remove (list->priv->map_native_path_to_object,
dkp_device_list_remove_cb, object);
g_ptr_array_remove (list->priv->array, object);
return TRUE;
}
@ -148,7 +148,7 @@ dkp_device_list_init (DkpDeviceList *list)
{
list->priv = DKP_DEVICE_LIST_GET_PRIVATE (list);
list->priv->array = g_ptr_array_new ();
list->priv->map_native_path_to_device = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
list->priv->map_native_path_to_object = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}
/**
@ -164,7 +164,7 @@ dkp_device_list_finalize (GObject *object)
list = DKP_DEVICE_LIST (object);
g_ptr_array_free (list->priv->array, TRUE);
g_hash_table_unref (list->priv->map_native_path_to_device);
g_hash_table_unref (list->priv->map_native_path_to_object);
G_OBJECT_CLASS (dkp_device_list_parent_class)->finalize (object);
}

View file

@ -53,13 +53,13 @@ typedef struct
GType dkp_device_list_get_type (void);
DkpDeviceList *dkp_device_list_new (void);
DkpDevice *dkp_device_list_lookup (DkpDeviceList *list,
DevkitDevice *d);
GObject *dkp_device_list_lookup (DkpDeviceList *list,
DevkitDevice *device);
gboolean dkp_device_list_insert (DkpDeviceList *list,
DevkitDevice *d,
DkpDevice *device);
DevkitDevice *device,
GObject *object);
gboolean dkp_device_list_remove (DkpDeviceList *list,
DkpDevice *device);
GObject *object);
const GPtrArray *dkp_device_list_get_array (DkpDeviceList *list);
G_END_DECLS