]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/dest-options.c
Merge pull request #5677 from reddevillg/patch-2
[thirdparty/cups.git] / cups / dest-options.c
index 8477ab30ab8849d1d7f597979629c2a7d1baf4c4..8c5fe666c457f13353141cc4b3ba5d7de1b1162b 100644 (file)
@@ -1,42 +1,10 @@
 /*
- * "$Id$"
+ * Destination option/media support for CUPS.
  *
- *   Destination option/media support for CUPS.
+ * Copyright © 2012-2019 by Apple Inc.
  *
- *   Copyright 2012 by Apple Inc.
- *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   cupsCheckDestSupported()  - Check that the option and value are supported
- *                              by the destination.
- *   cupsCopyDestConflicts()   - Get conflicts and resolutions for a new
- *                              option/value pair.
- *   cupsCopyDestInfo()        - Get the supported values/capabilities for the
- *                              destination.
- *   cupsFreeDestInfo()        - Free destination information obtained using
- *                              @link cupsCopyDestInfo@.
- *   cupsGetDestMediaByName()  - Get media names, dimensions, and margins.
- *   cupsGetDestMediaBySize()  - Get media names, dimensions, and margins.
- *   cups_add_dconstres()      - Add a constraint or resolver to an array.
- *   cups_compare_dconstres()  - Compare to resolver entries.
- *   cups_compare_media_db()   - Compare two media entries.
- *   cups_copy_media_db()      - Copy a media entry.
- *   cups_create_constraints() - Create the constraints and resolvers arrays.
- *   cups_create_defaults()    - Create the -default option array.
- *   cups_create_media_db()    - Create the media database.
- *   cups_free_media_cb()      - Free a media entry.
- *   cups_get_media_db()       - Lookup the media entry for a given size.
- *   cups_is_close_media_db()  - Compare two media entries to see if they are
- *                              close to the same size.
- *   cups_test_constraints()   - Test constraints.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
  */
 
 #include "cups-private.h"
+#include "debug-internal.h"
+
+
+/*
+ * Local constants...
+ */
+
+#define _CUPS_MEDIA_READY_TTL  30      /* Life of xxx-ready values */
 
 
 /*
  */
 
 static void            cups_add_dconstres(cups_array_t *a, ipp_t *collection);
+static int             cups_collection_contains(ipp_t *test, ipp_t *match);
+static size_t          cups_collection_string(ipp_attribute_t *attr, char *buffer, size_t bufsize) _CUPS_NONNULL((1,2));
 static int             cups_compare_dconstres(_cups_dconstres_t *a,
                                               _cups_dconstres_t *b);
 static int             cups_compare_media_db(_cups_media_db_t *a,
                                              _cups_media_db_t *b);
 static _cups_media_db_t        *cups_copy_media_db(_cups_media_db_t *mdb);
+static void            cups_create_cached(http_t *http, cups_dinfo_t *dinfo,
+                                          unsigned flags);
 static void            cups_create_constraints(cups_dinfo_t *dinfo);
 static void            cups_create_defaults(cups_dinfo_t *dinfo);
-static void            cups_create_media_db(cups_dinfo_t *dinfo);
+static void            cups_create_media_db(cups_dinfo_t *dinfo,
+                                            unsigned flags);
 static void            cups_free_media_db(_cups_media_db_t *mdb);
-static int             cups_get_media_db(cups_dinfo_t *dinfo,
-                                         _pwg_media_t *pwg, unsigned flags,
+static int             cups_get_media_db(http_t *http, cups_dinfo_t *dinfo,
+                                         pwg_media_t *pwg, unsigned flags,
                                          cups_size_t *size);
 static int             cups_is_close_media_db(_cups_media_db_t *a,
                                               _cups_media_db_t *b);
@@ -72,6 +53,105 @@ static cups_array_t *cups_test_constraints(cups_dinfo_t *dinfo,
                                               cups_option_t *options,
                                               int *num_conflicts,
                                               cups_option_t **conflicts);
+static void            cups_update_ready(http_t *http, cups_dinfo_t *dinfo);
+
+
+/*
+ * 'cupsAddDestMediaOptions()' - Add the option corresponding to the specified media size.
+ *
+ * @since CUPS 2.3/macOS 10.14@
+ */
+
+int                                    /* O  - New number of options */
+cupsAddDestMediaOptions(
+    http_t        *http,               /* I  - Connection to destination */
+    cups_dest_t   *dest,               /* I  - Destination */
+    cups_dinfo_t  *dinfo,              /* I  - Destination information */
+    unsigned      flags,               /* I  - Media matching flags */
+    cups_size_t   *size,               /* I  - Media size */
+    int           num_options,         /* I  - Current number of options */
+    cups_option_t **options)           /* IO - Options */
+{
+  cups_array_t         *db;            /* Media database */
+  _cups_media_db_t     *mdb;           /* Media database entry */
+  char                 value[2048];    /* Option value */
+
+
+ /*
+  * Range check input...
+  */
+
+  if (!http || !dest || !dinfo || !size || !options)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (num_options);
+  }
+
+ /*
+  * Find the matching media size...
+  */
+
+  if (flags & CUPS_MEDIA_FLAGS_READY)
+    db = dinfo->ready_db;
+  else
+    db = dinfo->media_db;
+
+  DEBUG_printf(("1cupsAddDestMediaOptions: size->media=\"%s\"", size->media));
+
+  for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
+  {
+    if (mdb->key && !strcmp(mdb->key, size->media))
+      break;
+    else if (mdb->size_name && !strcmp(mdb->size_name, size->media))
+      break;
+  }
+
+  if (!mdb)
+  {
+    for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
+    {
+      if (mdb->width == size->width && mdb->length == size->length && mdb->bottom == size->bottom && mdb->left == size->left && mdb->right == size->right && mdb->top == size->top)
+       break;
+    }
+  }
+
+  if (!mdb)
+  {
+    for (mdb = (_cups_media_db_t *)cupsArrayFirst(db); mdb; mdb = (_cups_media_db_t *)cupsArrayNext(db))
+    {
+      if (mdb->width == size->width && mdb->length == size->length)
+       break;
+    }
+  }
+
+  if (!mdb)
+  {
+    DEBUG_puts("1cupsAddDestMediaOptions: Unable to find matching size.");
+    return (num_options);
+  }
+
+  DEBUG_printf(("1cupsAddDestMediaOptions: MATCH mdb%p [key=\"%s\" size_name=\"%s\" source=\"%s\" type=\"%s\" width=%d length=%d B%d L%d R%d T%d]", (void *)mdb, mdb->key, mdb->size_name, mdb->source, mdb->type, mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top));
+
+  if (mdb->source)
+  {
+    if (mdb->type)
+      snprintf(value, sizeof(value), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-source=\"%s\" media-type=\"%s\"}", mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top, mdb->source, mdb->type);
+    else
+      snprintf(value, sizeof(value), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-source=\"%s\"}", mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top, mdb->source);
+  }
+  else if (mdb->type)
+  {
+    snprintf(value, sizeof(value), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d media-type=\"%s\"}", mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top, mdb->type);
+  }
+  else
+  {
+    snprintf(value, sizeof(value), "{media-size={x-dimension=%d y-dimension=%d} media-bottom-margin=%d media-left-margin=%d media-right-margin=%d media-top-margin=%d}", mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top);
+  }
+
+  num_options = cupsAddOption("media-col", value, num_options, options);
+
+  return (num_options);
+}
 
 
 /*
@@ -80,7 +160,7 @@ static cups_array_t  *cups_test_constraints(cups_dinfo_t *dinfo,
  *
  * Returns 1 if supported, 0 otherwise.
  *
- * @since CUPS 1.6/OS X 10.8@
+ * @since CUPS 1.6/macOS 10.8@
  */
 
 int                                    /* O - 1 if supported, 0 otherwise */
