]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $" |
ef416fc2 | 3 | * |
4 | * User-defined destination (and option) support for the Common UNIX | |
5 | * Printing System (CUPS). | |
6 | * | |
5a6b583a | 7 | * Copyright 2007-2010 by Apple Inc. |
b86bc4cf | 8 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 9 | * |
10 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 15 | * |
16 | * This file is subject to the Apple OS-Developed Software exception. | |
17 | * | |
18 | * Contents: | |
19 | * | |
080811b1 MS |
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 | |
24 | * server. | |
25 | * cupsGetDests2() - Get the list of destinations from the specified | |
26 | * server. | |
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 | |
31 | * server. | |
32 | * cupsSetDests2() - Save the list of destinations for the specified | |
33 | * server. | |
426c6a59 | 34 | * appleCopyLocations() - Copy the location history array. |
749b1e90 | 35 | * appleCopyNetwork() - Get the network ID for the current location. |
426c6a59 | 36 | * appleGetPaperSize() - Get the default paper size. |
080811b1 MS |
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. | |
426c6a59 MS |
40 | * cups_add_dest() - Add a destination to the array. |
41 | * cups_compare_dests() - Compare two destinations. | |
42 | * cups_find_dest() - Find a destination using a binary search. | |
080811b1 MS |
43 | * cups_get_default() - Get the default destination from an lpoptions file. |
44 | * cups_get_dests() - Get destinations from a file. | |
45 | * cups_get_sdests() - Get destinations from a server. | |
426c6a59 MS |
46 | * cups_make_string() - Make a comma-separated string of values from an IPP |
47 | * attribute. | |
ef416fc2 | 48 | */ |
49 | ||
50 | /* | |
51 | * Include necessary headers... | |
52 | */ | |
53 | ||
a4924f6c | 54 | #include "debug.h" |
ef416fc2 | 55 | #include "globals.h" |
54afec33 | 56 | #include "pwg-private.h" |
ef416fc2 | 57 | #include <stdlib.h> |
58 | #include <ctype.h> | |
b423cd4c | 59 | #include <sys/stat.h> |
ef416fc2 | 60 | |
fa73b229 | 61 | #ifdef HAVE_NOTIFY_H |
62 | # include <notify.h> | |
63 | #endif /* HAVE_NOTIFY_H */ | |
64 | ||
080811b1 MS |
65 | #ifdef __APPLE__ |
66 | # include <sys/cdefs.h> | |
67 | # include <CoreFoundation/CoreFoundation.h> | |
68 | # include <SystemConfiguration/SystemConfiguration.h> | |
426c6a59 | 69 | # define kDefaultPaperIDKey CFSTR("DefaultPaperID") |
080811b1 MS |
70 | # define kLocationHistoryArrayKey CFSTR("kLocationHistoryArrayKeyTMP") |
71 | # define kLocationNetworkKey CFSTR("kLocationNetworkKey") | |
72 | # define kLocationPrinterIDKey CFSTR("kLocationPrinterIDKey") | |
73 | # define kPMPrintingPreferences CFSTR("com.apple.print.PrintingPrefs") | |
74 | # define kUseLastPrinterAsCurrentPrinterKey CFSTR("UseLastPrinterAsCurrentPrinter") | |
75 | #endif /* __APPLE__ */ | |
76 | ||
ef416fc2 | 77 | |
78 | /* | |
79 | * Local functions... | |
80 | */ | |
81 | ||
080811b1 | 82 | #ifdef __APPLE__ |
06d4e77b | 83 | static CFArrayRef appleCopyLocations(void); |
749b1e90 | 84 | static CFStringRef appleCopyNetwork(void); |
426c6a59 | 85 | static char *appleGetPaperSize(char *name, int namesize); |
080811b1 MS |
86 | static CFStringRef appleGetPrinter(CFArrayRef locations, CFStringRef network, |
87 | CFIndex *locindex); | |
88 | static void appleSetDefault(const char *name); | |
89 | static int appleUseLastPrinter(void); | |
90 | #endif /* __APPLE__ */ | |
426c6a59 MS |
91 | static cups_dest_t *cups_add_dest(const char *name, const char *instance, |
92 | int *num_dests, cups_dest_t **dests); | |
93 | static int cups_compare_dests(cups_dest_t *a, cups_dest_t *b); | |
94 | static int cups_find_dest(const char *name, const char *instance, | |
95 | int num_dests, cups_dest_t *dests, int prev, | |
96 | int *rdiff); | |
080811b1 | 97 | static char *cups_get_default(const char *filename, char *namebuf, |
426c6a59 | 98 | size_t namesize, const char **instance); |
a4924f6c | 99 | static int cups_get_dests(const char *filename, const char *match_name, |
38e73f87 MS |
100 | const char *match_inst, int user_default_set, |
101 | int num_dests, cups_dest_t **dests); | |
a4924f6c MS |
102 | static int cups_get_sdests(http_t *http, ipp_op_t op, const char *name, |
103 | int num_dests, cups_dest_t **dests); | |
426c6a59 MS |
104 | static char *cups_make_string(ipp_attribute_t *attr, char *buffer, |
105 | size_t bufsize); | |
ef416fc2 | 106 | |
107 | ||
108 | /* | |
109 | * 'cupsAddDest()' - Add a destination to the list of destinations. | |
110 | * | |
2abf387c | 111 | * This function cannot be used to add a new class or printer queue, |
112 | * it only adds a new container of saved options for the named | |
113 | * destination or instance. | |
114 | * | |
115 | * If the named destination already exists, the destination list is | |
116 | * returned unchanged. Adding a new instance of a destination creates | |
117 | * a copy of that destination's options. | |
118 | * | |
5a738aea | 119 | * Use the @link cupsSaveDests@ function to save the updated list of |
2abf387c | 120 | * destinations to the user's lpoptions file. |
ef416fc2 | 121 | */ |
122 | ||
123 | int /* O - New number of destinations */ | |
2abf387c | 124 | cupsAddDest(const char *name, /* I - Destination name */ |
5a738aea | 125 | const char *instance, /* I - Instance name or @code NULL@ for none/primary */ |
ef416fc2 | 126 | int num_dests, /* I - Number of destinations */ |
127 | cups_dest_t **dests) /* IO - Destinations */ | |
128 | { | |
129 | int i; /* Looping var */ | |
130 | cups_dest_t *dest; /* Destination pointer */ | |
bf3816c7 | 131 | cups_dest_t *parent = NULL; /* Parent destination */ |
426c6a59 MS |
132 | cups_option_t *doption, /* Current destination option */ |
133 | *poption; /* Current parent option */ | |
ef416fc2 | 134 | |
135 | ||
2abf387c | 136 | if (!name || !dests) |
ef416fc2 | 137 | return (0); |
138 | ||
426c6a59 MS |
139 | if (!cupsGetDest(name, instance, num_dests, *dests)) |
140 | { | |
41681883 | 141 | if (instance && !cupsGetDest(name, NULL, num_dests, *dests)) |
426c6a59 | 142 | return (num_dests); |
ef416fc2 | 143 | |
426c6a59 | 144 | dest = cups_add_dest(name, instance, &num_dests, dests); |
2abf387c | 145 | |
5a662dc0 MS |
146 | /* |
147 | * Find the base dest again now the array has been realloc'd. | |
148 | */ | |
149 | ||
150 | parent = cupsGetDest(name, NULL, num_dests, *dests); | |
151 | ||
426c6a59 MS |
152 | if (instance && parent && parent->num_options > 0) |
153 | { | |
154 | /* | |
155 | * Copy options from parent... | |
156 | */ | |
ef416fc2 | 157 | |
426c6a59 | 158 | dest->options = calloc(sizeof(cups_option_t), parent->num_options); |
2abf387c | 159 | |
426c6a59 MS |
160 | if (dest->options) |
161 | { | |
162 | dest->num_options = parent->num_options; | |
ef416fc2 | 163 | |
426c6a59 MS |
164 | for (i = dest->num_options, doption = dest->options, |
165 | poption = parent->options; | |
166 | i > 0; | |
167 | i --, doption ++, poption ++) | |
168 | { | |
169 | doption->name = _cupsStrRetain(poption->name); | |
170 | doption->value = _cupsStrRetain(poption->value); | |
171 | } | |
172 | } | |
2abf387c | 173 | } |
174 | } | |
175 | ||
426c6a59 | 176 | return (num_dests); |
ef416fc2 | 177 | } |
178 | ||
179 | ||
180 | /* | |
181 | * 'cupsFreeDests()' - Free the memory used by the list of destinations. | |
182 | */ | |
183 | ||
184 | void | |
185 | cupsFreeDests(int num_dests, /* I - Number of destinations */ | |
186 | cups_dest_t *dests) /* I - Destinations */ | |
187 | { | |
188 | int i; /* Looping var */ | |
189 | cups_dest_t *dest; /* Current destination */ | |
190 | ||
191 | ||
192 | if (num_dests == 0 || dests == NULL) | |
193 | return; | |
194 | ||
195 | for (i = num_dests, dest = dests; i > 0; i --, dest ++) | |
196 | { | |
2e4ff8af MS |
197 | _cupsStrFree(dest->name); |
198 | _cupsStrFree(dest->instance); | |
ef416fc2 | 199 | |
200 | cupsFreeOptions(dest->num_options, dest->options); | |
201 | } | |
202 | ||
203 | free(dests); | |
204 | } | |
205 | ||
206 | ||
207 | /* | |
208 | * 'cupsGetDest()' - Get the named destination from the list. | |
209 | * | |
5a738aea | 210 | * Use the @link cupsGetDests@ or @link cupsGetDests2@ functions to get a |
ef416fc2 | 211 | * list of supported destinations for the current user. |
212 | */ | |
213 | ||
5a738aea MS |
214 | cups_dest_t * /* O - Destination pointer or @code NULL@ */ |
215 | cupsGetDest(const char *name, /* I - Destination name or @code NULL@ for the default destination */ | |
216 | const char *instance, /* I - Instance name or @code NULL@ */ | |
ef416fc2 | 217 | int num_dests, /* I - Number of destinations */ |
218 | cups_dest_t *dests) /* I - Destinations */ | |
219 | { | |
426c6a59 MS |
220 | int diff, /* Result of comparison */ |
221 | match; /* Matching index */ | |
ef416fc2 | 222 | |
223 | ||
2abf387c | 224 | if (num_dests <= 0 || !dests) |
ef416fc2 | 225 | return (NULL); |
226 | ||
2abf387c | 227 | if (!name) |
ef416fc2 | 228 | { |
229 | /* | |
230 | * NULL name for default printer. | |
231 | */ | |
232 | ||
233 | while (num_dests > 0) | |
234 | { | |
235 | if (dests->is_default) | |
236 | return (dests); | |
237 | ||
238 | num_dests --; | |
239 | dests ++; | |
240 | } | |
241 | } | |
242 | else | |
243 | { | |
244 | /* | |
245 | * Lookup name and optionally the instance... | |
246 | */ | |
247 | ||
426c6a59 | 248 | match = cups_find_dest(name, instance, num_dests, dests, -1, &diff); |
ef416fc2 | 249 | |
426c6a59 MS |
250 | if (!diff) |
251 | return (dests + match); | |
ef416fc2 | 252 | } |
253 | ||
254 | return (NULL); | |
255 | } | |
256 | ||
257 | ||
258 | /* | |
259 | * 'cupsGetDests()' - Get the list of destinations from the default server. | |
ecdc0628 | 260 | * |
261 | * Starting with CUPS 1.2, the returned list of destinations include the | |
262 | * printer-info, printer-is-accepting-jobs, printer-is-shared, | |
263 | * printer-make-and-model, printer-state, printer-state-change-time, | |
426c6a59 MS |
264 | * printer-state-reasons, and printer-type attributes as options. CUPS 1.4 |
265 | * adds the marker-change-time, marker-colors, marker-high-levels, | |
266 | * marker-levels, marker-low-levels, marker-message, marker-names, | |
267 | * marker-types, and printer-commands attributes as well. | |
2abf387c | 268 | * |
5a738aea MS |
269 | * Use the @link cupsFreeDests@ function to free the destination list and |
270 | * the @link cupsGetDest@ function to find a particular destination. | |
ef416fc2 | 271 | */ |
272 | ||
273 | int /* O - Number of destinations */ | |
274 | cupsGetDests(cups_dest_t **dests) /* O - Destinations */ | |
275 | { | |
3d052e43 | 276 | return (cupsGetDests2(CUPS_HTTP_DEFAULT, dests)); |
ef416fc2 | 277 | } |
278 | ||
279 | ||
280 | /* | |
281 | * 'cupsGetDests2()' - Get the list of destinations from the specified server. | |
282 | * | |
ecdc0628 | 283 | * Starting with CUPS 1.2, the returned list of destinations include the |
284 | * printer-info, printer-is-accepting-jobs, printer-is-shared, | |
285 | * printer-make-and-model, printer-state, printer-state-change-time, | |
426c6a59 MS |
286 | * printer-state-reasons, and printer-type attributes as options. CUPS 1.4 |
287 | * adds the marker-change-time, marker-colors, marker-high-levels, | |
288 | * marker-levels, marker-low-levels, marker-message, marker-names, | |
289 | * marker-types, and printer-commands attributes as well. | |
ecdc0628 | 290 | * |
5a738aea MS |
291 | * Use the @link cupsFreeDests@ function to free the destination list and |
292 | * the @link cupsGetDest@ function to find a particular destination. | |
2abf387c | 293 | * |
426c6a59 | 294 | * @since CUPS 1.1.21/Mac OS X 10.4@ |
ef416fc2 | 295 | */ |
296 | ||
297 | int /* O - Number of destinations */ | |
568fa3fa | 298 | cupsGetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 299 | cups_dest_t **dests) /* O - Destinations */ |
300 | { | |
301 | int i; /* Looping var */ | |
302 | int num_dests; /* Number of destinations */ | |
303 | cups_dest_t *dest; /* Destination pointer */ | |
304 | const char *home; /* HOME environment variable */ | |
b423cd4c | 305 | char filename[1024]; /* Local ~/.cups/lpoptions file */ |
ef416fc2 | 306 | const char *defprinter; /* Default printer */ |
307 | char name[1024], /* Copy of printer name */ | |
38e73f87 MS |
308 | *instance, /* Pointer to instance name */ |
309 | *user_default; /* User default printer */ | |
ef416fc2 | 310 | int num_reals; /* Number of real queues */ |
311 | cups_dest_t *reals; /* Real queues */ | |
3d052e43 | 312 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
ef416fc2 | 313 | |
314 | ||
315 | /* | |
316 | * Range check the input... | |
317 | */ | |
318 | ||
3d052e43 | 319 | if (!dests) |
1ff0402e MS |
320 | { |
321 | _cupsSetError(IPP_INTERNAL_ERROR, _("Bad NULL dests pointer"), 1); | |
ef416fc2 | 322 | return (0); |
1ff0402e | 323 | } |
ef416fc2 | 324 | |
325 | /* | |
326 | * Initialize destination array... | |
327 | */ | |
328 | ||
329 | num_dests = 0; | |
330 | *dests = (cups_dest_t *)0; | |
331 | ||
332 | /* | |
333 | * Grab the printers and classes... | |
334 | */ | |
335 | ||
a4924f6c | 336 | num_dests = cups_get_sdests(http, CUPS_GET_PRINTERS, NULL, num_dests, dests); |
b19ccc9e MS |
337 | |
338 | if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE) | |
339 | { | |
340 | cupsFreeDests(num_dests, *dests); | |
341 | *dests = (cups_dest_t *)0; | |
342 | return (0); | |
343 | } | |
ef416fc2 | 344 | |
345 | /* | |
346 | * Make a copy of the "real" queues for a later sanity check... | |
347 | */ | |
348 | ||
349 | if (num_dests > 0) | |
350 | { | |
351 | num_reals = num_dests; | |
352 | reals = calloc(num_reals, sizeof(cups_dest_t)); | |
353 | ||
354 | if (reals) | |
355 | memcpy(reals, *dests, num_reals * sizeof(cups_dest_t)); | |
356 | else | |
357 | num_reals = 0; | |
358 | } | |
359 | else | |
360 | { | |
361 | num_reals = 0; | |
362 | reals = NULL; | |
363 | } | |
364 | ||
365 | /* | |
366 | * Grab the default destination... | |
367 | */ | |
368 | ||
38e73f87 MS |
369 | if ((user_default = _cupsUserDefault(name, sizeof(name))) != NULL) |
370 | defprinter = name; | |
371 | else if ((defprinter = cupsGetDefault2(http)) != NULL) | |
372 | { | |
373 | strlcpy(name, defprinter, sizeof(name)); | |
374 | defprinter = name; | |
375 | } | |
080811b1 MS |
376 | |
377 | if (defprinter) | |
ef416fc2 | 378 | { |
379 | /* | |
38e73f87 | 380 | * Separate printer and instance name... |
ef416fc2 | 381 | */ |
382 | ||
ef416fc2 | 383 | if ((instance = strchr(name, '/')) != NULL) |
384 | *instance++ = '\0'; | |
385 | ||
386 | /* | |
387 | * Lookup the printer and instance and make it the default... | |
388 | */ | |
389 | ||
390 | if ((dest = cupsGetDest(name, instance, num_dests, *dests)) != NULL) | |
391 | dest->is_default = 1; | |
392 | } | |
393 | else | |
ef416fc2 | 394 | instance = NULL; |
ef416fc2 | 395 | |
396 | /* | |
b423cd4c | 397 | * Load the /etc/cups/lpoptions and ~/.cups/lpoptions files... |
ef416fc2 | 398 | */ |
399 | ||
400 | snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); | |
38e73f87 MS |
401 | num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL, |
402 | num_dests, dests); | |
ef416fc2 | 403 | |
404 | if ((home = getenv("HOME")) != NULL) | |
405 | { | |
b423cd4c | 406 | snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); |
b423cd4c | 407 | |
38e73f87 MS |
408 | num_dests = cups_get_dests(filename, NULL, NULL, user_default != NULL, |
409 | num_dests, dests); | |
ef416fc2 | 410 | } |
411 | ||
412 | /* | |
413 | * Validate the current default destination - this prevents old | |
b423cd4c | 414 | * Default lines in /etc/cups/lpoptions and ~/.cups/lpoptions from |
ef416fc2 | 415 | * pointing to a non-existent printer or class... |
416 | */ | |
417 | ||
418 | if (num_reals) | |
419 | { | |
420 | /* | |
421 | * See if we have a default printer... | |
422 | */ | |
423 | ||
424 | if ((dest = cupsGetDest(NULL, NULL, num_dests, *dests)) != NULL) | |
425 | { | |
426 | /* | |
427 | * Have a default; see if it is real... | |
428 | */ | |
429 | ||
430 | dest = cupsGetDest(dest->name, NULL, num_reals, reals); | |
431 | } | |
432 | ||
433 | /* | |
434 | * If dest is NULL, then no default (that exists) is set, so we | |
435 | * need to set a default if one exists... | |
436 | */ | |
437 | ||
438 | if (dest == NULL && defprinter != NULL) | |
439 | { | |
440 | for (i = 0; i < num_dests; i ++) | |
441 | (*dests)[i].is_default = 0; | |
442 | ||
443 | if ((dest = cupsGetDest(name, instance, num_dests, *dests)) != NULL) | |
444 | dest->is_default = 1; | |
445 | } | |
446 | ||
447 | /* | |
448 | * Free memory... | |
449 | */ | |
450 | ||
451 | free(reals); | |
452 | } | |
453 | ||
454 | /* | |
455 | * Return the number of destinations... | |
456 | */ | |
457 | ||
1ff0402e MS |
458 | if (num_dests > 0) |
459 | _cupsSetError(IPP_OK, NULL, 0); | |
460 | ||
ef416fc2 | 461 | return (num_dests); |
462 | } | |
463 | ||
464 | ||
a4924f6c MS |
465 | /* |
466 | * 'cupsGetNamedDest()' - Get options for the named destination. | |
467 | * | |
468 | * This function is optimized for retrieving a single destination and should | |
5a738aea MS |
469 | * be used instead of @link cupsGetDests@ and @link cupsGetDest@ when you either |
470 | * know the name of the destination or want to print to the default destination. | |
471 | * If @code NULL@ is returned, the destination does not exist or there is no | |
472 | * default destination. | |
a4924f6c | 473 | * |
5a738aea MS |
474 | * If "http" is @code CUPS_HTTP_DEFAULT@, the connection to the default print |
475 | * server will be used. | |
a4924f6c | 476 | * |
5a738aea MS |
477 | * If "name" is @code NULL@, the default printer for the current user will be |
478 | * returned. | |
a4924f6c | 479 | * |
5a738aea MS |
480 | * The returned destination must be freed using @link cupsFreeDests@ with a |
481 | * "num_dests" value of 1. | |
a4924f6c | 482 | * |
178cb736 | 483 | * @since CUPS 1.4/Mac OS X 10.6@ |
a4924f6c MS |
484 | */ |
485 | ||
5a738aea | 486 | cups_dest_t * /* O - Destination or @code NULL@ */ |
568fa3fa MS |
487 | cupsGetNamedDest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
488 | const char *name, /* I - Destination name or @code NULL@ for the default destination */ | |
5a738aea | 489 | const char *instance) /* I - Instance name or @code NULL@ */ |
a4924f6c MS |
490 | { |
491 | cups_dest_t *dest; /* Destination */ | |
492 | char filename[1024], /* Path to lpoptions */ | |
493 | defname[256]; /* Default printer name */ | |
494 | const char *home = getenv("HOME"); /* Home directory */ | |
38e73f87 | 495 | int set_as_default = 0; /* Set returned destination as default */ |
a4924f6c MS |
496 | ipp_op_t op = IPP_GET_PRINTER_ATTRIBUTES; |
497 | /* IPP operation to get server ops */ | |
498 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
499 | ||
500 | ||
a4924f6c MS |
501 | /* |
502 | * If "name" is NULL, find the default destination... | |
503 | */ | |
504 | ||
505 | if (!name) | |
506 | { | |
38e73f87 MS |
507 | set_as_default = 1; |
508 | name = _cupsUserDefault(defname, sizeof(defname)); | |
a4924f6c MS |
509 | |
510 | if (!name && home) | |
511 | { | |
512 | /* | |
513 | * No default in the environment, try the user's lpoptions files... | |
514 | */ | |
515 | ||
516 | snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); | |
517 | ||
5a6b583a | 518 | name = cups_get_default(filename, defname, sizeof(defname), &instance); |
a4924f6c MS |
519 | } |
520 | ||
521 | if (!name) | |
522 | { | |
523 | /* | |
524 | * Still not there? Try the system lpoptions file... | |
525 | */ | |
526 | ||
527 | snprintf(filename, sizeof(filename), "%s/lpoptions", | |
528 | cg->cups_serverroot); | |
529 | name = cups_get_default(filename, defname, sizeof(defname), &instance); | |
530 | } | |
531 | ||
532 | if (!name) | |
533 | { | |
534 | /* | |
535 | * No locally-set default destination, ask the server... | |
536 | */ | |
537 | ||
538 | op = CUPS_GET_DEFAULT; | |
539 | } | |
540 | } | |
541 | ||
542 | /* | |
543 | * Get the printer's attributes... | |
544 | */ | |
545 | ||
546 | if (!cups_get_sdests(http, op, name, 0, &dest)) | |
745129be | 547 | { |
ba55dc12 | 548 | if (op == CUPS_GET_DEFAULT || (name && !set_as_default)) |
745129be MS |
549 | return (NULL); |
550 | ||
551 | /* | |
552 | * The default printer from environment variables or from a | |
553 | * configuration file does not exist. Find out the real default. | |
554 | */ | |
555 | ||
7a0cbd5e | 556 | if (!cups_get_sdests(http, CUPS_GET_DEFAULT, NULL, 0, &dest)) |
745129be MS |
557 | return (NULL); |
558 | } | |
a4924f6c MS |
559 | |
560 | if (instance) | |
561 | dest->instance = _cupsStrAlloc(instance); | |
562 | ||
38e73f87 MS |
563 | if (set_as_default) |
564 | dest->is_default = 1; | |
565 | ||
a4924f6c MS |
566 | /* |
567 | * Then add local options... | |
568 | */ | |
569 | ||
570 | snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); | |
38e73f87 | 571 | cups_get_dests(filename, name, instance, 1, 1, &dest); |
a4924f6c MS |
572 | |
573 | if (home) | |
574 | { | |
575 | snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); | |
576 | ||
38e73f87 | 577 | cups_get_dests(filename, name, instance, 1, 1, &dest); |
a4924f6c MS |
578 | } |
579 | ||
580 | /* | |
581 | * Return the result... | |
582 | */ | |
583 | ||
584 | return (dest); | |
585 | } | |
586 | ||
587 | ||
f7deaa1a | 588 | /* |
589 | * 'cupsRemoveDest()' - Remove a destination from the destination list. | |
590 | * | |
591 | * Removing a destination/instance does not delete the class or printer | |
592 | * queue, merely the lpoptions for that destination/instance. Use the | |
5a738aea MS |
593 | * @link cupsSetDests@ or @link cupsSetDests2@ functions to save the new |
594 | * options for the user. | |
f7deaa1a | 595 | * |
426c6a59 | 596 | * @since CUPS 1.3/Mac OS X 10.5@ |
f7deaa1a | 597 | */ |
598 | ||
599 | int /* O - New number of destinations */ | |
600 | cupsRemoveDest(const char *name, /* I - Destination name */ | |
5a738aea | 601 | const char *instance, /* I - Instance name or @code NULL@ */ |
f7deaa1a | 602 | int num_dests, /* I - Number of destinations */ |
603 | cups_dest_t **dests) /* IO - Destinations */ | |
604 | { | |
605 | int i; /* Index into destinations */ | |
606 | cups_dest_t *dest; /* Pointer to destination */ | |
607 | ||
608 | ||
609 | /* | |
610 | * Find the destination... | |
611 | */ | |
612 | ||
613 | if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL) | |
614 | return (num_dests); | |
615 | ||
616 | /* | |
617 | * Free memory... | |
618 | */ | |
619 | ||
2e4ff8af MS |
620 | _cupsStrFree(dest->name); |
621 | _cupsStrFree(dest->instance); | |
f7deaa1a | 622 | cupsFreeOptions(dest->num_options, dest->options); |
623 | ||
624 | /* | |
625 | * Remove the destination from the array... | |
626 | */ | |
627 | ||
628 | num_dests --; | |
629 | ||
630 | i = dest - *dests; | |
631 | ||
632 | if (i < num_dests) | |
633 | memmove(dest, dest + 1, (num_dests - i) * sizeof(cups_dest_t)); | |
634 | ||
635 | return (num_dests); | |
636 | } | |
637 | ||
638 | ||
639 | /* | |
3d052e43 | 640 | * 'cupsSetDefaultDest()' - Set the default destination. |
f7deaa1a | 641 | * |
426c6a59 | 642 | * @since CUPS 1.3/Mac OS X 10.5@ |
f7deaa1a | 643 | */ |
644 | ||
645 | void | |
646 | cupsSetDefaultDest( | |
647 | const char *name, /* I - Destination name */ | |
5a738aea | 648 | const char *instance, /* I - Instance name or @code NULL@ */ |
f7deaa1a | 649 | int num_dests, /* I - Number of destinations */ |
650 | cups_dest_t *dests) /* I - Destinations */ | |
651 | { | |
652 | int i; /* Looping var */ | |
653 | cups_dest_t *dest; /* Current destination */ | |
654 | ||
655 | ||
656 | /* | |
657 | * Range check input... | |
658 | */ | |
659 | ||
660 | if (!name || num_dests <= 0 || !dests) | |
661 | return; | |
662 | ||
663 | /* | |
664 | * Loop through the array and set the "is_default" flag for the matching | |
665 | * destination... | |
666 | */ | |
667 | ||
668 | for (i = num_dests, dest = dests; i > 0; i --, dest ++) | |
669 | dest->is_default = !strcasecmp(name, dest->name) && | |
670 | ((!instance && !dest->instance) || | |
671 | (instance && dest->instance && | |
672 | !strcasecmp(instance, dest->instance))); | |
673 | } | |
674 | ||
675 | ||
ef416fc2 | 676 | /* |
677 | * 'cupsSetDests()' - Save the list of destinations for the default server. | |
678 | * | |
679 | * This function saves the destinations to /etc/cups/lpoptions when run | |
b423cd4c | 680 | * as root and ~/.cups/lpoptions when run as a normal user. |
ef416fc2 | 681 | */ |
682 | ||
683 | void | |
684 | cupsSetDests(int num_dests, /* I - Number of destinations */ | |
685 | cups_dest_t *dests) /* I - Destinations */ | |
686 | { | |
3d052e43 | 687 | cupsSetDests2(CUPS_HTTP_DEFAULT, num_dests, dests); |
ef416fc2 | 688 | } |
689 | ||
690 | ||
691 | /* | |
692 | * 'cupsSetDests2()' - Save the list of destinations for the specified server. | |
693 | * | |
694 | * This function saves the destinations to /etc/cups/lpoptions when run | |
b423cd4c | 695 | * as root and ~/.cups/lpoptions when run as a normal user. |
ef416fc2 | 696 | * |
426c6a59 | 697 | * @since CUPS 1.1.21/Mac OS X 10.4@ |
ef416fc2 | 698 | */ |
699 | ||
700 | int /* O - 0 on success, -1 on error */ | |
568fa3fa | 701 | cupsSetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 702 | int num_dests, /* I - Number of destinations */ |
703 | cups_dest_t *dests) /* I - Destinations */ | |
704 | { | |
705 | int i, j; /* Looping vars */ | |
706 | int wrote; /* Wrote definition? */ | |
707 | cups_dest_t *dest; /* Current destination */ | |
708 | cups_option_t *option; /* Current option */ | |
8ca02f3c | 709 | _ipp_option_t *match; /* Matching attribute for option */ |
ef416fc2 | 710 | FILE *fp; /* File pointer */ |
b86bc4cf | 711 | #ifndef WIN32 |
ef416fc2 | 712 | const char *home; /* HOME environment variable */ |
b86bc4cf | 713 | #endif /* WIN32 */ |
ef416fc2 | 714 | char filename[1024]; /* lpoptions file */ |
715 | int num_temps; /* Number of temporary destinations */ | |
716 | cups_dest_t *temps, /* Temporary destinations */ | |
717 | *temp; /* Current temporary dest */ | |
718 | const char *val; /* Value of temporary option */ | |
3d052e43 | 719 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
ef416fc2 | 720 | |
721 | ||
722 | /* | |
723 | * Range check the input... | |
724 | */ | |
725 | ||
3d052e43 | 726 | if (!num_dests || !dests) |
ef416fc2 | 727 | return (-1); |
728 | ||
729 | /* | |
730 | * Get the server destinations... | |
731 | */ | |
732 | ||
a4924f6c | 733 | num_temps = cups_get_sdests(http, CUPS_GET_PRINTERS, NULL, 0, &temps); |
426c6a59 MS |
734 | |
735 | if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE) | |
736 | { | |
737 | cupsFreeDests(num_temps, temps); | |
738 | return (-1); | |
739 | } | |
ef416fc2 | 740 | |
741 | /* | |
742 | * Figure out which file to write to... | |
743 | */ | |
744 | ||
745 | snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot); | |
746 | ||
747 | #ifndef WIN32 | |
748 | if (getuid()) | |
749 | { | |
750 | /* | |
751 | * Merge in server defaults... | |
752 | */ | |
753 | ||
38e73f87 | 754 | num_temps = cups_get_dests(filename, NULL, NULL, 0, num_temps, &temps); |
ef416fc2 | 755 | |
756 | /* | |
757 | * Point to user defaults... | |
758 | */ | |
759 | ||
760 | if ((home = getenv("HOME")) != NULL) | |
b423cd4c | 761 | { |
b423cd4c | 762 | /* |
763 | * Create ~/.cups subdirectory... | |
764 | */ | |
765 | ||
766 | snprintf(filename, sizeof(filename), "%s/.cups", home); | |
767 | if (access(filename, 0)) | |
768 | mkdir(filename, 0700); | |
769 | ||
770 | snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home); | |
771 | } | |
ef416fc2 | 772 | } |
773 | #endif /* !WIN32 */ | |
774 | ||
775 | /* | |
776 | * Try to open the file... | |
777 | */ | |
778 | ||
779 | if ((fp = fopen(filename, "w")) == NULL) | |
780 | { | |
781 | cupsFreeDests(num_temps, temps); | |
782 | return (-1); | |
783 | } | |
784 | ||
d6ae789d | 785 | #ifndef WIN32 |
786 | /* | |
787 | * Set the permissions to 0644 when saving to the /etc/cups/lpoptions | |
788 | * file... | |
789 | */ | |
790 | ||
791 | if (!getuid()) | |
792 | fchmod(fileno(fp), 0644); | |
793 | #endif /* !WIN32 */ | |
794 | ||
ef416fc2 | 795 | /* |
796 | * Write each printer; each line looks like: | |
797 | * | |
798 | * Dest name[/instance] options | |
799 | * Default name[/instance] options | |
800 | */ | |
801 | ||
802 | for (i = num_dests, dest = dests; i > 0; i --, dest ++) | |
803 | if (dest->instance != NULL || dest->num_options != 0 || dest->is_default) | |
804 | { | |
805 | if (dest->is_default) | |
806 | { | |
807 | fprintf(fp, "Default %s", dest->name); | |
808 | if (dest->instance) | |
809 | fprintf(fp, "/%s", dest->instance); | |
810 | ||
811 | wrote = 1; | |
812 | } | |
813 | else | |
814 | wrote = 0; | |
815 | ||
816 | if ((temp = cupsGetDest(dest->name, dest->instance, num_temps, temps)) == NULL) | |
817 | temp = cupsGetDest(dest->name, NULL, num_temps, temps); | |
818 | ||
819 | for (j = dest->num_options, option = dest->options; j > 0; j --, option ++) | |
820 | { | |
8ca02f3c | 821 | /* |
822 | * See if this option is a printer attribute; if so, skip it... | |
823 | */ | |
824 | ||
825 | if ((match = _ippFindOption(option->name)) != NULL && | |
826 | match->group_tag == IPP_TAG_PRINTER) | |
827 | continue; | |
828 | ||
ef416fc2 | 829 | /* |
830 | * See if the server/global options match these; if so, don't | |
831 | * write 'em. | |
832 | */ | |
833 | ||
8ca02f3c | 834 | if (temp && |
835 | (val = cupsGetOption(option->name, temp->num_options, | |
836 | temp->options)) != NULL && | |
837 | !strcasecmp(val, option->value)) | |
838 | continue; | |
ef416fc2 | 839 | |
840 | /* | |
841 | * Options don't match, write to the file... | |
842 | */ | |
843 | ||
844 | if (!wrote) | |
845 | { | |
846 | fprintf(fp, "Dest %s", dest->name); | |
847 | if (dest->instance) | |
848 | fprintf(fp, "/%s", dest->instance); | |
849 | wrote = 1; | |
850 | } | |
851 | ||
852 | if (option->value[0]) | |
853 | { | |
8ca02f3c | 854 | if (strchr(option->value, ' ') || |
855 | strchr(option->value, '\\') || | |
856 | strchr(option->value, '\"') || | |
857 | strchr(option->value, '\'')) | |
858 | { | |
859 | /* | |
860 | * Quote the value... | |
861 | */ | |
862 | ||
863 | fprintf(fp, " %s=\"", option->name); | |
864 | ||
865 | for (val = option->value; *val; val ++) | |
866 | { | |
867 | if (strchr("\"\'\\", *val)) | |
868 | putc('\\', fp); | |
869 | ||
870 | putc(*val, fp); | |
871 | } | |
872 | ||
873 | putc('\"', fp); | |
874 | } | |
875 | else | |
876 | { | |
877 | /* | |
878 | * Store the literal value... | |
879 | */ | |
880 | ||
ef416fc2 | 881 | fprintf(fp, " %s=%s", option->name, option->value); |
8ca02f3c | 882 | } |
ef416fc2 | 883 | } |
884 | else | |
885 | fprintf(fp, " %s", option->name); | |
886 | } | |
887 | ||
888 | if (wrote) | |
889 | fputs("\n", fp); | |
890 | } | |
891 | ||
892 | /* | |
fa73b229 | 893 | * Free the temporary destinations and close the file... |
ef416fc2 | 894 | */ |
895 | ||
896 | cupsFreeDests(num_temps, temps); | |
897 | ||
fa73b229 | 898 | fclose(fp); |
899 | ||
080811b1 MS |
900 | #ifdef __APPLE__ |
901 | /* | |
902 | * Set the default printer for this location - this allows command-line | |
903 | * and GUI applications to share the same default destination... | |
904 | */ | |
905 | ||
906 | if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL) | |
907 | appleSetDefault(dest->name); | |
908 | #endif /* __APPLE__ */ | |
909 | ||
fa73b229 | 910 | #ifdef HAVE_NOTIFY_POST |
ef416fc2 | 911 | /* |
fa73b229 | 912 | * Send a notification so that MacOS X applications can know about the |
913 | * change, too. | |
ef416fc2 | 914 | */ |
915 | ||
fa73b229 | 916 | notify_post("com.apple.printerListChange"); |
917 | #endif /* HAVE_NOTIFY_POST */ | |
ef416fc2 | 918 | |
919 | return (0); | |
920 | } | |
921 | ||
922 | ||
38e73f87 MS |
923 | /* |
924 | * '_cupsUserDefault()' - Get the user default printer from environment | |
925 | * variables and location information. | |
926 | */ | |
927 | ||
928 | char * /* O - Default printer or NULL */ | |
929 | _cupsUserDefault(char *name, /* I - Name buffer */ | |
930 | size_t namesize) /* I - Size of name buffer */ | |
931 | { | |
932 | const char *env; /* LPDEST or PRINTER env variable */ | |
933 | #ifdef __APPLE__ | |
934 | CFStringRef network; /* Network location */ | |
935 | CFArrayRef locations; /* Location array */ | |
936 | CFStringRef locprinter; /* Current printer */ | |
937 | #endif /* __APPLE__ */ | |
938 | ||
939 | ||
940 | if ((env = getenv("LPDEST")) == NULL) | |
941 | if ((env = getenv("PRINTER")) != NULL && !strcmp(env, "lp")) | |
942 | env = NULL; | |
943 | ||
944 | if (env) | |
945 | { | |
946 | strlcpy(name, env, namesize); | |
947 | return (name); | |
948 | } | |
949 | ||
950 | #ifdef __APPLE__ | |
951 | /* | |
952 | * Use location-based defaults if "use last printer" is selected in the | |
953 | * system preferences... | |
954 | */ | |
955 | ||
956 | if (!appleUseLastPrinter()) | |
957 | { | |
e07d4801 | 958 | DEBUG_puts("1_cupsUserDefault: Not using last printer as default..."); |
38e73f87 MS |
959 | name[0] = '\0'; |
960 | return (NULL); | |
961 | } | |
962 | ||
963 | /* | |
964 | * Get the current location... | |
965 | */ | |
966 | ||
967 | if ((network = appleCopyNetwork()) == NULL) | |
968 | { | |
e07d4801 | 969 | DEBUG_puts("1_cupsUserDefault: Unable to get current network..."); |
38e73f87 MS |
970 | name[0] = '\0'; |
971 | return (NULL); | |
972 | } | |
973 | ||
974 | # ifdef DEBUG | |
975 | CFStringGetCString(network, name, namesize, kCFStringEncodingUTF8); | |
e07d4801 | 976 | DEBUG_printf(("2_cupsUserDefault: network=\"%s\"", name)); |
38e73f87 MS |
977 | # endif /* DEBUG */ |
978 | ||
979 | /* | |
980 | * Lookup the network in the preferences... | |
981 | */ | |
982 | ||
983 | if ((locations = appleCopyLocations()) == NULL) | |
984 | { | |
985 | /* | |
986 | * Missing or bad location array, so no location-based default... | |
987 | */ | |
988 | ||
e07d4801 | 989 | DEBUG_puts("1_cupsUserDefault: Missing or bad location history array..."); |
38e73f87 MS |
990 | |
991 | CFRelease(network); | |
992 | ||
993 | name[0] = '\0'; | |
994 | return (NULL); | |
995 | } | |
996 | ||
e07d4801 | 997 | DEBUG_printf(("2_cupsUserDefault: Got location, %d entries...", |
38e73f87 MS |
998 | (int)CFArrayGetCount(locations))); |
999 | ||
1000 | if ((locprinter = appleGetPrinter(locations, network, NULL)) != NULL) | |
1001 | CFStringGetCString(locprinter, name, namesize, kCFStringEncodingUTF8); | |
1002 | else | |
1003 | name[0] = '\0'; | |
1004 | ||
1005 | CFRelease(network); | |
1006 | CFRelease(locations); | |
1007 | ||
e07d4801 | 1008 | DEBUG_printf(("1_cupsUserDefault: Returning \"%s\"...", name)); |
38e73f87 MS |
1009 | |
1010 | return (*name ? name : NULL); | |
1011 | ||
1012 | #else | |
1013 | /* | |
1014 | * No location-based defaults on this platform... | |
1015 | */ | |
1016 | ||
1017 | name[0] = '\0'; | |
1018 | return (NULL); | |
1019 | #endif /* __APPLE__ */ | |
1020 | } | |
1021 | ||
1022 | ||
080811b1 | 1023 | #ifdef __APPLE__ |
06d4e77b MS |
1024 | /* |
1025 | * 'appleCopyLocations()' - Copy the location history array. | |
1026 | */ | |
1027 | ||
1028 | static CFArrayRef /* O - Location array or NULL */ | |
1029 | appleCopyLocations(void) | |
1030 | { | |
1031 | CFArrayRef locations; /* Location array */ | |
1032 | ||
1033 | ||
1034 | /* | |
1035 | * Look up the location array in the preferences... | |
1036 | */ | |
1037 | ||
1038 | if ((locations = CFPreferencesCopyAppValue(kLocationHistoryArrayKey, | |
1039 | kPMPrintingPreferences)) == NULL) | |
1040 | return (NULL); | |
1041 | ||
1042 | if (CFGetTypeID(locations) != CFArrayGetTypeID()) | |
1043 | { | |
1044 | CFRelease(locations); | |
1045 | return (NULL); | |
1046 | } | |
1047 | ||
1048 | return (locations); | |
1049 | } | |
1050 | ||
1051 | ||
749b1e90 MS |
1052 | /* |
1053 | * 'appleCopyNetwork()' - Get the network ID for the current location. | |
1054 | */ | |
1055 | ||
1056 | static CFStringRef /* O - Network ID */ | |
1057 | appleCopyNetwork(void) | |
1058 | { | |
1059 | SCDynamicStoreRef dynamicStore; /* System configuration data */ | |
1060 | CFStringRef key; /* Current network configuration key */ | |
1061 | CFDictionaryRef ip_dict; /* Network configuration data */ | |
1062 | CFStringRef network = NULL; /* Current network ID */ | |
1063 | ||
1064 | ||
1065 | if ((dynamicStore = SCDynamicStoreCreate(NULL, CFSTR("Printing"), NULL, | |
1066 | NULL)) != NULL) | |
1067 | { | |
1068 | if ((key = SCDynamicStoreKeyCreateNetworkGlobalEntity( | |
1069 | NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4)) != NULL) | |
1070 | { | |
1071 | if ((ip_dict = SCDynamicStoreCopyValue(dynamicStore, key)) != NULL) | |
1072 | { | |
1073 | if ((network = CFDictionaryGetValue(ip_dict, | |
1074 | kSCPropNetIPv4Router)) != NULL) | |
1075 | CFRetain(network); | |
1076 | ||
1077 | CFRelease(ip_dict); | |
1078 | } | |
1079 | ||
1080 | CFRelease(key); | |
1081 | } | |
1082 | ||
1083 | CFRelease(dynamicStore); | |
1084 | } | |
1085 | ||
1086 | return (network); | |
1087 | } | |
1088 | ||
1089 | ||
426c6a59 MS |
1090 | /* |
1091 | * 'appleGetPaperSize()' - Get the default paper size. | |
1092 | */ | |
1093 | ||
1094 | static char * /* O - Default paper size */ | |
1095 | appleGetPaperSize(char *name, /* I - Paper size name buffer */ | |
1096 | int namesize) /* I - Size of buffer */ | |
1097 | { | |
54afec33 MS |
1098 | CFStringRef defaultPaperID; /* Default paper ID */ |
1099 | _pwg_media_t *pwgmedia; /* PWG media size */ | |
426c6a59 MS |
1100 | |
1101 | ||
1102 | defaultPaperID = CFPreferencesCopyAppValue(kDefaultPaperIDKey, | |
1103 | kPMPrintingPreferences); | |
1104 | if (!defaultPaperID || | |
557dde9f | 1105 | CFGetTypeID(defaultPaperID) != CFStringGetTypeID() || |
426c6a59 MS |
1106 | !CFStringGetCString(defaultPaperID, name, namesize, |
1107 | kCFStringEncodingUTF8)) | |
1108 | name[0] = '\0'; | |
54afec33 | 1109 | else if ((pwgmedia = _pwgMediaForLegacy(name)) != NULL) |
c168a833 | 1110 | strlcpy(name, pwgmedia->pwg, namesize); |
426c6a59 MS |
1111 | |
1112 | if (defaultPaperID) | |
1113 | CFRelease(defaultPaperID); | |
1114 | ||
1115 | return (name); | |
1116 | } | |
1117 | ||
1118 | ||
080811b1 MS |
1119 | /* |
1120 | * 'appleGetPrinter()' - Get a printer from the history array. | |
1121 | */ | |
1122 | ||
1123 | static CFStringRef /* O - Printer name or NULL */ | |
1124 | appleGetPrinter(CFArrayRef locations, /* I - Location array */ | |
1125 | CFStringRef network, /* I - Network name */ | |
1126 | CFIndex *locindex) /* O - Index in array */ | |
1127 | { | |
1128 | CFIndex i, /* Looping var */ | |
1129 | count; /* Number of locations */ | |
1130 | CFDictionaryRef location; /* Current location */ | |
1131 | CFStringRef locnetwork, /* Current network */ | |
1132 | locprinter; /* Current printer */ | |
1133 | ||
1134 | ||
1135 | for (i = 0, count = CFArrayGetCount(locations); i < count; i ++) | |
1136 | if ((location = CFArrayGetValueAtIndex(locations, i)) != NULL && | |
1137 | CFGetTypeID(location) == CFDictionaryGetTypeID()) | |
1138 | { | |
1139 | if ((locnetwork = CFDictionaryGetValue(location, | |
1140 | kLocationNetworkKey)) != NULL && | |
1141 | CFGetTypeID(locnetwork) == CFStringGetTypeID() && | |
1142 | CFStringCompare(network, locnetwork, 0) == kCFCompareEqualTo && | |
1143 | (locprinter = CFDictionaryGetValue(location, | |
1144 | kLocationPrinterIDKey)) != NULL && | |
1145 | CFGetTypeID(locprinter) == CFStringGetTypeID()) | |
1146 | { | |
1147 | if (locindex) | |
1148 | *locindex = i; | |
1149 | ||
1150 | return (locprinter); | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | return (NULL); | |
1155 | } | |
1156 | ||
1157 | ||
1158 | /* | |
1159 | * 'appleSetDefault()' - Set the default printer for this location. | |
1160 | */ | |
1161 | ||
1162 | static void | |
1163 | appleSetDefault(const char *name) /* I - Default printer/class name */ | |
1164 | { | |
1165 | CFStringRef network; /* Current network */ | |
1166 | CFArrayRef locations; /* Old locations array */ | |
1167 | CFIndex locindex; /* Index in locations array */ | |
1168 | CFStringRef locprinter; /* Current printer */ | |
1169 | CFMutableArrayRef newlocations; /* New locations array */ | |
1170 | CFMutableDictionaryRef newlocation; /* New location */ | |
1171 | CFStringRef newprinter; /* New printer */ | |
1172 | ||
1173 | ||
1174 | /* | |
1175 | * Get the current location... | |
1176 | */ | |
1177 | ||
749b1e90 | 1178 | if ((network = appleCopyNetwork()) == NULL) |
080811b1 | 1179 | { |
e07d4801 | 1180 | DEBUG_puts("1appleSetDefault: Unable to get current network..."); |
080811b1 MS |
1181 | return; |
1182 | } | |
1183 | ||
080811b1 MS |
1184 | if ((newprinter = CFStringCreateWithCString(kCFAllocatorDefault, name, |
1185 | kCFStringEncodingUTF8)) == NULL) | |
1186 | { | |
1187 | CFRelease(network); | |
1188 | return; | |
1189 | } | |
1190 | ||
1191 | /* | |
1192 | * Lookup the network in the preferences... | |
1193 | */ | |
1194 | ||
06d4e77b | 1195 | if ((locations = appleCopyLocations()) != NULL) |
080811b1 MS |
1196 | locprinter = appleGetPrinter(locations, network, &locindex); |
1197 | else | |
1198 | { | |
1199 | locprinter = NULL; | |
1200 | locindex = -1; | |
1201 | } | |
1202 | ||
1203 | if (!locprinter || | |
1204 | CFStringCompare(locprinter, newprinter, 0) != kCFCompareEqualTo) | |
1205 | { | |
1206 | /* | |
1207 | * Need to change the locations array... | |
1208 | */ | |
1209 | ||
1210 | if (locations) | |
1211 | { | |
1212 | newlocations = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, | |
1213 | locations); | |
1214 | ||
1215 | if (locprinter) | |
1216 | CFArrayRemoveValueAtIndex(newlocations, locindex); | |
1217 | } | |
1218 | else | |
1219 | newlocations = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL); | |
1220 | ||
1221 | newlocation = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
1222 | &kCFTypeDictionaryKeyCallBacks, | |
1223 | &kCFTypeDictionaryValueCallBacks); | |
1224 | ||
1225 | if (newlocation && newlocations) | |
1226 | { | |
1227 | /* | |
1228 | * Put the new location at the front of the array... | |
1229 | */ | |
1230 | ||
1231 | CFDictionaryAddValue(newlocation, kLocationNetworkKey, network); | |
1232 | CFDictionaryAddValue(newlocation, kLocationPrinterIDKey, newprinter); | |
1233 | CFArrayInsertValueAtIndex(newlocations, 0, newlocation); | |
1234 | ||
1235 | /* | |
1236 | * Limit the number of locations to 10... | |
1237 | */ | |
1238 | ||
1239 | while (CFArrayGetCount(newlocations) > 10) | |
1240 | CFArrayRemoveValueAtIndex(newlocations, 10); | |
1241 | ||
1242 | /* | |
1243 | * Push the changes out... | |
1244 | */ | |
1245 | ||
1246 | CFPreferencesSetAppValue(kLocationHistoryArrayKey, newlocations, | |
1247 | kPMPrintingPreferences); | |
1248 | CFPreferencesAppSynchronize(kPMPrintingPreferences); | |
38e73f87 | 1249 | notify_post("com.apple.printerPrefsChange"); |
080811b1 MS |
1250 | } |
1251 | ||
1252 | if (newlocations) | |
1253 | CFRelease(newlocations); | |
1254 | ||
1255 | if (newlocation) | |
1256 | CFRelease(newlocation); | |
1257 | } | |
91c84a35 | 1258 | |
06d4e77b MS |
1259 | if (locations) |
1260 | CFRelease(locations); | |
1261 | ||
749b1e90 | 1262 | CFRelease(network); |
080811b1 MS |
1263 | CFRelease(newprinter); |
1264 | } | |
1265 | ||
1266 | ||
1267 | /* | |
1268 | * 'appleUseLastPrinter()' - Get the default printer preference value. | |
1269 | */ | |
1270 | ||
1271 | static int /* O - 1 to use last printer, 0 otherwise */ | |
1272 | appleUseLastPrinter(void) | |
1273 | { | |
1274 | CFPropertyListRef uselast; /* Use last printer preference value */ | |
1275 | ||
1276 | ||
ef55b745 MS |
1277 | if (getenv("CUPS_DISABLE_APPLE_DEFAULT")) |
1278 | return (0); | |
1279 | ||
080811b1 MS |
1280 | if ((uselast = CFPreferencesCopyAppValue(kUseLastPrinterAsCurrentPrinterKey, |
1281 | kPMPrintingPreferences)) != NULL) | |
1282 | { | |
1283 | CFRelease(uselast); | |
1284 | ||
1285 | if (uselast == kCFBooleanFalse) | |
1286 | return (0); | |
1287 | } | |
1288 | ||
1289 | return (1); | |
1290 | } | |
1291 | #endif /* __APPLE__ */ | |
1292 | ||
1293 | ||
426c6a59 MS |
1294 | /* |
1295 | * 'cups_add_dest()' - Add a destination to the array. | |
1296 | * | |
1297 | * Unlike cupsAddDest(), this function does not check for duplicates. | |
1298 | */ | |
1299 | ||
1300 | static cups_dest_t * /* O - New destination */ | |
1301 | cups_add_dest(const char *name, /* I - Name of destination */ | |
1302 | const char *instance, /* I - Instance or NULL */ | |
1303 | int *num_dests, /* IO - Number of destinations */ | |
1304 | cups_dest_t **dests) /* IO - Destinations */ | |
1305 | { | |
1306 | int insert, /* Insertion point */ | |
1307 | diff; /* Result of comparison */ | |
1308 | cups_dest_t *dest; /* Destination pointer */ | |
1309 | ||
1310 | ||
1311 | /* | |
1312 | * Add new destination... | |
1313 | */ | |
1314 | ||
1315 | if (*num_dests == 0) | |
1316 | dest = malloc(sizeof(cups_dest_t)); | |
1317 | else | |
1318 | dest = realloc(*dests, sizeof(cups_dest_t) * (*num_dests + 1)); | |
1319 | ||
1320 | if (!dest) | |
1321 | return (NULL); | |
1322 | ||
1323 | *dests = dest; | |
1324 | ||
1325 | /* | |
1326 | * Find where to insert the destination... | |
1327 | */ | |
1328 | ||
1329 | if (*num_dests == 0) | |
1330 | insert = 0; | |
1331 | else | |
1332 | { | |
1333 | insert = cups_find_dest(name, instance, *num_dests, *dests, *num_dests - 1, | |
1334 | &diff); | |
1335 | ||
1336 | if (diff > 0) | |
1337 | insert ++; | |
1338 | } | |
1339 | ||
1340 | /* | |
1341 | * Move the array elements as needed... | |
1342 | */ | |
1343 | ||
1344 | if (insert < *num_dests) | |
1345 | memmove(*dests + insert + 1, *dests + insert, | |
1346 | (*num_dests - insert) * sizeof(cups_dest_t)); | |
1347 | ||
1348 | (*num_dests) ++; | |
1349 | ||
1350 | /* | |
1351 | * Initialize the destination... | |
1352 | */ | |
1353 | ||
1354 | dest = *dests + insert; | |
1355 | dest->name = _cupsStrAlloc(name); | |
1356 | dest->instance = _cupsStrAlloc(instance); | |
1357 | dest->is_default = 0; | |
1358 | dest->num_options = 0; | |
1359 | dest->options = (cups_option_t *)0; | |
1360 | ||
1361 | return (dest); | |
1362 | } | |
1363 | ||
1364 | ||
1365 | /* | |
1366 | * 'cups_compare_dests()' - Compare two destinations. | |
1367 | */ | |
1368 | ||
1369 | static int /* O - Result of comparison */ | |
1370 | cups_compare_dests(cups_dest_t *a, /* I - First destination */ | |
1371 | cups_dest_t *b) /* I - Second destination */ | |
1372 | { | |
1373 | int diff; /* Difference */ | |
1374 | ||
1375 | ||
1376 | if ((diff = strcasecmp(a->name, b->name)) != 0) | |
1377 | return (diff); | |
1378 | else if (a->instance && b->instance) | |
1379 | return (strcasecmp(a->instance, b->instance)); | |
1380 | else | |
1381 | return ((a->instance && !b->instance) - (!a->instance && b->instance)); | |
1382 | } | |
1383 | ||
1384 | ||
1385 | /* | |
1386 | * 'cups_find_dest()' - Find a destination using a binary search. | |
1387 | */ | |
1388 | ||
1389 | static int /* O - Index of match */ | |
1390 | cups_find_dest(const char *name, /* I - Destination name */ | |
1391 | const char *instance, /* I - Instance or NULL */ | |
1392 | int num_dests, /* I - Number of destinations */ | |
1393 | cups_dest_t *dests, /* I - Destinations */ | |
1394 | int prev, /* I - Previous index */ | |
1395 | int *rdiff) /* O - Difference of match */ | |
1396 | { | |
1397 | int left, /* Low mark for binary search */ | |
1398 | right, /* High mark for binary search */ | |
1399 | current, /* Current index */ | |
1400 | diff; /* Result of comparison */ | |
1401 | cups_dest_t key; /* Search key */ | |
1402 | ||
1403 | ||
1404 | key.name = (char *)name; | |
1405 | key.instance = (char *)instance; | |
1406 | ||
1407 | if (prev >= 0) | |
1408 | { | |
1409 | /* | |
1410 | * Start search on either side of previous... | |
1411 | */ | |
1412 | ||
1413 | if ((diff = cups_compare_dests(&key, dests + prev)) == 0 || | |
1414 | (diff < 0 && prev == 0) || | |
1415 | (diff > 0 && prev == (num_dests - 1))) | |
1416 | { | |
1417 | *rdiff = diff; | |
1418 | return (prev); | |
1419 | } | |
1420 | else if (diff < 0) | |
1421 | { | |
1422 | /* | |
1423 | * Start with previous on right side... | |
1424 | */ | |
1425 | ||
1426 | left = 0; | |
1427 | right = prev; | |
1428 | } | |
1429 | else | |
1430 | { | |
1431 | /* | |
1432 | * Start wih previous on left side... | |
1433 | */ | |
1434 | ||
1435 | left = prev; | |
1436 | right = num_dests - 1; | |
1437 | } | |
1438 | } | |
1439 | else | |
1440 | { | |
1441 | /* | |
1442 | * Start search in the middle... | |
1443 | */ | |
1444 | ||
1445 | left = 0; | |
1446 | right = num_dests - 1; | |
1447 | } | |
1448 | ||
1449 | do | |
1450 | { | |
1451 | current = (left + right) / 2; | |
1452 | diff = cups_compare_dests(&key, dests + current); | |
1453 | ||
1454 | if (diff == 0) | |
1455 | break; | |
1456 | else if (diff < 0) | |
1457 | right = current; | |
1458 | else | |
1459 | left = current; | |
1460 | } | |
1461 | while ((right - left) > 1); | |
1462 | ||
1463 | if (diff != 0) | |
1464 | { | |
1465 | /* | |
1466 | * Check the last 1 or 2 elements... | |
1467 | */ | |
1468 | ||
1469 | if ((diff = cups_compare_dests(&key, dests + left)) <= 0) | |
1470 | current = left; | |
1471 | else | |
1472 | { | |
1473 | diff = cups_compare_dests(&key, dests + right); | |
1474 | current = right; | |
1475 | } | |
1476 | } | |
1477 | ||
1478 | /* | |
1479 | * Return the closest destination and the difference... | |
1480 | */ | |
1481 | ||
1482 | *rdiff = diff; | |
1483 | ||
1484 | return (current); | |
1485 | } | |
1486 | ||
1487 | ||
a4924f6c MS |
1488 | /* |
1489 | * 'cups_get_default()' - Get the default destination from an lpoptions file. | |
1490 | */ | |
1491 | ||
080811b1 | 1492 | static char * /* O - Default destination or NULL */ |
a4924f6c MS |
1493 | cups_get_default(const char *filename, /* I - File to read */ |
1494 | char *namebuf, /* I - Name buffer */ | |
1495 | size_t namesize, /* I - Size of name buffer */ | |
1496 | const char **instance) /* I - Instance */ | |
1497 | { | |
1498 | cups_file_t *fp; /* lpoptions file */ | |
1499 | char line[8192], /* Line from file */ | |
1500 | *value, /* Value for line */ | |
1501 | *nameptr; /* Pointer into name */ | |
1502 | int linenum; /* Current line */ | |
1503 | ||
1504 | ||
1505 | *namebuf = '\0'; | |
1506 | ||
1507 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
1508 | { | |
1509 | linenum = 0; | |
1510 | ||
1511 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
1512 | { | |
1513 | if (!strcasecmp(line, "default") && value) | |
1514 | { | |
1515 | strlcpy(namebuf, value, namesize); | |
1516 | ||
1517 | if ((nameptr = strchr(namebuf, ' ')) != NULL) | |
1518 | *nameptr = '\0'; | |
1519 | if ((nameptr = strchr(namebuf, '\t')) != NULL) | |
1520 | *nameptr = '\0'; | |
1521 | ||
1522 | if ((nameptr = strchr(namebuf, '/')) != NULL) | |
1523 | *nameptr++ = '\0'; | |
1524 | ||
1525 | *instance = nameptr; | |
1526 | break; | |
1527 | } | |
1528 | } | |
1529 | ||
1530 | cupsFileClose(fp); | |
1531 | } | |
1532 | ||
1533 | return (*namebuf ? namebuf : NULL); | |
1534 | } | |
1535 | ||
1536 | ||
ef416fc2 | 1537 | /* |
1538 | * 'cups_get_dests()' - Get destinations from a file. | |
1539 | */ | |
1540 | ||
1541 | static int /* O - Number of destinations */ | |
38e73f87 MS |
1542 | cups_get_dests( |
1543 | const char *filename, /* I - File to read from */ | |
1544 | const char *match_name, /* I - Destination name we want */ | |
1545 | const char *match_inst, /* I - Instance name we want */ | |
1546 | int user_default_set, /* I - User default printer set? */ | |
1547 | int num_dests, /* I - Number of destinations */ | |
1548 | cups_dest_t **dests) /* IO - Destinations */ | |
ef416fc2 | 1549 | { |
1550 | int i; /* Looping var */ | |
1551 | cups_dest_t *dest; /* Current destination */ | |
a4924f6c | 1552 | cups_file_t *fp; /* File pointer */ |
ef416fc2 | 1553 | char line[8192], /* Line from file */ |
1554 | *lineptr, /* Pointer into line */ | |
1555 | *name, /* Name of destination/option */ | |
1556 | *instance; /* Instance of destination */ | |
a4924f6c | 1557 | int linenum; /* Current line number */ |
ef416fc2 | 1558 | |
1559 | ||
e07d4801 | 1560 | DEBUG_printf(("7cups_get_dests(filename=\"%s\", match_name=\"%s\", " |
38e73f87 | 1561 | "match_inst=\"%s\", user_default_set=%d, num_dests=%d, " |
e07d4801 | 1562 | "dests=%p)", filename, match_name, match_inst, |
38e73f87 | 1563 | user_default_set, num_dests, dests)); |
a4924f6c MS |
1564 | |
1565 | /* | |
1566 | * Try to open the file... | |
1567 | */ | |
1568 | ||
1569 | if ((fp = cupsFileOpen(filename, "r")) == NULL) | |
1570 | return (num_dests); | |
1571 | ||
ef416fc2 | 1572 | /* |
1573 | * Read each printer; each line looks like: | |
1574 | * | |
1575 | * Dest name[/instance] options | |
1576 | * Default name[/instance] options | |
1577 | */ | |
1578 | ||
a4924f6c MS |
1579 | linenum = 0; |
1580 | ||
1581 | while (cupsFileGetConf(fp, line, sizeof(line), &lineptr, &linenum)) | |
ef416fc2 | 1582 | { |
1583 | /* | |
1584 | * See what type of line it is... | |
1585 | */ | |
1586 | ||
e07d4801 | 1587 | DEBUG_printf(("9cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"", |
38e73f87 | 1588 | linenum, line, lineptr)); |
ef416fc2 | 1589 | |
a4924f6c MS |
1590 | if ((strcasecmp(line, "dest") && strcasecmp(line, "default")) || !lineptr) |
1591 | { | |
e07d4801 | 1592 | DEBUG_puts("9cups_get_dests: Not a dest or default line..."); |
ef416fc2 | 1593 | continue; |
a4924f6c | 1594 | } |
ef416fc2 | 1595 | |
1596 | name = lineptr; | |
1597 | ||
1598 | /* | |
1599 | * Search for an instance... | |
1600 | */ | |
1601 | ||
1602 | while (!isspace(*lineptr & 255) && *lineptr && *lineptr != '/') | |
1603 | lineptr ++; | |
1604 | ||
ef416fc2 | 1605 | if (*lineptr == '/') |
1606 | { | |
1607 | /* | |
1608 | * Found an instance... | |
1609 | */ | |
1610 | ||
1611 | *lineptr++ = '\0'; | |
1612 | instance = lineptr; | |
1613 | ||
1614 | /* | |
1615 | * Search for an instance... | |
1616 | */ | |
1617 | ||
1618 | while (!isspace(*lineptr & 255) && *lineptr) | |
1619 | lineptr ++; | |
1620 | } | |
1621 | else | |
1622 | instance = NULL; | |
1623 | ||
a4924f6c MS |
1624 | if (*lineptr) |
1625 | *lineptr++ = '\0'; | |
1626 | ||
e07d4801 | 1627 | DEBUG_printf(("9cups_get_dests: name=\"%s\", instance=\"%s\"", name, |
a4924f6c | 1628 | instance)); |
ef416fc2 | 1629 | |
1630 | /* | |
1631 | * See if the primary instance of the destination exists; if not, | |
1632 | * ignore this entry and move on... | |
1633 | */ | |
1634 | ||
a4924f6c MS |
1635 | if (match_name) |
1636 | { | |
1637 | if (strcasecmp(name, match_name) || | |
1638 | (!instance && match_inst) || | |
1639 | (instance && !match_inst) || | |
1640 | (instance && strcasecmp(instance, match_inst))) | |
1641 | continue; | |
ef416fc2 | 1642 | |
a4924f6c MS |
1643 | dest = *dests; |
1644 | } | |
1645 | else if (cupsGetDest(name, NULL, num_dests, *dests) == NULL) | |
1646 | { | |
e07d4801 | 1647 | DEBUG_puts("9cups_get_dests: Not found!"); |
a4924f6c MS |
1648 | continue; |
1649 | } | |
1650 | else | |
ef416fc2 | 1651 | { |
1652 | /* | |
a4924f6c | 1653 | * Add the destination... |
ef416fc2 | 1654 | */ |
1655 | ||
a4924f6c MS |
1656 | num_dests = cupsAddDest(name, instance, num_dests, dests); |
1657 | ||
1658 | if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL) | |
1659 | { | |
1660 | /* | |
1661 | * Out of memory! | |
1662 | */ | |
1663 | ||
e07d4801 | 1664 | DEBUG_puts("9cups_get_dests: Out of memory!"); |
a4924f6c MS |
1665 | break; |
1666 | } | |
ef416fc2 | 1667 | } |
1668 | ||
1669 | /* | |
1670 | * Add options until we hit the end of the line... | |
1671 | */ | |
1672 | ||
1673 | dest->num_options = cupsParseOptions(lineptr, dest->num_options, | |
1674 | &(dest->options)); | |
1675 | ||
a4924f6c MS |
1676 | /* |
1677 | * If we found what we were looking for, stop now... | |
1678 | */ | |
1679 | ||
1680 | if (match_name) | |
1681 | break; | |
1682 | ||
ef416fc2 | 1683 | /* |
1684 | * Set this as default if needed... | |
1685 | */ | |
1686 | ||
38e73f87 | 1687 | if (!user_default_set && !strcasecmp(line, "default")) |
ef416fc2 | 1688 | { |
e07d4801 | 1689 | DEBUG_puts("9cups_get_dests: Setting as default..."); |
a4924f6c | 1690 | |
ef416fc2 | 1691 | for (i = 0; i < num_dests; i ++) |
1692 | (*dests)[i].is_default = 0; | |
1693 | ||
1694 | dest->is_default = 1; | |
1695 | } | |
1696 | } | |
1697 | ||
1698 | /* | |
1699 | * Close the file and return... | |
1700 | */ | |
1701 | ||
a4924f6c | 1702 | cupsFileClose(fp); |
ef416fc2 | 1703 | |
1704 | return (num_dests); | |
1705 | } | |
1706 | ||
1707 | ||
1708 | /* | |
1709 | * 'cups_get_sdests()' - Get destinations from a server. | |
1710 | */ | |
1711 | ||
1712 | static int /* O - Number of destinations */ | |
568fa3fa | 1713 | cups_get_sdests(http_t *http, /* I - Connection to server or CUPS_HTTP_DEFAULT */ |
a4924f6c MS |
1714 | ipp_op_t op, /* I - IPP operation */ |
1715 | const char *name, /* I - Name of destination */ | |
ef416fc2 | 1716 | int num_dests, /* I - Number of destinations */ |
1717 | cups_dest_t **dests) /* IO - Destinations */ | |
1718 | { | |
1719 | cups_dest_t *dest; /* Current destination */ | |
1720 | ipp_t *request, /* IPP Request */ | |
1721 | *response; /* IPP Response */ | |
1722 | ipp_attribute_t *attr; /* Current attribute */ | |
426c6a59 MS |
1723 | const char *printer_name; /* printer-name attribute */ |
1724 | char uri[1024]; /* printer-uri value */ | |
f7deaa1a | 1725 | int num_options; /* Number of options */ |
1726 | cups_option_t *options; /* Options */ | |
426c6a59 MS |
1727 | #ifdef __APPLE__ |
1728 | char media_default[41]; /* Default paper size */ | |
1729 | #endif /* __APPLE__ */ | |
f7deaa1a | 1730 | char optname[1024], /* Option name */ |
1731 | value[2048], /* Option value */ | |
1732 | *ptr; /* Pointer into name/value */ | |
ef416fc2 | 1733 | static const char * const pattrs[] = /* Attributes we're interested in */ |
1734 | { | |
f42414bf | 1735 | "auth-info-required", |
d1c13e16 | 1736 | "device-uri", |
fa73b229 | 1737 | "job-sheets-default", |
426c6a59 MS |
1738 | "marker-change-time", |
1739 | "marker-colors", | |
1740 | "marker-high-levels", | |
1741 | "marker-levels", | |
1742 | "marker-low-levels", | |
1743 | "marker-message", | |
1744 | "marker-names", | |
1745 | "marker-types", | |
1746 | #ifdef __APPLE__ | |
1747 | "media-supported", | |
1748 | #endif /* __APPLE__ */ | |
1749 | "printer-commands", | |
d1c13e16 | 1750 | "printer-defaults", |
fa73b229 | 1751 | "printer-info", |
1752 | "printer-is-accepting-jobs", | |
1753 | "printer-is-shared", | |
f42414bf | 1754 | "printer-location", |
fa73b229 | 1755 | "printer-make-and-model", |
ef416fc2 | 1756 | "printer-name", |
fa73b229 | 1757 | "printer-state", |
1758 | "printer-state-change-time", | |
1759 | "printer-state-reasons", | |
f7deaa1a | 1760 | "printer-type", |
d1c13e16 | 1761 | "printer-uri-supported" |
ef416fc2 | 1762 | }; |
1763 | ||
1764 | ||
426c6a59 MS |
1765 | #ifdef __APPLE__ |
1766 | /* | |
1767 | * Get the default paper size... | |
1768 | */ | |
1769 | ||
1770 | appleGetPaperSize(media_default, sizeof(media_default)); | |
1771 | #endif /* __APPLE__ */ | |
1772 | ||
ef416fc2 | 1773 | /* |
426c6a59 MS |
1774 | * Build a CUPS_GET_PRINTERS or IPP_GET_PRINTER_ATTRIBUTES request, which |
1775 | * require the following attributes: | |
ef416fc2 | 1776 | * |
1777 | * attributes-charset | |
1778 | * attributes-natural-language | |
fa73b229 | 1779 | * requesting-user-name |
426c6a59 | 1780 | * printer-uri [for IPP_GET_PRINTER_ATTRIBUTES] |
ef416fc2 | 1781 | */ |
1782 | ||
fa73b229 | 1783 | request = ippNewRequest(op); |
ef416fc2 | 1784 | |
1785 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1786 | "requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]), | |
1787 | NULL, pattrs); | |
1788 | ||
fa73b229 | 1789 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, |
1790 | "requesting-user-name", NULL, cupsUser()); | |
1791 | ||
7a0cbd5e | 1792 | if (name && op != CUPS_GET_DEFAULT) |
a4924f6c MS |
1793 | { |
1794 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
1795 | "localhost", ippPort(), "/printers/%s", name); | |
1796 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
1797 | uri); | |
1798 | } | |
1799 | ||
ef416fc2 | 1800 | /* |
1801 | * Do the request and get back a response... | |
1802 | */ | |
1803 | ||
1804 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
1805 | { | |
1806 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
1807 | { | |
1808 | /* | |
1809 | * Skip leading attributes until we hit a printer... | |
1810 | */ | |
1811 | ||
1812 | while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER) | |
1813 | attr = attr->next; | |
1814 | ||
1815 | if (attr == NULL) | |
1816 | break; | |
1817 | ||
1818 | /* | |
f7deaa1a | 1819 | * Pull the needed attributes from this printer... |
ef416fc2 | 1820 | */ |
1821 | ||
a4924f6c MS |
1822 | printer_name = NULL; |
1823 | num_options = 0; | |
1824 | options = NULL; | |
ef416fc2 | 1825 | |
426c6a59 | 1826 | for (; attr && attr->group_tag == IPP_TAG_PRINTER; attr = attr->next) |
ef416fc2 | 1827 | { |
426c6a59 MS |
1828 | if (attr->value_tag != IPP_TAG_INTEGER && |
1829 | attr->value_tag != IPP_TAG_ENUM && | |
1830 | attr->value_tag != IPP_TAG_BOOLEAN && | |
1831 | attr->value_tag != IPP_TAG_TEXT && | |
1832 | attr->value_tag != IPP_TAG_TEXTLANG && | |
1833 | attr->value_tag != IPP_TAG_NAME && | |
1834 | attr->value_tag != IPP_TAG_NAMELANG && | |
1835 | attr->value_tag != IPP_TAG_KEYWORD && | |
d1c13e16 MS |
1836 | attr->value_tag != IPP_TAG_RANGE && |
1837 | attr->value_tag != IPP_TAG_URI) | |
426c6a59 | 1838 | continue; |
f42414bf | 1839 | |
426c6a59 | 1840 | if (!strcmp(attr->name, "auth-info-required") || |
d1c13e16 | 1841 | !strcmp(attr->name, "device-uri") || |
426c6a59 MS |
1842 | !strcmp(attr->name, "marker-change-time") || |
1843 | !strcmp(attr->name, "marker-colors") || | |
1844 | !strcmp(attr->name, "marker-high-levels") || | |
1845 | !strcmp(attr->name, "marker-levels") || | |
1846 | !strcmp(attr->name, "marker-low-levels") || | |
1847 | !strcmp(attr->name, "marker-message") || | |
1848 | !strcmp(attr->name, "marker-names") || | |
1849 | !strcmp(attr->name, "marker-types") || | |
1850 | !strcmp(attr->name, "printer-commands") || | |
1851 | !strcmp(attr->name, "printer-info") || | |
1852 | !strcmp(attr->name, "printer-is-shared") || | |
1853 | !strcmp(attr->name, "printer-make-and-model") || | |
1854 | !strcmp(attr->name, "printer-state") || | |
1855 | !strcmp(attr->name, "printer-state-change-time") || | |
1856 | !strcmp(attr->name, "printer-type") || | |
1857 | !strcmp(attr->name, "printer-is-accepting-jobs") || | |
1858 | !strcmp(attr->name, "printer-location") || | |
d1c13e16 MS |
1859 | !strcmp(attr->name, "printer-state-reasons") || |
1860 | !strcmp(attr->name, "printer-uri-supported")) | |
ef416fc2 | 1861 | { |
426c6a59 MS |
1862 | /* |
1863 | * Add a printer description attribute... | |
1864 | */ | |
1865 | ||
1866 | num_options = cupsAddOption(attr->name, | |
1867 | cups_make_string(attr, value, | |
1868 | sizeof(value)), | |
1869 | num_options, &options); | |
1870 | } | |
557dde9f | 1871 | #ifdef __APPLE__ |
426c6a59 MS |
1872 | else if (!strcmp(attr->name, "media-supported")) |
1873 | { | |
1874 | /* | |
1875 | * See if we can set a default media size... | |
1876 | */ | |
1877 | ||
d1c13e16 MS |
1878 | int i; /* Looping var */ |
1879 | ||
426c6a59 MS |
1880 | for (i = 0; i < attr->num_values; i ++) |
1881 | if (!strcasecmp(media_default, attr->values[i].string.text)) | |
1882 | { | |
1883 | num_options = cupsAddOption("media", media_default, num_options, | |
1884 | &options); | |
1885 | break; | |
1886 | } | |
1887 | } | |
557dde9f | 1888 | #endif /* __APPLE__ */ |
fa73b229 | 1889 | else if (!strcmp(attr->name, "printer-name") && |
1890 | attr->value_tag == IPP_TAG_NAME) | |
a4924f6c | 1891 | printer_name = attr->values[0].string.text; |
f7deaa1a | 1892 | else if (strncmp(attr->name, "notify-", 7) && |
1893 | (attr->value_tag == IPP_TAG_BOOLEAN || | |
1894 | attr->value_tag == IPP_TAG_ENUM || | |
1895 | attr->value_tag == IPP_TAG_INTEGER || | |
1896 | attr->value_tag == IPP_TAG_KEYWORD || | |
1897 | attr->value_tag == IPP_TAG_NAME || | |
1898 | attr->value_tag == IPP_TAG_RANGE) && | |
426c6a59 | 1899 | (ptr = strstr(attr->name, "-default")) != NULL) |
f7deaa1a | 1900 | { |
f7deaa1a | 1901 | /* |
1902 | * Add a default option... | |
1903 | */ | |
1904 | ||
1905 | strlcpy(optname, attr->name, sizeof(optname)); | |
426c6a59 MS |
1906 | optname[ptr - attr->name] = '\0'; |
1907 | ||
1908 | if (strcasecmp(optname, "media") || | |
1909 | !cupsGetOption("media", num_options, options)) | |
1910 | num_options = cupsAddOption(optname, | |
1911 | cups_make_string(attr, value, | |
1912 | sizeof(value)), | |
1913 | num_options, &options); | |
f7deaa1a | 1914 | } |
ef416fc2 | 1915 | } |
1916 | ||
1917 | /* | |
1918 | * See if we have everything needed... | |
1919 | */ | |
1920 | ||
a4924f6c | 1921 | if (!printer_name) |
ef416fc2 | 1922 | { |
f7deaa1a | 1923 | cupsFreeOptions(num_options, options); |
1924 | ||
ef416fc2 | 1925 | if (attr == NULL) |
1926 | break; | |
1927 | else | |
1928 | continue; | |
1929 | } | |
1930 | ||
426c6a59 | 1931 | if ((dest = cups_add_dest(printer_name, NULL, &num_dests, dests)) != NULL) |
fa73b229 | 1932 | { |
f7deaa1a | 1933 | dest->num_options = num_options; |
1934 | dest->options = options; | |
fa73b229 | 1935 | } |
426c6a59 MS |
1936 | else |
1937 | cupsFreeOptions(num_options, options); | |
f7deaa1a | 1938 | |
ef416fc2 | 1939 | if (attr == NULL) |
1940 | break; | |
1941 | } | |
1942 | ||
1943 | ippDelete(response); | |
1944 | } | |
1945 | ||
1946 | /* | |
1947 | * Return the count... | |
1948 | */ | |
1949 | ||
1950 | return (num_dests); | |
1951 | } | |
1952 | ||
1953 | ||
426c6a59 MS |
1954 | /* |
1955 | * 'cups_make_string()' - Make a comma-separated string of values from an IPP | |
1956 | * attribute. | |
1957 | */ | |
1958 | ||
1959 | static char * /* O - New string */ | |
1960 | cups_make_string( | |
1961 | ipp_attribute_t *attr, /* I - Attribute to convert */ | |
1962 | char *buffer, /* I - Buffer */ | |
1963 | size_t bufsize) /* I - Size of buffer */ | |
1964 | { | |
1965 | int i; /* Looping var */ | |
1966 | char *ptr, /* Pointer into buffer */ | |
1967 | *end, /* Pointer to end of buffer */ | |
1968 | *valptr; /* Pointer into string attribute */ | |
1969 | ||
1970 | ||
1971 | /* | |
1972 | * Return quickly if we have a single string value... | |
1973 | */ | |
1974 | ||
1975 | if (attr->num_values == 1 && | |
1976 | attr->value_tag != IPP_TAG_INTEGER && | |
1977 | attr->value_tag != IPP_TAG_ENUM && | |
1978 | attr->value_tag != IPP_TAG_BOOLEAN && | |
1979 | attr->value_tag != IPP_TAG_RANGE) | |
1980 | return (attr->values[0].string.text); | |
1981 | ||
1982 | /* | |
1983 | * Copy the values to the string, separating with commas and escaping strings | |
1984 | * as needed... | |
1985 | */ | |
1986 | ||
1987 | end = buffer + bufsize - 1; | |
1988 | ||
1989 | for (i = 0, ptr = buffer; i < attr->num_values && ptr < end; i ++) | |
1990 | { | |
1991 | if (i) | |
1992 | *ptr++ = ','; | |
1993 | ||
1994 | switch (attr->value_tag) | |
1995 | { | |
1996 | case IPP_TAG_INTEGER : | |
1997 | case IPP_TAG_ENUM : | |
1998 | snprintf(ptr, end - ptr + 1, "%d", attr->values[i].integer); | |
1999 | break; | |
2000 | ||
2001 | case IPP_TAG_BOOLEAN : | |
2002 | if (attr->values[i].boolean) | |
2003 | strlcpy(ptr, "true", end - ptr + 1); | |
2004 | else | |
2005 | strlcpy(ptr, "false", end - ptr + 1); | |
2006 | break; | |
2007 | ||
2008 | case IPP_TAG_RANGE : | |
2009 | if (attr->values[i].range.lower == attr->values[i].range.upper) | |
2010 | snprintf(ptr, end - ptr + 1, "%d", attr->values[i].range.lower); | |
2011 | else | |
2012 | snprintf(ptr, end - ptr + 1, "%d-%d", attr->values[i].range.lower, | |
2013 | attr->values[i].range.upper); | |
2014 | break; | |
2015 | ||
2016 | default : | |
2017 | for (valptr = attr->values[i].string.text; | |
2018 | *valptr && ptr < end;) | |
2019 | { | |
2020 | if (strchr(" \t\n\\\'\"", *valptr)) | |
2021 | { | |
2022 | if (ptr >= (end - 1)) | |
2023 | break; | |
2024 | ||
2025 | *ptr++ = '\\'; | |
2026 | } | |
2027 | ||
2028 | *ptr++ = *valptr++; | |
2029 | } | |
2030 | ||
2031 | *ptr = '\0'; | |
2032 | break; | |
2033 | } | |
2034 | ||
2035 | ptr += strlen(ptr); | |
2036 | } | |
2037 | ||
2038 | *ptr = '\0'; | |
2039 | ||
2040 | return (buffer); | |
2041 | } | |
2042 | ||
2043 | ||
ef416fc2 | 2044 | /* |
b19ccc9e | 2045 | * End of "$Id: dest.c 7946 2008-09-16 23:27:54Z mike $". |
ef416fc2 | 2046 | */ |