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

/*
* Copyright (c) 2015-2017 elementary LLC (http://launchpad.net/wingpanel-indicator-network)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 2.1 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "networkapplet.h"
#include <glib-object.h>
#include <NetworkManager.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gio/gio.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  {
	NETWORK_ETHER_INTERFACE_0_PROPERTY,
	NETWORK_ETHER_INTERFACE_NUM_PROPERTIES
};
static GParamSpec* network_ether_interface_properties[NETWORK_ETHER_INTERFACE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block3Data Block3Data;
#define _g_ptr_array_unref0(var) ((var == NULL) ? NULL : (var = (g_ptr_array_unref (var), NULL)))

struct _NetworkEtherInterfacePrivate {
	NetworkWidgetsSwitch* ethernet_item;
};

struct _Block3Data {
	int _ref_count_;
	NetworkEtherInterface* self;
	NMClient* nm_client;
};

static gint NetworkEtherInterface_private_offset;
static gpointer network_ether_interface_parent_class = NULL;
static GType network_ether_interface_type_id = 0;

static Block3Data* block3_data_ref (Block3Data* _data3_);
static void block3_data_unref (void * _userdata_);
static void __lambda15_ (NetworkEtherInterface* self);
static void ___lambda15__g_object_notify (GObject* _sender,
                                   GParamSpec* pspec,
                                   gpointer self);
static void __lambda16_ (Block3Data* _data3_);
static void ____lambda17_ (NetworkEtherInterface* self);
static void _____lambda17__gasync_ready_callback (GObject* source_object,
                                           GAsyncResult* res,
                                           gpointer self);
static void ___lambda16__g_object_notify (GObject* _sender,
                                   GParamSpec* pspec,
                                   gpointer self);
static void __lambda18_ (NetworkEtherInterface* self);
static void ___lambda18__nm_device_state_changed (NMDevice* _sender,
                                           guint new_state,
                                           guint old_state,
                                           guint reason,
                                           gpointer self);
static void network_ether_interface_real_update (NetworkWidgetNMInterface* base);
static void network_ether_interface_finalize (GObject * obj);

static inline gpointer
network_ether_interface_get_instance_private (NetworkEtherInterface* self)
{
	return G_STRUCT_MEMBER_P (self, NetworkEtherInterface_private_offset);
}

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

static Block3Data*
block3_data_ref (Block3Data* _data3_)
{
	g_atomic_int_inc (&_data3_->_ref_count_);
	return _data3_;
}

static void
block3_data_unref (void * _userdata_)
{
	Block3Data* _data3_;
	_data3_ = (Block3Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data3_->_ref_count_)) {
		NetworkEtherInterface* self;
		self = _data3_->self;
		_g_object_unref0 (_data3_->nm_client);
		_g_object_unref0 (self);
		g_slice_free (Block3Data, _data3_);
	}
}

static void
__lambda15_ (NetworkEtherInterface* self)
{
	NetworkWidgetsSwitch* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	_tmp0_ = self->priv->ethernet_item;
	_tmp1_ = network_widget_nm_interface_get_display_title ((NetworkWidgetNMInterface*) self);
	_tmp2_ = _tmp1_;
	network_widgets_switch_set_caption (_tmp0_, _tmp2_);
}

static void
___lambda15__g_object_notify (GObject* _sender,
                              GParamSpec* pspec,
                              gpointer self)
{
	__lambda15_ ((NetworkEtherInterface*) self);
}

static gpointer
_g_ptr_array_ref0 (gpointer self)
{
	return self ? g_ptr_array_ref (self) : NULL;
}

static void
____lambda17_ (NetworkEtherInterface* self)
{
	g_debug ("EtherInterface.vala:43: Successfully disconnected.");
}

static void
_____lambda17__gasync_ready_callback (GObject* source_object,
                                      GAsyncResult* res,
                                      gpointer self)
{
	____lambda17_ ((NetworkEtherInterface*) self);
	g_object_unref (self);
}

static void
__lambda16_ (Block3Data* _data3_)
{
	NetworkEtherInterface* self;
	gboolean _tmp0_ = FALSE;
	NetworkWidgetsSwitch* _tmp1_;
	gboolean _tmp2_;
	gboolean _tmp3_;
	self = _data3_->self;
	g_debug ("EtherInterface.vala:32: update");
	_tmp1_ = self->priv->ethernet_item;
	_tmp2_ = network_widgets_switch_get_active (_tmp1_);
	_tmp3_ = _tmp2_;
	if (_tmp3_) {
		NMDevice* _tmp4_;
		_tmp4_ = ((NetworkWidgetNMInterface*) self)->device;
		_tmp0_ = nm_device_get_state (_tmp4_) == NM_DEVICE_STATE_DISCONNECTED;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		NMConnection* connection = NULL;
		NMConnection* _tmp5_;
		GPtrArray* remote_array = NULL;
		NMDevice* _tmp6_;
		GPtrArray* _tmp7_;
		GPtrArray* _tmp8_;
		GPtrArray* _tmp9_;
		_tmp5_ = nm_simple_connection_new ();
		connection = _tmp5_;
		_tmp6_ = ((NetworkWidgetNMInterface*) self)->device;
		_tmp7_ = nm_device_get_available_connections (_tmp6_);
		_tmp8_ = _g_ptr_array_ref0 (_tmp7_);
		remote_array = _tmp8_;
		_tmp9_ = remote_array;
		if (_tmp9_ == NULL) {
			g_critical ("EtherInterface.vala:37: Unable to find an ethernet connection to activ" \
"ate");
		} else {
			NMConnection* _tmp10_;
			GPtrArray* _tmp11_;
			gconstpointer _tmp12_;
			const gchar* _tmp13_;
			NMConnection* _tmp14_;
			NMDevice* _tmp15_;
			_tmp10_ = connection;
			_tmp11_ = remote_array;
			_tmp12_ = g_ptr_array_index (_tmp11_, (guint) 0);
			_tmp13_ = nm_connection_get_path ((NMConnection*) ((NMRemoteConnection*) _tmp12_));
			nm_connection_set_path (_tmp10_, _tmp13_);
			_tmp14_ = connection;
			_tmp15_ = ((NetworkWidgetNMInterface*) self)->device;
			nm_client_activate_connection_async (_data3_->nm_client, _tmp14_, _tmp15_, NULL, NULL, NULL, NULL);
		}
		_g_ptr_array_unref0 (remote_array);
		_g_object_unref0 (connection);
	} else {
		gboolean _tmp16_ = FALSE;
		NetworkWidgetsSwitch* _tmp17_;
		gboolean _tmp18_;
		gboolean _tmp19_;
		_tmp17_ = self->priv->ethernet_item;
		_tmp18_ = network_widgets_switch_get_active (_tmp17_);
		_tmp19_ = _tmp18_;
		if (!_tmp19_) {
			NMDevice* _tmp20_;
			_tmp20_ = ((NetworkWidgetNMInterface*) self)->device;
			_tmp16_ = nm_device_get_state (_tmp20_) == NM_DEVICE_STATE_ACTIVATED;
		} else {
			_tmp16_ = FALSE;
		}
		if (_tmp16_) {
			NMDevice* _tmp21_;
			_tmp21_ = ((NetworkWidgetNMInterface*) self)->device;
			nm_device_disconnect_async (_tmp21_, NULL, _____lambda17__gasync_ready_callback, g_object_ref (self));
		}
	}
}

static void
___lambda16__g_object_notify (GObject* _sender,
                              GParamSpec* pspec,
                              gpointer self)
{
	__lambda16_ (self);
}

static void
__lambda18_ (NetworkEtherInterface* self)
{
	network_widget_nm_interface_update ((NetworkWidgetNMInterface*) self);
}

static void
___lambda18__nm_device_state_changed (NMDevice* _sender,
                                      guint new_state,
                                      guint old_state,
                                      guint reason,
                                      gpointer self)
{
	__lambda18_ ((NetworkEtherInterface*) self);
}

NetworkEtherInterface*
network_ether_interface_construct (GType object_type,
                                   NMClient* nm_client,
                                   NMDevice* _device)
{
	NetworkEtherInterface * self = NULL;
	Block3Data* _data3_;
	NMClient* _tmp0_;
	NMDevice* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp3_;
	NetworkWidgetsSwitch* _tmp4_;
	NetworkWidgetsSwitch* _tmp5_;
	GtkStyleContext* _tmp6_;
	NetworkWidgetsSwitch* _tmp7_;
	NetworkWidgetsSwitch* _tmp8_;
	NMDevice* _tmp9_;
	g_return_val_if_fail (nm_client != NULL, NULL);
	_data3_ = g_slice_new0 (Block3Data);
	_data3_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (nm_client);
	_g_object_unref0 (_data3_->nm_client);
	_data3_->nm_client = _tmp0_;
	self = (NetworkEtherInterface*) network_abstract_ether_interface_construct (object_type);
	_data3_->self = g_object_ref (self);
	_tmp1_ = _g_object_ref0 (_device);
	_g_object_unref0 (((NetworkWidgetNMInterface*) self)->device);
	((NetworkWidgetNMInterface*) self)->device = _tmp1_;
	_tmp2_ = network_widget_nm_interface_get_display_title ((NetworkWidgetNMInterface*) self);
	_tmp3_ = _tmp2_;
	_tmp4_ = network_widgets_switch_new (_tmp3_);
	g_object_ref_sink (_tmp4_);
	_g_object_unref0 (self->priv->ethernet_item);
	self->priv->ethernet_item = _tmp4_;
	g_signal_connect_object ((GObject*) self, "notify::display-title", (GCallback) ___lambda15__g_object_notify, self, 0);
	_tmp5_ = self->priv->ethernet_item;
	_tmp6_ = gtk_widget_get_style_context ((GtkWidget*) _tmp5_);
	gtk_style_context_add_class (_tmp6_, "h4");
	_tmp7_ = self->priv->ethernet_item;
	g_signal_connect_data ((GObject*) _tmp7_, "notify::active", (GCallback) ___lambda16__g_object_notify, block3_data_ref (_data3_), (GClosureNotify) block3_data_unref, 0);
	_tmp8_ = self->priv->ethernet_item;
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) _tmp8_);
	_tmp9_ = ((NetworkWidgetNMInterface*) self)->device;
	g_signal_connect_object (_tmp9_, "state-changed", (GCallback) ___lambda18__nm_device_state_changed, self, 0);
	block3_data_unref (_data3_);
	_data3_ = NULL;
	return self;
}

NetworkEtherInterface*
network_ether_interface_new (NMClient* nm_client,
                             NMDevice* _device)
{
	return network_ether_interface_construct (NETWORK_TYPE_ETHER_INTERFACE, nm_client, _device);
}

static void
network_ether_interface_real_update (NetworkWidgetNMInterface* base)
{
	NetworkEtherInterface * self;
	NMDevice* _tmp0_;
	self = (NetworkEtherInterface*) base;
	_tmp0_ = ((NetworkWidgetNMInterface*) self)->device;
	switch (nm_device_get_state (_tmp0_)) {
		case NM_DEVICE_STATE_UNKNOWN:
		case NM_DEVICE_STATE_UNMANAGED:
		case NM_DEVICE_STATE_DEACTIVATING:
		case NM_DEVICE_STATE_FAILED:
		{
			NetworkWidgetsSwitch* _tmp1_;
			NetworkWidgetsSwitch* _tmp2_;
			_tmp1_ = self->priv->ethernet_item;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp1_, FALSE);
			_tmp2_ = self->priv->ethernet_item;
			network_widgets_switch_set_active (_tmp2_, FALSE);
			network_widget_nm_interface_set_state ((NetworkWidgetNMInterface*) self, NETWORK_STATE_FAILED_WIRED);
			break;
		}
		case NM_DEVICE_STATE_UNAVAILABLE:
		{
			NetworkWidgetsSwitch* _tmp3_;
			NetworkWidgetsSwitch* _tmp4_;
			_tmp3_ = self->priv->ethernet_item;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp3_, FALSE);
			_tmp4_ = self->priv->ethernet_item;
			network_widgets_switch_set_active (_tmp4_, FALSE);
			network_widget_nm_interface_set_state ((NetworkWidgetNMInterface*) self, NETWORK_STATE_WIRED_UNPLUGGED);
			break;
		}
		case NM_DEVICE_STATE_DISCONNECTED:
		{
			NetworkWidgetsSwitch* _tmp5_;
			NetworkWidgetsSwitch* _tmp6_;
			_tmp5_ = self->priv->ethernet_item;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp5_, TRUE);
			_tmp6_ = self->priv->ethernet_item;
			network_widgets_switch_set_active (_tmp6_, FALSE);
			network_widget_nm_interface_set_state ((NetworkWidgetNMInterface*) self, NETWORK_STATE_WIRED_UNPLUGGED);
			break;
		}
		case NM_DEVICE_STATE_PREPARE:
		case NM_DEVICE_STATE_CONFIG:
		case NM_DEVICE_STATE_NEED_AUTH:
		case NM_DEVICE_STATE_IP_CONFIG:
		case NM_DEVICE_STATE_IP_CHECK:
		case NM_DEVICE_STATE_SECONDARIES:
		{
			NetworkWidgetsSwitch* _tmp7_;
			NetworkWidgetsSwitch* _tmp8_;
			_tmp7_ = self->priv->ethernet_item;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp7_, TRUE);
			_tmp8_ = self->priv->ethernet_item;
			network_widgets_switch_set_active (_tmp8_, TRUE);
			network_widget_nm_interface_set_state ((NetworkWidgetNMInterface*) self, NETWORK_STATE_CONNECTING_WIRED);
			break;
		}
		case NM_DEVICE_STATE_ACTIVATED:
		{
			NetworkWidgetsSwitch* _tmp9_;
			NetworkWidgetsSwitch* _tmp10_;
			_tmp9_ = self->priv->ethernet_item;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp9_, TRUE);
			_tmp10_ = self->priv->ethernet_item;
			network_widgets_switch_set_active (_tmp10_, TRUE);
			network_widget_nm_interface_set_state ((NetworkWidgetNMInterface*) self, NETWORK_STATE_CONNECTED_WIRED);
			break;
		}
		default:
		break;
	}
}

static void
network_ether_interface_class_init (NetworkEtherInterfaceClass * klass,
                                    gpointer klass_data)
{
	network_ether_interface_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &NetworkEtherInterface_private_offset);
	((NetworkWidgetNMInterfaceClass *) klass)->update = (void (*) (NetworkWidgetNMInterface*)) network_ether_interface_real_update;
	G_OBJECT_CLASS (klass)->finalize = network_ether_interface_finalize;
}

static void
network_ether_interface_instance_init (NetworkEtherInterface * self,
                                       gpointer klass)
{
	self->priv = network_ether_interface_get_instance_private (self);
}

static void
network_ether_interface_finalize (GObject * obj)
{
	NetworkEtherInterface * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, NETWORK_TYPE_ETHER_INTERFACE, NetworkEtherInterface);
	_g_object_unref0 (self->priv->ethernet_item);
	G_OBJECT_CLASS (network_ether_interface_parent_class)->finalize (obj);
}

GType
network_ether_interface_get_type (void)
{
	return network_ether_interface_type_id;
}

GType
network_ether_interface_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (NetworkEtherInterfaceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) network_ether_interface_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (NetworkEtherInterface), 0, (GInstanceInitFunc) network_ether_interface_instance_init, NULL };
	network_ether_interface_type_id = g_type_module_register_type (module, NETWORK_TYPE_ABSTRACT_ETHER_INTERFACE, "NetworkEtherInterface", &g_define_type_info, 0);
	NetworkEtherInterface_private_offset = sizeof (NetworkEtherInterfacePrivate);
	return network_ether_interface_type_id;
}

