/* rygel-tracker-deletion-query.c generated by valac 0.56.1, the Vala compiler
 * generated from rygel-tracker-deletion-query.vala, do not modify */

/*
 * Copyright (C) 2010-2012 Nokia Corporation.
 *
 * Author: Zeeshan Ali <zeenix@gmail.com>
 *         Jens Georg <jensg@openismus.com>
 *
 * This file is part of Rygel.
 *
 * Rygel 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.
 *
 * Rygel 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 "rygel-tracker3.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include <libtracker-sparql/tracker-sparql.h>
#include <gee.h>
#include <glib-object.h>

#define _g_free0(var) (var = (g_free (var), NULL))
#define _rygel_tracker_query_triplet_unref0(var) ((var == NULL) ? NULL : (var = (rygel_tracker_query_triplet_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _rygel_tracker_query_unref0(var) ((var == NULL) ? NULL : (var = (rygel_tracker_query_unref (var), NULL)))
typedef struct _RygelTrackerDeletionQueryExecuteData RygelTrackerDeletionQueryExecuteData;

struct _RygelTrackerDeletionQueryPrivate {
	gchar* id;
};

struct _RygelTrackerDeletionQueryExecuteData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	RygelTrackerDeletionQuery* self;
	TrackerSparqlConnection* resources;
	gchar* str;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	GError* _inner_error0_;
};

static gint RygelTrackerDeletionQuery_private_offset;
static gpointer rygel_tracker_deletion_query_parent_class = NULL;

static void rygel_tracker_deletion_query_real_execute_data_free (gpointer _data);
static void rygel_tracker_deletion_query_real_execute (RygelTrackerQuery* base,
                                                TrackerSparqlConnection* resources,
                                                GAsyncReadyCallback _callback_,
                                                gpointer _user_data_);
static gboolean rygel_tracker_deletion_query_real_execute_co (RygelTrackerDeletionQueryExecuteData* _data_);
static void rygel_tracker_deletion_query_execute_ready (GObject* source_object,
                                                 GAsyncResult* _res_,
                                                 gpointer _user_data_);
static gchar* rygel_tracker_deletion_query_real_to_string (RygelTrackerQuery* base);
static void rygel_tracker_deletion_query_finalize (RygelTrackerQuery * obj);
static GType rygel_tracker_deletion_query_get_type_once (void);

static inline gpointer
rygel_tracker_deletion_query_get_instance_private (RygelTrackerDeletionQuery* self)
{
	return G_STRUCT_MEMBER_P (self, RygelTrackerDeletionQuery_private_offset);
}

RygelTrackerDeletionQuery*
rygel_tracker_deletion_query_construct (GType object_type,
                                        const gchar* id)
{
	RygelTrackerDeletionQuery* self = NULL;
	RygelTrackerQueryTriplets* triplets = NULL;
	RygelTrackerQueryTriplets* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	RygelTrackerQueryTriplet* _tmp5_;
	RygelTrackerQueryTriplet* _tmp6_;
	gchar* _tmp7_;
	g_return_val_if_fail (id != NULL, NULL);
	_tmp0_ = rygel_tracker_query_triplets_new ();
	triplets = _tmp0_;
	_tmp1_ = g_strconcat ("<", id, NULL);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_strconcat (_tmp2_, ">", NULL);
	_tmp4_ = _tmp3_;
	_tmp5_ = rygel_tracker_query_triplet_new (_tmp4_, "a", "rdfs:Resource");
	_tmp6_ = _tmp5_;
	gee_abstract_collection_add ((GeeAbstractCollection*) triplets, _tmp6_);
	_rygel_tracker_query_triplet_unref0 (_tmp6_);
	_g_free0 (_tmp4_);
	_g_free0 (_tmp2_);
	self = (RygelTrackerDeletionQuery*) rygel_tracker_query_construct (object_type, triplets);
	_tmp7_ = g_strdup (id);
	_g_free0 (self->priv->id);
	self->priv->id = _tmp7_;
	_g_object_unref0 (triplets);
	return self;
}

RygelTrackerDeletionQuery*
rygel_tracker_deletion_query_new (const gchar* id)
{
	return rygel_tracker_deletion_query_construct (RYGEL_TRACKER_TYPE_DELETION_QUERY, id);
}

static void
rygel_tracker_deletion_query_real_execute_data_free (gpointer _data)
{
	RygelTrackerDeletionQueryExecuteData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->resources);
	_rygel_tracker_query_unref0 (_data_->self);
	g_slice_free (RygelTrackerDeletionQueryExecuteData, _data_);
}

static gpointer
_rygel_tracker_query_ref0 (gpointer self)
{
	return self ? rygel_tracker_query_ref (self) : NULL;
}

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

static void
rygel_tracker_deletion_query_real_execute (RygelTrackerQuery* base,
                                           TrackerSparqlConnection* resources,
                                           GAsyncReadyCallback _callback_,
                                           gpointer _user_data_)
{
	RygelTrackerDeletionQuery * self;
	RygelTrackerDeletionQueryExecuteData* _data_;
	RygelTrackerDeletionQuery* _tmp0_;
	TrackerSparqlConnection* _tmp1_;
	g_return_if_fail (resources != NULL);
	self = (RygelTrackerDeletionQuery*) base;
	_data_ = g_slice_new0 (RygelTrackerDeletionQueryExecuteData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, rygel_tracker_deletion_query_real_execute_data_free);
	_tmp0_ = _rygel_tracker_query_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_object_ref0 (resources);
	_g_object_unref0 (_data_->resources);
	_data_->resources = _tmp1_;
	rygel_tracker_deletion_query_real_execute_co (_data_);
}

static void
rygel_tracker_deletion_query_execute_finish (RygelTrackerQuery* base,
                                             GAsyncResult* _res_,
                                             GError** error)
{
	RygelTrackerDeletionQueryExecuteData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

static void
rygel_tracker_deletion_query_execute_ready (GObject* source_object,
                                            GAsyncResult* _res_,
                                            gpointer _user_data_)
{
	RygelTrackerDeletionQueryExecuteData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	rygel_tracker_deletion_query_real_execute_co (_data_);
}

static gboolean
rygel_tracker_deletion_query_real_execute_co (RygelTrackerDeletionQueryExecuteData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = rygel_tracker_query_to_string ((RygelTrackerQuery*) _data_->self);
	_data_->str = _data_->_tmp0_;
	g_debug ("rygel-tracker-deletion-query.vala:49: Executing SPARQL query: %s", _data_->str);
	_data_->_state_ = 1;
	tracker_sparql_connection_update_async (_data_->resources, _data_->str, NULL, rygel_tracker_deletion_query_execute_ready, _data_);
	return FALSE;
	_state_1:
	tracker_sparql_connection_update_finish (_data_->resources, _data_->_res_, &_data_->_inner_error0_);
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		_g_free0 (_data_->str);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_tmp1_ = _data_->self->priv->id;
	g_debug ("rygel-tracker-deletion-query.vala:53: Deleted item '%s' from Tracker s" \
"tore", _data_->_tmp1_);
	_g_free0 (_data_->str);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static gchar*
rygel_tracker_deletion_query_real_to_string (RygelTrackerQuery* base)
{
	RygelTrackerDeletionQuery * self;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* result;
	self = (RygelTrackerDeletionQuery*) base;
	_tmp0_ = RYGEL_TRACKER_QUERY_CLASS (rygel_tracker_deletion_query_parent_class)->to_string (G_TYPE_CHECK_INSTANCE_CAST (self, RYGEL_TRACKER_TYPE_QUERY, RygelTrackerQuery));
	_tmp1_ = _tmp0_;
	_tmp2_ = g_strconcat ("DELETE { ", _tmp1_, NULL);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_strconcat (_tmp3_, " }", NULL);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	_g_free0 (_tmp1_);
	result = _tmp5_;
	return result;
}

static void
rygel_tracker_deletion_query_class_init (RygelTrackerDeletionQueryClass * klass,
                                         gpointer klass_data)
{
	rygel_tracker_deletion_query_parent_class = g_type_class_peek_parent (klass);
	((RygelTrackerQueryClass *) klass)->finalize = rygel_tracker_deletion_query_finalize;
	g_type_class_adjust_private_offset (klass, &RygelTrackerDeletionQuery_private_offset);
	((RygelTrackerQueryClass *) klass)->execute = (void (*) (RygelTrackerQuery*, TrackerSparqlConnection*, GAsyncReadyCallback, gpointer)) rygel_tracker_deletion_query_real_execute;
	((RygelTrackerQueryClass *) klass)->execute_finish = (void (*) (RygelTrackerQuery*, GAsyncResult*, GError**)) rygel_tracker_deletion_query_execute_finish;
	((RygelTrackerQueryClass *) klass)->to_string = (gchar* (*) (RygelTrackerQuery*)) rygel_tracker_deletion_query_real_to_string;
}

static void
rygel_tracker_deletion_query_instance_init (RygelTrackerDeletionQuery * self,
                                            gpointer klass)
{
	self->priv = rygel_tracker_deletion_query_get_instance_private (self);
}

static void
rygel_tracker_deletion_query_finalize (RygelTrackerQuery * obj)
{
	RygelTrackerDeletionQuery * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TRACKER_TYPE_DELETION_QUERY, RygelTrackerDeletionQuery);
	_g_free0 (self->priv->id);
	RYGEL_TRACKER_QUERY_CLASS (rygel_tracker_deletion_query_parent_class)->finalize (obj);
}

/**
 * Represents Tracker SPARQL Deletion query
 */
