summaryrefslogtreecommitdiff
path: root/clients/tui/newt/nmt-newt-utils.c
blob: a8541829de9bad3c586d21d874d1d0da22822692 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2013 Red Hat, Inc.
 */

/**
 * SECTION:nmt-newt-utils
 * @short_description: Utility functions
 */

#include "nm-default.h"

#include <errno.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/wait.h>

#include "nmt-newt-utils.h"

static void
nmt_newt_dialog_g_log_handler (const char     *log_domain,
                               GLogLevelFlags  log_level,
                               const char     *message,
                               gpointer        user_data)
{
	const char *level_name;
	char *full_message;
	int screen_width, screen_height;
	newtComponent text, ok, form;
	newtGrid grid;

	g_assert (!(log_level & G_LOG_FLAG_RECURSION));

	if (log_level & G_LOG_LEVEL_DEBUG)
		return;

	switch (log_level & G_LOG_LEVEL_MASK) {
	case G_LOG_LEVEL_ERROR:
		level_name = "ERROR";
		break;
	case G_LOG_LEVEL_CRITICAL:
		level_name = "CRITICAL";
		break;
	case G_LOG_LEVEL_WARNING:
		level_name = "WARNING";
		break;
	case G_LOG_LEVEL_MESSAGE:
		level_name = "Message";
		break;
	default:
		level_name = NULL;
	}

	full_message = g_strdup_printf ("%s%s%s%s%s",
	                                log_domain ? log_domain : "",
	                                log_domain && level_name ? " " : "",
	                                level_name ? level_name : "",
	                                log_domain || level_name ? ": " : "",
	                                message);

	/* newtWinMessage() wraps the window too narrowly by default, so
	 * we don't want to use that. But we intentionally avoid using any
	 * NmtNewt classes, to avoid possible error recursion.
	 */

	newtGetScreenSize (&screen_width, &screen_height);
	text = newtTextboxReflowed (-1, -1, full_message, MAX (70, screen_width - 10), 0, 0, 0);
	g_free (full_message);

	ok = newtButton (-1, -1, "OK");

	grid = newtCreateGrid (1, 2);
	newtGridSetField (grid, 0, 0, NEWT_GRID_COMPONENT, text, 0, 0, 0, 0, 0, 0);
	newtGridSetField (grid, 0, 1, NEWT_GRID_COMPONENT, ok, 0, 1, 0, 0,
	                  NEWT_ANCHOR_RIGHT, 0);

	newtGridWrappedWindow (grid, (char *) (level_name ? level_name : ""));
	newtGridFree (grid, TRUE);

	form = newtForm (NULL, NULL, 0);
	newtFormAddComponents (form, text, ok, NULL);
	newtRunForm (form);
	newtFormDestroy (form);
	newtPopWindow ();
}

static void
nmt_newt_basic_g_log_handler (const char     *log_domain,
                              GLogLevelFlags  log_level,
                              const char     *message,
                              gpointer        user_data)
{
	newtSuspend ();
	g_log_default_handler (log_domain, log_level, message, NULL);
	newtResume ();
}

static void
nmt_newt_suspend_callback (gpointer user_data)
{
	newtSuspend ();
	kill (getpid (), SIGTSTP);
	newtResume ();
}

/**
 * nmt_newt_init:
 *
 * Wrapper for newtInit() that also does some nmt-newt-internal setup.
 * This should be called once, before any other nmt-newt functions.
 */
void
nmt_newt_init (void)
{
	newtInit ();
	newtCls ();

	newtSetColor (NEWT_COLORSET_CHECKBOX, "black", "lightgray");
	newtSetColor (NMT_NEWT_COLORSET_BAD_LABEL, "red", "lightgray");
	newtSetColor (NMT_NEWT_COLORSET_PLAIN_LABEL, "black", "lightgray");
	newtSetColor (NMT_NEWT_COLORSET_DISABLED_BUTTON, "blue", "lightgray");
	newtSetColor (NMT_NEWT_COLORSET_TEXTBOX_WITH_BACKGROUND, "black", "white");

	if (g_getenv ("NMTUI_DEBUG"))
		g_log_set_default_handler (nmt_newt_dialog_g_log_handler, NULL);
	else
		g_log_set_default_handler (nmt_newt_basic_g_log_handler, NULL);

	newtSetSuspendCallback (nmt_newt_suspend_callback, NULL);
}

