]> Pileus Git - ~andy/gtk/blobdiff - modules/printbackends/cups/gtkprintbackendcups.c
Extract reasons and reasons_desc arrays to file level
[~andy/gtk] / modules / printbackends / cups / gtkprintbackendcups.c
index aafe774f5c2f78473673fac82ff7fb8905c749a2..f26214af6a17c5cc6abf5dc2cbf62140e47b85fc 100644 (file)
  * 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__
+#define _GNU_SOURCE
+#endif
+
 #include "config.h"
 #include <ctype.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
+#include <time.h>
+/* Cups 1.6 deprecates ppdFindAttr(), ppdFindCustomOption(),
+ * ppdFirstCustomParam(), and ppdNextCustomParam() among others. This
+ * turns off the warning so that it will compile.
+ */
+#ifdef HAVE_CUPS_API_1_6
+# define _PPD_DEPRECATED
+#endif
 
 #include <cups/cups.h>
 #include <cups/language.h>
@@ -49,6 +59,9 @@
 
 #include "gtkcupsutils.h"
 
+#ifdef HAVE_COLORD
+#include <colord.h>
+#endif
 
 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
 
@@ -87,8 +100,11 @@ typedef struct
 
   http_t *http;
   GtkCupsRequest *request;
+  GtkCupsPollState poll_state;
   GPollFD *data_poll;
   GtkPrintBackendCups *backend;
+  GtkPrintCupsResponseCallbackFunc callback;
+  gpointer                         callback_data;
 
 } GtkPrintCupsDispatchWatch;
 
@@ -105,14 +121,22 @@ struct _GtkPrintBackendCups
   
   guint list_printers_poll;
   guint list_printers_pending : 1;
+  gint  list_printers_attempts;
   guint got_default_printer   : 1;
   guint default_printer_poll;
   GtkCupsConnectionTest *cups_connection_test;
+  gint  reading_ppds;
 
   char **covers;
-  char  *default_cover_before;
-  char  *default_cover_after;
   int    number_of_covers;
+
+  GList      *requests;
+  GHashTable *auth;
+  gchar      *username;
+  gboolean    authentication_lock;
+#ifdef HAVE_COLORD
+  CdClient   *colord_client;
+#endif
 };
 
 static GObjectClass *backend_parent_class;
@@ -146,12 +170,12 @@ static GList *              cups_printer_list_papers               (GtkPrinter
 static GtkPageSetup *       cups_printer_get_default_page_size     (GtkPrinter                        *printer);
 static void                 cups_printer_request_details           (GtkPrinter                        *printer);
 static gboolean             cups_request_default_printer           (GtkPrintBackendCups               *print_backend);
-static void                 cups_request_ppd                       (GtkPrinter                        *printer);
-static void                 cups_printer_get_hard_margins          (GtkPrinter                        *printer,
-                                                                   double                            *top,
-                                                                   double                            *bottom,
-                                                                   double                            *left,
-                                                                   double                            *right);
+static gboolean             cups_request_ppd                       (GtkPrinter                        *printer);
+static gboolean             cups_printer_get_hard_margins          (GtkPrinter                        *printer,
+                                                                   gdouble                           *top,
+                                                                   gdouble                           *bottom,
+                                                                   gdouble                           *left,
+                                                                   gdouble                           *right);
 static GtkPrintCapabilities cups_printer_get_capabilities          (GtkPrinter                        *printer);
 static void                 set_option_from_settings               (GtkPrinterOption                  *option,
                                                                    GtkPrintSettings                  *setting);
@@ -171,11 +195,18 @@ static cairo_surface_t *    cups_printer_create_cairo_surface      (GtkPrinter
                                                                    gdouble                            height,
                                                                    GIOChannel                        *cache_io);
 
+static void                 gtk_print_backend_cups_set_password    (GtkPrintBackend                   *backend, 
+                                                                    gchar                            **auth_info_required,
+                                                                    gchar                            **auth_info);
+
+void                        overwrite_and_free                      (gpointer                          data);
+static gboolean             is_address_local                        (const gchar                      *address);
+static gboolean             request_auth_info                       (gpointer                          data);
 
 static void
 gtk_print_backend_cups_register_type (GTypeModule *module)
 {
-  static const GTypeInfo print_backend_cups_info =
+  const GTypeInfo print_backend_cups_info =
   {
     sizeof (GtkPrintBackendCupsClass),
     NULL,              /* base_init */
@@ -215,7 +246,22 @@ pb_module_create (void)
 {
   return gtk_print_backend_cups_new ();
 }
-
+/* CUPS 1.6 Getter/Setter Functions CUPS 1.6 makes private most of the
+ * IPP structures and enforces access via new getter functions, which
+ * are unfortunately not available in earlier versions. We define
+ * below those getter functions as macros for use when building
+ * against earlier CUPS versions.
+ */
+#ifndef HAVE_CUPS_API_1_6
+#define ippGetOperation(ipp_request) ipp_request->request.op.operation_id
+#define ippGetInteger(attr, index) attr->values[index].integer
+#define ippGetBoolean(attr, index) attr->values[index].boolean
+#define ippGetString(attr, index, foo) attr->values[index].string.text
+#define ippGetValueTag(attr) attr->value_tag
+#define ippGetName(attr) attr->name
+#define ippGetCount(attr) attr->num_values
+#define ippGetGroupTag(attr) attr->group_tag
+#endif
 /*
  * GtkPrintBackendCups
  */
@@ -266,6 +312,7 @@ gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
   backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
   backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
   backend_class->printer_get_capabilities = cups_printer_get_capabilities;
+  backend_class->set_password = gtk_print_backend_cups_set_password;
 }
 
 static cairo_status_t
@@ -297,7 +344,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;
@@ -313,18 +360,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));
 
@@ -345,27 +393,25 @@ cups_printer_create_cairo_surface (GtkPrinter       *printer,
 
               if (sscanf (ppd_attr_res->value, "%dx%ddpi", &res_x, &res_y) == 2)
                 {
-                  if (res_x != 0 && res_y != 0)
+                  if (res_x > 0 && res_y > 0)
                     gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
                 }
               else if (sscanf (ppd_attr_res->value, "%ddpi", &res) == 1)
                 {
-                  if (res != 0)
+                  if (res > 0)
                     gtk_print_settings_set_resolution (settings, res);
                 }
-              else
-                gtk_print_settings_set_resolution (settings, 300);
             }
         }
 
-      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);
@@ -374,24 +420,20 @@ cups_printer_create_cairo_surface (GtkPrinter       *printer,
 
       ppd_attr_screen_freq = ppdFindAttr (ppd_file, "ScreenFreq", NULL);
 
-      if (ppd_attr_res_screen_freq != NULL)
+      if (ppd_attr_res_screen_freq != NULL && atof (ppd_attr_res_screen_freq->value) > 0.0)
         gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_res_screen_freq->value));
-      else if (ppd_attr_screen_freq != NULL)
+      else if (ppd_attr_screen_freq != NULL && atof (ppd_attr_screen_freq->value) > 0.0)
         gtk_print_settings_set_printer_lpi (settings, atof (ppd_attr_screen_freq->value));
-      else
-        gtk_print_settings_set_printer_lpi (settings, 150.0);
-    }
-  else
-    {
-      gtk_print_settings_set_resolution (settings, 300);
-      gtk_print_settings_set_printer_lpi (settings, 150.0);
     }
 
-  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),
@@ -447,7 +489,7 @@ cups_print_cb (GtkPrintBackendCups *print_backend,
       ipp_t *response = gtk_cups_result_get_response (result);
 
       if ((attr = ippFindAttribute (response, "job-id", IPP_TAG_INTEGER)) != NULL)
-       job_id = attr->values[0].integer;
+       job_id = ippGetInteger (attr, 0);
 
       if (!gtk_print_job_get_track_print_status (ps->job) || job_id == 0)
        gtk_print_job_set_status (ps->job, GTK_PRINT_STATUS_FINISHED);
@@ -467,22 +509,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
@@ -495,6 +583,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;
@@ -506,12 +595,13 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
   cups_printer = GTK_PRINTER_CUPS (gtk_print_job_get_printer (job));
   settings = gtk_print_job_get_settings (job);
 
-  request = gtk_cups_request_new (NULL,
-                                  GTK_CUPS_POST,
-                                  IPP_PRINT_JOB,
-                                 data_io,
-                                 NULL, 
-                                 cups_printer->device_uri);
+  request = gtk_cups_request_new_with_username (NULL,
+                                                GTK_CUPS_POST,
+                                                IPP_PRINT_JOB,
+                                                data_io,
+                                                NULL,
+                                                cups_printer->device_uri,
+                                                GTK_PRINT_BACKEND_CUPS (print_backend)->username);
 
 #if (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR >= 2) || CUPS_VERSION_MAJOR > 1
   httpAssembleURIf (HTTP_URI_CODING_ALL,
@@ -541,14 +631,21 @@ 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;
   ps->dnotify = dnotify;
   ps->job = g_object_ref (job);
 
+  request->need_auth_info = cups_printer->auth_info_required != NULL;
+  request->auth_info_required = g_strdupv (cups_printer->auth_info_required);
+
   cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
                         request,
                         (GtkPrintCupsResponseCallbackFunc) cups_print_cb,
@@ -556,6 +653,16 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
                         (GDestroyNotify)cups_free_print_stream_data);
 }
 
