[gnome-control-center/wip/msanchez/printers-clean-heads: 1/3] printers: Added new function pp_maintenance_command_is_supported()



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

    printers: Added new function pp_maintenance_command_is_supported()
    
    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 |  170 ++++++++++++++++--------------
 panels/printers/pp-maintenance-command.h |    3 +
 2 files changed, 94 insertions(+), 79 deletions(-)
---
diff --git a/panels/printers/pp-maintenance-command.c b/panels/printers/pp-maintenance-command.c
index 899ce6c..f335e0f 100644
--- a/panels/printers/pp-maintenance-command.c
+++ b/panels/printers/pp-maintenance-command.c
@@ -184,104 +184,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)
             {
-              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 +283,58 @@ pp_maintenance_command_execute_finish (PpMaintenanceCommand  *command,
 
   return g_simple_async_result_get_op_res_gboolean (simple);
 }
+
+gboolean
+pp_maintenance_command_is_supported (const gchar *printer_name,
+                                     const gchar *command)
+{
+  static const char *attrs[] = {"printer-commands"};
+  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);
+  ippAddStrings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+                 "requested-attributes", 1, NULL, attrs);
+  response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
+  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 &&
+              (ippGetValueTag (attr) == IPP_TAG_KEYWORD))
+            {
+              printer_commands = g_strdup (ippGetString (attr, 0, NULL));
+            }
+        }
+
+      ippDelete (response);
+    }
+
+  if (printer_commands)
+    {
+      command_lowercase = g_ascii_strdown (command, -1);
+      printer_commands_lowercase = g_ascii_strdown (printer_commands, -1);
+
+      if (g_strrstr (printer_commands_lowercase, command_lowercase))
+        is_supported = TRUE;
+
+      g_free (command_lowercase);
+      g_free (printer_commands_lowercase);
+      g_free (printer_commands);
+    }
+
+  return is_supported;
+}
diff --git a/panels/printers/pp-maintenance-command.h b/panels/printers/pp-maintenance-command.h
index 3e922dc..c660047 100644
--- a/panels/printers/pp-maintenance-command.h
+++ b/panels/printers/pp-maintenance-command.h
@@ -63,6 +63,9 @@ gboolean              pp_maintenance_command_execute_finish (PpMaintenanceComman
                                                              GAsyncResult          *result,
                                                              GError               **error);
 
+gboolean              pp_maintenance_command_is_supported   (const gchar *printer_name,
+                                                             const gchar *command);
+
 G_END_DECLS
 
 #endif /* __PP_MAINTENANCE_COMMAND_H__ */


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