Use a single include.
[mmediamanager.git] / src / mm-hit-collection.c
blob1b3227540684375c86852e0243882f5c36bfa73f
1 /* MManager - a Desktop wide manager for multimedia applications.
3 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <glib.h>
22 #include <glib-object.h>
24 #include "mm-hit-collection.h"
26 #define MM_HIT_COLLECTION_GET_PRIVATE(o) \
27 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_HIT_COLLECTION, MMHitCollectionDetails))
29 G_DEFINE_TYPE (MMHitCollection, mm_hit_collection, G_TYPE_OBJECT);
31 struct _MMHitCollectionDetails {
32 GPtrArray *hits;
33 guint current_offset;
36 static void
37 unref_hit (gpointer hit,
38 gpointer unused)
40 g_object_unref (hit);
43 static void
44 mm_hit_collection_finalize (GObject *o)
46 MMHitCollection *c = MM_HIT_COLLECTION (o);
48 /* unref all the hits first */
49 g_ptr_array_foreach (c->details->hits, unref_hit, NULL);
50 g_ptr_array_free (c->details->hits, FALSE);
52 G_OBJECT_CLASS (mm_hit_collection_parent_class)->finalize (o);
55 static void
56 mm_hit_collection_class_init (MMHitCollectionClass *klass)
58 G_OBJECT_CLASS (klass)->finalize = mm_hit_collection_finalize;
60 g_type_class_add_private (klass, sizeof (MMHitCollectionDetails));
63 static void
64 mm_hit_collection_init (MMHitCollection *c)
66 MMHitCollectionDetails *details = c->details = MM_HIT_COLLECTION_GET_PRIVATE (c);
68 details->hits = g_ptr_array_new ();
69 details->current_offset = 0;
72 /* public methods */
74 MMHitCollection *
75 mm_hit_collection_new (void)
77 MMHitCollection *c;
79 c = g_object_new (MM_TYPE_HIT_COLLECTION, NULL);
81 return c;
84 void
85 mm_hit_collection_add_hit (MMHitCollection *c,
86 MMHit *hit)
88 MMHitCollectionDetails *details = c->details;
90 g_ptr_array_add (details->hits, g_object_ref (hit));
93 MMHit *
94 mm_hit_collection_get_next_hit (MMHitCollection *c)
96 MMHitCollectionDetails *details = c->details;
97 MMHit *hit;
99 if (details->hits->len == 0 ||
100 details->current_offset >= details->hits->len) {
101 /* no hits in this collection or we already got all results */
102 hit = NULL;
103 } else {
104 hit = g_ptr_array_index (details->hits, details->current_offset);
105 details->current_offset++;
108 return hit;