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
35 #include "gtk-infoprint.h"
36 #include "hildon-input-mode-hint.h"
38 #include <hildon-widgets/hildon-caption.h>
39 #include <hildon-widgets/hildon-get-password-dialog.h>
46 #define _(String) dgettext(PACKAGE, String)
48 static GtkDialogClass * parent_class;
50 typedef struct _HildonGetPasswordDialogPrivate
51 HildonGetPasswordDialogPrivate;
53 struct _HildonGetPasswordDialogPrivate {
55 GtkButton *cancelButton;
57 GtkLabel *domainLabel;
58 HildonCaption *passwordEntry;
63 #define HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(obj) \
64 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
65 HILDON_TYPE_GET_PASSWORD_DIALOG, HildonGetPasswordDialogPrivate));
68 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass *
70 static void hildon_get_password_dialog_init(HildonGetPasswordDialog *
72 static void hildon_get_password_set_property(GObject * object,
76 static void hildon_get_password_get_property(GObject * object,
77 guint prop_id, GValue * value,
79 static void create_contents(HildonGetPasswordDialog *dialog);
80 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason,
93 /* Private functions */
95 hildon_get_password_set_property(GObject * object,
97 const GValue * value, GParamSpec * pspec)
99 HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
100 HildonGetPasswordDialogPrivate *priv;
102 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
106 /* Set label text representing password domain */
107 gtk_label_set_text(priv->domainLabel, g_value_get_string(value));
110 gtk_entry_set_text(GTK_ENTRY
111 (gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
112 g_value_get_string(value));
114 case PROP_NUMBERS_ONLY:
115 /* Set input mode for the password entry */
116 g_object_set(G_OBJECT(gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
118 (g_value_get_boolean(value)
119 ? HILDON_INPUT_MODE_HINT_NUMERIC
120 : HILDON_INPUT_MODE_HINT_ALPHANUMERICSPECIAL),
123 case PROP_CAPTION_LABEL:
124 hildon_get_password_dialog_set_caption(dialog, g_value_get_string(value));
127 hildon_get_password_dialog_set_max_characters(dialog,
128 g_value_get_int(value));
131 priv->get_old = g_value_get_boolean(value);
132 create_contents(dialog);
135 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
141 hildon_get_password_get_property(GObject * object,
143 GValue * value, GParamSpec * pspec)
145 HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
146 HildonGetPasswordDialogPrivate *priv;
151 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
155 string = gtk_label_get_text(priv->domainLabel);
156 g_value_set_string(value, string);
159 string = gtk_entry_get_text(GTK_ENTRY(priv->passwordEntry));
160 g_value_set_string(value, string);
162 case PROP_NUMBERS_ONLY:
163 /* This property is set if and only if the input mode
164 of the password entry has been set to numeric only */
165 g_object_get(G_OBJECT(gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
166 "input-mode", &input_mode);
167 g_value_set_boolean(value,
168 (input_mode == HILDON_INPUT_MODE_HINT_NUMERIC));
170 case PROP_CAPTION_LABEL:
171 string = hildon_caption_get_label(priv->passwordEntry);
172 g_value_set_string(value, string);
175 max_length = gtk_entry_get_max_length(
176 GTK_ENTRY (hildon_caption_get_control (priv->passwordEntry)));
177 g_value_set_int(value, max_length);
180 g_value_set_boolean(value, priv->get_old);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
189 hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
192 GObjectClass *object_class = G_OBJECT_CLASS(class);
194 parent_class = g_type_class_peek_parent(class);
196 /* Override virtual functions */
197 object_class->set_property = hildon_get_password_set_property;
198 object_class->get_property = hildon_get_password_get_property;
200 /* Install new properties */
201 g_object_class_install_property
204 g_param_spec_string ("domain",
206 "Set domain(content) for optional label.",
210 g_object_class_install_property
213 g_param_spec_string ("password",
215 "Set content for password entry",
219 g_object_class_install_property
222 g_param_spec_boolean ("numbers_only",
224 "Set entry to accept only numeric values",
228 g_object_class_install_property
231 g_param_spec_string ("caption-label",
233 "The text to be set as the caption label",
237 g_object_class_install_property
240 g_param_spec_int ("max-characters",
241 "Maximum Characters",
242 "The maximum number of characters the password"
249 g_object_class_install_property
252 g_param_spec_boolean ("get-old",
254 "TRUE if dialog is a get old password dialog, "
255 "FALSE if dialog is a get password dialog",
257 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
259 /* Install private structure */
260 g_type_class_add_private(class,
261 sizeof(HildonGetPasswordDialogPrivate));
265 hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
267 /* Set initial properties for the dialog; the actual contents are
268 created once the 'get-old' property is set with g_object_new */
269 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
270 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
271 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
275 create_contents(HildonGetPasswordDialog *dialog)
277 HildonGetPasswordDialogPrivate *priv;
278 GtkSizeGroup * group;
281 /* Cache private pointer for faster member access */
282 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
284 /* Sizegroup for captions */
285 group = GTK_SIZE_GROUP(gtk_size_group_new
286 (GTK_SIZE_GROUP_HORIZONTAL));
289 gtk_window_set_title(GTK_WINDOW(dialog),
291 ? _(HILDON_GET_PASSWORD_DIALOG_TITLE)
292 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_TITLE));
294 /* Optional password domain label */
295 priv->domainLabel = GTK_LABEL(gtk_label_new(NULL));
299 GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
301 ? _(HILDON_GET_PASSWORD_DIALOG_OK)
302 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_OK)),
306 GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
308 ? _(HILDON_GET_PASSWORD_DIALOG_CANCEL)
309 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_CANCEL)),
310 GTK_RESPONSE_CANCEL));
312 /* Create password text entry */
313 control = gtk_entry_new();
314 gtk_entry_set_visibility(GTK_ENTRY(control), FALSE);
315 priv->passwordEntry = HILDON_CAPTION
316 (hildon_caption_new(group,
318 ? _(HILDON_GET_PASSWORD_DIALOG_PASSWORD)
319 : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD)),
321 HILDON_CAPTION_OPTIONAL));
322 hildon_caption_set_separator(HILDON_CAPTION(priv->passwordEntry), "");
324 /* Do the basic layout */
325 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
326 GTK_WIDGET(priv->domainLabel), FALSE, FALSE, 0);
327 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
328 GTK_WIDGET(priv->passwordEntry), FALSE, FALSE, 0);
329 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
331 /* Ensure group is freed when all its contents have been removed */
332 g_object_unref(group);
336 /* Public functions */
339 * hildon_get_password_dialog_get_type:
341 * Returns GType for HildonGetPasswordDialog as produced by
342 * g_type_register_static().
344 * Return value: HildonGetPasswordDialog type
346 GType hildon_get_password_dialog_get_type(void)
348 static GType dialog_type = 0;
351 static const GTypeInfo dialog_info = {
352 sizeof(HildonGetPasswordDialogClass),
353 NULL, /* base_init */
354 NULL, /* base_finalize */
355 (GClassInitFunc) hildon_get_password_dialog_class_init,
356 NULL, /* class_finalize */
357 NULL, /* class_data */
358 sizeof(HildonGetPasswordDialog),
360 (GInstanceInitFunc) hildon_get_password_dialog_init
363 dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
364 "HildonGetPasswordDialog",
371 * hildon_get_password_dialog_new:
372 * @parent: parent window; can be NULL
373 * @get_old_password_title: FALSE creates a new get password dialog and
374 * TRUE creates a new get old password dialog
376 * Construct a new HildonGetPasswordDialog.
378 * Return value: a new #GtkWidget of type HildonGetPasswordDialog
380 GtkWidget *hildon_get_password_dialog_new(GtkWindow * parent,
381 gboolean get_old_password_title)
383 HildonGetPasswordDialog *dialog = g_object_new
384 (HILDON_TYPE_GET_PASSWORD_DIALOG,
385 "get-old", get_old_password_title, NULL);
387 if (parent != NULL) {
388 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
391 return GTK_WIDGET(dialog);
395 * hildon_get_password_dialog_new:
396 * @parent: parent window; can be NULL
397 * @password: a default password to be shown in password field.
398 * @get_old_password_title: FALSE creates a new get password dialog and
399 * TRUE creates a new get old password dialog
401 * Same as #hildon_get_password_dialog_new but with a default password
404 * Return value: a new #GtkWidget of type HildonGetPasswordDialog
406 GtkWidget *hildon_get_password_dialog_new_with_default (GtkWindow * parent,
407 const gchar *password,
408 gboolean get_old_password_title)
412 dialog = hildon_get_password_dialog_new(parent, get_old_password_title);
414 g_object_set(G_OBJECT(dialog), "password", password, NULL);
416 return GTK_WIDGET(dialog);
420 * hildon_get_password_dialog_get_password:
421 * @dialog: pointer to HildonSetPasswordDialog
423 * Gets the currently inputted password.
425 * Return value: current password ( if the dialog is successfully
426 * accepted with 'OK' )
429 *hildon_get_password_dialog_get_password(HildonGetPasswordDialog *
435 HildonGetPasswordDialogPrivate *priv;
437 g_return_val_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog), NULL);
438 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
440 /* Retrieve the password entry widget */
441 entry1 = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(
442 priv->passwordEntry)));
444 text1 = GTK_ENTRY(entry1)->text;
450 * hildon_get_password_dialog_set_domain(GtkWidget *dialog,
451 * @dialog: the dialog
452 * @domain: the domain or some other descriptive text to be set.
454 * sets the optional descriptive text
457 void hildon_get_password_dialog_set_domain(HildonGetPasswordDialog *dialog,
460 HildonGetPasswordDialogPrivate *priv;
462 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
464 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
465 gtk_label_set_text(priv->domainLabel, domain);
470 * hildon_get_password_dialog_set_title:
471 * @dialog: the dialog
472 * @new_title: the text to be set as the dialog title.
474 * sets the dialog title
476 void hildon_get_password_dialog_set_title(HildonGetPasswordDialog *dialog,
477 const gchar *new_title)
480 /* FIXME: This method is completely useless, should be deprecated/removed */
481 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
482 g_return_if_fail (new_title !=NULL);
483 gtk_window_set_title(GTK_WINDOW(dialog),
488 * hildon_get_password_dialog_set_caption:
489 * @dialog: the dialog
490 * @new_caption: the text to be set as the caption label.
492 * sets the password entry field's neigbouring label.
495 void hildon_get_password_dialog_set_caption(HildonGetPasswordDialog *dialog,
496 const gchar *new_caption)
500 HildonGetPasswordDialogPrivate *priv;
502 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
503 g_return_if_fail (new_caption != NULL);
505 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
506 hildon_caption_set_label(priv->passwordEntry, new_caption);
511 * hildon_get_password_dialog_set_max_characters:
512 * @dialog: the dialog
513 * @max_characters: the maximum number of characters the password dialog
516 * sets the maximum number of characters allowed as the password
519 void hildon_get_password_dialog_set_max_characters (HildonGetPasswordDialog *dialog, gint max_characters )
521 HildonGetPasswordDialogPrivate *priv;
523 g_return_if_fail (max_characters > 0);
524 g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
526 priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
528 /* Apply the given length to password entry */
529 gtk_entry_set_max_length(GTK_ENTRY
530 (hildon_caption_get_control
531 (priv->passwordEntry)),
534 /* Connect callback to show error banner if the limit is exceeded */
535 g_signal_connect(GTK_ENTRY
536 (hildon_caption_get_control
537 (priv->passwordEntry)),
539 G_CALLBACK(_invalid_input),
544 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason,
547 if (reason==GTK_INVALID_INPUT_MAX_CHARS_REACHED) {
548 gtk_infoprint(GTK_WINDOW(widget), _(HILDON_GET_PASSWORD_DIALOG_MAX_CHARS));