gcr: Add GcrSingleCollection internal class
[platform/upstream/gcr.git] / gcr / gcr-single-collection.c
1 /*
2  * gnome-keyring
3  *
4  * Copyright (C) 2010 Stefan Walter
5  *
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.
10  *
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.
15  *
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
19  * 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include "gcr-collection.h"
25 #include "gcr-single-collection.h"
26
27 #include <string.h>
28
29 /**
30  * GcrSingleCollection:
31  *
32  * A single implementation of #GcrCollection.
33  */
34
35 struct _GcrSingleCollection {
36         GObject parent;
37         GObject *object;
38 };
39
40 /**
41  * GcrSingleCollectionClass:
42  * @parent_class: The parent class
43  *
44  * The class for #GcrSingleCollection.
45  */
46
47 struct _GcrSingleCollectionClass {
48         GObjectClass parent_class;
49 };
50
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));
54
55 /* -----------------------------------------------------------------------------
56  * OBJECT
57  */
58
59 static void
60 _gcr_single_collection_init (GcrSingleCollection *self)
61 {
62
63 }
64
65 static void
66 _gcr_single_collection_dispose (GObject *obj)
67 {
68         GcrSingleCollection *self = GCR_SINGLE_COLLECTION (obj);
69
70         _gcr_single_collection_set_object (self, NULL);
71
72         G_OBJECT_CLASS (_gcr_single_collection_parent_class)->dispose (obj);
73 }
74
75 static void
76 _gcr_single_collection_class_init (GcrSingleCollectionClass *klass)
77 {
78         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
79         gobject_class->dispose = _gcr_single_collection_dispose;
80 }
81
82 static guint
83 _gcr_single_collection_real_get_length (GcrCollection *coll)
84 {
85         GcrSingleCollection *self = GCR_SINGLE_COLLECTION (coll);
86         return self->object == NULL ? 0 : 1;
87 }
88
89 static GList*
90 _gcr_single_collection_real_get_objects (GcrCollection *coll)
91 {
92         GcrSingleCollection *self = GCR_SINGLE_COLLECTION (coll);
93         return self->object == NULL ? NULL : g_list_append (NULL, self->object);
94 }
95
96 static gboolean
97 _gcr_single_collection_real_contains (GcrCollection *collection,
98                                       GObject *object)
99 {
100         GcrSingleCollection *self = GCR_SINGLE_COLLECTION (collection);
101         return self->object == object;
102 }
103
104 static void
105 _gcr_single_collection_iface (GcrCollectionIface *iface)
106 {
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;
110 }
111
112 /* -----------------------------------------------------------------------------
113  * PUBLIC
114  */
115
116 GcrCollection *
117 _gcr_single_collection_new (GObject *object)
118 {
119         GcrSingleCollection *self;
120
121         self = g_object_new (GCR_TYPE_SINGLE_COLLECTION, NULL);
122         _gcr_single_collection_set_object (self, object);
123
124         return GCR_COLLECTION (self);
125 }
126
127 GObject *
128 _gcr_single_collection_get_object (GcrSingleCollection *self)
129 {
130         g_return_val_if_fail (GCR_IS_SINGLE_COLLECTION (self), NULL);
131         return self->object;
132 }
133
134 void
135 _gcr_single_collection_set_object (GcrSingleCollection *self,
136                                    GObject *object)
137 {
138         GObject *obj;
139
140         g_return_if_fail (GCR_IS_SINGLE_COLLECTION (self));
141         g_return_if_fail (object == NULL || G_IS_OBJECT (object));
142
143         if (object == self->object)
144                 return;
145
146         if (self->object) {
147                 obj = self->object;
148                 self->object = NULL;
149                 gcr_collection_emit_removed (GCR_COLLECTION (self), obj);
150                 g_object_unref (obj);
151         }
152
153         if (object) {
154                 self->object = g_object_ref (object);
155                 gcr_collection_emit_added (GCR_COLLECTION (self), self->object);
156         }
157 }