/* rygel-energy-management.c generated by valac 0.56.19, the Vala compiler
 * generated from rygel-energy-management.vala, do not modify */

/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: Jussi Kukkonen <jussi.kukkonen@intel.com>
 *
 * 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
 */
/* EnergyManagement service implementation. The service will be run on
 * plugins that have
 *     energy-management=true
 * set in their configuration. It requires UPower to function properly.
 *
 * Every network interface that supports Wake-On should have a
 * configuration group:
 *     [EnergyManagement-eth0]
 *     mode-on-suspend=IP-down-WakeOn
 *     supported-transport=UDP-Broadcast
 *     password=FEEDDEADBEEF
 * mode-on-suspend is required (without it the mode will always be
 * "Unimplemented"), other configuration items are not.
 */

#include <glib-object.h>
#include <gio/gio.h>
#include <string.h>
#include "rygel-core.h"
#include <glib.h>
#include <stdlib.h>
#include <libgupnp/gupnp.h>
#include <libgssdp/gssdp.h>
#include <glib/gi18n-lib.h>

#define RYGEL_ENERGY_MANAGEMENT_TEMPLATE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<NetworkInterfaceInfo xsi:schemaLocation=\"urn:schemas-upnp-org:lp:em-" \
"NetworkInterfaceInfo http://www.upnp.org/schemas/lp/em-NetworkInterfac" \
"eInfo.xsd\" " "                      xmlns=\"urn:schemas-upnp-org:lp:em-NetworkInterf" \
"aceInfo\" " "                      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-ins" \
"tance\">" "%s" "</NetworkInterfaceInfo>"
#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

#define TYPE_UPOWER (upower_get_type ())
#define UPOWER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_UPOWER, UPower))
#define IS_UPOWER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_UPOWER))
#define UPOWER_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TYPE_UPOWER, UPowerIface))

typedef struct _UPower UPower;
typedef struct _UPowerIface UPowerIface;

#define TYPE_UPOWER_PROXY (upower_proxy_get_type ())
enum  {
	UPOWER_SLEEPING_SIGNAL,
	UPOWER_RESUMING_SIGNAL,
	UPOWER_NUM_SIGNALS
};
static guint upower_signals[UPOWER_NUM_SIGNALS] = {0};
typedef GDBusProxy UPowerProxy;
typedef GDBusProxyClass UPowerProxyClass;
enum  {
	RYGEL_ENERGY_MANAGEMENT_0_PROPERTY,
	RYGEL_ENERGY_MANAGEMENT_NUM_PROPERTIES
};
static GParamSpec* rygel_energy_management_properties[RYGEL_ENERGY_MANAGEMENT_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_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))

struct _UPowerIface {
	GTypeInterface parent_iface;
};

struct _RygelEnergyManagementPrivate {
	RygelConfiguration* config;
	gboolean sleeping;
	UPower* upower;
};

static gint RygelEnergyManagement_private_offset;
static gpointer rygel_energy_management_parent_class = NULL;

VALA_EXTERN GType upower_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType upower_proxy_get_type (void) G_GNUC_CONST ;
VALA_EXTERN guint upower_register_object (void* object,
                              GDBusConnection* connection,
                              const gchar* path,
                              GError** error);
static GType upower_get_type_once (void);
static void upower_proxy_g_signal (GDBusProxy* proxy,
                            const gchar* sender_name,
                            const gchar* signal_name,
                            GVariant* parameters);
static void _dbus_handle_upower_sleeping (UPower* self,
                                   GVariant* parameters);
static void _dbus_handle_upower_resuming (UPower* self,
                                   GVariant* parameters);
static void upower_proxy_upower_interface_init (UPowerIface* iface);
static void upower_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* upower_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 upower_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_upower_sleeping (GObject* _sender,
                            gpointer* _data);
static void _dbus_upower_resuming (GObject* _sender,
                            gpointer* _data);
static void _upower_unregister_object (gpointer user_data);
static void rygel_energy_management_real_constructed (GObject* base);
static void rygel_energy_management_upower_sleeping_cb (RygelEnergyManagement* self);
static void _rygel_energy_management_upower_sleeping_cb_upower_sleeping (UPower* _sender,
                                                                  gpointer self);
