/* rygel-tracker-cleanup-query.c generated by valac 0.42.2, 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.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 <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_;
	GTask* _async_result;
	GAsyncReadyCallback _callback_;
	gboolean _task_complete_;
	RygelTrackerCleanupQuery* self;
	TrackerSparqlConnection* resources;
	gchar* str;
	gchar* _tmp0_;
	GError * _inner_error_;
};


static gint RygelTrackerCleanupQuery_private_offset;
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;
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_async_ready_wrapper (GObject *source_object,
                                                                   GAsyncResult *res,
                                                                   void *user_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);


static inline gpointer
rygel_tracker_cleanup_query_get_instance_private (RygelTrackerCleanupQuery* self)
{
	return G_STRUCT_MEMBER_P (self, RygelTrackerCleanupQuery_private_offset);
}


RygelTrackerCleanupQuery*
rygel_tracker_cleanup_query_construct (GType object_type,
                                       const gchar* category)
{
	RygelTrackerCleanupQuery* self = NULL;
	RygelTrackerQueryTriplets* triplets = NULL;
	RygelTrackerQueryTriplets* _tmp0_;
	RygelTrackerQueryTriplet* _tmp1_;
	RygelTrackerQueryTriplet* _tmp2_;
	gchar* _tmp3_;
	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_ = g_strdup (category);
	_g_free0 (self->priv->category);
	self->priv->category = _tmp3_;
	_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 void
rygel_tracker_cleanup_query_real_execute_async_ready_wrapper (GObject *source_object,
                                                              GAsyncResult *res,
                                                              void *user_data)
{
	RygelTrackerCleanupQueryExecuteData* _task_data_;
	_task_data_ = g_task_get_task_data (G_TASK (res));
	if (_task_data_->_callback_ != NULL) {
		_task_data_->_callback_ (source_object, res, user_data);
	}
	_task_data_->_task_complete_ = TRUE;
}


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_;
	TrackerSparqlConnection* _tmp1_;
	self = (RygelTrackerCleanupQuery*) base;
	_data_ = g_slice_new0 (RygelTrackerCleanupQueryExecuteData);
	_data_->_callback_ = _callback_;
	_data_->_async_result = g_task_new (NULL, NULL, rygel_tracker_cleanup_query_real_execute_async_ready_wrapper, _user_data_);
	if (_callback_ == NULL) {
		_data_->_task_complete_ = TRUE;
	}
	g_task_set_task_data (_data_->_async_result, _data_, rygel_tracker_cleanup_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_cleanup_query_real_execute_co (_data_);
}


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


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_;
	_data_->_task_complete_ = TRUE;
	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_ = rygel_tracker_query_to_string ((RygelTrackerQuery*) _data_->self);
	_data_->str = _data_->_tmp0_;
	g_debug ("rygel-tracker-cleanup-query.vala:48: Executing SPARQL query: %s", _data_->str);
	_data_->_state_ = 1;
	tracker_sparql_connection_update_async (_data_->resources, _data_->str, G_PRIORITY_DEFAULT, NULL, rygel_tracker_cleanup_query_execute_ready, _data_);
	return FALSE;
	_state_1:
	tracker_sparql_connection_update_finish (_data_->resources, _data_->_res_, &_data_->_inner_error_);
	if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error_);
		_g_free0 (_data_->str);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_g_free0 (_data_->str);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (_data_->_task_complete_ != TRUE) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	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_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	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);
	((RygelTrackerQueryClass *) klass)->finalize = rygel_tracker_cleanup_query_finalize;
	g_type_class_adjust_private_offset (klass, &RygelTrackerCleanupQuery_private_offset);
	((RygelTrackerQueryClass *) klass)->execute = (void (*) (RygelTrackerQuery *, TrackerSparqlConnection*, GAsyncReadyCallback, gpointer)) rygel_tracker_cleanup_query_real_execute;
	((RygelTrackerQueryClass *) klass)->execute_finish = (void (*) (RygelTrackerQuery *, GAsyncResult*, GError**)) rygel_tracker_cleanup_query_real_execute_finish;
	((RygelTrackerQueryClass *) klass)->to_string = (gchar* (*) (RygelTrackerQuery *)) rygel_tracker_cleanup_query_real_to_string;
}


static void
rygel_tracker_cleanup_query_instance_init (RygelTrackerCleanupQuery * self)
{
	self->priv = rygel_tracker_cleanup_query_get_instance_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);
		RygelTrackerCleanupQuery_private_offset = g_type_add_instance_private (rygel_tracker_cleanup_query_type_id, sizeof (RygelTrackerCleanupQueryPrivate));
		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;
}



