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

/*-
 * Copyright (c) 2017 elementary LLC.
 *
 * This program 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.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: David Hewitt <davidmhewitt@gmail.com>
 */

#include "network.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <glib-object.h>
#include <NetworkManager.h>
#include <gio/gio.h>
#include <switchboard.h>
#include <glib/gi18n-lib.h>
#include <stdlib.h>
#include <string.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_WIDGETS_MODEM_INTERFACE_0_PROPERTY,
	NETWORK_WIDGETS_MODEM_INTERFACE_NUM_PROPERTIES
};
static GParamSpec* network_widgets_modem_interface_properties[NETWORK_WIDGETS_MODEM_INTERFACE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block4Data Block4Data;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _NetworkWidgetsModemInterfacePrivate {
	GtkRevealer* top_revealer;
};

struct _Block4Data {
	int _ref_count_;
	NetworkWidgetsModemInterface* self;
	GtkButton* settings_button;
};

static gint NetworkWidgetsModemInterface_private_offset;
static gpointer network_widgets_modem_interface_parent_class = NULL;

static Block4Data* block4_data_ref (Block4Data* _data4_);
static void block4_data_unref (void * _userdata_);
static void _network_widgets_page_update_nm_device_state_changed (NMDevice* _sender,
                                                           guint new_state,
                                                           guint old_state,
                                                           guint reason,
                                                           gpointer self);
static void _network_widgets_page_edit_connections_gtk_button_clicked (GtkButton* _sender,
                                                                gpointer self);
static void _network_widgets_page_open_advanced_settings_gtk_button_clicked (GtkButton* _sender,
                                                                      gpointer self);
static void __lambda34_ (Block4Data* _data4_);
static void ___lambda34__g_object_notify (GObject* _sender,
                                   GParamSpec* pspec,
                                   gpointer self);
static void network_widgets_modem_interface_real_update_name (NetworkWidgetsPage* base,
                                                       gint count);
static void network_widgets_modem_interface_real_update (NetworkWidgetsPage* base);
static void network_widgets_modem_interface_finalize (GObject * obj);
static GType network_widgets_modem_interface_get_type_once (void);

static inline gpointer
network_widgets_modem_interface_get_instance_private (NetworkWidgetsModemInterface* self)
{
	return G_STRUCT_MEMBER_P (self, NetworkWidgetsModemInterface_private_offset);
}

static Block4Data*
block4_data_ref (Block4Data* _data4_)
{
	g_atomic_int_inc (&_data4_->_ref_count_);
	return _data4_;
}

static void
block4_data_unref (void * _userdata_)
{
	Block4Data* _data4_;
	_data4_ = (Block4Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data4_->_ref_count_)) {
		NetworkWidgetsModemInterface* self;
		self = _data4_->self;
		_g_object_unref0 (_data4_->settings_button);
		_g_object_unref0 (self);
		g_slice_free (Block4Data, _data4_);
	}
}

static void
_network_widgets_page_update_nm_device_state_changed (NMDevice* _sender,
                                                      guint new_state,
                                                      guint old_state,
                                                      guint reason,
                                                      gpointer self)
{
	network_widgets_page_update ((NetworkWidgetsPage*) self);
}

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

static void
_network_widgets_page_edit_connections_gtk_button_clicked (GtkButton* _sender,
                                                           gpointer self)
{
	network_widgets_page_edit_connections ((NetworkWidgetsPage*) self);
}

static void
_network_widgets_page_open_advanced_settings_gtk_button_clicked (GtkButton* _sender,
                                                                 gpointer self)
{
	network_widgets_page_open_advanced_settings ((NetworkWidgetsPage*) self);
}

static void
__lambda34_ (Block4Data* _data4_)
{
	NetworkWidgetsModemInterface* self;
	const gchar* _tmp0_;
	self = _data4_->self;
	_tmp0_ = ((NetworkWidgetsPage*) self)->uuid;
	gtk_widget_set_sensitive ((GtkWidget*) _data4_->settings_button, g_strcmp0 (_tmp0_, "") != 0);
}

static void
___lambda34__g_object_notify (GObject* _sender,
                              GParamSpec* pspec,
                              gpointer self)
{
	__lambda34_ (self);
}

