[gnome-control-center] printers: do async connection test during launch of panel



commit 0a7cfa47fb84b2a4024123f5943e33875850569e
Author: Felipe Borges <felipeborges gnome org>
Date:   Wed Feb 17 10:56:16 2016 +0100

    printers: do async connection test during launch of panel
    
    https://bugzilla.gnome.org/show_bug.cgi?id=748336

 panels/printers/cc-printers-panel.c |   73 +++++++++++++++++++++++++---------
 panels/printers/pp-cups.c           |   36 +++++++++++++++++
 panels/printers/pp-cups.h           |    7 +++
 3 files changed, 96 insertions(+), 20 deletions(-)
---
diff --git a/panels/printers/cc-printers-panel.c b/panels/printers/cc-printers-panel.c
index b7cce46..5727eb7 100644
--- a/panels/printers/cc-printers-panel.c
+++ b/panels/printers/cc-printers-panel.c
@@ -2778,27 +2778,66 @@ printer_options_cb (GtkToolButton *toolbutton,
     }
 }
 
+static void
+cups_status_check_cb (GObject      *source_object,
+                      GAsyncResult *result,
+                      gpointer      user_data)
+{
+  CcPrintersPanelPrivate *priv;
+  CcPrintersPanel        *self = (CcPrintersPanel*) user_data;
+  gboolean                success;
+  PpCups                 *cups = PP_CUPS (source_object);
+
+  priv = self->priv;
+
+  success = pp_cups_connection_test_finish (cups, result);
+  if (success)
+    {
+      actualize_printers_list (self);
+      attach_to_cups_notifier (self);
+
+      g_source_remove (priv->cups_status_check_id);
+      priv->cups_status_check_id = 0;
+    }
+
+  g_object_unref (cups);
+}
+
 static gboolean
 cups_status_check (gpointer user_data)
 {
   CcPrintersPanelPrivate  *priv;
   CcPrintersPanel         *self = (CcPrintersPanel*) user_data;
-  gboolean                 result = TRUE;
-  http_t                  *http;
+  PpCups                  *cups;
 
-  priv = self->priv = PRINTERS_PANEL_PRIVATE (self);
+  priv = self->priv;
+
+  cups = pp_cups_new ();
+  pp_cups_connection_test_async (cups, cups_status_check_cb, self);
 
-  http = httpConnectEncrypt (cupsServer (), ippPort (), cupsEncryption ());
-  if (http)
+  return priv->cups_status_check_id != 0;
+}
+
+static void
+connection_test_cb (GObject      *source_object,
+                    GAsyncResult *result,
+                    gpointer      user_data)
+{
+  CcPrintersPanelPrivate *priv;
+  CcPrintersPanel        *self = (CcPrintersPanel*) user_data;
+  gboolean                success;
+  PpCups                 *cups = PP_CUPS (source_object);
+
+  priv = self->priv;
+
+  success = pp_cups_connection_test_finish (cups, result);
+  if (!success)
     {
-      httpClose (http);
-      actualize_printers_list (self);
-      attach_to_cups_notifier (self);
-      priv->cups_status_check_id = 0;
-      result = FALSE;
+      priv->cups_status_check_id =
+        g_timeout_add_seconds (CUPS_STATUS_CHECK_INTERVAL, cups_status_check, self);
     }
 
-  return result;
+  g_object_unref (cups);
 }
 
 static void
@@ -2905,8 +2944,8 @@ cc_printers_panel_init (CcPrintersPanel *self)
   CcPrintersPanelPrivate *priv;
   GtkWidget              *top_widget;
   GtkWidget              *widget;
+  PpCups                 *cups;
   GError                 *error = NULL;
-  http_t                 *http;
   gchar                  *objects[] = { "main-vbox", NULL };
   GtkStyleContext        *context;
   guint                   builder_result;
@@ -3068,14 +3107,8 @@ Please check your installation");
                       get_all_ppds_async_cb,
                       self);
 
-  http = httpConnectEncrypt (cupsServer (), ippPort (), cupsEncryption ());
-  if (!http)
-    {
-      priv->cups_status_check_id =
-        g_timeout_add_seconds (CUPS_STATUS_CHECK_INTERVAL, cups_status_check, self);
-    }
-  else
-    httpClose (http);
+  cups = pp_cups_new ();
+  pp_cups_connection_test_async (cups, connection_test_cb, self);
 
   gtk_container_add (GTK_CONTAINER (self), top_widget);
   gtk_widget_show_all (GTK_WIDGET (self));
diff --git a/panels/printers/pp-cups.c b/panels/printers/pp-cups.c
index 3af57b2..1e38e98 100644
--- a/panels/printers/pp-cups.c
+++ b/panels/printers/pp-cups.c
@@ -89,3 +89,39 @@ pp_cups_get_dests_finish (PpCups        *cups,
 
   return g_task_propagate_pointer (G_TASK (res), error);
 }
+
+static void
+connection_test_thread (GTask        *task,
+                        gpointer      source_object,
+                        gpointer      task_data,
+                        GCancellable *cancellable)
+{
+  http_t *http;
+
+  http = httpConnectEncrypt (cupsServer (), ippPort (), cupsEncryption ());
+  g_task_return_boolean (task, http != NULL);
+
+  httpClose (http);
+}
+
+void
+pp_cups_connection_test_async (PpCups              *cups,
+                               GAsyncReadyCallback  callback,
+                               gpointer             user_data)
+{
+  GTask *task;
+
+  task = g_task_new (cups, NULL, callback, user_data);
+  g_task_run_in_thread (task, connection_test_thread);
+
+  g_object_unref (task);
+}
+
+gboolean
+pp_cups_connection_test_finish (PpCups         *cups,
+                                GAsyncResult   *result)
+{
+  g_return_val_if_fail (g_task_is_valid (result, cups), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (result), NULL);
+}
diff --git a/panels/printers/pp-cups.h b/panels/printers/pp-cups.h
index a17766c..89a06d1 100644
--- a/panels/printers/pp-cups.h
+++ b/panels/printers/pp-cups.h
@@ -65,6 +65,13 @@ PpCupsDests *pp_cups_get_dests_finish (PpCups               *cups,
                                        GAsyncResult         *result,
                                        GError              **error);
 
+void         pp_cups_connection_test_async (PpCups              *cups,
+                                            GAsyncReadyCallback  callback,
+                                            gpointer             user_data);
+
+gboolean     pp_cups_connection_test_finish (PpCups         *cups,
+                                             GAsyncResult   *result);
+
 G_END_DECLS
 
 #endif /* __PP_CUPS_H__ */


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