/* DBusServer.c generated by valac 0.56.19, the Vala compiler
 * generated from DBusServer.vala, do not modify */

/*
 * Copyright © 2023 Alain M. (https://github.com/alainm23/planify)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU 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
 *
 * Authored by: Alain M. <alainmh23@gmail.com>
 */

#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#define SERVICES_DBUS_SERVER_DBUS_NAME "io.github.alainm23.planify"
#define SERVICES_DBUS_SERVER_DBUS_PATH "/io/github/alainm23/planify"
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define SERVICES_TYPE_DBUS_SERVER (services_dbus_server_get_type ())
#define SERVICES_DBUS_SERVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SERVICES_TYPE_DBUS_SERVER, ServicesDBusServer))
#define SERVICES_DBUS_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SERVICES_TYPE_DBUS_SERVER, ServicesDBusServerClass))
#define SERVICES_IS_DBUS_SERVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SERVICES_TYPE_DBUS_SERVER))
#define SERVICES_IS_DBUS_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SERVICES_TYPE_DBUS_SERVER))
#define SERVICES_DBUS_SERVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SERVICES_TYPE_DBUS_SERVER, ServicesDBusServerClass))

typedef struct _ServicesDBusServer ServicesDBusServer;
typedef struct _ServicesDBusServerClass ServicesDBusServerClass;
typedef struct _ServicesDBusServerPrivate ServicesDBusServerPrivate;
enum  {
	SERVICES_DBUS_SERVER_0_PROPERTY,
	SERVICES_DBUS_SERVER_NUM_PROPERTIES
};
static GParamSpec* services_dbus_server_properties[SERVICES_DBUS_SERVER_NUM_PROPERTIES];
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
enum  {
	SERVICES_DBUS_SERVER_ITEM_ADDED_SIGNAL,
	SERVICES_DBUS_SERVER_NUM_SIGNALS
};
static guint services_dbus_server_signals[SERVICES_DBUS_SERVER_NUM_SIGNALS] = {0};
#define _g_free0(var) (var = (g_free (var), NULL))

#define TYPE_DBUS_SERVER_ERROR (dbus_server_error_get_type ())

struct _ServicesDBusServer {
	GObject parent_instance;
	ServicesDBusServerPrivate * priv;
};

struct _ServicesDBusServerClass {
	GObjectClass parent_class;
};

typedef enum  {
	DBUS_SERVER_ERROR_SOME_ERROR
} DBusServerError;
#define DBUS_SERVER_ERROR dbus_server_error_quark ()

static gpointer services_dbus_server_parent_class = NULL;
static GOnce services_dbus_server_instance;
static GOnce services_dbus_server_instance = G_ONCE_INIT;

VALA_EXTERN GType services_dbus_server_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ServicesDBusServer, g_object_unref)
VALA_EXTERN guint services_dbus_server_register_object (void* object,
                                            GDBusConnection* connection,
                                            const gchar* path,
                                            GError** error);
VALA_EXTERN ServicesDBusServer* services_dbus_server_get_default (void);
static ServicesDBusServer* __lambda960_ (void);
VALA_EXTERN ServicesDBusServer* services_dbus_server_new (void);
VALA_EXTERN ServicesDBusServer* services_dbus_server_construct (GType object_type);
static gpointer ___lambda960__gthread_func (gpointer self);
VALA_EXTERN void services_dbus_server_add_item (ServicesDBusServer* self,
                                    const gchar* id,
                                    GError** error);
static void services_dbus_server_on_bus_aquired (ServicesDBusServer* self,
                                          GDBusConnection* conn);
static GObject * services_dbus_server_constructor (GType type,
                                            guint n_construct_properties,
                                            GObjectConstructParam * construct_properties);
static void _services_dbus_server___lambda961_ (ServicesDBusServer* self,
                                         GDBusConnection* connection);
static void __services_dbus_server___lambda961__gbus_acquired_callback (GDBusConnection* connection,
                                                                 const gchar* name,
                                                                 gpointer self);
static void _services_dbus_server___lambda962_ (ServicesDBusServer* self);
static void __services_dbus_server___lambda962__gbus_name_acquired_callback (GDBusConnection* connection,
                                                                      const gchar* name,
                                                                      gpointer self);
static void services_dbus_server_finalize (GObject * obj);
static GType services_dbus_server_get_type_once (void);
static void _dbus_services_dbus_server_add_item (ServicesDBusServer* self,
                                          GVariant* _parameters_,
                                          GDBusMethodInvocation* invocation);
static void services_dbus_server_dbus_interface_method_call (GDBusConnection* connection,
                                                      const gchar* sender,
                                                      const gchar* object_path,
                                                      const gchar* interface_name,
                                                      const gchar* method_name,
                                                      GVariant* parameters,
                                                      GDBusMethodInvocation* invocation,
                                                      gpointer user_data);