@@ -89,7 +169,7 @@ cupsCheckDestSupported(
     cups_dest_t  *dest,                        /* I - Destination */
     cups_dinfo_t *dinfo,               /* I - Destination information */
     const char   *option,              /* I - Option */
-    const char   *value)               /* I - Value */
+    const char   *value)               /* I - Value or @code NULL@ */
 {
   int                  i;              /* Looping var */
   char                 temp[1024];     /* Temporary string */
@@ -99,13 +179,21 @@ cupsCheckDestSupported(
   ipp_res_t            units_value;    /* Resolution units */
   ipp_attribute_t      *attr;          /* Attribute */
   _ipp_value_t         *attrval;       /* Current attribute value */
+  _ipp_option_t                *map;           /* Option mapping information */
 
 
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
  /*
   * Range check input...
   */
 
-  if (!http || !dest || !dinfo || !option || !value)
+  if (!http || !dest || !dinfo || !option)
     return (0);
 
  /*
@@ -123,7 +211,10 @@ cupsCheckDestSupported(
   if (!attr)
     return (0);
 
- /*
+  if (!value)
+    return (1);
+
+/*
   * Compare values...
   */
 
@@ -133,11 +224,11 @@ cupsCheckDestSupported(
     * Check range of custom media sizes...
     */
 
-    _pwg_media_t       *pwg;           /* Current PWG media size info */
-    int                        min_width,      /* Minimum width */
-                       min_length,     /* Minimum length */
-                       max_width,      /* Maximum width */
-                       max_length;     /* Maximum length */
+    pwg_media_t        *pwg;           /* Current PWG media size info */
+    int                min_width,      /* Minimum width */
+               min_length,     /* Minimum length */
+               max_width,      /* Maximum width */
+               max_length;     /* Maximum length */
 
    /*
     * Get the minimum and maximum size...
@@ -151,13 +242,13 @@ cupsCheckDestSupported(
         i --, attrval ++)
     {
       if (!strncmp(attrval->string.text, "custom_min_", 11) &&
-          (pwg = _pwgMediaForPWG(attrval->string.text)) != NULL)
+          (pwg = pwgMediaForPWG(attrval->string.text)) != NULL)
       {
         min_width  = pwg->width;
         min_length = pwg->length;
       }
       else if (!strncmp(attrval->string.text, "custom_max_", 11) &&
-              (pwg = _pwgMediaForPWG(attrval->string.text)) != NULL)
+              (pwg = pwgMediaForPWG(attrval->string.text)) != NULL)
       {
         max_width  = pwg->width;
         max_length = pwg->length;
@@ -169,7 +260,7 @@ cupsCheckDestSupported(
     */
 
     if (min_width < INT_MAX && max_width > 0 &&
-        (pwg = _pwgMediaForPWG(value)) != NULL &&
+        (pwg = pwgMediaForPWG(value)) != NULL &&
         pwg->width >= min_width && pwg->width <= max_width &&
         pwg->length >= min_length && pwg->length <= max_length)
       return (1);
@@ -180,9 +271,14 @@ cupsCheckDestSupported(
     * Check literal values...
     */
 
+    map = _ippFindOption(option);
+
     switch (attr->value_tag)
     {
       case IPP_TAG_INTEGER :
+          if (map && map->value_tag == IPP_TAG_STRING)
+            return (strlen(value) <= (size_t)attr->values[0].integer);
+
       case IPP_TAG_ENUM :
           int_value = atoi(value);
 
@@ -195,7 +291,10 @@ cupsCheckDestSupported(
           return (attr->values[0].boolean);
 
       case IPP_TAG_RANGE :
-          int_value = atoi(value);
+          if (map && map->value_tag == IPP_TAG_STRING)
+            int_value = (int)strlen(value);
+          else
+            int_value = atoi(value);
 
           for (i = 0; i < attr->num_values; i ++)
             if (int_value >= attr->values[i].range.lower &&
@@ -277,7 +376,7 @@ cupsCheckDestSupported(
  * If cupsCopyDestConflicts returns 1 but "num_resolved" and "resolved" are set
  * to 0 and @code NULL@, respectively, then the conflict cannot be resolved.
  *
- * @since CUPS 1.6/OS X 10.8@
+ * @since CUPS 1.6/macOS 10.8@
  */
 
 int                                    /* O - 1 if there is a conflict, 0 if none, -1 on error */
@@ -304,7 +403,7 @@ cupsCopyDestConflicts(
                *myres = NULL,          /* My resolved options */
                *myoption,              /* My current option */
                *option;                /* Current option */
-  cups_array_t *active,                /* Active conflicts */
+  cups_array_t *active = NULL,         /* Active conflicts */
                *pass = NULL,           /* Resolvers for this pass */
                *resolvers = NULL,      /* Resolvers we have used */
                *test;                  /* Test array for conflicts */
@@ -331,6 +430,13 @@ cupsCopyDestConflicts(
   if (resolved)
     *resolved = NULL;
 
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
  /*
   * Range check input...
   */
@@ -562,7 +668,7 @@ cupsCopyDestConflicts(
  * The caller is responsible for calling @link cupsFreeDestInfo@ on the return
  * value. @code NULL@ is returned on error.
  *
- * @since CUPS 1.6/OS X 10.8@
+ * @since CUPS 1.6/macOS 10.8@
  */
 
 cups_dinfo_t *                         /* O - Destination information */
@@ -571,6 +677,7 @@ cupsCopyDestInfo(
     cups_dest_t *dest)                 /* I - Destination */
 {
   cups_dinfo_t *dinfo;                 /* Destination information */
+  unsigned     dflags;                 /* Destination flags */
   ipp_t                *request,               /* Get-Printer-Attributes request */
                *response;              /* Supported attributes */
   int          tries,                  /* Number of tries so far */
@@ -580,6 +687,7 @@ cupsCopyDestInfo(
   char         resource[1024];         /* Resource path */
   int          version;                /* IPP version */
   ipp_status_t status;                 /* Status of request */
+  _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals */
   static const char * const requested_attrs[] =
   {                                    /* Requested attributes */
     "job-template",
@@ -588,8 +696,35 @@ cupsCopyDestInfo(
   };
 
 
-  DEBUG_printf(("cupsCopyDestSupported(http=%p, dest=%p(%s))", http, dest,
-                dest ? dest->name : ""));
+  DEBUG_printf(("cupsCopyDestInfo(http=%p, dest=%p(%s))", (void *)http, (void *)dest, dest ? dest->name : ""));
+
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+  {
+    DEBUG_puts("1cupsCopyDestInfo: Default server connection.");
+    http   = _cupsConnect();
+    dflags = CUPS_DEST_FLAGS_NONE;
+  }
+#ifdef AF_LOCAL
+  else if (httpAddrFamily(http->hostaddr) == AF_LOCAL)
+  {
+    DEBUG_puts("1cupsCopyDestInfo: Connection to server (domain socket).");
+    dflags = CUPS_DEST_FLAGS_NONE;
+  }
+#endif /* AF_LOCAL */
+  else if ((strcmp(http->hostname, cg->server) && cg->server[0] != '/') || cg->ipp_port != httpAddrPort(http->hostaddr))
+  {
+    DEBUG_printf(("1cupsCopyDestInfo: Connection to device (%s).", http->hostname));
+    dflags = CUPS_DEST_FLAGS_DEVICE;
+  }
+  else
+  {
+    DEBUG_printf(("1cupsCopyDestInfo: Connection to server (%s).", http->hostname));
+    dflags = CUPS_DEST_FLAGS_NONE;
+  }
 
  /*
   * Range check input...
@@ -602,8 +737,11 @@ cupsCopyDestInfo(
   * Get the printer URI and resource path...
   */
 
-  if ((uri = _cupsGetDestResource(dest, resource, sizeof(resource))) == NULL)
+  if ((uri = _cupsGetDestResource(dest, dflags, resource, sizeof(resource))) == NULL)
+  {
+    DEBUG_puts("1cupsCopyDestInfo: Unable to get resource.");
     return (NULL);
+  }
 
  /*
   * Get the supported attributes...
@@ -620,32 +758,29 @@ cupsCopyDestInfo(
     * Send a Get-Printer-Attributes request...
     */
 
-    request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
-                uri);
-    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
-                NULL, cupsUser());
-    ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
-                 "requested-attributes",
-                 (int)(sizeof(requested_attrs) / sizeof(requested_attrs[0])),
-                 NULL, requested_attrs);
+    request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
+
+    ippSetVersion(request, version / 10, version % 10);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
+    ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", (int)(sizeof(requested_attrs) / sizeof(requested_attrs[0])), NULL, requested_attrs);
     response = cupsDoRequest(http, request, resource);
     status   = cupsLastError();
 
-    if (status > IPP_OK_SUBST)
+    if (status > IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED)
     {
-      DEBUG_printf(("cupsCopyDestSupported: Get-Printer-Attributes for '%s' "
-                   "returned %s (%s)", dest->name, ippErrorString(status),
-                   cupsLastErrorString()));
+      DEBUG_printf(("1cupsCopyDestInfo: Get-Printer-Attributes for '%s' returned %s (%s)", dest->name, ippErrorString(status), cupsLastErrorString()));
 
       ippDelete(response);
       response = NULL;
 
-      if (status == IPP_VERSION_NOT_SUPPORTED && version > 11)
+      if ((status == IPP_STATUS_ERROR_BAD_REQUEST || status == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) && version > 11)
+      {
         version = 11;
-      else if (status == IPP_PRINTER_BUSY)
+      }
+      else if (status == IPP_STATUS_ERROR_BUSY)
       {
-        sleep(delay);
+        sleep((unsigned)delay);
 
         delay = _cupsNextDelay(delay, &prev_delay);
       }
@@ -658,7 +793,10 @@ cupsCopyDestInfo(
   while (!response && tries < 10);
 
   if (!response)
+  {
+    DEBUG_puts("1cupsCopyDestInfo: Unable to get printer attributes.");
     return (NULL);
+  }
 
  /*
   * Allocate a cups_dinfo_t structure and return it...
@@ -666,11 +804,14 @@ cupsCopyDestInfo(
 
   if ((dinfo = calloc(1, sizeof(cups_dinfo_t))) == NULL)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
     ippDelete(response);
     return (NULL);
   }
 
+  DEBUG_printf(("1cupsCopyDestInfo: version=%d, uri=\"%s\", resource=\"%s\".", version, uri, resource));
+
+  dinfo->version  = version;
   dinfo->uri      = uri;
   dinfo->resource = _cupsStrAlloc(resource);
   dinfo->attrs    = response;
@@ -679,9 +820,157 @@ cupsCopyDestInfo(
 }
 
 
+/*
+ * 'cupsFindDestDefault()' - Find the default value(s) for the given option.
+ *
+ * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
+ * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
+ * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
+ * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
+ * functions to inspect the default value(s) as needed.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+ipp_attribute_t        *                       /* O - Default attribute or @code NULL@ for none */
+cupsFindDestDefault(
+    http_t       *http,                        /* I - Connection to destination */
+    cups_dest_t  *dest,                        /* I - Destination */
+    cups_dinfo_t *dinfo,               /* I - Destination information */
+    const char   *option)              /* I - Option/attribute name */
+{
+  char name[IPP_MAX_NAME];             /* Attribute name */
+
+
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
+ /*
+  * Range check input...
+  */
+
+  if (!http || !dest || !dinfo || !option)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (NULL);
+  }
+
+ /*
+  * Find and return the attribute...
+  */
+
+  snprintf(name, sizeof(name), "%s-default", option);
+  return (ippFindAttribute(dinfo->attrs, name, IPP_TAG_ZERO));
+}
+
+
+/*
+ * 'cupsFindDestReady()' - Find the default value(s) for the given option.
+ *
+ * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
+ * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
+ * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
+ * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
+ * functions to inspect the default value(s) as needed.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+ipp_attribute_t        *                       /* O - Default attribute or @code NULL@ for none */
+cupsFindDestReady(
+    http_t       *http,                        /* I - Connection to destination */
+    cups_dest_t  *dest,                        /* I - Destination */
+    cups_dinfo_t *dinfo,               /* I - Destination information */
+    const char   *option)              /* I - Option/attribute name */
+{
+  char name[IPP_MAX_NAME];             /* Attribute name */
+
+
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
+ /*
+  * Range check input...
+  */
+
+  if (!http || !dest || !dinfo || !option)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (NULL);
+  }
+
+ /*
+  * Find and return the attribute...
+  */
+
+  cups_update_ready(http, dinfo);
+
+  snprintf(name, sizeof(name), "%s-ready", option);
+  return (ippFindAttribute(dinfo->ready_attrs, name, IPP_TAG_ZERO));
+}
+
+
+/*
+ * 'cupsFindDestSupported()' - Find the default value(s) for the given option.
+ *
+ * The returned value is an IPP attribute. Use the @code ippGetBoolean@,
+ * @code ippGetCollection@, @code ippGetCount@, @code ippGetDate@,
+ * @code ippGetInteger@, @code ippGetOctetString@, @code ippGetRange@,
+ * @code ippGetResolution@, @code ippGetString@, and @code ippGetValueTag@
+ * functions to inspect the default value(s) as needed.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+ipp_attribute_t        *                       /* O - Default attribute or @code NULL@ for none */
+cupsFindDestSupported(
+    http_t       *http,                        /* I - Connection to destination */
+    cups_dest_t  *dest,                        /* I - Destination */
+    cups_dinfo_t *dinfo,               /* I - Destination information */
+    const char   *option)              /* I - Option/attribute name */
+{
+  char name[IPP_MAX_NAME];             /* Attribute name */
+
+
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
+ /*
+  * Range check input...
+  */
+
+  if (!http || !dest || !dinfo || !option)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (NULL);
+  }
+
+ /*
+  * Find and return the attribute...
+  */
+
+  snprintf(name, sizeof(name), "%s-supported", option);
+  return (ippFindAttribute(dinfo->attrs, name, IPP_TAG_ZERO));
+}
+
+
 /*
  * 'cupsFreeDestInfo()' - Free destination information obtained using
  *                        @link cupsCopyDestInfo@.
+ *
+ * @since CUPS 1.6/macOS 10.8@
  */
 
 void
@@ -707,12 +996,104 @@ cupsFreeDestInfo(cups_dinfo_t *dinfo)    /* I - Destination information */
 
   cupsArrayDelete(dinfo->media_db);
 
+  cupsArrayDelete(dinfo->cached_db);
+
+  ippDelete(dinfo->ready_attrs);
+  cupsArrayDelete(dinfo->ready_db);
+
   ippDelete(dinfo->attrs);
 
   free(dinfo);
 }
 
 
+/*
+ * 'cupsGetDestMediaByIndex()' - Get a media name, dimension, and margins for a
+ *                               specific size.
+ *
+ * The @code flags@ parameter determines which set of media are indexed.  For
+ * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will get the Nth
+ * borderless size supported by the printer.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+int                                    /* O - 1 on success, 0 on failure */
+cupsGetDestMediaByIndex(
+    http_t       *http,                        /* I - Connection to destination */
+    cups_dest_t  *dest,                        /* I - Destination */
+    cups_dinfo_t *dinfo,               /* I - Destination information */
+    int          n,                    /* I - Media size number (0-based) */
+    unsigned     flags,                        /* I - Media flags */
+    cups_size_t  *size)                        /* O - Media size information */
+{
+  _cups_media_db_t     *nsize;         /* Size for N */
+  pwg_media_t          *pwg;           /* PWG media name for size */
+
+
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
+ /*
+  * Range check input...
+  */
+
+  if (size)
+    memset(size, 0, sizeof(cups_size_t));
+
+  if (!http || !dest || !dinfo || n < 0 || !size)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (0);
+  }
+
+ /*
+  * Load media list as needed...
+  */
+
+  if (flags & CUPS_MEDIA_FLAGS_READY)
+    cups_update_ready(http, dinfo);
+
+  if (!dinfo->cached_db || dinfo->cached_flags != flags)
+    cups_create_cached(http, dinfo, flags);
+
+ /*
+  * Copy the size over and return...
+  */
+
+  if ((nsize = (_cups_media_db_t *)cupsArrayIndex(dinfo->cached_db, n)) == NULL)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (0);
+  }
+
+  if (nsize->key)
+    strlcpy(size->media, nsize->key, sizeof(size->media));
+  else if (nsize->size_name)
+    strlcpy(size->media, nsize->size_name, sizeof(size->media));
+  else if ((pwg = pwgMediaForSize(nsize->width, nsize->length)) != NULL)
+    strlcpy(size->media, pwg->pwg, sizeof(size->media));
+  else
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (0);
+  }
+
+  size->width  = nsize->width;
+  size->length = nsize->length;
+  size->bottom = nsize->bottom;
+  size->left   = nsize->left;
+  size->right  = nsize->right;
+  size->top    = nsize->top;
+
+  return (1);
+}
+
+
 /*
  * 'cupsGetDestMediaByName()' - Get media names, dimensions, and margins.
  *
@@ -730,7 +1111,7 @@ cupsFreeDestInfo(cups_dinfo_t *dinfo)      /* I - Destination information */
  *
  * Returns 1 when there is a match and 0 if there is not a match.
  *
- * @since CUPS 1.6/OS X 10.8@
+ * @since CUPS 1.6/macOS 10.8@
  */
 
 int                                    /* O - 1 on match, 0 on failure */
@@ -742,9 +1123,16 @@ cupsGetDestMediaByName(
     unsigned     flags,                        /* I - Media matching flags */
     cups_size_t  *size)                        /* O - Media size information */
 {
-  _pwg_media_t         *pwg;           /* PWG media info */
+  pwg_media_t          *pwg;           /* PWG media info */
 
 
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
  /*
   * Range check input...
   */
@@ -754,7 +1142,7 @@ cupsGetDestMediaByName(
 
   if (!http || !dest || !dinfo || !media || !size)
   {
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
     return (0);
   }
 
@@ -762,11 +1150,11 @@ cupsGetDestMediaByName(
   * Lookup the media size name...
   */
 
-  if ((pwg = _pwgMediaForPWG(media)) == NULL)
-    if ((pwg = _pwgMediaForLegacy(media)) == NULL)
+  if ((pwg = pwgMediaForPWG(media)) == NULL)
+    if ((pwg = pwgMediaForLegacy(media)) == NULL)
     {
       DEBUG_printf(("1cupsGetDestMediaByName: Unknown size '%s'.", media));
-      _cupsSetError(IPP_INTERNAL_ERROR, _("Unknown media size name."), 1);
+      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unknown media size name."), 1);
       return (0);
     }
 
@@ -774,7 +1162,7 @@ cupsGetDestMediaByName(
   * Lookup the size...
   */
 
-  return (cups_get_media_db(dinfo, pwg, flags, size));
+  return (cups_get_media_db(http, dinfo, pwg, flags, size));
 }
 
 
@@ -795,7 +1183,7 @@ cupsGetDestMediaByName(
  *
  * Returns 1 when there is a match and 0 if there is not a match.
  *
- * @since CUPS 1.6/OS X 10.8@
+ * @since CUPS 1.6/macOS 10.8@
  */
 
 int                                    /* O - 1 on match, 0 on failure */
@@ -810,66 +1198,530 @@ cupsGetDestMediaBySize(
     unsigned     flags,                        /* I - Media matching flags */
     cups_size_t  *size)                        /* O - Media size information */
 {
-  _pwg_media_t         *pwg;           /* PWG media info */
+  pwg_media_t          *pwg;           /* PWG media info */
+
+
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
+ /*
+  * Range check input...
+  */
+
+  if (size)
+    memset(size, 0, sizeof(cups_size_t));
+
+  if (!http || !dest || !dinfo || width <= 0 || length <= 0 || !size)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (0);
+  }
+
+ /*
+  * Lookup the media size name...
+  */
+
+  if ((pwg = pwgMediaForSize(width, length)) == NULL)
+  {
+    DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width,
+                  length));
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Invalid media size."), 1);
+    return (0);
+  }
+
+ /*
+  * Lookup the size...
+  */
+
+  return (cups_get_media_db(http, dinfo, pwg, flags, size));
+}
+
+
+/*
+ * 'cupsGetDestMediaCount()' - Get the number of sizes supported by a
+ *                             destination.
+ *
+ * The @code flags@ parameter determines the set of media sizes that are
+ * counted.  For example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return
+ * the number of borderless sizes.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+int                                    /* O - Number of sizes */
+cupsGetDestMediaCount(
+    http_t       *http,                        /* I - Connection to destination */
+    cups_dest_t  *dest,                        /* I - Destination */
+    cups_dinfo_t *dinfo,               /* I - Destination information */
+    unsigned     flags)                        /* I - Media flags */
+{
+ /*
+  * Get the default connection as needed...
+  */
 
+  if (!http)
+    http = _cupsConnect();
 
  /*
   * Range check input...
   */
 
-  if (size)
-    memset(size, 0, sizeof(cups_size_t));
+  if (!http || !dest || !dinfo)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (0);
+  }
+
+ /*
+  * Load media list as needed...
+  */
+
+  if (flags & CUPS_MEDIA_FLAGS_READY)
+    cups_update_ready(http, dinfo);
+
+  if (!dinfo->cached_db || dinfo->cached_flags != flags)
+    cups_create_cached(http, dinfo, flags);
+
+  return (cupsArrayCount(dinfo->cached_db));
+}
+
+
+/*
+ * 'cupsGetDestMediaDefault()' - Get the default size for a destination.
+ *
+ * The @code flags@ parameter determines which default size is returned.  For
+ * example, passing @code CUPS_MEDIA_FLAGS_BORDERLESS@ will return the default
+ * borderless size, typically US Letter or A4, but sometimes 4x6 photo media.
+ *
+ * @since CUPS 1.7/macOS 10.9@
+ */
+
+int                                    /* O - 1 on success, 0 on failure */
+cupsGetDestMediaDefault(
+    http_t       *http,                        /* I - Connection to destination */
+    cups_dest_t  *dest,                        /* I - Destination */
+    cups_dinfo_t *dinfo,               /* I - Destination information */
+    unsigned     flags,                        /* I - Media flags */
+    cups_size_t  *size)                        /* O - Media size information */
+{
+  const char   *media;                 /* Default media size */
+
+
+ /*
+  * Get the default connection as needed...
+  */
+
+  if (!http)
+    http = _cupsConnect();
+
+ /*
+  * Range check input...
+  */
+
+  if (size)
+    memset(size, 0, sizeof(cups_size_t));
+
+  if (!http || !dest || !dinfo || !size)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+    return (0);
+  }
+
+ /*
+  * Get the default media size, if any...
+  */
+
+  if ((media = cupsGetOption("media", dest->num_options, dest->options)) == NULL)
+    media = "na_letter_8.5x11in";
+
+  if (cupsGetDestMediaByName(http, dest, dinfo, media, flags, size))
+    return (1);
+
+  if (strcmp(media, "na_letter_8.5x11in") && cupsGetDestMediaByName(http, dest, dinfo, "iso_a4_210x297mm", flags, size))
+    return (1);
+
+  if (strcmp(media, "iso_a4_210x297mm") && cupsGetDestMediaByName(http, dest, dinfo, "na_letter_8.5x11in", flags, size))
+    return (1);
+
+  if ((flags & CUPS_MEDIA_FLAGS_BORDERLESS) && cupsGetDestMediaByName(http, dest, dinfo, "na_index_4x6in", flags, size))
+    return (1);
+
+ /*
+  * Fall back to the first matching media size...
+  */
+
+  return (cupsGetDestMediaByIndex(http, dest, dinfo, 0, flags, size));
+}
+
+
+/*
+ * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
+ */
+
+static void
+cups_add_dconstres(
+    cups_array_t *a,                   /* I - Array */
+    ipp_t        *collection)          /* I - Collection value */
+{
+  ipp_attribute_t      *attr;          /* Attribute */
+  _cups_dconstres_t    *temp;          /* Current constraint/resolver */
+
+
+  if ((attr = ippFindAttribute(collection, "resolver-name",
+                               IPP_TAG_NAME)) == NULL)
+    return;
+
+  if ((temp = calloc(1, sizeof(_cups_dconstres_t))) == NULL)
+    return;
+
+  temp->name       = attr->values[0].string.text;
+  temp->collection = collection;
+
+  cupsArrayAdd(a, temp);
+}
+
+
+/*
+ * 'cups_collection_contains()' - Check whether test collection is contained in the matching collection.
+ */
+
+static int                             /* O - 1 on a match, 0 on a non-match */
+cups_collection_contains(ipp_t *test,  /* I - Collection to test */
+                         ipp_t *match) /* I - Matching values */
+{
+  int                  i, j,           /* Looping vars */
+                       mcount,         /* Number of match values */
+                       tcount;         /* Number of test values */
+  ipp_attribute_t      *tattr,         /* Testing attribute */
+                       *mattr;         /* Matching attribute */
+  const char           *tval;          /* Testing string value */
+
+
+  for (mattr = ippFirstAttribute(match); mattr; mattr = ippNextAttribute(match))
+  {
+    if ((tattr = ippFindAttribute(test, ippGetName(mattr), IPP_TAG_ZERO)) == NULL)
+      return (0);
+
+    tcount = ippGetCount(tattr);
+
+    switch (ippGetValueTag(mattr))
+    {
+      case IPP_TAG_INTEGER :
+      case IPP_TAG_ENUM :
+          if (ippGetValueTag(tattr) != ippGetValueTag(mattr))
+            return (0);
+
+          for (i = 0; i < tcount; i ++)
+          {
+            if (!ippContainsInteger(mattr, ippGetInteger(tattr, i)))
+              return (0);
+          }
+          break;
+
+      case IPP_TAG_RANGE :
+          if (ippGetValueTag(tattr) != IPP_TAG_INTEGER)
+            return (0);
+
+          for (i = 0; i < tcount; i ++)
+          {
+            if (!ippContainsInteger(mattr, ippGetInteger(tattr, i)))
+              return (0);
+          }
+          break;
+
+      case IPP_TAG_BOOLEAN :
+          if (ippGetValueTag(tattr) != IPP_TAG_BOOLEAN || ippGetBoolean(tattr, 0) != ippGetBoolean(mattr, 0))
+            return (0);
+          break;
+
+      case IPP_TAG_TEXTLANG :
+      case IPP_TAG_NAMELANG :
+      case IPP_TAG_TEXT :
+      case IPP_TAG_NAME :
+      case IPP_TAG_KEYWORD :
+      case IPP_TAG_URI :
+      case IPP_TAG_URISCHEME :
+      case IPP_TAG_CHARSET :
+      case IPP_TAG_LANGUAGE :
+      case IPP_TAG_MIMETYPE :
+          for (i = 0; i < tcount; i ++)
+          {
+            if ((tval = ippGetString(tattr, i, NULL)) == NULL || !ippContainsString(mattr, tval))
+              return (0);
+          }
+          break;
 
-  if (!http || !dest || !dinfo || width <= 0 || length <= 0 || !size)
-  {
-    _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
-    return (0);
-  }
+      case IPP_TAG_BEGIN_COLLECTION :
+          for (i = 0; i < tcount; i ++)
+          {
+            ipp_t *tcol = ippGetCollection(tattr, i);
+                                       /* Testing collection */
 
- /*
-  * Lookup the media size name...
-  */
+            for (j = 0, mcount = ippGetCount(mattr); j < mcount; j ++)
+              if (!cups_collection_contains(tcol, ippGetCollection(mattr, j)))
+                return (0);
+          }
+          break;
 
-  if ((pwg = _pwgMediaForSize(width, length)) == NULL)
-  {
-    DEBUG_printf(("1cupsGetDestMediaBySize: Invalid size %dx%d.", width,
-                  length));
-    _cupsSetError(IPP_INTERNAL_ERROR, _("Invalid media size."), 1);
-    return (0);
+      default :
+          return (0);
+    }
   }
 
- /*
-  * Lookup the size...
-  */
-
-  return (cups_get_media_db(dinfo, pwg, flags, size));
+  return (1);
 }
 
 
 /*
- * 'cups_add_dconstres()' - Add a constraint or resolver to an array.
+ * 'cups_collection_string()' - Convert an IPP collection to an option string.
  */
 
-static void
-cups_add_dconstres(
-    cups_array_t *a,                   /* I - Array */
-    ipp_t        *collection)          /* I - Collection value */
+static size_t                          /* O - Number of bytes needed */
+cups_collection_string(
+    ipp_attribute_t *attr,             /* I - Collection attribute */
+    char            *buffer,           /* I - String buffer */
+    size_t          bufsize)           /* I - Size of buffer */
 {
-  ipp_attribute_t      *attr;          /* Attribute */
-  _cups_dconstres_t    *temp;          /* Current constraint/resolver */
+  int                  i, j,           /* Looping vars */
+                       count,          /* Number of collection values */
+                       mcount;         /* Number of member values */
+  ipp_t                        *col;           /* Collection */
+  ipp_attribute_t      *first,         /* First member attribute */
+                       *member;        /* Member attribute */
+  char                 *bufptr,        /* Pointer into buffer */
+                       *bufend,        /* End of buffer */
+                       temp[100];      /* Temporary string */
+  const char           *mptr;          /* Pointer into member value */
+  int                  mlen;           /* Length of octetString */
+
+
+  bufptr = buffer;
+  bufend = buffer + bufsize - 1;
+
+  for (i = 0, count = ippGetCount(attr); i < count; i ++)
+  {
+    col = ippGetCollection(attr, i);
 
+    if (i)
+    {
+      if (bufptr < bufend)
+        *bufptr++ = ',';
+      else
+        bufptr ++;
+    }
 
-  if ((attr = ippFindAttribute(collection, "resolver-name",
-                               IPP_TAG_NAME)) == NULL)
-    return;
+    if (bufptr < bufend)
+      *bufptr++ = '{';
+    else
+      bufptr ++;
 
-  if ((temp = calloc(1, sizeof(_cups_dconstres_t))) == NULL)
-    return;
+    for (member = first = ippFirstAttribute(col); member; member = ippNextAttribute(col))
+    {
+      const char *mname = ippGetName(member);
 
-  temp->name       = attr->values[0].string.text;
-  temp->collection = collection;
+      if (member != first)
+      {
+       if (bufptr < bufend)
+         *bufptr++ = ' ';
+       else
+         bufptr ++;
+      }
 
-  cupsArrayAdd(a, temp);
+      if (ippGetValueTag(member) == IPP_TAG_BOOLEAN)
+      {
+        if (!ippGetBoolean(member, 0))
+        {
+         if (bufptr < bufend)
+           strlcpy(bufptr, "no", (size_t)(bufend - bufptr + 1));
+         bufptr += 2;
+        }
+
+       if (bufptr < bufend)
+         strlcpy(bufptr, mname, (size_t)(bufend - bufptr + 1));
+       bufptr += strlen(mname);
+        continue;
+      }
+
+      if (bufptr < bufend)
+        strlcpy(bufptr, mname, (size_t)(bufend - bufptr + 1));
+      bufptr += strlen(mname);
+
+      if (bufptr < bufend)
+        *bufptr++ = '=';
+      else
+        bufptr ++;
+
+      if (ippGetValueTag(member) == IPP_TAG_BEGIN_COLLECTION)
+      {
+       /*
+       * Convert sub-collection...
+       */
+
+       bufptr += cups_collection_string(member, bufptr, bufptr < bufend ? (size_t)(bufend - bufptr + 1) : 0);
+      }
+      else
+      {
+       /*
+        * Convert simple type...
+        */
+
+       for (j = 0, mcount = ippGetCount(member); j < mcount; j ++)
+       {
+         if (j)
+         {
+           if (bufptr < bufend)
+             *bufptr++ = ',';
+           else
+             bufptr ++;
+         }
+
+          switch (ippGetValueTag(member))
+          {
+            case IPP_TAG_INTEGER :
+            case IPP_TAG_ENUM :
+                bufptr += snprintf(bufptr, bufptr < bufend ? (size_t)(bufend - bufptr + 1) : 0, "%d", ippGetInteger(member, j));
+                break;
+
+           case IPP_TAG_STRING :
+               if (bufptr < bufend)
+                 *bufptr++ = '\"';
+               else
+                 bufptr ++;
+
+               for (mptr = (const char *)ippGetOctetString(member, j, &mlen); mlen > 0; mlen --, mptr ++)
+               {
+                 if (*mptr == '\"' || *mptr == '\\')
+                 {
+                   if (bufptr < bufend)
+                     *bufptr++ = '\\';
+                   else
+                     bufptr ++;
+                 }
+
+                 if (bufptr < bufend)
+                   *bufptr++ = *mptr;
+                 else
+                   bufptr ++;
+                }
+
+               if (bufptr < bufend)
+                 *bufptr++ = '\"';
+               else
+                 bufptr ++;
+               break;
+
+            case IPP_TAG_DATE :
+               {
+                 unsigned year;        /* Year */
+                 const ipp_uchar_t *date = ippGetDate(member, j);
+                                       /* Date value */
+
+                 year = ((unsigned)date[0] << 8) + (unsigned)date[1];
+
+                 if (date[9] == 0 && date[10] == 0)
+                   snprintf(temp, sizeof(temp), "%04u-%02u-%02uT%02u:%02u:%02uZ", year, date[2], date[3], date[4], date[5], date[6]);
+                 else
+                   snprintf(temp, sizeof(temp), "%04u-%02u-%02uT%02u:%02u:%02u%c%02u%02u", year, date[2], date[3], date[4], date[5], date[6], date[8], date[9], date[10]);
+
+                 if (bufptr < bufend)
+                   strlcpy(bufptr, temp, (size_t)(bufend - bufptr + 1));
+
+                 bufptr += strlen(temp);
+               }
+                break;
+
+            case IPP_TAG_RESOLUTION :
+                {
+                  int          xres,   /* Horizontal resolution */
+                               yres;   /* Vertical resolution */
+                  ipp_res_t    units;  /* Resolution units */
+
+                  xres = ippGetResolution(member, j, &yres, &units);
+
+                  if (xres == yres)
+                    snprintf(temp, sizeof(temp), "%d%s", xres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm");
+                 else
+                    snprintf(temp, sizeof(temp), "%dx%d%s", xres, yres, units == IPP_RES_PER_INCH ? "dpi" : "dpcm");
+
+                 if (bufptr < bufend)
+                   strlcpy(bufptr, temp, (size_t)(bufend - bufptr + 1));
+
+                 bufptr += strlen(temp);
+                }
+                break;
+
+            case IPP_TAG_RANGE :
+                {
+                  int          lower,  /* Lower bound */
+                               upper;  /* Upper bound */
+
+                  lower = ippGetRange(member, j, &upper);
+
+                 snprintf(temp, sizeof(temp), "%d-%d", lower, upper);
+
+                 if (bufptr < bufend)
+                   strlcpy(bufptr, temp, (size_t)(bufend - bufptr + 1));
+
+                 bufptr += strlen(temp);
+                }
+                break;
+
+            case IPP_TAG_TEXTLANG :
+            case IPP_TAG_NAMELANG :
+            case IPP_TAG_TEXT :
+            case IPP_TAG_NAME :
+            case IPP_TAG_KEYWORD :
+            case IPP_TAG_URI :
+            case IPP_TAG_URISCHEME :
+            case IPP_TAG_CHARSET :
+            case IPP_TAG_LANGUAGE :
+            case IPP_TAG_MIMETYPE :
+               if (bufptr < bufend)
+                 *bufptr++ = '\"';
+               else
+                 bufptr ++;
+
+               for (mptr = ippGetString(member, j, NULL); *mptr; mptr ++)
+               {
+                 if (*mptr == '\"' || *mptr == '\\')
+                 {
+                   if (bufptr < bufend)
+                     *bufptr++ = '\\';
+                   else
+                     bufptr ++;
+                 }
+
+                 if (bufptr < bufend)
+                   *bufptr++ = *mptr;
+                 else
+                   bufptr ++;
+                }
+
+               if (bufptr < bufend)
+                 *bufptr++ = '\"';
+               else
+                 bufptr ++;
+                break;
+
+            default :
+                break;
+          }
+       }
+      }
+    }
+
+    if (bufptr < bufend)
+      *bufptr++ = '}';
+    else
+      bufptr ++;
+  }
+
+  *bufptr = '\0';
+  return ((size_t)(bufptr - buffer + 1));
 }
 
 
@@ -943,6 +1795,86 @@ cups_copy_media_db(
 }
 
 
+/*
+ * 'cups_create_cached()' - Create the media selection cache.
+ */
+
+static void
+cups_create_cached(http_t       *http, /* I - Connection to destination */
+                   cups_dinfo_t *dinfo,        /* I - Destination information */
+                   unsigned     flags) /* I - Media selection flags */
+{
+  cups_array_t         *db;            /* Media database array to use */
+  _cups_media_db_t     *mdb,           /* Media database entry */
+                       *first;         /* First entry this size */
+
+
+  DEBUG_printf(("3cups_create_cached(http=%p, dinfo=%p, flags=%u)", (void *)http, (void *)dinfo, flags));
+
+  if (dinfo->cached_db)
+    cupsArrayDelete(dinfo->cached_db);
+
+  dinfo->cached_db    = cupsArrayNew(NULL, NULL);
+  dinfo->cached_flags = flags;
+
+  if (flags & CUPS_MEDIA_FLAGS_READY)
+  {
+    DEBUG_puts("4cups_create_cached: ready media");
+
+    cups_update_ready(http, dinfo);
+    db = dinfo->ready_db;
+  }
+  else
+  {
+    DEBUG_puts("4cups_create_cached: supported media");
+
+    if (!dinfo->media_db)
+      cups_create_media_db(dinfo, CUPS_MEDIA_FLAGS_DEFAULT);
+
+    db = dinfo->media_db;
+  }
+
+  for (mdb = (_cups_media_db_t *)cupsArrayFirst(db), first = mdb;
+       mdb;
+       mdb = (_cups_media_db_t *)cupsArrayNext(db))
+  {
+    DEBUG_printf(("4cups_create_cached: %p key=\"%s\", type=\"%s\", %dx%d, B%d L%d R%d T%d", (void *)mdb, mdb->key, mdb->type, mdb->width, mdb->length, mdb->bottom, mdb->left, mdb->right, mdb->top));
+
+    if (flags & CUPS_MEDIA_FLAGS_BORDERLESS)
+    {
+      if (!mdb->left && !mdb->right && !mdb->top && !mdb->bottom)
+      {
+        DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb));
+        cupsArrayAdd(dinfo->cached_db, mdb);
+      }
+    }
+    else if (flags & CUPS_MEDIA_FLAGS_DUPLEX)
+    {
+      if (first->width != mdb->width || first->length != mdb->length)
+      {
+       DEBUG_printf(("4cups_create_cached: add %p", (void *)first));
+        cupsArrayAdd(dinfo->cached_db, first);
+        first = mdb;
+      }
+      else if (mdb->left >= first->left && mdb->right >= first->right && mdb->top >= first->top && mdb->bottom >= first->bottom &&
+              (mdb->left != first->left || mdb->right != first->right || mdb->top != first->top || mdb->bottom != first->bottom))
+        first = mdb;
+    }
+    else
+    {
+      DEBUG_printf(("4cups_create_cached: add %p", (void *)mdb));
+      cupsArrayAdd(dinfo->cached_db, mdb);
+    }
+  }
+
+  if (flags & CUPS_MEDIA_FLAGS_DUPLEX)
+  {
+    DEBUG_printf(("4cups_create_cached: add %p", (void *)first));
+    cupsArrayAdd(dinfo->cached_db, first);
+  }
+}
+
+
 /*
  * 'cups_create_constraints()' - Create the constraints and resolvers arrays.
  */
@@ -980,8 +1912,6 @@ cups_create_constraints(
 
 /*
  * 'cups_create_defaults()' - Create the -default option array.
- *
- * TODO: Need to support collection defaults...
  */
 
 static void
@@ -1000,32 +1930,26 @@ cups_create_defaults(
   * xxx=value to the defaults option array.
   */
 
-  for (attr = ippFirstAttribute(dinfo->attrs);
-       attr;
-       attr = ippNextAttribute(dinfo->attrs))
+  for (attr = ippFirstAttribute(dinfo->attrs); attr; attr = ippNextAttribute(dinfo->attrs))
   {
-    if (!attr->name || attr->group_tag != IPP_TAG_PRINTER)
-      continue;
-
-    if (attr->value_tag == IPP_TAG_BEGIN_COLLECTION)
-      continue;                                /* TODO: STR #4096 */
-
-    if ((nameptr = attr->name + strlen(attr->name) - 8) <= attr->name ||
-        strcmp(nameptr, "-default"))
+    if (!ippGetName(attr) || ippGetGroupTag(attr) != IPP_TAG_PRINTER)
       continue;
 
-    strlcpy(name, attr->name, sizeof(name));
-    if ((nameptr = name + strlen(name) - 8) <= name ||
-        strcmp(nameptr, "-default"))
+    strlcpy(name, ippGetName(attr), sizeof(name));
+    if ((nameptr = name + strlen(name) - 8) <= name || strcmp(nameptr, "-default"))
       continue;
 
     *nameptr = '\0';
 
-    if (ippAttributeString(attr, value, sizeof(value)) >= sizeof(value))
+    if (ippGetValueTag(attr) == IPP_TAG_BEGIN_COLLECTION)
+    {
+      if (cups_collection_string(attr, value, sizeof(value)) >= sizeof(value))
+        continue;
+    }
+    else if (ippAttributeString(attr, value, sizeof(value)) >= sizeof(value))
       continue;
 
-    dinfo->num_defaults = cupsAddOption(name, value, dinfo->num_defaults,
-                                        &dinfo->defaults);
+    dinfo->num_defaults = cupsAddOption(name, value, dinfo->num_defaults, &dinfo->defaults);
   }
 }
 
@@ -1036,7 +1960,8 @@ cups_create_defaults(
 
 static void
 cups_create_media_db(
-    cups_dinfo_t *dinfo)               /* I - Destination information */
+    cups_dinfo_t *dinfo,               /* I - Destination information */
+    unsigned     flags)                        /* I - Media flags */
 {
   int                  i;              /* Looping var */
   _ipp_value_t         *val;           /* Current value */
@@ -1044,21 +1969,41 @@ cups_create_media_db(
                        *media_attr,    /* media-xxx */
                        *x_dimension,   /* x-dimension */
                        *y_dimension;   /* y-dimension */
-  _pwg_media_t         *pwg;           /* PWG media info */
+  pwg_media_t          *pwg;           /* PWG media info */
+  cups_array_t         *db;            /* New media database array */
   _cups_media_db_t     mdb;            /* Media entry */
+  char                 media_key[256]; /* Synthesized media-key value */
 
 
-  dinfo->media_db = cupsArrayNew3((cups_array_func_t)cups_compare_media_db,
-                                  NULL, NULL, 0,
-                                  (cups_acopy_func_t)cups_copy_media_db,
-                                  (cups_afree_func_t)cups_free_media_db);
-  dinfo->min_size.width  = INT_MAX;
-  dinfo->min_size.length = INT_MAX;
-  dinfo->max_size.width  = 0;
-  dinfo->max_size.length = 0;
+  db = cupsArrayNew3((cups_array_func_t)cups_compare_media_db,
+                    NULL, NULL, 0,
+                    (cups_acopy_func_t)cups_copy_media_db,
+                    (cups_afree_func_t)cups_free_media_db);
 
-  if ((media_col_db = ippFindAttribute(dinfo->attrs, "media-col-database",
-                                       IPP_TAG_BEGIN_COLLECTION)) != NULL)
+  if (flags == CUPS_MEDIA_FLAGS_READY)
+  {
+    dinfo->ready_db = db;
+
+    media_col_db = ippFindAttribute(dinfo->ready_attrs, "media-col-ready",
+                                   IPP_TAG_BEGIN_COLLECTION);
+    media_attr   = ippFindAttribute(dinfo->ready_attrs, "media-ready",
+                                   IPP_TAG_ZERO);
+  }
+  else
+  {
+    dinfo->media_db        = db;
+    dinfo->min_size.width  = INT_MAX;
+    dinfo->min_size.length = INT_MAX;
+    dinfo->max_size.width  = 0;
+    dinfo->max_size.length = 0;
+
+    media_col_db = ippFindAttribute(dinfo->attrs, "media-col-database",
+                                   IPP_TAG_BEGIN_COLLECTION);
+    media_attr   = ippFindAttribute(dinfo->attrs, "media-supported",
+                                   IPP_TAG_ZERO);
+  }
+
+  if (media_col_db)
   {
     _ipp_value_t       *custom = NULL; /* Custom size range value */
 
@@ -1075,15 +2020,32 @@ cups_create_media_db(
                                        /* media-size collection value */
 
         if ((x_dimension = ippFindAttribute(media_size, "x-dimension",
-                                          IPP_TAG_INTEGER)) != NULL &&
+                                            IPP_TAG_INTEGER)) != NULL &&
            (y_dimension = ippFindAttribute(media_size, "y-dimension",
                                            IPP_TAG_INTEGER)) != NULL)
        {
+        /*
+         * Fixed size...
+         */
+
          mdb.width  = x_dimension->values[0].integer;
          mdb.length = y_dimension->values[0].integer;
        }
-        else if ((x_dimension = ippFindAttribute(media_size, "x-dimension",
-                                              IPP_TAG_RANGE)) != NULL &&
+       else if ((x_dimension = ippFindAttribute(media_size, "x-dimension",
+                                                IPP_TAG_INTEGER)) != NULL &&
+                (y_dimension = ippFindAttribute(media_size, "y-dimension",
+                                                IPP_TAG_RANGE)) != NULL)
+       {
+        /*
+         * Roll limits...
+         */
+
+         mdb.width  = x_dimension->values[0].integer;
+         mdb.length = y_dimension->values[0].range.upper;
+       }
+        else if (flags != CUPS_MEDIA_FLAGS_READY &&
+                 (x_dimension = ippFindAttribute(media_size, "x-dimension",
+                                                IPP_TAG_RANGE)) != NULL &&
                 (y_dimension = ippFindAttribute(media_size, "y-dimension",
                                                 IPP_TAG_RANGE)) != NULL)
        {
@@ -1107,67 +2069,97 @@ cups_create_media_db(
          dinfo->max_size.right  = 635; /* Default 1/4" side margins */
          dinfo->max_size.top    =
          dinfo->max_size.bottom = 1270; /* Default 1/2" top/bottom margins */
-
          continue;
        }
       }
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-color",
-                                         IPP_TAG_ZERO)) != NULL &&
-          (media_attr->value_tag == IPP_TAG_NAME ||
-           media_attr->value_tag == IPP_TAG_NAMELANG ||
-           media_attr->value_tag == IPP_TAG_KEYWORD))
+      if ((media_attr = ippFindAttribute(val->collection, "media-color", IPP_TAG_ZERO)) != NULL && (media_attr->value_tag == IPP_TAG_NAME || media_attr->value_tag == IPP_TAG_NAMELANG || media_attr->value_tag == IPP_TAG_KEYWORD))
         mdb.color = media_attr->values[0].string.text;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-info",
-                                         IPP_TAG_TEXT)) != NULL)
+      if ((media_attr = ippFindAttribute(val->collection, "media-info", IPP_TAG_TEXT)) != NULL)
         mdb.info = media_attr->values[0].string.text;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-key",
-                                         IPP_TAG_ZERO)) != NULL &&
-          (media_attr->value_tag == IPP_TAG_NAME ||
-           media_attr->value_tag == IPP_TAG_NAMELANG ||
-           media_attr->value_tag == IPP_TAG_KEYWORD))
+      if ((media_attr = ippFindAttribute(val->collection, "media-key", IPP_TAG_ZERO)) != NULL && (media_attr->value_tag == IPP_TAG_NAME || media_attr->value_tag == IPP_TAG_NAMELANG || media_attr->value_tag == IPP_TAG_KEYWORD))
         mdb.key = media_attr->values[0].string.text;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-size-name",
-                                         IPP_TAG_ZERO)) != NULL &&
-          (media_attr->value_tag == IPP_TAG_NAME ||
-           media_attr->value_tag == IPP_TAG_NAMELANG ||
-           media_attr->value_tag == IPP_TAG_KEYWORD))
+      if ((media_attr = ippFindAttribute(val->collection, "media-size-name", IPP_TAG_ZERO)) != NULL && (media_attr->value_tag == IPP_TAG_NAME || media_attr->value_tag == IPP_TAG_NAMELANG || media_attr->value_tag == IPP_TAG_KEYWORD))
         mdb.size_name = media_attr->values[0].string.text;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-source",
-                                         IPP_TAG_ZERO)) != NULL &&
-          (media_attr->value_tag == IPP_TAG_NAME ||
-           media_attr->value_tag == IPP_TAG_NAMELANG ||
-           media_attr->value_tag == IPP_TAG_KEYWORD))
+      if ((media_attr = ippFindAttribute(val->collection, "media-source", IPP_TAG_ZERO)) != NULL && (media_attr->value_tag == IPP_TAG_NAME || media_attr->value_tag == IPP_TAG_NAMELANG || media_attr->value_tag == IPP_TAG_KEYWORD))
         mdb.source = media_attr->values[0].string.text;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-type",
