Beispiel #1
0
/**
 * nmt_editor_new:
 * @connection: the #NMConnection to edit
 *
 * Creates a new #NmtEditor to edit @connection.
 *
 * Returns: a new #NmtEditor
 */
NmtNewtForm *
nmt_editor_new (NMConnection *connection)
{
	NMEditorConnectionTypeData *type_data;

	type_data = nm_editor_utils_get_connection_type_data (connection);
	if (!type_data) {
		NMSettingConnection *s_con;

		s_con = nm_connection_get_setting_connection (connection);
		if (s_con) {
			nmt_newt_message_dialog (_("Could not create editor for connection '%s' of type '%s'."),
			                         nm_connection_get_id (connection),
			                         nm_setting_connection_get_connection_type (s_con));
		} else {
			nmt_newt_message_dialog (_("Could not create editor for invalid connection '%s'."),
			                         nm_connection_get_id (connection));
		}

		return NULL;
	}

	return g_object_new (NMT_TYPE_EDITOR,
	                     "connection", connection,
	                     "type-data", type_data,
	                     "title", _("Edit connection"),
	                     "fullscreen-vertical", TRUE,
	                     NULL);
}
static NmtNewtForm *
nmt_connect_connection (const char *identifier)
{
	NmtNewtWidget *list;
	NMConnection *connection;
	NMDevice *device;
	NMObject *specific_object;
	NMActiveConnection *ac;

	list = nmt_connect_connection_list_new ();
	if (!nmt_connect_connection_list_get_connection (NMT_CONNECT_CONNECTION_LIST (list),
	                                                 identifier,
	                                                 &connection,
	                                                 &device,
	                                                 &specific_object,
	                                                 &ac))
		nmt_newt_message_dialog (_("No such connection '%s'"), identifier);
	else if (ac)
		nmt_newt_message_dialog (_("Connection is already active"));
	else
		activate_connection (connection, device, specific_object);
	g_object_unref (list);

	return NULL;
}
NmtNewtForm *
nmtui_hostname (int argc, char **argv)
{
	const char *hostname;
	char *tmp = NULL;
	NmtSyncOp op;
	GError *error = NULL;

	if (argc == 2)
		hostname = argv[1];
	else
		hostname = tmp = nmtui_hostname_run_dialog ();

	if (hostname) {
		nmt_sync_op_init (&op);
		nm_client_save_hostname_async (nm_client, hostname, NULL, hostname_set, &op);
		if (nmt_sync_op_wait_boolean (&op, &error)) {
			/* Translators: this indicates the result. ie, "I have set the hostname to ..." */
			nmt_newt_message_dialog (_("Set hostname to '%s'"), hostname);
		} else {
			nmt_newt_message_dialog (_("Unable to set hostname: %s"), error->message);
			g_error_free (error);
		}

		g_free (tmp);
	}

	return NULL;
}
Beispiel #4
0
static void
save_connection_and_exit (NmtNewtButton *button,
                          gpointer       user_data)
{
	NmtEditor *editor = user_data;
	NmtEditorPrivate *priv = NMT_EDITOR_GET_PRIVATE (editor);
	NmtSyncOp op;
	GError *error = NULL;

	if (!nm_connection_replace_settings_from_connection (priv->orig_connection,
	                                                     priv->edit_connection,
	                                                     &error)) {
		nmt_newt_message_dialog (_("Error saving connection: %s"), error->message);
		g_error_free (error);
		return;
	}

	nmt_sync_op_init (&op);
	if (NM_IS_REMOTE_CONNECTION (priv->orig_connection)) {
		nm_remote_connection_commit_changes (NM_REMOTE_CONNECTION (priv->orig_connection),
		                                     connection_updated, &op);
		if (!nmt_sync_op_wait_boolean (&op, &error)) {
			nmt_newt_message_dialog (_("Unable to save connection: %s"),
			                         error->message);
			g_error_free (error);
			return;
		}

		/* Clear secrets so they don't lay around in memory; they'll get
		 * requested again anyway next time the connection is edited.
		 */
		nm_connection_clear_secrets (priv->orig_connection);
	} else {
		nm_remote_settings_add_connection (nm_settings, priv->orig_connection,
		                                   connection_added, &op);
		if (!nmt_sync_op_wait_boolean (&op, &error)) {
			nmt_newt_message_dialog (_("Unable to add new connection: %s"),
			                         error->message);
			g_error_free (error);
			return;
		}
	}

	nmt_newt_form_quit (NMT_NEWT_FORM (editor));
}
static void
connection_deleted_callback (GObject      *connection,
                             GAsyncResult *result,
                             gpointer      user_data)
{
	ConnectionDeleteData *data = user_data;
	GError *error = NULL;

	if (!nm_remote_connection_delete_finish (data->connection, result, &error)) {
		nmt_newt_message_dialog (_("Unable to delete connection: %s"),
		                         error->message);
	} else
		data->got_callback = TRUE;

	if (error || (data->got_callback && data->got_signal))
		nmt_sync_op_complete_boolean (&data->op, error == NULL, error);
	g_clear_error (&error);
}
NmtNewtForm *
nmtui_edit (gboolean is_top, int argc, char **argv)
{
	NMConnection *conn = NULL;

	if (argc == 2) {
		if (nm_utils_is_uuid (argv[1]))
			conn = NM_CONNECTION (nm_client_get_connection_by_uuid (nm_client, argv[1]));
		if (!conn)
			conn = NM_CONNECTION (nm_client_get_connection_by_id (nm_client, argv[1]));

		if (!conn) {
			nmt_newt_message_dialog ("%s: no such connection '%s'\n", argv[0], argv[1]);
			return NULL;
		}

		return nmt_editor_new (conn);
	} else
		return nmt_edit_main_connection_list (is_top);
}
static void
remove_one_connection (NMRemoteConnection *connection)
{
	ConnectionDeleteData data;
	GError *error = NULL;

	data.got_callback = data.got_signal = FALSE;
	nmt_sync_op_init (&data.op);

	data.connection = connection;
	g_signal_connect (nm_client, NM_CLIENT_CONNECTION_REMOVED,
	                  G_CALLBACK (connection_removed_signal), &data);
	nm_remote_connection_delete_async (connection, NULL, connection_deleted_callback, &data);

	if (!nmt_sync_op_wait_boolean (&data.op, &error)) {
		nmt_newt_message_dialog (_("Could not delete connection '%s': %s"),
		                         nm_connection_get_id (NM_CONNECTION (connection)),
		                         error->message);
		g_error_free (error);
	}

	g_signal_handlers_disconnect_by_func (nm_client, G_CALLBACK (connection_removed_signal), &data);
}
/**
 * nmt_newt_edit_string:
 * @data: data to edit
 *
 * libnewt does not have a multi-line editable text component, so
 * nmt-newt provides this function instead, which will open the user's
 * editor to edit a file containing the given @data (ensuring that the
 * current screen state is saved before starting the editor and
 * restored after it returns).
 *
 * Returns: the edited data, or %NULL if an error occurred.
 */