/**
 * nmt_newt_finished:
 *
 * Wrapper for newtFinished(). Should be called at the end of the program.
 */
void
nmt_newt_finished (void)
{
	newtFinished ();
	g_log_set_default_handler (g_log_default_handler, NULL);
}

/**
 * nmt_newt_message_dialog:
 * @message: a printf()-style message format
 * @...: arguments
 *
 * Displays the given message in a dialog box with a single "OK"
 * button, and returns after the user clicks "OK".
 */
void
nmt_newt_message_dialog  (const char *message,
                          ...)
{
	va_list ap;
	char *msg, *msg_lc, *ok_lc;

	va_start (ap, message);
	msg = g_strdup_vprintf (message, ap);
	va_end (ap);

	msg_lc = nmt_newt_locale_from_utf8 (msg);
	ok_lc = nmt_newt_locale_from_utf8 (_("OK"));
	newtWinMessage (NULL, ok_lc, "%s", msg_lc);

	g_free (ok_lc);
	g_free (msg_lc);
	g_free (msg);
}

/**
 * nmt_newt_choice_dialog:
 * @button1: the label for the first button
 * @button2: the label for the second button
 * @message: a printf()-style message format
 * @...: arguments
 *
 * Displays the given message in a dialog box with two buttons with
 * the indicated labels, and waits for the user to click one.
 *
 * Returns: which button was clicked: 0 for @button1 or 1 for @button2
 */
int
nmt_newt_choice_dialog (const char *button1,
                        const char *button2,
                        const char *message,
                        ...)
{
	va_list ap;
	char *msg, *msg_lc, *button1_lc, *button2_lc;
	int choice;

	va_start (ap, message);
	msg = g_strdup_vprintf (message, ap);
	va_end (ap);

	msg_lc = nmt_newt_locale_from_utf8 (msg);
	button1_lc = nmt_newt_locale_from_utf8 (button1);
	button2_lc = nmt_newt_locale_from_utf8 (button2);
	choice = newtWinChoice (NULL, button1_lc, button2_lc, "%s", msg_lc);

	g_free (button1_lc);
	g_free (button2_lc);
	g_free (msg_lc);
	g_free (msg);

	return choice;
}

/**
 * nmt_newt_locale_to_utf8:
 * @str_lc: a string in the user's locale encoding
 *
 * Convenience wrapper around g_locale_to_utf8().
 *
 * Note that libnewt works in terms of the user's locale character
 * set, NOT UTF-8, so all strings received from libnewt must be
 * converted back to UTF-8 before being returned to the caller or used
 * in other APIs.
 *
 * Returns: @str_lc, converted to UTF-8.
 */
char *
nmt_newt_locale_to_utf8 (const char *str_lc)
{
	char *str_utf8;

	str_utf8 = g_locale_to_utf8 (str_lc, -1, NULL, NULL, NULL);
	if (!str_utf8)
		str_utf8 = g_strdup ("");
	return str_utf8;
}

/**
 * nmt_newt_locale_from_utf8:
 * @str_utf8: a UTF-8 string
 *
 * Convenience wrapper around g_locale_from_utf8().
 *
 * Note that libnewt works in terms of the user's locale character
 * set, NOT UTF-8, so all strings from nmt-newt must be converted to
 * locale encoding before being passed to libnewt.
 *
 * Returns: @str_utf8, converted to the user's locale encoding.
 */
char *
nmt_newt_locale_from_utf8 (const char *str_utf8)
{
	char *str_lc;

	str_lc = g_locale_from_utf8 (str_utf8, -1, NULL, NULL, NULL);
	if (!str_lc)
		str_lc = g_strdup ("");
	return str_lc;
}

/**
 * nmt_newt_text_width
 * @str: a UTF-8 string
 *
 * Computes the width (in terminal columns) of @str.
 *
 * Returns: the width of @str
 */
int
nmt_newt_text_width (const char *str)
{
	int width;
	gunichar ch;

	for (width = 0; *str; str = g_utf8_next_char (str)) {
		ch = g_utf8_get_char (str);

		/* Based on _vte_iso2022_unichar_width */
		if (G_LIKELY (ch < 0x80))
			width += 1;
		else if (G_UNLIKELY (g_unichar_iszerowidth (ch)))
			width += 0;
		else if (G_UNLIKELY (g_unichar_iswide (ch)))
			width += 2;
		else
			width += 1;
	}

	return width;
}

/**
 * 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;
}