[gnome-disk-utility/udisks2-port] Add a password strength widget and use it
- From: David Zeuthen <davidz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-disk-utility/udisks2-port] Add a password strength widget and use it
- Date: Mon, 5 Dec 2011 22:21:35 +0000 (UTC)
commit 3326895282d30e73493f12ff200b29ea50efda6a
Author: David Zeuthen <davidz redhat com>
Date: Mon Dec 5 17:21:04 2011 -0500
Add a password strength widget and use it
Signed-off-by: David Zeuthen <davidz redhat com>
data/ui/filesystem-create.ui | 60 +++++--
src/palimpsest/Makefile.am | 1 +
src/palimpsest/gducreatefilesystemwidget.c | 13 ++
src/palimpsest/gdupasswordstrengthwidget.c | 273 ++++++++++++++++++++++++++++
src/palimpsest/gdupasswordstrengthwidget.h | 42 +++++
src/palimpsest/gdutypes.h | 3 +
6 files changed, 379 insertions(+), 13 deletions(-)
---
diff --git a/data/ui/filesystem-create.ui b/data/ui/filesystem-create.ui
index 3bd8249..abf38a7 100644
--- a/data/ui/filesystem-create.ui
+++ b/data/ui/filesystem-create.ui
@@ -170,6 +170,46 @@
</packing>
</child>
<child>
+ <object class="GtkComboBox" id="type-combobox">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="id_column">0</property>
+ </object>
+ <packing>
+ <property name="left_attach">1</property>
+ <property name="top_attach">0</property>
+ <property name="width">1</property>
+ <property name="height">1</property>
+ </packing>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
+ <placeholder/>
+ </child>
+ <child>
<object class="GtkCheckButton" id="show-passphrase-checkbutton">
<property name="label" translatable="yes">_Show Passphrases</property>
<property name="visible">True</property>
@@ -186,33 +226,27 @@
</object>
<packing>
<property name="left_attach">1</property>
- <property name="top_attach">5</property>
+ <property name="top_attach">6</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
- <object class="GtkComboBox" id="type-combobox">
+ <object class="GtkBox" id="passphrase-strength-box">
<property name="visible">True</property>
<property name="can_focus">False</property>
- <property name="id_column">0</property>
+ <property name="no_show_all">True</property>
+ <child>
+ <placeholder/>
+ </child>
</object>
<packing>
<property name="left_attach">1</property>
- <property name="top_attach">0</property>
+ <property name="top_attach">5</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
- <child>
- <placeholder/>
- </child>
- <child>
- <placeholder/>
- </child>
- <child>
- <placeholder/>
- </child>
</object>
</child>
</object>
diff --git a/src/palimpsest/Makefile.am b/src/palimpsest/Makefile.am
index f834b5b..d15096e 100644
--- a/src/palimpsest/Makefile.am
+++ b/src/palimpsest/Makefile.am
@@ -43,6 +43,7 @@ palimpsest_SOURCES = \
gduformatdiskdialog.h gduformatdiskdialog.c \
gducreatediskimagedialog.h gducreatediskimagedialog.c \
gdurestorediskimagedialog.h gdurestorediskimagedialog.c \
+ gdupasswordstrengthwidget.h gdupasswordstrengthwidget.c \
$(enum_built_sources) \
$(NULL)
diff --git a/src/palimpsest/gducreatefilesystemwidget.c b/src/palimpsest/gducreatefilesystemwidget.c
index f34c2ae..8ba59a8 100644
--- a/src/palimpsest/gducreatefilesystemwidget.c
+++ b/src/palimpsest/gducreatefilesystemwidget.c
@@ -31,6 +31,7 @@
#include "gduapplication.h"
#include "gduwindow.h"
#include "gducreatefilesystemwidget.h"
+#include "gdupasswordstrengthwidget.h"
typedef struct _GduCreateFilesystemWidgetClass GduCreateFilesystemWidgetClass;
struct _GduCreateFilesystemWidget
@@ -53,6 +54,8 @@ struct _GduCreateFilesystemWidget
GtkWidget *confirm_passphrase_label;
GtkWidget *confirm_passphrase_entry;
GtkWidget *show_passphrase_checkbutton;
+ GtkWidget *passphrase_strengh_box;
+ GtkWidget *passphrase_strengh_widget;
gchar *fstype;
gchar *name;
@@ -220,6 +223,8 @@ update (GduCreateFilesystemWidget *widget)
has_info = TRUE;
}
}
+ gdu_password_strength_widget_set_password (GDU_PASSWORD_STRENGTH_WIDGET (widget->passphrase_strengh_widget),
+ gtk_entry_get_text (GTK_ENTRY (widget->passphrase_entry)));
}
else if (g_strcmp0 (id, "custom") == 0)
{
@@ -271,6 +276,7 @@ update (GduCreateFilesystemWidget *widget)
gtk_widget_show (widget->confirm_passphrase_label);
gtk_widget_show (widget->confirm_passphrase_entry);
gtk_widget_show (widget->show_passphrase_checkbutton);
+ gtk_widget_show (widget->passphrase_strengh_box);
}
else
{
@@ -279,6 +285,7 @@ update (GduCreateFilesystemWidget *widget)
gtk_widget_hide (widget->confirm_passphrase_label);
gtk_widget_hide (widget->confirm_passphrase_entry);
gtk_widget_hide (widget->show_passphrase_checkbutton);
+ gtk_widget_hide (widget->passphrase_strengh_box);
}
/* update local widget state for our users */
@@ -490,6 +497,12 @@ gdu_create_filesystem_widget_constructed (GObject *object)
g_signal_connect (widget->confirm_passphrase_entry, "notify::text", G_CALLBACK (on_property_changed), widget);
widget->show_passphrase_checkbutton = GTK_WIDGET (gtk_builder_get_object (widget->builder, "show-passphrase-checkbutton"));
g_signal_connect (widget->show_passphrase_checkbutton, "notify::active", G_CALLBACK (on_property_changed), widget);
+ widget->passphrase_strengh_box = GTK_WIDGET (gtk_builder_get_object (widget->builder, "passphrase-strength-box"));
+ widget->passphrase_strengh_widget = gdu_password_strength_widget_new ();
+ gtk_widget_set_tooltip_markup (widget->passphrase_strengh_widget,
+ _("The strength of the passphrase"));
+ gtk_box_pack_start (GTK_BOX (widget->passphrase_strengh_box), widget->passphrase_strengh_widget,
+ TRUE, TRUE, 0);
/* reparent and nuke the dummy window */
gtk_widget_reparent (widget->grid, GTK_WIDGET (widget));
diff --git a/src/palimpsest/gdupasswordstrengthwidget.c b/src/palimpsest/gdupasswordstrengthwidget.c
new file mode 100644
index 0000000..a452e3c
--- /dev/null
+++ b/src/palimpsest/gdupasswordstrengthwidget.c
@@ -0,0 +1,273 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2008-2011 Red Hat, Inc.
+ *
+ * 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 of the License, or (at your option) 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
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: David Zeuthen <davidz redhat com>
+ */
+
+#include "config.h"
+#include <glib/gi18n-lib.h>
+
+#include <math.h>
+#include <gdk/gdkkeysyms.h>
+#include <gdk/gdkx.h>
+#include <stdlib.h>
+
+#include "gdupasswordstrengthwidget.h"
+
+typedef struct _GduPasswordStrengthWidgetClass GduPasswordStrengthWidgetClass;
+
+struct _GduPasswordStrengthWidget
+{
+ GtkHBox parent;
+
+ GtkWidget *progress_bar;
+ GtkWidget *notebook;
+
+ gchar *password;
+};
+
+struct _GduPasswordStrengthWidgetClass
+{
+ GtkHBoxClass parent_class;
+};
+
+enum
+{
+ PROP_0,
+ PROP_PASSWORD,
+};
+
+G_DEFINE_TYPE (GduPasswordStrengthWidget, gdu_password_strength_widget, GTK_TYPE_HBOX)
+
+static void
+gdu_password_strength_widget_finalize (GObject *object)
+{
+ GduPasswordStrengthWidget *widget = GDU_PASSWORD_STRENGTH_WIDGET (object);
+
+ g_free (widget->password);
+
+ G_OBJECT_CLASS (gdu_password_strength_widget_parent_class)->finalize (object);
+}
+
+static void
+gdu_password_strength_widget_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GduPasswordStrengthWidget *widget = GDU_PASSWORD_STRENGTH_WIDGET (object);
+
+ switch (property_id)
+ {
+ case PROP_PASSWORD:
+ g_value_set_string (value, widget->password);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+gdu_password_strength_widget_set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GduPasswordStrengthWidget *widget = GDU_PASSWORD_STRENGTH_WIDGET (object);
+
+ switch (property_id)
+ {
+ case PROP_PASSWORD:
+ widget->password = g_value_dup_string (value);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+/* TODO: probably do something more sophisticated here */
+
+/* This code is based on the Master Password dialog in Firefox
+ * (pref-masterpass.js)
+ * Original code triple-licensed under the MPL, GPL, and LGPL
+ * so is license-compatible with this file
+ */
+static gdouble
+compute_password_strength (const gchar *password)
+{
+ gint length;
+ gint upper, lower, digit, misc;
+ gint i;
+ gdouble strength;
+
+ length = strlen (password);
+ upper = 0;
+ lower = 0;
+ digit = 0;
+ misc = 0;
+
+ for (i = 0; i < length ; i++)
+ {
+ if (g_ascii_isdigit (password[i]))
+ digit++;
+ else if (g_ascii_islower (password[i]))
+ lower++;
+ else if (g_ascii_isupper (password[i]))
+ upper++;
+ else
+ misc++;
+ }
+
+ if (length > 5)
+ length = 5;
+
+ if (digit > 3)
+ digit = 3;
+
+ if (upper > 3)
+ upper = 3;
+
+ if (misc > 3)
+ misc = 3;
+
+ strength = ((length * 0.1) - 0.2) +
+ (digit * 0.1) +
+ (misc * 0.15) +
+ (upper * 0.1);
+
+ strength = CLAMP (strength, 0.0, 1.0);
+
+ return strength;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static const gchar *strengths[4] =
+{
+ N_("Weak"),
+ N_("Fair"),
+ N_("Good"),
+ N_("Strong"),
+};
+
+#define NUM_STRENGTH_LABELS 4
+
+static void
+update (GduPasswordStrengthWidget *widget)
+{
+ gdouble strength;
+ gint tab_num;
+
+ if (widget->password != NULL)
+ strength = compute_password_strength (widget->password);
+ else
+ strength = 0.0;
+
+ g_warn_if_fail (strength >= 0.0 && strength <= 1.0);
+
+ tab_num = (gint) floor (NUM_STRENGTH_LABELS * strength);
+ if (tab_num < 0)
+ tab_num = 0;
+ if (tab_num > NUM_STRENGTH_LABELS - 1)
+ tab_num = NUM_STRENGTH_LABELS - 1;
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (widget->notebook), tab_num);
+ gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (widget->progress_bar), strength);
+}
+
+
+static void
+gdu_password_strength_widget_constructed (GObject *object)
+{
+ GduPasswordStrengthWidget *widget = GDU_PASSWORD_STRENGTH_WIDGET (object);
+ guint n;
+
+ gtk_box_set_spacing (GTK_BOX (widget), 6);
+
+ widget->progress_bar = gtk_progress_bar_new ();
+ gtk_box_pack_start (GTK_BOX (widget), widget->progress_bar, TRUE, TRUE, 0);
+
+ widget->notebook = gtk_notebook_new ();
+ gtk_notebook_set_show_tabs (GTK_NOTEBOOK (widget->notebook), FALSE);
+ gtk_box_pack_start (GTK_BOX (widget), widget->notebook, FALSE, TRUE, 0);
+
+ for (n = 0; n < NUM_STRENGTH_LABELS; n++)
+ {
+ GtkWidget *label;
+ gchar *s;
+ label = gtk_label_new (NULL);
+ gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
+ s = g_strdup_printf ("<small>%s</small>", gettext (strengths[n]));
+ gtk_label_set_markup (GTK_LABEL (label), s);
+ g_free (s);
+ gtk_notebook_append_page (GTK_NOTEBOOK (widget->notebook), label, NULL);
+ }
+
+ gtk_widget_show_all (GTK_WIDGET (widget));
+
+ update (widget);
+
+ if (G_OBJECT_CLASS (gdu_password_strength_widget_parent_class)->constructed != NULL)
+ G_OBJECT_CLASS (gdu_password_strength_widget_parent_class)->constructed (object);
+}
+
+static void
+gdu_password_strength_widget_class_init (GduPasswordStrengthWidgetClass *klass)
+{
+ GObjectClass *gobject_class;
+
+ gobject_class = G_OBJECT_CLASS (klass);
+ gobject_class->get_property = gdu_password_strength_widget_get_property;
+ gobject_class->set_property = gdu_password_strength_widget_set_property;
+ gobject_class->finalize = gdu_password_strength_widget_finalize;
+ gobject_class->constructed = gdu_password_strength_widget_constructed;
+
+ g_object_class_install_property (gobject_class, PROP_PASSWORD,
+ g_param_spec_string ("password", NULL, NULL,
+ NULL,
+ G_PARAM_READABLE |
+ G_PARAM_STATIC_STRINGS));
+}
+
+static void
+gdu_password_strength_widget_init (GduPasswordStrengthWidget *widget)
+{
+}
+
+GtkWidget *
+gdu_password_strength_widget_new (void)
+{
+ return GTK_WIDGET (g_object_new (GDU_TYPE_PASSWORD_STRENGTH_WIDGET,
+ NULL));
+}
+
+void
+gdu_password_strength_widget_set_password (GduPasswordStrengthWidget *widget,
+ const gchar *password)
+{
+ g_return_if_fail (GDU_IS_PASSWORD_STRENGTH_WIDGET (widget));
+ g_free (widget->password);
+ widget->password = g_strdup (password);
+ update (widget);
+}
diff --git a/src/palimpsest/gdupasswordstrengthwidget.h b/src/palimpsest/gdupasswordstrengthwidget.h
new file mode 100644
index 0000000..9d03e47
--- /dev/null
+++ b/src/palimpsest/gdupasswordstrengthwidget.h
@@ -0,0 +1,42 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2008-2011 Red Hat, Inc.
+ *
+ * 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 of the License, or (at your option) 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
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: David Zeuthen <davidz redhat com>
+ */
+
+#ifndef __GDU_PASSWORD_STRENGTH_WIDGET_H__
+#define __GDU_PASSWORD_STRENGTH_WIDGET_H__
+
+#include <gtk/gtk.h>
+#include "gdutypes.h"
+
+G_BEGIN_DECLS
+
+#define GDU_TYPE_PASSWORD_STRENGTH_WIDGET gdu_password_strength_widget_get_type()
+#define GDU_PASSWORD_STRENGTH_WIDGET(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GDU_TYPE_PASSWORD_STRENGTH_WIDGET, GduPasswordStrengthWidget))
+#define GDU_IS_PASSWORD_STRENGTH_WIDGET(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GDU_TYPE_PASSWORD_STRENGTH_WIDGET))
+
+GType gdu_password_strength_widget_get_type (void) G_GNUC_CONST;
+GtkWidget* gdu_password_strength_widget_new (void);
+void gdu_password_strength_widget_set_password (GduPasswordStrengthWidget *widget,
+ const gchar *password);
+
+G_END_DECLS
+
+#endif /* __GDU_PASSWORD_STRENGTH_WIDGET_H__ */
diff --git a/src/palimpsest/gdutypes.h b/src/palimpsest/gdutypes.h
index e2987a3..588b206 100644
--- a/src/palimpsest/gdutypes.h
+++ b/src/palimpsest/gdutypes.h
@@ -46,6 +46,9 @@ typedef struct _GduVolumeGrid GduVolumeGrid;
struct _GduCreateFilesystemWidget;
typedef struct _GduCreateFilesystemWidget GduCreateFilesystemWidget;
+struct _GduPasswordStrengthWidget;
+typedef struct _GduPasswordStrengthWidget GduPasswordStrengthWidget;
+
G_END_DECLS
#endif /* __GDU_TYPES_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]