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



commit 16d32c4e3390c282851c944de41ea1bfac00326f
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Mar 1 13:30:55 2016 +0100

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

 panels/printers/cc-printers-panel.c |  101 ++++++++++++++++++++++++------
 panels/printers/pp-cups.c           |  115 +++++++++++++++++++++++++++++++++++
 panels/printers/pp-cups.h           |   11 +++
 panels/printers/pp-utils.c          |   75 -----------------------
 panels/printers/pp-utils.h          |    5 --
 5 files changed, 206 insertions(+), 101 deletions(-)
---
diff --git a/panels/printers/cc-printers-panel.c b/panels/printers/cc-printers-panel.c
index 1159495..f6d82cc 100644
--- a/panels/printers/cc-printers-panel.c
+++ b/panels/printers/cc-printers-panel.c
@@ -104,6 +104,7 @@ struct _CcPrintersPanelPrivate
   PPDList      *all_ppds_list;
   GHashTable   *preferred_drivers;
   GCancellable *get_all_ppds_cancellable;
+  GCancellable *subscription_renew_cancellable;
 
   gchar    *new_printer_name;
   gchar    *new_printer_location;
@@ -186,6 +187,9 @@ cc_printers_panel_dispose (GObject *object)
       priv->permission = NULL;
     }
 
+  g_cancellable_cancel (priv->subscription_renew_cancellable);
+  g_clear_object (&priv->subscription_renew_cancellable);
+
   detach_from_cups_notifier (CC_PRINTERS_PANEL (object));
 
   if (priv->cups_status_check_id > 0)
@@ -401,43 +405,77 @@ on_cups_notification (GDBusConnection *connection,
     }
 }
 
+static gchar *subscription_events[] = {
+  "printer-added",
+  "printer-deleted",
+  "printer-stopped",
+  "printer-state-changed",
+  "job-created",
+  "job-completed",
+  NULL};
+
+static void
+renew_subscription_cb (GObject      *source_object,
+                      GAsyncResult *result,
+                      gpointer      user_data)
+{
+  CcPrintersPanelPrivate *priv;
+  CcPrintersPanel        *self = (CcPrintersPanel*) user_data;
+  PpCups                 *cups = PP_CUPS (source_object);
+  gint                    subscription_id;
+
+  subscription_id = pp_cups_renew_subscription_finish (cups, result);
+  g_object_unref (source_object);
+
+  if (subscription_id > 0)
+    {
+      priv = self->priv;
+
+      priv->subscription_id = subscription_id;
+    }
+}
+
 static gboolean
 renew_subscription (gpointer data)
 {
   CcPrintersPanelPrivate *priv;
   CcPrintersPanel        *self = (CcPrintersPanel*) data;
-  static const char * const events[] = {
-          "printer-added",
-          "printer-deleted",
-          "printer-stopped",
-          "printer-state-changed",
-          "job-created",
-          "job-completed"};
+  PpCups                 *cups;
 
   priv = PRINTERS_PANEL_PRIVATE (self);
 
-  priv->subscription_id = renew_cups_subscription (priv->subscription_id,
-                                                   events,
-                                                   G_N_ELEMENTS (events),
-                                                   SUBSCRIPTION_DURATION);
-
-  if (priv->subscription_id > 0)
-    return TRUE;
-  else
-    return FALSE;
+  cups = pp_cups_new ();
+  pp_cups_renew_subscription_async (cups,
+                                    priv->subscription_id,
+                                    subscription_events,
+                                    SUBSCRIPTION_DURATION,
+                                    priv->subscription_renew_cancellable,
+                                    renew_subscription_cb,
+                                    data);
+
+  return G_SOURCE_CONTINUE;
 }
 
 static void
-attach_to_cups_notifier (gpointer data)
+attach_to_cups_notifier_cb (GObject      *source_object,
+                            GAsyncResult *result,
+                            gpointer      user_data)
 {
   CcPrintersPanelPrivate *priv;
-  CcPrintersPanel        *self = (CcPrintersPanel*) data;
+  CcPrintersPanel        *self = (CcPrintersPanel*) user_data;
+  PpCups                 *cups = PP_CUPS (source_object);
   GError                 *error = NULL;
+  gint                    subscription_id;
 
-  priv = PRINTERS_PANEL_PRIVATE (self);
+  subscription_id = pp_cups_renew_subscription_finish (cups, result);
+  g_object_unref (source_object);
 
-  if (renew_subscription (self))
+  if (subscription_id > 0)
     {
+      priv = self->priv;
+
+      priv->subscription_id = subscription_id;
+
       priv->subscription_renewal_id =
         g_timeout_add_seconds (RENEW_INTERVAL, renew_subscription, self);
 
@@ -473,7 +511,26 @@ attach_to_cups_notifier (gpointer data)
     }
 }
 
