nm-vpn-connection.c 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* -*- 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.
 */

22
#include "nm-default.h"
23

24
#include "nm-vpn-connection.h"
25

26
#include "nm-dbus-interface.h"
27 28 29
#include "nm-utils.h"
#include "nm-object-private.h"
#include "nm-active-connection.h"
Dan Winship's avatar
Dan Winship committed
30 31
#include "nm-dbus-helpers.h"

32
#include "introspection/org.freedesktop.NetworkManager.VPN.Connection.h"
33

34
G_DEFINE_TYPE (NMVpnConnection, nm_vpn_connection, NM_TYPE_ACTIVE_CONNECTION)
35

36
#define NM_VPN_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_VPN_CONNECTION, NMVpnConnectionPrivate))
37

38 39
G_STATIC_ASSERT (sizeof (NMVpnConnectionStateReason) == sizeof (NMActiveConnectionStateReason));

40 41
typedef struct {
	char *banner;
42 43
	NMVpnConnectionState vpn_state;
} NMVpnConnectionPrivate;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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:
63
 * @vpn: a #NMVpnConnection
64
 *
65
 * Gets the VPN login banner of the active #NMVpnConnection.
66 67 68 69 70
 *
 * 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 *
71
nm_vpn_connection_get_banner (NMVpnConnection *vpn)
72 73 74
{
	g_return_val_if_fail (NM_IS_VPN_CONNECTION (vpn), NULL);

75
	return nm_str_not_empty (NM_VPN_CONNECTION_GET_PRIVATE (vpn)->banner);
76 77 78 79
}

/**
 * nm_vpn_connection_get_vpn_state:
80
 * @vpn: a #NMVpnConnection
81
 *
82
 * Gets the current #NMVpnConnection state.
83 84 85
 *
 * Returns: the VPN state of the active VPN connection.
 **/
86 87
NMVpnConnectionState
nm_vpn_connection_get_vpn_state (NMVpnConnection *vpn)
88 89 90 91 92 93 94
{
	g_return_val_if_fail (NM_IS_VPN_CONNECTION (vpn), NM_VPN_CONNECTION_STATE_UNKNOWN);

	return NM_VPN_CONNECTION_GET_PRIVATE (vpn)->vpn_state;
}

static void
Dan Winship's avatar
Dan Winship committed
95 96 97
vpn_state_changed_proxy (NMDBusVpnConnection *proxy,
                         guint vpn_state,
                         guint reason,
98 99
                         gpointer user_data)
{
100 101
	NMVpnConnection *connection = NM_VPN_CONNECTION (user_data);
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection);
102 103 104 105 106 107 108 109 110 111 112

	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
113
nm_vpn_connection_init (NMVpnConnection *connection)
114
{
115
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection);
116 117 118 119 120

	priv->vpn_state = NM_VPN_CONNECTION_STATE_UNKNOWN;
}

static void
121
init_dbus (NMObject *object)
122
{
123
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (object);
124 125 126 127 128
	const NMPropertiesInfo property_info[] = {
		{ NM_VPN_CONNECTION_BANNER,    &priv->banner },
		{ NM_VPN_CONNECTION_VPN_STATE, &priv->vpn_state },
		{ NULL },
	};
Dan Winship's avatar
Dan Winship committed
129
	GDBusProxy *proxy;
130

131 132 133
	NM_OBJECT_CLASS (nm_vpn_connection_parent_class)->init_dbus (object);

	_nm_object_register_properties (object,
134
	                                NM_DBUS_INTERFACE_VPN_CONNECTION,
135 136
	                                property_info);

137
	proxy = _nm_object_get_proxy (object, NM_DBUS_INTERFACE_VPN_CONNECTION);
138 139
	g_signal_connect_object (proxy, "vpn-state-changed",
	                         G_CALLBACK (vpn_state_changed_proxy), object, 0);
140
	g_object_unref (proxy);
141 142 143 144 145
}

static void
finalize (GObject *object)
{
146
	NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (object);
147 148 149 150 151 152 153 154 155 156 157 158

	g_free (priv->banner);

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

static void
get_property (GObject *object,
              guint prop_id,
              GValue *value,
              GParamSpec *pspec)
{
159
	NMVpnConnection *self = NM_VPN_CONNECTION (object);
160 161 162

	switch (prop_id) {
	case PROP_VPN_STATE:
163
		g_value_set_enum (value, nm_vpn_connection_get_vpn_state (self));
164 165 166 167 168 169 170 171 172 173 174
		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
175
nm_vpn_connection_class_init (NMVpnConnectionClass *connection_class)
176 177
{
	GObjectClass *object_class = G_OBJECT_CLASS (connection_class);
178
	NMObjectClass *nm_object_class = NM_OBJECT_CLASS (connection_class);
179

180
	g_type_class_add_private (connection_class, sizeof (NMVpnConnectionPrivate));
181 182 183 184 185

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

186 187
	nm_object_class->init_dbus = init_dbus;

188 189 190
	/* properties */

	/**
191
	 * NMVpnConnection:vpn-state:
192 193 194 195 196
	 *
	 * The VPN state of the active VPN connection.
	 **/
	g_object_class_install_property
		(object_class, PROP_VPN_STATE,
197 198
		 g_param_spec_enum (NM_VPN_CONNECTION_VPN_STATE, "", "",
		                    NM_TYPE_VPN_CONNECTION_STATE,
199 200 201 202 203
		                    NM_VPN_CONNECTION_STATE_UNKNOWN,
		                    G_PARAM_READABLE |
		                    G_PARAM_STATIC_STRINGS));

	/**
204
	 * NMVpnConnection:banner:
205 206 207 208 209 210 211 212 213 214 215
	 *
	 * 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 */
216
	G_GNUC_BEGIN_IGNORE_DEPRECATIONS
217 218 219 220
	signals[VPN_STATE_CHANGED] =
		g_signal_new ("vpn-state-changed",
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_FIRST,
221
		              G_STRUCT_OFFSET (NMVpnConnectionClass, vpn_state_changed),
222 223 224
		              NULL, NULL, NULL,
		              G_TYPE_NONE, 2,
		              G_TYPE_UINT, G_TYPE_UINT);
225
	G_GNUC_END_IGNORE_DEPRECATIONS
226
}