summaryrefslogtreecommitdiff
path: root/gcr/gcr-collection.c
blob: 54c4667404993b3c7dd07e974e10351d808c0319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * gnome-keyring
 *
 * Copyright (C) 2010 Stefan Walter
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gcr-collection.h"

/**
 * SECTION:gcr-collection
 * @title: GcrCollection
 * @short_description: A collection of objects.
 *
 * A #GcrCollection is used to group a set of objects. This is an abstract
 * interface which can be used to determine which objects show up in a selector
 * or other user interface element.
 *
 * Use gcr_simple_collection_new() to create a concrete implementation of this
 * interface which you can add objects to.
 */

/**
 * GcrCollection:
 *
 * A #GcrCollection is used to group a set of objects.
 */

enum {
	ADDED,
	REMOVED,
	LAST_SIGNAL,
};

static guint signals[LAST_SIGNAL] = { 0 };


typedef GcrCollectionIface GcrCollectionInterface;

G_DEFINE_INTERFACE (GcrCollection, gcr_collection, G_TYPE_OBJECT);

static void
gcr_collection_default_init (GcrCollectionIface *iface)
{
	static volatile gsize initialized = 0;

	if (g_once_init_enter (&initialized)) {

		/**
		 * GcrCollection::added:
		 * @self: the collection
		 * @object: (type GObject.Object): object that was added
		 *
		 * This signal is emitted when an object is added to the collection.
		 */
		signals[ADDED] = g_signal_new ("added", GCR_TYPE_COLLECTION,
		                               G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GcrCollectionIface, added),
		                               NULL, NULL, NULL,
		                               G_TYPE_NONE, 1, G_TYPE_OBJECT);

		/**
		 * GcrCollection::removed:
		 * @self: the collection
		 * @object: (type GObject.Object): object that was removed
		 *
		 * This signal is emitted when an object is removed from the collection.
		 */
		signals[REMOVED] = g_signal_new ("removed", GCR_TYPE_COLLECTION,
		                                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GcrCollectionIface, removed),
		                                 NULL, NULL, NULL,
		                                 G_TYPE_NONE, 1, G_TYPE_OBJECT);

		g_once_init_leave (&initialized, 1);
	}
}

/* -----------------------------------------------------------------------------
 * PUBLIC
 */


/**
 * gcr_collection_get_length:
 * @self: The collection
 *
 * Get the number of objects in this collection.
 *
 * Returns: The number of objects.
 */
guint
gcr_collection_get_length (GcrCollection *self)
{
	g_return_val_if_fail (GCR_IS_COLLECTION (self), 0);
	g_return_val_if_fail (GCR_COLLECTION_GET_INTERFACE (self)->get_length, 0);
	return GCR_COLLECTION_GET_INTERFACE (self)->get_length (self);
}

/**
 * gcr_collection_get_objects:
 * @self: The collection
 *
 * Get a list of the objects in this collection.
 *
 * Returns: (transfer container) (element-type GObject.Object): a list of the objects
 *          in this collection, which should be freed with g_list_free()
 */
GList*
gcr_collection_get_objects (GcrCollection *self)
{
	g_return_val_if_fail (GCR_IS_COLLECTION (self), 0);
	g_return_val_if_fail (GCR_COLLECTION_GET_INTERFACE (self)->get_objects, 0);
	return GCR_COLLECTION_GET_INTERFACE (self)->get_objects (self);
}

/**
 * gcr_collection_contains:
 * @self: the collection
 * @object: object to check
 *
 * Check whether the collection contains an object or not.
 *
 * Returns: whether the collection contains this object
 */
gboolean
gcr_collection_contains (GcrCollection *self,
                         GObject *object)
{
	g_return_val_if_fail (GCR_IS_COLLECTION (self), FALSE);
	g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
	g_return_val_if_fail (GCR_COLLECTION_GET_INTERFACE (self)->contains, FALSE);
	return GCR_COLLECTION_GET_INTERFACE (self)->contains (self, object);
}

/**
 * gcr_collection_emit_added:
 * @self: The collection
 * @object: The object that was added
 *
 * Emit the #GcrCollection::added signal for the given object. This function
 * is used by implementors of this interface.
 */
void
gcr_collection_emit_added (GcrCollection *self, GObject *object)
{
	g_return_if_fail (GCR_IS_COLLECTION (self));
	g_signal_emit (self, signals[ADDED], 0, object);
}

/**
 * gcr_collection_emit_removed:
 * @self: The collection
 * @object: The object that was removed
 *
 * Emit the #GcrCollection::removed signal for the given object. This function
 * is used by implementors of this interface.
 */
void
gcr_collection_emit_removed (GcrCollection *self, GObject *object)
{
	g_return_if_fail (GCR_IS_COLLECTION (self));
	g_signal_emit (self, signals[REMOVED], 0, object);
}