+void overwrite_and_free (gpointer data)
+{
+  gchar *password = (gchar *) data;
+
+  if (password != NULL)
+    {
+      memset (password, 0, strlen (password));
+      g_free (password);
+    }
+}
 
 static void
 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
@@ -563,15 +670,25 @@ gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
   backend_cups->list_printers_poll = FALSE;  
   backend_cups->got_default_printer = FALSE;  
   backend_cups->list_printers_pending = FALSE;
+  backend_cups->list_printers_attempts = 0;
+  backend_cups->reading_ppds = 0;
+
+  backend_cups->requests = NULL;
+  backend_cups->auth = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, overwrite_and_free);
+  backend_cups->authentication_lock = FALSE;
 
   backend_cups->covers = NULL;
-  backend_cups->default_cover_before = NULL;
-  backend_cups->default_cover_after = NULL;
   backend_cups->number_of_covers = 0;
 
   backend_cups->default_printer_poll = 0;
   backend_cups->cups_connection_test = NULL;
 
+  backend_cups->username = NULL;
+
+#ifdef HAVE_COLORD
+  backend_cups->colord_client = cd_client_new ();
+#endif
+
   cups_get_local_default_printer (backend_cups);
 }
 
@@ -591,12 +708,17 @@ gtk_print_backend_cups_finalize (GObject *object)
   g_strfreev (backend_cups->covers);
   backend_cups->number_of_covers = 0;
 
-  g_free (backend_cups->default_cover_before);
-  g_free (backend_cups->default_cover_after);
-
   gtk_cups_connection_test_free (backend_cups->cups_connection_test);
   backend_cups->cups_connection_test = NULL;
 
+  g_hash_table_destroy (backend_cups->auth);
+
+  g_free (backend_cups->username);
+
+#ifdef HAVE_COLORD
+  g_object_unref (backend_cups->colord_client);
+#endif
+
   backend_parent_class->finalize (object);
 }
 
@@ -613,6 +735,7 @@ gtk_print_backend_cups_dispose (GObject *object)
   if (backend_cups->list_printers_poll > 0)
     g_source_remove (backend_cups->list_printers_poll);
   backend_cups->list_printers_poll = 0;
+  backend_cups->list_printers_attempts = 0;
   
   if (backend_cups->default_printer_poll > 0)
     g_source_remove (backend_cups->default_printer_poll);
@@ -621,56 +744,429 @@ gtk_print_backend_cups_dispose (GObject *object)
   backend_parent_class->dispose (object);
 }
 
+static gboolean
+is_address_local (const gchar *address)
+{
+  if (address[0] == '/' ||
+      strcmp (address, "127.0.0.1") == 0 ||
+      strcmp (address, "[::1]") == 0)
+    return TRUE;
+  else
+    return FALSE;
+}
+
+static void
+gtk_print_backend_cups_set_password (GtkPrintBackend  *backend,
+                                     gchar           **auth_info_required,
+                                     gchar           **auth_info)
+{
+  GtkPrintBackendCups *cups_backend = GTK_PRINT_BACKEND_CUPS (backend);
+  GList *l;
+  char   dispatch_hostname[HTTP_MAX_URI];
+  gchar *username = NULL;
+  gchar *hostname = NULL;
+  gchar *password = NULL;
+  gint   length;
+  gint   i;
+
+  length = g_strv_length (auth_info_required);
+
+  if (auth_info != NULL)
+    for (i = 0; i < length; i++)
+      {
+        if (g_strcmp0 (auth_info_required[i], "username") == 0)
+          username = g_strdup (auth_info[i]);
+        else if (g_strcmp0 (auth_info_required[i], "hostname") == 0)
+          hostname = g_strdup (auth_info[i]);
+        else if (g_strcmp0 (auth_info_required[i], "password") == 0)
+          password = g_strdup (auth_info[i]);
+      }
+
+  if (hostname != NULL && username != NULL && password != 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);
+
+
+  for (l = cups_backend->requests; l; l = l->next)
+    {
+      GtkPrintCupsDispatchWatch *dispatch = l->data;
+
+      httpGetHostname (dispatch->request->http, dispatch_hostname, sizeof (dispatch_hostname));
+      if (is_address_local (dispatch_hostname))
+        strcpy (dispatch_hostname, "localhost");
+
+      if (dispatch->request->need_auth_info)
+        {
+          if (auth_info != NULL)
+            {
+              dispatch->request->auth_info = g_new0 (gchar *, length + 1);
+              for (i = 0; i < length; i++)
+                dispatch->request->auth_info[i] = g_strdup (auth_info[i]);
+            }
+          dispatch->backend->authentication_lock = FALSE;
+          dispatch->request->need_auth_info = FALSE;
+        }
+      else if (dispatch->request->password_state == GTK_CUPS_PASSWORD_REQUESTED || auth_info == NULL)
+        {
+          overwrite_and_free (dispatch->request->password);
+          dispatch->request->password = g_strdup (password);
+          g_free (dispatch->request->username);
+          dispatch->request->username = g_strdup (username);
+          dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
+          dispatch->backend->authentication_lock = FALSE;
+        }
+    }
+}
 
 static gboolean
-cups_dispatch_watch_check (GSource *source)
+request_password (gpointer data)
+{
+  GtkPrintCupsDispatchWatch *dispatch = data;
+  const gchar               *username;
+  gchar                     *password;
+  gchar                     *prompt = NULL;
+  gchar                     *key = NULL;
+  char                       hostname[HTTP_MAX_URI];
+  gchar                    **auth_info_required;
+  gchar                    **auth_info_default;
+  gchar                    **auth_info_display;
+  gboolean                  *auth_info_visible;
+  gint                       length = 3;
+  gint                       i;
+
+  if (dispatch->backend->authentication_lock)
+    return G_SOURCE_REMOVE;
+
+  httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
+  if (is_address_local (hostname))
+    strcpy (hostname, "localhost");
+
+  if (dispatch->backend->username != NULL)
+    username = dispatch->backend->username;
+  else
+    username = cupsUser ();
+
+  auth_info_required = g_new0 (gchar*, length + 1);
+  auth_info_required[0] = g_strdup ("hostname");
+  auth_info_required[1] = g_strdup ("username");
+  auth_info_required[2] = g_strdup ("password");
+
+  auth_info_default = g_new0 (gchar*, length + 1);
+  auth_info_default[0] = g_strdup (hostname);
+  auth_info_default[1] = g_strdup (username);
+
+  auth_info_display = g_new0 (gchar*, length + 1);
+  auth_info_display[1] = g_strdup (_("Username:"));
+  auth_info_display[2] = g_strdup (_("Password:"));
+
+  auth_info_visible = g_new0 (gboolean, length + 1);
+  auth_info_visible[1] = TRUE;
+
+  key = g_strconcat (username, "@", hostname, NULL);
+  password = g_hash_table_lookup (dispatch->backend->auth, key);
+
+  if (password && dispatch->request->password_state != GTK_CUPS_PASSWORD_NOT_VALID)
+    {
+      GTK_NOTE (PRINTING,
+                g_print ("CUPS backend: using stored password for %s\n", key));
+
+      overwrite_and_free (dispatch->request->password);
+      dispatch->request->password = g_strdup (password);
+      g_free (dispatch->request->username);
+      dispatch->request->username = g_strdup (username);
+      dispatch->request->password_state = GTK_CUPS_PASSWORD_HAS;
+    }
+  else
+    {
+      const char *job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
+      const char *printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
+      char *printer_name = NULL;
+
+      if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
+        printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
+
+      if (dispatch->request->password_state == GTK_CUPS_PASSWORD_NOT_VALID)
+        g_hash_table_remove (dispatch->backend->auth, key);
+
+      dispatch->request->password_state = GTK_CUPS_PASSWORD_REQUESTED;
+
+      dispatch->backend->authentication_lock = TRUE;
+
+      switch (ippGetOperation (dispatch->request->ipp_request))
+        {
+          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);
+            else
+              prompt = g_strdup_printf ( _("Authentication is required to print a document on %s"), hostname);
+            break;
+          case IPP_GET_JOB_ATTRIBUTES:
+            if (job_title != NULL)
+              prompt = g_strdup_printf ( _("Authentication is required to get attributes of job '%s'"), job_title);
+            else
+              prompt = g_strdup ( _("Authentication is required to get attributes of a job"));
+            break;
+          case IPP_GET_PRINTER_ATTRIBUTES:
+            if (printer_name != NULL)
+              prompt = g_strdup_printf ( _("Authentication is required to get attributes of printer %s"), printer_name);
+            else
+              prompt = g_strdup ( _("Authentication is required to get attributes of a printer"));
+            break;
+          case CUPS_GET_DEFAULT:
+            prompt = g_strdup_printf ( _("Authentication is required to get default printer of %s"), hostname);
+            break;
+          case CUPS_GET_PRINTERS:
+            prompt = g_strdup_printf ( _("Authentication is required to get printers from %s"), hostname);
+            break;
+          default:
+            /* work around gcc warning about 0 not being a value for this enum */
+            if (ippGetOperation (dispatch->request->ipp_request) == 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;
+        }
+
+      g_free (printer_name);
+
+      g_signal_emit_by_name (dispatch->backend, "request-password", 
+                             auth_info_required, auth_info_default, auth_info_display, auth_info_visible, prompt);
+
+      g_free (prompt);
+    }
+
+  for (i = 0; i < length; i++)
+    {
+      g_free (auth_info_required[i]);
+      g_free (auth_info_default[i]);
+      g_free (auth_info_display[i]);
+    }
+
+  g_free (auth_info_required);
+  g_free (auth_info_default);
+  g_free (auth_info_display);
+  g_free (auth_info_visible);
+  g_free (key);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+cups_dispatch_add_poll (GSource *source)
 {
   GtkPrintCupsDispatchWatch *dispatch;
   GtkCupsPollState poll_state;
-  gboolean result;
-
-  GTK_NOTE (PRINTING,
-            g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source)); 
 
   dispatch = (GtkPrintCupsDispatchWatch *) 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);
