2006-09-12 Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
[hildon] / hildon-widgets / hildon-get-password-dialog.c
index f672a99..c4c952f 100644 (file)
@@ -1,14 +1,14 @@
 /*
  * This file is part of hildon-libs
  *
- * Copyright (C) 2005 Nokia Corporation.
+ * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
  *
- * Contact: Luc Pionchon <luc.pionchon@nokia.com>
+ * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License or any later version.
  *
  * This library is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  *
  */
 
+/**
+ * SECTION:hildon-get-password-dialog
+ * @short_description: A widget used to get a password
+ * @see_also: #HildonSetPasswordDialog
+ * 
+ * HildonGetPasswordDialog prompts the user for a password.  It allows
+ * inputting password, with an optional configurable label eg. for
+ * showing the domain. The maximum length of the password can be set.
+ */
+
 #include <glib.h>
 
 #include <errno.h>
@@ -32,9 +42,8 @@
 
 #include <gtk/gtk.h>
 
-/* FIXME: These two includes are broken */
-#include <hildon-lgpl/hildon-widgets/gtk-infoprint.h>
-#include <hildon-lgpl/hildon-widgets/hildon-input-mode-hint.h>
+#include "gtk-infoprint.h"
+#include "hildon-input-mode-hint.h"
 
 #include <hildon-widgets/hildon-caption.h>
 #include <hildon-widgets/hildon-get-password-dialog.h>
@@ -57,6 +66,7 @@ struct _HildonGetPasswordDialogPrivate {
   
   GtkLabel *domainLabel;
   HildonCaption *passwordEntry;
+  gboolean get_old;
 };
 
 
@@ -76,11 +86,9 @@ static void hildon_get_password_set_property(GObject * object,
 static void hildon_get_password_get_property(GObject * object,
                                              guint prop_id, GValue * value,
                                              GParamSpec * pspec);
-void hildon_get_password_dialog_set_domain(HildonGetPasswordDialog *dialog, 
-                                          gchar *domain);
-
+static void create_contents(HildonGetPasswordDialog *dialog);
 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason, 
-                          gpointer user_data);
+                          gpointer unused);
 
 enum{
     PROP_NONE = 0,
@@ -88,7 +96,8 @@ enum{
     PROP_PASSWORD,
     PROP_NUMBERS_ONLY,
     PROP_CAPTION_LABEL,
-    PROP_MAX_CHARS
+    PROP_MAX_CHARS,
+    PROP_GET_OLD
 };
 
 /* Private functions */
@@ -113,18 +122,24 @@ hildon_get_password_set_property(GObject * object,
                       g_value_get_string(value));
     break;
   case PROP_NUMBERS_ONLY:
-    /* FIXME: This is broken, property value is not used in any way */
-    g_object_set( G_OBJECT
-                 (gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
-                  "input-mode", HILDON_INPUT_MODE_HINT_NUMERIC, NULL );
+    /* Set input mode for the password entry */
+    g_object_set(G_OBJECT(gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
+                "input-mode",
+                (g_value_get_boolean(value)
+                 ? HILDON_INPUT_MODE_HINT_NUMERIC
+                 : HILDON_INPUT_MODE_HINT_ALPHANUMERICSPECIAL),
+                NULL);
     break;
   case PROP_CAPTION_LABEL:
     hildon_get_password_dialog_set_caption(dialog, g_value_get_string(value));
     break;
   case PROP_MAX_CHARS:
-    /* FIXME: This is broken. set_max_characters wants an int, not string */
     hildon_get_password_dialog_set_max_characters(dialog, 
-                                                 g_value_get_string(value));
+                                                 g_value_get_int(value));
+    break;
+  case PROP_GET_OLD:
+    priv->get_old = g_value_get_boolean(value);
+    create_contents(dialog);
     break;
   default:
     G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
@@ -140,7 +155,8 @@ hildon_get_password_get_property(GObject * object,
     HildonGetPasswordDialog *dialog = HILDON_GET_PASSWORD_DIALOG(object);
     HildonGetPasswordDialogPrivate *priv;
     const gchar *string;
-    gint max_length; 
+    gint max_length;
+    gint input_mode;
 
     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
     
@@ -148,10 +164,19 @@ hildon_get_password_get_property(GObject * object,
     case PROP_DOMAIN:
       string = gtk_label_get_text(priv->domainLabel);
       g_value_set_string(value, string);
+      break;
     case PROP_PASSWORD:
       string = gtk_entry_get_text(GTK_ENTRY(priv->passwordEntry));
       g_value_set_string(value, string);
       break;
+    case PROP_NUMBERS_ONLY:
+      /* This property is set if and only if the input mode
+        of the password entry has been set to numeric only */
+      g_object_get(G_OBJECT(gtk_bin_get_child(GTK_BIN(priv->passwordEntry))),
+                  "input-mode", &input_mode);
+      g_value_set_boolean(value,
+                         (input_mode == HILDON_INPUT_MODE_HINT_NUMERIC));
+      break;
     case PROP_CAPTION_LABEL:
       string = hildon_caption_get_label(priv->passwordEntry);
       g_value_set_string(value, string);
@@ -161,6 +186,9 @@ hildon_get_password_get_property(GObject * object,
              GTK_ENTRY (hildon_caption_get_control (priv->passwordEntry)));
       g_value_set_int(value, max_length);
       break;
+    case PROP_GET_OLD:
+      g_value_set_boolean(value, priv->get_old);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
       break;
@@ -180,7 +208,6 @@ hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
   object_class->get_property = hildon_get_password_get_property;
   
   /* Install new properties */
-  /* FIXME: Why this is not READWRITE */
   g_object_class_install_property 
     (object_class, 
      PROP_DOMAIN, 
@@ -188,7 +215,7 @@ hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
                          "Domain",
                          "Set domain(content) for optional label.",
                          NULL,
-                         G_PARAM_WRITABLE));
+                         G_PARAM_READWRITE));
   
   g_object_class_install_property
     (object_class, 
@@ -199,7 +226,6 @@ hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
                          "DEFAULT",
                          G_PARAM_READWRITE));
 