static GType
rygel_tracker_deletion_query_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerDeletionQueryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_deletion_query_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerDeletionQuery), 0, (GInstanceInitFunc) rygel_tracker_deletion_query_instance_init, NULL };
	GType rygel_tracker_deletion_query_type_id;
	rygel_tracker_deletion_query_type_id = g_type_register_static (RYGEL_TRACKER_TYPE_QUERY, "RygelTrackerDeletionQuery", &g_define_type_info, 0);
	RygelTrackerDeletionQuery_private_offset = g_type_add_instance_private (rygel_tracker_deletion_query_type_id, sizeof (RygelTrackerDeletionQueryPrivate));
	return rygel_tracker_deletion_query_type_id;
}

GType
rygel_tracker_deletion_query_get_type (void)
{
	static volatile gsize rygel_tracker_deletion_query_type_id__once = 0;
	if (g_once_init_enter (&rygel_tracker_deletion_query_type_id__once)) {
		GType rygel_tracker_deletion_query_type_id;
		rygel_tracker_deletion_query_type_id = rygel_tracker_deletion_query_get_type_once ();
		g_once_init_leave (&rygel_tracker_deletion_query_type_id__once, rygel_tracker_deletion_query_type_id);
	}
	return rygel_tracker_deletion_query_type_id__once;
}