-         g_source_add_poll (source, dispatch->data_poll);
-       }
-      else
-       {
+         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;
          else if (poll_state == GTK_CUPS_HTTP_WRITE)
            dispatch->data_poll->events = G_IO_OUT | G_IO_ERR;
          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
+          dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
+          g_source_add_poll (source, dispatch->data_poll);
+        }
     }
-    
-  if (poll_state != GTK_CUPS_HTTP_IDLE)  
+}
+
+static gboolean
+check_auth_info (gpointer user_data)
+{
+  GtkPrintCupsDispatchWatch *dispatch;
+  dispatch = (GtkPrintCupsDispatchWatch *) user_data;
+
+  if (!dispatch->request->need_auth_info)
+    {
+      if (dispatch->request->auth_info == NULL)
+        {
+          dispatch->callback (GTK_PRINT_BACKEND (dispatch->backend),
+                              gtk_cups_request_get_result (dispatch->request),
+                              dispatch->callback_data);
+          g_source_destroy ((GSource *) dispatch);
+        }
+      else
+        {
+          gint length;
+          gint i;
+
+          length = g_strv_length (dispatch->request->auth_info_required);
+
+          gtk_cups_request_ipp_add_strings (dispatch->request,
+                                            IPP_TAG_JOB,
+                                            IPP_TAG_TEXT,
+                                            "auth-info",
+                                            length,
+                                            NULL,
+                                            (const char * const *) dispatch->request->auth_info);
+
+          g_source_attach ((GSource *) dispatch, NULL);
+          g_source_unref ((GSource *) dispatch);
+
+          for (i = 0; i < length; i++)
+            overwrite_and_free (dispatch->request->auth_info[i]);
+          g_free (dispatch->request->auth_info);
+          dispatch->request->auth_info = NULL;
+        }
+
+      return G_SOURCE_REMOVE;
+    }
+
+  return G_SOURCE_CONTINUE;
+}
+
+static gboolean
+request_auth_info (gpointer user_data)
+{
+  GtkPrintCupsDispatchWatch  *dispatch;
+  const char                 *job_title;
+  const char                 *printer_uri;
+  gchar                      *prompt = NULL;
+  char                       *printer_name = NULL;
+  gint                        length;
+  gint                        i;
+  gboolean                   *auth_info_visible = NULL;
+  gchar                     **auth_info_default = NULL;
+  gchar                     **auth_info_display = NULL;
+
+  dispatch = (GtkPrintCupsDispatchWatch *) user_data;
+
+  if (dispatch->backend->authentication_lock)
+    return FALSE;
+
+  job_title = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_NAME, "job-name");
+  printer_uri = gtk_cups_request_ipp_get_string (dispatch->request, IPP_TAG_URI, "printer-uri");
+  length = g_strv_length (dispatch->request->auth_info_required);
+
+  auth_info_visible = g_new0 (gboolean, length);
+  auth_info_default = g_new0 (gchar *, length + 1);
+  auth_info_display = g_new0 (gchar *, length + 1);
+
+  for (i = 0; i < length; i++)
+    {
+      if (g_strcmp0 (dispatch->request->auth_info_required[i], "domain") == 0)
+        {
+          auth_info_display[i] = g_strdup (_("Domain:"));
+          auth_info_default[i] = g_strdup ("WORKGROUP");
+          auth_info_visible[i] = TRUE;
+        }
+      else if (g_strcmp0 (dispatch->request->auth_info_required[i], "username") == 0)
+        {
+          auth_info_display[i] = g_strdup (_("Username:"));
+          if (dispatch->backend->username != NULL)
+            auth_info_default[i] = g_strdup (dispatch->backend->username);
+          else
+            auth_info_default[i] = g_strdup (cupsUser ());
+          auth_info_visible[i] = TRUE;
+        }
+      else if (g_strcmp0 (dispatch->request->auth_info_required[i], "password") == 0)
+        {
+          auth_info_display[i] = g_strdup (_("Password:"));
+          auth_info_visible[i] = FALSE;
+        }
+    }
+
+  if (printer_uri != NULL && strrchr (printer_uri, '/') != NULL)
+    printer_name = g_strdup (strrchr (printer_uri, '/') + 1);
+
+  dispatch->backend->authentication_lock = TRUE;
+
+  if (job_title != NULL)
+    {
+      if (printer_name != NULL)
+        prompt = g_strdup_printf ( _("Authentication is required to print document '%s' on printer %s"), job_title, printer_name);
+      else
+        prompt = g_strdup_printf ( _("Authentication is required to print document '%s'"), job_title);
+    }
+  else
+    {
+      if (printer_name != NULL)
+        prompt = g_strdup_printf ( _("Authentication is required to print this document on printer %s"), printer_name);
+      else
+        prompt = g_strdup ( _("Authentication is required to print this document"));
+    }
+
+  g_signal_emit_by_name (dispatch->backend, "request-password",
+                         dispatch->request->auth_info_required,
+                         auth_info_default,
+                         auth_info_display,
+                         auth_info_visible,
+                         prompt);
+
+  for (i = 0; i < length; i++)
+    {
+      g_free (auth_info_default[i]);
+      g_free (auth_info_display[i]);
+    }
+
+  g_free (auth_info_default);
+  g_free (auth_info_display);
+  g_free (printer_name);
+  g_free (prompt);
+
+  g_idle_add (check_auth_info, user_data);
+
+  return FALSE;
+}
+
+static gboolean
+cups_dispatch_watch_check (GSource *source)
+{
+  GtkPrintCupsDispatchWatch *dispatch;
+  GtkCupsPollState poll_state;
+  gboolean result;
+
+  GTK_NOTE (PRINTING,
+            g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source)); 
+
+  dispatch = (GtkPrintCupsDispatchWatch *) source;
+
+  poll_state = gtk_cups_request_get_poll_state (dispatch->request);
+
+  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);
       g_free (dispatch->data_poll);
       dispatch->data_poll = NULL;
     }
+
+  if (dispatch->request->need_password && dispatch->request->password_state != GTK_CUPS_PASSWORD_REQUESTED)
+    {
+      dispatch->request->need_password = FALSE;
+      g_idle_add (request_password, dispatch);
+      result = FALSE;
+    }
   
   return result;
 }
