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

/*
* Copyright 2020 elementary, Inc.
*
* 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 2 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
*/

#include "slingshot.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <gio/gunixsocketaddress.h>
#include <unistd.h>
#include <glib/gstdio.h>

#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

enum  {
	SYNAPSE_WORKER_LINK_0_PROPERTY,
	SYNAPSE_WORKER_LINK_ADDRESS_PROPERTY,
	SYNAPSE_WORKER_LINK_NUM_PROPERTIES
};
static GParamSpec* synapse_worker_link_properties[SYNAPSE_WORKER_LINK_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
enum  {
	SYNAPSE_WORKER_LINK_ON_CONNECTION_ACCEPTED_SIGNAL,
	SYNAPSE_WORKER_LINK_NUM_SIGNALS
};
static guint synapse_worker_link_signals[SYNAPSE_WORKER_LINK_NUM_SIGNALS] = {0};

struct _SynapseWorkerLinkPrivate {
	gchar* _address;
	GDBusServer* dbus_server;
	GDBusAuthObserver* auth_observer;
};

static gint SynapseWorkerLink_private_offset;
static gpointer synapse_worker_link_parent_class = NULL;
static GType synapse_worker_link_type_id = 0;

static void synapse_worker_link_set_address (SynapseWorkerLink* self,
                                      const gchar* value);
static GObject * synapse_worker_link_constructor (GType type,
                                           guint n_construct_properties,
                                           GObjectConstructParam * construct_properties);
static gboolean _synapse_worker_link___lambda122_ (SynapseWorkerLink* self,
                                            const gchar* mechanism);
static gboolean __synapse_worker_link___lambda122__g_dbus_auth_observer_allow_mechanism (GDBusAuthObserver* _sender,
                                                                                  const gchar* mechanism,
                                                                                  gpointer self);
static gboolean _synapse_worker_link___lambda123_ (SynapseWorkerLink* self,
                                            GIOStream* stream,
                                            GCredentials* credentials);
static gboolean __synapse_worker_link___lambda123__g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver* _sender,
                                                                                               GIOStream* stream,
                                                                                               GCredentials* credentials,
                                                                                               gpointer self);
static gboolean _synapse_worker_link____lambda124_ (SynapseWorkerLink* self,
                                             GDBusConnection* connection);
static gboolean __synapse_worker_link____lambda124__g_dbus_server_new_connection (GDBusServer* _sender,
                                                                           GDBusConnection* connection,
                                                                           gpointer self);
static void synapse_worker_link_finalize (GObject * obj);
static void _vala_synapse_worker_link_get_property (GObject * object,
                                             guint property_id,
                                             GValue * value,
                                             GParamSpec * pspec);
static void _vala_synapse_worker_link_set_property (GObject * object,
                                             guint property_id,
                                             const GValue * value,
                                             GParamSpec * pspec);

static inline gpointer
synapse_worker_link_get_instance_private (SynapseWorkerLink* self)
{
	return G_STRUCT_MEMBER_P (self, SynapseWorkerLink_private_offset);
}

void
synapse_worker_link_start (SynapseWorkerLink* self)
{
	const gchar* _tmp0_;
	GDBusServer* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_address;
	g_debug ("worker-link.vala:89: D-Bus Server listening at %s", _tmp0_);
	_tmp1_ = self->priv->dbus_server;
	g_dbus_server_start (_tmp1_);
}

SynapseWorkerLink*
synapse_worker_link_construct (GType object_type)
{
	SynapseWorkerLink * self = NULL;
	self = (SynapseWorkerLink*) g_object_new (object_type, NULL);
	return self;
}

SynapseWorkerLink*
synapse_worker_link_new (void)
{
	return synapse_worker_link_construct (SYNAPSE_TYPE_WORKER_LINK);
}

const gchar*
synapse_worker_link_get_address (SynapseWorkerLink* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_address;
	result = _tmp0_;
	return result;
}

static void
synapse_worker_link_set_address (SynapseWorkerLink* self,
                                 const gchar* value)
{
	gchar* old_value;
	g_return_if_fail (self != NULL);
	old_value = synapse_worker_link_get_address (self);
	if (g_strcmp0 (value, old_value) != 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (value);
		_g_free0 (self->priv->_address);
		self->priv->_address = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, synapse_worker_link_properties[SYNAPSE_WORKER_LINK_ADDRESS_PROPERTY]);
	}
}

static gboolean
_synapse_worker_link___lambda122_ (SynapseWorkerLink* self,
                                   const gchar* mechanism)
{
	gboolean result;
	g_return_val_if_fail (mechanism != NULL, FALSE);
	result = g_strcmp0 (mechanism, "EXTERNAL") == 0;
	return result;
}

static gboolean
__synapse_worker_link___lambda122__g_dbus_auth_observer_allow_mechanism (GDBusAuthObserver* _sender,
                                                                         const gchar* mechanism,
                                                                         gpointer self)
{
	gboolean result;
	result = _synapse_worker_link___lambda122_ ((SynapseWorkerLink*) self, mechanism);
	return result;
}

