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