d051a8190fe34b7704c4575cb4f0d65459d80f3a
[hildon] / src / hildon-set-password-dialog.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-set-password-dialog
27  * @short_description: A dialog used to set, change or remove a password.
28  * @see_also: #HildonGetPasswordDialog
29  *
30  * HildonSetPasswordDialog allows setting and changing a password. 
31  * 
32  * In Change mode: Dialog is used to change or remove an existing
33  * password. Unselecting the check box dims the password fields below
34  * it. If the dialog is accepted with 'OK' while the check box is
35  * unselected, a Confirmation Note is shown.  If the Confirmation Note
36  * Dialog is accepted with 'Remove', the password protection is removed.  
37  * 
38  * In Set mode: Set Password Dialog is used to define a password, or
39  * change a password that cannot be removed.
40  */
41
42 #undef                                          HILDON_DISABLE_DEPRECATED
43
44 #ifdef                                          HAVE_CONFIG_H
45 #include                                        <config.h>
46 #endif
47
48 #include                                        <errno.h>
49 #include                                        <string.h>
50 #include                                        <strings.h>
51 #include                                        <unistd.h>
52 #include                                        <stdio.h>
53 #include                                        <libintl.h>
54 #include                                        <glib.h>
55 #include                                        <gdk/gdkkeysyms.h>
56
57 #include                                        "hildon-set-password-dialog.h"
58 #include                                        "hildon-caption.h"
59 #include                                        "hildon-note.h"
60 #include                                        "hildon-defines.h"
61 #include                                        "hildon-banner.h"
62 #include                                        "hildon-set-password-dialog-private.h"
63
64 #define                                         HILDON_SET_PASSWORD_DIALOG_TITLE "ecdg_ti_set_password"
65
66 #define                                         HILDON_SET_PASSWORD_DIALOG_PASSWORD "ecdg_fi_set_passwd_enter_pwd"
67
68 #define                                         HILDON_SET_PASSWORD_DIALOG_VERIFY_PASSWORD "ecdg_fi_set_passwd_confirm"
69
70 #define                                         HILDON_SET_PASSWORD_DIALOG_OK "wdgt_bd_done"
71
72 #define                                         HILDON_SET_PASSWORD_DIALOG_CANCEL "ecdg_bd_set_password_dialog_cancel"
73
74 #define                                         HILDON_SET_MODIFY_PASSWORD_DIALOG_TITLE "ckdg_ti_dialog_c_passwd_change_password"
75
76 #define                                         HILDON_SET_MODIFY_PASSWORD_DIALOG_LABEL "ckdg_fi_dialog_c_passwd_pwd_protect"
77
78 #define                                         HILDON_SET_MODIFY_PASSWORD_DIALOG_PASSWORD "ckdg_fi_dialog_c_passwd_new_pwd"
79
80 #define                                         HILDON_SET_MODIFY_PASSWORD_DIALOG_VERIFY_PASSWORD "ckdg_fi_dialog_c_passwd_ver_pwd"
81
82 #define                                         HILDON_SET_MODIFY_PASSWORD_DIALOG_OK "ckdg_bd_change_password_dialog_ok"
83
84 #define                                         HILDON_SET_MODIFY_PASSWORD_DIALOG_CANCEL "ckdg_bd_change_password_dialog_cancel"
85
86 #define                                         HILDON_SET_PASSWORD_DIALOG_MISMATCH "ecdg_ib_passwords_do_not_match"
87
88 #define                                         HILDON_SET_PASSWORD_DIALOG_EMPTY "ecdg_ib_password_is_empty"
89
90 #define                                         HILDON_SET_PASSWORD_DIALOG_REMOVE_PROTECTION "ckdg_nc_dialog_c_passwd_remove_pwd"
91
92 #define                                         HILDON_REMOVE_PROTECTION_CONFIRMATION_REMOVE "ckdg_bd_dialog_c_passwd_remove_button"
93
94 #define                                         HILDON_REMOVE_PROTECTION_CONFIRMATION_CANCEL "ckdg_bd_dialog_c_passwd_cancel_button"
95
96 #define                                         _(String) dgettext("hildon-libs", String)
97
98 #define                                         c_(String) dgettext("hildon-common-strings", String)
99
100 static GtkDialogClass*                          parent_class;
101
102 static void
103 hildon_set_password_dialog_class_init           (HildonSetPasswordDialogClass *class);
104
105 static void 
106 hildon_set_password_dialog_init                 (HildonSetPasswordDialog *dialog);
107
108 static void 
109 hildon_checkbox_toggled                         (GtkWidget *widget, 
110                                                  gpointer dialog);
111
112 static void
113 hildon_set_password_response_change             (GtkDialog *d,
114                                                  gint arg1, 
115                                                  gpointer unused);
116
117 static void
118 hildon_set_password_response_set                (GtkDialog * d, 
119                                                  gint arg1, 
120                                                  gpointer unused);
121
122 static void 
123 create_contents                                 (HildonSetPasswordDialog *dialog);
124
125 static void
126 hildon_set_password_set_property                (GObject *object,
127                                                  guint prop_id,
128                                                  const GValue *value,
129                                                  GParamSpec *pspec);
130
131 static void
132 hildon_set_password_get_property                (GObject *object,
133                                                  guint prop_id, 
134                                                  GValue *value,
135                                                  GParamSpec *pspec);
136
137 enum 
138 {
139     PROP_0,
140     PROP_MESSAGE,
141     PROP_PASSWORD,
142     PROP_HILDON_PASSWORD_DIALOG
143 };
144
145 static void
146 hildon_set_password_set_property                (GObject *object,
147                                                  guint prop_id,
148                                                  const GValue *value, 
149                                                  GParamSpec *pspec)
150 {
151     HildonSetPasswordDialog *dialog = HILDON_SET_PASSWORD_DIALOG (object);
152     HildonSetPasswordDialogPrivate *priv;
153
154     priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
155     g_assert (priv);
156
157     switch (prop_id) {
158
159         case PROP_MESSAGE:
160             /* Update domain label to display new value */
161             gtk_label_set_text (priv->message_label, g_value_get_string (value));
162             break;
163
164         case PROP_PASSWORD:
165             /* Update password entry to display new value */
166             gtk_entry_set_text (GTK_ENTRY (priv->pwd1st_entry), g_value_get_string (value));
167             break;
168
169         case PROP_HILDON_PASSWORD_DIALOG:
170             /* Note this is a G_PARAM_CONSTRUCT_ONLY type property */
171             priv->protection = g_value_get_boolean (value);
172
173             /* We now have the necessary information to populate the dialog */
174             /* FIXME It looks kinda extremally bad that this is here... 
175              * what about situations where this prop is NOT set during 
176              * constructing? */
177             create_contents (dialog);
178             break;
179
180         default:
181             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182             break;
183     }
184 }
185
186 static void
187 hildon_set_password_get_property                (GObject *object,
188                                                  guint prop_id,
189                                                  GValue *value,
190                                                  GParamSpec *pspec)
191 {
192     HildonSetPasswordDialogPrivate *priv = NULL;
193
194     priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (object);
195     g_assert (priv);
196
197     switch (prop_id) {
198
199         case PROP_MESSAGE:
200             g_value_set_string (value, gtk_label_get_text (priv->message_label));
201             break;
202
203         case PROP_PASSWORD:
204             g_value_set_string (value,
205                     gtk_entry_get_text (GTK_ENTRY (priv->pwd1st_entry)));
206             break;
207
208         case PROP_HILDON_PASSWORD_DIALOG:
209             g_value_set_boolean (value, priv->protection);
210             break;
211
212         default:
213             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214             break;
215     }
216 }
217
218
219 static void
220 create_contents                                 (HildonSetPasswordDialog *dialog)
221 {
222     HildonSetPasswordDialogPrivate *priv = NULL;
223     AtkObject *atk_aux = NULL;
224
225     GtkSizeGroup *group;
226
227     priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
228     g_assert (priv);
229     priv->checkbox = NULL;
230
231     /* Size group for labels */
232     group = GTK_SIZE_GROUP (gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL));
233
234     gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
235
236     /* Setup and pack domain label */
237     priv->message_label = GTK_LABEL (gtk_label_new (NULL));
238     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
239             GTK_WIDGET(priv->message_label), FALSE, FALSE, 0);
240     gtk_widget_show (GTK_WIDGET (priv->message_label));
241
242     if (priv->protection == TRUE) {
243         /* Use Change Password Dialog strings */
244         priv->pwd1st_caption_string = _(HILDON_SET_MODIFY_PASSWORD_DIALOG_PASSWORD);
245         priv->pwd2nd_caption_string = _(HILDON_SET_MODIFY_PASSWORD_DIALOG_VERIFY_PASSWORD);
246
247         /* Setup checkbox to enable/disable password protection */
248         priv->checkbox = gtk_check_button_new ();
249         gtk_widget_show (priv->checkbox);
250         priv->checkbox_caption = hildon_caption_new
251             (group,
252              _(HILDON_SET_MODIFY_PASSWORD_DIALOG_LABEL),
253              priv->checkbox,
254              NULL, HILDON_CAPTION_OPTIONAL);
255         hildon_caption_set_separator (HILDON_CAPTION (priv->checkbox_caption), "");
256         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
257                 priv->checkbox_caption, TRUE, TRUE, 0);
258         gtk_widget_show (priv->checkbox_caption);
259         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->checkbox),
260                 TRUE);
261         gtk_signal_connect (GTK_OBJECT (priv->checkbox), "toggled",
262                 G_CALLBACK (hildon_checkbox_toggled), dialog);
263
264         /* Setup appropriate response handler */
265         g_signal_connect (G_OBJECT (dialog), "response",
266                 G_CALLBACK (hildon_set_password_response_change),
267                 NULL);
268     } else {
269         /* Use Set Password Dialog strings */
270         priv->pwd1st_caption_string = _(HILDON_SET_PASSWORD_DIALOG_PASSWORD);
271         priv->pwd2nd_caption_string = _(HILDON_SET_PASSWORD_DIALOG_VERIFY_PASSWORD);
272
273         /* Setup appropriate response handler */
274         g_signal_connect (G_OBJECT (dialog), "response",
275                 G_CALLBACK(hildon_set_password_response_set),
276                 NULL);
277     }
278
279     /* Create the password field */
280     priv->pwd1st_entry = gtk_entry_new ();
281     if ((atk_aux = gtk_widget_get_accessible(priv->pwd1st_entry)))
282       {
283         atk_object_set_name(atk_aux, _("Old Passwd"));
284       }
285     
286
287 #ifdef MAEMO_GTK
288     g_object_set (priv->pwd1st_entry, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
289 #endif
290
291     gtk_entry_set_visibility (GTK_ENTRY(priv->pwd1st_entry), FALSE);
292     gtk_widget_show (priv->pwd1st_entry);
293     priv->pwd1st_caption = hildon_caption_new (group,
294             priv->pwd1st_caption_string,
295             priv->pwd1st_entry,
296             NULL, HILDON_CAPTION_OPTIONAL);
297
298     hildon_caption_set_separator (HILDON_CAPTION(priv->pwd1st_caption), "");
299     gtk_entry_set_visibility (GTK_ENTRY (priv->pwd1st_entry), FALSE);
300     gtk_box_pack_start (GTK_BOX(GTK_DIALOG (dialog)->vbox),
301             priv->pwd1st_caption, TRUE, TRUE, 0);
302     gtk_widget_show (priv->pwd1st_caption);
303
304     /* Create the password verify field */
305     priv->pwd2nd_entry = gtk_entry_new();
306     if ((atk_aux = gtk_widget_get_accessible(priv->pwd2nd_entry)))
307       {
308         atk_object_set_name(atk_aux, _("New Passwd"));
309       }
310
311
312 #ifdef MAEMO_GTK
313     g_object_set (priv->pwd2nd_entry, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
314 #endif
315
316     gtk_widget_show (priv->pwd2nd_entry);
317     priv->pwd2nd_caption = hildon_caption_new (group,
318             priv->pwd2nd_caption_string,
319             priv->pwd2nd_entry,
320             NULL, HILDON_CAPTION_OPTIONAL);
321     hildon_caption_set_separator (HILDON_CAPTION (priv->pwd2nd_caption), "");
322     gtk_entry_set_visibility (GTK_ENTRY (priv->pwd2nd_entry), FALSE);
323     gtk_box_pack_start (GTK_BOX(GTK_DIALOG (dialog)->vbox),
324             priv->pwd2nd_caption, TRUE, TRUE, 0);
325     gtk_widget_show (priv->pwd2nd_caption);
326
327     /* Set dialog title */
328     gtk_window_set_title (GTK_WINDOW (dialog),
329             _(priv->protection
330                 ? HILDON_SET_MODIFY_PASSWORD_DIALOG_TITLE
331                 : HILDON_SET_PASSWORD_DIALOG_TITLE));
332
333     /* Create the OK/CANCEL buttons */
334     gtk_dialog_add_button (GTK_DIALOG (dialog),  _(priv->protection
335                                 ? HILDON_SET_MODIFY_PASSWORD_DIALOG_OK
336                                 : HILDON_SET_PASSWORD_DIALOG_OK), GTK_RESPONSE_OK);
337
338     gtk_dialog_add_button (GTK_DIALOG (dialog), _(priv->protection
339                                ? HILDON_SET_MODIFY_PASSWORD_DIALOG_CANCEL
340                                : HILDON_SET_PASSWORD_DIALOG_CANCEL), GTK_RESPONSE_CANCEL);
341
342     gtk_widget_show_all (GTK_DIALOG (dialog)->vbox);
343     gtk_widget_show_all (GTK_DIALOG (dialog)->action_area);
344  
345     /* Ensure group is freed when all its contents have been removed */
346     g_object_unref (group);
347 }
348
349 static void
350 hildon_set_password_dialog_class_init           (HildonSetPasswordDialogClass *class)
351 {
352     GObjectClass *object_class = G_OBJECT_CLASS (class);
353
354     parent_class = g_type_class_peek_parent (class);
355
356     /* Override virtual methods */
357     object_class->set_property = hildon_set_password_set_property;
358     object_class->get_property = hildon_set_password_get_property;
359
360     /* Install new properties */
361     g_object_class_install_property (object_class, 
362             PROP_MESSAGE, 
363             g_param_spec_string ("message",
364                 "Message",
365                 "A message to display to the user",
366                 NULL,
367                 G_PARAM_READWRITE));
368
369     /**
370      * HildonSetPasswordDialog:modify-protection:
371      *
372      * Password type.
373      */
374     g_object_class_install_property (object_class, 
375             PROP_HILDON_PASSWORD_DIALOG, 
376             g_param_spec_boolean ("modify-protection",
377                 "Password type",
378                 "Set type to dialog",
379                 TRUE, 
380                 G_PARAM_CONSTRUCT_ONLY |
381                 G_PARAM_READWRITE));
382
383     /**
384      * HildonSetPasswordDialog:password:
385      *
386      * Content of the password field.
387      */
388     g_object_class_install_property (object_class, 
389             PROP_PASSWORD, 
390             g_param_spec_string ("password",
391                 "Password content",
392                 "Set content to dialog",
393                 "DEFAULT",
394                 G_PARAM_READWRITE));
395
396     /* Install private structure */
397     g_type_class_add_private (class,
398             sizeof (HildonSetPasswordDialogPrivate));
399 }
400
401 static void
402 hildon_set_password_dialog_init                 (HildonSetPasswordDialog *dialog)
403 {
404     /* Most of the initializations are done in create_contents()
405        after the 'modify_protection' property has been set */
406
407     gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
408 }
409
410
411 /* We come here when response button is clicked and dialog 
412    is used to change existing password. */
413 static void
414 hildon_set_password_response_change             (GtkDialog *dialog, 
415                                                  gint arg1,
416                                                  gpointer unused)
417 {
418     GtkEntry *pwd1st_entry;
419     GtkEntry *pwd2nd_entry;
420     gchar *text1;
421     gchar *text2;
422     HildonNote *note;
423     gint i;
424     HildonSetPasswordDialogPrivate *priv;
425
426     priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
427     g_assert (priv);
428
429     /* Password and verification */
430     pwd1st_entry = GTK_ENTRY (gtk_bin_get_child
431             (GTK_BIN (priv->pwd1st_caption)));
432     pwd2nd_entry = GTK_ENTRY (gtk_bin_get_child
433             (GTK_BIN (priv->pwd2nd_caption)));
434     text1 = GTK_ENTRY (pwd1st_entry)->text;
435     text2 = GTK_ENTRY (pwd2nd_entry)->text;
436
437     /* User accepted the dialog */
438     if (arg1 == GTK_RESPONSE_OK) {
439         /* Is the checkbox marked, so password protection is still in use? */  
440         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->checkbox))){
441             /* Yes, Something is given as password as well? */
442             if (text1[0] != '\0') {
443                 if (strcmp (text1, text2) == 0) {
444                     /* Passwords match, so accept change password */
445                     priv->protection = TRUE;
446
447                 } else if (text2[0] == '\0') {
448                     /* Second field is empty, so show error, but don't clear fields */
449                     g_signal_stop_emission_by_name (G_OBJECT(dialog),
450                             "response");
451
452                     hildon_banner_show_information (GTK_WIDGET (dialog), NULL,
453                             c_(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
454
455                     gtk_widget_grab_focus (GTK_WIDGET (pwd2nd_entry));
456
457                 } else {
458                     /* Error: Passwords don't match, so start over */
459                     g_signal_stop_emission_by_name (G_OBJECT(dialog),
460                             "response");
461
462                     gtk_entry_set_text (pwd1st_entry, "");
463                     gtk_entry_set_text (pwd2nd_entry, "");
464
465                     hildon_banner_show_information (GTK_WIDGET (dialog), NULL,
466                             c_(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
467
468                     gtk_widget_grab_focus (GTK_WIDGET (pwd1st_entry));
469                 }
470             } else {
471                 /* No, the password is empty */
472                 g_signal_stop_emission_by_name (G_OBJECT (dialog), "response");
473
474                 if (text2[0] == '\0') {
475                     /* Error: Both fields are empty */
476                     hildon_banner_show_information (GTK_WIDGET (dialog), NULL, c_(HILDON_SET_PASSWORD_DIALOG_EMPTY));
477                 } else {
478                     /* Error: Second field doesn't match
479                        the empty first field, so start over */
480                     hildon_banner_show_information (GTK_WIDGET (dialog), NULL, c_(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
481                     gtk_entry_set_text(pwd2nd_entry, "");
482                 }
483
484                 gtk_widget_grab_focus (GTK_WIDGET (pwd1st_entry));
485             }
486         } else {
487             /* No, user wants to remove password protection. 
488                Confirm remove password protection */
489             note = HILDON_NOTE (hildon_note_new_confirmation
490                     (GTK_WINDOW (dialog),
491                      c_(HILDON_SET_PASSWORD_DIALOG_REMOVE_PROTECTION)));
492
493             hildon_note_set_button_texts
494                 (HILDON_NOTE (note),
495                  c_(HILDON_REMOVE_PROTECTION_CONFIRMATION_REMOVE), 
496                  c_(HILDON_REMOVE_PROTECTION_CONFIRMATION_CANCEL));
497
498             /* Display confirmation note */
499             i = gtk_dialog_run (GTK_DIALOG (note));
500
501             gtk_widget_destroy (GTK_WIDGET (note));
502
503             if (i == GTK_RESPONSE_OK)
504                 /* Remove password protection */
505                 priv->protection = FALSE;
506             else {
507                 /* Remove password protection cancelled */
508                 priv->protection = TRUE;
509                 g_signal_stop_emission_by_name (G_OBJECT(dialog), "response");
510             }
511         }
512
513     } else {
514         /* Watch out for fading boolean values */
515         priv->protection = TRUE;
516     }
517 }
518
519 /* We come here when response button is clicked and dialog 
520    is used to set new password. */
521 static void
522 hildon_set_password_response_set                (GtkDialog *dialog, 
523                                                  gint arg1,
524                                                  gpointer unused)
525 {
526     GtkEntry *pwd1st_entry;
527     GtkEntry *pwd2nd_entry;
528     gchar *text1;
529     gchar *text2;
530
531     HildonSetPasswordDialogPrivate *priv;
532
533     priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
534     g_assert (priv);
535
536     /* Password and confirmation */
537     pwd1st_entry = GTK_ENTRY (gtk_bin_get_child
538             (GTK_BIN (priv->pwd1st_caption)));
539
540     pwd2nd_entry = GTK_ENTRY (gtk_bin_get_child
541             (GTK_BIN (priv->pwd2nd_caption)));
542
543     text1 = GTK_ENTRY (pwd1st_entry)->text;
544     text2 = GTK_ENTRY (pwd2nd_entry)->text;
545
546     if (arg1 == GTK_RESPONSE_OK) {
547         /* User provided something for password? */
548         if (text1[0] != '\0') {
549             if (strcmp (text1, text2) == 0) {
550                 /* Passwords match, so accept set password */
551                 priv->protection = TRUE;
552
553             } else if (text2[0] == '\0') {
554                 /* Second field is empty, so show error,
555                    but don't clear the fields */
556                 g_signal_stop_emission_by_name (G_OBJECT(dialog), "response");
557                 hildon_banner_show_information (GTK_WIDGET (dialog), NULL, c_(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
558
559                 gtk_widget_grab_focus (GTK_WIDGET (priv->pwd2nd_entry));
560
561             } else {
562                 /* Error: Passwords don't match, so start over */
563                 g_signal_stop_emission_by_name (G_OBJECT(dialog), "response");
564                 gtk_entry_set_text (pwd1st_entry, "");
565                 gtk_entry_set_text (pwd2nd_entry, "");
566                 hildon_banner_show_information (GTK_WIDGET (dialog), NULL, c_(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
567
568                 gtk_widget_grab_focus (GTK_WIDGET (priv->pwd1st_entry));
569             }
570         } else {
571             /* First field is empty */
572             g_signal_stop_emission_by_name (G_OBJECT (dialog), "response");
573             if (text2[0] == '\0') {
574                 /* Error: Both fields are empty */
575                 hildon_banner_show_information (GTK_WIDGET (dialog), NULL, c_(HILDON_SET_PASSWORD_DIALOG_EMPTY));
576             } else {
577                 /* Error: Second field doesn't match
578                    the empty first field, so start over */
579                 hildon_banner_show_information (GTK_WIDGET (dialog), NULL, c_(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
580                 gtk_entry_set_text (pwd2nd_entry, "");
581             }
582
583             gtk_widget_grab_focus (GTK_WIDGET (pwd1st_entry));
584         }
585     } else { 
586         /* Watch out for fading boolean values */
587         priv->protection = FALSE;
588     }         
589 }
590
591 static void
592 hildon_checkbox_toggled                         (GtkWidget *widget, 
593                                                  gpointer dialog)
594 {
595     HildonSetPasswordDialogPrivate *priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
596     gboolean active;
597
598     g_assert (priv);
599
600     /* If the user enabled/disabled the password protection feature
601        we enable/disable password entries accordingly */
602     active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
603     gtk_widget_set_sensitive (GTK_WIDGET (priv->pwd1st_entry), active);
604     gtk_widget_set_sensitive (GTK_WIDGET (priv->pwd2nd_entry), active);
605 }
606
607 /**
608  * hildon_set_password_dialog_get_type:
609  *
610  * Returns GType for HildonPasswordDialog as produced by
611  * g_type_register_static().
612  *
613  * Returns: HildonSetPasswordDialog type
614  */
615 GType G_GNUC_CONST
616 hildon_set_password_dialog_get_type             (void)
617 {
618     static GType dialog_type = 0;
619
620     if (! dialog_type) {
621         static const GTypeInfo dialog_info = {
622             sizeof (HildonSetPasswordDialogClass),
623             NULL,       /* base_init */
624             NULL,       /* base_finalize */
625             (GClassInitFunc) hildon_set_password_dialog_class_init,
626             NULL,       /* class_finalize */
627             NULL,       /* class_data */
628             sizeof (HildonSetPasswordDialog),
629             0,  /* n_preallocs */
630             (GInstanceInitFunc) hildon_set_password_dialog_init
631         };
632
633         dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
634                 "HildonSetPasswordDialog",
635                 &dialog_info, 0);
636     }
637
638     return dialog_type;
639 }
640
641 /**
642  * hildon_set_password_dialog_new:
643  * @parent: parent window; can be NULL
644  * @modify_protection: TRUE creates a new change password dialog and FALSE
645  *                     creates a new set password dialog 
646  * 
647  * Constructs a new HildonSetPasswordDialog.
648  *
649  * Returns: a new #GtkWidget of type HildonSetPasswordDialog
650  */
651 GtkWidget*
652 hildon_set_password_dialog_new                  (GtkWindow *parent,
653                                                  gboolean modify_protection)
654 {
655     return hildon_set_password_dialog_new_with_default (parent, "", modify_protection);
656 }
657
658 /**
659  * hildon_set_password_dialog_new_with_default:
660  * @parent: parent window; can be NULL
661  * @password: a default password to be shown in password field
662  * @modify_protection: TRUE creates a new change password dialog and FALSE
663  *                     creates a new set password dialog 
664  * 
665  * Same as #hildon_set_password_dialog_new, but with a default password
666  * in password field.
667  *
668  * Returns: a new #GtkWidget of type HildonSetPasswordDialog
669  */
670
671 GtkWidget*
672 hildon_set_password_dialog_new_with_default     (GtkWindow *parent,
673                                                  const gchar *password,
674                                                  gboolean modify_protection)
675 {
676     GtkWidget *dialog = g_object_new (HILDON_TYPE_SET_PASSWORD_DIALOG,
677             "modify_protection", modify_protection,
678             "password", password, NULL);
679
680     if (parent != NULL) {
681         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
682     }
683
684     return dialog;
685 }
686
687 /**
688  * hildon_set_password_dialog_get_password:
689  * @dialog: pointer to HildonSetPasswordDialog
690  * 
691  * Returns current password.
692  *
693  * Returns: changed password ( if the dialog is successfully 
694  * accepted with 'OK' ( and when the check box is 'ON' ( in Change Password
695  * Dialog ))
696  */
697 const gchar*
698 hildon_set_password_dialog_get_password         (HildonSetPasswordDialog *dialog)
699 {
700     HildonSetPasswordDialogPrivate *priv;
701
702     g_return_val_if_fail (HILDON_IS_SET_PASSWORD_DIALOG (dialog), NULL);
703
704     priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
705     g_assert (priv);
706
707     return GTK_ENTRY (priv->pwd1st_entry)->text;
708 }
709
710 /**
711  * hildon_set_password_dialog_get_protected:
712  * @dialog: pointer to HildonSetPasswordDialog
713  * 
714  * Returns the protection mode.
715  *
716  * Returns: password protection mode ( TRUE when the protection is
717  *               'ON' and FALSE when the protection is 'OFF' )
718  */
719 gboolean
720 hildon_set_password_dialog_get_protected        (HildonSetPasswordDialog *dialog)
721 {
722     HildonSetPasswordDialogPrivate *priv;
723
724     g_return_val_if_fail (HILDON_IS_SET_PASSWORD_DIALOG (dialog), FALSE);
725
726     priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
727     g_assert (priv);
728
729     return priv->protection;
730 }
731
732 /**
733  * hildon_set_password_dialog_set_message:
734  * @dialog: the dialog
735  * @message: the message or some other descriptive text to be set
736  * 
737  * Sets the optional descriptive text.
738  */
739 void 
740 hildon_set_password_dialog_set_message          (HildonSetPasswordDialog *dialog, 
741                                                  const gchar *message)
742 {
743     HildonSetPasswordDialogPrivate *priv = NULL;
744
745     g_return_if_fail (HILDON_IS_SET_PASSWORD_DIALOG (dialog));
746
747     priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
748     g_assert (priv);
749
750     gtk_label_set_text (priv->message_label, message);  
751 }