NetworkManager/src/nmtui/nmt-password-dialog.c
Thomas Haller 615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00

337 lines
9.8 KiB
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2013 Red Hat, Inc.
*/
/**
* SECTION:nmt-password-dialog
* @short_description: A password dialog
*
* #NmtPasswordDialog is the password dialog used to get connection
* secrets when activating a connection.
*/
#include "libnm-client-aux-extern/nm-default-client.h"
#include "nmt-password-dialog.h"
#include "libnmc-base/nm-secret-agent-simple.h"
#include "nmtui.h"
G_DEFINE_TYPE(NmtPasswordDialog, nmt_password_dialog, NMT_TYPE_NEWT_FORM)
#define NMT_PASSWORD_DIALOG_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_PASSWORD_DIALOG, NmtPasswordDialogPrivate))
typedef struct {
char *request_id;
char *prompt;
GPtrArray *secrets;
GPtrArray *entries;
NmtNewtWidget *ok, *cancel;
NmtNewtWidget *last_entry;
NmtNewtWidget *secret_grid;
gboolean succeeded;
} NmtPasswordDialogPrivate;
enum {
PROP_0,
PROP_REQUEST_ID,
PROP_PROMPT,
PROP_SECRETS,
LAST_PROP
};
/**
* nmt_password_dialog_new:
* @request_id: the request ID from the #NMSecretAgentSimple
* @title: the dialog title
* @prompt: the prompt text to display
* @secrets: (element-type #NMSecretAgentSimpleSecret): the secrets requested
*
* Creates a new #NmtPasswordDialog to request passwords from
* the user.
*
* Returns: a new #NmtPasswordDialog.
*/
NmtNewtForm *
nmt_password_dialog_new(const char *request_id,
const char *title,
const char *prompt,
GPtrArray *secrets)
{
return g_object_new(NMT_TYPE_PASSWORD_DIALOG,
"request-id",
request_id,
"title",
title,
"prompt",
prompt,
"secrets",
secrets,
"escape-exits",
TRUE,
NULL);
}
static void
nmt_password_dialog_init(NmtPasswordDialog *dialog)
{
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
priv->entries = g_ptr_array_new();
}
static void
maybe_save_input_and_exit(NmtNewtWidget *widget, gpointer dialog)
{
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
int i;
/* This gets invoked when the user types Return in the final entry,
* but the form may not be fully valid in that case.
*/
if (!nmt_newt_widget_get_valid(priv->secret_grid))
return;
priv->succeeded = TRUE;
for (i = 0; i < priv->secrets->len; i++) {
NMSecretAgentSimpleSecret *secret = priv->secrets->pdata[i];
g_free(secret->value);
g_object_get(priv->entries->pdata[i], "text", &secret->value, NULL);
}
nmt_newt_form_quit(nmt_newt_widget_get_form(widget));
}
static void
nmt_password_dialog_constructed(GObject *object)
{
NmtPasswordDialog *dialog = NMT_PASSWORD_DIALOG(object);
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
NmtNewtWidget *widget;
NmtNewtGrid *grid, *secret_grid;
NmtNewtButtonBox *bbox;
int i;
widget = nmt_newt_grid_new();
nmt_newt_form_set_content(NMT_NEWT_FORM(dialog), widget);
grid = NMT_NEWT_GRID(widget);
widget = nmt_newt_textbox_new(0, 60);
nmt_newt_textbox_set_text(NMT_NEWT_TEXTBOX(widget), priv->prompt);
nmt_newt_grid_add(grid, widget, 0, 0);
widget = nmt_newt_grid_new();
nmt_newt_grid_add(grid, widget, 0, 1);
nmt_newt_widget_set_padding(widget, 0, 1, 0, 1);
priv->secret_grid = widget;
secret_grid = NMT_NEWT_GRID(widget);
for (i = 0; i < priv->secrets->len; i++) {
NMSecretAgentSimpleSecret *secret = priv->secrets->pdata[i];
NmtNewtEntryFlags flags;
widget = nmt_newt_label_new(secret->pretty_name);
nmt_newt_grid_add(secret_grid, widget, 0, i);
nmt_newt_widget_set_padding(widget, 4, 0, 1, 0);
flags = NMT_NEWT_ENTRY_NONEMPTY;
if (secret->is_secret)
flags |= NMT_NEWT_ENTRY_PASSWORD;
widget = nmt_newt_entry_new(30, flags);
if (secret->value)
nmt_newt_entry_set_text(NMT_NEWT_ENTRY(widget), secret->value);
nmt_newt_grid_add(secret_grid, widget, 1, i);
g_ptr_array_add(priv->entries, widget);
if (i == priv->secrets->len - 1) {
priv->last_entry = widget;
g_signal_connect(widget, "activated", G_CALLBACK(maybe_save_input_and_exit), dialog);
}
}
widget = nmt_newt_button_box_new(NMT_NEWT_BUTTON_BOX_HORIZONTAL);
nmt_newt_grid_add(grid, widget, 0, 2);
bbox = NMT_NEWT_BUTTON_BOX(widget);
priv->cancel = nmt_newt_button_box_add_end(NMT_NEWT_BUTTON_BOX(bbox), _("Cancel"));
nmt_newt_widget_set_exit_on_activate(priv->cancel, TRUE);
priv->ok = nmt_newt_button_box_add_end(NMT_NEWT_BUTTON_BOX(bbox), _("OK"));
g_signal_connect(priv->ok, "activated", G_CALLBACK(maybe_save_input_and_exit), dialog);
g_object_bind_property(priv->secret_grid,
"valid",
priv->ok,
"sensitive",
G_BINDING_SYNC_CREATE);
G_OBJECT_CLASS(nmt_password_dialog_parent_class)->constructed(object);
}
static void
nmt_password_dialog_finalize(GObject *object)
{
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(object);
g_free(priv->request_id);
g_free(priv->prompt);
nm_clear_pointer(&priv->entries, g_ptr_array_unref);
nm_clear_pointer(&priv->secrets, g_ptr_array_unref);
G_OBJECT_CLASS(nmt_password_dialog_parent_class)->finalize(object);
}
static void
nmt_password_dialog_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(object);
switch (prop_id) {
case PROP_REQUEST_ID:
priv->request_id = g_value_dup_string(value);
break;
case PROP_PROMPT:
priv->prompt = g_value_dup_string(value);
break;
case PROP_SECRETS:
priv->secrets = g_value_dup_boxed(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
nmt_password_dialog_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(object);
switch (prop_id) {
case PROP_REQUEST_ID:
g_value_set_string(value, priv->request_id);
break;
case PROP_PROMPT:
g_value_set_string(value, priv->prompt);
break;
case PROP_SECRETS:
g_value_set_boxed(value, priv->secrets);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
nmt_password_dialog_class_init(NmtPasswordDialogClass *dialog_class)
{
GObjectClass *object_class = G_OBJECT_CLASS(dialog_class);
g_type_class_add_private(dialog_class, sizeof(NmtPasswordDialogPrivate));
/* virtual methods */
object_class->constructed = nmt_password_dialog_constructed;
object_class->set_property = nmt_password_dialog_set_property;
object_class->get_property = nmt_password_dialog_get_property;
object_class->finalize = nmt_password_dialog_finalize;
/**
* NmtPasswordDialog:request-id:
*
* The request ID from the #NMSecretAgentSimple
*/
g_object_class_install_property(
object_class,
PROP_REQUEST_ID,
g_param_spec_string("request-id",
"",
"",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
/**
* NmtPasswordDialog:prompt:
*
* The prompt text.
*/
g_object_class_install_property(
object_class,
PROP_PROMPT,
g_param_spec_string("prompt",
"",
"",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
/**
* NmtPasswordDialog:secrets:
*
* The array of request secrets
*
* Element-Type: #NMSecretAgentSimpleSecret.
*/
g_object_class_install_property(
object_class,
PROP_SECRETS,
g_param_spec_boxed("secrets",
"",
"",
G_TYPE_PTR_ARRAY,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}
/**
* nmt_password_dialog_succeeded:
* @dialog: the #NmtPasswordDialog
*
* After the dialog has exited, returns %TRUE if the user clicked
* "OK", %FALSE if "Cancel".
*
* Returns: whether the dialog succeeded.
*/
gboolean
nmt_password_dialog_succeeded(NmtPasswordDialog *dialog)
{
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
return priv->succeeded;
}
/**
* nmt_password_dialog_get_request_id:
* @dialog: the #NmtPasswordDialog
*
* Gets the dialog's request ID.
*
* Returns: the dialog's request ID.
*/
const char *
nmt_password_dialog_get_request_id(NmtPasswordDialog *dialog)
{
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
return priv->request_id;
}
/**
* nmt_password_dialog_get_secrets:
* @dialog: the #NmtPasswordDialog
*
* Gets the dialog's secrets array.
*
* Returns: (transfer none): the dialog's secrets array.
*/
GPtrArray *
nmt_password_dialog_get_secrets(NmtPasswordDialog *dialog)
{
NmtPasswordDialogPrivate *priv = NMT_PASSWORD_DIALOG_GET_PRIVATE(dialog);
return priv->secrets;
}