static void rygel_energy_management_upower_resuming_cb (RygelEnergyManagement* self);
static void _rygel_energy_management_upower_resuming_cb_upower_resuming (UPower* _sender,
                                                                  gpointer self);
static void rygel_energy_management_query_network_interface_info_cb (RygelEnergyManagement* self,
                                                              GUPnPService* em,
                                                              const gchar* var,
                                                              GValue* val);
static void _rygel_energy_management_query_network_interface_info_cb_gupnp_service_query_variable (GUPnPService* _sender,
                                                                                            const gchar* variable,
                                                                                            GValue* value,
                                                                                            gpointer self);
static void rygel_energy_management_query_proxied_network_interface_info_cb (RygelEnergyManagement* self,
                                                                      GUPnPService* em,
                                                                      const gchar* var,
                                                                      GValue* val);
static void _rygel_energy_management_query_proxied_network_interface_info_cb_gupnp_service_query_variable (GUPnPService* _sender,
                                                                                                    const gchar* variable,
                                                                                                    GValue* value,
                                                                                                    gpointer self);
static void rygel_energy_management_get_interface_info_cb (RygelEnergyManagement* self,
                                                    GUPnPService* em,
                                                    GUPnPServiceAction* action);
static void _rygel_energy_management_get_interface_info_cb_gupnp_service_action_invoked (GUPnPService* _sender,
                                                                                  GUPnPServiceAction* action,
                                                                                  gpointer self);
static gchar* rygel_energy_management_create_network_interface_info (RygelEnergyManagement* self);
gboolean rygel_energy_management_get_mac_and_network_type (const gchar* iface,
                                                           gchar** mac,
                                                           gchar** type);
static gchar* rygel_energy_management_create_proxied_network_interface_info (RygelEnergyManagement* self);
static void rygel_energy_management_finalize (GObject * obj);
static GType rygel_energy_management_get_type_once (void);

static const GDBusMethodInfo * const _upower_dbus_method_info[] = {NULL};
static const GDBusArgInfo * const _upower_dbus_arg_info_sleeping[] = {NULL};
static const GDBusSignalInfo _upower_dbus_signal_info_sleeping = {-1, "Sleeping", (GDBusArgInfo **) (&_upower_dbus_arg_info_sleeping), NULL};
static const GDBusArgInfo * const _upower_dbus_arg_info_resuming[] = {NULL};
static const GDBusSignalInfo _upower_dbus_signal_info_resuming = {-1, "Resuming", (GDBusArgInfo **) (&_upower_dbus_arg_info_resuming), NULL};
static const GDBusSignalInfo * const _upower_dbus_signal_info[] = {&_upower_dbus_signal_info_sleeping, &_upower_dbus_signal_info_resuming, NULL};
static const GDBusPropertyInfo * const _upower_dbus_property_info[] = {NULL};
static const GDBusInterfaceInfo _upower_dbus_interface_info = {-1, "org.freedesktop.UPower", (GDBusMethodInfo **) (&_upower_dbus_method_info), (GDBusSignalInfo **) (&_upower_dbus_signal_info), (GDBusPropertyInfo **) (&_upower_dbus_property_info), NULL};
static const GDBusInterfaceVTable _upower_dbus_interface_vtable = {upower_dbus_interface_method_call, upower_dbus_interface_get_property, upower_dbus_interface_set_property};