-                                         IPP_TAG_ZERO)) != NULL &&
-          (media_attr->value_tag == IPP_TAG_NAME ||
-           media_attr->value_tag == IPP_TAG_NAMELANG ||
-           media_attr->value_tag == IPP_TAG_KEYWORD))
+      if ((media_attr = ippFindAttribute(val->collection, "media-type", IPP_TAG_ZERO)) != NULL && (media_attr->value_tag == IPP_TAG_NAME || media_attr->value_tag == IPP_TAG_NAMELANG || media_attr->value_tag == IPP_TAG_KEYWORD))
         mdb.type = media_attr->values[0].string.text;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-bottom-margin",
-                                         IPP_TAG_INTEGER)) != NULL)
+      if ((media_attr = ippFindAttribute(val->collection, "media-bottom-margin", IPP_TAG_INTEGER)) != NULL)
         mdb.bottom = media_attr->values[0].integer;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-left-margin",
-                                         IPP_TAG_INTEGER)) != NULL)
+      if ((media_attr = ippFindAttribute(val->collection, "media-left-margin", IPP_TAG_INTEGER)) != NULL)
         mdb.left = media_attr->values[0].integer;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-right-margin",
-                                         IPP_TAG_INTEGER)) != NULL)
+      if ((media_attr = ippFindAttribute(val->collection, "media-right-margin", IPP_TAG_INTEGER)) != NULL)
         mdb.right = media_attr->values[0].integer;
 
