[gnome-control-center/add-account: 2/5] user-accounts: Add frob-account-dialog test tool



commit 44ba4895e41fd200d0bebf1c8fb55dd2669e4b95
Author: Stef Walter <stefw gnome org>
Date:   Wed May 30 17:49:19 2012 +0200

    user-accounts: Add frob-account-dialog test tool
    
     * Shows the add account dialog without the rest of the
       control center
     * And fix some other bugs

 panels/user-accounts/Makefile.am           |   20 +++
 panels/user-accounts/frob-account-dialog.c |   61 +++++++
 panels/user-accounts/um-account-dialog.c   |  248 +++++++++++++++-------------
 3 files changed, 215 insertions(+), 114 deletions(-)
---
diff --git a/panels/user-accounts/Makefile.am b/panels/user-accounts/Makefile.am
index 4325987..f23d8d8 100644
--- a/panels/user-accounts/Makefile.am
+++ b/panels/user-accounts/Makefile.am
@@ -4,6 +4,8 @@ SUBDIRS = data
 cappletname = user-accounts
 NULL =
 
+noinst_PROGRAMS = frob-account-dialog
+
 ccpanelsdir = $(PANELS_DIR)
 ccpanels_LTLIBRARIES = libuser-accounts.la
 
@@ -69,6 +71,24 @@ endif
 
 libuser_accounts_la_LDFLAGS = $(PANEL_LDFLAGS)
 
+frob_account_dialog_SOURCES = \
+	frob-account-dialog.c \
+	um-account-dialog.h \
+	um-account-dialog.c \
+	um-user.h \
+	um-user.c \
+	um-user-manager.h \
+	um-user-manager.c \
+	um-utils.h \
+	um-utils.c
+
+frob_account_dialog_LDADD = \
+	$(libuser_accounts_la_LIBADD)
+
+frob_account_dialog_CFLAGS = \
+	$(AM_CFLAGS) \
+	-DUIDIR=\""$(pkgdatadir)/ui/user-accounts"\"
+
 CLEANFILES =				\
 	$(BUILT_SOURCES)		\
 	$(NULL)
diff --git a/panels/user-accounts/frob-account-dialog.c b/panels/user-accounts/frob-account-dialog.c
new file mode 100644
index 0000000..619b53c
--- /dev/null
+++ b/panels/user-accounts/frob-account-dialog.c
@@ -0,0 +1,61 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+  *
+  * Copyright (C) 2012 Red Hat, Inc.
+  *
+  * This program is free software; you can redistribute it and/or modify
+  * it under the terms of the GNU General Public License as published by
+  * the Free Software Foundation; either version 2 of the License, or
+  * (at your option) any later version.
+  *
+  * This program 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 General Public License for more details.
+  *
+  * You should have received a copy of the GNU General Public License
+  * along with this program; if not, write to the Free Software
+  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+  */
+
+#include "um-account-dialog.h"
+
+#include <gtk/gtk.h>
+
+static void
+on_dialog_complete (GObject *object,
+                    GAsyncResult *result,
+                    gpointer user_data)
+{
+	GMainLoop *loop = user_data;
+	UmUser *user;
+
+	user = um_account_dialog_finish (UM_ACCOUNT_DIALOG (object), result);
+	if (user == NULL) {
+		g_printerr ("No user created\n");
+	} else {
+		g_printerr ("User created: %s\n", um_user_get_user_name (user));
+		g_object_unref (user);
+	}
+
+	g_main_loop_quit (loop);
+}
+
+int
+main (int argc,
+      char *argv[])
+{
+	UmAccountDialog *dialog;
+	GMainLoop *loop;
+
+	gtk_init (&argc, &argv);
+
+	dialog = um_account_dialog_new ();
+	loop = g_main_loop_new (NULL, FALSE);
+
+	um_account_dialog_perform (dialog, NULL, on_dialog_complete, loop);
+
+	g_main_loop_run (loop);
+	g_main_loop_unref (loop);
+
+	return 0;
+}
diff --git a/panels/user-accounts/um-account-dialog.c b/panels/user-accounts/um-account-dialog.c
index db39638..518b8cb 100644
--- a/panels/user-accounts/um-account-dialog.c
+++ b/panels/user-accounts/um-account-dialog.c
@@ -45,8 +45,8 @@ struct _UmAccountDialog {
 	GtkDialog parent;
 	GtkNotebook *notebook;
 	GSimpleAsyncResult *async;
-	GtkWidget *buttons;
-	gboolean buttons_updating;
+	GtkWidget *bar;
+	gboolean bar_updating;
 
 	/* Local users */
 	GtkWidget *local_login;
@@ -133,6 +133,131 @@ dialog_validate (UmAccountDialog *self)
 }
 
 static void