- static void
+static void
+attach_to_cups_notifier (gpointer data)
+{
+  CcPrintersPanelPrivate *priv;
+  CcPrintersPanel        *self = (CcPrintersPanel*) data;
+  PpCups                 *cups;
+
+  priv = self->priv;
+
+  cups = pp_cups_new ();
+  pp_cups_renew_subscription_async (cups,
+                                    priv->subscription_id,
+                                    subscription_events,
+                                    SUBSCRIPTION_DURATION,
+                                    priv->subscription_renew_cancellable,
+                                    attach_to_cups_notifier_cb,
+                                    data);
+}
+
+static void
 subscription_cancel_cb (GObject      *source_object,
                         GAsyncResult *result,
                         gpointer      user_data)
@@ -3142,6 +3199,8 @@ cc_printers_panel_init (CcPrintersPanel *self)
 \"org.opensuse.cupspkhelper.mechanism.all-edit\" installed. \
 Please check your installation");
 
+  priv->subscription_renew_cancellable = g_cancellable_new ();
+
   populate_printers_list (self);
   attach_to_cups_notifier (self);
 
diff --git a/panels/printers/pp-cups.c b/panels/printers/pp-cups.c
index cb0a921..b6692ed 100644
--- a/panels/printers/pp-cups.c
+++ b/panels/printers/pp-cups.c
@@ -177,3 +177,118 @@ pp_cups_cancel_subscription_finish (PpCups       *cups,
 
   return g_task_propagate_boolean (G_TASK (result), NULL);
 }
