Blame gcr/gcr-union-collection.c

Packit Service f02b19
/*
Packit Service f02b19
 * gnome-keyring
Packit Service f02b19
 *
Packit Service f02b19
 * Copyright (C) 2011 Collabora Ltd.
Packit Service f02b19
 *
Packit Service f02b19
 * This program is free software; you can redistribute it and/or modify
Packit Service f02b19
 * it under the terms of the GNU Lesser General Public License as
Packit Service f02b19
 * published by the Free Software Foundation; either version 2.1 of
Packit Service f02b19
 * the License, or (at your option) any later version.
Packit Service f02b19
 *
Packit Service f02b19
 * This program is distributed in the hope that it will be useful, but
Packit Service f02b19
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service f02b19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service f02b19
 * Lesser General Public License for more details.
Packit Service f02b19
 *
Packit Service f02b19
 * You should have received a copy of the GNU Lesser General Public
Packit Service f02b19
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit Service f02b19
 *
Packit Service f02b19
 * Author: Stef Walter <stefw@collabora.co.uk>
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
#include "config.h"
Packit Service f02b19
Packit Service f02b19
#include "gcr-collection.h"
Packit Service f02b19
#include "gcr-internal.h"
Packit Service f02b19
#include "gcr-union-collection.h"
Packit Service f02b19
Packit Service f02b19
#include <string.h>
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * SECTION:gcr-union-collection
Packit Service f02b19
 * @title: GcrUnionCollection
Packit Service f02b19
 * @short_description: A GcrCollection which combines other collections
Packit Service f02b19
 *
Packit Service f02b19
 * An implementation of #GcrCollection, which combines the objects in
Packit Service f02b19
 * other #GcrCollections. Use gcr_union_collection_add() to add and
Packit Service f02b19
 * gcr_union_collection_remove() to remove them.
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * GcrUnionCollection:
Packit Service f02b19
 *
Packit Service f02b19
 * A union implementation of #GcrCollection.
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * GcrUnionCollectionClass:
Packit Service f02b19
 * @parent_class: The parent class
Packit Service f02b19
 *
Packit Service f02b19
 * The class for #GcrUnionCollection.
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
struct _GcrUnionCollectionPrivate {
Packit Service f02b19
	GHashTable *items;
Packit Service f02b19
	GHashTable *collections;
Packit Service f02b19
};
Packit Service f02b19
Packit Service f02b19
static void      gcr_collection_iface       (GcrCollectionIface *iface);
Packit Service f02b19
Packit Service f02b19
G_DEFINE_TYPE_WITH_CODE (GcrUnionCollection, gcr_union_collection, G_TYPE_OBJECT,
Packit Service f02b19
                         G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, gcr_collection_iface));
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
on_collection_added (GcrCollection *collection,
Packit Service f02b19
                     GObject *object,
Packit Service f02b19
                     gpointer user_data)
Packit Service f02b19
{
Packit Service f02b19
	GcrUnionCollection *self = GCR_UNION_COLLECTION (user_data);
Packit Service f02b19
	gint *count;
Packit Service f02b19
Packit Service f02b19
	g_object_ref (object);
Packit Service f02b19
Packit Service f02b19
	count = g_hash_table_lookup (self->pv->items, object);
Packit Service f02b19
	if (count == NULL) {
Packit Service f02b19
		count = g_new0 (gint, 1);
Packit Service f02b19
		*count = 1;
Packit Service f02b19
		g_hash_table_insert (self->pv->items, object, count);
Packit Service f02b19
		gcr_collection_emit_added (GCR_COLLECTION (self), object);
Packit Service f02b19
	} else {
Packit Service f02b19
		g_assert (*count > 0);
Packit Service f02b19
		(*count)++;
Packit Service f02b19
	}
Packit Service f02b19
Packit Service f02b19
	g_object_unref (object);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
on_collection_removed (GcrCollection *collection,
Packit Service f02b19
                       GObject *object,
Packit Service f02b19
                       gpointer user_data)
Packit Service f02b19
{
Packit Service f02b19
	GcrUnionCollection *self = GCR_UNION_COLLECTION (user_data);
Packit Service f02b19
	gint *count;
Packit Service f02b19
Packit Service f02b19
	g_object_ref (object);
Packit Service f02b19
Packit Service f02b19
	count = g_hash_table_lookup (self->pv->items, object);
Packit Service f02b19
	if (count != NULL) {
Packit Service f02b19
		g_assert (*count > 0);
Packit Service f02b19
		(*count)--;
Packit Service f02b19
Packit Service f02b19
		if (*count == 0) {
Packit Service f02b19
			g_hash_table_remove (self->pv->items, object);
Packit Service f02b19
			gcr_collection_emit_removed (GCR_COLLECTION (self), object);
Packit Service f02b19
		}
Packit Service f02b19
	} else {
Packit Service f02b19
		g_warning ("Object of type %s that exists in an underlying "
Packit Service f02b19
		           "collection of a GcrUnionCollection appeared without "
Packit Service f02b19
		           "emitting 'added' signal.", G_OBJECT_TYPE_NAME (object));
Packit Service f02b19
	}
Packit Service f02b19
Packit Service f02b19
	g_object_unref (object);
Packit Service f02b19
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
connect_to_collection (GcrUnionCollection *self,
Packit Service f02b19
                       GcrCollection *collection)
Packit Service f02b19
{
Packit Service f02b19
	g_signal_connect (collection, "added", G_CALLBACK (on_collection_added), self);
Packit Service f02b19
	g_signal_connect (collection, "removed", G_CALLBACK (on_collection_removed), self);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
disconnect_from_collection (GcrUnionCollection *self,
Packit Service f02b19
                            GcrCollection *collection)
Packit Service f02b19
{
Packit Service f02b19
	g_signal_handlers_disconnect_by_func (collection, on_collection_added, self);
Packit Service f02b19
	g_signal_handlers_disconnect_by_func (collection, on_collection_removed, self);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_union_collection_init (GcrUnionCollection *self)
Packit Service f02b19
{
Packit Service f02b19
	self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_UNION_COLLECTION, GcrUnionCollectionPrivate);
Packit Service f02b19
	self->pv->items = g_hash_table_new_full (g_direct_hash, g_direct_equal,
Packit Service f02b19
	                                         NULL, g_free);
Packit Service f02b19
	self->pv->collections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
Packit Service f02b19
	                                               g_object_unref, NULL);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_union_collection_dispose (GObject *obj)
Packit Service f02b19
{
Packit Service f02b19
	GcrUnionCollection *self = GCR_UNION_COLLECTION (obj);
Packit Service f02b19
	GHashTableIter iter;
Packit Service f02b19
	GcrCollection *collection;
Packit Service f02b19
Packit Service f02b19
	g_hash_table_iter_init (&iter, self->pv->collections);
Packit Service f02b19
	while (g_hash_table_iter_next (&iter, (gpointer *)&collection, NULL))
Packit Service f02b19
		disconnect_from_collection (self, collection);
Packit Service f02b19
	g_hash_table_remove_all (self->pv->collections);
Packit Service f02b19
	g_hash_table_remove_all (self->pv->items);
Packit Service f02b19
Packit Service f02b19
	G_OBJECT_CLASS (gcr_union_collection_parent_class)->dispose (obj);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_union_collection_finalize (GObject *obj)
Packit Service f02b19
{
Packit Service f02b19
	GcrUnionCollection *self = GCR_UNION_COLLECTION (obj);
Packit Service f02b19
Packit Service f02b19
	g_assert (g_hash_table_size (self->pv->items) == 0);
Packit Service f02b19
	g_hash_table_destroy (self->pv->items);
Packit Service f02b19
Packit Service f02b19
	g_assert (g_hash_table_size (self->pv->collections) == 0);
Packit Service f02b19
	g_hash_table_destroy (self->pv->collections);
Packit Service f02b19
Packit Service f02b19
	G_OBJECT_CLASS (gcr_union_collection_parent_class)->finalize (obj);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_union_collection_class_init (GcrUnionCollectionClass *klass)
Packit Service f02b19
{
Packit Service f02b19
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit Service f02b19
	gobject_class->dispose = gcr_union_collection_dispose;
Packit Service f02b19
	gobject_class->finalize = gcr_union_collection_finalize;
Packit Service f02b19
	g_type_class_add_private (gobject_class, sizeof (GcrUnionCollectionPrivate));
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static guint
Packit Service f02b19
gcr_union_collection_real_get_length (GcrCollection *coll)
Packit Service f02b19
{
Packit Service f02b19
	GcrUnionCollection *self = GCR_UNION_COLLECTION (coll);
Packit Service f02b19
	return g_hash_table_size (self->pv->items);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static GList*
Packit Service f02b19
gcr_union_collection_real_get_objects (GcrCollection *coll)
Packit Service f02b19
{
Packit Service f02b19
	GcrUnionCollection *self = GCR_UNION_COLLECTION (coll);
Packit Service f02b19
	return g_hash_table_get_keys (self->pv->items);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static gboolean
Packit Service f02b19
gcr_union_collection_real_contains (GcrCollection *collection,
Packit Service f02b19
                                    GObject *object)
Packit Service f02b19
{
Packit Service f02b19
	GcrUnionCollection *self = GCR_UNION_COLLECTION (collection);
Packit Service f02b19
	return g_hash_table_lookup (self->pv->items, object) ? TRUE : FALSE;
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
static void
Packit Service f02b19
gcr_collection_iface (GcrCollectionIface *iface)
Packit Service f02b19
{
Packit Service f02b19
	iface->get_length = gcr_union_collection_real_get_length;
Packit Service f02b19
	iface->get_objects = gcr_union_collection_real_get_objects;
Packit Service f02b19
	iface->contains = gcr_union_collection_real_contains;
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/* -----------------------------------------------------------------------------
Packit Service f02b19
 * PUBLIC
Packit Service f02b19
 */
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_union_collection_new:
Packit Service f02b19
 *
