hythmbox-2.98/rhythmdb/rhythmdb-query-result-list.c

No issues found

  1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2  *
  3  *  Copyright (C) 2010 Jonathan Matthew <jonathan@d14n.org>
  4  *
  5  *  This program is free software; you can redistribute it and/or modify
  6  *  it under the terms of the GNU General Public License as published by
  7  *  the Free Software Foundation; either version 2 of the License, or
  8  *  (at your option) any later version.
  9  *
 10  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 11  *  GStreamer plugins to be used and distributed together with GStreamer
 12  *  and Rhythmbox. This permission is above and beyond the permissions granted
 13  *  by the GPL license by which Rhythmbox is covered. If you modify this code
 14  *  you may extend this exception to your version of the code, but you are not
 15  *  obligated to do so. If you do not wish to do so, delete this exception
 16  *  statement from your version.
 17  *
 18  *  This program is distributed in the hope that it will be useful,
 19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21  *  GNU General Public License for more details.
 22  *
 23  *  You should have received a copy of the GNU General Public License
 24  *  along with this program; if not, write to the Free Software
 25  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 26  *
 27  */
 28 
 29 #include "config.h"
 30 
 31 #include "rhythmdb.h"
 32 #include "rhythmdb-query-result-list.h"
 33 #include "rb-debug.h"
 34 #include "rb-util.h"
 35 
 36 struct _RhythmDBQueryResultListPrivate
 37 {
 38 	gboolean complete;
 39 	GList *results;
 40 };
 41 
 42 enum {
 43 	COMPLETE,
 44 	LAST_SIGNAL
 45 };
 46 
 47 static guint rhythmdb_query_result_list_signals[LAST_SIGNAL] = { 0 };
 48 
 49 static void rhythmdb_query_result_list_query_results_init (RhythmDBQueryResultsIface *iface);
 50 
 51 G_DEFINE_TYPE_WITH_CODE(RhythmDBQueryResultList, rhythmdb_query_result_list, G_TYPE_OBJECT,
 52 			G_IMPLEMENT_INTERFACE(RHYTHMDB_TYPE_QUERY_RESULTS,
 53 					      rhythmdb_query_result_list_query_results_init));
 54 
 55 
 56 static void
 57 impl_set_query (RhythmDBQueryResults *results, GPtrArray *query)
 58 {
 59 }
 60 
 61 static void
 62 impl_add_results (RhythmDBQueryResults *results, GPtrArray *entries)
 63 {
 64 	RhythmDBQueryResultList *list = RHYTHMDB_QUERY_RESULT_LIST (results);
 65 	int i;
 66 
 67 	for (i = 0; i < entries->len; i++) {
 68 		RhythmDBEntry *entry;
 69 		entry = g_ptr_array_index (entries, i);
 70 		rhythmdb_entry_ref (entry);
 71 
 72 		list->priv->results = g_list_prepend (list->priv->results, entry);
 73 	}
 74 }
 75 
 76 static void
 77 impl_query_complete (RhythmDBQueryResults *results)
 78 {
 79 	RhythmDBQueryResultList *list = RHYTHMDB_QUERY_RESULT_LIST (results);
 80 
 81 	list->priv->results = g_list_reverse (list->priv->results);
 82 	list->priv->complete = TRUE;
 83 
 84 	g_signal_emit (G_OBJECT (results), rhythmdb_query_result_list_signals[COMPLETE], 0);
 85 }
 86 
 87 static void
 88 impl_finalize (GObject *object)
 89 {
 90 	RhythmDBQueryResultList *list = RHYTHMDB_QUERY_RESULT_LIST (object);
 91 
 92 	rb_list_destroy_free (list->priv->results, (GDestroyNotify)rhythmdb_entry_unref);
 93 
 94 	G_OBJECT_CLASS (rhythmdb_query_result_list_parent_class)->finalize (object);
 95 }
 96 
 97 static void
 98 rhythmdb_query_result_list_init (RhythmDBQueryResultList *list)
 99 {
100 	list->priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
101 						  RHYTHMDB_TYPE_QUERY_RESULT_LIST,
102 						  RhythmDBQueryResultListPrivate);
103 }
104 
105 static void
106 rhythmdb_query_result_list_query_results_init (RhythmDBQueryResultsIface *iface)
107 {
108 	iface->set_query = impl_set_query;
109 	iface->add_results = impl_add_results;
110 	iface->query_complete = impl_query_complete;
111 }
112 
113 static void
114 rhythmdb_query_result_list_class_init (RhythmDBQueryResultListClass *klass)
115 {
116 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
117 
118 	object_class->finalize = impl_finalize;
119 
120 	/**
121 	 * RhythmDBQueryResultList::complete:
122 	 * @list: the #RhythmDBQueryResultList
123 	 *
124 	 * Emitted when the database query is complete.
125 	 */
126 	rhythmdb_query_result_list_signals[COMPLETE] =
127 		g_signal_new ("complete",
128 			      RHYTHMDB_TYPE_QUERY_RESULT_LIST,
129 			      G_SIGNAL_RUN_LAST,
130 			      G_STRUCT_OFFSET (RhythmDBQueryResultListClass, complete),
131 			      NULL, NULL,
132 			      g_cclosure_marshal_VOID__VOID,
133 			      G_TYPE_NONE, 0);
134 
135 	g_type_class_add_private (klass, sizeof (RhythmDBQueryResultListPrivate));
136 }
137 
138 
139 /**
140  * rhythmdb_query_result_list_new:
141  *
142  * Creates a new empty query result list.
143  *
144  * Return value: (transfer full): query result list
145  */
146 RhythmDBQueryResultList *
147 rhythmdb_query_result_list_new (void)
148 {
149 	GObject *obj;
150 	obj = g_object_new (RHYTHMDB_TYPE_QUERY_RESULT_LIST, NULL);
151 	return RHYTHMDB_QUERY_RESULT_LIST (obj);
152 }
153 
154 /**
155  * rhythmdb_query_result_list_get_results:
156  * @list: a #RhythmDBQueryResultList
157  *
158  * Returns the results from the query.
159  *
160  * Return value: (transfer none) (element-type RhythmDBEntry): list of results
161  */
162 GList *
163 rhythmdb_query_result_list_get_results (RhythmDBQueryResultList *list)
164 {
165 	g_assert (list->priv->complete);
166 	return list->priv->results;
167 }