NetworkWidgetsModemInterface*
network_widgets_modem_interface_construct (GType object_type,
                                           NMDevice* device)
{
	NetworkWidgetsModemInterface * self = NULL;
	Block4Data* _data4_;
	GThemedIcon* _tmp0_;
	GThemedIcon* _tmp1_;
	NetworkWidgetsInfoBox* _tmp2_;
	NetworkWidgetsInfoBox* _tmp3_;
	GtkWidget* _tmp4_;
	GtkRevealer* _tmp5_ = NULL;
	GtkRevealer* _tmp6_;
	GtkButton* connections_button = NULL;
	GtkButton* _tmp7_;
	GtkButton* _tmp8_;
	const gchar* _tmp9_;
	g_return_val_if_fail (device != NULL, NULL);
	_data4_ = g_slice_new0 (Block4Data);
	_data4_->_ref_count_ = 1;
	_tmp0_ = (GThemedIcon*) g_themed_icon_new ("network-cellular");
	_tmp1_ = _tmp0_;
	self = (NetworkWidgetsModemInterface*) g_object_new (object_type, "activatable", TRUE, "device", device, "icon", _tmp1_, NULL);
	_data4_->self = g_object_ref (self);
	_g_object_unref0 (_tmp1_);
	g_signal_connect_object (device, "state-changed", (GCallback) _network_widgets_page_update_nm_device_state_changed, (NetworkWidgetsPage*) self, 0);
	_tmp2_ = ((NetworkWidgetsPage*) self)->info_box;
	gtk_widget_set_halign ((GtkWidget*) _tmp2_, GTK_ALIGN_CENTER);
	_tmp3_ = ((NetworkWidgetsPage*) self)->info_box;
	_tmp4_ = _g_object_ref0 ((GtkWidget*) _tmp3_);
	_tmp5_ = (GtkRevealer*) gtk_revealer_new ();
	gtk_widget_set_valign ((GtkWidget*) _tmp5_, GTK_ALIGN_START);
	gtk_revealer_set_transition_type (_tmp5_, GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN);
	gtk_revealer_set_child (_tmp5_, _tmp4_);
	_g_object_unref0 (_tmp4_);
	g_object_ref_sink (_tmp5_);
	_g_object_unref0 (self->priv->top_revealer);
	self->priv->top_revealer = _tmp5_;
	_tmp6_ = self->priv->top_revealer;
	switchboard_settings_page_set_child ((SwitchboardSettingsPage*) self, (GtkWidget*) _tmp6_);
	_tmp7_ = switchboard_settings_page_add_button ((SwitchboardSettingsPage*) self, _ ("Edit Connections…"));
	connections_button = _tmp7_;
	g_signal_connect_object (connections_button, "clicked", (GCallback) _network_widgets_page_edit_connections_gtk_button_clicked, (NetworkWidgetsPage*) self, 0);
	_tmp8_ = switchboard_settings_page_add_button ((SwitchboardSettingsPage*) self, _ ("Advanced Settings…"));
	_data4_->settings_button = _tmp8_;
	g_signal_connect_object (_data4_->settings_button, "clicked", (GCallback) _network_widgets_page_open_advanced_settings_gtk_button_clicked, (NetworkWidgetsPage*) self, 0);
	_tmp9_ = ((NetworkWidgetsPage*) self)->uuid;
	gtk_widget_set_sensitive ((GtkWidget*) _data4_->settings_button, g_strcmp0 (_tmp9_, "") != 0);
	g_signal_connect_data ((GObject*) self, "notify::uuid", (GCallback) ___lambda34__g_object_notify, block4_data_ref (_data4_), (GClosureNotify) block4_data_unref, 0);
	network_widgets_page_update ((NetworkWidgetsPage*) self);
	_g_object_unref0 (connections_button);
	block4_data_unref (_data4_);
	_data4_ = NULL;
	return self;
}

NetworkWidgetsModemInterface*
network_widgets_modem_interface_new (NMDevice* device)
{
	return network_widgets_modem_interface_construct (NETWORK_WIDGETS_TYPE_MODEM_INTERFACE, device);
}

