/* rygel-tracker-insertion-query.c generated by valac 0.56.1, the Vala compiler
 * generated from rygel-tracker-insertion-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 "rygel-server.h"
#include <gee.h>
#include <glib-object.h>

#define RYGEL_TRACKER_INSERTION_QUERY_TEMP_ID "x"
#define RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID "_:" RYGEL_TRACKER_INSERTION_QUERY_TEMP_ID
#define RYGEL_TRACKER_INSERTION_QUERY_MINER_SERVICE "org.freedesktop.Tracker3.Miner.Files.Index"
#define RYGEL_TRACKER_INSERTION_QUERY_MINER_PATH "/org/freedesktop/Tracker3/Miner/Files/Index"
#define RYGEL_TRACKER_INSERTION_QUERY_MINER_GRAPH "tracker:FileSystem"
#define RYGEL_TRACKER_INSERTION_QUERY_RESOURCE_ID_QUERY_TEMPLATE "SELECT ?resource WHERE { ?f a nie:DataObject; nie:url '%s'; nie:interp" \
"retedAs ?resource }"
#define RYGEL_TRACKER_INSERTION_QUERY_RESOURCE_NOT_BOUND_TEMPLATE "OPTIONAL { GRAPH Tracker:FileSystem { ?resource a nie:DataObject; nie:" \
"url '%s' }} " "FILTER (!bound(?resource))"

#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_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_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)))
#define _g_variant_iter_free0(var) ((var == NULL) ? NULL : (var = (g_variant_iter_free (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _RygelTrackerInsertionQueryExecuteData RygelTrackerInsertionQueryExecuteData;

struct _RygelTrackerInsertionQueryPrivate {
	gchar* uri;
};

struct _RygelTrackerInsertionQueryExecuteData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	RygelTrackerInsertionQuery* self;
	TrackerSparqlConnection* resources;
	gchar* str;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	GVariant* v;
	const gchar* _tmp2_;
	GVariant* _tmp3_;
	GVariantIter* iter1;
	GVariantIter* iter2;
	GVariantIter* iter3;
	gchar* key;
	GVariant* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	GVariant* _tmp7_;
	GVariantIter* _tmp8_;
	GVariantIter* _tmp9_;
	GVariantIter* _tmp10_;
	gboolean _tmp11_;
	GVariantIter* _tmp12_;
	GVariantIter* _tmp13_;
	gboolean _tmp14_;
	GVariantIter* _tmp15_;
	gchar* _tmp16_;
	gchar* _tmp17_;
	gboolean _tmp18_;
	const gchar* _tmp19_;
	gchar* _tmp20_;
	gchar* _tmp21_;
	const gchar* _tmp22_;
	TrackerSparqlCursor* cursor;
	gchar* _tmp23_;
	gchar* _tmp24_;
	TrackerSparqlCursor* _tmp25_;
	TrackerSparqlCursor* _tmp26_;
	gboolean _tmp27_;
	TrackerSparqlCursor* _tmp28_;
	TrackerSparqlCursor* _tmp29_;
	const gchar* _tmp30_;
	gchar* _tmp31_;
	GError* _error_;
	GError* _tmp32_;
	const gchar* _tmp33_;
	TrackerSparqlCursor* _tmp34_;
	GFile* file;
	const gchar* _tmp35_;
	GFile* _tmp36_;
	gboolean _tmp37_;
	GFile* _tmp38_;
	GFile* _tmp39_;
	RygelTrackerMinerFilesIndexIface* miner;
	RygelTrackerMinerFilesIndexIface* _tmp40_;
	RygelTrackerMinerFilesIndexIface* _tmp41_;
	const gchar* _tmp42_;
	const gchar* _tmp43_;
	GError* _inner_error0_;
};

static gint RygelTrackerInsertionQuery_private_offset;
static gpointer rygel_tracker_insertion_query_parent_class = NULL;

static void rygel_tracker_insertion_query_real_execute_data_free (gpointer _data);
static void rygel_tracker_insertion_query_real_execute (RygelTrackerQuery* base,
                                                 TrackerSparqlConnection* resources,
                                                 GAsyncReadyCallback _callback_,
                                                 gpointer _user_data_);
static gboolean rygel_tracker_insertion_query_real_execute_co (RygelTrackerInsertionQueryExecuteData* _data_);
static void rygel_tracker_insertion_query_execute_ready (GObject* source_object,
                                                  GAsyncResult* _res_,
                                                  gpointer _user_data_);
static gchar* rygel_tracker_insertion_query_get_resource_id_query (RygelTrackerInsertionQuery* self);
static gchar* rygel_tracker_insertion_query_real_to_string (RygelTrackerQuery* base);
static void rygel_tracker_insertion_query_finalize (RygelTrackerQuery * obj);
static GType rygel_tracker_insertion_query_get_type_once (void);

static inline gpointer
rygel_tracker_insertion_query_get_instance_private (RygelTrackerInsertionQuery* self)
{
	return G_STRUCT_MEMBER_P (self, RygelTrackerInsertionQuery_private_offset);
}

RygelTrackerInsertionQuery*
rygel_tracker_insertion_query_construct (GType object_type,
                                         RygelMediaFileItem* item,
                                         const gchar* category)
{
	RygelTrackerInsertionQuery* self = NULL;
	gchar* type = NULL;
	gchar* _tmp0_;
	GFile* file = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_;
	GFile* _tmp3_;
	GFile* _tmp4_;
	gchar* urn = NULL;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	GFile* _tmp9_;
	RygelTrackerQueryTriplets* triplets = NULL;
	RygelTrackerQueryTriplets* _tmp11_;
	RygelTrackerQueryTriplets* _tmp12_;
	RygelTrackerQueryTriplet* _tmp13_;
	RygelTrackerQueryTriplet* _tmp14_;
	RygelTrackerQueryTriplets* _tmp15_;
	const gchar* _tmp16_;
	RygelTrackerQueryTriplet* _tmp17_;
	RygelTrackerQueryTriplet* _tmp18_;
	RygelTrackerQueryTriplets* _tmp19_;
	RygelTrackerQueryTriplet* _tmp20_;
	RygelTrackerQueryTriplet* _tmp21_;
	RygelTrackerQueryTriplets* _tmp22_;
	const gchar* _tmp23_;
	const gchar* _tmp24_;
	gchar* _tmp25_;
	gchar* _tmp26_;
	gchar* _tmp27_;
	gchar* _tmp28_;
	RygelTrackerQueryTriplet* _tmp29_;
	RygelTrackerQueryTriplet* _tmp30_;
	gchar* dlna_profile = NULL;
	gchar* _tmp31_;
	const gchar* _tmp32_;
	const gchar* _tmp33_;
	RygelTrackerQueryTriplets* _tmp37_;
	const gchar* _tmp38_;
	gchar* _tmp39_;
	gchar* _tmp40_;
	gchar* _tmp41_;
	gchar* _tmp42_;
	RygelTrackerQueryTriplet* _tmp43_;
	RygelTrackerQueryTriplet* _tmp44_;
	RygelTrackerQueryTriplets* _tmp45_;
	const gchar* _tmp46_;
	const gchar* _tmp47_;
	gchar* _tmp48_;
	gchar* _tmp49_;
	gchar* _tmp50_;
	gchar* _tmp51_;
	RygelTrackerQueryTriplet* _tmp52_;
	RygelTrackerQueryTriplet* _tmp53_;
	RygelTrackerQueryTriplets* _tmp54_;
	const gchar* _tmp55_;
	RygelTrackerQueryTriplet* _tmp56_;
	RygelTrackerQueryTriplet* _tmp57_;
	gchar* date = NULL;
	const gchar* _tmp58_;
	const gchar* _tmp59_;
	RygelTrackerQueryTriplets* _tmp68_;
	const gchar* _tmp69_;
	gchar* _tmp70_;
	gchar* _tmp71_;
	gchar* _tmp72_;
	gchar* _tmp73_;
	RygelTrackerQueryTriplet* _tmp74_;
	RygelTrackerQueryTriplet* _tmp75_;
	RygelTrackerQueryTriplets* _tmp76_;
	const gchar* _tmp77_;
	RygelTrackerQueryTriplet* _tmp78_;
	RygelTrackerQueryTriplet* _tmp79_;
	RygelTrackerQueryTriplets* _tmp80_;
	const gchar* _tmp81_;
	RygelTrackerQueryTriplet* _tmp82_;
	RygelTrackerQueryTriplet* _tmp83_;
	RygelTrackerQueryTriplets* _tmp84_;
	const gchar* _tmp85_;
	RygelTrackerQueryTriplet* _tmp86_;
	RygelTrackerQueryTriplet* _tmp87_;
	gint64 _tmp88_;
	gint64 _tmp89_;
	RygelTrackerQueryTriplets* _tmp102_;
	gchar* _tmp103_;
	g_return_val_if_fail (item != NULL, NULL);
	g_return_val_if_fail (category != NULL, NULL);
	_tmp0_ = g_strdup ("nie:InformationElement");
	type = _tmp0_;
	_tmp1_ = rygel_media_object_get_primary_uri ((RygelMediaObject*) item);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_file_new_for_uri (_tmp2_);
	_tmp4_ = _tmp3_;
	_g_free0 (_tmp2_);
	file = _tmp4_;
	_tmp5_ = rygel_media_object_get_primary_uri ((RygelMediaObject*) item);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_strdup_printf ("<%s>", _tmp6_);
	_tmp8_ = _tmp7_;
	_g_free0 (_tmp6_);
	urn = _tmp8_;
	_tmp9_ = file;
	if (!g_file_is_native (_tmp9_)) {
		gchar* _tmp10_;
		_tmp10_ = g_strdup ("nfo:RemoteDataObject");
		_g_free0 (type);
		type = _tmp10_;
	}
	_tmp11_ = rygel_tracker_query_triplets_new ();
	triplets = _tmp11_;
	_tmp12_ = triplets;
	_tmp13_ = rygel_tracker_query_triplet_new_with_graph ("Tracker:Audio", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID, "a", category);
	_tmp14_ = _tmp13_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp12_, _tmp14_);
	_rygel_tracker_query_triplet_unref0 (_tmp14_);
	_tmp15_ = triplets;
	_tmp16_ = type;
	_tmp17_ = rygel_tracker_query_triplet_new_with_graph ("Tracker:Audio", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID, "a", _tmp16_);
	_tmp18_ = _tmp17_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp15_, _tmp18_);
	_rygel_tracker_query_triplet_unref0 (_tmp18_);
	_tmp19_ = triplets;
	_tmp20_ = rygel_tracker_query_triplet_new_with_graph ("Tracker:Audio", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID, "nie:generator", "\"rygel\"");
	_tmp21_ = _tmp20_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp19_, _tmp21_);
	_rygel_tracker_query_triplet_unref0 (_tmp21_);
	_tmp22_ = triplets;
	_tmp23_ = rygel_media_object_get_title ((RygelMediaObject*) item);
	_tmp24_ = _tmp23_;
	_tmp25_ = g_strconcat ("\"", _tmp24_, NULL);
	_tmp26_ = _tmp25_;
	_tmp27_ = g_strconcat (_tmp26_, "\"", NULL);
	_tmp28_ = _tmp27_;
	_tmp29_ = rygel_tracker_query_triplet_new_with_graph ("Tracker:Audio", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID, "nie:title", _tmp28_);
	_tmp30_ = _tmp29_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp22_, _tmp30_);
	_rygel_tracker_query_triplet_unref0 (_tmp30_);
	_g_free0 (_tmp28_);
	_g_free0 (_tmp26_);
	_tmp31_ = g_strdup ("");
	dlna_profile = _tmp31_;
	_tmp32_ = rygel_media_file_item_get_dlna_profile (item);
	_tmp33_ = _tmp32_;
	if (_tmp33_ != NULL) {
		const gchar* _tmp34_;
		const gchar* _tmp35_;
		gchar* _tmp36_;
		_tmp34_ = rygel_media_file_item_get_dlna_profile (item);
		_tmp35_ = _tmp34_;
		_tmp36_ = g_strdup (_tmp35_);
		_g_free0 (dlna_profile);
		dlna_profile = _tmp36_;
	}
	_tmp37_ = triplets;
	_tmp38_ = dlna_profile;
	_tmp39_ = g_strconcat ("\"", _tmp38_, NULL);
	_tmp40_ = _tmp39_;
	_tmp41_ = g_strconcat (_tmp40_, "\"", NULL);
	_tmp42_ = _tmp41_;
	_tmp43_ = rygel_tracker_query_triplet_new_with_graph ("Tracker:Audio", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID, "nmm:dlnaProfile", _tmp42_);
	_tmp44_ = _tmp43_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp37_, _tmp44_);
	_rygel_tracker_query_triplet_unref0 (_tmp44_);
	_g_free0 (_tmp42_);
	_g_free0 (_tmp40_);
	_tmp45_ = triplets;
	_tmp46_ = rygel_media_file_item_get_mime_type (item);
	_tmp47_ = _tmp46_;
	_tmp48_ = g_strconcat ("\"", _tmp47_, NULL);
	_tmp49_ = _tmp48_;
	_tmp50_ = g_strconcat (_tmp49_, "\"", NULL);
	_tmp51_ = _tmp50_;
	_tmp52_ = rygel_tracker_query_triplet_new_with_graph ("Tracker:Audio", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID, "nie:mimeType", _tmp51_);
	_tmp53_ = _tmp52_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp45_, _tmp53_);
	_rygel_tracker_query_triplet_unref0 (_tmp53_);
	_g_free0 (_tmp51_);
	_g_free0 (_tmp49_);
	_tmp54_ = triplets;
	_tmp55_ = urn;
	_tmp56_ = rygel_tracker_query_triplet_new_with_graph ("Tracker:Audio", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID, "nie:isStoredAs", _tmp55_);
	_tmp57_ = _tmp56_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp54_, _tmp57_);
	_rygel_tracker_query_triplet_unref0 (_tmp57_);
	_tmp58_ = rygel_media_object_get_date ((RygelMediaObject*) item);
	_tmp59_ = _tmp58_;
	if (_tmp59_ == NULL) {
		GDateTime* now = NULL;
		GDateTime* _tmp60_;
		GDateTime* _tmp61_;
		gchar* _tmp62_;
		gchar* _tmp63_;
		gchar* _tmp64_;
		_tmp60_ = g_date_time_new_now_utc ();
		now = _tmp60_;
		_tmp61_ = now;
		_tmp62_ = g_date_time_format (_tmp61_, "%Y-%m-%dT%H:%M:%S");
		_tmp63_ = _tmp62_;
		_tmp64_ = g_strdup_printf ("%sZ", _tmp63_);
		_g_free0 (date);
		date = _tmp64_;
		_g_free0 (_tmp63_);
		_g_date_time_unref0 (now);
	} else {
		const gchar* _tmp65_;
		const gchar* _tmp66_;
		gchar* _tmp67_;
		_tmp65_ = rygel_media_object_get_date ((RygelMediaObject*) item);
		_tmp66_ = _tmp65_;
		_tmp67_ = g_strdup (_tmp66_);
		_g_free0 (date);
		date = _tmp67_;
	}
	_tmp68_ = triplets;
	_tmp69_ = date;
	_tmp70_ = g_strconcat ("\"", _tmp69_, NULL);
	_tmp71_ = _tmp70_;
	_tmp72_ = g_strconcat (_tmp71_, "\"^^xsd:dateTime", NULL);
	_tmp73_ = _tmp72_;
	_tmp74_ = rygel_tracker_query_triplet_new_with_graph ("Tracker:Audio", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID, "nie:contentCreated", _tmp73_);
	_tmp75_ = _tmp74_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp68_, _tmp75_);
	_rygel_tracker_query_triplet_unref0 (_tmp75_);
	_g_free0 (_tmp73_);
	_g_free0 (_tmp71_);
	_tmp76_ = triplets;
	_tmp77_ = urn;
	_tmp78_ = rygel_tracker_query_triplet_new_with_graph (RYGEL_TRACKER_INSERTION_QUERY_MINER_GRAPH, _tmp77_, "a", "nie:DataObject");
	_tmp79_ = _tmp78_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp76_, _tmp79_);
	_rygel_tracker_query_triplet_unref0 (_tmp79_);
	_tmp80_ = triplets;
	_tmp81_ = urn;
	_tmp82_ = rygel_tracker_query_triplet_new_with_graph (RYGEL_TRACKER_INSERTION_QUERY_MINER_GRAPH, _tmp81_, "nie:interpretedAs", RYGEL_TRACKER_INSERTION_QUERY_QUERY_ID);
	_tmp83_ = _tmp82_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp80_, _tmp83_);
	_rygel_tracker_query_triplet_unref0 (_tmp83_);
	_tmp84_ = triplets;
	_tmp85_ = urn;
	_tmp86_ = rygel_tracker_query_triplet_new_with_graph (RYGEL_TRACKER_INSERTION_QUERY_MINER_GRAPH, _tmp85_, "tracker:available", "true");
	_tmp87_ = _tmp86_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp84_, _tmp87_);
	_rygel_tracker_query_triplet_unref0 (_tmp87_);
	_tmp88_ = rygel_media_file_item_get_size (item);
	_tmp89_ = _tmp88_;
	if (_tmp89_ > ((gint64) 0)) {
		RygelTrackerQueryTriplets* _tmp90_;
		const gchar* _tmp91_;
		gint64 _tmp92_;
		gint64 _tmp93_;
		gchar* _tmp94_;
		gchar* _tmp95_;
		gchar* _tmp96_;
		gchar* _tmp97_;
		gchar* _tmp98_;
		gchar* _tmp99_;
		RygelTrackerQueryTriplet* _tmp100_;
		RygelTrackerQueryTriplet* _tmp101_;
		_tmp90_ = triplets;
		_tmp91_ = urn;
		_tmp92_ = rygel_media_file_item_get_size (item);
		_tmp93_ = _tmp92_;
		_tmp94_ = g_strdup_printf ("%" G_GINT64_FORMAT, _tmp93_);
		_tmp95_ = _tmp94_;
		_tmp96_ = g_strconcat ("\"", _tmp95_, NULL);
		_tmp97_ = _tmp96_;
		_tmp98_ = g_strconcat (_tmp97_, "\"", NULL);
		_tmp99_ = _tmp98_;
		_tmp100_ = rygel_tracker_query_triplet_new_with_graph (RYGEL_TRACKER_INSERTION_QUERY_MINER_GRAPH, _tmp91_, "nie:byteSize", _tmp99_);
		_tmp101_ = _tmp100_;
		gee_abstract_collection_add ((GeeAbstractCollection*) _tmp90_, _tmp101_);
		_rygel_tracker_query_triplet_unref0 (_tmp101_);
		_g_free0 (_tmp99_);
		_g_free0 (_tmp97_);
		_g_free0 (_tmp95_);
	}
	_tmp102_ = triplets;
	self = (RygelTrackerInsertionQuery*) rygel_tracker_query_construct (object_type, _tmp102_);
	_tmp103_ = rygel_media_object_get_primary_uri ((RygelMediaObject*) item);
	_g_free0 (self->priv->uri);
	self->priv->uri = _tmp103_;
	_g_free0 (date);
	_g_free0 (dlna_profile);
	_g_object_unref0 (triplets);
	_g_free0 (urn);
	_g_object_unref0 (file);
	_g_free0 (type);
	return self;
}

RygelTrackerInsertionQuery*
rygel_tracker_insertion_query_new (RygelMediaFileItem* item,
                                   const gchar* category)
{
	return rygel_tracker_insertion_query_construct (RYGEL_TRACKER_TYPE_INSERTION_QUERY, item, category);
}

static void
rygel_tracker_insertion_query_real_execute_data_free (gpointer _data)
{
	RygelTrackerInsertionQueryExecuteData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->resources);
	_rygel_tracker_query_unref0 (_data_->self);
	g_slice_free (RygelTrackerInsertionQueryExecuteData, _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_insertion_query_real_execute (RygelTrackerQuery* base,
                                            TrackerSparqlConnection* resources,
                                            GAsyncReadyCallback _callback_,
                                            gpointer _user_data_)
{
	RygelTrackerInsertionQuery * self;
	RygelTrackerInsertionQueryExecuteData* _data_;
	RygelTrackerInsertionQuery* _tmp0_;
	TrackerSparqlConnection* _tmp1_;
	g_return_if_fail (resources != NULL);
	self = (RygelTrackerInsertionQuery*) base;
	_data_ = g_slice_new0 (RygelTrackerInsertionQueryExecuteData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, rygel_tracker_insertion_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_insertion_query_real_execute_co (_data_);
}

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

static void
rygel_tracker_insertion_query_execute_ready (GObject* source_object,
                                             GAsyncResult* _res_,
                                             gpointer _user_data_)
{
	RygelTrackerInsertionQueryExecuteData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	rygel_tracker_insertion_query_real_execute_co (_data_);
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

static gboolean
rygel_tracker_insertion_query_real_execute_co (RygelTrackerInsertionQueryExecuteData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = rygel_tracker_query_to_string ((RygelTrackerQuery*) _data_->self);
	_data_->str = _data_->_tmp0_;
	_data_->_tmp1_ = _data_->str;
	g_debug ("rygel-tracker-insertion-query.vala:125: Executing SPARQL query: %s", _data_->_tmp1_);
	_data_->_tmp2_ = _data_->str;
	_data_->_state_ = 1;
	tracker_sparql_connection_update_blank_async (_data_->resources, _data_->_tmp2_, NULL, rygel_tracker_insertion_query_execute_ready, _data_);
	return FALSE;
	_state_1:
	_data_->_tmp3_ = tracker_sparql_connection_update_blank_finish (_data_->resources, _data_->_res_, &_data_->_inner_error0_);
	_data_->v = _data_->_tmp3_;
	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_->key = NULL;
	_data_->_tmp4_ = _data_->v;
	_data_->_tmp5_ = g_variant_print (_data_->_tmp4_, TRUE);
	_data_->_tmp6_ = _data_->_tmp5_;
	g_debug ("rygel-tracker-insertion-query.vala:131: Result: %s", _data_->_tmp6_);
	_g_free0 (_data_->_tmp6_);
	_data_->_tmp7_ = _data_->v;
	_data_->_tmp8_ = g_variant_iter_new (_data_->_tmp7_);
	_g_variant_iter_free0 (_data_->iter1);
	_data_->iter1 = _data_->_tmp8_;
	while (TRUE) {
		_data_->_tmp9_ = _data_->iter1;
		_data_->_tmp10_ = NULL;
		_data_->_tmp11_ = g_variant_iter_next (_data_->_tmp9_, "aa{ss}", &_data_->_tmp10_, NULL);
		_g_variant_iter_free0 (_data_->iter2);
		_data_->iter2 = _data_->_tmp10_;
		if (!_data_->_tmp11_) {
			break;
		}
		while (TRUE) {
			_data_->_tmp12_ = _data_->iter2;
			_data_->_tmp13_ = NULL;
			_data_->_tmp14_ = g_variant_iter_next (_data_->_tmp12_, "a{ss}", &_data_->_tmp13_, NULL);
			_g_variant_iter_free0 (_data_->iter3);
			_data_->iter3 = _data_->_tmp13_;
			if (!_data_->_tmp14_) {
				break;
			}
			while (TRUE) {
				_data_->_tmp15_ = _data_->iter3;
				_data_->_tmp16_ = NULL;
				_data_->_tmp17_ = NULL;
				_data_->_tmp18_ = g_variant_iter_next (_data_->_tmp15_, "{ss}", &_data_->_tmp16_, &_data_->_tmp17_, NULL);
				_g_free0 (_data_->key);
				_data_->key = _data_->_tmp16_;
				_g_free0 (_data_->self->id);
				_data_->self->id = _data_->_tmp17_;
				if (!_data_->_tmp18_) {
					break;
				}
				break;
			}
		}
	}
	_data_->_tmp19_ = _data_->self->id;
	if (_data_->_tmp19_ == NULL) {
		_data_->_tmp20_ = rygel_tracker_insertion_query_get_resource_id_query (_data_->self);
		_data_->_tmp21_ = _data_->_tmp20_;
		_data_->_tmp22_ = string_to_string (_data_->_tmp21_);
		g_debug ("rygel-tracker-insertion-query.vala:144: Item already exists, running q" \
"uery %s", _data_->_tmp22_);
		_g_free0 (_data_->_tmp21_);
		_data_->_tmp23_ = rygel_tracker_insertion_query_get_resource_id_query (_data_->self);
		_data_->_tmp24_ = _data_->_tmp23_;
		_data_->_state_ = 2;
		tracker_sparql_connection_query_async (_data_->resources, _data_->_tmp24_, NULL, rygel_tracker_insertion_query_execute_ready, _data_);
		return FALSE;
		_state_2:
		_data_->_tmp25_ = tracker_sparql_connection_query_finish (_data_->resources, _data_->_res_, &_data_->_inner_error0_);
		_data_->_tmp26_ = _data_->_tmp25_;
		_g_free0 (_data_->_tmp24_);
		_data_->cursor = _data_->_tmp26_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
			_g_free0 (_data_->key);
			_g_variant_iter_free0 (_data_->iter3);
			_g_variant_iter_free0 (_data_->iter2);
			_g_variant_iter_free0 (_data_->iter1);
			_g_variant_unref0 (_data_->v);
			_g_free0 (_data_->str);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		{
			while (TRUE) {
				_data_->_tmp28_ = _data_->cursor;
				_data_->_tmp27_ = tracker_sparql_cursor_next (_data_->_tmp28_, NULL, &_data_->_inner_error0_);
				if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
					goto __catch0_g_error;
				}
				if (!_data_->_tmp27_) {
					break;
				}
				_data_->_tmp29_ = _data_->cursor;
				_data_->_tmp30_ = tracker_sparql_cursor_get_string (_data_->_tmp29_, 0, NULL);
				_data_->_tmp31_ = g_strdup (_data_->_tmp30_);
				_g_free0 (_data_->self->id);
				_data_->self->id = _data_->_tmp31_;
				break;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			_data_->_error_ = _data_->_inner_error0_;
			_data_->_inner_error0_ = NULL;
			_data_->_tmp32_ = _data_->_error_;
			_data_->_tmp33_ = _data_->_tmp32_->message;
			g_debug ("rygel-tracker-insertion-query.vala:154: Failed to query resource: %s", _data_->_tmp33_);
			_g_error_free0 (_data_->_error_);
		}
		__finally0:
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
			_g_object_unref0 (_data_->cursor);
			_g_free0 (_data_->key);
			_g_variant_iter_free0 (_data_->iter3);
			_g_variant_iter_free0 (_data_->iter2);
			_g_variant_iter_free0 (_data_->iter1);
			_g_variant_unref0 (_data_->v);
			_g_free0 (_data_->str);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		_data_->_tmp34_ = _data_->cursor;
		tracker_sparql_cursor_close (_data_->_tmp34_);
		_g_object_unref0 (_data_->cursor);
	} else {
		_data_->_tmp35_ = _data_->self->priv->uri;
		_data_->_tmp36_ = g_file_new_for_uri (_data_->_tmp35_);
		_data_->file = _data_->_tmp36_;
		_data_->_tmp38_ = _data_->file;
		if (g_file_is_native (_data_->_tmp38_)) {
			_data_->_tmp39_ = _data_->file;
			_data_->_tmp37_ = g_file_query_exists (_data_->_tmp39_, NULL);
		} else {
			_data_->_tmp37_ = FALSE;
		}
		if (_data_->_tmp37_) {
			_data_->_tmp40_ = (RygelTrackerMinerFilesIndexIface*) g_initable_new (RYGEL_TRACKER_TYPE_MINER_FILES_INDEX_IFACE_PROXY, NULL, &_data_->_inner_error0_, "g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, "g-name", RYGEL_TRACKER_INSERTION_QUERY_MINER_SERVICE, "g-bus-type", G_BUS_TYPE_SESSION, "g-object-path", RYGEL_TRACKER_INSERTION_QUERY_MINER_PATH, "g-interface-name", "org.freedesktop.Tracker1.Miner.Files.Index", NULL);
			_data_->miner = (RygelTrackerMinerFilesIndexIface*) _data_->_tmp40_;
			if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
				g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
				_g_object_unref0 (_data_->file);
				_g_free0 (_data_->key);
				_g_variant_iter_free0 (_data_->iter3);
				_g_variant_iter_free0 (_data_->iter2);
				_g_variant_iter_free0 (_data_->iter1);
				_g_variant_unref0 (_data_->v);
				_g_free0 (_data_->str);
				g_object_unref (_data_->_async_result);
				return FALSE;
			}
			_data_->_tmp41_ = _data_->miner;
			_data_->_tmp42_ = _data_->self->priv->uri;
			rygel_tracker_miner_files_index_iface_index_file (_data_->_tmp41_, _data_->_tmp42_, NULL, NULL);
			_g_object_unref0 (_data_->miner);
		}
		_g_object_unref0 (_data_->file);
	}
	_data_->_tmp43_ = _data_->self->id;
	g_debug ("rygel-tracker-insertion-query.vala:170: Created item in Tracker store " \
"with ID '%s'", _data_->_tmp43_);
	_g_free0 (_data_->key);
	_g_variant_iter_free0 (_data_->iter3);
	_g_variant_iter_free0 (_data_->iter2);
	_g_variant_iter_free0 (_data_->iter1);
	_g_variant_unref0 (_data_->v);
	_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_insertion_query_real_to_string (RygelTrackerQuery* base)
{
	RygelTrackerInsertionQuery * self;
	gchar* query = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* result;
	self = (RygelTrackerInsertionQuery*) base;
	_tmp0_ = RYGEL_TRACKER_QUERY_CLASS (rygel_tracker_insertion_query_parent_class)->to_string (G_TYPE_CHECK_INSTANCE_CAST (self, RYGEL_TRACKER_TYPE_QUERY, RygelTrackerQuery));
	_tmp1_ = _tmp0_;
	_tmp2_ = g_strconcat ("INSERT { ", _tmp1_, NULL);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_strconcat (_tmp3_, " }", NULL);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	_g_free0 (_tmp1_);
	query = _tmp5_;
	result = query;
	return result;
}

static gchar*
rygel_tracker_insertion_query_get_resource_id_query (RygelTrackerInsertionQuery* self)
{
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->uri;
	_tmp1_ = g_strdup_printf (RYGEL_TRACKER_INSERTION_QUERY_RESOURCE_ID_QUERY_TEMPLATE, _tmp0_);
	result = _tmp1_;
	return result;
}

static void
rygel_tracker_insertion_query_class_init (RygelTrackerInsertionQueryClass * klass,
                                          gpointer klass_data)
{
	rygel_tracker_insertion_query_parent_class = g_type_class_peek_parent (klass);
	((RygelTrackerQueryClass *) klass)->finalize = rygel_tracker_insertion_query_finalize;
	g_type_class_adjust_private_offset (klass, &RygelTrackerInsertionQuery_private_offset);
	((RygelTrackerQueryClass *) klass)->execute = (void (*) (RygelTrackerQuery*, TrackerSparqlConnection*, GAsyncReadyCallback, gpointer)) rygel_tracker_insertion_query_real_execute;
	((RygelTrackerQueryClass *) klass)->execute_finish = (void (*) (RygelTrackerQuery*, GAsyncResult*, GError**)) rygel_tracker_insertion_query_execute_finish;
	((RygelTrackerQueryClass *) klass)->to_string = (gchar* (*) (RygelTrackerQuery*)) rygel_tracker_insertion_query_real_to_string;
}

static void
rygel_tracker_insertion_query_instance_init (RygelTrackerInsertionQuery * self,
                                             gpointer klass)
{
	self->priv = rygel_tracker_insertion_query_get_instance_private (self);
}

static void
rygel_tracker_insertion_query_finalize (RygelTrackerQuery * obj)
{
	RygelTrackerInsertionQuery * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TRACKER_TYPE_INSERTION_QUERY, RygelTrackerInsertionQuery);
	_g_free0 (self->id);
	_g_free0 (self->priv->uri);
	RYGEL_TRACKER_QUERY_CLASS (rygel_tracker_insertion_query_parent_class)->finalize (obj);
}

/**
 * Represents Tracker SPARQL Insertion query
 */
