2 * Copyright (C) 2006 John (J5) Palmieri <johnp@redhat.com>
3 * Copyright (C) 2011 Richard Hughes <rhughes@redhat.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include <glib/gi18n-lib.h>
30 #include "gtkprintercups.h"
37 static void gtk_printer_cups_init (GtkPrinterCups *printer);
38 static void gtk_printer_cups_class_init (GtkPrinterCupsClass *class);
39 static void gtk_printer_cups_finalize (GObject *object);
41 static GtkPrinterClass *gtk_printer_cups_parent_class;
42 static GType gtk_printer_cups_type = 0;
44 static void gtk_printer_cups_set_property (GObject *object,
48 static void gtk_printer_cups_get_property (GObject *object,
54 gtk_printer_cups_register_type (GTypeModule *module)
56 const GTypeInfo object_info =
58 sizeof (GtkPrinterCupsClass),
60 (GBaseFinalizeFunc) NULL,
61 (GClassInitFunc) gtk_printer_cups_class_init,
62 NULL, /* class_finalize */
63 NULL, /* class_data */
64 sizeof (GtkPrinterCups),
66 (GInstanceInitFunc) gtk_printer_cups_init,
69 gtk_printer_cups_type = g_type_module_register_type (module,
76 gtk_printer_cups_get_type (void)
78 return gtk_printer_cups_type;
82 gtk_printer_cups_class_init (GtkPrinterCupsClass *class)
84 GObjectClass *object_class = (GObjectClass *) class;
86 object_class->finalize = gtk_printer_cups_finalize;
87 object_class->set_property = gtk_printer_cups_set_property;
88 object_class->get_property = gtk_printer_cups_get_property;
90 gtk_printer_cups_parent_class = g_type_class_peek_parent (class);
92 g_object_class_install_property (G_OBJECT_CLASS (class),
94 g_param_spec_string ("profile-title",
95 P_("Color Profile Title"),
96 P_("The title of the color profile to use"),
102 gtk_printer_cups_init (GtkPrinterCups *printer)
104 printer->device_uri = NULL;
105 printer->printer_uri = NULL;
107 printer->hostname = NULL;
109 printer->ppd_name = NULL;
110 printer->ppd_file = NULL;
111 printer->default_cover_before = NULL;
112 printer->default_cover_after = NULL;
113 printer->remote = FALSE;
114 printer->get_remote_ppd_poll = 0;
115 printer->get_remote_ppd_attempts = 0;
116 printer->remote_cups_connection_test = NULL;
117 printer->auth_info_required = NULL;
121 gtk_printer_cups_finalize (GObject *object)
123 GtkPrinterCups *printer;
125 g_return_if_fail (object != NULL);
127 printer = GTK_PRINTER_CUPS (object);
129 g_free (printer->device_uri);
130 g_free (printer->printer_uri);
131 g_free (printer->hostname);
132 g_free (printer->ppd_name);
133 g_free (printer->default_cover_before);
134 g_free (printer->default_cover_after);
135 g_strfreev (printer->auth_info_required);
138 g_cancellable_cancel (printer->colord_cancellable);
139 g_object_unref (printer->colord_cancellable);
140 g_free (printer->colord_title);
141 g_free (printer->colord_qualifier);
142 if (printer->colord_client)
143 g_object_unref (printer->colord_client);
144 if (printer->colord_device)
145 g_object_unref (printer->colord_device);
146 if (printer->colord_profile)
147 g_object_unref (printer->colord_profile);
150 if (printer->ppd_file)
151 ppdClose (printer->ppd_file);
153 if (printer->get_remote_ppd_poll > 0)
154 g_source_remove (printer->get_remote_ppd_poll);
155 printer->get_remote_ppd_attempts = 0;
157 gtk_cups_connection_test_free (printer->remote_cups_connection_test);
159 G_OBJECT_CLASS (gtk_printer_cups_parent_class)->finalize (object);
163 gtk_printer_cups_set_property (GObject *object,
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177 gtk_printer_cups_get_property (GObject *object,
183 GtkPrinterCups *printer = GTK_PRINTER_CUPS (object);
188 case PROP_PROFILE_TITLE:
190 if (printer->colord_title)
191 g_value_set_string (value, printer->colord_title);
193 g_value_set_static_string (value, "");
195 g_value_set_static_string (value, NULL);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 colord_update_ui_from_settings (GtkPrinterCups *printer)
209 const gchar *title = NULL;
211 /* not yet connected to colord */
212 if (printer->colord_client == NULL)
214 if (!cd_client_get_connected (printer->colord_client))
217 /* failed to get a colord device for the printer */
218 if (printer->colord_device == NULL)
220 /* TRANSLATORS: when we're running an old CUPS, and
221 * it hasn't registered the device with colord */
222 title = _("Color management unavailable");
226 /* when colord prevents us from connecting (should not happen) */
227 if (!cd_device_get_connected (printer->colord_device))
230 /* failed to get a colord device for the printer */
231 if (printer->colord_profile == NULL)
233 /* TRANSLATORS: when there is no color profile available */
234 title = _("No profile available");
238 /* when colord prevents us from connecting (should not happen) */
239 if (!cd_profile_get_connected (printer->colord_profile))
241 title = cd_profile_get_title (printer->colord_profile);
244 /* TRANSLATORS: when the color profile has no title */
245 title = _("Unspecified profile");
251 if (g_strcmp0 (title, printer->colord_title) != 0)
253 g_free (printer->colord_title);
254 printer->colord_title = g_strdup (title);
255 g_object_notify (G_OBJECT (printer), "profile-title");
261 colord_client_profile_connect_cb (GObject *source_object,
266 GError *error = NULL;
267 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
269 ret = cd_profile_connect_finish (CD_PROFILE (source_object),
274 g_warning ("failed to get properties from the profile: %s",
276 g_error_free (error);
280 colord_update_ui_from_settings (printer);
284 colord_client_device_get_profile_for_qualifiers_cb (GObject *source_object,
288 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
289 GError *error = NULL;
291 printer->colord_profile = cd_device_get_profile_for_qualifiers_finish (printer->colord_device,
294 if (printer->colord_profile == NULL)
296 /* not having a profile for a qualifier is not a warning */
297 g_debug ("no profile for device %s: %s",
298 cd_device_get_id (printer->colord_device),
300 g_error_free (error);
304 /* get details about the profile */
305 cd_profile_connect (printer->colord_profile,
306 printer->colord_cancellable,
307 colord_client_profile_connect_cb,
311 colord_update_ui_from_settings (printer);
315 gtk_printer_cups_update_settings (GtkPrinterCups *printer,
316 GtkPrintSettings *settings,
317 GtkPrinterOptionSet *set)
319 gchar *qualifier = NULL;
320 gchar **qualifiers = NULL;
321 GtkPrinterOption *option;
322 const gchar *format[3];
324 /* nothing set yet */
325 if (printer->colord_device == NULL)
327 if (!cd_device_get_connected (printer->colord_device))
330 /* cupsICCQualifier1 */
331 option = gtk_printer_option_set_lookup (set, "cups-ColorSpace");
333 option = gtk_printer_option_set_lookup (set, "cups-ColorModel");
335 format[0] = option->value;
339 /* cupsICCQualifier2 */
340 option = gtk_printer_option_set_lookup (set, "cups-OutputMode");
342 format[1] = option->value;
346 /* cupsICCQualifier3 */
347 option = gtk_printer_option_set_lookup (set, "cups-Resolution");
351 /* get profile for the device given the qualifier */
352 qualifier = g_strdup_printf ("%s.%s.%s,%s.%s.*,%s.*.*",
353 format[0], format[1], format[2],
354 format[0], format[1],
357 /* only requery colord if the option that was changed would give
358 * us a different profile result */
359 if (g_strcmp0 (qualifier, printer->colord_qualifier) == 0)
362 qualifiers = g_strsplit (qualifier, ",", -1);
363 cd_device_get_profile_for_qualifiers (printer->colord_device,
364 (const gchar **) qualifiers,
365 printer->colord_cancellable,
366 colord_client_device_get_profile_for_qualifiers_cb,
369 /* save for the future */
370 g_free (printer->colord_qualifier);
371 printer->colord_qualifier = g_strdup (qualifier);
374 colord_update_ui_from_settings (printer);
377 g_strfreev (qualifiers);
381 colord_client_device_connect_cb (GObject *source_object,
385 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
387 GError *error = NULL;
389 /* get details about the device */
390 ret = cd_device_connect_finish (CD_DEVICE (source_object), res, &error);
393 g_warning ("failed to get properties from the colord device: %s",
395 g_error_free (error);
400 colord_update_ui_from_settings (printer);
404 colord_client_find_device_cb (GObject *source_object,
408 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
409 GError *error = NULL;
411 /* get the new device */
412 printer->colord_device = cd_client_find_device_finish (printer->colord_client,
415 if (printer->colord_device == NULL)
417 g_warning ("failed to get find a colord device: %s",
419 g_error_free (error);
423 /* get details about the device */
424 g_cancellable_reset (printer->colord_cancellable);
425 cd_device_connect (printer->colord_device,
426 printer->colord_cancellable,
427 colord_client_device_connect_cb,
431 colord_update_ui_from_settings (printer);
435 colord_update_device (GtkPrinterCups *printer)
437 gchar *colord_device_id = NULL;
439 /* not yet connected to the daemon */
440 if (!cd_client_get_connected (printer->colord_client))
443 /* not yet assigned a printer */
444 if (printer->ppd_file == NULL)
447 /* old cached profile no longer valid */
448 if (printer->colord_profile)
450 g_object_unref (printer->colord_profile);
451 printer->colord_profile = NULL;
454 /* old cached device no longer valid */
455 if (printer->colord_device)
457 g_object_unref (printer->colord_device);
458 printer->colord_device = NULL;
461 /* generate a known ID */
462 colord_device_id = g_strdup_printf ("cups-%s", gtk_printer_get_name (GTK_PRINTER (printer)));
464 g_cancellable_reset (printer->colord_cancellable);
465 cd_client_find_device (printer->colord_client,
467 printer->colord_cancellable,
468 colord_client_find_device_cb,
471 g_free (colord_device_id);
474 colord_update_ui_from_settings (printer);
478 colord_client_connect_cb (GObject *source_object,
483 GError *error = NULL;
484 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
486 ret = cd_client_connect_finish (CD_CLIENT (source_object),
490 g_warning ("failed to contact colord: %s", error->message);
491 g_error_free (error);
494 /* refresh the device */
495 colord_update_device (printer);
500 * gtk_printer_cups_new:
502 * Creates a new #GtkPrinterCups.
504 * Return value: a new #GtkPrinterCups
509 gtk_printer_cups_new (const char *name,
510 GtkPrintBackend *backend,
511 gpointer colord_client)
514 gboolean accepts_pdf;
515 GtkPrinterCups *printer;
517 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
523 result = g_object_new (GTK_TYPE_PRINTER_CUPS,
527 "accepts-pdf", accepts_pdf,
529 printer = GTK_PRINTER_CUPS (result);
532 /* connect to colord */
533 if (colord_client != NULL)
535 printer->colord_cancellable = g_cancellable_new ();
536 printer->colord_client = g_object_ref (CD_CLIENT (colord_client));
537 cd_client_connect (printer->colord_client,
538 printer->colord_cancellable,
539 colord_client_connect_cb,
547 gtk_printer_cups_get_ppd (GtkPrinterCups *printer)
549 return printer->ppd_file;
553 gtk_printer_cups_get_ppd_name (GtkPrinterCups *printer)
557 result = printer->ppd_name;
560 result = gtk_printer_get_name (GTK_PRINTER (printer));