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



commit e50476990daad1f6b90657ea867896bf24ec9426
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 will be used both from the _pp_maintenance_command_execute_thread(),
    before actually running a command, and from pp-printer-entry.c to know
    whether the 'Clean' command is available, to show the relevant action.

 panels/printers/pp-maintenance-command.c |   55 ++++++++++++++++++++++++++++++
 panels/printers/pp-maintenance-command.h |    3 ++
 2 files changed, 58 insertions(+), 0 deletions(-)
---
diff --git a/panels/printers/pp-maintenance-command.c b/panels/printers/pp-maintenance-command.c
index 899ce6c..4f565c8 100644
--- a/panels/printers/pp-maintenance-command.c
+++ b/panels/printers/pp-maintenance-command.c
@@ -326,3 +326,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]