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

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
/*
    This file is part of Déjà Dup.
    © 2009 Michael Terry <mike@mterry.name>,
    © 2009 Andrew Fister <temposs@gmail.com>

    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 <dbus/dbus-glib-lowlevel.h>
#include <dbus/dbus-glib.h>


#define DEJA_DUP_TYPE_NETWORK_MANAGER (deja_dup_network_manager_get_type ())
#define DEJA_DUP_NETWORK_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_NETWORK_MANAGER, DejaDupNetworkManager))
#define DEJA_DUP_NETWORK_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_NETWORK_MANAGER, DejaDupNetworkManagerClass))
#define DEJA_DUP_IS_NETWORK_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_NETWORK_MANAGER))
#define DEJA_DUP_IS_NETWORK_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_NETWORK_MANAGER))
#define DEJA_DUP_NETWORK_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_NETWORK_MANAGER, DejaDupNetworkManagerClass))

typedef struct _DejaDupNetworkManager DejaDupNetworkManager;
typedef struct _DejaDupNetworkManagerClass DejaDupNetworkManagerClass;
typedef struct _DejaDupNetworkManagerPrivate DejaDupNetworkManagerPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _dbus_g_connection_unref0(var) ((var == NULL) ? NULL : (var = (dbus_g_connection_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _DejaDupNetworkManager {
	GObject parent_instance;
	DejaDupNetworkManagerPrivate * priv;
};

struct _DejaDupNetworkManagerClass {
	GObjectClass parent_class;
};

struct _DejaDupNetworkManagerPrivate {
	gboolean _connected;
	DBusGProxy* nm;
};


static DejaDupNetworkManager* deja_dup_network_manager_singleton;
static DejaDupNetworkManager* deja_dup_network_manager_singleton = NULL;
static gpointer deja_dup_network_manager_parent_class = NULL;

GType deja_dup_network_manager_get_type (void);
#define DEJA_DUP_NETWORK_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DEJA_DUP_TYPE_NETWORK_MANAGER, DejaDupNetworkManagerPrivate))
enum  {
	DEJA_DUP_NETWORK_MANAGER_DUMMY_PROPERTY,
	DEJA_DUP_NETWORK_MANAGER_CONNECTED
};
#define DEJA_DUP_NETWORK_MANAGER_NM_STATE_CONNECTED ((guint32) 3)
DejaDupNetworkManager* deja_dup_network_manager_new (void);
DejaDupNetworkManager* deja_dup_network_manager_construct (GType object_type);
DejaDupNetworkManager* deja_dup_network_manager_get (void);
static inline guint32 _dynamic_get_State0 (DBusGProxy* obj);
void deja_dup_network_manager_set_connected (DejaDupNetworkManager* self, gboolean value);
void deja_dup_network_manager_nm_state_changed (DejaDupNetworkManager* self, DBusGProxy* obj, guint32 new_state);
static void _deja_dup_network_manager_nm_state_changed_dynamic_StateChanged0_ (DBusGProxy* _sender, guint32 new_state, gpointer self);
void _dynamic_StateChanged1_connect (gpointer obj, const char * signal_name, GCallback handler, gpointer data);
static void deja_dup_network_manager_init_dbus_to_network_manager (DejaDupNetworkManager* self, GError** error);
gboolean deja_dup_network_manager_get_connected (DejaDupNetworkManager* self);
static GObject * deja_dup_network_manager_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void deja_dup_network_manager_finalize (GObject* obj);
static void deja_dup_network_manager_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void deja_dup_network_manager_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;
}


DejaDupNetworkManager* deja_dup_network_manager_get (void) {
	DejaDupNetworkManager* result = NULL;
	if (deja_dup_network_manager_singleton == NULL) {
		DejaDupNetworkManager* _tmp0_;
		deja_dup_network_manager_singleton = (_tmp0_ = deja_dup_network_manager_new (), _g_object_unref0 (deja_dup_network_manager_singleton), _tmp0_);
	}
	result = _g_object_ref0 (deja_dup_network_manager_singleton);
	return result;
}


DejaDupNetworkManager* deja_dup_network_manager_construct (GType object_type) {
	DejaDupNetworkManager * self;
	self = g_object_newv (object_type, 0, NULL);
	return self;
}


DejaDupNetworkManager* deja_dup_network_manager_new (void) {
	return deja_dup_network_manager_construct (DEJA_DUP_TYPE_NETWORK_MANAGER);
}


static inline guint32 _dynamic_get_State0 (DBusGProxy* obj) {
	DBusGProxy* property_proxy;
	GValue gvalue = {0};
	guint32 result;
	property_proxy = dbus_g_proxy_new_from_proxy (obj, DBUS_INTERFACE_PROPERTIES, NULL);
	dbus_g_proxy_call (property_proxy, "Get", NULL, G_TYPE_STRING, dbus_g_proxy_get_interface (obj), G_TYPE_STRING, "State", G_TYPE_INVALID, G_TYPE_VALUE, &gvalue, G_TYPE_INVALID);
	g_object_unref (property_proxy);
	result = g_value_get_uint (&gvalue);
	return result;
}


static void _deja_dup_network_manager_nm_state_changed_dynamic_StateChanged0_ (DBusGProxy* _sender, guint32 new_state, gpointer self) {
	deja_dup_network_manager_nm_state_changed (self, _sender, new_state);
}


void _dynamic_StateChanged1_connect (gpointer obj, const char * signal_name, GCallback handler, gpointer data) {
	dbus_g_object_register_marshaller (g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, G_TYPE_UINT, G_TYPE_INVALID);
	dbus_g_proxy_add_signal (obj, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (obj, signal_name, handler, data, NULL);
}


static void deja_dup_network_manager_init_dbus_to_network_manager (DejaDupNetworkManager* self, GError** error) {
	GError * _inner_error_;
	DBusGConnection* conn;
	DBusGProxy* _tmp0_;
	guint32 network_manager_state;
	g_return_if_fail (self != NULL);
	_inner_error_ = NULL;
	conn = dbus_g_bus_get (DBUS_BUS_SYSTEM, &_inner_error_);
	if (_inner_error_ != NULL) {
		g_propagate_error (error, _inner_error_);
		return;
	}
	self->priv->nm = (_tmp0_ = dbus_g_proxy_new_for_name (conn, "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager"), _g_object_unref0 (self->priv->nm), _tmp0_);
	network_manager_state = _dynamic_get_State0 (self->priv->nm);
	deja_dup_network_manager_set_connected (self, network_manager_state == DEJA_DUP_NETWORK_MANAGER_NM_STATE_CONNECTED);
	_dynamic_StateChanged1_connect (self->priv->nm, "StateChanged", (GCallback) _deja_dup_network_manager_nm_state_changed_dynamic_StateChanged0_, self);
	_dbus_g_connection_unref0 (conn);
}


void deja_dup_network_manager_nm_state_changed (DejaDupNetworkManager* self, DBusGProxy* obj, guint32 new_state) {
	gboolean was_connected;
	g_return_if_fail (self != NULL);
	g_return_if_fail (obj != NULL);
	was_connected = self->priv->_connected;
	deja_dup_network_manager_set_connected (self, new_state == DEJA_DUP_NETWORK_MANAGER_NM_STATE_CONNECTED);
	if (was_connected != self->priv->_connected) {
		g_signal_emit_by_name (self, "changed", self->priv->_connected);
	}
}


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


void deja_dup_network_manager_set_connected (DejaDupNetworkManager* self, gboolean value) {
	g_return_if_fail (self != NULL);
	self->priv->_connected = value;
	g_object_notify ((GObject *) self, "connected");
}


static GObject * deja_dup_network_manager_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	DejaDupNetworkManager * self;
	GError * _inner_error_;
	parent_class = G_OBJECT_CLASS (deja_dup_network_manager_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DEJA_DUP_NETWORK_MANAGER (obj);
	_inner_error_ = NULL;
	{
		{
			deja_dup_network_manager_init_dbus_to_network_manager (self, &_inner_error_);
			if (_inner_error_ != NULL) {
				goto __catch32_g_error;
			}
		}
		goto __finally32;
		__catch32_g_error:
		{
			GError * e;
			e = _inner_error_;
			_inner_error_ = NULL;
			{
				g_warning ("NetworkManager.vala:47: %s\n", e->message);
				_g_error_free0 (e);
			}
		}
		__finally32:
		if (_inner_error_ != NULL) {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
		}
	}
	return obj;
}


static void deja_dup_network_manager_class_init (DejaDupNetworkManagerClass * klass) {
	deja_dup_network_manager_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (DejaDupNetworkManagerPrivate));
	G_OBJECT_CLASS (klass)->get_property = deja_dup_network_manager_get_property;
	G_OBJECT_CLASS (klass)->set_property = deja_dup_network_manager_set_property;
	G_OBJECT_CLASS (klass)->constructor = deja_dup_network_manager_constructor;
	G_OBJECT_CLASS (klass)->finalize = deja_dup_network_manager_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DEJA_DUP_NETWORK_MANAGER_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));
	g_signal_new ("changed", DEJA_DUP_TYPE_NETWORK_MANAGER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
}


static void deja_dup_network_manager_instance_init (DejaDupNetworkManager * self) {
	self->priv = DEJA_DUP_NETWORK_MANAGER_GET_PRIVATE (self);
	self->priv->_connected = TRUE;
}


static void deja_dup_network_manager_finalize (GObject* obj) {
	DejaDupNetworkManager * self;
	self = DEJA_DUP_NETWORK_MANAGER (obj);
	_g_object_unref0 (self->priv->nm);
	G_OBJECT_CLASS (deja_dup_network_manager_parent_class)->finalize (obj);
}


GType deja_dup_network_manager_get_type (void) {
	static volatile gsize deja_dup_network_manager_type_id__volatile = 0;
	if (g_once_init_enter (&deja_dup_network_manager_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DejaDupNetworkManagerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_network_manager_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupNetworkManager), 0, (GInstanceInitFunc) deja_dup_network_manager_instance_init, NULL };
		GType deja_dup_network_manager_type_id;
		deja_dup_network_manager_type_id = g_type_register_static (G_TYPE_OBJECT, "DejaDupNetworkManager", &g_define_type_info, 0);
		g_once_init_leave (&deja_dup_network_manager_type_id__volatile, deja_dup_network_manager_type_id);
	}
	return deja_dup_network_manager_type_id__volatile;
}


static void deja_dup_network_manager_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	DejaDupNetworkManager * self;
	self = DEJA_DUP_NETWORK_MANAGER (object);
	switch (property_id) {
		case DEJA_DUP_NETWORK_MANAGER_CONNECTED:
		g_value_set_boolean (value, deja_dup_network_manager_get_connected (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void deja_dup_network_manager_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	DejaDupNetworkManager * self;
	self = DEJA_DUP_NETWORK_MANAGER (object);
	switch (property_id) {
		case DEJA_DUP_NETWORK_MANAGER_CONNECTED:
		deja_dup_network_manager_set_connected (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}




