2 * "$Id: dest.c 6943 2007-09-10 23:00:33Z mike $"
4 * User-defined destination (and option) support for the Common UNIX
5 * Printing System (CUPS).
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 1997-2007 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * cupsAddDest() - Add a destination to the list of destinations.
21 * cupsFreeDests() - Free the memory used by the list of destinations.
22 * cupsGetDest() - Get the named destination from the list.
23 * cupsGetDests() - Get the list of destinations from the default
25 * cupsGetDests2() - Get the list of destinations from the specified
27 * cupsGetNamedDest() - Get options for the named destination.
28 * cupsRemoveDest() - Remove a destination from the destination list.
29 * cupsSetDefaultDest() - Set the default destination.
30 * cupsSetDests() - Save the list of destinations for the default
32 * cupsSetDests2() - Save the list of destinations for the specified
34 * appleGetDefault() - Get the default printer for this location.
35 * appleGetLocations() - Get the location history array.
36 * appleGetNetwork() - Get the network ID for the current location.
37 * appleGetPrinter() - Get a printer from the history array.
38 * appleSetDefault() - Set the default printer for this location.
39 * appleUseLastPrinter() - Get the default printer preference value.
40 * cups_get_default() - Get the default destination from an lpoptions file.
41 * cups_get_dests() - Get destinations from a file.
42 * cups_get_sdests() - Get destinations from a server.
46 * Include necessary headers...
57 #endif /* HAVE_NOTIFY_H */
60 # include <sys/cdefs.h>
61 # include <CoreFoundation/CoreFoundation.h>
62 # include <SystemConfiguration/SystemConfiguration.h>
63 # define kLocationHistoryArrayKey CFSTR("kLocationHistoryArrayKeyTMP")
64 # define kLocationNetworkKey CFSTR("kLocationNetworkKey")
65 # define kLocationPrinterIDKey CFSTR("kLocationPrinterIDKey")
66 # define kPMPrintingPreferences CFSTR("com.apple.print.PrintingPrefs")
67 # define kUseLastPrinterAsCurrentPrinterKey CFSTR("UseLastPrinterAsCurrentPrinter")
68 #endif /* __APPLE__ */
76 static char *appleGetDefault(char *name
, int namesize
);
77 static CFArrayRef
appleGetLocations(void);
78 static CFStringRef
appleGetNetwork(void);
79 static CFStringRef
appleGetPrinter(CFArrayRef locations
, CFStringRef network
,
81 static void appleSetDefault(const char *name
);
82 static int appleUseLastPrinter(void);
83 #endif /* __APPLE__ */
84 static char *cups_get_default(const char *filename
, char *namebuf
,
85 size_t namesize
, const char **instance
);
86 static int cups_get_dests(const char *filename
, const char *match_name
,
87 const char *match_inst
, int num_dests
,
89 static int cups_get_sdests(http_t
*http
, ipp_op_t op
, const char *name
,
90 int num_dests
, cups_dest_t
**dests
);
94 * 'cupsAddDest()' - Add a destination to the list of destinations.
96 * This function cannot be used to add a new class or printer queue,
97 * it only adds a new container of saved options for the named
98 * destination or instance.
100 * If the named destination already exists, the destination list is
101 * returned unchanged. Adding a new instance of a destination creates
102 * a copy of that destination's options.
104 * Use the cupsSaveDests() function to save the updated list of
105 * destinations to the user's lpoptions file.
108 int /* O - New number of destinations */
109 cupsAddDest(const char *name
, /* I - Destination name */
110 const char *instance
, /* I - Instance name or NULL for none/primary */
111 int num_dests
, /* I - Number of destinations */
112 cups_dest_t
**dests
) /* IO - Destinations */
114 int i
; /* Looping var */
115 cups_dest_t
*dest
; /* Destination pointer */
116 cups_dest_t
*parent
; /* Parent destination */
117 cups_option_t
*option
; /* Current option */
123 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
127 * Add new destination...
131 dest
= malloc(sizeof(cups_dest_t
));
133 dest
= realloc(*dests
, sizeof(cups_dest_t
) * (num_dests
+ 1));
141 * Find where to insert the destination...
144 for (i
= num_dests
; i
> 0; i
--, dest
++)
145 if (strcasecmp(name
, dest
->name
) < 0)
147 else if (!instance
&& dest
->instance
)
149 else if (!strcasecmp(name
, dest
->name
) &&
150 instance
&& dest
->instance
&&
151 strcasecmp(instance
, dest
->instance
) < 0)
155 memmove(dest
+ 1, dest
, i
* sizeof(cups_dest_t
));
158 * Initialize the destination...
161 dest
->name
= _cupsStrAlloc(name
);
162 dest
->is_default
= 0;
163 dest
->num_options
= 0;
164 dest
->options
= (cups_option_t
*)0;
167 dest
->instance
= NULL
;
171 * Copy options from the primary instance...
174 dest
->instance
= _cupsStrAlloc(instance
);
176 if ((parent
= cupsGetDest(name
, NULL
, num_dests
+ 1, *dests
)) != NULL
)
178 for (i
= parent
->num_options
, option
= parent
->options
;
181 dest
->num_options
= cupsAddOption(option
->name
, option
->value
,
187 return (num_dests
+ 1);
192 * 'cupsFreeDests()' - Free the memory used by the list of destinations.
196 cupsFreeDests(int num_dests
, /* I - Number of destinations */
197 cups_dest_t
*dests
) /* I - Destinations */
199 int i
; /* Looping var */
200 cups_dest_t
*dest
; /* Current destination */
203 if (num_dests
== 0 || dests
== NULL
)
206 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
208 _cupsStrFree(dest
->name
);
209 _cupsStrFree(dest
->instance
);
211 cupsFreeOptions(dest
->num_options
, dest
->options
);
219 * 'cupsGetDest()' - Get the named destination from the list.
221 * Use the cupsGetDests() or cupsGetDests2() functions to get a
222 * list of supported destinations for the current user.
225 cups_dest_t
* /* O - Destination pointer or NULL */
226 cupsGetDest(const char *name
, /* I - Destination name or NULL for the default destination */
227 const char *instance
, /* I - Instance name or NULL */
228 int num_dests
, /* I - Number of destinations */
229 cups_dest_t
*dests
) /* I - Destinations */
231 int comp
; /* Result of comparison */
234 if (num_dests
<= 0 || !dests
)
240 * NULL name for default printer.
243 while (num_dests
> 0)
245 if (dests
->is_default
)
255 * Lookup name and optionally the instance...
258 while (num_dests
> 0)
260 if ((comp
= strcasecmp(name
, dests
->name
)) < 0)
264 if ((!instance
&& !dests
->instance
) ||
265 (instance
!= NULL
&& dests
->instance
!= NULL
&&
266 !strcasecmp(instance
, dests
->instance
)))
280 * 'cupsGetDests()' - Get the list of destinations from the default server.
282 * Starting with CUPS 1.2, the returned list of destinations include the
283 * printer-info, printer-is-accepting-jobs, printer-is-shared,
284 * printer-make-and-model, printer-state, printer-state-change-time,
285 * printer-state-reasons, and printer-type attributes as options.
287 * Use the cupsFreeDests() function to free the destination list and
288 * the cupsGetDest() function to find a particular destination.
291 int /* O - Number of destinations */
292 cupsGetDests(cups_dest_t
**dests
) /* O - Destinations */
294 return (cupsGetDests2(CUPS_HTTP_DEFAULT
, dests
));
299 * 'cupsGetDests2()' - Get the list of destinations from the specified server.
301 * Starting with CUPS 1.2, the returned list of destinations include the
302 * printer-info, printer-is-accepting-jobs, printer-is-shared,
303 * printer-make-and-model, printer-state, printer-state-change-time,
304 * printer-state-reasons, and printer-type attributes as options.
306 * Use the cupsFreeDests() function to free the destination list and
307 * the cupsGetDest() function to find a particular destination.
309 * @since CUPS 1.1.21@
312 int /* O - Number of destinations */
313 cupsGetDests2(http_t
*http
, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
314 cups_dest_t
**dests
) /* O - Destinations */
316 int i
; /* Looping var */
317 int num_dests
; /* Number of destinations */
318 cups_dest_t
*dest
; /* Destination pointer */
319 const char *home
; /* HOME environment variable */
320 char filename
[1024]; /* Local ~/.cups/lpoptions file */
321 const char *defprinter
; /* Default printer */
322 char name
[1024], /* Copy of printer name */
323 *instance
; /* Pointer to instance name */
324 int num_reals
; /* Number of real queues */
325 cups_dest_t
*reals
; /* Real queues */
326 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
330 * Range check the input...
337 * Initialize destination array...
341 *dests
= (cups_dest_t
*)0;
344 * Grab the printers and classes...
347 num_dests
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, NULL
, num_dests
, dests
);
348 num_dests
= cups_get_sdests(http
, CUPS_GET_CLASSES
, NULL
, num_dests
, dests
);
351 * Make a copy of the "real" queues for a later sanity check...
356 num_reals
= num_dests
;
357 reals
= calloc(num_reals
, sizeof(cups_dest_t
));
360 memcpy(reals
, *dests
, num_reals
* sizeof(cups_dest_t
));
371 * Grab the default destination...
375 if ((defprinter
= appleGetDefault(name
, sizeof(name
))) == NULL
)
376 #endif /* __APPLE__ */
377 defprinter
= cupsGetDefault2(http
);
382 * Grab printer and instance name...
386 if (name
!= defprinter
)
387 #endif /* __APPLE__ */
388 strlcpy(name
, defprinter
, sizeof(name
));
390 if ((instance
= strchr(name
, '/')) != NULL
)
394 * Lookup the printer and instance and make it the default...
397 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
398 dest
->is_default
= 1;
403 * This initialization of "instance" is unnecessary, but avoids a
404 * compiler warning...
411 * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files...
414 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
415 num_dests
= cups_get_dests(filename
, NULL
, NULL
, num_dests
, dests
);
417 if ((home
= getenv("HOME")) != NULL
)
419 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
420 if (access(filename
, 0))
421 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
423 num_dests
= cups_get_dests(filename
, NULL
, NULL
, num_dests
, dests
);
427 * Validate the current default destination - this prevents old
428 * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from
429 * pointing to a non-existent printer or class...
435 * See if we have a default printer...
438 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, *dests
)) != NULL
)
441 * Have a default; see if it is real...
444 dest
= cupsGetDest(dest
->name
, NULL
, num_reals
, reals
);
448 * If dest is NULL, then no default (that exists) is set, so we
449 * need to set a default if one exists...
452 if (dest
== NULL
&& defprinter
!= NULL
)
454 for (i
= 0; i
< num_dests
; i
++)
455 (*dests
)[i
].is_default
= 0;
457 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) != NULL
)
458 dest
->is_default
= 1;
469 * Return the number of destinations...
477 * 'cupsGetNamedDest()' - Get options for the named destination.
479 * This function is optimized for retrieving a single destination and should
480 * be used instead of cupsGetDests() and cupsGetDest() when you either know
481 * the name of the destination or want to print to the default destination.
482 * If NULL is returned, the destination does not exist or there is no default
485 * If "http" is CUPS_HTTP_DEFAULT, the connection to the default print server
488 * If "name" is NULL, the default printer for the current user will be returned.
490 * The returned destination must be freed using cupsFreeDests() with a
496 cups_dest_t
* /* O - Destination or NULL */
497 cupsGetNamedDest(http_t
*http
, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
498 const char *name
, /* I - Destination name or NULL */
499 const char *instance
) /* I - Instance name or NULL */
501 cups_dest_t
*dest
; /* Destination */
502 char filename
[1024], /* Path to lpoptions */
503 defname
[256]; /* Default printer name */
504 const char *home
= getenv("HOME"); /* Home directory */
505 ipp_op_t op
= IPP_GET_PRINTER_ATTRIBUTES
;
506 /* IPP operation to get server ops */
507 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
511 * If "name" is NULL, find the default destination...
516 if ((name
= getenv("LPDEST")) == NULL
)
517 if ((name
= getenv("PRINTER")) != NULL
&& !strcmp(name
, "lp"))
523 * No default in the environment, try the user's lpoptions files...
526 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
528 if ((name
= cups_get_default(filename
, defname
, sizeof(defname
),
531 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
532 name
= cups_get_default(filename
, defname
, sizeof(defname
),
540 * Still not there? Try the system lpoptions file...
543 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
544 cg
->cups_serverroot
);
545 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
551 * No locally-set default destination, ask the server...
554 op
= CUPS_GET_DEFAULT
;
559 * Get the printer's attributes...
562 if (!cups_get_sdests(http
, op
, name
, 0, &dest
))
566 dest
->instance
= _cupsStrAlloc(instance
);
569 * Then add local options...
572 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
573 cups_get_dests(filename
, name
, instance
, 1, &dest
);
577 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
579 if (access(filename
, 0))
580 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
582 cups_get_dests(filename
, name
, instance
, 1, &dest
);
586 * Return the result...
594 * 'cupsRemoveDest()' - Remove a destination from the destination list.
596 * Removing a destination/instance does not delete the class or printer
597 * queue, merely the lpoptions for that destination/instance. Use the
598 * cupsSetDests() or cupsSetDests2() functions to save the new options
604 int /* O - New number of destinations */
605 cupsRemoveDest(const char *name
, /* I - Destination name */
606 const char *instance
, /* I - Instance name or NULL */
607 int num_dests
, /* I - Number of destinations */
608 cups_dest_t
**dests
) /* IO - Destinations */
610 int i
; /* Index into destinations */
611 cups_dest_t
*dest
; /* Pointer to destination */
615 * Find the destination...
618 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
625 _cupsStrFree(dest
->name
);
626 _cupsStrFree(dest
->instance
);
627 cupsFreeOptions(dest
->num_options
, dest
->options
);
630 * Remove the destination from the array...
638 memmove(dest
, dest
+ 1, (num_dests
- i
) * sizeof(cups_dest_t
));
645 * 'cupsSetDefaultDest()' - Set the default destination.
652 const char *name
, /* I - Destination name */
653 const char *instance
, /* I - Instance name or NULL */
654 int num_dests
, /* I - Number of destinations */
655 cups_dest_t
*dests
) /* I - Destinations */
657 int i
; /* Looping var */
658 cups_dest_t
*dest
; /* Current destination */
662 * Range check input...
665 if (!name
|| num_dests
<= 0 || !dests
)
669 * Loop through the array and set the "is_default" flag for the matching
673 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
674 dest
->is_default
= !strcasecmp(name
, dest
->name
) &&
675 ((!instance
&& !dest
->instance
) ||
676 (instance
&& dest
->instance
&&
677 !strcasecmp(instance
, dest
->instance
)));
682 * 'cupsSetDests()' - Save the list of destinations for the default server.
684 * This function saves the destinations to /etc/cups/lpoptions when run
685 * as root and ~/.cups/lpoptions when run as a normal user.
689 cupsSetDests(int num_dests
, /* I - Number of destinations */
690 cups_dest_t
*dests
) /* I - Destinations */
692 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
697 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
699 * This function saves the destinations to /etc/cups/lpoptions when run
700 * as root and ~/.cups/lpoptions when run as a normal user.
702 * @since CUPS 1.1.21@
705 int /* O - 0 on success, -1 on error */
706 cupsSetDests2(http_t
*http
, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
707 int num_dests
, /* I - Number of destinations */
708 cups_dest_t
*dests
) /* I - Destinations */
710 int i
, j
; /* Looping vars */
711 int wrote
; /* Wrote definition? */
712 cups_dest_t
*dest
; /* Current destination */
713 cups_option_t
*option
; /* Current option */
714 _ipp_option_t
*match
; /* Matching attribute for option */
715 FILE *fp
; /* File pointer */
717 const char *home
; /* HOME environment variable */
719 char filename
[1024]; /* lpoptions file */
720 int num_temps
; /* Number of temporary destinations */
721 cups_dest_t
*temps
, /* Temporary destinations */
722 *temp
; /* Current temporary dest */
723 const char *val
; /* Value of temporary option */
724 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
728 * Range check the input...
731 if (!num_dests
|| !dests
)
735 * Get the server destinations...
738 num_temps
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, NULL
, 0, &temps
);
739 num_temps
= cups_get_sdests(http
, CUPS_GET_CLASSES
, NULL
, num_temps
, &temps
);
742 * Figure out which file to write to...
745 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
751 * Merge in server defaults...
754 num_temps
= cups_get_dests(filename
, NULL
, NULL
, num_temps
, &temps
);
757 * Point to user defaults...
760 if ((home
= getenv("HOME")) != NULL
)
763 * Remove the old ~/.lpoptions file...
766 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
770 * Create ~/.cups subdirectory...
773 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
774 if (access(filename
, 0))
775 mkdir(filename
, 0700);
777 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
783 * Try to open the file...
786 if ((fp
= fopen(filename
, "w")) == NULL
)
788 cupsFreeDests(num_temps
, temps
);
794 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
799 fchmod(fileno(fp
), 0644);
803 * Write each printer; each line looks like:
805 * Dest name[/instance] options
806 * Default name[/instance] options
809 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
810 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
812 if (dest
->is_default
)
814 fprintf(fp
, "Default %s", dest
->name
);
816 fprintf(fp
, "/%s", dest
->instance
);
823 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
824 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
826 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
829 * See if this option is a printer attribute; if so, skip it...
832 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
833 match
->group_tag
== IPP_TAG_PRINTER
)
837 * See if the server/global options match these; if so, don't
842 (val
= cupsGetOption(option
->name
, temp
->num_options
,
843 temp
->options
)) != NULL
&&
844 !strcasecmp(val
, option
->value
))
848 * Options don't match, write to the file...
853 fprintf(fp
, "Dest %s", dest
->name
);
855 fprintf(fp
, "/%s", dest
->instance
);
859 if (option
->value
[0])
861 if (strchr(option
->value
, ' ') ||
862 strchr(option
->value
, '\\') ||
863 strchr(option
->value
, '\"') ||
864 strchr(option
->value
, '\''))
870 fprintf(fp
, " %s=\"", option
->name
);
872 for (val
= option
->value
; *val
; val
++)
874 if (strchr("\"\'\\", *val
))
885 * Store the literal value...
888 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
892 fprintf(fp
, " %s", option
->name
);
900 * Free the temporary destinations and close the file...
903 cupsFreeDests(num_temps
, temps
);
909 * Set the default printer for this location - this allows command-line
910 * and GUI applications to share the same default destination...
913 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
914 appleSetDefault(dest
->name
);
915 #endif /* __APPLE__ */
917 #ifdef HAVE_NOTIFY_POST
919 * Send a notification so that MacOS X applications can know about the
923 notify_post("com.apple.printerListChange");
924 #endif /* HAVE_NOTIFY_POST */
932 * 'appleGetDefault()' - Get the default printer for this location.
935 static char * /* O - Name or NULL if no default */
936 appleGetDefault(char *name
, /* I - Name buffer */
937 int namesize
) /* I - Size of name buffer */
939 CFStringRef network
; /* Network location */
940 CFArrayRef locations
; /* Location array */
941 CFStringRef locprinter
; /* Current printer */
945 * Use location-based defaults if "use last printer" is selected in the
946 * system preferences...
949 if (!appleUseLastPrinter())
951 DEBUG_puts("appleGetDefault: Not using last printer as default...");
956 * Get the current location...
959 if ((network
= appleGetNetwork()) == NULL
)
961 DEBUG_puts("appleGetDefault: Unable to get current network...");
966 CFStringGetCString(network
, name
, namesize
, kCFStringEncodingUTF8
);
967 printf("appleGetDefault: network=\"%s\"\n", name
);
971 * Lookup the network in the preferences...
974 if ((locations
= appleGetLocations()) == NULL
)
977 * Missing or bad location array, so no location-based default...
980 DEBUG_puts("appleGetDefault: Missing or bad location history array...");
986 DEBUG_printf(("appleGetDefault: Got location, %d entries...\n",
987 (int)CFArrayGetCount(locations
)));
989 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
990 CFStringGetCString(locprinter
, name
, namesize
, kCFStringEncodingUTF8
);
994 CFRelease(locations
);
997 DEBUG_printf(("appleGetDefault: Returning \"%s\"...\n", name
));
999 return (*name
? name
: NULL
);
1004 * 'appleGetLocations()' - Get the location history array.
1007 static CFArrayRef
/* O - Location array or NULL */
1008 appleGetLocations(void)
1010 CFArrayRef locations
; /* Location array */
1014 * Look up the location array in the preferences...
1017 if ((locations
= CFPreferencesCopyAppValue(kLocationHistoryArrayKey
,
1018 kPMPrintingPreferences
)) == NULL
)
1021 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
1023 CFRelease(locations
);
1032 * 'appleGetNetwork()' - Get the network ID for the current location.
1035 static CFStringRef
/* O - Network ID */
1036 appleGetNetwork(void)
1038 SCDynamicStoreRef dynamicStore
; /* System configuration data */
1039 CFStringRef key
; /* Current network configuration key */
1040 CFDictionaryRef ip_dict
; /* Network configuration data */
1041 CFStringRef network
= NULL
; /* Current network ID */
1044 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("Printing"), NULL
,
1047 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
1048 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
1050 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
1052 if ((network
= CFDictionaryGetValue(ip_dict
,
1053 kSCPropNetIPv4Router
)) != NULL
)
1062 CFRelease(dynamicStore
);
1070 * 'appleGetPrinter()' - Get a printer from the history array.
1073 static CFStringRef
/* O - Printer name or NULL */
1074 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
1075 CFStringRef network
, /* I - Network name */
1076 CFIndex
*locindex
) /* O - Index in array */
1078 CFIndex i
, /* Looping var */
1079 count
; /* Number of locations */
1080 CFDictionaryRef location
; /* Current location */
1081 CFStringRef locnetwork
, /* Current network */
1082 locprinter
; /* Current printer */
1085 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
1086 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
1087 CFGetTypeID(location
) == CFDictionaryGetTypeID())
1089 if ((locnetwork
= CFDictionaryGetValue(location
,
1090 kLocationNetworkKey
)) != NULL
&&
1091 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
1092 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
1093 (locprinter
= CFDictionaryGetValue(location
,
1094 kLocationPrinterIDKey
)) != NULL
&&
1095 CFGetTypeID(locprinter
) == CFStringGetTypeID())
1100 return (locprinter
);
1109 * 'appleSetDefault()' - Set the default printer for this location.
1113 appleSetDefault(const char *name
) /* I - Default printer/class name */
1115 CFStringRef network
; /* Current network */
1116 CFArrayRef locations
; /* Old locations array */
1117 CFIndex locindex
; /* Index in locations array */
1118 CFStringRef locprinter
; /* Current printer */
1119 CFMutableArrayRef newlocations
; /* New locations array */
1120 CFMutableDictionaryRef newlocation
; /* New location */
1121 CFStringRef newprinter
; /* New printer */
1125 * Get the current location...
1128 if ((network
= appleGetNetwork()) == NULL
)
1130 DEBUG_puts("appleSetDefault: Unable to get current network...");
1134 if ((newprinter
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
1135 kCFStringEncodingUTF8
)) == NULL
)
1142 * Lookup the network in the preferences...
1145 if ((locations
= appleGetLocations()) != NULL
)
1146 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
1154 CFStringCompare(locprinter
, newprinter
, 0) != kCFCompareEqualTo
)
1157 * Need to change the locations array...
1162 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
1166 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
1169 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, NULL
);
1171 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
1172 &kCFTypeDictionaryKeyCallBacks
,
1173 &kCFTypeDictionaryValueCallBacks
);
1175 if (newlocation
&& newlocations
)
1178 * Put the new location at the front of the array...
1181 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
1182 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, newprinter
);
1183 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
1186 * Limit the number of locations to 10...
1189 while (CFArrayGetCount(newlocations
) > 10)
1190 CFArrayRemoveValueAtIndex(newlocations
, 10);
1193 * Push the changes out...
1196 CFPreferencesSetAppValue(kLocationHistoryArrayKey
, newlocations
,
1197 kPMPrintingPreferences
);
1198 CFPreferencesAppSynchronize(kPMPrintingPreferences
);
1202 CFRelease(newlocations
);
1205 CFRelease(newlocation
);
1209 CFRelease(locations
);
1211 CFRelease(newprinter
);
1216 * 'appleUseLastPrinter()' - Get the default printer preference value.
1219 static int /* O - 1 to use last printer, 0 otherwise */
1220 appleUseLastPrinter(void)
1222 CFPropertyListRef uselast
; /* Use last printer preference value */
1225 if ((uselast
= CFPreferencesCopyAppValue(kUseLastPrinterAsCurrentPrinterKey
,
1226 kPMPrintingPreferences
)) != NULL
)
1230 if (uselast
== kCFBooleanFalse
)
1236 #endif /* __APPLE__ */
1240 * 'cups_get_default()' - Get the default destination from an lpoptions file.
1243 static char * /* O - Default destination or NULL */
1244 cups_get_default(const char *filename
, /* I - File to read */
1245 char *namebuf
, /* I - Name buffer */
1246 size_t namesize
, /* I - Size of name buffer */
1247 const char **instance
) /* I - Instance */
1249 cups_file_t
*fp
; /* lpoptions file */
1250 char line
[8192], /* Line from file */
1251 *value
, /* Value for line */
1252 *nameptr
; /* Pointer into name */
1253 int linenum
; /* Current line */
1258 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1262 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1264 if (!strcasecmp(line
, "default") && value
)
1266 strlcpy(namebuf
, value
, namesize
);
1268 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
1270 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
1273 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
1276 *instance
= nameptr
;
1284 return (*namebuf
? namebuf
: NULL
);
1289 * 'cups_get_dests()' - Get destinations from a file.
1292 static int /* O - Number of destinations */
1293 cups_get_dests(const char *filename
, /* I - File to read from */
1294 const char *match_name
, /* I - Destination name we want */
1295 const char *match_inst
, /* I - Instance name we want */
1296 int num_dests
, /* I - Number of destinations */
1297 cups_dest_t
**dests
) /* IO - Destinations */
1299 int i
; /* Looping var */
1300 cups_dest_t
*dest
; /* Current destination */
1301 cups_file_t
*fp
; /* File pointer */
1302 char line
[8192], /* Line from file */
1303 *lineptr
, /* Pointer into line */
1304 *name
, /* Name of destination/option */
1305 *instance
; /* Instance of destination */
1306 int linenum
; /* Current line number */
1307 const char *printer
; /* PRINTER or LPDEST */
1310 DEBUG_printf(("cups_get_dests(filename=\"%s\", match_name=\"%s\", "
1311 "match_inst=\"%s\", num_dests=%d, dests=%p)\n", filename
,
1312 match_name
? match_name
: "(null)",
1313 match_inst
? match_inst
: "(null)", num_dests
, dests
));
1316 * Try to open the file...
1319 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1323 * Check environment variables...
1326 if ((printer
= getenv("LPDEST")) == NULL
)
1327 if ((printer
= getenv("PRINTER")) != NULL
)
1328 if (strcmp(printer
, "lp") == 0)
1331 DEBUG_printf(("cups_get_dests: printer=\"%s\"\n",
1332 printer
? printer
: "(null)"));
1335 * Read each printer; each line looks like:
1337 * Dest name[/instance] options
1338 * Default name[/instance] options
1343 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
1346 * See what type of line it is...
1349 DEBUG_printf(("cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"\n",
1350 linenum
, line
, lineptr
? lineptr
: "(null)"));
1352 if ((strcasecmp(line
, "dest") && strcasecmp(line
, "default")) || !lineptr
)
1354 DEBUG_puts("cups_get_dests: Not a dest or default line...");
1361 * Search for an instance...
1364 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
1367 if (*lineptr
== '/')
1370 * Found an instance...
1377 * Search for an instance...
1380 while (!isspace(*lineptr
& 255) && *lineptr
)
1389 DEBUG_printf(("cups_get_dests: name=\"%s\", instance=\"%s\"\n", name
,
1393 * See if the primary instance of the destination exists; if not,
1394 * ignore this entry and move on...
1399 if (strcasecmp(name
, match_name
) ||
1400 (!instance
&& match_inst
) ||
1401 (instance
&& !match_inst
) ||
1402 (instance
&& strcasecmp(instance
, match_inst
)))
1407 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
1409 DEBUG_puts("cups_get_dests: Not found!");
1415 * Add the destination...
1418 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
1420 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1426 DEBUG_puts("cups_get_dests: Out of memory!");
1432 * Add options until we hit the end of the line...
1435 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
1439 * If we found what we were looking for, stop now...
1446 * Set this as default if needed...
1449 if (!printer
&& !strcasecmp(line
, "default"))
1451 DEBUG_puts("cups_get_dests: Setting as default...");
1453 for (i
= 0; i
< num_dests
; i
++)
1454 (*dests
)[i
].is_default
= 0;
1456 dest
->is_default
= 1;
1461 * Close the file and return...
1471 * 'cups_get_sdests()' - Get destinations from a server.
1474 static int /* O - Number of destinations */
1475 cups_get_sdests(http_t
*http
, /* I - HTTP connection or CUPS_HTTP_DEFAULT */
1476 ipp_op_t op
, /* I - IPP operation */
1477 const char *name
, /* I - Name of destination */
1478 int num_dests
, /* I - Number of destinations */
1479 cups_dest_t
**dests
) /* IO - Destinations */
1481 int i
; /* Looping var */
1482 cups_dest_t
*dest
; /* Current destination */
1483 ipp_t
*request
, /* IPP Request */
1484 *response
; /* IPP Response */
1485 ipp_attribute_t
*attr
; /* Current attribute */
1486 int accepting
, /* printer-is-accepting-jobs attribute */
1487 shared
, /* printer-is-shared attribute */
1488 state
, /* printer-state attribute */
1489 change_time
, /* printer-state-change-time attribute */
1490 type
; /* printer-type attribute */
1491 const char *info
, /* printer-info attribute */
1492 *location
, /* printer-location attribute */
1493 *make_model
, /* printer-make-and-model attribute */
1494 *printer_name
; /* printer-name attribute */
1495 char uri
[1024], /* printer-uri value */
1496 job_sheets
[1024], /* job-sheets-default attribute */
1497 auth_info_req
[1024], /* auth-info-required attribute */
1498 reasons
[1024]; /* printer-state-reasons attribute */
1499 int num_options
; /* Number of options */
1500 cups_option_t
*options
; /* Options */
1501 char optname
[1024], /* Option name */
1502 value
[2048], /* Option value */
1503 *ptr
; /* Pointer into name/value */
1504 static const char * const pattrs
[] = /* Attributes we're interested in */
1506 "auth-info-required",
1507 "job-sheets-default",
1509 "printer-is-accepting-jobs",
1510 "printer-is-shared",
1512 "printer-make-and-model",
1515 "printer-state-change-time",
1516 "printer-state-reasons",
1523 * Build a CUPS_GET_PRINTERS or CUPS_GET_CLASSES request, which require
1524 * the following attributes:
1526 * attributes-charset
1527 * attributes-natural-language
1528 * requesting-user-name
1531 request
= ippNewRequest(op
);
1533 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1534 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1537 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1538 "requesting-user-name", NULL
, cupsUser());
1542 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1543 "localhost", ippPort(), "/printers/%s", name
);
1544 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1549 * Do the request and get back a response...
1552 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1554 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1557 * Skip leading attributes until we hit a printer...
1560 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1567 * Pull the needed attributes from this printer...
1575 printer_name
= NULL
;
1579 state
= IPP_PRINTER_IDLE
;
1580 type
= CUPS_PRINTER_LOCAL
;
1582 auth_info_req
[0] = '\0';
1583 job_sheets
[0] = '\0';
1586 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
1588 if (!strcmp(attr
->name
, "auth-info-required") &&
1589 attr
->value_tag
== IPP_TAG_KEYWORD
)
1591 strlcpy(auth_info_req
, attr
->values
[0].string
.text
,
1592 sizeof(auth_info_req
));
1594 for (i
= 1, ptr
= auth_info_req
+ strlen(auth_info_req
);
1595 i
< attr
->num_values
;
1598 snprintf(ptr
, sizeof(auth_info_req
) - (ptr
- auth_info_req
), ",%s",
1599 attr
->values
[i
].string
.text
);
1603 else if (!strcmp(attr
->name
, "job-sheets-default") &&
1604 (attr
->value_tag
== IPP_TAG_KEYWORD
||
1605 attr
->value_tag
== IPP_TAG_NAME
))
1607 if (attr
->num_values
== 2)
1608 snprintf(job_sheets
, sizeof(job_sheets
), "%s,%s",
1609 attr
->values
[0].string
.text
, attr
->values
[1].string
.text
);
1611 strlcpy(job_sheets
, attr
->values
[0].string
.text
,
1612 sizeof(job_sheets
));
1614 else if (!strcmp(attr
->name
, "printer-info") &&
1615 attr
->value_tag
== IPP_TAG_TEXT
)
1616 info
= attr
->values
[0].string
.text
;
1617 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
1618 attr
->value_tag
== IPP_TAG_BOOLEAN
)
1619 accepting
= attr
->values
[0].boolean
;
1620 else if (!strcmp(attr
->name
, "printer-is-shared") &&
1621 attr
->value_tag
== IPP_TAG_BOOLEAN
)
1622 shared
= attr
->values
[0].boolean
;
1623 else if (!strcmp(attr
->name
, "printer-location") &&
1624 attr
->value_tag
== IPP_TAG_TEXT
)
1625 location
= attr
->values
[0].string
.text
;
1626 else if (!strcmp(attr
->name
, "printer-make-and-model") &&
1627 attr
->value_tag
== IPP_TAG_TEXT
)
1628 make_model
= attr
->values
[0].string
.text
;
1629 else if (!strcmp(attr
->name
, "printer-name") &&
1630 attr
->value_tag
== IPP_TAG_NAME
)
1631 printer_name
= attr
->values
[0].string
.text
;
1632 else if (!strcmp(attr
->name
, "printer-state") &&
1633 attr
->value_tag
== IPP_TAG_ENUM
)
1634 state
= attr
->values
[0].integer
;
1635 else if (!strcmp(attr
->name
, "printer-state-change-time") &&
1636 attr
->value_tag
== IPP_TAG_INTEGER
)
1637 change_time
= attr
->values
[0].integer
;
1638 else if (!strcmp(attr
->name
, "printer-state-reasons") &&
1639 attr
->value_tag
== IPP_TAG_KEYWORD
)
1641 strlcpy(reasons
, attr
->values
[0].string
.text
, sizeof(reasons
));
1642 for (i
= 1, ptr
= reasons
+ strlen(reasons
);
1643 i
< attr
->num_values
;
1646 snprintf(ptr
, sizeof(reasons
) - (ptr
- reasons
), ",%s",
1647 attr
->values
[i
].string
.text
);
1651 else if (!strcmp(attr
->name
, "printer-type") &&
1652 attr
->value_tag
== IPP_TAG_ENUM
)
1653 type
= attr
->values
[0].integer
;
1654 else if (strncmp(attr
->name
, "notify-", 7) &&
1655 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1656 attr
->value_tag
== IPP_TAG_ENUM
||
1657 attr
->value_tag
== IPP_TAG_INTEGER
||
1658 attr
->value_tag
== IPP_TAG_KEYWORD
||
1659 attr
->value_tag
== IPP_TAG_NAME
||
1660 attr
->value_tag
== IPP_TAG_RANGE
) &&
1661 strstr(attr
->name
, "-default"))
1663 char *valptr
; /* Pointer into attribute value */
1667 * Add a default option...
1670 strlcpy(optname
, attr
->name
, sizeof(optname
));
1671 if ((ptr
= strstr(optname
, "-default")) != NULL
)
1675 for (i
= 0, ptr
= value
; i
< attr
->num_values
; i
++)
1677 if (ptr
>= (value
+ sizeof(value
) - 1))
1683 switch (attr
->value_tag
)
1685 case IPP_TAG_INTEGER
:
1687 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d",
1688 attr
->values
[i
].integer
);
1691 case IPP_TAG_BOOLEAN
:
1692 if (attr
->values
[i
].boolean
)
1693 strlcpy(ptr
, "true", sizeof(value
) - (ptr
- value
));
1695 strlcpy(ptr
, "false", sizeof(value
) - (ptr
- value
));
1698 case IPP_TAG_RANGE
:
1699 if (attr
->values
[i
].range
.lower
==
1700 attr
->values
[i
].range
.upper
)
1701 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d",
1702 attr
->values
[i
].range
.lower
);
1704 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d-%d",
1705 attr
->values
[i
].range
.lower
,
1706 attr
->values
[i
].range
.upper
);
1710 for (valptr
= attr
->values
[i
].string
.text
;
1711 *valptr
&& ptr
< (value
+ sizeof(value
) - 2);)
1713 if (strchr(" \t\n\\\'\"", *valptr
))
1726 num_options
= cupsAddOption(optname
, value
, num_options
, &options
);
1733 * See if we have everything needed...
1738 cupsFreeOptions(num_options
, options
);
1746 num_dests
= cupsAddDest(printer_name
, NULL
, num_dests
, dests
);
1748 if ((dest
= cupsGetDest(printer_name
, NULL
, num_dests
, *dests
)) != NULL
)
1750 dest
->num_options
= num_options
;
1751 dest
->options
= options
;
1756 if (auth_info_req
[0])
1757 dest
->num_options
= cupsAddOption("auth-info-required", auth_info_req
,
1762 dest
->num_options
= cupsAddOption("job-sheets", job_sheets
,
1767 dest
->num_options
= cupsAddOption("printer-info", info
,
1771 sprintf(value
, "%d", accepting
);
1772 dest
->num_options
= cupsAddOption("printer-is-accepting-jobs", value
,
1776 sprintf(value
, "%d", shared
);
1777 dest
->num_options
= cupsAddOption("printer-is-shared", value
,
1782 dest
->num_options
= cupsAddOption("printer-location",
1783 location
, dest
->num_options
,
1787 dest
->num_options
= cupsAddOption("printer-make-and-model",
1788 make_model
, dest
->num_options
,
1791 sprintf(value
, "%d", state
);
1792 dest
->num_options
= cupsAddOption("printer-state", value
,
1798 sprintf(value
, "%d", change_time
);
1799 dest
->num_options
= cupsAddOption("printer-state-change-time", value
,
1805 dest
->num_options
= cupsAddOption("printer-state-reasons", reasons
,
1809 sprintf(value
, "%d", type
);
1810 dest
->num_options
= cupsAddOption("printer-type", value
,
1815 cupsFreeOptions(num_options
, options
);
1821 ippDelete(response
);
1825 * Return the count...
1833 * End of "$Id: dest.c 6943 2007-09-10 23:00:33Z mike $".