[gnome-online-accounts/gnome-3-8] mail-auth: Add properties for "input" and "output"



commit 85d2e50bbc6b462afde59b6b7f62984d16455a60
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Mar 8 22:39:53 2013 +0100

    mail-auth: Add properties for "input" and "output"
    
    We will add a virtual method for doing STARTTLS. So instead of passing
    the input and output streams to each method, lets just set them on the
    GoaMailAuth implementations themselves.
    
    Fixes: https://bugzilla.gnome.org/695355

 src/goabackend/goaimapauthlogin.c |    9 +-
 src/goabackend/goamailauth.c      |  200 ++++++++++++++++++++++++++-----------
 src/goabackend/goamailauth.h      |   12 +-
 src/goabackend/goamailclient.c    |   18 ++-
 src/goabackend/goasmtpauthplain.c |    9 +-
 5 files changed, 171 insertions(+), 77 deletions(-)
---
diff --git a/src/goabackend/goaimapauthlogin.c b/src/goabackend/goaimapauthlogin.c
index ec489c2..c35a8a2 100644
--- a/src/goabackend/goaimapauthlogin.c
+++ b/src/goabackend/goaimapauthlogin.c
@@ -72,8 +72,6 @@ enum
 
 static gboolean goa_imap_auth_login_is_needed (GoaMailAuth        *auth);
 static gboolean goa_imap_auth_login_run_sync (GoaMailAuth         *_auth,
-                                              GDataInputStream    *input,
-                                              GDataOutputStream   *output,
                                               GCancellable        *cancellable,
                                               GError             **error);
 
@@ -299,12 +297,12 @@ goa_imap_auth_login_is_needed (GoaMailAuth *_auth)
 
 static gboolean
 goa_imap_auth_login_run_sync (GoaMailAuth         *_auth,
-                              GDataInputStream    *input,
-                              GDataOutputStream   *output,
                               GCancellable        *cancellable,
                               GError             **error)
 {
   GoaImapAuthLogin *auth = GOA_IMAP_AUTH_LOGIN (_auth);
+  GDataInputStream *input;
+  GDataOutputStream *output;
   gchar *request;
   gchar *response;
   gboolean ret;
@@ -351,6 +349,9 @@ goa_imap_auth_login_run_sync (GoaMailAuth         *_auth,
       goto out;
     }
 
+  input = goa_mail_auth_get_input (_auth);
+  output = goa_mail_auth_get_output (_auth);
+
   request = g_strdup_printf ("A001 LOGIN \"%s\" \"%s\"\r\n", auth->username, password);
   if (!g_data_output_stream_put_string (output, request, cancellable, error))
     goto out;
diff --git a/src/goabackend/goamailauth.c b/src/goabackend/goamailauth.c
index b7f0f90..4984b03 100644
--- a/src/goabackend/goamailauth.c
+++ b/src/goabackend/goamailauth.c
@@ -27,68 +27,33 @@
 
 #include "goamailauth.h"
 
-G_DEFINE_ABSTRACT_TYPE (GoaMailAuth, goa_mail_auth, G_TYPE_OBJECT);
-
-/* ---------------------------------------------------------------------------------------------------- */
-
-typedef struct
+struct _GoaMailAuthPrivate
 {
-  GAsyncReadyCallback callback;
   GDataInputStream *input;
   GDataOutputStream *output;
-  gpointer user_data;
-} RunData;
-
-static void
-mail_auth_run_data_free (RunData *data)
-{
-  g_object_unref (data->input);
-  g_object_unref (data->output);
-  g_slice_free (RunData, data);
-}
+};
 
-static RunData *
-mail_auth_run_data_new (GDataInputStream *input,
-                        GDataOutputStream *output,
-                        GAsyncReadyCallback callback,
-                        gpointer user_data)
+enum
 {
-  RunData *data;
+  PROP_0,
+  PROP_INPUT,
+  PROP_OUTPUT
+};
 
