[gnome-control-center/wip/msanchez/printers-clean-heads: 112/114] printers: Added new async API to check availability of maintenance commands
- From: Mario Sanchez Prada <msanchez src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-control-center/wip/msanchez/printers-clean-heads: 112/114] printers: Added new async API to check availability of maintenance commands
- Date: Mon, 13 Jun 2016 13:49:29 +0000 (UTC)
commit a54ef75e37420be1397736fc255458bf05858c78
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 | 201 +++++++++++++++++++-----------
panels/printers/pp-maintenance-command.h | 30 +++--
2 files changed, 149 insertions(+), 82 deletions(-)
---
diff --git a/panels/printers/pp-maintenance-command.c b/panels/printers/pp-maintenance-command.c
index 899ce6c..90c636d 100644
--- a/panels/printers/pp-maintenance-command.c
+++ b/panels/printers/pp-maintenance-command.c
@@ -177,111 +177,170 @@ pp_maintenance_command_new (const gchar *printer_name,
NULL);
}
-static void
-_pp_maintenance_command_execute_thread (GSimpleAsyncResult *res,
- GObject *object,
- GCancellable *cancellable)
+static gboolean
+_pp_maintenance_command_is_supported (const gchar *printer_name,
+ const gchar *command)
{
- 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;
+ 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",
- priv->printer_name);
+ 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);
+ ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+ "requested-attributes", NULL, "printer-commands");
response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
-
- if (response)
+ if (response != NULL)
{
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 (attr != NULL && ippGetCount (attr) > 0 &&
+ ippGetValueTag (attr) != IPP_TAG_NOVALUE &&
+ (ippGetValueTag (attr) == IPP_TAG_KEYWORD))
{
- if (ippGetValueTag (attr) == IPP_TAG_KEYWORD)
- {
- printer_commands = g_strdup (ippGetString (attr, 0, NULL));
- }
- }
- else
- {
- success = TRUE;
+ printer_commands = g_strdup (ippGetString (attr, 0, NULL));
}
}
ippDelete (response);
}
- if (printer_commands)
+ if (printer_commands != NULL)
{
- command_lowercase = g_ascii_strdown (priv->command, -1);
+ command_lowercase = g_ascii_strdown (command, -1);
printer_commands_lowercase = g_ascii_strdown (printer_commands, -1);
- if (g_strrstr (printer_commands_lowercase, command_lowercase))
- {
- request = ippNewRequest (IPP_PRINT_JOB);
+ if (g_strcmp0 (printer_commands_lowercase, command_lowercase) == 0)
+ is_supported = TRUE;
- 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");
+ g_free (command_lowercase);
+ g_free (printer_commands_lowercase);
+ g_free (printer_commands);
+ }
- fd = g_file_open_tmp ("ccXXXXXX", &file_name, &error);
+ g_free (printer_uri);
- if (fd != -1)
- {
- FILE *file;
+ return is_supported;
+}
- file = fdopen (fd, "w");
- fprintf (file, "#CUPS-COMMAND\n");
- fprintf (file, "%s\n", priv->command);
- fclose (file);
+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;
- response = cupsDoFileRequest (CUPS_HTTP_DEFAULT, request, "/", file_name);
- g_unlink (file_name);
+ success = _pp_maintenance_command_is_supported (priv->printer_name, priv->command);
+ g_simple_async_result_set_op_res_gboolean (res, success);
+}
- if (response)
- {
- if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
- {
- success = TRUE;
- }
+void
+pp_maintenance_command_is_supported_async (PpMaintenanceCommand *command,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GSimpleAsyncResult *res;
- ippDelete (response);
- }
- }
+ res = g_simple_async_result_new (G_OBJECT (command), callback, user_data,
pp_maintenance_command_is_supported_async);
- g_free (file_name);
- }
- else
+ 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);
+}
+
+static void
+_pp_maintenance_command_execute_thread (GSimpleAsyncResult *res,
+ GObject *object,
+ GCancellable *cancellable)
+{
+ PpMaintenanceCommand *command = (PpMaintenanceCommand *) object;
+ PpMaintenanceCommandPrivate *priv = command->priv;
+ gboolean success = FALSE;
+ GError *error = NULL;
+
+ 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;
+
+ printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
+ priv->printer_name);
+
+ request = ippNewRequest (IPP_PRINT_JOB);
+
+ 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");
+
+ fd = g_file_open_tmp ("ccXXXXXX", &file_name, &error);
+
+ if (fd != -1)
{
- success = TRUE;
+ 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 != NULL)
+ {
+ if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
+ {
+ 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)
{
diff --git a/panels/printers/pp-maintenance-command.h b/panels/printers/pp-maintenance-command.h
index 3e922dc..746ee18 100644
--- a/panels/printers/pp-maintenance-command.h
+++ b/panels/printers/pp-maintenance-command.h
@@ -48,21 +48,29 @@ 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_is_supported_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_is_supported_finish (PpMaintenanceCommand *command,
+ GAsyncResult *result,
+ GError **error);
+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);
G_END_DECLS
#endif /* __PP_MAINTENANCE_COMMAND_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]