static void
pp_maintenance_command_set_property (GObject      *object,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *param_spec)
{
  PpMaintenanceCommand *self = PP_MAINTENANCE_COMMAND (object);

  switch (prop_id)
    {
      case PROP_PRINTER_NAME:
        g_free (self->priv->printer_name);
        self->priv->printer_name = g_value_dup_string (value);
        break;
      case PROP_COMMAND:
        g_free (self->priv->command);
        self->priv->command = g_value_dup_string (value);
        break;
      case PROP_TITLE:
        g_free (self->priv->title);
        self->priv->title = g_value_dup_string (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
                                           prop_id,
                                           param_spec);
        break;
    }
}
static void
_pp_maintenance_command_is_supported_thread (GTask        *task,
                                             gpointer      source_object,
                                             gpointer      task_data,
                                             GCancellable *cancellable)
{
  PpMaintenanceCommand        *command = PP_MAINTENANCE_COMMAND (source_object);
  PpMaintenanceCommandPrivate *priv = command->priv;
  gboolean                     success = FALSE;

  success = _pp_maintenance_command_is_supported (priv->printer_name, priv->command);
  g_task_return_boolean (task, success);
}
static void
pp_maintenance_command_finalize (GObject *object)
{
  PpMaintenanceCommandPrivate *priv;

  priv = PP_MAINTENANCE_COMMAND (object)->priv;

  g_clear_pointer (&priv->printer_name, g_free);
  g_clear_pointer (&priv->command, g_free);
  g_clear_pointer (&priv->title, g_free);

  G_OBJECT_CLASS (pp_maintenance_command_parent_class)->finalize (object);
}
static void
_pp_maintenance_command_execute_thread (GTask        *task,
                                        gpointer      source_object,
                                        gpointer      task_data,
                                        GCancellable *cancellable)
{
  PpMaintenanceCommand        *command = PP_MAINTENANCE_COMMAND (source_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)
        {
          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 (file_name);
      g_free (printer_uri);
    }
  else
    {
      success = TRUE;
    }

  if (!success)
    {
      g_task_return_new_error (task,
                               G_IO_ERROR,
                               G_IO_ERROR_FAILED,
                               "Execution of maintenance command failed.");
    }

  g_task_return_boolean (task, success);
}