summaryrefslogtreecommitdiff
path: root/src/progress.c
blob: 8e6e8951d54dae81dd2a3241c998d0b102b85cf9 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* vim: colorcolumn=80 ts=4 sw=4
 */
/*
 * progress.c
 *
 * Copyright © 2002 Sun Microsystems, Inc.
 * Copyright © 2021-2023 Logan Rathbone
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Original Author: Glynn Foster <glynn.foster@sun.com>
 */


#include "util.h"
#include "zenity.h"

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include <config.h>

static GtkBuilder *builder;
static ZenityData *zen_data;

static int pulsate_timeout = -1;
static gboolean autokill;
static gboolean no_cancel;
static gboolean auto_close;

static void zenity_progress_dialog_response (GtkWidget *widget, char *rstr, gpointer data);

static gboolean
zenity_progress_pulsate_progress_bar (gpointer user_data)
{
	gtk_progress_bar_pulse (GTK_PROGRESS_BAR (user_data));

	return TRUE;
}

static void
zenity_progress_pulsate_stop (void)
{
	if (pulsate_timeout > 0) {
		g_source_remove (pulsate_timeout);
		pulsate_timeout = -1;
	}
}

static void
zenity_progress_pulsate_start (GObject *progress_bar)
{
	if (pulsate_timeout == -1) {
		pulsate_timeout = g_timeout_add (100,
				zenity_progress_pulsate_progress_bar, progress_bar);
	}
}

static void
zenity_progress_update_time_remaining (ZenityProgressData *progress_data)
{
	static GObject *progress_time = NULL;
	static time_t start_time = (time_t) (-1);
	float percentage = progress_data->percentage;

	if (progress_time == NULL)
		progress_time = gtk_builder_get_object (builder,
				"zenity_progress_time");

	if (start_time == (time_t) (-1) || percentage <= 0.0 ||
		percentage >= 100.0)
	{
		start_time = time (NULL);
		gtk_label_set_text (GTK_LABEL (progress_time), "");
	}
	else
	{
		time_t current_time = time (NULL);
		time_t elapsed_time = current_time - start_time;
		time_t total_time =
			(time_t) (100.0 * elapsed_time / progress_data->percentage);
		time_t remaining_time = total_time - elapsed_time;
		gulong hours, minutes, seconds;
		g_autofree char *remaining_message = NULL;

		seconds = (gulong) (remaining_time % 60);
		remaining_time /= 60;
		minutes = (gulong) (remaining_time % 60);
		remaining_time /= 60;
		hours = (gulong) remaining_time;

		remaining_message =
			g_strdup_printf (_("Time remaining: %lu:%02lu:%02lu"),
					hours, minutes, seconds);
		gtk_label_set_text (GTK_LABEL (progress_time), remaining_message);
	}
}

static float
stof (const char *s)
{
	float rez = 0, fact = 1;

	if (*s == '-') {
		s++;
		fact = -1;
	}

	for (int point_seen = 0; *s; s++)
	{
		int d;

		if (*s == '.' || *s == ',') {
			point_seen = 1;
			continue;
		}

		d = *s - '0';
		if (d >= 0 && d <= 9) {
			if (point_seen) fact /= 10.0f;
			rez = rez * 10.0f + (float)d;
		}
	}
	return rez * fact;
}