@@ -680,6 +1176,7 @@ cups_dispatch_watch_prepare (GSource *source,
                             gint    *timeout_)
 {
   GtkPrintCupsDispatchWatch *dispatch;
+  gboolean result;
 
   dispatch = (GtkPrintCupsDispatchWatch *) source;
 
@@ -687,8 +1184,12 @@ cups_dispatch_watch_prepare (GSource *source,
             g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
 
   *timeout_ = -1;
-  
-  return gtk_cups_request_read_write (dispatch->request);
+
+  result = gtk_cups_request_read_write (dispatch->request, TRUE);
+
+  cups_dispatch_add_poll (source);
+
+  return result;
 }
 
 static gboolean
@@ -730,12 +1231,39 @@ static void
 cups_dispatch_watch_finalize (GSource *source)
 {
   GtkPrintCupsDispatchWatch *dispatch;
+  GtkCupsResult *result;
 
   GTK_NOTE (PRINTING,
             g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
 
   dispatch = (GtkPrintCupsDispatchWatch *) source;
 
+  result = gtk_cups_request_get_result (dispatch->request);
+  if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH)
+    {
+      const gchar *username;
+      gchar        hostname[HTTP_MAX_URI];
+      gchar       *key;
+    
+      httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
+      if (is_address_local (hostname))
+        strcpy (hostname, "localhost");
+
+      if (dispatch->backend->username != NULL)
+        username = dispatch->backend->username;
+      else
+        username = cupsUser ();
+
+      key = g_strconcat (username, "@", hostname, NULL);
+      GTK_NOTE (PRINTING,
+                g_print ("CUPS backend: removing stored password for %s\n", key));
+      g_hash_table_remove (dispatch->backend->auth, key);
+      g_free (key);
+      
+      if (dispatch->backend)
+        dispatch->backend->authentication_lock = FALSE;
+    }
+
   gtk_cups_request_free (dispatch->request);
 
   if (dispatch->backend)
@@ -749,11 +1277,20 @@ cups_dispatch_watch_finalize (GSource *source)
        * of print backends. See _gtk_print_backend_create for the
        * disabling.
        */
+
+      dispatch->backend->requests = g_list_remove (dispatch->backend->requests, dispatch);
+
+      
       g_object_unref (dispatch->backend);
       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 = {
@@ -775,18 +1312,33 @@ 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;
+
+  print_backend->requests = g_list_prepend (print_backend->requests, dispatch);
 
   g_source_set_callback ((GSource *) dispatch, (GSourceFunc) callback, user_data, notify);
 
-  g_source_attach ((GSource *) dispatch, NULL);
-  g_source_unref ((GSource *) dispatch);
+  if (request->need_auth_info)
+    {
+      dispatch->callback = callback;
+      dispatch->callback_data = user_data;
+      request_auth_info (dispatch);
+    }
+  else
+    {
+      g_source_attach ((GSource *) dispatch, NULL);
+      g_source_unref ((GSource *) dispatch);
+    }
 }
 
 #if 0
@@ -839,7 +1391,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 = "";
@@ -885,12 +1437,13 @@ cups_request_printer_info (GtkPrintBackendCups *print_backend,
       "job-sheets-default"
     };
 
-  request = gtk_cups_request_new (NULL,
-                                  GTK_CUPS_POST,
-                                  IPP_GET_PRINTER_ATTRIBUTES,
-                                 NULL,
-                                 NULL,
-                                 NULL);
+  request = gtk_cups_request_new_with_username (NULL,
+                                                GTK_CUPS_POST,
+                                                IPP_GET_PRINTER_ATTRIBUTES,
+                                                NULL,
+                                                NULL,
+                                                NULL,
+                                                print_backend->username);
 
   printer_uri = g_strdup_printf ("ipp://localhost/printers/%s",
                                   printer_name);
@@ -958,10 +1511,15 @@ cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
     }
 
   data->counter++;
-  
+
   response = gtk_cups_result_get_response (result);
 
   state = 0;
+
+#ifdef HAVE_CUPS_API_1_6
+  attr = ippFindAttribute (response, "job-state", IPP_TAG_INTEGER);
+  state = ippGetInteger (attr, 0);
+#else
   for (attr = response->attrs; attr != NULL; attr = attr->next) 
     {
       if (!attr->name)
@@ -969,7 +1527,8 @@ cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
       
       _CUPS_MAP_ATTR_INT (attr, state, "job-state");
     }
-  
+#endif
+
   done = FALSE;
   switch (state)
     {
@@ -1024,12 +1583,13 @@ cups_request_job_info (CupsJobPollData *data)
   GtkCupsRequest *request;
   gchar *job_uri;
 
-  request = gtk_cups_request_new (NULL,
-                                  GTK_CUPS_POST,
-                                  IPP_GET_JOB_ATTRIBUTES,
-                                 NULL,
-                                 NULL,
-                                 NULL);
+  request = gtk_cups_request_new_with_username (NULL,
+                                                GTK_CUPS_POST,
+                                                IPP_GET_JOB_ATTRIBUTES,
+                                                NULL,
+                                                NULL,
+                                                NULL,
+                                                data->print_backend->username);
 
   job_uri = g_strdup_printf ("ipp://localhost/jobs/%d", data->job_id);
   gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION, IPP_TAG_URI,
@@ -1053,7 +1613,7 @@ cups_job_info_poll_timeout (gpointer user_data)
   else
     cups_request_job_info (data);
   
-  return FALSE;
+  return G_SOURCE_REMOVE;
 }
 
 static void
@@ -1092,6 +1652,42 @@ find_printer (GtkPrinter  *printer,
   printer_name = gtk_printer_get_name (printer);
   return g_ascii_strcasecmp (printer_name, find_name);
 }
+/* Printer messages we're interested in */
+static const char * const printer_messages[] =
+  {
+    "toner-low",
+    "toner-empty",
+    "developer-low",
+    "developer-empty",
+    "marker-supply-low",
+    "marker-supply-empty",
+    "cover-open",
+    "door-open",
+    "media-low",
+    "media-empty",
+    "offline",
+    "other"
+  };
+/* Our translatable versions of the printer messages */
+static const char * printer_strings[] =
+  {
+    N_("Printer '%s' is low on toner."),
+    N_("Printer '%s' has no toner left."),
+    /* Translators: "Developer" like on photo development context */
+    N_("Printer '%s' is low on developer."),
+    /* Translators: "Developer" like on photo development context */
+    N_("Printer '%s' is out of developer."),
+    /* Translators: "marker" is one color bin of the printer */
+    N_("Printer '%s' is low on at least one marker supply."),
+    /* Translators: "marker" is one color bin of the printer */
+    N_("Printer '%s' is out of at least one marker supply."),
+    N_("The cover is open on printer '%s'."),
+    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 offline."),
+    N_("There is a problem on printer '%s'.")
+  };
 
 static void
 cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
@@ -1103,6 +1699,7 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
   ipp_t *response;
   gboolean list_has_changed;
   GList *removed_printer_checklist;
+  gchar *remote_default_printer = NULL;
 
   GDK_THREADS_ENTER ();
 
@@ -1116,8 +1713,20 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
   if (gtk_cups_result_is_error (result))
     {
       GTK_NOTE (PRINTING, 
-                g_warning ("CUPS Backend: Error getting printer list: %s", 
-                          gtk_cups_result_get_error_string (result)));
+                g_warning ("CUPS Backend: Error getting printer list: %s %d %d", 
+                           gtk_cups_result_get_error_string (result),
+                           gtk_cups_result_get_error_type (result),
+                           gtk_cups_result_get_error_code (result)));
+
+      if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
+          gtk_cups_result_get_error_code (result) == 1)
+        {
+          /* Canceled by user, stop popping up more password dialogs */
+          if (cups_backend->list_printers_poll > 0)
+            g_source_remove (cups_backend->list_printers_poll);
+          cups_backend->list_printers_poll = 0;
+          cups_backend->list_printers_attempts = 0;
+        }
 
       goto done;
     }
@@ -1150,89 +1759,58 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
       gint printer_state_reason_level = 0; /* 0 - none, 1 - report, 2 - warning, 3 - error */
       gboolean interested_in = FALSE;
       gboolean found = FALSE;
