[gnome-control-center/wip/feborges/new-addprinter-dialog: 3/7] printers: Merge authentication dialog into "Add Printer"



commit 22a940d4a9d50eb60ef195493fa7fb9374b444a9
Author: Felipe Borges <felipeborges gnome org>
Date:   Thu Nov 10 13:55:07 2016 +0100

    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

 panels/printers/new-printer-dialog.ui   |  321 ++++++++++++++++++++-----------
 panels/printers/pp-new-printer-dialog.c |  114 +++++++++++
 panels/printers/pp-samba.c              |  197 +++++--------------
 panels/printers/pp-samba.h              |    4 +
 4 files changed, 378 insertions(+), 258 deletions(-)
---
diff --git a/panels/printers/new-printer-dialog.ui b/panels/printers/new-printer-dialog.ui
index 418a28b..3bfa6e4 100644
--- a/panels/printers/new-printer-dialog.ui
+++ b/panels/printers/new-printer-dialog.ui
@@ -94,167 +94,270 @@
         <property name="spacing">10</property>
         <property name="border_width">0</property>
         <child>
-          <object class="GtkBox" id="box2">
+          <object class="GtkStack" id="dialog-stack">
             <property name="visible">True</property>
-            <property name="can_focus">False</property>
-            <property name="orientation">vertical</property>
-            <property name="hexpand">True</property>
-            <property name="vexpand">True</property>
+            <property name="border_width">0</property>
             <child>
-              <object class="GtkStack" id="stack">
+              <object class="GtkBox" id="box2">
                 <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="transition-type">none</property>
-                <style>
-                  <class name="view"/>
-                </style>
+                <property name="can_focus">False</property>
+                <property name="orientation">vertical</property>
+                <property name="hexpand">True</property>
+                <property name="vexpand">True</property>
                 <child>
-                  <object class="GtkScrolledWindow" id="scrolledwindow1">
+                  <object class="GtkStack" id="stack">
                     <property name="visible">True</property>
-                    <property name="can_focus">False</property>
+                    <property name="can_focus">True</property>
+                    <property name="transition-type">none</property>
+                    <style>
+                      <class name="view"/>
+                    </style>
                     <child>
-                      <object class="GtkTreeView" id="devices-treeview">
+                      <object class="GtkScrolledWindow" id="scrolledwindow1">
                         <property name="visible">True</property>
-                        <property name="can_focus">True</property>
-                        <property name="model">devices-model-filter</property>
-                        <property name="headers_visible">False</property>
-                        <property name="enable-grid-lines">GTK_TREE_VIEW_GRID_LINES_HORIZONTAL</property>
-                        <child internal-child="selection">
-                          <object class="GtkTreeSelection" id="treeview-selection"/>
+                        <property name="can_focus">False</property>
+                        <child>
+                          <object class="GtkTreeView" id="devices-treeview">
+                            <property name="visible">True</property>
+                            <property name="can_focus">True</property>
+                            <property name="model">devices-model-filter</property>
+                            <property name="headers_visible">False</property>
+                            <property name="enable-grid-lines">GTK_TREE_VIEW_GRID_LINES_HORIZONTAL</property>
+                            <child internal-child="selection">
+                              <object class="GtkTreeSelection" id="treeview-selection"/>
+                            </child>
+                          </object>
                         </child>
                       </object>
+                      <packing>
+                        <property name="name">standard-page</property>
+                      </packing>
                     </child>
-                  </object>
-                  <packing>
-                    <property name="name">standard-page</property>
-                  </packing>
-                </child>
-                <child>
-                  <object class="GtkBox">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <property name="orientation">GTK_ORIENTATION_VERTICAL</property>
-                    <property name="valign">GTK_ALIGN_CENTER</property>
-                    <property name="spacing">10</property>
                     <child>