static void
upower_default_init (UPowerIface * iface,
                     gpointer iface_data)
{
	upower_signals[UPOWER_SLEEPING_SIGNAL] = g_signal_new ("sleeping", TYPE_UPOWER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	upower_signals[UPOWER_RESUMING_SIGNAL] = g_signal_new ("resuming", TYPE_UPOWER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

 G_GNUC_NO_INLINE static GType
upower_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (UPowerIface), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) upower_default_init, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
	GType upower_type_id;
	upower_type_id = g_type_register_static (G_TYPE_INTERFACE, "UPower", &g_define_type_info, 0);
	g_type_interface_add_prerequisite (upower_type_id, G_TYPE_OBJECT);
	g_type_set_qdata (upower_type_id, g_quark_from_static_string ("vala-dbus-proxy-type"), (void*) upower_proxy_get_type);
	g_type_set_qdata (upower_type_id, g_quark_from_static_string ("vala-dbus-interface-name"), "org.freedesktop.UPower");
	g_type_set_qdata (upower_type_id, g_quark_from_static_string ("vala-dbus-interface-info"), (void*) (&_upower_dbus_interface_info));
	g_type_set_qdata (upower_type_id, g_quark_from_static_string ("vala-dbus-register-object"), (void*) upower_register_object);
	return upower_type_id;
}

GType
upower_get_type (void)
{
	static volatile gsize upower_type_id__once = 0;
	if (g_once_init_enter (&upower_type_id__once)) {
		GType upower_type_id;
		upower_type_id = upower_get_type_once ();
		g_once_init_leave (&upower_type_id__once, upower_type_id);
	}
	return upower_type_id__once;
}

G_DEFINE_TYPE_EXTENDED (UPowerProxy, upower_proxy, G_TYPE_DBUS_PROXY, 0, G_IMPLEMENT_INTERFACE (TYPE_UPOWER, upower_proxy_upower_interface_init) )
static void
upower_proxy_class_init (UPowerProxyClass* klass)
{
	G_DBUS_PROXY_CLASS (klass)->g_signal = upower_proxy_g_signal;
}

static void
_dbus_handle_upower_sleeping (UPower* self,
                              GVariant* parameters)
{
	GVariantIter _arguments_iter;
	g_variant_iter_init (&_arguments_iter, parameters);
	g_signal_emit_by_name (self, "sleeping");
}

static void
_dbus_handle_upower_resuming (UPower* self,
                              GVariant* parameters)
{
	GVariantIter _arguments_iter;
	g_variant_iter_init (&_arguments_iter, parameters);
	g_signal_emit_by_name (self, "resuming");
}

static void
upower_proxy_g_signal (GDBusProxy* proxy,
                       const gchar* sender_name,
                       const gchar* signal_name,
                       GVariant* parameters)
{
	if (strcmp (signal_name, "Sleeping") == 0) {
		_dbus_handle_upower_sleeping ((UPower*) proxy, parameters);
	} else if (strcmp (signal_name, "Resuming") == 0) {
		_dbus_handle_upower_resuming ((UPower*) proxy, parameters);
	}
}

static void
upower_proxy_init (UPowerProxy* self)
{
	g_dbus_proxy_set_interface_info (G_DBUS_PROXY (self), (GDBusInterfaceInfo *) (&_upower_dbus_interface_info));
}

static void
upower_proxy_upower_interface_init (UPowerIface* iface)
{
}

static void
upower_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)
{
	g_object_unref (invocation);
}

static GVariant*
upower_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
upower_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_upower_sleeping (GObject* _sender,
                       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);
	_arguments = g_variant_builder_end (&_arguments_builder);
	g_dbus_connection_emit_signal (_connection, NULL, _path, "org.freedesktop.UPower", "Sleeping", _arguments, NULL);
}

static void
_dbus_upower_resuming (GObject* _sender,
                       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);
	_arguments = g_variant_builder_end (&_arguments_builder);
	g_dbus_connection_emit_signal (_connection, NULL, _path, "org.freedesktop.UPower", "Resuming", _arguments, NULL);
}

guint
upower_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 *) (&_upower_dbus_interface_info), &_upower_dbus_interface_vtable, data, _upower_unregister_object, error);
	if (!result) {
		return 0;
	}
	g_signal_connect (object, "sleeping", (GCallback) _dbus_upower_sleeping, data);
	g_signal_connect (object, "resuming", (GCallback) _dbus_upower_resuming, data);
	return result;
}

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

static inline gpointer
rygel_energy_management_get_instance_private (RygelEnergyManagement* self)
{
	return G_STRUCT_MEMBER_P (self, RygelEnergyManagement_private_offset);
}

static void
_rygel_energy_management_upower_sleeping_cb_upower_sleeping (UPower* _sender,
                                                             gpointer self)
{
	rygel_energy_management_upower_sleeping_cb ((RygelEnergyManagement*) self);
}

static void
_rygel_energy_management_upower_resuming_cb_upower_resuming (UPower* _sender,
                                                             gpointer self)
{
	rygel_energy_management_upower_resuming_cb ((RygelEnergyManagement*) self);
}