-      static const char * const reasons[] =    /* Reasons we're interested in */
-        {
-          "toner-low",
-          "toner-empty",
-          "developer-low",
-          "developer-empty",
-          "marker-supply-low",
-          "marker-supply-empty",
-          "cover-open",
-          "door-open",
-          "media-low",
-          "media-empty",
-          "offline",
-          "connecting-to-device",
-          "other"
-        };
-      static const char * reasons_descs[] =
-        {
-          N_("Printer '%s' is low on toner."),
-          N_("Printer '%s' has no toner left."),
-          /* Translators: "Developer" like on photo development context */
-          N_("Printer '%s' is low on developer."),
-          /* Translators: "Developer" like on photo development context */
-          N_("Printer '%s' is out of developer."),
-          /* Translators: "marker" is one color bin of the printer */
-          N_("Printer '%s' is low on at least one marker supply."),
-          /* Translators: "marker" is one color bin of the printer */
-          N_("Printer '%s' is out of at least one marker supply."),
-          N_("The cover is open on printer '%s'."),
-          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_("There is a problem on printer '%s'.")
-        };
       gboolean is_paused = FALSE;
       gboolean is_accepting_jobs = TRUE;
       gboolean default_printer = FALSE;
       gboolean got_printer_type = FALSE;
-      
+      const gchar   *default_cover_before = NULL;
+      const 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)
+      while (attr != NULL && ippGetGroupTag (attr) != IPP_TAG_PRINTER)
         attr = attr->next;
 
       if (attr == NULL)
         break;
 
-      while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
+      while (attr != NULL && ippGetGroupTag (attr) == IPP_TAG_PRINTER)
       {
-        if (strcmp (attr->name, "printer-name") == 0 &&
-           attr->value_tag == IPP_TAG_NAME)
-         printer_name = attr->values[0].string.text;
-       else if (strcmp (attr->name, "printer-uri-supported") == 0 &&
-                attr->value_tag == IPP_TAG_URI)
-         printer_uri = attr->values[0].string.text;
-       else if (strcmp (attr->name, "member-uris") == 0 &&
-                attr->value_tag == IPP_TAG_URI)
-         member_uris = attr->values[0].string.text;
-        else if (strcmp (attr->name, "printer-location") == 0)
-          location = attr->values[0].string.text;
-        else if (strcmp (attr->name, "printer-info") == 0)
-          description = attr->values[0].string.text;
-        else if (strcmp (attr->name, "printer-state-message") == 0)
-          state_msg = attr->values[0].string.text;
-        else if (strcmp (attr->name, "printer-state-reasons") == 0)
+        if (strcmp (ippGetName (attr), "printer-name") == 0 &&
+           ippGetValueTag (attr) == IPP_TAG_NAME)
+           printer_name = ippGetString (attr, 0, NULL);
+       else if (strcmp (ippGetName (attr), "printer-uri-supported") == 0 &&
+                ippGetValueTag (attr) == IPP_TAG_URI)
+           printer_uri = ippGetString (attr, 0, NULL);
+       else if (strcmp (ippGetName (attr), "member-uris") == 0 &&
+                ippGetValueTag (attr) == IPP_TAG_URI)
+           member_uris = ippGetString (attr, 0, NULL);
+        else if (strcmp (ippGetName (attr), "printer-location") == 0)
+           location = ippGetString (attr, 0, NULL);
+        else if (strcmp (ippGetName (attr), "printer-info") == 0)
+           description = ippGetString (attr, 0, NULL);
+        else if (strcmp (ippGetName (attr), "printer-state-message") == 0)
+           state_msg = ippGetString (attr, 0, NULL);
+        else if (strcmp (ippGetName (attr), "printer-state-reasons") == 0)
           /* Store most important reason to reason_msg and set
              its importance at printer_state_reason_level */
           {
-            for (i = 0; i < attr->num_values; i++)
+            for (i = 0; i < ippGetCount (attr); i++)
               {
-                if (strcmp (attr->values[i].string.text, "none") != 0)
+                 if (strcmp (ippGetString (attr, i, NULL), "none") != 0)
                   {
                     /* Sets is_paused flag for paused printer. */
-                    if (strcmp (attr->values[i].string.text, "paused") == 0)
+                     if (strcmp (ippGetString (attr, i, NULL), "paused") == 0)
                       {
                         is_paused = TRUE;
                       }
 
                     interested_in = FALSE;
                     for (j = 0; j < G_N_ELEMENTS (reasons); j++)
-                        if (strncmp (attr->values[i].string.text, reasons[j], strlen (reasons[j])) == 0)
+                        if (strncmp (ippGetString (attr, i, NULL), reasons[j], strlen (reasons[j])) == 0)
                           {
                             interested_in = TRUE;
                             break;
@@ -1240,78 +1818,91 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
 
                     if (interested_in)
                       {
-                        if (g_str_has_suffix (attr->values[i].string.text, "-report"))
+                         if (g_str_has_suffix (ippGetString (attr, i, NULL), "-report"))
                           {
                             if (printer_state_reason_level <= 1)
                               {
-                                reason_msg = attr->values[i].string.text;
+                                 reason_msg = ippGetString (attr, i, NULL);
                                 printer_state_reason_level = 1;
                               }
                           }
-                        else if (g_str_has_suffix (attr->values[i].string.text, "-warning"))
+                         else if (g_str_has_suffix (ippGetString (attr, i, NULL), "-warning"))
                           {
                             if (printer_state_reason_level <= 2)
                               {
-                                reason_msg = attr->values[i].string.text;
+                                 reason_msg = ippGetString (attr, i, NULL);
                                 printer_state_reason_level = 2;
-                              }
+                             }
                           }
                         else  /* It is error in the case of no suffix. */
                           {
-                            reason_msg = attr->values[i].string.text;
+                             reason_msg = ippGetString (attr, i, NULL);
                             printer_state_reason_level = 3;
                           }
                       }
                   }
               }
           }
-        else if (strcmp (attr->name, "printer-state") == 0)
-          state = attr->values[0].integer;
-        else if (strcmp (attr->name, "queued-job-count") == 0)
-          job_count = attr->values[0].integer;
-        else if (strcmp (attr->name, "printer-is-accepting-jobs") == 0)
+        else if (strcmp (ippGetName (attr), "printer-state") == 0)
+          state = ippGetInteger (attr, 0);
+        else if (strcmp (ippGetName (attr), "queued-job-count") == 0)
+          job_count = ippGetInteger (attr, 0);
+        else if (strcmp (ippGetName (attr), "printer-is-accepting-jobs") == 0)
           {
-            if (attr->values[0].boolean == 1)
+            if (ippGetBoolean (attr, 0) == 1)
               is_accepting_jobs = TRUE;
             else
               is_accepting_jobs = FALSE;
           }
-        else if (strcmp (attr->name, "job-sheets-supported") == 0)
+        else if (strcmp (ippGetName (attr), "job-sheets-supported") == 0)
           {
             if (cups_backend->covers == NULL)
               {
-                cups_backend->number_of_covers = attr->num_values;
+                cups_backend->number_of_covers = ippGetCount (attr);
                 cups_backend->covers = g_new (char *, cups_backend->number_of_covers + 1);
-
                 for (i = 0; i < cups_backend->number_of_covers; i++)
-                  cups_backend->covers[i] = g_strdup (attr->values[i].string.text);
-
+                   cups_backend->covers[i] = g_strdup (ippGetString (attr, i, NULL));
                 cups_backend->covers[cups_backend->number_of_covers] = NULL;
               }
           }
-        else if (strcmp (attr->name, "job-sheets-default") == 0)
+        else if (strcmp (ippGetName (attr), "job-sheets-default") == 0)
           {
-            if ( (cups_backend->default_cover_before == NULL) && (cups_backend->default_cover_after == NULL))
+            if (ippGetCount (attr) == 2)
               {
-                if (attr->num_values == 2)
-                  {
-                    cups_backend->default_cover_before = g_strdup (attr->values[0].string.text);
-                    cups_backend->default_cover_after = g_strdup (attr->values[1].string.text);
-                  }
+                 default_cover_before = ippGetString (attr, 0, NULL);
+                 default_cover_after = ippGetString (attr, 1, NULL);
               }
           }
-        else if (strcmp (attr->name, "printer-type") == 0)
+        else if (strcmp (ippGetName (attr), "printer-type") == 0)
           {
             got_printer_type = TRUE;
-            if (attr->values[0].integer & 0x00020000)
+            if (ippGetInteger (attr, 0) & 0x00020000)
               default_printer = TRUE;
             else
               default_printer = FALSE;
+
+            if (ippGetInteger (attr, 0) & 0x00000002)
+              remote_printer = TRUE;
+            else
+              remote_printer = FALSE;
+          }
+        else if (strcmp (ippGetName (attr), "auth-info-required") == 0)
+          {
+             if (strcmp (ippGetString (attr, 0, NULL), "none") != 0)
+              {
+                auth_info_required = g_new0 (gchar *, ippGetCount (attr) + 1);
+                for (i = 0; i < ippGetCount (attr); i++)
+                   auth_info_required[i] = g_strdup (ippGetString (attr, i, NULL));
+              }
+          }
+        else if (strcmp (attr->name, "number-up-default") == 0)
+          {
+            default_number_up = attr->values[0].integer;
           }
         else
          {
            GTK_NOTE (PRINTING,
-                      g_print ("CUPS Backend: Attribute %s ignored", attr->name));
+                      g_print ("CUPS Backend: Attribute %s ignored", ippGetName (attr)));
          }
 
         attr = attr->next;
