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 * 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 (gtk_bin_get_child(GTK_BIN(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(gtk_bin_get_child(GTK_BIN(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(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(gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
176 "input-mode", &input_mode);
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_visibility(GTK_ENTRY(control), FALSE);
325 priv->passwordEntry = HILDON_CAPTION
326 (hildon_caption_new(group,
328 ? _(HILDON_GET_PASSWORD_DIALOG_PASSWORD)
329 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD)),
331 HILDON_CAPTION_OPTIONAL));
332 hildon_caption_set_separator(HILDON_CAPTION(priv->passwordEntry), "");
334 /* Do the basic layout */
335 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
336 GTK_WIDGET(priv->domainLabel), FALSE, FALSE, 0);
337 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
338 GTK_WIDGET(priv->passwordEntry), FALSE, FALSE, 0);
339 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
341 /* Ensure group is freed when all its contents have been removed */
342 g_object_unref(group);
346 /* Public functions */
349 * hildon_get_password_dialog_get_type:
351 * Returns GType for HildonGetPasswordDialog as produced by
352 * g_type_register_static().
354 * Returns: HildonGetPasswordDialog type
356 GType hildon_get_password_dialog_get_type(void)
358 static GType dialog_type = 0;
361 static const GTypeInfo dialog_info = {
362 sizeof(HildonGetPasswordDialogClass),
363 NULL, /* base_init */
364 NULL, /* base_finalize */
365 (GClassInitFunc) hildon_get_password_dialog_class_init,
366 NULL, /* class_finalize */
367 NULL, /* class_data */
368 sizeof(HildonGetPasswordDialog),
370 (GInstanceInitFunc) hildon_get_password_dialog_init
373 dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
374 "HildonGetPasswordDialog",
381 * hildon_get_password_dialog_new:
382 * @parent: parent window; can be NULL
383 * @get_old: FALSE creates a new get password dialog and
384 * TRUE creates a new get old password dialog. That is,
385 * if the password to be obtained is the old password,
386 * this parameter is specified TRUE.
388 * Construct a new HildonGetPasswordDialog.
390 * Returns: a new #GtkWidget of type HildonGetPasswordDialog
392 GtkWidget *hildon_get_password_dialog_new(GtkWindow * parent,
395 HildonGetPasswordDialog *dialog = g_object_new
396 (HILDON_TYPE_GET_PASSWORD_DIALOG,
397 "get-old", get_old, NULL);
399 if (parent != NULL) {
400 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
403 return GTK_WIDGET(dialog);
407 * hildon_get_password_dialog_new_with_default:
408 * @parent: parent window; can be NULL
409 * @password: a default password to be shown in password field
410 * @get_old: FALSE creates a new get password dialog and
411 * TRUE creates a new get old password dialog.That is,
412 * if the password to be obtained is the old password,
413 * this parameter is specified TRUE.
416 * Same as #hildon_get_password_dialog_new but with a default password
419 * Returns: a new #GtkWidget of type HildonGetPasswordDialog
421 GtkWidget *hildon_get_password_dialog_new_with_default (GtkWindow * parent,
422 const gchar *password,
427 dialog = hildon_get_password_dialog_new(parent, get_old);
429 g_object_set(G_OBJECT(dialog), "password", password, NULL);
431 return GTK_WIDGET(dialog);
435 * hildon_get_password_dialog_get_password:
436 * @dialog: pointer to HildonSetPasswordDialog
438 * Gets the currently inputted password.
440 * Returns: current password ( if the dialog is successfully
441 * accepted with 'OK' )
444 *hildon_get_password_dialog_get_password(HildonGetPasswordDialog *
450 HildonGetPasswordDialogPrivate *priv;
452 g_return_val_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog), NULL);
453 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
455 /* Retrieve the password entry widget */
456 entry1 = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(
457 priv->passwordEntry)));
459 text1 = GTK_ENTRY(entry1)->text;
465 * hildon_get_password_dialog_set_domain(GtkWidget *dialog,
466 * @dialog: the dialog
467 * @domain: the domain or some other descriptive text to be set
469 * Sets the optional descriptive text.
472 void hildon_get_password_dialog_set_domain(HildonGetPasswordDialog *dialog,
475 HildonGetPasswordDialogPrivate *priv;
477 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
479 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
480 gtk_label_set_text(priv->domainLabel, domain);
485 * hildon_get_password_dialog_set_title:
486 * @dialog: the dialog
487 * @new_title: the text to be set as the dialog title
489 * Sets the dialog title.
491 * DEPRECATED! use gtk_window_set_title instead.
493 void hildon_get_password_dialog_set_title(HildonGetPasswordDialog *dialog,
494 const gchar *new_title)
497 /* FIXME: This method is completely useless, should be deprecated/removed */
498 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
499 g_return_if_fail (new_title !=NULL);
500 gtk_window_set_title(GTK_WINDOW(dialog),
505 * hildon_get_password_dialog_set_caption:
506 * @dialog: the dialog
507 * @new_caption: the text to be set as the caption label
509 * Sets the password entry field's neigbouring label.
512 void hildon_get_password_dialog_set_caption(HildonGetPasswordDialog *dialog,
513 const gchar *new_caption)
517 HildonGetPasswordDialogPrivate *priv;
519 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
520 g_return_if_fail (new_caption != NULL);
522 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
523 hildon_caption_set_label(priv->passwordEntry, new_caption);
528 * hildon_get_password_dialog_set_max_characters:
529 * @dialog: the dialog
530 * @max_characters: the maximum number of characters the password dialog
532 * @new_caption: the text to be set as the caption label
534 * sets the maximum number of characters allowed as the password
537 void hildon_get_password_dialog_set_max_characters (HildonGetPasswordDialog *dialog, gint max_characters )
539 HildonGetPasswordDialogPrivate *priv;
541 g_return_if_fail (max_characters > 0);
542 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
544 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
546 /* Apply the given length to password entry */
547 gtk_entry_set_max_length(GTK_ENTRY
548 (hildon_caption_get_control
549 (priv->passwordEntry)),
552 /* Connect callback to show error banner if the limit is exceeded */
553 g_signal_connect(GTK_ENTRY
554 (hildon_caption_get_control
555 (priv->passwordEntry)),
557 G_CALLBACK(_invalid_input),
562 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason,
565 if (reason==GTK_INVALID_INPUT_MAX_CHARS_REACHED) {
566 gtk_infoprint(GTK_WINDOW(widget), _(HILDON_GET_PASSWORD_DIALOG_MAX_CHARS));