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