4 * Copyright (C) 2010 Stefan Walter
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #include "gcr-collection.h"
25 #include "gcr-single-collection.h"
30 * GcrSingleCollection:
32 * A single implementation of #GcrCollection.
35 struct _GcrSingleCollection {
41 * GcrSingleCollectionClass:
42 * @parent_class: The parent class
44 * The class for #GcrSingleCollection.
47 struct _GcrSingleCollectionClass {
48 GObjectClass parent_class;
51 static void _gcr_single_collection_iface (GcrCollectionIface *iface);
52 G_DEFINE_TYPE_WITH_CODE (GcrSingleCollection, _gcr_single_collection, G_TYPE_OBJECT,
53 G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, _gcr_single_collection_iface));
55 /* -----------------------------------------------------------------------------
60 _gcr_single_collection_init (GcrSingleCollection *self)
66 _gcr_single_collection_dispose (GObject *obj)
68 GcrSingleCollection *self = GCR_SINGLE_COLLECTION (obj);
70 _gcr_single_collection_set_object (self, NULL);
72 G_OBJECT_CLASS (_gcr_single_collection_parent_class)->dispose (obj);
76 _gcr_single_collection_class_init (GcrSingleCollectionClass *klass)
78 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
79 gobject_class->dispose = _gcr_single_collection_dispose;
83 _gcr_single_collection_real_get_length (GcrCollection *coll)
85 GcrSingleCollection *self = GCR_SINGLE_COLLECTION (coll);
86 return self->object == NULL ? 0 : 1;
90 _gcr_single_collection_real_get_objects (GcrCollection *coll)
92 GcrSingleCollection *self = GCR_SINGLE_COLLECTION (coll);
93 return self->object == NULL ? NULL : g_list_append (NULL, self->object);
97 _gcr_single_collection_real_contains (GcrCollection *collection,
100 GcrSingleCollection *self = GCR_SINGLE_COLLECTION (collection);
101 return self->object == object;
105 _gcr_single_collection_iface (GcrCollectionIface *iface)
107 iface->get_length = _gcr_single_collection_real_get_length;
108 iface->get_objects = _gcr_single_collection_real_get_objects;
109 iface->contains = _gcr_single_collection_real_contains;
112 /* -----------------------------------------------------------------------------
117 _gcr_single_collection_new (GObject *object)
119 GcrSingleCollection *self;
121 self = g_object_new (GCR_TYPE_SINGLE_COLLECTION, NULL);
122 _gcr_single_collection_set_object (self, object);
124 return GCR_COLLECTION (self);
128 _gcr_single_collection_get_object (GcrSingleCollection *self)
130 g_return_val_if_fail (GCR_IS_SINGLE_COLLECTION (self), NULL);
135 _gcr_single_collection_set_object (GcrSingleCollection *self,
140 g_return_if_fail (GCR_IS_SINGLE_COLLECTION (self));
141 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
143 if (object == self->object)
149 gcr_collection_emit_removed (GCR_COLLECTION (self), obj);
150 g_object_unref (obj);
154 self->object = g_object_ref (object);
155 gcr_collection_emit_added (GCR_COLLECTION (self), self->object);