static void
_rygel_energy_management_query_network_interface_info_cb_gupnp_service_query_variable (GUPnPService* _sender,
                                                                                       const gchar* variable,
                                                                                       GValue* value,
                                                                                       gpointer self)
{
	rygel_energy_management_query_network_interface_info_cb ((RygelEnergyManagement*) self, _sender, variable, value);
}

static void
_rygel_energy_management_query_proxied_network_interface_info_cb_gupnp_service_query_variable (GUPnPService* _sender,
                                                                                               const gchar* variable,
                                                                                               GValue* value,
                                                                                               gpointer self)
{
	rygel_energy_management_query_proxied_network_interface_info_cb ((RygelEnergyManagement*) self, _sender, variable, value);
}

static void
_rygel_energy_management_get_interface_info_cb_gupnp_service_action_invoked (GUPnPService* _sender,
                                                                             GUPnPServiceAction* action,
                                                                             gpointer self)
{
	rygel_energy_management_get_interface_info_cb ((RygelEnergyManagement*) self, _sender, action);
}

static void
rygel_energy_management_real_constructed (GObject* base)
{
	RygelEnergyManagement * self;
	RygelMetaConfig* _tmp0_;
	GError* _inner_error0_ = NULL;
	self = (RygelEnergyManagement*) base;
	G_OBJECT_CLASS (rygel_energy_management_parent_class)->constructed ((GObject*) G_TYPE_CHECK_INSTANCE_CAST (self, gupnp_service_get_type (), GUPnPService));
	_tmp0_ = rygel_meta_config_get_default ();
	_g_object_unref0 (self->priv->config);
	self->priv->config = (RygelConfiguration*) _tmp0_;
	self->priv->sleeping = FALSE;
	{
		UPower* _tmp1_ = NULL;
		UPower* _tmp2_;
		UPower* _tmp3_;
		UPower* _tmp4_;
		UPower* _tmp5_;
		_tmp2_ = (UPower*) g_initable_new (TYPE_UPOWER_PROXY, NULL, &_inner_error0_, "g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, "g-name", "org.freedesktop.UPower", "g-bus-type", G_BUS_TYPE_SYSTEM, "g-object-path", "/org/freedesktop/UPower", "g-interface-name", "org.freedesktop.UPower", NULL);
		_tmp1_ = (UPower*) _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_IO_ERROR) {
				goto __catch0_g_io_error;
			}
			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;
		}
		_tmp3_ = _tmp1_;
		_tmp1_ = NULL;
		_g_object_unref0 (self->priv->upower);
		self->priv->upower = _tmp3_;
		_tmp4_ = self->priv->upower;
		g_signal_connect_object (_tmp4_, "sleeping", (GCallback) _rygel_energy_management_upower_sleeping_cb_upower_sleeping, self, 0);
		_tmp5_ = self->priv->upower;
		g_signal_connect_object (_tmp5_, "resuming", (GCallback) _rygel_energy_management_upower_resuming_cb_upower_resuming, self, 0);
		_g_object_unref0 (_tmp1_);
	}
	goto __finally0;
	__catch0_g_io_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__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;
	}
	g_signal_connect_object ((GUPnPService*) self, "query-variable::NetworkInterfaceInfo", (GCallback) _rygel_energy_management_query_network_interface_info_cb_gupnp_service_query_variable, self, 0);
	g_signal_connect_object ((GUPnPService*) self, "query-variable::ProxiedNetworkInterfaceInfo", (GCallback) _rygel_energy_management_query_proxied_network_interface_info_cb_gupnp_service_query_variable, self, 0);
	g_signal_connect_object ((GUPnPService*) self, "action-invoked::GetInterfaceInfo", (GCallback) _rygel_energy_management_get_interface_info_cb_gupnp_service_action_invoked, self, 0);
}

static void
rygel_energy_management_upower_sleeping_cb (RygelEnergyManagement* self)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	if (self->priv->sleeping == TRUE) {
		return;
	}
	self->priv->sleeping = TRUE;
	_tmp0_ = rygel_energy_management_create_network_interface_info (self);
	_tmp1_ = _tmp0_;
	gupnp_service_notify ((GUPnPService*) self, "NetworkInterfaceInfo", G_TYPE_STRING, _tmp1_, NULL);
	_g_free0 (_tmp1_);
}

