/*
- * "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $"
+ * "$Id: dest.c 9568 2011-02-25 06:13:56Z mike $"
*
- * User-defined destination (and option) support for the Common UNIX
- * Printing System (CUPS).
+ * User-defined destination (and option) support for CUPS.
*
- * Copyright 2007-2008 by Apple Inc.
+ * Copyright 2007-2011 by Apple Inc.
* Copyright 1997-2007 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
*
* Contents:
*
- * cupsAddDest() - Add a destination to the list of destinations.
- * cupsFreeDests() - Free the memory used by the list of destinations.
- * cupsGetDest() - Get the named destination from the list.
- * cupsGetDests() - Get the list of destinations from the default
- * server.
- * cupsGetDests2() - Get the list of destinations from the specified
- * server.
- * cupsGetNamedDest() - Get options for the named destination.
- * cupsRemoveDest() - Remove a destination from the destination list.
- * cupsSetDefaultDest() - Set the default destination.
- * cupsSetDests() - Save the list of destinations for the default
- * server.
- * cupsSetDests2() - Save the list of destinations for the specified
- * server.
- * appleCopyLocations() - Get the location history array.
- * appleCopyNetwork() - Get the network ID for the current location.
- * appleGetDefault() - Get the default printer for this location.
- * appleGetPrinter() - Get a printer from the history array.
- * appleSetDefault() - Set the default printer for this location.
- * appleUseLastPrinter() - Get the default printer preference value.
- * cups_get_default() - Get the default destination from an lpoptions file.
- * cups_get_dests() - Get destinations from a file.
- * cups_get_sdests() - Get destinations from a server.
+ * cupsAddDest() - Add a destination to the list of
+ * destinations.
+ * _cupsAppleCopyDefaultPaperID() - Get the default paper ID.
+ * _cupsAppleCopyDefaultPrinter() - Get the default printer at this location.
+ * _cupsAppleGetUseLastPrinter() - Get whether to use the last used printer.
+ * _cupsAppleSetDefaultPaperID() - Set the default paper id.
+ * _cupsAppleSetDefaultPrinter() - Set the default printer for this location.
+ * _cupsAppleSetUseLastPrinter() - Set whether to use the last used printer.
+ * cupsFreeDests() - Free the memory used by the list of
+ * destinations.
+ * cupsGetDest() - Get the named destination from the list.
+ * _cupsGetDests() - Get destinations from a server.
+ * cupsGetDests() - Get the list of destinations from the
+ * default server.
+ * cupsGetDests2() - Get the list of destinations from the
+ * specified server.
+ * cupsGetNamedDest() - Get options for the named destination.
+ * cupsRemoveDest() - Remove a destination from the destination
+ * list.
+ * cupsSetDefaultDest() - Set the default destination.
+ * cupsSetDests() - Save the list of destinations for the
+ * default server.
+ * cupsSetDests2() - Save the list of destinations for the
+ * specified server.
+ * _cupsUserDefault() - Get the user default printer from
+ * environment variables and location
+ * information.
+ * appleCopyLocations() - Copy the location history array.
+ * appleCopyNetwork() - Get the network ID for the current
+ * location.
+ * appleGetPaperSize() - Get the default paper size.
+ * appleGetPrinter() - Get a printer from the history array.
+ * cups_add_dest() - Add a destination to the array.
+ * cups_compare_dests() - Compare two destinations.
+ * cups_find_dest() - Find a destination using a binary search.
+ * cups_get_default() - Get the default destination from an
+ * lpoptions file.
+ * cups_get_dests() - Get destinations from a file.
+ * cups_make_string() - Make a comma-separated string of values
+ * from an IPP attribute.
*/
/*
* Include necessary headers...
*/
-#include "debug.h"
-#include "globals.h"
-#include <stdlib.h>
-#include <ctype.h>
+#include "cups-private.h"
#include <sys/stat.h>
#ifdef HAVE_NOTIFY_H
#endif /* HAVE_NOTIFY_H */
#ifdef __APPLE__
-# include <sys/cdefs.h>
-# include <CoreFoundation/CoreFoundation.h>
# include <SystemConfiguration/SystemConfiguration.h>
-# define kLocationHistoryArrayKey CFSTR("kLocationHistoryArrayKeyTMP")
-# define kLocationNetworkKey CFSTR("kLocationNetworkKey")
-# define kLocationPrinterIDKey CFSTR("kLocationPrinterIDKey")
-# define kPMPrintingPreferences CFSTR("com.apple.print.PrintingPrefs")
-# define kUseLastPrinterAsCurrentPrinterKey CFSTR("UseLastPrinterAsCurrentPrinter")
+# define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
+# define kDefaultPaperIDKey CFSTR("DefaultPaperID")
+# define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
+# define kLocationNetworkKey CFSTR("Network")
+# define kLocationPrinterIDKey CFSTR("PrinterID")
+# define kUseLastPrinter CFSTR("UseLastPrinter")
#endif /* __APPLE__ */
#ifdef __APPLE__
static CFArrayRef appleCopyLocations(void);
static CFStringRef appleCopyNetwork(void);
-static char *appleGetDefault(char *name, int namesize);
+static char *appleGetPaperSize(char *name, int namesize);
static CFStringRef appleGetPrinter(CFArrayRef locations, CFStringRef network,
CFIndex *locindex);
-static void appleSetDefault(const char *name);
-static int appleUseLastPrinter(void);
#endif /* __APPLE__ */
+static cups_dest_t *cups_add_dest(const char *name, const char *instance,
+ int *num_dests, cups_dest_t **dests);
+static int cups_compare_dests(cups_dest_t *a, cups_dest_t *b);
+static int cups_find_dest(const char *name, const char *instance,
+ int num_dests, cups_dest_t *dests, int prev,
+ int *rdiff);
static char *cups_get_default(const char *filename, char *namebuf,
- size_t namesize, const char **instance);
+ size_t namesize, const char **instance);
static int cups_get_dests(const char *filename, const char *match_name,
- const char *match_inst, int num_dests,
- cups_dest_t **dests);
-static int cups_get_sdests(http_t *http, ipp_op_t op, const char *name,
- int num_dests, cups_dest_t **dests);
+ const char *match_inst, int user_default_set,
+ int num_dests, cups_dest_t **dests);
+static char *cups_make_string(ipp_attribute_t *attr, char *buffer,
+ size_t bufsize);
/*
{
int i; /* Looping var */
cups_dest_t *dest; /* Destination pointer */
- cups_dest_t *parent; /* Parent destination */
- cups_option_t *option; /* Current option */
+ cups_dest_t *parent = NULL; /* Parent destination */
+ cups_option_t *doption, /* Current destination option */
+ *poption; /* Current parent option */
if (!name || !dests)
return (0);
- if (cupsGetDest(name, instance, num_dests, *dests))
- return (num_dests);
+ if (!cupsGetDest(name, instance, num_dests, *dests))
+ {
+ if (instance && !cupsGetDest(name, NULL, num_dests, *dests))
+ return (num_dests);
+
+ dest = cups_add_dest(name, instance, &num_dests, dests);
+
+ /*
+ * Find the base dest again now the array has been realloc'd.
+ */
+
+ parent = cupsGetDest(name, NULL, num_dests, *dests);
+
+ if (instance && parent && parent->num_options > 0)
+ {
+ /*
+ * Copy options from parent...
+ */
+
+ dest->options = calloc(sizeof(cups_option_t), parent->num_options);
+
+ if (dest->options)
+ {
+ dest->num_options = parent->num_options;
+
+ for (i = dest->num_options, doption = dest->options,
+ poption = parent->options;
+ i > 0;
+ i --, doption ++, poption ++)
+ {
+ doption->name = _cupsStrRetain(poption->name);
+ doption->value = _cupsStrRetain(poption->value);
+ }
+ }
+ }
+ }
+
+ return (num_dests);
+}
+
+
+#ifdef __APPLE__
+/*
+ * '_cupsAppleCopyDefaultPaperID()' - Get the default paper ID.
+ */
+
+CFStringRef /* O - Default paper ID */
+_cupsAppleCopyDefaultPaperID(void)
+{
+ return (CFPreferencesCopyAppValue(kDefaultPaperIDKey,
+ kCUPSPrintingPrefs));
+}
+
+
+/*
+ * '_cupsAppleCopyDefaultPrinter()' - Get the default printer at this location.
+ */
+
+CFStringRef /* O - Default printer name */
+_cupsAppleCopyDefaultPrinter(void)
+{
+ CFStringRef network; /* Network location */
+ CFArrayRef locations; /* Location array */
+ CFStringRef locprinter; /* Current printer */
+
/*
- * Add new destination...
+ * Use location-based defaults only if "use last printer" is selected in the
+ * system preferences...
*/
- if (num_dests == 0)
- dest = malloc(sizeof(cups_dest_t));
- else
- dest = realloc(*dests, sizeof(cups_dest_t) * (num_dests + 1));
+ if (!_cupsAppleGetUseLastPrinter())
+ {
+ DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Not using last printer as "
+ "default.");
+ return (NULL);
+ }
- if (dest == NULL)
- return (num_dests);
+ /*
+ * Get the current location...
+ */
- *dests = dest;
+ if ((network = appleCopyNetwork()) == NULL)
+ {
+ DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Unable to get current "
+ "network.");
+ return (NULL);
+ }
+
+//# ifdef DEBUG
+// CFStringGetCString(network, name, namesize, kCFStringEncodingUTF8);
+// DEBUG_printf(("2_cupsUserDefault: network=\"%s\"", name));
+//# endif /* DEBUG */
/*
- * Find where to insert the destination...
+ * Lookup the network in the preferences...
*/
- for (i = num_dests; i > 0; i --, dest ++)
- if (strcasecmp(name, dest->name) < 0)
- break;
- else if (!instance && dest->instance)
- break;
- else if (!strcasecmp(name, dest->name) &&
- instance && dest->instance &&
- strcasecmp(instance, dest->instance) < 0)
- break;
+ if ((locations = appleCopyLocations()) == NULL)
+ {
+ /*
+ * Missing or bad location array, so no location-based default...
+ */
+
+ DEBUG_puts("1_cupsAppleCopyDefaultPrinter: Missing or bad last used "
+ "printer array.");
+
+ CFRelease(network);
+
+ return (NULL);
+ }
+
+ DEBUG_printf(("1_cupsAppleCopyDefaultPrinter: Got locations, %d entries.",
+ (int)CFArrayGetCount(locations)));
+
+ if ((locprinter = appleGetPrinter(locations, network, NULL)) != NULL)
+ CFRetain(locprinter);
+
+ CFRelease(network);
+ CFRelease(locations);
+
+ return (locprinter);
+}
+
+
+/*
+ * '_cupsAppleGetUseLastPrinter()' - Get whether to use the last used printer.
+ */
+
+int /* O - 1 to use last printer, 0 otherwise */
+_cupsAppleGetUseLastPrinter(void)
+{
+ Boolean uselast, /* Use last printer preference value */
+ uselast_set; /* Valid is set? */
+
+
+ if (getenv("CUPS_DISABLE_APPLE_DEFAULT"))
+ return (0);
+
+ uselast = CFPreferencesGetAppBooleanValue(kUseLastPrinter,
+ kCUPSPrintingPrefs,
+ &uselast_set);
+ if (!uselast_set)
+ return (1);
+ else
+ return (uselast);
+}
+
+
+/*
+ * '_cupsAppleSetDefaultPaperID()' - Set the default paper id.
+ */
+
+void
+_cupsAppleSetDefaultPaperID(
+ CFStringRef name) /* I - New paper ID */
+{
+ CFPreferencesSetAppValue(kDefaultPaperIDKey, name, kCUPSPrintingPrefs);
+ CFPreferencesAppSynchronize(kCUPSPrintingPrefs);
+ notify_post("com.apple.printerPrefsChange");
+}
+
+
+/*
+ * '_cupsAppleSetDefaultPrinter()' - Set the default printer for this location.
+ */
+
+void
+_cupsAppleSetDefaultPrinter(
+ CFStringRef name) /* I - Default printer/class name */
+{
+ CFStringRef network; /* Current network */
+ CFArrayRef locations; /* Old locations array */
+ CFIndex locindex; /* Index in locations array */
+ CFStringRef locprinter; /* Current printer */
+ CFMutableArrayRef newlocations; /* New locations array */
+ CFMutableDictionaryRef newlocation; /* New location */
- if (i > 0)
- memmove(dest + 1, dest, i * sizeof(cups_dest_t));
/*
- * Initialize the destination...
+ * Get the current location...
*/
- dest->name = _cupsStrAlloc(name);
- dest->is_default = 0;
- dest->num_options = 0;
- dest->options = (cups_option_t *)0;
+ if ((network = appleCopyNetwork()) == NULL)
+ {
+ DEBUG_puts("1_cupsAppleSetDefaultPrinter: Unable to get current network...");
+ return;
+ }
- if (!instance)
- dest->instance = NULL;
+ /*
+ * Lookup the network in the preferences...
+ */
+
+ if ((locations = appleCopyLocations()) != NULL)
+ locprinter = appleGetPrinter(locations, network, &locindex);
else
+ {
+ locprinter = NULL;
+ locindex = -1;
+ }
+
+ if (!locprinter || CFStringCompare(locprinter, name, 0) != kCFCompareEqualTo)
{
/*
- * Copy options from the primary instance...
+ * Need to change the locations array...
*/
- dest->instance = _cupsStrAlloc(instance);
+ if (locations)
+ {
+ newlocations = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0,
+ locations);
- if ((parent = cupsGetDest(name, NULL, num_dests + 1, *dests)) != NULL)
+ if (locprinter)
+ CFArrayRemoveValueAtIndex(newlocations, locindex);
+ }
+ else
+ newlocations = CFArrayCreateMutable(kCFAllocatorDefault, 0,
+ &kCFTypeArrayCallBacks);
+
+ newlocation = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
+ &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+
+ if (newlocation && newlocations)
{
- for (i = parent->num_options, option = parent->options;
- i > 0;
- i --, option ++)
- dest->num_options = cupsAddOption(option->name, option->value,
- dest->num_options,
- &(dest->options));
+ /*
+ * Put the new location at the front of the array...
+ */
+
+ CFDictionaryAddValue(newlocation, kLocationNetworkKey, network);
+ CFDictionaryAddValue(newlocation, kLocationPrinterIDKey, name);
+ CFArrayInsertValueAtIndex(newlocations, 0, newlocation);
+
+ /*
+ * Limit the number of locations to 10...
+ */
+
+ while (CFArrayGetCount(newlocations) > 10)
+ CFArrayRemoveValueAtIndex(newlocations, 10);
+
+ /*
+ * Push the changes out...
+ */
+
+ CFPreferencesSetAppValue(kLastUsedPrintersKey, newlocations,
+ kCUPSPrintingPrefs);
+ CFPreferencesAppSynchronize(kCUPSPrintingPrefs);
+ notify_post("com.apple.printerPrefsChange");
}
+
+ if (newlocations)
+ CFRelease(newlocations);
+
+ if (newlocation)
+ CFRelease(newlocation);
}
- return (num_dests + 1);
+ if (locations)
+ CFRelease(locations);
+
+ CFRelease(network);
+}
+
+
+/*
+ * '_cupsAppleSetUseLastPrinter()' - Set whether to use the last used printer.
+ */
+
+void
+_cupsAppleSetUseLastPrinter(
+ int uselast) /* O - 1 to use last printer, 0 otherwise */
+{
+ CFPreferencesSetAppValue(kUseLastPrinter,
+ uselast ? kCFBooleanTrue : kCFBooleanFalse,
+ kCUPSPrintingPrefs);
+ CFPreferencesAppSynchronize(kCUPSPrintingPrefs);
+ notify_post("com.apple.printerPrefsChange");
}
+#endif /* __APPLE__ */
/*
int num_dests, /* I - Number of destinations */
cups_dest_t *dests) /* I - Destinations */
{
- int comp; /* Result of comparison */
+ int diff, /* Result of comparison */
+ match; /* Matching index */
+
+
+ if (num_dests <= 0 || !dests)
+ return (NULL);
+
+ if (!name)
+ {
+ /*
+ * NULL name for default printer.
+ */
+
+ while (num_dests > 0)
+ {
+ if (dests->is_default)
+ return (dests);
+
+ num_dests --;
+ dests ++;
+ }
+ }
+ else
+ {
+ /*
+ * Lookup name and optionally the instance...
+ */
+
+ match = cups_find_dest(name, instance, num_dests, dests, -1, &diff);
+
+ if (!diff)
+ return (dests + match);
+ }
+
+ return (NULL);
+}
+
+
+/*
+ * '_cupsGetDests()' - Get destinations from a server.
+ *
+ * "op" is CUPS_GET_PRINTERS to get a full list, CUPS_GET_DEFAULT to get the
+ * system-wide default printer, or IPP_GET_PRINTER_ATTRIBUTES for a known
+ * printer.
+ *
+ * "name" is the name of an existing printer and is only used when "op" is
+ * IPP_GET_PRINTER_ATTRIBUTES.
+ *
+ * "dest" is initialized to point to the array of destinations.
+ *
+ * 0 is returned if there are no printers, no default printer, or the named
+ * printer does not exist, respectively.
+ *
+ * Free the memory used by the destination array using the @link cupsFreeDests@
+ * function.
+ *
+ * Note: On Mac OS X this function also gets the default paper from the system
+ * preferences (~/L/P/org.cups.PrintingPrefs.plist) and includes it in the
+ * options array for each destination that supports it.
+ */
+
+int /* O - Number of destinations */
+_cupsGetDests(http_t *http, /* I - Connection to server or CUPS_HTTP_DEFAULT */
+ ipp_op_t op, /* I - IPP operation */
+ const char *name, /* I - Name of destination */
+ cups_dest_t **dests) /* IO - Destinations */
+{
+ int num_dests = 0; /* Number of destinations */
+ cups_dest_t *dest; /* Current destination */
+ ipp_t *request, /* IPP Request */
+ *response; /* IPP Response */
+ ipp_attribute_t *attr; /* Current attribute */
+ const char *printer_name; /* printer-name attribute */
+ char uri[1024]; /* printer-uri value */
+ int num_options; /* Number of options */
+ cups_option_t *options; /* Options */
+#ifdef __APPLE__
+ char media_default[41]; /* Default paper size */
+#endif /* __APPLE__ */
+ char optname[1024], /* Option name */
+ value[2048], /* Option value */
+ *ptr; /* Pointer into name/value */
+ static const char * const pattrs[] = /* Attributes we're interested in */
+ {
+ "auth-info-required",
+ "device-uri",
+ "job-sheets-default",
+ "marker-change-time",
+ "marker-colors",
+ "marker-high-levels",
+ "marker-levels",
+ "marker-low-levels",
+ "marker-message",
+ "marker-names",
+ "marker-types",
+#ifdef __APPLE__
+ "media-supported",
+#endif /* __APPLE__ */
+ "printer-commands",
+ "printer-defaults",
+ "printer-info",
+ "printer-is-accepting-jobs",
+ "printer-is-shared",
+ "printer-location",
+ "printer-make-and-model",
+ "printer-name",
+ "printer-state",
+ "printer-state-change-time",
+ "printer-state-reasons",
+ "printer-type",
+ "printer-uri-supported"
+ };
+
+
+#ifdef __APPLE__
+ /*
+ * Get the default paper size...
+ */
+
+ appleGetPaperSize(media_default, sizeof(media_default));
+#endif /* __APPLE__ */
+
+ /*
+ * Build a CUPS_GET_PRINTERS or IPP_GET_PRINTER_ATTRIBUTES request, which
+ * require the following attributes:
+ *
+ * attributes-charset
+ * attributes-natural-language
+ * requesting-user-name
+ * printer-uri [for IPP_GET_PRINTER_ATTRIBUTES]
+ */
+ request = ippNewRequest(op);
+
+ ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
+ "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
+ NULL, pattrs);
+
+ ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
+ "requesting-user-name", NULL, cupsUser());
+
+ if (name && op != CUPS_GET_DEFAULT)
+ {
+ httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+ "localhost", ippPort(), "/printers/%s", name);
+ ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
+ uri);
+ }
+
+ /*
+ * Do the request and get back a response...
+ */
+
+ if ((response = cupsDoRequest(http, request, "/")) != NULL)
+ {
+ for (attr = response->attrs; attr != NULL; attr = attr->next)
+ {
+ /*
+ * Skip leading attributes until we hit a printer...
+ */
+
+ while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
+ attr = attr->next;
+
+ if (attr == NULL)
+ break;
+
+ /*
+ * Pull the needed attributes from this printer...
+ */
+
+ printer_name = NULL;
+ num_options = 0;
+ options = NULL;
+
+ for (; attr && attr->group_tag == IPP_TAG_PRINTER; attr = attr->next)
+ {
+ if (attr->value_tag != IPP_TAG_INTEGER &&
+ attr->value_tag != IPP_TAG_ENUM &&
+ attr->value_tag != IPP_TAG_BOOLEAN &&
+ attr->value_tag != IPP_TAG_TEXT &&
+ attr->value_tag != IPP_TAG_TEXTLANG &&
+ attr->value_tag != IPP_TAG_NAME &&
+ attr->value_tag != IPP_TAG_NAMELANG &&
+ attr->value_tag != IPP_TAG_KEYWORD &&
+ attr->value_tag != IPP_TAG_RANGE &&
+ attr->value_tag != IPP_TAG_URI)
+ continue;
+
+ if (!strcmp(attr->name, "auth-info-required") ||
+ !strcmp(attr->name, "device-uri") ||
+ !strcmp(attr->name, "marker-change-time") ||
+ !strcmp(attr->name, "marker-colors") ||
+ !strcmp(attr->name, "marker-high-levels") ||
+ !strcmp(attr->name, "marker-levels") ||
+ !strcmp(attr->name, "marker-low-levels") ||
+ !strcmp(attr->name, "marker-message") ||
+ !strcmp(attr->name, "marker-names") ||
+ !strcmp(attr->name, "marker-types") ||
+ !strcmp(attr->name, "printer-commands") ||
+ !strcmp(attr->name, "printer-info") ||
+ !strcmp(attr->name, "printer-is-shared") ||
+ !strcmp(attr->name, "printer-make-and-model") ||
+ !strcmp(attr->name, "printer-state") ||
+ !strcmp(attr->name, "printer-state-change-time") ||
+ !strcmp(attr->name, "printer-type") ||
+ !strcmp(attr->name, "printer-is-accepting-jobs") ||
+ !strcmp(attr->name, "printer-location") ||
+ !strcmp(attr->name, "printer-state-reasons") ||
+ !strcmp(attr->name, "printer-uri-supported"))
+ {
+ /*
+ * Add a printer description attribute...
+ */
+
+ num_options = cupsAddOption(attr->name,
+ cups_make_string(attr, value,
+ sizeof(value)),
+ num_options, &options);
+ }
+#ifdef __APPLE__
+ else if (!strcmp(attr->name, "media-supported"))
+ {
+ /*
+ * See if we can set a default media size...
+ */
+
+ int i; /* Looping var */
+
+ for (i = 0; i < attr->num_values; i ++)
+ if (!strcasecmp(media_default, attr->values[i].string.text))
+ {
+ num_options = cupsAddOption("media", media_default, num_options,
+ &options);
+ break;
+ }
+ }
+#endif /* __APPLE__ */
+ else if (!strcmp(attr->name, "printer-name") &&
+ attr->value_tag == IPP_TAG_NAME)
+ printer_name = attr->values[0].string.text;
+ else if (strncmp(attr->name, "notify-", 7) &&
+ (attr->value_tag == IPP_TAG_BOOLEAN ||
+ attr->value_tag == IPP_TAG_ENUM ||
+ attr->value_tag == IPP_TAG_INTEGER ||
+ attr->value_tag == IPP_TAG_KEYWORD ||
+ attr->value_tag == IPP_TAG_NAME ||
+ attr->value_tag == IPP_TAG_RANGE) &&
+ (ptr = strstr(attr->name, "-default")) != NULL)
+ {
+ /*
+ * Add a default option...
+ */
- if (num_dests <= 0 || !dests)
- return (NULL);
+ strlcpy(optname, attr->name, sizeof(optname));
+ optname[ptr - attr->name] = '\0';
+
+ if (strcasecmp(optname, "media") ||
+ !cupsGetOption("media", num_options, options))
+ num_options = cupsAddOption(optname,
+ cups_make_string(attr, value,
+ sizeof(value)),
+ num_options, &options);
+ }
+ }
- if (!name)
- {
- /*
- * NULL name for default printer.
- */
+ /*
+ * See if we have everything needed...
+ */
- while (num_dests > 0)
- {
- if (dests->is_default)
- return (dests);
+ if (!printer_name)
+ {
+ cupsFreeOptions(num_options, options);
- num_dests --;
- dests ++;
- }
- }
- else
- {
- /*
- * Lookup name and optionally the instance...
- */
+ if (attr == NULL)
+ break;
+ else
+ continue;
+ }
- while (num_dests > 0)
- {
- if ((comp = strcasecmp(name, dests->name)) < 0)
- return (NULL);
- else if (comp == 0)
+ if ((dest = cups_add_dest(printer_name, NULL, &num_dests, dests)) != NULL)
{
- if ((!instance && !dests->instance) ||
- (instance != NULL && dests->instance != NULL &&
- !strcasecmp(instance, dests->instance)))
- return (dests);
+ dest->num_options = num_options;
+ dest->options = options;
}
+ else
+ cupsFreeOptions(num_options, options);
- num_dests --;
- dests ++;
+ if (attr == NULL)
+ break;
}
+
+ ippDelete(response);
}
- return (NULL);
+ /*
+ * Return the count...
+ */
+
+ return (num_dests);
}
* Starting with CUPS 1.2, the returned list of destinations include the
* printer-info, printer-is-accepting-jobs, printer-is-shared,
* printer-make-and-model, printer-state, printer-state-change-time,
- * printer-state-reasons, and printer-type attributes as options.
+ * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
+ * adds the marker-change-time, marker-colors, marker-high-levels,
+ * marker-levels, marker-low-levels, marker-message, marker-names,
+ * marker-types, and printer-commands attributes as well.
*
* Use the @link cupsFreeDests@ function to free the destination list and
* the @link cupsGetDest@ function to find a particular destination.
* Starting with CUPS 1.2, the returned list of destinations include the
* printer-info, printer-is-accepting-jobs, printer-is-shared,
* printer-make-and-model, printer-state, printer-state-change-time,
- * printer-state-reasons, and printer-type attributes as options.
+ * printer-state-reasons, and printer-type attributes as options. CUPS 1.4
+ * adds the marker-change-time, marker-colors, marker-high-levels,
+ * marker-levels, marker-low-levels, marker-message, marker-names,
+ * marker-types, and printer-commands attributes as well.
*
* Use the @link cupsFreeDests@ function to free the destination list and
* the @link cupsGetDest@ function to find a particular destination.
*
- * @since CUPS 1.1.21@
+ * @since CUPS 1.1.21/Mac OS X 10.4@
*/
int /* O - Number of destinations */
char filename[1024]; /* Local ~/.cups/lpoptions file */
const char *defprinter; /* Default printer */
char name[1024], /* Copy of printer name */
- *instance; /* Pointer to instance name */
+ *instance, /* Pointer to instance name */
+ *user_default; /* User default printer */
int num_reals; /* Number of real queues */
cups_dest_t *reals; /* Real queues */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
return (0);
}
- /*
- * Initialize destination array...
- */
-
- num_dests = 0;
- *dests = (cups_dest_t *)0;
-
/*
* Grab the printers and classes...
*/
- num_dests = cups_get_sdests(http, CUPS_GET_PRINTERS, NULL, num_dests, dests);
- if (cupsLastError() < IPP_REDIRECTION_OTHER_SITE)
- num_dests = cups_get_sdests(http, CUPS_GET_CLASSES, NULL, num_dests, dests);
+ *dests = (cups_dest_t *)0;
+ num_dests = _cupsGetDests(http, CUPS_GET_PRINTERS, NULL, dests);
if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE)
{
* Grab the default destination...
*/
-#ifdef __APPLE__
- if ((defprinter = appleGetDefault(name, sizeof(name))) == NULL)
-#endif /* __APPLE__ */
- defprinter = cupsGetDefault2(http);
+ if ((user_default = _cupsUserDefault(name, sizeof(name))) != NULL)
+ defprinter = name;
+ else if ((defprinter = cupsGetDefault2(http)) != NULL)
+ {
+ strlcpy(name, defprinter, sizeof(name));
+ defprinter = name;
+ }
if (defprinter)
{
/*
- * Grab printer and instance name...
+ * Separate printer and instance name...
*/
-#ifdef __APPLE__
- if (name != defprinter)
-#endif /* __APPLE__ */
- strlcpy(name, defprinter, sizeof(name));
-
if ((instance = strchr(name, '/')) != NULL)
*instance++ = '\0';
dest->is_default = 1;
}
else
- {
- /*
- * This initialization of "instance" is unnecessary, but avoids a
- * compiler warning...
- */
-
instance = NULL;
- }
/*
* Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
*/
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
- num_dests = cups_get_dests(filename, NULL, NULL, num_dests, dests);
+ num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL,
+ num_dests, dests);
if ((home = getenv("HOME")) != NULL)
{
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
- if (access(filename, 0))
- snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
- num_dests = cups_get_dests(filename, NULL, NULL, num_dests, dests);
+ num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL,
+ num_dests, dests);
}
/*
* The returned destination must be freed using @link cupsFreeDests@ with a
* "num_dests" value of 1.
*
- * @since CUPS 1.4@
+ * @since CUPS 1.4/Mac OS X 10.6@
*/
cups_dest_t * /* O - Destination or @code NULL@ */
char filename[1024], /* Path to lpoptions */
defname[256]; /* Default printer name */
const char *home = getenv("HOME"); /* Home directory */
+ int set_as_default = 0; /* Set returned destination as default */
ipp_op_t op = IPP_GET_PRINTER_ATTRIBUTES;
/* IPP operation to get server ops */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
if (!name)
{
- if ((name = getenv("LPDEST")) == NULL)
- if ((name = getenv("PRINTER")) != NULL && !strcmp(name, "lp"))
- name = NULL;
+ set_as_default = 1;
+ name = _cupsUserDefault(defname, sizeof(defname));
+
+ if (name)
+ {
+ char *ptr; /* Temporary pointer... */
- if (!name && home)
+ if ((ptr = strchr(defname, '/')) != NULL)
+ {
+ *ptr++ = '\0';
+ instance = ptr;
+ }
+ else
+ instance = NULL;
+ }
+ else if (home)
{
/*
* No default in the environment, try the user's lpoptions files...
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
- if ((name = cups_get_default(filename, defname, sizeof(defname),
- &instance)) == NULL)
- {
- snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
- name = cups_get_default(filename, defname, sizeof(defname),
- &instance);
- }
+ name = cups_get_default(filename, defname, sizeof(defname), &instance);
}
if (!name)
* Get the printer's attributes...
*/
- if (!cups_get_sdests(http, op, name, 0, &dest))
- return (NULL);
+ if (!_cupsGetDests(http, op, name, &dest))
+ {
+ if (op == CUPS_GET_DEFAULT || (name && !set_as_default))
+ return (NULL);
+
+ /*
+ * The default printer from environment variables or from a
+ * configuration file does not exist. Find out the real default.
+ */
+
+ if (!_cupsGetDests(http, CUPS_GET_DEFAULT, NULL, &dest))
+ return (NULL);
+ }
if (instance)
dest->instance = _cupsStrAlloc(instance);
+ if (set_as_default)
+ dest->is_default = 1;
+
/*
* Then add local options...
*/
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
- cups_get_dests(filename, name, instance, 1, &dest);
+ cups_get_dests(filename, name, instance, 1, 1, &dest);
if (home)
{
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
- if (access(filename, 0))
- snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
-
- cups_get_dests(filename, name, instance, 1, &dest);
+ cups_get_dests(filename, name, instance, 1, 1, &dest);
}
/*
* @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
* options for the user.
*
- * @since CUPS 1.3@
+ * @since CUPS 1.3/Mac OS X 10.5@
*/
int /* O - New number of destinations */
/*
* 'cupsSetDefaultDest()' - Set the default destination.
*
- * @since CUPS 1.3@
+ * @since CUPS 1.3/Mac OS X 10.5@
*/
void
* This function saves the destinations to /etc/cups/lpoptions when run
* as root and ~/.cups/lpoptions when run as a normal user.
*
- * @since CUPS 1.1.21@
+ * @since CUPS 1.1.21/Mac OS X 10.4@
*/
int /* O - 0 on success, -1 on error */
* Get the server destinations...
*/
- num_temps = cups_get_sdests(http, CUPS_GET_PRINTERS, NULL, 0, &temps);
- num_temps = cups_get_sdests(http, CUPS_GET_CLASSES, NULL, num_temps, &temps);
+ num_temps = _cupsGetDests(http, CUPS_GET_PRINTERS, NULL, &temps);
+
+ if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE)
+ {
+ cupsFreeDests(num_temps, temps);
+ return (-1);
+ }
/*
* Figure out which file to write to...
* Merge in server defaults...
*/
- num_temps = cups_get_dests(filename, NULL, NULL, num_temps, &temps);
+ num_temps = cups_get_dests(filename, NULL, NULL, 0, num_temps, &temps);
/*
* Point to user defaults...
if ((home = getenv("HOME")) != NULL)
{
- /*
- * Remove the old ~/.lpoptions file...
- */
-
- snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
- unlink(filename);
-
/*
* Create ~/.cups subdirectory...
*/
*/
if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL)
- appleSetDefault(dest->name);
+ {
+ CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault,
+ dest->name,
+ kCFStringEncodingUTF8);
+ /* Default printer name */
+
+ if (name)
+ {
+ _cupsAppleSetDefaultPrinter(name);
+ CFRelease(name);
+ }
+ }
#endif /* __APPLE__ */
#ifdef HAVE_NOTIFY_POST
}
+/*
+ * '_cupsUserDefault()' - Get the user default printer from environment
+ * variables and location information.
+ */
+
+char * /* O - Default printer or NULL */
+_cupsUserDefault(char *name, /* I - Name buffer */
+ size_t namesize) /* I - Size of name buffer */
+{
+ const char *env; /* LPDEST or PRINTER env variable */
+#ifdef __APPLE__
+ CFStringRef locprinter; /* Last printer as this location */
+#endif /* __APPLE__ */
+
+
+ if ((env = getenv("LPDEST")) == NULL)
+ if ((env = getenv("PRINTER")) != NULL && !strcmp(env, "lp"))
+ env = NULL;
+
+ if (env)
+ {
+ strlcpy(name, env, namesize);
+ return (name);
+ }
+
+#ifdef __APPLE__
+ /*
+ * Use location-based defaults if "use last printer" is selected in the
+ * system preferences...
+ */
+
+ if ((locprinter = _cupsAppleCopyDefaultPrinter()) != NULL)
+ {
+ CFStringGetCString(locprinter, name, namesize, kCFStringEncodingUTF8);
+ CFRelease(locprinter);
+ }
+ else
+ name[0] = '\0';
+
+ DEBUG_printf(("1_cupsUserDefault: Returning \"%s\".", name));
+
+ return (*name ? name : NULL);
+
+#else
+ /*
+ * No location-based defaults on this platform...
+ */
+
+ name[0] = '\0';
+ return (NULL);
+#endif /* __APPLE__ */
+}
+
+
#ifdef __APPLE__
/*
* 'appleCopyLocations()' - Copy the location history array.
* Look up the location array in the preferences...
*/
- if ((locations = CFPreferencesCopyAppValue(kLocationHistoryArrayKey,
- kPMPrintingPreferences)) == NULL)
+ if ((locations = CFPreferencesCopyAppValue(kLastUsedPrintersKey,
+ kCUPSPrintingPrefs)) == NULL)
return (NULL);
if (CFGetTypeID(locations) != CFArrayGetTypeID())
CFStringRef network = NULL; /* Current network ID */
- if ((dynamicStore = SCDynamicStoreCreate(NULL, CFSTR("Printing"), NULL,
+ if ((dynamicStore = SCDynamicStoreCreate(NULL, CFSTR("libcups"), NULL,
NULL)) != NULL)
{
+ /*
+ * First use the IPv6 router address, if available, since that will generally
+ * be a globally-unique link-local address.
+ */
+
if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity(
- NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)) != NULL)
+ NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6)) != NULL)
{
if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL)
{
if ((network = CFDictionaryGetValue(ip_dict,
- kSCPropNetIPv4Router)) != NULL)
+ kSCPropNetIPv6Router)) != NULL)
CFRetain(network);
CFRelease(ip_dict);
CFRelease(key);
}
+ /*
+ * If that doesn't work, try the IPv4 router address. This isn't as unique
+ * and will likely be a 10.x.y.z or 192.168.y.z address...
+ */
+
+ if (!network)
+ {
+ if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity(
+ NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)) != NULL)
+ {
+ if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL)
+ {
+ if ((network = CFDictionaryGetValue(ip_dict,
+ kSCPropNetIPv4Router)) != NULL)
+ CFRetain(network);
+
+ CFRelease(ip_dict);
+ }
+
+ CFRelease(key);
+ }
+ }
+
CFRelease(dynamicStore);
}
/*
- * 'appleGetDefault()' - Get the default printer for this location.
+ * 'appleGetPaperSize()' - Get the default paper size.
*/
-static char * /* O - Name or NULL if no default */
-appleGetDefault(char *name, /* I - Name buffer */
- int namesize) /* I - Size of name buffer */
+char * /* O - Default paper size */
+appleGetPaperSize(char *name, /* I - Paper size name buffer */
+ int namesize) /* I - Size of buffer */
{
- CFStringRef network; /* Network location */
- CFArrayRef locations; /* Location array */
- CFStringRef locprinter; /* Current printer */
-
-
- /*
- * Use location-based defaults if "use last printer" is selected in the
- * system preferences...
- */
-
- if (!appleUseLastPrinter())
- {
- DEBUG_puts("appleGetDefault: Not using last printer as default...");
- return (NULL);
- }
-
- /*
- * Get the current location...
- */
-
- if ((network = appleCopyNetwork()) == NULL)
- {
- DEBUG_puts("appleGetDefault: Unable to get current network...");
- return (NULL);
- }
-
-#ifdef DEBUG
- CFStringGetCString(network, name, namesize, kCFStringEncodingUTF8);
- DEBUG_printf(("appleGetDefault: network=\"%s\"\n", name));
-#endif /* DEBUG */
-
- /*
- * Lookup the network in the preferences...
- */
-
- if ((locations = appleCopyLocations()) == NULL)
- {
- /*
- * Missing or bad location array, so no location-based default...
- */
-
- DEBUG_puts("appleGetDefault: Missing or bad location history array...");
-
- CFRelease(network);
+ CFStringRef defaultPaperID; /* Default paper ID */
+ _pwg_media_t *pwgmedia; /* PWG media size */
- return (NULL);
- }
-
- DEBUG_printf(("appleGetDefault: Got location, %d entries...\n",
- (int)CFArrayGetCount(locations)));
- if ((locprinter = appleGetPrinter(locations, network, NULL)) != NULL)
- CFStringGetCString(locprinter, name, namesize, kCFStringEncodingUTF8);
- else
+ defaultPaperID = _cupsAppleCopyDefaultPaperID();
+ if (!defaultPaperID ||
+ CFGetTypeID(defaultPaperID) != CFStringGetTypeID() ||
+ !CFStringGetCString(defaultPaperID, name, namesize,
+ kCFStringEncodingUTF8))
name[0] = '\0';
+ else if ((pwgmedia = _pwgMediaForLegacy(name)) != NULL)
+ strlcpy(name, pwgmedia->pwg, namesize);
- CFRelease(network);
- CFRelease(locations);
-
- DEBUG_printf(("appleGetDefault: Returning \"%s\"...\n", name));
+ if (defaultPaperID)
+ CFRelease(defaultPaperID);
- return (*name ? name : NULL);
+ return (name);
}
if (locindex)
*locindex = i;
- return (locprinter);
- }
- }
+ return (locprinter);
+ }
+ }
+
+ return (NULL);
+}
+#endif /* __APPLE__ */
+
+
+/*
+ * 'cups_add_dest()' - Add a destination to the array.
+ *
+ * Unlike cupsAddDest(), this function does not check for duplicates.
+ */
+
+static cups_dest_t * /* O - New destination */
+cups_add_dest(const char *name, /* I - Name of destination */
+ const char *instance, /* I - Instance or NULL */
+ int *num_dests, /* IO - Number of destinations */
+ cups_dest_t **dests) /* IO - Destinations */
+{
+ int insert, /* Insertion point */
+ diff; /* Result of comparison */
+ cups_dest_t *dest; /* Destination pointer */
+
+
+ /*
+ * Add new destination...
+ */
+
+ if (*num_dests == 0)
+ dest = malloc(sizeof(cups_dest_t));
+ else
+ dest = realloc(*dests, sizeof(cups_dest_t) * (*num_dests + 1));
+
+ if (!dest)
+ return (NULL);
+
+ *dests = dest;
+
+ /*
+ * Find where to insert the destination...
+ */
+
+ if (*num_dests == 0)
+ insert = 0;
+ else
+ {
+ insert = cups_find_dest(name, instance, *num_dests, *dests, *num_dests - 1,
+ &diff);
+
+ if (diff > 0)
+ insert ++;
+ }
+
+ /*
+ * Move the array elements as needed...
+ */
+
+ if (insert < *num_dests)
+ memmove(*dests + insert + 1, *dests + insert,
+ (*num_dests - insert) * sizeof(cups_dest_t));
+
+ (*num_dests) ++;
- return (NULL);
+ /*
+ * Initialize the destination...
+ */
+
+ dest = *dests + insert;
+ dest->name = _cupsStrAlloc(name);
+ dest->instance = _cupsStrAlloc(instance);
+ dest->is_default = 0;
+ dest->num_options = 0;
+ dest->options = (cups_option_t *)0;
+
+ return (dest);
}
/*
- * 'appleSetDefault()' - Set the default printer for this location.
+ * 'cups_compare_dests()' - Compare two destinations.
*/
-static void
-appleSetDefault(const char *name) /* I - Default printer/class name */
+static int /* O - Result of comparison */
+cups_compare_dests(cups_dest_t *a, /* I - First destination */
+ cups_dest_t *b) /* I - Second destination */
{
- CFStringRef network; /* Current network */
- CFArrayRef locations; /* Old locations array */
- CFIndex locindex; /* Index in locations array */
- CFStringRef locprinter; /* Current printer */
- CFMutableArrayRef newlocations; /* New locations array */
- CFMutableDictionaryRef newlocation; /* New location */
- CFStringRef newprinter; /* New printer */
+ int diff; /* Difference */
- /*
- * Get the current location...
- */
+ if ((diff = strcasecmp(a->name, b->name)) != 0)
+ return (diff);
+ else if (a->instance && b->instance)
+ return (strcasecmp(a->instance, b->instance));
+ else
+ return ((a->instance && !b->instance) - (!a->instance && b->instance));
+}
- if ((network = appleCopyNetwork()) == NULL)
- {
- DEBUG_puts("appleSetDefault: Unable to get current network...");
- return;
- }
- if ((newprinter = CFStringCreateWithCString(kCFAllocatorDefault, name,
- kCFStringEncodingUTF8)) == NULL)
- {
- CFRelease(network);
- return;
- }
+/*
+ * 'cups_find_dest()' - Find a destination using a binary search.
+ */
- /*
- * Lookup the network in the preferences...
- */
+static int /* O - Index of match */
+cups_find_dest(const char *name, /* I - Destination name */
+ const char *instance, /* I - Instance or NULL */
+ int num_dests, /* I - Number of destinations */
+ cups_dest_t *dests, /* I - Destinations */
+ int prev, /* I - Previous index */
+ int *rdiff) /* O - Difference of match */
+{
+ int left, /* Low mark for binary search */
+ right, /* High mark for binary search */
+ current, /* Current index */
+ diff; /* Result of comparison */
+ cups_dest_t key; /* Search key */
- if ((locations = appleCopyLocations()) != NULL)
- locprinter = appleGetPrinter(locations, network, &locindex);
- else
- {
- locprinter = NULL;
- locindex = -1;
- }
- if (!locprinter ||
- CFStringCompare(locprinter, newprinter, 0) != kCFCompareEqualTo)
+ key.name = (char *)name;
+ key.instance = (char *)instance;
+
+ if (prev >= 0)
{
/*
- * Need to change the locations array...
+ * Start search on either side of previous...
*/
- if (locations)
+ if ((diff = cups_compare_dests(&key, dests + prev)) == 0 ||
+ (diff < 0 && prev == 0) ||
+ (diff > 0 && prev == (num_dests - 1)))
{
- newlocations = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0,
- locations);
-
- if (locprinter)
- CFArrayRemoveValueAtIndex(newlocations, locindex);
+ *rdiff = diff;
+ return (prev);
}
- else
- newlocations = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
-
- newlocation = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
- &kCFTypeDictionaryKeyCallBacks,
- &kCFTypeDictionaryValueCallBacks);
-
- if (newlocation && newlocations)
+ else if (diff < 0)
{
/*
- * Put the new location at the front of the array...
- */
-
- CFDictionaryAddValue(newlocation, kLocationNetworkKey, network);
- CFDictionaryAddValue(newlocation, kLocationPrinterIDKey, newprinter);
- CFArrayInsertValueAtIndex(newlocations, 0, newlocation);
-
- /*
- * Limit the number of locations to 10...
+ * Start with previous on right side...
*/
- while (CFArrayGetCount(newlocations) > 10)
- CFArrayRemoveValueAtIndex(newlocations, 10);
-
+ left = 0;
+ right = prev;
+ }
+ else
+ {
/*
- * Push the changes out...
+ * Start wih previous on left side...
*/
- CFPreferencesSetAppValue(kLocationHistoryArrayKey, newlocations,
- kPMPrintingPreferences);
- CFPreferencesAppSynchronize(kPMPrintingPreferences);
+ left = prev;
+ right = num_dests - 1;
}
-
- if (newlocations)
- CFRelease(newlocations);
-
- if (newlocation)
- CFRelease(newlocation);
}
+ else
+ {
+ /*
+ * Start search in the middle...
+ */
- if (locations)
- CFRelease(locations);
-
- CFRelease(network);
- CFRelease(newprinter);
-}
-
-
-/*
- * 'appleUseLastPrinter()' - Get the default printer preference value.
- */
+ left = 0;
+ right = num_dests - 1;
+ }
-static int /* O - 1 to use last printer, 0 otherwise */
-appleUseLastPrinter(void)
-{
- CFPropertyListRef uselast; /* Use last printer preference value */
+ do
+ {
+ current = (left + right) / 2;
+ diff = cups_compare_dests(&key, dests + current);
+ if (diff == 0)
+ break;
+ else if (diff < 0)
+ right = current;
+ else
+ left = current;
+ }
+ while ((right - left) > 1);
- if ((uselast = CFPreferencesCopyAppValue(kUseLastPrinterAsCurrentPrinterKey,
- kPMPrintingPreferences)) != NULL)
+ if (diff != 0)
{
- CFRelease(uselast);
+ /*
+ * Check the last 1 or 2 elements...
+ */
- if (uselast == kCFBooleanFalse)
- return (0);
+ if ((diff = cups_compare_dests(&key, dests + left)) <= 0)
+ current = left;
+ else
+ {
+ diff = cups_compare_dests(&key, dests + right);
+ current = right;
+ }
}
- return (1);
+ /*
+ * Return the closest destination and the difference...
+ */
+
+ *rdiff = diff;
+
+ return (current);
}
-#endif /* __APPLE__ */
/*
*/
static int /* O - Number of destinations */
-cups_get_dests(const char *filename, /* I - File to read from */
- const char *match_name, /* I - Destination name we want */
- const char *match_inst, /* I - Instance name we want */
- int num_dests, /* I - Number of destinations */
- cups_dest_t **dests) /* IO - Destinations */
+cups_get_dests(
+ const char *filename, /* I - File to read from */
+ const char *match_name, /* I - Destination name we want */
+ const char *match_inst, /* I - Instance name we want */
+ int user_default_set, /* I - User default printer set? */
+ int num_dests, /* I - Number of destinations */
+ cups_dest_t **dests) /* IO - Destinations */
{
int i; /* Looping var */
cups_dest_t *dest; /* Current destination */
*name, /* Name of destination/option */
*instance; /* Instance of destination */
int linenum; /* Current line number */
- const char *printer; /* PRINTER or LPDEST */
- DEBUG_printf(("cups_get_dests(filename=\"%s\", match_name=\"%s\", "
- "match_inst=\"%s\", num_dests=%d, dests=%p)\n", filename,
- match_name ? match_name : "(null)",
- match_inst ? match_inst : "(null)", num_dests, dests));
+ DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", "
+ "match_inst=\"%s\", user_default_set=%d, num_dests=%d, "
+ "dests=%p)", filename, match_name, match_inst,
+ user_default_set, num_dests, dests));
/*
* Try to open the file...
if ((fp = cupsFileOpen(filename, "r")) == NULL)
return (num_dests);
- /*
- * Check environment variables...
- */
-
- if ((printer = getenv("LPDEST")) == NULL)
- if ((printer = getenv("PRINTER")) != NULL)
- if (strcmp(printer, "lp") == 0)
- printer = NULL;
-
- DEBUG_printf(("cups_get_dests: printer=\"%s\"\n",
- printer ? printer : "(null)"));
-
/*
* Read each printer; each line looks like:
*
* See what type of line it is...
*/
- DEBUG_printf(("cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"\n",
- linenum, line, lineptr ? lineptr : "(null)"));
+ DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"",
+ linenum, line, lineptr));
if ((strcasecmp(line, "dest") && strcasecmp(line, "default")) || !lineptr)
{
- DEBUG_puts("cups_get_dests: Not a dest or default line...");
+ DEBUG_puts("9cups_get_dests: Not a dest or default line...");
continue;
}
if (*lineptr)
*lineptr++ = '\0';
- DEBUG_printf(("cups_get_dests: name=\"%s\", instance=\"%s\"\n", name,
+ DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name,
instance));
/*
}
else if (cupsGetDest(name, NULL, num_dests, *dests) == NULL)
{
- DEBUG_puts("cups_get_dests: Not found!");
+ DEBUG_puts("9cups_get_dests: Not found!");
continue;
}
else
* Out of memory!
*/
- DEBUG_puts("cups_get_dests: Out of memory!");
+ DEBUG_puts("9cups_get_dests: Out of memory!");
break;
}
}
* Set this as default if needed...
*/
- if (!printer && !strcasecmp(line, "default"))
+ if (!user_default_set && !strcasecmp(line, "default"))
{
- DEBUG_puts("cups_get_dests: Setting as default...");
+ DEBUG_puts("9cups_get_dests: Setting as default...");
for (i = 0; i < num_dests; i ++)
(*dests)[i].is_default = 0;
/*
- * 'cups_get_sdests()' - Get destinations from a server.
+ * 'cups_make_string()' - Make a comma-separated string of values from an IPP
+ * attribute.
*/
-static int /* O - Number of destinations */
-cups_get_sdests(http_t *http, /* I - Connection to server or CUPS_HTTP_DEFAULT */
- ipp_op_t op, /* I - IPP operation */
- const char *name, /* I - Name of destination */
- int num_dests, /* I - Number of destinations */
- cups_dest_t **dests) /* IO - Destinations */
+static char * /* O - New string */
+cups_make_string(
+ ipp_attribute_t *attr, /* I - Attribute to convert */
+ char *buffer, /* I - Buffer */
+ size_t bufsize) /* I - Size of buffer */
{
int i; /* Looping var */
- cups_dest_t *dest; /* Current destination */
- ipp_t *request, /* IPP Request */
- *response; /* IPP Response */
- ipp_attribute_t *attr; /* Current attribute */
- int accepting, /* printer-is-accepting-jobs attribute */
- shared, /* printer-is-shared attribute */
- state, /* printer-state attribute */
- change_time, /* printer-state-change-time attribute */
- type; /* printer-type attribute */
- const char *info, /* printer-info attribute */
- *location, /* printer-location attribute */
- *make_model, /* printer-make-and-model attribute */
- *printer_name; /* printer-name attribute */
- char uri[1024], /* printer-uri value */
- job_sheets[1024], /* job-sheets-default attribute */
- auth_info_req[1024], /* auth-info-required attribute */
- reasons[1024]; /* printer-state-reasons attribute */
- int num_options; /* Number of options */
- cups_option_t *options; /* Options */
- char optname[1024], /* Option name */
- value[2048], /* Option value */
- *ptr; /* Pointer into name/value */
- static const char * const pattrs[] = /* Attributes we're interested in */
- {
- "auth-info-required",
- "job-sheets-default",
- "printer-info",
- "printer-is-accepting-jobs",
- "printer-is-shared",
- "printer-location",
- "printer-make-and-model",
- "printer-name",
- "printer-state",
- "printer-state-change-time",
- "printer-state-reasons",
- "printer-type",
- "printer-defaults"
- };
+ char *ptr, /* Pointer into buffer */
+ *end, /* Pointer to end of buffer */
+ *valptr; /* Pointer into string attribute */
/*
- * Build a CUPS_GET_PRINTERS or CUPS_GET_CLASSES request, which require
- * the following attributes:
- *
- * attributes-charset
- * attributes-natural-language
- * requesting-user-name
+ * Return quickly if we have a single string value...
*/
- request = ippNewRequest(op);
-
- ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
- "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
- NULL, pattrs);
-
- ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
- "requesting-user-name", NULL, cupsUser());
-
- if (name)
- {
- httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
- "localhost", ippPort(), "/printers/%s", name);
- ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
- uri);
- }
+ if (attr->num_values == 1 &&
+ attr->value_tag != IPP_TAG_INTEGER &&
+ attr->value_tag != IPP_TAG_ENUM &&
+ attr->value_tag != IPP_TAG_BOOLEAN &&
+ attr->value_tag != IPP_TAG_RANGE)
+ return (attr->values[0].string.text);
/*
- * Do the request and get back a response...
+ * Copy the values to the string, separating with commas and escaping strings
+ * as needed...
*/
- if ((response = cupsDoRequest(http, request, "/")) != NULL)
- {
- for (attr = response->attrs; attr != NULL; attr = attr->next)
- {
- /*
- * Skip leading attributes until we hit a printer...
- */
-
- while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
- attr = attr->next;
-
- if (attr == NULL)
- break;
-
- /*
- * Pull the needed attributes from this printer...
- */
-
- accepting = 0;
- change_time = 0;
- info = NULL;
- location = NULL;
- make_model = NULL;
- printer_name = NULL;
- num_options = 0;
- options = NULL;
- shared = 1;
- state = IPP_PRINTER_IDLE;
- type = CUPS_PRINTER_LOCAL;
+ end = buffer + bufsize - 1;
- auth_info_req[0] = '\0';
- job_sheets[0] = '\0';
- reasons[0] = '\0';
+ for (i = 0, ptr = buffer; i < attr->num_values && ptr < end; i ++)
+ {
+ if (i)
+ *ptr++ = ',';
- while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
- {
- if (!strcmp(attr->name, "auth-info-required") &&
- attr->value_tag == IPP_TAG_KEYWORD)
- {
- strlcpy(auth_info_req, attr->values[0].string.text,
- sizeof(auth_info_req));
+ switch (attr->value_tag)
+ {
+ case IPP_TAG_INTEGER :
+ case IPP_TAG_ENUM :
+ snprintf(ptr, end - ptr + 1, "%d", attr->values[i].integer);
+ break;
- for (i = 1, ptr = auth_info_req + strlen(auth_info_req);
- i < attr->num_values;
- i ++)
- {
- snprintf(ptr, sizeof(auth_info_req) - (ptr - auth_info_req), ",%s",
- attr->values[i].string.text);
- ptr += strlen(ptr);
- }
- }
- else if (!strcmp(attr->name, "job-sheets-default") &&
- (attr->value_tag == IPP_TAG_KEYWORD ||
- attr->value_tag == IPP_TAG_NAME))
- {
- if (attr->num_values == 2)
- snprintf(job_sheets, sizeof(job_sheets), "%s,%s",
- attr->values[0].string.text, attr->values[1].string.text);
+ case IPP_TAG_BOOLEAN :
+ if (attr->values[i].boolean)
+ strlcpy(ptr, "true", end - ptr + 1);
else
- strlcpy(job_sheets, attr->values[0].string.text,
- sizeof(job_sheets));
- }
- else if (!strcmp(attr->name, "printer-info") &&
- attr->value_tag == IPP_TAG_TEXT)
- info = attr->values[0].string.text;
- else if (!strcmp(attr->name, "printer-is-accepting-jobs") &&
- attr->value_tag == IPP_TAG_BOOLEAN)
- accepting = attr->values[0].boolean;
- else if (!strcmp(attr->name, "printer-is-shared") &&
- attr->value_tag == IPP_TAG_BOOLEAN)
- shared = attr->values[0].boolean;
- else if (!strcmp(attr->name, "printer-location") &&
- attr->value_tag == IPP_TAG_TEXT)
- location = attr->values[0].string.text;
- else if (!strcmp(attr->name, "printer-make-and-model") &&
- attr->value_tag == IPP_TAG_TEXT)
- make_model = attr->values[0].string.text;
- else if (!strcmp(attr->name, "printer-name") &&
- attr->value_tag == IPP_TAG_NAME)
- printer_name = attr->values[0].string.text;
- else if (!strcmp(attr->name, "printer-state") &&
- attr->value_tag == IPP_TAG_ENUM)
- state = attr->values[0].integer;
- else if (!strcmp(attr->name, "printer-state-change-time") &&
- attr->value_tag == IPP_TAG_INTEGER)
- change_time = attr->values[0].integer;
- else if (!strcmp(attr->name, "printer-state-reasons") &&
- attr->value_tag == IPP_TAG_KEYWORD)
- {
- strlcpy(reasons, attr->values[0].string.text, sizeof(reasons));
- for (i = 1, ptr = reasons + strlen(reasons);
- i < attr->num_values;
- i ++)
- {
- snprintf(ptr, sizeof(reasons) - (ptr - reasons), ",%s",
- attr->values[i].string.text);
- ptr += strlen(ptr);
- }
- }
- else if (!strcmp(attr->name, "printer-type") &&
- attr->value_tag == IPP_TAG_ENUM)
- type = attr->values[0].integer;
- else if (strncmp(attr->name, "notify-", 7) &&
- (attr->value_tag == IPP_TAG_BOOLEAN ||
- attr->value_tag == IPP_TAG_ENUM ||
- attr->value_tag == IPP_TAG_INTEGER ||
- attr->value_tag == IPP_TAG_KEYWORD ||
- attr->value_tag == IPP_TAG_NAME ||
- attr->value_tag == IPP_TAG_RANGE) &&
- strstr(attr->name, "-default"))
- {
- char *valptr; /* Pointer into attribute value */
-
-
- /*
- * Add a default option...
- */
+ strlcpy(ptr, "false", end - ptr + 1);
+ break;
- strlcpy(optname, attr->name, sizeof(optname));
- if ((ptr = strstr(optname, "-default")) != NULL)
- *ptr = '\0';
+ case IPP_TAG_RANGE :
+ if (attr->values[i].range.lower == attr->values[i].range.upper)
+ snprintf(ptr, end - ptr + 1, "%d", attr->values[i].range.lower);
+ else
+ snprintf(ptr, end - ptr + 1, "%d-%d", attr->values[i].range.lower,
+ attr->values[i].range.upper);
+ break;
- value[0] = '\0';
- for (i = 0, ptr = value; i < attr->num_values; i ++)
+ default :
+ for (valptr = attr->values[i].string.text;
+ *valptr && ptr < end;)
{
- if (ptr >= (value + sizeof(value) - 1))
- break;
-
- if (i)
- *ptr++ = ',';
-
- switch (attr->value_tag)
+ if (strchr(" \t\n\\\'\"", *valptr))
{
- case IPP_TAG_INTEGER :
- case IPP_TAG_ENUM :
- snprintf(ptr, sizeof(value) - (ptr - value), "%d",
- attr->values[i].integer);
- break;
-
- case IPP_TAG_BOOLEAN :
- if (attr->values[i].boolean)
- strlcpy(ptr, "true", sizeof(value) - (ptr - value));
- else
- strlcpy(ptr, "false", sizeof(value) - (ptr - value));
- break;
-
- case IPP_TAG_RANGE :
- if (attr->values[i].range.lower ==
- attr->values[i].range.upper)
- snprintf(ptr, sizeof(value) - (ptr - value), "%d",
- attr->values[i].range.lower);
- else
- snprintf(ptr, sizeof(value) - (ptr - value), "%d-%d",
- attr->values[i].range.lower,
- attr->values[i].range.upper);
- break;
-
- default :
- for (valptr = attr->values[i].string.text;
- *valptr && ptr < (value + sizeof(value) - 2);)
- {
- if (strchr(" \t\n\\\'\"", *valptr))
- *ptr++ = '\\';
-
- *ptr++ = *valptr++;
- }
-
- *ptr = '\0';
- break;
- }
-
- ptr += strlen(ptr);
- }
-
- num_options = cupsAddOption(optname, value, num_options, &options);
- }
+ if (ptr >= (end - 1))
+ break;
- attr = attr->next;
- }
-
- /*
- * See if we have everything needed...
- */
+ *ptr++ = '\\';
+ }
- if (!printer_name)
- {
- cupsFreeOptions(num_options, options);
+ *ptr++ = *valptr++;
+ }
- if (attr == NULL)
+ *ptr = '\0';
break;
- else
- continue;
- }
-
- num_dests = cupsAddDest(printer_name, NULL, num_dests, dests);
-
- if ((dest = cupsGetDest(printer_name, NULL, num_dests, *dests)) != NULL)
- {
- dest->num_options = num_options;
- dest->options = options;
-
- num_options = 0;
- options = NULL;
-
- if (auth_info_req[0])
- dest->num_options = cupsAddOption("auth-info-required", auth_info_req,
- dest->num_options,
- &(dest->options));
-
- if (job_sheets[0])
- dest->num_options = cupsAddOption("job-sheets", job_sheets,
- dest->num_options,
- &(dest->options));
-
- if (info)
- dest->num_options = cupsAddOption("printer-info", info,
- dest->num_options,
- &(dest->options));
-
- sprintf(value, "%d", accepting);
- dest->num_options = cupsAddOption("printer-is-accepting-jobs", value,
- dest->num_options,
- &(dest->options));
-
- sprintf(value, "%d", shared);
- dest->num_options = cupsAddOption("printer-is-shared", value,
- dest->num_options,
- &(dest->options));
-
- if (location)
- dest->num_options = cupsAddOption("printer-location",
- location, dest->num_options,
- &(dest->options));
-
- if (make_model)
- dest->num_options = cupsAddOption("printer-make-and-model",
- make_model, dest->num_options,
- &(dest->options));
-
- sprintf(value, "%d", state);
- dest->num_options = cupsAddOption("printer-state", value,
- dest->num_options,
- &(dest->options));
-
- if (change_time)
- {
- sprintf(value, "%d", change_time);
- dest->num_options = cupsAddOption("printer-state-change-time", value,
- dest->num_options,
- &(dest->options));
- }
-
- if (reasons[0])
- dest->num_options = cupsAddOption("printer-state-reasons", reasons,
- dest->num_options,
- &(dest->options));
-
- sprintf(value, "%d", type);
- dest->num_options = cupsAddOption("printer-type", value,
- dest->num_options,
- &(dest->options));
- }
-
- cupsFreeOptions(num_options, options);
-
- if (attr == NULL)
- break;
}
- ippDelete(response);
+ ptr += strlen(ptr);
}
- /*
- * Return the count...
- */
+ *ptr = '\0';
- return (num_dests);
+ return (buffer);
}
/*
- * End of "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $".
+ * End of "$Id: dest.c 9568 2011-02-25 06:13:56Z mike $".
*/