File: oobs-list.c

package info (click to toggle)
liboobs 3.0.0-4.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,284 kB
  • sloc: sh: 10,131; ansic: 9,920; makefile: 176; xml: 1
file content (636 lines) | stat: -rw-r--r-- 14,610 bytes parent folder | download | duplicates (7)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* Copyright (C) 2005 Carlos Garnacho
 *
 * 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Carlos Garnacho Parro  <carlosg@gnome.org>
 */

#include <dbus/dbus.h>
#include <glib-object.h>
#include <string.h>

#include "oobs-object.h"
#include "oobs-list.h"
#include "oobs-list-private.h"

/**
 * SECTION:oobs-list
 * @title: OobsList
 * @short_description: Object to deal with several configuration elements
 * @see_also: #OobsObject
 **/

#define OOBS_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OOBS_TYPE_LIST, OobsListPrivate))

typedef struct _OobsListPrivate OobsListPrivate;

struct _OobsListPrivate
{
  GList *list;
  guint  stamp;

  GType  contained_type;
  gboolean locked;
};

static void oobs_list_class_init (OobsListClass *class);
static void oobs_list_init       (OobsList      *list);
static void oobs_list_finalize   (GObject      *object);

static void oobs_list_set_property (GObject      *object,
				    guint         prop_id,
				    const GValue *value,
				    GParamSpec   *pspec);
enum
{
  PROP_0,
  PROP_CONTAINED_TYPE
};

G_DEFINE_TYPE (OobsList, oobs_list, G_TYPE_OBJECT);

GType
oobs_list_iter_get_type (void)
{
  static GType iter_type = 0;
  
  if (iter_type == 0)
    iter_type = g_boxed_type_register_static ("OobsListIter",
					      (GBoxedCopyFunc) oobs_list_iter_copy,
					      (GBoxedFreeFunc) oobs_list_iter_free);

  return iter_type;
}

