* Remove unused comments and checks.
[modest] / src / maemo / easysetup / modest-wizard-dialog.c
1 /*
2  * This is a copy of modest-wizard-dialog.h with a rename and some API additions,
3  * for osso-modest-easysetup.
4  * 
5  * This file was part of modest-libs
6  *
7  * Copyright (C) 2005, 2006, 2007 Nokia Corporation, all rights reserved.
8  *
9  */
10  
11 /**
12  * SECTION:modest-wizard-dialog
13  * @short_description: A widget to create a guided installation
14  * process wizard
15  *
16  * #ModestWizardDialog is a widget to create a guided installation
17  * process. The dialog has four standard buttons, previous, next,
18  * finish, cancel, and contains several pages with optional icons.
19  * Response buttons are dimmed/undimmed automatically and the standard
20  * icon is shown/hidden in response to page navigation. The notebook
21  * widget provided by users contains the actual wizard pages.
22  */
23
24 #include <config.h>
25 #include <gtk/gtkdialog.h>
26 #include <gtk/gtknotebook.h>
27 #include <gtk/gtkimage.h>
28 #include <gtk/gtkbox.h>
29 #include <gtk/gtkhbox.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtkbutton.h>
32 #include <gtk/gtk.h>
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef MODEST_HILDON_VERSION_0
39 #include <hildon-widgets/hildon-defines.h>
40 #else
41 #include <hildon/hildon-defines.h>
42 #endif /*MODEST_HILDON_VERSION_0*/
43
44 #include "modest-wizard-dialog.h"
45
46 #include <libintl.h>
47
48 /* Specify the hildon-libs translation domain,
49  * so we can reuse its translations 
50  * instead of repeating them in our own translations.
51  */
52 /* #define _(String) dgettext(PACKAGE, String) */
53
54 #define _(String) dgettext("hildon-libs", String)
55
56 static GtkDialogClass *parent_class;
57
58 static void class_init              (ModestWizardDialogClass   *wizard_dialog_class);
59
60 static void init                    (ModestWizardDialog        *wizard_dialog);
61
62 static void create_title            (ModestWizardDialog        *wizard_dialog);
63
64 static void set_property            (GObject                   *object,
65                                      guint                     property_id,
66                                      const GValue              *value,
67                                      GParamSpec                *pspec);
68
69 static void get_property            (GObject                   *object,
70                                      guint                     property_id,
71                                      GValue                    *value,
72                                      GParamSpec                *pspec);
73
74 static void finalize                (GObject                   *object);
75
76 static void response                (ModestWizardDialog        *wizard, 
77                                      gint                      response_id,
78                                      gpointer                  unused);
79
80 static void make_buttons_sensitive  (ModestWizardDialog *wizard_dialog,
81                                      gboolean           previous,
82                                      gboolean           finish,
83                                      gboolean next);
84                                      
85 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
86 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
87
88 enum {
89     PROP_ZERO,
90     PROP_WIZARD_NAME,
91     PROP_WIZARD_NOTEBOOK,
92     PROP_WIZARD_AUTOTITLE
93 };
94
95 struct _ModestWizardDialogPrivate {
96     gchar       *wizard_name;
97     GtkNotebook *notebook;
98     GtkBox      *box;
99     GtkWidget   *image;
100     gboolean    autotitle;
101 };
102
103
104 GType
105 modest_wizard_dialog_get_type (void)
106 {
107     static GType wizard_dialog_type = 0;
108
109     if (!wizard_dialog_type) {
110
111         static const GTypeInfo wizard_dialog_info = {
112             sizeof (ModestWizardDialogClass),
113             NULL,       /* base_init      */
114             NULL,       /* base_finalize  */
115             (GClassInitFunc) class_init,
116             NULL,       /* class_finalize */
117             NULL,       /* class_data     */
118             sizeof (ModestWizardDialog),
119             0,          /* n_preallocs    */
120             (GInstanceInitFunc) init,
121         };
122
123         wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
124                                                      "ModestWizardDialog",
125                                                      &wizard_dialog_info,
126                                                      0);
127     }
128
129     return wizard_dialog_type;
130 }
131
132 static void
133 class_init (ModestWizardDialogClass *wizard_dialog_class)
134 {
135     GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
136
137     parent_class = g_type_class_peek_parent (wizard_dialog_class);
138
139     g_type_class_add_private (wizard_dialog_class,
140                               sizeof(ModestWizardDialogPrivate));
141
142     /* Override virtual methods */
143     object_class->set_property = set_property;
144     object_class->get_property = get_property;
145     object_class->finalize     = finalize;
146
147     /**
148      * ModestWizardDialog:wizard-name:
149      *
150      * The name of the wizard.
151      */
152     g_object_class_install_property (object_class, PROP_WIZARD_NAME,
153             g_param_spec_string 
154             ("wizard-name",
155              "Wizard Name",
156              "The name of the ModestWizardDialog",
157              NULL,
158              G_PARAM_READWRITE));
159
160     /**
161      * ModestWizardDialog:wizard-notebook:
162      *
163      * The notebook object, which is used by the ModestWizardDialog.
164      */
165     g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
166             g_param_spec_object 
167             ("wizard-notebook",
168              "Wizard Notebook",
169              "GtkNotebook object to be used in the "
170              "ModestWizardDialog",
171              GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
172
173     /**
174      * ModestWizardDialog:autotitle
175      *
176      * If the wizard should automatically try to change the window title when changing steps. 
177      * Set to FALSE if you'd like to override the default behaviour. 
178      *
179      * Since: 0.14.5 
180      */
181     g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
182             g_param_spec_boolean 
183             ("autotitle",
184              "AutoTitle",
185              "If the wizard should autotitle itself",
186              TRUE, 
187              G_PARAM_READWRITE));
188 }
189
190 static void 
191 finalize (GObject *object)
192 {
193     ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
194     g_return_if_fail (dialog != NULL);
195
196     if (dialog->priv->wizard_name != NULL)
197         g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
198     
199     if (G_OBJECT_CLASS (parent_class)->finalize)
200         G_OBJECT_CLASS (parent_class)->finalize(object);
201 }
202
203 /* Disable or enable the Previous, Next and Finish buttons */
204 static void
205 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
206                         gboolean previous,
207                         gboolean finish,
208                         gboolean next)
209 {
210     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
211                                        MODEST_WIZARD_DIALOG_PREVIOUS,
212                                        previous);
213
214     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
215                                        MODEST_WIZARD_DIALOG_FINISH,
216                                        finish);
217
218     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
219                                        MODEST_WIZARD_DIALOG_NEXT,
220                                        next);
221 }
222
223 static void 
224 init (ModestWizardDialog *wizard_dialog)
225 {
226     /* Initialize private structure for faster member access */
227     ModestWizardDialogPrivate *priv =
228         G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
229                 MODEST_TYPE_WIZARD_DIALOG,
230                 ModestWizardDialogPrivate);
231
232     GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
233
234     /* Init internal widgets */
235     GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
236     gtk_dialog_set_has_separator (dialog, FALSE);
237     wizard_dialog->priv = priv;
238     priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
239 #ifdef MODEST_HILDON_VERSION_0
240     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
241                                                 HILDON_ICON_SIZE_WIDG_WIZARD);
242 #else
243     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
244                                                 HILDON_ICON_SIZE_WIZARD);
245 #endif /*MODEST_HILDON_VERSION_0*/
246     /* Default values for user provided properties */
247     priv->notebook = NULL;
248     priv->wizard_name = NULL;
249     priv->autotitle = TRUE;
250
251     /* Build wizard layout */
252     gtk_box_pack_start_defaults (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box));
253     gtk_box_pack_start_defaults (GTK_BOX (priv->box), GTK_WIDGET (vbox));
254     gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), FALSE, FALSE, 0);
255
256     /* Add response buttons: finish, previous, next, cancel */
257     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
258     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
259     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
260     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
261
262     /* Set initial button states: previous and finish buttons are disabled */
263     make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
264
265     /* Show all the internal widgets */
266     gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
267
268     /* connect to dialog's response signal */
269     g_signal_connect (G_OBJECT (dialog), "response",
270             G_CALLBACK (response), NULL);
271 }
272
273 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
274 static void on_notebook_page_added(GtkNotebook *notebook, 
275                                    GtkWidget   *child,
276                                    guint        page_num,
277                                    gpointer     user_data)
278 {
279         ModestWizardDialog* dialog = NULL;
280
281         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
282         dialog = MODEST_WIZARD_DIALOG(user_data);
283
284         /* The title should show the total number of pages: */
285         create_title (dialog);
286 }
287
288 static void on_notebook_page_removed(GtkNotebook *notebook, 
289                                      GtkWidget   *child,
290                                      guint        page_num,
291                                      gpointer     user_data)
292 {
293         ModestWizardDialog* dialog = NULL;
294
295         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
296         dialog = MODEST_WIZARD_DIALOG(user_data);
297
298         /* The title should show the total number of pages: */
299         create_title (dialog);
300 }
301 #endif /* GTK_CHECK_VERSION */
302
303 static void
304 connect_to_notebook_signals(ModestWizardDialog* dialog)
305 {
306 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
307         ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
308         g_return_if_fail (priv->notebook);
309         
310         /* Connect to the notebook signals,
311          * so we can update the title when necessary: */
312         g_signal_connect (G_OBJECT (priv->notebook), "page-added",
313                       G_CALLBACK (on_notebook_page_added), dialog);
314         g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
315                       G_CALLBACK (on_notebook_page_removed), dialog);
316 #endif /* GTK_CHECK_VERSION */
317 }
318
319
320 static void
321 set_property (GObject      *object, 
322               guint        property_id,
323               const GValue *value, 
324               GParamSpec   *pspec)
325 {
326     ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
327
328     switch (property_id) {
329
330         case PROP_WIZARD_AUTOTITLE:
331
332             priv->autotitle = g_value_get_boolean (value);
333
334             if (priv->autotitle && 
335                 priv->wizard_name && 
336                 priv->notebook)
337                 create_title (MODEST_WIZARD_DIALOG (object));
338             else if (priv->wizard_name)
339                 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
340             
341             break;
342
343         case PROP_WIZARD_NAME: 
344
345             /* Set new wizard name. This name will appear in titlebar */
346             if (priv->wizard_name)
347                 g_free (priv->wizard_name);
348
349             gchar *str = (gchar *) g_value_get_string (value);
350             g_return_if_fail (str != NULL);
351
352             priv->wizard_name = g_strdup (str);
353
354             /* We need notebook in order to create title, since page information
355                is used in title generation */
356             
357             if (priv->notebook && priv->autotitle)
358                 create_title (MODEST_WIZARD_DIALOG (object));
359     
360             break;
361
362         case PROP_WIZARD_NOTEBOOK: {
363
364             GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
365             g_return_if_fail (book != NULL);
366
367             priv->notebook = book;
368
369             /* Set the default properties for the notebook (disable tabs,
370              * and remove borders) to make it look like a nice wizard widget */
371             gtk_notebook_set_show_tabs (priv->notebook, FALSE);
372             gtk_notebook_set_show_border (priv->notebook, FALSE);
373             gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
374
375             /* Show the notebook so that a gtk_widget_show on the dialog is
376              * all that is required to display the dialog correctly */
377             gtk_widget_show ( GTK_WIDGET (priv->notebook));
378
379             /* Update dialog title to reflect current page stats etc */ 
380             ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);      
381             if (priv->wizard_name && priv->autotitle)
382                 create_title (wizard_dialog);
383                 
384             connect_to_notebook_signals (wizard_dialog);
385             
386             }break;
387
388         default:
389             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
390             break;
391     }
392 }
393
394 static void
395 get_property (GObject      *object,
396               guint        property_id,
397               GValue       *value,
398               GParamSpec   *pspec)
399 {
400     ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
401
402     switch (property_id) {
403
404         case PROP_WIZARD_NAME:
405             g_value_set_string (value, priv->wizard_name);
406             break;
407
408         case PROP_WIZARD_NOTEBOOK:
409             g_value_set_object (value, priv->notebook);
410             break;
411
412         default:
413             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
414             break;
415     }
416 }
417
418 /*
419  * Creates the title of the dialog taking into account the current 
420  * page of the notebook.
421  */
422 static void
423 create_title (ModestWizardDialog *wizard_dialog)
424 {
425     gint pages, current;
426     gchar *str = NULL;
427     ModestWizardDialogPrivate *priv = NULL;
428     GtkNotebook *notebook = NULL;
429
430     g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
431     g_return_if_fail (wizard_dialog->priv != NULL);
432
433     priv = wizard_dialog->priv;    
434     notebook = priv->notebook;
435
436     if (!notebook)
437         return;
438
439     /* Get page information, we'll need that when creating title */
440     pages = gtk_notebook_get_n_pages (notebook);
441     current = gtk_notebook_get_current_page (priv->notebook);
442     if (current < 0)
443         current = 0;
444
445     /* the welcome title on the initial page */
446     if (current == 0) {
447         str = g_strdup_printf (_("ecdg_ti_wizard_welcome"), 
448                 priv->wizard_name, pages);
449     } else {
450         const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
451                 gtk_notebook_get_nth_page (notebook, current));
452
453         str = g_strdup_printf (_("ecdg_ti_wizard_step"), 
454                 priv->wizard_name, current + 1, pages, steps);
455     }
456
457     /* Update the dialog to display the generated title */
458     gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
459     g_free (str);
460 }
461
462 /*
463  * Response signal handler. This function is needed because GtkDialog's 
464  * handler for this signal closes the dialog and we don't want that, we 
465  * want to change pages and, dimm certain response buttons. Overriding the 
466  * virtual function would not work because that would be called after the 
467  * signal handler implemented by GtkDialog.
468  * FIXME: There is a much saner way to do that [MDK]
469  */
470 static void 
471 response (ModestWizardDialog   *wizard_dialog,
472           gint                 response_id,
473           gpointer             unused)
474 {
475     ModestWizardDialogPrivate *priv = wizard_dialog->priv;
476     GtkNotebook *notebook = priv->notebook;
477     gint current = 0;
478     gboolean is_first, is_last;
479     
480     switch (response_id) {
481         
482         case MODEST_WIZARD_DIALOG_PREVIOUS:
483             gtk_notebook_prev_page (notebook); /* go to previous page */
484             break;
485
486         case MODEST_WIZARD_DIALOG_NEXT:
487                 if (invoke_before_next_vfunc (wizard_dialog))
488                 gtk_notebook_next_page (notebook); /* go to next page */
489                 
490             break;
491
492         case MODEST_WIZARD_DIALOG_CANCEL:
493                 return;
494                 break;      
495         case MODEST_WIZARD_DIALOG_FINISH:
496                 if (invoke_before_next_vfunc (wizard_dialog))
497                 return;
498             
499             break;
500
501     }
502
503     current = gtk_notebook_get_current_page (notebook);
504     gint last = gtk_notebook_get_n_pages (notebook) - 1;
505     is_last = current == last;
506     is_first = current == 0;
507     
508     /* If first page, previous and finish are disabled, 
509        if last page, next is disabled */
510     make_buttons_sensitive (wizard_dialog,
511             !is_first /* previous */, !is_first /* finish */, !is_last /* next*/);
512             
513     /* Allow derived classes to disable buttons to prevent navigation,
514      * according to their own validation logic: */
515     invoke_enable_buttons_vfunc (wizard_dialog);
516     
517     /* Don't let the dialog close */
518     g_signal_stop_emission_by_name (wizard_dialog, "response");
519
520     /* We show the default image on first and last pages */
521     last = gtk_notebook_get_n_pages (notebook) - 1;
522     if (current == last || current == 0)
523         gtk_widget_show (GTK_WIDGET(priv->image));
524     else
525         gtk_widget_hide (GTK_WIDGET(priv->image));
526
527     /* New page number may appear in the title, update it */
528     if (priv->autotitle) 
529         create_title (wizard_dialog);
530 }
531
532 /**
533  * modest_wizard_dialog_new:
534  * @parent: a #GtkWindow
535  * @wizard_name: the name of dialog
536  * @notebook: the notebook to be shown on the dialog
537  *
538  * Creates a new #ModestWizardDialog.
539  *
540  * Returns: a new #ModestWizardDialog
541  */
542 GtkWidget*
543 modest_wizard_dialog_new (GtkWindow   *parent,
544                           const char  *wizard_name,
545                           GtkNotebook *notebook)
546 {
547     GtkWidget *widget;
548
549     g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
550
551     widget = GTK_WIDGET (g_object_new
552             (MODEST_TYPE_WIZARD_DIALOG,
553              "wizard-name", wizard_name,
554              "wizard-notebook", notebook, NULL));
555
556     if (parent)
557         gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
558
559     return widget;
560 }
561
562 /**
563  * modest_wizard_dialog_force_title_update:
564  * @wizard_dialog: The wizard dialog
565  *
566  * Force the title to be rebuilt, for instance when you have added or 
567  * removed notebook pages. This function is not necessary when using GTK+ 2.10, 
568  * because that has GtkNotebook signals that will be used to update the title 
569  * automatically.
570  */
571 void
572 modest_wizard_dialog_force_title_update (ModestWizardDialog   *wizard_dialog)
573 {
574         create_title (wizard_dialog);
575 }
576
577 static gboolean
578 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
579 {
580         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
581         
582         /* Call the vfunc, which may be overridden by derived classes: */
583         if (klass->before_next) {
584                 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
585         
586                 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
587                 
588                 /* Get widgets for the two pages: */
589                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
590                 
591                 GtkWidget* next_page_widget = NULL;
592                 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
593                         next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
594                 
595                 /* Ask the vfunc implementation whether navigation should be allowed: */
596                 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
597         }
598         
599         /* Allow navigation by default if there is no vfunc implementation: */
600         return TRUE;
601 }
602
603 static void
604 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
605 {
606         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
607         
608         /* Call the vfunc, which may be overridden by derived classes: */
609         if (klass->enable_buttons) {
610                 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
611         
612                 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
613                 
614                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
615                         
616                 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);
617         }
618 }