Packit Service f02b19
 * Create a new #GcrUnionCollection.
Packit Service f02b19
 *
Packit Service f02b19
 * Returns: (transfer full) (type Gcr.UnionCollection): a newly allocated
Packit Service f02b19
 *          collection, which should be freed with g_object_unref()
Packit Service f02b19
 */
Packit Service f02b19
GcrCollection *
Packit Service f02b19
gcr_union_collection_new (void)
Packit Service f02b19
{
Packit Service f02b19
	return g_object_new (GCR_TYPE_UNION_COLLECTION, NULL);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_union_collection_add:
Packit Service f02b19
 * @self: The union collection
Packit Service f02b19
 * @collection: The collection whose objects to add
Packit Service f02b19
 *
Packit Service f02b19
 * Add objects from this collection to the union
Packit Service f02b19
 */
Packit Service f02b19
void
Packit Service f02b19
gcr_union_collection_add (GcrUnionCollection *self,
Packit Service f02b19
                          GcrCollection *collection)
Packit Service f02b19
{
Packit Service f02b19
	g_return_if_fail (GCR_IS_UNION_COLLECTION (self));
Packit Service f02b19
	g_return_if_fail (GCR_IS_COLLECTION (collection));
Packit Service f02b19
	gcr_union_collection_take (self, g_object_ref (collection));
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_union_collection_take:
Packit Service f02b19
 * @self: The union collection
Packit Service f02b19
 * @collection: The collection whose objects to add
Packit Service f02b19
 *
Packit Service f02b19
 * Add objects from this collection to the union. Do not add an additional
Packit Service f02b19
 * reference to the collection.
Packit Service f02b19
 */
Packit Service f02b19
void
Packit Service f02b19
gcr_union_collection_take (GcrUnionCollection *self,
Packit Service f02b19
                           GcrCollection *collection)
Packit Service f02b19
{
Packit Service f02b19
	GList *objects, *l;
Packit Service f02b19
Packit Service f02b19
	g_return_if_fail (GCR_IS_UNION_COLLECTION (self));
Packit Service f02b19
	g_return_if_fail (GCR_IS_COLLECTION (collection));
Packit Service f02b19
	g_return_if_fail (!g_hash_table_lookup (self->pv->collections, collection));
Packit Service f02b19
Packit Service f02b19
	g_object_ref (collection);
Packit Service f02b19
Packit Service f02b19
	g_hash_table_insert (self->pv->collections, collection, collection);
Packit Service f02b19
	connect_to_collection (self, collection);
Packit Service f02b19
Packit Service f02b19
	objects = gcr_collection_get_objects (collection);
Packit Service f02b19
	for (l = objects; l != NULL; l = g_list_next (l))
Packit Service f02b19
		on_collection_added (collection, l->data, self);
Packit Service f02b19
	g_list_free (objects);
Packit Service f02b19
Packit Service f02b19
	g_object_unref (collection);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_union_collection_remove:
Packit Service f02b19
 * @self: The collection
Packit Service f02b19
 * @collection: The collection whose objects to remove
Packit Service f02b19
 *
Packit Service f02b19
 * Remove an object from the collection.
Packit Service f02b19
 */
Packit Service f02b19
void
Packit Service f02b19
gcr_union_collection_remove (GcrUnionCollection *self,
Packit Service f02b19
                             GcrCollection *collection)
Packit Service f02b19
{
Packit Service f02b19
	GList *objects, *l;
Packit Service f02b19
Packit Service f02b19
	g_return_if_fail (GCR_IS_UNION_COLLECTION (self));
Packit Service f02b19
	g_return_if_fail (GCR_IS_COLLECTION (collection));
Packit Service f02b19
	g_return_if_fail (g_hash_table_lookup (self->pv->collections, collection));
Packit Service f02b19
Packit Service f02b19
	g_object_ref (collection);
Packit Service f02b19
Packit Service f02b19
	g_hash_table_remove (self->pv->collections, collection);
Packit Service f02b19
	disconnect_from_collection (self, collection);
Packit Service f02b19
Packit Service f02b19
	objects = gcr_collection_get_objects (collection);
Packit Service f02b19
	for (l = objects; l != NULL; l = g_list_next (l))
Packit Service f02b19
		on_collection_removed (collection, l->data, self);
Packit Service f02b19
	g_list_free (objects);
Packit Service f02b19
Packit Service f02b19
	g_object_unref (collection);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_union_collection_have:
Packit Service f02b19
 * @self: the union collection
Packit Service f02b19
 * @collection: the collection to check
Packit Service f02b19
 *
Packit Service f02b19
 * Check whether the collection is present in the union.
Packit Service f02b19
 *
Packit Service f02b19
 * Returns: whether present or not
Packit Service f02b19
 */
Packit Service f02b19
gboolean
Packit Service f02b19
gcr_union_collection_have (GcrUnionCollection *self,
Packit Service f02b19
                           GcrCollection *collection)
Packit Service f02b19
{
Packit Service f02b19
	g_return_val_if_fail (GCR_IS_UNION_COLLECTION (self), FALSE);
Packit Service f02b19
	g_return_val_if_fail (GCR_IS_COLLECTION (collection), FALSE);
Packit Service f02b19
	return g_hash_table_lookup (self->pv->collections, collection) != NULL;
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_union_collection_size:
Packit Service f02b19
 * @self: the union collection
Packit Service f02b19
 *
Packit Service f02b19
 * Return the number of collections in this union. This does not reflect
Packit Service f02b19
 * the number of objects in the combined collection.
Packit Service f02b19
 *
Packit Service f02b19
 * Returns: number of collections inlcuded
Packit Service f02b19
 */
Packit Service f02b19
guint
Packit Service f02b19
gcr_union_collection_size (GcrUnionCollection *self)
Packit Service f02b19
{
Packit Service f02b19
	g_return_val_if_fail (GCR_IS_UNION_COLLECTION (self), FALSE);
Packit Service f02b19
	return g_hash_table_size (self->pv->collections);
Packit Service f02b19
}
Packit Service f02b19
Packit Service f02b19
/**
Packit Service f02b19
 * gcr_union_collection_elements:
Packit Service f02b19
 * @self: the union collection
Packit Service f02b19
 *
Packit Service f02b19
 * Get the collections that have been added to this union.
Packit Service f02b19
 *
Packit Service f02b19
 * Returns: (element-type Gcr.Collection) (transfer container): collections
Packit Service f02b19
 *          added to the union
Packit Service f02b19
 */
Packit Service f02b19
GList *
Packit Service f02b19
gcr_union_collection_elements (GcrUnionCollection *self)
Packit Service f02b19
{
Packit Service f02b19
	g_return_val_if_fail (GCR_IS_UNION_COLLECTION (self), NULL);
Packit Service f02b19
	return g_hash_table_get_values (self->pv->collections);
Packit Service f02b19
}