2 * This file is part of hildon-libs
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 the domain. The maximum length of the password can be set.
45 #include "gtk-infoprint.h"
46 #include "hildon-input-mode-hint.h"
48 #include <hildon-widgets/hildon-caption.h>
49 #include <hildon-widgets/hildon-get-password-dialog.h>
56 #define _(String) dgettext(PACKAGE, String)
58 static GtkDialogClass * parent_class;
60 typedef struct _HildonGetPasswordDialogPrivate
61 HildonGetPasswordDialogPrivate;
63 struct _HildonGetPasswordDialogPrivate {
65 GtkButton *cancelButton;
67 GtkLabel *domainLabel;
68 HildonCaption *passwordEntry;
73 #define HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(obj) \
74 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
75 HILDON_TYPE_GET_PASSWORD_DIALOG, HildonGetPasswordDialogPrivate));
78 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass *
80 static void hildon_get_password_dialog_init(HildonGetPasswordDialog *
82 static void hildon_get_password_set_property(GObject * object,
86 static void hildon_get_password_get_property(GObject * object,
87 guint prop_id, GValue * value,
89 static void create_contents(HildonGetPasswordDialog *dialog);
90 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason,
103 /* Private functions */
105 hildon_get_password_set_property(GObject * object,
107 const GValue * value, GParamSpec * pspec)
109 HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
110 HildonGetPasswordDialogPrivate *priv;
112 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
116 /* Set label text representing password domain */
117 gtk_label_set_text(priv->domainLabel, g_value_get_string(value));
120 gtk_entry_set_text(GTK_ENTRY
121 (hildon_caption_get_control (priv->passwordEntry)),
122 g_value_get_string(value));
124 case PROP_NUMBERS_ONLY:
125 /* Set input mode for the password entry */
126 g_object_set(G_OBJECT(hildon_caption_get_control(priv->passwordEntry)),
128 (g_value_get_boolean(value)
129 ? HILDON_INPUT_MODE_HINT_NUMERIC
130 : HILDON_INPUT_MODE_HINT_ALPHANUMERICSPECIAL),
133 case PROP_CAPTION_LABEL:
134 hildon_get_password_dialog_set_caption(dialog, g_value_get_string(value));
137 hildon_get_password_dialog_set_max_characters(dialog,
138 g_value_get_int(value));
141 priv->get_old = g_value_get_boolean(value);
142 create_contents(dialog);
145 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
151 hildon_get_password_get_property(GObject * object,
153 GValue * value, GParamSpec * pspec)
155 HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
156 HildonGetPasswordDialogPrivate *priv;
161 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
165 string = gtk_label_get_text(priv->domainLabel);
166 g_value_set_string(value, string);
169 string = gtk_entry_get_text(GTK_ENTRY (hildon_caption_get_control(priv->passwordEntry)));
170 g_value_set_string(value, string);
172 case PROP_NUMBERS_ONLY:
173 /* This property is set if and only if the input mode
174 of the password entry has been set to numeric only */
175 g_object_get(G_OBJECT(hildon_caption_get_control(priv->passwordEntry)),
176 "input-mode", &input_mode, NULL);
177 g_value_set_boolean(value,
178 (input_mode == HILDON_INPUT_MODE_HINT_NUMERIC));
180 case PROP_CAPTION_LABEL:
181 string = hildon_caption_get_label(priv->passwordEntry);
182 g_value_set_string(value, string);
185 max_length = gtk_entry_get_max_length(
186 GTK_ENTRY (hildon_caption_get_control (priv->passwordEntry)));
187 g_value_set_int(value, max_length);
190 g_value_set_boolean(value, priv->get_old);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
199 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
202 GObjectClass *object_class = G_OBJECT_CLASS(class);
204 parent_class = g_type_class_peek_parent(class);
206 /* Override virtual functions */
207 object_class->set_property = hildon_get_password_set_property;
208 object_class->get_property = hildon_get_password_get_property;
210 /* Install new properties */
211 g_object_class_install_property
214 g_param_spec_string ("domain",
216 "Set domain(content) for optional label.",
220 g_object_class_install_property
223 g_param_spec_string ("password",
225 "Set content for password entry",
229 g_object_class_install_property
232 g_param_spec_boolean ("numbers_only",
234 "Set entry to accept only numeric values",
238 g_object_class_install_property
241 g_param_spec_string ("caption-label",
243 "The text to be set as the caption label",
247 g_object_class_install_property
250 g_param_spec_int ("max-characters",
251 "Maximum Characters",
252 "The maximum number of characters the password"
259 g_object_class_install_property
262 g_param_spec_boolean ("get-old",
264 "TRUE if dialog is a get old password dialog, "
265 "FALSE if dialog is a get password dialog",
267 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
269 /* Install private structure */
270 g_type_class_add_private(class,
271 sizeof(HildonGetPasswordDialogPrivate));
275 hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
277 /* Set initial properties for the dialog; the actual contents are
278 created once the 'get-old' property is set with g_object_new */
279 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
280 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
281 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
285 create_contents(HildonGetPasswordDialog *dialog)
287 HildonGetPasswordDialogPrivate *priv;
288 GtkSizeGroup * group;
291 /* Cache private pointer for faster member access */
292 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
294 /* Sizegroup for captions */
295 group = GTK_SIZE_GROUP(gtk_size_group_new
296 (GTK_SIZE_GROUP_HORIZONTAL));
299 gtk_window_set_title(GTK_WINDOW(dialog),
301 ? _(HILDON_GET_PASSWORD_DIALOG_TITLE)
302 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_TITLE));
304 /* Optional password domain label */
305 priv->domainLabel = GTK_LABEL(gtk_label_new(NULL));
309 GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
311 ? _(HILDON_GET_PASSWORD_DIALOG_OK)
312 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_OK)),
316 GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
318 ? _(HILDON_GET_PASSWORD_DIALOG_CANCEL)
319 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_CANCEL)),
320 GTK_RESPONSE_CANCEL));
322 /* Create password text entry */
323 control = gtk_entry_new();
324 gtk_entry_set_width_chars (GTK_ENTRY (control), 20);
326 g_object_set (control, "hildon-input-mode", HILDON_GTK_INPUT_MODE_FULL, NULL);
327 gtk_entry_set_visibility(GTK_ENTRY(control), FALSE);
328 priv->passwordEntry = HILDON_CAPTION
329 (hildon_caption_new(group,
331 ? _(HILDON_GET_PASSWORD_DIALOG_PASSWORD)
332 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD)),
334 HILDON_CAPTION_OPTIONAL));
335 hildon_caption_set_separator(HILDON_CAPTION(priv->passwordEntry), "");
337 /* Do the basic layout */
338 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
339 GTK_WIDGET(priv->domainLabel), FALSE, FALSE, 0);
340 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
341 GTK_WIDGET(priv->passwordEntry), FALSE, FALSE, 0);
342 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
344 /* Ensure group is freed when all its contents have been removed */
345 g_object_unref(group);
349 /* Public functions */
352 * hildon_get_password_dialog_get_type:
354 * Returns GType for HildonGetPasswordDialog as produced by
355 * g_type_register_static().
357 * Returns: HildonGetPasswordDialog type
359 GType hildon_get_password_dialog_get_type(void)
361 static GType dialog_type = 0;
364 static const GTypeInfo dialog_info = {
365 sizeof(HildonGetPasswordDialogClass),
366 NULL, /* base_init */
367 NULL, /* base_finalize */
368 (GClassInitFunc) hildon_get_password_dialog_class_init,
369 NULL, /* class_finalize */
370 NULL, /* class_data */
371 sizeof(HildonGetPasswordDialog),
373 (GInstanceInitFunc) hildon_get_password_dialog_init
376 dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
377 "HildonGetPasswordDialog",
384 * hildon_get_password_dialog_new:
385 * @parent: parent window; can be NULL
386 * @get_old: FALSE creates a new get password dialog and
387 * TRUE creates a new get old password dialog. That is,
388 * if the password to be obtained is the old password,
389 * this parameter is specified TRUE.
391 * Construct a new HildonGetPasswordDialog.
393 * Returns: a new #GtkWidget of type HildonGetPasswordDialog
395 GtkWidget *hildon_get_password_dialog_new(GtkWindow * parent,
398 HildonGetPasswordDialog *dialog = g_object_new
399 (HILDON_TYPE_GET_PASSWORD_DIALOG,
400 "get-old", get_old, NULL);
402 if (parent != NULL) {
403 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
406 return GTK_WIDGET(dialog);
410 * hildon_get_password_dialog_new_with_default:
411 * @parent: parent window; can be NULL
412 * @password: a default password to be shown in password field
413 * @get_old: FALSE creates a new get password dialog and
414 * TRUE creates a new get old password dialog.That is,
415 * if the password to be obtained is the old password,
416 * this parameter is specified TRUE.
419 * Same as #hildon_get_password_dialog_new but with a default password
422 * Returns: a new #GtkWidget of type HildonGetPasswordDialog
424 GtkWidget *hildon_get_password_dialog_new_with_default (GtkWindow * parent,
425 const gchar *password,
430 dialog = hildon_get_password_dialog_new(parent, get_old);
432 g_object_set(G_OBJECT(dialog), "password", password, NULL);
434 return GTK_WIDGET(dialog);
438 * hildon_get_password_dialog_get_password:
439 * @dialog: pointer to HildonSetPasswordDialog
441 * Gets the currently inputted password.
443 * Returns: current password ( if the dialog is successfully
444 * accepted with 'OK' )
447 *hildon_get_password_dialog_get_password(HildonGetPasswordDialog *
453 HildonGetPasswordDialogPrivate *priv;
455 g_return_val_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog), NULL);
456 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
458 /* Retrieve the password entry widget */
459 entry1 = GTK_ENTRY (hildon_caption_get_control(priv->passwordEntry));
460 text1 = GTK_ENTRY(entry1)->text;
466 * hildon_get_password_dialog_set_domain(GtkWidget *dialog,
467 * @dialog: the dialog
468 * @domain: the domain or some other descriptive text to be set
470 * Sets the optional descriptive text.
473 void hildon_get_password_dialog_set_domain(HildonGetPasswordDialog *dialog,
476 HildonGetPasswordDialogPrivate *priv;
478 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
480 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
481 gtk_label_set_text(priv->domainLabel, domain);
486 * hildon_get_password_dialog_set_title:
487 * @dialog: the dialog
488 * @new_title: the text to be set as the dialog title
490 * Sets the dialog title.
492 * DEPRECATED! use gtk_window_set_title instead.
494 void hildon_get_password_dialog_set_title(HildonGetPasswordDialog *dialog,
495 const gchar *new_title)
498 /* FIXME: This method is completely useless, should be deprecated/removed */
499 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
500 g_return_if_fail (new_title !=NULL);
501 gtk_window_set_title(GTK_WINDOW(dialog),
506 * hildon_get_password_dialog_set_caption:
507 * @dialog: the dialog
508 * @new_caption: the text to be set as the caption label
510 * Sets the password entry field's neigbouring label.
513 void hildon_get_password_dialog_set_caption(HildonGetPasswordDialog *dialog,
514 const gchar *new_caption)
518 HildonGetPasswordDialogPrivate *priv;
520 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
521 g_return_if_fail (new_caption != NULL);
523 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
524 hildon_caption_set_label(priv->passwordEntry, new_caption);
529 * hildon_get_password_dialog_set_max_characters:
530 * @dialog: the dialog
531 * @max_characters: the maximum number of characters the password dialog
533 * @new_caption: the text to be set as the caption label
535 * sets the maximum number of characters allowed as the password
538 void hildon_get_password_dialog_set_max_characters (HildonGetPasswordDialog *dialog, gint max_characters )
540 HildonGetPasswordDialogPrivate *priv;
542 g_return_if_fail (max_characters > 0);
543 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
545 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
547 /* Apply the given length to password entry */
548 gtk_entry_set_max_length(GTK_ENTRY
549 (hildon_caption_get_control
550 (priv->passwordEntry)),
553 /* Connect callback to show error banner if the limit is exceeded */
554 g_signal_connect(GTK_ENTRY
555 (hildon_caption_get_control
556 (priv->passwordEntry)),
558 G_CALLBACK(_invalid_input),
563 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason,
566 if (reason==GTK_INVALID_INPUT_MAX_CHARS_REACHED) {
567 gtk_infoprint(GTK_WINDOW(widget), _(HILDON_GET_PASSWORD_DIALOG_MAX_CHARS));