Debian Codegraph @

src/modules/frontend/gtk/progress.c

raw
1 /*****************************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * cdebconf is (c) 2000-2007 Randolph Chung and others under the following
7 * license.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *****************************************************************************/
33
34 /** @file progress.c
35 * progress bar support for the GTK+ frontend of cdebconf
36 */
37
38 #include "progress.h"
39
40 #include <string.h>
41 #include <glib.h>
42 #include <gdk/gdkkeysyms.h>
43 #include <gtk/gtk.h>
44
45 #include "frontend.h"
46
47 #include "cdebconf_gtk.h"
48 #include "fe_data.h"
49 #include "ui.h"
50 #ifdef DI_UDEB
51 # include "di.h"
52 #endif /* DI_UDEB */
53
54 /** Horizontal padding of the progress bar */
55 #define PROGRESSBAR_HPADDING 60
56 /** Vertical padding of the progress bar */
57 #define PROGRESSBAR_VPADDING 60
58
59 /** Internal data for progress bar support.
60 *
61 * @see frontend_data#progress_data
62 */
63 struct progress_data {
64 /** cdebconf frontend */
65 struct frontend * fe;
66
67 /** progress bar widget */
68 GtkWidget * progress_bar;
69
70 /** label to display progress info */
71 GtkWidget * progress_label;
72
73 /** container for progress bar and progress info */
74 GtkWidget * progress_box;
75
76 /** cancel button
77 *
78 * @see frontend_data#action_box
79 */
80 GtkWidget * cancel_button;
81
82 /** store title in order to restore it after a GO inside PROGRESS */
83 gchar * fe_title;
84 };
85
86 /** Create the progress bar widget in the given container.
87 *
88 * @param progress_data progress data
89 * @param container the container where will be added the progress bar
90 */
91 static void create_progress_bar(struct progress_data * progress_data,
92 GtkWidget * container)
93 {
94 GtkWidget * progress_bar;
95
96 progress_bar = gtk_progress_bar_new();
97
98 gtk_progress_bar_set_ellipsize(GTK_PROGRESS_BAR(progress_bar),
99 PANGO_ELLIPSIZE_MIDDLE);
100
101 gtk_box_pack_start(GTK_BOX(container), progress_bar,
102 FALSE /* don't expand */,
103 FALSE /* don't fill */, 0 /* padding */);
104
105 g_object_ref(G_OBJECT(progress_bar));
106 progress_data->progress_bar = progress_bar;
107 }
108
109 /** Unference the progress bar.
110 *
111 * The container has to take care of destroying the widget.
112 *
113 * @param progress_data progress data
114 */
115 static void destroy_progress_bar(struct progress_data * progress_data)
116 {
117 GtkWidget * progress_bar = progress_data->progress_bar;
118
119 if (NULL != progress_bar) {
120 progress_data->progress_bar = NULL;
121 g_object_unref(progress_bar);
122 /* widget will be destroyed by destroy_progress_box */
123 }
124 }
125
126 /** Create the label widget in the given container.
127 *
128 * @param progress_data progress data
129 * @param container the container where the label will be added
130 */
131 static void create_progress_label(struct progress_data * progress_data,
132 GtkWidget * container)
133 {
134 struct frontend_data * fe_data = progress_data->fe->data;
135 GtkStyle * style;
136 GtkWidget * progress_label;
137 PangoFontDescription * font_desc;
138
139 /* XXX: check null! */
140 progress_label = gtk_entry_new();
141
142 style = gtk_widget_get_style(fe_data->window);
143 gtk_widget_modify_base(progress_label, GTK_STATE_NORMAL, style->bg);
144 gtk_editable_set_editable(GTK_EDITABLE(progress_label), FALSE);
145 gtk_entry_set_has_frame(GTK_ENTRY(progress_label), FALSE);
146 gtk_widget_set_can_focus(GTK_WIDGET(progress_label), FALSE);
147
148 /* XXX: check null! */
149 font_desc = pango_font_description_new();
150 pango_font_description_set_style(font_desc, PANGO_STYLE_ITALIC);
151 gtk_widget_modify_font(progress_label, font_desc);
152 pango_font_description_free(font_desc);
153
154 gtk_box_pack_start(GTK_BOX(container), progress_label,
155 FALSE /* don't expand */, FALSE /* don't fill */,
156 DEFAULT_PADDING);
157
158 g_object_ref(G_OBJECT(progress_label));
159 progress_data->progress_label = progress_label;
160 }
161
162 /** Unreference the label.
163 *
164 * The container has to take care of destroying the widget.
165 *
166 * @param progress_data progress data
167 */
168 static void destroy_progress_label(struct progress_data * progress_data)
169 {
170 GtkWidget * progress_label = progress_data->progress_label;
171
172 if (NULL != progress_label) {
173 progress_data->progress_label = NULL;
174 g_object_unref(progress_label);
175 /* widget will be destroyed by destroy_progress_box */
176 }
177 }
178
179 /** Create the container including the progress bar and the associated
180 * information label.
181 *
182 * @param progress_data progress data
183 */
184 static void create_progress_box(struct progress_data * progress_data)
185 {
186 GtkWidget * progress_box;
187
188 progress_box = gtk_vbox_new(FALSE /* don't make children equal */,
189 0 /* padding */);
190
191 create_progress_bar(progress_data, progress_box);
192 create_progress_label(progress_data, progress_box);
193
194 cdebconf_gtk_center_widget(&progress_box, PROGRESSBAR_HPADDING,
195 PROGRESSBAR_VPADDING);
196
197 g_object_ref(G_OBJECT(progress_box));
198 progress_data->progress_box = progress_box;
199 }
200
201 /** Unreference the container with the progress bar and the associated label.
202 *
203 * This will take care of destroying the label and the progress bar widgets.
204 *
205 * @param progress_data progress data
206 */
207 static void destroy_progress_box(struct progress_data * progress_data)
208 {
209 GtkWidget * progress_box = progress_data->progress_box;
210
211 if (NULL != progress_box) {
212 progress_data->progress_box = NULL;
213 g_object_unref(progress_box);
214 gtk_widget_destroy(progress_box);
215 }
216 destroy_progress_label(progress_data);
217 destroy_progress_bar(progress_data);
218 }
219
220 /** Show the progress widgets.
221 *
222 * This will actually add the widgets to the corresponding containers.
223 * The main title saved when starting the PROGRESS operation will be restored
224 * from the value saved when START was called. This is needed when GO is
225 * called during a PROGRESS operation.
226 *
227 * @param fe cdebconf frontend
228 * @see cdebconf_gtk_hide_progress
229 */
230 void cdebconf_gtk_show_progress(struct frontend * fe)
231 {
232 struct frontend_data * fe_data = fe->data;
233 struct progress_data * progress_data = fe_data->progress_data;
234
235 g_assert(NULL != progress_data);
236 if (NULL == gtk_widget_get_parent(progress_data->progress_box)) {
237 gtk_box_pack_start(
238 GTK_BOX(fe_data->target_box), progress_data->progress_box,
239 FALSE /* don't expand */, FALSE /* don't fill */, DEFAULT_PADDING);
240 }
241 if (NULL != progress_data->cancel_button &&
242 NULL == gtk_widget_get_parent(progress_data->cancel_button)) {
243 gtk_box_pack_start(
244 GTK_BOX(fe_data->action_box), progress_data->cancel_button,
245 TRUE /* expand */, TRUE /* fill */, DEFAULT_PADDING);
246 }
247 /* restore main title */
248 g_free(fe->title);
249 fe->title = g_strdup(progress_data->fe_title);
250 cdebconf_gtk_update_frontend_title(fe);
251 gtk_widget_show_all(progress_data->progress_box);
252 gtk_widget_show_all(fe_data->action_box);
253 }
254
255 /** Hide the progress widgets.
256 *
257 * This will actually remove the widgets from their respective containers
258 * in order to leave the room for GO handling.
259 *
260 * @param fe cdebconf frotend
261 * @see cdebconf_gtk_show_progress
262 */
263 void cdebconf_gtk_hide_progress(struct frontend * fe)
264 {
265 struct frontend_data * fe_data = fe->data;
266 struct progress_data * progress_data = fe_data->progress_data;
267
268 if (NULL != progress_data) {
269 if (NULL != progress_data->progress_box) {
270 gtk_container_remove(GTK_CONTAINER(fe_data->target_box),
271 progress_data->progress_box);
272 }
273 if (NULL != progress_data->cancel_button) {
274 gtk_container_remove(GTK_CONTAINER(fe_data->action_box),
275 progress_data->cancel_button);
276 }
277 }
278 }
279
280 /** Key event handler implementing the "Cancel" key shortcut.
281 *
282 * @param widget main window
283 * @param key the pressed key
284 * @param fe cdebconf frontend
285 * @return TRUE if "Cancel" was handled, FALSE otherwise
286 */
287 static gboolean handle_cancel_key(GtkWidget * widget, GdkEventKey * key,
288 struct frontend * fe)
289 {
290 if (GDK_KEY_Escape == key->keyval) {
291 cdebconf_gtk_set_answer_goback(fe);
292 return TRUE;
293 }
294 return FALSE;
295 }
296
297 /** Handler for the "clicked" signal on cancel button.
298 *
299 * @param widget button
300 * @param fe cdebconf frontend
301 */
302 static void handle_cancel_click(GtkWidget * widget,
303 struct frontend * fe)
304 {
305 /* Give user feedback that button has been clicked. */
306 gtk_widget_set_sensitive(widget, FALSE);
307 cdebconf_gtk_set_answer_goback(fe);
308 }
309
310 /** Create the "Cancel" button.
311 *
312 * @param progress_data progress data
313 * @see frontend_data#action_box
314 */
315 static void create_cancel_button(struct progress_data * progress_data)
316 {
317 struct frontend * fe = progress_data->fe;
318 GtkWidget * button;
319 char * label;
320
321 label = cdebconf_gtk_get_text(fe, "debconf/button-cancel", "Cancel");
322 button = gtk_button_new_with_label(label);
323 g_free(label);
324
325 g_signal_connect(G_OBJECT(button), "clicked",
326 G_CALLBACK(handle_cancel_click), fe);
327 cdebconf_gtk_add_global_key_handler(
328 fe, button, G_CALLBACK(handle_cancel_key));
329 cdebconf_gtk_add_button(fe, button);
330
331 g_object_ref(G_OBJECT(button));
332 progress_data->cancel_button = button;
333 }
334
335 /** Unreference and destroy the "Cancel" button.
336 *
337 * @param progress_data progress data
338 */
339 static void destroy_cancel_button(struct progress_data * progress_data)
340 {
341 GtkWidget * cancel_button = progress_data->cancel_button;
342
343 if (NULL != cancel_button) {
344 progress_data->cancel_button = NULL;
345 g_object_unref(G_OBJECT(cancel_button));
346 gtk_widget_destroy(cancel_button);
347 }
348 }
349
350 /** Implements the can_cancel_progress method of cdebconf frontends.
351 *
352 * @param fe cdebconf frontend
353 * @return TRUE if progress bar can be canceled
354 */
355 bool cdebconf_gtk_can_cancel_progress(struct frontend * fe)
356 {
357 return DCF_CAPB_PROGRESSCANCEL ==
358 (fe->capability & DCF_CAPB_PROGRESSCANCEL);
359 }
360
361 /** Init progress handling.
362 *
363 * This will create the private data structure and the necessary widgets.
364 *
365 * @param fe cdebconf frontend
366 * @return TRUE if initialization was successful, FALSE otherwise
367 */
368 static gboolean init_progress(struct frontend * fe)
369 {
370 struct frontend_data * fe_data = fe->data;
371 struct progress_data * progress_data;
372
373 g_assert(NULL == fe_data->progress_data);
374
375 if (NULL == (progress_data = g_malloc0(sizeof (struct progress_data)))) {
376 g_warning("g_malloc0 failed.");
377 return FALSE;
378 }
379 progress_data->fe = fe;
380 progress_data->fe_title = g_strdup(fe->title);
381 create_progress_box(progress_data);
382 if (CAN_CANCEL_PROGRESS(fe)) {
383 create_cancel_button(progress_data);
384 }
385 fe_data->progress_data = progress_data;
386
387 return TRUE;
388 }
389
390 /** Destroy the progress handling widgets and data structures.
391 *
392 * @param fe cdebconf frontend
393 */
394 static void destroy_progress(struct frontend * fe)
395 {
396 struct frontend_data * fe_data = fe->data;
397 struct progress_data * progress_data = fe_data->progress_data;
398
399 if (NULL == progress_data) {
400 return;
401 }
402 g_free(progress_data->fe_title);
403 fe_data->progress_data = NULL;
404 destroy_cancel_button(progress_data);
405 destroy_progress_box(progress_data);
406 g_free(progress_data);
407 }
408
409 /** Update the progress bar filling and title.
410 *
411 * The title will be taken from the progress_title field of the frontend.
412 *
413 * @param fe cdebconf frontend
414 * @param fraction progress bar filling value
415 */
416 static void update_progress_bar(struct frontend * fe, gdouble fraction)
417 {
418 struct frontend_data * fe_data = fe->data;
419 GtkWidget * progress_bar = fe_data->progress_data->progress_bar;
420 gchar * title_desc;
421
422 g_assert(NULL != progress_bar);
423
424 title_desc = q_get_raw_description(fe->progress_title);
425 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress_bar), title_desc);
426 g_free(title_desc);
427 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar),
428 fraction /* empty progress bar */);
429 }
430
431 /** Implements the progress_start method of cdebconf frontends.
432 *
433 * @param fe cdebconf frontend
434 * @param min minimum value representing the operation in progress
435 * @param max maximum value representing the operation in progress
436 * @param title initial title of the operation in progress
437 */
438 void cdebconf_gtk_progress_start(struct frontend * fe, int min, int max,
439 struct question * title)
440 {
441 struct frontend_data * fe_data = fe->data;
442
443 if (NULL != fe_data->setters) {
444 /* called out of order */
445 return;
446 }
447 if (NULL != fe_data->progress_data) {
448 /* nested progress bar... let's clean up first */
449 cdebconf_gtk_progress_stop(fe);
450 }
451 cdebconf_gtk_set_answer(fe, DC_NO_ANSWER);
452
453 gdk_threads_enter();
454
455 #ifdef DI_UDEB
456 cdebconf_gtk_di_run_dialog(fe);
457 #endif /* DI_UDEB */
458
459 init_progress(fe);
460
461 question_deref(fe->progress_title);
462 fe->progress_title = title;
463 question_ref(fe->progress_title);
464
465 update_progress_bar(fe, 0.0 /* empty */);
466
467 /* XXX: I feel strange to have to set these fields here...
468 * Should them be moved elsewhere? And API of this handler
469 * changed accordingly? */
470 fe->progress_min = min;
471 fe->progress_max = max;
472 fe->progress_cur = min;
473
474 /* XXX: rename progress_box */
475 cdebconf_gtk_show_progress(fe);
476
477 gdk_threads_leave();
478 }
479
480 /** Implements the progress_set methods of cdebconf frontends.
481 *
482 * @param fe cdebconf frontend
483 * @param val current value representing the operation in progress
484 * @return DC_GOBACK if progress was canceled, DC_NOTOK if error happened and
485 * DC_OK otherwise.
486 */
487 int cdebconf_gtk_progress_set(struct frontend * fe, int val)
488 {
489 struct frontend_data * fe_data = fe->data;
490 gdouble progress;
491
492 /* XXX: should this kind of sanity checks be in the frontend? */
493 if (fe->progress_max < val || fe->progress_min > val) {
494 return DC_NOTOK;
495 }
496 if (NULL == fe_data->progress_data) {
497 /* called out of order */
498 return DC_NOTOK;
499 }
500
501 gdk_threads_enter();
502 fe->progress_cur = val;
503 if ((fe->progress_max - fe->progress_min) > 0) {
504 progress = (gdouble) (fe->progress_cur - fe->progress_min) /
505 (gdouble) (fe->progress_max - fe->progress_min);
506 update_progress_bar(fe, progress);
507 }
508 cdebconf_gtk_show_progress(fe);
509 gdk_threads_leave();
510
511 return fe_data->answer;
512 }
513
514 /** Implements the progress_info method of cdebconf frontends.
515 *
516 * This will update the progress label.
517 *
518 * @param fe cdebconf frontend
519 * @param info current information about the operation in progress
520 * @see progress_data#progress_label
521 */
522 int cdebconf_gtk_progress_info(struct frontend * fe, struct question * info)
523 {
524 struct frontend_data * fe_data = fe->data;
525 struct progress_data * progress_data = fe_data->progress_data;
526 gchar * info_desc;
527
528 if (NULL == progress_data) {
529 /* called out of order */
530 return DC_NOTOK;
531 }
532
533 info_desc = q_get_raw_description(info);
534 gdk_threads_enter();
535 gtk_entry_set_text(GTK_ENTRY(progress_data->progress_label), info_desc);
536 gdk_threads_leave();
537 g_free(info_desc);
538
539 if (DC_NO_ANSWER == fe_data->answer) {
540 return DC_OK;
541 }
542 return fe_data->answer;
543 }
544
545 /** Implements the progress_stop method of cdebconf frontends.
546 *
547 * This will destroy the widgets and data structures.
548 *
549 * @param fe cdebconf frontend
550 */
551 void cdebconf_gtk_progress_stop(struct frontend * fe)
552 {
553 struct frontend_data * fe_data = fe->data;
554 struct progress_data * progress_data = fe_data->progress_data;
555
556 if (NULL == progress_data) {
557 /* called out of order */
558 return;
559 }
560
561 gdk_threads_enter();
562 destroy_progress(fe);
563 gdk_threads_leave();
564 }
565
566 /* vim: et sw=4 si
567 */