@@ -1330,8 +1921,16 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
         {
           if (default_printer && !cups_backend->got_default_printer)
             {
-              cups_backend->got_default_printer = TRUE;
-              cups_backend->default_printer = g_strdup (printer_name);
+              if (!remote_printer)
+                {
+                  cups_backend->got_default_printer = TRUE;
+                  cups_backend->default_printer = g_strdup (printer_name);
+                }
+              else
+                {
+                  if (remote_default_printer == NULL)
+                    remote_default_printer = g_strdup (printer_name);
+                }
             }
         }
       else
@@ -1357,7 +1956,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);
 
@@ -1415,9 +2020,17 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
 
          g_free (cups_server);
 
+          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;
          
+          cups_printer->auth_info_required = g_strdupv (auth_info_required);
+          g_strfreev (auth_info_required);
+
          printer = GTK_PRINTER (cups_printer);
          
          if (cups_backend->default_printer != NULL &&
@@ -1430,6 +2043,8 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
       else
        g_object_ref (printer);
 
+      GTK_PRINTER_CUPS (printer)->remote = remote_printer;
+
       gtk_printer_set_is_paused (printer, is_paused);
       gtk_printer_set_is_accepting_jobs (printer, is_accepting_jobs);
 
@@ -1518,13 +2133,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),
@@ -1541,8 +2156,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;
     }
   
@@ -1552,6 +2166,26 @@ done:
   
   gtk_print_backend_set_list_done (backend);
 
+  if (!cups_backend->got_default_printer && remote_default_printer != NULL)
+    {
+      cups_backend->default_printer = g_strdup (remote_default_printer);
+      cups_backend->got_default_printer = TRUE;
+      g_free (remote_default_printer);
+
+      if (cups_backend->default_printer != NULL)
+        {
+          GtkPrinter *default_printer = NULL;
+          default_printer = gtk_print_backend_find_printer (GTK_PRINT_BACKEND (cups_backend),
+                                                            cups_backend->default_printer);
+          if (default_printer != NULL)
+            {
+              gtk_printer_set_is_default (default_printer, TRUE);
+              g_signal_emit_by_name (GTK_PRINT_BACKEND (cups_backend),
+                                     "printer-status-changed", default_printer);
+            }
+        }
+    }
+
   GDK_THREADS_LEAVE ();
 }
 
@@ -1590,26 +2224,44 @@ cups_request_printer_list (GtkPrintBackendCups *cups_backend)
       "printer-is-accepting-jobs",
       "job-sheets-supported",
       "job-sheets-default",
-      "printer-type"
+      "printer-type",
+      "auth-info-required",
+      "number-up-default"
     };
 
-  if (cups_backend->list_printers_pending)
+  if (cups_backend->reading_ppds > 0 || cups_backend->list_printers_pending)
     return TRUE;
 
   state = gtk_cups_connection_test_get_state (cups_backend->cups_connection_test);
   update_backend_status (cups_backend, state);
 
+  if (cups_backend->list_printers_attempts == 60)
+    {
+      cups_backend->list_printers_attempts = -1;
+      if (cups_backend->list_printers_poll > 0)
+        g_source_remove (cups_backend->list_printers_poll);
+      cups_backend->list_printers_poll = gdk_threads_add_timeout (200,
+                                           (GSourceFunc) cups_request_printer_list,
+                                           cups_backend);
+    }
+  else if (cups_backend->list_printers_attempts != -1)
+    cups_backend->list_printers_attempts++;
+
   if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
     return TRUE;
+  else
+    if (cups_backend->list_printers_attempts > 0)
+      cups_backend->list_printers_attempts = 60;
 
   cups_backend->list_printers_pending = TRUE;
 
-  request = gtk_cups_request_new (NULL,
-                                  GTK_CUPS_POST,
-                                  CUPS_GET_PRINTERS,
-                                 NULL,
-                                 NULL,
-                                 NULL);
+  request = gtk_cups_request_new_with_username (NULL,
+                                                GTK_CUPS_POST,
+                                                CUPS_GET_PRINTERS,
+                                                NULL,
+                                                NULL,
+                                                NULL,
+                                                cups_backend->username);
 
   gtk_cups_request_ipp_add_strings (request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
                                    "requested-attributes", G_N_ELEMENTS (pattrs),
@@ -1637,9 +2289,9 @@ cups_get_printer_list (GtkPrintBackend *backend)
   if (cups_backend->list_printers_poll == 0)
     {
       if (cups_request_printer_list (cups_backend))
-        cups_backend->list_printers_poll = gdk_threads_add_timeout_seconds (3,
-                                                        (GSourceFunc) cups_request_printer_list,
-                                                        backend);
+        cups_backend->list_printers_poll = gdk_threads_add_timeout (50,
+                                             (GSourceFunc) cups_request_printer_list,
+                                             backend);
     }
 }
 
@@ -1665,7 +2317,6 @@ cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
                      GtkCupsResult       *result,
                      GetPPDData          *data)
 {
-  ipp_t *response;
   GtkPrinter *printer;
 
   GDK_THREADS_ENTER ();
@@ -1675,6 +2326,7 @@ cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
 
   printer = GTK_PRINTER (data->printer);
   GTK_PRINTER_CUPS (printer)->reading_ppd = FALSE;
+  print_backend->reading_ppds--;
 
   if (gtk_cups_result_is_error (result))
     {
@@ -1693,11 +2345,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);
   
@@ -1708,7 +2361,7 @@ done:
   GDK_THREADS_LEAVE ();
 }
 
-static void
+static gboolean
 cups_request_ppd (GtkPrinter *printer)
 {
   GError *error;
@@ -1728,6 +2381,41 @@ cups_request_ppd (GtkPrinter *printer)
   GTK_NOTE (PRINTING,
             g_print ("CUPS Backend: %s\n", G_STRFUNC));
 
+  if (cups_printer->remote)
+    {
+      GtkCupsConnectionState state;
+
+      state = gtk_cups_connection_test_get_state (cups_printer->remote_cups_connection_test);
+
+      if (state == GTK_CUPS_CONNECTION_IN_PROGRESS)
+        {
+          if (cups_printer->get_remote_ppd_attempts == 60)
+            {
+              cups_printer->get_remote_ppd_attempts = -1;
+              if (cups_printer->get_remote_ppd_poll > 0)
+                g_source_remove (cups_printer->get_remote_ppd_poll);
+              cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (200,
+                                                    (GSourceFunc) cups_request_ppd,
+                                                    printer);
+            }
+          else if (cups_printer->get_remote_ppd_attempts != -1)
+            cups_printer->get_remote_ppd_attempts++;
+
+          return TRUE;
+        }
+
+      gtk_cups_connection_test_free (cups_printer->remote_cups_connection_test);
+      cups_printer->remote_cups_connection_test = NULL;
+      cups_printer->get_remote_ppd_poll = 0;
+      cups_printer->get_remote_ppd_attempts = 0;
+
+      if (state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
+        {
+          g_signal_emit_by_name (printer, "details-acquired", FALSE);
+          return FALSE;
+        }
+    }
+
   http = httpConnectEncrypt (cups_printer->hostname, 
                             cups_printer->port,
                             cupsEncryption ());
@@ -1740,7 +2428,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);
@@ -1757,7 +2445,7 @@ cups_request_ppd (GtkPrinter *printer)
       g_free (data);
 
       g_signal_emit_by_name (printer, "details-acquired", FALSE);
-      return;
+      return FALSE;
     }
     
   data->http = http;
@@ -1771,28 +2459,33 @@ cups_request_ppd (GtkPrinter *printer)
   resource = g_strdup_printf ("/printers/%s.ppd", 
                               gtk_printer_cups_get_ppd_name (GTK_PRINTER_CUPS (printer)));
 
