summaryrefslogtreecommitdiff
path: root/gcr/gcr-single-collection.c
blob: 85a0ebbebe4c4d1d686fd79a664105cd987a5fa1 (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
/*
 * 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"
#include "gcr-single-collection.h"

#include <string.h>

/**
 * GcrSingleCollection:
 *
 * A single implementation of #GcrCollection.
 */

struct _GcrSingleCollection {
	GObject parent;
	GObject *object;
};

/**
 * GcrSingleCollectionClass:
 * @parent_class: The parent class
 *
 * The class for #GcrSingleCollection.
 */

struct _GcrSingleCollectionClass {
	GObjectClass parent_class;
};

static void _gcr_single_collection_iface (GcrCollectionIface *iface);
G_DEFINE_TYPE_WITH_CODE (GcrSingleCollection, _gcr_single_collection, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GCR_TYPE_COLLECTION, _gcr_single_collection_iface));

/* -----------------------------------------------------------------------------
 * OBJECT
 */

static void
_gcr_single_collection_init (GcrSingleCollection *self)
{

}

static void
_gcr_single_collection_dispose (GObject *obj)
{
	GcrSingleCollection *self = GCR_SINGLE_COLLECTION (obj);

	_gcr_single_collection_set_object (self, NULL);

	G_OBJECT_CLASS (_gcr_single_collection_parent_class)->dispose (obj);
}

static void
_gcr_single_collection_class_init (GcrSingleCollectionClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	gobject_class->dispose = _gcr_single_collection_dispose;
}

static guint
_gcr_single_collection_real_get_length (GcrCollection *coll)
{
	GcrSingleCollection *self = GCR_SINGLE_COLLECTION (coll);
	return self->object == NULL ? 0 : 1;
}

static GList*
_gcr_single_collection_real_get_objects (GcrCollection *coll)
{
	GcrSingleCollection *self = GCR_SINGLE_COLLECTION (coll);
	return self->object == NULL ? NULL : g_list_append (NULL, self->object);
}

static gboolean
_gcr_single_collection_real_contains (GcrCollection *collection,
                                      GObject *object)
{
	GcrSingleCollection *self = GCR_SINGLE_COLLECTION (collection);
	return self->object == object;
}

static void
_gcr_single_collection_iface (GcrCollectionIface *iface)
{
	iface->get_length = _gcr_single_collection_real_get_length;
	iface->get_objects = _gcr_single_collection_real_get_objects;
	iface->contains = _gcr_single_collection_real_contains;
}

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

GcrCollection *
_gcr_single_collection_new (GObject *object)
{
	GcrSingleCollection *self;

	self = g_object_new (GCR_TYPE_SINGLE_COLLECTION, NULL);
	_gcr_single_collection_set_object (self, object);

	return GCR_COLLECTION (self);
}

GObject *
_gcr_single_collection_get_object (GcrSingleCollection *self)
{
	g_return_val_if_fail (GCR_IS_SINGLE_COLLECTION (self), NULL);
	return self->object;
}

void
_gcr_single_collection_set_object (GcrSingleCollection *self,
                                   GObject *object)
{
	GObject *obj;

	g_return_if_fail (GCR_IS_SINGLE_COLLECTION (self));
	g_return_if_fail (object == NULL || G_IS_OBJECT (object));

	if (object == self->object)
		return;

	if (self->object) {
		obj = self->object;
		self->object = NULL;
		gcr_collection_emit_removed (GCR_COLLECTION (self), obj);
		g_object_unref (obj);
	}

	if (object) {
		self->object = g_object_ref (object);
		gcr_collection_emit_added (GCR_COLLECTION (self), self->object);
	}
}