-                      <object class="GtkImage">
+                      <object class="GtkBox">
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
-                        <property name="valign">GTK_ALIGN_START</property>
-                        <property name="pixel_size">72</property>
-                        <property name="icon_name">printer-symbolic</property>
-                        <property name="opacity">0.6</property>
-                        <style>
-                          <class name="dim-label"/>
-                        </style>
+                        <property name="orientation">GTK_ORIENTATION_VERTICAL</property>
+                        <property name="valign">GTK_ALIGN_CENTER</property>
+                        <property name="spacing">10</property>
+                        <child>
+                          <object class="GtkImage">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <property name="valign">GTK_ALIGN_START</property>
+                            <property name="pixel_size">72</property>
+                            <property name="icon_name">printer-symbolic</property>
+                            <property name="opacity">0.6</property>
+                            <style>
+                              <class name="dim-label"/>
+                            </style>
+                          </object>
+                          <packing>
+                            <property name="expand">False</property>
+                            <property name="fill">False</property>
+                            <property name="position">0</property>
+                          </packing>
+                        </child>
+                        <child>
+                          <object class="GtkLabel" id="no-devices-label">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <property name="sensitive">False</property>
+                            <property name="label" translatable="yes" comments="Translators: No printers 
were detected">No Printers Found</property>
+                            <property name="opacity">0.6</property>
+                            <attributes>
+                              <attribute name="weight" value="bold"/>
+                            </attributes>
+                          </object>
+                        </child>
                       </object>
                       <packing>
-                        <property name="expand">False</property>
-                        <property name="fill">False</property>
-                        <property name="position">0</property>
+                        <property name="name">no-printers-page</property>
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkLabel" id="no-devices-label">
+                      <object class="GtkBox">
                         <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="sensitive">False</property>
-                        <property name="label" translatable="yes" comments="Translators: No printers were 
detected">No Printers Found</property>
-                        <property name="opacity">0.6</property>
-                        <attributes>
-                          <attribute name="weight" value="bold"/>
-                        </attributes>
+                        <style>
+                          <class name="view"/>
+                        </style>
+                        <child>
+                          <object class="GtkSpinner" id="spinner">
+                            <property name="visible">True</property>
+                            <property name="active">True</property>
+                            <property name="expand">True</property>
+                            <property name="sensitive">False</property>
+                            <property name="opacity">0.6</property>
+                          </object>
+                        </child>
                       </object>
+                      <packing>
+                        <property name="name">loading-page</property>
+                      </packing>
                     </child>
                   </object>
                   <packing>
-                    <property name="name">no-printers-page</property>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                    <property name="position">0</property>
                   </packing>
                 </child>
                 <child>
-                  <object class="GtkBox">
+                  <object class="GtkSeparator">
                     <property name="visible">True</property>
+                  </object>
+                  <packing>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkToolbar" id="toolbar1">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="toolbar_style">icons</property>
+                    <property name="icon_size">1</property>
                     <style>
-                      <class name="view"/>
+                      <class name="toolbar"/>
                     </style>
                     <child>
-                      <object class="GtkSpinner" id="spinner">
+                      <object class="GtkToolItem" id="toolbutton1">
                         <property name="visible">True</property>
-                        <property name="active">True</property>
-                        <property name="expand">True</property>
-                        <property name="sensitive">False</property>
-                        <property name="opacity">0.6</property>
+                        <property name="can_focus">False</property>
+                        <child>
+                          <object class="GtkBox" id="box1">
+                            <property name="visible">True</property>
+                            <property name="can_focus">False</property>
+                            <property name="border_width">4</property>
+                            <child>
+                              <object class="GtkSearchEntry" id="search-entry">
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="has_tooltip">True</property>
+                                <property name="invisible_char">●</property>
+                                <property name="truncate_multiline">True</property>
+                                <property name="invisible_char_set">True</property>
+                                <property name="placeholder_text" translatable="yes" comments="Translators: 
The entered text should contain network address of a printer or a text which will filter found devices (their 
names and locations)">Enter a network address or search for a printer</property>
+                                <property name="margin_start">40</property>
+                                <property name="margin_end">40</property>
+                              </object>
+                              <packing>
+                                <property name="expand">True</property>
+                                <property name="fill">True</property>
+                                <property name="position">1</property>
+                              </packing>
+                            </child>
+                          </object>
+                        </child>
                       </object>
+                      <packing>
+                        <property name="expand">True</property>
+                        <property name="homogeneous">True</property>
+                      </packing>
                     </child>
                   </object>
                   <packing>
-                    <property name="name">loading-page</property>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">2</property>
                   </packing>
                 </child>
               </object>
               <packing>
-                <property name="expand">True</property>
-                <property name="fill">True</property>
-                <property name="position">0</property>
+                <property name="name">addprinter-page</property>
               </packing>
             </child>
             <child>
