2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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; version 2.1 of
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 * SECTION:hildon-get-password-dialog
27 * @short_description: A widget used to get a password
28 * @see_also: #HildonSetPasswordDialog
30 * HildonGetPasswordDialog prompts the user for a password. It allows
31 * inputting password, with an optional configurable label eg. for
32 * showing a custom message. The maximum length of the password can be set.
39 #include "hildon-get-password-dialog.h"
47 #include "hildon-input-mode-hint.h"
48 #include "hildon-caption.h"
49 #include "hildon-banner.h"
51 #include "hildon-get-password-dialog-private.h"
53 #define _(String) dgettext("hildon-libs", String)
55 #define HILDON_GET_PASSWORD_DIALOG_TITLE "ecdg_ti_get_old_password"
57 #define HILDON_GET_PASSWORD_DIALOG_PASSWORD "ecdg_fi_get_old_pwd_enter_pwd"
59 #define HILDON_GET_PASSWORD_DIALOG_OK "ecdg_bd_get_old_password_dialog_ok"
61 #define HILDON_GET_PASSWORD_DIALOG_CANCEL "ecdg_bd_get_old_password_dialog_cancel"
63 #define HILDON_GET_PASSWORD_VERIFY_DIALOG_TITLE "ecdg_ti_verify_password"
65 #define HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD "ecdg_fi_verify_pwd_enter_pwd"
67 #define HILDON_GET_PASSWORD_VERIFY_DIALOG_OK "ecdg_bd_verify_password_dialog_ok"
69 #define HILDON_GET_PASSWORD_VERIFY_DIALOG_CANCEL "ecdg_bd_verify_password_dialog_cancel"
71 #define HILDON_GET_PASSWORD_DIALOG_MAX_CHARS "ckdg_ib_maximum_characters_reached"
73 static GtkDialogClass* parent_class;
76 hildon_get_password_dialog_class_init (HildonGetPasswordDialogClass *class);
79 hildon_get_password_dialog_init (HildonGetPasswordDialog *widget);
82 hildon_get_password_set_property (GObject *object,
88 hildon_get_password_get_property (GObject *object,
94 create_contents (HildonGetPasswordDialog *dialog);
97 invalid_input (GtkWidget *widget,
98 GtkInvalidInputType reason,
112 /* Private functions */
114 hildon_get_password_set_property (GObject *object,
119 HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG (object);
120 HildonGetPasswordDialogPrivate *priv;
122 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE (object);
128 /* Set label text representing password domain */
129 gtk_label_set_text (priv->message_label, g_value_get_string (value));
133 gtk_entry_set_text(GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->password_entry))),
134 g_value_get_string(value));
137 case PROP_NUMBERS_ONLY:
138 /* Set input mode for the password entry */
139 g_object_set(G_OBJECT (gtk_bin_get_child GTK_BIN ((priv->password_entry))),
141 (g_value_get_boolean (value)
142 ? HILDON_INPUT_MODE_HINT_NUMERIC
143 : HILDON_INPUT_MODE_HINT_ALPHANUMERICSPECIAL),
147 case PROP_CAPTION_LABEL:
148 hildon_get_password_dialog_set_caption (dialog, g_value_get_string (value));
152 hildon_get_password_dialog_set_max_characters (dialog, g_value_get_int (value));
156 priv->get_old = g_value_get_boolean (value);
157 create_contents (dialog);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 hildon_get_password_get_property (GObject *object,
172 HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG (object);
173 HildonGetPasswordDialogPrivate *priv;
178 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
184 string = gtk_label_get_text (priv->message_label);
185 g_value_set_string (value, string);
189 string = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->password_entry))));
190 g_value_set_string (value, string);
193 case PROP_NUMBERS_ONLY:
194 /* This property is set if and only if the input mode
195 of the password entry has been set to numeric only */
196 g_object_get (G_OBJECT (gtk_bin_get_child (GTK_BIN (priv->password_entry))),
197 "input-mode", &input_mode, NULL);
198 g_value_set_boolean (value,
199 (input_mode == HILDON_INPUT_MODE_HINT_NUMERIC));
202 case PROP_CAPTION_LABEL:
203 string = hildon_caption_get_label (priv->password_entry);
204 g_value_set_string (value, string);
208 max_length = gtk_entry_get_max_length
209 (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->password_entry))));
210 g_value_set_int (value, max_length);
214 g_value_set_boolean (value, priv->get_old);
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 hildon_get_password_dialog_class_init (HildonGetPasswordDialogClass *class)
226 GObjectClass *object_class = G_OBJECT_CLASS (class);
228 parent_class = g_type_class_peek_parent (class);
230 /* Override virtual functions */
231 object_class->set_property = hildon_get_password_set_property;
232 object_class->get_property = hildon_get_password_get_property;
234 /* Install new properties */
235 g_object_class_install_property
238 g_param_spec_string ("message",
240 "Set optional message",
244 g_object_class_install_property
247 g_param_spec_string ("password",
253 g_object_class_install_property
256 g_param_spec_boolean ("numbers_only",
258 "Set entry to accept only numeric values",
262 g_object_class_install_property
265 g_param_spec_string ("caption-label",
267 "The text to be set as the caption label",
271 g_object_class_install_property
274 g_param_spec_int ("max-characters",
275 "Maximum Characters",
276 "The maximum number of characters the password"
283 g_object_class_install_property
286 g_param_spec_boolean ("get-old",
288 "TRUE if dialog is a get old password dialog, "
289 "FALSE if dialog is a get password dialog",
291 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
293 /* Install private structure */
294 g_type_class_add_private (class, sizeof (HildonGetPasswordDialogPrivate));
298 hildon_get_password_dialog_init (HildonGetPasswordDialog *dialog)
300 /* Set initial properties for the dialog; the actual contents are
301 created once the 'get-old' property is set with g_object_new */
303 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
304 gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
305 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
309 create_contents (HildonGetPasswordDialog *dialog)
311 HildonGetPasswordDialogPrivate *priv;
312 GtkSizeGroup * group;
315 /* Cache private pointer for faster member access */
316 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
319 /* Sizegroup for captions */
320 group = GTK_SIZE_GROUP (gtk_size_group_new
321 (GTK_SIZE_GROUP_HORIZONTAL));
324 gtk_window_set_title (GTK_WINDOW (dialog),
326 ? _(HILDON_GET_PASSWORD_DIALOG_TITLE)
327 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_TITLE));
329 /* Optional password domain label */
330 priv->message_label = GTK_LABEL (gtk_label_new (NULL));
333 gtk_dialog_add_button (GTK_DIALOG (dialog),
335 ? _(HILDON_GET_PASSWORD_DIALOG_OK)
336 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_OK)),
339 gtk_dialog_add_button (GTK_DIALOG (dialog),
341 ? _(HILDON_GET_PASSWORD_DIALOG_CANCEL)
342 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_CANCEL)),
343 GTK_RESPONSE_CANCEL);
345 /* Create password text entry */
346 control = gtk_entry_new ();
347 gtk_entry_set_width_chars (GTK_ENTRY (control), 20);
349 g_object_set (control, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
351 gtk_entry_set_visibility (GTK_ENTRY(control), FALSE);
352 priv->password_entry = HILDON_CAPTION
353 (hildon_caption_new(group,
355 ? _(HILDON_GET_PASSWORD_DIALOG_PASSWORD)
356 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD)),
358 HILDON_CAPTION_OPTIONAL));
359 hildon_caption_set_separator (HILDON_CAPTION (priv->password_entry), "");
361 /* Do the basic layout */
362 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
363 GTK_WIDGET (priv->message_label), FALSE, FALSE, 0);
364 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
365 GTK_WIDGET (priv->password_entry), FALSE, FALSE, 0);
366 gtk_widget_show_all (GTK_DIALOG (dialog)->vbox);
368 /* Ensure group is freed when all its contents have been removed */
369 g_object_unref (group);
373 * hildon_get_password_dialog_get_type:
375 * Returns GType for HildonGetPasswordDialog as produced by
376 * g_type_register_static().
378 * Returns: HildonGetPasswordDialog type
381 hildon_get_password_dialog_get_type (void)
383 static GType dialog_type = 0;
386 static const GTypeInfo dialog_info = {
387 sizeof (HildonGetPasswordDialogClass),
388 NULL, /* base_init */
389 NULL, /* base_finalize */
390 (GClassInitFunc) hildon_get_password_dialog_class_init,
391 NULL, /* class_finalize */
392 NULL, /* class_data */
393 sizeof (HildonGetPasswordDialog),
395 (GInstanceInitFunc) hildon_get_password_dialog_init
398 dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
399 "HildonGetPasswordDialog",
406 * hildon_get_password_dialog_new:
407 * @parent: parent window; can be NULL
408 * @get_old: FALSE creates a new get password dialog and
409 * TRUE creates a new get old password dialog. That is,
410 * if the password to be obtained is the old password,
411 * this parameter is specified TRUE.
413 * Construct a new HildonGetPasswordDialog.
415 * Returns: a new #GtkWidget of type HildonGetPasswordDialog
418 hildon_get_password_dialog_new (GtkWindow *parent,
421 HildonGetPasswordDialog *dialog = g_object_new
422 (HILDON_TYPE_GET_PASSWORD_DIALOG,
423 "get-old", get_old, NULL);
425 if (parent != NULL) {
426 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
429 return (GtkWidget *) dialog;
433 * hildon_get_password_dialog_new_with_default:
434 * @parent: parent window; can be NULL
435 * @password: a default password to be shown in password field
436 * @get_old: FALSE creates a new get password dialog and
437 * TRUE creates a new get old password dialog.That is,
438 * if the password to be obtained is the old password,
439 * this parameter is specified TRUE.
442 * Same as #hildon_get_password_dialog_new but with a default password
445 * Returns: a new #GtkWidget of type HildonGetPasswordDialog
448 hildon_get_password_dialog_new_with_default (GtkWindow *parent,
449 const gchar *password,
454 dialog = hildon_get_password_dialog_new (parent, get_old);
456 if (password != NULL)
457 g_object_set (G_OBJECT (dialog), "password", password, NULL);
459 return GTK_WIDGET (dialog);
463 * hildon_get_password_dialog_get_password:
464 * @dialog: pointer to HildonSetPasswordDialog
466 * Gets the currently entered password.
468 * Returns: current password entered by the user.
471 hildon_get_password_dialog_get_password (HildonGetPasswordDialog *dialog)
476 HildonGetPasswordDialogPrivate *priv;
478 g_return_val_if_fail (HILDON_IS_GET_PASSWORD_DIALOG (dialog), NULL);
479 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
482 /* Retrieve the password entry widget */
483 entry1 = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->password_entry)));
484 text1 = GTK_ENTRY (entry1)->text;
490 * hildon_get_password_dialog_set_message
491 * @dialog: the dialog
492 * @message: a custom message or some other descriptive text to be set
494 * Sets the optional descriptive text.
497 hildon_get_password_dialog_set_message (HildonGetPasswordDialog *dialog,
498 const gchar *message)
500 HildonGetPasswordDialogPrivate *priv;
502 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG (dialog));
504 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
507 gtk_label_set_text (priv->message_label, message);
512 * hildon_get_password_dialog_set_caption:
513 * @dialog: the dialog
514 * @new_caption: the text to be set as the caption label
516 * Sets the password entry field's neigbouring label.
519 hildon_get_password_dialog_set_caption (HildonGetPasswordDialog *dialog,
520 const gchar *new_caption)
522 HildonGetPasswordDialogPrivate *priv;
524 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG (dialog));
525 g_return_if_fail (new_caption != NULL);
527 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
530 hildon_caption_set_label (priv->password_entry, new_caption);
534 * hildon_get_password_dialog_set_max_characters:
535 * @dialog: the dialog
536 * @max_characters: the maximum number of characters the password dialog
538 * @new_caption: the text to be set as the caption label
540 * sets the maximum number of characters allowed as the password
543 hildon_get_password_dialog_set_max_characters (HildonGetPasswordDialog *dialog,
546 HildonGetPasswordDialogPrivate *priv;
548 g_return_if_fail (max_characters > 0);
549 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG (dialog));
551 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE (dialog);
554 /* Apply the given length to password entry */
555 gtk_entry_set_max_length (GTK_ENTRY
557 GTK_BIN ((priv->password_entry))),
560 /* FIXME There is a bug here -- the prev. signal needs to be
561 * disconnected before connecting the new signal. Besides, this
562 * should go into the constructor */
564 /* Connect callback to show error banner if the limit is exceeded */
565 g_signal_connect (GTK_ENTRY
567 GTK_BIN ((priv->password_entry))),
569 G_CALLBACK (invalid_input),
574 /* Callback that gets called when maximum chars is reached in the entry */
576 invalid_input (GtkWidget *widget,
577 GtkInvalidInputType reason,
580 if (reason == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
582 hildon_banner_show_information (widget, NULL, _(HILDON_GET_PASSWORD_DIALOG_MAX_CHARS));