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, see <http://www.gnu.org/licenses/>.
21 #include <glib/gi18n-lib.h>
28 #include "gtkprintercups.h"
35 static void gtk_printer_cups_init (GtkPrinterCups *printer);
36 static void gtk_printer_cups_class_init (GtkPrinterCupsClass *class);
37 static void gtk_printer_cups_finalize (GObject *object);
39 static GtkPrinterClass *gtk_printer_cups_parent_class;
40 static GType gtk_printer_cups_type = 0;
42 static void gtk_printer_cups_set_property (GObject *object,
46 static void gtk_printer_cups_get_property (GObject *object,
52 gtk_printer_cups_register_type (GTypeModule *module)
54 const GTypeInfo object_info =
56 sizeof (GtkPrinterCupsClass),
58 (GBaseFinalizeFunc) NULL,
59 (GClassInitFunc) gtk_printer_cups_class_init,
60 NULL, /* class_finalize */
61 NULL, /* class_data */
62 sizeof (GtkPrinterCups),
64 (GInstanceInitFunc) gtk_printer_cups_init,
67 gtk_printer_cups_type = g_type_module_register_type (module,
74 gtk_printer_cups_get_type (void)
76 return gtk_printer_cups_type;
80 gtk_printer_cups_class_init (GtkPrinterCupsClass *class)
82 GObjectClass *object_class = (GObjectClass *) class;
84 object_class->finalize = gtk_printer_cups_finalize;
85 object_class->set_property = gtk_printer_cups_set_property;
86 object_class->get_property = gtk_printer_cups_get_property;
88 gtk_printer_cups_parent_class = g_type_class_peek_parent (class);
90 g_object_class_install_property (G_OBJECT_CLASS (class),
92 g_param_spec_string ("profile-title",
93 P_("Color Profile Title"),
94 P_("The title of the color profile to use"),
100 gtk_printer_cups_init (GtkPrinterCups *printer)
102 printer->device_uri = NULL;
103 printer->printer_uri = NULL;
105 printer->hostname = NULL;
107 printer->ppd_name = NULL;
108 printer->ppd_file = NULL;
109 printer->default_cover_before = NULL;
110 printer->default_cover_after = NULL;
111 printer->remote = FALSE;
112 printer->get_remote_ppd_poll = 0;
113 printer->get_remote_ppd_attempts = 0;
114 printer->remote_cups_connection_test = NULL;
115 printer->auth_info_required = NULL;
116 printer->default_number_up = 1;
120 gtk_printer_cups_finalize (GObject *object)
122 GtkPrinterCups *printer;
124 g_return_if_fail (object != NULL);
126 printer = GTK_PRINTER_CUPS (object);
128 g_free (printer->device_uri);
129 g_free (printer->printer_uri);
130 g_free (printer->hostname);
131 g_free (printer->ppd_name);
132 g_free (printer->default_cover_before);
133 g_free (printer->default_cover_after);
134 g_strfreev (printer->auth_info_required);
137 g_cancellable_cancel (printer->colord_cancellable);
138 g_object_unref (printer->colord_cancellable);
139 g_free (printer->colord_title);
140 g_free (printer->colord_qualifier);
141 if (printer->colord_client)
142 g_object_unref (printer->colord_client);
143 if (printer->colord_device)
144 g_object_unref (printer->colord_device);
145 if (printer->colord_profile)
146 g_object_unref (printer->colord_profile);
149 if (printer->ppd_file)
150 ppdClose (printer->ppd_file);
152 if (printer->get_remote_ppd_poll > 0)
153 g_source_remove (printer->get_remote_ppd_poll);
154 printer->get_remote_ppd_attempts = 0;
156 gtk_cups_connection_test_free (printer->remote_cups_connection_test);
158 G_OBJECT_CLASS (gtk_printer_cups_parent_class)->finalize (object);
162 gtk_printer_cups_set_property (GObject *object,
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 gtk_printer_cups_get_property (GObject *object,
182 GtkPrinterCups *printer = GTK_PRINTER_CUPS (object);
187 case PROP_PROFILE_TITLE:
189 if (printer->colord_title)
190 g_value_set_string (value, printer->colord_title);
192 g_value_set_static_string (value, "");
194 g_value_set_static_string (value, NULL);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206 colord_update_ui_from_settings (GtkPrinterCups *printer)
208 const gchar *title = NULL;
210 /* not yet connected to colord */
211 if (printer->colord_client == NULL)
213 if (!cd_client_get_connected (printer->colord_client))
216 /* failed to get a colord device for the printer */
217 if (printer->colord_device == NULL)
219 /* TRANSLATORS: when we're running an old CUPS, and
220 * it hasn't registered the device with colord */
221 title = _("Color management unavailable");
225 /* when colord prevents us from connecting (should not happen) */
226 if (!cd_device_get_connected (printer->colord_device))
229 /* failed to get a colord device for the printer */
230 if (printer->colord_profile == NULL)
232 /* TRANSLATORS: when there is no color profile available */
233 title = _("No profile available");
237 /* when colord prevents us from connecting (should not happen) */
238 if (!cd_profile_get_connected (printer->colord_profile))
240 title = cd_profile_get_title (printer->colord_profile);
243 /* TRANSLATORS: when the color profile has no title */
244 title = _("Unspecified profile");
250 if (g_strcmp0 (title, printer->colord_title) != 0)
252 g_free (printer->colord_title);
253 printer->colord_title = g_strdup (title);
254 g_object_notify (G_OBJECT (printer), "profile-title");
260 colord_client_profile_connect_cb (GObject *source_object,
265 GError *error = NULL;
266 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
268 ret = cd_profile_connect_finish (CD_PROFILE (source_object),
273 g_warning ("failed to get properties from the profile: %s",
275 g_error_free (error);
279 colord_update_ui_from_settings (printer);
281 g_object_unref (printer);
285 colord_client_device_get_profile_for_qualifiers_cb (GObject *source_object,
289 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
290 GError *error = NULL;
292 printer->colord_profile = cd_device_get_profile_for_qualifiers_finish (printer->colord_device,
295 if (printer->colord_profile == NULL)
297 /* not having a profile for a qualifier is not a warning */
298 g_debug ("no profile for device %s: %s",
299 cd_device_get_id (printer->colord_device),
301 g_error_free (error);
305 /* get details about the profile */
306 cd_profile_connect (printer->colord_profile,
307 printer->colord_cancellable,
308 colord_client_profile_connect_cb,
309 g_object_ref (printer));
312 colord_update_ui_from_settings (printer);
314 g_object_unref (printer);
318 gtk_printer_cups_update_settings (GtkPrinterCups *printer,
319 GtkPrintSettings *settings,
320 GtkPrinterOptionSet *set)
322 gchar *qualifier = NULL;
323 gchar **qualifiers = NULL;
324 GtkPrinterOption *option;
325 const gchar *format[3];
327 /* nothing set yet */
328 if (printer->colord_device == NULL)
330 if (!cd_device_get_connected (printer->colord_device))
333 /* cupsICCQualifier1 */
334 option = gtk_printer_option_set_lookup (set, "cups-ColorSpace");
336 option = gtk_printer_option_set_lookup (set, "cups-ColorModel");
338 format[0] = option->value;
342 /* cupsICCQualifier2 */
343 option = gtk_printer_option_set_lookup (set, "cups-OutputMode");
345 format[1] = option->value;
349 /* cupsICCQualifier3 */
350 option = gtk_printer_option_set_lookup (set, "cups-Resolution");
352 format[2] = option->value;
356 /* get profile for the device given the qualifier */
357 qualifier = g_strdup_printf ("%s.%s.%s,%s.%s.*,%s.*.*",
358 format[0], format[1], format[2],
359 format[0], format[1],
362 /* only requery colord if the option that was changed would give
363 * us a different profile result */
364 if (g_strcmp0 (qualifier, printer->colord_qualifier) == 0)
367 qualifiers = g_strsplit (qualifier, ",", -1);
368 cd_device_get_profile_for_qualifiers (printer->colord_device,
369 (const gchar **) qualifiers,
370 printer->colord_cancellable,
371 colord_client_device_get_profile_for_qualifiers_cb,
372 g_object_ref (printer));
374 /* save for the future */
375 g_free (printer->colord_qualifier);
376 printer->colord_qualifier = g_strdup (qualifier);
379 colord_update_ui_from_settings (printer);
382 g_strfreev (qualifiers);
386 colord_client_device_connect_cb (GObject *source_object,
390 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
392 GError *error = NULL;
394 /* get details about the device */
395 ret = cd_device_connect_finish (CD_DEVICE (source_object), res, &error);
398 g_warning ("failed to get properties from the colord device: %s",
400 g_error_free (error);
405 colord_update_ui_from_settings (printer);
407 g_object_unref (printer);
411 colord_client_find_device_cb (GObject *source_object,
415 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
416 GError *error = NULL;
418 /* get the new device */
419 printer->colord_device = cd_client_find_device_finish (printer->colord_client,
422 if (printer->colord_device == NULL)
424 g_warning ("failed to get find a colord device: %s",
426 g_error_free (error);
430 /* get details about the device */
431 g_cancellable_reset (printer->colord_cancellable);
432 cd_device_connect (printer->colord_device,
433 printer->colord_cancellable,
434 colord_client_device_connect_cb,
435 g_object_ref (printer));
438 colord_update_ui_from_settings (printer);
440 g_object_unref (printer);
444 colord_update_device (GtkPrinterCups *printer)
446 gchar *colord_device_id = NULL;
448 /* not yet connected to the daemon */
449 if (!cd_client_get_connected (printer->colord_client))
452 /* not yet assigned a printer */
453 if (printer->ppd_file == NULL)
456 /* old cached profile no longer valid */
457 if (printer->colord_profile)
459 g_object_unref (printer->colord_profile);
460 printer->colord_profile = NULL;
463 /* old cached device no longer valid */
464 if (printer->colord_device)
466 g_object_unref (printer->colord_device);
467 printer->colord_device = NULL;
470 /* generate a known ID */
471 colord_device_id = g_strdup_printf ("cups-%s", gtk_printer_get_name (GTK_PRINTER (printer)));
473 g_cancellable_reset (printer->colord_cancellable);
474 cd_client_find_device (printer->colord_client,
476 printer->colord_cancellable,
477 colord_client_find_device_cb,
478 g_object_ref (printer));
480 g_free (colord_device_id);
483 colord_update_ui_from_settings (printer);
487 colord_client_connect_cb (GObject *source_object,
492 GError *error = NULL;
493 GtkPrinterCups *printer = GTK_PRINTER_CUPS (user_data);
495 ret = cd_client_connect_finish (CD_CLIENT (source_object),
499 g_warning ("failed to contact colord: %s", error->message);
500 g_error_free (error);
503 /* refresh the device */
504 colord_update_device (printer);
506 g_object_unref (printer);
511 * gtk_printer_cups_new:
513 * Creates a new #GtkPrinterCups.
515 * Return value: a new #GtkPrinterCups
520 gtk_printer_cups_new (const char *name,
521 GtkPrintBackend *backend,
522 gpointer colord_client)
525 gboolean accepts_pdf;
526 GtkPrinterCups *printer;
528 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
534 result = g_object_new (GTK_TYPE_PRINTER_CUPS,
538 "accepts-pdf", accepts_pdf,
540 printer = GTK_PRINTER_CUPS (result);
543 /* connect to colord */
544 if (colord_client != NULL)
546 printer->colord_cancellable = g_cancellable_new ();
547 printer->colord_client = g_object_ref (CD_CLIENT (colord_client));
548 cd_client_connect (printer->colord_client,
549 printer->colord_cancellable,
550 colord_client_connect_cb,
551 g_object_ref (printer));
558 gtk_printer_cups_get_ppd (GtkPrinterCups *printer)
560 return printer->ppd_file;
564 gtk_printer_cups_get_ppd_name (GtkPrinterCups *printer)
568 result = printer->ppd_name;
571 result = gtk_printer_get_name (GTK_PRINTER (printer));