-              <object class="GtkSeparator">
+              <object class="GtkGrid">
                 <property name="visible">True</property>
-              </object>
-              <packing>
-                <property name="position">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkToolbar" id="toolbar1">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="toolbar_style">icons</property>
-                <property name="icon_size">1</property>
+                <property name="margin">20</property>
+                <property name="row_spacing">10</property>
+                <property name="column_spacing">15</property>
+                <property name="expand">True</property>
                 <style>
-                  <class name="toolbar"/>
+                  <class name="background"/>
                 </style>
                 <child>
-                  <object class="GtkToolItem" id="toolbutton1">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <child>
-                      <object class="GtkBox" id="box1">
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="border_width">4</property>
-                        <child>
-                          <object class="GtkSearchEntry" id="search-entry">
-                            <property name="visible">True</property>
-                            <property name="can_focus">True</property>
-                            <property name="has_tooltip">True</property>
-                            <property name="invisible_char">●</property>
-                            <property name="truncate_multiline">True</property>
-                            <property name="invisible_char_set">True</property>
-                            <property name="placeholder_text" translatable="yes" comments="Translators: The 
entered text should contain network address of a printer or a text which will filter found devices (their 
names and locations)">Enter a network address or search for a printer</property>
-                            <property name="margin_start">40</property>
-                            <property name="margin_end">40</property>
-                          </object>
-                          <packing>
-                            <property name="expand">True</property>
-                            <property name="fill">True</property>
-                            <property name="position">1</property>
-                          </packing>
-                        </child>
-                      </object>
-                    </child>
+                  <object class="GtkImage">
+                    <property name="pixel_size">48</property>
+                    <property name="icon_name">dialog-password-symbolic</property>
                   </object>
                   <packing>
-                    <property name="expand">True</property>
-                    <property name="homogeneous">True</property>
+                    <property name="left_attach">0</property>
+                    <property name="top_attach">0</property>
+                    <property name="height">2</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel">
+                    <property name="label" translatable="yes">Authentication Required</property>
+                    <property name="xalign">0</property>
+                    <attributes>
+                      <attribute name="weight" value="bold"/>
+                    </attributes>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="authentication-text">
+                    <property name="wrap">True</property>
+                    <property name="max_width_chars">36</property>
+                    <property name="xalign">0</property>
+                    <property name="label" translatable="yes">Enter username and password to view printers 
on Print Server.</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">1</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel">
+                    <property name="label" translatable="yes">Username</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">0</property>
+                    <property name="top_attach">2</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkEntry" id="username-entry">
+                    <property name="can_focus">True</property>
+                    <property name="invisible_char">●</property>
+                    <property name="activates_default">True</property>
+                    <property name="hexpand">True</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">2</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkLabel">
+                    <property name="label" translatable="yes">Password</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">0</property>
+                    <property name="top_attach">3</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkEntry" id="password-entry">
+                    <property name="can_focus">True</property>
+                    <property name="visibility">False</property>
+                    <property name="invisible_char">●</property>
+                    <property name="activates_default">True</property>
+                    <property name="hexpand">True</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">3</property>
                   </packing>
                 </child>
               </object>
               <packing>
-                <property name="expand">False</property>
-                <property name="fill">True</property>
-                <property name="position">2</property>
+                <property name="name">authentication-page</property>
               </packing>
             </child>
           </object>
diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c
index d154bdb..de35a62 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);
@@ -330,6 +333,107 @@ get_authenticated_samba_devices_cb (GObject      *source_object,
 }
 
 static void
