[gnome-control-center/wip/msanchez/printers-clean-heads: 1/3] printers: Added new async API to check availability of maintenance commands



commit 5da4253a53d7821b25a8079c460178c990b2be6d
Author: Mario Sanchez Prada <mario endlessm com>
Date:   Thu Apr 28 18:58:20 2016 +0100

    printers: Added new async API to check availability of maintenance commands
    
    This cleans the code up a bit so that we can extract part of the logic
    from _pp_maintenance_command_execute_thread() before getting into fixing
    the problem in the logic checking whether a CUPS command is available.
    
    Besides, it will be useful to have this logic extracted as it will be used
    later on from pp-printer-entry.c to know whether the "Clean" command is
    available, in order to show a menu item "Clean Print Heads" (bug 764620).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=766861

 panels/printers/pp-maintenance-command.c |  220 +++++++++++++++++++-----------
 panels/printers/pp-maintenance-command.h |   29 +++--
 2 files changed, 159 insertions(+), 90 deletions(-)
---
diff --git a/panels/printers/pp-maintenance-command.c b/panels/printers/pp-maintenance-command.c
index 899ce6c..fe399f5 100644
--- a/panels/printers/pp-maintenance-command.c
+++ b/panels/printers/pp-maintenance-command.c
@@ -177,6 +177,9 @@ pp_maintenance_command_new (const gchar *printer_name,
                        NULL);
 }
 
+static gboolean _pp_maintenance_command_is_supported (const gchar *printer_name,
+                                                      const gchar *command);
+
 static void
 _pp_maintenance_command_execute_thread (GSimpleAsyncResult *res,
                                         GObject            *object,
@@ -184,104 +187,61 @@ _pp_maintenance_command_execute_thread (GSimpleAsyncResult *res,
 {
   PpMaintenanceCommand        *command = (PpMaintenanceCommand *) object;
   PpMaintenanceCommandPrivate *priv = command->priv;
-  static const char           *attrs[] = {"printer-commands"};
-  ipp_attribute_t             *attr = NULL;
   gboolean                     success = FALSE;
   GError                      *error = NULL;
-  ipp_t                       *request;
-  ipp_t                       *response = NULL;
-  gchar                       *printer_uri;
-  gchar                       *printer_commands = NULL;
-  gchar                       *printer_commands_lowercase = NULL;
-  gchar                       *command_lowercase;
-  gchar                       *file_name = NULL;
-  int                          fd = -1;
 
-  printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
-                                 priv->printer_name);
+  if (_pp_maintenance_command_is_supported (priv->printer_name, priv->command))
+    {
+      ipp_t *request;
+      ipp_t *response = NULL;
+      gchar *printer_uri;
+      gchar *file_name = NULL;
+      int    fd = -1;
 
-  request = ippNewRequest (IPP_GET_PRINTER_ATTRIBUTES);
-  ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
-                "printer-uri", NULL, printer_uri);
-  ippAddStrings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
-                 "requested-attributes", 1, NULL, attrs);
-  response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
+      printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
+                                     priv->printer_name);
 
-  if (response)
-    {
-      if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
-        {
-          attr = ippFindAttribute (response, "printer-commands", IPP_TAG_ZERO);
-          if (attr && ippGetCount (attr) > 0 && ippGetValueTag (attr) != IPP_TAG_NOVALUE)
-            {
-              if (ippGetValueTag (attr) == IPP_TAG_KEYWORD)
-                {
-                  printer_commands = g_strdup (ippGetString (attr, 0, NULL));
-                }
-            }
-          else
-            {
-              success = TRUE;
-            }
-        }
+      request = ippNewRequest (IPP_PRINT_JOB);
 
-      ippDelete (response);
-    }
+      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
+                    "printer-uri", NULL, printer_uri);
+      ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+                    "job-name", NULL, priv->title);
+      ippAddString (request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
+                    "document-format", NULL, "application/vnd.cups-command");
 