static void
rygel_energy_management_upower_resuming_cb (RygelEnergyManagement* self)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	if (self->priv->sleeping == FALSE) {
		return;
	}
	self->priv->sleeping = FALSE;
	_tmp0_ = rygel_energy_management_create_network_interface_info (self);
	_tmp1_ = _tmp0_;
	gupnp_service_notify ((GUPnPService*) self, "NetworkInterfaceInfo", G_TYPE_STRING, _tmp1_, NULL);
	_g_free0 (_tmp1_);
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			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 NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			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 NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	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 NULL;
}

static gchar*
rygel_energy_management_create_network_interface_info (RygelEnergyManagement* self)
{
	gchar* mac_address = NULL;
	gchar* type = NULL;
	gboolean success = FALSE;
	gchar* iface = NULL;
	GUPnPRootDevice* _tmp0_;
	GUPnPRootDevice* _tmp1_;
	GUPnPRootDevice* _tmp2_;
	GUPnPContext* _tmp3_;
	GUPnPContext* _tmp4_;
	const gchar* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* config_section = NULL;
	gchar* _tmp9_;
	gchar* _tmp10_ = NULL;
	gchar* _tmp11_ = NULL;
	gboolean _tmp12_;
	gchar* mac = NULL;
	gchar* _tmp13_;
	gchar* wake_pattern = NULL;
	gchar* _tmp14_;
	GInetAddress* ip_addr = NULL;
	GUPnPRootDevice* _tmp19_;
	GUPnPRootDevice* _tmp20_;
	GUPnPRootDevice* _tmp21_;
	GUPnPContext* _tmp22_;
	GUPnPContext* _tmp23_;
	GInetAddress* _tmp24_;
	GInetAddress* _tmp25_;
	GInetAddress* _tmp26_;
	gboolean _tmp27_ = FALSE;
	GInetAddress* _tmp28_;
	gboolean is_ipv6 = FALSE;
	gint _tmp32_ = 0;
	gint _tmp33_ = 0;
	gchar* associated_ips = NULL;
	GInetAddress* _tmp34_;
	gchar* _tmp35_;
	gchar* _tmp36_;
	gchar* _tmp37_;
	gchar* _tmp38_;
	gchar* mode = NULL;
	gboolean _tmp39_ = FALSE;
	gchar* transport_node = NULL;
	gchar* device_info = NULL;
	GUPnPRootDevice* _tmp52_;
	GUPnPRootDevice* _tmp53_;
	GUPnPRootDevice* _tmp54_;
	const gchar* _tmp55_;
	const gchar* _tmp56_;
	GUPnPRootDevice* _tmp57_;
	GUPnPRootDevice* _tmp58_;
	GUPnPRootDevice* _tmp59_;
	gchar* _tmp60_;
	gchar* _tmp61_;
	const gchar* _tmp62_;
	const gchar* _tmp63_;
	const gchar* _tmp64_;
	const gchar* _tmp65_;
	gchar* _tmp66_;
	gchar* _tmp67_;
	gchar* _tmp68_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	success = TRUE;
	g_object_get ((GUPnPService*) self, "root-device", &_tmp0_, NULL);
	_tmp1_ = _tmp0_;
	_tmp2_ = _tmp1_;
	_tmp3_ = gupnp_device_info_get_context ((GUPnPDeviceInfo*) _tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = gssdp_client_get_interface ((GSSDPClient*) _tmp4_);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_strdup (_tmp6_);
	_tmp8_ = _tmp7_;
	_g_object_unref0 (_tmp2_);
	iface = _tmp8_;
	_tmp9_ = g_strdup_printf ("EnergyManagement-%s", iface);
	config_section = _tmp9_;
	_tmp12_ = rygel_energy_management_get_mac_and_network_type (iface, &_tmp10_, &_tmp11_);
	_g_free0 (mac_address);
	mac_address = _tmp10_;
	_g_free0 (type);
	type = _tmp11_;
	success = _tmp12_;
	_tmp13_ = string_replace (mac_address, ":", "");
	mac = _tmp13_;
	_tmp14_ = g_strdup_printf ("FFFFFFFFFFFF%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", mac, mac, mac, mac, mac, mac, mac, mac, mac, mac, mac, mac, mac, mac, mac, mac);
	wake_pattern = _tmp14_;
	{
		gchar* password = NULL;
		RygelConfiguration* _tmp15_;
		gchar* _tmp16_;
		const gchar* _tmp17_;
		gchar* _tmp18_;
		_tmp15_ = self->priv->config;
		_tmp16_ = rygel_configuration_get_string (_tmp15_, config_section, "password", &_inner_error0_);
		password = _tmp16_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp17_ = wake_pattern;
		_tmp18_ = g_strconcat (_tmp17_, password, NULL);
		_g_free0 (wake_pattern);
		wake_pattern = _tmp18_;
		_g_free0 (password);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (wake_pattern);
		_g_free0 (mac);
		_g_free0 (config_section);
		_g_free0 (iface);
		_g_free0 (type);
		_g_free0 (mac_address);
		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 NULL;
	}
	g_object_get ((GUPnPService*) self, "root-device", &_tmp19_, NULL);
	_tmp20_ = _tmp19_;
	_tmp21_ = _tmp20_;
	_tmp22_ = gupnp_device_info_get_context ((GUPnPDeviceInfo*) _tmp21_);
	_tmp23_ = _tmp22_;
	_tmp24_ = gssdp_client_get_address ((GSSDPClient*) _tmp23_);
	_tmp25_ = _tmp24_;
	_tmp26_ = _tmp25_;
	_g_object_unref0 (_tmp21_);
	ip_addr = _tmp26_;
	_tmp28_ = ip_addr;
	if (_tmp28_ != NULL) {
		GInetAddress* _tmp29_;
		GSocketFamily _tmp30_;
		GSocketFamily _tmp31_;
		_tmp29_ = ip_addr;
		_tmp30_ = g_inet_address_get_family (_tmp29_);
		_tmp31_ = _tmp30_;
		_tmp27_ = _tmp31_ == G_SOCKET_FAMILY_IPV6;
	} else {
		_tmp27_ = FALSE;
	}
	is_ipv6 = _tmp27_;
	if (is_ipv6) {
		_tmp32_ = 6;
	} else {
		_tmp32_ = 4;
	}
	if (is_ipv6) {
		_tmp33_ = 6;
	} else {
		_tmp33_ = 4;
	}
	_tmp34_ = ip_addr;
	_tmp35_ = g_inet_address_to_string (_tmp34_);
	_tmp36_ = _tmp35_;
	_tmp37_ = g_strdup_printf ("<Ipv%d>%s</Ipv%d>", _tmp32_, _tmp36_, _tmp33_);
	_tmp38_ = _tmp37_;
	_g_free0 (_tmp36_);
	associated_ips = _tmp38_;
	if (!success) {
		_tmp39_ = TRUE;
	} else {
		UPower* _tmp40_;
		_tmp40_ = self->priv->upower;
		_tmp39_ = _tmp40_ == NULL;
	}
	if (_tmp39_) {
		gchar* _tmp41_;
		_tmp41_ = g_strdup ("Unimplemented");
		_g_free0 (mode);
		mode = _tmp41_;
	} else {
		{
			gchar* sleep_mode = NULL;
			RygelConfiguration* _tmp42_;
			gchar* _tmp43_;
			const gchar* _tmp44_ = NULL;
			gchar* _tmp46_;
			_tmp42_ = self->priv->config;
			_tmp43_ = rygel_configuration_get_string (_tmp42_, config_section, "mode-on-suspend", &_inner_error0_);
			sleep_mode = _tmp43_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
			if (self->priv->sleeping) {
				const gchar* _tmp45_;
				_tmp45_ = sleep_mode;
				_tmp44_ = _tmp45_;
			} else {
				_tmp44_ = "IP-up";
			}
			_tmp46_ = g_strdup (_tmp44_);
			_g_free0 (mode);
			mode = _tmp46_;
			_g_free0 (sleep_mode);
		}
		goto __finally1;
		__catch1_g_error:
		{
			gchar* _tmp47_;
			g_clear_error (&_inner_error0_);
			_tmp47_ = g_strdup ("Unimplemented");
			_g_free0 (mode);
			mode = _tmp47_;
		}
		__finally1:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (mode);
			_g_free0 (associated_ips);
			_g_object_unref0 (ip_addr);
			_g_free0 (wake_pattern);
			_g_free0 (mac);
			_g_free0 (config_section);
			_g_free0 (iface);
			_g_free0 (type);
			_g_free0 (mac_address);
			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 NULL;
		}
	}
	{
		gchar* val = NULL;
		RygelConfiguration* _tmp48_;
		gchar* _tmp49_;
		gchar* _tmp50_;
		_tmp48_ = self->priv->config;
		_tmp49_ = rygel_configuration_get_string (_tmp48_, config_section, "supported-transport", &_inner_error0_);
		val = _tmp49_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch2_g_error;
		}
		_tmp50_ = g_strdup_printf ("<WakeSupportedTransport>%s</WakeSupportedTransport>", val);
		_g_free0 (transport_node);
		transport_node = _tmp50_;
		_g_free0 (val);
	}
	goto __finally2;
	__catch2_g_error:
	{
		gchar* _tmp51_;
		g_clear_error (&_inner_error0_);
		_tmp51_ = g_strdup ("");
		_g_free0 (transport_node);
		transport_node = _tmp51_;
	}
	__finally2:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (transport_node);
		_g_free0 (mode);
		_g_free0 (associated_ips);
		_g_object_unref0 (ip_addr);
		_g_free0 (wake_pattern);
		_g_free0 (mac);
		_g_free0 (config_section);
		_g_free0 (iface);
		_g_free0 (type);
		_g_free0 (mac_address);
		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 NULL;
	}
	g_object_get ((GUPnPService*) self, "root-device", &_tmp52_, NULL);
	_tmp53_ = _tmp52_;
	_tmp54_ = _tmp53_;
	_tmp55_ = gupnp_device_info_get_udn ((GUPnPDeviceInfo*) _tmp54_);
	_tmp56_ = _tmp55_;
	g_object_get ((GUPnPService*) self, "root-device", &_tmp57_, NULL);
	_tmp58_ = _tmp57_;
	_tmp59_ = _tmp58_;
	_tmp60_ = gupnp_device_info_get_friendly_name ((GUPnPDeviceInfo*) _tmp59_);
	_tmp61_ = _tmp60_;
	_tmp62_ = mode;
	_tmp63_ = associated_ips;
	_tmp64_ = wake_pattern;
	_tmp65_ = transport_node;
	_tmp66_ = g_strdup_printf ("<DeviceInterface>" "<DeviceUUID>%s</DeviceUUID>" "<FriendlyName>%s</FriendlyName>" "<NetworkInterface>" "<SystemName>%s</SystemName>" "<MacAddress>%s</MacAddress>" "<InterfaceType>%s</InterfaceType>" "<NetworkInterfaceMode>%s</NetworkInterfaceMode>" "<AssociatedIpAddresses>%s</AssociatedIpAddresses>" "<WakeOnPattern>%s</WakeOnPattern>" "%s" "</NetworkInterface>" "</DeviceInterface>", _tmp56_, _tmp61_, iface, mac_address, type, _tmp62_, _tmp63_, _tmp64_, _tmp65_);
	_tmp67_ = _tmp66_;
	_g_free0 (_tmp61_);
	_g_object_unref0 (_tmp59_);
	_g_object_unref0 (_tmp54_);
	device_info = _tmp67_;
	_tmp68_ = g_strdup_printf (RYGEL_ENERGY_MANAGEMENT_TEMPLATE, device_info);
	result = _tmp68_;
	_g_free0 (device_info);
	_g_free0 (transport_node);
	_g_free0 (mode);
	_g_free0 (associated_ips);
	_g_object_unref0 (ip_addr);
	_g_free0 (wake_pattern);
	_g_free0 (mac);
	_g_free0 (config_section);
	_g_free0 (iface);
	_g_free0 (type);
	_g_free0 (mac_address);
	return result;
}

