From afac6d534008f30ae7b246a38d3fdafa1312d34b Mon Sep 17 00:00:00 2001 From: Felipe Borges Date: Thu, 10 Nov 2016 13:55:07 +0100 Subject: [PATCH] printers: Merge authentication dialog into "Add Printer" This is a redesign based on the newest mockups available at https://wiki.gnome.org/Design/SystemSettings/Printers#Guidelines https://bugzilla.gnome.org/show_bug.cgi?id=760783 --- panels/printers/new-printer-dialog.ui | 394 ++++++++++++++++-------- panels/printers/pp-new-printer-dialog.c | 112 ++++++- panels/printers/pp-samba.c | 204 ++++-------- panels/printers/pp-samba.h | 4 + 4 files changed, 420 insertions(+), 294 deletions(-) diff --git a/panels/printers/new-printer-dialog.ui b/panels/printers/new-printer-dialog.ui index 418a28b38..121936211 100644 --- a/panels/printers/new-printer-dialog.ui +++ b/panels/printers/new-printer-dialog.ui @@ -61,7 +61,7 @@ - add-button + addprinter-page @@ -79,6 +79,23 @@ unlock-button + + + _Unlock + True + True + True + True + True + True + + + + authentication-page + + GTK_PACK_END @@ -94,167 +111,270 @@ 10 0 - + True - False - vertical - True - True + 0 - + True - True - none - + False + vertical + True + True - - True - False - - - True - True - devices-model-filter - False - GTK_TREE_VIEW_GRID_LINES_HORIZONTAL - - - - - - - - standard-page - - - - - True - False - GTK_ORIENTATION_VERTICAL - GTK_ALIGN_CENTER - 10 - - - True - False - GTK_ALIGN_START - 72 - printer-symbolic - 0.6 - - - - False - False - 0 - - - - - True - False - False - No Printers Found - 0.6 - - - - - - - - no-printers-page - - - - + True + True + none - - True - True - True - False - 0.6 - - - - - loading-page - - - - - True - True - 0 - - - - - True - - - 1 - - - - - True - False - icons - 1 - - - - True - False - - + True False - 4 - + True True - True - - True - True - Enter a network address or search for a printer - 40 - 40 + devices-model-filter + False + GTK_TREE_VIEW_GRID_LINES_HORIZONTAL + + + - - True - True - 1 - + + standard-page + + + + + True + False + GTK_ORIENTATION_VERTICAL + GTK_ALIGN_CENTER + 10 + + + True + False + GTK_ALIGN_START + 72 + printer-symbolic + 0.6 + + + + False + False + 0 + + + + + True + False + False + No Printers Found + 0.6 + + + + + + + + no-printers-page + + + + + True + + + + True + True + True + False + 0.6 + + + + + loading-page + True - True + True + 0 + + + + + True + + + 1 + + + + + True + False + icons + 1 + + + + True + False + + + True + False + 4 + + + True + True + True + + True + True + Enter a network address or search for a printer + 40 + 40 + + + True + True + 1 + + + + + + + True + True + + + + + False + True + 2 - False - True - 2 + addprinter-page + + + + + True + 20 + 10 + 15 + True + + + + 48 + dialog-password-symbolic + + + 0 + 0 + 2 + + + + + Authentication Required + 0 + + + + + + 1 + 0 + + + + + True + 36 + 0 + Enter username and password to view printers on Print Server. + + + 1 + 1 + + + + + Username + + + 0 + 2 + + + + + True + + True + True + + + 1 + 2 + + + + + Password + + + 0 + 3 + + + + + True + False + + True + True + + + 1 + 3 + + + + + authentication-page diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c index 4d9112160..9c4360048 100644 --- a/panels/printers/pp-new-printer-dialog.c +++ b/panels/printers/pp-new-printer-dialog.c @@ -55,6 +55,9 @@ #define WID(s) GTK_WIDGET (gtk_builder_get_object (priv->builder, s)) +#define AUTHENTICATION_PAGE "authentication-page" +#define ADDPRINTER_PAGE "addprinter-page" + static void set_device (PpNewPrinterDialog *dialog, PpPrintDevice *device, GtkTreeIter *iter); @@ -329,6 +332,93 @@ get_authenticated_samba_devices_cb (GObject *source_object, g_free (data); } +static void +go_to_page (PpNewPrinterDialog *dialog, + const gchar *page) +{ + PpNewPrinterDialogPrivate *priv = dialog->priv; + GtkStack *stack; + + stack = GTK_STACK (WID ("dialog-stack")); + gtk_stack_set_visible_child_name (stack, page); + + stack = GTK_STACK (WID ("headerbar-topright-buttons")); + gtk_stack_set_visible_child_name (stack, page); +} + +static gchar * +get_entry_text (const gchar *object_name, + PpNewPrinterDialog *dialog) +{ + PpNewPrinterDialogPrivate *priv = dialog->priv; + + return g_strdup (gtk_entry_get_text (GTK_ENTRY (WID (object_name)))); +} + +static void +on_authenticate (GtkWidget *button, + gpointer user_data) +{ + PpNewPrinterDialog *dialog = PP_NEW_PRINTER_DIALOG (user_data); + PpNewPrinterDialogPrivate *priv = dialog->priv; + gchar *username = NULL; + gchar *password = NULL; + + username = get_entry_text ("username-entry", dialog); + password = get_entry_text ("password-entry", dialog); + + if ((username == NULL) || (username[0] == '\0') || + (password == NULL) || (password[0] == '\0')) + { + g_clear_pointer (&username, g_free); + g_clear_pointer (&password, g_free); + return; + } + + pp_samba_set_auth_info (PP_SAMBA (priv->samba_host), username, password); + + go_to_page (dialog, ADDPRINTER_PAGE); +} + +static void +on_authentication_required (PpHost *host, + gpointer user_data) +{ + PpNewPrinterDialogPrivate *priv; + PpNewPrinterDialog *dialog = PP_NEW_PRINTER_DIALOG (user_data); + + priv = dialog->priv; + + gtk_header_bar_set_subtitle (GTK_HEADER_BAR (WID ("headerbar")), NULL); + go_to_page (dialog, AUTHENTICATION_PAGE); + + g_signal_connect (WID ("authenticate-button"), "clicked", G_CALLBACK (on_authenticate), dialog); +} + +static void +auth_entries_changed (GtkEditable *editable, + gpointer user_data) +{ + PpNewPrinterDialogPrivate *priv; + PpNewPrinterDialog *dialog = PP_NEW_PRINTER_DIALOG (user_data); + gboolean can_authenticate = FALSE; + gchar *username = NULL; + gchar *password = NULL; + + priv = dialog->priv; + + username = get_entry_text ("username-entry", dialog); + password = get_entry_text ("password-entry", dialog); + + can_authenticate = (username != NULL && username[0] != '\0' && + password != NULL && password[0] != '\0'); + + gtk_widget_set_sensitive (WID ("authenticate-button"), can_authenticate); + + g_clear_pointer (&username, g_free); + g_clear_pointer (&password, g_free); +} + static void authenticate_samba_server (GtkButton *button, gpointer user_data) @@ -338,9 +428,12 @@ authenticate_samba_server (GtkButton *button, GtkTreeModel *model; GtkTreeIter iter; AuthSMBData *data; - PpSamba *samba_host; gchar *server_name = NULL; + gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE); + gtk_widget_set_sensitive (WID ("authenticate-button"), FALSE); + gtk_widget_grab_focus (WID ("username-entry")); + if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (priv->treeview), &model, &iter)) { gtk_tree_model_get (model, &iter, @@ -349,8 +442,13 @@ authenticate_samba_server (GtkButton *button, if (server_name != NULL) { - samba_host = pp_samba_new (GTK_WINDOW (priv->dialog), - server_name); + g_clear_object (&priv->samba_host); + + priv->samba_host = pp_samba_new (GTK_WINDOW (priv->dialog), server_name); + g_signal_connect_object (priv->samba_host, + "authentication-required", + G_CALLBACK (on_authentication_required), + dialog, 0); priv->samba_authenticated_searching = TRUE; update_dialog_state (dialog); @@ -359,7 +457,7 @@ authenticate_samba_server (GtkButton *button, data->server_name = server_name; data->dialog = dialog; - pp_samba_get_devices_async (samba_host, + pp_samba_get_devices_async (priv->samba_host, TRUE, priv->cancellable, get_authenticated_samba_devices_cb, @@ -418,6 +516,10 @@ pp_new_printer_dialog_init (PpNewPrinterDialog *dialog) widget = WID ("unlock-button"); g_signal_connect (widget, "clicked", G_CALLBACK (authenticate_samba_server), dialog); + /* Authentication form widgets */ + g_signal_connect (WID ("username-entry"), "changed", G_CALLBACK (auth_entries_changed), dialog); + g_signal_connect (WID ("password-entry"), "changed", G_CALLBACK (auth_entries_changed), dialog); + /* Set junctions */ widget = WID ("scrolledwindow1"); context = gtk_widget_get_style_context (widget); @@ -514,7 +616,7 @@ device_selection_changed_cb (GtkTreeSelection *selection, if (authentication_needed) gtk_stack_set_visible_child_name (GTK_STACK (stack), "unlock-button"); else - gtk_stack_set_visible_child_name (GTK_STACK (stack), "add-button"); + gtk_stack_set_visible_child_name (GTK_STACK (stack), ADDPRINTER_PAGE); } } diff --git a/panels/printers/pp-samba.c b/panels/printers/pp-samba.c index eef3bf722..4667911f8 100644 --- a/panels/printers/pp-samba.c +++ b/panels/printers/pp-samba.c @@ -26,23 +26,21 @@ #include #include -#include "pp-authentication-dialog.h" - #define POLL_DELAY 100000 struct _PpSambaPrivate { - GtkWindow *parent; + /* Auth info */ + gchar *server; + gchar *share; + gchar *workgroup; + gchar *username; + gchar *password; + gboolean waiting; }; G_DEFINE_TYPE (PpSamba, pp_samba, PP_TYPE_HOST); -enum -{ - PROP_0 = 0, - PROP_PARENT, -}; - static void pp_samba_finalize (GObject *object) { @@ -50,55 +48,15 @@ pp_samba_finalize (GObject *object) priv = PP_SAMBA (object)->priv; - g_object_unref (priv->parent); + g_free (priv->server); + g_free (priv->share); + g_free (priv->workgroup); + g_free (priv->username); + g_free (priv->password); G_OBJECT_CLASS (pp_samba_parent_class)->finalize (object); } -static void -pp_samba_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *param_spec) -{ - PpSamba *self = PP_SAMBA (object); - - switch (prop_id) - { - case PROP_PARENT: - g_value_set_pointer (value, self->priv->parent); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, - prop_id, - param_spec); - break; - } -} - -static void -pp_samba_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *param_spec) -{ - PpSamba *self = PP_SAMBA (object); - - switch (prop_id) - { - case PROP_PARENT: - if (self->priv->parent) - g_object_unref (self->priv->parent); - self->priv->parent = g_object_ref (G_OBJECT (g_value_get_pointer (value))); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, - prop_id, - param_spec); - break; - } -} - static void pp_samba_class_init (PpSambaClass *klass) { @@ -106,18 +64,7 @@ pp_samba_class_init (PpSambaClass *klass) g_type_class_add_private (klass, sizeof (PpSambaPrivate)); - gobject_class->set_property = pp_samba_set_property; - gobject_class->get_property = pp_samba_get_property; gobject_class->finalize = pp_samba_finalize; - - /* - * Used for authentication dialog. - */ - g_object_class_install_property (gobject_class, PROP_PARENT, - g_param_spec_pointer ("parent", - "Parent", - "Parent window", - G_PARAM_READWRITE)); } static void @@ -133,42 +80,17 @@ pp_samba_new (GtkWindow *parent, const gchar *hostname) { return g_object_new (PP_TYPE_SAMBA, - "parent", parent, "hostname", hostname, NULL); } typedef struct { - gchar *server; - gchar *share; - gchar *workgroup; - gchar *username; - gchar *password; -} SMBAuthInfo; - -static void -smb_auth_info_free (SMBAuthInfo *auth_info) -{ - if (auth_info) - { - g_free (auth_info->server); - g_free (auth_info->share); - g_free (auth_info->workgroup); - g_free (auth_info->username); - g_free (auth_info->password); - g_free (auth_info); - } -} - -typedef struct -{ + PpSamba *samba; PpDevicesList *devices; GMainContext *context; gboolean waiting; gboolean auth_if_needed; - GtkWindow *parent; - SMBAuthInfo *auth_info; gboolean hostname_set; gboolean cancelled; } SMBData; @@ -179,63 +101,46 @@ smb_data_free (SMBData *data) if (data) { pp_devices_list_free (data->devices); - smb_auth_info_free (data->auth_info); - g_object_unref (data->parent); g_free (data); } } -static void -auth_cb (PpAuthenticationDialog *auth_dialog, - gint response_id, - const gchar *username, - const gchar *password, - gpointer user_data) -{ - SMBData *data = (SMBData *) user_data; - - if (username && username[0] != '\0') - { - g_free (data->auth_info->username); - data->auth_info->username = g_strdup (username); - } - - if (password && password[0] != '\0') - { - g_free (data->auth_info->password); - data->auth_info->password = g_strdup (password); - } - - g_object_unref (auth_dialog); - - if (response_id == GTK_RESPONSE_CANCEL || - response_id == GTK_RESPONSE_DELETE_EVENT) - data->cancelled = TRUE; - - data->waiting = FALSE; -} - static gboolean get_auth_info (gpointer user_data) { - PpAuthenticationDialog *auth_dialog; - SMBData *data = (SMBData *) user_data; - gchar *text; + SMBData *data = (SMBData *) user_data; + PpSamba *samba = PP_SAMBA (data->samba); - /* Translators: Samba server needs authentication of the user to show list of its printers. */ - text = g_strdup_printf (_("Enter your username and password to view printers available on %s."), - data->auth_info->server); - auth_dialog = pp_authentication_dialog_new (data->parent, - text, - data->auth_info->username); - g_signal_connect (auth_dialog, "response", G_CALLBACK (auth_cb), user_data); + samba->priv->waiting = TRUE; - g_free (text); + g_signal_emit_by_name (samba, "authentication-required"); return FALSE; } +void +pp_samba_set_auth_info (PpSamba *samba, + const gchar *username, + const gchar *password) +{ + PpSambaPrivate *priv = samba->priv; + + if ((username != NULL) && (username[0] != '\0')) + { + g_free (priv->username); + priv->username = g_strdup (username); + } + + if ((password != NULL) && (password[0] != '\0')) + { + g_free (priv->password); + priv->password = g_strdup (password); + } + + priv->waiting = FALSE; +} + static void auth_fn (SMBCCTX *smb_context, const char *server, @@ -247,21 +152,20 @@ auth_fn (SMBCCTX *smb_context, char *password, int pwmaxlen) { + PpSamba *samba; GSource *source; SMBData *data; data = (SMBData *) smbc_getOptionUserData (smb_context); + samba = data->samba; if (!data->cancelled) { - data->auth_info = g_new (SMBAuthInfo, 1); - data->auth_info->server = g_strdup (server); - data->auth_info->share = g_strdup (share); - data->auth_info->workgroup = g_strdup (workgroup); - data->auth_info->username = g_strdup (username); - data->auth_info->password = g_strdup (password); - - data->waiting = TRUE; + samba->priv->server = g_strdup (server); + samba->priv->share = g_strdup (share); + samba->priv->workgroup = g_strdup (workgroup); + samba->priv->username = g_strdup (username); + samba->priv->password = g_strdup (password); source = g_idle_source_new (); g_source_set_callback (source, @@ -276,19 +180,16 @@ auth_fn (SMBCCTX *smb_context, * from this synchronous callback so we are blocking * until we get them */ - while (data->waiting) + while (samba->priv->waiting) { g_usleep (POLL_DELAY); } - if (g_strcmp0 (username, data->auth_info->username) != 0) - g_strlcpy (username, data->auth_info->username, unmaxlen); + if (g_strcmp0 (username, samba->priv->username) != 0) + g_strlcpy (username, samba->priv->username, unmaxlen); - if (g_strcmp0 (password, data->auth_info->password) != 0) - g_strlcpy (password, data->auth_info->password, pwmaxlen); - - smb_auth_info_free (data->auth_info); - data->auth_info = NULL; + if (g_strcmp0 (password, samba->priv->password) != 0) + g_strlcpy (password, samba->priv->password, pwmaxlen); } } @@ -448,6 +349,7 @@ _pp_samba_get_devices_thread (GSimpleAsyncResult *res, data = g_simple_async_result_get_op_res_gpointer (res); data->devices = g_new0 (PpDevicesList, 1); data->devices->devices = NULL; + data->samba = PP_SAMBA (object); g_mutex_lock (&mutex); @@ -493,7 +395,6 @@ pp_samba_get_devices_async (PpSamba *samba, gpointer user_data) { GSimpleAsyncResult *res; - PpSambaPrivate *priv = samba->priv; SMBData *data; gchar *hostname = NULL; @@ -504,7 +405,6 @@ pp_samba_get_devices_async (PpSamba *samba, data->devices = NULL; data->context = g_main_context_default (); data->hostname_set = hostname != NULL; - data->parent = g_object_ref (priv->parent); data->auth_if_needed = auth_if_needed; g_simple_async_result_set_check_cancellable (res, cancellable); diff --git a/panels/printers/pp-samba.h b/panels/printers/pp-samba.h index 7df00561d..4dad0d613 100644 --- a/panels/printers/pp-samba.h +++ b/panels/printers/pp-samba.h @@ -63,6 +63,10 @@ PpDevicesList *pp_samba_get_devices_finish (PpSamba *samba, GAsyncResult *result, GError **error); +void pp_samba_set_auth_info (PpSamba *samba, + const gchar *username, + const gchar *password); + G_END_DECLS #endif /* __PP_SAMBA_H__ */