2 * "$Id: dest.c 7460 2008-04-16 02:19:54Z 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 @link 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 @code 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 (cupsGetDest(name
, instance
, num_dests
, *dests
))
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 @link cupsGetDests@ or @link cupsGetDests2@ functions to get a
222 * list of supported destinations for the current user.
225 cups_dest_t
* /* O - Destination pointer or @code NULL@ */
226 cupsGetDest(const char *name
, /* I - Destination name or @code NULL@ for the default destination */
227 const char *instance
, /* I - Instance name or @code 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 @link cupsFreeDests@ function to free the destination list and
288 * the @link 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 @link cupsFreeDests@ function to free the destination list and
307 * the @link 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 - Connection to server or @code 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 @link cupsGetDests@ and @link cupsGetDest@ when you either
481 * know the name of the destination or want to print to the default destination.
482 * If @code NULL@ is returned, the destination does not exist or there is no
483 * default destination.
485 * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print
486 * server will be used.
488 * If "name" is @code NULL@, the default printer for the current user will be
491 * The returned destination must be freed using @link cupsFreeDests@ with a
492 * "num_dests" value of 1.
497 cups_dest_t
* /* O - Destination or @code NULL@ */
498 cupsGetNamedDest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
499 const char *name
, /* I - Destination name or @code NULL@ for the default destination */
500 const char *instance
) /* I - Instance name or @code NULL@ */
502 cups_dest_t
*dest
; /* Destination */
503 char filename
[1024], /* Path to lpoptions */
504 defname
[256]; /* Default printer name */
505 const char *home
= getenv("HOME"); /* Home directory */
506 ipp_op_t op
= IPP_GET_PRINTER_ATTRIBUTES
;
507 /* IPP operation to get server ops */
508 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
512 * If "name" is NULL, find the default destination...
517 if ((name
= getenv("LPDEST")) == NULL
)
518 if ((name
= getenv("PRINTER")) != NULL
&& !strcmp(name
, "lp"))
524 * No default in the environment, try the user's lpoptions files...
527 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
529 if ((name
= cups_get_default(filename
, defname
, sizeof(defname
),
532 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
533 name
= cups_get_default(filename
, defname
, sizeof(defname
),
541 * Still not there? Try the system lpoptions file...
544 snprintf(filename
, sizeof(filename
), "%s/lpoptions",
545 cg
->cups_serverroot
);
546 name
= cups_get_default(filename
, defname
, sizeof(defname
), &instance
);
552 * No locally-set default destination, ask the server...
555 op
= CUPS_GET_DEFAULT
;
560 * Get the printer's attributes...
563 if (!cups_get_sdests(http
, op
, name
, 0, &dest
))
567 dest
->instance
= _cupsStrAlloc(instance
);
570 * Then add local options...
573 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
574 cups_get_dests(filename
, name
, instance
, 1, &dest
);
578 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
580 if (access(filename
, 0))
581 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
583 cups_get_dests(filename
, name
, instance
, 1, &dest
);
587 * Return the result...
595 * 'cupsRemoveDest()' - Remove a destination from the destination list.
597 * Removing a destination/instance does not delete the class or printer
598 * queue, merely the lpoptions for that destination/instance. Use the
599 * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new
600 * options for the user.
605 int /* O - New number of destinations */
606 cupsRemoveDest(const char *name
, /* I - Destination name */
607 const char *instance
, /* I - Instance name or @code NULL@ */
608 int num_dests
, /* I - Number of destinations */
609 cups_dest_t
**dests
) /* IO - Destinations */
611 int i
; /* Index into destinations */
612 cups_dest_t
*dest
; /* Pointer to destination */
616 * Find the destination...
619 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
626 _cupsStrFree(dest
->name
);
627 _cupsStrFree(dest
->instance
);
628 cupsFreeOptions(dest
->num_options
, dest
->options
);
631 * Remove the destination from the array...
639 memmove(dest
, dest
+ 1, (num_dests
- i
) * sizeof(cups_dest_t
));
646 * 'cupsSetDefaultDest()' - Set the default destination.
653 const char *name
, /* I - Destination name */
654 const char *instance
, /* I - Instance name or @code NULL@ */
655 int num_dests
, /* I - Number of destinations */
656 cups_dest_t
*dests
) /* I - Destinations */
658 int i
; /* Looping var */
659 cups_dest_t
*dest
; /* Current destination */
663 * Range check input...
666 if (!name
|| num_dests
<= 0 || !dests
)
670 * Loop through the array and set the "is_default" flag for the matching
674 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
675 dest
->is_default
= !strcasecmp(name
, dest
->name
) &&
676 ((!instance
&& !dest
->instance
) ||
677 (instance
&& dest
->instance
&&
678 !strcasecmp(instance
, dest
->instance
)));
683 * 'cupsSetDests()' - Save the list of destinations for the default server.
685 * This function saves the destinations to /etc/cups/lpoptions when run
686 * as root and ~/.cups/lpoptions when run as a normal user.
690 cupsSetDests(int num_dests
, /* I - Number of destinations */
691 cups_dest_t
*dests
) /* I - Destinations */
693 cupsSetDests2(CUPS_HTTP_DEFAULT
, num_dests
, dests
);
698 * 'cupsSetDests2()' - Save the list of destinations for the specified server.
700 * This function saves the destinations to /etc/cups/lpoptions when run
701 * as root and ~/.cups/lpoptions when run as a normal user.
703 * @since CUPS 1.1.21@
706 int /* O - 0 on success, -1 on error */
707 cupsSetDests2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
708 int num_dests
, /* I - Number of destinations */
709 cups_dest_t
*dests
) /* I - Destinations */
711 int i
, j
; /* Looping vars */
712 int wrote
; /* Wrote definition? */
713 cups_dest_t
*dest
; /* Current destination */
714 cups_option_t
*option
; /* Current option */
715 _ipp_option_t
*match
; /* Matching attribute for option */
716 FILE *fp
; /* File pointer */
718 const char *home
; /* HOME environment variable */
720 char filename
[1024]; /* lpoptions file */
721 int num_temps
; /* Number of temporary destinations */
722 cups_dest_t
*temps
, /* Temporary destinations */
723 *temp
; /* Current temporary dest */
724 const char *val
; /* Value of temporary option */
725 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
729 * Range check the input...
732 if (!num_dests
|| !dests
)
736 * Get the server destinations...
739 num_temps
= cups_get_sdests(http
, CUPS_GET_PRINTERS
, NULL
, 0, &temps
);
740 num_temps
= cups_get_sdests(http
, CUPS_GET_CLASSES
, NULL
, num_temps
, &temps
);
743 * Figure out which file to write to...
746 snprintf(filename
, sizeof(filename
), "%s/lpoptions", cg
->cups_serverroot
);
752 * Merge in server defaults...
755 num_temps
= cups_get_dests(filename
, NULL
, NULL
, num_temps
, &temps
);
758 * Point to user defaults...
761 if ((home
= getenv("HOME")) != NULL
)
764 * Remove the old ~/.lpoptions file...
767 snprintf(filename
, sizeof(filename
), "%s/.lpoptions", home
);
771 * Create ~/.cups subdirectory...
774 snprintf(filename
, sizeof(filename
), "%s/.cups", home
);
775 if (access(filename
, 0))
776 mkdir(filename
, 0700);
778 snprintf(filename
, sizeof(filename
), "%s/.cups/lpoptions", home
);
784 * Try to open the file...
787 if ((fp
= fopen(filename
, "w")) == NULL
)
789 cupsFreeDests(num_temps
, temps
);
795 * Set the permissions to 0644 when saving to the /etc/cups/lpoptions
800 fchmod(fileno(fp
), 0644);
804 * Write each printer; each line looks like:
806 * Dest name[/instance] options
807 * Default name[/instance] options
810 for (i
= num_dests
, dest
= dests
; i
> 0; i
--, dest
++)
811 if (dest
->instance
!= NULL
|| dest
->num_options
!= 0 || dest
->is_default
)
813 if (dest
->is_default
)
815 fprintf(fp
, "Default %s", dest
->name
);
817 fprintf(fp
, "/%s", dest
->instance
);
824 if ((temp
= cupsGetDest(dest
->name
, dest
->instance
, num_temps
, temps
)) == NULL
)
825 temp
= cupsGetDest(dest
->name
, NULL
, num_temps
, temps
);
827 for (j
= dest
->num_options
, option
= dest
->options
; j
> 0; j
--, option
++)
830 * See if this option is a printer attribute; if so, skip it...
833 if ((match
= _ippFindOption(option
->name
)) != NULL
&&
834 match
->group_tag
== IPP_TAG_PRINTER
)
838 * See if the server/global options match these; if so, don't
843 (val
= cupsGetOption(option
->name
, temp
->num_options
,
844 temp
->options
)) != NULL
&&
845 !strcasecmp(val
, option
->value
))
849 * Options don't match, write to the file...
854 fprintf(fp
, "Dest %s", dest
->name
);
856 fprintf(fp
, "/%s", dest
->instance
);
860 if (option
->value
[0])
862 if (strchr(option
->value
, ' ') ||
863 strchr(option
->value
, '\\') ||
864 strchr(option
->value
, '\"') ||
865 strchr(option
->value
, '\''))
871 fprintf(fp
, " %s=\"", option
->name
);
873 for (val
= option
->value
; *val
; val
++)
875 if (strchr("\"\'\\", *val
))
886 * Store the literal value...
889 fprintf(fp
, " %s=%s", option
->name
, option
->value
);
893 fprintf(fp
, " %s", option
->name
);
901 * Free the temporary destinations and close the file...
904 cupsFreeDests(num_temps
, temps
);
910 * Set the default printer for this location - this allows command-line
911 * and GUI applications to share the same default destination...
914 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
915 appleSetDefault(dest
->name
);
916 #endif /* __APPLE__ */
918 #ifdef HAVE_NOTIFY_POST
920 * Send a notification so that MacOS X applications can know about the
924 notify_post("com.apple.printerListChange");
925 #endif /* HAVE_NOTIFY_POST */
933 * 'appleGetDefault()' - Get the default printer for this location.
936 static char * /* O - Name or NULL if no default */
937 appleGetDefault(char *name
, /* I - Name buffer */
938 int namesize
) /* I - Size of name buffer */
940 CFStringRef network
; /* Network location */
941 CFArrayRef locations
; /* Location array */
942 CFStringRef locprinter
; /* Current printer */
946 * Use location-based defaults if "use last printer" is selected in the
947 * system preferences...
950 if (!appleUseLastPrinter())
952 DEBUG_puts("appleGetDefault: Not using last printer as default...");
957 * Get the current location...
960 if ((network
= appleGetNetwork()) == NULL
)
962 DEBUG_puts("appleGetDefault: Unable to get current network...");
967 CFStringGetCString(network
, name
, namesize
, kCFStringEncodingUTF8
);
968 DEBUG_printf(("appleGetDefault: network=\"%s\"\n", name
));
972 * Lookup the network in the preferences...
975 if ((locations
= appleGetLocations()) == NULL
)
978 * Missing or bad location array, so no location-based default...
981 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 DEBUG_printf(("appleGetDefault: Returning \"%s\"...\n", name
));
996 return (*name
? name
: NULL
);
1001 * 'appleGetLocations()' - Get the location history array.
1004 static CFArrayRef
/* O - Location array or NULL */
1005 appleGetLocations(void)
1007 CFArrayRef locations
; /* Location array */
1011 * Look up the location array in the preferences...
1014 if ((locations
= CFPreferencesCopyAppValue(kLocationHistoryArrayKey
,
1015 kPMPrintingPreferences
)) == NULL
)
1018 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
1020 CFRelease(locations
);
1029 * 'appleGetNetwork()' - Get the network ID for the current location.
1032 static CFStringRef
/* O - Network ID */
1033 appleGetNetwork(void)
1035 SCDynamicStoreRef dynamicStore
; /* System configuration data */
1036 CFStringRef key
; /* Current network configuration key */
1037 CFDictionaryRef ip_dict
; /* Network configuration data */
1038 CFStringRef network
= NULL
; /* Current network ID */
1041 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("Printing"), NULL
,
1044 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
1045 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
1047 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
1049 if ((network
= CFDictionaryGetValue(ip_dict
,
1050 kSCPropNetIPv4Router
)) != NULL
)
1059 CFRelease(dynamicStore
);
1067 * 'appleGetPrinter()' - Get a printer from the history array.
1070 static CFStringRef
/* O - Printer name or NULL */
1071 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
1072 CFStringRef network
, /* I - Network name */
1073 CFIndex
*locindex
) /* O - Index in array */
1075 CFIndex i
, /* Looping var */
1076 count
; /* Number of locations */
1077 CFDictionaryRef location
; /* Current location */
1078 CFStringRef locnetwork
, /* Current network */
1079 locprinter
; /* Current printer */
1082 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
1083 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
1084 CFGetTypeID(location
) == CFDictionaryGetTypeID())
1086 if ((locnetwork
= CFDictionaryGetValue(location
,
1087 kLocationNetworkKey
)) != NULL
&&
1088 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
1089 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
1090 (locprinter
= CFDictionaryGetValue(location
,
1091 kLocationPrinterIDKey
)) != NULL
&&
1092 CFGetTypeID(locprinter
) == CFStringGetTypeID())
1097 return (locprinter
);
1106 * 'appleSetDefault()' - Set the default printer for this location.
1110 appleSetDefault(const char *name
) /* I - Default printer/class name */
1112 CFStringRef network
; /* Current network */
1113 CFArrayRef locations
; /* Old locations array */
1114 CFIndex locindex
; /* Index in locations array */
1115 CFStringRef locprinter
; /* Current printer */
1116 CFMutableArrayRef newlocations
; /* New locations array */
1117 CFMutableDictionaryRef newlocation
; /* New location */
1118 CFStringRef newprinter
; /* New printer */
1122 * Get the current location...
1125 if ((network
= appleGetNetwork()) == NULL
)
1127 DEBUG_puts("appleSetDefault: Unable to get current network...");
1131 if ((newprinter
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
1132 kCFStringEncodingUTF8
)) == NULL
)
1139 * Lookup the network in the preferences...
1142 if ((locations
= appleGetLocations()) != NULL
)
1143 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
1151 CFStringCompare(locprinter
, newprinter
, 0) != kCFCompareEqualTo
)
1154 * Need to change the locations array...
1159 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
1163 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
1166 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, NULL
);
1168 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
1169 &kCFTypeDictionaryKeyCallBacks
,
1170 &kCFTypeDictionaryValueCallBacks
);
1172 if (newlocation
&& newlocations
)
1175 * Put the new location at the front of the array...
1178 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
1179 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, newprinter
);
1180 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
1183 * Limit the number of locations to 10...
1186 while (CFArrayGetCount(newlocations
) > 10)
1187 CFArrayRemoveValueAtIndex(newlocations
, 10);
1190 * Push the changes out...
1193 CFPreferencesSetAppValue(kLocationHistoryArrayKey
, newlocations
,
1194 kPMPrintingPreferences
);
1195 CFPreferencesAppSynchronize(kPMPrintingPreferences
);
1199 CFRelease(newlocations
);
1202 CFRelease(newlocation
);
1205 CFRelease(newprinter
);
1210 * 'appleUseLastPrinter()' - Get the default printer preference value.
1213 static int /* O - 1 to use last printer, 0 otherwise */
1214 appleUseLastPrinter(void)
1216 CFPropertyListRef uselast
; /* Use last printer preference value */
1219 if ((uselast
= CFPreferencesCopyAppValue(kUseLastPrinterAsCurrentPrinterKey
,
1220 kPMPrintingPreferences
)) != NULL
)
1224 if (uselast
== kCFBooleanFalse
)
1230 #endif /* __APPLE__ */
1234 * 'cups_get_default()' - Get the default destination from an lpoptions file.
1237 static char * /* O - Default destination or NULL */
1238 cups_get_default(const char *filename
, /* I - File to read */
1239 char *namebuf
, /* I - Name buffer */
1240 size_t namesize
, /* I - Size of name buffer */
1241 const char **instance
) /* I - Instance */
1243 cups_file_t
*fp
; /* lpoptions file */
1244 char line
[8192], /* Line from file */
1245 *value
, /* Value for line */
1246 *nameptr
; /* Pointer into name */
1247 int linenum
; /* Current line */
1252 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1256 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1258 if (!strcasecmp(line
, "default") && value
)
1260 strlcpy(namebuf
, value
, namesize
);
1262 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
1264 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
1267 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
1270 *instance
= nameptr
;
1278 return (*namebuf
? namebuf
: NULL
);
1283 * 'cups_get_dests()' - Get destinations from a file.
1286 static int /* O - Number of destinations */
1287 cups_get_dests(const char *filename
, /* I - File to read from */
1288 const char *match_name
, /* I - Destination name we want */
1289 const char *match_inst
, /* I - Instance name we want */
1290 int num_dests
, /* I - Number of destinations */
1291 cups_dest_t
**dests
) /* IO - Destinations */
1293 int i
; /* Looping var */
1294 cups_dest_t
*dest
; /* Current destination */
1295 cups_file_t
*fp
; /* File pointer */
1296 char line
[8192], /* Line from file */
1297 *lineptr
, /* Pointer into line */
1298 *name
, /* Name of destination/option */
1299 *instance
; /* Instance of destination */
1300 int linenum
; /* Current line number */
1301 const char *printer
; /* PRINTER or LPDEST */
1304 DEBUG_printf(("cups_get_dests(filename=\"%s\", match_name=\"%s\", "
1305 "match_inst=\"%s\", num_dests=%d, dests=%p)\n", filename
,
1306 match_name
? match_name
: "(null)",
1307 match_inst
? match_inst
: "(null)", num_dests
, dests
));
1310 * Try to open the file...
1313 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1317 * Check environment variables...
1320 if ((printer
= getenv("LPDEST")) == NULL
)
1321 if ((printer
= getenv("PRINTER")) != NULL
)
1322 if (strcmp(printer
, "lp") == 0)
1325 DEBUG_printf(("cups_get_dests: printer=\"%s\"\n",
1326 printer
? printer
: "(null)"));
1329 * Read each printer; each line looks like:
1331 * Dest name[/instance] options
1332 * Default name[/instance] options
1337 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
1340 * See what type of line it is...
1343 DEBUG_printf(("cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"\n",
1344 linenum
, line
, lineptr
? lineptr
: "(null)"));
1346 if ((strcasecmp(line
, "dest") && strcasecmp(line
, "default")) || !lineptr
)
1348 DEBUG_puts("cups_get_dests: Not a dest or default line...");
1355 * Search for an instance...
1358 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
1361 if (*lineptr
== '/')
1364 * Found an instance...
1371 * Search for an instance...
1374 while (!isspace(*lineptr
& 255) && *lineptr
)
1383 DEBUG_printf(("cups_get_dests: name=\"%s\", instance=\"%s\"\n", name
,
1387 * See if the primary instance of the destination exists; if not,
1388 * ignore this entry and move on...
1393 if (strcasecmp(name
, match_name
) ||
1394 (!instance
&& match_inst
) ||
1395 (instance
&& !match_inst
) ||
1396 (instance
&& strcasecmp(instance
, match_inst
)))
1401 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
1403 DEBUG_puts("cups_get_dests: Not found!");
1409 * Add the destination...
1412 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
1414 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1420 DEBUG_puts("cups_get_dests: Out of memory!");
1426 * Add options until we hit the end of the line...
1429 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
1433 * If we found what we were looking for, stop now...
1440 * Set this as default if needed...
1443 if (!printer
&& !strcasecmp(line
, "default"))
1445 DEBUG_puts("cups_get_dests: Setting as default...");
1447 for (i
= 0; i
< num_dests
; i
++)
1448 (*dests
)[i
].is_default
= 0;
1450 dest
->is_default
= 1;
1455 * Close the file and return...
1465 * 'cups_get_sdests()' - Get destinations from a server.
1468 static int /* O - Number of destinations */
1469 cups_get_sdests(http_t
*http
, /* I - Connection to server or CUPS_HTTP_DEFAULT */
1470 ipp_op_t op
, /* I - IPP operation */
1471 const char *name
, /* I - Name of destination */
1472 int num_dests
, /* I - Number of destinations */
1473 cups_dest_t
**dests
) /* IO - Destinations */
1475 int i
; /* Looping var */
1476 cups_dest_t
*dest
; /* Current destination */
1477 ipp_t
*request
, /* IPP Request */
1478 *response
; /* IPP Response */
1479 ipp_attribute_t
*attr
; /* Current attribute */
1480 int accepting
, /* printer-is-accepting-jobs attribute */
1481 shared
, /* printer-is-shared attribute */
1482 state
, /* printer-state attribute */
1483 change_time
, /* printer-state-change-time attribute */
1484 type
; /* printer-type attribute */
1485 const char *info
, /* printer-info attribute */
1486 *location
, /* printer-location attribute */
1487 *make_model
, /* printer-make-and-model attribute */
1488 *printer_name
; /* printer-name attribute */
1489 char uri
[1024], /* printer-uri value */
1490 job_sheets
[1024], /* job-sheets-default attribute */
1491 auth_info_req
[1024], /* auth-info-required attribute */
1492 reasons
[1024]; /* printer-state-reasons attribute */
1493 int num_options
; /* Number of options */
1494 cups_option_t
*options
; /* Options */
1495 char optname
[1024], /* Option name */
1496 value
[2048], /* Option value */
1497 *ptr
; /* Pointer into name/value */
1498 static const char * const pattrs
[] = /* Attributes we're interested in */
1500 "auth-info-required",
1501 "job-sheets-default",
1503 "printer-is-accepting-jobs",
1504 "printer-is-shared",
1506 "printer-make-and-model",
1509 "printer-state-change-time",
1510 "printer-state-reasons",
1517 * Build a CUPS_GET_PRINTERS or CUPS_GET_CLASSES request, which require
1518 * the following attributes:
1520 * attributes-charset
1521 * attributes-natural-language
1522 * requesting-user-name
1525 request
= ippNewRequest(op
);
1527 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1528 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1531 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1532 "requesting-user-name", NULL
, cupsUser());
1536 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1537 "localhost", ippPort(), "/printers/%s", name
);
1538 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1543 * Do the request and get back a response...
1546 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1548 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1551 * Skip leading attributes until we hit a printer...
1554 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1561 * Pull the needed attributes from this printer...
1569 printer_name
= NULL
;
1573 state
= IPP_PRINTER_IDLE
;
1574 type
= CUPS_PRINTER_LOCAL
;
1576 auth_info_req
[0] = '\0';
1577 job_sheets
[0] = '\0';
1580 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
1582 if (!strcmp(attr
->name
, "auth-info-required") &&
1583 attr
->value_tag
== IPP_TAG_KEYWORD
)
1585 strlcpy(auth_info_req
, attr
->values
[0].string
.text
,
1586 sizeof(auth_info_req
));
1588 for (i
= 1, ptr
= auth_info_req
+ strlen(auth_info_req
);
1589 i
< attr
->num_values
;
1592 snprintf(ptr
, sizeof(auth_info_req
) - (ptr
- auth_info_req
), ",%s",
1593 attr
->values
[i
].string
.text
);
1597 else if (!strcmp(attr
->name
, "job-sheets-default") &&
1598 (attr
->value_tag
== IPP_TAG_KEYWORD
||
1599 attr
->value_tag
== IPP_TAG_NAME
))
1601 if (attr
->num_values
== 2)
1602 snprintf(job_sheets
, sizeof(job_sheets
), "%s,%s",
1603 attr
->values
[0].string
.text
, attr
->values
[1].string
.text
);
1605 strlcpy(job_sheets
, attr
->values
[0].string
.text
,
1606 sizeof(job_sheets
));
1608 else if (!strcmp(attr
->name
, "printer-info") &&
1609 attr
->value_tag
== IPP_TAG_TEXT
)
1610 info
= attr
->values
[0].string
.text
;
1611 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
1612 attr
->value_tag
== IPP_TAG_BOOLEAN
)
1613 accepting
= attr
->values
[0].boolean
;
1614 else if (!strcmp(attr
->name
, "printer-is-shared") &&
1615 attr
->value_tag
== IPP_TAG_BOOLEAN
)
1616 shared
= attr
->values
[0].boolean
;
1617 else if (!strcmp(attr
->name
, "printer-location") &&
1618 attr
->value_tag
== IPP_TAG_TEXT
)
1619 location
= attr
->values
[0].string
.text
;
1620 else if (!strcmp(attr
->name
, "printer-make-and-model") &&
1621 attr
->value_tag
== IPP_TAG_TEXT
)
1622 make_model
= attr
->values
[0].string
.text
;
1623 else if (!strcmp(attr
->name
, "printer-name") &&
1624 attr
->value_tag
== IPP_TAG_NAME
)
1625 printer_name
= attr
->values
[0].string
.text
;
1626 else if (!strcmp(attr
->name
, "printer-state") &&
1627 attr
->value_tag
== IPP_TAG_ENUM
)
1628 state
= attr
->values
[0].integer
;
1629 else if (!strcmp(attr
->name
, "printer-state-change-time") &&
1630 attr
->value_tag
== IPP_TAG_INTEGER
)
1631 change_time
= attr
->values
[0].integer
;
1632 else if (!strcmp(attr
->name
, "printer-state-reasons") &&
1633 attr
->value_tag
== IPP_TAG_KEYWORD
)
1635 strlcpy(reasons
, attr
->values
[0].string
.text
, sizeof(reasons
));
1636 for (i
= 1, ptr
= reasons
+ strlen(reasons
);
1637 i
< attr
->num_values
;
1640 snprintf(ptr
, sizeof(reasons
) - (ptr
- reasons
), ",%s",
1641 attr
->values
[i
].string
.text
);
1645 else if (!strcmp(attr
->name
, "printer-type") &&
1646 attr
->value_tag
== IPP_TAG_ENUM
)
1647 type
= attr
->values
[0].integer
;
1648 else if (strncmp(attr
->name
, "notify-", 7) &&
1649 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1650 attr
->value_tag
== IPP_TAG_ENUM
||
1651 attr
->value_tag
== IPP_TAG_INTEGER
||
1652 attr
->value_tag
== IPP_TAG_KEYWORD
||
1653 attr
->value_tag
== IPP_TAG_NAME
||
1654 attr
->value_tag
== IPP_TAG_RANGE
) &&
1655 strstr(attr
->name
, "-default"))
1657 char *valptr
; /* Pointer into attribute value */
1661 * Add a default option...
1664 strlcpy(optname
, attr
->name
, sizeof(optname
));
1665 if ((ptr
= strstr(optname
, "-default")) != NULL
)
1669 for (i
= 0, ptr
= value
; i
< attr
->num_values
; i
++)
1671 if (ptr
>= (value
+ sizeof(value
) - 1))
1677 switch (attr
->value_tag
)
1679 case IPP_TAG_INTEGER
:
1681 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d",
1682 attr
->values
[i
].integer
);
1685 case IPP_TAG_BOOLEAN
:
1686 if (attr
->values
[i
].boolean
)
1687 strlcpy(ptr
, "true", sizeof(value
) - (ptr
- value
));
1689 strlcpy(ptr
, "false", sizeof(value
) - (ptr
- value
));
1692 case IPP_TAG_RANGE
:
1693 if (attr
->values
[i
].range
.lower
==
1694 attr
->values
[i
].range
.upper
)
1695 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d",
1696 attr
->values
[i
].range
.lower
);
1698 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d-%d",
1699 attr
->values
[i
].range
.lower
,
1700 attr
->values
[i
].range
.upper
);
1704 for (valptr
= attr
->values
[i
].string
.text
;
1705 *valptr
&& ptr
< (value
+ sizeof(value
) - 2);)
1707 if (strchr(" \t\n\\\'\"", *valptr
))
1720 num_options
= cupsAddOption(optname
, value
, num_options
, &options
);
1727 * See if we have everything needed...
1732 cupsFreeOptions(num_options
, options
);
1740 num_dests
= cupsAddDest(printer_name
, NULL
, num_dests
, dests
);
1742 if ((dest
= cupsGetDest(printer_name
, NULL
, num_dests
, *dests
)) != NULL
)
1744 dest
->num_options
= num_options
;
1745 dest
->options
= options
;
1750 if (auth_info_req
[0])
1751 dest
->num_options
= cupsAddOption("auth-info-required", auth_info_req
,
1756 dest
->num_options
= cupsAddOption("job-sheets", job_sheets
,
1761 dest
->num_options
= cupsAddOption("printer-info", info
,
1765 sprintf(value
, "%d", accepting
);
1766 dest
->num_options
= cupsAddOption("printer-is-accepting-jobs", value
,
1770 sprintf(value
, "%d", shared
);
1771 dest
->num_options
= cupsAddOption("printer-is-shared", value
,
1776 dest
->num_options
= cupsAddOption("printer-location",
1777 location
, dest
->num_options
,
1781 dest
->num_options
= cupsAddOption("printer-make-and-model",
1782 make_model
, dest
->num_options
,
1785 sprintf(value
, "%d", state
);
1786 dest
->num_options
= cupsAddOption("printer-state", value
,
1792 sprintf(value
, "%d", change_time
);
1793 dest
->num_options
= cupsAddOption("printer-state-change-time", value
,
1799 dest
->num_options
= cupsAddOption("printer-state-reasons", reasons
,
1803 sprintf(value
, "%d", type
);
1804 dest
->num_options
= cupsAddOption("printer-type", value
,
1809 cupsFreeOptions(num_options
, options
);
1815 ippDelete(response
);
1819 * Return the count...
1827 * End of "$Id: dest.c 7460 2008-04-16 02:19:54Z mike $".