static gchar*
rygel_energy_management_create_proxied_network_interface_info (RygelEnergyManagement* self)
{
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup_printf (RYGEL_ENERGY_MANAGEMENT_TEMPLATE, "");
	result = _tmp0_;
	return result;
}

static void
rygel_energy_management_query_network_interface_info_cb (RygelEnergyManagement* self,
                                                         GUPnPService* em,
                                                         const gchar* var,
                                                         GValue* val)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (em != NULL);
	g_return_if_fail (var != NULL);
	g_return_if_fail (val != NULL);
	g_value_init (val, G_TYPE_STRING);
	_tmp0_ = rygel_energy_management_create_network_interface_info (self);
	_tmp1_ = _tmp0_;
	g_value_set_string (val, _tmp1_);
	_g_free0 (_tmp1_);
}

static void
rygel_energy_management_query_proxied_network_interface_info_cb (RygelEnergyManagement* self,
                                                                 GUPnPService* em,
                                                                 const gchar* var,
                                                                 GValue* val)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (em != NULL);
	g_return_if_fail (var != NULL);
	g_return_if_fail (val != NULL);
	g_value_init (val, G_TYPE_STRING);
	_tmp0_ = rygel_energy_management_create_proxied_network_interface_info (self);
	_tmp1_ = _tmp0_;
	g_value_set_string (val, _tmp1_);
	_g_free0 (_tmp1_);
}