static GVariant* services_dbus_server_dbus_interface_get_property (GDBusConnection* connection,
                                                            const gchar* sender,
                                                            const gchar* object_path,
                                                            const gchar* interface_name,
                                                            const gchar* property_name,
                                                            GError** error,
                                                            gpointer user_data);
static gboolean services_dbus_server_dbus_interface_set_property (GDBusConnection* connection,
                                                           const gchar* sender,
                                                           const gchar* object_path,
                                                           const gchar* interface_name,
                                                           const gchar* property_name,
                                                           GVariant* value,
                                                           GError** error,
                                                           gpointer user_data);
static void _dbus_services_dbus_server_item_added (GObject* _sender,
                                            const gchar* id,
                                            gpointer* _data);
static void _services_dbus_server_unregister_object (gpointer user_data);
VALA_EXTERN GQuark dbus_server_error_quark (void);
VALA_EXTERN GType dbus_server_error_get_type (void) G_GNUC_CONST ;

static const GDBusArgInfo _services_dbus_server_dbus_arg_info_add_item_id = {-1, "id", "s", NULL};
static const GDBusArgInfo * const _services_dbus_server_dbus_arg_info_add_item_in[] = {&_services_dbus_server_dbus_arg_info_add_item_id, NULL};
static const GDBusArgInfo * const _services_dbus_server_dbus_arg_info_add_item_out[] = {NULL};
static const GDBusMethodInfo _services_dbus_server_dbus_method_info_add_item = {-1, "AddItem", (GDBusArgInfo **) (&_services_dbus_server_dbus_arg_info_add_item_in), (GDBusArgInfo **) (&_services_dbus_server_dbus_arg_info_add_item_out), NULL};
static const GDBusMethodInfo * const _services_dbus_server_dbus_method_info[] = {&_services_dbus_server_dbus_method_info_add_item, NULL};
static const GDBusArgInfo _services_dbus_server_dbus_arg_info_item_added_id = {-1, "id", "s", NULL};
static const GDBusArgInfo * const _services_dbus_server_dbus_arg_info_item_added[] = {&_services_dbus_server_dbus_arg_info_item_added_id, NULL};
static const GDBusSignalInfo _services_dbus_server_dbus_signal_info_item_added = {-1, "ItemAdded", (GDBusArgInfo **) (&_services_dbus_server_dbus_arg_info_item_added), NULL};
static const GDBusSignalInfo * const _services_dbus_server_dbus_signal_info[] = {&_services_dbus_server_dbus_signal_info_item_added, NULL};
static const GDBusPropertyInfo * const _services_dbus_server_dbus_property_info[] = {NULL};
static const GDBusInterfaceInfo _services_dbus_server_dbus_interface_info = {-1, "io.github.alainm23.planify", (GDBusMethodInfo **) (&_services_dbus_server_dbus_method_info), (GDBusSignalInfo **) (&_services_dbus_server_dbus_signal_info), (GDBusPropertyInfo **) (&_services_dbus_server_dbus_property_info), NULL};
static const GDBusInterfaceVTable _services_dbus_server_dbus_interface_vtable = {services_dbus_server_dbus_interface_method_call, services_dbus_server_dbus_interface_get_property, services_dbus_server_dbus_interface_set_property};
static const GDBusErrorEntry dbus_server_error_entries[] = {{DBUS_SERVER_ERROR_SOME_ERROR, "io.github.alainm23.planify.SomeError"}};

static ServicesDBusServer*
__lambda960_ (void)
{
	ServicesDBusServer* _tmp0_;
	ServicesDBusServer* result;
	_tmp0_ = services_dbus_server_new ();
	result = _tmp0_;
	return result;
}

static gpointer
___lambda960__gthread_func (gpointer self)
{
	gpointer result;
	result = __lambda960_ ();
	return result;
}

ServicesDBusServer*
services_dbus_server_get_default (void)
{
	gconstpointer _tmp0_;
	ServicesDBusServer* result;
	_tmp0_ = g_once (&services_dbus_server_instance, ___lambda960__gthread_func, NULL);
	result = (ServicesDBusServer*) _tmp0_;
	return result;
}

void
services_dbus_server_add_item (ServicesDBusServer* self,
                               const gchar* id,
                               GError** error)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (id != NULL);
	g_signal_emit (self, services_dbus_server_signals[SERVICES_DBUS_SERVER_ITEM_ADDED_SIGNAL], 0, id);
}