char *
nmt_newt_edit_string (const char *data)
{
	gssize len, nwrote;
	char *filename, *argv[3];
	GError *error = NULL;
	int fd, status;
	char *new_data = NULL;

	fd = g_file_open_tmp ("XXXXXX.json", &filename, &error);
	if (fd == -1) {
		nmt_newt_message_dialog (_("Could not create temporary file: %s"), error->message);
		g_error_free (error);
		return NULL;
	}

	len = data ? strlen (data) : 0;
	while (len) {
		do
			nwrote = write (fd, data, len);
		while (nwrote == -1 && errno == EINTR);

		len -= nwrote;
		data += nwrote;
	}
	close (fd);

	argv[0] = (char *) g_getenv ("VISUAL");
	if (!argv[0])
		argv[0] = (char *) g_getenv ("EDITOR");
	if (!argv[0])
		argv[0] = (char *) "vi";
	argv[1] = filename;
	argv[2] = NULL;

	newtSuspend ();
	g_spawn_sync (NULL, argv, NULL,
	              G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN,
	              NULL, NULL, NULL, NULL,
	              &status, &error);
	newtResume ();

	if (error) {
		nmt_newt_message_dialog (_("Could not create temporary file: %s"), error->message);
		g_error_free (error);
		goto done;
	}

#if GLIB_CHECK_VERSION (2, 34, 0)
	G_GNUC_BEGIN_IGNORE_DEPRECATIONS
	if (!g_spawn_check_exit_status (status, &error)) {
		nmt_newt_message_dialog (_("Editor failed: %s"), error->message);
		g_error_free (error);
		goto done;
	}
	G_GNUC_END_IGNORE_DEPRECATIONS
#else
	if (WIFEXITED (status)) {
		if (WEXITSTATUS (status) != 0)
			nmt_newt_message_dialog (_("Editor failed with status %d"), WEXITSTATUS (status));
	} else if (WIFSIGNALED (status))
		nmt_newt_message_dialog (_("Editor failed with signal %d"), WTERMSIG (status));
#endif

	if (!g_file_get_contents (filename, &new_data, NULL, &error)) {
		nmt_newt_message_dialog (_("Could not re-read file: %s"), error->message);
		g_error_free (error);
		goto done;
	}

 done:
	unlink (filename);
	g_free (filename);

	return new_data;
}	
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);
}