static void
rygel_energy_management_get_interface_info_cb (RygelEnergyManagement* self,
                                               GUPnPService* em,
                                               GUPnPServiceAction* action)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (em != NULL);
	g_return_if_fail (action != NULL);
	if (gupnp_service_action_get_argument_count (action) != ((guint) 0)) {
		gupnp_service_action_return_error (action, (guint) 402, _ ("Invalid argument"));
		return;
	}
	_tmp0_ = rygel_energy_management_create_network_interface_info (self);
	_tmp1_ = _tmp0_;
	gupnp_service_action_set (action, "NetworkInterfaceInfo", G_TYPE_STRING, _tmp1_, NULL);
	_g_free0 (_tmp1_);
	_tmp2_ = rygel_energy_management_create_proxied_network_interface_info (self);
	_tmp3_ = _tmp2_;
	gupnp_service_action_set (action, "ProxiedNetworkInterfaceInfo", G_TYPE_STRING, _tmp3_, NULL);
	_g_free0 (_tmp3_);
	gupnp_service_action_return_success (action);
}

RygelEnergyManagement*
rygel_energy_management_construct (GType object_type)
{
	RygelEnergyManagement * self = NULL;
	self = (RygelEnergyManagement*) g_object_new (object_type, NULL);
	return self;
}

