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

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
/*
    This file is part of Déjà Dup.
    For copyright information, see AUTHORS.

    Déjà Dup 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 3 of the License, or
    (at your option) any later version.

    Déjà Dup 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 Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>


#define DEJA_DUP_TYPE_NETWORK (deja_dup_network_get_type ())
#define DEJA_DUP_NETWORK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_NETWORK, DejaDupNetwork))
#define DEJA_DUP_NETWORK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_NETWORK, DejaDupNetworkClass))
#define DEJA_DUP_IS_NETWORK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_NETWORK))
#define DEJA_DUP_IS_NETWORK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_NETWORK))
#define DEJA_DUP_NETWORK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_NETWORK, DejaDupNetworkClass))

typedef struct _DejaDupNetwork DejaDupNetwork;
typedef struct _DejaDupNetworkClass DejaDupNetworkClass;
typedef struct _DejaDupNetworkPrivate DejaDupNetworkPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _DejaDupNetworkEnsureStatusData DejaDupNetworkEnsureStatusData;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _DejaDupNetworkCanReachData DejaDupNetworkCanReachData;

struct _DejaDupNetwork {
	GObject parent_instance;
	DejaDupNetworkPrivate * priv;
};

struct _DejaDupNetworkClass {
	GObjectClass parent_class;
};

struct _DejaDupNetworkPrivate {
	gboolean _connected;
};

struct _DejaDupNetworkEnsureStatusData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	DejaDupNetwork* network;
	DejaDupNetwork* _tmp0_;
};

struct _DejaDupNetworkCanReachData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	DejaDupNetwork* self;
	gchar* url;
	gboolean result;
	GNetworkMonitor* mon;
	GNetworkMonitor* _tmp0_;
	GNetworkMonitor* _tmp1_;
	GNetworkAddress* socket;
	const gchar* _tmp2_;
	GNetworkAddress* _tmp3_;
	gboolean _tmp4_;
	GNetworkMonitor* _tmp5_;
	GNetworkAddress* _tmp6_;
	gboolean _tmp7_;
	GError* e;
	GError* _tmp8_;
	const gchar* _tmp9_;
	GError * _inner_error_;
};


static gpointer deja_dup_network_parent_class = NULL;
static DejaDupNetwork* deja_dup_network_singleton;
static DejaDupNetwork* deja_dup_network_singleton = NULL;

GType deja_dup_network_get_type (void) G_GNUC_CONST;
#define DEJA_DUP_NETWORK_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DEJA_DUP_TYPE_NETWORK, DejaDupNetworkPrivate))
enum  {
	DEJA_DUP_NETWORK_DUMMY_PROPERTY,
	DEJA_DUP_NETWORK_CONNECTED
};
DejaDupNetwork* deja_dup_network_get (void);
DejaDupNetwork* deja_dup_network_new (void);
DejaDupNetwork* deja_dup_network_construct (GType object_type);
static void deja_dup_network_ensure_status_data_free (gpointer _data);
void deja_dup_network_ensure_status (GAsyncReadyCallback _callback_, gpointer _user_data_);
void deja_dup_network_ensure_status_finish (GAsyncResult* _res_);
static gboolean deja_dup_network_ensure_status_co (DejaDupNetworkEnsureStatusData* _data_);
static void deja_dup_network_update_status (DejaDupNetwork* self);
static void deja_dup_network_can_reach_data_free (gpointer _data);
void deja_dup_network_can_reach (DejaDupNetwork* self, const gchar* url, GAsyncReadyCallback _callback_, gpointer _user_data_);
gboolean deja_dup_network_can_reach_finish (DejaDupNetwork* self, GAsyncResult* _res_);
static gboolean deja_dup_network_can_reach_co (DejaDupNetworkCanReachData* _data_);
static void deja_dup_network_can_reach_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
static void deja_dup_network_handle_changed (DejaDupNetwork* self, gboolean available);
gboolean deja_dup_network_get_connected (DejaDupNetwork* self);
void deja_dup_network_set_connected (DejaDupNetwork* self, gboolean value);
static GObject * deja_dup_network_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void _deja_dup_network_handle_changed_g_network_monitor_network_changed (GNetworkMonitor* _sender, gboolean available, gpointer self);
static void deja_dup_network_finalize (GObject* obj);
static void _vala_deja_dup_network_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_deja_dup_network_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


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


DejaDupNetwork* deja_dup_network_get (void) {
	DejaDupNetwork* result = NULL;
	DejaDupNetwork* _tmp0_ = NULL;
	DejaDupNetwork* _tmp2_ = NULL;
	DejaDupNetwork* _tmp3_ = NULL;
	_tmp0_ = deja_dup_network_singleton;
	if (_tmp0_ == NULL) {
		DejaDupNetwork* _tmp1_ = NULL;
		_tmp1_ = deja_dup_network_new ();
		_g_object_unref0 (deja_dup_network_singleton);
		deja_dup_network_singleton = _tmp1_;
	}
	_tmp2_ = deja_dup_network_singleton;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	result = _tmp3_;
	return result;
}


static void deja_dup_network_ensure_status_data_free (gpointer _data) {
	DejaDupNetworkEnsureStatusData* _data_;
	_data_ = _data;
	g_slice_free (DejaDupNetworkEnsureStatusData, _data_);
}


void deja_dup_network_ensure_status (GAsyncReadyCallback _callback_, gpointer _user_data_) {
	DejaDupNetworkEnsureStatusData* _data_;
	_data_ = g_slice_new0 (DejaDupNetworkEnsureStatusData);
	_data_->_async_result = g_simple_async_result_new (NULL, _callback_, _user_data_, deja_dup_network_ensure_status);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, deja_dup_network_ensure_status_data_free);
	deja_dup_network_ensure_status_co (_data_);
}


void deja_dup_network_ensure_status_finish (GAsyncResult* _res_) {
	DejaDupNetworkEnsureStatusData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static gboolean deja_dup_network_ensure_status_co (DejaDupNetworkEnsureStatusData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = NULL;
	_data_->_tmp0_ = deja_dup_network_get ();
	_data_->network = _data_->_tmp0_;
	deja_dup_network_update_status (_data_->network);
	_g_object_unref0 (_data_->network);
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static void deja_dup_network_can_reach_data_free (gpointer _data) {
	DejaDupNetworkCanReachData* _data_;
	_data_ = _data;
	_g_free0 (_data_->url);
	_g_object_unref0 (_data_->self);
	g_slice_free (DejaDupNetworkCanReachData, _data_);
}


void deja_dup_network_can_reach (DejaDupNetwork* self, const gchar* url, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	DejaDupNetworkCanReachData* _data_;
	DejaDupNetwork* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	_data_ = g_slice_new0 (DejaDupNetworkCanReachData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, deja_dup_network_can_reach);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, deja_dup_network_can_reach_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = url;
	_tmp2_ = g_strdup (_tmp1_);
	_g_free0 (_data_->url);
	_data_->url = _tmp2_;
	deja_dup_network_can_reach_co (_data_);
}


gboolean deja_dup_network_can_reach_finish (DejaDupNetwork* self, GAsyncResult* _res_) {
	gboolean result;
	DejaDupNetworkCanReachData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
	result = _data_->result;
	return result;
}


static void deja_dup_network_can_reach_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	DejaDupNetworkCanReachData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	deja_dup_network_can_reach_co (_data_);
}


static gboolean deja_dup_network_can_reach_co (DejaDupNetworkCanReachData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = NULL;
	_data_->_tmp0_ = g_network_monitor_get_default ();
	_data_->_tmp1_ = NULL;
	_data_->_tmp1_ = _g_object_ref0 (_data_->_tmp0_);
	_data_->mon = _data_->_tmp1_;
	{
		_data_->_tmp2_ = NULL;
		_data_->_tmp2_ = _data_->url;
		_data_->_tmp3_ = NULL;
		_data_->_tmp3_ = g_network_address_parse_uri (_data_->_tmp2_, (guint16) 0, &_data_->_inner_error_);
		_data_->socket = _data_->_tmp3_;
		if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
			goto __catch34_g_error;
		}
		_data_->_tmp5_ = NULL;
		_data_->_tmp5_ = _data_->mon;
		_data_->_tmp6_ = NULL;
		_data_->_tmp6_ = _data_->socket;
		_data_->_state_ = 1;
		g_network_monitor_can_reach_async (_data_->_tmp5_, (GSocketConnectable*) _data_->_tmp6_, NULL, deja_dup_network_can_reach_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp7_ = FALSE;
		_data_->_tmp7_ = g_network_monitor_can_reach_finish (_data_->_tmp5_, _data_->_res_, &_data_->_inner_error_);
		_data_->_tmp4_ = _data_->_tmp7_;
		if (G_UNLIKELY (_data_->_inner_error_ != NULL)) {
			_g_object_unref0 (_data_->socket);
			goto __catch34_g_error;
		}
		_data_->result = _data_->_tmp4_;
		_g_object_unref0 (_data_->socket);
		_g_object_unref0 (_data_->mon);
		if (_data_->_state_ == 0) {
			g_simple_async_result_complete_in_idle (_data_->_async_result);
		} else {
			g_simple_async_result_complete (_data_->_async_result);
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	goto __finally34;
	__catch34_g_error:
	{
		_data_->e = _data_->_inner_error_;
		_data_->_inner_error_ = NULL;
		_data_->_tmp8_ = NULL;
		_data_->_tmp8_ = _data_->e;
		_data_->_tmp9_ = NULL;
		_data_->_tmp9_ = _data_->_tmp8_->message;
		g_warning ("Network.vala:48: %s", _data_->_tmp9_);
		_data_->result = FALSE;
		_g_error_free0 (_data_->e);
		_g_object_unref0 (_data_->mon);
		if (_data_->_state_ == 0) {
			g_simple_async_result_complete_in_idle (_data_->_async_result);
		} else {
			g_simple_async_result_complete (_data_->_async_result);
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	__finally34:
	_g_object_unref0 (_data_->mon);
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
	g_clear_error (&_data_->_inner_error_);
	return FALSE;
	_g_object_unref0 (_data_->mon);
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static void deja_dup_network_handle_changed (DejaDupNetwork* self, gboolean available) {
	g_return_if_fail (self != NULL);
	deja_dup_network_update_status (self);
}


static void deja_dup_network_update_status (DejaDupNetwork* self) {
	GNetworkMonitor* mon = NULL;
	GNetworkMonitor* _tmp0_ = NULL;
	GNetworkMonitor* _tmp1_ = NULL;
	GNetworkMonitor* _tmp2_ = NULL;
	gboolean _tmp3_ = FALSE;
	gboolean _tmp4_ = FALSE;
	gboolean _tmp5_ = FALSE;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_network_monitor_get_default ();
	_tmp1_ = _g_object_ref0 (_tmp0_);
	mon = _tmp1_;
	_tmp2_ = mon;
	_tmp3_ = g_network_monitor_get_network_available (_tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = self->priv->_connected;
	if (_tmp4_ != _tmp5_) {
		GNetworkMonitor* _tmp6_ = NULL;
		gboolean _tmp7_ = FALSE;
		gboolean _tmp8_ = FALSE;
		_tmp6_ = mon;
		_tmp7_ = g_network_monitor_get_network_available (_tmp6_);
		_tmp8_ = _tmp7_;
		deja_dup_network_set_connected (self, _tmp8_);
	}
	_g_object_unref0 (mon);
}


DejaDupNetwork* deja_dup_network_construct (GType object_type) {
	DejaDupNetwork * self = NULL;
	self = (DejaDupNetwork*) g_object_new (object_type, NULL);
	return self;
}


DejaDupNetwork* deja_dup_network_new (void) {
	return deja_dup_network_construct (DEJA_DUP_TYPE_NETWORK);
}


gboolean deja_dup_network_get_connected (DejaDupNetwork* self) {
	gboolean result;
	gboolean _tmp0_ = FALSE;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = self->priv->_connected;
	result = _tmp0_;
	return result;
}


void deja_dup_network_set_connected (DejaDupNetwork* self, gboolean value) {
	gboolean _tmp0_ = FALSE;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	self->priv->_connected = _tmp0_;
	g_object_notify ((GObject *) self, "connected");
}


static void _deja_dup_network_handle_changed_g_network_monitor_network_changed (GNetworkMonitor* _sender, gboolean available, gpointer self) {
	deja_dup_network_handle_changed ((DejaDupNetwork*) self, available);
}


static GObject * deja_dup_network_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	DejaDupNetwork * self;
	GNetworkMonitor* mon = NULL;
	GNetworkMonitor* _tmp0_ = NULL;
	GNetworkMonitor* _tmp1_ = NULL;
	GNetworkMonitor* _tmp2_ = NULL;
	parent_class = G_OBJECT_CLASS (deja_dup_network_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DEJA_DUP_TYPE_NETWORK, DejaDupNetwork);
	_tmp0_ = g_network_monitor_get_default ();
	_tmp1_ = _g_object_ref0 (_tmp0_);
	mon = _tmp1_;
	_tmp2_ = mon;
	g_signal_connect_object (_tmp2_, "network-changed", (GCallback) _deja_dup_network_handle_changed_g_network_monitor_network_changed, self, 0);
	_g_object_unref0 (mon);
	return obj;
}


static void deja_dup_network_class_init (DejaDupNetworkClass * klass) {
	deja_dup_network_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (DejaDupNetworkPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_deja_dup_network_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_deja_dup_network_set_property;
	G_OBJECT_CLASS (klass)->constructor = deja_dup_network_constructor;
	G_OBJECT_CLASS (klass)->finalize = deja_dup_network_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DEJA_DUP_NETWORK_CONNECTED, g_param_spec_boolean ("connected", "connected", "connected", TRUE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void deja_dup_network_instance_init (DejaDupNetwork * self) {
	self->priv = DEJA_DUP_NETWORK_GET_PRIVATE (self);
	self->priv->_connected = TRUE;
}


static void deja_dup_network_finalize (GObject* obj) {
	DejaDupNetwork * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DEJA_DUP_TYPE_NETWORK, DejaDupNetwork);
	G_OBJECT_CLASS (deja_dup_network_parent_class)->finalize (obj);
}


GType deja_dup_network_get_type (void) {
	static volatile gsize deja_dup_network_type_id__volatile = 0;
	if (g_once_init_enter (&deja_dup_network_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DejaDupNetworkClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_network_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupNetwork), 0, (GInstanceInitFunc) deja_dup_network_instance_init, NULL };
		GType deja_dup_network_type_id;
		deja_dup_network_type_id = g_type_register_static (G_TYPE_OBJECT, "DejaDupNetwork", &g_define_type_info, 0);
		g_once_init_leave (&deja_dup_network_type_id__volatile, deja_dup_network_type_id);
	}
	return deja_dup_network_type_id__volatile;
}


static void _vala_deja_dup_network_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	DejaDupNetwork * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, DEJA_DUP_TYPE_NETWORK, DejaDupNetwork);
	switch (property_id) {
		case DEJA_DUP_NETWORK_CONNECTED:
		g_value_set_boolean (value, deja_dup_network_get_connected (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_deja_dup_network_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	DejaDupNetwork * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, DEJA_DUP_TYPE_NETWORK, DejaDupNetwork);
	switch (property_id) {
		case DEJA_DUP_NETWORK_CONNECTED:
		deja_dup_network_set_connected (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



