Commit afac6d53 authored by Felipe Borges's avatar Felipe Borges 👣

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
parent e5106e9a
This diff is collapsed.
......@@ -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);
}
}
......
......@@ -26,23 +26,21 @@
#include <libsmbclient.h>
#include <errno.h>
#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,61 +101,44 @@ 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)
static gboolean
get_auth_info (gpointer user_data)
{
SMBData *data = (SMBData *) user_data;
PpSamba *samba = PP_SAMBA (data->samba);
if (username && username[0] != '\0')
{
g_free (data->auth_info->username);
data->auth_info->username = g_strdup (username);
}
samba->priv->waiting = TRUE;
if (password && password[0] != '\0')
{
g_free (data->auth_info->password);
data->auth_info->password = g_strdup (password);
}
g_object_unref (auth_dialog);
g_signal_emit_by_name (samba, "authentication-required");
if (response_id == GTK_RESPONSE_CANCEL ||
response_id == GTK_RESPONSE_DELETE_EVENT)
data->cancelled = TRUE;
data->waiting = FALSE;
return FALSE;
}
static gboolean
get_auth_info (gpointer user_data)
void
pp_samba_set_auth_info (PpSamba *samba,
const gchar *username,
const gchar *password)
{
PpAuthenticationDialog *auth_dialog;
SMBData *data = (SMBData *) user_data;
gchar *text;
PpSambaPrivate *priv = samba->priv;
/* 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);
if ((username != NULL) && (username[0] != '\0'))
{
g_free (priv->username);
priv->username = g_strdup (username);
}
g_free (text);
if ((password != NULL) && (password[0] != '\0'))
{
g_free (priv->password);
priv->password = g_strdup (password);
}
return FALSE;
priv->waiting = FALSE;
}
static void
......@@ -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 (password, data->auth_info->password) != 0)
g_strlcpy (password, data->auth_info->password, pwmaxlen);
if (g_strcmp0 (username, samba->priv->username) != 0)
g_strlcpy (username, samba->priv->username, unmaxlen);
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);
......
......@@ -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__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment