|
Packit Service |
87a54e |
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
Packit |
5756e2 |
/*
|
|
Packit |
5756e2 |
* Copyright (C) 2013 Red Hat, Inc.
|
|
Packit |
5756e2 |
*/
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
/**
|
|
Packit |
5756e2 |
* SECTION:nmtui
|
|
Packit |
5756e2 |
* @short_description: nmtui toplevel
|
|
Packit |
5756e2 |
*
|
|
Packit |
5756e2 |
* The top level of nmtui. Exists mostly just to call nmtui_connect(),
|
|
Packit |
5756e2 |
* nmtui_edit(), and nmtui_hostname().
|
|
Packit |
5756e2 |
*/
|
|
Packit |
5756e2 |
|
|
Packit Service |
2bceb2 |
#include "libnm/nm-default-client.h"
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
#include "nmtui.h"
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
#include <locale.h>
|
|
Packit |
5756e2 |
#include <stdlib.h>
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
#include "nm-libnm-aux/nm-libnm-aux.h"
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
#include "nmt-newt.h"
|
|
Packit |
5756e2 |
#include "nm-editor-bindings.h"
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
#include "nmtui-edit.h"
|
|
Packit |
5756e2 |
#include "nmtui-connect.h"
|
|
Packit |
5756e2 |
#include "nmtui-hostname.h"
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
NMClient * nm_client;
|
|
Packit |
5756e2 |
static GMainLoop *loop;
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
typedef NmtNewtForm *(*NmtuiSubprogram)(gboolean is_top, int argc, char **argv);
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static const struct {
|
|
Packit Service |
a1bd4f |
const char * name, *shortcut, *arg;
|
|
Packit Service |
a1bd4f |
const char * display_name;
|
|
Packit Service |
a1bd4f |
NmtuiSubprogram func;
|
|
Packit |
5756e2 |
} subprograms[] = {
|
|
Packit Service |
a1bd4f |
{"edit", "nmtui-edit", N_("connection"), N_("Edit a connection"), nmtui_edit},
|
|
Packit Service |
a1bd4f |
{"connect", "nmtui-connect", N_("connection"), N_("Activate a connection"), nmtui_connect},
|
|
Packit Service |
a1bd4f |
{"hostname", "nmtui-hostname", N_("new hostname"), N_("Set system hostname"), nmtui_hostname}};
|
|
Packit Service |
a1bd4f |
static const int num_subprograms = G_N_ELEMENTS(subprograms);
|
|
Packit |
5756e2 |
static NmtNewtForm *toplevel_form;
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static NmtNewtForm *
|
|
Packit Service |
a1bd4f |
quit_func(int argc, char **argv)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
if (toplevel_form)
|
|
Packit Service |
a1bd4f |
nmt_newt_form_quit(toplevel_form);
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
nmtui_quit();
|
|
Packit |
5756e2 |
|
|
Packit Service |
a1bd4f |
return NULL;
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
main_list_activated(NmtNewtWidget *widget, NmtNewtListbox *listbox)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtNewtForm * form;
|
|
Packit Service |
a1bd4f |
NmtuiSubprogram sub;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
sub = nmt_newt_listbox_get_active_key(listbox);
|
|
Packit Service |
a1bd4f |
if (sub) {
|
|
Packit Service |
a1bd4f |
form = sub(FALSE, 0, NULL);
|
|
Packit Service |
a1bd4f |
if (form) {
|
|
Packit Service |
a1bd4f |
nmt_newt_form_show(form);
|
|
Packit Service |
a1bd4f |
g_object_unref(form);
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
}
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static NmtNewtForm *
|
|
Packit Service |
a1bd4f |
nmtui_main(gboolean is_top, int argc, char **argv)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtNewtForm * form;
|
|
Packit Service |
a1bd4f |
NmtNewtWidget * widget, *ok;
|
|
Packit Service |
a1bd4f |
NmtNewtGrid * grid;
|
|
Packit Service |
a1bd4f |
NmtNewtListbox * listbox;
|
|
Packit Service |
a1bd4f |
NmtNewtButtonBox *bbox;
|
|
Packit Service |
a1bd4f |
int i;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
form = g_object_new(NMT_TYPE_NEWT_FORM,
|
|
Packit Service |
a1bd4f |
"title",
|
|
Packit Service |
a1bd4f |
_("NetworkManager TUI"),
|
|
Packit Service |
a1bd4f |
"escape-exits",
|
|
Packit Service |
a1bd4f |
TRUE,
|
|
Packit Service |
a1bd4f |
NULL);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
widget = nmt_newt_grid_new();
|
|
Packit Service |
a1bd4f |
nmt_newt_form_set_content(form, widget);
|
|
Packit Service |
a1bd4f |
grid = NMT_NEWT_GRID(widget);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
widget = nmt_newt_label_new(_("Please select an option"));
|
|
Packit Service |
a1bd4f |
nmt_newt_grid_add(grid, widget, 0, 0);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
widget = g_object_new(NMT_TYPE_NEWT_LISTBOX,
|
|
Packit Service |
a1bd4f |
"height",
|
|
Packit Service |
a1bd4f |
num_subprograms + 2,
|
|
Packit Service |
a1bd4f |
"skip-null-keys",
|
|
Packit Service |
a1bd4f |
TRUE,
|
|
Packit Service |
a1bd4f |
NULL);
|
|
Packit Service |
a1bd4f |
nmt_newt_grid_add(grid, widget, 0, 1);
|
|
Packit Service |
a1bd4f |
nmt_newt_widget_set_padding(widget, 0, 1, 0, 1);
|
|
Packit Service |
a1bd4f |
listbox = NMT_NEWT_LISTBOX(widget);
|
|
Packit Service |
a1bd4f |
g_signal_connect(widget, "activated", G_CALLBACK(main_list_activated), listbox);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
for (i = 0; i < num_subprograms; i++) {
|
|
Packit Service |
a1bd4f |
nmt_newt_listbox_append(listbox, _(subprograms[i].display_name), subprograms[i].func);
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
nmt_newt_listbox_append(listbox, "", NULL);
|
|
Packit Service |
a1bd4f |
nmt_newt_listbox_append(listbox, _("Quit"), quit_func);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
widget = nmt_newt_button_box_new(NMT_NEWT_BUTTON_BOX_HORIZONTAL);
|
|
Packit Service |
a1bd4f |
nmt_newt_grid_add(grid, widget, 0, 2);
|
|
Packit Service |
a1bd4f |
bbox = NMT_NEWT_BUTTON_BOX(widget);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
ok = nmt_newt_button_box_add_end(bbox, _("OK"));
|
|
Packit Service |
a1bd4f |
g_signal_connect(ok, "activated", G_CALLBACK(main_list_activated), listbox);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
toplevel_form = form;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
return form;
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
/**
|
|
Packit |
5756e2 |
* nmtui_quit:
|
|
Packit |
5756e2 |
*
|
|
Packit |
5756e2 |
* Causes nmtui to exit.
|
|
Packit |
5756e2 |
*/
|
|
Packit |
5756e2 |
void
|
|
Packit Service |
a1bd4f |
nmtui_quit(void)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
g_main_loop_quit(loop);
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
usage(void)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
const char *argv0 = g_get_prgname();
|
|
Packit Service |
a1bd4f |
const char *usage_str = _("Usage");
|
|
Packit Service |
a1bd4f |
int i;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
for (i = 0; i < num_subprograms; i++) {
|
|
Packit Service |
a1bd4f |
if (!strcmp(argv0, subprograms[i].shortcut)) {
|
|
Packit Service |
a1bd4f |
g_printerr("%s: %s [%s]\n", usage_str, argv0, _(subprograms[i].arg));
|
|
Packit Service |
a1bd4f |
exit(1);
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
g_printerr("%s: nmtui\n", usage_str);
|
|
Packit Service |
a1bd4f |
for (i = 0; i < num_subprograms; i++) {
|
|
Packit Service |
a1bd4f |
g_printerr("%*s nmtui %s [%s]\n",
|
|
Packit Service |
a1bd4f |
nmt_newt_text_width(usage_str),
|
|
Packit Service |
a1bd4f |
" ",
|
|
Packit Service |
a1bd4f |
subprograms[i].name,
|
|
Packit Service |
a1bd4f |
_(subprograms[i].arg));
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
exit(1);
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
typedef struct {
|
|
Packit Service |
a1bd4f |
NmtuiSubprogram subprogram;
|
|
Packit Service |
a1bd4f |
int argc;
|
|
Packit Service |
a1bd4f |
char ** argv;
|
|
Packit |
5756e2 |
} NmtuiStartupData;
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static void
|
|
Packit Service |
a1bd4f |
toplevel_form_quit(NmtNewtForm *form, gpointer user_data)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
nmtui_quit();
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
static gboolean
|
|
Packit Service |
a1bd4f |
idle_run_subprogram(gpointer user_data)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
NmtuiStartupData *data = user_data;
|
|
Packit Service |
a1bd4f |
NmtNewtForm * form;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
form = data->subprogram(TRUE, data->argc, data->argv);
|
|
Packit Service |
a1bd4f |
if (form) {
|
|
Packit Service |
a1bd4f |
g_signal_connect(form, "quit", G_CALLBACK(toplevel_form_quit), NULL);
|
|
Packit Service |
a1bd4f |
nmt_newt_form_show(form);
|
|
Packit Service |
a1bd4f |
g_object_unref(form);
|
|
Packit Service |
a1bd4f |
} else
|
|
Packit Service |
a1bd4f |
nmtui_quit();
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
return FALSE;
|
|
Packit |
5756e2 |
}
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
gboolean sleep_on_startup = FALSE;
|
|
Packit Service |
a1bd4f |
gboolean noinit = FALSE;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
GOptionEntry entries[] = {{"sleep",
|
|
Packit Service |
a1bd4f |
's',
|
|
Packit Service |
a1bd4f |
G_OPTION_FLAG_HIDDEN,
|
|
Packit Service |
a1bd4f |
G_OPTION_ARG_NONE,
|
|
Packit Service |
a1bd4f |
&sleep_on_startup,
|
|
Packit Service |
a1bd4f |
"Sleep on startup",
|
|
Packit Service |
a1bd4f |
NULL},
|
|
Packit Service |
a1bd4f |
{"noinit",
|
|
Packit Service |
a1bd4f |
'n',
|
|
Packit Service |
a1bd4f |
G_OPTION_FLAG_HIDDEN,
|
|
Packit Service |
a1bd4f |
G_OPTION_ARG_NONE,
|
|
Packit Service |
a1bd4f |
&noinit,
|
|
Packit Service |
a1bd4f |
"Don't initialize newt",
|
|
Packit Service |
a1bd4f |
NULL},
|
|
Packit Service |
a1bd4f |
{NULL}};
|
|
Packit |
5756e2 |
|
|
Packit |
5756e2 |
int
|
|
Packit Service |
a1bd4f |
main(int argc, char **argv)
|
|
Packit |
5756e2 |
{
|
|
Packit Service |
a1bd4f |
GOptionContext * opts;
|
|
Packit Service |
a1bd4f |
GError * error = NULL;
|
|
Packit Service |
a1bd4f |
NmtuiStartupData startup_data;
|
|
Packit Service |
a1bd4f |
const char * prgname;
|
|
Packit Service |
a1bd4f |
int i;
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
setlocale(LC_ALL, "");
|
|
Packit Service |
a1bd4f |
bindtextdomain(GETTEXT_PACKAGE, NMLOCALEDIR);
|
|
Packit Service |
a1bd4f |
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
|
|
Packit Service |
a1bd4f |
textdomain(GETTEXT_PACKAGE);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
opts = g_option_context_new(NULL);
|
|
Packit Service |
a1bd4f |
g_option_context_add_main_entries(opts, entries, NULL);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
if (!g_option_context_parse(opts, &argc, &argv, &error)) {
|
|
Packit Service |
a1bd4f |
g_printerr("%s: %s: %s\n", argv[0], _("Could not parse arguments"), error->message);
|
|
Packit Service |
a1bd4f |
exit(1);
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
g_option_context_free(opts);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
nm_editor_bindings_init();
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
if (!nmc_client_new_waitsync(NULL,
|
|
Packit Service |
a1bd4f |
&nm_client,
|
|
Packit Service |
a1bd4f |
&error,
|
|
Packit Service |
a1bd4f |
NM_CLIENT_INSTANCE_FLAGS,
|
|
Packit Service |
a1bd4f |
(guint) NM_CLIENT_INSTANCE_FLAGS_NO_AUTO_FETCH_PERMISSIONS,
|
|
Packit Service |
a1bd4f |
NULL)) {
|
|
Packit Service |
a1bd4f |
g_printerr(_("Could not contact NetworkManager: %s.\n"), error->message);
|
|
Packit Service |
a1bd4f |
g_error_free(error);
|
|
Packit Service |
a1bd4f |
exit(1);
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
if (!nm_client_get_nm_running(nm_client)) {
|
|
Packit Service |
a1bd4f |
g_printerr("%s\n", _("NetworkManager is not running."));
|
|
Packit Service |
a1bd4f |
exit(1);
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
if (sleep_on_startup)
|
|
Packit Service |
a1bd4f |
sleep(5);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
startup_data.subprogram = NULL;
|
|
Packit Service |
a1bd4f |
prgname = g_get_prgname();
|
|
Packit Service |
a1bd4f |
if (g_str_has_prefix(prgname, "lt-"))
|
|
Packit Service |
a1bd4f |
prgname += 3;
|
|
Packit Service |
a1bd4f |
if (!strcmp(prgname, "nmtui")) {
|
|
Packit Service |
a1bd4f |
if (argc > 1) {
|
|
Packit Service |
a1bd4f |
for (i = 0; i < num_subprograms; i++) {
|
|
Packit Service |
a1bd4f |
if (!strcmp(argv[1], subprograms[i].name)) {
|
|
Packit Service |
a1bd4f |
argc--;
|
|
Packit Service |
a1bd4f |
argv[0] = (char *) subprograms[i].shortcut;
|
|
Packit Service |
a1bd4f |
memmove(&argv[1], &argv[2], argc * sizeof(char *));
|
|
Packit Service |
a1bd4f |
startup_data.subprogram = subprograms[i].func;
|
|
Packit Service |
a1bd4f |
break;
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
} else
|
|
Packit Service |
a1bd4f |
startup_data.subprogram = nmtui_main;
|
|
Packit Service |
a1bd4f |
} else {
|
|
Packit Service |
a1bd4f |
for (i = 0; i < num_subprograms; i++) {
|
|
Packit Service |
a1bd4f |
if (!strcmp(prgname, subprograms[i].shortcut)) {
|
|
Packit Service |
a1bd4f |
startup_data.subprogram = subprograms[i].func;
|
|
Packit Service |
a1bd4f |
break;
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
}
|
|
Packit Service |
a1bd4f |
if (!startup_data.subprogram)
|
|
Packit Service |
a1bd4f |
usage();
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
if (!noinit)
|
|
Packit Service |
a1bd4f |
nmt_newt_init();
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
startup_data.argc = argc;
|
|
Packit Service |
a1bd4f |
startup_data.argv = argv;
|
|
Packit Service |
a1bd4f |
g_idle_add(idle_run_subprogram, &startup_data);
|
|
Packit Service |
a1bd4f |
loop = g_main_loop_new(NULL, FALSE);
|
|
Packit Service |
a1bd4f |
g_main_loop_run(loop);
|
|
Packit Service |
a1bd4f |
g_main_loop_unref(loop);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
if (!noinit)
|
|
Packit Service |
a1bd4f |
nmt_newt_finished();
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
g_object_unref(nm_client);
|
|
Packit Service |
a1bd4f |
|
|
Packit Service |
a1bd4f |
return 0;
|
|
Packit |
5756e2 |
}
|