]> Pileus Git - ~andy/gtk/blobdiff - modules/printbackends/cups/gtkprintbackendcups.c
printing: Localize loaded PPD files
[~andy/gtk] / modules / printbackends / cups / gtkprintbackendcups.c
index 39248711963d57f08791986feed9655ac9613668..120f0c13038e3ea873103bd27e502a23c09e7702 100644 (file)
@@ -14,9 +14,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifdef __linux__
@@ -54,6 +52,9 @@
 
 #include "gtkcupsutils.h"
 
+#ifdef HAVE_COLORD
+#include <colord.h>
+#endif
 
 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
 
@@ -92,6 +93,7 @@ typedef struct
 
   http_t *http;
   GtkCupsRequest *request;
+  GtkCupsPollState poll_state;
   GPollFD *data_poll;
   GtkPrintBackendCups *backend;
   GtkPrintCupsResponseCallbackFunc callback;
@@ -125,6 +127,9 @@ struct _GtkPrintBackendCups
   GHashTable *auth;
   gchar      *username;
   gboolean    authentication_lock;
+#ifdef HAVE_COLORD
+  CdClient   *colord_client;
+#endif
 };
 
 static GObjectClass *backend_parent_class;
@@ -317,7 +322,7 @@ _cairo_write_to_cups (void                *closure,
        }    
 
       GTK_NOTE (PRINTING,
-                g_print ("CUPS Backend: Wrote %i bytes to temp file\n", written));
+                g_print ("CUPS Backend: Wrote %"G_GSIZE_FORMAT" bytes to temp file\n", written));
 
       data += written;
       length -= written;
@@ -333,18 +338,19 @@ cups_printer_create_cairo_surface (GtkPrinter       *printer,
                                   gdouble           height,
                                   GIOChannel       *cache_io)
 {
-  cairo_surface_t *surface; 
+  cairo_surface_t *surface;
   ppd_file_t      *ppd_file = NULL;
   ppd_attr_t      *ppd_attr = NULL;
   ppd_attr_t      *ppd_attr_res = NULL;
   ppd_attr_t      *ppd_attr_screen_freq = NULL;
   ppd_attr_t      *ppd_attr_res_screen_freq = NULL;
   gchar           *res_string = NULL;
-  int              level = 2;
-  /* TODO: check if it is a ps or pdf printer */
-  
-  surface = cairo_ps_surface_create_for_stream  (_cairo_write_to_cups, cache_io, width, height);
+  gint             level = 2;
+
+  if (gtk_printer_accepts_pdf (printer))
+    surface = cairo_pdf_surface_create_for_stream (_cairo_write_to_cups, cache_io, width, height);
+  else
+    surface = cairo_ps_surface_create_for_stream  (_cairo_write_to_cups, cache_io, width, height);
 
   ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
 
@@ -376,14 +382,14 @@ cups_printer_create_cairo_surface (GtkPrinter       *printer,
             }
         }
 
-      res_string = g_strdup_printf ("%ddpi", 
+      res_string = g_strdup_printf ("%ddpi",
                                     gtk_print_settings_get_resolution (settings));
       ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
       g_free (res_string);
 
       if (ppd_attr_res_screen_freq == NULL)
         {
-          res_string = g_strdup_printf ("%dx%ddpi", 
+          res_string = g_strdup_printf ("%dx%ddpi",
                                         gtk_print_settings_get_resolution_x (settings),
                                         gtk_print_settings_get_resolution_y (settings));
           ppd_attr_res_screen_freq = ppdFindAttr (ppd_file, "ResScreenFreq", res_string);
@@ -398,11 +404,14 @@ cups_printer_create_cairo_surface (GtkPrinter       *printer,
         gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_screen_freq->value));
     }
 
-  if (level == 2)
-    cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
+  if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PS)
+    {
+      if (level == 2)
+        cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_2);
 