+
+typedef struct {
+  gint id;
+  gchar **events;
+  int lease_duration;
+} CRSData;
+
+static void
+crs_data_free (CRSData *data)
+{
+  g_strfreev (data->events);
+  g_slice_free (CRSData, data);
+}
+
+static void
+renew_subscription_thread (GTask        *task,
+                           gpointer      source_object,
+                           gpointer      task_data,
+                           GCancellable *cancellable)
+{
+  ipp_attribute_t *attr = NULL;
+  CRSData         *subscription_data = task_data;
+  ipp_t           *request;
+  ipp_t           *response = NULL;
+  gint             result = -1;
+
+  if (g_cancellable_is_cancelled (cancellable))
+    return;
+
+  if (subscription_data->id > 0)
+    {
+      request = ippNewRequest (IPP_RENEW_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", subscription_data->id);
+      ippAddInteger (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
+                    "notify-lease-duration", subscription_data->lease_duration);
+      response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
+      if (response != NULL && ippGetStatusCode (response) <= IPP_OK_CONFLICT)
+        {
+          if ((attr = ippFindAttribute (response, "notify-lease-duration", IPP_TAG_INTEGER)) == NULL)
+            g_debug ("No notify-lease-duration in response!\n");
+          else if (ippGetInteger (attr, 0) == subscription_data->lease_duration)
+            result = subscription_data->id;
+        }
+    }
+
+  if (result < 0)
+    {
+      request = ippNewRequest (IPP_CREATE_PRINTER_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 ());
+      ippAddStrings (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
+                    "notify-events", g_strv_length (subscription_data->events), NULL,
+                     (const char * const *) subscription_data->events);
+      ippAddString (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
+                   "notify-pull-method", NULL, "ippget");
+      ippAddString (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
+                   "notify-recipient-uri", NULL, "dbus://");
+      ippAddInteger (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
+                    "notify-lease-duration", subscription_data->lease_duration);
+      response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
+
+      if (response != NULL && ippGetStatusCode (response) <= IPP_OK_CONFLICT)
+        {
+          if ((attr = ippFindAttribute (response, "notify-subscription-id", IPP_TAG_INTEGER)) == NULL)
+            g_debug ("No notify-subscription-id in response!\n");
+          else
+            result = ippGetInteger (attr, 0);
+        }
+    }
+
+  ippDelete (response);
+
+  g_task_return_int (task, result);
+}
+
+void
+pp_cups_renew_subscription_async  (PpCups               *cups,
+                                   gint                  subscription_id,
+                                   gchar               **events,
+                                   gint                  lease_duration,
+                                   GCancellable         *cancellable,
+                                   GAsyncReadyCallback   callback,
+                                   gpointer              user_data)
+{
+  CRSData *subscription_data;
+  GTask   *task;
+
+  subscription_data = g_slice_new (CRSData);
+  subscription_data->id = subscription_id;
+  subscription_data->events = g_strdupv (events);
+  subscription_data->lease_duration = lease_duration;
+
+  task = g_task_new (cups, cancellable, callback, user_data);
+  g_task_set_task_data (task, subscription_data, (GDestroyNotify) crs_data_free);
+  g_task_run_in_thread (task, renew_subscription_thread);
+
+  g_object_unref (task);
+}
+
+/* Returns id of renewed subscription or new id */
+gint
+pp_cups_renew_subscription_finish (PpCups       *cups,
+                                   GAsyncResult *result)
+{
+  g_return_val_if_fail (g_task_is_valid (result, cups), FALSE);
+
+  return g_task_propagate_int (G_TASK (result), NULL);
+}
diff --git a/panels/printers/pp-cups.h b/panels/printers/pp-cups.h
index 68fab6d..863ca00 100644
--- a/panels/printers/pp-cups.h
+++ b/panels/printers/pp-cups.h
@@ -80,6 +80,17 @@ void         pp_cups_cancel_subscription_async    (PpCups              *cups,
 gboolean     pp_cups_cancel_subscription_finish   (PpCups                *cups,
                                                    GAsyncResult          *result);
 
+void         pp_cups_renew_subscription_async  (PpCups                *cups,
+                                                gint                   subscription_id,
+                                                gchar                **events,
+                                                gint                   lease_duration,
+                                                GCancellable          *cancellable,
+                                                GAsyncReadyCallback    callback,
+                                                gpointer               user_data);
+
+gint         pp_cups_renew_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 685eb3f..dc66061 100644
--- a/panels/printers/pp-utils.c
+++ b/panels/printers/pp-utils.c
@@ -226,81 +226,6 @@ get_ppd_attribute (const gchar *ppd_file_name,
   return result;
 }
 
-/* Returns id of renewed subscription or new id */
-gint
-renew_cups_subscription (gint id,
-                         const char * const *events,
-                         gint num_events,
-                         gint lease_duration)
-{
-  ipp_attribute_t              *attr = NULL;
-  http_t                       *http;
-  ipp_t                        *request;
-  ipp_t                        *response = NULL;
-  gint                          result = -1;
-
-  if ((http = httpConnectEncrypt (cupsServer (), ippPort (),
-                                  cupsEncryption ())) == NULL) {
-    g_debug ("Connection to CUPS server \'%s\' failed.", cupsServer ());
-  }
-  else {
-    if (id >= 0) {
-      request = ippNewRequest (IPP_RENEW_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);
-      ippAddInteger (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
-                    "notify-lease-duration", lease_duration);
-      response = cupsDoRequest (http, request, "/");
-      if (response != NULL &&
-          ippGetStatusCode (response) <= IPP_OK_CONFLICT) {
-        if ((attr = ippFindAttribute (response, "notify-lease-duration",
-                                      IPP_TAG_INTEGER)) == NULL)
-          g_debug ("No notify-lease-duration in response!\n");
-        else
-          if (ippGetInteger (attr, 0) == lease_duration)
-            result = id;
-      }
-    }
-
-    if (result < 0) {
-      request = ippNewRequest (IPP_CREATE_PRINTER_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 ());
-      ippAddStrings (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
-                     "notify-events", num_events, NULL, events);
-      ippAddString (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
-                    "notify-pull-method", NULL, "ippget");
-      ippAddString (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
-                    "notify-recipient-uri", NULL, "dbus://");
-      ippAddInteger (request, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
-                     "notify-lease-duration", lease_duration);
-      response = cupsDoRequest (http, request, "/");
-
-      if (response != NULL &&
-          ippGetStatusCode (response) <= IPP_OK_CONFLICT) {
-        if ((attr = ippFindAttribute (response, "notify-subscription-id",
-                                      IPP_TAG_INTEGER)) == NULL)
-          g_debug ("No notify-subscription-id in response!\n");
-        else
-          result = ippGetInteger (attr, 0);
-      }
-    }
-
-    if (response)
-      ippDelete (response);
-
-    httpClose (http);
-  }
-
-  return result;
-}
-
 /*  Set default destination in ~/.cups/lpoptions.
  *  Unset default destination if "dest" is NULL.
  */
diff --git a/panels/printers/pp-utils.h b/panels/printers/pp-utils.h
index ed3abf1..17dbaf7 100644
--- a/panels/printers/pp-utils.h
+++ b/panels/printers/pp-utils.h
@@ -95,11 +95,6 @@ char       *get_dest_attr (const char *dest_name,
 gchar      *get_ppd_attribute (const gchar *ppd_file_name,
                                const gchar *attribute_name);
 
-gint        renew_cups_subscription (gint id,
-                                     const char * const *events,
-                                     gint num_events,
-                                     gint lease_duration);
-
 void        set_local_default_printer (const gchar *printer_name);
 
 gboolean    printer_set_location (const gchar *printer_name,


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