File: progress.c

package info (click to toggle)
setools 3.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,840 kB
  • sloc: ansic: 82,247; tcl: 13,145; cpp: 4,885; makefile: 1,603; yacc: 779; lex: 296; python: 57; sh: 50
file content (202 lines) | stat: -rw-r--r-- 5,446 bytes parent folder | download | duplicates (4)
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
/**
 *  @file
 *  Routines to show a progress dialog, indicating that the
 *  application is doing something.
 *
 *  @author Jeremy A. Mowery jmowery@tresys.com
 *  @author Jason Tang jtang@tresys.com
 *
 *  Copyright (C) 2006-2007 Tresys Technology, LLC
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include "progress.h"
#include "utilgui.h"

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gprintf.h>

struct progress
{
	toplevel_t *top;
	GtkWidget *progress;
	GtkWidget *label1, *label2;
	char *s;
	int done;
	GCond *cond;
	GMutex *mutex;
};

progress_t *progress_create(toplevel_t * top)
{
	progress_t *p;
	GtkWidget *vbox;

	if ((p = calloc(1, sizeof(*p))) == NULL) {
		return NULL;
	}
	p->top = top;
	p->progress = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_modal(GTK_WINDOW(p->progress), TRUE);
	gtk_window_set_transient_for(GTK_WINDOW(p->progress), toplevel_get_window(top));
	gtk_window_set_default_size(GTK_WINDOW(p->progress), 300, 100);
	vbox = gtk_vbox_new(FALSE, 2);
	gtk_container_add(GTK_CONTAINER(p->progress), vbox);
	p->label1 = gtk_label_new(NULL);
	gtk_container_add(GTK_CONTAINER(vbox), p->label1);
	p->label2 = gtk_label_new(NULL);
	gtk_container_add(GTK_CONTAINER(vbox), p->label2);
	gtk_widget_show(p->label1);
	gtk_widget_show(p->label2);
	gtk_widget_show(vbox);
	util_cursor_wait(p->progress);
	p->cond = g_cond_new();
	p->mutex = g_mutex_new();
	return p;
}

void progress_destroy(progress_t ** progress)
{
	if (progress != NULL && *progress != NULL) {
		free((*progress)->s);
		g_cond_free((*progress)->cond);
		g_mutex_free((*progress)->mutex);
		free(*progress);
		*progress = NULL;
	}
}

void progress_show(progress_t * progress, const char *title)
{
	gtk_label_set_text(GTK_LABEL(progress->label1), title);
	gtk_label_set_text(GTK_LABEL(progress->label2), "");
	gtk_widget_show(progress->progress);
	gtk_window_deiconify(GTK_WINDOW(progress->progress));
	gtk_window_set_title(GTK_WINDOW(progress->progress), title);
	progress->done = 0;
}

void progress_hide(progress_t * progress)
{
	gtk_widget_hide(progress->progress);
}

int progress_wait(progress_t * progress)
{
	GTimeVal wait_time = { 0, 50000 };
	g_mutex_lock(progress->mutex);
	while (!progress->done) {
		if (progress->s != NULL) {
			gtk_label_set_text(GTK_LABEL(progress->label2), progress->s);
			free(progress->s);
			progress->s = NULL;
		}
		// only process one event -- the policy source could
		// still be loading, and this dialog should not block
		// until the entire source has been read
		gtk_main_iteration_do(FALSE);
		g_cond_timed_wait(progress->cond, progress->mutex, &wait_time);
	}
	g_mutex_unlock(progress->mutex);
	if (progress->done < 0) {
		toplevel_ERR(progress->top, GTK_LABEL(progress->label2)->label);
		return progress->done;
	} else if (progress->done > 1) {
		toplevel_WARN(progress->top, GTK_LABEL(progress->label2)->label);
		return progress->done - 1;
	} else {
		progress->done = 0;
		return 0;
	}
}

void progress_done(progress_t * progress)
{
	g_mutex_lock(progress->mutex);
	progress->done = 1;
	g_cond_signal(progress->cond);
	g_mutex_unlock(progress->mutex);
}

void progress_warn(progress_t * progress, char *reason, ...)
{
	gchar *s;
	va_list ap;
	g_mutex_lock(progress->mutex);
	if (reason != NULL) {
		va_start(ap, reason);
		g_vasprintf(&s, reason, ap);
		free(progress->s);
		progress->s = s;
		va_end(ap);
	}
	progress->done = 2;
	g_cond_signal(progress->cond);
	g_mutex_unlock(progress->mutex);
}

void progress_abort(progress_t * progress, char *reason, ...)
{
	gchar *s;
	va_list ap;
	g_mutex_lock(progress->mutex);
	if (reason != NULL) {
		va_start(ap, reason);
		g_vasprintf(&s, reason, ap);
		free(progress->s);
		progress->s = s;
		va_end(ap);
	}
	progress->done = -1;
	g_cond_signal(progress->cond);
	g_mutex_unlock(progress->mutex);
}

static void progress_update_label(progress_t * progress, const char *fmt, va_list va_args)
{
	gchar *s = NULL;
	g_vasprintf(&s, fmt, va_args);
	g_mutex_lock(progress->mutex);
	free(progress->s);
	progress->s = s;
	g_cond_signal(progress->cond);
	g_mutex_unlock(progress->mutex);
}

void progress_update(progress_t * progress, char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	progress_update_label(progress, fmt, ap);
	va_end(ap);
}

void progress_poldiff_handle_func(void *arg, const poldiff_t * diff __attribute__ ((unused)), int level
				  __attribute__ ((unused)), const char *fmt, va_list va_args)
{
	progress_t *progress = arg;
	progress_update_label(progress, fmt, va_args);
}

void progress_apol_handle_func(void *varg, const apol_policy_t * p __attribute__ ((unused)), int level
			       __attribute__ ((unused)), const char *fmt, va_list argp)
{
	progress_t *progress = varg;
	progress_update_label(progress, fmt, argp);
}