summaryrefslogtreecommitdiff
path: root/clients/tui/nmtui-hostname.c
blob: 07e0f6058ca0a5fc5605987670bb61bf7145e581 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2013 Red Hat, Inc.
 */

/**
 * SECTION:nmtui-hostname
 * @short_description: hostname-setting functionality
 *
 * nmtui-hostname implements the "set hostname" functionality
 */

#include "libnm/nm-default-client.h"

#include "nmt-newt.h"

#include "nmtui.h"
#include "nmtui-hostname.h"
#include "nmt-utils.h"

static char *
nmtui_hostname_run_dialog(void)
{
    NmtNewtForm *     form;
    NmtNewtWidget *   widget, *ok, *cancel;
    NmtNewtGrid *     grid;
    NmtNewtEntry *    entry;
    NmtNewtButtonBox *bbox;
    char *            hostname, *ret = NULL;

    form = g_object_new(NMT_TYPE_NEWT_FORM, "title", _("Set Hostname"), "escape-exits", TRUE, NULL);

    widget = nmt_newt_grid_new();
    nmt_newt_form_set_content(form, widget);
    grid = NMT_NEWT_GRID(widget);

    widget = nmt_newt_label_new(_("Hostname"));
    nmt_newt_grid_add(grid, widget, 0, 0);

    widget = nmt_newt_entry_new(40, 0);
    nmt_newt_widget_set_exit_on_activate(widget, TRUE);
    nmt_newt_grid_add(grid, widget, 1, 0);
    nmt_newt_widget_set_padding(widget, 1, 0, 0, 0);
    entry = NMT_NEWT_ENTRY(widget);

    widget = nmt_newt_button_box_new(NMT_NEWT_BUTTON_BOX_HORIZONTAL);
    nmt_newt_grid_add(grid, widget, 1, 1);
    nmt_newt_widget_set_padding(widget, 0, 1, 0, 0);
    bbox = NMT_NEWT_BUTTON_BOX(widget);

    cancel = nmt_newt_button_box_add_end(bbox, _("Cancel"));
    nmt_newt_widget_set_exit_on_activate(cancel, TRUE);
    ok = nmt_newt_button_box_add_end(bbox, _("OK"));
    nmt_newt_widget_set_exit_on_activate(ok, TRUE);

    g_object_get(G_OBJECT(nm_client), NM_CLIENT_HOSTNAME, &hostname, NULL);
    nmt_newt_entry_set_text(entry, hostname);
    g_free(hostname);

    widget = nmt_newt_form_run_sync(form);
    if (widget == (NmtNewtWidget *) entry || widget == ok)
        ret = g_strdup(nmt_newt_entry_get_text(entry));

    g_object_unref(form);
    return ret;
}

static void
hostname_set(GObject *object, GAsyncResult *result, gpointer op)
{
    GError *error = NULL;

    nm_client_save_hostname_finish(NM_CLIENT(object), result, &error);
    nmt_sync_op_complete_boolean(op, error == NULL, error);
    g_clear_error(&error);
}

NmtNewtForm *
nmtui_hostname(gboolean is_top, 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;
}