2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: PdmXp.c /main/4 1996/08/12 18:43:03 cde-hp $ */
28 * (c) Copyright 1996 Digital Equipment Corporation.
29 * (c) Copyright 1996 Hewlett-Packard Company.
30 * (c) Copyright 1996 International Business Machines Corp.
31 * (c) Copyright 1996 Sun Microsystems, Inc.
32 * (c) Copyright 1996 Novell, Inc.
33 * (c) Copyright 1996 FUJITSU LIMITED.
34 * (c) Copyright 1996 Hitachi.
40 #include <X11/Intrinsic.h>
43 PDMXP_JOB, PDMXP_DOC, PDMXP_PRINTER, PDMXP_SERVER,
44 PDMXP_BAD_POOL /* should always be last in list */
49 * static function declarations
51 static const char* PdmXpGetQualifier(PdmXp* me);
52 static char* PdmXpBuildResourceName(PdmXp* me, PdmOid id_att);
53 static XrmDatabase PdmXpLoadPool(PdmXp* me, XPAttributes type);
57 * ------------------------------------------------------------------------
62 * Creates a new PdmXp instance structure.
66 * The new PdmXp instance structure.
72 return (PdmXp*)XtCalloc(1, sizeof(PdmXp));
76 * ------------------------------------------------------------------------
81 * Closes an existing Xp connection, and frees the passed
82 * PdmXp instance structure.
90 PdmXpDelete(PdmXp* me)
97 * ------------------------------------------------------------------------
102 * This function opens the passed print display specifier and sets
103 * the passed print context on the newly opened print display.
107 * If successful, the print display pointer is returned. If unable to
108 * open the display, or if the display does not support the Xp
109 * extension, NULL is returned.
118 * only maintain one connection
122 * open the passed display spec
124 me->display = XOpenDisplay(display_spec);
130 * check to see if the display is a print server
132 if(XpQueryExtension(me->display, &event_base, &error_base))
135 * set the passed print context on the print display
137 me->context = strtoul(context_str, (char**)NULL, 0);
139 * load the resource DB qualifier
141 PdmXpGetQualifier(me);
145 XCloseDisplay(me->display);
146 me->display = (Display*)NULL;
154 * ------------------------------------------------------------------------
159 * Closes the print display.
167 PdmXpClose(PdmXp* me)
173 for(i = 0; i < PDMXP_POOL_COUNT; i++)
175 if(me->pool[i] != (XrmDatabase)NULL)
177 XrmDestroyDatabase(me->pool[i]);
178 me->pool[i] == (XrmDatabase)NULL;
181 XCloseDisplay(me->display);
183 me->context = (XPContext)NULL;
188 * ------------------------------------------------------------------------
189 * Name: PdmXpLoadPool
201 PdmXpLoadPool(PdmXp* me, XPAttributes type)
205 * determine the index into the pool array based on the Xp pool type
222 return (XrmDatabase)NULL;
226 * get the attributes from the X print server
228 if(me->pool[i] == (XrmDatabase)NULL)
230 XTextProperty text_prop;
234 text_prop.value = (unsigned char*)
235 XpGetAttributes(me->display, me->context, type);
236 text_prop.encoding = XInternAtom(me->display, "COMPOUND_TEXT", False);
237 text_prop.format = 8;
238 text_prop.nitems = strlen((char*)text_prop.value);
240 XmbTextPropertyToTextList(me->display, &text_prop, &list, &count))
243 me->pool[i] = XrmGetStringDatabase(list[0]);
245 XFreeStringList(list);
253 * ------------------------------------------------------------------------
254 * Name: PdmXpGetQualifier
265 PdmXpGetQualifier(PdmXp* me)
267 if(me->qualifier == (char*)NULL)
269 if(PdmXpLoadPool(me, XPPrinterAttr) != (XrmDatabase)NULL)
274 if(XrmGetResource(me->pool[PDMXP_PRINTER],
275 "qualifier", "qualifier", &str_type, &value))
277 me->qualifier = XtNewString((char*)value.addr);
278 me->qualifier_len = strlen(me->qualifier);
282 return me->qualifier;
288 * ------------------------------------------------------------------------
289 * Name: PdmXpBuildResourceName
297 * A new fully-qualified resource name. It is the caller's
298 * responsibility to free the returned string by calling XtFree.
302 PdmXpBuildResourceName(PdmXp* me, PdmOid id_att)
308 * allocate memory for the resource name
310 oid_str_len = PdmOidStringLength(id_att);
312 XtMalloc(me->qualifier_len + 1 + oid_str_len + 1);
314 * build the resource name from the printer name and the string value
315 * for the passed attribute id
317 strncpy(ptr, me->qualifier, me->qualifier_len);
318 ptr += me->qualifier_len;
321 strncpy(ptr, PdmOidString(id_att), oid_str_len);
332 * ------------------------------------------------------------------------
333 * Name: PdmXpGetValue
341 * pdmoid_none if the attribute value is not found.
345 PdmXpGetValue(PdmXp* me,
351 value = PdmXpGetStringValue(me, type, id_att);
353 return PdmOidFromString(value);
357 * ------------------------------------------------------------------------
358 * Name: PdmXpGetStringValue
366 * NULL if the attribute value is not found, or if the resource
367 * representation type is not a string.
371 PdmXpGetStringValue(PdmXp* me,
382 pool = PdmXpLoadPool(me, type);
383 if(pool == (XrmDatabase)NULL)
384 return (const char*)NULL;
386 res_name = PdmXpBuildResourceName(me, id_att);
387 found = XrmGetResource(pool, res_name, res_name, &str_type, &value);
394 return (const char*)value.addr;
396 return (const char*)NULL;
400 * ------------------------------------------------------------------------
401 * Name: PdmXpSetValue
413 PdmXpSetValue(PdmXp* me,
418 PdmXpSetStringValue(me, type, id_att, PdmOidString(id_val));
422 * ------------------------------------------------------------------------
423 * Name: PdmXpSetStringValue
435 PdmXpSetStringValue(PdmXp* me,
444 pool = PdmXpLoadPool(me, type);
445 if(pool == (XrmDatabase)NULL)
448 res_name = PdmXpBuildResourceName(me, id_att);
449 XrmPutStringResource(&pool, res_name, (char*)str_val);
454 * ------------------------------------------------------------------------
455 * Name: PdmXpUpdateAttributes
467 PdmXpUpdateAttributes(PdmXp* me)
469 char fname[L_tmpnam];
481 XTextProperty text_prop;
483 for(i = 0; i < PDMXP_POOL_COUNT; i++)
485 if(me->pool[i] != (XrmDatabase)NULL)
499 * write out the attribute pool to a file Xrm DB
501 XrmPutFileDatabase(me->pool[i], fname);
503 * open the new file Xrm DB
505 if(fp = fopen(fname, "r"))
508 * read the file to create a string Xrm DB
510 fstat(fileno(fp), &stbuf);
511 if(stbuf.st_size + 1 > data_size)
513 data_size = stbuf.st_size + 1;
514 data = XtRealloc(data, data_size);
516 retlen = read(fileno(fp), data, stbuf.st_size);
521 * convert to compund text
524 XmbTextListToTextProperty(me->display,
530 * use the string Xrm DB to update the Xp server
532 XpSetAttributes(me->display, me->context,
533 type, (char*)text_prop.value,
536 XFree(text_prop.value);