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

/*
 * Copyright (C) 2011-2012 Nokia Corporation.
 *
 * Author: 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 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 program; 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 <gio/gio.h>
#include <libtracker-sparql/tracker-sparql.h>
#include <stdlib.h>
#include <string.h>
#include <gee.h>


#define RYGEL_TRACKER_TYPE_QUERY (rygel_tracker_query_get_type ())
#define RYGEL_TRACKER_QUERY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_QUERY, RygelTrackerQuery))
#define RYGEL_TRACKER_QUERY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TRACKER_TYPE_QUERY, RygelTrackerQueryClass))
#define RYGEL_TRACKER_IS_QUERY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_QUERY))
#define RYGEL_TRACKER_IS_QUERY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TRACKER_TYPE_QUERY))
#define RYGEL_TRACKER_QUERY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TRACKER_TYPE_QUERY, RygelTrackerQueryClass))

typedef struct _RygelTrackerQuery RygelTrackerQuery;
typedef struct _RygelTrackerQueryClass RygelTrackerQueryClass;
typedef struct _RygelTrackerQueryPrivate RygelTrackerQueryPrivate;

#define RYGEL_TRACKER_TYPE_QUERY_TRIPLETS (rygel_tracker_query_triplets_get_type ())
#define RYGEL_TRACKER_QUERY_TRIPLETS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_QUERY_TRIPLETS, RygelTrackerQueryTriplets))
#define RYGEL_TRACKER_QUERY_TRIPLETS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TRACKER_TYPE_QUERY_TRIPLETS, RygelTrackerQueryTripletsClass))
#define RYGEL_TRACKER_IS_QUERY_TRIPLETS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_QUERY_TRIPLETS))
#define RYGEL_TRACKER_IS_QUERY_TRIPLETS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TRACKER_TYPE_QUERY_TRIPLETS))
#define RYGEL_TRACKER_QUERY_TRIPLETS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TRACKER_TYPE_QUERY_TRIPLETS, RygelTrackerQueryTripletsClass))

typedef struct _RygelTrackerQueryTriplets RygelTrackerQueryTriplets;
typedef struct _RygelTrackerQueryTripletsClass RygelTrackerQueryTripletsClass;

#define RYGEL_TRACKER_TYPE_CLEANUP_QUERY (rygel_tracker_cleanup_query_get_type ())
#define RYGEL_TRACKER_CLEANUP_QUERY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_CLEANUP_QUERY, RygelTrackerCleanupQuery))
#define RYGEL_TRACKER_CLEANUP_QUERY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TRACKER_TYPE_CLEANUP_QUERY, RygelTrackerCleanupQueryClass))
#define RYGEL_TRACKER_IS_CLEANUP_QUERY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_CLEANUP_QUERY))
#define RYGEL_TRACKER_IS_CLEANUP_QUERY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TRACKER_TYPE_CLEANUP_QUERY))
#define RYGEL_TRACKER_CLEANUP_QUERY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TRACKER_TYPE_CLEANUP_QUERY, RygelTrackerCleanupQueryClass))

typedef struct _RygelTrackerCleanupQuery RygelTrackerCleanupQuery;
typedef struct _RygelTrackerCleanupQueryClass RygelTrackerCleanupQueryClass;
typedef struct _RygelTrackerCleanupQueryPrivate RygelTrackerCleanupQueryPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))

#define RYGEL_TRACKER_TYPE_QUERY_TRIPLET (rygel_tracker_query_triplet_get_type ())
#define RYGEL_TRACKER_QUERY_TRIPLET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TRACKER_TYPE_QUERY_TRIPLET, RygelTrackerQueryTriplet))
#define RYGEL_TRACKER_QUERY_TRIPLET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TRACKER_TYPE_QUERY_TRIPLET, RygelTrackerQueryTripletClass))
#define RYGEL_TRACKER_IS_QUERY_TRIPLET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TRACKER_TYPE_QUERY_TRIPLET))
#define RYGEL_TRACKER_IS_QUERY_TRIPLET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TRACKER_TYPE_QUERY_TRIPLET))
#define RYGEL_TRACKER_QUERY_TRIPLET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TRACKER_TYPE_QUERY_TRIPLET, RygelTrackerQueryTripletClass))

typedef struct _RygelTrackerQueryTriplet RygelTrackerQueryTriplet;
typedef struct _RygelTrackerQueryTripletClass RygelTrackerQueryTripletClass;
#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 _RygelTrackerCleanupQueryExecuteData RygelTrackerCleanupQueryExecuteData;
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

struct _RygelTrackerQuery {
	GTypeInstance parent_instance;
	volatile int ref_count;
	RygelTrackerQueryPrivate * priv;
	RygelTrackerQueryTriplets* triplets;
};

struct _RygelTrackerQueryClass {
	GTypeClass parent_class;
	void (*finalize) (RygelTrackerQuery *self);
	void (*execute) (RygelTrackerQuery* self, TrackerSparqlConnection* resources, GAsyncReadyCallback _callback_, gpointer _user_data_);
	void (*execute_finish) (RygelTrackerQuery* self, GAsyncResult* _res_, GError** error);
	gchar* (*to_string) (RygelTrackerQuery* self);
};

struct _RygelTrackerCleanupQuery {
	RygelTrackerQuery parent_instance;
	RygelTrackerCleanupQueryPrivate * priv;
};

struct _RygelTrackerCleanupQueryClass {
	RygelTrackerQueryClass parent_class;
};

struct _RygelTrackerCleanupQueryPrivate {
	gchar* category;
};

struct _RygelTrackerCleanupQueryExecuteData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	RygelTrackerCleanupQuery* self;
	TrackerSparqlConnection* resources;
	gchar* str;
	gchar* _tmp0_;
	TrackerSparqlConnection* _tmp1_;
	GError * _inner_error_;
};


static gpointer rygel_tracker_cleanup_query_parent_class = NULL;

gpointer rygel_tracker_query_ref (gpointer instance);
void rygel_tracker_query_unref (gpointer instance);
GParamSpec* rygel_tracker_param_spec_query (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void rygel_tracker_value_set_query (GValue* value, gpointer v_object);
void rygel_tracker_value_take_query (GValue* value, gpointer v_object);
gpointer rygel_tracker_value_get_query (const GValue* value);
GType rygel_tracker_query_get_type (void) G_GNUC_CONST;
GType rygel_tracker_query_triplets_get_type (void) G_GNUC_CONST;
GType rygel_tracker_cleanup_query_get_type (void) G_GNUC_CONST;
#define RYGEL_TRACKER_CLEANUP_QUERY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RYGEL_TRACKER_TYPE_CLEANUP_QUERY, RygelTrackerCleanupQueryPrivate))
enum  {
	RYGEL_TRACKER_CLEANUP_QUERY_DUMMY_PROPERTY
};
RygelTrackerCleanupQuery* rygel_tracker_cleanup_query_new (const gchar* category);
RygelTrackerCleanupQuery* rygel_tracker_cleanup_query_construct (GType object_type, const gchar* category);
RygelTrackerQueryTriplets* rygel_tracker_query_triplets_new (void);
RygelTrackerQueryTriplets* rygel_tracker_query_triplets_construct (GType object_type);
RygelTrackerQueryTriplet* rygel_tracker_query_triplet_new (const gchar* subject, const gchar* predicate, const gchar* obj);
RygelTrackerQueryTriplet* rygel_tracker_query_triplet_construct (GType object_type, const gchar* subject, const gchar* predicate, const gchar* obj);
gpointer rygel_tracker_query_triplet_ref (gpointer instance);
void rygel_tracker_query_triplet_unref (gpointer instance);
GParamSpec* rygel_tracker_param_spec_query_triplet (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void rygel_tracker_value_set_query_triplet (GValue* value, gpointer v_object);
void rygel_tracker_value_take_query_triplet (GValue* value, gpointer v_object);
gpointer rygel_tracker_value_get_query_triplet (const GValue* value);
GType rygel_tracker_query_triplet_get_type (void) G_GNUC_CONST;
RygelTrackerQuery* rygel_tracker_query_construct (GType object_type, RygelTrackerQueryTriplets* triplets);
static void rygel_tracker_cleanup_query_real_execute_data_free (gpointer _data);
static void rygel_tracker_cleanup_query_real_execute (RygelTrackerQuery* base, TrackerSparqlConnection* resources, GAsyncReadyCallback _callback_, gpointer _user_data_);
static gboolean rygel_tracker_cleanup_query_real_execute_co (RygelTrackerCleanupQueryExecuteData* _data_);
gchar* rygel_tracker_query_to_string (RygelTrackerQuery* self);
static void rygel_tracker_cleanup_query_execute_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
static gchar* rygel_tracker_cleanup_query_real_to_string (RygelTrackerQuery* base);
static void rygel_tracker_cleanup_query_finalize (RygelTrackerQuery* obj);


RygelTrackerCleanupQuery* rygel_tracker_cleanup_query_construct (GType object_type, const gchar* category) {
	RygelTrackerCleanupQuery* self = NULL;
	RygelTrackerQueryTriplets* triplets = NULL;
	RygelTrackerQueryTriplets* _tmp0_ = NULL;
	RygelTrackerQueryTriplet* _tmp1_ = NULL;
	RygelTrackerQueryTriplet* _tmp2_ = NULL;
	const gchar* _tmp3_ = NULL;
	gchar* _tmp4_ = NULL;
	g_return_val_if_fail (category != NULL, NULL);
	_tmp0_ = rygel_tracker_query_triplets_new ();
	triplets = _tmp0_;
	_tmp1_ = rygel_tracker_query_triplet_new ("?r", "a", "rdfs:Resource");
	_tmp2_ = _tmp1_;
	gee_abstract_collection_add ((GeeAbstractCollection*) triplets, _tmp2_);
	_rygel_tracker_query_triplet_unref0 (_tmp2_);
	self = (RygelTrackerCleanupQuery*) rygel_tracker_query_construct (object_type, triplets);
	_tmp3_ = category;
	_tmp4_ = g_strdup (_tmp3_);
	_g_free0 (self->priv->category);
	self->priv->category = _tmp4_;
	_g_object_unref0 (triplets);
	return self;
}


RygelTrackerCleanupQuery* rygel_tracker_cleanup_query_new (const gchar* category) {
	return rygel_tracker_cleanup_query_construct (RYGEL_TRACKER_TYPE_CLEANUP_QUERY, category);
}


static void rygel_tracker_cleanup_query_real_execute_data_free (gpointer _data) {
	RygelTrackerCleanupQueryExecuteData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->resources);
	_rygel_tracker_query_unref0 (_data_->self);
	g_slice_free (RygelTrackerCleanupQueryExecuteData, _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_cleanup_query_real_execute (RygelTrackerQuery* base, TrackerSparqlConnection* resources, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	RygelTrackerCleanupQuery * self;
	RygelTrackerCleanupQueryExecuteData* _data_;
	RygelTrackerCleanupQuery* _tmp0_ = NULL;
	TrackerSparqlConnection* _tmp1_ = NULL;
	TrackerSparqlConnection* _tmp2_ = NULL;
	self = (RygelTrackerCleanupQuery*) base;
	_data_ = g_slice_new0 (RygelTrackerCleanupQueryExecuteData);
	_data_->_async_result = g_simple_async_result_new (NULL, _callback_, _user_data_, rygel_tracker_cleanup_query_real_execute);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, rygel_tracker_cleanup_query_real_execute_data_free);
	_tmp0_ = _rygel_tracker_query_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = resources;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	_g_object_unref0 (_data_->resources);
	_data_->resources = _tmp2_;
	rygel_tracker_cleanup_query_real_execute_co (_data_);
}


static void rygel_tracker_cleanup_query_real_execute_finish (RygelTrackerQuery* base, GAsyncResult* _res_, GError** error) {
	RygelTrackerCleanupQueryExecuteData* _data_;
	if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (_res_), error)) {
		return;
	}
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static void rygel_tracker_cleanup_query_execute_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	RygelTrackerCleanupQueryExecuteData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	rygel_tracker_cleanup_query_real_execute_co (_data_);
}


static gboolean rygel_tracker_cleanup_query_real_execute_co (RygelTrackerCleanupQueryExecuteData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = NULL;
	_data_->_tmp0_ = rygel_tracker_query_to_string ((RygelTrackerQuery*) _data_->self);
	_data_->str = _data_->_tmp0_;
	g_debug ("rygel-tracker-cleanup-query.vala:47: Executing SPARQL query: %s", _data_->str);
	_data_->_tmp1_ = NULL;
	_data_->_tmp1_ = _data_->resources;
	_data_->_state_ = 1;
	tracker_sparql_connection_update_async (_data_->_tmp1_, _data_->str, G_PRIORITY_DEFAULT, NULL, rygel_tracker_cleanup_query_execute_ready, _data_);
	return FALSE;
	_state_1:
	tracker_sparql_connection_update_finish (_data_->_tmp1_, _data_->_res_, &_data_->_inner_error_);
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		if (((_data_->_inner_error_->domain == G_IO_ERROR) || (_data_->_inner_error_->domain == TRACKER_SPARQL_ERROR)) || (_data_->_inner_error_->domain == G_DBUS_ERROR)) {
			g_simple_async_result_set_from_error (_data_->_async_result, _data_->_inner_error_);
			g_error_free (_data_->_inner_error_);
			_g_free0 (_data_->str);
			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;
		} else {
			_g_free0 (_data_->str);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
			g_clear_error (&_data_->_inner_error_);
			return FALSE;
		}
	}
	_g_free0 (_data_->str);
	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 gchar* rygel_tracker_cleanup_query_real_to_string (RygelTrackerQuery* base) {
	RygelTrackerCleanupQuery * self;
	gchar* result = NULL;
	GString* _result_ = NULL;
	GString* _tmp0_ = NULL;
	gchar* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	const gchar* _tmp3_ = NULL;
	const gchar* _tmp4_ = NULL;
	gchar* _tmp5_ = NULL;
	self = (RygelTrackerCleanupQuery*) base;
	_tmp0_ = g_string_new ("");
	_result_ = _tmp0_;
	g_string_append (_result_, "DELETE {");
	_tmp1_ = RYGEL_TRACKER_QUERY_CLASS (rygel_tracker_cleanup_query_parent_class)->to_string (G_TYPE_CHECK_INSTANCE_CAST (self, RYGEL_TRACKER_TYPE_QUERY, RygelTrackerQuery));
	_tmp2_ = _tmp1_;
	g_string_append (_result_, _tmp2_);
	_g_free0 (_tmp2_);
	g_string_append (_result_, "} WHERE {");
	_tmp3_ = self->priv->category;
	g_string_append_printf (_result_, "?r a nie:DataObject, %s . ", _tmp3_);
	g_string_append (_result_, " ?r nie:generator \"rygel\". ");
	g_string_append (_result_, "FILTER(NOT EXISTS { ?r ");
	g_string_append (_result_, "a nfo:FileDataObject. })}");
	_tmp4_ = _result_->str;
	_tmp5_ = g_strdup (_tmp4_);
	result = _tmp5_;
	_g_string_free0 (_result_);
	return result;
}


static void rygel_tracker_cleanup_query_class_init (RygelTrackerCleanupQueryClass * klass) {
	rygel_tracker_cleanup_query_parent_class = g_type_class_peek_parent (klass);
	RYGEL_TRACKER_QUERY_CLASS (klass)->finalize = rygel_tracker_cleanup_query_finalize;
	g_type_class_add_private (klass, sizeof (RygelTrackerCleanupQueryPrivate));
	RYGEL_TRACKER_QUERY_CLASS (klass)->execute = rygel_tracker_cleanup_query_real_execute;
	RYGEL_TRACKER_QUERY_CLASS (klass)->execute_finish = rygel_tracker_cleanup_query_real_execute_finish;
	RYGEL_TRACKER_QUERY_CLASS (klass)->to_string = rygel_tracker_cleanup_query_real_to_string;
}


static void rygel_tracker_cleanup_query_instance_init (RygelTrackerCleanupQuery * self) {
	self->priv = RYGEL_TRACKER_CLEANUP_QUERY_GET_PRIVATE (self);
}


static void rygel_tracker_cleanup_query_finalize (RygelTrackerQuery* obj) {
	RygelTrackerCleanupQuery * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TRACKER_TYPE_CLEANUP_QUERY, RygelTrackerCleanupQuery);
	_g_free0 (self->priv->category);
	RYGEL_TRACKER_QUERY_CLASS (rygel_tracker_cleanup_query_parent_class)->finalize (obj);
}


/**
 * Represents Tracker SPARQL Deletion query
 */
GType rygel_tracker_cleanup_query_get_type (void) {
	static volatile gsize rygel_tracker_cleanup_query_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_tracker_cleanup_query_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerCleanupQueryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_cleanup_query_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerCleanupQuery), 0, (GInstanceInitFunc) rygel_tracker_cleanup_query_instance_init, NULL };
		GType rygel_tracker_cleanup_query_type_id;
		rygel_tracker_cleanup_query_type_id = g_type_register_static (RYGEL_TRACKER_TYPE_QUERY, "RygelTrackerCleanupQuery", &g_define_type_info, 0);
		g_once_init_leave (&rygel_tracker_cleanup_query_type_id__volatile, rygel_tracker_cleanup_query_type_id);
	}
	return rygel_tracker_cleanup_query_type_id__volatile;
}



