/* RygelStartupManager.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from RygelStartupManager.vala, do not modify */

/*
 * Copyright (c) 2011-2024 elementary, Inc. (https://elementaryos.org)
 *
 * 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 "io.elementary.settings.sharing.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>

#define SHARING_BACKEND_RYGEL_STARTUP_MANAGER_RYGEL_SERVICE_NAME "rygel"
#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  {
	SHARING_BACKEND_RYGEL_STARTUP_MANAGER_0_PROPERTY,
	SHARING_BACKEND_RYGEL_STARTUP_MANAGER_NUM_PROPERTIES
};
static GParamSpec* sharing_backend_rygel_startup_manager_properties[SHARING_BACKEND_RYGEL_STARTUP_MANAGER_NUM_PROPERTIES];
#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)))
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _SharingBackendRygelStartupManagerSetServiceEnabledData SharingBackendRygelStartupManagerSetServiceEnabledData;

struct _SharingBackendRygelStartupManagerPrivate {
	SharingBackendSharingDBusInterface* sharing;
};

struct _SharingBackendRygelStartupManagerSetServiceEnabledData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	SharingBackendRygelStartupManager* self;
	gboolean enable;
	SharingBackendSharingDBusInterface* _tmp0_;
	GError* e;
	GError* _tmp1_;
	const gchar* _tmp2_;
	SharingBackendSharingDBusInterface* _tmp3_;
	SharingBackendSharingDBusInterface* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	GError* _vala1_e;
	GError* _tmp8_;
	const gchar* _tmp9_;
	GError* _inner_error0_;
};

static gint SharingBackendRygelStartupManager_private_offset;
static gpointer sharing_backend_rygel_startup_manager_parent_class = NULL;

static void sharing_backend_rygel_startup_manager_set_service_enabled_data_free (gpointer _data);
static gboolean sharing_backend_rygel_startup_manager_set_service_enabled_co (SharingBackendRygelStartupManagerSetServiceEnabledData* _data_);
static void _vala_SharingBackendSharingDBusInterfaceSharingNetwork_array_free (SharingBackendSharingDBusInterfaceSharingNetwork * array,
                                                                        gssize array_length);
static GObject * sharing_backend_rygel_startup_manager_constructor (GType type,
                                                             guint n_construct_properties,
                                                             GObjectConstructParam * construct_properties);
static void sharing_backend_rygel_startup_manager_finalize (GObject * obj);
static GType sharing_backend_rygel_startup_manager_get_type_once (void);

static inline gpointer
sharing_backend_rygel_startup_manager_get_instance_private (SharingBackendRygelStartupManager* self)
{
	return G_STRUCT_MEMBER_P (self, SharingBackendRygelStartupManager_private_offset);
}

static void
sharing_backend_rygel_startup_manager_set_service_enabled_data_free (gpointer _data)
{
	SharingBackendRygelStartupManagerSetServiceEnabledData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (SharingBackendRygelStartupManagerSetServiceEnabledData, _data_);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

void
sharing_backend_rygel_startup_manager_set_service_enabled (SharingBackendRygelStartupManager* self,
                                                           gboolean enable,
                                                           GAsyncReadyCallback _callback_,
                                                           gpointer _user_data_)
{
	SharingBackendRygelStartupManagerSetServiceEnabledData* _data_;
	SharingBackendRygelStartupManager* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (SharingBackendRygelStartupManagerSetServiceEnabledData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, sharing_backend_rygel_startup_manager_set_service_enabled_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_data_->enable = enable;
	sharing_backend_rygel_startup_manager_set_service_enabled_co (_data_);
}

void
sharing_backend_rygel_startup_manager_set_service_enabled_finish (SharingBackendRygelStartupManager* self,
                                                                  GAsyncResult* _res_)
{
	SharingBackendRygelStartupManagerSetServiceEnabledData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static gboolean
sharing_backend_rygel_startup_manager_set_service_enabled_co (SharingBackendRygelStartupManagerSetServiceEnabledData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	if (_data_->enable) {
		{
			_data_->_tmp0_ = _data_->self->priv->sharing;
			sharing_backend_sharing_dbus_interface_enable_service (_data_->_tmp0_, SHARING_BACKEND_RYGEL_STARTUP_MANAGER_RYGEL_SERVICE_NAME, &_data_->_inner_error0_);
			if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			_data_->e = _data_->_inner_error0_;
			_data_->_inner_error0_ = NULL;
			_data_->_tmp1_ = _data_->e;
			_data_->_tmp2_ = _data_->_tmp1_->message;
			g_warning ("RygelStartupManager.vala:41: Enabling media server failed: %s", _data_->_tmp2_);
			_g_error_free0 (_data_->e);
		}
		__finally0:
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
			g_clear_error (&_data_->_inner_error0_);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
	} else {
		{
			_data_->_tmp3_ = _data_->self->priv->sharing;
			_data_->_tmp4_ = _data_->self->priv->sharing;
			_data_->_tmp5_ = sharing_backend_sharing_dbus_interface_get_current_network (_data_->_tmp4_);
			_data_->_tmp6_ = _data_->_tmp5_;
			_data_->_tmp7_ = _data_->_tmp6_;
			sharing_backend_sharing_dbus_interface_disable_service (_data_->_tmp3_, SHARING_BACKEND_RYGEL_STARTUP_MANAGER_RYGEL_SERVICE_NAME, _data_->_tmp7_, &_data_->_inner_error0_);
			_g_free0 (_data_->_tmp7_);
			if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
		}
		goto __finally1;
		__catch1_g_error:
		{
			_data_->_vala1_e = _data_->_inner_error0_;
			_data_->_inner_error0_ = NULL;
			_data_->_tmp8_ = _data_->_vala1_e;
			_data_->_tmp9_ = _data_->_tmp8_->message;
			g_warning ("RygelStartupManager.vala:47: Disabling media server failed: %s", _data_->_tmp9_);
			_g_error_free0 (_data_->_vala1_e);
		}
		__finally1:
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
			g_clear_error (&_data_->_inner_error0_);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
	}
	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 void
_vala_SharingBackendSharingDBusInterfaceSharingNetwork_array_free (SharingBackendSharingDBusInterfaceSharingNetwork * array,
                                                                   gssize array_length)
{
	if (array != NULL) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			sharing_backend_sharing_dbus_interface_sharing_network_destroy (&array[i]);
		}
	}
	g_free (array);
}

gboolean
sharing_backend_rygel_startup_manager_get_service_enabled (SharingBackendRygelStartupManager* self)
{
	SharingBackendSharingDBusInterfaceSharingNetwork* networks = NULL;
	gint networks_length1 = 0;
	gint _networks_size_ = 0;
	gchar* current_network = NULL;
	SharingBackendSharingDBusInterface* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp10_;
	SharingBackendSharingDBusInterfaceSharingNetwork* _tmp11_;
	gint _tmp11__length1;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	{
		SharingBackendSharingDBusInterfaceSharingNetwork* _tmp0_ = NULL;
		SharingBackendSharingDBusInterface* _tmp1_;
		gint _tmp2_ = 0;
		SharingBackendSharingDBusInterfaceSharingNetwork* _tmp3_;
		gint _tmp0__length1;
		gint __tmp0__size_;
		SharingBackendSharingDBusInterfaceSharingNetwork* _tmp4_;
		gint _tmp4__length1;
		_tmp1_ = self->priv->sharing;
		_tmp3_ = sharing_backend_sharing_dbus_interface_list_networks (_tmp1_, SHARING_BACKEND_RYGEL_STARTUP_MANAGER_RYGEL_SERVICE_NAME, &_tmp2_, &_inner_error0_);
		_tmp0_ = _tmp3_;
		_tmp0__length1 = _tmp2_;
		__tmp0__size_ = _tmp0__length1;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp4_ = _tmp0_;
		_tmp4__length1 = _tmp0__length1;
		_tmp0_ = NULL;
		_tmp0__length1 = 0;
		networks = (_vala_SharingBackendSharingDBusInterfaceSharingNetwork_array_free (networks, networks_length1), NULL);
		networks = _tmp4_;
		networks_length1 = _tmp4__length1;
		_networks_size_ = networks_length1;
		_tmp0_ = (_vala_SharingBackendSharingDBusInterfaceSharingNetwork_array_free (_tmp0_, _tmp0__length1), NULL);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp5_;
		const gchar* _tmp6_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp5_ = e;
		_tmp6_ = _tmp5_->message;
		g_warning ("RygelStartupManager.vala:57: Getting media server status failed: %s", _tmp6_);
		result = FALSE;
		_g_error_free0 (e);
		networks = (_vala_SharingBackendSharingDBusInterfaceSharingNetwork_array_free (networks, networks_length1), NULL);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp7_ = FALSE;
		networks = (_vala_SharingBackendSharingDBusInterfaceSharingNetwork_array_free (networks, networks_length1), 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 _tmp7_;
	}
	_tmp8_ = self->priv->sharing;
	_tmp9_ = sharing_backend_sharing_dbus_interface_get_current_network (_tmp8_);
	_tmp10_ = _tmp9_;
	current_network = _tmp10_;
	_tmp11_ = networks;
	_tmp11__length1 = networks_length1;
	{
		SharingBackendSharingDBusInterfaceSharingNetwork* network_collection = NULL;
		gint network_collection_length1 = 0;
		gint _network_collection_size_ = 0;
		gint network_it = 0;
		network_collection = _tmp11_;
		network_collection_length1 = _tmp11__length1;
		for (network_it = 0; network_it < network_collection_length1; network_it = network_it + 1) {
			SharingBackendSharingDBusInterfaceSharingNetwork _tmp12_;
			SharingBackendSharingDBusInterfaceSharingNetwork _tmp13_ = {0};
			SharingBackendSharingDBusInterfaceSharingNetwork network = {0};
			_tmp12_ = network_collection[network_it];
			sharing_backend_sharing_dbus_interface_sharing_network_copy (&_tmp12_, &_tmp13_);
			network = _tmp13_;
			{
				SharingBackendSharingDBusInterfaceSharingNetwork _tmp14_;
				const gchar* _tmp15_;
				const gchar* _tmp16_;
				_tmp14_ = network;
				_tmp15_ = _tmp14_.uuid;
				_tmp16_ = current_network;
				if (g_strcmp0 (_tmp15_, _tmp16_) == 0) {
					result = TRUE;
					sharing_backend_sharing_dbus_interface_sharing_network_destroy (&network);
					_g_free0 (current_network);
					networks = (_vala_SharingBackendSharingDBusInterfaceSharingNetwork_array_free (networks, networks_length1), NULL);
					return result;
				}
				sharing_backend_sharing_dbus_interface_sharing_network_destroy (&network);
			}
		}
	}
	result = FALSE;
	_g_free0 (current_network);
	networks = (_vala_SharingBackendSharingDBusInterfaceSharingNetwork_array_free (networks, networks_length1), NULL);
	return result;
}

SharingBackendRygelStartupManager*
sharing_backend_rygel_startup_manager_construct (GType object_type)
{
	SharingBackendRygelStartupManager * self = NULL;
	self = (SharingBackendRygelStartupManager*) g_object_new (object_type, NULL);
	return self;
}

SharingBackendRygelStartupManager*
sharing_backend_rygel_startup_manager_new (void)
{
	return sharing_backend_rygel_startup_manager_construct (SHARING_BACKEND_TYPE_RYGEL_STARTUP_MANAGER);
}

static GObject *
sharing_backend_rygel_startup_manager_constructor (GType type,
                                                   guint n_construct_properties,
                                                   GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	SharingBackendRygelStartupManager * self;
	GError* _inner_error0_ = NULL;
	parent_class = G_OBJECT_CLASS (sharing_backend_rygel_startup_manager_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SHARING_BACKEND_TYPE_RYGEL_STARTUP_MANAGER, SharingBackendRygelStartupManager);
	{
		SharingBackendSharingDBusInterface* _tmp0_ = NULL;
		SharingBackendSharingDBusInterface* _tmp1_;
		SharingBackendSharingDBusInterface* _tmp2_;
		_tmp1_ = (SharingBackendSharingDBusInterface*) g_initable_new (SHARING_BACKEND_TYPE_SHARING_DBUS_INTERFACE_PROXY, NULL, &_inner_error0_, "g-flags", 0, "g-name", SHARING_BACKEND_SHARING_DBUS_INTERFACE_SERVICE_NAME, "g-bus-type", G_BUS_TYPE_SESSION, "g-object-path", SHARING_BACKEND_SHARING_DBUS_INTERFACE_OBJECT_PATH, "g-interface-name", "org.gnome.SettingsDaemon.Sharing", NULL);
		_tmp0_ = (SharingBackendSharingDBusInterface*) _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_object_unref0 (self->priv->sharing);
		self->priv->sharing = _tmp2_;
		_g_object_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		g_warning ("RygelStartupManager.vala:32: Getting Sharing proxy failed: %s", _tmp4_);
		_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 obj;
}

static void
sharing_backend_rygel_startup_manager_class_init (SharingBackendRygelStartupManagerClass * klass,
                                                  gpointer klass_data)
{
	sharing_backend_rygel_startup_manager_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &SharingBackendRygelStartupManager_private_offset);
	G_OBJECT_CLASS (klass)->constructor = sharing_backend_rygel_startup_manager_constructor;
	G_OBJECT_CLASS (klass)->finalize = sharing_backend_rygel_startup_manager_finalize;
}

static void
sharing_backend_rygel_startup_manager_instance_init (SharingBackendRygelStartupManager * self,
                                                     gpointer klass)
{
	self->priv = sharing_backend_rygel_startup_manager_get_instance_private (self);
	self->priv->sharing = NULL;
}

static void
sharing_backend_rygel_startup_manager_finalize (GObject * obj)
{
	SharingBackendRygelStartupManager * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SHARING_BACKEND_TYPE_RYGEL_STARTUP_MANAGER, SharingBackendRygelStartupManager);
	_g_object_unref0 (self->priv->sharing);
	G_OBJECT_CLASS (sharing_backend_rygel_startup_manager_parent_class)->finalize (obj);
}

static GType
sharing_backend_rygel_startup_manager_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (SharingBackendRygelStartupManagerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) sharing_backend_rygel_startup_manager_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SharingBackendRygelStartupManager), 0, (GInstanceInitFunc) sharing_backend_rygel_startup_manager_instance_init, NULL };
	GType sharing_backend_rygel_startup_manager_type_id;
	sharing_backend_rygel_startup_manager_type_id = g_type_register_static (G_TYPE_OBJECT, "SharingBackendRygelStartupManager", &g_define_type_info, 0);
	SharingBackendRygelStartupManager_private_offset = g_type_add_instance_private (sharing_backend_rygel_startup_manager_type_id, sizeof (SharingBackendRygelStartupManagerPrivate));
	return sharing_backend_rygel_startup_manager_type_id;
}

GType
sharing_backend_rygel_startup_manager_get_type (void)
{
	static volatile gsize sharing_backend_rygel_startup_manager_type_id__once = 0;
	if (g_once_init_enter (&sharing_backend_rygel_startup_manager_type_id__once)) {
		GType sharing_backend_rygel_startup_manager_type_id;
		sharing_backend_rygel_startup_manager_type_id = sharing_backend_rygel_startup_manager_get_type_once ();
		g_once_init_leave (&sharing_backend_rygel_startup_manager_type_id__once, sharing_backend_rygel_startup_manager_type_id);
	}
	return sharing_backend_rygel_startup_manager_type_id__once;
}

