[rhythmbox] rhythmdb: add a RhythmDBQueryResults object that stores entries in a list



commit 3d370aaed640cfcf4496a3de82b5913ccef0186b
Author: Jonathan Matthew <jonathan d14n org>
Date:   Thu Aug 19 23:06:27 2010 +1000

    rhythmdb: add a RhythmDBQueryResults object that stores entries in a list
    
    This is more convenient in cases where we just want to process the query
    results in a simple loop and then discard them.  It might be a little bit
    more efficient too.

 rhythmdb/Makefile.am                  |    2 +
 rhythmdb/rhythmdb-query-result-list.c |  152 +++++++++++++++++++++++++++++++++
 rhythmdb/rhythmdb-query-result-list.h |   72 ++++++++++++++++
 3 files changed, 226 insertions(+), 0 deletions(-)
---
diff --git a/rhythmdb/Makefile.am b/rhythmdb/Makefile.am
index a45cf25..18cf59e 100644
--- a/rhythmdb/Makefile.am
+++ b/rhythmdb/Makefile.am
@@ -21,6 +21,7 @@ rhythmdbinclude_HEADERS =				\
 	rhythmdb.h					\
 	rhythmdb-property-model.h			\
 	rhythmdb-query-model.h				\
+	rhythmdb-query-result-list.h			\
 	rhythmdb-query-results.h			\
 	rhythmdb-import-job.h				\
 	rhythmdb-entry.h				\
@@ -35,6 +36,7 @@ librhythmdb_la_SOURCES =				\
 	rhythmdb-query.c				\
 	rhythmdb-property-model.c			\
 	rhythmdb-query-model.c				\
+	rhythmdb-query-result-list.c			\
 	rhythmdb-query-results.c			\
 	rhythmdb-import-job.c				\
 	rhythmdb-entry-type.c				\
