2007-10-13 Johannes Schmid <jhs@gnome.org>
[anjuta-git-plugin.git] / plugins / symbol-db / symbol-db-engine-iterator.c
blob784a501da6ecd5483d1ef8b369af6da6849d6c11
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) Massimo Cora' 2007 <maxcvs@email.it>
5 *
6 * anjuta is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include <libanjuta/interfaces/ianjuta-iterable.h>
26 #include <libanjuta/anjuta-utils.h>
27 #include <libanjuta/anjuta-debug.h>
28 #include "symbol-db-engine-iterator-node.h"
29 #include "symbol-db-engine-iterator.h"
30 #include "symbol-db-engine.h"
32 struct _SymbolDBEngineIteratorPriv
34 GdaDataModel *data_model;
35 GdaDataModelIter *data_iter;
37 gint total_rows;
40 static SymbolDBEngineIteratorNodeClass* parent_class = NULL;
42 static void
43 sdb_engine_iterator_instance_init (SymbolDBEngineIterator *object)
45 SymbolDBEngineIterator *sdbi;
47 sdbi = SYMBOL_DB_ENGINE_ITERATOR (object);
48 sdbi->priv = g_new0 (SymbolDBEngineIteratorPriv, 1);
50 /* initialize some priv data */
51 sdbi->priv->data_model = NULL;
52 sdbi->priv->data_iter = NULL;
53 sdbi->priv->total_rows = -1;
56 static void
57 sdb_engine_iterator_finalize (GObject *object)
59 SymbolDBEngineIterator *dbi;
60 SymbolDBEngineIteratorPriv *priv;
62 dbi = SYMBOL_DB_ENGINE_ITERATOR (object);
63 priv = dbi->priv;
65 if (priv->data_model)
66 g_object_unref (priv->data_model);
68 g_free (priv);
69 G_OBJECT_CLASS (parent_class)->finalize (object);
72 static void
73 sdb_engine_iterator_class_init (SymbolDBEngineIteratorClass *klass)
75 GObjectClass* object_class = G_OBJECT_CLASS (klass);
76 parent_class = g_type_class_ref (SYMBOL_TYPE_DB_ENGINE_ITERATOR_NODE);
78 object_class->finalize = sdb_engine_iterator_finalize;
81 SymbolDBEngineIterator *
82 symbol_db_engine_iterator_new (GdaDataModel *model)
84 SymbolDBEngineIterator *dbi;
85 SymbolDBEngineIteratorPriv *priv;
87 g_return_val_if_fail (model != NULL, NULL);
89 dbi = g_object_new (SYMBOL_TYPE_DB_ENGINE_ITERATOR, NULL);
91 priv = dbi->priv;
93 priv->data_model = model;
94 priv->data_iter = gda_data_model_iter_new (model);
96 /* because gda_data_model_get_n_rows () could be cpu-intensive, we'll
97 * proxy this value, e.g. it's calculated if it is really needed */
98 priv->total_rows = -1;
100 /* to avoid calling everytime this function when we want to use the IteratorNode,
101 * just do it now.
103 symbol_db_engine_iterator_first (dbi);
105 /* set the data_iter no the base class */
106 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (dbi),
107 priv->data_iter);
109 ianjuta_iterable_first (IANJUTA_ITERABLE (dbi), NULL);
110 return dbi;
114 * Set the iterator at the first node.
116 gboolean
117 symbol_db_engine_iterator_first (SymbolDBEngineIterator *dbi)
119 SymbolDBEngineIteratorPriv *priv;
121 g_return_val_if_fail (dbi != NULL, FALSE);
122 priv = dbi->priv;
124 return gda_data_model_iter_set_at_row (priv->data_iter, 0);
128 gboolean
129 symbol_db_engine_iterator_move_next (SymbolDBEngineIterator *dbi)
131 SymbolDBEngineIteratorPriv *priv;
133 g_return_val_if_fail (dbi != NULL, FALSE);
134 priv = dbi->priv;
136 return gda_data_model_iter_move_next (priv->data_iter);
139 gboolean
140 symbol_db_engine_iterator_move_prev (SymbolDBEngineIterator *dbi)
142 SymbolDBEngineIteratorPriv *priv;
144 g_return_val_if_fail (dbi != NULL, FALSE);
145 priv = dbi->priv;
147 return gda_data_model_iter_move_prev (priv->data_iter);
151 * @return -1 on error
153 gint
154 symbol_db_engine_iterator_get_n_items (SymbolDBEngineIterator *dbi)
156 SymbolDBEngineIteratorPriv *priv;
158 g_return_val_if_fail (dbi != NULL, -1);
159 priv = dbi->priv;
161 if (priv->total_rows < 0)
163 /* try to get the number of rows */
164 priv->total_rows = gda_data_model_get_n_rows (priv->data_model);
167 return priv->total_rows;
171 * Set the iterator at the last node.
173 gboolean
174 symbol_db_engine_iterator_last (SymbolDBEngineIterator *dbi)
176 SymbolDBEngineIteratorPriv *priv;
178 g_return_val_if_fail (dbi != NULL, FALSE);
179 priv = dbi->priv;
181 return gda_data_model_iter_set_at_row (priv->data_iter,
182 symbol_db_engine_iterator_get_n_items (dbi));
185 gboolean
186 symbol_db_engine_iterator_set_position (SymbolDBEngineIterator *dbi, gint pos)
188 SymbolDBEngineIteratorPriv *priv;
190 g_return_val_if_fail (dbi != NULL, FALSE);
191 priv = dbi->priv;
193 return gda_data_model_iter_set_at_row (priv->data_iter, pos);
196 gint
197 symbol_db_engine_iterator_get_position (SymbolDBEngineIterator *dbi)
199 SymbolDBEngineIteratorPriv *priv;
201 g_return_val_if_fail (dbi != NULL, FALSE);
202 priv = dbi->priv;
204 return gda_data_model_iter_get_row (priv->data_iter);
208 void
209 symbol_db_engine_iterator_foreach (SymbolDBEngineIterator *dbi, GFunc callback,
210 gpointer user_data)
212 SymbolDBEngineIteratorPriv *priv;
214 g_return_if_fail (dbi != NULL);
215 priv = dbi->priv;
217 gint saved_pos = symbol_db_engine_iterator_get_position (dbi);
219 symbol_db_engine_iterator_last (dbi);
221 while (!symbol_db_engine_iterator_move_next (dbi))
222 callback (dbi, user_data);
223 symbol_db_engine_iterator_set_position (dbi, saved_pos);
226 /* IAnjutaIterable implementation */
228 static gboolean
229 isymbol_iter_first (IAnjutaIterable *iterable, GError **err)
231 SymbolDBEngineIterator *dbi = SYMBOL_DB_ENGINE_ITERATOR (iterable);
232 SymbolDBEngineIteratorPriv *priv;
234 g_return_val_if_fail (dbi != NULL, FALSE);
235 priv = dbi->priv;
238 if (symbol_db_engine_iterator_first (dbi) == FALSE)
240 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
241 NULL);
243 return FALSE;
245 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
246 priv->data_iter);
247 return TRUE;
250 static gboolean
251 isymbol_iter_next (IAnjutaIterable *iterable, GError **err)
253 SymbolDBEngineIterator *dbi = SYMBOL_DB_ENGINE_ITERATOR (iterable);
254 SymbolDBEngineIteratorPriv *priv;
256 g_return_val_if_fail (dbi != NULL, FALSE);
257 priv = dbi->priv;
259 if (symbol_db_engine_iterator_move_next (dbi) == FALSE)
261 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
262 NULL);
264 return FALSE;
266 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
267 priv->data_iter);
269 return TRUE;
272 static gboolean
273 isymbol_iter_previous (IAnjutaIterable *iterable, GError **err)
275 SymbolDBEngineIterator *dbi = SYMBOL_DB_ENGINE_ITERATOR (iterable);
276 SymbolDBEngineIteratorPriv *priv;
278 g_return_val_if_fail (dbi != NULL, FALSE);
279 priv = dbi->priv;
281 if (symbol_db_engine_iterator_move_prev (dbi) == FALSE)
283 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
284 NULL);
286 return FALSE;
288 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
289 priv->data_iter);
291 return TRUE;
294 static gboolean
295 isymbol_iter_last (IAnjutaIterable *iterable, GError **err)
297 SymbolDBEngineIterator *dbi = SYMBOL_DB_ENGINE_ITERATOR (iterable);
298 SymbolDBEngineIteratorPriv *priv;
300 g_return_val_if_fail (dbi != NULL, FALSE);
301 priv = dbi->priv;
303 if (symbol_db_engine_iterator_last (dbi) == FALSE)
305 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
306 NULL);
308 return FALSE;
310 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
311 priv->data_iter);
313 return TRUE;
316 static void
317 isymbol_iter_foreach (IAnjutaIterable *iterable, GFunc callback,
318 gpointer user_data, GError **err)
320 SymbolDBEngineIterator *dbi = SYMBOL_DB_ENGINE_ITERATOR (iterable);
321 SymbolDBEngineIteratorPriv *priv;
323 g_return_if_fail (dbi != NULL);
324 priv = dbi->priv;
326 symbol_db_engine_iterator_foreach (dbi, callback, user_data);
329 static gboolean
330 isymbol_iter_set_position (IAnjutaIterable *iterable,
331 gint position, GError **err)
333 SymbolDBEngineIterator *dbi = SYMBOL_DB_ENGINE_ITERATOR (iterable);
334 SymbolDBEngineIteratorPriv *priv;
336 g_return_val_if_fail (dbi != NULL, FALSE);
337 priv = dbi->priv;
339 if (symbol_db_engine_iterator_set_position (dbi, position) == FALSE)
341 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
342 NULL);
344 return FALSE;
346 symbol_db_engine_iterator_node_set_data (SYMBOL_DB_ENGINE_ITERATOR_NODE (iterable),
347 priv->data_iter);
349 return TRUE;
352 static gint
353 isymbol_iter_get_position (IAnjutaIterable *iterable, GError **err)
355 SymbolDBEngineIterator *dbi = SYMBOL_DB_ENGINE_ITERATOR (iterable);
356 SymbolDBEngineIteratorPriv *priv;
358 g_return_val_if_fail (dbi != NULL, FALSE);
359 priv = dbi->priv;
361 return symbol_db_engine_iterator_get_position (dbi);
364 static gint
365 isymbol_iter_get_length (IAnjutaIterable *iterable, GError **err)
367 SymbolDBEngineIterator *dbi = SYMBOL_DB_ENGINE_ITERATOR (iterable);
368 SymbolDBEngineIteratorPriv *priv;
370 g_return_val_if_fail (dbi != NULL, FALSE);
371 priv = dbi->priv;
373 return symbol_db_engine_iterator_get_n_items (dbi);
376 static IAnjutaIterable *
377 isymbol_iter_clone (IAnjutaIterable *iter, GError **e)
379 DEBUG_PRINT ("isymbol_iter_clone () UNIMPLEMENTED");
380 return NULL;
383 static void
384 isymbol_iter_assign (IAnjutaIterable *iter, IAnjutaIterable *src_iter, GError **e)
386 DEBUG_PRINT ("isymbol_iter_assign () UNIMPLEMENTED");
390 static void
391 isymbol_iter_iface_init (IAnjutaIterableIface *iface, GError **err)
393 iface->first = isymbol_iter_first;
394 iface->next = isymbol_iter_next;
395 iface->previous = isymbol_iter_previous;
396 iface->last = isymbol_iter_last;
397 iface->foreach = isymbol_iter_foreach;
398 iface->set_position = isymbol_iter_set_position;
399 iface->get_position = isymbol_iter_get_position;
400 iface->get_length = isymbol_iter_get_length;
401 iface->clone = isymbol_iter_clone;
402 iface->assign = isymbol_iter_assign;
407 ANJUTA_TYPE_BEGIN (SymbolDBEngineIterator, sdb_engine_iterator, SYMBOL_TYPE_DB_ENGINE_ITERATOR_NODE);
408 ANJUTA_TYPE_ADD_INTERFACE (isymbol_iter, IANJUTA_TYPE_ITERABLE);
409 ANJUTA_TYPE_END;