-  /* FIXME: Why this is not READWRITE?? */
   g_object_class_install_property
     (object_class, 
      PROP_NUMBERS_ONLY,
@@ -207,7 +233,7 @@ hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
                          "NumbersOnly",
                          "Set entry to accept only numeric values",
                          FALSE,
-                         G_PARAM_WRITABLE));
+                         G_PARAM_READWRITE));
 
   g_object_class_install_property
     (object_class, 
@@ -230,6 +256,16 @@ hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
                       0,
                       G_PARAM_READWRITE));
 
+  g_object_class_install_property
+    (object_class,
+     PROP_GET_OLD,
+     g_param_spec_boolean ("get-old",
+                          "Get Old Password",
+                          "TRUE if dialog is a get old password dialog, "
+                          "FALSE if dialog is a get password dialog",
+                          FALSE,
+                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
     /* Install private structure */
     g_type_class_add_private(class,
                              sizeof(HildonGetPasswordDialogPrivate));
@@ -238,22 +274,32 @@ hildon_get_password_dialog_class_init(HildonGetPasswordDialogClass * class)
 static void
 hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
 {
+    /* Set initial properties for the dialog; the actual contents are
+       created once the 'get-old' property is set with g_object_new */
+    gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
+    gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
+    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+}
+
+static void
+create_contents(HildonGetPasswordDialog *dialog)
+{
+    HildonGetPasswordDialogPrivate *priv;
     GtkSizeGroup * group;
     GtkWidget *control;
 
-    /* Cache private pointer for faster member access */  
-    HildonGetPasswordDialogPrivate *priv =
-      HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
+    /* Cache private pointer for faster member access */
+    priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
 
     /* Sizegroup for captions */
     group = GTK_SIZE_GROUP(gtk_size_group_new
                           (GTK_SIZE_GROUP_HORIZONTAL));
 
-    /* Initial properties for the dialog */
-    gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
-    gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
-    gtk_window_set_title(GTK_WINDOW(dialog), 
-                        _(HILDON_GET_PASSWORD_DIALOG_TITLE));
+    /* Dialog title */
+    gtk_window_set_title(GTK_WINDOW(dialog),
+                        priv->get_old
+                        ? _(HILDON_GET_PASSWORD_DIALOG_TITLE)
+                        : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_TITLE));
 
     /* Optional password domain label */
     priv->domainLabel = GTK_LABEL(gtk_label_new(NULL));
@@ -261,11 +307,16 @@ hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
     /* Create buttons */
     priv->okButton =
       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
-                                       _(HILDON_GET_PASSWORD_DIALOG_OK),
+                                      (priv->get_old
+                                       ? _(HILDON_GET_PASSWORD_DIALOG_OK)
+                                       : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_OK)),
+
                                        GTK_RESPONSE_OK));
     priv->cancelButton =
       GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