-      if ((media_attr = ippFindAttribute(val->collection, "media-top-margin",
-                                         IPP_TAG_INTEGER)) != NULL)
+      if ((media_attr = ippFindAttribute(val->collection, "media-top-margin", IPP_TAG_INTEGER)) != NULL)
         mdb.top = media_attr->values[0].integer;
 
-      cupsArrayAdd(dinfo->media_db, &mdb);
+      if (!mdb.key)
+      {
+        if (!mdb.size_name && (pwg = pwgMediaForSize(mdb.width, mdb.length)) != NULL)
+         mdb.size_name = (char *)pwg->pwg;
+
+        if (!mdb.size_name)
+        {
+         /*
+          * Use a CUPS-specific identifier if we don't have a size name...
+          */
+
+         if (flags & CUPS_MEDIA_FLAGS_READY)
+           snprintf(media_key, sizeof(media_key), "cups-media-ready-%d", i + 1);
+         else
+           snprintf(media_key, sizeof(media_key), "cups-media-%d", i + 1);
+        }
+        else if (mdb.source)
+        {
+         /*
+          * Generate key using size name, source, and type (if set)...
+          */
+
+          if (mdb.type)
+            snprintf(media_key, sizeof(media_key), "%s_%s_%s", mdb.size_name, mdb.source, mdb.type);
+         else
+            snprintf(media_key, sizeof(media_key), "%s_%s", mdb.size_name, mdb.source);
+        }
+        else if (mdb.type)
+        {
+         /*
+          * Generate key using size name and type...
+          */
+
+         snprintf(media_key, sizeof(media_key), "%s_%s", mdb.size_name, mdb.type);
+        }
+        else
+        {
+         /*
+          * Key is just the size name...
+          */
+
+          strlcpy(media_key, mdb.size_name, sizeof(media_key));
+        }
+
+       /*
+        * Append "_borderless" for borderless media...
+        */
+
+        if (!mdb.bottom && !mdb.left && !mdb.right && !mdb.top)
+          strlcat(media_key, "_borderless", sizeof(media_key));
+
+        mdb.key = media_key;
+      }
+
+      DEBUG_printf(("1cups_create_media_db: Adding media: key=\"%s\", width=%d, length=%d, source=\"%s\", type=\"%s\".", mdb.key, mdb.width, mdb.length, mdb.source, mdb.type));
+
+      cupsArrayAdd(db, &mdb);
     }
 
     if (custom)
