1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright 2012 Red Hat, Inc,
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
* Author: Marek Kasik <mkasik@redhat.com>
22
#include <glib/gstdio.h>
24
#include "pp-maintenance-command.h"
28
#if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
29
#define HAVE_CUPS_1_6 1
33
#define ippGetCount(attr) attr->num_values
34
#define ippGetValueTag(attr) attr->value_tag
35
#define ippGetStatusCode(ipp) ipp->request.status.status_code
36
#define ippGetString(attr, element, language) attr->values[element].string.text
39
struct _PpMaintenanceCommandPrivate
46
G_DEFINE_TYPE (PpMaintenanceCommand, pp_maintenance_command, G_TYPE_OBJECT);
56
pp_maintenance_command_finalize (GObject *object)
58
PpMaintenanceCommandPrivate *priv;
60
priv = PP_MAINTENANCE_COMMAND (object)->priv;
62
g_clear_pointer (&priv->printer_name, g_free);
63
g_clear_pointer (&priv->command, g_free);
64
g_clear_pointer (&priv->title, g_free);
66
G_OBJECT_CLASS (pp_maintenance_command_parent_class)->finalize (object);
70
pp_maintenance_command_get_property (GObject *object,
73
GParamSpec *param_spec)
75
PpMaintenanceCommand *self;
77
self = PP_MAINTENANCE_COMMAND (object);
81
case PROP_PRINTER_NAME:
82
g_value_set_string (value, self->priv->printer_name);
85
g_value_set_string (value, self->priv->command);
88
g_value_set_string (value, self->priv->title);
91
G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
99
pp_maintenance_command_set_property (GObject *object,
102
GParamSpec *param_spec)
104
PpMaintenanceCommand *self = PP_MAINTENANCE_COMMAND (object);
108
case PROP_PRINTER_NAME:
109
g_free (self->priv->printer_name);
110
self->priv->printer_name = g_value_dup_string (value);
113
g_free (self->priv->command);
114
self->priv->command = g_value_dup_string (value);
117
g_free (self->priv->title);
118
self->priv->title = g_value_dup_string (value);
121
G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
129
pp_maintenance_command_class_init (PpMaintenanceCommandClass *klass)
131
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133
g_type_class_add_private (klass, sizeof (PpMaintenanceCommandPrivate));
135
gobject_class->set_property = pp_maintenance_command_set_property;
136
gobject_class->get_property = pp_maintenance_command_get_property;
137
gobject_class->finalize = pp_maintenance_command_finalize;
139
g_object_class_install_property (gobject_class, PROP_PRINTER_NAME,
140
g_param_spec_string ("printer-name",
142
"Name of the printer",
146
g_object_class_install_property (gobject_class, PROP_COMMAND,
147
g_param_spec_string ("command",
148
"Maintenance command",
149
"Command to execute",
153
g_object_class_install_property (gobject_class, PROP_TITLE,
154
g_param_spec_string ("title",
156
"Title of the job by which the command will be executed",
162
pp_maintenance_command_init (PpMaintenanceCommand *command)
164
command->priv = G_TYPE_INSTANCE_GET_PRIVATE (command,
165
PP_TYPE_MAINTENANCE_COMMAND,
166
PpMaintenanceCommandPrivate);
169
PpMaintenanceCommand *
170
pp_maintenance_command_new (const gchar *printer_name,
171
const gchar *command,
174
return g_object_new (PP_TYPE_MAINTENANCE_COMMAND,
175
"printer-name", printer_name,
182
_pp_maintenance_command_execute_thread (GSimpleAsyncResult *res,
184
GCancellable *cancellable)
186
PpMaintenanceCommand *command = (PpMaintenanceCommand *) object;
187
PpMaintenanceCommandPrivate *priv = command->priv;
188
static const char *attrs[] = {"printer-commands"};
189
ipp_attribute_t *attr = NULL;
190
gboolean success = FALSE;
191
GError *error = NULL;
193
ipp_t *response = NULL;
195
gchar *printer_commands = NULL;
196
gchar *printer_commands_lowercase = NULL;
197
gchar *command_lowercase;
198
gchar *file_name = NULL;
201
printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
204
request = ippNewRequest (IPP_GET_PRINTER_ATTRIBUTES);
205
ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
206
"printer-uri", NULL, printer_uri);
207
ippAddStrings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
208
"requested-attributes", 1, NULL, attrs);
209
response = cupsDoRequest (CUPS_HTTP_DEFAULT, request, "/");
213
if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
215
attr = ippFindAttribute (response, "printer-commands", IPP_TAG_ZERO);
216
if (attr && ippGetCount (attr) > 0 && ippGetValueTag (attr) != IPP_TAG_NOVALUE)
218
if (ippGetValueTag (attr) == IPP_TAG_KEYWORD)
220
printer_commands = g_strdup (ippGetString (attr, 0, NULL));
229
ippDelete (response);
232
if (printer_commands)
234
command_lowercase = g_ascii_strdown (priv->command, -1);
235
printer_commands_lowercase = g_ascii_strdown (printer_commands, -1);
237
if (g_strrstr (printer_commands_lowercase, command_lowercase))
239
request = ippNewRequest (IPP_PRINT_JOB);
241
ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_URI,
242
"printer-uri", NULL, printer_uri);
243
ippAddString (request, IPP_TAG_OPERATION, IPP_TAG_NAME,
244
"job-name", NULL, priv->title);
245
ippAddString (request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
246
"document-format", NULL, "application/vnd.cups-command");
248
fd = g_file_open_tmp ("ccXXXXXX", &file_name, &error);
254
file = fdopen (fd, "w");
255
fprintf (file, "#CUPS-COMMAND\n");
256
fprintf (file, "%s\n", priv->command);
259
response = cupsDoFileRequest (CUPS_HTTP_DEFAULT, request, "/", file_name);
260
g_unlink (file_name);
264
if (ippGetStatusCode (response) <= IPP_OK_CONFLICT)
269
ippDelete (response);
280
g_free (command_lowercase);
281
g_free (printer_commands_lowercase);
282
g_free (printer_commands);
285
g_free (printer_uri);
289
g_simple_async_result_set_error (res,
292
"Execution of maintenance command failed.");
295
g_simple_async_result_set_op_res_gboolean (res, success);
299
pp_maintenance_command_execute_async (PpMaintenanceCommand *command,
300
GCancellable *cancellable,
301
GAsyncReadyCallback callback,
304
GSimpleAsyncResult *res;
306
res = g_simple_async_result_new (G_OBJECT (command), callback, user_data, pp_maintenance_command_execute_async);
308
g_simple_async_result_set_check_cancellable (res, cancellable);
309
g_simple_async_result_run_in_thread (res, _pp_maintenance_command_execute_thread, 0, cancellable);
311
g_object_unref (res);
315
pp_maintenance_command_execute_finish (PpMaintenanceCommand *command,
319
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
321
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == pp_maintenance_command_execute_async);
323
if (g_simple_async_result_propagate_error (simple, error))
328
return g_simple_async_result_get_op_res_gboolean (simple);