static gboolean
_synapse_worker_link___lambda123_ (SynapseWorkerLink* self,
                                   GIOStream* stream,
                                   GCredentials* credentials)
{
	GCredentials* own_credentials = NULL;
	GCredentials* _tmp0_;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (stream != NULL, FALSE);
	if (credentials == NULL) {
		result = FALSE;
		return result;
	}
	_tmp0_ = g_credentials_new ();
	own_credentials = _tmp0_;
	{
		gboolean _tmp1_ = FALSE;
		GCredentials* _tmp2_;
		_tmp2_ = own_credentials;
		_tmp1_ = g_credentials_is_same_user (credentials, _tmp2_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		result = _tmp1_;
		_g_object_unref0 (own_credentials);
		return result;
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
		result = FALSE;
		_g_object_unref0 (own_credentials);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp3_ = FALSE;
		_g_object_unref0 (own_credentials);
		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 _tmp3_;
	}
	_g_object_unref0 (own_credentials);
}

static gboolean
__synapse_worker_link___lambda123__g_dbus_auth_observer_authorize_authenticated_peer (GDBusAuthObserver* _sender,
                                                                                      GIOStream* stream,
                                                                                      GCredentials* credentials,
                                                                                      gpointer self)
{
	gboolean result;
	result = _synapse_worker_link___lambda123_ ((SynapseWorkerLink*) self, stream, credentials);
	return result;
}

static gboolean
_synapse_worker_link____lambda124_ (SynapseWorkerLink* self,
                                    GDBusConnection* connection)
{
	GCredentials* credentials = NULL;
	GCredentials* _tmp0_;
	GCredentials* _tmp1_;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (connection != NULL, FALSE);
	g_dbus_connection_set_exit_on_close (connection, FALSE);
	_tmp0_ = g_dbus_connection_get_peer_credentials (connection);
	credentials = _tmp0_;
	_tmp1_ = credentials;
	if (_tmp1_ == NULL) {
		result = FALSE;
		return result;
	}
	{
		GCredentials* _tmp2_;
		_tmp2_ = credentials;
		g_credentials_get_unix_user (_tmp2_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
		result = FALSE;
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp3_ = FALSE;
		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 _tmp3_;
	}
	g_signal_emit (self, synapse_worker_link_signals[SYNAPSE_WORKER_LINK_ON_CONNECTION_ACCEPTED_SIGNAL], 0, connection);
	result = TRUE;
	return result;
}

static gboolean
__synapse_worker_link____lambda124__g_dbus_server_new_connection (GDBusServer* _sender,
                                                                  GDBusConnection* connection,
                                                                  gpointer self)
{
	gboolean result;
	result = _synapse_worker_link____lambda124_ ((SynapseWorkerLink*) self, connection);
	return result;
}

static GObject *
synapse_worker_link_constructor (GType type,
                                 guint n_construct_properties,
                                 GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	SynapseWorkerLink * self;
	gchar* guid = NULL;
	gchar* _tmp8_;
	GDBusAuthObserver* _tmp9_;
	GDBusAuthObserver* _tmp10_;
	GDBusAuthObserver* _tmp11_;
	GError* _inner_error0_ = NULL;
	parent_class = G_OBJECT_CLASS (synapse_worker_link_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SYNAPSE_TYPE_WORKER_LINK, SynapseWorkerLink);
	if (g_unix_socket_address_abstract_names_supported ()) {
		gchar* _tmp0_;
		gchar* _tmp1_;
		_tmp0_ = g_strdup_printf ("unix:abstract=/tmp/dbus-applications-menu-%u", (guint) getpid ());
		_tmp1_ = _tmp0_;
		synapse_worker_link_set_address (self, _tmp1_);
		_g_free0 (_tmp1_);
	} else {
		{
			gchar* tmpdir = NULL;
			gchar* _tmp2_;
			const gchar* _tmp3_;
			gchar* _tmp4_;
			gchar* _tmp5_;
			_tmp2_ = g_dir_make_tmp ("dbus-applications-menu-XXXXXX", &_inner_error0_);
			tmpdir = _tmp2_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
			_tmp3_ = tmpdir;
			_tmp4_ = g_strdup_printf ("unix:tmpdir=%s", _tmp3_);
			_tmp5_ = _tmp4_;
			synapse_worker_link_set_address (self, _tmp5_);
			_g_free0 (_tmp5_);
			_g_free0 (tmpdir);
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* e = NULL;
			GError* _tmp6_;
			const gchar* _tmp7_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp6_ = e;
			_tmp7_ = _tmp6_->message;
			g_error ("worker-link.vala:35: Failed to determine temporary directory for D-Bus" \
": %s", _tmp7_);
			_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_);
		}
	}
	_tmp8_ = g_dbus_generate_guid ();
	guid = _tmp8_;
	_tmp9_ = g_dbus_auth_observer_new ();
	_g_object_unref0 (self->priv->auth_observer);
	self->priv->auth_observer = _tmp9_;
	_tmp10_ = self->priv->auth_observer;
	g_signal_connect_object (_tmp10_, "allow-mechanism", (GCallback) __synapse_worker_link___lambda122__g_dbus_auth_observer_allow_mechanism, self, 0);
	_tmp11_ = self->priv->auth_observer;
	g_signal_connect_object (_tmp11_, "authorize-authenticated-peer", (GCallback) __synapse_worker_link___lambda123__g_dbus_auth_observer_authorize_authenticated_peer, self, 0);
	{
		GDBusServer* _tmp12_ = NULL;
		const gchar* _tmp13_;
		const gchar* _tmp14_;
		GDBusAuthObserver* _tmp15_;
		GDBusServer* _tmp16_;
		GDBusServer* _tmp17_;
		GDBusServer* _tmp18_;
		_tmp13_ = self->priv->_address;
		_tmp14_ = guid;
		_tmp15_ = self->priv->auth_observer;
		_tmp16_ = g_dbus_server_new_sync (_tmp13_, G_DBUS_SERVER_FLAGS_NONE, _tmp14_, _tmp15_, NULL, &_inner_error0_);
		_tmp12_ = _tmp16_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch1_g_error;
		}
		_tmp17_ = _tmp12_;
		_tmp12_ = NULL;
		_g_object_unref0 (self->priv->dbus_server);
		self->priv->dbus_server = _tmp17_;
		_tmp18_ = self->priv->dbus_server;
		g_signal_connect_object (_tmp18_, "new-connection", (GCallback) __synapse_worker_link____lambda124__g_dbus_server_new_connection, self, 0);
		_g_object_unref0 (_tmp12_);
	}
	goto __finally1;
	__catch1_g_error:
	{
		GError* e = NULL;
		GError* _tmp19_;
		const gchar* _tmp20_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp19_ = e;
		_tmp20_ = _tmp19_->message;
		g_error ("worker-link.vala:83: Failed to create D-Bus server: %s", _tmp20_);
		_g_error_free0 (e);
	}
	__finally1:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (guid);
		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_);
	}
	_g_free0 (guid);
	return obj;
}