-  if (level == 3)
-    cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
+      if (level == 3)
+        cairo_ps_surface_restrict_to_level (surface, CAIRO_PS_LEVEL_3);
+    }
 
   cairo_surface_set_fallback_resolution (surface,
                                          2.0 * gtk_print_settings_get_printer_lpi (settings),
@@ -478,22 +487,68 @@ cups_print_cb (GtkPrintBackendCups *print_backend,
   GDK_THREADS_LEAVE ();  
 }
 
+typedef struct {
+  GtkCupsRequest *request;
+  GtkPrinterCups *printer;
+} CupsOptionsData;
+
 static void
 add_cups_options (const gchar *key,
                  const gchar *value,
                  gpointer     user_data)
 {
-  GtkCupsRequest *request = user_data;
+  CupsOptionsData *data = (CupsOptionsData *) user_data;
+  GtkCupsRequest *request = data->request;
+  GtkPrinterCups *printer = data->printer;
+  gboolean custom_value = FALSE;
+  gchar *new_value = NULL;
+  gint i;
+
+  if (!key || !value)
+    return;
 
   if (!g_str_has_prefix (key, "cups-"))
     return;
 
   if (strcmp (value, "gtk-ignore-value") == 0)
     return;
-  
+
   key = key + strlen ("cups-");
 
-  gtk_cups_request_encode_option (request, key, value);
+  if (printer && printer->ppd_file)
+    {
+      ppd_coption_t *coption;
+      gboolean       found = FALSE;
+      gboolean       custom_values_enabled = FALSE;
+
+      coption = ppdFindCustomOption (printer->ppd_file, key);
+      if (coption && coption->option)
+        {
+          for (i = 0; i < coption->option->num_choices; i++)
+            {
+              /* Are custom values enabled ? */
+              if (g_str_equal (coption->option->choices[i].choice, "Custom"))
+                custom_values_enabled = TRUE;
+
+              /* Is the value among available choices ? */
+              if (g_str_equal (coption->option->choices[i].choice, value))
+                found = TRUE;
+            }
+
+          if (custom_values_enabled && !found)
+            custom_value = TRUE;
+        }
+    }
+
+  /* Add "Custom." prefix to custom values. */
+  if (custom_value)
+    {
+      new_value = g_strdup_printf ("Custom.%s", value);
+      gtk_cups_request_encode_option (request, key, new_value);
+      g_free (new_value);
+    }
+  else
+    gtk_cups_request_encode_option (request, key, value);
 }
 
 static void
@@ -506,6 +561,7 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
 {
   GtkPrinterCups *cups_printer;
   CupsPrintStreamData *ps;
+  CupsOptionsData *options_data;
   GtkCupsRequest *request;
   GtkPrintSettings *settings;
   const gchar *title;
@@ -553,8 +609,12 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
                                      IPP_TAG_NAME, "job-name", 
                                      NULL, title);
 
-  gtk_print_settings_foreach (settings, add_cups_options, request);
-  
+  options_data = g_new0 (CupsOptionsData, 1);
+  options_data->request = request;
+  options_data->printer = cups_printer;
+  gtk_print_settings_foreach (settings, add_cups_options, options_data);
+  g_free (options_data);
+
   ps = g_new0 (CupsPrintStreamData, 1);
   ps->callback = callback;
   ps->user_data = user_data;
@@ -603,6 +663,10 @@ gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
 
   backend_cups->username = NULL;
 
+#ifdef HAVE_COLORD
+  backend_cups->colord_client = cd_client_new ();
+#endif
+
   cups_get_local_default_printer (backend_cups);
 }
 
@@ -629,6 +693,10 @@ gtk_print_backend_cups_finalize (GObject *object)
 
   g_free (backend_cups->username);
 
+#ifdef HAVE_COLORD
+  g_object_unref (backend_cups->colord_client);
+#endif
+
   backend_parent_class->finalize (object);
 }
 
@@ -665,40 +733,6 @@ is_address_local (const gchar *address)
     return FALSE;
 }
 
