root/trunk/libtinymailui-gtk/tny-gtk-header-list-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-header-list-model.h>
23
24 static GObjectClass *parent_class = NULL;
25
26 #include "tny-gtk-header-list-iterator-priv.h"
27
28 GType _tny_gtk_header_list_iterator_get_type (void);
29
30
31
32 static void
33 _tny_gtk_header_list_iterator_set_model (TnyGtkHeaderListIterator *self, TnyGtkHeaderListModel *model)
34 {
35         if (self->model)
36                 g_object_unref (self->model);
37
38         self->model = g_object_ref (model);
39
40         /* It's not a list_copy, so don't free this list when
41            destructing this iterator. Current is used as a ptr
42            to the 'current' GList node.
43
44            When the iterator starts, it points to 'start', or,
45            the first node in the list. */
46
47         self->current = 0;
48
49         return;
50 }
51
52
53
54 TnyIterator*
55 _tny_gtk_header_list_iterator_new (TnyGtkHeaderListModel *model)
56 {
57         TnyGtkHeaderListIterator *self = g_object_new (TNY_TYPE_GTK_HEADER_LIST_ITERATOR, NULL);
58
59         _tny_gtk_header_list_iterator_set_model (self, model);
60
61         return TNY_ITERATOR (self);
62 }
63
64 static void
65 tny_gtk_header_list_iterator_instance_init (GTypeInstance *instance, gpointer g_class)
66 {
67         TnyGtkHeaderListIterator *self = (TnyGtkHeaderListIterator *)instance;
68
69         self->model = NULL;
70         self->current = 0;
71
72         return;
73 }
74
75 static void
76 tny_gtk_header_list_iterator_finalize (GObject *object)
77 {
78         TnyGtkHeaderListIterator *self = (TnyGtkHeaderListIterator *) object;
79
80         if (self->model)
81                 g_object_unref (self->model);
82
83         (*parent_class->finalize) (object);
84
85         return;
86 }
87
88
89 void 
90 _tny_gtk_header_list_iterator_next_nl (TnyGtkHeaderListIterator *me)
91 {
92         me->current++;
93         return;
94 }
95
96 static void
97 tny_gtk_header_list_iterator_next (TnyIterator *self)
98 {
99         TnyGtkHeaderListIterator *me = (TnyGtkHeaderListIterator*) self;
100         TnyGtkHeaderListModelPriv *mpriv;
101
102         if (G_UNLIKELY (!me || !me->model))
103                 return;
104
105         mpriv = TNY_GTK_HEADER_LIST_MODEL_GET_PRIVATE (me->model);
106
107         /* Move the iterator to the next node */
108
109         g_static_rec_mutex_lock (mpriv->iterator_lock);
110         me->current++;
111         g_static_rec_mutex_unlock (mpriv->iterator_lock);
112
113         return;
114 }
115
116 void
117 _tny_gtk_header_list_iterator_prev_nl (TnyGtkHeaderListIterator *me)
118 {
119         me->current--;
120         return;
121 }
122
123 static void
124 tny_gtk_header_list_iterator_prev (TnyIterator *self)
125 {
126         TnyGtkHeaderListIterator *me = (TnyGtkHeaderListIterator*) self;
127         TnyGtkHeaderListModelPriv *mpriv;
128
129         if (G_UNLIKELY (!me || !me->model))
130                 return;
131
132         /* Move the iterator to the previous node */
133
134         mpriv = TNY_GTK_HEADER_LIST_MODEL_GET_PRIVATE (me->model);
135
136         g_static_rec_mutex_lock (mpriv->iterator_lock);
137         me->current--;
138         g_static_rec_mutex_unlock (mpriv->iterator_lock);
139
140         return;
141 }
142
143 gboolean
144 _tny_gtk_header_list_iterator_is_done_nl (TnyGtkHeaderListIterator *me)
145 {
146         TnyGtkHeaderListModelPriv *mpriv;
147
148         if (G_UNLIKELY (!me  || !me->model))
149                 return TRUE;
150
151         mpriv = TNY_GTK_HEADER_LIST_MODEL_GET_PRIVATE (me->model);
152
153         return (/*me->current < 0 || */me->current >= mpriv->items->len);
154 }
155
156 static gboolean
157 tny_gtk_header_list_iterator_is_done (TnyIterator *self)
158 {
159         TnyGtkHeaderListIterator *me = (TnyGtkHeaderListIterator*) self;
160         TnyGtkHeaderListModelPriv *mpriv;
161         gboolean retval = FALSE;
162
163         if (G_UNLIKELY (!me || !me->model))
164                 return FALSE;
165
166         mpriv = TNY_GTK_HEADER_LIST_MODEL_GET_PRIVATE (me->model);
167         g_static_rec_mutex_lock (mpriv->iterator_lock);
168         retval = (/* me->current < 0 || */me->current >= mpriv->items->len);
169         g_static_rec_mutex_unlock (mpriv->iterator_lock);
170
171         return retval;
172 }
173
174
175 void
176 _tny_gtk_header_list_iterator_first_nl (TnyGtkHeaderListIterator *me)
177 {
178         me->current = 0;
179
180         return;
181 }
182
183 static void
184 tny_gtk_header_list_iterator_first (TnyIterator *self)
185 {
186         TnyGtkHeaderListIterator *me = (TnyGtkHeaderListIterator*) self;
187         TnyGtkHeaderListModelPriv *mpriv;
188
189         if (G_UNLIKELY (!me || !me->current || !me->model))
190                 return;
191
192         mpriv = TNY_GTK_HEADER_LIST_MODEL_GET_PRIVATE (me->model);
193
194         g_static_rec_mutex_lock (mpriv->iterator_lock);
195         me->current = 0;
196         g_static_rec_mutex_unlock (mpriv->iterator_lock);
197
198         return;
199 }
200
201
202 void
203 _tny_gtk_header_list_iterator_nth_nl (TnyGtkHeaderListIterator *me, guint nth)
204 {
205         me->current = nth;
206         return;
207 }
208
209 static void
210 tny_gtk_header_list_iterator_nth (TnyIterator *self, guint nth)
211 {
212         TnyGtkHeaderListIterator *me = (TnyGtkHeaderListIterator*) self;
213         TnyGtkHeaderListModelPriv *mpriv;
214
215         if (G_UNLIKELY (!me || !me->current || !me->model))
216                 return;
217
218         mpriv = TNY_GTK_HEADER_LIST_MODEL_GET_PRIVATE (me->model);
219
220         g_static_rec_mutex_lock (mpriv->iterator_lock);
221         me->current = nth;
222         g_static_rec_mutex_unlock (mpriv->iterator_lock);
223
224         return;
225 }
226
227 /* exception: don't ref */
228 gpointer
229 _tny_gtk_header_list_iterator_get_current_nl (TnyGtkHeaderListIterator *me)
230 {
231         TnyGtkHeaderListModelPriv *mpriv = TNY_GTK_HEADER_LIST_MODEL_GET_PRIVATE (me->model);
232         return mpriv->items->pdata[me->current];
233 }
234
235 static GObject*
236 tny_gtk_header_list_iterator_get_current (TnyIterator *self)
237 {
238         TnyGtkHeaderListIterator *me = (TnyGtkHeaderListIterator*) self;
239         TnyGtkHeaderListModelPriv *mpriv;
240         gpointer retval;
241
242         if (G_UNLIKELY (!me || !me->model))
243                 return NULL;
244
245         /* Give the data of the current node */
246
247         mpriv = TNY_GTK_HEADER_LIST_MODEL_GET_PRIVATE (me->model);
248
249         g_static_rec_mutex_lock (mpriv->iterator_lock);
250         retval = mpriv->items->pdata[me->current];
251         if (retval)
252                 g_object_ref (retval);
253         g_static_rec_mutex_unlock (mpriv->iterator_lock);
254
255
256         return (GObject*) retval;
257 }
258
259
260 static TnyList*
261 tny_gtk_header_list_iterator_get_list (TnyIterator *self)
262 {
263         TnyGtkHeaderListIterator *me = (TnyGtkHeaderListIterator*) self;
264
265         /* Return the list */
266
267         if (G_UNLIKELY (!me || !me->model))
268                 return NULL;
269
270         g_object_ref (me->model);
271
272         return TNY_LIST (me->model);
273 }
274
275 static void
276 tny_iterator_init (TnyIteratorIface *klass)
277 {
278         klass->next= tny_gtk_header_list_iterator_next;
279         klass->prev= tny_gtk_header_list_iterator_prev;
280         klass->first= tny_gtk_header_list_iterator_first;
281         klass->nth= tny_gtk_header_list_iterator_nth;
282         klass->get_current= tny_gtk_header_list_iterator_get_current;
283         klass->get_list= tny_gtk_header_list_iterator_get_list;
284         klass->is_done = tny_gtk_header_list_iterator_is_done;
285
286         return;
287 }
288
289 static void
290 tny_gtk_header_list_iterator_class_init (TnyGtkHeaderListIteratorClass *klass)
291 {
292         GObjectClass *object_class;
293
294         parent_class = g_type_class_peek_parent (klass);
295         object_class = (GObjectClass*) klass;
296
297         object_class->finalize = tny_gtk_header_list_iterator_finalize;
298
299         return;
300 }
301
302 static gpointer
303 _tny_gtk_header_list_iterator_register_type (gpointer notused)
304 {
305         GType type = 0;
306
307         static const GTypeInfo info =
308                 {
309                   sizeof (TnyGtkHeaderListIteratorClass),
310                   NULL,   /* base_init */
311                   NULL,   /* base_finalize */
312                   (GClassInitFunc) tny_gtk_header_list_iterator_class_init,   /* class_init */
313                   NULL,   /* class_finalize */
314                   NULL,   /* class_data */
315                   sizeof (TnyGtkHeaderListIterator),
316                   0,      /* n_preallocs */
317                   tny_gtk_header_list_iterator_instance_init,    /* instance_init */
318                   NULL
319                 };
320
321         static const GInterfaceInfo tny_iterator_info =
322                 {
323                   (GInterfaceInitFunc) tny_iterator_init, /* interface_init */
324                   NULL,         /* interface_finalize */
325                   NULL          /* interface_data */
326                 };
327
328         type = g_type_register_static (G_TYPE_OBJECT,
329                                        "TnyGtkHeaderListIterator",
330                                        &info, 0);
331
332         g_type_add_interface_static (type, TNY_TYPE_ITERATOR,
333                                      &tny_iterator_info);
334
335         return GUINT_TO_POINTER (type);
336 }
337
338 GType
339 _tny_gtk_header_list_iterator_get_type (void)
340 {
341         static GOnce once = G_ONCE_INIT;
342         g_once (&once, _tny_gtk_header_list_iterator_register_type, NULL);
343         return GPOINTER_TO_UINT (once.retval);
344 }
Note: See TracBrowser for help on using the browser.