/* rygel-lms-database.c generated by valac 0.32.1, the Vala compiler
 * generated from rygel-lms-database.vala, do not modify */

/*
 * Copyright (C) 2009,2011,2016 Jens Georg <mail@jensge.org>,
 *           (C) 2013 Intel Corporation.
 *
 * Author: Jussi Kukkonen <jussi.kukkonen@intel.com>
 *         Jens Georg <mail@jensge.org>
 *
 * 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 <rygel-db.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>


#define RYGEL_LMS_TYPE_DATABASE (rygel_lms_database_get_type ())
#define RYGEL_LMS_DATABASE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_LMS_TYPE_DATABASE, RygelLMSDatabase))
#define RYGEL_LMS_DATABASE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_LMS_TYPE_DATABASE, RygelLMSDatabaseClass))
#define RYGEL_LMS_IS_DATABASE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_LMS_TYPE_DATABASE))
#define RYGEL_LMS_IS_DATABASE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_LMS_TYPE_DATABASE))
#define RYGEL_LMS_DATABASE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_LMS_TYPE_DATABASE, RygelLMSDatabaseClass))

typedef struct _RygelLMSDatabase RygelLMSDatabase;
typedef struct _RygelLMSDatabaseClass RygelLMSDatabaseClass;
typedef struct _RygelLMSDatabasePrivate RygelLMSDatabasePrivate;

#define RYGEL_LMS_TYPE_DBUS (rygel_lms_dbus_get_type ())
#define RYGEL_LMS_DBUS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_LMS_TYPE_DBUS, RygelLMSDBus))
#define RYGEL_LMS_IS_DBUS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_LMS_TYPE_DBUS))
#define RYGEL_LMS_DBUS_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), RYGEL_LMS_TYPE_DBUS, RygelLMSDBusIface))

typedef struct _RygelLMSDBus RygelLMSDBus;
typedef struct _RygelLMSDBusIface RygelLMSDBusIface;

#define RYGEL_LMS_TYPE_DBUS_PROXY (rygel_lms_dbus_proxy_get_type ())
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))
#define _g_variant_iter_free0(var) ((var == NULL) ? NULL : (var = (g_variant_iter_free (var), NULL)))

struct _RygelLMSDatabase {
	RygelDatabaseDatabase parent_instance;
	RygelLMSDatabasePrivate * priv;
};

struct _RygelLMSDatabaseClass {
	RygelDatabaseDatabaseClass parent_class;
};

struct _RygelLMSDBusIface {
	GTypeInterface parent_iface;
	gchar* (*get_data_base_path) (RygelLMSDBus* self);
	guint64 (*get_update_id) (RygelLMSDBus* self);
};

struct _RygelLMSDatabasePrivate {
	RygelLMSDBus* lms_proxy;
	guint64 update_id;
};


static gpointer rygel_lms_database_parent_class = NULL;
static GInitableIface* rygel_lms_database_g_initable_parent_iface = NULL;

GType rygel_lms_database_get_type (void) G_GNUC_CONST;
GType rygel_lms_dbus_get_type (void) G_GNUC_CONST;
GType rygel_lms_dbus_proxy_get_type (void) G_GNUC_CONST;
guint rygel_lms_dbus_register_object (void* object, GDBusConnection* connection, const gchar* path, GError** error);
#define RYGEL_LMS_DATABASE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RYGEL_LMS_TYPE_DATABASE, RygelLMSDatabasePrivate))
enum  {
	RYGEL_LMS_DATABASE_DUMMY_PROPERTY
};
RygelLMSDatabase* rygel_lms_database_new (GError** error);
RygelLMSDatabase* rygel_lms_database_construct (GType object_type, GError** error);
static gboolean rygel_lms_database_real_init (GInitable* base, GCancellable* cancellable, GError** error);
gchar* rygel_lms_dbus_get_data_base_path (RygelLMSDBus* self);
guint64 rygel_lms_dbus_get_update_id (RygelLMSDBus* self);
static void rygel_lms_database_on_lms_properties_changed (RygelLMSDatabase* self, GDBusProxy* lms_proxy, GVariant* changed, gchar** invalidated, int invalidated_length1);
static void _rygel_lms_database_on_lms_properties_changed_g_dbus_proxy_g_properties_changed (GDBusProxy* _sender, GVariant* changed_properties, gchar** invalidated_properties, gpointer self);
static gchar* _variant_get1 (GVariant* value);
static guint64 _variant_get2 (GVariant* value);
static guint64 _variant_get3 (GVariant* value);
static void g_cclosure_user_marshal_VOID__UINT64_UINT64 (GClosure * closure, GValue * return_value, guint n_param_values, const GValue * param_values, gpointer invocation_hint, gpointer marshal_data);
static void rygel_lms_database_finalize (GObject* obj);
static gint _vala_array_length (gpointer array);


RygelLMSDatabase* rygel_lms_database_construct (GType object_type, GError** error) {
	RygelLMSDatabase * self = NULL;
	GError * _inner_error_ = NULL;
	self = (RygelLMSDatabase*) g_object_new (object_type, "name", ":memory:", "flags", RYGEL_DATABASE_FLAGS_READ_ONLY, NULL);
	g_initable_init ((GInitable*) self, NULL, &_inner_error_);
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_object_unref0 (self);
		return NULL;
	}
	return self;
}


RygelLMSDatabase* rygel_lms_database_new (GError** error) {
	return rygel_lms_database_construct (RYGEL_LMS_TYPE_DATABASE, error);
}


static void _rygel_lms_database_on_lms_properties_changed_g_dbus_proxy_g_properties_changed (GDBusProxy* _sender, GVariant* changed_properties, gchar** invalidated_properties, gpointer self) {
	rygel_lms_database_on_lms_properties_changed ((RygelLMSDatabase*) self, _sender, changed_properties, invalidated_properties, _vala_array_length (invalidated_properties));
}


static gboolean rygel_lms_database_real_init (GInitable* base, GCancellable* cancellable, GError** error) {
	RygelLMSDatabase * self;
	gboolean result = FALSE;
	gchar* db_path = NULL;
	const gchar* _tmp17_ = NULL;
	gboolean _tmp18_ = FALSE;
	gboolean _tmp19_ = FALSE;
	GError * _inner_error_ = NULL;
	self = (RygelLMSDatabase*) base;
	{
		RygelLMSDBus* _tmp0_ = NULL;
		RygelLMSDBus* _tmp1_ = NULL;
		RygelLMSDBus* _tmp2_ = NULL;
		RygelLMSDBus* _tmp3_ = NULL;
		gchar* _tmp4_ = NULL;
		gchar* _tmp5_ = NULL;
		const gchar* _tmp6_ = NULL;
		RygelLMSDBus* _tmp7_ = NULL;
		guint64 _tmp8_ = 0ULL;
		guint64 _tmp9_ = 0ULL;
		guint64 _tmp10_ = 0ULL;
		RygelLMSDBus* _tmp11_ = NULL;
		_tmp1_ = g_initable_new (RYGEL_LMS_TYPE_DBUS_PROXY, NULL, &_inner_error_, "g-flags", 0, "g-name", "org.lightmediascanner", "g-bus-type", G_BUS_TYPE_SESSION, "g-object-path", "/org/lightmediascanner/Scanner1", "g-interface-name", "org.lightmediascanner.Scanner1", "g-interface-info", g_type_get_qdata (RYGEL_LMS_TYPE_DBUS, g_quark_from_static_string ("vala-dbus-interface-info")), NULL);
		_tmp0_ = (RygelLMSDBus*) _tmp1_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			if (_inner_error_->domain == G_IO_ERROR) {
				goto __catch11_g_io_error;
			}
			_g_free0 (db_path);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return FALSE;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_object_unref0 (self->priv->lms_proxy);
		self->priv->lms_proxy = _tmp2_;
		_tmp3_ = self->priv->lms_proxy;
		_tmp4_ = rygel_lms_dbus_get_data_base_path (_tmp3_);
		_tmp5_ = _tmp4_;
		_g_free0 (db_path);
		db_path = _tmp5_;
		_tmp6_ = db_path;
		g_debug ("rygel-lms-database.vala:49: Got db path %s from LMS over dbus", _tmp6_);
		_tmp7_ = self->priv->lms_proxy;
		_tmp8_ = rygel_lms_dbus_get_update_id (_tmp7_);
		_tmp9_ = _tmp8_;
		self->priv->update_id = _tmp9_;
		_tmp10_ = self->priv->update_id;
		g_debug ("rygel-lms-database.vala:51: Got updated id %lld from LMS over dbus", _tmp10_);
		_tmp11_ = self->priv->lms_proxy;
		g_signal_connect_object ((GDBusProxy*) _tmp11_, "g-properties-changed", (GCallback) _rygel_lms_database_on_lms_properties_changed_g_dbus_proxy_g_properties_changed, self, 0);
		_g_object_unref0 (_tmp0_);
	}
	goto __finally11;
	__catch11_g_io_error:
	{
		GError* e = NULL;
		GError* _tmp12_ = NULL;
		const gchar* _tmp13_ = NULL;
		const gchar* _tmp14_ = NULL;
		gchar* _tmp15_ = NULL;
		const gchar* _tmp16_ = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp12_ = e;
		_tmp13_ = _tmp12_->message;
		g_warning ("rygel-lms-database.vala:55: Couldn't get LMS Dbus proxy: %s", _tmp13_);
		_tmp14_ = g_get_user_config_dir ();
		_tmp15_ = g_strconcat (_tmp14_, "/lightmediascannerd/db.sqlite3", NULL);
		_g_free0 (db_path);
		db_path = _tmp15_;
		_tmp16_ = db_path;
		g_debug ("rygel-lms-database.vala:58: Using default sqlite database location %s", _tmp16_);
		_g_error_free0 (e);
	}
	__finally11:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_free0 (db_path);
		return FALSE;
	}
	_tmp17_ = db_path;
	rygel_database_database_set_name ((RygelDatabaseDatabase*) self, _tmp17_);
	_tmp19_ = rygel_lms_database_g_initable_parent_iface->init ((GInitable*) G_TYPE_CHECK_INSTANCE_CAST (self, RYGEL_DATABASE_TYPE_DATABASE, RygelDatabaseDatabase), NULL, &_inner_error_);
	_tmp18_ = _tmp19_;
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_propagate_error (error, _inner_error_);
		_g_free0 (db_path);
		return FALSE;
	}
	result = _tmp18_;
	_g_free0 (db_path);
	return result;
}


static gchar* _variant_get1 (GVariant* value) {
	return g_variant_dup_string (value, NULL);
}


static guint64 _variant_get2 (GVariant* value) {
	return g_variant_get_uint64 (value);
}


static guint64 _variant_get3 (GVariant* value) {
	return g_variant_get_uint64 (value);
}


static void rygel_lms_database_on_lms_properties_changed (RygelLMSDatabase* self, GDBusProxy* lms_proxy, GVariant* changed, gchar** invalidated, int invalidated_length1) {
	GVariant* _tmp0_ = NULL;
	const GVariantType* _tmp1_ = NULL;
	const GVariantType* _tmp2_ = NULL;
	gboolean _tmp3_ = FALSE;
	g_return_if_fail (self != NULL);
	g_return_if_fail (lms_proxy != NULL);
	g_return_if_fail (changed != NULL);
	_tmp0_ = changed;
	_tmp1_ = g_variant_get_type (_tmp0_);
	_tmp2_ = G_VARIANT_TYPE_VARDICT;
	_tmp3_ = g_variant_type_equal (_tmp1_, _tmp2_);
	if (!_tmp3_) {
		return;
	}
	{
		GVariantIter* _changed_prop_it = NULL;
		GVariant* _tmp4_ = NULL;
		GVariantIter* _tmp5_ = NULL;
		GVariant* changed_prop = NULL;
		_tmp4_ = changed;
		_tmp5_ = g_variant_iter_new (_tmp4_);
		_changed_prop_it = _tmp5_;
		while (TRUE) {
			GVariantIter* _tmp6_ = NULL;
			GVariant* _tmp7_ = NULL;
			GVariant* _tmp8_ = NULL;
			gchar* key = NULL;
			GVariant* _tmp9_ = NULL;
			GVariant* _tmp10_ = NULL;
			GVariant* _tmp11_ = NULL;
			gchar* _tmp12_ = NULL;
			gchar* _tmp13_ = NULL;
			GVariant* value = NULL;
			GVariant* _tmp14_ = NULL;
			GVariant* _tmp15_ = NULL;
			GVariant* _tmp16_ = NULL;
			GVariant* _tmp17_ = NULL;
			GVariant* _tmp18_ = NULL;
			const gchar* _tmp19_ = NULL;
			GVariant* _tmp20_ = NULL;
			gchar* _tmp21_ = NULL;
			gchar* _tmp22_ = NULL;
			const gchar* _tmp23_ = NULL;
			const gchar* _tmp24_ = NULL;
			GQuark _tmp26_ = 0U;
			static GQuark _tmp25_label0 = 0;
			_tmp6_ = _changed_prop_it;
			_tmp7_ = g_variant_iter_next_value (_tmp6_);
			_g_variant_unref0 (changed_prop);
			changed_prop = _tmp7_;
			_tmp8_ = changed_prop;
			if (!(_tmp8_ != NULL)) {
				break;
			}
			_tmp9_ = changed_prop;
			_tmp10_ = g_variant_get_child_value (_tmp9_, (gsize) 0);
			_tmp11_ = _tmp10_;
			_tmp12_ = _variant_get1 (_tmp11_);
			_tmp13_ = _tmp12_;
			_g_variant_unref0 (_tmp11_);
			key = _tmp13_;
			_tmp14_ = changed_prop;
			_tmp15_ = g_variant_get_child_value (_tmp14_, (gsize) 1);
			_tmp16_ = _tmp15_;
			_tmp17_ = g_variant_get_child_value (_tmp16_, (gsize) 0);
			_tmp18_ = _tmp17_;
			_g_variant_unref0 (_tmp16_);
			value = _tmp18_;
			_tmp19_ = key;
			_tmp20_ = value;
			_tmp21_ = g_variant_print (_tmp20_, TRUE);
			_tmp22_ = _tmp21_;
			g_debug ("rygel-lms-database.vala:77: LMS property %s changed value to %s", _tmp19_, _tmp22_);
			_g_free0 (_tmp22_);
			_tmp23_ = key;
			_tmp24_ = _tmp23_;
			_tmp26_ = (NULL == _tmp24_) ? 0 : g_quark_from_string (_tmp24_);
			if (_tmp26_ == ((0 != _tmp25_label0) ? _tmp25_label0 : (_tmp25_label0 = g_quark_from_static_string ("UpdateID")))) {
				switch (0) {
					default:
					{
						guint64 _tmp27_ = 0ULL;
						GVariant* _tmp28_ = NULL;
						guint64 _tmp29_ = 0ULL;
						GVariant* _tmp30_ = NULL;
						guint64 _tmp31_ = 0ULL;
						_tmp27_ = self->priv->update_id;
						_tmp28_ = value;
						_tmp29_ = _variant_get2 (_tmp28_);
						g_signal_emit_by_name (self, "db-updated", _tmp27_, _tmp29_);
						_tmp30_ = value;
						_tmp31_ = _variant_get3 (_tmp30_);
						self->priv->update_id = _tmp31_;
						break;
					}
				}
			}
			_g_variant_unref0 (value);
			_g_free0 (key);
		}
		_g_variant_unref0 (changed_prop);
		_g_variant_iter_free0 (_changed_prop_it);
	}
}


static void g_cclosure_user_marshal_VOID__UINT64_UINT64 (GClosure * closure, GValue * return_value, guint n_param_values, const GValue * param_values, gpointer invocation_hint, gpointer marshal_data) {
	typedef void (*GMarshalFunc_VOID__UINT64_UINT64) (gpointer data1, guint64 arg_1, guint64 arg_2, gpointer data2);
	register GMarshalFunc_VOID__UINT64_UINT64 callback;
	register GCClosure * cc;
	register gpointer data1;
	register gpointer data2;
	cc = (GCClosure *) closure;
	g_return_if_fail (n_param_values == 3);
	if (G_CCLOSURE_SWAP_DATA (closure)) {
		data1 = closure->data;
		data2 = param_values->data[0].v_pointer;
	} else {
		data1 = param_values->data[0].v_pointer;
		data2 = closure->data;
	}
	callback = (GMarshalFunc_VOID__UINT64_UINT64) (marshal_data ? marshal_data : cc->callback);
	callback (data1, g_value_get_uint64 (param_values + 1), g_value_get_uint64 (param_values + 2), data2);
}


static void rygel_lms_database_class_init (RygelLMSDatabaseClass * klass) {
	rygel_lms_database_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RygelLMSDatabasePrivate));
	G_OBJECT_CLASS (klass)->finalize = rygel_lms_database_finalize;
	g_signal_new ("db_updated", RYGEL_LMS_TYPE_DATABASE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_user_marshal_VOID__UINT64_UINT64, G_TYPE_NONE, 2, G_TYPE_UINT64, G_TYPE_UINT64);
}


static void rygel_lms_database_g_initable_interface_init (GInitableIface * iface) {
	rygel_lms_database_g_initable_parent_iface = g_type_interface_peek_parent (iface);
	iface->init = (gboolean (*)(GInitable*, GCancellable*, GError**)) rygel_lms_database_real_init;
}


static void rygel_lms_database_instance_init (RygelLMSDatabase * self) {
	self->priv = RYGEL_LMS_DATABASE_GET_PRIVATE (self);
}


static void rygel_lms_database_finalize (GObject* obj) {
	RygelLMSDatabase * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_LMS_TYPE_DATABASE, RygelLMSDatabase);
	_g_object_unref0 (self->priv->lms_proxy);
	G_OBJECT_CLASS (rygel_lms_database_parent_class)->finalize (obj);
}


GType rygel_lms_database_get_type (void) {
	static volatile gsize rygel_lms_database_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_lms_database_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelLMSDatabaseClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_lms_database_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelLMSDatabase), 0, (GInstanceInitFunc) rygel_lms_database_instance_init, NULL };
		static const GInterfaceInfo g_initable_info = { (GInterfaceInitFunc) rygel_lms_database_g_initable_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
		GType rygel_lms_database_type_id;
		rygel_lms_database_type_id = g_type_register_static (RYGEL_DATABASE_TYPE_DATABASE, "RygelLMSDatabase", &g_define_type_info, 0);
		g_type_add_interface_static (rygel_lms_database_type_id, G_TYPE_INITABLE, &g_initable_info);
		g_once_init_leave (&rygel_lms_database_type_id__volatile, rygel_lms_database_type_id);
	}
	return rygel_lms_database_type_id__volatile;
}


static gint _vala_array_length (gpointer array) {
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}



