]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/getdevices.c
Merge changes from CUPS 1.4svn-r8606.
[thirdparty/cups.git] / cups / getdevices.c
index 6843e5afd12562c07deedfdd5cd43e4104798c9b..81f885912d40b4a373ae4064df46e68d54edce87 100644 (file)
@@ -3,7 +3,7 @@
  *
  *   cupsGetDevices implementation for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2008 by Apple Inc.
+ *   Copyright 2008-2009 by Apple Inc.
  *
  *   These coded instructions, statements, and computer programs are the
  *   property of Apple Inc. and are protected by Federal copyright
@@ -15,6 +15,7 @@
  *
  * Contents:
  *
+ *   cupsGetDevices() - Get available printer devices.
  */
 
 /*
@@ -30,8 +31,9 @@
  *
  * This function sends a CUPS-Get-Devices request and streams the discovered
  * devices to the specified callback function. The "timeout" parameter controls
- * how long the request lasts, while the "exclude_schemes" parameter provides
- * a comma-delimited list of backends to omit from the request.
+ * how long the request lasts, while the "include_schemes" and "exclude_schemes"
+ * parameters provide comma-delimited lists of backends to include or omit from
+ * the request respectively.
  *
  * @since CUPS 1.4@
  */
@@ -40,6 +42,7 @@ ipp_status_t                          /* O - Request status - @code IPP_OK@ on success. */
 cupsGetDevices(
     http_t           *http,            /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
     int              timeout,          /* I - Timeout in seconds or @code CUPS_TIMEOUT_DEFAULT@ */
+    const char       *include_schemes, /* I - Comma-separated URI schemes to include or @code CUPS_INCLUDE_ALL@ */
     const char       *exclude_schemes, /* I - Comma-separated URI schemes to exclude or @code CUPS_EXCLUDE_NONE@ */
     cups_device_cb_t callback,         /* I - Callback function */
     void             *user_data)       /* I - User data pointer */