static void
services_dbus_server_on_bus_aquired (ServicesDBusServer* self,
                                     GDBusConnection* conn)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (conn != NULL);
	{
		ServicesDBusServer* _tmp0_;
		_tmp0_ = services_dbus_server_get_default ();
		services_dbus_server_register_object (_tmp0_, conn, SERVICES_DBUS_SERVER_DBUS_PATH, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		const gchar* _tmp1_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp1_ = e->message;
		g_error ("DBusServer.vala:54: %s", _tmp1_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught 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;
	}
}

ServicesDBusServer*
services_dbus_server_construct (GType object_type)
{
	ServicesDBusServer * self = NULL;
	self = (ServicesDBusServer*) g_object_new (object_type, NULL);
	return self;
}

ServicesDBusServer*
services_dbus_server_new (void)
{
	return services_dbus_server_construct (SERVICES_TYPE_DBUS_SERVER);
}

static void
_services_dbus_server___lambda961_ (ServicesDBusServer* self,
                                    GDBusConnection* connection)
{
	g_return_if_fail (connection != NULL);
	services_dbus_server_on_bus_aquired (self, connection);
}

static void
__services_dbus_server___lambda961__gbus_acquired_callback (GDBusConnection* connection,
                                                            const gchar* name,
                                                            gpointer self)
{
	_services_dbus_server___lambda961_ ((ServicesDBusServer*) self, connection);
}

static void
_services_dbus_server___lambda962_ (ServicesDBusServer* self)
{
}

static void
__services_dbus_server___lambda962__gbus_name_acquired_callback (GDBusConnection* connection,
                                                                 const gchar* name,
                                                                 gpointer self)
{
	_services_dbus_server___lambda962_ ((ServicesDBusServer*) self);
}

static GObject *
services_dbus_server_constructor (GType type,
                                  guint n_construct_properties,
                                  GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ServicesDBusServer * self;
	parent_class = G_OBJECT_CLASS (services_dbus_server_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SERVICES_TYPE_DBUS_SERVER, ServicesDBusServer);
	g_bus_own_name_with_closures (G_BUS_TYPE_SESSION, SERVICES_DBUS_SERVER_DBUS_NAME, G_BUS_NAME_OWNER_FLAGS_NONE, (GClosure*) ((__services_dbus_server___lambda961__gbus_acquired_callback == NULL) ? NULL : g_cclosure_new ((GCallback) __services_dbus_server___lambda961__gbus_acquired_callback, g_object_ref (self), (GClosureNotify) g_object_unref)), (GClosure*) ((__services_dbus_server___lambda962__gbus_name_acquired_callback == NULL) ? NULL : g_cclosure_new ((GCallback) __services_dbus_server___lambda962__gbus_name_acquired_callback, g_object_ref (self), (GClosureNotify) g_object_unref)), (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)));
	return obj;
}

static void
services_dbus_server_class_init (ServicesDBusServerClass * klass,
                                 gpointer klass_data)
{
	services_dbus_server_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->constructor = services_dbus_server_constructor;
	G_OBJECT_CLASS (klass)->finalize = services_dbus_server_finalize;
	services_dbus_server_signals[SERVICES_DBUS_SERVER_ITEM_ADDED_SIGNAL] = g_signal_new ("item-added", SERVICES_TYPE_DBUS_SERVER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
}

static void
services_dbus_server_instance_init (ServicesDBusServer * self,
                                    gpointer klass)
{
}

static void
services_dbus_server_finalize (GObject * obj)
{
	ServicesDBusServer * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SERVICES_TYPE_DBUS_SERVER, ServicesDBusServer);
	G_OBJECT_CLASS (services_dbus_server_parent_class)->finalize (obj);
}

static GType
services_dbus_server_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ServicesDBusServerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) services_dbus_server_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ServicesDBusServer), 0, (GInstanceInitFunc) services_dbus_server_instance_init, NULL };
	GType services_dbus_server_type_id;
	services_dbus_server_type_id = g_type_register_static (G_TYPE_OBJECT, "ServicesDBusServer", &g_define_type_info, 0);
	g_type_set_qdata (services_dbus_server_type_id, g_quark_from_static_string ("vala-dbus-register-object"), (void*) services_dbus_server_register_object);
	return services_dbus_server_type_id;
}

GType
services_dbus_server_get_type (void)
{
	static volatile gsize services_dbus_server_type_id__once = 0;
	if (g_once_init_enter (&services_dbus_server_type_id__once)) {
		GType services_dbus_server_type_id;
		services_dbus_server_type_id = services_dbus_server_get_type_once ();
		g_once_init_leave (&services_dbus_server_type_id__once, services_dbus_server_type_id);
	}
	return services_dbus_server_type_id__once;
}