+label_set_bold (GtkLabel *label,
+                gboolean bold)
+{
+	PangoAttrList *attrs;
+	PangoAttribute *attr;
+
+	attrs = pango_attr_list_new ();
+	attr = pango_attr_weight_new (bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
+	pango_attr_list_insert (attrs, attr);
+	gtk_label_set_attributes (label, attrs);
+	pango_attr_list_unref (attrs);
+}
+
+static void
+bar_update (UmAccountDialog *self,
+            GtkToggleButton *active)
+{
+	gboolean set_active;
+	GList *children, *l;
+	GList *visible = NULL;
+	gint current = -1;
+	gint idx = 0;
+
+	g_assert (!self->bar_updating);
+	self->bar_updating = TRUE;
+
+	children = gtk_container_get_children (GTK_CONTAINER (self->bar));
+	for (l = children; l != NULL; l = g_list_next (l)) {
+		if (GTK_IS_TOGGLE_BUTTON (l->data) &&
+		    gtk_widget_get_visible (l->data))
+			visible = g_list_append (visible, l->data);
+	}
+
+	/* One should always be visible */
+	g_return_if_fail (visible != NULL);
+
+	/* Show bar if more than one visible */
+	gtk_widget_set_visible (GTK_WIDGET (self->bar), visible->next != NULL);
+
+	if (active == NULL) {
+		/* Try to keep active the same */
+		for (l = visible; l != NULL; l = g_list_next (l)) {
+			if (gtk_toggle_button_get_active (l->data)) {
+				active = l->data;
+				break;
+			}
+		}
+
+		/* Otherwise switch to the first page */
+		if (active == NULL)
+			active = visible->data;
+	}
+
+	for (l = children; l != NULL; l = g_list_next (l)) {
+		if (!GTK_IS_TOGGLE_BUTTON (l->data))
+			continue;
+		set_active = (l->data == active);
+		if (gtk_toggle_button_get_active (l->data) != set_active)
+			gtk_toggle_button_set_active (l->data, set_active);
+		label_set_bold (GTK_LABEL (gtk_bin_get_child (l->data)), set_active);
+		if (set_active)
+			current = idx;
+		idx++;
+	}
+
+	g_list_free (children);
+	g_list_free (visible);
+
+	if (current != -1) {
+		gtk_notebook_set_current_page (self->notebook, current);
+		dialog_validate (self);
+	}
+
+	self->bar_updating = FALSE;
+}
+
+static void
+on_bar_toggled (GtkToggleButton *toggle,
+                gpointer user_data)
+{
+	UmAccountDialog *self = UM_ACCOUNT_DIALOG (user_data);
+
+	if (self->bar_updating)
+		return;
+
+	/* Undo the toggle if already pressed */
+	if (!gtk_toggle_button_get_active (toggle))
+		gtk_toggle_button_set_active (toggle, TRUE);
+
+	/* Otherwise update everything */
+	else
+		bar_update (self, toggle);
+}
+
+static void
+bar_construct (UmAccountDialog *self,
+               GtkBuilder *builder)
+{
+	GtkToggleButton *first = NULL;
+	GtkStyleContext *context;
+	GList *children, *l;
+
+	self->bar = (GtkWidget *) gtk_builder_get_object (builder, "account-buttons");
+
+	context = gtk_widget_get_style_context (self->bar);
+	gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
+
+	children = gtk_container_get_children (GTK_CONTAINER (self->bar));
+	for (l = children; l != NULL; l = g_list_next (l)) {
+		if (!GTK_IS_TOGGLE_BUTTON (l->data))
+			continue;
+		if (first == NULL)
+			first = l->data;
+		g_signal_connect (l->data, "toggled",
+		                  G_CALLBACK (on_bar_toggled), self);
+	}
+
+	g_list_free (children);
+
+	/* Update and select the first button */
+	if (first != NULL)
+		gtk_toggle_button_set_active (first, TRUE);
+}
+
+static void
 on_local_login_changed (GtkComboBoxText *combo,
                         gpointer user_data)
 {
@@ -222,6 +347,10 @@ on_create_local_user (GObject         *object,
 			gtk_window_present (GTK_WINDOW (dialog));
 		}
 		g_error_free (error);
+	} else {
+		g_simple_async_result_set_op_res_gpointer (self->async,
+		                                           g_object_ref (user),
+		                                           g_object_unref);
 	}
 
 	g_simple_async_result_complete_in_idle (self->async);
@@ -252,8 +381,6 @@ local_add (UmAccountDialog *self)
 	                             g_object_ref (self),
 	                             g_object_unref);
 	g_object_unref (manager);
