]> 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 e4cde709dfb677a97f45b6888e36fd38dcbbdbce..f26214af6a17c5cc6abf5dc2cbf62140e47b85fc 100644 (file)
@@ -14,9 +14,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifdef __linux__
 #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>
@@ -241,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
  */
@@ -469,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);
@@ -491,7 +511,7 @@ cups_print_cb (GtkPrintBackendCups *print_backend,
 
 typedef struct {
   GtkCupsRequest *request;
-  GtkPrinterOptionSet *options;
+  GtkPrinterCups *printer;
 } CupsOptionsData;
 
 static void
@@ -501,9 +521,13 @@ add_cups_options (const gchar *key,
 {
   CupsOptionsData *data = (CupsOptionsData *) user_data;
   GtkCupsRequest *request = data->request;
-  GtkPrinterOptionSet *options = data->options;
-  GtkPrinterOption *option = NULL;
+  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;
@@ -511,17 +535,37 @@ add_cups_options (const gchar *key,
   if (strcmp (value, "gtk-ignore-value") == 0)
     return;
 
-  option = gtk_printer_option_set_lookup (options, key);
-  
   key = key + strlen ("cups-");
 
-  /* Add "Custom." prefix to custom values */
-  if (value && option &&
-      !gtk_printer_option_has_choice (option, value))
-    new_value = g_strdup_printf ("Custom.%s", value);
+  if (printer && printer->ppd_file)
+    {
+      ppd_coption_t *coption;
+      gboolean       found = FALSE;
+      gboolean       custom_values_enabled = FALSE;
 
-  if (new_value)
+      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);
     }
@@ -542,9 +586,6 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
   CupsOptionsData *options_data;
   GtkCupsRequest *request;
   GtkPrintSettings *settings;
-  GtkPrinterOptionSet *options;
-  GtkPrintCapabilities capabilities;
-  GtkPageSetup *page_setup;
   const gchar *title;
   char  printer_absolute_uri[HTTP_MAX_URI];
 
@@ -553,8 +594,6 @@ 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);
-  capabilities = cups_printer_get_capabilities (GTK_PRINTER (cups_printer));
-  page_setup = gtk_printer_get_default_page_size (GTK_PRINTER (cups_printer));
 
   request = gtk_cups_request_new_with_username (NULL,
                                                 GTK_CUPS_POST,
@@ -592,16 +631,10 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
                                      IPP_TAG_NAME, "job-name", 
                                      NULL, title);
 
-  options = cups_printer_get_options (GTK_PRINTER (cups_printer), settings, page_setup, capabilities);
-
   options_data = g_new0 (CupsOptionsData, 1);
   options_data->request = request;
-  options_data->options = options;
-
+  options_data->printer = cups_printer;
   gtk_print_settings_foreach (settings, add_cups_options, options_data);
-
-  g_object_unref (page_setup);
-  g_object_unref (options);
   g_free (options_data);
 
   ps = g_new0 (CupsPrintStreamData, 1);
@@ -722,40 +755,6 @@ is_address_local (const gchar *address)
     return FALSE;
 }
 
-#ifndef HAVE_CUPS_API_1_2
-/* Included from CUPS library because of backward compatibility */
-const char *
-httpGetHostname(http_t *http,
-                char   *s,
-                int    slen)
-{
-  struct hostent *host;
-
-  if (!s || slen <= 1)
-    return (NULL);
-
-  if (http)
-    {
-      if (http->hostname[0] == '/')
-        g_strlcpy (s, "localhost", slen);
-      else
-        g_strlcpy (s, http->hostname, slen);
-    }
-  else
-    {
-      if (gethostname (s, slen) < 0)
-        g_strlcpy (s, "localhost", slen);
-
-      if (!strchr (s, '.'))
-        {
-          if ((host = gethostbyname (s)) != NULL && host->h_name)
-            g_strlcpy (s, host->h_name, slen);
-        }
-    }
-  return (s);
-}
-#endif
-
 static void
 gtk_print_backend_cups_set_password (GtkPrintBackend  *backend,
                                      gchar           **auth_info_required,
@@ -843,7 +842,7 @@ request_password (gpointer data)
   gint                       i;
 
   if (dispatch->backend->authentication_lock)
-    return FALSE;
+    return G_SOURCE_REMOVE;
 
   httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
   if (is_address_local (hostname))
@@ -900,7 +899,7 @@ request_password (gpointer data)
 
       dispatch->backend->authentication_lock = TRUE;
 
-      switch (dispatch->request->ipp_request->request.op.operation_id)
+      switch (ippGetOperation (dispatch->request->ipp_request))
         {
           case IPP_PRINT_JOB:
             if (job_title != NULL && printer_name != NULL)
@@ -928,7 +927,7 @@ request_password (gpointer data)
             break;
           default:
             /* work around gcc warning about 0 not being a value for this enum */
-            if (dispatch->request->ipp_request->request.op.operation_id == 0)
+            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);
@@ -956,7 +955,7 @@ request_password (gpointer data)
   g_free (auth_info_visible);
   g_free (key);
 
-  return FALSE;
+  return G_SOURCE_REMOVE;
 }
 
 static void
@@ -991,11 +990,7 @@ cups_dispatch_add_poll (GSource *source)
          else
            dispatch->data_poll->events = 0;
 
-#ifdef HAVE_CUPS_API_1_2
           dispatch->data_poll->fd = httpGetFd (dispatch->request->http);
-#else
-          dispatch->data_poll->fd = dispatch->request->http->fd;
-#endif
           g_source_add_poll (source, dispatch->data_poll);
         }
     }