+go_to_page (PpNewPrinterDialog *dialog,
+            gchar              *page)
+{
+  PpNewPrinterDialogPrivate *priv = dialog->priv;
+  GtkStack                  *stack;
+
+  stack = GTK_STACK (WID ("dialog-stack"));
+  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))));
+}
+
+typedef struct
+{
+  PpNewPrinterDialog *dialog;
+  PpHost *host;
+} AuthenticationData;
+
+static void
+on_authenticate (GtkWidget *button,
+                 gpointer   user_data)
+{
+  PpNewPrinterDialogPrivate *priv;
+  AuthenticationData        *data = (AuthenticationData *) user_data;
+  gchar                     *username = NULL;
+  gchar                     *password = NULL;
+
+  priv = PP_NEW_PRINTER_DIALOG (data->dialog)->priv;
+
+  username = get_entry_text ("username-entry", data->dialog);
+  password = get_entry_text ("password-entry", data->dialog);
+
+  if ((username == NULL) || (username[0] == '\0'))
+    g_clear_pointer (&username, g_free);
+
+  if ((password == NULL) || (password[0] == '\0'))
+    {
+      g_clear_pointer (&password, g_free);
+      return;
+    }
+
+  pp_samba_set_auth_info (PP_SAMBA (data->host), username, password);
+
+  go_to_page (data->dialog, ADDPRINTER_PAGE);
+  g_signal_handlers_disconnect_by_func (WID ("unlock-button"),
+                                        on_authenticate, NULL);
+}
+
+static void
+on_authentication_required (PpHost   *host,
+                            gpointer  user_data)
+{
+  PpNewPrinterDialogPrivate *priv;
+  PpNewPrinterDialog        *dialog = PP_NEW_PRINTER_DIALOG (user_data);
+  AuthenticationData        *data;
+
+  priv = dialog->priv;
+
+  data = g_new (AuthenticationData, 1);
+  data->dialog = dialog;
+  data->host = host;
+
+  gtk_header_bar_set_subtitle (GTK_HEADER_BAR (WID ("headerbar")), NULL);
+  go_to_page (dialog, AUTHENTICATION_PAGE);
+
+  g_signal_connect (WID ("unlock-button"), "clicked", G_CALLBACK (on_authenticate), data);
+  g_signal_connect (WID ("username-entry"), "activate", G_CALLBACK (on_authenticate), data);
+  g_signal_connect (WID ("password-entry"), "activate", G_CALLBACK (on_authenticate), data);
+}
+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);
+
+  if (username != NULL && password != NULL)
+    can_authenticate = (strlen (username) > 0 && strlen (password) > 0);
+
+  gtk_widget_set_sensitive (WID ("unlock-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)
 {
@@ -341,6 +445,8 @@ authenticate_samba_server (GtkButton *button,
   PpSamba                   *samba_host;
   gchar                     *server_name = NULL;
 
+  gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE);
+
   if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (priv->treeview), &model, &iter))
     {
       gtk_tree_model_get (model, &iter,
@@ -351,6 +457,10 @@ authenticate_samba_server (GtkButton *button,
         {
           samba_host = pp_samba_new (GTK_WINDOW (priv->dialog),
                                      server_name);
+          g_signal_connect (samba_host,
+                            "authentication-required",
+                            G_CALLBACK (on_authentication_required),
+                            dialog);
 
           priv->samba_authenticated_searching = TRUE;
           update_dialog_state (dialog);
@@ -418,6 +528,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);
diff --git a/panels/printers/pp-samba.c b/panels/printers/pp-samba.c
index 9c5ee4b..fb56e9d 100644
--- a/panels/printers/pp-samba.c
+++ b/panels/printers/pp-samba.c
@@ -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,74 +48,23 @@ 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)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (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,41 @@ 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;
-
-  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);
+  SMBData                *data = (SMBData *) user_data;
 
-  if (response_id == GTK_RESPONSE_CANCEL ||
-      response_id == GTK_RESPONSE_DELETE_EVENT)
-    data->cancelled = TRUE;
+  g_signal_emit_by_name (data->samba, "authentication-required");
 
-  data->waiting = FALSE;
+  return FALSE;
 }
 
-static gboolean
-get_auth_info (gpointer user_data)
+void
+pp_samba_set_auth_info (PpSamba *samba,
+                        gchar   *username,
+                        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 +149,22 @@ 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);
+      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);
 
-      data->waiting = TRUE;
+      samba->priv->waiting = TRUE;
 
       source = g_idle_source_new ();
       g_source_set_callback (source,
@@ -276,19 +179,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 +348,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);
 
@@ -491,7 +392,6 @@ pp_samba_get_devices_async (PpSamba             *samba,
                             gpointer             user_data)
 {
   GSimpleAsyncResult *res;
-  PpSambaPrivate     *priv = samba->priv;
   SMBData            *data;
   gchar              *hostname;
 
@@ -502,7 +402,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 7df0056..03c8e5d 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,
+                                            gchar               *username,
+                                            gchar               *password);
+
 G_END_DECLS
 
 #endif /* __PP_SAMBA_H__ */


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