-  if (printer_commands)
-    {
-      command_lowercase = g_ascii_strdown (priv->command, -1);
-      printer_commands_lowercase = g_ascii_strdown (printer_commands, -1);
+      fd = g_file_open_tmp ("ccXXXXXX", &file_name, &error);
 
-      if (g_strrstr (printer_commands_lowercase, command_lowercase))
+      if (fd != -1)
         {
-          request = ippNewRequest (IPP_PRINT_JOB);
+          FILE *file;
 
-          ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
-                        "printer-uri", NULL, printer_uri);
-          ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
-                        "job-name", NULL, priv->title);
-          ippAddString (request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
-                        "document-format", NULL, "application/vnd.cups-command");
+          file = fdopen (fd, "w");
+          fprintf (file, "#CUPS-COMMAND\n");
+          fprintf (file, "%s\n", priv->command);
+          fclose (file);
 
-          fd = g_file_open_tmp ("ccXXXXXX", &file_name, &error);
+          response = cupsDoFileRequest (CUPS_HTTP_DEFAULT, request, "/", file_name);
+          g_unlink (file_name);
 
-          if (fd != -1)
+          if (response != NULL)
             {
-              FILE *file;
-
-              file = fdopen (fd, "w");
-              fprintf (file, "#CUPS-COMMAND\n");
-              fprintf (file, "%s\n", priv->command);
-              fclose (file);
-
-              response = cupsDoFileRequest (CUPS_HTTP_DEFAULT, request, "/", file_name);
-              g_unlink (file_name);
-
-              if (response)
+              if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
                 {
-                  if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
-                    {
-                      success = TRUE;
-                    }
-
-                  ippDelete (response);
+                  success = TRUE;
                 }
-            }
 
-          g_free (file_name);
-        }
-      else
-        {
-          success = TRUE;
+              ippDelete (response);
+            }
         }
 
-      g_free (command_lowercase);
-      g_free (printer_commands_lowercase);
-      g_free (printer_commands);
+      g_free (file_name);
+      g_free (printer_uri);
+    }
+  else
+    {
+      success = TRUE;
     }