RygelEnergyManagement*
rygel_energy_management_new (void)
{
	return rygel_energy_management_construct (RYGEL_TYPE_ENERGY_MANAGEMENT);
}

static void
rygel_energy_management_class_init (RygelEnergyManagementClass * klass,
                                    gpointer klass_data)
{
	rygel_energy_management_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &RygelEnergyManagement_private_offset);
	((GObjectClass *) klass)->constructed = (void (*) (GObject*)) rygel_energy_management_real_constructed;
	G_OBJECT_CLASS (klass)->finalize = rygel_energy_management_finalize;
}

static void
rygel_energy_management_instance_init (RygelEnergyManagement * self,
                                       gpointer klass)
{
	self->priv = rygel_energy_management_get_instance_private (self);
}

static void
rygel_energy_management_finalize (GObject * obj)
{
	RygelEnergyManagement * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_ENERGY_MANAGEMENT, RygelEnergyManagement);
	_g_object_unref0 (self->priv->config);
	_g_object_unref0 (self->priv->upower);
	G_OBJECT_CLASS (rygel_energy_management_parent_class)->finalize (obj);
}

/**
 * Implementation of UPnP EnergyManagement service.
 */
 G_GNUC_NO_INLINE static GType
rygel_energy_management_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (RygelEnergyManagementClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_energy_management_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelEnergyManagement), 0, (GInstanceInitFunc) rygel_energy_management_instance_init, NULL };
	GType rygel_energy_management_type_id;
	rygel_energy_management_type_id = g_type_register_static (gupnp_service_get_type (), "RygelEnergyManagement", &g_define_type_info, 0);
	RygelEnergyManagement_private_offset = g_type_add_instance_private (rygel_energy_management_type_id, sizeof (RygelEnergyManagementPrivate));
	return rygel_energy_management_type_id;
}

GType
rygel_energy_management_get_type (void)
{
	static volatile gsize rygel_energy_management_type_id__once = 0;
	if (g_once_init_enter (&rygel_energy_management_type_id__once)) {
		GType rygel_energy_management_type_id;
		rygel_energy_management_type_id = rygel_energy_management_get_type_once ();
		g_once_init_leave (&rygel_energy_management_type_id__once, rygel_energy_management_type_id);
	}
	return rygel_energy_management_type_id__once;
}

