/* tracker-array-cursor.c generated by valac 0.9.5.4-19a5, the Vala compiler
 * generated from tracker-array-cursor.vala, do not modify */

/*
 * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
 *
 * This library 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.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <glib.h>
#include <glib-object.h>
#include <tracker-sparql.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>


#define TRACKER_BUS_TYPE_ARRAY_CURSOR (tracker_bus_array_cursor_get_type ())
#define TRACKER_BUS_ARRAY_CURSOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TRACKER_BUS_TYPE_ARRAY_CURSOR, TrackerBusArrayCursor))
#define TRACKER_BUS_ARRAY_CURSOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TRACKER_BUS_TYPE_ARRAY_CURSOR, TrackerBusArrayCursorClass))
#define TRACKER_BUS_IS_ARRAY_CURSOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TRACKER_BUS_TYPE_ARRAY_CURSOR))
#define TRACKER_BUS_IS_ARRAY_CURSOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TRACKER_BUS_TYPE_ARRAY_CURSOR))
#define TRACKER_BUS_ARRAY_CURSOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TRACKER_BUS_TYPE_ARRAY_CURSOR, TrackerBusArrayCursorClass))

typedef struct _TrackerBusArrayCursor TrackerBusArrayCursor;
typedef struct _TrackerBusArrayCursorClass TrackerBusArrayCursorClass;
typedef struct _TrackerBusArrayCursorPrivate TrackerBusArrayCursorPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _TrackerBusArrayCursorNextAsyncData TrackerBusArrayCursorNextAsyncData;

struct _TrackerBusArrayCursor {
	TrackerSparqlCursor parent_instance;
	TrackerBusArrayCursorPrivate * priv;
};

struct _TrackerBusArrayCursorClass {
	TrackerSparqlCursorClass parent_class;
};

struct _TrackerBusArrayCursorPrivate {
	gint rows;
	gint current_row;
	char** results;
	gint results_length1;
	gint results_length2;
	gint cols;
};

struct _TrackerBusArrayCursorNextAsyncData {
	int _state_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	TrackerBusArrayCursor* self;
	GCancellable* cancellable;
	gboolean result;
	gboolean _tmp0_;
	GError * _inner_error_;
};


static gpointer tracker_bus_array_cursor_parent_class = NULL;

GType tracker_bus_array_cursor_get_type (void) G_GNUC_CONST;
#define TRACKER_BUS_ARRAY_CURSOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRACKER_BUS_TYPE_ARRAY_CURSOR, TrackerBusArrayCursorPrivate))
enum  {
	TRACKER_BUS_ARRAY_CURSOR_DUMMY_PROPERTY,
	TRACKER_BUS_ARRAY_CURSOR_N_COLUMNS
};
TrackerBusArrayCursor* tracker_bus_array_cursor_new (char** results, int results_length1, int results_length2, gint rows, gint cols);
TrackerBusArrayCursor* tracker_bus_array_cursor_construct (GType object_type, char** results, int results_length1, int results_length2, gint rows, gint cols);
static const char* tracker_bus_array_cursor_real_get_string (TrackerSparqlCursor* base, gint column, glong* length);
static gboolean tracker_bus_array_cursor_real_next (TrackerSparqlCursor* base, GCancellable* cancellable, GError** error);
static void tracker_bus_array_cursor_real_next_async_data_free (gpointer _data);
static void tracker_bus_array_cursor_real_next_async (TrackerSparqlCursor* base, GCancellable* cancellable, GAsyncReadyCallback _callback_, gpointer _user_data_);
static void tracker_bus_array_cursor_next_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
static gboolean tracker_bus_array_cursor_real_next_async_co (TrackerBusArrayCursorNextAsyncData* data);
static void tracker_bus_array_cursor_real_rewind (TrackerSparqlCursor* base);
static void tracker_bus_array_cursor_finalize (GObject* obj);
static void tracker_bus_array_cursor_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);



TrackerBusArrayCursor* tracker_bus_array_cursor_construct (GType object_type, char** results, int results_length1, int results_length2, gint rows, gint cols) {
	TrackerBusArrayCursor * self;
	char** _tmp0_;
	char** _tmp1_;
	self = (TrackerBusArrayCursor*) tracker_sparql_cursor_construct (object_type);
	self->priv->rows = rows;
	self->priv->cols = cols;
	self->priv->results = (_tmp1_ = (_tmp0_ = results, results = NULL, _tmp0_), self->priv->results = (_vala_array_free (self->priv->results, self->priv->results_length1 * self->priv->results_length2, (GDestroyNotify) g_free), NULL), self->priv->results_length1 = results_length1, self->priv->results_length2 = results_length2, _tmp1_);
	results = (_vala_array_free (results, results_length1 * results_length2, (GDestroyNotify) g_free), NULL);
	return self;
}


TrackerBusArrayCursor* tracker_bus_array_cursor_new (char** results, int results_length1, int results_length2, gint rows, gint cols) {
	return tracker_bus_array_cursor_construct (TRACKER_BUS_TYPE_ARRAY_CURSOR, results, results_length1, results_length2, rows, cols);
}


static glong string_get_length (const char* self) {
	glong result;
	g_return_val_if_fail (self != NULL, 0L);
	result = g_utf8_strlen (self, -1);
	return result;
}


static const char* tracker_bus_array_cursor_real_get_string (TrackerSparqlCursor* base, gint column, glong* length) {
	TrackerBusArrayCursor * self;
	const char* result = NULL;
	const char* str;
	self = (TrackerBusArrayCursor*) base;
	g_return_val_if_fail (self->priv->current_row >= 0, NULL);
	str = NULL;
	str = self->priv->results[(self->priv->current_row * self->priv->results_length2) + column];
	if ((length) != NULL) {
		*length = string_get_length (str);
	}
	result = str;
	return result;
}


static gboolean tracker_bus_array_cursor_real_next (TrackerSparqlCursor* base, GCancellable* cancellable, GError** error) {
	TrackerBusArrayCursor * self;
	gboolean result = FALSE;
	self = (TrackerBusArrayCursor*) base;
	if (self->priv->current_row >= (self->priv->rows - 1)) {
		result = FALSE;
		return result;
	}
	self->priv->current_row++;
	result = TRUE;
	return result;
}


static void tracker_bus_array_cursor_real_next_async_data_free (gpointer _data) {
	TrackerBusArrayCursorNextAsyncData* data;
	data = _data;
	_g_object_unref0 (data->cancellable);
	g_object_unref (data->self);
	g_slice_free (TrackerBusArrayCursorNextAsyncData, data);
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static void tracker_bus_array_cursor_real_next_async (TrackerSparqlCursor* base, GCancellable* cancellable, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	TrackerBusArrayCursor * self;
	TrackerBusArrayCursorNextAsyncData* _data_;
	self = (TrackerBusArrayCursor*) base;
	_data_ = g_slice_new0 (TrackerBusArrayCursorNextAsyncData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, tracker_bus_array_cursor_real_next_async);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, tracker_bus_array_cursor_real_next_async_data_free);
	_data_->self = g_object_ref (self);
	_data_->cancellable = _g_object_ref0 (cancellable);
	tracker_bus_array_cursor_real_next_async_co (_data_);
}


static gboolean tracker_bus_array_cursor_real_next_finish (TrackerSparqlCursor* base, GAsyncResult* _res_, GError** error) {
	gboolean result;
	TrackerBusArrayCursorNextAsyncData* _data_;
	if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (_res_), error)) {
		return FALSE;
	}
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
	result = _data_->result;
	return result;
}


static void tracker_bus_array_cursor_next_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	TrackerBusArrayCursorNextAsyncData* data;
	data = _user_data_;
	data->_res_ = _res_;
	tracker_bus_array_cursor_real_next_async_co (data);
}


static gboolean tracker_bus_array_cursor_real_next_async_co (TrackerBusArrayCursorNextAsyncData* data) {
	switch (data->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		data->_tmp0_ = tracker_sparql_cursor_next ((TrackerSparqlCursor*) data->self, data->cancellable, &data->_inner_error_);
		if (data->_inner_error_ != NULL) {
			g_simple_async_result_set_from_error (data->_async_result, data->_inner_error_);
			g_error_free (data->_inner_error_);
			{
				if (data->_state_ == 0) {
					g_simple_async_result_complete_in_idle (data->_async_result);
				} else {
					g_simple_async_result_complete (data->_async_result);
				}
				g_object_unref (data->_async_result);
				return FALSE;
			}
		}
		data->result = data->_tmp0_;
		{
			if (data->_state_ == 0) {
				g_simple_async_result_complete_in_idle (data->_async_result);
			} else {
				g_simple_async_result_complete (data->_async_result);
			}
			g_object_unref (data->_async_result);
			return FALSE;
		}
	}
	{
		if (data->_state_ == 0) {
			g_simple_async_result_complete_in_idle (data->_async_result);
		} else {
			g_simple_async_result_complete (data->_async_result);
		}
		g_object_unref (data->_async_result);
		return FALSE;
	}
}


static void tracker_bus_array_cursor_real_rewind (TrackerSparqlCursor* base) {
	TrackerBusArrayCursor * self;
	self = (TrackerBusArrayCursor*) base;
	self->priv->current_row = 0;
}


static gint tracker_bus_array_cursor_real_get_n_columns (TrackerSparqlCursor* base) {
	gint result;
	TrackerBusArrayCursor* self;
	self = (TrackerBusArrayCursor*) base;
	result = self->priv->cols;
	return result;
}


static void tracker_bus_array_cursor_class_init (TrackerBusArrayCursorClass * klass) {
	tracker_bus_array_cursor_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (TrackerBusArrayCursorPrivate));
	TRACKER_SPARQL_CURSOR_CLASS (klass)->get_string = tracker_bus_array_cursor_real_get_string;
	TRACKER_SPARQL_CURSOR_CLASS (klass)->next = tracker_bus_array_cursor_real_next;
	TRACKER_SPARQL_CURSOR_CLASS (klass)->next_async = tracker_bus_array_cursor_real_next_async;
	TRACKER_SPARQL_CURSOR_CLASS (klass)->next_finish = tracker_bus_array_cursor_real_next_finish;
	TRACKER_SPARQL_CURSOR_CLASS (klass)->rewind = tracker_bus_array_cursor_real_rewind;
	TRACKER_SPARQL_CURSOR_CLASS (klass)->get_n_columns = tracker_bus_array_cursor_real_get_n_columns;
	G_OBJECT_CLASS (klass)->get_property = tracker_bus_array_cursor_get_property;
	G_OBJECT_CLASS (klass)->finalize = tracker_bus_array_cursor_finalize;
	g_object_class_override_property (G_OBJECT_CLASS (klass), TRACKER_BUS_ARRAY_CURSOR_N_COLUMNS, "n-columns");
}


static void tracker_bus_array_cursor_instance_init (TrackerBusArrayCursor * self) {
	self->priv = TRACKER_BUS_ARRAY_CURSOR_GET_PRIVATE (self);
	self->priv->current_row = -1;
}


static void tracker_bus_array_cursor_finalize (GObject* obj) {
	TrackerBusArrayCursor * self;
	self = TRACKER_BUS_ARRAY_CURSOR (obj);
	self->priv->results = (_vala_array_free (self->priv->results, self->priv->results_length1 * self->priv->results_length2, (GDestroyNotify) g_free), NULL);
	G_OBJECT_CLASS (tracker_bus_array_cursor_parent_class)->finalize (obj);
}


GType tracker_bus_array_cursor_get_type (void) {
	static volatile gsize tracker_bus_array_cursor_type_id__volatile = 0;
	if (g_once_init_enter (&tracker_bus_array_cursor_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (TrackerBusArrayCursorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) tracker_bus_array_cursor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (TrackerBusArrayCursor), 0, (GInstanceInitFunc) tracker_bus_array_cursor_instance_init, NULL };
		GType tracker_bus_array_cursor_type_id;
		tracker_bus_array_cursor_type_id = g_type_register_static (TRACKER_SPARQL_TYPE_CURSOR, "TrackerBusArrayCursor", &g_define_type_info, 0);
		g_once_init_leave (&tracker_bus_array_cursor_type_id__volatile, tracker_bus_array_cursor_type_id);
	}
	return tracker_bus_array_cursor_type_id__volatile;
}


static void tracker_bus_array_cursor_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	TrackerBusArrayCursor * self;
	self = TRACKER_BUS_ARRAY_CURSOR (object);
	switch (property_id) {
		case TRACKER_BUS_ARRAY_CURSOR_N_COLUMNS:
		g_value_set_int (value, tracker_sparql_cursor_get_n_columns ((TrackerSparqlCursor*) self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}