-#ifndef HAVE_CUPS_API_1_2
-/* Included from CUPS library because of backward compatibility */
-const char *
-httpGetHostname(http_t *http,
-                char   *s,
-                int    slen)
-{
-  struct hostent *host;
-
-  if (!s || slen <= 1)
-    return (NULL);
-
-  if (http)
-    {
-      if (http->hostname[0] == '/')
-        g_strlcpy (s, "localhost", slen);
-      else
-        g_strlcpy (s, http->hostname, slen);
-    }
-  else
-    {
-      if (gethostname (s, slen) < 0)
-        g_strlcpy (s, "localhost", slen);
-
-      if (!strchr (s, '.'))
-        {
-          if ((host = gethostbyname (s)) != NULL && host->h_name)
-            g_strlcpy (s, host->h_name, slen);
-        }
-    }
-  return (s);
-}
-#endif
-
 static void
 gtk_print_backend_cups_set_password (GtkPrintBackend  *backend,
                                      gchar           **auth_info_required,
@@ -707,7 +741,6 @@ gtk_print_backend_cups_set_password (GtkPrintBackend  *backend,
   GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
   GList *l;
   char   dispatch_hostname[HTTP_MAX_URI];
-  gchar *key;
   gchar *username = NULL;
   gchar *hostname = NULL;
   gchar *password = NULL;
@@ -729,15 +762,15 @@ gtk_print_backend_cups_set_password (GtkPrintBackend  *backend,
 
   if (hostname != NULL && username != NULL && password != NULL)
     {
-      key = g_strconcat (username, "@", hostname, NULL);
+      gchar *key = g_strconcat (username, "@", hostname, NULL);
       g_hash_table_insert (cups_backend->auth, key, g_strdup (password));
+      GTK_NOTE (PRINTING,
+                g_print ("CUPS backend: storing password for %s\n", key));
     }
 
   g_free (cups_backend->username);
   cups_backend->username = g_strdup (username);
 
-  GTK_NOTE (PRINTING,
-            g_print ("CUPS backend: storing password for %s\n", key));
 
   for (l = cups_backend->requests; l; l = l->next)
     {
@@ -787,7 +820,7 @@ request_password (gpointer data)
   gint                       i;
 
   if (dispatch->backend->authentication_lock)
-    return FALSE;
+    return G_SOURCE_REMOVE;
 
   httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
   if (is_address_local (hostname))
@@ -846,9 +879,6 @@ request_password (gpointer data)
 
       switch (dispatch->request->ipp_request->request.op.operation_id)
         {
-          case 0:
-            prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
-            break;
           case IPP_PRINT_JOB:
             if (job_title != NULL && printer_name != NULL)
               prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
@@ -874,7 +904,11 @@ request_password (gpointer data)
             prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
             break;
           default:
-            prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
+            /* work around gcc warning about 0 not being a value for this enum */
+            if (dispatch->request->ipp_request->request.op.operation_id == 0)
+              prompt = g_strdup_printf ( _("Authentication is required to get a file from %s"), hostname);
+            else
+              prompt = g_strdup_printf ( _("Authentication is required on %s"), hostname);
             break;
         }
 
@@ -899,7 +933,7 @@ request_password (gpointer data)
   g_free (auth_info_visible);
   g_free (key);
 
-  return FALSE;
+  return G_SOURCE_REMOVE;
 }
 
 static void
@@ -912,11 +946,20 @@ cups_dispatch_add_poll (GSource *source)
 
   poll_state = gtk_cups_request_get_poll_state (dispatch->request);
 
+  /* Remove the old source if the poll state changed. */
+  if (poll_state != dispatch->poll_state && dispatch->data_poll != NULL)
+    {
+      g_source_remove_poll (source, dispatch->data_poll);
+      g_free (dispatch->data_poll);
+      dispatch->data_poll = NULL;
+    }
+
   if (dispatch->request->http != NULL)
     {
       if (dispatch->data_poll == NULL)
         {
          dispatch->data_poll = g_new0 (GPollFD, 1);
+         dispatch->poll_state = poll_state;
 
          if (poll_state == GTK_CUPS_HTTP_READ)
            dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
@@ -925,11 +968,7 @@ cups_dispatch_add_poll (GSource *source)
          else
            dispatch->data_poll->events = 0;
 
-#ifdef HAVE_CUPS_API_1_2
           dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
-#else
-          dispatch->data_poll->fd = dispatch->request->http->fd;
-#endif
           g_source_add_poll (source, dispatch->data_poll);
         }
     }
@@ -963,7 +1002,7 @@ check_auth_info (gpointer user_data)
                                             "auth-info",
                                             length,
                                             NULL,
-                                            dispatch->request->auth_info);
+                                            (const char * const *) dispatch->request->auth_info);
 
           g_source_attach ((GSource *) dispatch, NULL);
           g_source_unref ((GSource *) dispatch);
@@ -974,10 +1013,10 @@ check_auth_info (gpointer user_data)
           dispatch->request->auth_info = NULL;
         }
 
-      return FALSE;
+      return G_SOURCE_REMOVE;
     }
 
-  return TRUE;
+  return G_SOURCE_CONTINUE;
 }
 
 static gboolean
@@ -1088,13 +1127,11 @@ cups_dispatch_watch_check (GSource *source)
 
   poll_state = gtk_cups_request_get_poll_state (dispatch->request);
 
-  cups_dispatch_add_poll (source);
-    
   if (poll_state != GTK_CUPS_HTTP_IDLE && !dispatch->request->need_password)
     if (!(dispatch->data_poll->revents & dispatch->data_poll->events)) 
        return FALSE;
   
