[gnome-control-center/wip/msanchez/printers-clean-heads: 5/6] printers: Check all supported CUPS commands, not just the first one



commit d3c18acc19bbb3566a7619484b7b4ae527c2fd58
Author: Mario Sanchez Prada <mario endlessm com>
Date:   Fri Apr 29 12:13:02 2016 +0100

    printers: Check all supported CUPS commands, not just the first one
    
    At the moment, we were querying the printer for its 'printers-comands'
    attribute with a call to ippFindAttribute() and then extracting its
    value with ippGetString (attr, 0, NULL) to further search that string
    for ocurrences of the maintenance command we want to execute.
    
    The problem is that the 'printers-comands' attribute returned is not
    a single value like the one that lpoptions -p would return if called
    from command line (e.g. 'AutoConfigure,Clean,PrintSelfTestPage'), but
    a list of sevaral values instead, so that logic will only work for the
    'AutoConfigure' command, which so far has been enough since it's the
    only one we've ever needed for a long time.
    
    We need to fix this now we are adding support for the 'Clean' command,
    otherwise it won't be possible to use that feature, even if the printer
    supports it (e.g. Canon PIXMA MG2410 does, using cnijfilter drivers).
    
    Thus, use an array of strings to store every supported command and
    check the desired command against it, instead of just the first one.

 panels/printers/pp-maintenance-command.c |   39 +++++++++++++++++++++--------
 1 files changed, 28 insertions(+), 11 deletions(-)
---
diff --git a/panels/printers/pp-maintenance-command.c b/panels/printers/pp-maintenance-command.c
index f335e0f..eb2ddc6 100644
--- a/panels/printers/pp-maintenance-command.c
+++ b/panels/printers/pp-maintenance-command.c
@@ -294,9 +294,8 @@ pp_maintenance_command_is_supported (const gchar *printer_name,
   ipp_t             *request;
   ipp_t             *response = NULL;
   gchar             *printer_uri;
-  gchar             *printer_commands = NULL;
-  gchar             *printer_commands_lowercase = NULL;
   gchar             *command_lowercase;
+  GPtrArray         *available_commands = NULL;
 
   printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
                                  printer_name);
@@ -311,30 +310,48 @@ pp_maintenance_command_is_supported (const gchar *printer_name,
     {
       if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
         {
+          int commands_count;
+
           attr = ippFindAttribute (response, "printer-commands", IPP_TAG_ZERO);
-          if (attr && ippGetCount (attr) > 0 &&
+          commands_count = attr ? ippGetCount (attr) : 0;
+          if (commands_count > 0 &&
               ippGetValueTag (attr) != IPP_TAG_NOVALUE &&
               (ippGetValueTag (attr) == IPP_TAG_KEYWORD))
             {
-              printer_commands = g_strdup (ippGetString (attr, 0, NULL));
+              int i;
+
+              available_commands = g_ptr_array_new ();
+              for (i = 0; i < commands_count; ++i)
+                {
+                  /* Array gains ownership of the lower-cased string */
+                  g_ptr_array_add (available_commands, g_ascii_strdown (ippGetString (attr, i, NULL), -1));
+                }
             }
         }
 
       ippDelete (response);
     }
 
-  if (printer_commands)
+  if (available_commands)
     {
-      command_lowercase = g_ascii_strdown (command, -1);
-      printer_commands_lowercase = g_ascii_strdown (printer_commands, -1);
+      int i;
 
-      if (g_strrstr (printer_commands_lowercase, command_lowercase))
-        is_supported = TRUE;
+      command_lowercase = g_ascii_strdown (command, -1);
+      for (i = 0; i < available_commands->len; ++i)
+        {
+          const gchar *available_command = g_ptr_array_index (available_commands, i);
+          if (g_strrstr (available_command, command_lowercase))
+            {
+              is_supported = TRUE;
+              break;
+            }
+        }
 
       g_free (command_lowercase);
-      g_free (printer_commands_lowercase);
-      g_free (printer_commands);
+      g_ptr_array_free (available_commands, TRUE);
     }
 
+  g_free (printer_uri);
+
   return is_supported;
 }


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