static void
network_widgets_modem_interface_real_update_name (NetworkWidgetsPage* base,
                                                  gint count)
{
	NetworkWidgetsModemInterface * self;
	NMDevice* _tmp0_;
	NMDevice* _tmp1_;
	self = (NetworkWidgetsModemInterface*) base;
	_tmp0_ = network_widgets_page_get_device ((NetworkWidgetsPage*) self);
	_tmp1_ = _tmp0_;
	if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp1_, nm_device_modem_get_type ())) {
		NMDeviceModemCapabilities capabilities = 0U;
		NMDevice* _tmp2_;
		NMDevice* _tmp3_;
		_tmp2_ = network_widgets_page_get_device ((NetworkWidgetsPage*) self);
		_tmp3_ = _tmp2_;
		capabilities = nm_device_modem_get_current_capabilities (G_TYPE_CHECK_INSTANCE_CAST (_tmp3_, nm_device_modem_get_type (), NMDeviceModem));
		if (count > 1) {
			gchar* name = NULL;
			NMDevice* _tmp4_;
			NMDevice* _tmp5_;
			const gchar* _tmp6_;
			gchar* _tmp7_;
			_tmp4_ = network_widgets_page_get_device ((NetworkWidgetsPage*) self);
			_tmp5_ = _tmp4_;
			_tmp6_ = nm_device_get_description (_tmp5_);
			_tmp7_ = g_strdup (_tmp6_);
			name = _tmp7_;
			if ((capabilities & NM_DEVICE_MODEM_CAPABILITY_POTS) == NM_DEVICE_MODEM_CAPABILITY_POTS) {
				const gchar* _tmp8_;
				gchar* _tmp9_;
				gchar* _tmp10_;
				_tmp8_ = name;
				_tmp9_ = g_strdup_printf (_ ("Modem: %s"), _tmp8_);
				_tmp10_ = _tmp9_;
				switchboard_settings_page_set_title ((SwitchboardSettingsPage*) self, _tmp10_);
				_g_free0 (_tmp10_);
			} else {
				const gchar* _tmp11_;
				gchar* _tmp12_;
				gchar* _tmp13_;
				_tmp11_ = name;
				_tmp12_ = g_strdup_printf (_ ("Mobile Broadband: %s"), _tmp11_);
				_tmp13_ = _tmp12_;
				switchboard_settings_page_set_title ((SwitchboardSettingsPage*) self, _tmp13_);
				_g_free0 (_tmp13_);
			}
			_g_free0 (name);
		} else {
			if ((capabilities & NM_DEVICE_MODEM_CAPABILITY_POTS) == NM_DEVICE_MODEM_CAPABILITY_POTS) {
				switchboard_settings_page_set_title ((SwitchboardSettingsPage*) self, _ ("Modem"));
			} else {
				switchboard_settings_page_set_title ((SwitchboardSettingsPage*) self, _ ("Mobile Broadband"));
			}
		}
	} else {
		NETWORK_WIDGETS_PAGE_CLASS (network_widgets_modem_interface_parent_class)->update_name (G_TYPE_CHECK_INSTANCE_CAST (self, NETWORK_WIDGETS_TYPE_PAGE, NetworkWidgetsPage), count);
	}
}

static void
network_widgets_modem_interface_real_update (NetworkWidgetsPage* base)
{
	NetworkWidgetsModemInterface * self;
	GtkRevealer* _tmp0_;
	GtkSwitch* _tmp1_;
	GtkSwitch* _tmp2_;
	gboolean _tmp3_;
	gboolean _tmp4_;
	NMDevice* _tmp5_;
	NMDevice* _tmp6_;
	NMDeviceState _tmp7_;
	NMDeviceState _tmp8_;
	NMDevice* _tmp9_;
	NMDevice* _tmp10_;
	NMDeviceState _tmp11_;
	NMDeviceState _tmp12_;
	self = (NetworkWidgetsModemInterface*) base;
	_tmp0_ = self->priv->top_revealer;
	_tmp1_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
	_tmp2_ = _tmp1_;
	_tmp3_ = gtk_switch_get_active (_tmp2_);
	_tmp4_ = _tmp3_;
	gtk_revealer_set_reveal_child (_tmp0_, _tmp4_);
	NETWORK_WIDGETS_PAGE_CLASS (network_widgets_modem_interface_parent_class)->update (G_TYPE_CHECK_INSTANCE_CAST (self, NETWORK_WIDGETS_TYPE_PAGE, NetworkWidgetsPage));
	_tmp5_ = network_widgets_page_get_device ((NetworkWidgetsPage*) self);
	_tmp6_ = _tmp5_;
	_tmp7_ = nm_device_get_state (_tmp6_);
	_tmp8_ = _tmp7_;
	network_widgets_page_set_state ((NetworkWidgetsPage*) self, _tmp8_);
	_tmp9_ = network_widgets_page_get_device ((NetworkWidgetsPage*) self);
	_tmp10_ = _tmp9_;
	_tmp11_ = nm_device_get_state (_tmp10_);
	_tmp12_ = _tmp11_;
	switch (_tmp12_) {
		case NM_DEVICE_STATE_UNKNOWN:
		case NM_DEVICE_STATE_UNMANAGED:
		case NM_DEVICE_STATE_UNAVAILABLE:
		case NM_DEVICE_STATE_FAILED:
		{
			GtkSwitch* _tmp13_;
			GtkSwitch* _tmp14_;
			GtkSwitch* _tmp15_;
			GtkSwitch* _tmp16_;
			_tmp13_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
			_tmp14_ = _tmp13_;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp14_, FALSE);
			_tmp15_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
			_tmp16_ = _tmp15_;
			gtk_switch_set_active (_tmp16_, FALSE);
			break;
		}
		case NM_DEVICE_STATE_DISCONNECTED:
		case NM_DEVICE_STATE_DEACTIVATING:
		{
			GtkSwitch* _tmp17_;
			GtkSwitch* _tmp18_;
			GtkSwitch* _tmp19_;
			GtkSwitch* _tmp20_;
			_tmp17_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
			_tmp18_ = _tmp17_;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp18_, TRUE);
			_tmp19_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
			_tmp20_ = _tmp19_;
			gtk_switch_set_active (_tmp20_, FALSE);
			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:
		{
			GtkSwitch* _tmp21_;
			GtkSwitch* _tmp22_;
			GtkSwitch* _tmp23_;
			GtkSwitch* _tmp24_;
			_tmp21_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
			_tmp22_ = _tmp21_;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp22_, TRUE);
			_tmp23_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
			_tmp24_ = _tmp23_;
			gtk_switch_set_active (_tmp24_, TRUE);
			break;
		}
		case NM_DEVICE_STATE_ACTIVATED:
		{
			GtkSwitch* _tmp25_;
			GtkSwitch* _tmp26_;
			GtkSwitch* _tmp27_;
			GtkSwitch* _tmp28_;
			_tmp25_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
			_tmp26_ = _tmp25_;
			gtk_widget_set_sensitive ((GtkWidget*) _tmp26_, TRUE);
			_tmp27_ = switchboard_settings_page_get_status_switch ((SwitchboardSettingsPage*) self);
			_tmp28_ = _tmp27_;
			gtk_switch_set_active (_tmp28_, TRUE);
			break;
		}
		default:
		break;
	}
}