-
-  g_free (printer_uri);
 
   if (!success)
     {
@@ -326,3 +286,105 @@ pp_maintenance_command_execute_finish (PpMaintenanceCommand  *command,
 
   return g_simple_async_result_get_op_res_gboolean (simple);
 }
+
+static gboolean
+_pp_maintenance_command_is_supported (const gchar *printer_name,
+                                      const gchar *command)
+{
+  ipp_attribute_t   *attr = NULL;
+  gboolean           is_supported = FALSE;
+  ipp_t             *request;
+  ipp_t             *response = NULL;
+  gchar             *printer_uri;
+  gchar             *printer_commands = NULL;
+  gchar             *printer_commands_lowercase = NULL;
+  gchar             *command_lowercase;
+
+  printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
+                                 printer_name);
+
+  request = ippNewRequest (IPP_GET_PRINTER_ATTRIBUTES);
+  ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
+                "printer-uri", NULL, printer_uri);
+  ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+                "requested-attributes", NULL, "printer-commands");
+  response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
+  if (response != NULL)
+    {
+      if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
+        {
+          attr = ippFindAttribute (response, "printer-commands", IPP_TAG_ZERO);
+          if (attr != NULL && ippGetCount (attr) > 0 &&
+              ippGetValueTag (attr) != IPP_TAG_NOVALUE &&
+              (ippGetValueTag (attr) == IPP_TAG_KEYWORD))
+            {
+              printer_commands = g_strdup (ippGetString (attr, 0, NULL));
+            }
+        }
+
+      ippDelete (response);
+    }
+
+  if (printer_commands != NULL)
+    {
+      command_lowercase = g_ascii_strdown (command, -1);
+      printer_commands_lowercase = g_ascii_strdown (printer_commands, -1);
+
+      if (g_strcmp0 (printer_commands_lowercase, command_lowercase) == 0)
+        is_supported = TRUE;
+
+      g_free (command_lowercase);
+      g_free (printer_commands_lowercase);
+      g_free (printer_commands);
+    }
+
+  g_free (printer_uri);
+
+  return is_supported;
+}
+
+static void
+_pp_maintenance_command_is_supported_thread (GSimpleAsyncResult *res,
+                                             GObject            *object,
+                                             GCancellable       *cancellable)
+{
+  PpMaintenanceCommand        *command = (PpMaintenanceCommand *) object;
+  PpMaintenanceCommandPrivate *priv = command->priv;
+  gboolean                     success = FALSE;
+
+  success = _pp_maintenance_command_is_supported (priv->printer_name, priv->command);
+  g_simple_async_result_set_op_res_gboolean (res, success);
+}
+
+void
+pp_maintenance_command_is_supported_async  (PpMaintenanceCommand *command,
+                                            GCancellable         *cancellable,
+                                            GAsyncReadyCallback   callback,
+                                            gpointer              user_data)
+{
+  GSimpleAsyncResult *res;
+
+  res = g_simple_async_result_new (G_OBJECT (command), callback, user_data, 
pp_maintenance_command_is_supported_async);
+
+  g_simple_async_result_set_check_cancellable (res, cancellable);
+  g_simple_async_result_run_in_thread (res, _pp_maintenance_command_is_supported_thread, 0, cancellable);
+
+  g_object_unref (res);
+}
+
+gboolean
+pp_maintenance_command_is_supported_finish (PpMaintenanceCommand  *command,
+                                            GAsyncResult          *res,
+                                            GError               **error)
+{
+  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
+
+  g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == 
pp_maintenance_command_is_supported_async);
+
+  if (g_simple_async_result_propagate_error (simple, error))
+    {
+      return FALSE;
+    }
+
+  return g_simple_async_result_get_op_res_gboolean (simple);
+}
diff --git a/panels/printers/pp-maintenance-command.h b/panels/printers/pp-maintenance-command.h
index 3e922dc..cf9a44e 100644
--- a/panels/printers/pp-maintenance-command.h
+++ b/panels/printers/pp-maintenance-command.h
@@ -48,21 +48,28 @@ struct _PpMaintenanceCommandClass
   GObjectClass parent_class;
 };
 
-GType   pp_maintenance_command_get_type                     (void) G_GNUC_CONST;
+GType   pp_maintenance_command_get_type                          (void) G_GNUC_CONST;
 
-PpMaintenanceCommand *pp_maintenance_command_new            (const gchar *printer_name,
-                                                             const gchar *command,
-                                                             const gchar *title);
+PpMaintenanceCommand *pp_maintenance_command_new                 (const gchar *printer_name,
+                                                                  const gchar *command,
+                                                                  const gchar *title);
 
-void                  pp_maintenance_command_execute_async  (PpMaintenanceCommand *command,
-                                                             GCancellable         *cancellable,
-                                                             GAsyncReadyCallback   callback,
-                                                             gpointer              user_data);
+void                  pp_maintenance_command_execute_async       (PpMaintenanceCommand *command,
+                                                                  GCancellable         *cancellable,
+                                                                  GAsyncReadyCallback   callback,
+                                                                  gpointer              user_data);
 
-gboolean              pp_maintenance_command_execute_finish (PpMaintenanceCommand  *command,
-                                                             GAsyncResult          *result,
-                                                             GError               **error);
+gboolean              pp_maintenance_command_execute_finish      (PpMaintenanceCommand  *command,
+                                                                  GAsyncResult          *result,
+                                                                  GError               **error);
+void                  pp_maintenance_command_is_supported_async  (PpMaintenanceCommand *command,
+                                                                  GCancellable         *cancellable,
+                                                                  GAsyncReadyCallback   callback,
+                                                                  gpointer              user_data);
 
+gboolean              pp_maintenance_command_is_supported_finish (PpMaintenanceCommand  *command,
+                                                                  GAsyncResult          *result,
+                                                                  GError               **error);
 G_END_DECLS
 
 #endif /* __PP_MAINTENANCE_COMMAND_H__ */


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