static void
_dbus_services_dbus_server_add_item (ServicesDBusServer* self,
                                     GVariant* _parameters_,
                                     GDBusMethodInvocation* invocation)
{
	GError* error = NULL;
	GVariantIter _arguments_iter;
	gchar* id = NULL;
	GVariant* _tmp0_;
	GDBusMessage* _reply_message = NULL;
	GVariant* _reply;
	GVariantBuilder _reply_builder;
	g_variant_iter_init (&_arguments_iter, _parameters_);
	_tmp0_ = g_variant_iter_next_value (&_arguments_iter);
	id = g_variant_dup_string (_tmp0_, NULL);
	g_variant_unref (_tmp0_);
	services_dbus_server_add_item (self, id, &error);
	if (error) {
		g_dbus_method_invocation_take_error (invocation, error);
		goto _error;
	}
	_reply_message = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
	g_variant_builder_init (&_reply_builder, G_VARIANT_TYPE_TUPLE);
	_reply = g_variant_builder_end (&_reply_builder);
	g_dbus_message_set_body (_reply_message, _reply);
	g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), _reply_message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
	g_object_unref (invocation);
	g_object_unref (_reply_message);
	_error:
	_g_free0 (id);
	;
}

static void
services_dbus_server_dbus_interface_method_call (GDBusConnection* connection,
                                                 const gchar* sender,
                                                 const gchar* object_path,
                                                 const gchar* interface_name,
                                                 const gchar* method_name,
                                                 GVariant* parameters,
                                                 GDBusMethodInvocation* invocation,
                                                 gpointer user_data)
{
	gpointer* data;
	gpointer object;
	data = user_data;
	object = data[0];
	if (strcmp (method_name, "AddItem") == 0) {
		_dbus_services_dbus_server_add_item (object, parameters, invocation);
	} else {
		g_object_unref (invocation);
	}
}

static GVariant*
services_dbus_server_dbus_interface_get_property (GDBusConnection* connection,
                                                  const gchar* sender,
                                                  const gchar* object_path,
                                                  const gchar* interface_name,
                                                  const gchar* property_name,
                                                  GError** error,
                                                  gpointer user_data)
{
	return NULL;
}

static gboolean
services_dbus_server_dbus_interface_set_property (GDBusConnection* connection,
                                                  const gchar* sender,
                                                  const gchar* object_path,
                                                  const gchar* interface_name,
                                                  const gchar* property_name,
                                                  GVariant* value,
                                                  GError** error,
                                                  gpointer user_data)
{
	return FALSE;
}

static void
_dbus_services_dbus_server_item_added (GObject* _sender,
                                       const gchar* id,
                                       gpointer* _data)
{
	GDBusConnection * _connection;
	const gchar * _path;
	GVariant *_arguments;
	GVariantBuilder _arguments_builder;
	_connection = _data[1];
	_path = _data[2];
	g_variant_builder_init (&_arguments_builder, G_VARIANT_TYPE_TUPLE);
	g_variant_builder_add_value (&_arguments_builder, g_variant_new_string (id));
	_arguments = g_variant_builder_end (&_arguments_builder);
	g_dbus_connection_emit_signal (_connection, NULL, _path, "io.github.alainm23.planify", "ItemAdded", _arguments, NULL);
}

guint
services_dbus_server_register_object (gpointer object,
                                      GDBusConnection* connection,
                                      const gchar* path,
                                      GError** error)
{
	guint result;
	gpointer *data;
	data = g_new (gpointer, 3);
	data[0] = g_object_ref (object);
	data[1] = g_object_ref (connection);
	data[2] = g_strdup (path);
	result = g_dbus_connection_register_object (connection, path, (GDBusInterfaceInfo *) (&_services_dbus_server_dbus_interface_info), &_services_dbus_server_dbus_interface_vtable, data, _services_dbus_server_unregister_object, error);
	if (!result) {
		return 0;
	}
	g_signal_connect (object, "item-added", (GCallback) _dbus_services_dbus_server_item_added, data);
	return result;
}

static void
_services_dbus_server_unregister_object (gpointer user_data)
{
	gpointer* data;
	data = user_data;
	g_signal_handlers_disconnect_by_func (data[0], _dbus_services_dbus_server_item_added, data);
	g_object_unref (data[0]);
	g_object_unref (data[1]);
	g_free (data[2]);
	g_free (data);
}

GQuark
dbus_server_error_quark (void)
{
	static volatile gsize dbus_server_error_quark_volatile = 0;
	g_dbus_error_register_error_domain ("dbus-server-error-quark", &dbus_server_error_quark_volatile, dbus_server_error_entries, G_N_ELEMENTS (dbus_server_error_entries));
	return (GQuark) dbus_server_error_quark_volatile;
}