-                                       _(HILDON_GET_PASSWORD_DIALOG_CANCEL),
+                                       (priv->get_old
+                                       ? _(HILDON_GET_PASSWORD_DIALOG_CANCEL)
+                                       : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_CANCEL)),
                                        GTK_RESPONSE_CANCEL));
 
     /* Create password text entry */
@@ -273,11 +324,12 @@ hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
     gtk_entry_set_visibility(GTK_ENTRY(control), FALSE);
     priv->passwordEntry = HILDON_CAPTION
       (hildon_caption_new(group,
-                         _(HILDON_GET_PASSWORD_DIALOG_PASSWORD ),
+                         (priv->get_old
+                          ? _(HILDON_GET_PASSWORD_DIALOG_PASSWORD)
+                          : _(HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD)),
                          control, NULL,
                          HILDON_CAPTION_OPTIONAL));
     hildon_caption_set_separator(HILDON_CAPTION(priv->passwordEntry), "");
-    
 
     /* Do the basic layout */
     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
@@ -286,11 +338,11 @@ hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
                        GTK_WIDGET(priv->passwordEntry), FALSE, FALSE, 0);
     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
     
-    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
-
-    /* FIXME: Do not leak group */
+    /* Ensure group is freed when all its contents have been removed */
+    g_object_unref(group);
 }
 