diff --git a/rhythmdb/rhythmdb-query-result-list.c b/rhythmdb/rhythmdb-query-result-list.c
new file mode 100644
index 0000000..d0fd4cb
--- /dev/null
+++ b/rhythmdb/rhythmdb-query-result-list.c
@@ -0,0 +1,152 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ *  Copyright (C) 2010 Jonathan Matthew <jonathan d14n org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The Rhythmbox authors hereby grant permission for non-GPL compatible
+ *  GStreamer plugins to be used and distributed together with GStreamer
+ *  and Rhythmbox. This permission is above and beyond the permissions granted
+ *  by the GPL license by which Rhythmbox is covered. If you modify this code
+ *  you may extend this exception to your version of the code, but you are not
+ *  obligated to do so. If you do not wish to do so, delete this exception
+ *  statement from your 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
+ *
+ */
+
+#include "config.h"
+
+#include "rhythmdb.h"
+#include "rhythmdb-query-result-list.h"
+#include "rb-debug.h"
+#include "rb-util.h"
+
+struct _RhythmDBQueryResultListPrivate
+{
+	gboolean complete;
+	GList *results;
+};
+
+enum {
+	COMPLETE,
+	LAST_SIGNAL
+};
+
+static guint rhythmdb_query_result_list_signals[LAST_SIGNAL] = { 0 };
+
+static void rhythmdb_query_result_list_query_results_init (RhythmDBQueryResultsIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE(RhythmDBQueryResultList, rhythmdb_query_result_list, G_TYPE_OBJECT,
+			G_IMPLEMENT_INTERFACE(RHYTHMDB_TYPE_QUERY_RESULTS,
+					      rhythmdb_query_result_list_query_results_init));
+
+
+static void
+impl_set_query (RhythmDBQueryResults *results, GPtrArray *query)
+{
+}
+
+static void
+impl_add_results (RhythmDBQueryResults *results, GPtrArray *entries)
+{
+	RhythmDBQueryResultList *list = RHYTHMDB_QUERY_RESULT_LIST (results);
+	int i;
+
+	for (i = 0; i < entries->len; i++) {
+		RhythmDBEntry *entry;
+		entry = g_ptr_array_index (entries, i);
+		rhythmdb_entry_ref (entry);
+
+		list->priv->results = g_list_prepend (list->priv->results, entry);
+	}
+}
+
+static void
+impl_query_complete (RhythmDBQueryResults *results)
+{
+	RhythmDBQueryResultList *list = RHYTHMDB_QUERY_RESULT_LIST (results);
+
+	list->priv->results = g_list_reverse (list->priv->results);
+	list->priv->complete = TRUE;
+
+	g_signal_emit (G_OBJECT (results), rhythmdb_query_result_list_signals[COMPLETE], 0);
+}
+
+static void
+impl_finalize (GObject *object)
+{
+	RhythmDBQueryResultList *list = RHYTHMDB_QUERY_RESULT_LIST (object);
+
+	rb_list_destroy_free (list->priv->results, (GDestroyNotify)rhythmdb_entry_unref);
+
+	G_OBJECT_CLASS (rhythmdb_query_result_list_parent_class)->finalize (object);
+}
+
+static void
+rhythmdb_query_result_list_init (RhythmDBQueryResultList *list)
+{
+	list->priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
+						  RHYTHMDB_TYPE_QUERY_RESULT_LIST,
+						  RhythmDBQueryResultListPrivate);
+}
+
+static void
+rhythmdb_query_result_list_query_results_init (RhythmDBQueryResultsIface *iface)
+{
+	iface->set_query = impl_set_query;
+	iface->add_results = impl_add_results;
+	iface->query_complete = impl_query_complete;
+}
+
+static void
+rhythmdb_query_result_list_class_init (RhythmDBQueryResultListClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+	object_class->finalize = impl_finalize;
+
+	/**
+	 * RhythmDBQueryResultList::complete:
+	 * @list: the #RhythmDBQueryResultList
+	 *
+	 * Emitted when the database query is complete.
+	 */
+	rhythmdb_query_result_list_signals[COMPLETE] =
+		g_signal_new ("complete",
+			      RHYTHMDB_TYPE_QUERY_RESULT_LIST,
+			      G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (RhythmDBQueryResultListClass, complete),
+			      NULL, NULL,
+			      g_cclosure_marshal_VOID__VOID,
+			      G_TYPE_NONE, 0);
+
+	g_type_class_add_private (klass, sizeof (RhythmDBQueryResultListPrivate));
+}
+
+
+RhythmDBQueryResultList *
+rhythmdb_query_result_list_new (void)
+{
+	GObject *obj;
+	obj = g_object_new (RHYTHMDB_TYPE_QUERY_RESULT_LIST, NULL);
+	return RHYTHMDB_QUERY_RESULT_LIST (obj);
+}
+
+GList *
+rhythmdb_query_result_list_get_results (RhythmDBQueryResultList *list)
+{
+	g_assert (list->priv->complete);
+	return list->priv->results;
+}
diff --git a/rhythmdb/rhythmdb-query-result-list.h b/rhythmdb/rhythmdb-query-result-list.h
new file mode 100644
index 0000000..d454cef
--- /dev/null
+++ b/rhythmdb/rhythmdb-query-result-list.h
@@ -0,0 +1,72 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ *  Copyright (C) 2010 Jonathan Matthew  <jonathan d14n org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The Rhythmbox authors hereby grant permission for non-GPL compatible
+ *  GStreamer plugins to be used and distributed together with GStreamer
+ *  and Rhythmbox. This permission is above and beyond the permissions granted
+ *  by the GPL license by which Rhythmbox is covered. If you modify this code
+ *  you may extend this exception to your version of the code, but you are not
+ *  obligated to do so. If you do not wish to do so, delete this exception
+ *  statement from your 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
+ *
+ */
+
+#include <glib-object.h>
+
+#ifndef RHYTHMDB_QUERY_RESULT_LIST_H
+#define RHYTHMDB_QUERY_RESULT_LIST_H
+
+G_BEGIN_DECLS
+
+#define RHYTHMDB_TYPE_QUERY_RESULT_LIST         (rhythmdb_query_result_list_get_type ())
+#define RHYTHMDB_QUERY_RESULT_LIST(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), RHYTHMDB_TYPE_QUERY_RESULT_LIST, RhythmDBQueryResultList))
+#define RHYTHMDB_QUERY_RESULT_LIST_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), RHYTHMDB_TYPE_QUERY_RESULT_LIST, RhythmDBQueryResultListClass))
+#define RHYTHMDB_IS_QUERY_RESULT_LIST(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), RHYTHMDB_TYPE_QUERY_RESULT_LIST))
+#define RHYTHMDB_IS_QUERY_RESULT_LIST_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), RHYTHMDB_TYPE_QUERY_RESULT_LIST))
+#define RHYTHMDB_QUERY_RESULT_LIST_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RHYTHMDB_TYPE_QUERY_RESULT_LIST, RhythmDBQueryResultListClass))
+
+GType rhythmdb_query_result_list_limit_type_get_type (void);
+#define RHYTHMDB_TYPE_QUERY_RESULT_LIST_LIMIT_TYPE (rhythmdb_query_result_list_limit_type_get_type ())
+
+typedef struct _RhythmDBQueryResultList RhythmDBQueryResultList;
+typedef struct _RhythmDBQueryResultListClass RhythmDBQueryResultListClass;
+typedef struct _RhythmDBQueryResultListPrivate RhythmDBQueryResultListPrivate;
+
+struct _RhythmDBQueryResultList
+{
+	GObject parent;
+	RhythmDBQueryResultListPrivate *priv;
+};
+
+struct _RhythmDBQueryResultListClass
+{
+	GObjectClass parent;
+
+	/* signals */
+	void	(*complete)		(RhythmDBQueryResultList *list);
+};
+
+GType			rhythmdb_query_result_list_get_type		(void);
+
+RhythmDBQueryResultList *rhythmdb_query_result_list_new 		(void);
+
+GList 			*rhythmdb_query_result_list_get_results 	(RhythmDBQueryResultList *list);
+
+G_END_DECLS
+
+#endif /* RHYTHMDB_QUERY_RESULT_LIST_H */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]