-  data = g_slice_new0 (RunData);
-  data->input = g_object_ref (input);
-  data->output = g_object_ref (output);
-  data->callback = callback;
-  data->user_data = user_data;
-
-  return data;
-}
+G_DEFINE_ABSTRACT_TYPE (GoaMailAuth, goa_mail_auth, G_TYPE_OBJECT);
 
 /* ---------------------------------------------------------------------------------------------------- */
 
 static void
-mail_auth_run_async_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
-{
-  RunData *data = user_data;
-
-  if (data->callback != NULL)
-    data->callback (source_object, res, data->user_data);
-
-  mail_auth_run_data_free (data);
-}
-
-static void
 mail_auth_run_in_thread_func (GSimpleAsyncResult *res, GObject *object, GCancellable *cancellable)
 {
   GError *error;
-  RunData *data;
   gboolean op_res;
 
-  data = (RunData *) g_async_result_get_user_data (G_ASYNC_RESULT (res));
   op_res = FALSE;
 
   error = NULL;
-  if (!goa_mail_auth_run_sync (GOA_MAIL_AUTH (object), data->input, data->output, cancellable, &error))
+  if (!goa_mail_auth_run_sync (GOA_MAIL_AUTH (object), cancellable, &error))
     {
       g_simple_async_result_take_error (res, error);
       goto out;
@@ -103,13 +68,103 @@ mail_auth_run_in_thread_func (GSimpleAsyncResult *res, GObject *object, GCancell
 /* ---------------------------------------------------------------------------------------------------- */
 
 static void
+goa_mail_auth_dispose (GObject *object)
+{
+  GoaMailAuth *auth = GOA_MAIL_AUTH (object);
+  GoaMailAuthPrivate *priv = auth->priv;
+
+  g_clear_object (&priv->input);
+  g_clear_object (&priv->output);
+
+  G_OBJECT_CLASS (goa_mail_auth_parent_class)->dispose (object);
+}
+
+static void
+goa_mail_auth_get_property (GObject      *object,
+                            guint         prop_id,
+                            GValue       *value,
+                            GParamSpec   *pspec)
+{
+  GoaMailAuth *auth = GOA_MAIL_AUTH (object);
+  GoaMailAuthPrivate *priv = auth->priv;
+
+  switch (prop_id)
+    {
+    case PROP_INPUT:
+      g_value_set_object (value, priv->input);
+      break;
+
+    case PROP_OUTPUT:
+      g_value_set_object (value, priv->output);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+goa_mail_auth_set_property (GObject      *object,
+                            guint         prop_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
+{
+  GoaMailAuth *auth = GOA_MAIL_AUTH (object);
+  GoaMailAuthPrivate *priv = auth->priv;
+
+  switch (prop_id)
+    {
+    case PROP_INPUT:
+      priv->input = g_value_dup_object (value);
+      break;
+
+    case PROP_OUTPUT:
+      priv->output = g_value_dup_object (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static void
 goa_mail_auth_init (GoaMailAuth *client)
 {
+  client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client, GOA_TYPE_MAIL_AUTH, GoaMailAuthPrivate);
 }
 
 static void
 goa_mail_auth_class_init (GoaMailAuthClass *klass)
 {
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->dispose      = goa_mail_auth_dispose;
+  gobject_class->get_property = goa_mail_auth_get_property;
+  gobject_class->set_property = goa_mail_auth_set_property;
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_INPUT,
+                                   g_param_spec_object ("input",
+                                                        "input",
+                                                        "input",
+                                                        G_TYPE_DATA_INPUT_STREAM,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_OUTPUT,
+                                   g_param_spec_object ("output",
+                                                        "output",
+                                                        "output",
+                                                        G_TYPE_DATA_OUTPUT_STREAM,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_STATIC_STRINGS));
+
+  g_type_class_add_private (klass, sizeof (GoaMailAuthPrivate));
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -125,39 +180,28 @@ goa_mail_auth_is_needed (GoaMailAuth *auth)
 
 gboolean
 goa_mail_auth_run_sync (GoaMailAuth         *auth,
-                        GDataInputStream    *input,
-                        GDataOutputStream   *output,
                         GCancellable        *cancellable,
                         GError             **error)
 {
   g_return_val_if_fail (GOA_IS_MAIL_AUTH (auth), FALSE);
-  g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (input), FALSE);
-  g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (output), FALSE);
   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
-  return GOA_MAIL_AUTH_GET_CLASS (auth)->run_sync (auth, input, output, cancellable, error);
+  return GOA_MAIL_AUTH_GET_CLASS (auth)->run_sync (auth, cancellable, error);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
 
 void
 goa_mail_auth_run (GoaMailAuth         *auth,
-                   GDataInputStream    *input,
-                   GDataOutputStream   *output,
                    GCancellable        *cancellable,
                    GAsyncReadyCallback  callback,
                    gpointer             user_data)
 {
   GSimpleAsyncResult *simple;
-  RunData *data;
 
   g_return_if_fail (GOA_IS_MAIL_AUTH (auth));
-  g_return_if_fail (G_IS_DATA_INPUT_STREAM (input));
-  g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (output));
   g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
 
-  data = mail_auth_run_data_new (input, output, callback, user_data);
-
-  simple = g_simple_async_result_new (G_OBJECT (auth), mail_auth_run_async_cb, data, goa_mail_auth_run);
+  simple = g_simple_async_result_new (G_OBJECT (auth), callback, user_data, goa_mail_auth_run);
   g_simple_async_result_set_handle_cancellation (simple, TRUE);
 
   g_simple_async_result_run_in_thread (simple, mail_auth_run_in_thread_func, G_PRIORITY_DEFAULT, 
cancellable);
@@ -182,3 +226,45 @@ goa_mail_auth_run_finish (GoaMailAuth         *auth,
 
   return g_simple_async_result_get_op_res_gboolean (simple);
 }
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+GDataInputStream *
+goa_mail_auth_get_input (GoaMailAuth *auth)
+{
+  return auth->priv->input;
+}
+
+void
+goa_mail_auth_set_input (GoaMailAuth      *auth,
+                         GDataInputStream *input)
+{
+  GoaMailAuthPrivate *priv = auth->priv;
+
+  if (priv->input == input)
+    return;
+
+  g_clear_object (&priv->input);
+  priv->input = g_object_ref (input);
+  g_object_notify (G_OBJECT (auth), "input");
+}
+
+GDataOutputStream *
+goa_mail_auth_get_output (GoaMailAuth  *auth)
+{
+  return auth->priv->output;
+}
+
+void
+goa_mail_auth_set_output (GoaMailAuth       *auth,
+                          GDataOutputStream *output)
+{
+  GoaMailAuthPrivate *priv = auth->priv;
+
+  if (priv->output == output)
+    return;
+
+  g_clear_object (&priv->output);
+  priv->output = g_object_ref (output);
+  g_object_notify (G_OBJECT (auth), "output");
+}
diff --git a/src/goabackend/goamailauth.h b/src/goabackend/goamailauth.h
index 841e78e..6cdff1d 100644
--- a/src/goabackend/goamailauth.h
+++ b/src/goabackend/goamailauth.h
@@ -57,8 +57,6 @@ struct _GoaMailAuthClass
   GObjectClass parent_class;
   gboolean (*is_needed) (GoaMailAuth        *auth);
   gboolean (*run_sync) (GoaMailAuth         *auth,
-                        GDataInputStream    *input,
-                        GDataOutputStream   *output,
                         GCancellable        *cancellable,
                         GError             **error);
 };
@@ -66,8 +64,6 @@ struct _GoaMailAuthClass
 GType     goa_mail_auth_get_type     (void) G_GNUC_CONST;
 gboolean  goa_mail_auth_is_needed    (GoaMailAuth         *auth);
 void      goa_mail_auth_run          (GoaMailAuth         *auth,
-                                      GDataInputStream    *input,
-                                      GDataOutputStream   *output,
                                       GCancellable        *cancellable,
                                       GAsyncReadyCallback  callback,
                                       gpointer             user_data);
@@ -75,10 +71,14 @@ gboolean  goa_mail_auth_run_finish   (GoaMailAuth         *auth,
                                       GAsyncResult        *res,
                                       GError             **error);
 gboolean  goa_mail_auth_run_sync     (GoaMailAuth         *auth,
-                                      GDataInputStream    *input,
-                                      GDataOutputStream   *output,
                                       GCancellable        *cancellable,
                                       GError             **error);
+GDataInputStream *goa_mail_auth_get_input (GoaMailAuth    *auth);
+void      goa_mail_auth_set_input    (GoaMailAuth         *auth,
+                                      GDataInputStream    *input);
+GDataOutputStream *goa_mail_auth_get_output (GoaMailAuth  *auth);
+void      goa_mail_auth_set_output   (GoaMailAuth         *auth,
+                                      GDataOutputStream   *input);
 
 G_END_DECLS
 
diff --git a/src/goabackend/goamailclient.c b/src/goabackend/goamailclient.c
index 9d15eda..8449d54 100644
--- a/src/goabackend/goamailclient.c
+++ b/src/goabackend/goamailclient.c
@@ -165,6 +165,8 @@ static void
 mail_client_check_connect_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   CheckData *data = user_data;
+  GDataInputStream *input;
+  GDataOutputStream *output;
   GInputStream *base_input;
   GError *error;
   GOutputStream *base_output;
@@ -202,15 +204,19 @@ mail_client_check_connect_cb (GObject *source_object, GAsyncResult *res, gpointe
   g_socket_set_timeout (socket, COMMAND_TIMEOUT_SEC);
 
   base_input = g_io_stream_get_input_stream (G_IO_STREAM (data->conn));
-  data->input = g_data_input_stream_new (base_input);
-  g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (data->input), FALSE);
-  g_data_input_stream_set_newline_type (data->input, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
+  input = g_data_input_stream_new (base_input);
+  g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (input), FALSE);
+  g_data_input_stream_set_newline_type (input, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
+  goa_mail_auth_set_input (data->auth, input);
+  g_object_unref (input);
 
   base_output = g_io_stream_get_output_stream (G_IO_STREAM (data->conn));
-  data->output = g_data_output_stream_new (base_output);
-  g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (data->output), FALSE);
+  output = g_data_output_stream_new (base_output);
+  g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (output), FALSE);
+  goa_mail_auth_set_output (data->auth, output);
+  g_object_unref (output);
 
-  goa_mail_auth_run (data->auth, data->input, data->output, data->cancellable, mail_client_check_auth_cb, 
data);
+  goa_mail_auth_run (data->auth, data->cancellable, mail_client_check_auth_cb, data);
   return;
 
  error:
diff --git a/src/goabackend/goasmtpauthplain.c b/src/goabackend/goasmtpauthplain.c
index a735008..b0049a8 100644
--- a/src/goabackend/goasmtpauthplain.c
+++ b/src/goabackend/goasmtpauthplain.c
@@ -77,8 +77,6 @@ enum
 
 static gboolean goa_smtp_auth_plain_is_needed (GoaMailAuth        *_auth);
 static gboolean goa_smtp_auth_plain_run_sync (GoaMailAuth         *_auth,
-                                              GDataInputStream    *input,
-                                              GDataOutputStream   *output,
                                               GCancellable        *cancellable,
                                               GError             **error);
 
@@ -336,12 +334,12 @@ goa_smtp_auth_plain_is_needed (GoaMailAuth *_auth)
 
 static gboolean
 goa_smtp_auth_plain_run_sync (GoaMailAuth         *_auth,
-                              GDataInputStream    *input,
-                              GDataOutputStream   *output,
                               GCancellable        *cancellable,
                               GError             **error)
 {
   GoaSmtpAuthPlain *auth = GOA_SMTP_AUTH_PLAIN (_auth);
+  GDataInputStream *input;
+  GDataOutputStream *output;
   gboolean plain_supported;
   gboolean ret;
   gchar *auth_arg_base64;
@@ -439,6 +437,9 @@ goa_smtp_auth_plain_run_sync (GoaMailAuth         *_auth,
       goto out;
     }
 
+  input = goa_mail_auth_get_input (_auth);
+  output = goa_mail_auth_get_output (_auth);
+
   /* Check the greeting */
 
   response = g_data_input_stream_read_line (input, NULL, cancellable, error);


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