-  result = gtk_cups_request_read_write (dispatch->request);
+  result = gtk_cups_request_read_write (dispatch->request, FALSE);
   if (result && dispatch->data_poll != NULL)
     {
       g_source_remove_poll (source, dispatch->data_poll);
@@ -1125,8 +1162,8 @@ cups_dispatch_watch_prepare (GSource *source,
             g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
 
   *timeout_ = -1;
-  
-  result = gtk_cups_request_read_write (dispatch->request);
+
+  result = gtk_cups_request_read_write (dispatch->request, TRUE);
 
   cups_dispatch_add_poll (source);
 
@@ -1226,7 +1263,12 @@ cups_dispatch_watch_finalize (GSource *source)
       dispatch->backend = NULL;
     }
 
-  g_free (dispatch->data_poll);
+  if (dispatch->data_poll)
+    {
+      g_source_remove_poll (source, dispatch->data_poll);
+      g_free (dispatch->data_poll);
+      dispatch->data_poll = NULL;
+    }
 }
 
 static GSourceFuncs _cups_dispatch_watch_funcs = {
@@ -1248,12 +1290,14 @@ cups_request_execute (GtkPrintBackendCups              *print_backend,
 
   dispatch = (GtkPrintCupsDispatchWatch *) g_source_new (&_cups_dispatch_watch_funcs, 
                                                          sizeof (GtkPrintCupsDispatchWatch));
+  g_source_set_name (&dispatch->source, "GTK+ CUPS backend");
 
   GTK_NOTE (PRINTING,
             g_print ("CUPS Backend: %s <source %p> - Executing cups request on server '%s' and resource '%s'\n", G_STRFUNC, dispatch, request->server, request->resource));
 
   dispatch->request = request;
   dispatch->backend = g_object_ref (print_backend);
+  dispatch->poll_state = GTK_CUPS_HTTP_IDLE;
   dispatch->data_poll = NULL;
   dispatch->callback = NULL;
   dispatch->callback_data = NULL;
@@ -1325,7 +1369,7 @@ cups_request_printer_info_cb (GtkPrintBackendCups *backend,
   response = gtk_cups_result_get_response (result);
 
   /* TODO: determine printer type and use correct icon */
-  gtk_printer_set_icon_name (printer, "gtk-print");
+  gtk_printer_set_icon_name (printer, "printer");
  
   state_msg = "";
   loc = "";
@@ -1541,7 +1585,7 @@ cups_job_info_poll_timeout (gpointer user_data)
   else
     cups_request_job_info (data);
   
-  return FALSE;
+  return G_SOURCE_REMOVE;
 }
 
 static void
@@ -1664,7 +1708,6 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
           "media-low",
           "media-empty",
           "offline",
-          "connecting-to-device",
           "other"
         };
       static const char * reasons_descs[] =
@@ -1683,8 +1726,7 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
           N_("The door is open on printer '%s'."),
           N_("Printer '%s' is low on paper."),
           N_("Printer '%s' is out of paper."),
-          N_("Printer '%s' is currently off-line."),
-          N_("Printer '%s' may not be connected."),
+          N_("Printer '%s' is currently offline."),
           N_("There is a problem on printer '%s'.")
         };
       gboolean is_paused = FALSE;
@@ -1695,7 +1737,8 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
       gchar   *default_cover_after = NULL;
       gboolean remote_printer = FALSE;
       gchar  **auth_info_required = NULL;
-      
+      gint     default_number_up = 1;
+
       /* Skip leading attributes until we hit a printer...
        */
       while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
@@ -1822,6 +1865,10 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
                   auth_info_required[i] = g_strdup (attr->values[i].string.text);
               }
           }