static void
synapse_worker_link_class_init (SynapseWorkerLinkClass * klass,
                                gpointer klass_data)
{
	synapse_worker_link_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &SynapseWorkerLink_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_synapse_worker_link_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_synapse_worker_link_set_property;
	G_OBJECT_CLASS (klass)->constructor = synapse_worker_link_constructor;
	G_OBJECT_CLASS (klass)->finalize = synapse_worker_link_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), SYNAPSE_WORKER_LINK_ADDRESS_PROPERTY, synapse_worker_link_properties[SYNAPSE_WORKER_LINK_ADDRESS_PROPERTY] = g_param_spec_string ("address", "address", "address", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	synapse_worker_link_signals[SYNAPSE_WORKER_LINK_ON_CONNECTION_ACCEPTED_SIGNAL] = g_signal_new ("on-connection-accepted", SYNAPSE_TYPE_WORKER_LINK, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, g_dbus_connection_get_type ());
}

static void
synapse_worker_link_instance_init (SynapseWorkerLink * self,
                                   gpointer klass)
{
	self->priv = synapse_worker_link_get_instance_private (self);
}

static void
synapse_worker_link_finalize (GObject * obj)
{
	SynapseWorkerLink * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SYNAPSE_TYPE_WORKER_LINK, SynapseWorkerLink);
	_g_free0 (self->priv->_address);
	_g_object_unref0 (self->priv->dbus_server);
	_g_object_unref0 (self->priv->auth_observer);
	G_OBJECT_CLASS (synapse_worker_link_parent_class)->finalize (obj);
}

GType
synapse_worker_link_get_type (void)
{
	return synapse_worker_link_type_id;
}

GType
synapse_worker_link_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (SynapseWorkerLinkClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) synapse_worker_link_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SynapseWorkerLink), 0, (GInstanceInitFunc) synapse_worker_link_instance_init, NULL };
	synapse_worker_link_type_id = g_type_module_register_type (module, G_TYPE_OBJECT, "SynapseWorkerLink", &g_define_type_info, 0);
	SynapseWorkerLink_private_offset = sizeof (SynapseWorkerLinkPrivate);
	return synapse_worker_link_type_id;
}

static void
_vala_synapse_worker_link_get_property (GObject * object,
                                        guint property_id,
                                        GValue * value,
                                        GParamSpec * pspec)
{
	SynapseWorkerLink * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SYNAPSE_TYPE_WORKER_LINK, SynapseWorkerLink);
	switch (property_id) {
		case SYNAPSE_WORKER_LINK_ADDRESS_PROPERTY:
		g_value_set_string (value, synapse_worker_link_get_address (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_synapse_worker_link_set_property (GObject * object,
                                        guint property_id,
                                        const GValue * value,
                                        GParamSpec * pspec)
{
	SynapseWorkerLink * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SYNAPSE_TYPE_WORKER_LINK, SynapseWorkerLink);
	switch (property_id) {
		case SYNAPSE_WORKER_LINK_ADDRESS_PROPERTY:
		synapse_worker_link_set_address (self, g_value_get_string (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