static void
network_widgets_modem_interface_class_init (NetworkWidgetsModemInterfaceClass * klass,
                                            gpointer klass_data)
{
	network_widgets_modem_interface_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &NetworkWidgetsModemInterface_private_offset);
	((NetworkWidgetsPageClass *) klass)->update_name = (void (*) (NetworkWidgetsPage*, gint)) network_widgets_modem_interface_real_update_name;
	((NetworkWidgetsPageClass *) klass)->update = (void (*) (NetworkWidgetsPage*)) network_widgets_modem_interface_real_update;
	G_OBJECT_CLASS (klass)->finalize = network_widgets_modem_interface_finalize;
}

static void
network_widgets_modem_interface_instance_init (NetworkWidgetsModemInterface * self,
                                               gpointer klass)
{
	self->priv = network_widgets_modem_interface_get_instance_private (self);
}

static void
network_widgets_modem_interface_finalize (GObject * obj)
{
	NetworkWidgetsModemInterface * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, NETWORK_WIDGETS_TYPE_MODEM_INTERFACE, NetworkWidgetsModemInterface);
	_g_object_unref0 (self->priv->top_revealer);
	G_OBJECT_CLASS (network_widgets_modem_interface_parent_class)->finalize (obj);
}

static GType
network_widgets_modem_interface_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (NetworkWidgetsModemInterfaceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) network_widgets_modem_interface_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (NetworkWidgetsModemInterface), 0, (GInstanceInitFunc) network_widgets_modem_interface_instance_init, NULL };
	GType network_widgets_modem_interface_type_id;
	network_widgets_modem_interface_type_id = g_type_register_static (NETWORK_WIDGETS_TYPE_PAGE, "NetworkWidgetsModemInterface", &g_define_type_info, 0);
	NetworkWidgetsModemInterface_private_offset = g_type_add_instance_private (network_widgets_modem_interface_type_id, sizeof (NetworkWidgetsModemInterfacePrivate));
	return network_widgets_modem_interface_type_id;
}

GType
network_widgets_modem_interface_get_type (void)
{
	static volatile gsize network_widgets_modem_interface_type_id__once = 0;
	if (g_once_init_enter (&network_widgets_modem_interface_type_id__once)) {
		GType network_widgets_modem_interface_type_id;
		network_widgets_modem_interface_type_id = network_widgets_modem_interface_get_type_once ();
		g_once_init_leave (&network_widgets_modem_interface_type_id__once, network_widgets_modem_interface_type_id);
	}
	return network_widgets_modem_interface_type_id__once;
}