+        else if (strcmp (attr->name, "number-up-default") == 0)
+          {
+            default_number_up = attr->values[0].integer;
+          }
         else
          {
            GTK_NOTE (PRINTING,
@@ -1879,7 +1926,13 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
          char *cups_server;            /* CUPS server */
          
           list_has_changed = TRUE;
-         cups_printer = gtk_printer_cups_new (printer_name, backend);
+#ifdef HAVE_COLORD
+          cups_printer = gtk_printer_cups_new (printer_name,
+                                               backend,
+                                               cups_backend->colord_client);
+#else
+          cups_printer = gtk_printer_cups_new (printer_name, backend, NULL);
+#endif
 
          cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
 
@@ -1940,6 +1993,8 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
           cups_printer->default_cover_before = g_strdup (default_cover_before);
           cups_printer->default_cover_after = g_strdup (default_cover_after);
 
+          cups_printer->default_number_up = default_number_up;
+
          cups_printer->hostname = g_strdup (hostname);
          cups_printer->port = port;
          
@@ -2048,13 +2103,13 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
       /* Set printer icon according to importance
          (none, report, warning, error - report is omitted). */
       if (printer_state_reason_level == 3)
-        gtk_printer_set_icon_name (printer, "gtk-print-error");
+        gtk_printer_set_icon_name (printer, "printer-error");
       else if (printer_state_reason_level == 2)
-        gtk_printer_set_icon_name (printer, "gtk-print-warning");
+        gtk_printer_set_icon_name (printer, "printer-warning");
       else if (gtk_printer_is_paused (printer))
-        gtk_printer_set_icon_name (printer, "gtk-print-paused");
+        gtk_printer_set_icon_name (printer, "printer-paused");
       else
-        gtk_printer_set_icon_name (printer, "gtk-print");
+        gtk_printer_set_icon_name (printer, "printer");
 
       if (status_changed)
         g_signal_emit_by_name (GTK_PRINT_BACKEND (backend),
@@ -2071,8 +2126,7 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
      as inactive if it is in the list, emitting a printer_removed signal */
   if (removed_printer_checklist != NULL)
     {
-      g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
-      g_list_free (removed_printer_checklist);
+      g_list_free_full (removed_printer_checklist, (GDestroyNotify) mark_printer_inactive);
       list_has_changed = TRUE;
     }
   
@@ -2141,7 +2195,8 @@ cups_request_printer_list (GtkPrintBackendCups *cups_backend)
       "job-sheets-supported",
       "job-sheets-default",
       "printer-type",
-      "auth-info-required"
+      "auth-info-required",
+      "number-up-default"
     };
 
   if (cups_backend->reading_ppds > 0 || cups_backend->list_printers_pending)
@@ -2232,7 +2287,6 @@ cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
                      GtkCupsResult       *result,
                      GetPPDData          *data)
 {
-  ipp_t *response;
   GtkPrinter *printer;
 
   GDK_THREADS_ENTER ();
@@ -2261,11 +2315,12 @@ cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
       goto done;
     }
 
-  response = gtk_cups_result_get_response (result);
-
   /* let ppdOpenFd take over the ownership of the open file */
   g_io_channel_seek_position (data->ppd_io, 0, G_SEEK_SET, NULL);
   data->printer->ppd_file = ppdOpenFd (dup (g_io_channel_unix_get_fd (data->ppd_io)));
+#if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
+  ppdLocalize (data->printer->ppd_file);
+#endif
 
   ppdMarkDefaults (data->printer->ppd_file);
   
@@ -2343,7 +2398,7 @@ cups_request_ppd (GtkPrinter *printer)
 
 #ifdef G_ENABLE_DEBUG 
   /* If we are debugging printing don't delete the tmp files */
-  if (!(gtk_debug_flags & GTK_DEBUG_PRINTING))
+  if (!(gtk_get_debug_flags () & GTK_DEBUG_PRINTING))
     unlink (ppd_filename);
 #else
   unlink (ppd_filename);
@@ -2830,13 +2885,23 @@ static const struct {
 static const struct {
   const char *ppd_keyword;
   const char *name;
-} option_names[] = {
+} ppd_option_names[] = {
   {"Duplex", "gtk-duplex" },
   {"MediaType", "gtk-paper-type"},
   {"InputSlot", "gtk-paper-source"},
   {"OutputBin", "gtk-output-tray"},
 };
 
+static const struct {
+  const char *lpoption;
+  const char *name;
+} lpoption_names[] = {
+  {"number-up", "gtk-n-up" },
+  {"number-up-layout", "gtk-n-up-layout"},
+  {"job-billing", "gtk-billing-info"},
+  {"job-priority", "gtk-job-prio"},
+};
+
 /* keep sorted when changing */
 static const char *color_option_whitelist[] = {
   "BRColorEnhancement",
@@ -3208,9 +3273,7 @@ create_pickone_option (ppd_file_t   *ppd_file,
   char *label;
   int n_choices;
   int i;
-#ifdef HAVE_CUPS_API_1_2
   ppd_coption_t *coption;
-#endif
 
   g_assert (ppd_option->ui == PPD_UI_PICKONE);
   
@@ -3226,7 +3289,6 @@ create_pickone_option (ppd_file_t   *ppd_file,
 
       label = get_option_text (ppd_file, ppd_option);
 
-#ifdef HAVE_CUPS_API_1_2
       coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
 
       if (coption)
@@ -3279,7 +3341,6 @@ create_pickone_option (ppd_file_t   *ppd_file,
            g_warning ("CUPS Backend: Multi-parameter PPD Custom Option not supported");
 #endif
        }
-#endif /* HAVE_CUPS_API_1_2 */
 
       if (!option)
         option = gtk_printer_option_new (gtk_name, label,
@@ -3301,7 +3362,18 @@ create_pickone_option (ppd_file_t   *ppd_file,
              option->choices_display[i] = get_choice_text (ppd_file, available[i]);
            }
        }
-      gtk_printer_option_set (option, ppd_option->defchoice);
+
+      if (option->type != GTK_PRINTER_OPTION_TYPE_PICKONE)
+        {
+          if (g_str_has_prefix (ppd_option->defchoice, "Custom."))
+            gtk_printer_option_set (option, ppd_option->defchoice + 7);
+          else
+            gtk_printer_option_set (option, ppd_option->defchoice);
+        }
+      else
+        {
+          gtk_printer_option_set (option, ppd_option->defchoice);
+        }
     }
 #ifdef PRINT_IGNORED_OPTIONS
   else
@@ -3352,17 +3424,33 @@ create_boolean_option (ppd_file_t   *ppd_file,
 }
 
 static gchar *
-get_option_name (const gchar *keyword)
+get_ppd_option_name (const gchar *keyword)
 {
   int i;
 
-  for (i = 0; i < G_N_ELEMENTS (option_names); i++)
-    if (strcmp (option_names[i].ppd_keyword, keyword) == 0)
-      return g_strdup (option_names[i].name);
+  for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
+    if (strcmp (ppd_option_names[i].ppd_keyword, keyword) == 0)
+      return g_strdup (ppd_option_names[i].name);
 
   return g_strdup_printf ("cups-%s", keyword);
 }
 
+static gchar *
+get_lpoption_name (const gchar *lpoption)
+{
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (ppd_option_names); i++)
+    if (strcmp (ppd_option_names[i].ppd_keyword, lpoption) == 0)
+      return g_strdup (ppd_option_names[i].name);
+
+  for (i = 0; i < G_N_ELEMENTS (lpoption_names); i++)
+    if (strcmp (lpoption_names[i].lpoption, lpoption) == 0)
+      return g_strdup (lpoption_names[i].name);
+
+  return g_strdup_printf ("cups-%s", lpoption);
+}
+
 static int
 strptr_cmp (const void *a, 
            const void *b)
@@ -3397,7 +3485,7 @@ handle_option (GtkPrinterOptionSet *set,
   if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
     return;
 
-  name = get_option_name (ppd_option->keyword);
+  name = get_ppd_option_name (ppd_option->keyword);
 
   option = NULL;
   if (ppd_option->ui == PPD_UI_PICKONE)
@@ -3485,6 +3573,23 @@ handle_group (GtkPrinterOptionSet *set,
 
 }
 
+#ifdef HAVE_COLORD
+
+typedef struct {
+        GtkPrintSettings     *settings;
+        GtkPrinter           *printer;
+} GtkPrintBackendCupsColordHelper;
+
+static void
+colord_printer_option_set_changed_cb (GtkPrinterOptionSet *set,
+                                      GtkPrintBackendCupsColordHelper *helper)
+{
+  gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (helper->printer),
+                                    helper->settings,
+                                    set);
+}
+#endif
+
 static GtkPrinterOptionSet *
 cups_printer_get_options (GtkPrinter           *printer,
                          GtkPrintSettings     *settings,
@@ -3516,46 +3621,15 @@ cups_printer_get_options (GtkPrinter           *printer,
   GtkPrintBackendCups *backend;
   GtkTextDirection text_direction;
   GtkPrinterCups *cups_printer = NULL;
-
+#ifdef HAVE_COLORD
+  GtkPrintBackendCupsColordHelper *helper;
+#endif
+  char *default_number_up;
 
   set = gtk_printer_option_set_new ();
 
   /* Cups specific, non-ppd related settings */
 
-   /* Translators, this string is used to label the pages-per-sheet option 
-    * in the print dialog 
-    */
-  option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
-  gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
-                                        n_up, n_up);
-  gtk_printer_option_set (option, "1");
-  set_option_from_settings (option, settings);
-  gtk_printer_option_set_add (set, option);
-  g_object_unref (option);
-
-  if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
-    {
-      for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
-        n_up_layout_display[i] = _(n_up_layout_display[i]);
-  
-       /* Translators, this string is used to label the option in the print 
-        * dialog that controls in what order multiple pages are arranged 
-        */
-      option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
-      gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
-                                             n_up_layout, n_up_layout_display);
-
-      text_direction = gtk_widget_get_default_direction ();
-      if (text_direction == GTK_TEXT_DIR_LTR)
-        gtk_printer_option_set (option, "lrtb");
-      else
-        gtk_printer_option_set (option, "rltb");
-
-      set_option_from_settings (option, settings);
-      gtk_printer_option_set_add (set, option);
-      g_object_unref (option);
-    }
-
   for (i = 0; i < G_N_ELEMENTS(prio_display); i++)
     prio_display[i] = _(prio_display[i]);
   
@@ -3596,6 +3670,42 @@ cups_printer_get_options (GtkPrinter           *printer,
       gpointer value;
       gint j;
 
+       /* Translators, this string is used to label the pages-per-sheet option 
+        * in the print dialog 
+        */
+      option = gtk_printer_option_new ("gtk-n-up", _("Pages per Sheet"), GTK_PRINTER_OPTION_TYPE_PICKONE);
+      gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up),
+                                            n_up, n_up);
+      default_number_up = g_strdup_printf ("%d", cups_printer->default_number_up);
+      gtk_printer_option_set (option, default_number_up);
+      g_free (default_number_up);
+      set_option_from_settings (option, settings);
+      gtk_printer_option_set_add (set, option);
+      g_object_unref (option);
+
+      if (cups_printer_get_capabilities (printer) & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
+        {
+          for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
+            n_up_layout_display[i] = _(n_up_layout_display[i]);
+
+           /* Translators, this string is used to label the option in the print 
+            * dialog that controls in what order multiple pages are arranged 
+            */
+          option = gtk_printer_option_new ("gtk-n-up-layout", _("Page Ordering"), GTK_PRINTER_OPTION_TYPE_PICKONE);
+          gtk_printer_option_choices_from_array (option, G_N_ELEMENTS (n_up_layout),
+                                                 n_up_layout, n_up_layout_display);
+
+          text_direction = gtk_widget_get_default_direction ();
+          if (text_direction == GTK_TEXT_DIR_LTR)
+            gtk_printer_option_set (option, "lrtb");
+          else
+            gtk_printer_option_set (option, "rltb");
+
+          set_option_from_settings (option, settings);
+          gtk_printer_option_set_add (set, option);
+          g_object_unref (option);
+        }
+
       num_of_covers = backend->number_of_covers;
       cover = g_new (char *, num_of_covers + 1);
       cover[num_of_covers] = NULL;
@@ -3721,15 +3831,99 @@ cups_printer_get_options (GtkPrinter           *printer,
       if (STRING_IN_TABLE (opts[i].name, cups_option_blacklist))
         continue;
 
-      name = get_option_name (opts[i].name);
-      option = gtk_printer_option_set_lookup (set, name);
-      if (option)
-        gtk_printer_option_set (option, opts[i].value);
+      name = get_lpoption_name (opts[i].name);
+      if (strcmp (name, "cups-job-sheets") == 0)
+        {
+          gchar **values;
+          gint    num_values;
+          
+          values = g_strsplit (opts[i].value, ",", 2);
+          num_values = g_strv_length (values);
+
+          option = gtk_printer_option_set_lookup (set, "gtk-cover-before");
+          if (option && num_values > 0)
+            gtk_printer_option_set (option, g_strstrip (values[0]));
+
+          option = gtk_printer_option_set_lookup (set, "gtk-cover-after");
+          if (option && num_values > 1)
+            gtk_printer_option_set (option, g_strstrip (values[1]));
+
+          g_strfreev (values);
+        }
+      else if (strcmp (name, "cups-job-hold-until") == 0)
+        {
+          GtkPrinterOption *option2 = NULL;
+
+          option = gtk_printer_option_set_lookup (set, "gtk-print-time-text");
+          if (option && opts[i].value)
+            {
+              option2 = gtk_printer_option_set_lookup (set, "gtk-print-time");
+              if (option2)
+                {
+                  if (strcmp (opts[i].value, "indefinite") == 0)
+                    gtk_printer_option_set (option2, "on-hold");
+                  else
+                    {
+                      gtk_printer_option_set (option2, "at");
+                      gtk_printer_option_set (option, opts[i].value);
+                    }
+                }
+            }
+        }
+      else if (strcmp (name, "cups-sides") == 0)
+        {
+          option = gtk_printer_option_set_lookup (set, "gtk-duplex");
+          if (option && opts[i].value)
+            {
+              if (strcmp (opts[i].value, "two-sided-short-edge") == 0)
+                gtk_printer_option_set (option, "DuplexTumble");
+              else if (strcmp (opts[i].value, "two-sided-long-edge") == 0)
+                gtk_printer_option_set (option, "DuplexNoTumble");
+            }
+        }
+      else
+        {
+          option = gtk_printer_option_set_lookup (set, name);
+          if (option)
+            gtk_printer_option_set (option, opts[i].value);
+        }
       g_free (name);
     }
 
   cupsFreeOptions (num_opts, opts);
 
+#ifdef HAVE_COLORD
+  /* TRANSLATORS: this this the ICC color profile to use for this job */
+  option = gtk_printer_option_new ("colord-profile",
+                                   _("Printer Profile"),
+                                   GTK_PRINTER_OPTION_TYPE_INFO);
+
+  /* assign it to the color page */
+  option->group = g_strdup ("ColorPage");
+
+  /* TRANSLATORS: this is when color profile information is unavailable */
+  gtk_printer_option_set (option, _("Unavailable"));
+  gtk_printer_option_set_add (set, option);
+
+  /* watch to see if the user changed the options */
+  helper = g_new (GtkPrintBackendCupsColordHelper, 1);
+  helper->printer = printer;
+  helper->settings = settings;
+  g_signal_connect_data (set, "changed",
+                         G_CALLBACK (colord_printer_option_set_changed_cb),
+                         helper,
+                         (GClosureNotify) g_free,
+                         0);
+
+  /* initial coldplug */
+  gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (printer),
+                                    settings, set);
+  g_object_bind_property (printer, "profile-title",
+                          option, "value",
+                          G_BINDING_DEFAULT);
+
+#endif
+
   return set;
 }
 
@@ -3740,7 +3934,7 @@ mark_option_from_set (GtkPrinterOptionSet *set,
                      ppd_option_t        *ppd_option)
 {
   GtkPrinterOption *option;
-  char *name = get_option_name (ppd_option->keyword);
+  char *name = get_ppd_option_name (ppd_option->keyword);
 
   option = gtk_printer_option_set_lookup (set, name);
 
@@ -3775,7 +3969,7 @@ set_conflicts_from_option (GtkPrinterOptionSet *set,
 
   if (ppd_option->conflicted)
     {
-      name = get_option_name (ppd_option->keyword);
+      name = get_ppd_option_name (ppd_option->keyword);
       option = gtk_printer_option_set_lookup (set, name);
 
       if (option)
@@ -4013,23 +4207,29 @@ set_option_from_settings (GtkPrinterOption *option,
        gtk_printer_option_set (option, cups_value);
       else
        {
-         int res = gtk_print_settings_get_resolution (settings);
-         int res_x = gtk_print_settings_get_resolution_x (settings);
-         int res_y = gtk_print_settings_get_resolution_y (settings);
+         if (gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION, -1) != -1 ||
+             gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_X, -1) != -1 ||
+             gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_Y, -1) != -1 ||
+             option->value == NULL || option->value[0] == '\0')
+           {
+              int res = gtk_print_settings_get_resolution (settings);
+              int res_x = gtk_print_settings_get_resolution_x (settings);
+              int res_y = gtk_print_settings_get_resolution_y (settings);
 
-          if (res_x != res_y)
-            {
-             value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
-             gtk_printer_option_set (option, value);
-             g_free (value);
+              if (res_x != res_y)
+                {
+                  value = g_strdup_printf ("%dx%ddpi", res_x, res_y);
+                  gtk_printer_option_set (option, value);
+                  g_free (value);
+                }
+              else if (res != 0)
+                {
+                  value = g_strdup_printf ("%ddpi", res);
+                  gtk_printer_option_set (option, value);
+                  g_free (value);
+                }
             }
-          else if (res != 0)
-           {
-             value = g_strdup_printf ("%ddpi", res);
-             gtk_printer_option_set (option, value);
-             g_free (value);
-           }
-       }
+        }
     }
   else if (strcmp (option->name, "gtk-paper-type") == 0)
     map_settings_to_option (option, media_type_map, G_N_ELEMENTS (media_type_map),
@@ -4289,44 +4489,49 @@ cups_printer_prepare_for_print (GtkPrinter       *printer,
                                GtkPrintSettings *settings,
                                GtkPageSetup     *page_setup)
 {
+  GtkPrintPages pages;
+  GtkPageRange *ranges;
+  gint n_ranges;
   GtkPageSet page_set;
   GtkPaperSize *paper_size;
   const char *ppd_paper_name;
   double scale;
 
-  print_job->print_pages = gtk_print_settings_get_print_pages (settings);
-  print_job->page_ranges = NULL;
-  print_job->num_page_ranges = 0;
-  
-  if (print_job->print_pages == GTK_PRINT_PAGES_RANGES)
-    print_job->page_ranges =
-      gtk_print_settings_get_page_ranges (settings,
-                                         &print_job->num_page_ranges);
-  
+  pages = gtk_print_settings_get_print_pages (settings);
+  gtk_print_job_set_pages (print_job, pages);
+
+  if (pages == GTK_PRINT_PAGES_RANGES)
+    ranges = gtk_print_settings_get_page_ranges (settings, &n_ranges);
+  else
+    {
+      ranges = NULL;
+      n_ranges = 0;
+    }
+
+  gtk_print_job_set_page_ranges (print_job, ranges, n_ranges);
   if (gtk_print_settings_get_collate (settings))
     gtk_print_settings_set (settings, "cups-Collate", "True");
-  print_job->collate = FALSE;
+  gtk_print_job_set_collate (print_job, FALSE);
 
   if (gtk_print_settings_get_reverse (settings))
     gtk_print_settings_set (settings, "cups-OutputOrder", "Reverse");
-  print_job->reverse = FALSE;
+  gtk_print_job_set_reverse (print_job, FALSE);
 
   if (gtk_print_settings_get_n_copies (settings) > 1)
     gtk_print_settings_set_int (settings, "cups-copies",
-                               gtk_print_settings_get_n_copies (settings));
-  print_job->num_copies = 1;
+                                gtk_print_settings_get_n_copies (settings));
+  gtk_print_job_set_num_copies (print_job, 1);
 
   scale = gtk_print_settings_get_scale (settings);
-  print_job->scale = 1.0;
   if (scale != 100.0)
-    print_job->scale = scale/100.0;
+    gtk_print_job_set_scale (print_job, scale / 100.0);
 
   page_set = gtk_print_settings_get_page_set (settings);
   if (page_set == GTK_PAGE_SET_EVEN)
     gtk_print_settings_set (settings, "cups-page-set", "even");
   else if (page_set == GTK_PAGE_SET_ODD)
     gtk_print_settings_set (settings, "cups-page-set", "odd");
-  print_job->page_set = GTK_PAGE_SET_ALL;
+  gtk_print_job_set_page_set (print_job, GTK_PAGE_SET_ALL);
 
   paper_size = gtk_page_setup_get_paper_size (page_setup);
   ppd_paper_name = gtk_paper_size_get_ppd_name (paper_size);
@@ -4378,7 +4583,7 @@ cups_printer_prepare_for_print (GtkPrinter       *printer,
       g_type_class_unref (enum_class);
     }
 
-  print_job->rotate_to_orientation = TRUE;
+  gtk_print_job_set_rotate (print_job, TRUE);
 }
 
 static GtkPageSetup *
@@ -4461,7 +4666,12 @@ cups_printer_get_default_page_size (GtkPrinter *printer)
     return NULL;
 
   option = ppdFindOption (ppd_file, "PageSize");
+  if (option == NULL)
+    return NULL;
+
   size = ppdPageSize (ppd_file, option->defchoice); 
+  if (size == NULL)
+    return NULL;
 
   return create_page_setup (ppd_file, size);
 }