nm-vpn-connection.c 7.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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.
 *
 * Copyright 2007 - 2008 Novell, Inc.
 * Copyright 2007 - 2012 Red Hat, Inc.
 */

#include <string.h>
#include "nm-vpn-connection.h"
#include "NetworkManager.h"
#include "nm-utils.h"
#include "nm-object-private.h"
#include "nm-active-connection.h"

29
G_DEFINE_TYPE (NMVpnConnection, nm_vpn_connection, NM_TYPE_ACTIVE_CONNECTION)
30

31
#define NM_VPN_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_VPN_CONNECTION, NMVpnConnectionPrivate))
32 33 34 35

typedef struct {
	DBusGProxy *proxy;
	char *banner;
36 37
	NMVpnConnectionState vpn_state;
} NMVpnConnectionPrivate;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

enum {
	PROP_0,
	PROP_VPN_STATE,
	PROP_BANNER,

	LAST_PROP
};

enum {
	VPN_STATE_CHANGED,

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };


/**
 * nm_vpn_connection_get_banner:
58
 * @vpn: a #NMVpnConnection
59
 *
60
 * Gets the VPN login banner of the active #NMVpnConnection.
61 62 63 64 65
 *
 * Returns: the VPN login banner of the VPN connection. This is the internal
 * string used by the connection, and must not be modified.
 **/
const char *
66
nm_vpn_connection_get_banner (NMVpnConnection *vpn)
67
{
68
	NMVpnConnectionPrivate *priv;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

	g_return_val_if_fail (NM_IS_VPN_CONNECTION (vpn), NULL);

	priv = NM_VPN_CONNECTION_GET_PRIVATE (vpn);

	/* We need to update vpn_state first in case it's unknown. */
	_nm_object_ensure_inited (NM_OBJECT (vpn));

	if (priv->vpn_state != NM_VPN_CONNECTION_STATE_ACTIVATED)
		return NULL;

	return priv->banner;
}

/**
 * nm_vpn_connection_get_vpn_state:
85
 * @vpn: a #NMVpnConnection
86
 *
87
 * Gets the current #NMVpnConnection state.
88 89 90
 *
 * Returns: the VPN state of the active VPN connection.
 **/
91 92
NMVpnConnectionState
nm_vpn_connection_get_vpn_state (NMVpnConnection *vpn)
93 94 95 96 97 98 99 100 101
{
	g_return_val_if_fail (NM_IS_VPN_CONNECTION (vpn), NM_VPN_CONNECTION_STATE_UNKNOWN);

	_nm_object_ensure_inited (NM_OBJECT (vpn));
	return NM_VPN_CONNECTION_GET_PRIVATE (vpn)->vpn_state;
}

static void
vpn_state_changed_proxy (DBusGProxy *proxy,
102 103
                         NMVpnConnectionState vpn_state,
                         NMVpnConnectionStateReason reason,
104 105
                         gpointer user_data)
{
106 107
	NMVpnConnection *connection = NM_VPN_CONNECTION (user_data);
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection);
108 109 110 111 112 113 114 115 116 117 118

	if (priv->vpn_state != vpn_state) {
		priv->vpn_state = vpn_state;
		g_signal_emit (connection, signals[VPN_STATE_CHANGED], 0, vpn_state, reason);
		g_object_notify (G_OBJECT (connection), NM_VPN_CONNECTION_VPN_STATE);
	}
}

/*****************************************************************************/

static void
119
nm_vpn_connection_init (NMVpnConnection *connection)
120
{
121
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection);
122 123 124 125 126

	priv->vpn_state = NM_VPN_CONNECTION_STATE_UNKNOWN;
}

static void
127
register_properties (NMVpnConnection *connection)
128
{
129
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection);
130 131 132 133 134 135 136 137 138 139 140 141 142 143
	const NMPropertiesInfo property_info[] = {
		{ NM_VPN_CONNECTION_BANNER,    &priv->banner },
		{ NM_VPN_CONNECTION_VPN_STATE, &priv->vpn_state },
		{ NULL },
	};

	_nm_object_register_properties (NM_OBJECT (connection),
	                                priv->proxy,
	                                property_info);
}

static void
constructed (GObject *object)
{
144
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (object);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

	G_OBJECT_CLASS (nm_vpn_connection_parent_class)->constructed (object);

	priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_VPN_CONNECTION);

	dbus_g_object_register_marshaller (g_cclosure_marshal_generic,
	                                   G_TYPE_NONE,
	                                   G_TYPE_UINT, G_TYPE_UINT,
	                                   G_TYPE_INVALID);
	dbus_g_proxy_add_signal (priv->proxy, "VpnStateChanged", G_TYPE_UINT, G_TYPE_UINT, G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (priv->proxy,
	                             "VpnStateChanged",
	                             G_CALLBACK (vpn_state_changed_proxy),
	                             object,
	                             NULL);

	register_properties (NM_VPN_CONNECTION (object));
}

static void
finalize (GObject *object)
{
167
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (object);
168 169 170 171 172 173 174 175 176 177 178 179 180

	g_free (priv->banner);
	g_object_unref (priv->proxy);

	G_OBJECT_CLASS (nm_vpn_connection_parent_class)->finalize (object);
}

static void
get_property (GObject *object,
              guint prop_id,
              GValue *value,
              GParamSpec *pspec)
{
181
	NMVpnConnection *self = NM_VPN_CONNECTION (object);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

	_nm_object_ensure_inited (NM_OBJECT (object));

	switch (prop_id) {
	case PROP_VPN_STATE:
		g_value_set_uint (value, nm_vpn_connection_get_vpn_state (self));
		break;
	case PROP_BANNER:
		g_value_set_string (value, nm_vpn_connection_get_banner (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
199
nm_vpn_connection_class_init (NMVpnConnectionClass *connection_class)
200 201 202
{
	GObjectClass *object_class = G_OBJECT_CLASS (connection_class);

203
	g_type_class_add_private (connection_class, sizeof (NMVpnConnectionPrivate));
204 205 206 207 208 209 210 211 212

	/* virtual methods */
	object_class->constructed = constructed;
	object_class->get_property = get_property;
	object_class->finalize = finalize;

	/* properties */

	/**
213
	 * NMVpnConnection:vpn-state:
214 215 216 217 218 219 220 221 222 223 224 225 226
	 *
	 * The VPN state of the active VPN connection.
	 **/
	g_object_class_install_property
		(object_class, PROP_VPN_STATE,
		 g_param_spec_uint (NM_VPN_CONNECTION_VPN_STATE, "", "",
		                    NM_VPN_CONNECTION_STATE_UNKNOWN,
		                    NM_VPN_CONNECTION_STATE_DISCONNECTED,
		                    NM_VPN_CONNECTION_STATE_UNKNOWN,
		                    G_PARAM_READABLE |
		                    G_PARAM_STATIC_STRINGS));

	/**
227
	 * NMVpnConnection:banner:
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	 *
	 * The VPN login banner of the active VPN connection.
	 **/
	g_object_class_install_property
		(object_class, PROP_BANNER,
		 g_param_spec_string (NM_VPN_CONNECTION_BANNER, "", "",
		                      NULL,
		                      G_PARAM_READABLE |
		                      G_PARAM_STATIC_STRINGS));

	/* signals */
	signals[VPN_STATE_CHANGED] =
		g_signal_new ("vpn-state-changed",
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_FIRST,
243
		              G_STRUCT_OFFSET (NMVpnConnectionClass, vpn_state_changed),
244 245 246 247
		              NULL, NULL, NULL,
		              G_TYPE_NONE, 2,
		              G_TYPE_UINT, G_TYPE_UINT);
}