-  request = gtk_cups_request_new (data->http,
-                                  GTK_CUPS_GET,
-                                 0,
-                                  data->ppd_io,
-                                 cups_printer->hostname,
-                                 resource);
+  print_backend = gtk_printer_get_backend (printer);
+
+  request = gtk_cups_request_new_with_username (data->http,
+                                                GTK_CUPS_GET,
+                                                0,
+                                                data->ppd_io,
+                                                cups_printer->hostname,
+                                                resource,
+                                                GTK_PRINT_BACKEND_CUPS (print_backend)->username);
 
   GTK_NOTE (PRINTING,
             g_print ("CUPS Backend: Requesting resource %s to be written to temp file %s\n", resource, ppd_filename));
 
-  g_free (resource);
-  g_free (ppd_filename);
 
   cups_printer->reading_ppd = TRUE;
-
-  print_backend = gtk_printer_get_backend (printer);
+  GTK_PRINT_BACKEND_CUPS (print_backend)->reading_ppds++;
 
   cups_request_execute (GTK_PRINT_BACKEND_CUPS (print_backend),
                         request,
                         (GtkPrintCupsResponseCallbackFunc) cups_request_ppd_cb,
                         data,
                         (GDestroyNotify)get_ppd_data_free);
+
+  g_free (resource);
+  g_free (ppd_filename);
+
+  return FALSE;
 }
 
 /* Ordering matters for default preference */
@@ -1965,9 +2658,9 @@ cups_get_default_printer (GtkPrintBackendCups *backend)
   if (cups_backend->default_printer_poll == 0)
     {
       if (cups_request_default_printer (cups_backend))
-        cups_backend->default_printer_poll = gdk_threads_add_timeout (500,
-                                                                      (GSourceFunc) cups_request_default_printer,
-                                                                      backend);
+        cups_backend->default_printer_poll = gdk_threads_add_timeout (200,
+                                               (GSourceFunc) cups_request_default_printer,
+                                               backend);
     }
 }
 
@@ -2013,10 +2706,24 @@ cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
 
   GDK_THREADS_ENTER ();
 
+  if (gtk_cups_result_is_error (result))
+    {
+      if (gtk_cups_result_get_error_type (result) == GTK_CUPS_ERROR_AUTH &&
+          gtk_cups_result_get_error_code (result) == 1)
+        {
+          /* Canceled by user, stop popping up more password dialogs */
+          if (print_backend->list_printers_poll > 0)
+            g_source_remove (print_backend->list_printers_poll);
+          print_backend->list_printers_poll = 0;
+        }
+
+      return;
+    }
+
   response = gtk_cups_result_get_response (result);
   
   if ((attr = ippFindAttribute (response, "printer-name", IPP_TAG_NAME)) != NULL)
-    print_backend->default_printer = g_strdup (attr->values[0].string.text);
+      print_backend->default_printer = g_strdup (ippGetString (attr, 0, NULL));
 
   print_backend->got_default_printer = TRUE;
 
@@ -2051,12 +2758,13 @@ cups_request_default_printer (GtkPrintBackendCups *print_backend)
   if (state == GTK_CUPS_CONNECTION_IN_PROGRESS || state == GTK_CUPS_CONNECTION_NOT_AVAILABLE)
     return TRUE;
 
-  request = gtk_cups_request_new (NULL,
-                                  GTK_CUPS_POST,
-                                  CUPS_GET_DEFAULT,
-                                 NULL,
-                                 NULL,
-                                 NULL);
+  request = gtk_cups_request_new_with_username (NULL,
+                                                GTK_CUPS_POST,
+                                                CUPS_GET_DEFAULT,
+                                                NULL,
+                                                NULL,
+                                                NULL,
+                                                print_backend->username);
   
   cups_request_execute (print_backend,
                         request,
@@ -2075,7 +2783,22 @@ cups_printer_request_details (GtkPrinter *printer)
   cups_printer = GTK_PRINTER_CUPS (printer);
   if (!cups_printer->reading_ppd && 
       gtk_printer_cups_get_ppd (cups_printer) == NULL)
-    cups_request_ppd (printer); 
+    {
+      if (cups_printer->remote)
+        {
+          if (cups_printer->get_remote_ppd_poll == 0)
+            {
+              cups_printer->remote_cups_connection_test = gtk_cups_connection_test_new (cups_printer->hostname);
+
+              if (cups_request_ppd (printer))
+                cups_printer->get_remote_ppd_poll = gdk_threads_add_timeout (50,
+                                                    (GSourceFunc) cups_request_ppd,
+                                                    printer);
+            }
+        }
+      else
+        cups_request_ppd (printer);
+    }
 }
 
 static char *
@@ -2141,6 +2864,8 @@ static const struct {
   { "MediaType", N_("Paper Type") },
   { "InputSlot", N_("Paper Source") },
   { "OutputBin", N_("Output Tray") },
+  { "Resolution", N_("Resolution") },
+  { "PreFilter", N_("GhostScript pre-filtering") },
 };
 
 
@@ -2150,6 +2875,10 @@ static const struct {
   const char *translation;
 } cups_choice_translations[] = {
   { "Duplex", "None", N_("One Sided") },
+  /* Translators: this is an option of "Two Sided" */
+  { "Duplex", "DuplexNoTumble", N_("Long Edge (Standard)") },
+  /* Translators: this is an option of "Two Sided" */
+  { "Duplex", "DuplexTumble", N_("Short Edge (Flip)") },
   /* Translators: this is an option of "Paper Source" */
   { "InputSlot", "Auto", N_("Auto Select") },
   /* Translators: this is an option of "Paper Source" */
@@ -2162,18 +2891,47 @@ static const struct {
   { "InputSlot", "PrinterDefault", N_("Printer Default") },
   /* Translators: this is an option of "Paper Source" */
   { "InputSlot", "Unspecified", N_("Auto Select") },
+  /* Translators: this is an option of "Resolution" */
+  { "Resolution", "default", N_("Printer Default") },
+  /* Translators: this is an option of "GhostScript" */
+  { "PreFilter", "EmbedFonts", N_("Embed GhostScript fonts only") },
+  /* Translators: this is an option of "GhostScript" */
+  { "PreFilter", "Level1", N_("Convert to PS level 1") },
+  /* Translators: this is an option of "GhostScript" */
+  { "PreFilter", "Level2", N_("Convert to PS level 2") },
+  /* Translators: this is an option of "GhostScript" */
+  { "PreFilter", "No", N_("No pre-filtering") },
+};
+
+static const struct {
+  const char *name;
+  const char *translation;
+} cups_group_translations[] = {
+/* Translators: "Miscellaneous" is the label for a button, that opens
+   up an extra panel of settings in a print dialog. */
+  { "Miscellaneous", N_("Miscellaneous") },
 };
 
 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",
@@ -2545,9 +3303,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);
   
@@ -2563,7 +3319,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)
@@ -2616,7 +3371,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,
@@ -2638,7 +3392,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
@@ -2689,17 +3454,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)
@@ -2729,11 +3510,12 @@ handle_option (GtkPrinterOptionSet *set,
 {
   GtkPrinterOption *option;
   char *name;
+  int i;
 
   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)
@@ -2777,7 +3559,17 @@ handle_option (GtkPrinterOptionSet *set,
        }
       else
        {
-         option->group = g_strdup (toplevel_group->text);
+         for (i = 0; i < G_N_ELEMENTS (cups_group_translations); i++)
+           {
+             if (strcmp (cups_group_translations[i].name, toplevel_group->name) == 0)
+               {
+                 option->group = g_strdup (_(cups_group_translations[i].translation));
+                 break;
+               }
+           }
+
+         if (i == G_N_ELEMENTS (cups_group_translations))
+           option->group = g_strdup (toplevel_group->text);
        }
 
       set_option_from_settings (option, settings);
@@ -2811,6 +3603,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,
@@ -2841,46 +3650,16 @@ cups_printer_get_options (GtkPrinter           *printer,
   cups_option_t *opts = NULL;
   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]);
   