static void
oobs_list_class_init (OobsListClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = oobs_list_finalize;
  object_class->set_property = oobs_list_set_property;

  g_object_class_install_property (object_class,
				   PROP_CONTAINED_TYPE,
				   g_param_spec_pointer ("contained-type",
							 "Contained type",
							 "GType contained in the list",
							 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
  g_type_class_add_private (object_class,
			    sizeof (OobsListPrivate));
}

static void
oobs_list_init (OobsList *object)
{
  OobsListPrivate *priv;

  g_return_if_fail (OOBS_IS_LIST (object));
  priv = OOBS_LIST_GET_PRIVATE (object);

  priv->stamp  = 0;
  priv->list   = NULL;
  priv->locked = FALSE;
  object->_priv = priv;
}

static void
oobs_list_finalize (GObject *object)
{
  OobsList *list;
  OobsListPrivate *priv;

  g_return_if_fail (OOBS_IS_LIST (object));

  list = OOBS_LIST (object);
  priv = list->_priv;

  /* set locking to FALSE, the object
   * is already being finalized anyway */
  priv->locked = FALSE;

  if (priv && priv->list)
    oobs_list_clear (list);

  if (G_OBJECT_CLASS (oobs_list_parent_class)->finalize)
    (* G_OBJECT_CLASS (oobs_list_parent_class)->finalize) (object);
}

static void
oobs_list_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
			GParamSpec   *pspec)
{
  OobsList *list;
  OobsListPrivate *priv;
  GType *type;

  g_return_if_fail (OOBS_IS_LIST (object));

  list = OOBS_LIST (object);
  priv = list->_priv;

  switch (prop_id)
    {
    case PROP_CONTAINED_TYPE:
      type = g_value_get_pointer (value);
      priv->contained_type = *type;
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static gboolean
check_iter (OobsListPrivate *priv, OobsListIter *iter)
{
  if (priv->stamp != iter->stamp)
    {
      g_critical ("OobsList stamp and OobsListIter stamp differ");
      return FALSE;
    }

  if (g_list_position (priv->list, iter->data) == -1)
    return FALSE;

  return TRUE;
}

OobsList*
_oobs_list_new (const GType contained_type)
{
  return g_object_new (OOBS_TYPE_LIST,
		       "contained-type", &contained_type,
		       NULL);
}

void
_oobs_list_set_locked (OobsList *list, gboolean locked)
{
  OobsListPrivate *priv;

  g_return_if_fail (OOBS_IS_LIST (list));

  priv = list->_priv;
  priv->locked = locked;
}

/**
 * oobs_list_get_iter_first:
 * @list: An #OobsList
 * @iter: An uninitialized #OobsListIter
 * 
 * Initializes @iter the iterator pointing to the
 * first element in @list. if @list is empty, then FALSE
 * is returned, and @iter is not initialized.
 * 
 * Return Value: #TRUE if @iter was set
 **/
gboolean
oobs_list_get_iter_first (OobsList *list, OobsListIter *iter)
{
  OobsListPrivate *priv;

  g_return_val_if_fail (list != NULL, FALSE);
  g_return_val_if_fail (OOBS_IS_LIST (list), FALSE);

  priv = list->_priv;

  if (!priv->list)
    return FALSE;

  iter->stamp = priv->stamp;
  iter->data  = priv->list;

  return TRUE;
}

/**
 * oobs_list_iter_next:
 * @list: An #OobsList
 * @iter: A valid #OobsListIter pointing to an element in @list
 * 
 * Sets @iter to point to the element following it. If there's
 * no next @iter, of if @iter was invalid for @list, #FALSE is
 * returned, and @iter is set to invalid.
 * 
 * Return Value: #TRUE if iter has been correctly changed to the next element
 **/
gboolean
oobs_list_iter_next (OobsList *list, OobsListIter *iter)
{
  OobsListPrivate *priv;
  GList *data;

  g_return_val_if_fail (list != NULL, FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (iter->data != NULL, FALSE);
  g_return_val_if_fail (OOBS_IS_LIST (list), FALSE);

  priv = list->_priv;

  if (!check_iter (priv, iter))
    return FALSE;

  data = (GList *) iter->data;
  iter->data = data->next;

  return (iter->data != NULL);
}

/**
 * oobs_list_remove:
 * @list: An #OobsList
 * @iter: A valid #OobsListIter pointing to an element in @list
 * 
 * Removes an element pointed by @iter from @list. This function will not
 * be effective if @list is locked, or if @iter doesn't point to an element
 * contained in @list.
 * 
 * Return Value: #TRUE if the element was correctly removed
 **/
gboolean
oobs_list_remove (OobsList *list, OobsListIter *iter)
{
  OobsListPrivate *priv;
  GList *data, *next;
  gboolean list_locked;

  g_return_val_if_fail (list != NULL, FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (iter->data != NULL, FALSE);
  g_return_val_if_fail (OOBS_IS_LIST (list), FALSE);

  priv = list->_priv;

  list_locked = priv->locked;
  g_return_val_if_fail (list_locked != TRUE, FALSE);

  if (!check_iter (priv, iter))
    return FALSE;

  data = (GList *) iter->data;

  /* point to the next element */
  next = data->next;

  g_object_unref (data->data);
  priv->list = g_list_delete_link (priv->list, data);

  iter->data = next;

  return TRUE;
}

/**
 * oobs_list_append:
 * @list: An #OobsList
 * @iter: An unset #OobsListIter to set to the new element
 * 
 * Appends a new element to @list. @iter will be changed
 * to point to this element, to fill in values, you need to call
 * oobs_list_set().
 **/
void
oobs_list_append (OobsList *list, OobsListIter *iter)
{
  OobsListPrivate *priv;
  gboolean list_locked;

  g_return_if_fail (list != NULL);
  g_return_if_fail (iter != NULL);
  g_return_if_fail (OOBS_IS_LIST (list));

  priv = list->_priv;

  list_locked = priv->locked;
  g_return_if_fail (list_locked != TRUE);

  /* Change the stamp if the list was empty */
  if (!priv->list)
    priv->stamp++;

  priv->list = g_list_append (priv->list, NULL);

  iter->data = g_list_last (priv->list);
  iter->stamp = priv->stamp;
}

/**
 * oobs_list_prepend:
 * @list: An #OobsList
 * @iter: An unset #OobsListIter to set to the new element
 * 
 * Prepends a new element to @list. @iter will be changed
 * to point to this element, to fill in values, you need to call
 * oobs_list_set().
 **/
void
oobs_list_prepend (OobsList *list, OobsListIter *iter)
{
  OobsListPrivate *priv;
  gboolean list_locked;

  g_return_if_fail (list != NULL);
  g_return_if_fail (iter != NULL);
  g_return_if_fail (OOBS_IS_LIST (list));

  priv = list->_priv;

  list_locked = priv->locked;
  g_return_if_fail (list_locked != TRUE);

  /* Change the stamp if the list was empty */
  if (!priv->list)
    priv->stamp++;

  priv->list = g_list_prepend (priv->list, NULL);

  iter->data = priv->list;
  iter->stamp = priv->stamp;
}

/**
 * oobs_list_insert_after:
 * @list: An #OobsList
 * @anchor: A valid #OobsListIter
 * @iter: An unset #OobsListIter to set to the new element
 * 
 * Inserts a new element after @anchor. @iter will be changed
 * to point to this element, to fill in values, you need to call
 * oobs_list_set().
 **/
void
oobs_list_insert_after (OobsList     *list,
			OobsListIter *anchor,
			OobsListIter *iter)
{
  OobsListPrivate *priv;
  GList *node, *anchor_node;
  gboolean list_locked;

  g_return_if_fail (list != NULL);
  g_return_if_fail (anchor != NULL);
  g_return_if_fail (anchor->data != NULL);
  g_return_if_fail (iter != NULL);
  g_return_if_fail (OOBS_IS_LIST (list));

  priv = list->_priv;
  
  list_locked = priv->locked;
  g_return_if_fail (list_locked != TRUE);

  if (!check_iter (priv, anchor))
    return;

  anchor_node = anchor->data;
  node = g_list_alloc ();
  node->data = NULL;
  
  anchor_node->next->prev = node;
  node->next = anchor_node->next;
  anchor_node->next = node;
  node->prev = anchor_node;

  iter->stamp = priv->stamp;
  iter->data  = node;
}

/**
 * oobs_list_insert_before:
 * @list: An #OobsList
 * @anchor: A valid #OobsListIter
 * @iter: An unset #OobsListIter to set to the new element
 * 
 * Inserts a new element before @anchor. @iter will be changed
 * to point to this element, to fill in values, you need to call
 * oobs_list_set().
 **/
void
oobs_list_insert_before (OobsList     *list,
			 OobsListIter *anchor,
			 OobsListIter *iter)
{
  OobsListPrivate *priv;
  GList *node, *anchor_node;
  gboolean list_locked;

  /* FIXME: make it match with the
     gtk_list_store behavior and api */

  g_return_if_fail (list != NULL);
  g_return_if_fail (anchor != NULL);
  g_return_if_fail (anchor->data != NULL);
  g_return_if_fail (iter != NULL);
  g_return_if_fail (OOBS_IS_LIST (list));

  priv = list->_priv;
  
  list_locked = priv->locked;
  g_return_if_fail (list_locked != TRUE);

  if (!check_iter (priv, anchor))
    return;

  anchor_node = anchor->data;
  node = g_list_alloc ();
  node->data = NULL;

  anchor_node->prev->next = node;
  node->prev = anchor_node->prev;
  anchor_node->prev = node;
  node->next = anchor_node;

  iter->stamp = priv->stamp;
  iter->data  = node;
}

/**
 * oobs_list_get:
 * @list: an #OobsList
 * @iter: a valid #OobsListIter for the element being get
 * 
 * Retrieves a reference to the element
 * referenced by #OobsListIter.
 * 
 * Return Value: the element referenced by @iter
 **/
GObject*
oobs_list_get (OobsList     *list,
	       OobsListIter *iter)
{
  OobsListPrivate *priv;
  GList *node;

  g_return_val_if_fail (list != NULL, NULL);
  g_return_val_if_fail (iter != NULL, NULL);
  g_return_val_if_fail (iter->data != NULL, NULL);
  g_return_val_if_fail (OOBS_IS_LIST (list), NULL);

  node = iter->data;
  priv = list->_priv;

  g_return_val_if_fail (node->data != NULL, NULL);

  if (!check_iter (priv, iter))
    return NULL;

  return g_object_ref (node->data);
}

static gboolean
check_types (OobsList *list,
	     GObject  *data)
{
  OobsListPrivate *priv;

  priv = list->_priv;
  
  if (!G_TYPE_CHECK_INSTANCE_TYPE (data, priv->contained_type))
    {
      g_critical ("Trying to store a different object type in the list");
      return FALSE;
    }

  return TRUE;
}

/**
 * oobs_list_set:
 * @list: an #OobsList
 * @iter: a valid #OobsListIter for the element being set
 * @data: a pointer to the data being set
 * 
 * Sets the data for the element referenced by #OobsListIter.
 * This function will not be effective if the list is locked, or
 * if @data GType is different to the data contained in #OobsList.
 **/
void
oobs_list_set (OobsList     *list,
	       OobsListIter *iter,
	       gpointer      data)
{
  OobsListPrivate *priv;
  GList *node;
  gboolean list_locked;

  g_return_if_fail (list != NULL);
  g_return_if_fail (iter != NULL);
  g_return_if_fail (OOBS_IS_LIST (list));
  g_return_if_fail (G_IS_OBJECT (data));

  node = iter->data;
  priv = list->_priv;

  list_locked = priv->locked;
  g_return_if_fail (list_locked != TRUE);

  g_return_if_fail (node->data == NULL);

  if (!check_iter (priv, iter))
    return;

  if (!check_types (list, data))
    return;

  node->data = g_object_ref (data);
}

/**
 * oobs_list_clear:
 * @list: an #OobsList
 * 
 * Removes all contents from an #OobsList. This function will
 * not be effective if the list is locked.
 **/
void
oobs_list_clear (OobsList *list)
{
  OobsListPrivate *priv;
  gboolean list_locked;
  
  g_return_if_fail (list != NULL);
  g_return_if_fail (OOBS_IS_LIST (list));

  priv = list->_priv;

  list_locked = priv->locked;
  g_return_if_fail (list_locked != TRUE);

  if (priv->list)
    {
      g_list_foreach (priv->list, (GFunc) g_object_unref, NULL);
      g_list_free    (priv->list);
      priv->list = NULL;
    }
}

/**
 * oobs_list_get_n_items:
 * @list: An #OobsList.
 * 
 * Returns the number of elements that the list contains.
 * 
 * Return Value: the number of elements.
 **/
gint
oobs_list_get_n_items (OobsList *list)
{
  OobsListPrivate *priv;

  g_return_val_if_fail (OOBS_IS_LIST (list), 0);

  priv = list->_priv;

  return g_list_length (priv->list);
}

/**
 * oobs_list_iter_copy:
 * @iter: An #OobsListIter.
 * 
 * Returns a newly allocated copy of the given iterator. This function is not
 * intended for use in applications, because you can just copy the structs by
 * value (OobsListIter new_iter = iter;). You must free this iter with
 * oobs_list_iter_free().
 * 
 * Return Value: A newly allocated iterator.
 **/
OobsListIter*
oobs_list_iter_copy (OobsListIter *iter)
{
  OobsListIter *copy;

  copy = g_new0 (OobsListIter, 1);
  copy->stamp = iter->stamp;
  copy->data  = iter->data;

  return copy;
}

/**
 * oobs_list_iter_free:
 * @iter: An #OobsListIter.
 * 
 * Frees an iterator that has been allocated in the heap.
 **/
void
oobs_list_iter_free (OobsListIter *iter)
{
  g_free (iter);
}