-
-	gtk_widget_hide (GTK_WIDGET (self));
 }
 
 static void
@@ -262,7 +389,6 @@ enterprise_add (UmAccountDialog *self)
 	/* TODO: Implement adding of users */
 
 	g_simple_async_result_complete_in_idle (self->async);
-	gtk_widget_hide (GTK_WIDGET (self));
 }
 
 static void
@@ -302,113 +428,6 @@ um_account_dialog_init (UmAccountDialog *self)
 }
 
 static void
-label_set_bold (GtkLabel *label,
-                gboolean bold)
-{
-	PangoAttrList *attrs;
-	PangoAttribute *attr;
-
-	attrs = pango_attr_list_new ();
-	attr = pango_attr_weight_new (bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
-	pango_attr_list_insert (attrs, attr);
-	gtk_label_set_attributes (label, attrs);
-	pango_attr_list_unref (attrs);
-}
-
-static void
-buttons_update (UmAccountDialog *self,
-                GtkToggleButton *active)
-{
-	gboolean any_visible = FALSE;
-	gboolean set_active;
-	GList *children, *l;
-	gint current = -1;
-	gint idx = 0;
-
-	g_assert (!self->buttons_updating);
-	self->buttons_updating = TRUE;
-
-	children = gtk_container_get_children (GTK_CONTAINER (self->buttons));
-	for (l = children; l != NULL; l = g_list_next (l)) {
-		if (!GTK_IS_TOGGLE_BUTTON (l->data))
-			continue;
-
-		if (active != NULL) {
-			set_active = (l->data == active);
-			if (gtk_toggle_button_get_active (l->data) != set_active)
-				gtk_toggle_button_set_active (l->data, set_active);
-			label_set_bold (GTK_LABEL (gtk_bin_get_child (l->data)), set_active);
-			if (set_active)
-				current = idx;
-		}
-
-		if (gtk_widget_get_visible (l->data))
-			any_visible = TRUE;
-
-		idx++;
-	}
-
-	g_list_free (children);
-
-	if (current != -1) {
-		gtk_notebook_set_current_page (self->notebook, current);
-		dialog_validate (self);
-	}
-
-	gtk_widget_set_visible (GTK_WIDGET (self->buttons), any_visible);
-
-	self->buttons_updating = FALSE;
-}
-
-static void
-on_button_toggled (GtkToggleButton *toggle,
-                   gpointer user_data)
-{
-	UmAccountDialog *self = UM_ACCOUNT_DIALOG (user_data);
-
-	if (self->buttons_updating)
-		return;
-
-	/* Undo the toggle if already pressed */
-	if (!gtk_toggle_button_get_active (toggle))
-		gtk_toggle_button_set_active (toggle, TRUE);
-
-	/* Otherwise update everything */
-	else
-		buttons_update (self, toggle);
-}
-
-static void
-buttons_construct (UmAccountDialog *self,
-                   GtkBuilder *builder)
-{
-	GtkToggleButton *first = NULL;
-	GtkStyleContext *context;
-	GList *children, *l;
-
-	self->buttons = (GtkWidget *) gtk_builder_get_object (builder, "account-buttons");
-
-	context = gtk_widget_get_style_context (self->buttons);
-	gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);
-
-	children = gtk_container_get_children (GTK_CONTAINER (self->buttons));
-	for (l = children; l != NULL; l = g_list_next (l)) {
-		if (!GTK_IS_TOGGLE_BUTTON (l->data))
-			continue;
-		if (first == NULL)
-			first = l->data;
-		g_signal_connect (l->data, "toggled",
-		                  G_CALLBACK (on_button_toggled), self);
-	}
-
-	g_list_free (children);
-
-	/* Update and select the first button */
-	if (first != NULL)
-		gtk_toggle_button_set_active (first, TRUE);
-}
-
-static void
 um_account_dialog_constructed (GObject *obj)
 {
 	GtkBuilder *builder;
@@ -450,7 +469,7 @@ um_account_dialog_constructed (GObject *obj)
 	local_construct (self, builder);
 	enterprise_construct (self, builder);
 
-	buttons_construct (self, builder);
+	bar_construct (self, builder);
 	g_object_unref (builder);
 }
 
@@ -478,12 +497,13 @@ um_account_dialog_response (GtkDialog *dialog,
 	case GTK_RESPONSE_DELETE_EVENT:
 		g_simple_async_result_set_op_res_gpointer (self->async, NULL, NULL);
 		g_simple_async_result_complete_in_idle (self->async);
-		gtk_widget_hide (GTK_WIDGET (self));
 		break;
 	default:
 		g_warn_if_reached ();
 		break;
 	}
+
+	gtk_widget_hide (GTK_WIDGET (self));
 }
 
 static void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]