@@ -1040,10 +1035,10 @@ check_auth_info (gpointer user_data)
           dispatch->request->auth_info = NULL;
         }
 
-      return FALSE;
+      return G_SOURCE_REMOVE;
     }
 
-  return TRUE;
+  return G_SOURCE_CONTINUE;
 }
 
 static gboolean
@@ -1516,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)
@@ -1527,7 +1527,8 @@ cups_request_job_info_cb (GtkPrintBackendCups *print_backend,
       
       _CUPS_MAP_ATTR_INT (attr, state, "job-state");
     }
-  
+#endif
+
   done = FALSE;
   switch (state)
     {
@@ -1612,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
@@ -1651,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,
@@ -1722,91 +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",
-          "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 offline."),
-          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;
-      gchar   *default_cover_before = NULL;
-      gchar   *default_cover_after = NULL;
+      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;
@@ -1814,87 +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 (attr->num_values == 2)
+            if (ippGetCount (attr) == 2)
               {
-                default_cover_before = attr->values[0].string.text;
-                default_cover_after = 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 (attr->values[0].integer & 0x00000002)
+            if (ippGetInteger (attr, 0) & 0x00000002)
               remote_printer = TRUE;
             else
               remote_printer = FALSE;
           }
-        else if (strcmp (attr->name, "auth-info-required") == 0)
+        else if (strcmp (ippGetName (attr), "auth-info-required") == 0)
           {
-            if (strcmp (attr->values[0].string.text, "none") != 0)
+             if (strcmp (ippGetString (attr, 0, NULL), "none") != 0)
               {
-                auth_info_required = g_new0 (gchar *, attr->num_values + 1);
-                for (i = 0; i < attr->num_values; i++)
-                  auth_info_required[i] = g_strdup (attr->values[i].string.text);
+                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;
@@ -2015,6 +2023,8 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
           cups_printer->default_cover_before = g_strdup (default_cover_before);
           cups_printer->default_cover_after = g_strdup (default_cover_after);
 
+          cups_printer->default_number_up = default_number_up;
+
          cups_printer->hostname = g_strdup (hostname);
          cups_printer->port = port;
          
@@ -2215,7 +2225,8 @@ cups_request_printer_list (GtkPrintBackendCups *cups_backend)
       "job-sheets-supported",
       "job-sheets-default",
       "printer-type",
-      "auth-info-required"
+      "auth-info-required",
+      "number-up-default"
     };
 
   if (cups_backend->reading_ppds > 0 || cups_backend->list_printers_pending)
@@ -2337,6 +2348,9 @@ cups_request_ppd_cb (GtkPrintBackendCups *print_backend,
   /* 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);
   
@@ -2709,7 +2723,7 @@ cups_request_default_printer_cb (GtkPrintBackendCups *print_backend,
   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;
 
@@ -3289,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);
   
@@ -3307,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)
@@ -3360,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,
@@ -3382,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
@@ -3633,45 +3654,12 @@ cups_printer_get_options (GtkPrinter           *printer,
 #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]);
   
@@ -3712,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;
@@ -4672,7 +4696,12 @@ cups_printer_get_default_page_size (GtkPrinter *printer)
     return NULL;
 
   option = ppdFindOption (ppd_file, "PageSize");
+  if (option == NULL)
+    return NULL;
+
   size = ppdPageSize (ppd_file, option->defchoice); 
+  if (size == NULL)
+    return NULL;
 
   return create_page_setup (ppd_file, size);
 }