@@ -50,10 +53,11 @@ cupsGetDevices(
   const char   *device_class,          /* device-class value */
                *device_id,             /* device-id value */
                *device_info,           /* device-info value */
+               *device_location,       /* device-location value */
                *device_make_and_model, /* device-make-and-model value */
                *device_uri;            /* device-uri value */
   int          blocking;               /* Current blocking-IO mode */
-  cups_option_t        option;                 /* exclude-schemes option */
+  cups_option_t        option;                 /* in/exclude-schemes option */
   http_status_t        status;                 /* HTTP status of request */
   ipp_state_t  state;                  /* IPP response state */
 
@@ -62,6 +66,11 @@ cupsGetDevices(
   * Range check input...
   */
 
+  DEBUG_printf(("cupsGetDevices(http=%p, timeout=%d, include_schemes=\"%s\", "
+                "exclude_schemes=\"%s\", callback=%p, user_data=%p)", http,
+               timeout, include_schemes, exclude_schemes, callback,
+               user_data));
+
   if (!callback)
     return (IPP_INTERNAL_ERROR);
 
@@ -81,6 +90,14 @@ cupsGetDevices(
     ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "timeout",
                   timeout);
 
+  if (include_schemes)
+  {
+    option.name  = "include-schemes";
+    option.value = (char *)include_schemes;
+
+    cupsEncodeOptions2(request, 1, &option, IPP_TAG_OPERATION);
+  }
+
   if (exclude_schemes)
   {
     option.name  = "exclude-schemes";
@@ -95,10 +112,10 @@ cupsGetDevices(
 
   do
   {
-    DEBUG_puts("cupsGetDevices: Sending request...");
+    DEBUG_puts("2cupsGetDevices: Sending request...");
     status = cupsSendRequest(http, request, "/", ippLength(request));
 
-    DEBUG_puts("cupsGetDevices: Waiting for response status...");
+    DEBUG_puts("2cupsGetDevices: Waiting for response status...");
     while (status == HTTP_CONTINUE)
       status = httpUpdate(http);
 
@@ -112,14 +129,15 @@ cupsGetDevices(
        * See if we can do authentication...
        */
 
-       int auth_result;
+       DEBUG_puts("2cupsGetDevices: Need authorization...");
 
-       DEBUG_puts("cupsGetDevices: Need authorization...");
-
-       if ((auth_result = cupsDoAuthentication(http, "POST", "/")) == 0)
+       if (!cupsDoAuthentication(http, "POST", "/"))
          httpReconnect(http);
-       else if (auth_result < 0)
-         http->status = status = HTTP_FORBIDDEN;
+       else
+       {
+         status = HTTP_AUTHORIZATION_CANCELED;
+         break;
+       }
       }
 
 #ifdef HAVE_SSL
@@ -129,7 +147,7 @@ cupsGetDevices(
        * Force a reconnect with encryption...
        */
 
-       DEBUG_puts("cupsGetDevices: Need encryption...");
+       DEBUG_puts("2cupsGetDevices: Need encryption...");
 
        if (!httpReconnect(http))
          httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
@@ -139,7 +157,7 @@ cupsGetDevices(
   }
   while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED);
 
-  DEBUG_printf(("cupsGetDevices: status=%d\n", status));
+  DEBUG_printf(("2cupsGetDevices: status=%d", status));
 
   ippDelete(request);
 
@@ -160,18 +178,19 @@ cupsGetDevices(
   device_class          = NULL;
   device_id             = NULL;
   device_info           = NULL;
+  device_location       = "";
   device_make_and_model = NULL;
   device_uri            = NULL;
   attr                  = NULL;
 
-  DEBUG_puts("cupsGetDevices: Reading response...");
+  DEBUG_puts("2cupsGetDevices: Reading response...");
 
   do
   {
     if ((state = ippRead(http, response)) == IPP_ERROR)
       break;
 
-    DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state,
+    DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state,
                   response->last));
 
     if (!response->attrs)
@@ -184,19 +203,21 @@ cupsGetDevices(
       else
         attr = attr->next;
 
-      DEBUG_printf(("cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d\n",
-                    attr->name ? attr->name : "(null)", attr->value_tag));
+      DEBUG_printf(("2cupsGetDevices: attr->name=\"%s\", attr->value_tag=%d",
+                    attr->name, attr->value_tag));
 
       if (!attr->name)
       {
         if (device_class && device_id && device_info && device_make_and_model &&
            device_uri)
           (*callback)(device_class, device_id, device_info,
-                     device_make_and_model, device_uri, user_data);
+                     device_make_and_model, device_uri, device_location,
+                     user_data);
 
        device_class          = NULL;
        device_id             = NULL;
        device_info           = NULL;
+       device_location       = "";
        device_make_and_model = NULL;
        device_uri            = NULL;
       }
@@ -209,6 +230,9 @@ cupsGetDevices(
       else if (!strcmp(attr->name, "device-info") &&
                attr->value_tag == IPP_TAG_TEXT)
         device_info = attr->values[0].string.text;
+      else if (!strcmp(attr->name, "device-location") &&
+               attr->value_tag == IPP_TAG_TEXT)
+        device_location = attr->values[0].string.text;
       else if (!strcmp(attr->name, "device-make-and-model") &&
                attr->value_tag == IPP_TAG_TEXT)
         device_make_and_model = attr->values[0].string.text;
@@ -219,33 +243,34 @@ cupsGetDevices(
   }
   while (state != IPP_DATA);
 
-  DEBUG_printf(("cupsGetDevices: state=%d, response->last=%p\n", state,
+  DEBUG_printf(("2cupsGetDevices: state=%d, response->last=%p", state,
                response->last));
 
   if (device_class && device_id && device_info && device_make_and_model &&
       device_uri)
     (*callback)(device_class, device_id, device_info,
-               device_make_and_model, device_uri, user_data);
+               device_make_and_model, device_uri, device_location, user_data);
 
  /*
   * Set the IPP status and return...
   */
 
   httpBlocking(http, blocking);
+  httpFlush(http);
 
   if (status == IPP_ERROR)
-    _cupsSetError(IPP_ERROR, NULL);
+    _cupsSetError(IPP_ERROR, NULL, 0);
   else
   {
     attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
 
-    DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"\n",
+    DEBUG_printf(("cupsGetDevices: status-code=%s, status-message=\"%s\"",
                  ippErrorString(response->request.status.status_code),
                  attr ? attr->values[0].string.text : ""));
 
     _cupsSetError(response->request.status.status_code,
-                  attr ? attr->values[0].string.text :
-                      ippErrorString(response->request.status.status_code));
+                 attr ? attr->values[0].string.text :
+                     ippErrorString(response->request.status.status_code), 0);
   }
 
   ippDelete(response);