示例#1
0
static void
activate_ac_state_changed (GObject    *object,
                           GParamSpec *pspec,
                           gpointer    user_data)
{
	NmtSyncOp *op = user_data;
	NMActiveConnectionState state;
	GError *error = NULL;

	state = nm_active_connection_get_state (NM_ACTIVE_CONNECTION (object));
	if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATING)
		return;

	if (state != NM_ACTIVE_CONNECTION_STATE_ACTIVATED) {
		error = g_error_new_literal (NM_CLIENT_ERROR, NM_CLIENT_ERROR_FAILED,
		                             _("Activation failed"));
	}

	nmt_sync_op_complete_boolean (op, error == NULL, error);
	g_clear_error (&error);
}
static void
activate_connection_cb (gpointer user_data, const char *path, GError *error)
{
	struct start_network_data *data = (struct start_network_data *)user_data;
	NMActiveConnection *active;
	NMActiveConnectionState state;

	if (error) {
		gchar err[512];
		g_snprintf (err, sizeof(err), "Unable to connect: %s", error->message);
		network_status (data->client, err);
		g_main_loop_quit (loop);
		return;
	}

	active = nm_client_get_active_connection_by_path (data->client, path);
	if (!active) {
		gchar err[512];
		g_snprintf (err, sizeof(err), "Unable to obtain active connection state for %s", path);
		network_status (data->client, err);
		g_main_loop_quit (loop);
		return;
	}

	state = nm_active_connection_get_state (active);
	//g_print ("Connection %s state: %s\n", path, active_connection_state_to_string(state));

	/* If we actually connect, then quit */
	if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED)
	{
		g_main_loop_quit (loop);
		return;
	}

	g_signal_connect (active, "notify::state", G_CALLBACK (active_connection_state_cb), user_data);
	return;
}
示例#3
0
static void
activate_connection (NMConnection *connection,
                     NMDevice     *device,
                     NMObject     *specific_object)
{
	NmtNewtForm *form;
	gs_unref_object NMSecretAgentOld *agent = NULL;
	NmtNewtWidget *label;
	NmtSyncOp op;
	const char *specific_object_path;
	NMActiveConnection *ac;
	GError *error = NULL;

	form = g_object_new (NMT_TYPE_NEWT_FORM,
	                     "escape-exits", TRUE,
	                     NULL);
	label = nmt_newt_label_new (_("Connecting..."));
	nmt_newt_form_set_content (form, label);

	agent = nm_secret_agent_simple_new ("nmtui");
	if (agent) {
		if (connection) {
			nm_secret_agent_simple_enable (NM_SECRET_AGENT_SIMPLE (agent),
			                               nm_object_get_path (NM_OBJECT (connection)));
		}
		g_signal_connect (agent, "request-secrets", G_CALLBACK (secrets_requested), NULL);
	}

	specific_object_path = specific_object ? nm_object_get_path (specific_object) : NULL;

	/* There's no way to cancel an nm_client_activate_connection() /
	 * nm_client_add_and_activate_connection() call, so we always let them
	 * complete, even if the user hits Esc; they shouldn't normally take long
	 * to complete anyway.
	 */

	nmt_sync_op_init (&op);
	if (connection) {
		nm_client_activate_connection_async (nm_client,
		                                     connection, device, specific_object_path,
		                                     NULL, activate_callback, &op);
	} else {
		nm_client_add_and_activate_connection_async (nm_client,
		                                             NULL, device, specific_object_path,
		                                             NULL, add_and_activate_callback, &op);
	}

	nmt_newt_form_show (form);

	ac = nmt_sync_op_wait_pointer (&op, &error);
	if (!ac) {
		nmt_newt_message_dialog (_("Could not activate connection: %s"), error->message);
		g_clear_error (&error);
		goto done;
	} else if (nm_active_connection_get_state (ac) == NM_ACTIVE_CONNECTION_STATE_ACTIVATED) {
		/* Already active */
		goto done;
	} else if (!nmt_newt_widget_get_realized (NMT_NEWT_WIDGET (form))) {
		/* User already hit Esc */
		goto done;
	}

	if (agent && !connection) {
		connection = NM_CONNECTION (nm_active_connection_get_connection (ac));
		if (connection) {
			nm_secret_agent_simple_enable (NM_SECRET_AGENT_SIMPLE (agent),
			                               nm_object_get_path (NM_OBJECT (connection)));
		}
	}

	/* Now wait for the connection to actually reach the ACTIVATED state,
	 * allowing the user to cancel if it takes too long.
	 */

	nmt_sync_op_init (&op);

	g_signal_connect (form, "quit", G_CALLBACK (connect_cancelled), &op);
	g_signal_connect (ac, "notify::" NM_ACTIVE_CONNECTION_STATE,
	                  G_CALLBACK (activate_ac_state_changed), &op);

	if (!nmt_sync_op_wait_boolean (&op, &error)) {
		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
			nmt_newt_message_dialog (_("Could not activate connection: %s"), error->message);
		g_clear_error (&error);
	}

	g_signal_handlers_disconnect_by_func (form, G_CALLBACK (connect_cancelled), &op);
	g_signal_handlers_disconnect_by_func (ac, G_CALLBACK (activate_ac_state_changed), &op);

 done:
	if (nmt_newt_widget_get_realized (NMT_NEWT_WIDGET (form)))
		nmt_newt_form_quit (form);
	g_object_unref (form);

	if (agent)
		nm_secret_agent_old_unregister (agent, NULL, NULL);
}
示例#4
0
static void
device_state_changed (NMActiveConnection *active,
                      NMDevice *device,
                      NMDeviceState new_state,
                      NMDeviceState old_state)
{
	NMActiveConnectionState cur_ac_state = nm_active_connection_get_state (active);
	NMActiveConnectionState ac_state = NM_ACTIVE_CONNECTION_STATE_UNKNOWN;

	/* Decide which device state changes to handle when this active connection
	 * is not the device's current request.  Two cases here: (a) the AC is
	 * pending and not yet active, and (b) the AC was active but the device is
	 * entering DISCONNECTED state (which clears the device's current AC before
	 * emitting the state change signal).
	 */
	if (NM_ACTIVE_CONNECTION (nm_device_get_act_request (device)) != active) {
		/* Some other request is activating; this one must be pending */
		if (new_state >= NM_DEVICE_STATE_PREPARE)
			return;
		else if (new_state == NM_DEVICE_STATE_DISCONNECTED) {
			/* This request hasn't started activating yet; the device is
			 * disconnecting and cleaning up a previous activation request.
			 */
			if (cur_ac_state < NM_ACTIVE_CONNECTION_STATE_ACTIVATING)
				return;

			/* Catch device disconnections after this request has been active */
		}

		/* All states < DISCONNECTED are fatal and handled */
	}

	/* Set NMActiveConnection state based on the device's state */
	switch (new_state) {
	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:
		ac_state = NM_ACTIVE_CONNECTION_STATE_ACTIVATING;
		break;
	case NM_DEVICE_STATE_ACTIVATED:
		ac_state = NM_ACTIVE_CONNECTION_STATE_ACTIVATED;

		g_signal_connect (device, "notify::" NM_DEVICE_IP4_CONFIG,
		                  G_CALLBACK (device_notify), active);
		g_signal_connect (device, "notify::" NM_DEVICE_DHCP4_CONFIG,
		                  G_CALLBACK (device_notify), active);
		g_signal_connect (device, "notify::" NM_DEVICE_IP6_CONFIG,
		                  G_CALLBACK (device_notify), active);
		g_signal_connect (device, "notify::" NM_DEVICE_DHCP6_CONFIG,
		                  G_CALLBACK (device_notify), active);
		break;
	case NM_DEVICE_STATE_DEACTIVATING:
		ac_state = NM_ACTIVE_CONNECTION_STATE_DEACTIVATING;
		break;
	case NM_DEVICE_STATE_FAILED:
	case NM_DEVICE_STATE_DISCONNECTED:
	case NM_DEVICE_STATE_UNMANAGED:
	case NM_DEVICE_STATE_UNAVAILABLE:
		ac_state = NM_ACTIVE_CONNECTION_STATE_DEACTIVATED;

		g_signal_handlers_disconnect_by_func (device, G_CALLBACK (device_notify), active);
		break;
	default:
		break;
	}

	if (   ac_state == NM_ACTIVE_CONNECTION_STATE_DEACTIVATED
	    || ac_state == NM_ACTIVE_CONNECTION_STATE_UNKNOWN) {
		nm_active_connection_set_default (active, FALSE);
		nm_active_connection_set_default6 (active, FALSE);
	}

	nm_active_connection_set_state (active, ac_state);
}