2 * This file is part of hildon-libs
4 * Copyright (C) 2005 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@nokia.com>
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; either version 2.1 of
11 * the License, or (at your option) any later version.
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.
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
26 * @file hildon-set-password-dialog.c
28 * This file implements API for Hildon Set and Change Password dialog.
30 * Change password dialog:
31 * @desc: Change Password Dialog is used to change an existing one in
32 * situtations where the password can be removed. Unselecting the check
33 * box dims the password fields below it. If the dialog is accepted with
34 * 'OK' while the check box is unselected, a Confirmation Note is played.
35 * If a Confirmation Note Dialog is accepted with 'Remove', the password
36 * protection is removed.
38 * Set password dialog:
39 * @desc: Set Password Dialog is used to define a password, or change a
40 * password that cannot be removed.
41 * @seealso: #HildonGetPasswordDialog
50 #include <gdk/gdkkeysyms.h>
60 #include <hildon-widgets/hildon-caption.h>
61 #include <hildon-widgets/gtk-infoprint.h>
62 #include <hildon-widgets/hildon-set-password-dialog.h>
63 #include <hildon-widgets/hildon-note.h>
64 #include <hildon-widgets/hildon-defines.h>
67 #define _(String) dgettext(PACKAGE, String)
69 static GtkDialogClass *parent_class;
71 #define HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(obj)\
72 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
73 HILDON_TYPE_SET_PASSWORD_DIALOG, HildonSetPasswordDialogPrivate));
76 _HildonSetPasswordDialogPrivate HildonSetPasswordDialogPrivate;
79 hildon_set_password_dialog_class_init(HildonSetPasswordDialogClass *
81 static void hildon_set_password_dialog_init(HildonSetPasswordDialog *
83 static void hildon_checbox_toggled(GtkWidget * widget, gpointer data);
86 hildon_set_password_response_change(GtkDialog * d, gint arg1,
89 hildon_set_password_response_set(GtkDialog * d, gint arg1,
93 hildon_set_password_dialog_constructor(GType type,
94 guint n_construct_properties,
95 GObjectConstructParam *
96 construct_properties);
97 static void hildon_set_password_set_property(GObject * object,
101 static void hildon_set_password_get_property(GObject * object,
102 guint prop_id, GValue * value,
107 struct _HildonSetPasswordDialogPrivate {
109 GtkWidget *checkboxCaption;
112 GtkLabel *domainLabel;
115 GtkWidget *pwd1stEntry;
116 GtkWidget *pwd1stCaption;
117 gchar *pwd1stCaption_string;
120 GtkWidget *pwd2ndEntry;
121 GtkWidget *pwd2ndCaption;
122 gchar *pwd2ndCaption_string;
124 /* OK/Cancel buttons */
126 GtkWidget *cancelButton;
136 PROP_HILDON_PASSWORD_DIALOG
139 /* Private functions */
141 hildon_set_password_set_property(GObject * object,
143 const GValue * value, GParamSpec * pspec)
145 HildonSetPasswordDialog *dialog = HILDON_SET_PASSWORD_DIALOG(object);
146 HildonSetPasswordDialogPrivate *priv;
148 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
152 gtk_label_set_text(priv->domainLabel, g_value_get_string(value));
155 gtk_entry_set_text(GTK_ENTRY(priv->pwd1stEntry), g_value_get_string(value));
157 case PROP_HILDON_PASSWORD_DIALOG:
158 priv->protection = g_value_get_boolean(value);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
167 hildon_set_password_get_property(GObject * object,
169 GValue * value, GParamSpec * pspec)
171 HildonSetPasswordDialog *dialog = HILDON_SET_PASSWORD_DIALOG(object);
172 HildonSetPasswordDialogPrivate *priv;
175 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
179 string = gtk_label_get_text(priv->domainLabel);
180 g_value_set_string(value, string);
182 string = gtk_entry_get_text(GTK_ENTRY(priv->pwd1stEntry));
183 g_value_set_string(value, string);
185 case PROP_HILDON_PASSWORD_DIALOG:
186 g_value_set_boolean(value, g_value_get_boolean(value));
189 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
195 hildon_set_password_dialog_constructor(GType type,
196 guint n_construct_properties,
197 GObjectConstructParam *
198 construct_properties)
201 GObjectClass *parent_class;
202 HildonSetPasswordDialogPrivate *priv;
205 parent_class = G_OBJECT_CLASS(g_type_class_peek_parent
206 (g_type_class_peek(type)));
207 dialog = parent_class->constructor(type, n_construct_properties,
208 construct_properties);
210 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
212 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
214 priv->checkbox = NULL;
216 group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
218 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
220 priv->domainLabel = GTK_LABEL(gtk_label_new(NULL));
222 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
223 GTK_WIDGET(priv->domainLabel), FALSE, FALSE, 0);
225 gtk_widget_show(GTK_WIDGET(priv->domainLabel));
228 if (priv->protection == TRUE) {
229 /* create checkbox */
230 priv->pwd1stCaption_string = _(HILDON_SET_MODIFY_PASSWORD_DIALOG_PASSWORD);
231 priv->pwd2ndCaption_string = _(HILDON_SET_MODIFY_PASSWORD_DIALOG_VERIFY_PASSWORD);
233 priv->checkbox = gtk_check_button_new();
235 gtk_widget_show(priv->checkbox);
237 priv->checkboxCaption = hildon_caption_new
239 _(HILDON_SET_MODIFY_PASSWORD_DIALOG_LABEL),
241 NULL, HILDON_CAPTION_OPTIONAL);
242 hildon_caption_set_separator(HILDON_CAPTION(priv->checkboxCaption), "");
244 gtk_signal_connect(GTK_OBJECT(priv->checkbox), "toggled",
245 G_CALLBACK(hildon_checbox_toggled), dialog);
247 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
248 priv->checkboxCaption, TRUE, TRUE, 0);
249 gtk_widget_show(priv->checkboxCaption);
251 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(priv->checkbox),
255 priv->pwd1stCaption_string = _(HILDON_SET_PASSWORD_DIALOG_PASSWORD);
256 priv->pwd2ndCaption_string = _(HILDON_SET_PASSWORD_DIALOG_VERIFY_PASSWORD);
261 priv->pwd1stEntry = gtk_entry_new();
262 gtk_widget_show(priv->pwd1stEntry);
264 priv->pwd1stCaption = hildon_caption_new(group,
265 priv->pwd1stCaption_string,
267 NULL, HILDON_CAPTION_OPTIONAL);
268 hildon_caption_set_separator(HILDON_CAPTION(priv->pwd1stCaption), "");
270 gtk_entry_set_visibility(GTK_ENTRY(priv->pwd1stEntry), FALSE);
272 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
273 priv->pwd1stCaption, TRUE, TRUE, 0);
274 gtk_widget_show(priv->pwd1stCaption);
277 priv->pwd2ndEntry = gtk_entry_new();
278 gtk_widget_show(priv->pwd2ndEntry);
280 priv->pwd2ndCaption = hildon_caption_new(group,
281 priv->pwd2ndCaption_string,
283 NULL, HILDON_CAPTION_OPTIONAL);
284 hildon_caption_set_separator(HILDON_CAPTION(priv->pwd2ndCaption), "");
286 gtk_entry_set_visibility(GTK_ENTRY(priv->pwd2ndEntry), FALSE);
288 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
289 priv->pwd2ndCaption, TRUE, TRUE, 0);
291 gtk_widget_show(priv->pwd2ndCaption);
293 gtk_window_set_title(GTK_WINDOW(dialog),
295 ? HILDON_SET_MODIFY_PASSWORD_DIALOG_TITLE
296 : HILDON_SET_PASSWORD_DIALOG_TITLE));
298 /* Create the OK/CANCEL buttons */
299 priv->okButton = gtk_dialog_add_button
300 (GTK_DIALOG(dialog), _(priv->protection
301 ? HILDON_SET_MODIFY_PASSWORD_DIALOG_OK
302 : HILDON_SET_PASSWORD_DIALOG_OK),
304 priv->cancelButton = gtk_dialog_add_button
305 (GTK_DIALOG(dialog), _(priv->protection
306 ? HILDON_SET_MODIFY_PASSWORD_DIALOG_CANCEL
307 : HILDON_SET_PASSWORD_DIALOG_CANCEL),
308 GTK_RESPONSE_CANCEL);
310 /* gtk_window_resize(GTK_WINDOW(dialog), 370, 110); */
311 /* remove now, pixel perfection can address these majik thingys */
313 gtk_widget_show(priv->okButton);
314 gtk_widget_show(priv->cancelButton);
321 hildon_set_password_dialog_class_init(HildonSetPasswordDialogClass * class)
325 GObjectClass *object_class = G_OBJECT_CLASS(class);
327 parent_class = g_type_class_peek_parent(class);
329 object_class->set_property = hildon_set_password_set_property;
330 object_class->get_property = hildon_set_password_get_property;
331 object_class->constructor = hildon_set_password_dialog_constructor;
334 g_object_class_install_property(object_class,
336 g_param_spec_string ("domain",
338 "Set Domain (content) for domain label.",
342 g_object_class_install_property(object_class,
343 PROP_HILDON_PASSWORD_DIALOG,
344 g_param_spec_boolean ("modify_protection",
346 "Set type to dialog",
348 G_PARAM_CONSTRUCT_ONLY |
351 g_object_class_install_property(object_class,
353 g_param_spec_string ("password",
355 "Set content to dialog",
359 g_type_class_add_private(class,
360 sizeof(HildonSetPasswordDialogPrivate));
365 hildon_set_password_dialog_init(HildonSetPasswordDialog * dialog)
367 HildonSetPasswordDialogPrivate *priv;
369 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
371 priv->protection = FALSE;
375 hildon_set_password_response_change(GtkDialog * dialog, gint arg1,
378 GtkEntry *pwd1stEntry;
379 GtkEntry *pwd2ndEntry;
386 HildonSetPasswordDialogPrivate *priv;
388 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
390 pwd1stEntry = GTK_ENTRY(gtk_bin_get_child
391 (GTK_BIN(priv->pwd1stCaption)));
393 pwd2ndEntry = GTK_ENTRY(gtk_bin_get_child
394 (GTK_BIN(priv->pwd2ndCaption)));
396 text1 = GTK_ENTRY(pwd1stEntry)->text;
397 text2 = GTK_ENTRY(pwd2ndEntry)->text;
398 if (arg1 == GTK_RESPONSE_OK){
399 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(priv->checkbox))){
400 if(strcmp(text1, "" ) != 0){
401 if(strcmp (text1, text2) == 0){
402 priv->protection = TRUE;
403 } else if(strcmp(text2, "" ) == 0){
404 g_signal_stop_emission_by_name(G_OBJECT(dialog),
407 _(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
408 gtk_widget_grab_focus(GTK_WIDGET(pwd2ndEntry));
410 g_signal_stop_emission_by_name(G_OBJECT(dialog),
412 gtk_entry_set_text(pwd1stEntry, "");
413 gtk_entry_set_text(pwd2ndEntry, "");
415 _(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
416 gtk_widget_grab_focus(GTK_WIDGET(pwd1stEntry));
419 g_signal_stop_emission_by_name(G_OBJECT(dialog), "response");
420 if (strcmp(text2, "") == 0) {
421 gtk_infoprint (NULL, _(HILDON_SET_PASSWORD_DIALOG_EMPTY));
423 gtk_infoprint (NULL, _(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
424 gtk_entry_set_text(pwd2ndEntry, "");
426 gtk_widget_grab_focus(GTK_WIDGET(pwd1stEntry));
429 note = HILDON_NOTE(hildon_note_new_confirmation
431 _(HILDON_SET_PASSWORD_DIALOG_REMOVE_PROTECTION
434 hildon_note_set_button_texts
436 _(HILDON_REMOVE_PROTECTION_CONFIRMATION_REMOVE),
437 _(HILDON_REMOVE_PROTECTION_CONFIRMATION_CANCEL));
439 i = gtk_dialog_run(GTK_DIALOG(note));
441 gtk_widget_destroy(GTK_WIDGET(note));
443 if (i == GTK_RESPONSE_OK)
444 priv->protection = FALSE;
446 priv->protection = TRUE;
447 g_signal_stop_emission_by_name(G_OBJECT(dialog), "response");
452 priv->protection = TRUE;
457 hildon_set_password_response_set(GtkDialog * dialog, gint arg1,
460 GtkEntry *pwd1stEntry;
461 GtkEntry *pwd2ndEntry;
465 HildonSetPasswordDialogPrivate *priv;
467 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
469 pwd1stEntry = GTK_ENTRY(gtk_bin_get_child
470 (GTK_BIN(priv->pwd1stCaption)));
472 pwd2ndEntry = GTK_ENTRY(gtk_bin_get_child
473 (GTK_BIN(priv->pwd2ndCaption)));
475 text1 = GTK_ENTRY(pwd1stEntry)->text;
476 text2 = GTK_ENTRY(pwd2ndEntry)->text;
478 if (arg1 == GTK_RESPONSE_OK) {
479 if (strcmp (text1, "") != 0) {
480 if (strcmp (text1, text2) == 0) {
481 priv->protection = TRUE;
482 } else if (strcmp (text2, "") == 0) {
483 g_signal_stop_emission_by_name(G_OBJECT(dialog), "response");
484 gtk_infoprint (NULL, _(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
485 gtk_widget_grab_focus (GTK_WIDGET (priv->pwd2ndEntry));
487 g_signal_stop_emission_by_name(G_OBJECT(dialog), "response");
488 gtk_entry_set_text(pwd1stEntry, "");
489 gtk_entry_set_text(pwd2ndEntry, "");
490 gtk_infoprint (NULL, _(HILDON_SET_PASSWORD_DIALOG_MISMATCH ));
491 gtk_widget_grab_focus(GTK_WIDGET(priv->pwd1stEntry));
494 g_signal_stop_emission_by_name(G_OBJECT(dialog), "response");
495 if (strcmp(text2, "") == 0) {
496 gtk_infoprint (NULL, _(HILDON_SET_PASSWORD_DIALOG_EMPTY));
498 gtk_infoprint (NULL, _(HILDON_SET_PASSWORD_DIALOG_MISMATCH));
499 gtk_entry_set_text(pwd2ndEntry, "");
501 gtk_widget_grab_focus(GTK_WIDGET(pwd1stEntry));
504 priv->protection = FALSE;
508 static void hildon_checbox_toggled(GtkWidget * widget, gpointer data)
510 HildonSetPasswordDialog *dialog;
511 HildonSetPasswordDialogPrivate *priv;
513 dialog = HILDON_SET_PASSWORD_DIALOG(data);
514 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
516 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
517 gtk_widget_set_sensitive(GTK_WIDGET(priv->pwd1stEntry), TRUE);
518 gtk_widget_set_sensitive(GTK_WIDGET(priv->pwd2ndEntry), TRUE);
520 gtk_widget_set_sensitive(GTK_WIDGET(priv->pwd1stEntry), FALSE);
521 gtk_widget_set_sensitive(GTK_WIDGET(priv->pwd2ndEntry), FALSE);
525 /* Public functions */
528 * hildon_set_password_dialog_get_type:
530 * Returns GType for HildonPasswordDialog as produced by
531 * g_type_register_static().
533 * Return value: HildonSetPasswordDialog type
535 GType hildon_set_password_dialog_get_type()
537 static GType dialog_type = 0;
540 static const GTypeInfo dialog_info = {
541 sizeof(HildonSetPasswordDialogClass),
542 NULL, /* base_init */
543 NULL, /* base_finalize */
544 (GClassInitFunc) hildon_set_password_dialog_class_init,
545 NULL, /* class_finalize */
546 NULL, /* class_data */
547 sizeof(HildonSetPasswordDialog),
549 (GInstanceInitFunc) hildon_set_password_dialog_init
552 dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
553 "HildonSetPasswordDialog",
560 * hildon_set_password_dialog_new:
561 * @parent: parent window; can be NULL
562 * @modify_protection: TRUE creates a new change password dialog and FALSE
563 * creates a new set password dialog
565 * Construct a new HildonSetPasswordDialog.
567 * Return value: a new #GtkWidget of type HildonSetPasswordDialog
570 GtkWidget *hildon_set_password_dialog_new(GtkWindow * parent,
571 gboolean modify_protection)
573 GtkWidget *dialog = g_object_new
574 (HILDON_TYPE_SET_PASSWORD_DIALOG, "modify_protection",
575 modify_protection, NULL);
577 if (modify_protection == TRUE) {
578 g_signal_connect(G_OBJECT(dialog), "response",
579 G_CALLBACK(hildon_set_password_response_change),
582 g_signal_connect(G_OBJECT(dialog), "response",
583 G_CALLBACK(hildon_set_password_response_set),
586 if (parent != NULL) {
587 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
594 * hildon_set_password_dialog_new_with_default:
595 * @parent: parent window; can be NULL
596 * @password: a default password to be shown in password field.
597 * @modify_protection: TRUE creates a new change password dialog and FALSE
598 * creates a new set password dialog
600 * Same as #hildon_set_password_dialog_new, but with a default password
603 * Return value: a new #GtkWidget of type HildonSetPasswordDialog
606 GtkWidget *hildon_set_password_dialog_new_with_default
609 gboolean modify_protection)
613 dialog = hildon_set_password_dialog_new(parent,
616 g_object_set(G_OBJECT(dialog), "password", password, NULL);
622 * hildon_set_password_dialog_get_password:
623 * @dialog: pointer to HildonSetPasswordDialog
625 * Returns current password.
627 * Return value: changed password ( if the dialog is successfully
628 * accepted with 'OK' ( and when the check box is 'ON' ( in Change Password
632 *hildon_set_password_dialog_get_password(HildonSetPasswordDialog *
635 HildonSetPasswordDialogPrivate *priv;
637 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
639 return GTK_ENTRY(priv->pwd1stEntry)->text;
643 * hildon_set_password_dialog_get_protected:
644 * @dialog: pointer to HildonSetPasswordDialog
646 * Returns protection mode.
648 * Return value: password protection mode ( TRUE when the protection is
649 * 'ON' and FALSE when the protection is 'OFF' )
652 hildon_set_password_dialog_get_protected(HildonSetPasswordDialog * dialog)
654 HildonSetPasswordDialogPrivate *priv;
656 priv = HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
658 return priv->protection;
662 * hildon_set_password_dialog_set_domain(GtkWidget *dialog,
663 * @dialog: the dialog
664 * @domain: the domain or some other descriptive text to be set.
666 * sets the optional descriptive text
669 void hildon_set_password_dialog_set_domain(HildonSetPasswordDialog *dialog,
672 HildonSetPasswordDialogPrivate *priv =
673 HILDON_SET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
674 gtk_label_set_text(priv->domainLabel, domain);