static gboolean
zenity_progress_handle_stdin (GIOChannel *source, GIOCondition condition,
		gpointer data)
{
	ZenityProgressData *progress_data;
	GObject *progress_bar;
	GObject *progress_label;
	GtkWindow *parent;
	float percentage = 0.0;
	GIOStatus status = G_IO_STATUS_NORMAL;

	progress_data = data;
	progress_bar = gtk_builder_get_object (builder, "zenity_progress_bar");
	progress_label = gtk_builder_get_object (builder, "zenity_progress_text");
	parent = GTK_WINDOW(gtk_widget_get_native (GTK_WIDGET(progress_bar)));

	if ((condition & G_IO_IN) != 0)
	{
		g_autoptr(GString) string = g_string_new (NULL);
		g_autoptr(GError) error = NULL;

		while (source->is_readable != TRUE)
			;
		do {
			do {
				status = g_io_channel_read_line_string (
					source, string, NULL, &error);

				while (g_main_context_pending (NULL)) {
					g_main_context_iteration (NULL, FALSE);
				}
			} while (status == G_IO_STATUS_AGAIN);

			if (status != G_IO_STATUS_NORMAL)
			{
				if (error) {
					g_warning ("%s: %s",
							__func__, error->message);
					error = NULL;
				}
				continue;
			}

			if (! g_ascii_strncasecmp (string->str, "#", 1))
			{
				char *match;

				/* We have a comment, so let's try to change the label */
				match = g_strstr_len (string->str, strlen (string->str), "#");
				match++;
				gtk_label_set_text (GTK_LABEL (progress_label),
					g_strcompress (g_strchomp (g_strchug (match))));

			}
			else if (g_str_has_prefix (string->str, "pulsate"))
			{
				char *colon, *value;
				g_autofree char *command = NULL;

				zenity_util_strip_newline (string->str);

				colon = strchr (string->str, ':');
				if (colon == NULL) {
					continue;
				}

				/* split off the command and value */
				command =
					g_strstrip (g_strndup (string->str, colon - string->str));

				value = colon + 1;
				while (*value && g_ascii_isspace (*value))
					value++;

				if (! g_ascii_strcasecmp (value, "false"))
				{
					zenity_progress_pulsate_stop ();

					gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(progress_bar),
						progress_data->percentage / 100.0);
				}
				else {
					zenity_progress_pulsate_start (progress_bar);
				}
			}
			else
			{
				if (! g_ascii_isdigit (*(string->str)))
					continue;

				/* Now try to convert the thing to a number */
				percentage = CLAMP (stof (string->str), 0, 100);

				gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(progress_bar),
						percentage / 100.0);

				progress_data->percentage = percentage;

				if (progress_data->time_remaining == TRUE)
					zenity_progress_update_time_remaining (progress_data);

				if (percentage == 100)
				{
					if (!auto_close)
					{
						adw_message_dialog_set_response_enabled (ADW_MESSAGE_DIALOG(parent), "ok", TRUE);
						adw_message_dialog_set_default_response (ADW_MESSAGE_DIALOG(parent), "ok");
					}

					if (progress_data->autoclose)
					{
						zen_data->exit_code =
							zenity_util_return_exit_code (ZENITY_OK);

						zenity_util_gapp_quit (parent, zen_data);
					}
				}
			}

		} while ((g_io_channel_get_buffer_condition (source) & G_IO_IN) ==
				G_IO_IN &&
			status != G_IO_STATUS_EOF);
	}

	if ((condition & G_IO_IN) != G_IO_IN || status == G_IO_STATUS_EOF)
	{
		/* We assume that we are done, so stop the pulsating and de-sensitize
		 * the buttons */
		if (!no_cancel)
			adw_message_dialog_set_response_enabled (ADW_MESSAGE_DIALOG(parent), "cancel", FALSE);
		if (!auto_close)
		{
			adw_message_dialog_set_response_enabled (ADW_MESSAGE_DIALOG(parent), "ok", TRUE);
			adw_message_dialog_set_default_response (ADW_MESSAGE_DIALOG(parent), "ok");
		}

		gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), 1.0);

		zenity_progress_pulsate_stop ();

		if (progress_data->autoclose)
		{
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_OK);
			zenity_util_gapp_quit (parent, zen_data);
		}

		g_io_channel_shutdown (source, TRUE, NULL);
		return FALSE;
	}
	return TRUE;
}

static void
zenity_progress_read_info (ZenityProgressData *progress_data)
{
	GIOChannel *channel = g_io_channel_unix_new (0);

	g_io_channel_set_encoding (channel, NULL, NULL);
	g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
	g_io_add_watch (channel,
		G_IO_IN | G_IO_HUP,
		zenity_progress_handle_stdin,
		progress_data);
	/* We need to check the pulsate state here, because, the g_io_add_watch
	   doesn't call the zenity_progress_handle_stdin function if there's no
	   input. This fix the Bug 567663 */
	if (progress_data->pulsate)
	{
		GObject *progress_bar =
			gtk_builder_get_object (builder, "zenity_progress_bar");

		zenity_progress_pulsate_start (progress_bar);
	}
}

