[gnome-control-center] printers: cancel cups subscriptions asynchronously



commit 9f9c63fe8d1fe67c4989b87342589f8e09022583
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Mar 1 11:54:56 2016 +0100

    printers: cancel cups subscriptions asynchronously
    
    https://bugzilla.gnome.org/show_bug.cgi?id=748336

 panels/printers/cc-printers-panel.c |   19 ++++++++++++-
 panels/printers/pp-cups.c           |   52 +++++++++++++++++++++++++++++++++++
 panels/printers/pp-cups.h           |    8 +++++
 panels/printers/pp-utils.c          |   22 ---------------
 panels/printers/pp-utils.h          |    2 -
 5 files changed, 78 insertions(+), 25 deletions(-)
---
diff --git a/panels/printers/cc-printers-panel.c b/panels/printers/cc-printers-panel.c
index c25da05..e4ec22b 100644
--- a/panels/printers/cc-printers-panel.c
+++ b/panels/printers/cc-printers-panel.c
@@ -458,11 +458,23 @@ attach_to_cups_notifier (gpointer data)
     }
 }
 
+ static void
+subscription_cancel_cb (GObject      *source_object,
+                        GAsyncResult *result,
+                        gpointer      user_data)
+{
+  PpCups *cups = PP_CUPS (source_object);
+
+  pp_cups_cancel_subscription_finish (cups, result);
+  g_object_unref (source_object);
+}
+
 static void
 detach_from_cups_notifier (gpointer data)
 {
   CcPrintersPanelPrivate *priv;
   CcPrintersPanel        *self = (CcPrintersPanel*) data;
+  PpCups                 *cups;
 
   priv = PRINTERS_PANEL_PRIVATE (self);
 
@@ -472,7 +484,12 @@ detach_from_cups_notifier (gpointer data)
     priv->dbus_subscription_id = 0;
   }
 
-  cancel_cups_subscription (priv->subscription_id);
+  cups = pp_cups_new ();
+  pp_cups_cancel_subscription_async (cups,
+                                     priv->subscription_id,
+                                     subscription_cancel_cb,
+                                     NULL);
+
   priv->subscription_id = 0;
 
   if (priv->subscription_renewal_id != 0) {
diff --git a/panels/printers/pp-cups.c b/panels/printers/pp-cups.c
index 1e38e98..cb0a921 100644
--- a/panels/printers/pp-cups.c
+++ b/panels/printers/pp-cups.c
@@ -125,3 +125,55 @@ pp_cups_connection_test_finish (PpCups         *cups,
 
   return g_task_propagate_boolean (G_TASK (result), NULL);
 }
+
+/* Cancels subscription of given id */
+static void
+cancel_subscription_thread (GTask        *task,
+                            gpointer      source_object,
+                            gpointer      task_data,
+                            GCancellable *cancellable)
+{
+  ipp_t *request;
+  ipp_t *response = NULL;
+  gint   id = GPOINTER_TO_INT (task_data);
+
+  if (id >= 0)
+    {
+      request = ippNewRequest (IPP_CANCEL_SUBSCRIPTION);
+      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
+                    "printer-uri", NULL, "/");
+      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+                    "requesting-user-name", NULL, cupsUser ());
+      ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
+                     "notify-subscription-id", id);
+      response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
+    }
+
+  g_task_return_boolean (task, response != NULL && ippGetStatusCode (response) <= IPP_OK);
+
+  ippDelete (response);
+}
+
+void
+pp_cups_cancel_subscription_async (PpCups              *cups,
+                                   gint                 subscription_id,
+                                   GAsyncReadyCallback  callback,
+                                   gpointer             user_data)
+{
+  GTask *task;
+
+  task = g_task_new (cups, NULL, callback, user_data);
+  g_task_set_task_data (task, GINT_TO_POINTER (subscription_id), NULL);
+  g_task_run_in_thread (task, cancel_subscription_thread);
+
+  g_object_unref (task);
+}
+
+gboolean
+pp_cups_cancel_subscription_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 89a06d1..68fab6d 100644
--- a/panels/printers/pp-cups.h
+++ b/panels/printers/pp-cups.h
@@ -72,6 +72,14 @@ void         pp_cups_connection_test_async (PpCups              *cups,
 gboolean     pp_cups_connection_test_finish (PpCups         *cups,
                                              GAsyncResult   *result);
 
+void         pp_cups_cancel_subscription_async    (PpCups              *cups,
+                                                   gint                 subscription_id,
+                                                   GAsyncReadyCallback  callback,
+                                                   gpointer             user_data);
+
+gboolean     pp_cups_cancel_subscription_finish   (PpCups                *cups,
+                                                   GAsyncResult          *result);
+
 G_END_DECLS
 
 #endif /* __PP_CUPS_H__ */
diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c
index 151d337..685eb3f 100644
--- a/panels/printers/pp-utils.c
+++ b/panels/printers/pp-utils.c
@@ -226,28 +226,6 @@ get_ppd_attribute (const gchar *ppd_file_name,
   return result;
 }
 
-/* Cancels subscription of given id */
-void
-cancel_cups_subscription (gint id)
-{
-  http_t *http;
-  ipp_t  *request;
-
-  if (id >= 0 &&
-      ((http = httpConnectEncrypt (cupsServer (), ippPort (),
-                                  cupsEncryption ())) != NULL)) {
-    request = ippNewRequest (IPP_CANCEL_SUBSCRIPTION);
-    ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
-                 "printer-uri", NULL, "/");
-    ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
-                 "requesting-user-name", NULL, cupsUser ());
-    ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
-                  "notify-subscription-id", id);
-    ippDelete (cupsDoRequest (http, request, "/"));
-    httpClose (http);
-  }
-}
-
 /* Returns id of renewed subscription or new id */
 gint
 renew_cups_subscription (gint id,
diff --git a/panels/printers/pp-utils.h b/panels/printers/pp-utils.h
index 9783823..ed3abf1 100644
--- a/panels/printers/pp-utils.h
+++ b/panels/printers/pp-utils.h
@@ -95,8 +95,6 @@ char       *get_dest_attr (const char *dest_name,
 gchar      *get_ppd_attribute (const gchar *ppd_file_name,
                                const gchar *attribute_name);
 
-void        cancel_cups_subscription (gint id);
-
 gint        renew_cups_subscription (gint id,
                                      const char * const *events,
                                      gint num_events,


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