/* rygel-lms-database.c generated by valac 0.44.3, 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 <rygel-db.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.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 ())
enum  {
	RYGEL_LMS_DATABASE_0_PROPERTY,
	RYGEL_LMS_DATABASE_NUM_PROPERTIES
};
static GParamSpec* rygel_lms_database_properties[RYGEL_LMS_DATABASE_NUM_PROPERTIES];
#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)))
enum  {
	RYGEL_LMS_DATABASE_DB_UPDATED_SIGNAL,
	RYGEL_LMS_DATABASE_NUM_SIGNALS
};
static guint rygel_lms_database_signals[RYGEL_LMS_DATABASE_NUM_SIGNALS] = {0};

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 gint RygelLMSDatabase_private_offset;
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;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (RygelLMSDatabase, g_object_unref)
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);
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,
                                                   gint 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);

static inline gpointer
rygel_lms_database_get_instance_private (RygelLMSDatabase* self)
{
	return G_STRUCT_MEMBER_P (self, RygelLMSDatabase_private_offset);
}

RygelLMSDatabase*
rygel_lms_database_construct (GType object_type,
                              GError** error)
{
	RygelLMSDatabase * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (RygelLMSDatabase*) g_object_new (object_type, "name", ":memory:", "flags", RYGEL_DATABASE_FLAGS_READ_ONLY, NULL);
	g_initable_init ((GInitable*) self, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_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* _tmp18_;
	gboolean _tmp19_ = FALSE;
	GError* _inner_error0_ = NULL;
	self = (RygelLMSDatabase*) base;
	{
		RygelLMSDBus* _tmp0_ = NULL;
		RygelLMSDBus* _tmp1_;
		RygelLMSDBus* _tmp3_;
		RygelLMSDBus* _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		const gchar* _tmp7_;
		RygelLMSDBus* _tmp8_;
		guint64 _tmp9_;
		guint64 _tmp10_;
		RygelLMSDBus* _tmp11_;
		_tmp1_ = (RygelLMSDBus*) g_initable_new (RYGEL_LMS_TYPE_DBUS_PROXY, NULL, &_inner_error0_, "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_error0_ != NULL)) {
			gboolean _tmp2_ = FALSE;
			if (_inner_error0_->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_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return _tmp2_;
		}
		_tmp3_ = _tmp0_;
		_tmp0_ = NULL;
		_g_object_unref0 (self->priv->lms_proxy);
		self->priv->lms_proxy = _tmp3_;
		_tmp4_ = self->priv->lms_proxy;
		_tmp5_ = rygel_lms_dbus_get_data_base_path (_tmp4_);
		_tmp6_ = _tmp5_;
		_g_free0 (db_path);
		db_path = _tmp6_;
		_tmp7_ = db_path;
		g_debug ("rygel-lms-database.vala:49: Got db path %s from LMS over dbus", _tmp7_);
		_tmp8_ = self->priv->lms_proxy;
		_tmp9_ = rygel_lms_dbus_get_update_id (_tmp8_);
		_tmp10_ = _tmp9_;
		self->priv->update_id = _tmp10_;
		g_debug ("rygel-lms-database.vala:51: Got updated id %lld from LMS over dbus", self->priv->update_id);
		_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_;
		const gchar* _tmp13_;
		const gchar* _tmp14_;
		gchar* _tmp15_;
		const gchar* _tmp16_;
		e = _inner_error0_;
		_inner_error0_ = 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_error0_ != NULL)) {
		gboolean _tmp17_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_free0 (db_path);
		return _tmp17_;
	}
	_tmp18_ = db_path;
	rygel_database_database_set_name ((RygelDatabaseDatabase*) self, _tmp18_);
	_tmp19_ = rygel_lms_database_g_initable_parent_iface->init ((GInitable*) G_TYPE_CHECK_INSTANCE_CAST (self, RYGEL_DATABASE_TYPE_DATABASE, RygelDatabaseDatabase), NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp20_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_free0 (db_path);
		return _tmp20_;
	}
	result = _tmp19_;
	_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,
                                              gint invalidated_length1)
{
	const GVariantType* _tmp0_;
	const GVariantType* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (lms_proxy != NULL);
	g_return_if_fail (changed != NULL);
	_tmp0_ = g_variant_get_type (changed);
	_tmp1_ = G_VARIANT_TYPE_VARDICT;
	if (!g_variant_type_equal (_tmp0_, _tmp1_)) {
		return;
	}
	{
		GVariantIter* _changed_prop_it = NULL;
		GVariantIter* _tmp2_;
		GVariant* changed_prop = NULL;
		_tmp2_ = g_variant_iter_new (changed);
		_changed_prop_it = _tmp2_;
		while (TRUE) {
			GVariantIter* _tmp3_;
			GVariant* _tmp4_;
			GVariant* _tmp5_;
			gchar* key = NULL;
			GVariant* _tmp6_;
			GVariant* _tmp7_;
			GVariant* _tmp8_;
			gchar* _tmp9_;
			gchar* _tmp10_;
			GVariant* value = NULL;
			GVariant* _tmp11_;
			GVariant* _tmp12_;
			GVariant* _tmp13_;
			GVariant* _tmp14_;
			GVariant* _tmp15_;
			const gchar* _tmp16_;
			GVariant* _tmp17_;
			gchar* _tmp18_;
			gchar* _tmp19_;
			const gchar* _tmp20_;
			const gchar* _tmp21_;
			GQuark _tmp23_ = 0U;
			static GQuark _tmp22_label0 = 0;
			_tmp3_ = _changed_prop_it;
			_tmp4_ = g_variant_iter_next_value (_tmp3_);
			_g_variant_unref0 (changed_prop);
			changed_prop = _tmp4_;
			_tmp5_ = changed_prop;
			if (!(_tmp5_ != NULL)) {
				break;
			}
			_tmp6_ = changed_prop;
			_tmp7_ = g_variant_get_child_value (_tmp6_, (gsize) 0);
			_tmp8_ = _tmp7_;
			_tmp9_ = _variant_get1 (_tmp8_);
			_tmp10_ = _tmp9_;
			_g_variant_unref0 (_tmp8_);
			key = _tmp10_;
			_tmp11_ = changed_prop;
			_tmp12_ = g_variant_get_child_value (_tmp11_, (gsize) 1);
			_tmp13_ = _tmp12_;
			_tmp14_ = g_variant_get_child_value (_tmp13_, (gsize) 0);
			_tmp15_ = _tmp14_;
			_g_variant_unref0 (_tmp13_);
			value = _tmp15_;
			_tmp16_ = key;
			_tmp17_ = value;
			_tmp18_ = g_variant_print (_tmp17_, TRUE);
			_tmp19_ = _tmp18_;
			g_debug ("rygel-lms-database.vala:77: LMS property %s changed value to %s", _tmp16_, _tmp19_);
			_g_free0 (_tmp19_);
			_tmp20_ = key;
			_tmp21_ = _tmp20_;
			_tmp23_ = (NULL == _tmp21_) ? 0 : g_quark_from_string (_tmp21_);
			if (_tmp23_ == ((0 != _tmp22_label0) ? _tmp22_label0 : (_tmp22_label0 = g_quark_from_static_string ("UpdateID")))) {
				switch (0) {
					default:
					{
						GVariant* _tmp24_;
						guint64 _tmp25_;
						GVariant* _tmp26_;
						guint64 _tmp27_;
						_tmp24_ = value;
						_tmp25_ = _variant_get2 (_tmp24_);
						g_signal_emit (self, rygel_lms_database_signals[RYGEL_LMS_DATABASE_DB_UPDATED_SIGNAL], 0, self->priv->update_id, _tmp25_);
						_tmp26_ = value;
						_tmp27_ = _variant_get3 (_tmp26_);
						self->priv->update_id = _tmp27_;
						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,
                               gpointer klass_data)
{
	rygel_lms_database_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelLMSDatabase_private_offset);
	G_OBJECT_CLASS (klass)->finalize = rygel_lms_database_finalize;
	rygel_lms_database_signals[RYGEL_LMS_DATABASE_DB_UPDATED_SIGNAL] = 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,
                                              gpointer iface_data)
{
	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,
                                  gpointer klass)
{
	self->priv = rygel_lms_database_get_instance_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_initable_get_type (), &g_initable_info);
		RygelLMSDatabase_private_offset = g_type_add_instance_private (rygel_lms_database_type_id, sizeof (RygelLMSDatabasePrivate));
		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;
}

