root/trunk/libtinymailui-gtk/tny-gtk-attach-list-model-iterator.c

Revision 3666 (checked in by jdapena, 7 months ago)

* Use GOnce registering all types in tinymail to make

registering thread-safe.

Line 
1 /* libtinymail - The Tiny Mail base library
2  * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with self library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <config.h>
21
22 #include <tny-gtk-attach-list-model.h>
23
24 static GObjectClass *parent_class = NULL;
25
26 #include "tny-gtk-attach-list-model-iterator-priv.h"
27 #include "tny-gtk-attach-list-model-priv.h"
28
29 GType _tny_gtk_attach_list_model_iterator_get_type (void);
30
31
32 void 
33 _tny_gtk_attach_list_model_iterator_set_model (TnyGtkAttachListModelIterator *self, TnyGtkAttachListModel *model)
34 {
35         if (self->model)
36                 g_object_unref (self->model);
37
38         self->model = g_object_ref (model);
39         self->current = model->first;
40
41         return;
42 }
43
44
45
46 TnyIterator*
47 _tny_gtk_attach_list_model_iterator_new (TnyGtkAttachListModel *model)
48 {
49         TnyGtkAttachListModelIterator *self = g_object_new (TNY_TYPE_GTK_ATTACH_LIST_MODEL_ITERATOR, NULL);
50
51         _tny_gtk_attach_list_model_iterator_set_model (self, model);
52
53         return TNY_ITERATOR (self);
54 }
55
56 static void
57 tny_gtk_attach_list_model_iterator_instance_init (GTypeInstance *instance, gpointer g_class)
58 {
59         TnyGtkAttachListModelIterator *self = (TnyGtkAttachListModelIterator *)instance;
60
61         self->model = NULL;
62         self->current = NULL;
63
64         return;
65 }
66
67 static void
68 tny_gtk_attach_list_model_iterator_finalize (GObject *object)
69 {
70         TnyGtkAttachListModelIterator *self = (TnyGtkAttachListModelIterator *) object;
71
72         if (self->model)
73                 g_object_unref (self->model);
74
75         (*parent_class->finalize) (object);
76
77         return;
78 }
79
80
81 static void
82 tny_gtk_attach_list_model_iterator_next (TnyIterator *self)
83 {
84         TnyGtkAttachListModelIterator *me = (TnyGtkAttachListModelIterator*) self;
85
86         if (G_UNLIKELY (!me || !me->current || !me->model))
87                 return;
88
89         /* Move the iterator to the next node */
90
91         g_mutex_lock (me->model->iterator_lock);
92         me->current = g_list_next (me->current);
93         g_mutex_unlock (me->model->iterator_lock);
94
95         return;
96 }
97
98 static void
99 tny_gtk_attach_list_model_iterator_prev (TnyIterator *self)
100 {
101         TnyGtkAttachListModelIterator *me = (TnyGtkAttachListModelIterator*) self;
102
103         if (G_UNLIKELY (!me || !me->current || !me->model))
104                 return;
105
106         /* Move the iterator to the previous node */
107
108         g_mutex_lock (me->model->iterator_lock);
109         me->current = g_list_previous (me->current);
110         g_mutex_unlock (me->model->iterator_lock);
111
112         return;
113 }
114
115
116 static gboolean
117 tny_gtk_attach_list_model_iterator_is_done (TnyIterator *self)
118 {
119         TnyGtkAttachListModelIterator *me = (TnyGtkAttachListModelIterator*) self;
120        
121         if (G_UNLIKELY (!me || !me->model))
122                 return TRUE;
123
124         return me->current == NULL;
125 }
126
127
128
129 static void
130 tny_gtk_attach_list_model_iterator_first (TnyIterator *self)
131 {
132         TnyGtkAttachListModelIterator *me = (TnyGtkAttachListModelIterator*) self;
133
134         if (G_UNLIKELY (!me || !me->current || !me->model))
135                 return;
136
137         /* Move the iterator to the first node. We know that model always
138            keeps a reference to the first node, there's nothing wrong with
139            using that one. */
140
141         g_mutex_lock (me->model->iterator_lock);
142         me->current = me->model->first;
143         g_mutex_unlock (me->model->iterator_lock);
144
145         return;
146 }
147
148
149 static void
150 tny_gtk_attach_list_model_iterator_nth (TnyIterator *self, guint nth)
151 {
152         TnyGtkAttachListModelIterator *me = (TnyGtkAttachListModelIterator*) self;
153
154         if (G_UNLIKELY (!me || !me->current || !me->model))
155                 return;
156
157         /* Move the iterator to the nth node. We'll count from zero,
158            so we start with the first node of which we know the model
159            stored a reference. */
160
161         g_mutex_lock (me->model->iterator_lock);
162         me->current = g_list_nth (me->model->first, nth);
163         g_mutex_unlock (me->model->iterator_lock);
164
165         return;
166 }
167
168
169 static GObject*
170 tny_gtk_attach_list_model_iterator_get_current (TnyIterator *self)
171 {
172         TnyGtkAttachListModelIterator *me = (TnyGtkAttachListModelIterator*) self;
173         gpointer retval;
174
175         if (G_UNLIKELY (!me || !me->model))
176                 return NULL;
177
178         /* Give the data of the current node */
179
180         g_mutex_lock (me->model->iterator_lock);
181         retval = (G_UNLIKELY (me->current)) ? me->current->data : NULL;
182         g_mutex_unlock (me->model->iterator_lock);
183
184         if (retval)
185                 g_object_ref (G_OBJECT(retval));
186
187         return (GObject*)retval;
188 }
189
190
191 static TnyList*
192 tny_gtk_attach_list_model_iterator_get_list (TnyIterator *self)
193 {
194         TnyGtkAttachListModelIterator *me = (TnyGtkAttachListModelIterator*) self;
195
196         /* Return the list */
197
198         if (G_UNLIKELY (!me || !me->model))
199                 return NULL;
200
201         g_object_ref (G_OBJECT (me->model));
202
203         return TNY_LIST (me->model);
204 }
205
206 static void
207 tny_iterator_init (TnyIteratorIface *klass)
208 {
209
210         klass->next= tny_gtk_attach_list_model_iterator_next;
211         klass->prev= tny_gtk_attach_list_model_iterator_prev;
212         klass->first= tny_gtk_attach_list_model_iterator_first;
213         klass->nth= tny_gtk_attach_list_model_iterator_nth;
214         klass->get_current= tny_gtk_attach_list_model_iterator_get_current;
215         klass->get_list= tny_gtk_attach_list_model_iterator_get_list;
216         klass->is_done  = tny_gtk_attach_list_model_iterator_is_done;
217        
218         return;
219 }
220
221 static void
222 tny_gtk_attach_list_model_iterator_class_init (TnyGtkAttachListModelIteratorClass *klass)
223 {
224         GObjectClass *object_class;
225
226         parent_class = g_type_class_peek_parent (klass);
227         object_class = (GObjectClass*) klass;
228
229         object_class->finalize = tny_gtk_attach_list_model_iterator_finalize;
230
231         return;
232 }
233
234 static gpointer
235 _tny_gtk_attach_list_model_iterator_register_type (gpointer notused)
236 {
237         GType type = 0;
238
239         static const GTypeInfo info =
240                 {
241                   sizeof (TnyGtkAttachListModelIteratorClass),
242                   NULL,   /* base_init */
243                   NULL,   /* base_finalize */
244                   (GClassInitFunc) tny_gtk_attach_list_model_iterator_class_init,   /* class_init */
245                   NULL,   /* class_finalize */
246                   NULL,   /* class_data */
247                   sizeof (TnyGtkAttachListModelIterator),
248                   0,      /* n_preallocs */
249                   tny_gtk_attach_list_model_iterator_instance_init    /* instance_init */
250                 };
251
252         static const GInterfaceInfo tny_iterator_info =
253                 {
254                   (GInterfaceInitFunc) tny_iterator_init, /* interface_init */
255                   NULL,         /* interface_finalize */
256                   NULL          /* interface_data */
257                 };
258
259         type = g_type_register_static (G_TYPE_OBJECT,
260                                        "TnyGtkAttachListModelIterator",
261                                        &info, 0);
262
263         g_type_add_interface_static (type, TNY_TYPE_ITERATOR,
264                                      &tny_iterator_info);
265
266         return GUINT_TO_POINTER (type);
267 }
268
269 GType
270 _tny_gtk_attach_list_model_iterator_get_type (void)
271 {
272         static GOnce once = G_ONCE_INIT;
273         g_once (&once, _tny_gtk_attach_list_model_iterator_register_type, NULL);
274         return GPOINTER_TO_UINT (once.retval);
275 }
Note: See TracBrowser for help on using the browser.