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