void
zenity_progress (ZenityData *data, ZenityProgressData *progress_data)
{
	GtkWidget *dialog;
	GObject *text;
	GObject *progress_bar;

	zen_data = data;
	builder = zenity_util_load_ui_file ("zenity_progress_dialog", "zenity_progress_box", NULL);

	if (builder == NULL) {
		data->exit_code = zenity_util_return_exit_code (ZENITY_ERROR);
		return;
	}

	text = gtk_builder_get_object (builder, "zenity_progress_text");

	dialog = GTK_WIDGET(gtk_builder_get_object (builder,
				"zenity_progress_dialog"));

	progress_bar = gtk_builder_get_object (builder, "zenity_progress_bar");

	g_signal_connect (dialog, "response", G_CALLBACK(zenity_progress_dialog_response), data);

	if (data->dialog_title)
		adw_message_dialog_set_heading (ADW_MESSAGE_DIALOG(dialog), data->dialog_title);;

	gtk_window_set_icon_name (GTK_WINDOW(dialog),
			"appointment-soon");

	if (data->width > -1 || data->height > -1)
		gtk_window_set_default_size (GTK_WINDOW(dialog),
				data->width, data->height);

	if (data->width > -1)
	{
		gtk_widget_set_size_request (GTK_WIDGET(text), data->width, -1);
	}
#if 0
	else
	{
		g_signal_connect_after (text, "size-allocate",
			G_CALLBACK(zenity_text_size_allocate), data);

		g_signal_connect_after (progress_bar, "size-allocate",
			G_CALLBACK(zenity_text_size_allocate), data);
	}
#endif

	if (data->modal)
		gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

	if (data->extra_label)
	{
		ZENITY_UTIL_ADD_EXTRA_LABELS (dialog)
	}

	if (data->ok_label) {
		ZENITY_UTIL_SETUP_OK_BUTTON_LABEL (dialog);
	}

	if (data->cancel_label)
	{
		ZENITY_UTIL_SETUP_CANCEL_BUTTON_LABEL (dialog);
	}

	if (progress_data->dialog_text) {
		gtk_label_set_markup (GTK_LABEL(text),
				g_strcompress (progress_data->dialog_text));
	}

	if (progress_data->percentage > -1) {
		gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(progress_bar),
				progress_data->percentage / 100.0);
	}

	autokill = progress_data->autokill;
	auto_close = progress_data->autoclose;
	no_cancel = progress_data->no_cancel;

	/* Unlike some other dialogs, this one starts off blank and we need to add
	 * the OK/Cancel buttons depending on the options.
	 */
	if (no_cancel)
		gtk_window_set_deletable (GTK_WINDOW(dialog), FALSE);
	else
		adw_message_dialog_add_response (ADW_MESSAGE_DIALOG(dialog), "cancel", _("_Cancel"));

	if (!auto_close)
	{
		adw_message_dialog_add_response (ADW_MESSAGE_DIALOG(dialog), "ok", _("_OK"));
		adw_message_dialog_set_response_enabled (ADW_MESSAGE_DIALOG(dialog), "ok", FALSE);
	}

	zenity_util_show_dialog (dialog);
	zenity_progress_read_info (progress_data);

	if (data->timeout_delay > 0)
	{
		g_timeout_add_seconds (data->timeout_delay,
			(GSourceFunc) zenity_util_timeout_handle,
			NULL);
	}
	zenity_util_gapp_main (GTK_WINDOW(dialog));
}

static void
zenity_progress_dialog_response (GtkWidget *widget, char *rstr, gpointer data)
{
	ZenityExitCode response = zenity_util_parse_dialog_response (rstr);

	switch (response)
	{
		case ZENITY_OK:
			zenity_util_exit_code_with_data (ZENITY_OK, zen_data);
			break;

		case ZENITY_CANCEL:
			/* We do not want to kill the parent process, in order to give the
			 * user the ability to choose the action to be taken. But we want
			 * to give people the option to choose this behavior.
			*/
			if (autokill) {
				kill (getppid (), 1);
			}
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_CANCEL);
			break;

		case ZENITY_TIMEOUT:
			zenity_util_exit_code_with_data (ZENITY_TIMEOUT, zen_data);
			break;

		default:
			if (zen_data->extra_label &&
				response < (int)g_strv_length (zen_data->extra_label))
			{
				printf ("%s\n", zen_data->extra_label[response]);
			}
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_ESC);
			break;
	}
	zenity_util_gapp_quit (GTK_WINDOW(widget), zen_data);
}