+
 /* Public functions */
 
 /**
@@ -299,9 +351,9 @@ hildon_get_password_dialog_init(HildonGetPasswordDialog * dialog)
  * Returns GType for HildonGetPasswordDialog as produced by
  * g_type_register_static().
  *
- * Return value: HildonGetPasswordDialog type
- **/
-GType hildon_get_password_dialog_get_type()
+ * Returns: HildonGetPasswordDialog type
+ */
+GType hildon_get_password_dialog_get_type(void)
 {
     static GType dialog_type = 0;
 
@@ -328,36 +380,21 @@ GType hildon_get_password_dialog_get_type()
 /**
  * hildon_get_password_dialog_new:
  * @parent: parent window; can be NULL
- * @get_old_password_title: FALSE creates a new get password dialog and
- *                     TRUE creates a new get old password dialog 
+ * @get_old: FALSE creates a new get password dialog and
+ *           TRUE creates a new get old password dialog. That is,
+ *           if the password to be obtained is the old password, 
+ *           this parameter is specified TRUE.  
  * 
  * Construct a new HildonGetPasswordDialog.
  *
- * Return value: a new #GtkWidget of type HildonGetPasswordDialog
- **/
+ * Returns: a new #GtkWidget of type HildonGetPasswordDialog
+ */
 GtkWidget *hildon_get_password_dialog_new(GtkWindow * parent,
-                                          gboolean get_old_password_title)
+                                          gboolean get_old)
 {
     HildonGetPasswordDialog *dialog = g_object_new
         (HILDON_TYPE_GET_PASSWORD_DIALOG,
-         NULL);
-
-    if (get_old_password_title == FALSE) {
-      HildonGetPasswordDialogPrivate *priv;
-
-      /* Override "get old password" defaults set in dialog_init */
-      /* FIXME: This behaviour is confusing */
-      gtk_window_set_title(GTK_WINDOW(dialog), 
-                            _(HILDON_GET_PASSWORD_VERIFY_DIALOG_TITLE));
-               priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
-       gtk_button_set_label(priv->okButton, 
-                            _(HILDON_GET_PASSWORD_VERIFY_DIALOG_OK));
-       gtk_button_set_label(priv->cancelButton, 
-                            _(HILDON_GET_PASSWORD_VERIFY_DIALOG_CANCEL));
-       hildon_caption_set_label(priv->passwordEntry,
-                                _(HILDON_GET_PASSWORD_VERIFY_DIALOG_PASSWORD)
-                                );
-    }
+         "get-old", get_old, NULL);
 
     if (parent != NULL) {
         gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
@@ -367,24 +404,27 @@ GtkWidget *hildon_get_password_dialog_new(GtkWindow * parent,
 }
 
 /**
- * hildon_get_password_dialog_new:
+ * hildon_get_password_dialog_new_with_default:
  * @parent: parent window; can be NULL
- * @password: a default password to be shown in password field.
- * @get_old_password_title: FALSE creates a new get password dialog and
- *                     TRUE creates a new get old password dialog 
+ * @password: a default password to be shown in password field
+ * @get_old: FALSE creates a new get password dialog and
+ *           TRUE creates a new get old password dialog.That is,
+ *           if the password to be obtained is the old password,
+ *           this parameter is specified TRUE.
+ *                        
  * 
  * Same as #hildon_get_password_dialog_new but with a default password
  * in password field.
  *
- * Return value: a new #GtkWidget of type HildonGetPasswordDialog
- **/
+ * Returns: a new #GtkWidget of type HildonGetPasswordDialog
+ */
 GtkWidget *hildon_get_password_dialog_new_with_default (GtkWindow * parent,
-                                                       gchar *password,
-                                               gboolean get_old_password_title)
+                                                       const gchar *password,
+                                               gboolean get_old)
 {
     GtkWidget *dialog;
 
-    dialog = hildon_get_password_dialog_new(parent, get_old_password_title);
+    dialog = hildon_get_password_dialog_new(parent, get_old);
     if(password != NULL)
         g_object_set(G_OBJECT(dialog), "password", password, NULL);
 
@@ -397,9 +437,9 @@ GtkWidget *hildon_get_password_dialog_new_with_default (GtkWindow * parent,
  * 
  * Gets the currently inputted password.
  *
- * Return value: current password ( if the dialog is successfully 
+ * Returns: current password ( if the dialog is successfully 
  * accepted with 'OK'  )
- **/
+ */
 const gchar
     *hildon_get_password_dialog_get_password(HildonGetPasswordDialog *
                                              dialog)
@@ -409,6 +449,7 @@ const gchar
 
     HildonGetPasswordDialogPrivate *priv;
 
+    g_return_val_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog), NULL);
     priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
 
     /* Retrieve the password entry widget */
@@ -423,80 +464,84 @@ const gchar
 /**
  * hildon_get_password_dialog_set_domain(GtkWidget *dialog, 
  * @dialog: the dialog
- * @domain: the domain or some other descriptive text to be set.
+ * @domain: the domain or some other descriptive text to be set
  * 
- * sets the optional descriptive text
+ * Sets the optional descriptive text.
  */
 
 void hildon_get_password_dialog_set_domain(HildonGetPasswordDialog *dialog, 
-                                                gchar *domain)
+                                           const gchar *domain)
 {
-  HildonGetPasswordDialogPrivate *priv =
-    HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
+  HildonGetPasswordDialogPrivate *priv;
+
+  g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
+
+  priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
   gtk_label_set_text(priv->domainLabel, domain);
   
 }
 
 /**
- * hildon_get_password_dialog_set_dialog_title
- *        (HildonGetPasswordDialog *dialog, gchar *new_title) 
+ * hildon_get_password_dialog_set_title:
  * @dialog: the dialog
- * @new_ title: the text to be set as the dialog title.
+ * @new_title: the text to be set as the dialog title
  * 
- * sets the dialog title
+ * Sets the dialog title.
+ *
+ * DEPRECATED! use gtk_window_set_title instead.
  */
 void hildon_get_password_dialog_set_title(HildonGetPasswordDialog *dialog,
-                                         gchar *new_title)
+                                         const gchar *new_title)
 
 {
   /* FIXME: This method is completely useless, should be deprecated/removed */
+  g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
   g_return_if_fail (new_title !=NULL);
   gtk_window_set_title(GTK_WINDOW(dialog), 
                       new_title);
 }
 
 /**
- * hildon_get_password_dialog_set_caption
- *        (HildonGetPasswordDialog *dialog, gchar *new_caption) 
+ * hildon_get_password_dialog_set_caption:
  * @dialog: the dialog
- * @new_caption: the text to be set as the caption label.
+ * @new_caption: the text to be set as the caption label
  * 
- * sets the password entry field's neigbouring label.
+ * Sets the password entry field's neigbouring label.
  */
 