@@ -1205,8 +2197,7 @@ cups_create_media_db(
       }
     }
   }
-  else if ((media_attr = ippFindAttribute(dinfo->attrs, "media-supported",
-                                          IPP_TAG_ZERO)) != NULL &&
+  else if (media_attr &&
            (media_attr->value_tag == IPP_TAG_NAME ||
             media_attr->value_tag == IPP_TAG_NAMELANG ||
             media_attr->value_tag == IPP_TAG_KEYWORD))
@@ -1222,8 +2213,8 @@ cups_create_media_db(
          i > 0;
          i --, val ++)
     {
-      if ((pwg = _pwgMediaForPWG(val->string.text)) == NULL)
-        if ((pwg = _pwgMediaForLegacy(val->string.text)) == NULL)
+      if ((pwg = pwgMediaForPWG(val->string.text)) == NULL)
+        if ((pwg = pwgMediaForLegacy(val->string.text)) == NULL)
        {
          DEBUG_printf(("3cups_create_media_db: Ignoring unknown size '%s'.",
                        val->string.text));
@@ -1233,12 +2224,14 @@ cups_create_media_db(
       mdb.width  = pwg->width;
       mdb.length = pwg->length;
 
-      if (!strncmp(val->string.text, "custom_min_", 11))
+      if (flags != CUPS_MEDIA_FLAGS_READY &&
+          !strncmp(val->string.text, "custom_min_", 11))
       {
         mdb.size_name   = NULL;
         dinfo->min_size = mdb;
       }
-      else if (!strncmp(val->string.text, "custom_max_", 11))
+      else if (flags != CUPS_MEDIA_FLAGS_READY &&
+              !strncmp(val->string.text, "custom_max_", 11))
       {
         mdb.size_name   = NULL;
         dinfo->max_size = mdb;
@@ -1247,7 +2240,7 @@ cups_create_media_db(
       {
         mdb.size_name = val->string.text;
 
-        cupsArrayAdd(dinfo->media_db, &mdb);
+        cupsArrayAdd(db, &mdb);
       }
     }
   }
@@ -1284,11 +2277,13 @@ cups_free_media_db(
  */
 
 static int                             /* O - 1 on match, 0 on failure */
-cups_get_media_db(cups_dinfo_t *dinfo, /* I - Destination information */
-                  _pwg_media_t *pwg,   /* I - PWG media info */
+cups_get_media_db(http_t       *http,  /* I - Connection to destination */
+                  cups_dinfo_t *dinfo, /* I - Destination information */
+                  pwg_media_t  *pwg,   /* I - PWG media info */
                   unsigned     flags,  /* I - Media matching flags */
-                  cups_size_t *size)   /* O - Media size/margin/name info */
+                  cups_size_t  *size)  /* O - Media size/margin/name info */
 {
+  cups_array_t         *db;            /* Which media database to query */
   _cups_media_db_t     *mdb,           /* Current media database entry */
                        *best = NULL,   /* Best matching entry */
                        key;            /* Search key */
@@ -1298,8 +2293,18 @@ cups_get_media_db(cups_dinfo_t *dinfo,   /* I - Destination information */
   * Create the media database as needed...
   */
 
-  if (!dinfo->media_db)
-    cups_create_media_db(dinfo);
+  if (flags & CUPS_MEDIA_FLAGS_READY)
+  {
+    cups_update_ready(http, dinfo);
+    db = dinfo->ready_db;
+  }
+  else
+  {
+    if (!dinfo->media_db)
+      cups_create_media_db(dinfo, CUPS_MEDIA_FLAGS_DEFAULT);
+
+    db = dinfo->media_db;
+  }
 
  /*
   * Find a match...
@@ -1309,7 +2314,7 @@ cups_get_media_db(cups_dinfo_t *dinfo,    /* I - Destination information */
   key.width  = pwg->width;
   key.length = pwg->length;
 
-  if ((mdb = cupsArrayFind(dinfo->media_db, &key)) != NULL)
+  if ((mdb = cupsArrayFind(db, &key)) != NULL)
   {
    /*
     * Found an exact match, let's figure out the best margins for the flags
@@ -1324,12 +2329,11 @@ cups_get_media_db(cups_dinfo_t *dinfo,  /* I - Destination information */
       * Look for the smallest margins...
       */
 
-      if (best->left != 0 || best->right != 0 || best->top != 0 ||
-          best->bottom != 0)
+      if (best->left != 0 || best->right != 0 || best->top != 0 || best->bottom != 0)
       {
-       for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
+       for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
             mdb && !cups_compare_media_db(mdb, &key);
-            mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
+            mdb = (_cups_media_db_t *)cupsArrayNext(db))
        {
          if (mdb->left <= best->left && mdb->right <= best->right &&
              mdb->top <= best->top && mdb->bottom <= best->bottom)
@@ -1357,12 +2361,13 @@ cups_get_media_db(cups_dinfo_t *dinfo,  /* I - Destination information */
       * Look for the largest margins...
       */
 
-      for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
+      for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
           mdb && !cups_compare_media_db(mdb, &key);
-          mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
+          mdb = (_cups_media_db_t *)cupsArrayNext(db))
       {
        if (mdb->left >= best->left && mdb->right >= best->right &&
-           mdb->top >= best->top && mdb->bottom >= best->bottom)
+           mdb->top >= best->top && mdb->bottom >= best->bottom &&
+           (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
          best = mdb;
       }
     }
@@ -1372,16 +2377,15 @@ cups_get_media_db(cups_dinfo_t *dinfo,  /* I - Destination information */
       * Look for the smallest non-zero margins...
       */
 
-      for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
+      for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
           mdb && !cups_compare_media_db(mdb, &key);
-          mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
+          mdb = (_cups_media_db_t *)cupsArrayNext(db))
       {
        if (((mdb->left > 0 && mdb->left <= best->left) || best->left == 0) &&
-           ((mdb->right > 0 && mdb->right <= best->right) ||
-            best->right == 0) &&
+           ((mdb->right > 0 && mdb->right <= best->right) || best->right == 0) &&
            ((mdb->top > 0 && mdb->top <= best->top) || best->top == 0) &&
-           ((mdb->bottom > 0 && mdb->bottom <= best->bottom) ||
-            best->bottom == 0))
+           ((mdb->bottom > 0 && mdb->bottom <= best->bottom) || best->bottom == 0) &&
+           (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
          best = mdb;
       }
     }
@@ -1434,9 +2438,9 @@ cups_get_media_db(cups_dinfo_t *dinfo,    /* I - Destination information */
     * Find a close size...
     */
 
-    for (mdb = (_cups_media_db_t *)cupsArrayFirst(dinfo->media_db);
+    for (mdb = (_cups_media_db_t *)cupsArrayFirst(db);
          mdb;
-         mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
+         mdb = (_cups_media_db_t *)cupsArrayNext(db))
       if (cups_is_close_media_db(mdb, &key))
         break;
 
@@ -1454,12 +2458,13 @@ cups_get_media_db(cups_dinfo_t *dinfo,  /* I - Destination information */
       if (best->left != 0 || best->right != 0 || best->top != 0 ||
           best->bottom != 0)
       {
-       for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
+       for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
             mdb && cups_is_close_media_db(mdb, &key);
-            mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
+            mdb = (_cups_media_db_t *)cupsArrayNext(db))
        {
          if (mdb->left <= best->left && mdb->right <= best->right &&
-             mdb->top <= best->top && mdb->bottom <= best->bottom)
+             mdb->top <= best->top && mdb->bottom <= best->bottom &&
+             (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
          {
            best = mdb;
            if (mdb->left == 0 && mdb->right == 0 && mdb->bottom == 0 &&
@@ -1475,12 +2480,13 @@ cups_get_media_db(cups_dinfo_t *dinfo,  /* I - Destination information */
       * Look for the largest margins...
       */
 
-      for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
+      for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
           mdb && cups_is_close_media_db(mdb, &key);
-          mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
+          mdb = (_cups_media_db_t *)cupsArrayNext(db))
       {
        if (mdb->left >= best->left && mdb->right >= best->right &&
-           mdb->top >= best->top && mdb->bottom >= best->bottom)
+           mdb->top >= best->top && mdb->bottom >= best->bottom &&
+           (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
          best = mdb;
       }
     }
@@ -1490,16 +2496,17 @@ cups_get_media_db(cups_dinfo_t *dinfo,  /* I - Destination information */
       * Look for the smallest non-zero margins...
       */
 
-      for (mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db);
+      for (mdb = (_cups_media_db_t *)cupsArrayNext(db);
           mdb && cups_is_close_media_db(mdb, &key);
-          mdb = (_cups_media_db_t *)cupsArrayNext(dinfo->media_db))
+          mdb = (_cups_media_db_t *)cupsArrayNext(db))
       {
        if (((mdb->left > 0 && mdb->left <= best->left) || best->left == 0) &&
            ((mdb->right > 0 && mdb->right <= best->right) ||
             best->right == 0) &&
            ((mdb->top > 0 && mdb->top <= best->top) || best->top == 0) &&
            ((mdb->bottom > 0 && mdb->bottom <= best->bottom) ||
-            best->bottom == 0))
+            best->bottom == 0) &&
+           (mdb->bottom != best->bottom || mdb->left != best->left || mdb->right != best->right || mdb->top != best->top))
          best = mdb;
       }
     }
@@ -1511,12 +2518,14 @@ cups_get_media_db(cups_dinfo_t *dinfo,  /* I - Destination information */
     * Return the matching size...
     */
 
-    if (best->size_name)
-      strlcpy(size->media, best->size_name, sizeof(size->media));
-    else if (best->key)
+    if (best->key)
       strlcpy(size->media, best->key, sizeof(size->media));
-    else
+    else if (best->size_name)
+      strlcpy(size->media, best->size_name, sizeof(size->media));
+    else if (pwg && pwg->pwg)
       strlcpy(size->media, pwg->pwg, sizeof(size->media));
+    else
+      strlcpy(size->media, "unknown", sizeof(size->media));
 
     size->width  = best->width;
     size->length = best->length;
@@ -1558,8 +2567,6 @@ cups_is_close_media_db(
 
 /*
  * 'cups_test_constraints()' - Test constraints.
- *
- * TODO: STR #4096 - Need to properly support media-col contraints...
  */
 
 static cups_array_t *                  /* O - Active constraints */
@@ -1573,11 +2580,13 @@ cups_test_constraints(
     cups_option_t **conflicts)         /* O - Conflicting options */
 {
   int                  i,              /* Looping var */
+                       count,          /* Number of values */
                        match;          /* Value matches? */
   int                  num_matching;   /* Number of matching options */
   cups_option_t                *matching;      /* Matching options */
   _cups_dconstres_t    *c;             /* Current constraint */
   cups_array_t         *active = NULL; /* Active constraints */
+  ipp_t                        *col;           /* Collection value */
   ipp_attribute_t      *attr;          /* Current attribute */
   _ipp_value_t         *attrval;       /* Current attribute value */
   const char           *value;         /* Current value */
@@ -1599,17 +2608,13 @@ cups_test_constraints(
          attr;
          attr = ippNextAttribute(c->collection))
     {
-      if (attr->value_tag == IPP_TAG_BEGIN_COLLECTION)
-        break;                         /* TODO: STR #4096 */
-
      /*
       * Get the value for the current attribute in the constraint...
       */
 
       if (new_option && new_value && !strcmp(attr->name, new_option))
         value = new_value;
-      else if ((value = cupsGetOption(attr->name, num_options,
-                                      options)) == NULL)
+      else if ((value = cupsGetOption(attr->name, num_options, options)) == NULL)
         value = cupsGetOption(attr->name, dinfo->num_defaults, dinfo->defaults);
 
       if (!value)
@@ -1724,6 +2729,22 @@ cups_test_constraints(
             }
            break;
 
+        case IPP_TAG_BEGIN_COLLECTION :
+            col = ippNew();
+            _cupsEncodeOption(col, IPP_TAG_ZERO, NULL, ippGetName(attr), value);
+
+            for (i = 0, count = ippGetCount(attr); i < count; i ++)
+            {
+              if (cups_collection_contains(col, ippGetCollection(attr, i)))
+              {
+                match = 1;
+                break;
+             }
+            }
+
+            ippDelete(col);
+            break;
+
         default :
             break;
       }
@@ -1746,8 +2767,7 @@ cups_test_constraints(
         cups_option_t  *moption;       /* Matching option */
 
         for (i = num_matching, moption = matching; i > 0; i --, moption ++)
-          *num_conflicts = cupsAddOption(moption->name, moption->value,
-                                        *num_conflicts, conflicts);
+          *num_conflicts = cupsAddOption(moption->name, moption->value, *num_conflicts, conflicts);
       }
     }
 
@@ -1759,5 +2779,73 @@ cups_test_constraints(
 
 
 /*
- * End of "$Id$".
+ * 'cups_update_ready()' - Update xxx-ready attributes for the printer.
  */
+
+static void
+cups_update_ready(http_t       *http,  /* I - Connection to destination */
+                  cups_dinfo_t *dinfo) /* I - Destination information */
+{
+  ipp_t        *request;                       /* Get-Printer-Attributes request */
+  static const char * const pattrs[] = /* Printer attributes we want */
+  {
+    "finishings-col-ready",
+    "finishings-ready",
+    "job-finishings-col-ready",
+    "job-finishings-ready",
+    "media-col-ready",
+    "media-ready"
+  };
+
+
+ /*
+  * Don't update more than once every 30 seconds...
+  */
+
+  if ((time(NULL) - dinfo->ready_time) < _CUPS_MEDIA_READY_TTL)
+    return;
+
+ /*
+  * Free any previous results...
+  */
+
+  if (dinfo->cached_flags & CUPS_MEDIA_FLAGS_READY)
+  {
+    cupsArrayDelete(dinfo->cached_db);
+    dinfo->cached_db    = NULL;
+    dinfo->cached_flags = CUPS_MEDIA_FLAGS_DEFAULT;
+  }
+
+  ippDelete(dinfo->ready_attrs);
+  dinfo->ready_attrs = NULL;
+
+  cupsArrayDelete(dinfo->ready_db);
+  dinfo->ready_db = NULL;
+
+ /*
+  * Query the xxx-ready values...
+  */
+
+  request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
+  ippSetVersion(request, dinfo->version / 10, dinfo->version % 10);
+
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
+               dinfo->uri);
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
+               NULL, cupsUser());
+  ippAddStrings(request, IPP_TAG_OPERATION, IPP_CONST_TAG(IPP_TAG_KEYWORD), "requested-attributes", (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
+
+  dinfo->ready_attrs = cupsDoRequest(http, request, dinfo->resource);
+
+ /*
+  * Update the ready media database...
+  */
+
+  cups_create_media_db(dinfo, CUPS_MEDIA_FLAGS_READY);
+
+ /*
+  * Update last lookup time and return...
+  */
+
+  dinfo->ready_time = time(NULL);
+}