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 @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 ((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 @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...");
987 DEBUG_printf(("appleGetDefault: Got location, %d entries...\n",
988 (int)CFArrayGetCount(locations
)));
990 if ((locprinter
= appleGetPrinter(locations
, network
, NULL
)) != NULL
)
991 CFStringGetCString(locprinter
, name
, namesize
, kCFStringEncodingUTF8
);
995 CFRelease(locations
);
998 DEBUG_printf(("appleGetDefault: Returning \"%s\"...\n", name
));
1000 return (*name
? name
: NULL
);
1005 * 'appleGetLocations()' - Get the location history array.
1008 static CFArrayRef
/* O - Location array or NULL */
1009 appleGetLocations(void)
1011 CFArrayRef locations
; /* Location array */
1015 * Look up the location array in the preferences...
1018 if ((locations
= CFPreferencesCopyAppValue(kLocationHistoryArrayKey
,
1019 kPMPrintingPreferences
)) == NULL
)
1022 if (CFGetTypeID(locations
) != CFArrayGetTypeID())
1024 CFRelease(locations
);
1033 * 'appleGetNetwork()' - Get the network ID for the current location.
1036 static CFStringRef
/* O - Network ID */
1037 appleGetNetwork(void)
1039 SCDynamicStoreRef dynamicStore
; /* System configuration data */
1040 CFStringRef key
; /* Current network configuration key */
1041 CFDictionaryRef ip_dict
; /* Network configuration data */
1042 CFStringRef network
= NULL
; /* Current network ID */
1045 if ((dynamicStore
= SCDynamicStoreCreate(NULL
, CFSTR("Printing"), NULL
,
1048 if ((key
= SCDynamicStoreKeyCreateNetworkGlobalEntity(
1049 NULL
, kSCDynamicStoreDomainState
, kSCEntNetIPv4
)) != NULL
)
1051 if ((ip_dict
= SCDynamicStoreCopyValue(dynamicStore
, key
)) != NULL
)
1053 if ((network
= CFDictionaryGetValue(ip_dict
,
1054 kSCPropNetIPv4Router
)) != NULL
)
1063 CFRelease(dynamicStore
);
1071 * 'appleGetPrinter()' - Get a printer from the history array.
1074 static CFStringRef
/* O - Printer name or NULL */
1075 appleGetPrinter(CFArrayRef locations
, /* I - Location array */
1076 CFStringRef network
, /* I - Network name */
1077 CFIndex
*locindex
) /* O - Index in array */
1079 CFIndex i
, /* Looping var */
1080 count
; /* Number of locations */
1081 CFDictionaryRef location
; /* Current location */
1082 CFStringRef locnetwork
, /* Current network */
1083 locprinter
; /* Current printer */
1086 for (i
= 0, count
= CFArrayGetCount(locations
); i
< count
; i
++)
1087 if ((location
= CFArrayGetValueAtIndex(locations
, i
)) != NULL
&&
1088 CFGetTypeID(location
) == CFDictionaryGetTypeID())
1090 if ((locnetwork
= CFDictionaryGetValue(location
,
1091 kLocationNetworkKey
)) != NULL
&&
1092 CFGetTypeID(locnetwork
) == CFStringGetTypeID() &&
1093 CFStringCompare(network
, locnetwork
, 0) == kCFCompareEqualTo
&&
1094 (locprinter
= CFDictionaryGetValue(location
,
1095 kLocationPrinterIDKey
)) != NULL
&&
1096 CFGetTypeID(locprinter
) == CFStringGetTypeID())
1101 return (locprinter
);
1110 * 'appleSetDefault()' - Set the default printer for this location.
1114 appleSetDefault(const char *name
) /* I - Default printer/class name */
1116 CFStringRef network
; /* Current network */
1117 CFArrayRef locations
; /* Old locations array */
1118 CFIndex locindex
; /* Index in locations array */
1119 CFStringRef locprinter
; /* Current printer */
1120 CFMutableArrayRef newlocations
; /* New locations array */
1121 CFMutableDictionaryRef newlocation
; /* New location */
1122 CFStringRef newprinter
; /* New printer */
1126 * Get the current location...
1129 if ((network
= appleGetNetwork()) == NULL
)
1131 DEBUG_puts("appleSetDefault: Unable to get current network...");
1135 if ((newprinter
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
1136 kCFStringEncodingUTF8
)) == NULL
)
1143 * Lookup the network in the preferences...
1146 if ((locations
= appleGetLocations()) != NULL
)
1147 locprinter
= appleGetPrinter(locations
, network
, &locindex
);
1155 CFStringCompare(locprinter
, newprinter
, 0) != kCFCompareEqualTo
)
1158 * Need to change the locations array...
1163 newlocations
= CFArrayCreateMutableCopy(kCFAllocatorDefault
, 0,
1167 CFArrayRemoveValueAtIndex(newlocations
, locindex
);
1170 newlocations
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, NULL
);
1172 newlocation
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
1173 &kCFTypeDictionaryKeyCallBacks
,
1174 &kCFTypeDictionaryValueCallBacks
);
1176 if (newlocation
&& newlocations
)
1179 * Put the new location at the front of the array...
1182 CFDictionaryAddValue(newlocation
, kLocationNetworkKey
, network
);
1183 CFDictionaryAddValue(newlocation
, kLocationPrinterIDKey
, newprinter
);
1184 CFArrayInsertValueAtIndex(newlocations
, 0, newlocation
);
1187 * Limit the number of locations to 10...
1190 while (CFArrayGetCount(newlocations
) > 10)
1191 CFArrayRemoveValueAtIndex(newlocations
, 10);
1194 * Push the changes out...
1197 CFPreferencesSetAppValue(kLocationHistoryArrayKey
, newlocations
,
1198 kPMPrintingPreferences
);
1199 CFPreferencesAppSynchronize(kPMPrintingPreferences
);
1203 CFRelease(newlocations
);
1206 CFRelease(newlocation
);
1210 CFRelease(locations
);
1212 CFRelease(newprinter
);
1217 * 'appleUseLastPrinter()' - Get the default printer preference value.
1220 static int /* O - 1 to use last printer, 0 otherwise */
1221 appleUseLastPrinter(void)
1223 CFPropertyListRef uselast
; /* Use last printer preference value */
1226 if ((uselast
= CFPreferencesCopyAppValue(kUseLastPrinterAsCurrentPrinterKey
,
1227 kPMPrintingPreferences
)) != NULL
)
1231 if (uselast
== kCFBooleanFalse
)
1237 #endif /* __APPLE__ */
1241 * 'cups_get_default()' - Get the default destination from an lpoptions file.
1244 static char * /* O - Default destination or NULL */
1245 cups_get_default(const char *filename
, /* I - File to read */
1246 char *namebuf
, /* I - Name buffer */
1247 size_t namesize
, /* I - Size of name buffer */
1248 const char **instance
) /* I - Instance */
1250 cups_file_t
*fp
; /* lpoptions file */
1251 char line
[8192], /* Line from file */
1252 *value
, /* Value for line */
1253 *nameptr
; /* Pointer into name */
1254 int linenum
; /* Current line */
1259 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1263 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
1265 if (!strcasecmp(line
, "default") && value
)
1267 strlcpy(namebuf
, value
, namesize
);
1269 if ((nameptr
= strchr(namebuf
, ' ')) != NULL
)
1271 if ((nameptr
= strchr(namebuf
, '\t')) != NULL
)
1274 if ((nameptr
= strchr(namebuf
, '/')) != NULL
)
1277 *instance
= nameptr
;
1285 return (*namebuf
? namebuf
: NULL
);
1290 * 'cups_get_dests()' - Get destinations from a file.
1293 static int /* O - Number of destinations */
1294 cups_get_dests(const char *filename
, /* I - File to read from */
1295 const char *match_name
, /* I - Destination name we want */
1296 const char *match_inst
, /* I - Instance name we want */
1297 int num_dests
, /* I - Number of destinations */
1298 cups_dest_t
**dests
) /* IO - Destinations */
1300 int i
; /* Looping var */
1301 cups_dest_t
*dest
; /* Current destination */
1302 cups_file_t
*fp
; /* File pointer */
1303 char line
[8192], /* Line from file */
1304 *lineptr
, /* Pointer into line */
1305 *name
, /* Name of destination/option */
1306 *instance
; /* Instance of destination */
1307 int linenum
; /* Current line number */
1308 const char *printer
; /* PRINTER or LPDEST */
1311 DEBUG_printf(("cups_get_dests(filename=\"%s\", match_name=\"%s\", "
1312 "match_inst=\"%s\", num_dests=%d, dests=%p)\n", filename
,
1313 match_name
? match_name
: "(null)",
1314 match_inst
? match_inst
: "(null)", num_dests
, dests
));
1317 * Try to open the file...
1320 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1324 * Check environment variables...
1327 if ((printer
= getenv("LPDEST")) == NULL
)
1328 if ((printer
= getenv("PRINTER")) != NULL
)
1329 if (strcmp(printer
, "lp") == 0)
1332 DEBUG_printf(("cups_get_dests: printer=\"%s\"\n",
1333 printer
? printer
: "(null)"));
1336 * Read each printer; each line looks like:
1338 * Dest name[/instance] options
1339 * Default name[/instance] options
1344 while (cupsFileGetConf(fp
, line
, sizeof(line
), &lineptr
, &linenum
))
1347 * See what type of line it is...
1350 DEBUG_printf(("cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"\n",
1351 linenum
, line
, lineptr
? lineptr
: "(null)"));
1353 if ((strcasecmp(line
, "dest") && strcasecmp(line
, "default")) || !lineptr
)
1355 DEBUG_puts("cups_get_dests: Not a dest or default line...");
1362 * Search for an instance...
1365 while (!isspace(*lineptr
& 255) && *lineptr
&& *lineptr
!= '/')
1368 if (*lineptr
== '/')
1371 * Found an instance...
1378 * Search for an instance...
1381 while (!isspace(*lineptr
& 255) && *lineptr
)
1390 DEBUG_printf(("cups_get_dests: name=\"%s\", instance=\"%s\"\n", name
,
1394 * See if the primary instance of the destination exists; if not,
1395 * ignore this entry and move on...
1400 if (strcasecmp(name
, match_name
) ||
1401 (!instance
&& match_inst
) ||
1402 (instance
&& !match_inst
) ||
1403 (instance
&& strcasecmp(instance
, match_inst
)))
1408 else if (cupsGetDest(name
, NULL
, num_dests
, *dests
) == NULL
)
1410 DEBUG_puts("cups_get_dests: Not found!");
1416 * Add the destination...
1419 num_dests
= cupsAddDest(name
, instance
, num_dests
, dests
);
1421 if ((dest
= cupsGetDest(name
, instance
, num_dests
, *dests
)) == NULL
)
1427 DEBUG_puts("cups_get_dests: Out of memory!");
1433 * Add options until we hit the end of the line...
1436 dest
->num_options
= cupsParseOptions(lineptr
, dest
->num_options
,
1440 * If we found what we were looking for, stop now...
1447 * Set this as default if needed...
1450 if (!printer
&& !strcasecmp(line
, "default"))
1452 DEBUG_puts("cups_get_dests: Setting as default...");
1454 for (i
= 0; i
< num_dests
; i
++)
1455 (*dests
)[i
].is_default
= 0;
1457 dest
->is_default
= 1;
1462 * Close the file and return...
1472 * 'cups_get_sdests()' - Get destinations from a server.
1475 static int /* O - Number of destinations */
1476 cups_get_sdests(http_t
*http
, /* I - Connection to server or CUPS_HTTP_DEFAULT */
1477 ipp_op_t op
, /* I - IPP operation */
1478 const char *name
, /* I - Name of destination */
1479 int num_dests
, /* I - Number of destinations */
1480 cups_dest_t
**dests
) /* IO - Destinations */
1482 int i
; /* Looping var */
1483 cups_dest_t
*dest
; /* Current destination */
1484 ipp_t
*request
, /* IPP Request */
1485 *response
; /* IPP Response */
1486 ipp_attribute_t
*attr
; /* Current attribute */
1487 int accepting
, /* printer-is-accepting-jobs attribute */
1488 shared
, /* printer-is-shared attribute */
1489 state
, /* printer-state attribute */
1490 change_time
, /* printer-state-change-time attribute */
1491 type
; /* printer-type attribute */
1492 const char *info
, /* printer-info attribute */
1493 *location
, /* printer-location attribute */
1494 *make_model
, /* printer-make-and-model attribute */
1495 *printer_name
; /* printer-name attribute */
1496 char uri
[1024], /* printer-uri value */
1497 job_sheets
[1024], /* job-sheets-default attribute */
1498 auth_info_req
[1024], /* auth-info-required attribute */
1499 reasons
[1024]; /* printer-state-reasons attribute */
1500 int num_options
; /* Number of options */
1501 cups_option_t
*options
; /* Options */
1502 char optname
[1024], /* Option name */
1503 value
[2048], /* Option value */
1504 *ptr
; /* Pointer into name/value */
1505 static const char * const pattrs
[] = /* Attributes we're interested in */
1507 "auth-info-required",
1508 "job-sheets-default",
1510 "printer-is-accepting-jobs",
1511 "printer-is-shared",
1513 "printer-make-and-model",
1516 "printer-state-change-time",
1517 "printer-state-reasons",
1524 * Build a CUPS_GET_PRINTERS or CUPS_GET_CLASSES request, which require
1525 * the following attributes:
1527 * attributes-charset
1528 * attributes-natural-language
1529 * requesting-user-name
1532 request
= ippNewRequest(op
);
1534 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1535 "requested-attributes", sizeof(pattrs
) / sizeof(pattrs
[0]),
1538 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1539 "requesting-user-name", NULL
, cupsUser());
1543 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1544 "localhost", ippPort(), "/printers/%s", name
);
1545 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
1550 * Do the request and get back a response...
1553 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1555 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1558 * Skip leading attributes until we hit a printer...
1561 while (attr
!= NULL
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
1568 * Pull the needed attributes from this printer...
1576 printer_name
= NULL
;
1580 state
= IPP_PRINTER_IDLE
;
1581 type
= CUPS_PRINTER_LOCAL
;
1583 auth_info_req
[0] = '\0';
1584 job_sheets
[0] = '\0';
1587 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_PRINTER
)
1589 if (!strcmp(attr
->name
, "auth-info-required") &&
1590 attr
->value_tag
== IPP_TAG_KEYWORD
)
1592 strlcpy(auth_info_req
, attr
->values
[0].string
.text
,
1593 sizeof(auth_info_req
));
1595 for (i
= 1, ptr
= auth_info_req
+ strlen(auth_info_req
);
1596 i
< attr
->num_values
;
1599 snprintf(ptr
, sizeof(auth_info_req
) - (ptr
- auth_info_req
), ",%s",
1600 attr
->values
[i
].string
.text
);
1604 else if (!strcmp(attr
->name
, "job-sheets-default") &&
1605 (attr
->value_tag
== IPP_TAG_KEYWORD
||
1606 attr
->value_tag
== IPP_TAG_NAME
))
1608 if (attr
->num_values
== 2)
1609 snprintf(job_sheets
, sizeof(job_sheets
), "%s,%s",
1610 attr
->values
[0].string
.text
, attr
->values
[1].string
.text
);
1612 strlcpy(job_sheets
, attr
->values
[0].string
.text
,
1613 sizeof(job_sheets
));
1615 else if (!strcmp(attr
->name
, "printer-info") &&
1616 attr
->value_tag
== IPP_TAG_TEXT
)
1617 info
= attr
->values
[0].string
.text
;
1618 else if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
1619 attr
->value_tag
== IPP_TAG_BOOLEAN
)
1620 accepting
= attr
->values
[0].boolean
;
1621 else if (!strcmp(attr
->name
, "printer-is-shared") &&
1622 attr
->value_tag
== IPP_TAG_BOOLEAN
)
1623 shared
= attr
->values
[0].boolean
;
1624 else if (!strcmp(attr
->name
, "printer-location") &&
1625 attr
->value_tag
== IPP_TAG_TEXT
)
1626 location
= attr
->values
[0].string
.text
;
1627 else if (!strcmp(attr
->name
, "printer-make-and-model") &&
1628 attr
->value_tag
== IPP_TAG_TEXT
)
1629 make_model
= attr
->values
[0].string
.text
;
1630 else if (!strcmp(attr
->name
, "printer-name") &&
1631 attr
->value_tag
== IPP_TAG_NAME
)
1632 printer_name
= attr
->values
[0].string
.text
;
1633 else if (!strcmp(attr
->name
, "printer-state") &&
1634 attr
->value_tag
== IPP_TAG_ENUM
)
1635 state
= attr
->values
[0].integer
;
1636 else if (!strcmp(attr
->name
, "printer-state-change-time") &&
1637 attr
->value_tag
== IPP_TAG_INTEGER
)
1638 change_time
= attr
->values
[0].integer
;
1639 else if (!strcmp(attr
->name
, "printer-state-reasons") &&
1640 attr
->value_tag
== IPP_TAG_KEYWORD
)
1642 strlcpy(reasons
, attr
->values
[0].string
.text
, sizeof(reasons
));
1643 for (i
= 1, ptr
= reasons
+ strlen(reasons
);
1644 i
< attr
->num_values
;
1647 snprintf(ptr
, sizeof(reasons
) - (ptr
- reasons
), ",%s",
1648 attr
->values
[i
].string
.text
);
1652 else if (!strcmp(attr
->name
, "printer-type") &&
1653 attr
->value_tag
== IPP_TAG_ENUM
)
1654 type
= attr
->values
[0].integer
;
1655 else if (strncmp(attr
->name
, "notify-", 7) &&
1656 (attr
->value_tag
== IPP_TAG_BOOLEAN
||
1657 attr
->value_tag
== IPP_TAG_ENUM
||
1658 attr
->value_tag
== IPP_TAG_INTEGER
||
1659 attr
->value_tag
== IPP_TAG_KEYWORD
||
1660 attr
->value_tag
== IPP_TAG_NAME
||
1661 attr
->value_tag
== IPP_TAG_RANGE
) &&
1662 strstr(attr
->name
, "-default"))
1664 char *valptr
; /* Pointer into attribute value */
1668 * Add a default option...
1671 strlcpy(optname
, attr
->name
, sizeof(optname
));
1672 if ((ptr
= strstr(optname
, "-default")) != NULL
)
1676 for (i
= 0, ptr
= value
; i
< attr
->num_values
; i
++)
1678 if (ptr
>= (value
+ sizeof(value
) - 1))
1684 switch (attr
->value_tag
)
1686 case IPP_TAG_INTEGER
:
1688 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d",
1689 attr
->values
[i
].integer
);
1692 case IPP_TAG_BOOLEAN
:
1693 if (attr
->values
[i
].boolean
)
1694 strlcpy(ptr
, "true", sizeof(value
) - (ptr
- value
));
1696 strlcpy(ptr
, "false", sizeof(value
) - (ptr
- value
));
1699 case IPP_TAG_RANGE
:
1700 if (attr
->values
[i
].range
.lower
==
1701 attr
->values
[i
].range
.upper
)
1702 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d",
1703 attr
->values
[i
].range
.lower
);
1705 snprintf(ptr
, sizeof(value
) - (ptr
- value
), "%d-%d",
1706 attr
->values
[i
].range
.lower
,
1707 attr
->values
[i
].range
.upper
);
1711 for (valptr
= attr
->values
[i
].string
.text
;
1712 *valptr
&& ptr
< (value
+ sizeof(value
) - 2);)
1714 if (strchr(" \t\n\\\'\"", *valptr
))
1727 num_options
= cupsAddOption(optname
, value
, num_options
, &options
);
1734 * See if we have everything needed...
1739 cupsFreeOptions(num_options
, options
);
1747 num_dests
= cupsAddDest(printer_name
, NULL
, num_dests
, dests
);
1749 if ((dest
= cupsGetDest(printer_name
, NULL
, num_dests
, *dests
)) != NULL
)
1751 dest
->num_options
= num_options
;
1752 dest
->options
= options
;
1757 if (auth_info_req
[0])
1758 dest
->num_options
= cupsAddOption("auth-info-required", auth_info_req
,
1763 dest
->num_options
= cupsAddOption("job-sheets", job_sheets
,
1768 dest
->num_options
= cupsAddOption("printer-info", info
,
1772 sprintf(value
, "%d", accepting
);
1773 dest
->num_options
= cupsAddOption("printer-is-accepting-jobs", value
,
1777 sprintf(value
, "%d", shared
);
1778 dest
->num_options
= cupsAddOption("printer-is-shared", value
,
1783 dest
->num_options
= cupsAddOption("printer-location",
1784 location
, dest
->num_options
,
1788 dest
->num_options
= cupsAddOption("printer-make-and-model",
1789 make_model
, dest
->num_options
,
1792 sprintf(value
, "%d", state
);
1793 dest
->num_options
= cupsAddOption("printer-state", value
,
1799 sprintf(value
, "%d", change_time
);
1800 dest
->num_options
= cupsAddOption("printer-state-change-time", value
,
1806 dest
->num_options
= cupsAddOption("printer-state-reasons", reasons
,
1810 sprintf(value
, "%d", type
);
1811 dest
->num_options
= cupsAddOption("printer-type", value
,
1816 cupsFreeOptions(num_options
, options
);
1822 ippDelete(response
);
1826 * Return the count...
1834 * End of "$Id: dest.c 6943 2007-09-10 23:00:33Z mike $".