-
 void hildon_get_password_dialog_set_caption(HildonGetPasswordDialog *dialog,
-                                                 gchar *new_caption)
+                                           const gchar *new_caption)
 {
   
 
-  HildonGetPasswordDialogPrivate *priv =
-    HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);  
-  g_return_if_fail (new_caption !=NULL);
+  HildonGetPasswordDialogPrivate *priv;
+
+  g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
+  g_return_if_fail (new_caption != NULL);
+
+  priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
   hildon_caption_set_label(priv->passwordEntry, new_caption);
 
 }
 
 /**
- * hildon_get_password_dialog_set_max_characters
- *        (HildonGetPasswordDialog *dialog, gint max_characters,
- *         gchar *error_ib_message) 
+ * hildon_get_password_dialog_set_max_characters:
  * @dialog: the dialog
  * @max_characters: the maximum number of characters the password dialog
- * accepts.
- * @new_caption: the text to be set as the caption label.
+ * accepts
+ * @new_caption: the text to be set as the caption label
  * 
- * sets the password entry field's neigbouring label.
+ * sets the maximum number of characters allowed as the password
  */
 
 void hildon_get_password_dialog_set_max_characters (HildonGetPasswordDialog *dialog, gint max_characters )
 {
+  HildonGetPasswordDialogPrivate *priv;
 
-  HildonGetPasswordDialogPrivate *priv =
-    HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);  
+  g_return_if_fail (max_characters > 0);
+  g_return_if_fail (HILDON_IS_GET_PASSWORD_DIALOG(dialog));
 
-  g_return_if_fail(max_characters >0);
-  g_return_if_fail(dialog);
+  priv = HILDON_GET_PASSWORD_DIALOG_GET_PRIVATE(dialog);
 
   /* Apply the given length to password entry */
   gtk_entry_set_max_length(GTK_ENTRY
@@ -512,14 +557,12 @@ void hildon_get_password_dialog_set_max_characters (HildonGetPasswordDialog *dia
                   G_CALLBACK(_invalid_input),
                   NULL
                   );
-
 }
 
 static void _invalid_input(GtkWidget *widget, GtkInvalidInputType reason, 
-                          gpointer user_data) 
+                          gpointer unused) 
 {
   if (reason==GTK_INVALID_INPUT_MAX_CHARS_REACHED) {
     gtk_infoprint(GTK_WINDOW(widget), _(HILDON_GET_PASSWORD_DIALOG_MAX_CHARS));
   }
-
 }