static GType
rygel_tracker_insertion_query_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelTrackerInsertionQueryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_tracker_insertion_query_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelTrackerInsertionQuery), 0, (GInstanceInitFunc) rygel_tracker_insertion_query_instance_init, NULL };
	GType rygel_tracker_insertion_query_type_id;
	rygel_tracker_insertion_query_type_id = g_type_register_static (RYGEL_TRACKER_TYPE_QUERY, "RygelTrackerInsertionQuery", &g_define_type_info, 0);
	RygelTrackerInsertionQuery_private_offset = g_type_add_instance_private (rygel_tracker_insertion_query_type_id, sizeof (RygelTrackerInsertionQueryPrivate));
	return rygel_tracker_insertion_query_type_id;
}

GType
rygel_tracker_insertion_query_get_type (void)
{
	static volatile gsize rygel_tracker_insertion_query_type_id__once = 0;
	if (g_once_init_enter (&rygel_tracker_insertion_query_type_id__once)) {
		GType rygel_tracker_insertion_query_type_id;
		rygel_tracker_insertion_query_type_id = rygel_tracker_insertion_query_get_type_once ();
		g_once_init_leave (&rygel_tracker_insertion_query_type_id__once, rygel_tracker_insertion_query_type_id);
	}
	return rygel_tracker_insertion_query_type_id__once;
}

