Added method save_settings to force from plugin code update of easysetup
[modest] / src / widgets / modest-wizard-dialog.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /**
31  * SECTION:modest-wizard-dialog
32  * @short_description: A widget to create a guided installation
33  * process wizard
34  *
35  * #ModestWizardDialog is a widget to create a guided installation
36  * process. The dialog has four standard buttons, previous, next,
37  * finish, cancel, and contains several pages with optional icons.
38  * Response buttons are dimmed/undimmed automatically and the standard
39  * icon is shown/hidden in response to page navigation. The notebook
40  * widget provided by users contains the actual wizard pages.
41  */
42
43 #include <config.h>
44 #include <gtk/gtkdialog.h>
45 #include <gtk/gtknotebook.h>
46 #include <gtk/gtkimage.h>
47 #include <gtk/gtkbox.h>
48 #include <gtk/gtkhbox.h>
49 #include <gtk/gtkvbox.h>
50 #include <gtk/gtkbutton.h>
51 #include <gtk/gtk.h>
52
53 #ifdef HAVE_CONFIG_H
54 #include <config.h>
55 #endif
56
57 #ifndef MODEST_TOOLKIT_GTK
58 #if MODEST_HILDON_API == 0
59 #include <hildon-widgets/hildon-defines.h>
60 #else
61 #include <hildon/hildon-defines.h>
62 #endif /*MODEST_HILDON_API == 0*/
63 #endif /*!MODEST_TOOLKIT_GTK*/
64
65 #include "modest-wizard-dialog.h"
66 #include "modest-debug.h"
67 #include "modest-text-utils.h"
68
69 static GtkDialogClass *parent_class;
70
71 static void class_init              (ModestWizardDialogClass   *wizard_dialog_class);
72
73 static void init                    (ModestWizardDialog        *wizard_dialog);
74
75 static void create_title            (ModestWizardDialog        *wizard_dialog);
76
77 static void set_property            (GObject                   *object,
78                                      guint                     property_id,
79                                      const GValue              *value,
80                                      GParamSpec                *pspec);
81
82 static void get_property            (GObject                   *object,
83                                      guint                     property_id,
84                                      GValue                    *value,
85                                      GParamSpec                *pspec);
86
87 static void finalize                (GObject                   *object);
88
89 static void response                (ModestWizardDialog        *wizard, 
90                                      gint                      response_id,
91                                      gpointer                  unused);
92
93 static void make_buttons_sensitive  (ModestWizardDialog *wizard_dialog,
94                                      gboolean           previous,
95                                      gboolean           finish,
96                                      gboolean next);
97
98 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
99 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
100 static void invoke_save_settings_vfunc (ModestWizardDialog *wizard_dialog);
101
102 enum {
103     PROP_ZERO,
104     PROP_WIZARD_NAME,
105     PROP_WIZARD_NOTEBOOK,
106     PROP_WIZARD_AUTOTITLE
107 };
108
109 struct _ModestWizardDialogPrivate {
110     gchar       *wizard_name;
111     GtkNotebook *notebook;
112     GtkBox      *box;
113     GtkWidget   *image;
114     gboolean    autotitle;
115
116     ModestWizardDialogResponseOverrideFunc override_func;
117 };
118
119
120 GType
121 modest_wizard_dialog_get_type (void)
122 {
123     static GType wizard_dialog_type = 0;
124
125     if (!wizard_dialog_type) {
126
127         static const GTypeInfo wizard_dialog_info = {
128             sizeof (ModestWizardDialogClass),
129             NULL,       /* base_init      */
130             NULL,       /* base_finalize  */
131             (GClassInitFunc) class_init,
132             NULL,       /* class_finalize */
133             NULL,       /* class_data     */
134             sizeof (ModestWizardDialog),
135             0,          /* n_preallocs    */
136             (GInstanceInitFunc) init,
137         };
138
139         wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
140                                                      "ModestWizardDialog",
141                                                      &wizard_dialog_info,
142                                                      0);
143     }
144
145     return wizard_dialog_type;
146 }
147
148 static void
149 class_init (ModestWizardDialogClass *wizard_dialog_class)
150 {
151     GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
152
153     parent_class = g_type_class_peek_parent (wizard_dialog_class);
154
155     g_type_class_add_private (wizard_dialog_class,
156                               sizeof(ModestWizardDialogPrivate));
157
158     /* Override virtual methods */
159     object_class->set_property = set_property;
160     object_class->get_property = get_property;
161     object_class->finalize     = finalize;
162
163     wizard_dialog_class->before_next = NULL;
164     wizard_dialog_class->save_settings = NULL;
165     wizard_dialog_class->enable_buttons = NULL;
166
167     /**
168      * ModestWizardDialog:wizard-name:
169      *
170      * The name of the wizard.
171      */
172     g_object_class_install_property (object_class, PROP_WIZARD_NAME,
173             g_param_spec_string 
174             ("wizard-name",
175              "Wizard Name",
176              "The name of the ModestWizardDialog",
177              NULL,
178              G_PARAM_READWRITE));
179
180     /**
181      * ModestWizardDialog:wizard-notebook:
182      *
183      * The notebook object, which is used by the ModestWizardDialog.
184      */
185     g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
186             g_param_spec_object 
187             ("wizard-notebook",
188              "Wizard Notebook",
189              "GtkNotebook object to be used in the "
190              "ModestWizardDialog",
191              GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
192
193     /**
194      * ModestWizardDialog:autotitle
195      *
196      * If the wizard should automatically try to change the window title when changing steps. 
197      * Set to FALSE if you'd like to override the default behaviour. 
198      *
199      * Since: 0.14.5 
200      */
201     g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
202             g_param_spec_boolean 
203             ("autotitle",
204              "AutoTitle",
205              "If the wizard should autotitle itself",
206              TRUE, 
207              G_PARAM_READWRITE));
208 }
209
210 static void 
211 finalize (GObject *object)
212 {
213     ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
214     g_return_if_fail (dialog != NULL);
215
216     if (dialog->priv->wizard_name != NULL)
217         g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
218     
219     if (G_OBJECT_CLASS (parent_class)->finalize)
220         G_OBJECT_CLASS (parent_class)->finalize(object);
221 }
222
223 /* Disable or enable the Previous, Next and Finish buttons */
224 static void
225 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
226                         gboolean previous,
227                         gboolean finish,
228                         gboolean next)
229 {
230     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
231                                        MODEST_WIZARD_DIALOG_PREVIOUS,
232                                        previous);
233
234     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
235                                        MODEST_WIZARD_DIALOG_FINISH,
236                                        finish);
237
238     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
239                                        MODEST_WIZARD_DIALOG_NEXT,
240                                        next);
241 }
242
243 static void 
244 init (ModestWizardDialog *wizard_dialog)
245 {
246     /* Initialize private structure for faster member access */
247     ModestWizardDialogPrivate *priv =
248         G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
249                 MODEST_TYPE_WIZARD_DIALOG,
250                 ModestWizardDialogPrivate);
251
252     GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
253
254     /* Init internal widgets */
255     GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
256     gtk_dialog_set_has_separator (dialog, FALSE);
257     wizard_dialog->priv = priv;
258     priv->override_func = NULL;
259     priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
260 #ifdef MODEST_TOOLKIT_HILDON2
261     priv->image = NULL;
262 #else
263 #ifdef MODEST_TOOLKIT_GTK
264     priv->image = gtk_image_new_from_stock (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG);
265 #else /*MODEST_TOOLKIT_GTK*/
266 #if MODEST_HILDON_API == 0
267     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
268                                                 HILDON_ICON_SIZE_WIDG_WIZARD);
269 #else       
270     static int icon_size = 0;
271     if (!icon_size)
272             icon_size = gtk_icon_size_register("modest_wizard", 50, 50);
273     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
274                                                 icon_size);
275 #endif /*MODEST_HILDON_API == 0*/
276 #endif /*!MODEST_TOOLKIT_GTK*/
277 #endif /*MODEST_TOOLKIT_HILDON2 */
278     /* Default values for user provided properties */
279     priv->notebook = NULL;
280     priv->wizard_name = NULL;
281     priv->autotitle = TRUE;
282
283     /* Build wizard layout */
284     gtk_box_pack_start_defaults (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box));
285     gtk_box_pack_start_defaults (GTK_BOX (priv->box), GTK_WIDGET (vbox));
286     if (priv->image)
287       gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), FALSE, FALSE, 0);
288
289     /* Add response buttons: finish, previous, next, cancel */
290 #ifdef MODEST_TOOLKIT_HILDON1
291     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
292     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
293     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
294     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
295 #endif
296 #ifdef MODEST_TOOLKIT_HILDON2
297     gtk_dialog_add_button (dialog, _HL("wdgt_bd_finish"), MODEST_WIZARD_DIALOG_FINISH);
298     gtk_dialog_add_button (dialog, _HL("wdgt_bd_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
299     gtk_dialog_add_button (dialog, _HL("wdgt_bd_next"), MODEST_WIZARD_DIALOG_NEXT);
300 #endif
301 #ifdef MODEST_TOOLKIT_GTK
302     gtk_dialog_add_button (dialog, GTK_STOCK_SAVE, MODEST_WIZARD_DIALOG_FINISH);
303     gtk_dialog_add_button (dialog, GTK_STOCK_GO_BACK, MODEST_WIZARD_DIALOG_PREVIOUS);
304     gtk_dialog_add_button (dialog, GTK_STOCK_GO_FORWARD, MODEST_WIZARD_DIALOG_NEXT);
305     gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, MODEST_WIZARD_DIALOG_CANCEL);
306 #endif
307
308     /* Set initial button states: previous and finish buttons are disabled */
309     make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
310
311     /* Show all the internal widgets */
312     gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
313
314     /* connect to dialog's response signal */
315     g_signal_connect (G_OBJECT (dialog), "response",
316             G_CALLBACK (response), NULL);
317
318 }
319
320 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
321 static void on_notebook_page_added(GtkNotebook *notebook, 
322                                    GtkWidget   *child,
323                                    guint        page_num,
324                                    gpointer     user_data)
325 {
326         ModestWizardDialog* dialog = NULL;
327
328         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
329         dialog = MODEST_WIZARD_DIALOG(user_data);
330
331         /* The title should show the total number of pages: */
332         create_title (dialog);
333 }
334
335 static void on_notebook_page_removed(GtkNotebook *notebook, 
336                                      GtkWidget   *child,
337                                      guint        page_num,
338                                      gpointer     user_data)
339 {
340         ModestWizardDialog* dialog = NULL;
341
342         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
343         dialog = MODEST_WIZARD_DIALOG(user_data);
344
345         /* The title should show the total number of pages: */
346         create_title (dialog);
347 }
348 #endif /* GTK_CHECK_VERSION */
349
350 static void
351 on_notebook_switch_page (GtkNotebook *notebook,
352                          GtkNotebookPage *page,
353                          guint page_num,
354                          ModestWizardDialog *self)
355 {
356         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(self));
357
358         create_title (self);
359 }
360
361 static void
362 connect_to_notebook_signals(ModestWizardDialog* dialog)
363 {
364 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
365         ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
366         g_return_if_fail (priv->notebook);
367         
368         /* Connect to the notebook signals,
369          * so we can update the title when necessary: */
370         g_signal_connect (G_OBJECT (priv->notebook), "page-added",
371                       G_CALLBACK (on_notebook_page_added), dialog);
372         g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
373                       G_CALLBACK (on_notebook_page_removed), dialog);
374 #endif /* GTK_CHECK_VERSION */
375         g_signal_connect_after (G_OBJECT (priv->notebook), "switch-page",
376                                 G_CALLBACK (on_notebook_switch_page), dialog);
377 }
378
379
380 static void
381 set_property (GObject      *object, 
382               guint        property_id,
383               const GValue *value, 
384               GParamSpec   *pspec)
385 {
386     ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
387
388     switch (property_id) {
389
390         case PROP_WIZARD_AUTOTITLE:
391
392             priv->autotitle = g_value_get_boolean (value);
393
394             if (priv->autotitle && 
395                 priv->wizard_name && 
396                 priv->notebook)
397                 create_title (MODEST_WIZARD_DIALOG (object));
398             else if (priv->wizard_name)
399                 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
400             
401             break;
402
403         case PROP_WIZARD_NAME: 
404
405             /* Set new wizard name. This name will appear in titlebar */
406             if (priv->wizard_name)
407                 g_free (priv->wizard_name);
408
409             gchar *str = (gchar *) g_value_get_string (value);
410             g_return_if_fail (str != NULL);
411
412             priv->wizard_name = g_strdup (str);
413
414             /* We need notebook in order to create title, since page information
415                is used in title generation */
416             
417             if (priv->notebook && priv->autotitle)
418                 create_title (MODEST_WIZARD_DIALOG (object));
419     
420             break;
421
422         case PROP_WIZARD_NOTEBOOK: {
423
424             GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
425             g_return_if_fail (book != NULL);
426
427             priv->notebook = book;
428
429             /* Set the default properties for the notebook (disable tabs,
430              * and remove borders) to make it look like a nice wizard widget */
431             gtk_notebook_set_show_tabs (priv->notebook, FALSE);
432             gtk_notebook_set_show_border (priv->notebook, FALSE);
433             gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
434
435             /* Show the notebook so that a gtk_widget_show on the dialog is
436              * all that is required to display the dialog correctly */
437             gtk_widget_show ( GTK_WIDGET (priv->notebook));
438
439             /* Update dialog title to reflect current page stats etc */ 
440             ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);      
441             if (priv->wizard_name && priv->autotitle)
442                 create_title (wizard_dialog);
443                 
444             connect_to_notebook_signals (wizard_dialog);
445             
446             }break;
447
448         default:
449             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
450             break;
451     }
452 }
453
454 static void
455 get_property (GObject      *object,
456               guint        property_id,
457               GValue       *value,
458               GParamSpec   *pspec)
459 {
460     ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
461
462     switch (property_id) {
463
464         case PROP_WIZARD_NAME:
465             g_value_set_string (value, priv->wizard_name);
466             break;
467
468         case PROP_WIZARD_NOTEBOOK:
469             g_value_set_object (value, priv->notebook);
470             break;
471
472         default:
473             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
474             break;
475     }
476 }
477
478 /*
479  * Creates the title of the dialog taking into account the current 
480  * page of the notebook.
481  */
482 static void
483 create_title (ModestWizardDialog *wizard_dialog)
484 {
485     gchar *str = NULL;
486     ModestWizardDialogPrivate *priv = NULL;
487     GtkNotebook *notebook = NULL;
488
489     g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
490     g_return_if_fail (wizard_dialog->priv != NULL);
491
492     priv = wizard_dialog->priv;    
493     notebook = priv->notebook;
494
495     if (!notebook)
496         return;
497
498     /* Get page information, we'll need that when creating title */
499     gint pages = gtk_notebook_get_n_pages (notebook);
500     if (pages == 0)
501       return;
502         
503     gint current = gtk_notebook_get_current_page (priv->notebook);
504     if (current < 0)
505         current = 0;
506
507     /* the welcome title on the initial page */
508     /* This is the standard wizard title, with, e.g., 1/4 at the end,
509          * but the Modest UI spec does not want this. */
510         /*
511     if (current == 0) {
512         str = g_strdup_printf (_HL("ecdg_ti_wizard_welcome"), 
513                 priv->wizard_name, pages);
514     } else {
515     */
516         const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
517                 gtk_notebook_get_nth_page (notebook, current));
518                 
519                 /* This is the standard wizard title, with, e.g., 1/4 at the end,
520                  * but the Modest UI spec does not want this.
521                  */
522                 /*
523         str = g_strdup_printf (_HL("ecdg_ti_wizard_step"), 
524                 priv->wizard_name, current + 1, pages, steps);
525         */
526
527         str = g_strdup_printf ((steps&&*steps)?_HL("%s: %s"):_HL("%s"), 
528                 priv->wizard_name, steps);
529     /* } */
530
531     /* Update the dialog to display the generated title */
532     gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
533     g_free (str);
534 }
535
536 /*
537  * Response signal handler. This function is needed because GtkDialog's 
538  * handler for this signal closes the dialog and we don't want that, we 
539  * want to change pages and, dim certain response buttons. Overriding the 
540  * virtual function would not work because that would be called after the 
541  * signal handler implemented by GtkDialog.
542  * FIXME: There is a much saner way to do that [MDK]
543  */
544 static void 
545 response (ModestWizardDialog   *wizard_dialog,
546           gint                 response_id,
547           gpointer             unused)
548 {
549     ModestWizardDialogPrivate *priv = wizard_dialog->priv;
550     GtkNotebook *notebook = priv->notebook;
551     gint current = 0;
552     gboolean is_first, is_last;
553
554     if (priv->override_func) {
555             if (priv->override_func (wizard_dialog, response_id, gtk_notebook_get_current_page (notebook))) {
556                     /* Don't let the dialog close */
557                     g_signal_stop_emission_by_name (wizard_dialog, "response");
558                     
559                     /* Force refresh of title */
560                     if (priv->autotitle) 
561                             create_title (wizard_dialog);
562                     return;
563             }
564     }
565     
566     switch (response_id) {
567         
568         case MODEST_WIZARD_DIALOG_PREVIOUS:
569             gtk_notebook_prev_page (notebook); /* go to previous page */
570             break;
571
572         case MODEST_WIZARD_DIALOG_NEXT:
573                 if (invoke_before_next_vfunc (wizard_dialog))
574                         gtk_notebook_next_page (notebook); /* go to next page */
575                 
576             break;
577
578         case MODEST_WIZARD_DIALOG_CANCEL:
579                 return;
580                 break;      
581         case MODEST_WIZARD_DIALOG_FINISH:
582                 if (invoke_before_next_vfunc (wizard_dialog))
583                 return;
584             
585             break;
586
587     }
588
589     current = gtk_notebook_get_current_page (notebook);
590     gint last = gtk_notebook_get_n_pages (notebook) - 1;
591     is_last = current == last;
592     is_first = current == 0;
593
594     /* If first page, previous and finish are disabled, 
595        if last page, next is disabled */
596     make_buttons_sensitive (wizard_dialog,
597                             (is_first) ? FALSE : TRUE,
598                             TRUE,
599                             (is_last) ? FALSE : TRUE);
600
601     /* Allow derived classes to disable buttons to prevent navigation,
602      * according to their own validation logic: */
603     invoke_enable_buttons_vfunc (wizard_dialog);
604
605     /* Don't let the dialog close */
606     g_signal_stop_emission_by_name (wizard_dialog, "response");
607
608     /* We show the default image on first and last pages */
609     last = gtk_notebook_get_n_pages (notebook) - 1;
610     if (priv->image) {
611             if (current == last || current == 0)
612                     gtk_widget_show (GTK_WIDGET(priv->image));
613             else
614                     gtk_widget_hide (GTK_WIDGET(priv->image));
615     }
616
617     /* New page number may appear in the title, update it */
618     if (priv->autotitle) 
619         create_title (wizard_dialog);
620 }
621
622 /**
623  * modest_wizard_dialog_new:
624  * @parent: a #GtkWindow
625  * @wizard_name: the name of dialog
626  * @notebook: the notebook to be shown on the dialog
627  *
628  * Creates a new #ModestWizardDialog.
629  *
630  * Returns: a new #ModestWizardDialog
631  */
632 GtkWidget*
633 modest_wizard_dialog_new (GtkWindow   *parent,
634                           const char  *wizard_name,
635                           GtkNotebook *notebook)
636 {
637     GtkWidget *widget;
638
639     g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
640
641     widget = GTK_WIDGET (g_object_new
642             (MODEST_TYPE_WIZARD_DIALOG,
643              "wizard-name", wizard_name,
644              "wizard-notebook", notebook, NULL));
645
646     if (parent)
647         gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
648
649     return widget;
650 }
651
652 /**
653  * modest_wizard_dialog_force_title_update:
654  * @wizard_dialog: The wizard dialog
655  *
656  * Force the title to be rebuilt, for instance when you have added or 
657  * removed notebook pages. This function is not necessary when using GTK+ 2.10, 
658  * because that has GtkNotebook signals that will be used to update the title 
659  * automatically.
660  */
661 void
662 modest_wizard_dialog_force_title_update (ModestWizardDialog   *wizard_dialog)
663 {
664         create_title (wizard_dialog);
665 }
666
667 static gboolean
668 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
669 {
670         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
671         
672         /* Call the vfunc, which may be overridden by derived classes: */
673         if (klass->before_next) {
674                 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
675         
676                 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
677                 
678                 /* Get widgets for the two pages: */
679                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
680                 
681                 GtkWidget* next_page_widget = NULL;
682                 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
683                         next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
684
685                 MODEST_DEBUG_BLOCK (
686                 g_debug ("Switching to page %d (%s)",
687                          gtk_notebook_page_num (priv->notebook, next_page_widget),
688                          gtk_notebook_get_tab_label_text (priv->notebook, next_page_widget));
689
690                 {
691                         GtkWidget *p;
692                         gint i;
693                         g_debug ("\t***************");
694                         for (i=0; i<gtk_notebook_get_n_pages(priv->notebook);i++) {
695                                 p = gtk_notebook_get_nth_page (priv->notebook, i);
696                                 g_debug ("\t%d - %s", i, gtk_notebook_get_tab_label_text (priv->notebook, p));
697                         }
698                         g_debug ("\t***************");
699                 }
700                                     );
701                 
702                 /* Ask the vfunc implementation whether navigation should be allowed: */
703                 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
704         }
705         
706         /* Allow navigation by default if there is no vfunc implementation: */
707         return TRUE;
708 }
709
710 static void
711 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
712 {
713         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
714         
715         /* Call the vfunc, which may be overridden by derived classes: */
716         if (klass->enable_buttons) {
717                 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
718         
719                 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
720                 
721                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
722                         
723                 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);
724         }
725 }
726
727 static void
728 invoke_save_settings_vfunc (ModestWizardDialog *wizard_dialog)
729 {
730         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
731         
732         /* Call the vfunc, which may be overridden by derived classes: */
733         if (klass->save_settings) {
734                 (*(klass->save_settings)) (wizard_dialog);
735         }
736 }
737
738 void 
739 modest_wizard_dialog_set_response_override_handler (ModestWizardDialog *wizard_dialog,
740                                                     ModestWizardDialogResponseOverrideFunc callback)
741 {
742     ModestWizardDialogPrivate *priv = wizard_dialog->priv;
743
744     priv->override_func = callback;
745 }
746
747 void
748 modest_wizard_dialog_save_settings (ModestWizardDialog *wizard_dialog)
749 {
750         g_return_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog));
751
752         invoke_save_settings_vfunc (wizard_dialog);
753 }