@@ -2905,8 +3684,9 @@ cups_printer_get_options (GtkPrinter           *printer,
   g_object_unref (option);
 
   backend = GTK_PRINT_BACKEND_CUPS (gtk_printer_get_backend (printer));
+  cups_printer = GTK_PRINTER_CUPS (printer);
 
-  if (backend != NULL)
+  if (backend != NULL && printer != NULL)
     {
       char *cover_default[] = {"none", "classified", "confidential", "secret", "standard", "topsecret", "unclassified" };
       /* Translators, these strings are names for various 'standard' cover 
@@ -2920,6 +3700,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;
@@ -2951,8 +3767,8 @@ cups_printer_get_options (GtkPrinter           *printer,
       gtk_printer_option_choices_from_array (option, num_of_covers,
                                         cover, cover_display_translated);
 
-      if (backend->default_cover_before != NULL)
-        gtk_printer_option_set (option, backend->default_cover_before);
+      if (cups_printer->default_cover_before != NULL)
+        gtk_printer_option_set (option, cups_printer->default_cover_before);
       else
         gtk_printer_option_set (option, "none");
       set_option_from_settings (option, settings);
@@ -2965,8 +3781,8 @@ cups_printer_get_options (GtkPrinter           *printer,
       option = gtk_printer_option_new ("gtk-cover-after", _("After"), GTK_PRINTER_OPTION_TYPE_PICKONE);
       gtk_printer_option_choices_from_array (option, num_of_covers,
                                         cover, cover_display_translated);
-      if (backend->default_cover_after != NULL)
-        gtk_printer_option_set (option, backend->default_cover_after);
+      if (cups_printer->default_cover_after != NULL)
+        gtk_printer_option_set (option, cups_printer->default_cover_after);
       else
         gtk_printer_option_set (option, "none");
       set_option_from_settings (option, settings);
@@ -3045,15 +3861,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;
 }
 
@@ -3064,7 +3964,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);
 
@@ -3099,7 +3999,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)
@@ -3337,23 +4237,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),
@@ -3440,12 +4346,12 @@ foreach_option_get_settings (GtkPrinterOption *option,
 
       if (sscanf (value, "%dx%ddpi", &res_x, &res_y) == 2)
         {
-          if (res_x != 0 && res_y != 0)
+          if (res_x > 0 && res_y > 0)
             gtk_print_settings_set_resolution_xy (settings, res_x, res_y);
         }
       else if (sscanf (value, "%ddpi", &res) == 1)
         {
-          if (res != 0)
+          if (res > 0)
             gtk_print_settings_set_resolution (settings, res);
         }
 
@@ -3476,6 +4382,87 @@ foreach_option_get_settings (GtkPrinterOption *option,
     gtk_print_settings_set (settings, option->name, value);
 }
 
+static gboolean
+supports_am_pm (void)
+{
+  struct tm tmp_tm = { 0 };
+  char   time[8];
+  int    length;
+
+  length = strftime (time, sizeof (time), "%p", &tmp_tm);
+
+  return length != 0;
+}
+
+/* Converts local time to UTC time. Local time has to be in one of these
+ * formats:  HH:MM:SS, HH:MM, HH:MM:SS {am, pm}, HH:MM {am, pm}, HH {am, pm},
+ * {am, pm} HH:MM:SS, {am, pm} HH:MM, {am, pm} HH.
+ * Returns a newly allocated string holding UTC time in HH:MM:SS format
+ * or NULL.
+ */
+gchar *
+localtime_to_utctime (const char *local_time)
+{
+  const char *formats_0[] = {" %I : %M : %S %p ", " %p %I : %M : %S ",
+                             " %H : %M : %S ",
+                             " %I : %M %p ", " %p %I : %M ",
+                             " %H : %M ",
+                             " %I %p ", " %p %I "};
+  const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
+  const char *end = NULL;
+  struct tm  *actual_local_time;
+  struct tm  *actual_utc_time;
+  struct tm   local_print_time;
+  struct tm   utc_print_time;
+  struct tm   diff_time;
+  gchar      *utc_time = NULL;
+  int         i, n;
+
+  if (local_time == NULL || local_time[0] == '\0')
+    return NULL;
+
+  n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
+
+  for (i = 0; i < n; i++)
+    {
+      local_print_time.tm_hour = 0;
+      local_print_time.tm_min  = 0;
+      local_print_time.tm_sec  = 0;
+
+      if (supports_am_pm ())
+        end = strptime (local_time, formats_0[i], &local_print_time);
+      else
+        end = strptime (local_time, formats_1[i], &local_print_time);
+
+      if (end != NULL && end[0] == '\0')
+        break;
+    }
+
+  if (end != NULL && end[0] == '\0')
+    {
+      time_t rawtime;
+      time (&rawtime);
+
+      actual_utc_time = g_memdup (gmtime (&rawtime), sizeof (struct tm));
+      actual_local_time = g_memdup (localtime (&rawtime), sizeof (struct tm));
+
+      diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
+      diff_time.tm_min  = actual_utc_time->tm_min  - actual_local_time->tm_min;
+      diff_time.tm_sec  = actual_utc_time->tm_sec  - actual_local_time->tm_sec;
+
+      utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
+      utc_print_time.tm_min  = ((local_print_time.tm_min  + diff_time.tm_min)  + 60) % 60;
+      utc_print_time.tm_sec  = ((local_print_time.tm_sec  + diff_time.tm_sec)  + 60) % 60;
+
+      utc_time = g_strdup_printf ("%02d:%02d:%02d",
+                                  utc_print_time.tm_hour,
+                                  utc_print_time.tm_min,
+                                  utc_print_time.tm_sec);
+    }
+
+  return utc_time;
+}
+
 static void
 cups_printer_get_settings_from_options (GtkPrinter          *printer,
                                        GtkPrinterOptionSet *options,
@@ -3506,8 +4493,21 @@ cups_printer_get_settings_from_options (GtkPrinter          *printer,
 
       print_at = gtk_print_settings_get (settings, "print-at");
       print_at_time = gtk_print_settings_get (settings, "print-at-time");
+
       if (strcmp (print_at, "at") == 0)
-       gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
+        {
+          gchar *utc_time = NULL;
+          
+          utc_time = localtime_to_utctime (print_at_time);
+
+          if (utc_time != NULL)
+            {
+              gtk_print_settings_set (settings, "cups-job-hold-until", utc_time);
+              g_free (utc_time);
+            }
+          else
+            gtk_print_settings_set (settings, "cups-job-hold-until", print_at_time);
+        }
       else if (strcmp (print_at, "on-hold") == 0)
        gtk_print_settings_set (settings, "cups-job-hold-until", "indefinite");
     }
@@ -3519,44 +4519,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);
@@ -3575,7 +4580,40 @@ cups_printer_prepare_for_print (GtkPrinter       *printer,
       g_free (custom_name);
     }
 
-  print_job->rotate_to_orientation = TRUE;
+  if (gtk_print_settings_get_number_up (settings) > 1)
+    {
+      GtkNumberUpLayout  layout = gtk_print_settings_get_number_up_layout (settings);
+      GEnumClass        *enum_class;
+      GEnumValue        *enum_value;
+
+      switch (gtk_page_setup_get_orientation (page_setup))
+        {
+          case GTK_PAGE_ORIENTATION_PORTRAIT:
+            break;
+          case GTK_PAGE_ORIENTATION_LANDSCAPE:
+            if (layout < 4)
+              layout = layout + 2 + 4 * (1 - layout / 2);
+            else
+              layout = layout - 3 - 2 * (layout % 2);
+            break;
+          case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
+            layout = (layout + 3 - 2 * (layout % 2)) % 4 + 4 * (layout / 4);
+            break;
+          case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
+            if (layout < 4)
+              layout = layout + 5 - 2 * (layout % 2);
+            else
+              layout = layout - 6 + 4 * (1 - (layout - 4) / 2);
+            break;
+        }
+
+      enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
+      enum_value = g_enum_get_value (enum_class, layout);
+      gtk_print_settings_set (settings, "cups-number-up-layout", enum_value->value_nick);
+      g_type_class_unref (enum_class);
+    }
+
+  gtk_print_job_set_rotate (print_job, TRUE);
 }
 
 static GtkPageSetup *
@@ -3658,12 +4696,17 @@ 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);
 }
 
-static void
+static gboolean
 cups_printer_get_hard_margins (GtkPrinter *printer,
                               gdouble    *top,
                               gdouble    *bottom,
@@ -3674,12 +4717,14 @@ cups_printer_get_hard_margins (GtkPrinter *printer,
 
   ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
   if (ppd_file == NULL)
-    return;
+    return FALSE;
 
   *left = ppd_file->custom_margins[0];
   *bottom = ppd_file->custom_margins[1];
   *right = ppd_file->custom_margins[2];
   *top = ppd_file->custom_margins[3];
+
+  return TRUE;
 }
 
 static GtkPrintCapabilities